blob: ac9db1533bf3ef436bebda365b887088c1968e65 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Victor Stinner27e2d1f2018-11-01 00:52:28 +010013#include "pycore_state.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();
Victor Stinner50b48572018-11-01 01:51:40 +0100160 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{
Victor Stinner50b48572018-11-01 01:51:40 +0100178 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179 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.
Victor Stinner50b48572018-11-01 01:51:40 +0100188 We therefore avoid PyThreadState_Get() which dumps a fatal error
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 in debug mode.
190 */
Victor Stinner50b48572018-11-01 01:51:40 +0100191 drop_gil(_PyThreadState_GET());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000192}
193
194void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000195PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000196{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 if (tstate == NULL)
198 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
199 /* Check someone has called PyEval_InitThreads() to create the lock */
200 assert(gil_created());
201 take_gil(tstate);
202 if (PyThreadState_Swap(tstate) != NULL)
203 Py_FatalError(
204 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000205}
206
207void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000208PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 if (tstate == NULL)
211 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
212 if (PyThreadState_Swap(NULL) != tstate)
213 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
214 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000215}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000216
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200217/* This function is called from PyOS_AfterFork_Child to destroy all threads
218 * which are not running in the child process, and clear internal locks
219 * which might be held by those threads.
220 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000221
222void
223PyEval_ReInitThreads(void)
224{
Victor Stinner50b48572018-11-01 01:51:40 +0100225 PyThreadState *current_tstate = _PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 if (!gil_created())
228 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 recreate_gil();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600230 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200231 take_gil(current_tstate);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600232 _PyRuntime.ceval.pending.main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000233
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200234 /* Destroy all threads except the current one */
235 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000236}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000237
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000238/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600239 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000240
241void
242_PyEval_SignalAsyncExc(void)
243{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000245}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000246
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000247PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000248PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000249{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 PyThreadState *tstate = PyThreadState_Swap(NULL);
251 if (tstate == NULL)
252 Py_FatalError("PyEval_SaveThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100253 assert(gil_created());
254 drop_gil(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000256}
257
258void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000259PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 if (tstate == NULL)
262 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100263 assert(gil_created());
264
265 int err = errno;
266 take_gil(tstate);
267 /* _Py_Finalizing is protected by the GIL */
268 if (_Py_IsFinalizing() && !_Py_CURRENTLY_FINALIZING(tstate)) {
269 drop_gil(tstate);
270 PyThread_exit_thread();
271 Py_UNREACHABLE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 }
Victor Stinner2914bb32018-01-29 11:57:45 +0100273 errno = err;
274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000276}
277
278
Guido van Rossuma9672091994-09-14 13:31:22 +0000279/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
280 signal handlers or Mac I/O completion routines) can schedule calls
281 to a function to be called synchronously.
282 The synchronous function is called with one void* argument.
283 It should return 0 for success or -1 for failure -- failure should
284 be accompanied by an exception.
285
286 If registry succeeds, the registry function returns 0; if it fails
287 (e.g. due to too many pending calls) it returns -1 (without setting
288 an exception condition).
289
290 Note that because registry may occur from within signal handlers,
291 or other asynchronous events, calling malloc() is unsafe!
292
Guido van Rossuma9672091994-09-14 13:31:22 +0000293 Any thread can schedule pending calls, but only the main thread
294 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000295 There is no facility to schedule calls to a particular thread, but
296 that should be easy to change, should that ever be required. In
297 that case, the static variables here should go into the python
298 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000299*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000300
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200301void
302_PyEval_SignalReceived(void)
303{
304 /* bpo-30703: Function called when the C signal handler of Python gets a
305 signal. We cannot queue a callback using Py_AddPendingCall() since
306 that function is not async-signal-safe. */
307 SIGNAL_PENDING_CALLS();
308}
309
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200310/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000311 scheduling to be made from any thread, and even from an executing
312 callback.
313 */
314
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000315int
316Py_AddPendingCall(int (*func)(void *), void *arg)
317{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 int i, j, result=0;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600319 PyThread_type_lock lock = _PyRuntime.ceval.pending.lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 /* try a few times for the lock. Since this mechanism is used
322 * for signal handling (on the main thread), there is a (slim)
323 * chance that a signal is delivered on the same thread while we
324 * hold the lock during the Py_MakePendingCalls() function.
325 * This avoids a deadlock in that case.
326 * Note that signals can be delivered on any thread. In particular,
327 * on Windows, a SIGINT is delivered on a system-created worker
328 * thread.
329 * We also check for lock being NULL, in the unlikely case that
330 * this function is called before any bytecode evaluation takes place.
331 */
332 if (lock != NULL) {
333 for (i = 0; i<100; i++) {
334 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
335 break;
336 }
337 if (i == 100)
338 return -1;
339 }
340
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600341 i = _PyRuntime.ceval.pending.last;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 j = (i + 1) % NPENDINGCALLS;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600343 if (j == _PyRuntime.ceval.pending.first) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 result = -1; /* Queue full */
345 } else {
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600346 _PyRuntime.ceval.pending.calls[i].func = func;
347 _PyRuntime.ceval.pending.calls[i].arg = arg;
348 _PyRuntime.ceval.pending.last = j;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 }
350 /* signal main loop */
351 SIGNAL_PENDING_CALLS();
352 if (lock != NULL)
353 PyThread_release_lock(lock);
354 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000355}
356
357int
358Py_MakePendingCalls(void)
359{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200360 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 int i;
362 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000363
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200364 assert(PyGILState_Check());
365
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600366 if (!_PyRuntime.ceval.pending.lock) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 /* initial allocation of the lock */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600368 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
369 if (_PyRuntime.ceval.pending.lock == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 return -1;
371 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 /* only service pending calls on main thread */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600374 if (_PyRuntime.ceval.pending.main_thread &&
375 PyThread_get_thread_ident() != _PyRuntime.ceval.pending.main_thread)
376 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 return 0;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600378 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200380 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200382 busy = 1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200383 /* unsignal before starting to call callbacks, so that any callback
384 added in-between re-signals */
385 UNSIGNAL_PENDING_CALLS();
386
387 /* Python signal handler doesn't really queue a callback: it only signals
388 that a signal was received, see _PyEval_SignalReceived(). */
389 if (PyErr_CheckSignals() < 0) {
390 goto error;
391 }
392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 /* perform a bounded number of calls, in case of recursion */
394 for (i=0; i<NPENDINGCALLS; i++) {
395 int j;
396 int (*func)(void *);
397 void *arg = NULL;
398
399 /* pop one item off the queue while holding the lock */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600400 PyThread_acquire_lock(_PyRuntime.ceval.pending.lock, WAIT_LOCK);
401 j = _PyRuntime.ceval.pending.first;
402 if (j == _PyRuntime.ceval.pending.last) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 func = NULL; /* Queue empty */
404 } else {
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600405 func = _PyRuntime.ceval.pending.calls[j].func;
406 arg = _PyRuntime.ceval.pending.calls[j].arg;
407 _PyRuntime.ceval.pending.first = (j + 1) % NPENDINGCALLS;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600409 PyThread_release_lock(_PyRuntime.ceval.pending.lock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 /* having released the lock, perform the callback */
411 if (func == NULL)
412 break;
413 r = func(arg);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200414 if (r) {
415 goto error;
416 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200418
Charles-François Natalif23339a2011-07-23 18:15:43 +0200419 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 return r;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200421
422error:
423 busy = 0;
424 SIGNAL_PENDING_CALLS(); /* We're not done yet */
425 return -1;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000426}
427
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000428/* The interpreter's recursion limit */
429
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000430#ifndef Py_DEFAULT_RECURSION_LIMIT
431#define Py_DEFAULT_RECURSION_LIMIT 1000
432#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600433
Eric Snow05351c12017-09-05 21:43:08 -0700434int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000435
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600436void
437_PyEval_Initialize(struct _ceval_runtime_state *state)
438{
439 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
440 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
441 _gil_initialize(&state->gil);
442}
443
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000444int
445Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000446{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600447 return _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000448}
449
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000450void
451Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000452{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600453 _PyRuntime.ceval.recursion_limit = new_limit;
454 _Py_CheckRecursionLimit = _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000455}
456
Armin Rigo2b3eb402003-10-28 12:05:48 +0000457/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
458 if the recursion_depth reaches _Py_CheckRecursionLimit.
459 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
460 to guarantee that _Py_CheckRecursiveCall() is regularly called.
461 Without USE_STACKCHECK, there is no need for this. */
462int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300463_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000464{
Victor Stinner50b48572018-11-01 01:51:40 +0100465 PyThreadState *tstate = _PyThreadState_GET();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600466 int recursion_limit = _PyRuntime.ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000467
468#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700469 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 if (PyOS_CheckStack()) {
471 --tstate->recursion_depth;
472 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
473 return -1;
474 }
pdox18967932017-10-25 23:03:01 -0700475 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700476 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700477#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 if (tstate->recursion_critical)
479 /* Somebody asked that we don't check for recursion. */
480 return 0;
481 if (tstate->overflowed) {
482 if (tstate->recursion_depth > recursion_limit + 50) {
483 /* Overflowing while handling an overflow. Give up. */
484 Py_FatalError("Cannot recover from stack overflow.");
485 }
486 return 0;
487 }
488 if (tstate->recursion_depth > recursion_limit) {
489 --tstate->recursion_depth;
490 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400491 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 "maximum recursion depth exceeded%s",
493 where);
494 return -1;
495 }
496 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000497}
498
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400499static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000500static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000501
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600502#define _Py_TracingPossible _PyRuntime.ceval.tracing_possible
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000503
Guido van Rossum374a9221991-04-04 10:40:29 +0000504
Guido van Rossumb209a111997-04-29 18:18:01 +0000505PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000506PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000507{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 return PyEval_EvalCodeEx(co,
509 globals, locals,
510 (PyObject **)NULL, 0,
511 (PyObject **)NULL, 0,
512 (PyObject **)NULL, 0,
513 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000514}
515
516
517/* Interpreter main loop */
518
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000519PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000520PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 /* This is for backward compatibility with extension modules that
522 used this API; core interpreter code should call
523 PyEval_EvalFrameEx() */
524 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000525}
526
527PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000528PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000529{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200530 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
531 return interp->eval_frame(f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700532}
533
Victor Stinnerc6944e72016-11-11 02:13:35 +0100534PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700535_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
536{
Guido van Rossum950361c1997-01-24 13:49:28 +0000537#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000539#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200540 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300541 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200542 int opcode; /* Current opcode */
543 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200544 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 PyObject *retval = NULL; /* Return value */
Victor Stinner50b48572018-11-01 01:51:40 +0100546 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 is true when the line being executed has changed. The
554 initial values are such as to make this false the first
555 time it is tested. */
556 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000557
Serhiy Storchakaab874002016-09-11 13:48:15 +0300558 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 PyObject *names;
560 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000561
Brett Cannon368b4b72012-04-02 12:17:59 -0400562#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200563 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400564#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200565
Antoine Pitroub52ec782009-01-25 16:34:23 +0000566/* Computed GOTOs, or
567 the-optimization-commonly-but-improperly-known-as-"threaded code"
568 using gcc's labels-as-values extension
569 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
570
571 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000573 combined with a lookup table of jump addresses. However, since the
574 indirect jump instruction is shared by all opcodes, the CPU will have a
575 hard time making the right prediction for where to jump next (actually,
576 it will be always wrong except in the uncommon case of a sequence of
577 several identical opcodes).
578
579 "Threaded code" in contrast, uses an explicit jump table and an explicit
580 indirect jump instruction at the end of each opcode. Since the jump
581 instruction is at a different address for each opcode, the CPU will make a
582 separate prediction for each of these instructions, which is equivalent to
583 predicting the second opcode of each opcode pair. These predictions have
584 a much better chance to turn out valid, especially in small bytecode loops.
585
586 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000588 and potentially many more instructions (depending on the pipeline width).
589 A correctly predicted branch, however, is nearly free.
590
591 At the time of this writing, the "threaded code" version is up to 15-20%
592 faster than the normal "switch" version, depending on the compiler and the
593 CPU architecture.
594
595 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
596 because it would render the measurements invalid.
597
598
599 NOTE: care must be taken that the compiler doesn't try to "optimize" the
600 indirect jumps by sharing them between all opcodes. Such optimizations
601 can be disabled on gcc by using the -fno-gcse flag (or possibly
602 -fno-crossjumping).
603*/
604
Antoine Pitrou042b1282010-08-13 21:15:58 +0000605#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000606#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000607#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000608#endif
609
Antoine Pitrou042b1282010-08-13 21:15:58 +0000610#ifdef HAVE_COMPUTED_GOTOS
611 #ifndef USE_COMPUTED_GOTOS
612 #define USE_COMPUTED_GOTOS 1
613 #endif
614#else
615 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
616 #error "Computed gotos are not supported on this compiler."
617 #endif
618 #undef USE_COMPUTED_GOTOS
619 #define USE_COMPUTED_GOTOS 0
620#endif
621
622#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000623/* Import the static jump table */
624#include "opcode_targets.h"
625
Antoine Pitroub52ec782009-01-25 16:34:23 +0000626#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700627 op: \
628 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000629
Antoine Pitroub52ec782009-01-25 16:34:23 +0000630#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600632 if (!_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 FAST_DISPATCH(); \
634 } \
635 continue; \
636 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000637
638#ifdef LLTRACE
639#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700641 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300643 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300644 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 } \
646 goto fast_next_opcode; \
647 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000648#else
649#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700651 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300653 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300654 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 } \
656 goto fast_next_opcode; \
657 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000658#endif
659
660#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700661#define TARGET(op) op
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300662
Antoine Pitroub52ec782009-01-25 16:34:23 +0000663#define DISPATCH() continue
664#define FAST_DISPATCH() goto fast_next_opcode
665#endif
666
667
Neal Norwitza81d2202002-07-14 00:27:26 +0000668/* Tuple access macros */
669
670#ifndef Py_DEBUG
671#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
672#else
673#define GETITEM(v, i) PyTuple_GetItem((v), (i))
674#endif
675
Guido van Rossum374a9221991-04-04 10:40:29 +0000676/* Code access macros */
677
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300678/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600679#define INSTR_OFFSET() \
680 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300681#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300682 _Py_CODEUNIT word = *next_instr; \
683 opcode = _Py_OPCODE(word); \
684 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300685 next_instr++; \
686 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300687#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
688#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000689
Raymond Hettingerf606f872003-03-16 03:11:04 +0000690/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 Some opcodes tend to come in pairs thus making it possible to
692 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300693 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 Verifying the prediction costs a single high-speed test of a register
696 variable against a constant. If the pairing was good, then the
697 processor's own internal branch predication has a high likelihood of
698 success, resulting in a nearly zero-overhead transition to the
699 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300700 including its unpredictable switch-case branch. Combined with the
701 processor's internal branch prediction, a successful PREDICT has the
702 effect of making the two opcodes run as if they were a single new opcode
703 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000704
Georg Brandl86b2fb92008-07-16 03:43:04 +0000705 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 predictions turned-on and interpret the results as if some opcodes
707 had been combined or turn-off predictions so that the opcode frequency
708 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000709
710 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 the CPU to record separate branch prediction information for each
712 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000713
Raymond Hettingerf606f872003-03-16 03:11:04 +0000714*/
715
Antoine Pitrou042b1282010-08-13 21:15:58 +0000716#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000718#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300719#define PREDICT(op) \
720 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300721 _Py_CODEUNIT word = *next_instr; \
722 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300723 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300724 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300725 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300726 goto PRED_##op; \
727 } \
728 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000729#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300730#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000731
Raymond Hettingerf606f872003-03-16 03:11:04 +0000732
Guido van Rossum374a9221991-04-04 10:40:29 +0000733/* Stack manipulation macros */
734
Martin v. Löwis18e16552006-02-15 17:27:45 +0000735/* The stack can grow at most MAXINT deep, as co_nlocals and
736 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000737#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
738#define EMPTY() (STACK_LEVEL() == 0)
739#define TOP() (stack_pointer[-1])
740#define SECOND() (stack_pointer[-2])
741#define THIRD() (stack_pointer[-3])
742#define FOURTH() (stack_pointer[-4])
743#define PEEK(n) (stack_pointer[-(n)])
744#define SET_TOP(v) (stack_pointer[-1] = (v))
745#define SET_SECOND(v) (stack_pointer[-2] = (v))
746#define SET_THIRD(v) (stack_pointer[-3] = (v))
747#define SET_FOURTH(v) (stack_pointer[-4] = (v))
748#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
749#define BASIC_STACKADJ(n) (stack_pointer += n)
750#define BASIC_PUSH(v) (*stack_pointer++ = (v))
751#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000752
Guido van Rossum96a42c81992-01-12 02:29:51 +0000753#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000755 lltrace && prtrace(TOP(), "push")); \
756 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000758 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +0000759#define STACK_GROW(n) do { \
760 assert(n >= 0); \
761 (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000762 lltrace && prtrace(TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000763 assert(STACK_LEVEL() <= co->co_stacksize); \
764 } while (0)
765#define STACK_SHRINK(n) do { \
766 assert(n >= 0); \
767 (void)(lltrace && prtrace(TOP(), "stackadj")); \
768 (void)(BASIC_STACKADJ(-n)); \
769 assert(STACK_LEVEL() <= co->co_stacksize); \
770 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +0000771#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000772 prtrace((STACK_POINTER)[-1], "ext_pop")), \
773 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000774#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000775#define PUSH(v) BASIC_PUSH(v)
776#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +0000777#define STACK_GROW(n) BASIC_STACKADJ(n)
778#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000779#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000780#endif
781
Guido van Rossum681d79a1995-07-18 14:51:37 +0000782/* Local variable macros */
783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000785
786/* The SETLOCAL() macro must not DECREF the local variable in-place and
787 then store the new value; it must copy the old value to a temporary
788 value, then store the new value, and then DECREF the temporary value.
789 This is because it is possible that during the DECREF the frame is
790 accessed by other code (e.g. a __del__ method or gc.collect()) and the
791 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000793 GETLOCAL(i) = value; \
794 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000795
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000796
797#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 while (STACK_LEVEL() > (b)->b_level) { \
799 PyObject *v = POP(); \
800 Py_XDECREF(v); \
801 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000802
803#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300804 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +0100806 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 assert(STACK_LEVEL() >= (b)->b_level + 3); \
808 while (STACK_LEVEL() > (b)->b_level + 3) { \
809 value = POP(); \
810 Py_XDECREF(value); \
811 } \
Mark Shannonae3087c2017-10-22 22:41:51 +0100812 exc_info = tstate->exc_info; \
813 type = exc_info->exc_type; \
814 value = exc_info->exc_value; \
815 traceback = exc_info->exc_traceback; \
816 exc_info->exc_type = POP(); \
817 exc_info->exc_value = POP(); \
818 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 Py_XDECREF(type); \
820 Py_XDECREF(value); \
821 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300822 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000823
Guido van Rossuma027efa1997-05-05 20:56:21 +0000824/* Start of code */
825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 /* push frame */
827 if (Py_EnterRecursiveCall(""))
828 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 if (tstate->use_tracing) {
833 if (tstate->c_tracefunc != NULL) {
834 /* tstate->c_tracefunc, if defined, is a
835 function that will be called on *every* entry
836 to a code block. Its return value, if not
837 None, is a function that will be called at
838 the start of each executed line of code.
839 (Actually, the function must return itself
840 in order to continue tracing.) The trace
841 functions are called with three arguments:
842 a pointer to the current frame, a string
843 indicating why the function is called, and
844 an argument which depends on the situation.
845 The global trace function is also called
846 whenever an exception is detected. */
847 if (call_trace_protected(tstate->c_tracefunc,
848 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100849 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 /* Trace function raised an error */
851 goto exit_eval_frame;
852 }
853 }
854 if (tstate->c_profilefunc != NULL) {
855 /* Similar for c_profilefunc, except it needn't
856 return itself and isn't called for "line" events */
857 if (call_trace_protected(tstate->c_profilefunc,
858 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100859 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 /* Profile function raised an error */
861 goto exit_eval_frame;
862 }
863 }
864 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000865
Łukasz Langaa785c872016-09-09 17:37:37 -0700866 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
867 dtrace_function_entry(f);
868
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 co = f->f_code;
870 names = co->co_names;
871 consts = co->co_consts;
872 fastlocals = f->f_localsplus;
873 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300874 assert(PyBytes_Check(co->co_code));
875 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +0300876 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
877 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
878 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300879 /*
880 f->f_lasti refers to the index of the last instruction,
881 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000882
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300883 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500884 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 When the PREDICT() macros are enabled, some opcode pairs follow in
887 direct succession without updating f->f_lasti. A successful
888 prediction effectively links the two codes together as if they
889 were a single new opcode; accordingly,f->f_lasti will point to
890 the first code in the pair (for instance, GET_ITER followed by
891 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300892 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300894 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300895 next_instr = first_instr;
896 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +0300897 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
898 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300899 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 stack_pointer = f->f_stacktop;
901 assert(stack_pointer != NULL);
902 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +0200903 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000904
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000905
Tim Peters5ca576e2001-06-18 22:08:13 +0000906#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200907 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000908#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000909
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400910 if (throwflag) /* support for generator.throw() */
911 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000912
Victor Stinnerace47d72013-07-18 01:41:08 +0200913#ifdef Py_DEBUG
914 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100915 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +0000916 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +0200917 assert(!PyErr_Occurred());
918#endif
919
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200920main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 assert(stack_pointer >= f->f_valuestack); /* else underflow */
923 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +0200924 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 /* Do periodic things. Doing this every time through
927 the loop would add too much overhead, so we do it
928 only every Nth instruction. We also do it if
929 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
930 event needs attention (e.g. a signal handler or
931 async I/O handler); see Py_AddPendingCall() and
932 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000933
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600934 if (_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +0300935 opcode = _Py_OPCODE(*next_instr);
936 if (opcode == SETUP_FINALLY ||
937 opcode == SETUP_WITH ||
938 opcode == BEFORE_ASYNC_WITH ||
939 opcode == YIELD_FROM) {
940 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -0700941 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +0300942 - If we're about to enter the 'with:'. It will prevent
943 emitting a resource warning in the common idiom
944 'with open(path) as file:'.
945 - If we're about to enter the 'async with:'.
946 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -0700947 *very* useful, but might help in some cases and it's
948 traditional)
949 - If we're resuming a chain of nested 'yield from' or
950 'await' calls, then each frame is parked with YIELD_FROM
951 as its next opcode. If the user hit control-C we want to
952 wait until we've reached the innermost frame before
953 running the signal handler and raising KeyboardInterrupt
954 (see bpo-30039).
955 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 goto fast_next_opcode;
957 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600958 if (_Py_atomic_load_relaxed(
959 &_PyRuntime.ceval.pending.calls_to_do))
960 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400961 if (Py_MakePendingCalls() < 0)
962 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600964 if (_Py_atomic_load_relaxed(
965 &_PyRuntime.ceval.gil_drop_request))
966 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 /* Give another thread a chance */
968 if (PyThreadState_Swap(NULL) != tstate)
969 Py_FatalError("ceval: tstate mix-up");
970 drop_gil(tstate);
971
972 /* Other threads may run now */
973
974 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -0700975
976 /* Check if we should make a quick exit. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600977 if (_Py_IsFinalizing() &&
978 !_Py_CURRENTLY_FINALIZING(tstate))
979 {
Benjamin Peterson17548dd2014-06-16 22:59:07 -0700980 drop_gil(tstate);
981 PyThread_exit_thread();
982 }
983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 if (PyThreadState_Swap(tstate) != NULL)
985 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 }
987 /* Check for asynchronous exceptions. */
988 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400989 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 tstate->async_exc = NULL;
991 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400992 PyErr_SetNone(exc);
993 Py_DECREF(exc);
994 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 }
996 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 fast_next_opcode:
999 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001000
Łukasz Langaa785c872016-09-09 17:37:37 -07001001 if (PyDTrace_LINE_ENABLED())
1002 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001007 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001008 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 /* see maybe_call_line_trace
1010 for expository comments */
1011 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 err = maybe_call_line_trace(tstate->c_tracefunc,
1014 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001015 tstate, f,
1016 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 /* Reload possibly changed frame fields */
1018 JUMPTO(f->f_lasti);
1019 if (f->f_stacktop != NULL) {
1020 stack_pointer = f->f_stacktop;
1021 f->f_stacktop = NULL;
1022 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001023 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001025 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001029
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001030 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001031 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001032#ifdef DYNAMIC_EXECUTION_PROFILE
1033#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 dxpairs[lastopcode][opcode]++;
1035 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001036#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001038#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001039
Guido van Rossum96a42c81992-01-12 02:29:51 +00001040#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 if (lltrace) {
1044 if (HAS_ARG(opcode)) {
1045 printf("%d: %d, %d\n",
1046 f->f_lasti, opcode, oparg);
1047 }
1048 else {
1049 printf("%d: %d\n",
1050 f->f_lasti, opcode);
1051 }
1052 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001053#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001058 It is essential that any operation that fails must goto error
1059 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001060
Benjamin Petersonddd19492018-09-16 22:38:02 -07001061 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001063 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001064
Benjamin Petersonddd19492018-09-16 22:38:02 -07001065 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001066 PyObject *value = GETLOCAL(oparg);
1067 if (value == NULL) {
1068 format_exc_check_arg(PyExc_UnboundLocalError,
1069 UNBOUNDLOCAL_ERROR_MSG,
1070 PyTuple_GetItem(co->co_varnames, oparg));
1071 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001073 Py_INCREF(value);
1074 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001076 }
1077
Benjamin Petersonddd19492018-09-16 22:38:02 -07001078 case TARGET(LOAD_CONST): {
1079 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001080 PyObject *value = GETITEM(consts, oparg);
1081 Py_INCREF(value);
1082 PUSH(value);
1083 FAST_DISPATCH();
1084 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001085
Benjamin Petersonddd19492018-09-16 22:38:02 -07001086 case TARGET(STORE_FAST): {
1087 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001088 PyObject *value = POP();
1089 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001091 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001092
Benjamin Petersonddd19492018-09-16 22:38:02 -07001093 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001094 PyObject *value = POP();
1095 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001097 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001098
Benjamin Petersonddd19492018-09-16 22:38:02 -07001099 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001100 PyObject *top = TOP();
1101 PyObject *second = SECOND();
1102 SET_TOP(second);
1103 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001105 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001106
Benjamin Petersonddd19492018-09-16 22:38:02 -07001107 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001108 PyObject *top = TOP();
1109 PyObject *second = SECOND();
1110 PyObject *third = THIRD();
1111 SET_TOP(second);
1112 SET_SECOND(third);
1113 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001115 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001116
Benjamin Petersonddd19492018-09-16 22:38:02 -07001117 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001118 PyObject *top = TOP();
1119 PyObject *second = SECOND();
1120 PyObject *third = THIRD();
1121 PyObject *fourth = FOURTH();
1122 SET_TOP(second);
1123 SET_SECOND(third);
1124 SET_THIRD(fourth);
1125 SET_FOURTH(top);
1126 FAST_DISPATCH();
1127 }
1128
Benjamin Petersonddd19492018-09-16 22:38:02 -07001129 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001130 PyObject *top = TOP();
1131 Py_INCREF(top);
1132 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001134 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001135
Benjamin Petersonddd19492018-09-16 22:38:02 -07001136 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001137 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001138 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001139 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001140 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001141 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001142 SET_TOP(top);
1143 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001144 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001145 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001146
Benjamin Petersonddd19492018-09-16 22:38:02 -07001147 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001148 PyObject *value = TOP();
1149 PyObject *res = PyNumber_Positive(value);
1150 Py_DECREF(value);
1151 SET_TOP(res);
1152 if (res == NULL)
1153 goto error;
1154 DISPATCH();
1155 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001156
Benjamin Petersonddd19492018-09-16 22:38:02 -07001157 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001158 PyObject *value = TOP();
1159 PyObject *res = PyNumber_Negative(value);
1160 Py_DECREF(value);
1161 SET_TOP(res);
1162 if (res == NULL)
1163 goto error;
1164 DISPATCH();
1165 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001166
Benjamin Petersonddd19492018-09-16 22:38:02 -07001167 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001168 PyObject *value = TOP();
1169 int err = PyObject_IsTrue(value);
1170 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 if (err == 0) {
1172 Py_INCREF(Py_True);
1173 SET_TOP(Py_True);
1174 DISPATCH();
1175 }
1176 else if (err > 0) {
1177 Py_INCREF(Py_False);
1178 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 DISPATCH();
1180 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001181 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001182 goto error;
1183 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001184
Benjamin Petersonddd19492018-09-16 22:38:02 -07001185 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001186 PyObject *value = TOP();
1187 PyObject *res = PyNumber_Invert(value);
1188 Py_DECREF(value);
1189 SET_TOP(res);
1190 if (res == NULL)
1191 goto error;
1192 DISPATCH();
1193 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001194
Benjamin Petersonddd19492018-09-16 22:38:02 -07001195 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001196 PyObject *exp = POP();
1197 PyObject *base = TOP();
1198 PyObject *res = PyNumber_Power(base, exp, Py_None);
1199 Py_DECREF(base);
1200 Py_DECREF(exp);
1201 SET_TOP(res);
1202 if (res == NULL)
1203 goto error;
1204 DISPATCH();
1205 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001206
Benjamin Petersonddd19492018-09-16 22:38:02 -07001207 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001208 PyObject *right = POP();
1209 PyObject *left = TOP();
1210 PyObject *res = PyNumber_Multiply(left, right);
1211 Py_DECREF(left);
1212 Py_DECREF(right);
1213 SET_TOP(res);
1214 if (res == NULL)
1215 goto error;
1216 DISPATCH();
1217 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001218
Benjamin Petersonddd19492018-09-16 22:38:02 -07001219 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001220 PyObject *right = POP();
1221 PyObject *left = TOP();
1222 PyObject *res = PyNumber_MatrixMultiply(left, right);
1223 Py_DECREF(left);
1224 Py_DECREF(right);
1225 SET_TOP(res);
1226 if (res == NULL)
1227 goto error;
1228 DISPATCH();
1229 }
1230
Benjamin Petersonddd19492018-09-16 22:38:02 -07001231 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001232 PyObject *divisor = POP();
1233 PyObject *dividend = TOP();
1234 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1235 Py_DECREF(dividend);
1236 Py_DECREF(divisor);
1237 SET_TOP(quotient);
1238 if (quotient == NULL)
1239 goto error;
1240 DISPATCH();
1241 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001242
Benjamin Petersonddd19492018-09-16 22:38:02 -07001243 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001244 PyObject *divisor = POP();
1245 PyObject *dividend = TOP();
1246 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1247 Py_DECREF(dividend);
1248 Py_DECREF(divisor);
1249 SET_TOP(quotient);
1250 if (quotient == NULL)
1251 goto error;
1252 DISPATCH();
1253 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001254
Benjamin Petersonddd19492018-09-16 22:38:02 -07001255 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001256 PyObject *divisor = POP();
1257 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001258 PyObject *res;
1259 if (PyUnicode_CheckExact(dividend) && (
1260 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1261 // fast path; string formatting, but not if the RHS is a str subclass
1262 // (see issue28598)
1263 res = PyUnicode_Format(dividend, divisor);
1264 } else {
1265 res = PyNumber_Remainder(dividend, divisor);
1266 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001267 Py_DECREF(divisor);
1268 Py_DECREF(dividend);
1269 SET_TOP(res);
1270 if (res == NULL)
1271 goto error;
1272 DISPATCH();
1273 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001274
Benjamin Petersonddd19492018-09-16 22:38:02 -07001275 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001276 PyObject *right = POP();
1277 PyObject *left = TOP();
1278 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001279 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1280 CPython using bytecode, it is simply worthless.
1281 See http://bugs.python.org/issue21955 and
1282 http://bugs.python.org/issue10044 for the discussion. In short,
1283 no patch shown any impact on a realistic benchmark, only a minor
1284 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001285 if (PyUnicode_CheckExact(left) &&
1286 PyUnicode_CheckExact(right)) {
1287 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001288 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001289 }
1290 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001291 sum = PyNumber_Add(left, right);
1292 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001293 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001294 Py_DECREF(right);
1295 SET_TOP(sum);
1296 if (sum == NULL)
1297 goto error;
1298 DISPATCH();
1299 }
1300
Benjamin Petersonddd19492018-09-16 22:38:02 -07001301 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001302 PyObject *right = POP();
1303 PyObject *left = TOP();
1304 PyObject *diff = PyNumber_Subtract(left, right);
1305 Py_DECREF(right);
1306 Py_DECREF(left);
1307 SET_TOP(diff);
1308 if (diff == NULL)
1309 goto error;
1310 DISPATCH();
1311 }
1312
Benjamin Petersonddd19492018-09-16 22:38:02 -07001313 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001314 PyObject *sub = POP();
1315 PyObject *container = TOP();
1316 PyObject *res = PyObject_GetItem(container, sub);
1317 Py_DECREF(container);
1318 Py_DECREF(sub);
1319 SET_TOP(res);
1320 if (res == NULL)
1321 goto error;
1322 DISPATCH();
1323 }
1324
Benjamin Petersonddd19492018-09-16 22:38:02 -07001325 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001326 PyObject *right = POP();
1327 PyObject *left = TOP();
1328 PyObject *res = PyNumber_Lshift(left, right);
1329 Py_DECREF(left);
1330 Py_DECREF(right);
1331 SET_TOP(res);
1332 if (res == NULL)
1333 goto error;
1334 DISPATCH();
1335 }
1336
Benjamin Petersonddd19492018-09-16 22:38:02 -07001337 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001338 PyObject *right = POP();
1339 PyObject *left = TOP();
1340 PyObject *res = PyNumber_Rshift(left, right);
1341 Py_DECREF(left);
1342 Py_DECREF(right);
1343 SET_TOP(res);
1344 if (res == NULL)
1345 goto error;
1346 DISPATCH();
1347 }
1348
Benjamin Petersonddd19492018-09-16 22:38:02 -07001349 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001350 PyObject *right = POP();
1351 PyObject *left = TOP();
1352 PyObject *res = PyNumber_And(left, right);
1353 Py_DECREF(left);
1354 Py_DECREF(right);
1355 SET_TOP(res);
1356 if (res == NULL)
1357 goto error;
1358 DISPATCH();
1359 }
1360
Benjamin Petersonddd19492018-09-16 22:38:02 -07001361 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001362 PyObject *right = POP();
1363 PyObject *left = TOP();
1364 PyObject *res = PyNumber_Xor(left, right);
1365 Py_DECREF(left);
1366 Py_DECREF(right);
1367 SET_TOP(res);
1368 if (res == NULL)
1369 goto error;
1370 DISPATCH();
1371 }
1372
Benjamin Petersonddd19492018-09-16 22:38:02 -07001373 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001374 PyObject *right = POP();
1375 PyObject *left = TOP();
1376 PyObject *res = PyNumber_Or(left, right);
1377 Py_DECREF(left);
1378 Py_DECREF(right);
1379 SET_TOP(res);
1380 if (res == NULL)
1381 goto error;
1382 DISPATCH();
1383 }
1384
Benjamin Petersonddd19492018-09-16 22:38:02 -07001385 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001386 PyObject *v = POP();
1387 PyObject *list = PEEK(oparg);
1388 int err;
1389 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001391 if (err != 0)
1392 goto error;
1393 PREDICT(JUMP_ABSOLUTE);
1394 DISPATCH();
1395 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001396
Benjamin Petersonddd19492018-09-16 22:38:02 -07001397 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001398 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001399 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001400 int err;
1401 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001403 if (err != 0)
1404 goto error;
1405 PREDICT(JUMP_ABSOLUTE);
1406 DISPATCH();
1407 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001408
Benjamin Petersonddd19492018-09-16 22:38:02 -07001409 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001410 PyObject *exp = POP();
1411 PyObject *base = TOP();
1412 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1413 Py_DECREF(base);
1414 Py_DECREF(exp);
1415 SET_TOP(res);
1416 if (res == NULL)
1417 goto error;
1418 DISPATCH();
1419 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001420
Benjamin Petersonddd19492018-09-16 22:38:02 -07001421 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001422 PyObject *right = POP();
1423 PyObject *left = TOP();
1424 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1425 Py_DECREF(left);
1426 Py_DECREF(right);
1427 SET_TOP(res);
1428 if (res == NULL)
1429 goto error;
1430 DISPATCH();
1431 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001432
Benjamin Petersonddd19492018-09-16 22:38:02 -07001433 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001434 PyObject *right = POP();
1435 PyObject *left = TOP();
1436 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1437 Py_DECREF(left);
1438 Py_DECREF(right);
1439 SET_TOP(res);
1440 if (res == NULL)
1441 goto error;
1442 DISPATCH();
1443 }
1444
Benjamin Petersonddd19492018-09-16 22:38:02 -07001445 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001446 PyObject *divisor = POP();
1447 PyObject *dividend = TOP();
1448 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1449 Py_DECREF(dividend);
1450 Py_DECREF(divisor);
1451 SET_TOP(quotient);
1452 if (quotient == NULL)
1453 goto error;
1454 DISPATCH();
1455 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001456
Benjamin Petersonddd19492018-09-16 22:38:02 -07001457 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001458 PyObject *divisor = POP();
1459 PyObject *dividend = TOP();
1460 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1461 Py_DECREF(dividend);
1462 Py_DECREF(divisor);
1463 SET_TOP(quotient);
1464 if (quotient == NULL)
1465 goto error;
1466 DISPATCH();
1467 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001468
Benjamin Petersonddd19492018-09-16 22:38:02 -07001469 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001470 PyObject *right = POP();
1471 PyObject *left = TOP();
1472 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1473 Py_DECREF(left);
1474 Py_DECREF(right);
1475 SET_TOP(mod);
1476 if (mod == NULL)
1477 goto error;
1478 DISPATCH();
1479 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001480
Benjamin Petersonddd19492018-09-16 22:38:02 -07001481 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001482 PyObject *right = POP();
1483 PyObject *left = TOP();
1484 PyObject *sum;
1485 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1486 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001487 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001488 }
1489 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001490 sum = PyNumber_InPlaceAdd(left, right);
1491 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001492 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001493 Py_DECREF(right);
1494 SET_TOP(sum);
1495 if (sum == NULL)
1496 goto error;
1497 DISPATCH();
1498 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001499
Benjamin Petersonddd19492018-09-16 22:38:02 -07001500 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001501 PyObject *right = POP();
1502 PyObject *left = TOP();
1503 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1504 Py_DECREF(left);
1505 Py_DECREF(right);
1506 SET_TOP(diff);
1507 if (diff == NULL)
1508 goto error;
1509 DISPATCH();
1510 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001511
Benjamin Petersonddd19492018-09-16 22:38:02 -07001512 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001513 PyObject *right = POP();
1514 PyObject *left = TOP();
1515 PyObject *res = PyNumber_InPlaceLshift(left, right);
1516 Py_DECREF(left);
1517 Py_DECREF(right);
1518 SET_TOP(res);
1519 if (res == NULL)
1520 goto error;
1521 DISPATCH();
1522 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001523
Benjamin Petersonddd19492018-09-16 22:38:02 -07001524 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001525 PyObject *right = POP();
1526 PyObject *left = TOP();
1527 PyObject *res = PyNumber_InPlaceRshift(left, right);
1528 Py_DECREF(left);
1529 Py_DECREF(right);
1530 SET_TOP(res);
1531 if (res == NULL)
1532 goto error;
1533 DISPATCH();
1534 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001535
Benjamin Petersonddd19492018-09-16 22:38:02 -07001536 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001537 PyObject *right = POP();
1538 PyObject *left = TOP();
1539 PyObject *res = PyNumber_InPlaceAnd(left, right);
1540 Py_DECREF(left);
1541 Py_DECREF(right);
1542 SET_TOP(res);
1543 if (res == NULL)
1544 goto error;
1545 DISPATCH();
1546 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001547
Benjamin Petersonddd19492018-09-16 22:38:02 -07001548 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001549 PyObject *right = POP();
1550 PyObject *left = TOP();
1551 PyObject *res = PyNumber_InPlaceXor(left, right);
1552 Py_DECREF(left);
1553 Py_DECREF(right);
1554 SET_TOP(res);
1555 if (res == NULL)
1556 goto error;
1557 DISPATCH();
1558 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001559
Benjamin Petersonddd19492018-09-16 22:38:02 -07001560 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001561 PyObject *right = POP();
1562 PyObject *left = TOP();
1563 PyObject *res = PyNumber_InPlaceOr(left, right);
1564 Py_DECREF(left);
1565 Py_DECREF(right);
1566 SET_TOP(res);
1567 if (res == NULL)
1568 goto error;
1569 DISPATCH();
1570 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001571
Benjamin Petersonddd19492018-09-16 22:38:02 -07001572 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001573 PyObject *sub = TOP();
1574 PyObject *container = SECOND();
1575 PyObject *v = THIRD();
1576 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001577 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001578 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001579 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001581 Py_DECREF(container);
1582 Py_DECREF(sub);
1583 if (err != 0)
1584 goto error;
1585 DISPATCH();
1586 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001587
Benjamin Petersonddd19492018-09-16 22:38:02 -07001588 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001589 PyObject *sub = TOP();
1590 PyObject *container = SECOND();
1591 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001592 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001593 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001594 err = PyObject_DelItem(container, sub);
1595 Py_DECREF(container);
1596 Py_DECREF(sub);
1597 if (err != 0)
1598 goto error;
1599 DISPATCH();
1600 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001601
Benjamin Petersonddd19492018-09-16 22:38:02 -07001602 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001603 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001604 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001605 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001606 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001607 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 PyErr_SetString(PyExc_RuntimeError,
1609 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001610 Py_DECREF(value);
1611 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001613 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001614 Py_DECREF(value);
1615 if (res == NULL)
1616 goto error;
1617 Py_DECREF(res);
1618 DISPATCH();
1619 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001620
Benjamin Petersonddd19492018-09-16 22:38:02 -07001621 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001622 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 switch (oparg) {
1624 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001625 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001626 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001628 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001629 /* fall through */
1630 case 0:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001631 if (do_raise(exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001632 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001633 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 break;
1635 default:
1636 PyErr_SetString(PyExc_SystemError,
1637 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 break;
1639 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001640 goto error;
1641 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001642
Benjamin Petersonddd19492018-09-16 22:38:02 -07001643 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001645 assert(f->f_iblock == 0);
1646 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001647 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001648
Benjamin Petersonddd19492018-09-16 22:38:02 -07001649 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001650 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001651 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001652 PyObject *obj = TOP();
1653 PyTypeObject *type = Py_TYPE(obj);
1654
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001655 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001656 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001657 }
Yury Selivanov75445082015-05-11 22:57:16 -04001658
1659 if (getter != NULL) {
1660 iter = (*getter)(obj);
1661 Py_DECREF(obj);
1662 if (iter == NULL) {
1663 SET_TOP(NULL);
1664 goto error;
1665 }
1666 }
1667 else {
1668 SET_TOP(NULL);
1669 PyErr_Format(
1670 PyExc_TypeError,
1671 "'async for' requires an object with "
1672 "__aiter__ method, got %.100s",
1673 type->tp_name);
1674 Py_DECREF(obj);
1675 goto error;
1676 }
1677
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001678 if (Py_TYPE(iter)->tp_as_async == NULL ||
1679 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001680
Yury Selivanov398ff912017-03-02 22:20:00 -05001681 SET_TOP(NULL);
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001682 PyErr_Format(
1683 PyExc_TypeError,
1684 "'async for' received an object from __aiter__ "
1685 "that does not implement __anext__: %.100s",
1686 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001687 Py_DECREF(iter);
1688 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001689 }
1690
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001691 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001692 DISPATCH();
1693 }
1694
Benjamin Petersonddd19492018-09-16 22:38:02 -07001695 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001696 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001697 PyObject *next_iter = NULL;
1698 PyObject *awaitable = NULL;
1699 PyObject *aiter = TOP();
1700 PyTypeObject *type = Py_TYPE(aiter);
1701
Yury Selivanoveb636452016-09-08 22:01:51 -07001702 if (PyAsyncGen_CheckExact(aiter)) {
1703 awaitable = type->tp_as_async->am_anext(aiter);
1704 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001705 goto error;
1706 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001707 } else {
1708 if (type->tp_as_async != NULL){
1709 getter = type->tp_as_async->am_anext;
1710 }
Yury Selivanov75445082015-05-11 22:57:16 -04001711
Yury Selivanoveb636452016-09-08 22:01:51 -07001712 if (getter != NULL) {
1713 next_iter = (*getter)(aiter);
1714 if (next_iter == NULL) {
1715 goto error;
1716 }
1717 }
1718 else {
1719 PyErr_Format(
1720 PyExc_TypeError,
1721 "'async for' requires an iterator with "
1722 "__anext__ method, got %.100s",
1723 type->tp_name);
1724 goto error;
1725 }
Yury Selivanov75445082015-05-11 22:57:16 -04001726
Yury Selivanoveb636452016-09-08 22:01:51 -07001727 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1728 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001729 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001730 PyExc_TypeError,
1731 "'async for' received an invalid object "
1732 "from __anext__: %.100s",
1733 Py_TYPE(next_iter)->tp_name);
1734
1735 Py_DECREF(next_iter);
1736 goto error;
1737 } else {
1738 Py_DECREF(next_iter);
1739 }
1740 }
Yury Selivanov75445082015-05-11 22:57:16 -04001741
1742 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001743 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001744 DISPATCH();
1745 }
1746
Benjamin Petersonddd19492018-09-16 22:38:02 -07001747 case TARGET(GET_AWAITABLE): {
1748 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001749 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001750 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001751
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03001752 if (iter == NULL) {
1753 format_awaitable_error(Py_TYPE(iterable),
1754 _Py_OPCODE(next_instr[-2]));
1755 }
1756
Yury Selivanov75445082015-05-11 22:57:16 -04001757 Py_DECREF(iterable);
1758
Yury Selivanovc724bae2016-03-02 11:30:46 -05001759 if (iter != NULL && PyCoro_CheckExact(iter)) {
1760 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1761 if (yf != NULL) {
1762 /* `iter` is a coroutine object that is being
1763 awaited, `yf` is a pointer to the current awaitable
1764 being awaited on. */
1765 Py_DECREF(yf);
1766 Py_CLEAR(iter);
1767 PyErr_SetString(
1768 PyExc_RuntimeError,
1769 "coroutine is being awaited already");
1770 /* The code below jumps to `error` if `iter` is NULL. */
1771 }
1772 }
1773
Yury Selivanov75445082015-05-11 22:57:16 -04001774 SET_TOP(iter); /* Even if it's NULL */
1775
1776 if (iter == NULL) {
1777 goto error;
1778 }
1779
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001780 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001781 DISPATCH();
1782 }
1783
Benjamin Petersonddd19492018-09-16 22:38:02 -07001784 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001785 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001786 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001787 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001788 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1789 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001790 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001791 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001792 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001793 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001794 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001795 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001796 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001797 Py_DECREF(v);
1798 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001799 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001800 if (tstate->c_tracefunc != NULL
1801 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001802 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001803 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001804 if (err < 0)
1805 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001806 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001807 SET_TOP(val);
1808 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001809 }
Martin Panter95f53c12016-07-18 08:23:26 +00001810 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001811 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001812 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001813 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001814 f->f_lasti -= sizeof(_Py_CODEUNIT);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001815 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001816 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001817
Benjamin Petersonddd19492018-09-16 22:38:02 -07001818 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07001820
1821 if (co->co_flags & CO_ASYNC_GENERATOR) {
1822 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
1823 Py_DECREF(retval);
1824 if (w == NULL) {
1825 retval = NULL;
1826 goto error;
1827 }
1828 retval = w;
1829 }
1830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001831 f->f_stacktop = stack_pointer;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001832 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001833 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001834
Benjamin Petersonddd19492018-09-16 22:38:02 -07001835 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001836 PyObject *type, *value, *traceback;
1837 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001838 PyTryBlock *b = PyFrame_BlockPop(f);
1839 if (b->b_type != EXCEPT_HANDLER) {
1840 PyErr_SetString(PyExc_SystemError,
1841 "popped block is not an except handler");
1842 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001843 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001844 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
1845 STACK_LEVEL() <= (b)->b_level + 4);
1846 exc_info = tstate->exc_info;
1847 type = exc_info->exc_type;
1848 value = exc_info->exc_value;
1849 traceback = exc_info->exc_traceback;
1850 exc_info->exc_type = POP();
1851 exc_info->exc_value = POP();
1852 exc_info->exc_traceback = POP();
1853 Py_XDECREF(type);
1854 Py_XDECREF(value);
1855 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001857 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001858
Benjamin Petersonddd19492018-09-16 22:38:02 -07001859 case TARGET(POP_BLOCK): {
1860 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001861 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001863 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001864
Benjamin Petersonddd19492018-09-16 22:38:02 -07001865 case TARGET(POP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001866 /* If oparg is 0 at the top of the stack are 1 or 6 values:
1867 Either:
1868 - TOP = NULL or an integer
1869 or:
1870 - (TOP, SECOND, THIRD) = exc_info()
1871 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
1872
1873 If oparg is 1 the value for 'return' was additionally pushed
1874 at the top of the stack.
1875 */
1876 PyObject *res = NULL;
1877 if (oparg) {
1878 res = POP();
1879 }
1880 PyObject *exc = POP();
1881 if (exc == NULL || PyLong_CheckExact(exc)) {
1882 Py_XDECREF(exc);
1883 }
1884 else {
1885 Py_DECREF(exc);
1886 Py_DECREF(POP());
1887 Py_DECREF(POP());
1888
1889 PyObject *type, *value, *traceback;
1890 _PyErr_StackItem *exc_info;
1891 PyTryBlock *b = PyFrame_BlockPop(f);
1892 if (b->b_type != EXCEPT_HANDLER) {
1893 PyErr_SetString(PyExc_SystemError,
1894 "popped block is not an except handler");
1895 Py_XDECREF(res);
1896 goto error;
1897 }
1898 assert(STACK_LEVEL() == (b)->b_level + 3);
1899 exc_info = tstate->exc_info;
1900 type = exc_info->exc_type;
1901 value = exc_info->exc_value;
1902 traceback = exc_info->exc_traceback;
1903 exc_info->exc_type = POP();
1904 exc_info->exc_value = POP();
1905 exc_info->exc_traceback = POP();
1906 Py_XDECREF(type);
1907 Py_XDECREF(value);
1908 Py_XDECREF(traceback);
1909 }
1910 if (oparg) {
1911 PUSH(res);
1912 }
1913 DISPATCH();
1914 }
1915
Benjamin Petersonddd19492018-09-16 22:38:02 -07001916 case TARGET(CALL_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001917 PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
1918 if (ret == NULL) {
1919 goto error;
1920 }
1921 PUSH(ret);
1922 JUMPBY(oparg);
1923 FAST_DISPATCH();
1924 }
1925
Benjamin Petersonddd19492018-09-16 22:38:02 -07001926 case TARGET(BEGIN_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001927 /* Push NULL onto the stack for using it in END_FINALLY,
1928 POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH.
1929 */
1930 PUSH(NULL);
1931 FAST_DISPATCH();
1932 }
1933
Benjamin Petersonddd19492018-09-16 22:38:02 -07001934 case TARGET(END_FINALLY): {
1935 PREDICTED(END_FINALLY);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001936 /* At the top of the stack are 1 or 6 values:
1937 Either:
1938 - TOP = NULL or an integer
1939 or:
1940 - (TOP, SECOND, THIRD) = exc_info()
1941 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
1942 */
1943 PyObject *exc = POP();
1944 if (exc == NULL) {
1945 FAST_DISPATCH();
1946 }
1947 else if (PyLong_CheckExact(exc)) {
1948 int ret = _PyLong_AsInt(exc);
1949 Py_DECREF(exc);
1950 if (ret == -1 && PyErr_Occurred()) {
1951 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001953 JUMPTO(ret);
1954 FAST_DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001956 else {
1957 assert(PyExceptionClass_Check(exc));
1958 PyObject *val = POP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001959 PyObject *tb = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001960 PyErr_Restore(exc, val, tb);
1961 goto exception_unwind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001963 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001964
Benjamin Petersonddd19492018-09-16 22:38:02 -07001965 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02001966 PyObject *exc = POP();
1967 assert(PyExceptionClass_Check(exc));
1968 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
1969 PyTryBlock *b = PyFrame_BlockPop(f);
1970 assert(b->b_type == EXCEPT_HANDLER);
1971 Py_DECREF(exc);
1972 UNWIND_EXCEPT_HANDLER(b);
1973 Py_DECREF(POP());
1974 JUMPBY(oparg);
1975 FAST_DISPATCH();
1976 }
1977 else {
1978 PyObject *val = POP();
1979 PyObject *tb = POP();
1980 PyErr_Restore(exc, val, tb);
1981 goto exception_unwind;
1982 }
1983 }
1984
Benjamin Petersonddd19492018-09-16 22:38:02 -07001985 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02001986 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001987
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001988 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001989 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001990 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
1991 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02001992 PyErr_SetString(PyExc_NameError,
1993 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001994 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001995 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001996 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001997 }
1998 else {
1999 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2000 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002001 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002002 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2003 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002004 if (PyErr_ExceptionMatches(PyExc_KeyError))
2005 PyErr_SetString(PyExc_NameError,
2006 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002007 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002008 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002010 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002011 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002012 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002013
Benjamin Petersonddd19492018-09-16 22:38:02 -07002014 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002015 PyObject *name = GETITEM(names, oparg);
2016 PyObject *v = POP();
2017 PyObject *ns = f->f_locals;
2018 int err;
2019 if (ns == NULL) {
2020 PyErr_Format(PyExc_SystemError,
2021 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002022 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002023 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002024 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002025 if (PyDict_CheckExact(ns))
2026 err = PyDict_SetItem(ns, name, v);
2027 else
2028 err = PyObject_SetItem(ns, name, v);
2029 Py_DECREF(v);
2030 if (err != 0)
2031 goto error;
2032 DISPATCH();
2033 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002034
Benjamin Petersonddd19492018-09-16 22:38:02 -07002035 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002036 PyObject *name = GETITEM(names, oparg);
2037 PyObject *ns = f->f_locals;
2038 int err;
2039 if (ns == NULL) {
2040 PyErr_Format(PyExc_SystemError,
2041 "no locals when deleting %R", name);
2042 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002043 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002044 err = PyObject_DelItem(ns, name);
2045 if (err != 0) {
2046 format_exc_check_arg(PyExc_NameError,
2047 NAME_ERROR_MSG,
2048 name);
2049 goto error;
2050 }
2051 DISPATCH();
2052 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002053
Benjamin Petersonddd19492018-09-16 22:38:02 -07002054 case TARGET(UNPACK_SEQUENCE): {
2055 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002056 PyObject *seq = POP(), *item, **items;
2057 if (PyTuple_CheckExact(seq) &&
2058 PyTuple_GET_SIZE(seq) == oparg) {
2059 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002060 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002061 item = items[oparg];
2062 Py_INCREF(item);
2063 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002064 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002065 } else if (PyList_CheckExact(seq) &&
2066 PyList_GET_SIZE(seq) == oparg) {
2067 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002068 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002069 item = items[oparg];
2070 Py_INCREF(item);
2071 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002073 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002075 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 } else {
2077 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002078 Py_DECREF(seq);
2079 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002080 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002081 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002082 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002083 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002084
Benjamin Petersonddd19492018-09-16 22:38:02 -07002085 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002086 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2087 PyObject *seq = POP();
2088
2089 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2090 stack_pointer + totalargs)) {
2091 stack_pointer += totalargs;
2092 } else {
2093 Py_DECREF(seq);
2094 goto error;
2095 }
2096 Py_DECREF(seq);
2097 DISPATCH();
2098 }
2099
Benjamin Petersonddd19492018-09-16 22:38:02 -07002100 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002101 PyObject *name = GETITEM(names, oparg);
2102 PyObject *owner = TOP();
2103 PyObject *v = SECOND();
2104 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002105 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002106 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002107 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002108 Py_DECREF(owner);
2109 if (err != 0)
2110 goto error;
2111 DISPATCH();
2112 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002113
Benjamin Petersonddd19492018-09-16 22:38:02 -07002114 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002115 PyObject *name = GETITEM(names, oparg);
2116 PyObject *owner = POP();
2117 int err;
2118 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2119 Py_DECREF(owner);
2120 if (err != 0)
2121 goto error;
2122 DISPATCH();
2123 }
2124
Benjamin Petersonddd19492018-09-16 22:38:02 -07002125 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002126 PyObject *name = GETITEM(names, oparg);
2127 PyObject *v = POP();
2128 int err;
2129 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002130 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002131 if (err != 0)
2132 goto error;
2133 DISPATCH();
2134 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002135
Benjamin Petersonddd19492018-09-16 22:38:02 -07002136 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002137 PyObject *name = GETITEM(names, oparg);
2138 int err;
2139 err = PyDict_DelItem(f->f_globals, name);
2140 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002141 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002142 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002143 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002144 }
2145 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002146 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002147
Benjamin Petersonddd19492018-09-16 22:38:02 -07002148 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002149 PyObject *name = GETITEM(names, oparg);
2150 PyObject *locals = f->f_locals;
2151 PyObject *v;
2152 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002153 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002154 "no locals when loading %R", name);
2155 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002156 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002157 if (PyDict_CheckExact(locals)) {
2158 v = PyDict_GetItem(locals, name);
2159 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002160 }
2161 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002162 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002163 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002164 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2165 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002166 PyErr_Clear();
2167 }
2168 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002169 if (v == NULL) {
2170 v = PyDict_GetItem(f->f_globals, name);
2171 Py_XINCREF(v);
2172 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002173 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002174 v = PyDict_GetItem(f->f_builtins, name);
2175 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002176 format_exc_check_arg(
2177 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002178 NAME_ERROR_MSG, name);
2179 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002180 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002181 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002182 }
2183 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002184 v = PyObject_GetItem(f->f_builtins, name);
2185 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002186 if (PyErr_ExceptionMatches(PyExc_KeyError))
2187 format_exc_check_arg(
2188 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002189 NAME_ERROR_MSG, name);
2190 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002191 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002192 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002193 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002195 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002196 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002197 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002198
Benjamin Petersonddd19492018-09-16 22:38:02 -07002199 case TARGET(LOAD_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002200 PyObject *name = GETITEM(names, oparg);
2201 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002202 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002203 && PyDict_CheckExact(f->f_builtins))
2204 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002205 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002206 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002207 name);
2208 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002209 if (!_PyErr_OCCURRED()) {
2210 /* _PyDict_LoadGlobal() returns NULL without raising
2211 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002212 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002213 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002214 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002215 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002217 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002219 else {
2220 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002221
2222 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002223 v = PyObject_GetItem(f->f_globals, name);
2224 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002225 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2226 goto error;
2227 PyErr_Clear();
2228
Victor Stinnerb4efc962015-11-20 09:24:02 +01002229 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002230 v = PyObject_GetItem(f->f_builtins, name);
2231 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002232 if (PyErr_ExceptionMatches(PyExc_KeyError))
2233 format_exc_check_arg(
2234 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002235 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002236 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002237 }
2238 }
2239 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002240 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002241 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002242 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002243
Benjamin Petersonddd19492018-09-16 22:38:02 -07002244 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002245 PyObject *v = GETLOCAL(oparg);
2246 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 SETLOCAL(oparg, NULL);
2248 DISPATCH();
2249 }
2250 format_exc_check_arg(
2251 PyExc_UnboundLocalError,
2252 UNBOUNDLOCAL_ERROR_MSG,
2253 PyTuple_GetItem(co->co_varnames, oparg)
2254 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002255 goto error;
2256 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002257
Benjamin Petersonddd19492018-09-16 22:38:02 -07002258 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002259 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002260 PyObject *oldobj = PyCell_GET(cell);
2261 if (oldobj != NULL) {
2262 PyCell_SET(cell, NULL);
2263 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002264 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002265 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002266 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002267 goto error;
2268 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002269
Benjamin Petersonddd19492018-09-16 22:38:02 -07002270 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002271 PyObject *cell = freevars[oparg];
2272 Py_INCREF(cell);
2273 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002274 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002275 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002276
Benjamin Petersonddd19492018-09-16 22:38:02 -07002277 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002278 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002279 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002280 assert(locals);
2281 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2282 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2283 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2284 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2285 if (PyDict_CheckExact(locals)) {
2286 value = PyDict_GetItem(locals, name);
2287 Py_XINCREF(value);
2288 }
2289 else {
2290 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002291 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002292 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2293 goto error;
2294 PyErr_Clear();
2295 }
2296 }
2297 if (!value) {
2298 PyObject *cell = freevars[oparg];
2299 value = PyCell_GET(cell);
2300 if (value == NULL) {
2301 format_exc_unbound(co, oparg);
2302 goto error;
2303 }
2304 Py_INCREF(value);
2305 }
2306 PUSH(value);
2307 DISPATCH();
2308 }
2309
Benjamin Petersonddd19492018-09-16 22:38:02 -07002310 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002311 PyObject *cell = freevars[oparg];
2312 PyObject *value = PyCell_GET(cell);
2313 if (value == NULL) {
2314 format_exc_unbound(co, oparg);
2315 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002317 Py_INCREF(value);
2318 PUSH(value);
2319 DISPATCH();
2320 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002321
Benjamin Petersonddd19492018-09-16 22:38:02 -07002322 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002323 PyObject *v = POP();
2324 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002325 PyObject *oldobj = PyCell_GET(cell);
2326 PyCell_SET(cell, v);
2327 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002328 DISPATCH();
2329 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002330
Benjamin Petersonddd19492018-09-16 22:38:02 -07002331 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002332 PyObject *str;
2333 PyObject *empty = PyUnicode_New(0, 0);
2334 if (empty == NULL) {
2335 goto error;
2336 }
2337 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2338 Py_DECREF(empty);
2339 if (str == NULL)
2340 goto error;
2341 while (--oparg >= 0) {
2342 PyObject *item = POP();
2343 Py_DECREF(item);
2344 }
2345 PUSH(str);
2346 DISPATCH();
2347 }
2348
Benjamin Petersonddd19492018-09-16 22:38:02 -07002349 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002350 PyObject *tup = PyTuple_New(oparg);
2351 if (tup == NULL)
2352 goto error;
2353 while (--oparg >= 0) {
2354 PyObject *item = POP();
2355 PyTuple_SET_ITEM(tup, oparg, item);
2356 }
2357 PUSH(tup);
2358 DISPATCH();
2359 }
2360
Benjamin Petersonddd19492018-09-16 22:38:02 -07002361 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002362 PyObject *list = PyList_New(oparg);
2363 if (list == NULL)
2364 goto error;
2365 while (--oparg >= 0) {
2366 PyObject *item = POP();
2367 PyList_SET_ITEM(list, oparg, item);
2368 }
2369 PUSH(list);
2370 DISPATCH();
2371 }
2372
Benjamin Petersonddd19492018-09-16 22:38:02 -07002373 case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL):
2374 case TARGET(BUILD_TUPLE_UNPACK):
2375 case TARGET(BUILD_LIST_UNPACK): {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002376 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002377 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002378 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002379 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002380
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002381 if (sum == NULL)
2382 goto error;
2383
2384 for (i = oparg; i > 0; i--) {
2385 PyObject *none_val;
2386
2387 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2388 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002389 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002390 PyErr_ExceptionMatches(PyExc_TypeError))
2391 {
2392 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002393 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002394 Py_DECREF(sum);
2395 goto error;
2396 }
2397 Py_DECREF(none_val);
2398 }
2399
2400 if (convert_to_tuple) {
2401 return_value = PyList_AsTuple(sum);
2402 Py_DECREF(sum);
2403 if (return_value == NULL)
2404 goto error;
2405 }
2406 else {
2407 return_value = sum;
2408 }
2409
2410 while (oparg--)
2411 Py_DECREF(POP());
2412 PUSH(return_value);
2413 DISPATCH();
2414 }
2415
Benjamin Petersonddd19492018-09-16 22:38:02 -07002416 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002417 PyObject *set = PySet_New(NULL);
2418 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002419 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002420 if (set == NULL)
2421 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002422 for (i = oparg; i > 0; i--) {
2423 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002424 if (err == 0)
2425 err = PySet_Add(set, item);
2426 Py_DECREF(item);
2427 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002428 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002429 if (err != 0) {
2430 Py_DECREF(set);
2431 goto error;
2432 }
2433 PUSH(set);
2434 DISPATCH();
2435 }
2436
Benjamin Petersonddd19492018-09-16 22:38:02 -07002437 case TARGET(BUILD_SET_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002438 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002439 PyObject *sum = PySet_New(NULL);
2440 if (sum == NULL)
2441 goto error;
2442
2443 for (i = oparg; i > 0; i--) {
2444 if (_PySet_Update(sum, PEEK(i)) < 0) {
2445 Py_DECREF(sum);
2446 goto error;
2447 }
2448 }
2449
2450 while (oparg--)
2451 Py_DECREF(POP());
2452 PUSH(sum);
2453 DISPATCH();
2454 }
2455
Benjamin Petersonddd19492018-09-16 22:38:02 -07002456 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002457 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002458 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2459 if (map == NULL)
2460 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002461 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002462 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002463 PyObject *key = PEEK(2*i);
2464 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002465 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002466 if (err != 0) {
2467 Py_DECREF(map);
2468 goto error;
2469 }
2470 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002471
2472 while (oparg--) {
2473 Py_DECREF(POP());
2474 Py_DECREF(POP());
2475 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002476 PUSH(map);
2477 DISPATCH();
2478 }
2479
Benjamin Petersonddd19492018-09-16 22:38:02 -07002480 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002481 _Py_IDENTIFIER(__annotations__);
2482 int err;
2483 PyObject *ann_dict;
2484 if (f->f_locals == NULL) {
2485 PyErr_Format(PyExc_SystemError,
2486 "no locals found when setting up annotations");
2487 goto error;
2488 }
2489 /* check if __annotations__ in locals()... */
2490 if (PyDict_CheckExact(f->f_locals)) {
2491 ann_dict = _PyDict_GetItemId(f->f_locals,
2492 &PyId___annotations__);
2493 if (ann_dict == NULL) {
2494 /* ...if not, create a new one */
2495 ann_dict = PyDict_New();
2496 if (ann_dict == NULL) {
2497 goto error;
2498 }
2499 err = _PyDict_SetItemId(f->f_locals,
2500 &PyId___annotations__, ann_dict);
2501 Py_DECREF(ann_dict);
2502 if (err != 0) {
2503 goto error;
2504 }
2505 }
2506 }
2507 else {
2508 /* do the same if locals() is not a dict */
2509 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2510 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002511 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002512 }
2513 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2514 if (ann_dict == NULL) {
2515 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2516 goto error;
2517 }
2518 PyErr_Clear();
2519 ann_dict = PyDict_New();
2520 if (ann_dict == NULL) {
2521 goto error;
2522 }
2523 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2524 Py_DECREF(ann_dict);
2525 if (err != 0) {
2526 goto error;
2527 }
2528 }
2529 else {
2530 Py_DECREF(ann_dict);
2531 }
2532 }
2533 DISPATCH();
2534 }
2535
Benjamin Petersonddd19492018-09-16 22:38:02 -07002536 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002537 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002538 PyObject *map;
2539 PyObject *keys = TOP();
2540 if (!PyTuple_CheckExact(keys) ||
2541 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2542 PyErr_SetString(PyExc_SystemError,
2543 "bad BUILD_CONST_KEY_MAP keys argument");
2544 goto error;
2545 }
2546 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2547 if (map == NULL) {
2548 goto error;
2549 }
2550 for (i = oparg; i > 0; i--) {
2551 int err;
2552 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2553 PyObject *value = PEEK(i + 1);
2554 err = PyDict_SetItem(map, key, value);
2555 if (err != 0) {
2556 Py_DECREF(map);
2557 goto error;
2558 }
2559 }
2560
2561 Py_DECREF(POP());
2562 while (oparg--) {
2563 Py_DECREF(POP());
2564 }
2565 PUSH(map);
2566 DISPATCH();
2567 }
2568
Benjamin Petersonddd19492018-09-16 22:38:02 -07002569 case TARGET(BUILD_MAP_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002570 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002571 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002572 if (sum == NULL)
2573 goto error;
2574
2575 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002576 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002577 if (PyDict_Update(sum, arg) < 0) {
2578 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2579 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002580 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002581 arg->ob_type->tp_name);
2582 }
2583 Py_DECREF(sum);
2584 goto error;
2585 }
2586 }
2587
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002588 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002589 Py_DECREF(POP());
2590 PUSH(sum);
2591 DISPATCH();
2592 }
2593
Benjamin Petersonddd19492018-09-16 22:38:02 -07002594 case TARGET(BUILD_MAP_UNPACK_WITH_CALL): {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002595 Py_ssize_t i;
2596 PyObject *sum = PyDict_New();
2597 if (sum == NULL)
2598 goto error;
2599
2600 for (i = oparg; i > 0; i--) {
2601 PyObject *arg = PEEK(i);
2602 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
2603 PyObject *func = PEEK(2 + oparg);
2604 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002605 format_kwargs_mapping_error(func, arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002606 }
2607 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2608 PyObject *exc, *val, *tb;
2609 PyErr_Fetch(&exc, &val, &tb);
2610 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
2611 PyObject *key = PyTuple_GET_ITEM(val, 0);
2612 if (!PyUnicode_Check(key)) {
2613 PyErr_Format(PyExc_TypeError,
2614 "%.200s%.200s keywords must be strings",
2615 PyEval_GetFuncName(func),
2616 PyEval_GetFuncDesc(func));
2617 } else {
2618 PyErr_Format(PyExc_TypeError,
2619 "%.200s%.200s got multiple "
2620 "values for keyword argument '%U'",
2621 PyEval_GetFuncName(func),
2622 PyEval_GetFuncDesc(func),
2623 key);
2624 }
2625 Py_XDECREF(exc);
2626 Py_XDECREF(val);
2627 Py_XDECREF(tb);
2628 }
2629 else {
2630 PyErr_Restore(exc, val, tb);
2631 }
2632 }
2633 Py_DECREF(sum);
2634 goto error;
2635 }
2636 }
2637
2638 while (oparg--)
2639 Py_DECREF(POP());
2640 PUSH(sum);
2641 DISPATCH();
2642 }
2643
Benjamin Petersonddd19492018-09-16 22:38:02 -07002644 case TARGET(MAP_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002645 PyObject *key = TOP();
2646 PyObject *value = SECOND();
2647 PyObject *map;
2648 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002649 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002650 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002651 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002652 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002653 Py_DECREF(value);
2654 Py_DECREF(key);
2655 if (err != 0)
2656 goto error;
2657 PREDICT(JUMP_ABSOLUTE);
2658 DISPATCH();
2659 }
2660
Benjamin Petersonddd19492018-09-16 22:38:02 -07002661 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002662 PyObject *name = GETITEM(names, oparg);
2663 PyObject *owner = TOP();
2664 PyObject *res = PyObject_GetAttr(owner, name);
2665 Py_DECREF(owner);
2666 SET_TOP(res);
2667 if (res == NULL)
2668 goto error;
2669 DISPATCH();
2670 }
2671
Benjamin Petersonddd19492018-09-16 22:38:02 -07002672 case TARGET(COMPARE_OP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002673 PyObject *right = POP();
2674 PyObject *left = TOP();
2675 PyObject *res = cmp_outcome(oparg, left, right);
2676 Py_DECREF(left);
2677 Py_DECREF(right);
2678 SET_TOP(res);
2679 if (res == NULL)
2680 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002681 PREDICT(POP_JUMP_IF_FALSE);
2682 PREDICT(POP_JUMP_IF_TRUE);
2683 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002684 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002685
Benjamin Petersonddd19492018-09-16 22:38:02 -07002686 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002687 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002688 PyObject *fromlist = POP();
2689 PyObject *level = TOP();
2690 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002691 res = import_name(f, name, fromlist, level);
2692 Py_DECREF(level);
2693 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002694 SET_TOP(res);
2695 if (res == NULL)
2696 goto error;
2697 DISPATCH();
2698 }
2699
Benjamin Petersonddd19492018-09-16 22:38:02 -07002700 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002701 PyObject *from = POP(), *locals;
2702 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002703 if (PyFrame_FastToLocalsWithError(f) < 0) {
2704 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002705 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002706 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002707
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002708 locals = f->f_locals;
2709 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002710 PyErr_SetString(PyExc_SystemError,
2711 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002712 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002713 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002714 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002715 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002716 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002717 Py_DECREF(from);
2718 if (err != 0)
2719 goto error;
2720 DISPATCH();
2721 }
Guido van Rossum25831651993-05-19 14:50:45 +00002722
Benjamin Petersonddd19492018-09-16 22:38:02 -07002723 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002724 PyObject *name = GETITEM(names, oparg);
2725 PyObject *from = TOP();
2726 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002727 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002728 PUSH(res);
2729 if (res == NULL)
2730 goto error;
2731 DISPATCH();
2732 }
Thomas Wouters52152252000-08-17 22:55:00 +00002733
Benjamin Petersonddd19492018-09-16 22:38:02 -07002734 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002735 JUMPBY(oparg);
2736 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002737 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002738
Benjamin Petersonddd19492018-09-16 22:38:02 -07002739 case TARGET(POP_JUMP_IF_FALSE): {
2740 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002741 PyObject *cond = POP();
2742 int err;
2743 if (cond == Py_True) {
2744 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002745 FAST_DISPATCH();
2746 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002747 if (cond == Py_False) {
2748 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002749 JUMPTO(oparg);
2750 FAST_DISPATCH();
2751 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002752 err = PyObject_IsTrue(cond);
2753 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002754 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002755 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002756 else if (err == 0)
2757 JUMPTO(oparg);
2758 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002759 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002761 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002762
Benjamin Petersonddd19492018-09-16 22:38:02 -07002763 case TARGET(POP_JUMP_IF_TRUE): {
2764 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002765 PyObject *cond = POP();
2766 int err;
2767 if (cond == Py_False) {
2768 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002769 FAST_DISPATCH();
2770 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002771 if (cond == Py_True) {
2772 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002773 JUMPTO(oparg);
2774 FAST_DISPATCH();
2775 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002776 err = PyObject_IsTrue(cond);
2777 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002778 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002779 JUMPTO(oparg);
2780 }
2781 else if (err == 0)
2782 ;
2783 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002784 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002785 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002786 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002787
Benjamin Petersonddd19492018-09-16 22:38:02 -07002788 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002789 PyObject *cond = TOP();
2790 int err;
2791 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002792 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002793 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002794 FAST_DISPATCH();
2795 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002796 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002797 JUMPTO(oparg);
2798 FAST_DISPATCH();
2799 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002800 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002801 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002802 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002803 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002804 }
2805 else if (err == 0)
2806 JUMPTO(oparg);
2807 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002808 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002809 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002810 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002811
Benjamin Petersonddd19492018-09-16 22:38:02 -07002812 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002813 PyObject *cond = TOP();
2814 int err;
2815 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002816 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002817 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002818 FAST_DISPATCH();
2819 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002820 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002821 JUMPTO(oparg);
2822 FAST_DISPATCH();
2823 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002824 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002825 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002826 JUMPTO(oparg);
2827 }
2828 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002829 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002830 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002831 }
2832 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002833 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002834 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002835 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002836
Benjamin Petersonddd19492018-09-16 22:38:02 -07002837 case TARGET(JUMP_ABSOLUTE): {
2838 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002839 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002840#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002841 /* Enabling this path speeds-up all while and for-loops by bypassing
2842 the per-loop checks for signals. By default, this should be turned-off
2843 because it prevents detection of a control-break in tight loops like
2844 "while 1: pass". Compile with this option turned-on when you need
2845 the speed-up and do not need break checking inside tight loops (ones
2846 that contain only instructions ending with FAST_DISPATCH).
2847 */
2848 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002849#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002850 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002851#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002852 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002853
Benjamin Petersonddd19492018-09-16 22:38:02 -07002854 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002855 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002856 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002857 PyObject *iter = PyObject_GetIter(iterable);
2858 Py_DECREF(iterable);
2859 SET_TOP(iter);
2860 if (iter == NULL)
2861 goto error;
2862 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002863 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002864 DISPATCH();
2865 }
2866
Benjamin Petersonddd19492018-09-16 22:38:02 -07002867 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04002868 /* before: [obj]; after [getiter(obj)] */
2869 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002870 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002871 if (PyCoro_CheckExact(iterable)) {
2872 /* `iterable` is a coroutine */
2873 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2874 /* and it is used in a 'yield from' expression of a
2875 regular generator. */
2876 Py_DECREF(iterable);
2877 SET_TOP(NULL);
2878 PyErr_SetString(PyExc_TypeError,
2879 "cannot 'yield from' a coroutine object "
2880 "in a non-coroutine generator");
2881 goto error;
2882 }
2883 }
2884 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002885 /* `iterable` is not a generator. */
2886 iter = PyObject_GetIter(iterable);
2887 Py_DECREF(iterable);
2888 SET_TOP(iter);
2889 if (iter == NULL)
2890 goto error;
2891 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002892 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002893 DISPATCH();
2894 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002895
Benjamin Petersonddd19492018-09-16 22:38:02 -07002896 case TARGET(FOR_ITER): {
2897 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002898 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002899 PyObject *iter = TOP();
2900 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2901 if (next != NULL) {
2902 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 PREDICT(STORE_FAST);
2904 PREDICT(UNPACK_SEQUENCE);
2905 DISPATCH();
2906 }
2907 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002908 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2909 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08002910 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002911 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002912 PyErr_Clear();
2913 }
2914 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00002915 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002916 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002918 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002919 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002920 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002921
Benjamin Petersonddd19492018-09-16 22:38:02 -07002922 case TARGET(SETUP_FINALLY): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002923 /* NOTE: If you add any new block-setup opcodes that
2924 are not try/except/finally handlers, you may need
2925 to update the PyGen_NeedsFinalizing() function.
2926 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002927
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002928 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002929 STACK_LEVEL());
2930 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002931 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002932
Benjamin Petersonddd19492018-09-16 22:38:02 -07002933 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04002934 _Py_IDENTIFIER(__aexit__);
2935 _Py_IDENTIFIER(__aenter__);
2936
2937 PyObject *mgr = TOP();
2938 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
2939 *enter;
2940 PyObject *res;
2941 if (exit == NULL)
2942 goto error;
2943 SET_TOP(exit);
2944 enter = special_lookup(mgr, &PyId___aenter__);
2945 Py_DECREF(mgr);
2946 if (enter == NULL)
2947 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002948 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04002949 Py_DECREF(enter);
2950 if (res == NULL)
2951 goto error;
2952 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002953 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002954 DISPATCH();
2955 }
2956
Benjamin Petersonddd19492018-09-16 22:38:02 -07002957 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04002958 PyObject *res = POP();
2959 /* Setup the finally block before pushing the result
2960 of __aenter__ on the stack. */
2961 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2962 STACK_LEVEL());
2963 PUSH(res);
2964 DISPATCH();
2965 }
2966
Benjamin Petersonddd19492018-09-16 22:38:02 -07002967 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05002968 _Py_IDENTIFIER(__exit__);
2969 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002970 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08002971 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002972 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08002973 if (enter == NULL)
2974 goto error;
2975 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08002976 if (exit == NULL) {
2977 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002978 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08002979 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002980 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002981 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002982 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002983 Py_DECREF(enter);
2984 if (res == NULL)
2985 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002986 /* Setup the finally block before pushing the result
2987 of __enter__ on the stack. */
2988 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2989 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002990
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002991 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002992 DISPATCH();
2993 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002994
Benjamin Petersonddd19492018-09-16 22:38:02 -07002995 case TARGET(WITH_CLEANUP_START): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002996 /* At the top of the stack are 1 or 6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002997 how/why we entered the finally clause:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002998 - TOP = NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002999 - (TOP, SECOND, THIRD) = exc_info()
3000 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003001 Below them is EXIT, the context.__exit__ or context.__aexit__
3002 bound method.
3003 In the first case, we must call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003004 EXIT(None, None, None)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003005 otherwise we must call
3006 EXIT(TOP, SECOND, THIRD)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003007
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003008 In the first case, we remove EXIT from the
3009 stack, leaving TOP, and push TOP on the stack.
3010 Otherwise we shift the bottom 3 values of the
3011 stack down, replace the empty spot with NULL, and push
3012 None on the stack.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003013
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003014 Finally we push the result of the call.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003015 */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003016 PyObject *stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003017 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003018 PyObject *exc, *val, *tb, *res;
3019
3020 val = tb = Py_None;
3021 exc = TOP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003022 if (exc == NULL) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003023 STACK_SHRINK(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003024 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003025 SET_TOP(exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003026 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003027 }
3028 else {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003029 assert(PyExceptionClass_Check(exc));
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003030 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003031 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003032 val = SECOND();
3033 tb = THIRD();
3034 tp2 = FOURTH();
3035 exc2 = PEEK(5);
3036 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003038 SET_VALUE(7, tb2);
3039 SET_VALUE(6, exc2);
3040 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003041 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3042 SET_FOURTH(NULL);
3043 /* We just shifted the stack down, so we have
3044 to tell the except handler block that the
3045 values are lower than it expects. */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003046 assert(f->f_iblock > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003047 block = &f->f_blockstack[f->f_iblock - 1];
3048 assert(block->b_type == EXCEPT_HANDLER);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003049 assert(block->b_level > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003050 block->b_level--;
3051 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003052
3053 stack[0] = exc;
3054 stack[1] = val;
3055 stack[2] = tb;
3056 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003057 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003058 if (res == NULL)
3059 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003060
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003061 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003062 PUSH(exc);
3063 PUSH(res);
3064 PREDICT(WITH_CLEANUP_FINISH);
3065 DISPATCH();
3066 }
3067
Benjamin Petersonddd19492018-09-16 22:38:02 -07003068 case TARGET(WITH_CLEANUP_FINISH): {
3069 PREDICTED(WITH_CLEANUP_FINISH);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003070 /* TOP = the result of calling the context.__exit__ bound method
3071 SECOND = either None or exception type
3072
3073 If SECOND is None below is NULL or the return address,
3074 otherwise below are 7 values representing an exception.
3075 */
Yury Selivanov75445082015-05-11 22:57:16 -04003076 PyObject *res = POP();
3077 PyObject *exc = POP();
3078 int err;
3079
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003080 if (exc != Py_None)
3081 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003082 else
3083 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003084
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003085 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003086 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003089 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090 else if (err > 0) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003091 /* There was an exception and a True return.
3092 * We must manually unwind the EXCEPT_HANDLER block
3093 * which was created when the exception was caught,
Quan Tian3bd0d622018-10-20 05:30:03 +08003094 * otherwise the stack will be in an inconsistent state.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003095 */
3096 PyTryBlock *b = PyFrame_BlockPop(f);
3097 assert(b->b_type == EXCEPT_HANDLER);
3098 UNWIND_EXCEPT_HANDLER(b);
3099 PUSH(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003100 }
3101 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003102 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003103 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003104
Benjamin Petersonddd19492018-09-16 22:38:02 -07003105 case TARGET(LOAD_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003106 /* Designed to work in tamdem with CALL_METHOD. */
3107 PyObject *name = GETITEM(names, oparg);
3108 PyObject *obj = TOP();
3109 PyObject *meth = NULL;
3110
3111 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3112
Yury Selivanovf2392132016-12-13 19:03:51 -05003113 if (meth == NULL) {
3114 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003115 goto error;
3116 }
3117
3118 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003119 /* We can bypass temporary bound method object.
3120 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003121
INADA Naoki015bce62017-01-16 17:23:30 +09003122 meth | self | arg1 | ... | argN
3123 */
3124 SET_TOP(meth);
3125 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003126 }
3127 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003128 /* meth is not an unbound method (but a regular attr, or
3129 something was returned by a descriptor protocol). Set
3130 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003131 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003132
3133 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003134 */
INADA Naoki015bce62017-01-16 17:23:30 +09003135 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003136 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003137 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003138 }
3139 DISPATCH();
3140 }
3141
Benjamin Petersonddd19492018-09-16 22:38:02 -07003142 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003143 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003144 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003145
3146 sp = stack_pointer;
3147
INADA Naoki015bce62017-01-16 17:23:30 +09003148 meth = PEEK(oparg + 2);
3149 if (meth == NULL) {
3150 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3151 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003152
3153 Stack layout:
3154
INADA Naoki015bce62017-01-16 17:23:30 +09003155 ... | NULL | callable | arg1 | ... | argN
3156 ^- TOP()
3157 ^- (-oparg)
3158 ^- (-oparg-1)
3159 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003160
Ville Skyttä49b27342017-08-03 09:00:59 +03003161 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003162 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003163 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003164 res = call_function(&sp, oparg, NULL);
3165 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003166 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003167 }
3168 else {
3169 /* This is a method call. Stack layout:
3170
INADA Naoki015bce62017-01-16 17:23:30 +09003171 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003172 ^- TOP()
3173 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003174 ^- (-oparg-1)
3175 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003176
INADA Naoki015bce62017-01-16 17:23:30 +09003177 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003178 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003179 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003180 */
3181 res = call_function(&sp, oparg + 1, NULL);
3182 stack_pointer = sp;
3183 }
3184
3185 PUSH(res);
3186 if (res == NULL)
3187 goto error;
3188 DISPATCH();
3189 }
3190
Benjamin Petersonddd19492018-09-16 22:38:02 -07003191 case TARGET(CALL_FUNCTION): {
3192 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003193 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003194 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003195 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003196 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003197 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003198 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003199 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003200 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003201 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003202 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003203
Benjamin Petersonddd19492018-09-16 22:38:02 -07003204 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003205 PyObject **sp, *res, *names;
3206
3207 names = POP();
3208 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003209 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003210 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003211 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003212 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003213 Py_DECREF(names);
3214
3215 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003216 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003217 }
3218 DISPATCH();
3219 }
3220
Benjamin Petersonddd19492018-09-16 22:38:02 -07003221 case TARGET(CALL_FUNCTION_EX): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003222 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003223 if (oparg & 0x01) {
3224 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003225 if (!PyDict_CheckExact(kwargs)) {
3226 PyObject *d = PyDict_New();
3227 if (d == NULL)
3228 goto error;
3229 if (PyDict_Update(d, kwargs) != 0) {
3230 Py_DECREF(d);
3231 /* PyDict_Update raises attribute
3232 * error (percolated from an attempt
3233 * to get 'keys' attribute) instead of
3234 * a type error if its second argument
3235 * is not a mapping.
3236 */
3237 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003238 format_kwargs_mapping_error(SECOND(), kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003239 }
Victor Stinnereece2222016-09-12 11:16:37 +02003240 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003241 goto error;
3242 }
3243 Py_DECREF(kwargs);
3244 kwargs = d;
3245 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003246 assert(PyDict_CheckExact(kwargs));
3247 }
3248 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003249 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003250 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003251 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003252 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003253 goto error;
3254 }
3255 Py_SETREF(callargs, PySequence_Tuple(callargs));
3256 if (callargs == NULL) {
3257 goto error;
3258 }
3259 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003260 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003261
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003262 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003263 Py_DECREF(func);
3264 Py_DECREF(callargs);
3265 Py_XDECREF(kwargs);
3266
3267 SET_TOP(result);
3268 if (result == NULL) {
3269 goto error;
3270 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003271 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003272 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003273
Benjamin Petersonddd19492018-09-16 22:38:02 -07003274 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003275 PyObject *qualname = POP();
3276 PyObject *codeobj = POP();
3277 PyFunctionObject *func = (PyFunctionObject *)
3278 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003279
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003280 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003281 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003282 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003283 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003284 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003285
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003286 if (oparg & 0x08) {
3287 assert(PyTuple_CheckExact(TOP()));
3288 func ->func_closure = POP();
3289 }
3290 if (oparg & 0x04) {
3291 assert(PyDict_CheckExact(TOP()));
3292 func->func_annotations = POP();
3293 }
3294 if (oparg & 0x02) {
3295 assert(PyDict_CheckExact(TOP()));
3296 func->func_kwdefaults = POP();
3297 }
3298 if (oparg & 0x01) {
3299 assert(PyTuple_CheckExact(TOP()));
3300 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003301 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003302
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003303 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003304 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003305 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003306
Benjamin Petersonddd19492018-09-16 22:38:02 -07003307 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003308 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003309 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003310 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003311 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003312 step = NULL;
3313 stop = POP();
3314 start = TOP();
3315 slice = PySlice_New(start, stop, step);
3316 Py_DECREF(start);
3317 Py_DECREF(stop);
3318 Py_XDECREF(step);
3319 SET_TOP(slice);
3320 if (slice == NULL)
3321 goto error;
3322 DISPATCH();
3323 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003324
Benjamin Petersonddd19492018-09-16 22:38:02 -07003325 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003326 /* Handles f-string value formatting. */
3327 PyObject *result;
3328 PyObject *fmt_spec;
3329 PyObject *value;
3330 PyObject *(*conv_fn)(PyObject *);
3331 int which_conversion = oparg & FVC_MASK;
3332 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3333
3334 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003335 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003336
3337 /* See if any conversion is specified. */
3338 switch (which_conversion) {
3339 case FVC_STR: conv_fn = PyObject_Str; break;
3340 case FVC_REPR: conv_fn = PyObject_Repr; break;
3341 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3342
3343 /* Must be 0 (meaning no conversion), since only four
3344 values are allowed by (oparg & FVC_MASK). */
3345 default: conv_fn = NULL; break;
3346 }
3347
3348 /* If there's a conversion function, call it and replace
3349 value with that result. Otherwise, just use value,
3350 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003351 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003352 result = conv_fn(value);
3353 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003354 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003355 Py_XDECREF(fmt_spec);
3356 goto error;
3357 }
3358 value = result;
3359 }
3360
3361 /* If value is a unicode object, and there's no fmt_spec,
3362 then we know the result of format(value) is value
3363 itself. In that case, skip calling format(). I plan to
3364 move this optimization in to PyObject_Format()
3365 itself. */
3366 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3367 /* Do nothing, just transfer ownership to result. */
3368 result = value;
3369 } else {
3370 /* Actually call format(). */
3371 result = PyObject_Format(value, fmt_spec);
3372 Py_DECREF(value);
3373 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003374 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003375 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003376 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003377 }
3378
Eric V. Smith135d5f42016-02-05 18:23:08 -05003379 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003380 DISPATCH();
3381 }
3382
Benjamin Petersonddd19492018-09-16 22:38:02 -07003383 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003384 int oldoparg = oparg;
3385 NEXTOPARG();
3386 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003387 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003388 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003389
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003390
Antoine Pitrou042b1282010-08-13 21:15:58 +00003391#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003392 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003393#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003394 default:
3395 fprintf(stderr,
3396 "XXX lineno: %d, opcode: %d\n",
3397 PyFrame_GetLineNumber(f),
3398 opcode);
3399 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003400 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003402 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003403
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003404 /* This should never be reached. Every opcode should end with DISPATCH()
3405 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003406 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003407
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003408error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003409 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003410#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003411 if (!PyErr_Occurred())
3412 PyErr_SetString(PyExc_SystemError,
3413 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003414#else
3415 assert(PyErr_Occurred());
3416#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003417
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003418 /* Log traceback info. */
3419 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003420
Benjamin Peterson51f46162013-01-23 08:38:47 -05003421 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003422 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3423 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003424
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003425exception_unwind:
3426 /* Unwind stacks if an exception occurred */
3427 while (f->f_iblock > 0) {
3428 /* Pop the current block. */
3429 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003431 if (b->b_type == EXCEPT_HANDLER) {
3432 UNWIND_EXCEPT_HANDLER(b);
3433 continue;
3434 }
3435 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003436 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003437 PyObject *exc, *val, *tb;
3438 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003439 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003440 /* Beware, this invalidates all b->b_* fields */
3441 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003442 PUSH(exc_info->exc_traceback);
3443 PUSH(exc_info->exc_value);
3444 if (exc_info->exc_type != NULL) {
3445 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003446 }
3447 else {
3448 Py_INCREF(Py_None);
3449 PUSH(Py_None);
3450 }
3451 PyErr_Fetch(&exc, &val, &tb);
3452 /* Make the raw exception data
3453 available to the handler,
3454 so a program can emulate the
3455 Python main loop. */
3456 PyErr_NormalizeException(
3457 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003458 if (tb != NULL)
3459 PyException_SetTraceback(val, tb);
3460 else
3461 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003462 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003463 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003464 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003465 exc_info->exc_value = val;
3466 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003467 if (tb == NULL)
3468 tb = Py_None;
3469 Py_INCREF(tb);
3470 PUSH(tb);
3471 PUSH(val);
3472 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003473 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003474 /* Resume normal execution */
3475 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003476 }
3477 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003478
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003479 /* End the loop as we still have an error */
3480 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003481 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003483 /* Pop remaining stack entries. */
3484 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003485 PyObject *o = POP();
3486 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003487 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003488
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003489 assert(retval == NULL);
3490 assert(PyErr_Occurred());
Guido van Rossumac7be682001-01-17 15:42:30 +00003491
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003492return_or_yield:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003493 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003494 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003495 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3496 tstate, f, PyTrace_RETURN, retval)) {
3497 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003498 }
3499 }
3500 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003501 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3502 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003503 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003504 }
3505 }
3506 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003508 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003509exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003510 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3511 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003512 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003513 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003514 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003515
Victor Stinnerefde1462015-03-21 15:04:43 +01003516 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003517}
3518
Benjamin Petersonb204a422011-06-05 22:04:07 -05003519static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003520format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3521{
3522 int err;
3523 Py_ssize_t len = PyList_GET_SIZE(names);
3524 PyObject *name_str, *comma, *tail, *tmp;
3525
3526 assert(PyList_CheckExact(names));
3527 assert(len >= 1);
3528 /* Deal with the joys of natural language. */
3529 switch (len) {
3530 case 1:
3531 name_str = PyList_GET_ITEM(names, 0);
3532 Py_INCREF(name_str);
3533 break;
3534 case 2:
3535 name_str = PyUnicode_FromFormat("%U and %U",
3536 PyList_GET_ITEM(names, len - 2),
3537 PyList_GET_ITEM(names, len - 1));
3538 break;
3539 default:
3540 tail = PyUnicode_FromFormat(", %U, and %U",
3541 PyList_GET_ITEM(names, len - 2),
3542 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003543 if (tail == NULL)
3544 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003545 /* Chop off the last two objects in the list. This shouldn't actually
3546 fail, but we can't be too careful. */
3547 err = PyList_SetSlice(names, len - 2, len, NULL);
3548 if (err == -1) {
3549 Py_DECREF(tail);
3550 return;
3551 }
3552 /* Stitch everything up into a nice comma-separated list. */
3553 comma = PyUnicode_FromString(", ");
3554 if (comma == NULL) {
3555 Py_DECREF(tail);
3556 return;
3557 }
3558 tmp = PyUnicode_Join(comma, names);
3559 Py_DECREF(comma);
3560 if (tmp == NULL) {
3561 Py_DECREF(tail);
3562 return;
3563 }
3564 name_str = PyUnicode_Concat(tmp, tail);
3565 Py_DECREF(tmp);
3566 Py_DECREF(tail);
3567 break;
3568 }
3569 if (name_str == NULL)
3570 return;
3571 PyErr_Format(PyExc_TypeError,
3572 "%U() missing %i required %s argument%s: %U",
3573 co->co_name,
3574 len,
3575 kind,
3576 len == 1 ? "" : "s",
3577 name_str);
3578 Py_DECREF(name_str);
3579}
3580
3581static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003582missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003583 PyObject **fastlocals)
3584{
Victor Stinner74319ae2016-08-25 00:04:09 +02003585 Py_ssize_t i, j = 0;
3586 Py_ssize_t start, end;
3587 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003588 const char *kind = positional ? "positional" : "keyword-only";
3589 PyObject *missing_names;
3590
3591 /* Compute the names of the arguments that are missing. */
3592 missing_names = PyList_New(missing);
3593 if (missing_names == NULL)
3594 return;
3595 if (positional) {
3596 start = 0;
3597 end = co->co_argcount - defcount;
3598 }
3599 else {
3600 start = co->co_argcount;
3601 end = start + co->co_kwonlyargcount;
3602 }
3603 for (i = start; i < end; i++) {
3604 if (GETLOCAL(i) == NULL) {
3605 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3606 PyObject *name = PyObject_Repr(raw);
3607 if (name == NULL) {
3608 Py_DECREF(missing_names);
3609 return;
3610 }
3611 PyList_SET_ITEM(missing_names, j++, name);
3612 }
3613 }
3614 assert(j == missing);
3615 format_missing(kind, co, missing_names);
3616 Py_DECREF(missing_names);
3617}
3618
3619static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003620too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3621 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003622{
3623 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003624 Py_ssize_t kwonly_given = 0;
3625 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003626 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003627 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003628
Benjamin Petersone109c702011-06-24 09:37:26 -05003629 assert((co->co_flags & CO_VARARGS) == 0);
3630 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003631 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3632 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003633 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003634 }
3635 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003636 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003637 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003638 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003639 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003640 }
3641 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003642 plural = (co_argcount != 1);
3643 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003644 }
3645 if (sig == NULL)
3646 return;
3647 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003648 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3649 kwonly_sig = PyUnicode_FromFormat(format,
3650 given != 1 ? "s" : "",
3651 kwonly_given,
3652 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003653 if (kwonly_sig == NULL) {
3654 Py_DECREF(sig);
3655 return;
3656 }
3657 }
3658 else {
3659 /* This will not fail. */
3660 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003661 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003662 }
3663 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003664 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003665 co->co_name,
3666 sig,
3667 plural ? "s" : "",
3668 given,
3669 kwonly_sig,
3670 given == 1 && !kwonly_given ? "was" : "were");
3671 Py_DECREF(sig);
3672 Py_DECREF(kwonly_sig);
3673}
3674
Guido van Rossumc2e20742006-02-27 22:32:47 +00003675/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003676 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003677 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003678
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003679PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003680_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003681 PyObject *const *args, Py_ssize_t argcount,
3682 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003683 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003684 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003685 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003686 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003687{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003688 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003689 PyFrameObject *f;
3690 PyObject *retval = NULL;
3691 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003692 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003693 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003694 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3695 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003696 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003698 if (globals == NULL) {
3699 PyErr_SetString(PyExc_SystemError,
3700 "PyEval_EvalCodeEx: NULL globals");
3701 return NULL;
3702 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003703
Victor Stinnerc7020012016-08-16 23:40:29 +02003704 /* Create the frame */
Victor Stinner50b48572018-11-01 01:51:40 +01003705 tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003706 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003707 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003708 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003709 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003710 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003711 fastlocals = f->f_localsplus;
3712 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003713
Victor Stinnerc7020012016-08-16 23:40:29 +02003714 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003715 if (co->co_flags & CO_VARKEYWORDS) {
3716 kwdict = PyDict_New();
3717 if (kwdict == NULL)
3718 goto fail;
3719 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003720 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003721 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003722 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003723 SETLOCAL(i, kwdict);
3724 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003725 else {
3726 kwdict = NULL;
3727 }
3728
3729 /* Copy positional arguments into local variables */
3730 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003731 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003732 }
3733 else {
3734 n = argcount;
3735 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003736 for (i = 0; i < n; i++) {
3737 x = args[i];
3738 Py_INCREF(x);
3739 SETLOCAL(i, x);
3740 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003741
3742 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003743 if (co->co_flags & CO_VARARGS) {
3744 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003745 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003746 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003747 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003748 SETLOCAL(total_args, u);
3749 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003750 x = args[i];
3751 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003752 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003753 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003754 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003755
Serhiy Storchakab7281052016-09-12 00:52:40 +03003756 /* Handle keyword arguments passed as two strided arrays */
3757 kwcount *= kwstep;
3758 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003759 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003760 PyObject *keyword = kwnames[i];
3761 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003762 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003763
Benjamin Petersonb204a422011-06-05 22:04:07 -05003764 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3765 PyErr_Format(PyExc_TypeError,
3766 "%U() keywords must be strings",
3767 co->co_name);
3768 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003769 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003770
Benjamin Petersonb204a422011-06-05 22:04:07 -05003771 /* Speed hack: do raw pointer compares. As names are
3772 normally interned this should almost always hit. */
3773 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3774 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003775 PyObject *name = co_varnames[j];
3776 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003777 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003778 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003779 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003780
Benjamin Petersonb204a422011-06-05 22:04:07 -05003781 /* Slow fallback, just in case */
3782 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003783 PyObject *name = co_varnames[j];
3784 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3785 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003786 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003787 }
3788 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003789 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003790 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003791 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003792
Victor Stinner231d1f32017-01-11 02:12:06 +01003793 assert(j >= total_args);
3794 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003795 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003796 "%U() got an unexpected keyword argument '%S'",
3797 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003798 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003799 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003800
Christian Heimes0bd447f2013-07-20 14:48:10 +02003801 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3802 goto fail;
3803 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003804 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003805
Benjamin Petersonb204a422011-06-05 22:04:07 -05003806 kw_found:
3807 if (GETLOCAL(j) != NULL) {
3808 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003809 "%U() got multiple values for argument '%S'",
3810 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003811 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003812 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003813 Py_INCREF(value);
3814 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003815 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003816
3817 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003818 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003819 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003820 goto fail;
3821 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003822
3823 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003824 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02003825 Py_ssize_t m = co->co_argcount - defcount;
3826 Py_ssize_t missing = 0;
3827 for (i = argcount; i < m; i++) {
3828 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003829 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02003830 }
3831 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003832 if (missing) {
3833 missing_arguments(co, missing, defcount, fastlocals);
3834 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003835 }
3836 if (n > m)
3837 i = n - m;
3838 else
3839 i = 0;
3840 for (; i < defcount; i++) {
3841 if (GETLOCAL(m+i) == NULL) {
3842 PyObject *def = defs[i];
3843 Py_INCREF(def);
3844 SETLOCAL(m+i, def);
3845 }
3846 }
3847 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003848
3849 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003850 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02003851 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003852 for (i = co->co_argcount; i < total_args; i++) {
3853 PyObject *name;
3854 if (GETLOCAL(i) != NULL)
3855 continue;
3856 name = PyTuple_GET_ITEM(co->co_varnames, i);
3857 if (kwdefs != NULL) {
3858 PyObject *def = PyDict_GetItem(kwdefs, name);
3859 if (def) {
3860 Py_INCREF(def);
3861 SETLOCAL(i, def);
3862 continue;
3863 }
3864 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003865 missing++;
3866 }
3867 if (missing) {
3868 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003869 goto fail;
3870 }
3871 }
3872
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003873 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003874 vars into frame. */
3875 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003876 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02003877 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05003878 /* Possibly account for the cell variable being an argument. */
3879 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003880 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003881 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003882 /* Clear the local copy. */
3883 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003884 }
3885 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003886 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003887 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003888 if (c == NULL)
3889 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003890 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003891 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003892
3893 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05003894 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3895 PyObject *o = PyTuple_GET_ITEM(closure, i);
3896 Py_INCREF(o);
3897 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003898 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003899
Yury Selivanoveb636452016-09-08 22:01:51 -07003900 /* Handle generator/coroutine/asynchronous generator */
3901 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003902 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04003903 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003904 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04003905
3906 if (is_coro && tstate->in_coroutine_wrapper) {
3907 assert(coro_wrapper != NULL);
3908 PyErr_Format(PyExc_RuntimeError,
3909 "coroutine wrapper %.200R attempted "
3910 "to recursively wrap %.200R",
3911 coro_wrapper,
3912 co);
3913 goto fail;
3914 }
Yury Selivanov75445082015-05-11 22:57:16 -04003915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003916 /* Don't need to keep the reference to f_back, it will be set
3917 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003918 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003920 /* Create a new generator that owns the ready to run frame
3921 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04003922 if (is_coro) {
3923 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07003924 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
3925 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003926 } else {
3927 gen = PyGen_NewWithQualName(f, name, qualname);
3928 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003929 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003930 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003931 }
INADA Naoki9c157762016-12-26 18:52:46 +09003932
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003933 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04003934
Yury Selivanov94c22632015-06-04 10:16:51 -04003935 if (is_coro && coro_wrapper != NULL) {
3936 PyObject *wrapped;
3937 tstate->in_coroutine_wrapper = 1;
3938 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
3939 tstate->in_coroutine_wrapper = 0;
3940 return wrapped;
3941 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04003942
Yury Selivanov75445082015-05-11 22:57:16 -04003943 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003944 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003945
Victor Stinner59a73272016-12-09 18:51:13 +01003946 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003947
Thomas Woutersce272b62007-09-19 21:19:28 +00003948fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003950 /* decref'ing the frame can cause __del__ methods to get invoked,
3951 which can call back into Python. While we're done with the
3952 current Python frame (f), the associated C stack is still in use,
3953 so recursion_depth must be boosted for the duration.
3954 */
3955 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003956 if (Py_REFCNT(f) > 1) {
3957 Py_DECREF(f);
3958 _PyObject_GC_TRACK(f);
3959 }
3960 else {
3961 ++tstate->recursion_depth;
3962 Py_DECREF(f);
3963 --tstate->recursion_depth;
3964 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003965 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003966}
3967
Victor Stinner40ee3012014-06-16 15:59:28 +02003968PyObject *
3969PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003970 PyObject *const *args, int argcount,
3971 PyObject *const *kws, int kwcount,
3972 PyObject *const *defs, int defcount,
3973 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02003974{
3975 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02003976 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06003977 kws, kws != NULL ? kws + 1 : NULL,
3978 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02003979 defs, defcount,
3980 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003981 NULL, NULL);
3982}
Tim Peters5ca576e2001-06-18 22:08:13 +00003983
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003984static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05003985special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003986{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003987 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05003988 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003989 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003990 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003991 return NULL;
3992 }
3993 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003994}
3995
3996
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003997/* Logic for the raise statement (too complicated for inlining).
3998 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003999static int
Collin Winter828f04a2007-08-31 00:04:24 +00004000do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004001{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004002 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004004 if (exc == NULL) {
4005 /* Reraise */
Victor Stinner50b48572018-11-01 01:51:40 +01004006 PyThreadState *tstate = _PyThreadState_GET();
Mark Shannonae3087c2017-10-22 22:41:51 +01004007 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004008 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004009 type = exc_info->exc_type;
4010 value = exc_info->exc_value;
4011 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004012 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004013 PyErr_SetString(PyExc_RuntimeError,
4014 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004015 return 0;
4016 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004017 Py_XINCREF(type);
4018 Py_XINCREF(value);
4019 Py_XINCREF(tb);
4020 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004021 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004022 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004024 /* We support the following forms of raise:
4025 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004026 raise <instance>
4027 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004029 if (PyExceptionClass_Check(exc)) {
4030 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004031 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004032 if (value == NULL)
4033 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004034 if (!PyExceptionInstance_Check(value)) {
4035 PyErr_Format(PyExc_TypeError,
4036 "calling %R should have returned an instance of "
4037 "BaseException, not %R",
4038 type, Py_TYPE(value));
4039 goto raise_error;
4040 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004041 }
4042 else if (PyExceptionInstance_Check(exc)) {
4043 value = exc;
4044 type = PyExceptionInstance_Class(exc);
4045 Py_INCREF(type);
4046 }
4047 else {
4048 /* Not something you can raise. You get an exception
4049 anyway, just not what you specified :-) */
4050 Py_DECREF(exc);
4051 PyErr_SetString(PyExc_TypeError,
4052 "exceptions must derive from BaseException");
4053 goto raise_error;
4054 }
Collin Winter828f04a2007-08-31 00:04:24 +00004055
Serhiy Storchakac0191582016-09-27 11:37:10 +03004056 assert(type != NULL);
4057 assert(value != NULL);
4058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004059 if (cause) {
4060 PyObject *fixed_cause;
4061 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004062 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004063 if (fixed_cause == NULL)
4064 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004065 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004066 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004067 else if (PyExceptionInstance_Check(cause)) {
4068 fixed_cause = cause;
4069 }
4070 else if (cause == Py_None) {
4071 Py_DECREF(cause);
4072 fixed_cause = NULL;
4073 }
4074 else {
4075 PyErr_SetString(PyExc_TypeError,
4076 "exception causes must derive from "
4077 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004078 goto raise_error;
4079 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004080 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004081 }
Collin Winter828f04a2007-08-31 00:04:24 +00004082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004083 PyErr_SetObject(type, value);
4084 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004085 Py_DECREF(value);
4086 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004087 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004088
4089raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004090 Py_XDECREF(value);
4091 Py_XDECREF(type);
4092 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004093 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004094}
4095
Tim Petersd6d010b2001-06-21 02:49:55 +00004096/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004097 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004098
Guido van Rossum0368b722007-05-11 16:50:42 +00004099 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4100 with a variable target.
4101*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004102
Barry Warsawe42b18f1997-08-25 22:13:04 +00004103static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004104unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004105{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004106 int i = 0, j = 0;
4107 Py_ssize_t ll = 0;
4108 PyObject *it; /* iter(v) */
4109 PyObject *w;
4110 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004112 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004114 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004115 if (it == NULL) {
4116 if (PyErr_ExceptionMatches(PyExc_TypeError) &&
4117 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4118 {
4119 PyErr_Format(PyExc_TypeError,
4120 "cannot unpack non-iterable %.200s object",
4121 v->ob_type->tp_name);
4122 }
4123 return 0;
4124 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004126 for (; i < argcnt; i++) {
4127 w = PyIter_Next(it);
4128 if (w == NULL) {
4129 /* Iterator done, via error or exhaustion. */
4130 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004131 if (argcntafter == -1) {
4132 PyErr_Format(PyExc_ValueError,
4133 "not enough values to unpack (expected %d, got %d)",
4134 argcnt, i);
4135 }
4136 else {
4137 PyErr_Format(PyExc_ValueError,
4138 "not enough values to unpack "
4139 "(expected at least %d, got %d)",
4140 argcnt + argcntafter, i);
4141 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004142 }
4143 goto Error;
4144 }
4145 *--sp = w;
4146 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004148 if (argcntafter == -1) {
4149 /* We better have exhausted the iterator now. */
4150 w = PyIter_Next(it);
4151 if (w == NULL) {
4152 if (PyErr_Occurred())
4153 goto Error;
4154 Py_DECREF(it);
4155 return 1;
4156 }
4157 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004158 PyErr_Format(PyExc_ValueError,
4159 "too many values to unpack (expected %d)",
4160 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004161 goto Error;
4162 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004164 l = PySequence_List(it);
4165 if (l == NULL)
4166 goto Error;
4167 *--sp = l;
4168 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004170 ll = PyList_GET_SIZE(l);
4171 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004172 PyErr_Format(PyExc_ValueError,
4173 "not enough values to unpack (expected at least %d, got %zd)",
4174 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004175 goto Error;
4176 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004178 /* Pop the "after-variable" args off the list. */
4179 for (j = argcntafter; j > 0; j--, i++) {
4180 *--sp = PyList_GET_ITEM(l, ll - j);
4181 }
4182 /* Resize the list. */
4183 Py_SIZE(l) = ll - argcntafter;
4184 Py_DECREF(it);
4185 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004186
Tim Petersd6d010b2001-06-21 02:49:55 +00004187Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004188 for (; i > 0; i--, sp++)
4189 Py_DECREF(*sp);
4190 Py_XDECREF(it);
4191 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004192}
4193
4194
Guido van Rossum96a42c81992-01-12 02:29:51 +00004195#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004196static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004197prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004198{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004199 printf("%s ", str);
4200 if (PyObject_Print(v, stdout, 0) != 0)
4201 PyErr_Clear(); /* Don't know what else to do */
4202 printf("\n");
4203 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004204}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004205#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004206
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004207static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004208call_exc_trace(Py_tracefunc func, PyObject *self,
4209 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004210{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004211 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004212 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004213 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004214 if (value == NULL) {
4215 value = Py_None;
4216 Py_INCREF(value);
4217 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004218 PyErr_NormalizeException(&type, &value, &orig_traceback);
4219 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004220 arg = PyTuple_Pack(3, type, value, traceback);
4221 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004222 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004223 return;
4224 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004225 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004226 Py_DECREF(arg);
4227 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004228 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004229 else {
4230 Py_XDECREF(type);
4231 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004232 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004233 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004234}
4235
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004236static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004237call_trace_protected(Py_tracefunc func, PyObject *obj,
4238 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004239 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004240{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004241 PyObject *type, *value, *traceback;
4242 int err;
4243 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004244 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004245 if (err == 0)
4246 {
4247 PyErr_Restore(type, value, traceback);
4248 return 0;
4249 }
4250 else {
4251 Py_XDECREF(type);
4252 Py_XDECREF(value);
4253 Py_XDECREF(traceback);
4254 return -1;
4255 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004256}
4257
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004258static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004259call_trace(Py_tracefunc func, PyObject *obj,
4260 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004261 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004262{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004263 int result;
4264 if (tstate->tracing)
4265 return 0;
4266 tstate->tracing++;
4267 tstate->use_tracing = 0;
4268 result = func(obj, frame, what, arg);
4269 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4270 || (tstate->c_profilefunc != NULL));
4271 tstate->tracing--;
4272 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004273}
4274
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004275PyObject *
4276_PyEval_CallTracing(PyObject *func, PyObject *args)
4277{
Victor Stinner50b48572018-11-01 01:51:40 +01004278 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004279 int save_tracing = tstate->tracing;
4280 int save_use_tracing = tstate->use_tracing;
4281 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004283 tstate->tracing = 0;
4284 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4285 || (tstate->c_profilefunc != NULL));
4286 result = PyObject_Call(func, args, NULL);
4287 tstate->tracing = save_tracing;
4288 tstate->use_tracing = save_use_tracing;
4289 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004290}
4291
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004292/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004293static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004294maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004295 PyThreadState *tstate, PyFrameObject *frame,
4296 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004298 int result = 0;
4299 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004301 /* If the last instruction executed isn't in the current
4302 instruction window, reset the window.
4303 */
4304 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4305 PyAddrPair bounds;
4306 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4307 &bounds);
4308 *instr_lb = bounds.ap_lower;
4309 *instr_ub = bounds.ap_upper;
4310 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004311 /* If the last instruction falls at the start of a line or if it
4312 represents a jump backwards, update the frame's line number and
4313 then call the trace function if we're tracing source lines.
4314 */
4315 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004316 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004317 if (frame->f_trace_lines) {
4318 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4319 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004320 }
George King20faa682017-10-18 17:44:22 -07004321 /* Always emit an opcode event if we're tracing all opcodes. */
4322 if (frame->f_trace_opcodes) {
4323 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4324 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004325 *instr_prev = frame->f_lasti;
4326 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004327}
4328
Fred Drake5755ce62001-06-27 19:19:46 +00004329void
4330PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004331{
Victor Stinner50b48572018-11-01 01:51:40 +01004332 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004333 PyObject *temp = tstate->c_profileobj;
4334 Py_XINCREF(arg);
4335 tstate->c_profilefunc = NULL;
4336 tstate->c_profileobj = NULL;
4337 /* Must make sure that tracing is not ignored if 'temp' is freed */
4338 tstate->use_tracing = tstate->c_tracefunc != NULL;
4339 Py_XDECREF(temp);
4340 tstate->c_profilefunc = func;
4341 tstate->c_profileobj = arg;
4342 /* Flag that tracing or profiling is turned on */
4343 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004344}
4345
4346void
4347PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4348{
Victor Stinner50b48572018-11-01 01:51:40 +01004349 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004350 PyObject *temp = tstate->c_traceobj;
4351 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4352 Py_XINCREF(arg);
4353 tstate->c_tracefunc = NULL;
4354 tstate->c_traceobj = NULL;
4355 /* Must make sure that profiling is not ignored if 'temp' is freed */
4356 tstate->use_tracing = tstate->c_profilefunc != NULL;
4357 Py_XDECREF(temp);
4358 tstate->c_tracefunc = func;
4359 tstate->c_traceobj = arg;
4360 /* Flag that tracing or profiling is turned on */
4361 tstate->use_tracing = ((func != NULL)
4362 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004363}
4364
Yury Selivanov75445082015-05-11 22:57:16 -04004365void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004366_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4367{
4368 assert(new_depth >= 0);
Victor Stinner50b48572018-11-01 01:51:40 +01004369 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004370 tstate->coroutine_origin_tracking_depth = new_depth;
4371}
4372
4373int
4374_PyEval_GetCoroutineOriginTrackingDepth(void)
4375{
Victor Stinner50b48572018-11-01 01:51:40 +01004376 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004377 return tstate->coroutine_origin_tracking_depth;
4378}
4379
4380void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004381_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004382{
Victor Stinner50b48572018-11-01 01:51:40 +01004383 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004384
Yury Selivanov75445082015-05-11 22:57:16 -04004385 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004386 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004387}
4388
4389PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004390_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004391{
Victor Stinner50b48572018-11-01 01:51:40 +01004392 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004393 return tstate->coroutine_wrapper;
4394}
4395
Yury Selivanoveb636452016-09-08 22:01:51 -07004396void
4397_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4398{
Victor Stinner50b48572018-11-01 01:51:40 +01004399 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004400
4401 Py_XINCREF(firstiter);
4402 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4403}
4404
4405PyObject *
4406_PyEval_GetAsyncGenFirstiter(void)
4407{
Victor Stinner50b48572018-11-01 01:51:40 +01004408 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004409 return tstate->async_gen_firstiter;
4410}
4411
4412void
4413_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4414{
Victor Stinner50b48572018-11-01 01:51:40 +01004415 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004416
4417 Py_XINCREF(finalizer);
4418 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4419}
4420
4421PyObject *
4422_PyEval_GetAsyncGenFinalizer(void)
4423{
Victor Stinner50b48572018-11-01 01:51:40 +01004424 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004425 return tstate->async_gen_finalizer;
4426}
4427
Guido van Rossumb209a111997-04-29 18:18:01 +00004428PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004429PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004430{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004431 PyFrameObject *current_frame = PyEval_GetFrame();
4432 if (current_frame == NULL)
Victor Stinnercaba55b2018-08-03 15:33:52 +02004433 return _PyInterpreterState_GET_UNSAFE()->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004434 else
4435 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004436}
4437
Guido van Rossumb209a111997-04-29 18:18:01 +00004438PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004439PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004441 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004442 if (current_frame == NULL) {
4443 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004444 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004445 }
4446
4447 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4448 return NULL;
4449
4450 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004451 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004452}
4453
Guido van Rossumb209a111997-04-29 18:18:01 +00004454PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004455PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004456{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004457 PyFrameObject *current_frame = PyEval_GetFrame();
4458 if (current_frame == NULL)
4459 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004460
4461 assert(current_frame->f_globals != NULL);
4462 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004463}
4464
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004465PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004466PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004467{
Victor Stinner50b48572018-11-01 01:51:40 +01004468 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004469 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004470}
4471
Guido van Rossum6135a871995-01-09 17:53:26 +00004472int
Tim Peters5ba58662001-07-16 02:29:45 +00004473PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004474{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004475 PyFrameObject *current_frame = PyEval_GetFrame();
4476 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004478 if (current_frame != NULL) {
4479 const int codeflags = current_frame->f_code->co_flags;
4480 const int compilerflags = codeflags & PyCF_MASK;
4481 if (compilerflags) {
4482 result = 1;
4483 cf->cf_flags |= compilerflags;
4484 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004485#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004486 if (codeflags & CO_GENERATOR_ALLOWED) {
4487 result = 1;
4488 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4489 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004490#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004491 }
4492 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004493}
4494
Guido van Rossum3f5da241990-12-20 15:06:42 +00004495
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004496const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004497PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004499 if (PyMethod_Check(func))
4500 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4501 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004502 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004503 else if (PyCFunction_Check(func))
4504 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4505 else
4506 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004507}
4508
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004509const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004510PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004511{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004512 if (PyMethod_Check(func))
4513 return "()";
4514 else if (PyFunction_Check(func))
4515 return "()";
4516 else if (PyCFunction_Check(func))
4517 return "()";
4518 else
4519 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004520}
4521
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004522#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004523if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004524 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4525 tstate, tstate->frame, \
4526 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004527 x = NULL; \
4528 } \
4529 else { \
4530 x = call; \
4531 if (tstate->c_profilefunc != NULL) { \
4532 if (x == NULL) { \
4533 call_trace_protected(tstate->c_profilefunc, \
4534 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004535 tstate, tstate->frame, \
4536 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004537 /* XXX should pass (type, value, tb) */ \
4538 } else { \
4539 if (call_trace(tstate->c_profilefunc, \
4540 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004541 tstate, tstate->frame, \
4542 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004543 Py_DECREF(x); \
4544 x = NULL; \
4545 } \
4546 } \
4547 } \
4548 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004549} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004550 x = call; \
4551 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004552
Victor Stinner415c5102017-01-11 00:54:57 +01004553/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4554 to reduce the stack consumption. */
4555Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004556call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004557{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004558 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004559 PyObject *func = *pfunc;
4560 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004561 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4562 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004563 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004565 /* Always dispatch PyCFunction first, because these are
4566 presumed to be the most frequent callable object.
4567 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004568 if (PyCFunction_Check(func)) {
Victor Stinner50b48572018-11-01 01:51:40 +01004569 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004570 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004571 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004572 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
Victor Stinner50b48572018-11-01 01:51:40 +01004573 PyThreadState *tstate = _PyThreadState_GET();
jdemeyer56868f92018-07-21 10:30:59 +02004574 if (nargs > 0 && tstate->use_tracing) {
4575 /* We need to create a temporary bound method as argument
4576 for profiling.
4577
4578 If nargs == 0, then this cannot work because we have no
4579 "self". In any case, the call itself would raise
4580 TypeError (foo needs an argument), so we just skip
4581 profiling. */
4582 PyObject *self = stack[0];
4583 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
jdemeyer147d9552018-07-23 18:41:20 +02004584 if (func != NULL) {
4585 C_TRACE(x, _PyCFunction_FastCallKeywords(func,
4586 stack+1, nargs-1,
4587 kwnames));
4588 Py_DECREF(func);
INADA Naoki93fac8d2017-03-07 14:24:37 +09004589 }
jdemeyer147d9552018-07-23 18:41:20 +02004590 else {
4591 x = NULL;
4592 }
INADA Naoki93fac8d2017-03-07 14:24:37 +09004593 }
4594 else {
4595 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4596 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004597 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004598 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004599 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004600 /* Optimize access to bound methods. Reuse the Python stack
4601 to pass 'self' as the first argument, replace 'func'
4602 with 'self'. It avoids the creation of a new temporary tuple
4603 for arguments (to replace func with self) when the method uses
4604 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004605 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004606 Py_INCREF(self);
4607 func = PyMethod_GET_FUNCTION(func);
4608 Py_INCREF(func);
4609 Py_SETREF(*pfunc, self);
4610 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004611 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004612 }
4613 else {
4614 Py_INCREF(func);
4615 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004616
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004617 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004618 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004619 }
4620 else {
4621 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4622 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004623 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004624 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004625
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004626 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4627
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004628 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004629 while ((*pp_stack) > pfunc) {
4630 w = EXT_POP(*pp_stack);
4631 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004632 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004634 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004635}
4636
Jeremy Hylton52820442001-01-03 23:52:36 +00004637static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004638do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004639{
jdemeyere89de732018-09-19 12:06:20 +02004640 PyObject *result;
4641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004642 if (PyCFunction_Check(func)) {
Victor Stinner50b48572018-11-01 01:51:40 +01004643 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004644 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004645 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004646 }
jdemeyere89de732018-09-19 12:06:20 +02004647 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
Victor Stinner50b48572018-11-01 01:51:40 +01004648 PyThreadState *tstate = _PyThreadState_GET();
jdemeyere89de732018-09-19 12:06:20 +02004649 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4650 if (nargs > 0 && tstate->use_tracing) {
4651 /* We need to create a temporary bound method as argument
4652 for profiling.
4653
4654 If nargs == 0, then this cannot work because we have no
4655 "self". In any case, the call itself would raise
4656 TypeError (foo needs an argument), so we just skip
4657 profiling. */
4658 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4659 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4660 if (func == NULL) {
4661 return NULL;
4662 }
4663
4664 C_TRACE(result, _PyCFunction_FastCallDict(func,
4665 &PyTuple_GET_ITEM(callargs, 1),
4666 nargs - 1,
4667 kwdict));
4668 Py_DECREF(func);
4669 return result;
4670 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004671 }
jdemeyere89de732018-09-19 12:06:20 +02004672 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004673}
4674
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004675/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004676 nb_index slot defined, and store in *pi.
4677 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004678 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004679 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004680*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004681int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004682_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004683{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004684 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004685 Py_ssize_t x;
4686 if (PyIndex_Check(v)) {
4687 x = PyNumber_AsSsize_t(v, NULL);
4688 if (x == -1 && PyErr_Occurred())
4689 return 0;
4690 }
4691 else {
4692 PyErr_SetString(PyExc_TypeError,
4693 "slice indices must be integers or "
4694 "None or have an __index__ method");
4695 return 0;
4696 }
4697 *pi = x;
4698 }
4699 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004700}
4701
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004702int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004703_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004704{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004705 Py_ssize_t x;
4706 if (PyIndex_Check(v)) {
4707 x = PyNumber_AsSsize_t(v, NULL);
4708 if (x == -1 && PyErr_Occurred())
4709 return 0;
4710 }
4711 else {
4712 PyErr_SetString(PyExc_TypeError,
4713 "slice indices must be integers or "
4714 "have an __index__ method");
4715 return 0;
4716 }
4717 *pi = x;
4718 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004719}
4720
4721
Guido van Rossum486364b2007-06-30 05:01:58 +00004722#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004723 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004724
Guido van Rossumb209a111997-04-29 18:18:01 +00004725static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004726cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004727{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004728 int res = 0;
4729 switch (op) {
4730 case PyCmp_IS:
4731 res = (v == w);
4732 break;
4733 case PyCmp_IS_NOT:
4734 res = (v != w);
4735 break;
4736 case PyCmp_IN:
4737 res = PySequence_Contains(w, v);
4738 if (res < 0)
4739 return NULL;
4740 break;
4741 case PyCmp_NOT_IN:
4742 res = PySequence_Contains(w, v);
4743 if (res < 0)
4744 return NULL;
4745 res = !res;
4746 break;
4747 case PyCmp_EXC_MATCH:
4748 if (PyTuple_Check(w)) {
4749 Py_ssize_t i, length;
4750 length = PyTuple_Size(w);
4751 for (i = 0; i < length; i += 1) {
4752 PyObject *exc = PyTuple_GET_ITEM(w, i);
4753 if (!PyExceptionClass_Check(exc)) {
4754 PyErr_SetString(PyExc_TypeError,
4755 CANNOT_CATCH_MSG);
4756 return NULL;
4757 }
4758 }
4759 }
4760 else {
4761 if (!PyExceptionClass_Check(w)) {
4762 PyErr_SetString(PyExc_TypeError,
4763 CANNOT_CATCH_MSG);
4764 return NULL;
4765 }
4766 }
4767 res = PyErr_GivenExceptionMatches(v, w);
4768 break;
4769 default:
4770 return PyObject_RichCompare(v, w, op);
4771 }
4772 v = res ? Py_True : Py_False;
4773 Py_INCREF(v);
4774 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004775}
4776
Thomas Wouters52152252000-08-17 22:55:00 +00004777static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004778import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4779{
4780 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004781 PyObject *import_func, *res;
4782 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004783
4784 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
4785 if (import_func == NULL) {
4786 PyErr_SetString(PyExc_ImportError, "__import__ not found");
4787 return NULL;
4788 }
4789
4790 /* Fast path for not overloaded __import__. */
Victor Stinnercaba55b2018-08-03 15:33:52 +02004791 if (import_func == _PyInterpreterState_GET_UNSAFE()->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004792 int ilevel = _PyLong_AsInt(level);
4793 if (ilevel == -1 && PyErr_Occurred()) {
4794 return NULL;
4795 }
4796 res = PyImport_ImportModuleLevelObject(
4797 name,
4798 f->f_globals,
4799 f->f_locals == NULL ? Py_None : f->f_locals,
4800 fromlist,
4801 ilevel);
4802 return res;
4803 }
4804
4805 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004806
4807 stack[0] = name;
4808 stack[1] = f->f_globals;
4809 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
4810 stack[3] = fromlist;
4811 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02004812 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004813 Py_DECREF(import_func);
4814 return res;
4815}
4816
4817static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004818import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004819{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004820 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02004821 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08004822 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004823
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004824 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02004825 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004826 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004827 /* Issue #17636: in case this failed because of a circular relative
4828 import, try to fallback on reading the module directly from
4829 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02004830 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07004831 if (pkgname == NULL) {
4832 goto error;
4833 }
Oren Milman6db70332017-09-19 14:23:01 +03004834 if (!PyUnicode_Check(pkgname)) {
4835 Py_CLEAR(pkgname);
4836 goto error;
4837 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004838 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07004839 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08004840 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004841 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07004842 }
Eric Snow3f9eee62017-09-15 16:35:20 -06004843 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004844 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07004845 if (x == NULL) {
4846 goto error;
4847 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004848 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004849 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07004850 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004851 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004852 if (pkgname == NULL) {
4853 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
4854 if (pkgname_or_unknown == NULL) {
4855 Py_XDECREF(pkgpath);
4856 return NULL;
4857 }
4858 } else {
4859 pkgname_or_unknown = pkgname;
4860 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004861
4862 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
4863 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08004864 errmsg = PyUnicode_FromFormat(
4865 "cannot import name %R from %R (unknown location)",
4866 name, pkgname_or_unknown
4867 );
4868 /* NULL check for errmsg done by PyErr_SetImportError. */
4869 PyErr_SetImportError(errmsg, pkgname, NULL);
4870 }
4871 else {
4872 errmsg = PyUnicode_FromFormat(
4873 "cannot import name %R from %R (%S)",
4874 name, pkgname_or_unknown, pkgpath
4875 );
4876 /* NULL check for errmsg done by PyErr_SetImportError. */
4877 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004878 }
4879
Xiang Zhang4830f582017-03-21 11:13:42 +08004880 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004881 Py_XDECREF(pkgname_or_unknown);
4882 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07004883 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00004884}
Guido van Rossumac7be682001-01-17 15:42:30 +00004885
Thomas Wouters52152252000-08-17 22:55:00 +00004886static int
4887import_all_from(PyObject *locals, PyObject *v)
4888{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004889 _Py_IDENTIFIER(__all__);
4890 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08004891 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004892 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004893 int skip_leading_underscores = 0;
4894 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004895
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004896 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
4897 return -1; /* Unexpected error */
4898 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004899 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004900 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
4901 return -1;
4902 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004903 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004904 PyErr_SetString(PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004905 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004906 return -1;
4907 }
4908 all = PyMapping_Keys(dict);
4909 Py_DECREF(dict);
4910 if (all == NULL)
4911 return -1;
4912 skip_leading_underscores = 1;
4913 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004915 for (pos = 0, err = 0; ; pos++) {
4916 name = PySequence_GetItem(all, pos);
4917 if (name == NULL) {
4918 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4919 err = -1;
4920 else
4921 PyErr_Clear();
4922 break;
4923 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08004924 if (!PyUnicode_Check(name)) {
4925 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
4926 if (modname == NULL) {
4927 Py_DECREF(name);
4928 err = -1;
4929 break;
4930 }
4931 if (!PyUnicode_Check(modname)) {
4932 PyErr_Format(PyExc_TypeError,
4933 "module __name__ must be a string, not %.100s",
4934 Py_TYPE(modname)->tp_name);
4935 }
4936 else {
4937 PyErr_Format(PyExc_TypeError,
4938 "%s in %U.%s must be str, not %.100s",
4939 skip_leading_underscores ? "Key" : "Item",
4940 modname,
4941 skip_leading_underscores ? "__dict__" : "__all__",
4942 Py_TYPE(name)->tp_name);
4943 }
4944 Py_DECREF(modname);
4945 Py_DECREF(name);
4946 err = -1;
4947 break;
4948 }
4949 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03004950 if (PyUnicode_READY(name) == -1) {
4951 Py_DECREF(name);
4952 err = -1;
4953 break;
4954 }
4955 if (PyUnicode_READ_CHAR(name, 0) == '_') {
4956 Py_DECREF(name);
4957 continue;
4958 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004959 }
4960 value = PyObject_GetAttr(v, name);
4961 if (value == NULL)
4962 err = -1;
4963 else if (PyDict_CheckExact(locals))
4964 err = PyDict_SetItem(locals, name, value);
4965 else
4966 err = PyObject_SetItem(locals, name, value);
4967 Py_DECREF(name);
4968 Py_XDECREF(value);
4969 if (err != 0)
4970 break;
4971 }
4972 Py_DECREF(all);
4973 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004974}
4975
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03004976static int
4977check_args_iterable(PyObject *func, PyObject *args)
4978{
4979 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
4980 PyErr_Format(PyExc_TypeError,
4981 "%.200s%.200s argument after * "
4982 "must be an iterable, not %.200s",
4983 PyEval_GetFuncName(func),
4984 PyEval_GetFuncDesc(func),
4985 args->ob_type->tp_name);
4986 return -1;
4987 }
4988 return 0;
4989}
4990
4991static void
4992format_kwargs_mapping_error(PyObject *func, PyObject *kwargs)
4993{
4994 PyErr_Format(PyExc_TypeError,
4995 "%.200s%.200s argument after ** "
4996 "must be a mapping, not %.200s",
4997 PyEval_GetFuncName(func),
4998 PyEval_GetFuncDesc(func),
4999 kwargs->ob_type->tp_name);
5000}
5001
Guido van Rossumac7be682001-01-17 15:42:30 +00005002static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005003format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005004{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005005 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005007 if (!obj)
5008 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005009
Serhiy Storchaka06515832016-11-20 09:13:07 +02005010 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005011 if (!obj_str)
5012 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005014 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005015}
Guido van Rossum950361c1997-01-24 13:49:28 +00005016
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005017static void
5018format_exc_unbound(PyCodeObject *co, int oparg)
5019{
5020 PyObject *name;
5021 /* Don't stomp existing exception */
5022 if (PyErr_Occurred())
5023 return;
5024 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5025 name = PyTuple_GET_ITEM(co->co_cellvars,
5026 oparg);
5027 format_exc_check_arg(
5028 PyExc_UnboundLocalError,
5029 UNBOUNDLOCAL_ERROR_MSG,
5030 name);
5031 } else {
5032 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5033 PyTuple_GET_SIZE(co->co_cellvars));
5034 format_exc_check_arg(PyExc_NameError,
5035 UNBOUNDFREE_ERROR_MSG, name);
5036 }
5037}
5038
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005039static void
5040format_awaitable_error(PyTypeObject *type, int prevopcode)
5041{
5042 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5043 if (prevopcode == BEFORE_ASYNC_WITH) {
5044 PyErr_Format(PyExc_TypeError,
5045 "'async with' received an object from __aenter__ "
5046 "that does not implement __await__: %.100s",
5047 type->tp_name);
5048 }
5049 else if (prevopcode == WITH_CLEANUP_START) {
5050 PyErr_Format(PyExc_TypeError,
5051 "'async with' received an object from __aexit__ "
5052 "that does not implement __await__: %.100s",
5053 type->tp_name);
5054 }
5055 }
5056}
5057
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005058static PyObject *
5059unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005060 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005061{
5062 PyObject *res;
5063 if (Py_REFCNT(v) == 2) {
5064 /* In the common case, there are 2 references to the value
5065 * stored in 'variable' when the += is performed: one on the
5066 * value stack (in 'v') and one still stored in the
5067 * 'variable'. We try to delete the variable now to reduce
5068 * the refcnt to 1.
5069 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005070 int opcode, oparg;
5071 NEXTOPARG();
5072 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005073 case STORE_FAST:
5074 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005075 PyObject **fastlocals = f->f_localsplus;
5076 if (GETLOCAL(oparg) == v)
5077 SETLOCAL(oparg, NULL);
5078 break;
5079 }
5080 case STORE_DEREF:
5081 {
5082 PyObject **freevars = (f->f_localsplus +
5083 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005084 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005085 if (PyCell_GET(c) == v) {
5086 PyCell_SET(c, NULL);
5087 Py_DECREF(v);
5088 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005089 break;
5090 }
5091 case STORE_NAME:
5092 {
5093 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005094 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005095 PyObject *locals = f->f_locals;
5096 if (PyDict_CheckExact(locals) &&
5097 PyDict_GetItem(locals, name) == v) {
5098 if (PyDict_DelItem(locals, name) != 0) {
5099 PyErr_Clear();
5100 }
5101 }
5102 break;
5103 }
5104 }
5105 }
5106 res = v;
5107 PyUnicode_Append(&res, w);
5108 return res;
5109}
5110
Guido van Rossum950361c1997-01-24 13:49:28 +00005111#ifdef DYNAMIC_EXECUTION_PROFILE
5112
Skip Montanarof118cb12001-10-15 20:51:38 +00005113static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005114getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005115{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005116 int i;
5117 PyObject *l = PyList_New(256);
5118 if (l == NULL) return NULL;
5119 for (i = 0; i < 256; i++) {
5120 PyObject *x = PyLong_FromLong(a[i]);
5121 if (x == NULL) {
5122 Py_DECREF(l);
5123 return NULL;
5124 }
5125 PyList_SetItem(l, i, x);
5126 }
5127 for (i = 0; i < 256; i++)
5128 a[i] = 0;
5129 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005130}
5131
5132PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005133_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005134{
5135#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005136 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005137#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005138 int i;
5139 PyObject *l = PyList_New(257);
5140 if (l == NULL) return NULL;
5141 for (i = 0; i < 257; i++) {
5142 PyObject *x = getarray(dxpairs[i]);
5143 if (x == NULL) {
5144 Py_DECREF(l);
5145 return NULL;
5146 }
5147 PyList_SetItem(l, i, x);
5148 }
5149 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005150#endif
5151}
5152
5153#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005154
5155Py_ssize_t
5156_PyEval_RequestCodeExtraIndex(freefunc free)
5157{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005158 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005159 Py_ssize_t new_index;
5160
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005161 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005162 return -1;
5163 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005164 new_index = interp->co_extra_user_count++;
5165 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005166 return new_index;
5167}
Łukasz Langaa785c872016-09-09 17:37:37 -07005168
5169static void
5170dtrace_function_entry(PyFrameObject *f)
5171{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005172 const char *filename;
5173 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005174 int lineno;
5175
5176 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5177 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5178 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5179
5180 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5181}
5182
5183static void
5184dtrace_function_return(PyFrameObject *f)
5185{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005186 const char *filename;
5187 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005188 int lineno;
5189
5190 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5191 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5192 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5193
5194 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5195}
5196
5197/* DTrace equivalent of maybe_call_line_trace. */
5198static void
5199maybe_dtrace_line(PyFrameObject *frame,
5200 int *instr_lb, int *instr_ub, int *instr_prev)
5201{
5202 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005203 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005204
5205 /* If the last instruction executed isn't in the current
5206 instruction window, reset the window.
5207 */
5208 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5209 PyAddrPair bounds;
5210 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5211 &bounds);
5212 *instr_lb = bounds.ap_lower;
5213 *instr_ub = bounds.ap_upper;
5214 }
5215 /* If the last instruction falls at the start of a line or if
5216 it represents a jump backwards, update the frame's line
5217 number and call the trace function. */
5218 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5219 frame->f_lineno = line;
5220 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5221 if (!co_filename)
5222 co_filename = "?";
5223 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5224 if (!co_name)
5225 co_name = "?";
5226 PyDTrace_LINE(co_filename, co_name, line);
5227 }
5228 *instr_prev = frame->f_lasti;
5229}