blob: 9f732f56ba86bfda64d140993b5237b7f1df2069 [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"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040015#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000016#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000017#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070018#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040019#include "setobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000020#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000021
Guido van Rossumc6004111993-11-05 10:22:19 +000022#include <ctype.h>
23
Guido van Rossum04691fc1992-08-12 15:35:34 +000024/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000025/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000026
Guido van Rossum408027e1996-12-30 16:17:54 +000027#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000028/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029#define LLTRACE 1 /* Low-level trace feature */
30#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000031#endif
32
Yury Selivanovf2392132016-12-13 19:03:51 -050033/* Private API for the LOAD_METHOD opcode. */
34extern int _PyObject_GetMethod(PyObject *, PyObject *, PyObject **);
35
Jeremy Hylton52820442001-01-03 23:52:36 +000036typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000037
Guido van Rossum374a9221991-04-04 10:40:29 +000038/* Forward declarations */
Victor Stinner415c5102017-01-11 00:54:57 +010039Py_LOCAL_INLINE(PyObject *) call_function(PyObject ***, Py_ssize_t, PyObject *);
Victor Stinnerf9b760f2016-09-09 10:17:08 -070040static PyObject * do_call_core(PyObject *, PyObject *, PyObject *);
Jeremy Hylton52820442001-01-03 23:52:36 +000041
Guido van Rossum0a066c01992-03-27 17:29:15 +000042#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000043static int lltrace;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020044static int prtrace(PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000045#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010046static int call_trace(Py_tracefunc, PyObject *,
47 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000049static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010050 PyThreadState *, PyFrameObject *,
51 int, PyObject *);
52static void call_exc_trace(Py_tracefunc, PyObject *,
53 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000054static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010055 PyThreadState *, PyFrameObject *, 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 *);
Serhiy Storchaka133138a2016-08-02 22:51:21 +030061static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *, PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +000062static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +000063static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +000064static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +000065static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +020066static PyObject * unicode_concatenate(PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030067 PyFrameObject *, const _Py_CODEUNIT *);
Benjamin Petersonce798522012-01-22 11:24:29 -050068static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Serhiy Storchaka25e4f772017-08-03 11:37:15 +030069static int check_args_iterable(PyObject *func, PyObject *vararg);
70static void format_kwargs_mapping_error(PyObject *func, PyObject *kwargs);
Guido van Rossum374a9221991-04-04 10:40:29 +000071
Paul Prescode68140d2000-08-30 20:25:01 +000072#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000074#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000076#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 "free variable '%.200s' referenced before assignment" \
78 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000079
Guido van Rossum950361c1997-01-24 13:49:28 +000080/* Dynamic execution profile */
81#ifdef DYNAMIC_EXECUTION_PROFILE
82#ifdef DXPAIRS
83static long dxpairs[257][256];
84#define dxp dxpairs[256]
85#else
86static long dxp[256];
87#endif
88#endif
89
Benjamin Petersond2be5b42010-09-10 22:47:02 +000090#ifdef WITH_THREAD
91#define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request)
92#else
93#define GIL_REQUEST 0
94#endif
95
Jeffrey Yasskin39370832010-05-03 19:29:34 +000096/* This can set eval_breaker to 0 even though gil_drop_request became
97 1. We believe this is all right because the eval loop will release
98 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +000099#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 _Py_atomic_store_relaxed( \
101 &eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000102 GIL_REQUEST | \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
104 pending_async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000105
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000106#ifdef WITH_THREAD
107
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000108#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 do { \
110 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
111 _Py_atomic_store_relaxed(&eval_breaker, 1); \
112 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000113
114#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 do { \
116 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
117 COMPUTE_EVAL_BREAKER(); \
118 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000119
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000120#endif
121
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000122/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000123#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 do { \
125 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
126 _Py_atomic_store_relaxed(&eval_breaker, 1); \
127 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000128
129#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 do { \
131 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
132 COMPUTE_EVAL_BREAKER(); \
133 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000134
135#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 do { \
137 pending_async_exc = 1; \
138 _Py_atomic_store_relaxed(&eval_breaker, 1); \
139 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000140
141#define UNSIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000143
144
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200145/* This single variable consolidates all requests to break out of the fast path
146 in the eval loop. */
147static _Py_atomic_int eval_breaker = {0};
148/* Request for running pending calls. */
149static _Py_atomic_int pendingcalls_to_do = {0};
150/* Request for looking at the `async_exc` field of the current thread state.
151 Guarded by the GIL. */
152static int pending_async_exc = 0;
153
Guido van Rossume59214e1994-08-30 08:01:59 +0000154#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000155
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000156#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000157#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000158#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000159#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000160
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000161static PyThread_type_lock pending_lock = 0; /* for pending calls */
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200162static unsigned long main_thread = 0;
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000163/* Request for dropping the GIL */
164static _Py_atomic_int gil_drop_request = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000165
166#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000167
Tim Peters7f468f22004-10-11 02:40:51 +0000168int
169PyEval_ThreadsInitialized(void)
170{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000172}
173
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000174void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000175PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 if (gil_created())
178 return;
179 create_gil();
180 take_gil(PyThreadState_GET());
181 main_thread = PyThread_get_thread_ident();
182 if (!pending_lock)
183 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000184}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000185
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000186void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000187_PyEval_FiniThreads(void)
188{
189 if (!gil_created())
190 return;
191 destroy_gil();
192 assert(!gil_created());
193}
194
195void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000196PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 PyThreadState *tstate = PyThreadState_GET();
199 if (tstate == NULL)
200 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
201 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000202}
203
204void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000205PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000206{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 /* This function must succeed when the current thread state is NULL.
208 We therefore avoid PyThreadState_GET() which dumps a fatal error
209 in debug mode.
210 */
211 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
212 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000213}
214
215void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000216PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000217{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 if (tstate == NULL)
219 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
220 /* Check someone has called PyEval_InitThreads() to create the lock */
221 assert(gil_created());
222 take_gil(tstate);
223 if (PyThreadState_Swap(tstate) != NULL)
224 Py_FatalError(
225 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000226}
227
228void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000229PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000230{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 if (tstate == NULL)
232 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
233 if (PyThreadState_Swap(NULL) != tstate)
234 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
235 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000236}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000237
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200238/* This function is called from PyOS_AfterFork_Child to destroy all threads
239 * which are not running in the child process, and clear internal locks
240 * which might be held by those threads.
241 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000242
243void
244PyEval_ReInitThreads(void)
245{
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200246 PyThreadState *current_tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 if (!gil_created())
249 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 recreate_gil();
251 pending_lock = PyThread_allocate_lock();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200252 take_gil(current_tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000254
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200255 /* Destroy all threads except the current one */
256 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000257}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000258
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000259#endif /* WITH_THREAD */
260
261/* This function is used to signal that async exceptions are waiting to be
262 raised, therefore it is also useful in non-threaded builds. */
263
264void
265_PyEval_SignalAsyncExc(void)
266{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000268}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000269
Guido van Rossumff4949e1992-08-05 19:58:53 +0000270/* Functions save_thread and restore_thread are always defined so
271 dynamically loaded modules needn't be compiled separately for use
272 with and without threads: */
273
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000274PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000275PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 PyThreadState *tstate = PyThreadState_Swap(NULL);
278 if (tstate == NULL)
279 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000280#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 if (gil_created())
282 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000283#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000285}
286
287void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000288PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000289{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 if (tstate == NULL)
291 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000292#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 if (gil_created()) {
294 int err = errno;
295 take_gil(tstate);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200296 /* _Py_Finalizing is protected by the GIL */
297 if (_Py_Finalizing && tstate != _Py_Finalizing) {
298 drop_gil(tstate);
299 PyThread_exit_thread();
300 assert(0); /* unreachable */
301 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 errno = err;
303 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000304#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000306}
307
308
Guido van Rossuma9672091994-09-14 13:31:22 +0000309/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
310 signal handlers or Mac I/O completion routines) can schedule calls
311 to a function to be called synchronously.
312 The synchronous function is called with one void* argument.
313 It should return 0 for success or -1 for failure -- failure should
314 be accompanied by an exception.
315
316 If registry succeeds, the registry function returns 0; if it fails
317 (e.g. due to too many pending calls) it returns -1 (without setting
318 an exception condition).
319
320 Note that because registry may occur from within signal handlers,
321 or other asynchronous events, calling malloc() is unsafe!
322
323#ifdef WITH_THREAD
324 Any thread can schedule pending calls, but only the main thread
325 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000326 There is no facility to schedule calls to a particular thread, but
327 that should be easy to change, should that ever be required. In
328 that case, the static variables here should go into the python
329 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000330#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000331*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000332
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200333void
334_PyEval_SignalReceived(void)
335{
336 /* bpo-30703: Function called when the C signal handler of Python gets a
337 signal. We cannot queue a callback using Py_AddPendingCall() since
338 that function is not async-signal-safe. */
339 SIGNAL_PENDING_CALLS();
340}
341
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000342#ifdef WITH_THREAD
343
344/* The WITH_THREAD implementation is thread-safe. It allows
345 scheduling to be made from any thread, and even from an executing
346 callback.
347 */
348
349#define NPENDINGCALLS 32
350static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 int (*func)(void *);
352 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000353} pendingcalls[NPENDINGCALLS];
354static int pendingfirst = 0;
355static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000356
357int
358Py_AddPendingCall(int (*func)(void *), void *arg)
359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 int i, j, result=0;
361 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 /* try a few times for the lock. Since this mechanism is used
364 * for signal handling (on the main thread), there is a (slim)
365 * chance that a signal is delivered on the same thread while we
366 * hold the lock during the Py_MakePendingCalls() function.
367 * This avoids a deadlock in that case.
368 * Note that signals can be delivered on any thread. In particular,
369 * on Windows, a SIGINT is delivered on a system-created worker
370 * thread.
371 * We also check for lock being NULL, in the unlikely case that
372 * this function is called before any bytecode evaluation takes place.
373 */
374 if (lock != NULL) {
375 for (i = 0; i<100; i++) {
376 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
377 break;
378 }
379 if (i == 100)
380 return -1;
381 }
382
383 i = pendinglast;
384 j = (i + 1) % NPENDINGCALLS;
385 if (j == pendingfirst) {
386 result = -1; /* Queue full */
387 } else {
388 pendingcalls[i].func = func;
389 pendingcalls[i].arg = arg;
390 pendinglast = j;
391 }
392 /* signal main loop */
393 SIGNAL_PENDING_CALLS();
394 if (lock != NULL)
395 PyThread_release_lock(lock);
396 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000397}
398
399int
400Py_MakePendingCalls(void)
401{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200402 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 int i;
404 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000405
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200406 assert(PyGILState_Check());
407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 if (!pending_lock) {
409 /* initial allocation of the lock */
410 pending_lock = PyThread_allocate_lock();
411 if (pending_lock == NULL)
412 return -1;
413 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 /* only service pending calls on main thread */
416 if (main_thread && PyThread_get_thread_ident() != main_thread)
417 return 0;
418 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200419 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200421 busy = 1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200422 /* unsignal before starting to call callbacks, so that any callback
423 added in-between re-signals */
424 UNSIGNAL_PENDING_CALLS();
425
426 /* Python signal handler doesn't really queue a callback: it only signals
427 that a signal was received, see _PyEval_SignalReceived(). */
428 if (PyErr_CheckSignals() < 0) {
429 goto error;
430 }
431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 /* perform a bounded number of calls, in case of recursion */
433 for (i=0; i<NPENDINGCALLS; i++) {
434 int j;
435 int (*func)(void *);
436 void *arg = NULL;
437
438 /* pop one item off the queue while holding the lock */
439 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
440 j = pendingfirst;
441 if (j == pendinglast) {
442 func = NULL; /* Queue empty */
443 } else {
444 func = pendingcalls[j].func;
445 arg = pendingcalls[j].arg;
446 pendingfirst = (j + 1) % NPENDINGCALLS;
447 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 PyThread_release_lock(pending_lock);
449 /* having released the lock, perform the callback */
450 if (func == NULL)
451 break;
452 r = func(arg);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200453 if (r) {
454 goto error;
455 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200457
Charles-François Natalif23339a2011-07-23 18:15:43 +0200458 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 return r;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200460
461error:
462 busy = 0;
463 SIGNAL_PENDING_CALLS(); /* We're not done yet */
464 return -1;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000465}
466
467#else /* if ! defined WITH_THREAD */
468
469/*
470 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
471 This code is used for signal handling in python that isn't built
472 with WITH_THREAD.
473 Don't use this implementation when Py_AddPendingCalls() can happen
474 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475
Guido van Rossuma9672091994-09-14 13:31:22 +0000476 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000477 (1) nested asynchronous calls to Py_AddPendingCall()
478 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000480 (1) is very unlikely because typically signal delivery
481 is blocked during signal handling. So it should be impossible.
482 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000483 The current code is safe against (2), but not against (1).
484 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000485 thread is present, interrupted by signals, and that the critical
486 section is protected with the "busy" variable. On Windows, which
487 delivers SIGINT on a system thread, this does not hold and therefore
488 Windows really shouldn't use this version.
489 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000490*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000491
Guido van Rossuma9672091994-09-14 13:31:22 +0000492#define NPENDINGCALLS 32
493static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 int (*func)(void *);
495 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000496} pendingcalls[NPENDINGCALLS];
497static volatile int pendingfirst = 0;
498static volatile int pendinglast = 0;
499
500int
Thomas Wouters334fb892000-07-25 12:56:38 +0000501Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000502{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 static volatile int busy = 0;
504 int i, j;
505 /* XXX Begin critical section */
506 if (busy)
507 return -1;
508 busy = 1;
509 i = pendinglast;
510 j = (i + 1) % NPENDINGCALLS;
511 if (j == pendingfirst) {
512 busy = 0;
513 return -1; /* Queue full */
514 }
515 pendingcalls[i].func = func;
516 pendingcalls[i].arg = arg;
517 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 SIGNAL_PENDING_CALLS();
520 busy = 0;
521 /* XXX End critical section */
522 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000523}
524
Guido van Rossum180d7b41994-09-29 09:45:57 +0000525int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000526Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000527{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 static int busy = 0;
529 if (busy)
530 return 0;
531 busy = 1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200532
533 /* unsignal before starting to call callbacks, so that any callback
534 added in-between re-signals */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 UNSIGNAL_PENDING_CALLS();
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200536 /* Python signal handler doesn't really queue a callback: it only signals
537 that a signal was received, see _PyEval_SignalReceived(). */
538 if (PyErr_CheckSignals() < 0) {
539 goto error;
540 }
541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 for (;;) {
543 int i;
544 int (*func)(void *);
545 void *arg;
546 i = pendingfirst;
547 if (i == pendinglast)
548 break; /* Queue empty */
549 func = pendingcalls[i].func;
550 arg = pendingcalls[i].arg;
551 pendingfirst = (i + 1) % NPENDINGCALLS;
552 if (func(arg) < 0) {
Masayuki Yamamoto0c311632017-07-05 17:39:17 +0900553 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 }
555 }
556 busy = 0;
557 return 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200558
559error:
560 busy = 0;
561 SIGNAL_PENDING_CALLS(); /* We're not done yet */
562 return -1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000563}
564
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000565#endif /* WITH_THREAD */
566
Guido van Rossuma9672091994-09-14 13:31:22 +0000567
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000568/* The interpreter's recursion limit */
569
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000570#ifndef Py_DEFAULT_RECURSION_LIMIT
571#define Py_DEFAULT_RECURSION_LIMIT 1000
572#endif
573static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
574int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000575
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000576int
577Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000578{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000580}
581
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000582void
583Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000584{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 recursion_limit = new_limit;
586 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000587}
588
Armin Rigo2b3eb402003-10-28 12:05:48 +0000589/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
590 if the recursion_depth reaches _Py_CheckRecursionLimit.
591 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
592 to guarantee that _Py_CheckRecursiveCall() is regularly called.
593 Without USE_STACKCHECK, there is no need for this. */
594int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300595_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000596{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000598
599#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 if (PyOS_CheckStack()) {
601 --tstate->recursion_depth;
602 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
603 return -1;
604 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000605#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 _Py_CheckRecursionLimit = recursion_limit;
607 if (tstate->recursion_critical)
608 /* Somebody asked that we don't check for recursion. */
609 return 0;
610 if (tstate->overflowed) {
611 if (tstate->recursion_depth > recursion_limit + 50) {
612 /* Overflowing while handling an overflow. Give up. */
613 Py_FatalError("Cannot recover from stack overflow.");
614 }
615 return 0;
616 }
617 if (tstate->recursion_depth > recursion_limit) {
618 --tstate->recursion_depth;
619 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400620 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 "maximum recursion depth exceeded%s",
622 where);
623 return -1;
624 }
625 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000626}
627
Guido van Rossum374a9221991-04-04 10:40:29 +0000628/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000629enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000630 WHY_NOT = 0x0001, /* No error */
631 WHY_EXCEPTION = 0x0002, /* Exception occurred */
Stefan Krahb7e10102010-06-23 18:42:39 +0000632 WHY_RETURN = 0x0008, /* 'return' statement */
633 WHY_BREAK = 0x0010, /* 'break' statement */
634 WHY_CONTINUE = 0x0020, /* 'continue' statement */
635 WHY_YIELD = 0x0040, /* 'yield' operator */
636 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000637};
Guido van Rossum374a9221991-04-04 10:40:29 +0000638
Benjamin Peterson87880242011-07-03 16:48:31 -0500639static void save_exc_state(PyThreadState *, PyFrameObject *);
640static void swap_exc_state(PyThreadState *, PyFrameObject *);
641static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400642static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000643static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000644
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000645/* Records whether tracing is on for any thread. Counts the number of
646 threads for which tstate->c_tracefunc is non-NULL, so if the value
647 is 0, we know we don't have to check this thread's c_tracefunc.
648 This speeds up the if statement in PyEval_EvalFrameEx() after
649 fast_next_opcode*/
650static int _Py_TracingPossible = 0;
651
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000652
Guido van Rossum374a9221991-04-04 10:40:29 +0000653
Guido van Rossumb209a111997-04-29 18:18:01 +0000654PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000655PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000656{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 return PyEval_EvalCodeEx(co,
658 globals, locals,
659 (PyObject **)NULL, 0,
660 (PyObject **)NULL, 0,
661 (PyObject **)NULL, 0,
662 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000663}
664
665
666/* Interpreter main loop */
667
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000668PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000669PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 /* This is for backward compatibility with extension modules that
671 used this API; core interpreter code should call
672 PyEval_EvalFrameEx() */
673 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000674}
675
676PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000677PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000678{
Brett Cannon3cebf932016-09-05 15:33:46 -0700679 PyThreadState *tstate = PyThreadState_GET();
680 return tstate->interp->eval_frame(f, throwflag);
681}
682
Victor Stinnerc6944e72016-11-11 02:13:35 +0100683PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700684_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
685{
Guido van Rossum950361c1997-01-24 13:49:28 +0000686#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000688#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200689 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300690 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200691 int opcode; /* Current opcode */
692 int oparg; /* Current opcode argument, if any */
693 enum why_code why; /* Reason for block stack unwind */
694 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 PyObject *retval = NULL; /* Return value */
696 PyThreadState *tstate = PyThreadState_GET();
697 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 is true when the line being executed has changed. The
704 initial values are such as to make this false the first
705 time it is tested. */
706 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000707
Serhiy Storchakaab874002016-09-11 13:48:15 +0300708 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 PyObject *names;
710 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000711
Brett Cannon368b4b72012-04-02 12:17:59 -0400712#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200713 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400714#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200715
Antoine Pitroub52ec782009-01-25 16:34:23 +0000716/* Computed GOTOs, or
717 the-optimization-commonly-but-improperly-known-as-"threaded code"
718 using gcc's labels-as-values extension
719 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
720
721 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000723 combined with a lookup table of jump addresses. However, since the
724 indirect jump instruction is shared by all opcodes, the CPU will have a
725 hard time making the right prediction for where to jump next (actually,
726 it will be always wrong except in the uncommon case of a sequence of
727 several identical opcodes).
728
729 "Threaded code" in contrast, uses an explicit jump table and an explicit
730 indirect jump instruction at the end of each opcode. Since the jump
731 instruction is at a different address for each opcode, the CPU will make a
732 separate prediction for each of these instructions, which is equivalent to
733 predicting the second opcode of each opcode pair. These predictions have
734 a much better chance to turn out valid, especially in small bytecode loops.
735
736 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000738 and potentially many more instructions (depending on the pipeline width).
739 A correctly predicted branch, however, is nearly free.
740
741 At the time of this writing, the "threaded code" version is up to 15-20%
742 faster than the normal "switch" version, depending on the compiler and the
743 CPU architecture.
744
745 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
746 because it would render the measurements invalid.
747
748
749 NOTE: care must be taken that the compiler doesn't try to "optimize" the
750 indirect jumps by sharing them between all opcodes. Such optimizations
751 can be disabled on gcc by using the -fno-gcse flag (or possibly
752 -fno-crossjumping).
753*/
754
Antoine Pitrou042b1282010-08-13 21:15:58 +0000755#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000756#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000757#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000758#endif
759
Antoine Pitrou042b1282010-08-13 21:15:58 +0000760#ifdef HAVE_COMPUTED_GOTOS
761 #ifndef USE_COMPUTED_GOTOS
762 #define USE_COMPUTED_GOTOS 1
763 #endif
764#else
765 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
766 #error "Computed gotos are not supported on this compiler."
767 #endif
768 #undef USE_COMPUTED_GOTOS
769 #define USE_COMPUTED_GOTOS 0
770#endif
771
772#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000773/* Import the static jump table */
774#include "opcode_targets.h"
775
Antoine Pitroub52ec782009-01-25 16:34:23 +0000776#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 TARGET_##op: \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000779
Antoine Pitroub52ec782009-01-25 16:34:23 +0000780#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 { \
782 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
783 FAST_DISPATCH(); \
784 } \
785 continue; \
786 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000787
788#ifdef LLTRACE
789#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700791 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300793 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300794 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 } \
796 goto fast_next_opcode; \
797 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000798#else
799#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700801 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300803 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300804 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 } \
806 goto fast_next_opcode; \
807 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000808#endif
809
810#else
811#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 case op:
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300813
Antoine Pitroub52ec782009-01-25 16:34:23 +0000814#define DISPATCH() continue
815#define FAST_DISPATCH() goto fast_next_opcode
816#endif
817
818
Neal Norwitza81d2202002-07-14 00:27:26 +0000819/* Tuple access macros */
820
821#ifndef Py_DEBUG
822#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
823#else
824#define GETITEM(v, i) PyTuple_GetItem((v), (i))
825#endif
826
Guido van Rossum374a9221991-04-04 10:40:29 +0000827/* Code access macros */
828
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300829/* The integer overflow is checked by an assertion below. */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300830#define INSTR_OFFSET() (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300831#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300832 _Py_CODEUNIT word = *next_instr; \
833 opcode = _Py_OPCODE(word); \
834 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300835 next_instr++; \
836 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300837#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
838#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000839
Raymond Hettingerf606f872003-03-16 03:11:04 +0000840/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 Some opcodes tend to come in pairs thus making it possible to
842 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300843 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000844
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 Verifying the prediction costs a single high-speed test of a register
846 variable against a constant. If the pairing was good, then the
847 processor's own internal branch predication has a high likelihood of
848 success, resulting in a nearly zero-overhead transition to the
849 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300850 including its unpredictable switch-case branch. Combined with the
851 processor's internal branch prediction, a successful PREDICT has the
852 effect of making the two opcodes run as if they were a single new opcode
853 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000854
Georg Brandl86b2fb92008-07-16 03:43:04 +0000855 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 predictions turned-on and interpret the results as if some opcodes
857 had been combined or turn-off predictions so that the opcode frequency
858 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000859
860 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 the CPU to record separate branch prediction information for each
862 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000863
Raymond Hettingerf606f872003-03-16 03:11:04 +0000864*/
865
Antoine Pitrou042b1282010-08-13 21:15:58 +0000866#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000868#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300869#define PREDICT(op) \
870 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300871 _Py_CODEUNIT word = *next_instr; \
872 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300873 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300874 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300875 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300876 goto PRED_##op; \
877 } \
878 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000879#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300880#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000881
Raymond Hettingerf606f872003-03-16 03:11:04 +0000882
Guido van Rossum374a9221991-04-04 10:40:29 +0000883/* Stack manipulation macros */
884
Martin v. Löwis18e16552006-02-15 17:27:45 +0000885/* The stack can grow at most MAXINT deep, as co_nlocals and
886 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000887#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
888#define EMPTY() (STACK_LEVEL() == 0)
889#define TOP() (stack_pointer[-1])
890#define SECOND() (stack_pointer[-2])
891#define THIRD() (stack_pointer[-3])
892#define FOURTH() (stack_pointer[-4])
893#define PEEK(n) (stack_pointer[-(n)])
894#define SET_TOP(v) (stack_pointer[-1] = (v))
895#define SET_SECOND(v) (stack_pointer[-2] = (v))
896#define SET_THIRD(v) (stack_pointer[-3] = (v))
897#define SET_FOURTH(v) (stack_pointer[-4] = (v))
898#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
899#define BASIC_STACKADJ(n) (stack_pointer += n)
900#define BASIC_PUSH(v) (*stack_pointer++ = (v))
901#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000902
Guido van Rossum96a42c81992-01-12 02:29:51 +0000903#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000905 lltrace && prtrace(TOP(), "push")); \
906 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000908 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000910 lltrace && prtrace(TOP(), "stackadj")); \
911 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +0000912#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000913 prtrace((STACK_POINTER)[-1], "ext_pop")), \
914 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000915#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000916#define PUSH(v) BASIC_PUSH(v)
917#define POP() BASIC_POP()
918#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000919#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000920#endif
921
Guido van Rossum681d79a1995-07-18 14:51:37 +0000922/* Local variable macros */
923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000925
926/* The SETLOCAL() macro must not DECREF the local variable in-place and
927 then store the new value; it must copy the old value to a temporary
928 value, then store the new value, and then DECREF the temporary value.
929 This is because it is possible that during the DECREF the frame is
930 accessed by other code (e.g. a __del__ method or gc.collect()) and the
931 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000933 GETLOCAL(i) = value; \
934 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000935
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000936
937#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 while (STACK_LEVEL() > (b)->b_level) { \
939 PyObject *v = POP(); \
940 Py_XDECREF(v); \
941 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000942
943#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300944 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 PyObject *type, *value, *traceback; \
946 assert(STACK_LEVEL() >= (b)->b_level + 3); \
947 while (STACK_LEVEL() > (b)->b_level + 3) { \
948 value = POP(); \
949 Py_XDECREF(value); \
950 } \
951 type = tstate->exc_type; \
952 value = tstate->exc_value; \
953 traceback = tstate->exc_traceback; \
954 tstate->exc_type = POP(); \
955 tstate->exc_value = POP(); \
956 tstate->exc_traceback = POP(); \
957 Py_XDECREF(type); \
958 Py_XDECREF(value); \
959 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300960 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000961
Guido van Rossuma027efa1997-05-05 20:56:21 +0000962/* Start of code */
963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 /* push frame */
965 if (Py_EnterRecursiveCall(""))
966 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 if (tstate->use_tracing) {
971 if (tstate->c_tracefunc != NULL) {
972 /* tstate->c_tracefunc, if defined, is a
973 function that will be called on *every* entry
974 to a code block. Its return value, if not
975 None, is a function that will be called at
976 the start of each executed line of code.
977 (Actually, the function must return itself
978 in order to continue tracing.) The trace
979 functions are called with three arguments:
980 a pointer to the current frame, a string
981 indicating why the function is called, and
982 an argument which depends on the situation.
983 The global trace function is also called
984 whenever an exception is detected. */
985 if (call_trace_protected(tstate->c_tracefunc,
986 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100987 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 /* Trace function raised an error */
989 goto exit_eval_frame;
990 }
991 }
992 if (tstate->c_profilefunc != NULL) {
993 /* Similar for c_profilefunc, except it needn't
994 return itself and isn't called for "line" events */
995 if (call_trace_protected(tstate->c_profilefunc,
996 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100997 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 /* Profile function raised an error */
999 goto exit_eval_frame;
1000 }
1001 }
1002 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001003
Łukasz Langaa785c872016-09-09 17:37:37 -07001004 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1005 dtrace_function_entry(f);
1006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 co = f->f_code;
1008 names = co->co_names;
1009 consts = co->co_consts;
1010 fastlocals = f->f_localsplus;
1011 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001012 assert(PyBytes_Check(co->co_code));
1013 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001014 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1015 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1016 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001017 /*
1018 f->f_lasti refers to the index of the last instruction,
1019 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001020
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001021 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001022 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 When the PREDICT() macros are enabled, some opcode pairs follow in
1025 direct succession without updating f->f_lasti. A successful
1026 prediction effectively links the two codes together as if they
1027 were a single new opcode; accordingly,f->f_lasti will point to
1028 the first code in the pair (for instance, GET_ITER followed by
1029 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001030 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001032 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001033 next_instr = first_instr;
1034 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001035 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1036 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001037 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 stack_pointer = f->f_stacktop;
1039 assert(stack_pointer != NULL);
1040 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001041 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001042
Yury Selivanoveb636452016-09-08 22:01:51 -07001043 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01001044 if (!throwflag && f->f_exc_type != NULL && f->f_exc_type != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 /* We were in an except handler when we left,
1046 restore the exception state which was put aside
1047 (see YIELD_VALUE). */
Benjamin Peterson87880242011-07-03 16:48:31 -05001048 swap_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 }
Benjamin Peterson87880242011-07-03 16:48:31 -05001050 else
1051 save_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001053
Tim Peters5ca576e2001-06-18 22:08:13 +00001054#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001055 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001056#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 why = WHY_NOT;
Guido van Rossumac7be682001-01-17 15:42:30 +00001059
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001060 if (throwflag) /* support for generator.throw() */
1061 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001062
Victor Stinnerace47d72013-07-18 01:41:08 +02001063#ifdef Py_DEBUG
1064 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001065 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001066 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +02001067 assert(!PyErr_Occurred());
1068#endif
1069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1072 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +02001073 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 /* Do periodic things. Doing this every time through
1076 the loop would add too much overhead, so we do it
1077 only every Nth instruction. We also do it if
1078 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1079 event needs attention (e.g. a signal handler or
1080 async I/O handler); see Py_AddPendingCall() and
1081 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 if (_Py_atomic_load_relaxed(&eval_breaker)) {
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001084 if (_Py_OPCODE(*next_instr) == SETUP_FINALLY ||
1085 _Py_OPCODE(*next_instr) == YIELD_FROM) {
1086 /* Two cases where we skip running signal handlers and other
1087 pending calls:
1088 - If we're about to enter the try: of a try/finally (not
1089 *very* useful, but might help in some cases and it's
1090 traditional)
1091 - If we're resuming a chain of nested 'yield from' or
1092 'await' calls, then each frame is parked with YIELD_FROM
1093 as its next opcode. If the user hit control-C we want to
1094 wait until we've reached the innermost frame before
1095 running the signal handler and raising KeyboardInterrupt
1096 (see bpo-30039).
1097 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 goto fast_next_opcode;
1099 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001101 if (Py_MakePendingCalls() < 0)
1102 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001104#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001105 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 /* Give another thread a chance */
1107 if (PyThreadState_Swap(NULL) != tstate)
1108 Py_FatalError("ceval: tstate mix-up");
1109 drop_gil(tstate);
1110
1111 /* Other threads may run now */
1112
1113 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001114
1115 /* Check if we should make a quick exit. */
1116 if (_Py_Finalizing && _Py_Finalizing != tstate) {
1117 drop_gil(tstate);
1118 PyThread_exit_thread();
1119 }
1120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 if (PyThreadState_Swap(tstate) != NULL)
1122 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001124#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 /* Check for asynchronous exceptions. */
1126 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001127 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 tstate->async_exc = NULL;
1129 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001130 PyErr_SetNone(exc);
1131 Py_DECREF(exc);
1132 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 }
1134 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 fast_next_opcode:
1137 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001138
Łukasz Langaa785c872016-09-09 17:37:37 -07001139 if (PyDTrace_LINE_ENABLED())
1140 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001143
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001145 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001146 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 /* see maybe_call_line_trace
1148 for expository comments */
1149 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 err = maybe_call_line_trace(tstate->c_tracefunc,
1152 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001153 tstate, f,
1154 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 /* Reload possibly changed frame fields */
1156 JUMPTO(f->f_lasti);
1157 if (f->f_stacktop != NULL) {
1158 stack_pointer = f->f_stacktop;
1159 f->f_stacktop = NULL;
1160 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001161 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001163 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001167
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001168 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001169 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001170#ifdef DYNAMIC_EXECUTION_PROFILE
1171#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 dxpairs[lastopcode][opcode]++;
1173 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001174#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001176#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001177
Guido van Rossum96a42c81992-01-12 02:29:51 +00001178#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 if (lltrace) {
1182 if (HAS_ARG(opcode)) {
1183 printf("%d: %d, %d\n",
1184 f->f_lasti, opcode, oparg);
1185 }
1186 else {
1187 printf("%d: %d\n",
1188 f->f_lasti, opcode);
1189 }
1190 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001191#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 /* BEWARE!
1196 It is essential that any operation that fails sets either
1197 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1198 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 TARGET(NOP)
1201 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001202
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001203 TARGET(LOAD_FAST) {
1204 PyObject *value = GETLOCAL(oparg);
1205 if (value == NULL) {
1206 format_exc_check_arg(PyExc_UnboundLocalError,
1207 UNBOUNDLOCAL_ERROR_MSG,
1208 PyTuple_GetItem(co->co_varnames, oparg));
1209 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001211 Py_INCREF(value);
1212 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001214 }
1215
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001216 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001217 TARGET(LOAD_CONST) {
1218 PyObject *value = GETITEM(consts, oparg);
1219 Py_INCREF(value);
1220 PUSH(value);
1221 FAST_DISPATCH();
1222 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001223
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001224 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001225 TARGET(STORE_FAST) {
1226 PyObject *value = POP();
1227 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001229 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001230
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001231 TARGET(POP_TOP) {
1232 PyObject *value = POP();
1233 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001235 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001236
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001237 TARGET(ROT_TWO) {
1238 PyObject *top = TOP();
1239 PyObject *second = SECOND();
1240 SET_TOP(second);
1241 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001243 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001244
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001245 TARGET(ROT_THREE) {
1246 PyObject *top = TOP();
1247 PyObject *second = SECOND();
1248 PyObject *third = THIRD();
1249 SET_TOP(second);
1250 SET_SECOND(third);
1251 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001253 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001254
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001255 TARGET(DUP_TOP) {
1256 PyObject *top = TOP();
1257 Py_INCREF(top);
1258 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001260 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001261
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001262 TARGET(DUP_TOP_TWO) {
1263 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001264 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001265 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001266 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001267 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001268 SET_TOP(top);
1269 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001270 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001271 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001272
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001273 TARGET(UNARY_POSITIVE) {
1274 PyObject *value = TOP();
1275 PyObject *res = PyNumber_Positive(value);
1276 Py_DECREF(value);
1277 SET_TOP(res);
1278 if (res == NULL)
1279 goto error;
1280 DISPATCH();
1281 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001282
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001283 TARGET(UNARY_NEGATIVE) {
1284 PyObject *value = TOP();
1285 PyObject *res = PyNumber_Negative(value);
1286 Py_DECREF(value);
1287 SET_TOP(res);
1288 if (res == NULL)
1289 goto error;
1290 DISPATCH();
1291 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001292
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001293 TARGET(UNARY_NOT) {
1294 PyObject *value = TOP();
1295 int err = PyObject_IsTrue(value);
1296 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 if (err == 0) {
1298 Py_INCREF(Py_True);
1299 SET_TOP(Py_True);
1300 DISPATCH();
1301 }
1302 else if (err > 0) {
1303 Py_INCREF(Py_False);
1304 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 DISPATCH();
1306 }
1307 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001308 goto error;
1309 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001310
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001311 TARGET(UNARY_INVERT) {
1312 PyObject *value = TOP();
1313 PyObject *res = PyNumber_Invert(value);
1314 Py_DECREF(value);
1315 SET_TOP(res);
1316 if (res == NULL)
1317 goto error;
1318 DISPATCH();
1319 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001320
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001321 TARGET(BINARY_POWER) {
1322 PyObject *exp = POP();
1323 PyObject *base = TOP();
1324 PyObject *res = PyNumber_Power(base, exp, Py_None);
1325 Py_DECREF(base);
1326 Py_DECREF(exp);
1327 SET_TOP(res);
1328 if (res == NULL)
1329 goto error;
1330 DISPATCH();
1331 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001332
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001333 TARGET(BINARY_MULTIPLY) {
1334 PyObject *right = POP();
1335 PyObject *left = TOP();
1336 PyObject *res = PyNumber_Multiply(left, right);
1337 Py_DECREF(left);
1338 Py_DECREF(right);
1339 SET_TOP(res);
1340 if (res == NULL)
1341 goto error;
1342 DISPATCH();
1343 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001344
Benjamin Petersond51374e2014-04-09 23:55:56 -04001345 TARGET(BINARY_MATRIX_MULTIPLY) {
1346 PyObject *right = POP();
1347 PyObject *left = TOP();
1348 PyObject *res = PyNumber_MatrixMultiply(left, right);
1349 Py_DECREF(left);
1350 Py_DECREF(right);
1351 SET_TOP(res);
1352 if (res == NULL)
1353 goto error;
1354 DISPATCH();
1355 }
1356
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001357 TARGET(BINARY_TRUE_DIVIDE) {
1358 PyObject *divisor = POP();
1359 PyObject *dividend = TOP();
1360 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1361 Py_DECREF(dividend);
1362 Py_DECREF(divisor);
1363 SET_TOP(quotient);
1364 if (quotient == NULL)
1365 goto error;
1366 DISPATCH();
1367 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001368
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001369 TARGET(BINARY_FLOOR_DIVIDE) {
1370 PyObject *divisor = POP();
1371 PyObject *dividend = TOP();
1372 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1373 Py_DECREF(dividend);
1374 Py_DECREF(divisor);
1375 SET_TOP(quotient);
1376 if (quotient == NULL)
1377 goto error;
1378 DISPATCH();
1379 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001380
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001381 TARGET(BINARY_MODULO) {
1382 PyObject *divisor = POP();
1383 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001384 PyObject *res;
1385 if (PyUnicode_CheckExact(dividend) && (
1386 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1387 // fast path; string formatting, but not if the RHS is a str subclass
1388 // (see issue28598)
1389 res = PyUnicode_Format(dividend, divisor);
1390 } else {
1391 res = PyNumber_Remainder(dividend, divisor);
1392 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001393 Py_DECREF(divisor);
1394 Py_DECREF(dividend);
1395 SET_TOP(res);
1396 if (res == NULL)
1397 goto error;
1398 DISPATCH();
1399 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001400
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001401 TARGET(BINARY_ADD) {
1402 PyObject *right = POP();
1403 PyObject *left = TOP();
1404 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001405 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1406 CPython using bytecode, it is simply worthless.
1407 See http://bugs.python.org/issue21955 and
1408 http://bugs.python.org/issue10044 for the discussion. In short,
1409 no patch shown any impact on a realistic benchmark, only a minor
1410 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001411 if (PyUnicode_CheckExact(left) &&
1412 PyUnicode_CheckExact(right)) {
1413 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001414 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001415 }
1416 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001417 sum = PyNumber_Add(left, right);
1418 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001419 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001420 Py_DECREF(right);
1421 SET_TOP(sum);
1422 if (sum == NULL)
1423 goto error;
1424 DISPATCH();
1425 }
1426
1427 TARGET(BINARY_SUBTRACT) {
1428 PyObject *right = POP();
1429 PyObject *left = TOP();
1430 PyObject *diff = PyNumber_Subtract(left, right);
1431 Py_DECREF(right);
1432 Py_DECREF(left);
1433 SET_TOP(diff);
1434 if (diff == NULL)
1435 goto error;
1436 DISPATCH();
1437 }
1438
1439 TARGET(BINARY_SUBSCR) {
1440 PyObject *sub = POP();
1441 PyObject *container = TOP();
1442 PyObject *res = PyObject_GetItem(container, sub);
1443 Py_DECREF(container);
1444 Py_DECREF(sub);
1445 SET_TOP(res);
1446 if (res == NULL)
1447 goto error;
1448 DISPATCH();
1449 }
1450
1451 TARGET(BINARY_LSHIFT) {
1452 PyObject *right = POP();
1453 PyObject *left = TOP();
1454 PyObject *res = PyNumber_Lshift(left, right);
1455 Py_DECREF(left);
1456 Py_DECREF(right);
1457 SET_TOP(res);
1458 if (res == NULL)
1459 goto error;
1460 DISPATCH();
1461 }
1462
1463 TARGET(BINARY_RSHIFT) {
1464 PyObject *right = POP();
1465 PyObject *left = TOP();
1466 PyObject *res = PyNumber_Rshift(left, right);
1467 Py_DECREF(left);
1468 Py_DECREF(right);
1469 SET_TOP(res);
1470 if (res == NULL)
1471 goto error;
1472 DISPATCH();
1473 }
1474
1475 TARGET(BINARY_AND) {
1476 PyObject *right = POP();
1477 PyObject *left = TOP();
1478 PyObject *res = PyNumber_And(left, right);
1479 Py_DECREF(left);
1480 Py_DECREF(right);
1481 SET_TOP(res);
1482 if (res == NULL)
1483 goto error;
1484 DISPATCH();
1485 }
1486
1487 TARGET(BINARY_XOR) {
1488 PyObject *right = POP();
1489 PyObject *left = TOP();
1490 PyObject *res = PyNumber_Xor(left, right);
1491 Py_DECREF(left);
1492 Py_DECREF(right);
1493 SET_TOP(res);
1494 if (res == NULL)
1495 goto error;
1496 DISPATCH();
1497 }
1498
1499 TARGET(BINARY_OR) {
1500 PyObject *right = POP();
1501 PyObject *left = TOP();
1502 PyObject *res = PyNumber_Or(left, right);
1503 Py_DECREF(left);
1504 Py_DECREF(right);
1505 SET_TOP(res);
1506 if (res == NULL)
1507 goto error;
1508 DISPATCH();
1509 }
1510
1511 TARGET(LIST_APPEND) {
1512 PyObject *v = POP();
1513 PyObject *list = PEEK(oparg);
1514 int err;
1515 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001517 if (err != 0)
1518 goto error;
1519 PREDICT(JUMP_ABSOLUTE);
1520 DISPATCH();
1521 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001522
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001523 TARGET(SET_ADD) {
1524 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001525 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001526 int err;
1527 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001529 if (err != 0)
1530 goto error;
1531 PREDICT(JUMP_ABSOLUTE);
1532 DISPATCH();
1533 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001534
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001535 TARGET(INPLACE_POWER) {
1536 PyObject *exp = POP();
1537 PyObject *base = TOP();
1538 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1539 Py_DECREF(base);
1540 Py_DECREF(exp);
1541 SET_TOP(res);
1542 if (res == NULL)
1543 goto error;
1544 DISPATCH();
1545 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001546
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001547 TARGET(INPLACE_MULTIPLY) {
1548 PyObject *right = POP();
1549 PyObject *left = TOP();
1550 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1551 Py_DECREF(left);
1552 Py_DECREF(right);
1553 SET_TOP(res);
1554 if (res == NULL)
1555 goto error;
1556 DISPATCH();
1557 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001558
Benjamin Petersond51374e2014-04-09 23:55:56 -04001559 TARGET(INPLACE_MATRIX_MULTIPLY) {
1560 PyObject *right = POP();
1561 PyObject *left = TOP();
1562 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1563 Py_DECREF(left);
1564 Py_DECREF(right);
1565 SET_TOP(res);
1566 if (res == NULL)
1567 goto error;
1568 DISPATCH();
1569 }
1570
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001571 TARGET(INPLACE_TRUE_DIVIDE) {
1572 PyObject *divisor = POP();
1573 PyObject *dividend = TOP();
1574 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1575 Py_DECREF(dividend);
1576 Py_DECREF(divisor);
1577 SET_TOP(quotient);
1578 if (quotient == NULL)
1579 goto error;
1580 DISPATCH();
1581 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001582
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001583 TARGET(INPLACE_FLOOR_DIVIDE) {
1584 PyObject *divisor = POP();
1585 PyObject *dividend = TOP();
1586 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1587 Py_DECREF(dividend);
1588 Py_DECREF(divisor);
1589 SET_TOP(quotient);
1590 if (quotient == NULL)
1591 goto error;
1592 DISPATCH();
1593 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001594
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001595 TARGET(INPLACE_MODULO) {
1596 PyObject *right = POP();
1597 PyObject *left = TOP();
1598 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1599 Py_DECREF(left);
1600 Py_DECREF(right);
1601 SET_TOP(mod);
1602 if (mod == NULL)
1603 goto error;
1604 DISPATCH();
1605 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001606
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001607 TARGET(INPLACE_ADD) {
1608 PyObject *right = POP();
1609 PyObject *left = TOP();
1610 PyObject *sum;
1611 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1612 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001613 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001614 }
1615 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001616 sum = PyNumber_InPlaceAdd(left, right);
1617 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001618 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001619 Py_DECREF(right);
1620 SET_TOP(sum);
1621 if (sum == NULL)
1622 goto error;
1623 DISPATCH();
1624 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001625
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001626 TARGET(INPLACE_SUBTRACT) {
1627 PyObject *right = POP();
1628 PyObject *left = TOP();
1629 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1630 Py_DECREF(left);
1631 Py_DECREF(right);
1632 SET_TOP(diff);
1633 if (diff == NULL)
1634 goto error;
1635 DISPATCH();
1636 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001637
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001638 TARGET(INPLACE_LSHIFT) {
1639 PyObject *right = POP();
1640 PyObject *left = TOP();
1641 PyObject *res = PyNumber_InPlaceLshift(left, right);
1642 Py_DECREF(left);
1643 Py_DECREF(right);
1644 SET_TOP(res);
1645 if (res == NULL)
1646 goto error;
1647 DISPATCH();
1648 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001649
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001650 TARGET(INPLACE_RSHIFT) {
1651 PyObject *right = POP();
1652 PyObject *left = TOP();
1653 PyObject *res = PyNumber_InPlaceRshift(left, right);
1654 Py_DECREF(left);
1655 Py_DECREF(right);
1656 SET_TOP(res);
1657 if (res == NULL)
1658 goto error;
1659 DISPATCH();
1660 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001661
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001662 TARGET(INPLACE_AND) {
1663 PyObject *right = POP();
1664 PyObject *left = TOP();
1665 PyObject *res = PyNumber_InPlaceAnd(left, right);
1666 Py_DECREF(left);
1667 Py_DECREF(right);
1668 SET_TOP(res);
1669 if (res == NULL)
1670 goto error;
1671 DISPATCH();
1672 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001673
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001674 TARGET(INPLACE_XOR) {
1675 PyObject *right = POP();
1676 PyObject *left = TOP();
1677 PyObject *res = PyNumber_InPlaceXor(left, right);
1678 Py_DECREF(left);
1679 Py_DECREF(right);
1680 SET_TOP(res);
1681 if (res == NULL)
1682 goto error;
1683 DISPATCH();
1684 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001685
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001686 TARGET(INPLACE_OR) {
1687 PyObject *right = POP();
1688 PyObject *left = TOP();
1689 PyObject *res = PyNumber_InPlaceOr(left, right);
1690 Py_DECREF(left);
1691 Py_DECREF(right);
1692 SET_TOP(res);
1693 if (res == NULL)
1694 goto error;
1695 DISPATCH();
1696 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001697
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001698 TARGET(STORE_SUBSCR) {
1699 PyObject *sub = TOP();
1700 PyObject *container = SECOND();
1701 PyObject *v = THIRD();
1702 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 STACKADJ(-3);
Martin Panter95f53c12016-07-18 08:23:26 +00001704 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001705 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001707 Py_DECREF(container);
1708 Py_DECREF(sub);
1709 if (err != 0)
1710 goto error;
1711 DISPATCH();
1712 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001713
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001714 TARGET(STORE_ANNOTATION) {
1715 _Py_IDENTIFIER(__annotations__);
1716 PyObject *ann_dict;
1717 PyObject *ann = POP();
1718 PyObject *name = GETITEM(names, oparg);
1719 int err;
1720 if (f->f_locals == NULL) {
1721 PyErr_Format(PyExc_SystemError,
1722 "no locals found when storing annotation");
1723 Py_DECREF(ann);
1724 goto error;
1725 }
1726 /* first try to get __annotations__ from locals... */
1727 if (PyDict_CheckExact(f->f_locals)) {
1728 ann_dict = _PyDict_GetItemId(f->f_locals,
1729 &PyId___annotations__);
1730 if (ann_dict == NULL) {
1731 PyErr_SetString(PyExc_NameError,
1732 "__annotations__ not found");
1733 Py_DECREF(ann);
1734 goto error;
1735 }
1736 Py_INCREF(ann_dict);
1737 }
1738 else {
1739 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
1740 if (ann_str == NULL) {
1741 Py_DECREF(ann);
1742 goto error;
1743 }
1744 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
1745 if (ann_dict == NULL) {
1746 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
1747 PyErr_SetString(PyExc_NameError,
1748 "__annotations__ not found");
1749 }
1750 Py_DECREF(ann);
1751 goto error;
1752 }
1753 }
1754 /* ...if succeeded, __annotations__[name] = ann */
1755 if (PyDict_CheckExact(ann_dict)) {
1756 err = PyDict_SetItem(ann_dict, name, ann);
1757 }
1758 else {
1759 err = PyObject_SetItem(ann_dict, name, ann);
1760 }
1761 Py_DECREF(ann_dict);
Yury Selivanov50c584f2016-09-08 23:38:21 -07001762 Py_DECREF(ann);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001763 if (err != 0) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001764 goto error;
1765 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001766 DISPATCH();
1767 }
1768
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001769 TARGET(DELETE_SUBSCR) {
1770 PyObject *sub = TOP();
1771 PyObject *container = SECOND();
1772 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 STACKADJ(-2);
Martin Panter95f53c12016-07-18 08:23:26 +00001774 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001775 err = PyObject_DelItem(container, sub);
1776 Py_DECREF(container);
1777 Py_DECREF(sub);
1778 if (err != 0)
1779 goto error;
1780 DISPATCH();
1781 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001782
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001783 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001784 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001785 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001786 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001787 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001788 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 PyErr_SetString(PyExc_RuntimeError,
1790 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001791 Py_DECREF(value);
1792 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001793 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001794 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001795 Py_DECREF(value);
1796 if (res == NULL)
1797 goto error;
1798 Py_DECREF(res);
1799 DISPATCH();
1800 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001801
Thomas Wouters434d0822000-08-24 20:11:32 +00001802#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001803 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001804#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001805 TARGET(RAISE_VARARGS) {
1806 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 switch (oparg) {
1808 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001809 cause = POP(); /* cause */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001810 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001811 exc = POP(); /* exc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001812 case 0: /* Fallthrough */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001813 if (do_raise(exc, cause)) {
1814 why = WHY_EXCEPTION;
1815 goto fast_block_end;
1816 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001817 break;
1818 default:
1819 PyErr_SetString(PyExc_SystemError,
1820 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001821 break;
1822 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001823 goto error;
1824 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001825
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001826 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827 retval = POP();
1828 why = WHY_RETURN;
1829 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001830 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001831
Yury Selivanov75445082015-05-11 22:57:16 -04001832 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001833 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001834 PyObject *iter = NULL;
1835 PyObject *awaitable = NULL;
1836 PyObject *obj = TOP();
1837 PyTypeObject *type = Py_TYPE(obj);
1838
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001839 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001840 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001841 }
Yury Selivanov75445082015-05-11 22:57:16 -04001842
1843 if (getter != NULL) {
1844 iter = (*getter)(obj);
1845 Py_DECREF(obj);
1846 if (iter == NULL) {
1847 SET_TOP(NULL);
1848 goto error;
1849 }
1850 }
1851 else {
1852 SET_TOP(NULL);
1853 PyErr_Format(
1854 PyExc_TypeError,
1855 "'async for' requires an object with "
1856 "__aiter__ method, got %.100s",
1857 type->tp_name);
1858 Py_DECREF(obj);
1859 goto error;
1860 }
1861
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001862 if (Py_TYPE(iter)->tp_as_async != NULL &&
1863 Py_TYPE(iter)->tp_as_async->am_anext != NULL) {
1864
1865 /* Starting with CPython 3.5.2 __aiter__ should return
1866 asynchronous iterators directly (not awaitables that
1867 resolve to asynchronous iterators.)
1868
1869 Therefore, we check if the object that was returned
1870 from __aiter__ has an __anext__ method. If it does,
1871 we wrap it in an awaitable that resolves to `iter`.
1872
1873 See http://bugs.python.org/issue27243 for more
1874 details.
1875 */
1876
1877 PyObject *wrapper = _PyAIterWrapper_New(iter);
1878 Py_DECREF(iter);
1879 SET_TOP(wrapper);
1880 DISPATCH();
1881 }
1882
Yury Selivanov5376ba92015-06-22 12:19:30 -04001883 awaitable = _PyCoro_GetAwaitableIter(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001884 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001885 _PyErr_FormatFromCause(
Yury Selivanov75445082015-05-11 22:57:16 -04001886 PyExc_TypeError,
1887 "'async for' received an invalid object "
1888 "from __aiter__: %.100s",
1889 Py_TYPE(iter)->tp_name);
1890
Yury Selivanov398ff912017-03-02 22:20:00 -05001891 SET_TOP(NULL);
Yury Selivanov75445082015-05-11 22:57:16 -04001892 Py_DECREF(iter);
1893 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001894 } else {
Yury Selivanov75445082015-05-11 22:57:16 -04001895 Py_DECREF(iter);
1896
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001897 if (PyErr_WarnFormat(
Yury Selivanov2edd8a12016-11-08 15:13:07 -05001898 PyExc_DeprecationWarning, 1,
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001899 "'%.100s' implements legacy __aiter__ protocol; "
1900 "__aiter__ should return an asynchronous "
1901 "iterator, not awaitable",
1902 type->tp_name))
1903 {
1904 /* Warning was converted to an error. */
1905 Py_DECREF(awaitable);
1906 SET_TOP(NULL);
1907 goto error;
1908 }
1909 }
1910
Yury Selivanov75445082015-05-11 22:57:16 -04001911 SET_TOP(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001912 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001913 DISPATCH();
1914 }
1915
1916 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001917 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001918 PyObject *next_iter = NULL;
1919 PyObject *awaitable = NULL;
1920 PyObject *aiter = TOP();
1921 PyTypeObject *type = Py_TYPE(aiter);
1922
Yury Selivanoveb636452016-09-08 22:01:51 -07001923 if (PyAsyncGen_CheckExact(aiter)) {
1924 awaitable = type->tp_as_async->am_anext(aiter);
1925 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001926 goto error;
1927 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001928 } else {
1929 if (type->tp_as_async != NULL){
1930 getter = type->tp_as_async->am_anext;
1931 }
Yury Selivanov75445082015-05-11 22:57:16 -04001932
Yury Selivanoveb636452016-09-08 22:01:51 -07001933 if (getter != NULL) {
1934 next_iter = (*getter)(aiter);
1935 if (next_iter == NULL) {
1936 goto error;
1937 }
1938 }
1939 else {
1940 PyErr_Format(
1941 PyExc_TypeError,
1942 "'async for' requires an iterator with "
1943 "__anext__ method, got %.100s",
1944 type->tp_name);
1945 goto error;
1946 }
Yury Selivanov75445082015-05-11 22:57:16 -04001947
Yury Selivanoveb636452016-09-08 22:01:51 -07001948 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1949 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001950 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001951 PyExc_TypeError,
1952 "'async for' received an invalid object "
1953 "from __anext__: %.100s",
1954 Py_TYPE(next_iter)->tp_name);
1955
1956 Py_DECREF(next_iter);
1957 goto error;
1958 } else {
1959 Py_DECREF(next_iter);
1960 }
1961 }
Yury Selivanov75445082015-05-11 22:57:16 -04001962
1963 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001964 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001965 DISPATCH();
1966 }
1967
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001968 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001969 TARGET(GET_AWAITABLE) {
1970 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001971 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001972
1973 Py_DECREF(iterable);
1974
Yury Selivanovc724bae2016-03-02 11:30:46 -05001975 if (iter != NULL && PyCoro_CheckExact(iter)) {
1976 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1977 if (yf != NULL) {
1978 /* `iter` is a coroutine object that is being
1979 awaited, `yf` is a pointer to the current awaitable
1980 being awaited on. */
1981 Py_DECREF(yf);
1982 Py_CLEAR(iter);
1983 PyErr_SetString(
1984 PyExc_RuntimeError,
1985 "coroutine is being awaited already");
1986 /* The code below jumps to `error` if `iter` is NULL. */
1987 }
1988 }
1989
Yury Selivanov75445082015-05-11 22:57:16 -04001990 SET_TOP(iter); /* Even if it's NULL */
1991
1992 if (iter == NULL) {
1993 goto error;
1994 }
1995
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001996 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001997 DISPATCH();
1998 }
1999
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002000 TARGET(YIELD_FROM) {
2001 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002002 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002003 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002004 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2005 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002006 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002007 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002008 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002009 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002010 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002011 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002012 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002013 Py_DECREF(v);
2014 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002015 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002016 if (tstate->c_tracefunc != NULL
2017 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002018 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002019 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002020 if (err < 0)
2021 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002022 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002023 SET_TOP(val);
2024 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002025 }
Martin Panter95f53c12016-07-18 08:23:26 +00002026 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002027 f->f_stacktop = stack_pointer;
2028 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002029 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002030 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002031 f->f_lasti -= sizeof(_Py_CODEUNIT);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002032 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002033 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002034
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002035 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002036 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002037
2038 if (co->co_flags & CO_ASYNC_GENERATOR) {
2039 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2040 Py_DECREF(retval);
2041 if (w == NULL) {
2042 retval = NULL;
2043 goto error;
2044 }
2045 retval = w;
2046 }
2047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 f->f_stacktop = stack_pointer;
2049 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002050 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002051 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002052
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002053 TARGET(POP_EXCEPT) {
2054 PyTryBlock *b = PyFrame_BlockPop(f);
2055 if (b->b_type != EXCEPT_HANDLER) {
2056 PyErr_SetString(PyExc_SystemError,
2057 "popped block is not an except handler");
2058 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002060 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002062 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002063
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002064 PREDICTED(POP_BLOCK);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002065 TARGET(POP_BLOCK) {
2066 PyTryBlock *b = PyFrame_BlockPop(f);
2067 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002068 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002069 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002072 TARGET(END_FINALLY) {
2073 PyObject *status = POP();
2074 if (PyLong_Check(status)) {
2075 why = (enum why_code) PyLong_AS_LONG(status);
2076 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077 if (why == WHY_RETURN ||
2078 why == WHY_CONTINUE)
2079 retval = POP();
2080 if (why == WHY_SILENCED) {
2081 /* An exception was silenced by 'with', we must
2082 manually unwind the EXCEPT_HANDLER block which was
2083 created when the exception was caught, otherwise
2084 the stack will be in an inconsistent state. */
2085 PyTryBlock *b = PyFrame_BlockPop(f);
2086 assert(b->b_type == EXCEPT_HANDLER);
2087 UNWIND_EXCEPT_HANDLER(b);
2088 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002089 Py_DECREF(status);
2090 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002092 Py_DECREF(status);
2093 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002094 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002095 else if (PyExceptionClass_Check(status)) {
2096 PyObject *exc = POP();
2097 PyObject *tb = POP();
2098 PyErr_Restore(status, exc, tb);
2099 why = WHY_EXCEPTION;
2100 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002102 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103 PyErr_SetString(PyExc_SystemError,
2104 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002105 Py_DECREF(status);
2106 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002107 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002108 Py_DECREF(status);
2109 DISPATCH();
2110 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002111
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002112 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002113 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002114
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002115 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002116 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002117 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
2118 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002119 PyErr_SetString(PyExc_NameError,
2120 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002121 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002122 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002123 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002124 }
2125 else {
2126 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2127 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002128 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002129 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2130 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002131 if (PyErr_ExceptionMatches(PyExc_KeyError))
2132 PyErr_SetString(PyExc_NameError,
2133 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002134 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002135 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002136 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002137 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002138 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002139 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002140
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002141 TARGET(STORE_NAME) {
2142 PyObject *name = GETITEM(names, oparg);
2143 PyObject *v = POP();
2144 PyObject *ns = f->f_locals;
2145 int err;
2146 if (ns == NULL) {
2147 PyErr_Format(PyExc_SystemError,
2148 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002149 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002150 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002151 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002152 if (PyDict_CheckExact(ns))
2153 err = PyDict_SetItem(ns, name, v);
2154 else
2155 err = PyObject_SetItem(ns, name, v);
2156 Py_DECREF(v);
2157 if (err != 0)
2158 goto error;
2159 DISPATCH();
2160 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002161
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002162 TARGET(DELETE_NAME) {
2163 PyObject *name = GETITEM(names, oparg);
2164 PyObject *ns = f->f_locals;
2165 int err;
2166 if (ns == NULL) {
2167 PyErr_Format(PyExc_SystemError,
2168 "no locals when deleting %R", name);
2169 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002171 err = PyObject_DelItem(ns, name);
2172 if (err != 0) {
2173 format_exc_check_arg(PyExc_NameError,
2174 NAME_ERROR_MSG,
2175 name);
2176 goto error;
2177 }
2178 DISPATCH();
2179 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002180
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002181 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002182 TARGET(UNPACK_SEQUENCE) {
2183 PyObject *seq = POP(), *item, **items;
2184 if (PyTuple_CheckExact(seq) &&
2185 PyTuple_GET_SIZE(seq) == oparg) {
2186 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002187 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002188 item = items[oparg];
2189 Py_INCREF(item);
2190 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002192 } else if (PyList_CheckExact(seq) &&
2193 PyList_GET_SIZE(seq) == oparg) {
2194 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002196 item = items[oparg];
2197 Py_INCREF(item);
2198 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002200 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002201 stack_pointer + oparg)) {
2202 STACKADJ(oparg);
2203 } else {
2204 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002205 Py_DECREF(seq);
2206 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002207 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002208 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002209 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002210 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002211
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002212 TARGET(UNPACK_EX) {
2213 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2214 PyObject *seq = POP();
2215
2216 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2217 stack_pointer + totalargs)) {
2218 stack_pointer += totalargs;
2219 } else {
2220 Py_DECREF(seq);
2221 goto error;
2222 }
2223 Py_DECREF(seq);
2224 DISPATCH();
2225 }
2226
2227 TARGET(STORE_ATTR) {
2228 PyObject *name = GETITEM(names, oparg);
2229 PyObject *owner = TOP();
2230 PyObject *v = SECOND();
2231 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002233 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002234 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002235 Py_DECREF(owner);
2236 if (err != 0)
2237 goto error;
2238 DISPATCH();
2239 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002240
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002241 TARGET(DELETE_ATTR) {
2242 PyObject *name = GETITEM(names, oparg);
2243 PyObject *owner = POP();
2244 int err;
2245 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2246 Py_DECREF(owner);
2247 if (err != 0)
2248 goto error;
2249 DISPATCH();
2250 }
2251
2252 TARGET(STORE_GLOBAL) {
2253 PyObject *name = GETITEM(names, oparg);
2254 PyObject *v = POP();
2255 int err;
2256 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002258 if (err != 0)
2259 goto error;
2260 DISPATCH();
2261 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002262
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002263 TARGET(DELETE_GLOBAL) {
2264 PyObject *name = GETITEM(names, oparg);
2265 int err;
2266 err = PyDict_DelItem(f->f_globals, name);
2267 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002268 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002269 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002270 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002271 }
2272 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002273 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002274
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002275 TARGET(LOAD_NAME) {
2276 PyObject *name = GETITEM(names, oparg);
2277 PyObject *locals = f->f_locals;
2278 PyObject *v;
2279 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002280 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002281 "no locals when loading %R", name);
2282 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002284 if (PyDict_CheckExact(locals)) {
2285 v = PyDict_GetItem(locals, name);
2286 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 }
2288 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002289 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002290 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002291 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2292 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002293 PyErr_Clear();
2294 }
2295 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002296 if (v == NULL) {
2297 v = PyDict_GetItem(f->f_globals, name);
2298 Py_XINCREF(v);
2299 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002300 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002301 v = PyDict_GetItem(f->f_builtins, name);
2302 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002303 format_exc_check_arg(
2304 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002305 NAME_ERROR_MSG, name);
2306 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002307 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002308 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002309 }
2310 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002311 v = PyObject_GetItem(f->f_builtins, name);
2312 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002313 if (PyErr_ExceptionMatches(PyExc_KeyError))
2314 format_exc_check_arg(
2315 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002316 NAME_ERROR_MSG, name);
2317 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002318 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002319 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002320 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002321 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002322 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002323 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002324 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002325
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002326 TARGET(LOAD_GLOBAL) {
2327 PyObject *name = GETITEM(names, oparg);
2328 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002329 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002330 && PyDict_CheckExact(f->f_builtins))
2331 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002332 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002333 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002334 name);
2335 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002336 if (!_PyErr_OCCURRED()) {
2337 /* _PyDict_LoadGlobal() returns NULL without raising
2338 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002339 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002340 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002341 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002342 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002343 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002344 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002345 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002346 else {
2347 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002348
2349 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002350 v = PyObject_GetItem(f->f_globals, name);
2351 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002352 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2353 goto error;
2354 PyErr_Clear();
2355
Victor Stinnerb4efc962015-11-20 09:24:02 +01002356 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002357 v = PyObject_GetItem(f->f_builtins, name);
2358 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002359 if (PyErr_ExceptionMatches(PyExc_KeyError))
2360 format_exc_check_arg(
2361 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002362 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002363 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002364 }
2365 }
2366 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002367 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002369 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002370
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002371 TARGET(DELETE_FAST) {
2372 PyObject *v = GETLOCAL(oparg);
2373 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002374 SETLOCAL(oparg, NULL);
2375 DISPATCH();
2376 }
2377 format_exc_check_arg(
2378 PyExc_UnboundLocalError,
2379 UNBOUNDLOCAL_ERROR_MSG,
2380 PyTuple_GetItem(co->co_varnames, oparg)
2381 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002382 goto error;
2383 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002384
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002385 TARGET(DELETE_DEREF) {
2386 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002387 PyObject *oldobj = PyCell_GET(cell);
2388 if (oldobj != NULL) {
2389 PyCell_SET(cell, NULL);
2390 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002391 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002392 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002393 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002394 goto error;
2395 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002396
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002397 TARGET(LOAD_CLOSURE) {
2398 PyObject *cell = freevars[oparg];
2399 Py_INCREF(cell);
2400 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002402 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002403
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002404 TARGET(LOAD_CLASSDEREF) {
2405 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002406 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002407 assert(locals);
2408 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2409 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2410 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2411 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2412 if (PyDict_CheckExact(locals)) {
2413 value = PyDict_GetItem(locals, name);
2414 Py_XINCREF(value);
2415 }
2416 else {
2417 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002418 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002419 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2420 goto error;
2421 PyErr_Clear();
2422 }
2423 }
2424 if (!value) {
2425 PyObject *cell = freevars[oparg];
2426 value = PyCell_GET(cell);
2427 if (value == NULL) {
2428 format_exc_unbound(co, oparg);
2429 goto error;
2430 }
2431 Py_INCREF(value);
2432 }
2433 PUSH(value);
2434 DISPATCH();
2435 }
2436
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002437 TARGET(LOAD_DEREF) {
2438 PyObject *cell = freevars[oparg];
2439 PyObject *value = PyCell_GET(cell);
2440 if (value == NULL) {
2441 format_exc_unbound(co, oparg);
2442 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002443 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002444 Py_INCREF(value);
2445 PUSH(value);
2446 DISPATCH();
2447 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002448
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002449 TARGET(STORE_DEREF) {
2450 PyObject *v = POP();
2451 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002452 PyObject *oldobj = PyCell_GET(cell);
2453 PyCell_SET(cell, v);
2454 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002455 DISPATCH();
2456 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002457
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002458 TARGET(BUILD_STRING) {
2459 PyObject *str;
2460 PyObject *empty = PyUnicode_New(0, 0);
2461 if (empty == NULL) {
2462 goto error;
2463 }
2464 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2465 Py_DECREF(empty);
2466 if (str == NULL)
2467 goto error;
2468 while (--oparg >= 0) {
2469 PyObject *item = POP();
2470 Py_DECREF(item);
2471 }
2472 PUSH(str);
2473 DISPATCH();
2474 }
2475
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002476 TARGET(BUILD_TUPLE) {
2477 PyObject *tup = PyTuple_New(oparg);
2478 if (tup == NULL)
2479 goto error;
2480 while (--oparg >= 0) {
2481 PyObject *item = POP();
2482 PyTuple_SET_ITEM(tup, oparg, item);
2483 }
2484 PUSH(tup);
2485 DISPATCH();
2486 }
2487
2488 TARGET(BUILD_LIST) {
2489 PyObject *list = PyList_New(oparg);
2490 if (list == NULL)
2491 goto error;
2492 while (--oparg >= 0) {
2493 PyObject *item = POP();
2494 PyList_SET_ITEM(list, oparg, item);
2495 }
2496 PUSH(list);
2497 DISPATCH();
2498 }
2499
Serhiy Storchaka73442852016-10-02 10:33:46 +03002500 TARGET(BUILD_TUPLE_UNPACK_WITH_CALL)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002501 TARGET(BUILD_TUPLE_UNPACK)
2502 TARGET(BUILD_LIST_UNPACK) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002503 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002504 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002505 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002506 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002507
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002508 if (sum == NULL)
2509 goto error;
2510
2511 for (i = oparg; i > 0; i--) {
2512 PyObject *none_val;
2513
2514 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2515 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002516 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002517 PyErr_ExceptionMatches(PyExc_TypeError))
2518 {
2519 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002520 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002521 Py_DECREF(sum);
2522 goto error;
2523 }
2524 Py_DECREF(none_val);
2525 }
2526
2527 if (convert_to_tuple) {
2528 return_value = PyList_AsTuple(sum);
2529 Py_DECREF(sum);
2530 if (return_value == NULL)
2531 goto error;
2532 }
2533 else {
2534 return_value = sum;
2535 }
2536
2537 while (oparg--)
2538 Py_DECREF(POP());
2539 PUSH(return_value);
2540 DISPATCH();
2541 }
2542
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002543 TARGET(BUILD_SET) {
2544 PyObject *set = PySet_New(NULL);
2545 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002546 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002547 if (set == NULL)
2548 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002549 for (i = oparg; i > 0; i--) {
2550 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002551 if (err == 0)
2552 err = PySet_Add(set, item);
2553 Py_DECREF(item);
2554 }
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002555 STACKADJ(-oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002556 if (err != 0) {
2557 Py_DECREF(set);
2558 goto error;
2559 }
2560 PUSH(set);
2561 DISPATCH();
2562 }
2563
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002564 TARGET(BUILD_SET_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002565 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002566 PyObject *sum = PySet_New(NULL);
2567 if (sum == NULL)
2568 goto error;
2569
2570 for (i = oparg; i > 0; i--) {
2571 if (_PySet_Update(sum, PEEK(i)) < 0) {
2572 Py_DECREF(sum);
2573 goto error;
2574 }
2575 }
2576
2577 while (oparg--)
2578 Py_DECREF(POP());
2579 PUSH(sum);
2580 DISPATCH();
2581 }
2582
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002583 TARGET(BUILD_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002584 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002585 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2586 if (map == NULL)
2587 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002588 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002589 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002590 PyObject *key = PEEK(2*i);
2591 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002592 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002593 if (err != 0) {
2594 Py_DECREF(map);
2595 goto error;
2596 }
2597 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002598
2599 while (oparg--) {
2600 Py_DECREF(POP());
2601 Py_DECREF(POP());
2602 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002603 PUSH(map);
2604 DISPATCH();
2605 }
2606
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002607 TARGET(SETUP_ANNOTATIONS) {
2608 _Py_IDENTIFIER(__annotations__);
2609 int err;
2610 PyObject *ann_dict;
2611 if (f->f_locals == NULL) {
2612 PyErr_Format(PyExc_SystemError,
2613 "no locals found when setting up annotations");
2614 goto error;
2615 }
2616 /* check if __annotations__ in locals()... */
2617 if (PyDict_CheckExact(f->f_locals)) {
2618 ann_dict = _PyDict_GetItemId(f->f_locals,
2619 &PyId___annotations__);
2620 if (ann_dict == NULL) {
2621 /* ...if not, create a new one */
2622 ann_dict = PyDict_New();
2623 if (ann_dict == NULL) {
2624 goto error;
2625 }
2626 err = _PyDict_SetItemId(f->f_locals,
2627 &PyId___annotations__, ann_dict);
2628 Py_DECREF(ann_dict);
2629 if (err != 0) {
2630 goto error;
2631 }
2632 }
2633 }
2634 else {
2635 /* do the same if locals() is not a dict */
2636 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2637 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002638 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002639 }
2640 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2641 if (ann_dict == NULL) {
2642 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2643 goto error;
2644 }
2645 PyErr_Clear();
2646 ann_dict = PyDict_New();
2647 if (ann_dict == NULL) {
2648 goto error;
2649 }
2650 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2651 Py_DECREF(ann_dict);
2652 if (err != 0) {
2653 goto error;
2654 }
2655 }
2656 else {
2657 Py_DECREF(ann_dict);
2658 }
2659 }
2660 DISPATCH();
2661 }
2662
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002663 TARGET(BUILD_CONST_KEY_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002664 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002665 PyObject *map;
2666 PyObject *keys = TOP();
2667 if (!PyTuple_CheckExact(keys) ||
2668 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2669 PyErr_SetString(PyExc_SystemError,
2670 "bad BUILD_CONST_KEY_MAP keys argument");
2671 goto error;
2672 }
2673 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2674 if (map == NULL) {
2675 goto error;
2676 }
2677 for (i = oparg; i > 0; i--) {
2678 int err;
2679 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2680 PyObject *value = PEEK(i + 1);
2681 err = PyDict_SetItem(map, key, value);
2682 if (err != 0) {
2683 Py_DECREF(map);
2684 goto error;
2685 }
2686 }
2687
2688 Py_DECREF(POP());
2689 while (oparg--) {
2690 Py_DECREF(POP());
2691 }
2692 PUSH(map);
2693 DISPATCH();
2694 }
2695
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002696 TARGET(BUILD_MAP_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002697 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002698 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002699 if (sum == NULL)
2700 goto error;
2701
2702 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002703 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002704 if (PyDict_Update(sum, arg) < 0) {
2705 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2706 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002707 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002708 arg->ob_type->tp_name);
2709 }
2710 Py_DECREF(sum);
2711 goto error;
2712 }
2713 }
2714
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002715 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002716 Py_DECREF(POP());
2717 PUSH(sum);
2718 DISPATCH();
2719 }
2720
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002721 TARGET(BUILD_MAP_UNPACK_WITH_CALL) {
2722 Py_ssize_t i;
2723 PyObject *sum = PyDict_New();
2724 if (sum == NULL)
2725 goto error;
2726
2727 for (i = oparg; i > 0; i--) {
2728 PyObject *arg = PEEK(i);
2729 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
2730 PyObject *func = PEEK(2 + oparg);
2731 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002732 format_kwargs_mapping_error(func, arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002733 }
2734 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2735 PyObject *exc, *val, *tb;
2736 PyErr_Fetch(&exc, &val, &tb);
2737 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
2738 PyObject *key = PyTuple_GET_ITEM(val, 0);
2739 if (!PyUnicode_Check(key)) {
2740 PyErr_Format(PyExc_TypeError,
2741 "%.200s%.200s keywords must be strings",
2742 PyEval_GetFuncName(func),
2743 PyEval_GetFuncDesc(func));
2744 } else {
2745 PyErr_Format(PyExc_TypeError,
2746 "%.200s%.200s got multiple "
2747 "values for keyword argument '%U'",
2748 PyEval_GetFuncName(func),
2749 PyEval_GetFuncDesc(func),
2750 key);
2751 }
2752 Py_XDECREF(exc);
2753 Py_XDECREF(val);
2754 Py_XDECREF(tb);
2755 }
2756 else {
2757 PyErr_Restore(exc, val, tb);
2758 }
2759 }
2760 Py_DECREF(sum);
2761 goto error;
2762 }
2763 }
2764
2765 while (oparg--)
2766 Py_DECREF(POP());
2767 PUSH(sum);
2768 DISPATCH();
2769 }
2770
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002771 TARGET(MAP_ADD) {
2772 PyObject *key = TOP();
2773 PyObject *value = SECOND();
2774 PyObject *map;
2775 int err;
2776 STACKADJ(-2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002777 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002778 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002779 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002780 Py_DECREF(value);
2781 Py_DECREF(key);
2782 if (err != 0)
2783 goto error;
2784 PREDICT(JUMP_ABSOLUTE);
2785 DISPATCH();
2786 }
2787
2788 TARGET(LOAD_ATTR) {
2789 PyObject *name = GETITEM(names, oparg);
2790 PyObject *owner = TOP();
2791 PyObject *res = PyObject_GetAttr(owner, name);
2792 Py_DECREF(owner);
2793 SET_TOP(res);
2794 if (res == NULL)
2795 goto error;
2796 DISPATCH();
2797 }
2798
2799 TARGET(COMPARE_OP) {
2800 PyObject *right = POP();
2801 PyObject *left = TOP();
2802 PyObject *res = cmp_outcome(oparg, left, right);
2803 Py_DECREF(left);
2804 Py_DECREF(right);
2805 SET_TOP(res);
2806 if (res == NULL)
2807 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 PREDICT(POP_JUMP_IF_FALSE);
2809 PREDICT(POP_JUMP_IF_TRUE);
2810 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002811 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002812
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002813 TARGET(IMPORT_NAME) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002814 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002815 PyObject *fromlist = POP();
2816 PyObject *level = TOP();
2817 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002818 res = import_name(f, name, fromlist, level);
2819 Py_DECREF(level);
2820 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002821 SET_TOP(res);
2822 if (res == NULL)
2823 goto error;
2824 DISPATCH();
2825 }
2826
2827 TARGET(IMPORT_STAR) {
2828 PyObject *from = POP(), *locals;
2829 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002830 if (PyFrame_FastToLocalsWithError(f) < 0) {
2831 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002832 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002833 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002834
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002835 locals = f->f_locals;
2836 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002837 PyErr_SetString(PyExc_SystemError,
2838 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002839 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002840 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002841 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002842 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002843 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002844 Py_DECREF(from);
2845 if (err != 0)
2846 goto error;
2847 DISPATCH();
2848 }
Guido van Rossum25831651993-05-19 14:50:45 +00002849
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002850 TARGET(IMPORT_FROM) {
2851 PyObject *name = GETITEM(names, oparg);
2852 PyObject *from = TOP();
2853 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002854 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002855 PUSH(res);
2856 if (res == NULL)
2857 goto error;
2858 DISPATCH();
2859 }
Thomas Wouters52152252000-08-17 22:55:00 +00002860
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002861 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002862 JUMPBY(oparg);
2863 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002864 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002865
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002866 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002867 TARGET(POP_JUMP_IF_FALSE) {
2868 PyObject *cond = POP();
2869 int err;
2870 if (cond == Py_True) {
2871 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 FAST_DISPATCH();
2873 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002874 if (cond == Py_False) {
2875 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 JUMPTO(oparg);
2877 FAST_DISPATCH();
2878 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002879 err = PyObject_IsTrue(cond);
2880 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002882 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002883 else if (err == 0)
2884 JUMPTO(oparg);
2885 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002886 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002887 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002888 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002889
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002890 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002891 TARGET(POP_JUMP_IF_TRUE) {
2892 PyObject *cond = POP();
2893 int err;
2894 if (cond == Py_False) {
2895 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002896 FAST_DISPATCH();
2897 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002898 if (cond == Py_True) {
2899 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002900 JUMPTO(oparg);
2901 FAST_DISPATCH();
2902 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002903 err = PyObject_IsTrue(cond);
2904 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002906 JUMPTO(oparg);
2907 }
2908 else if (err == 0)
2909 ;
2910 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002911 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002912 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002913 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002914
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002915 TARGET(JUMP_IF_FALSE_OR_POP) {
2916 PyObject *cond = TOP();
2917 int err;
2918 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002919 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002920 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002921 FAST_DISPATCH();
2922 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002923 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 JUMPTO(oparg);
2925 FAST_DISPATCH();
2926 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002927 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002928 if (err > 0) {
2929 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002930 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002931 }
2932 else if (err == 0)
2933 JUMPTO(oparg);
2934 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002935 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002937 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002938
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002939 TARGET(JUMP_IF_TRUE_OR_POP) {
2940 PyObject *cond = TOP();
2941 int err;
2942 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002943 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002944 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002945 FAST_DISPATCH();
2946 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002947 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002948 JUMPTO(oparg);
2949 FAST_DISPATCH();
2950 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002951 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002952 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002953 JUMPTO(oparg);
2954 }
2955 else if (err == 0) {
2956 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002957 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002958 }
2959 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002960 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002961 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002962 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002963
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002964 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002965 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002966 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002967#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002968 /* Enabling this path speeds-up all while and for-loops by bypassing
2969 the per-loop checks for signals. By default, this should be turned-off
2970 because it prevents detection of a control-break in tight loops like
2971 "while 1: pass". Compile with this option turned-on when you need
2972 the speed-up and do not need break checking inside tight loops (ones
2973 that contain only instructions ending with FAST_DISPATCH).
2974 */
2975 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002976#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002977 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002978#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002979 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002980
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002981 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002982 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002983 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002984 PyObject *iter = PyObject_GetIter(iterable);
2985 Py_DECREF(iterable);
2986 SET_TOP(iter);
2987 if (iter == NULL)
2988 goto error;
2989 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002990 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002991 DISPATCH();
2992 }
2993
2994 TARGET(GET_YIELD_FROM_ITER) {
2995 /* before: [obj]; after [getiter(obj)] */
2996 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002997 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002998 if (PyCoro_CheckExact(iterable)) {
2999 /* `iterable` is a coroutine */
3000 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3001 /* and it is used in a 'yield from' expression of a
3002 regular generator. */
3003 Py_DECREF(iterable);
3004 SET_TOP(NULL);
3005 PyErr_SetString(PyExc_TypeError,
3006 "cannot 'yield from' a coroutine object "
3007 "in a non-coroutine generator");
3008 goto error;
3009 }
3010 }
3011 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003012 /* `iterable` is not a generator. */
3013 iter = PyObject_GetIter(iterable);
3014 Py_DECREF(iterable);
3015 SET_TOP(iter);
3016 if (iter == NULL)
3017 goto error;
3018 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003019 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003020 DISPATCH();
3021 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003022
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003023 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003024 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003025 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003026 PyObject *iter = TOP();
3027 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3028 if (next != NULL) {
3029 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003030 PREDICT(STORE_FAST);
3031 PREDICT(UNPACK_SEQUENCE);
3032 DISPATCH();
3033 }
3034 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003035 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
3036 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08003037 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003038 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003039 PyErr_Clear();
3040 }
3041 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003042 STACKADJ(-1);
3043 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003044 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003045 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003046 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003047 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003048
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003049 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003050 why = WHY_BREAK;
3051 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003052 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003053
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003054 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003056 if (retval == NULL)
3057 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003058 why = WHY_CONTINUE;
3059 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003060 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003061
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003062 TARGET(SETUP_LOOP)
3063 TARGET(SETUP_EXCEPT)
3064 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065 /* NOTE: If you add any new block-setup opcodes that
3066 are not try/except/finally handlers, you may need
3067 to update the PyGen_NeedsFinalizing() function.
3068 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003070 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
3071 STACK_LEVEL());
3072 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003073 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003074
Yury Selivanov75445082015-05-11 22:57:16 -04003075 TARGET(BEFORE_ASYNC_WITH) {
3076 _Py_IDENTIFIER(__aexit__);
3077 _Py_IDENTIFIER(__aenter__);
3078
3079 PyObject *mgr = TOP();
3080 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3081 *enter;
3082 PyObject *res;
3083 if (exit == NULL)
3084 goto error;
3085 SET_TOP(exit);
3086 enter = special_lookup(mgr, &PyId___aenter__);
3087 Py_DECREF(mgr);
3088 if (enter == NULL)
3089 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003090 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003091 Py_DECREF(enter);
3092 if (res == NULL)
3093 goto error;
3094 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003095 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003096 DISPATCH();
3097 }
3098
3099 TARGET(SETUP_ASYNC_WITH) {
3100 PyObject *res = POP();
3101 /* Setup the finally block before pushing the result
3102 of __aenter__ on the stack. */
3103 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3104 STACK_LEVEL());
3105 PUSH(res);
3106 DISPATCH();
3107 }
3108
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003109 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003110 _Py_IDENTIFIER(__exit__);
3111 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003112 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08003113 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003114 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08003115 if (enter == NULL)
3116 goto error;
3117 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003118 if (exit == NULL) {
3119 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003120 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003121 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003122 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003123 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003124 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003125 Py_DECREF(enter);
3126 if (res == NULL)
3127 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003128 /* Setup the finally block before pushing the result
3129 of __enter__ on the stack. */
3130 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3131 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003132
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003133 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003134 DISPATCH();
3135 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003136
Yury Selivanov75445082015-05-11 22:57:16 -04003137 TARGET(WITH_CLEANUP_START) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04003138 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003139 how/why we entered the finally clause:
3140 - TOP = None
3141 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
3142 - TOP = WHY_*; no retval below it
3143 - (TOP, SECOND, THIRD) = exc_info()
3144 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
3145 Below them is EXIT, the context.__exit__ bound method.
3146 In the last case, we must call
3147 EXIT(TOP, SECOND, THIRD)
3148 otherwise we must call
3149 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003150
Benjamin Peterson8f169482013-10-29 22:25:06 -04003151 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003152 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04003153 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003156 In addition, if the stack represents an exception,
3157 *and* the function call returns a 'true' value, we
3158 push WHY_SILENCED onto the stack. END_FINALLY will
3159 then not re-raise the exception. (But non-local
3160 gotos should still be resumed.)
3161 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003162
Victor Stinner842cfff2016-12-01 14:45:31 +01003163 PyObject* stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003165 PyObject *exc, *val, *tb, *res;
3166
3167 val = tb = Py_None;
3168 exc = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003169 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003170 (void)POP();
3171 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003172 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003173 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003174 else if (PyLong_Check(exc)) {
3175 STACKADJ(-1);
3176 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003177 case WHY_RETURN:
3178 case WHY_CONTINUE:
3179 /* Retval in TOP. */
3180 exit_func = SECOND();
3181 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003182 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003183 break;
3184 default:
3185 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003186 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003187 break;
3188 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003189 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003190 }
3191 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003192 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003193 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003194 val = SECOND();
3195 tb = THIRD();
3196 tp2 = FOURTH();
3197 exc2 = PEEK(5);
3198 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003199 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003200 SET_VALUE(7, tb2);
3201 SET_VALUE(6, exc2);
3202 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003203 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3204 SET_FOURTH(NULL);
3205 /* We just shifted the stack down, so we have
3206 to tell the except handler block that the
3207 values are lower than it expects. */
3208 block = &f->f_blockstack[f->f_iblock - 1];
3209 assert(block->b_type == EXCEPT_HANDLER);
3210 block->b_level--;
3211 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003212
3213 stack[0] = exc;
3214 stack[1] = val;
3215 stack[2] = tb;
3216 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003217 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003218 if (res == NULL)
3219 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003220
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003221 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003222 PUSH(exc);
3223 PUSH(res);
3224 PREDICT(WITH_CLEANUP_FINISH);
3225 DISPATCH();
3226 }
3227
3228 PREDICTED(WITH_CLEANUP_FINISH);
3229 TARGET(WITH_CLEANUP_FINISH) {
3230 PyObject *res = POP();
3231 PyObject *exc = POP();
3232 int err;
3233
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003234 if (exc != Py_None)
3235 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003236 else
3237 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003238
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003239 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003240 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003242 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003243 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003244 else if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003245 /* There was an exception and a True return */
3246 PUSH(PyLong_FromLong((long) WHY_SILENCED));
3247 }
3248 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003249 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003250 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003251
Yury Selivanovf2392132016-12-13 19:03:51 -05003252 TARGET(LOAD_METHOD) {
3253 /* Designed to work in tamdem with CALL_METHOD. */
3254 PyObject *name = GETITEM(names, oparg);
3255 PyObject *obj = TOP();
3256 PyObject *meth = NULL;
3257
3258 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3259
Yury Selivanovf2392132016-12-13 19:03:51 -05003260 if (meth == NULL) {
3261 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003262 goto error;
3263 }
3264
3265 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003266 /* We can bypass temporary bound method object.
3267 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003268
INADA Naoki015bce62017-01-16 17:23:30 +09003269 meth | self | arg1 | ... | argN
3270 */
3271 SET_TOP(meth);
3272 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003273 }
3274 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003275 /* meth is not an unbound method (but a regular attr, or
3276 something was returned by a descriptor protocol). Set
3277 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003278 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003279
3280 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003281 */
INADA Naoki015bce62017-01-16 17:23:30 +09003282 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003283 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003284 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003285 }
3286 DISPATCH();
3287 }
3288
3289 TARGET(CALL_METHOD) {
3290 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003291 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003292
3293 sp = stack_pointer;
3294
INADA Naoki015bce62017-01-16 17:23:30 +09003295 meth = PEEK(oparg + 2);
3296 if (meth == NULL) {
3297 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3298 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003299
3300 Stack layout:
3301
INADA Naoki015bce62017-01-16 17:23:30 +09003302 ... | NULL | callable | arg1 | ... | argN
3303 ^- TOP()
3304 ^- (-oparg)
3305 ^- (-oparg-1)
3306 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003307
Ville Skyttä49b27342017-08-03 09:00:59 +03003308 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003309 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003310 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003311 res = call_function(&sp, oparg, NULL);
3312 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003313 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003314 }
3315 else {
3316 /* This is a method call. Stack layout:
3317
INADA Naoki015bce62017-01-16 17:23:30 +09003318 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003319 ^- TOP()
3320 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003321 ^- (-oparg-1)
3322 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003323
INADA Naoki015bce62017-01-16 17:23:30 +09003324 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003325 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003326 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003327 */
3328 res = call_function(&sp, oparg + 1, NULL);
3329 stack_pointer = sp;
3330 }
3331
3332 PUSH(res);
3333 if (res == NULL)
3334 goto error;
3335 DISPATCH();
3336 }
3337
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003338 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003339 TARGET(CALL_FUNCTION) {
3340 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003341 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003342 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003343 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003344 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003345 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003346 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003347 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003348 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003349 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003350
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003351 TARGET(CALL_FUNCTION_KW) {
3352 PyObject **sp, *res, *names;
3353
3354 names = POP();
3355 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003356 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003357 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003358 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003359 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003360 Py_DECREF(names);
3361
3362 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003363 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003364 }
3365 DISPATCH();
3366 }
3367
3368 TARGET(CALL_FUNCTION_EX) {
3369 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003370 if (oparg & 0x01) {
3371 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003372 if (!PyDict_CheckExact(kwargs)) {
3373 PyObject *d = PyDict_New();
3374 if (d == NULL)
3375 goto error;
3376 if (PyDict_Update(d, kwargs) != 0) {
3377 Py_DECREF(d);
3378 /* PyDict_Update raises attribute
3379 * error (percolated from an attempt
3380 * to get 'keys' attribute) instead of
3381 * a type error if its second argument
3382 * is not a mapping.
3383 */
3384 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003385 format_kwargs_mapping_error(SECOND(), kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003386 }
Victor Stinnereece2222016-09-12 11:16:37 +02003387 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003388 goto error;
3389 }
3390 Py_DECREF(kwargs);
3391 kwargs = d;
3392 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003393 assert(PyDict_CheckExact(kwargs));
3394 }
3395 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003396 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003397 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003398 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003399 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003400 goto error;
3401 }
3402 Py_SETREF(callargs, PySequence_Tuple(callargs));
3403 if (callargs == NULL) {
3404 goto error;
3405 }
3406 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003407 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003408
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003409 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003410 Py_DECREF(func);
3411 Py_DECREF(callargs);
3412 Py_XDECREF(kwargs);
3413
3414 SET_TOP(result);
3415 if (result == NULL) {
3416 goto error;
3417 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003418 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003419 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003420
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003421 TARGET(MAKE_FUNCTION) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003422 PyObject *qualname = POP();
3423 PyObject *codeobj = POP();
3424 PyFunctionObject *func = (PyFunctionObject *)
3425 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003426
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003427 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003428 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003429 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003430 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003431 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003432
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003433 if (oparg & 0x08) {
3434 assert(PyTuple_CheckExact(TOP()));
3435 func ->func_closure = POP();
3436 }
3437 if (oparg & 0x04) {
3438 assert(PyDict_CheckExact(TOP()));
3439 func->func_annotations = POP();
3440 }
3441 if (oparg & 0x02) {
3442 assert(PyDict_CheckExact(TOP()));
3443 func->func_kwdefaults = POP();
3444 }
3445 if (oparg & 0x01) {
3446 assert(PyTuple_CheckExact(TOP()));
3447 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003448 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003449
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003450 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003451 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003452 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003453
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003454 TARGET(BUILD_SLICE) {
3455 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003456 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003457 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003458 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003459 step = NULL;
3460 stop = POP();
3461 start = TOP();
3462 slice = PySlice_New(start, stop, step);
3463 Py_DECREF(start);
3464 Py_DECREF(stop);
3465 Py_XDECREF(step);
3466 SET_TOP(slice);
3467 if (slice == NULL)
3468 goto error;
3469 DISPATCH();
3470 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003471
Eric V. Smitha78c7952015-11-03 12:45:05 -05003472 TARGET(FORMAT_VALUE) {
3473 /* Handles f-string value formatting. */
3474 PyObject *result;
3475 PyObject *fmt_spec;
3476 PyObject *value;
3477 PyObject *(*conv_fn)(PyObject *);
3478 int which_conversion = oparg & FVC_MASK;
3479 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3480
3481 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003482 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003483
3484 /* See if any conversion is specified. */
3485 switch (which_conversion) {
3486 case FVC_STR: conv_fn = PyObject_Str; break;
3487 case FVC_REPR: conv_fn = PyObject_Repr; break;
3488 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3489
3490 /* Must be 0 (meaning no conversion), since only four
3491 values are allowed by (oparg & FVC_MASK). */
3492 default: conv_fn = NULL; break;
3493 }
3494
3495 /* If there's a conversion function, call it and replace
3496 value with that result. Otherwise, just use value,
3497 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003498 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003499 result = conv_fn(value);
3500 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003501 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003502 Py_XDECREF(fmt_spec);
3503 goto error;
3504 }
3505 value = result;
3506 }
3507
3508 /* If value is a unicode object, and there's no fmt_spec,
3509 then we know the result of format(value) is value
3510 itself. In that case, skip calling format(). I plan to
3511 move this optimization in to PyObject_Format()
3512 itself. */
3513 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3514 /* Do nothing, just transfer ownership to result. */
3515 result = value;
3516 } else {
3517 /* Actually call format(). */
3518 result = PyObject_Format(value, fmt_spec);
3519 Py_DECREF(value);
3520 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003521 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003522 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003523 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003524 }
3525
Eric V. Smith135d5f42016-02-05 18:23:08 -05003526 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003527 DISPATCH();
3528 }
3529
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003530 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003531 int oldoparg = oparg;
3532 NEXTOPARG();
3533 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003534 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003535 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003536
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003537
Antoine Pitrou042b1282010-08-13 21:15:58 +00003538#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003539 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003540#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003541 default:
3542 fprintf(stderr,
3543 "XXX lineno: %d, opcode: %d\n",
3544 PyFrame_GetLineNumber(f),
3545 opcode);
3546 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003547 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003548
3549#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003550 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003551#endif
3552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003553 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003554
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003555 /* This should never be reached. Every opcode should end with DISPATCH()
3556 or goto error. */
3557 assert(0);
Guido van Rossumac7be682001-01-17 15:42:30 +00003558
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003559error:
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003560
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003561 assert(why == WHY_NOT);
3562 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003563
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003564 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003565#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003566 if (!PyErr_Occurred())
3567 PyErr_SetString(PyExc_SystemError,
3568 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003569#else
3570 assert(PyErr_Occurred());
3571#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003572
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003573 /* Log traceback info. */
3574 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003575
Benjamin Peterson51f46162013-01-23 08:38:47 -05003576 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003577 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3578 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003579
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003580fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003581 assert(why != WHY_NOT);
3582
3583 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003584 while (why != WHY_NOT && f->f_iblock > 0) {
3585 /* Peek at the current block. */
3586 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003588 assert(why != WHY_YIELD);
3589 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3590 why = WHY_NOT;
3591 JUMPTO(PyLong_AS_LONG(retval));
3592 Py_DECREF(retval);
3593 break;
3594 }
3595 /* Now we have to pop the block. */
3596 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003598 if (b->b_type == EXCEPT_HANDLER) {
3599 UNWIND_EXCEPT_HANDLER(b);
3600 continue;
3601 }
3602 UNWIND_BLOCK(b);
3603 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3604 why = WHY_NOT;
3605 JUMPTO(b->b_handler);
3606 break;
3607 }
3608 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3609 || b->b_type == SETUP_FINALLY)) {
3610 PyObject *exc, *val, *tb;
3611 int handler = b->b_handler;
3612 /* Beware, this invalidates all b->b_* fields */
3613 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3614 PUSH(tstate->exc_traceback);
3615 PUSH(tstate->exc_value);
3616 if (tstate->exc_type != NULL) {
3617 PUSH(tstate->exc_type);
3618 }
3619 else {
3620 Py_INCREF(Py_None);
3621 PUSH(Py_None);
3622 }
3623 PyErr_Fetch(&exc, &val, &tb);
3624 /* Make the raw exception data
3625 available to the handler,
3626 so a program can emulate the
3627 Python main loop. */
3628 PyErr_NormalizeException(
3629 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003630 if (tb != NULL)
3631 PyException_SetTraceback(val, tb);
3632 else
3633 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003634 Py_INCREF(exc);
3635 tstate->exc_type = exc;
3636 Py_INCREF(val);
3637 tstate->exc_value = val;
3638 tstate->exc_traceback = tb;
3639 if (tb == NULL)
3640 tb = Py_None;
3641 Py_INCREF(tb);
3642 PUSH(tb);
3643 PUSH(val);
3644 PUSH(exc);
3645 why = WHY_NOT;
3646 JUMPTO(handler);
3647 break;
3648 }
3649 if (b->b_type == SETUP_FINALLY) {
3650 if (why & (WHY_RETURN | WHY_CONTINUE))
3651 PUSH(retval);
3652 PUSH(PyLong_FromLong((long)why));
3653 why = WHY_NOT;
3654 JUMPTO(b->b_handler);
3655 break;
3656 }
3657 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003659 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003661 if (why != WHY_NOT)
3662 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00003663
Victor Stinnerace47d72013-07-18 01:41:08 +02003664 assert(!PyErr_Occurred());
3665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003666 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003668 assert(why != WHY_YIELD);
3669 /* Pop remaining stack entries. */
3670 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003671 PyObject *o = POP();
3672 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003673 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003675 if (why != WHY_RETURN)
3676 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003677
Victor Stinner4a7cc882015-03-06 23:35:27 +01003678 assert((retval != NULL) ^ (PyErr_Occurred() != NULL));
Victor Stinnerace47d72013-07-18 01:41:08 +02003679
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003680fast_yield:
Yury Selivanoveb636452016-09-08 22:01:51 -07003681 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01003682
Benjamin Petersonac913412011-07-03 16:25:11 -05003683 /* The purpose of this block is to put aside the generator's exception
3684 state and restore that of the calling frame. If the current
3685 exception state is from the caller, we clear the exception values
3686 on the generator frame, so they are not swapped back in latter. The
3687 origin of the current exception state is determined by checking for
3688 except handler blocks, which we must be in iff a new exception
3689 state came into existence in this frame. (An uncaught exception
3690 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3691 int i;
Victor Stinner74319ae2016-08-25 00:04:09 +02003692 for (i = 0; i < f->f_iblock; i++) {
3693 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER) {
Benjamin Petersonac913412011-07-03 16:25:11 -05003694 break;
Victor Stinner74319ae2016-08-25 00:04:09 +02003695 }
3696 }
Benjamin Petersonac913412011-07-03 16:25:11 -05003697 if (i == f->f_iblock)
3698 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003699 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003700 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003701 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003702 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003704 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003705 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003706 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003707 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3708 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003709 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003710 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003711 why = WHY_EXCEPTION;
3712 }
3713 }
3714 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003715 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3716 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003717 PyTrace_RETURN, NULL);
3718 }
3719 }
3720 if (tstate->c_profilefunc) {
3721 if (why == WHY_EXCEPTION)
3722 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003723 tstate->c_profileobj,
3724 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003725 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003726 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3727 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003728 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003729 Py_CLEAR(retval);
Brett Cannonb94767f2011-02-22 20:15:44 +00003730 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003731 }
3732 }
3733 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003735 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003736exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003737 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3738 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003739 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003740 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003741 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003742
Victor Stinnerefde1462015-03-21 15:04:43 +01003743 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003744}
3745
Benjamin Petersonb204a422011-06-05 22:04:07 -05003746static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003747format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3748{
3749 int err;
3750 Py_ssize_t len = PyList_GET_SIZE(names);
3751 PyObject *name_str, *comma, *tail, *tmp;
3752
3753 assert(PyList_CheckExact(names));
3754 assert(len >= 1);
3755 /* Deal with the joys of natural language. */
3756 switch (len) {
3757 case 1:
3758 name_str = PyList_GET_ITEM(names, 0);
3759 Py_INCREF(name_str);
3760 break;
3761 case 2:
3762 name_str = PyUnicode_FromFormat("%U and %U",
3763 PyList_GET_ITEM(names, len - 2),
3764 PyList_GET_ITEM(names, len - 1));
3765 break;
3766 default:
3767 tail = PyUnicode_FromFormat(", %U, and %U",
3768 PyList_GET_ITEM(names, len - 2),
3769 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003770 if (tail == NULL)
3771 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003772 /* Chop off the last two objects in the list. This shouldn't actually
3773 fail, but we can't be too careful. */
3774 err = PyList_SetSlice(names, len - 2, len, NULL);
3775 if (err == -1) {
3776 Py_DECREF(tail);
3777 return;
3778 }
3779 /* Stitch everything up into a nice comma-separated list. */
3780 comma = PyUnicode_FromString(", ");
3781 if (comma == NULL) {
3782 Py_DECREF(tail);
3783 return;
3784 }
3785 tmp = PyUnicode_Join(comma, names);
3786 Py_DECREF(comma);
3787 if (tmp == NULL) {
3788 Py_DECREF(tail);
3789 return;
3790 }
3791 name_str = PyUnicode_Concat(tmp, tail);
3792 Py_DECREF(tmp);
3793 Py_DECREF(tail);
3794 break;
3795 }
3796 if (name_str == NULL)
3797 return;
3798 PyErr_Format(PyExc_TypeError,
3799 "%U() missing %i required %s argument%s: %U",
3800 co->co_name,
3801 len,
3802 kind,
3803 len == 1 ? "" : "s",
3804 name_str);
3805 Py_DECREF(name_str);
3806}
3807
3808static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003809missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003810 PyObject **fastlocals)
3811{
Victor Stinner74319ae2016-08-25 00:04:09 +02003812 Py_ssize_t i, j = 0;
3813 Py_ssize_t start, end;
3814 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003815 const char *kind = positional ? "positional" : "keyword-only";
3816 PyObject *missing_names;
3817
3818 /* Compute the names of the arguments that are missing. */
3819 missing_names = PyList_New(missing);
3820 if (missing_names == NULL)
3821 return;
3822 if (positional) {
3823 start = 0;
3824 end = co->co_argcount - defcount;
3825 }
3826 else {
3827 start = co->co_argcount;
3828 end = start + co->co_kwonlyargcount;
3829 }
3830 for (i = start; i < end; i++) {
3831 if (GETLOCAL(i) == NULL) {
3832 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3833 PyObject *name = PyObject_Repr(raw);
3834 if (name == NULL) {
3835 Py_DECREF(missing_names);
3836 return;
3837 }
3838 PyList_SET_ITEM(missing_names, j++, name);
3839 }
3840 }
3841 assert(j == missing);
3842 format_missing(kind, co, missing_names);
3843 Py_DECREF(missing_names);
3844}
3845
3846static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003847too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3848 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003849{
3850 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003851 Py_ssize_t kwonly_given = 0;
3852 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003853 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003854 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003855
Benjamin Petersone109c702011-06-24 09:37:26 -05003856 assert((co->co_flags & CO_VARARGS) == 0);
3857 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003858 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3859 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003860 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003861 }
3862 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003863 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003864 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003865 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003866 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003867 }
3868 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003869 plural = (co_argcount != 1);
3870 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003871 }
3872 if (sig == NULL)
3873 return;
3874 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003875 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3876 kwonly_sig = PyUnicode_FromFormat(format,
3877 given != 1 ? "s" : "",
3878 kwonly_given,
3879 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003880 if (kwonly_sig == NULL) {
3881 Py_DECREF(sig);
3882 return;
3883 }
3884 }
3885 else {
3886 /* This will not fail. */
3887 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003888 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003889 }
3890 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003891 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003892 co->co_name,
3893 sig,
3894 plural ? "s" : "",
3895 given,
3896 kwonly_sig,
3897 given == 1 && !kwonly_given ? "was" : "were");
3898 Py_DECREF(sig);
3899 Py_DECREF(kwonly_sig);
3900}
3901
Guido van Rossumc2e20742006-02-27 22:32:47 +00003902/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003903 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003904 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003905
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003906PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003907_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Victor Stinner74319ae2016-08-25 00:04:09 +02003908 PyObject **args, Py_ssize_t argcount,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003909 PyObject **kwnames, PyObject **kwargs,
3910 Py_ssize_t kwcount, int kwstep,
Victor Stinner74319ae2016-08-25 00:04:09 +02003911 PyObject **defs, Py_ssize_t defcount,
3912 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003913 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003914{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003915 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003916 PyFrameObject *f;
3917 PyObject *retval = NULL;
3918 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003919 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003920 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003921 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3922 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003923 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003925 if (globals == NULL) {
3926 PyErr_SetString(PyExc_SystemError,
3927 "PyEval_EvalCodeEx: NULL globals");
3928 return NULL;
3929 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003930
Victor Stinnerc7020012016-08-16 23:40:29 +02003931 /* Create the frame */
3932 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003933 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003934 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003935 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003936 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003937 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003938 fastlocals = f->f_localsplus;
3939 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003940
Victor Stinnerc7020012016-08-16 23:40:29 +02003941 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003942 if (co->co_flags & CO_VARKEYWORDS) {
3943 kwdict = PyDict_New();
3944 if (kwdict == NULL)
3945 goto fail;
3946 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003947 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003948 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003949 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003950 SETLOCAL(i, kwdict);
3951 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003952 else {
3953 kwdict = NULL;
3954 }
3955
3956 /* Copy positional arguments into local variables */
3957 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003958 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003959 }
3960 else {
3961 n = argcount;
3962 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003963 for (i = 0; i < n; i++) {
3964 x = args[i];
3965 Py_INCREF(x);
3966 SETLOCAL(i, x);
3967 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003968
3969 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003970 if (co->co_flags & CO_VARARGS) {
3971 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003972 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003973 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003974 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003975 SETLOCAL(total_args, u);
3976 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003977 x = args[i];
3978 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003979 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003980 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003981 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003982
Serhiy Storchakab7281052016-09-12 00:52:40 +03003983 /* Handle keyword arguments passed as two strided arrays */
3984 kwcount *= kwstep;
3985 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003986 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003987 PyObject *keyword = kwnames[i];
3988 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003989 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003990
Benjamin Petersonb204a422011-06-05 22:04:07 -05003991 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3992 PyErr_Format(PyExc_TypeError,
3993 "%U() keywords must be strings",
3994 co->co_name);
3995 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003996 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003997
Benjamin Petersonb204a422011-06-05 22:04:07 -05003998 /* Speed hack: do raw pointer compares. As names are
3999 normally interned this should almost always hit. */
4000 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
4001 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004002 PyObject *name = co_varnames[j];
4003 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004004 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004005 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004006 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004007
Benjamin Petersonb204a422011-06-05 22:04:07 -05004008 /* Slow fallback, just in case */
4009 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004010 PyObject *name = co_varnames[j];
4011 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4012 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004013 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004014 }
4015 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004016 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004017 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004018 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004019
Victor Stinner231d1f32017-01-11 02:12:06 +01004020 assert(j >= total_args);
4021 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004022 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02004023 "%U() got an unexpected keyword argument '%S'",
4024 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004025 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004026 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004027
Christian Heimes0bd447f2013-07-20 14:48:10 +02004028 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4029 goto fail;
4030 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004031 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004032
Benjamin Petersonb204a422011-06-05 22:04:07 -05004033 kw_found:
4034 if (GETLOCAL(j) != NULL) {
4035 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02004036 "%U() got multiple values for argument '%S'",
4037 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004038 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004039 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004040 Py_INCREF(value);
4041 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004042 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004043
4044 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004045 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004046 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004047 goto fail;
4048 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004049
4050 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004051 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02004052 Py_ssize_t m = co->co_argcount - defcount;
4053 Py_ssize_t missing = 0;
4054 for (i = argcount; i < m; i++) {
4055 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004056 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004057 }
4058 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004059 if (missing) {
4060 missing_arguments(co, missing, defcount, fastlocals);
4061 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004062 }
4063 if (n > m)
4064 i = n - m;
4065 else
4066 i = 0;
4067 for (; i < defcount; i++) {
4068 if (GETLOCAL(m+i) == NULL) {
4069 PyObject *def = defs[i];
4070 Py_INCREF(def);
4071 SETLOCAL(m+i, def);
4072 }
4073 }
4074 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004075
4076 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004077 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004078 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004079 for (i = co->co_argcount; i < total_args; i++) {
4080 PyObject *name;
4081 if (GETLOCAL(i) != NULL)
4082 continue;
4083 name = PyTuple_GET_ITEM(co->co_varnames, i);
4084 if (kwdefs != NULL) {
4085 PyObject *def = PyDict_GetItem(kwdefs, name);
4086 if (def) {
4087 Py_INCREF(def);
4088 SETLOCAL(i, def);
4089 continue;
4090 }
4091 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004092 missing++;
4093 }
4094 if (missing) {
4095 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004096 goto fail;
4097 }
4098 }
4099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004100 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004101 vars into frame. */
4102 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004103 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004104 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004105 /* Possibly account for the cell variable being an argument. */
4106 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004107 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004108 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004109 /* Clear the local copy. */
4110 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004111 }
4112 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004113 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004114 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004115 if (c == NULL)
4116 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004117 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004118 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004119
4120 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004121 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4122 PyObject *o = PyTuple_GET_ITEM(closure, i);
4123 Py_INCREF(o);
4124 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004125 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004126
Yury Selivanoveb636452016-09-08 22:01:51 -07004127 /* Handle generator/coroutine/asynchronous generator */
4128 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004129 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04004130 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004131 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004132
4133 if (is_coro && tstate->in_coroutine_wrapper) {
4134 assert(coro_wrapper != NULL);
4135 PyErr_Format(PyExc_RuntimeError,
4136 "coroutine wrapper %.200R attempted "
4137 "to recursively wrap %.200R",
4138 coro_wrapper,
4139 co);
4140 goto fail;
4141 }
Yury Selivanov75445082015-05-11 22:57:16 -04004142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004143 /* Don't need to keep the reference to f_back, it will be set
4144 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004145 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004147 /* Create a new generator that owns the ready to run frame
4148 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004149 if (is_coro) {
4150 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004151 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4152 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004153 } else {
4154 gen = PyGen_NewWithQualName(f, name, qualname);
4155 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004156 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004157 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004158 }
INADA Naoki9c157762016-12-26 18:52:46 +09004159
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004160 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004161
Yury Selivanov94c22632015-06-04 10:16:51 -04004162 if (is_coro && coro_wrapper != NULL) {
4163 PyObject *wrapped;
4164 tstate->in_coroutine_wrapper = 1;
4165 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4166 tstate->in_coroutine_wrapper = 0;
4167 return wrapped;
4168 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004169
Yury Selivanov75445082015-05-11 22:57:16 -04004170 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004171 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004172
Victor Stinner59a73272016-12-09 18:51:13 +01004173 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004174
Thomas Woutersce272b62007-09-19 21:19:28 +00004175fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004177 /* decref'ing the frame can cause __del__ methods to get invoked,
4178 which can call back into Python. While we're done with the
4179 current Python frame (f), the associated C stack is still in use,
4180 so recursion_depth must be boosted for the duration.
4181 */
4182 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09004183 if (Py_REFCNT(f) > 1) {
4184 Py_DECREF(f);
4185 _PyObject_GC_TRACK(f);
4186 }
4187 else {
4188 ++tstate->recursion_depth;
4189 Py_DECREF(f);
4190 --tstate->recursion_depth;
4191 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004192 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004193}
4194
Victor Stinner40ee3012014-06-16 15:59:28 +02004195PyObject *
4196PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
4197 PyObject **args, int argcount, PyObject **kws, int kwcount,
4198 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
4199{
4200 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004201 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004202 kws, kws != NULL ? kws + 1 : NULL,
4203 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004204 defs, defcount,
4205 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004206 NULL, NULL);
4207}
Tim Peters5ca576e2001-06-18 22:08:13 +00004208
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004209static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004210special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004211{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004212 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004213 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004214 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004215 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004216 return NULL;
4217 }
4218 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004219}
4220
4221
Benjamin Peterson87880242011-07-03 16:48:31 -05004222/* These 3 functions deal with the exception state of generators. */
4223
4224static void
4225save_exc_state(PyThreadState *tstate, PyFrameObject *f)
4226{
4227 PyObject *type, *value, *traceback;
4228 Py_XINCREF(tstate->exc_type);
4229 Py_XINCREF(tstate->exc_value);
4230 Py_XINCREF(tstate->exc_traceback);
4231 type = f->f_exc_type;
4232 value = f->f_exc_value;
4233 traceback = f->f_exc_traceback;
4234 f->f_exc_type = tstate->exc_type;
4235 f->f_exc_value = tstate->exc_value;
4236 f->f_exc_traceback = tstate->exc_traceback;
4237 Py_XDECREF(type);
4238 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004239 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05004240}
4241
4242static void
4243swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
4244{
4245 PyObject *tmp;
4246 tmp = tstate->exc_type;
4247 tstate->exc_type = f->f_exc_type;
4248 f->f_exc_type = tmp;
4249 tmp = tstate->exc_value;
4250 tstate->exc_value = f->f_exc_value;
4251 f->f_exc_value = tmp;
4252 tmp = tstate->exc_traceback;
4253 tstate->exc_traceback = f->f_exc_traceback;
4254 f->f_exc_traceback = tmp;
4255}
4256
4257static void
4258restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
4259{
4260 PyObject *type, *value, *tb;
4261 type = tstate->exc_type;
4262 value = tstate->exc_value;
4263 tb = tstate->exc_traceback;
4264 tstate->exc_type = f->f_exc_type;
4265 tstate->exc_value = f->f_exc_value;
4266 tstate->exc_traceback = f->f_exc_traceback;
4267 f->f_exc_type = NULL;
4268 f->f_exc_value = NULL;
4269 f->f_exc_traceback = NULL;
4270 Py_XDECREF(type);
4271 Py_XDECREF(value);
4272 Py_XDECREF(tb);
4273}
4274
4275
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004276/* Logic for the raise statement (too complicated for inlining).
4277 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004278static int
Collin Winter828f04a2007-08-31 00:04:24 +00004279do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004280{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004281 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004283 if (exc == NULL) {
4284 /* Reraise */
4285 PyThreadState *tstate = PyThreadState_GET();
4286 PyObject *tb;
4287 type = tstate->exc_type;
4288 value = tstate->exc_value;
4289 tb = tstate->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004290 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004291 PyErr_SetString(PyExc_RuntimeError,
4292 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004293 return 0;
4294 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004295 Py_XINCREF(type);
4296 Py_XINCREF(value);
4297 Py_XINCREF(tb);
4298 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004299 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004300 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004302 /* We support the following forms of raise:
4303 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004304 raise <instance>
4305 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004307 if (PyExceptionClass_Check(exc)) {
4308 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004309 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004310 if (value == NULL)
4311 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004312 if (!PyExceptionInstance_Check(value)) {
4313 PyErr_Format(PyExc_TypeError,
4314 "calling %R should have returned an instance of "
4315 "BaseException, not %R",
4316 type, Py_TYPE(value));
4317 goto raise_error;
4318 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004319 }
4320 else if (PyExceptionInstance_Check(exc)) {
4321 value = exc;
4322 type = PyExceptionInstance_Class(exc);
4323 Py_INCREF(type);
4324 }
4325 else {
4326 /* Not something you can raise. You get an exception
4327 anyway, just not what you specified :-) */
4328 Py_DECREF(exc);
4329 PyErr_SetString(PyExc_TypeError,
4330 "exceptions must derive from BaseException");
4331 goto raise_error;
4332 }
Collin Winter828f04a2007-08-31 00:04:24 +00004333
Serhiy Storchakac0191582016-09-27 11:37:10 +03004334 assert(type != NULL);
4335 assert(value != NULL);
4336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004337 if (cause) {
4338 PyObject *fixed_cause;
4339 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004340 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004341 if (fixed_cause == NULL)
4342 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004343 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004344 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004345 else if (PyExceptionInstance_Check(cause)) {
4346 fixed_cause = cause;
4347 }
4348 else if (cause == Py_None) {
4349 Py_DECREF(cause);
4350 fixed_cause = NULL;
4351 }
4352 else {
4353 PyErr_SetString(PyExc_TypeError,
4354 "exception causes must derive from "
4355 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004356 goto raise_error;
4357 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004358 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004359 }
Collin Winter828f04a2007-08-31 00:04:24 +00004360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004361 PyErr_SetObject(type, value);
4362 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004363 Py_DECREF(value);
4364 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004365 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004366
4367raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004368 Py_XDECREF(value);
4369 Py_XDECREF(type);
4370 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004371 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004372}
4373
Tim Petersd6d010b2001-06-21 02:49:55 +00004374/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004375 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004376
Guido van Rossum0368b722007-05-11 16:50:42 +00004377 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4378 with a variable target.
4379*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004380
Barry Warsawe42b18f1997-08-25 22:13:04 +00004381static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004382unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004383{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004384 int i = 0, j = 0;
4385 Py_ssize_t ll = 0;
4386 PyObject *it; /* iter(v) */
4387 PyObject *w;
4388 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004390 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004392 it = PyObject_GetIter(v);
4393 if (it == NULL)
4394 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00004395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004396 for (; i < argcnt; i++) {
4397 w = PyIter_Next(it);
4398 if (w == NULL) {
4399 /* Iterator done, via error or exhaustion. */
4400 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004401 if (argcntafter == -1) {
4402 PyErr_Format(PyExc_ValueError,
4403 "not enough values to unpack (expected %d, got %d)",
4404 argcnt, i);
4405 }
4406 else {
4407 PyErr_Format(PyExc_ValueError,
4408 "not enough values to unpack "
4409 "(expected at least %d, got %d)",
4410 argcnt + argcntafter, i);
4411 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004412 }
4413 goto Error;
4414 }
4415 *--sp = w;
4416 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004418 if (argcntafter == -1) {
4419 /* We better have exhausted the iterator now. */
4420 w = PyIter_Next(it);
4421 if (w == NULL) {
4422 if (PyErr_Occurred())
4423 goto Error;
4424 Py_DECREF(it);
4425 return 1;
4426 }
4427 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004428 PyErr_Format(PyExc_ValueError,
4429 "too many values to unpack (expected %d)",
4430 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004431 goto Error;
4432 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004434 l = PySequence_List(it);
4435 if (l == NULL)
4436 goto Error;
4437 *--sp = l;
4438 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004440 ll = PyList_GET_SIZE(l);
4441 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004442 PyErr_Format(PyExc_ValueError,
4443 "not enough values to unpack (expected at least %d, got %zd)",
4444 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004445 goto Error;
4446 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004448 /* Pop the "after-variable" args off the list. */
4449 for (j = argcntafter; j > 0; j--, i++) {
4450 *--sp = PyList_GET_ITEM(l, ll - j);
4451 }
4452 /* Resize the list. */
4453 Py_SIZE(l) = ll - argcntafter;
4454 Py_DECREF(it);
4455 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004456
Tim Petersd6d010b2001-06-21 02:49:55 +00004457Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004458 for (; i > 0; i--, sp++)
4459 Py_DECREF(*sp);
4460 Py_XDECREF(it);
4461 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004462}
4463
4464
Guido van Rossum96a42c81992-01-12 02:29:51 +00004465#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004466static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004467prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004468{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004469 printf("%s ", str);
4470 if (PyObject_Print(v, stdout, 0) != 0)
4471 PyErr_Clear(); /* Don't know what else to do */
4472 printf("\n");
4473 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004474}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004475#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004476
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004477static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004478call_exc_trace(Py_tracefunc func, PyObject *self,
4479 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004480{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004481 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004482 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004483 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004484 if (value == NULL) {
4485 value = Py_None;
4486 Py_INCREF(value);
4487 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004488 PyErr_NormalizeException(&type, &value, &orig_traceback);
4489 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004490 arg = PyTuple_Pack(3, type, value, traceback);
4491 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004492 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004493 return;
4494 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004495 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004496 Py_DECREF(arg);
4497 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004498 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004499 else {
4500 Py_XDECREF(type);
4501 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004502 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004503 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004504}
4505
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004506static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004507call_trace_protected(Py_tracefunc func, PyObject *obj,
4508 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004509 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004510{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004511 PyObject *type, *value, *traceback;
4512 int err;
4513 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004514 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004515 if (err == 0)
4516 {
4517 PyErr_Restore(type, value, traceback);
4518 return 0;
4519 }
4520 else {
4521 Py_XDECREF(type);
4522 Py_XDECREF(value);
4523 Py_XDECREF(traceback);
4524 return -1;
4525 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004526}
4527
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004528static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004529call_trace(Py_tracefunc func, PyObject *obj,
4530 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004531 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004532{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004533 int result;
4534 if (tstate->tracing)
4535 return 0;
4536 tstate->tracing++;
4537 tstate->use_tracing = 0;
4538 result = func(obj, frame, what, arg);
4539 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4540 || (tstate->c_profilefunc != NULL));
4541 tstate->tracing--;
4542 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004543}
4544
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004545PyObject *
4546_PyEval_CallTracing(PyObject *func, PyObject *args)
4547{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004548 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004549 int save_tracing = tstate->tracing;
4550 int save_use_tracing = tstate->use_tracing;
4551 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004553 tstate->tracing = 0;
4554 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4555 || (tstate->c_profilefunc != NULL));
4556 result = PyObject_Call(func, args, NULL);
4557 tstate->tracing = save_tracing;
4558 tstate->use_tracing = save_use_tracing;
4559 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004560}
4561
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004562/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004563static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004564maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004565 PyThreadState *tstate, PyFrameObject *frame,
4566 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004567{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004568 int result = 0;
4569 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004571 /* If the last instruction executed isn't in the current
4572 instruction window, reset the window.
4573 */
4574 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4575 PyAddrPair bounds;
4576 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4577 &bounds);
4578 *instr_lb = bounds.ap_lower;
4579 *instr_ub = bounds.ap_upper;
4580 }
4581 /* If the last instruction falls at the start of a line or if
4582 it represents a jump backwards, update the frame's line
4583 number and call the trace function. */
4584 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
4585 frame->f_lineno = line;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004586 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004587 }
4588 *instr_prev = frame->f_lasti;
4589 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004590}
4591
Fred Drake5755ce62001-06-27 19:19:46 +00004592void
4593PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004594{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004595 PyThreadState *tstate = PyThreadState_GET();
4596 PyObject *temp = tstate->c_profileobj;
4597 Py_XINCREF(arg);
4598 tstate->c_profilefunc = NULL;
4599 tstate->c_profileobj = NULL;
4600 /* Must make sure that tracing is not ignored if 'temp' is freed */
4601 tstate->use_tracing = tstate->c_tracefunc != NULL;
4602 Py_XDECREF(temp);
4603 tstate->c_profilefunc = func;
4604 tstate->c_profileobj = arg;
4605 /* Flag that tracing or profiling is turned on */
4606 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004607}
4608
4609void
4610PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4611{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004612 PyThreadState *tstate = PyThreadState_GET();
4613 PyObject *temp = tstate->c_traceobj;
4614 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4615 Py_XINCREF(arg);
4616 tstate->c_tracefunc = NULL;
4617 tstate->c_traceobj = NULL;
4618 /* Must make sure that profiling is not ignored if 'temp' is freed */
4619 tstate->use_tracing = tstate->c_profilefunc != NULL;
4620 Py_XDECREF(temp);
4621 tstate->c_tracefunc = func;
4622 tstate->c_traceobj = arg;
4623 /* Flag that tracing or profiling is turned on */
4624 tstate->use_tracing = ((func != NULL)
4625 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004626}
4627
Yury Selivanov75445082015-05-11 22:57:16 -04004628void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004629_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004630{
4631 PyThreadState *tstate = PyThreadState_GET();
4632
Yury Selivanov75445082015-05-11 22:57:16 -04004633 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004634 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004635}
4636
4637PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004638_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004639{
4640 PyThreadState *tstate = PyThreadState_GET();
4641 return tstate->coroutine_wrapper;
4642}
4643
Yury Selivanoveb636452016-09-08 22:01:51 -07004644void
4645_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4646{
4647 PyThreadState *tstate = PyThreadState_GET();
4648
4649 Py_XINCREF(firstiter);
4650 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4651}
4652
4653PyObject *
4654_PyEval_GetAsyncGenFirstiter(void)
4655{
4656 PyThreadState *tstate = PyThreadState_GET();
4657 return tstate->async_gen_firstiter;
4658}
4659
4660void
4661_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4662{
4663 PyThreadState *tstate = PyThreadState_GET();
4664
4665 Py_XINCREF(finalizer);
4666 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4667}
4668
4669PyObject *
4670_PyEval_GetAsyncGenFinalizer(void)
4671{
4672 PyThreadState *tstate = PyThreadState_GET();
4673 return tstate->async_gen_finalizer;
4674}
4675
Guido van Rossumb209a111997-04-29 18:18:01 +00004676PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004677PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004678{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004679 PyFrameObject *current_frame = PyEval_GetFrame();
4680 if (current_frame == NULL)
4681 return PyThreadState_GET()->interp->builtins;
4682 else
4683 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004684}
4685
Guido van Rossumb209a111997-04-29 18:18:01 +00004686PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004687PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004688{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004689 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004690 if (current_frame == NULL) {
4691 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004692 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004693 }
4694
4695 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4696 return NULL;
4697
4698 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004699 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004700}
4701
Guido van Rossumb209a111997-04-29 18:18:01 +00004702PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004703PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004704{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004705 PyFrameObject *current_frame = PyEval_GetFrame();
4706 if (current_frame == NULL)
4707 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004708
4709 assert(current_frame->f_globals != NULL);
4710 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004711}
4712
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004713PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004714PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004715{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004716 PyThreadState *tstate = PyThreadState_GET();
4717 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004718}
4719
Guido van Rossum6135a871995-01-09 17:53:26 +00004720int
Tim Peters5ba58662001-07-16 02:29:45 +00004721PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004722{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004723 PyFrameObject *current_frame = PyEval_GetFrame();
4724 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004725
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004726 if (current_frame != NULL) {
4727 const int codeflags = current_frame->f_code->co_flags;
4728 const int compilerflags = codeflags & PyCF_MASK;
4729 if (compilerflags) {
4730 result = 1;
4731 cf->cf_flags |= compilerflags;
4732 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004733#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004734 if (codeflags & CO_GENERATOR_ALLOWED) {
4735 result = 1;
4736 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4737 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004738#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004739 }
4740 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004741}
4742
Guido van Rossum3f5da241990-12-20 15:06:42 +00004743
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004744const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004745PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004746{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004747 if (PyMethod_Check(func))
4748 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4749 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004750 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004751 else if (PyCFunction_Check(func))
4752 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4753 else
4754 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004755}
4756
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004757const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004758PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004759{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004760 if (PyMethod_Check(func))
4761 return "()";
4762 else if (PyFunction_Check(func))
4763 return "()";
4764 else if (PyCFunction_Check(func))
4765 return "()";
4766 else
4767 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004768}
4769
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004770#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004771if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004772 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4773 tstate, tstate->frame, \
4774 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004775 x = NULL; \
4776 } \
4777 else { \
4778 x = call; \
4779 if (tstate->c_profilefunc != NULL) { \
4780 if (x == NULL) { \
4781 call_trace_protected(tstate->c_profilefunc, \
4782 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004783 tstate, tstate->frame, \
4784 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004785 /* XXX should pass (type, value, tb) */ \
4786 } else { \
4787 if (call_trace(tstate->c_profilefunc, \
4788 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004789 tstate, tstate->frame, \
4790 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004791 Py_DECREF(x); \
4792 x = NULL; \
4793 } \
4794 } \
4795 } \
4796 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004797} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004798 x = call; \
4799 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004800
Victor Stinner415c5102017-01-11 00:54:57 +01004801/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4802 to reduce the stack consumption. */
4803Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004804call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004805{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004806 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004807 PyObject *func = *pfunc;
4808 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004809 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4810 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004811 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004813 /* Always dispatch PyCFunction first, because these are
4814 presumed to be the most frequent callable object.
4815 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004816 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004817 PyThreadState *tstate = PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004818 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004819 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004820 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
4821 PyThreadState *tstate = PyThreadState_GET();
INADA Naoki93fac8d2017-03-07 14:24:37 +09004822 if (tstate->use_tracing && tstate->c_profilefunc) {
4823 // We need to create PyCFunctionObject for tracing.
4824 PyMethodDescrObject *descr = (PyMethodDescrObject*)func;
4825 func = PyCFunction_NewEx(descr->d_method, stack[0], NULL);
4826 if (func == NULL) {
4827 return NULL;
4828 }
4829 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack+1, nargs-1,
4830 kwnames));
4831 Py_DECREF(func);
4832 }
4833 else {
4834 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4835 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004836 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004837 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004838 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004839 /* Optimize access to bound methods. Reuse the Python stack
4840 to pass 'self' as the first argument, replace 'func'
4841 with 'self'. It avoids the creation of a new temporary tuple
4842 for arguments (to replace func with self) when the method uses
4843 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004844 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004845 Py_INCREF(self);
4846 func = PyMethod_GET_FUNCTION(func);
4847 Py_INCREF(func);
4848 Py_SETREF(*pfunc, self);
4849 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004850 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004851 }
4852 else {
4853 Py_INCREF(func);
4854 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004855
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004856 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004857 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004858 }
4859 else {
4860 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4861 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004862 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004863 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004864
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004865 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4866
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004867 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004868 while ((*pp_stack) > pfunc) {
4869 w = EXT_POP(*pp_stack);
4870 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004871 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004872
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004873 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004874}
4875
Jeremy Hylton52820442001-01-03 23:52:36 +00004876static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004877do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004878{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004879 if (PyCFunction_Check(func)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004880 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004881 PyThreadState *tstate = PyThreadState_GET();
4882 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004883 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004884 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004885 else {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004886 return PyObject_Call(func, callargs, kwdict);
Victor Stinner74319ae2016-08-25 00:04:09 +02004887 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004888}
4889
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004890/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004891 nb_index slot defined, and store in *pi.
4892 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004893 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004894 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004895*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004896int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004897_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004898{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004899 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004900 Py_ssize_t x;
4901 if (PyIndex_Check(v)) {
4902 x = PyNumber_AsSsize_t(v, NULL);
4903 if (x == -1 && PyErr_Occurred())
4904 return 0;
4905 }
4906 else {
4907 PyErr_SetString(PyExc_TypeError,
4908 "slice indices must be integers or "
4909 "None or have an __index__ method");
4910 return 0;
4911 }
4912 *pi = x;
4913 }
4914 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004915}
4916
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004917int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004918_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004919{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004920 Py_ssize_t x;
4921 if (PyIndex_Check(v)) {
4922 x = PyNumber_AsSsize_t(v, NULL);
4923 if (x == -1 && PyErr_Occurred())
4924 return 0;
4925 }
4926 else {
4927 PyErr_SetString(PyExc_TypeError,
4928 "slice indices must be integers or "
4929 "have an __index__ method");
4930 return 0;
4931 }
4932 *pi = x;
4933 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004934}
4935
4936
Guido van Rossum486364b2007-06-30 05:01:58 +00004937#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004938 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004939
Guido van Rossumb209a111997-04-29 18:18:01 +00004940static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004941cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004942{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004943 int res = 0;
4944 switch (op) {
4945 case PyCmp_IS:
4946 res = (v == w);
4947 break;
4948 case PyCmp_IS_NOT:
4949 res = (v != w);
4950 break;
4951 case PyCmp_IN:
4952 res = PySequence_Contains(w, v);
4953 if (res < 0)
4954 return NULL;
4955 break;
4956 case PyCmp_NOT_IN:
4957 res = PySequence_Contains(w, v);
4958 if (res < 0)
4959 return NULL;
4960 res = !res;
4961 break;
4962 case PyCmp_EXC_MATCH:
4963 if (PyTuple_Check(w)) {
4964 Py_ssize_t i, length;
4965 length = PyTuple_Size(w);
4966 for (i = 0; i < length; i += 1) {
4967 PyObject *exc = PyTuple_GET_ITEM(w, i);
4968 if (!PyExceptionClass_Check(exc)) {
4969 PyErr_SetString(PyExc_TypeError,
4970 CANNOT_CATCH_MSG);
4971 return NULL;
4972 }
4973 }
4974 }
4975 else {
4976 if (!PyExceptionClass_Check(w)) {
4977 PyErr_SetString(PyExc_TypeError,
4978 CANNOT_CATCH_MSG);
4979 return NULL;
4980 }
4981 }
4982 res = PyErr_GivenExceptionMatches(v, w);
4983 break;
4984 default:
4985 return PyObject_RichCompare(v, w, op);
4986 }
4987 v = res ? Py_True : Py_False;
4988 Py_INCREF(v);
4989 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004990}
4991
Thomas Wouters52152252000-08-17 22:55:00 +00004992static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004993import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4994{
4995 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004996 PyObject *import_func, *res;
4997 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004998
4999 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
5000 if (import_func == NULL) {
5001 PyErr_SetString(PyExc_ImportError, "__import__ not found");
5002 return NULL;
5003 }
5004
5005 /* Fast path for not overloaded __import__. */
5006 if (import_func == PyThreadState_GET()->interp->import_func) {
5007 int ilevel = _PyLong_AsInt(level);
5008 if (ilevel == -1 && PyErr_Occurred()) {
5009 return NULL;
5010 }
5011 res = PyImport_ImportModuleLevelObject(
5012 name,
5013 f->f_globals,
5014 f->f_locals == NULL ? Py_None : f->f_locals,
5015 fromlist,
5016 ilevel);
5017 return res;
5018 }
5019
5020 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005021
5022 stack[0] = name;
5023 stack[1] = f->f_globals;
5024 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5025 stack[3] = fromlist;
5026 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005027 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005028 Py_DECREF(import_func);
5029 return res;
5030}
5031
5032static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00005033import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005034{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005035 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005036 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08005037 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005038
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005039 x = PyObject_GetAttr(v, name);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005040 if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
5041 return x;
5042 /* Issue #17636: in case this failed because of a circular relative
5043 import, try to fallback on reading the module directly from
5044 sys.modules. */
5045 PyErr_Clear();
5046 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005047 if (pkgname == NULL) {
5048 goto error;
5049 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005050 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005051 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005052 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005053 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005054 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005055 x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005056 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005057 if (x == NULL) {
5058 goto error;
5059 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005060 Py_DECREF(pkgname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005061 Py_INCREF(x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005062 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005063 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005064 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005065 if (pkgname == NULL) {
5066 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5067 if (pkgname_or_unknown == NULL) {
5068 Py_XDECREF(pkgpath);
5069 return NULL;
5070 }
5071 } else {
5072 pkgname_or_unknown = pkgname;
5073 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005074
5075 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
5076 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08005077 errmsg = PyUnicode_FromFormat(
5078 "cannot import name %R from %R (unknown location)",
5079 name, pkgname_or_unknown
5080 );
5081 /* NULL check for errmsg done by PyErr_SetImportError. */
5082 PyErr_SetImportError(errmsg, pkgname, NULL);
5083 }
5084 else {
5085 errmsg = PyUnicode_FromFormat(
5086 "cannot import name %R from %R (%S)",
5087 name, pkgname_or_unknown, pkgpath
5088 );
5089 /* NULL check for errmsg done by PyErr_SetImportError. */
5090 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005091 }
5092
Xiang Zhang4830f582017-03-21 11:13:42 +08005093 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005094 Py_XDECREF(pkgname_or_unknown);
5095 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005096 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005097}
Guido van Rossumac7be682001-01-17 15:42:30 +00005098
Thomas Wouters52152252000-08-17 22:55:00 +00005099static int
5100import_all_from(PyObject *locals, PyObject *v)
5101{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005102 _Py_IDENTIFIER(__all__);
5103 _Py_IDENTIFIER(__dict__);
5104 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005105 PyObject *dict, *name, *value;
5106 int skip_leading_underscores = 0;
5107 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005109 if (all == NULL) {
5110 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5111 return -1; /* Unexpected error */
5112 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005113 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005114 if (dict == NULL) {
5115 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5116 return -1;
5117 PyErr_SetString(PyExc_ImportError,
5118 "from-import-* object has no __dict__ and no __all__");
5119 return -1;
5120 }
5121 all = PyMapping_Keys(dict);
5122 Py_DECREF(dict);
5123 if (all == NULL)
5124 return -1;
5125 skip_leading_underscores = 1;
5126 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005128 for (pos = 0, err = 0; ; pos++) {
5129 name = PySequence_GetItem(all, pos);
5130 if (name == NULL) {
5131 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5132 err = -1;
5133 else
5134 PyErr_Clear();
5135 break;
5136 }
5137 if (skip_leading_underscores &&
5138 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005139 PyUnicode_READY(name) != -1 &&
5140 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005141 {
5142 Py_DECREF(name);
5143 continue;
5144 }
5145 value = PyObject_GetAttr(v, name);
5146 if (value == NULL)
5147 err = -1;
5148 else if (PyDict_CheckExact(locals))
5149 err = PyDict_SetItem(locals, name, value);
5150 else
5151 err = PyObject_SetItem(locals, name, value);
5152 Py_DECREF(name);
5153 Py_XDECREF(value);
5154 if (err != 0)
5155 break;
5156 }
5157 Py_DECREF(all);
5158 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005159}
5160
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005161static int
5162check_args_iterable(PyObject *func, PyObject *args)
5163{
5164 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
5165 PyErr_Format(PyExc_TypeError,
5166 "%.200s%.200s argument after * "
5167 "must be an iterable, not %.200s",
5168 PyEval_GetFuncName(func),
5169 PyEval_GetFuncDesc(func),
5170 args->ob_type->tp_name);
5171 return -1;
5172 }
5173 return 0;
5174}
5175
5176static void
5177format_kwargs_mapping_error(PyObject *func, PyObject *kwargs)
5178{
5179 PyErr_Format(PyExc_TypeError,
5180 "%.200s%.200s argument after ** "
5181 "must be a mapping, not %.200s",
5182 PyEval_GetFuncName(func),
5183 PyEval_GetFuncDesc(func),
5184 kwargs->ob_type->tp_name);
5185}
5186
Guido van Rossumac7be682001-01-17 15:42:30 +00005187static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005188format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005189{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005190 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005192 if (!obj)
5193 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005194
Serhiy Storchaka06515832016-11-20 09:13:07 +02005195 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005196 if (!obj_str)
5197 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005199 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005200}
Guido van Rossum950361c1997-01-24 13:49:28 +00005201
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005202static void
5203format_exc_unbound(PyCodeObject *co, int oparg)
5204{
5205 PyObject *name;
5206 /* Don't stomp existing exception */
5207 if (PyErr_Occurred())
5208 return;
5209 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5210 name = PyTuple_GET_ITEM(co->co_cellvars,
5211 oparg);
5212 format_exc_check_arg(
5213 PyExc_UnboundLocalError,
5214 UNBOUNDLOCAL_ERROR_MSG,
5215 name);
5216 } else {
5217 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5218 PyTuple_GET_SIZE(co->co_cellvars));
5219 format_exc_check_arg(PyExc_NameError,
5220 UNBOUNDFREE_ERROR_MSG, name);
5221 }
5222}
5223
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005224static PyObject *
5225unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005226 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005227{
5228 PyObject *res;
5229 if (Py_REFCNT(v) == 2) {
5230 /* In the common case, there are 2 references to the value
5231 * stored in 'variable' when the += is performed: one on the
5232 * value stack (in 'v') and one still stored in the
5233 * 'variable'. We try to delete the variable now to reduce
5234 * the refcnt to 1.
5235 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005236 int opcode, oparg;
5237 NEXTOPARG();
5238 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005239 case STORE_FAST:
5240 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005241 PyObject **fastlocals = f->f_localsplus;
5242 if (GETLOCAL(oparg) == v)
5243 SETLOCAL(oparg, NULL);
5244 break;
5245 }
5246 case STORE_DEREF:
5247 {
5248 PyObject **freevars = (f->f_localsplus +
5249 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005250 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005251 if (PyCell_GET(c) == v) {
5252 PyCell_SET(c, NULL);
5253 Py_DECREF(v);
5254 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005255 break;
5256 }
5257 case STORE_NAME:
5258 {
5259 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005260 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005261 PyObject *locals = f->f_locals;
5262 if (PyDict_CheckExact(locals) &&
5263 PyDict_GetItem(locals, name) == v) {
5264 if (PyDict_DelItem(locals, name) != 0) {
5265 PyErr_Clear();
5266 }
5267 }
5268 break;
5269 }
5270 }
5271 }
5272 res = v;
5273 PyUnicode_Append(&res, w);
5274 return res;
5275}
5276
Guido van Rossum950361c1997-01-24 13:49:28 +00005277#ifdef DYNAMIC_EXECUTION_PROFILE
5278
Skip Montanarof118cb12001-10-15 20:51:38 +00005279static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005280getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005281{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005282 int i;
5283 PyObject *l = PyList_New(256);
5284 if (l == NULL) return NULL;
5285 for (i = 0; i < 256; i++) {
5286 PyObject *x = PyLong_FromLong(a[i]);
5287 if (x == NULL) {
5288 Py_DECREF(l);
5289 return NULL;
5290 }
5291 PyList_SetItem(l, i, x);
5292 }
5293 for (i = 0; i < 256; i++)
5294 a[i] = 0;
5295 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005296}
5297
5298PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005299_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005300{
5301#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005302 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005303#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005304 int i;
5305 PyObject *l = PyList_New(257);
5306 if (l == NULL) return NULL;
5307 for (i = 0; i < 257; i++) {
5308 PyObject *x = getarray(dxpairs[i]);
5309 if (x == NULL) {
5310 Py_DECREF(l);
5311 return NULL;
5312 }
5313 PyList_SetItem(l, i, x);
5314 }
5315 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005316#endif
5317}
5318
5319#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005320
5321Py_ssize_t
5322_PyEval_RequestCodeExtraIndex(freefunc free)
5323{
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005324 PyInterpreterState *interp = PyThreadState_Get()->interp;
Brett Cannon5c4de282016-09-07 11:16:41 -07005325 Py_ssize_t new_index;
5326
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005327 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005328 return -1;
5329 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005330 new_index = interp->co_extra_user_count++;
5331 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005332 return new_index;
5333}
Łukasz Langaa785c872016-09-09 17:37:37 -07005334
5335static void
5336dtrace_function_entry(PyFrameObject *f)
5337{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005338 const char *filename;
5339 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005340 int lineno;
5341
5342 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5343 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5344 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5345
5346 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5347}
5348
5349static void
5350dtrace_function_return(PyFrameObject *f)
5351{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005352 const char *filename;
5353 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005354 int lineno;
5355
5356 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5357 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5358 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5359
5360 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5361}
5362
5363/* DTrace equivalent of maybe_call_line_trace. */
5364static void
5365maybe_dtrace_line(PyFrameObject *frame,
5366 int *instr_lb, int *instr_ub, int *instr_prev)
5367{
5368 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005369 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005370
5371 /* If the last instruction executed isn't in the current
5372 instruction window, reset the window.
5373 */
5374 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5375 PyAddrPair bounds;
5376 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5377 &bounds);
5378 *instr_lb = bounds.ap_lower;
5379 *instr_ub = bounds.ap_upper;
5380 }
5381 /* If the last instruction falls at the start of a line or if
5382 it represents a jump backwards, update the frame's line
5383 number and call the trace function. */
5384 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5385 frame->f_lineno = line;
5386 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5387 if (!co_filename)
5388 co_filename = "?";
5389 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5390 if (!co_name)
5391 co_name = "?";
5392 PyDTrace_LINE(co_filename, co_name, line);
5393 }
5394 *instr_prev = frame->f_lasti;
5395}