blob: 59fc070f9e7ebda85e6a3086278323e0a18c688e [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 *);
Guido van Rossum374a9221991-04-04 10:40:29 +000069
Paul Prescode68140d2000-08-30 20:25:01 +000070#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000072#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000074#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 "free variable '%.200s' referenced before assignment" \
76 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000077
Guido van Rossum950361c1997-01-24 13:49:28 +000078/* Dynamic execution profile */
79#ifdef DYNAMIC_EXECUTION_PROFILE
80#ifdef DXPAIRS
81static long dxpairs[257][256];
82#define dxp dxpairs[256]
83#else
84static long dxp[256];
85#endif
86#endif
87
Benjamin Petersond2be5b42010-09-10 22:47:02 +000088#ifdef WITH_THREAD
89#define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request)
90#else
91#define GIL_REQUEST 0
92#endif
93
Jeffrey Yasskin39370832010-05-03 19:29:34 +000094/* This can set eval_breaker to 0 even though gil_drop_request became
95 1. We believe this is all right because the eval loop will release
96 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +000097#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 _Py_atomic_store_relaxed( \
99 &eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000100 GIL_REQUEST | \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
102 pending_async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000103
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000104#ifdef WITH_THREAD
105
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000106#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 do { \
108 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
109 _Py_atomic_store_relaxed(&eval_breaker, 1); \
110 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000111
112#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 do { \
114 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
115 COMPUTE_EVAL_BREAKER(); \
116 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000117
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000118#endif
119
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000120/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000121#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 do { \
123 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
124 _Py_atomic_store_relaxed(&eval_breaker, 1); \
125 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000126
127#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 do { \
129 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
130 COMPUTE_EVAL_BREAKER(); \
131 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000132
133#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 do { \
135 pending_async_exc = 1; \
136 _Py_atomic_store_relaxed(&eval_breaker, 1); \
137 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000138
139#define UNSIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000141
142
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200143/* This single variable consolidates all requests to break out of the fast path
144 in the eval loop. */
145static _Py_atomic_int eval_breaker = {0};
146/* Request for running pending calls. */
147static _Py_atomic_int pendingcalls_to_do = {0};
148/* Request for looking at the `async_exc` field of the current thread state.
149 Guarded by the GIL. */
150static int pending_async_exc = 0;
151
Guido van Rossume59214e1994-08-30 08:01:59 +0000152#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000153
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000154#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000155#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000156#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000157#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000158
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000159static PyThread_type_lock pending_lock = 0; /* for pending calls */
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200160static unsigned long main_thread = 0;
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000161/* Request for dropping the GIL */
162static _Py_atomic_int gil_drop_request = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000163
164#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000165
Tim Peters7f468f22004-10-11 02:40:51 +0000166int
167PyEval_ThreadsInitialized(void)
168{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000170}
171
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000172void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000173PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 if (gil_created())
176 return;
177 create_gil();
178 take_gil(PyThreadState_GET());
179 main_thread = PyThread_get_thread_ident();
180 if (!pending_lock)
181 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000182}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000183
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000184void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000185_PyEval_FiniThreads(void)
186{
187 if (!gil_created())
188 return;
189 destroy_gil();
190 assert(!gil_created());
191}
192
193void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000194PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 PyThreadState *tstate = PyThreadState_GET();
197 if (tstate == NULL)
198 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
199 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000200}
201
202void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000203PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 /* This function must succeed when the current thread state is NULL.
206 We therefore avoid PyThreadState_GET() which dumps a fatal error
207 in debug mode.
208 */
209 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
210 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000211}
212
213void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000214PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 if (tstate == NULL)
217 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
218 /* Check someone has called PyEval_InitThreads() to create the lock */
219 assert(gil_created());
220 take_gil(tstate);
221 if (PyThreadState_Swap(tstate) != NULL)
222 Py_FatalError(
223 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000224}
225
226void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000227PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000228{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 if (tstate == NULL)
230 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
231 if (PyThreadState_Swap(NULL) != tstate)
232 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
233 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000234}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000235
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200236/* This function is called from PyOS_AfterFork_Child to destroy all threads
237 * which are not running in the child process, and clear internal locks
238 * which might be held by those threads.
239 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000240
241void
242PyEval_ReInitThreads(void)
243{
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200244 PyThreadState *current_tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 if (!gil_created())
247 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 recreate_gil();
249 pending_lock = PyThread_allocate_lock();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200250 take_gil(current_tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000252
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200253 /* Destroy all threads except the current one */
254 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000255}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000256
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000257#endif /* WITH_THREAD */
258
259/* This function is used to signal that async exceptions are waiting to be
260 raised, therefore it is also useful in non-threaded builds. */
261
262void
263_PyEval_SignalAsyncExc(void)
264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000266}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000267
Guido van Rossumff4949e1992-08-05 19:58:53 +0000268/* Functions save_thread and restore_thread are always defined so
269 dynamically loaded modules needn't be compiled separately for use
270 with and without threads: */
271
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000272PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000273PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 PyThreadState *tstate = PyThreadState_Swap(NULL);
276 if (tstate == NULL)
277 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000278#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 if (gil_created())
280 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000281#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000283}
284
285void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000286PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 if (tstate == NULL)
289 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000290#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 if (gil_created()) {
292 int err = errno;
293 take_gil(tstate);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200294 /* _Py_Finalizing is protected by the GIL */
295 if (_Py_Finalizing && tstate != _Py_Finalizing) {
296 drop_gil(tstate);
297 PyThread_exit_thread();
298 assert(0); /* unreachable */
299 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 errno = err;
301 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000302#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000304}
305
306
Guido van Rossuma9672091994-09-14 13:31:22 +0000307/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
308 signal handlers or Mac I/O completion routines) can schedule calls
309 to a function to be called synchronously.
310 The synchronous function is called with one void* argument.
311 It should return 0 for success or -1 for failure -- failure should
312 be accompanied by an exception.
313
314 If registry succeeds, the registry function returns 0; if it fails
315 (e.g. due to too many pending calls) it returns -1 (without setting
316 an exception condition).
317
318 Note that because registry may occur from within signal handlers,
319 or other asynchronous events, calling malloc() is unsafe!
320
321#ifdef WITH_THREAD
322 Any thread can schedule pending calls, but only the main thread
323 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000324 There is no facility to schedule calls to a particular thread, but
325 that should be easy to change, should that ever be required. In
326 that case, the static variables here should go into the python
327 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000328#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000329*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000330
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200331void
332_PyEval_SignalReceived(void)
333{
334 /* bpo-30703: Function called when the C signal handler of Python gets a
335 signal. We cannot queue a callback using Py_AddPendingCall() since
336 that function is not async-signal-safe. */
337 SIGNAL_PENDING_CALLS();
338}
339
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000340#ifdef WITH_THREAD
341
342/* The WITH_THREAD implementation is thread-safe. It allows
343 scheduling to be made from any thread, and even from an executing
344 callback.
345 */
346
347#define NPENDINGCALLS 32
348static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 int (*func)(void *);
350 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000351} pendingcalls[NPENDINGCALLS];
352static int pendingfirst = 0;
353static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000354
355int
356Py_AddPendingCall(int (*func)(void *), void *arg)
357{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 int i, j, result=0;
359 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 /* try a few times for the lock. Since this mechanism is used
362 * for signal handling (on the main thread), there is a (slim)
363 * chance that a signal is delivered on the same thread while we
364 * hold the lock during the Py_MakePendingCalls() function.
365 * This avoids a deadlock in that case.
366 * Note that signals can be delivered on any thread. In particular,
367 * on Windows, a SIGINT is delivered on a system-created worker
368 * thread.
369 * We also check for lock being NULL, in the unlikely case that
370 * this function is called before any bytecode evaluation takes place.
371 */
372 if (lock != NULL) {
373 for (i = 0; i<100; i++) {
374 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
375 break;
376 }
377 if (i == 100)
378 return -1;
379 }
380
381 i = pendinglast;
382 j = (i + 1) % NPENDINGCALLS;
383 if (j == pendingfirst) {
384 result = -1; /* Queue full */
385 } else {
386 pendingcalls[i].func = func;
387 pendingcalls[i].arg = arg;
388 pendinglast = j;
389 }
390 /* signal main loop */
391 SIGNAL_PENDING_CALLS();
392 if (lock != NULL)
393 PyThread_release_lock(lock);
394 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000395}
396
397int
398Py_MakePendingCalls(void)
399{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200400 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 int i;
402 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000403
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200404 assert(PyGILState_Check());
405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 if (!pending_lock) {
407 /* initial allocation of the lock */
408 pending_lock = PyThread_allocate_lock();
409 if (pending_lock == NULL)
410 return -1;
411 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 /* only service pending calls on main thread */
414 if (main_thread && PyThread_get_thread_ident() != main_thread)
415 return 0;
416 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200417 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200419 busy = 1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200420 /* unsignal before starting to call callbacks, so that any callback
421 added in-between re-signals */
422 UNSIGNAL_PENDING_CALLS();
423
424 /* Python signal handler doesn't really queue a callback: it only signals
425 that a signal was received, see _PyEval_SignalReceived(). */
426 if (PyErr_CheckSignals() < 0) {
427 goto error;
428 }
429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 /* perform a bounded number of calls, in case of recursion */
431 for (i=0; i<NPENDINGCALLS; i++) {
432 int j;
433 int (*func)(void *);
434 void *arg = NULL;
435
436 /* pop one item off the queue while holding the lock */
437 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
438 j = pendingfirst;
439 if (j == pendinglast) {
440 func = NULL; /* Queue empty */
441 } else {
442 func = pendingcalls[j].func;
443 arg = pendingcalls[j].arg;
444 pendingfirst = (j + 1) % NPENDINGCALLS;
445 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 PyThread_release_lock(pending_lock);
447 /* having released the lock, perform the callback */
448 if (func == NULL)
449 break;
450 r = func(arg);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200451 if (r) {
452 goto error;
453 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200455
Charles-François Natalif23339a2011-07-23 18:15:43 +0200456 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 return r;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200458
459error:
460 busy = 0;
461 SIGNAL_PENDING_CALLS(); /* We're not done yet */
462 return -1;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000463}
464
465#else /* if ! defined WITH_THREAD */
466
467/*
468 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
469 This code is used for signal handling in python that isn't built
470 with WITH_THREAD.
471 Don't use this implementation when Py_AddPendingCalls() can happen
472 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473
Guido van Rossuma9672091994-09-14 13:31:22 +0000474 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000475 (1) nested asynchronous calls to Py_AddPendingCall()
476 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000478 (1) is very unlikely because typically signal delivery
479 is blocked during signal handling. So it should be impossible.
480 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000481 The current code is safe against (2), but not against (1).
482 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000483 thread is present, interrupted by signals, and that the critical
484 section is protected with the "busy" variable. On Windows, which
485 delivers SIGINT on a system thread, this does not hold and therefore
486 Windows really shouldn't use this version.
487 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000488*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000489
Guido van Rossuma9672091994-09-14 13:31:22 +0000490#define NPENDINGCALLS 32
491static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 int (*func)(void *);
493 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000494} pendingcalls[NPENDINGCALLS];
495static volatile int pendingfirst = 0;
496static volatile int pendinglast = 0;
497
498int
Thomas Wouters334fb892000-07-25 12:56:38 +0000499Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000500{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 static volatile int busy = 0;
502 int i, j;
503 /* XXX Begin critical section */
504 if (busy)
505 return -1;
506 busy = 1;
507 i = pendinglast;
508 j = (i + 1) % NPENDINGCALLS;
509 if (j == pendingfirst) {
510 busy = 0;
511 return -1; /* Queue full */
512 }
513 pendingcalls[i].func = func;
514 pendingcalls[i].arg = arg;
515 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 SIGNAL_PENDING_CALLS();
518 busy = 0;
519 /* XXX End critical section */
520 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000521}
522
Guido van Rossum180d7b41994-09-29 09:45:57 +0000523int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000524Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000525{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 static int busy = 0;
527 if (busy)
528 return 0;
529 busy = 1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200530
531 /* unsignal before starting to call callbacks, so that any callback
532 added in-between re-signals */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 UNSIGNAL_PENDING_CALLS();
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200534 /* Python signal handler doesn't really queue a callback: it only signals
535 that a signal was received, see _PyEval_SignalReceived(). */
536 if (PyErr_CheckSignals() < 0) {
537 goto error;
538 }
539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 for (;;) {
541 int i;
542 int (*func)(void *);
543 void *arg;
544 i = pendingfirst;
545 if (i == pendinglast)
546 break; /* Queue empty */
547 func = pendingcalls[i].func;
548 arg = pendingcalls[i].arg;
549 pendingfirst = (i + 1) % NPENDINGCALLS;
550 if (func(arg) < 0) {
Masayuki Yamamoto0c311632017-07-05 17:39:17 +0900551 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 }
553 }
554 busy = 0;
555 return 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200556
557error:
558 busy = 0;
559 SIGNAL_PENDING_CALLS(); /* We're not done yet */
560 return -1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000561}
562
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000563#endif /* WITH_THREAD */
564
Guido van Rossuma9672091994-09-14 13:31:22 +0000565
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000566/* The interpreter's recursion limit */
567
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000568#ifndef Py_DEFAULT_RECURSION_LIMIT
569#define Py_DEFAULT_RECURSION_LIMIT 1000
570#endif
571static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
572int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000573
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000574int
575Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000576{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000578}
579
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000580void
581Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 recursion_limit = new_limit;
584 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000585}
586
Armin Rigo2b3eb402003-10-28 12:05:48 +0000587/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
588 if the recursion_depth reaches _Py_CheckRecursionLimit.
589 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
590 to guarantee that _Py_CheckRecursiveCall() is regularly called.
591 Without USE_STACKCHECK, there is no need for this. */
592int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300593_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000594{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000596
597#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 if (PyOS_CheckStack()) {
599 --tstate->recursion_depth;
600 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
601 return -1;
602 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000603#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 _Py_CheckRecursionLimit = recursion_limit;
605 if (tstate->recursion_critical)
606 /* Somebody asked that we don't check for recursion. */
607 return 0;
608 if (tstate->overflowed) {
609 if (tstate->recursion_depth > recursion_limit + 50) {
610 /* Overflowing while handling an overflow. Give up. */
611 Py_FatalError("Cannot recover from stack overflow.");
612 }
613 return 0;
614 }
615 if (tstate->recursion_depth > recursion_limit) {
616 --tstate->recursion_depth;
617 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400618 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 "maximum recursion depth exceeded%s",
620 where);
621 return -1;
622 }
623 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000624}
625
Guido van Rossum374a9221991-04-04 10:40:29 +0000626/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000627enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000628 WHY_NOT = 0x0001, /* No error */
629 WHY_EXCEPTION = 0x0002, /* Exception occurred */
Stefan Krahb7e10102010-06-23 18:42:39 +0000630 WHY_RETURN = 0x0008, /* 'return' statement */
631 WHY_BREAK = 0x0010, /* 'break' statement */
632 WHY_CONTINUE = 0x0020, /* 'continue' statement */
633 WHY_YIELD = 0x0040, /* 'yield' operator */
634 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000635};
Guido van Rossum374a9221991-04-04 10:40:29 +0000636
Benjamin Peterson87880242011-07-03 16:48:31 -0500637static void save_exc_state(PyThreadState *, PyFrameObject *);
638static void swap_exc_state(PyThreadState *, PyFrameObject *);
639static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400640static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000641static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000642
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000643/* Records whether tracing is on for any thread. Counts the number of
644 threads for which tstate->c_tracefunc is non-NULL, so if the value
645 is 0, we know we don't have to check this thread's c_tracefunc.
646 This speeds up the if statement in PyEval_EvalFrameEx() after
647 fast_next_opcode*/
648static int _Py_TracingPossible = 0;
649
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000650
Guido van Rossum374a9221991-04-04 10:40:29 +0000651
Guido van Rossumb209a111997-04-29 18:18:01 +0000652PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000653PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000654{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 return PyEval_EvalCodeEx(co,
656 globals, locals,
657 (PyObject **)NULL, 0,
658 (PyObject **)NULL, 0,
659 (PyObject **)NULL, 0,
660 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000661}
662
663
664/* Interpreter main loop */
665
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000666PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000667PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 /* This is for backward compatibility with extension modules that
669 used this API; core interpreter code should call
670 PyEval_EvalFrameEx() */
671 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000672}
673
674PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000675PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000676{
Brett Cannon3cebf932016-09-05 15:33:46 -0700677 PyThreadState *tstate = PyThreadState_GET();
678 return tstate->interp->eval_frame(f, throwflag);
679}
680
Victor Stinnerc6944e72016-11-11 02:13:35 +0100681PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700682_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
683{
Guido van Rossum950361c1997-01-24 13:49:28 +0000684#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000686#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200687 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300688 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200689 int opcode; /* Current opcode */
690 int oparg; /* Current opcode argument, if any */
691 enum why_code why; /* Reason for block stack unwind */
692 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 PyObject *retval = NULL; /* Return value */
694 PyThreadState *tstate = PyThreadState_GET();
695 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 is true when the line being executed has changed. The
702 initial values are such as to make this false the first
703 time it is tested. */
704 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000705
Serhiy Storchakaab874002016-09-11 13:48:15 +0300706 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 PyObject *names;
708 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000709
Brett Cannon368b4b72012-04-02 12:17:59 -0400710#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200711 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400712#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200713
Antoine Pitroub52ec782009-01-25 16:34:23 +0000714/* Computed GOTOs, or
715 the-optimization-commonly-but-improperly-known-as-"threaded code"
716 using gcc's labels-as-values extension
717 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
718
719 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000721 combined with a lookup table of jump addresses. However, since the
722 indirect jump instruction is shared by all opcodes, the CPU will have a
723 hard time making the right prediction for where to jump next (actually,
724 it will be always wrong except in the uncommon case of a sequence of
725 several identical opcodes).
726
727 "Threaded code" in contrast, uses an explicit jump table and an explicit
728 indirect jump instruction at the end of each opcode. Since the jump
729 instruction is at a different address for each opcode, the CPU will make a
730 separate prediction for each of these instructions, which is equivalent to
731 predicting the second opcode of each opcode pair. These predictions have
732 a much better chance to turn out valid, especially in small bytecode loops.
733
734 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000736 and potentially many more instructions (depending on the pipeline width).
737 A correctly predicted branch, however, is nearly free.
738
739 At the time of this writing, the "threaded code" version is up to 15-20%
740 faster than the normal "switch" version, depending on the compiler and the
741 CPU architecture.
742
743 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
744 because it would render the measurements invalid.
745
746
747 NOTE: care must be taken that the compiler doesn't try to "optimize" the
748 indirect jumps by sharing them between all opcodes. Such optimizations
749 can be disabled on gcc by using the -fno-gcse flag (or possibly
750 -fno-crossjumping).
751*/
752
Antoine Pitrou042b1282010-08-13 21:15:58 +0000753#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000754#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000755#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000756#endif
757
Antoine Pitrou042b1282010-08-13 21:15:58 +0000758#ifdef HAVE_COMPUTED_GOTOS
759 #ifndef USE_COMPUTED_GOTOS
760 #define USE_COMPUTED_GOTOS 1
761 #endif
762#else
763 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
764 #error "Computed gotos are not supported on this compiler."
765 #endif
766 #undef USE_COMPUTED_GOTOS
767 #define USE_COMPUTED_GOTOS 0
768#endif
769
770#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000771/* Import the static jump table */
772#include "opcode_targets.h"
773
Antoine Pitroub52ec782009-01-25 16:34:23 +0000774#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 TARGET_##op: \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000777
Antoine Pitroub52ec782009-01-25 16:34:23 +0000778#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 { \
780 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
781 FAST_DISPATCH(); \
782 } \
783 continue; \
784 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000785
786#ifdef LLTRACE
787#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700789 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300791 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300792 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 } \
794 goto fast_next_opcode; \
795 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000796#else
797#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700799 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300801 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300802 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 } \
804 goto fast_next_opcode; \
805 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000806#endif
807
808#else
809#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 case op:
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300811
Antoine Pitroub52ec782009-01-25 16:34:23 +0000812#define DISPATCH() continue
813#define FAST_DISPATCH() goto fast_next_opcode
814#endif
815
816
Neal Norwitza81d2202002-07-14 00:27:26 +0000817/* Tuple access macros */
818
819#ifndef Py_DEBUG
820#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
821#else
822#define GETITEM(v, i) PyTuple_GetItem((v), (i))
823#endif
824
Guido van Rossum374a9221991-04-04 10:40:29 +0000825/* Code access macros */
826
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300827/* The integer overflow is checked by an assertion below. */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300828#define INSTR_OFFSET() (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300829#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300830 _Py_CODEUNIT word = *next_instr; \
831 opcode = _Py_OPCODE(word); \
832 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300833 next_instr++; \
834 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300835#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
836#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000837
Raymond Hettingerf606f872003-03-16 03:11:04 +0000838/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 Some opcodes tend to come in pairs thus making it possible to
840 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300841 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000842
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 Verifying the prediction costs a single high-speed test of a register
844 variable against a constant. If the pairing was good, then the
845 processor's own internal branch predication has a high likelihood of
846 success, resulting in a nearly zero-overhead transition to the
847 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300848 including its unpredictable switch-case branch. Combined with the
849 processor's internal branch prediction, a successful PREDICT has the
850 effect of making the two opcodes run as if they were a single new opcode
851 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000852
Georg Brandl86b2fb92008-07-16 03:43:04 +0000853 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 predictions turned-on and interpret the results as if some opcodes
855 had been combined or turn-off predictions so that the opcode frequency
856 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000857
858 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 the CPU to record separate branch prediction information for each
860 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000861
Raymond Hettingerf606f872003-03-16 03:11:04 +0000862*/
863
Antoine Pitrou042b1282010-08-13 21:15:58 +0000864#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000866#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300867#define PREDICT(op) \
868 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300869 _Py_CODEUNIT word = *next_instr; \
870 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300871 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300872 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300873 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300874 goto PRED_##op; \
875 } \
876 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000877#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300878#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000879
Raymond Hettingerf606f872003-03-16 03:11:04 +0000880
Guido van Rossum374a9221991-04-04 10:40:29 +0000881/* Stack manipulation macros */
882
Martin v. Löwis18e16552006-02-15 17:27:45 +0000883/* The stack can grow at most MAXINT deep, as co_nlocals and
884 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000885#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
886#define EMPTY() (STACK_LEVEL() == 0)
887#define TOP() (stack_pointer[-1])
888#define SECOND() (stack_pointer[-2])
889#define THIRD() (stack_pointer[-3])
890#define FOURTH() (stack_pointer[-4])
891#define PEEK(n) (stack_pointer[-(n)])
892#define SET_TOP(v) (stack_pointer[-1] = (v))
893#define SET_SECOND(v) (stack_pointer[-2] = (v))
894#define SET_THIRD(v) (stack_pointer[-3] = (v))
895#define SET_FOURTH(v) (stack_pointer[-4] = (v))
896#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
897#define BASIC_STACKADJ(n) (stack_pointer += n)
898#define BASIC_PUSH(v) (*stack_pointer++ = (v))
899#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000900
Guido van Rossum96a42c81992-01-12 02:29:51 +0000901#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000903 lltrace && prtrace(TOP(), "push")); \
904 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000906 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000908 lltrace && prtrace(TOP(), "stackadj")); \
909 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +0000910#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000911 prtrace((STACK_POINTER)[-1], "ext_pop")), \
912 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000913#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000914#define PUSH(v) BASIC_PUSH(v)
915#define POP() BASIC_POP()
916#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000917#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000918#endif
919
Guido van Rossum681d79a1995-07-18 14:51:37 +0000920/* Local variable macros */
921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000923
924/* The SETLOCAL() macro must not DECREF the local variable in-place and
925 then store the new value; it must copy the old value to a temporary
926 value, then store the new value, and then DECREF the temporary value.
927 This is because it is possible that during the DECREF the frame is
928 accessed by other code (e.g. a __del__ method or gc.collect()) and the
929 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000931 GETLOCAL(i) = value; \
932 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000933
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000934
935#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 while (STACK_LEVEL() > (b)->b_level) { \
937 PyObject *v = POP(); \
938 Py_XDECREF(v); \
939 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000940
941#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300942 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 PyObject *type, *value, *traceback; \
944 assert(STACK_LEVEL() >= (b)->b_level + 3); \
945 while (STACK_LEVEL() > (b)->b_level + 3) { \
946 value = POP(); \
947 Py_XDECREF(value); \
948 } \
949 type = tstate->exc_type; \
950 value = tstate->exc_value; \
951 traceback = tstate->exc_traceback; \
952 tstate->exc_type = POP(); \
953 tstate->exc_value = POP(); \
954 tstate->exc_traceback = POP(); \
955 Py_XDECREF(type); \
956 Py_XDECREF(value); \
957 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300958 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000959
Guido van Rossuma027efa1997-05-05 20:56:21 +0000960/* Start of code */
961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 /* push frame */
963 if (Py_EnterRecursiveCall(""))
964 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 if (tstate->use_tracing) {
969 if (tstate->c_tracefunc != NULL) {
970 /* tstate->c_tracefunc, if defined, is a
971 function that will be called on *every* entry
972 to a code block. Its return value, if not
973 None, is a function that will be called at
974 the start of each executed line of code.
975 (Actually, the function must return itself
976 in order to continue tracing.) The trace
977 functions are called with three arguments:
978 a pointer to the current frame, a string
979 indicating why the function is called, and
980 an argument which depends on the situation.
981 The global trace function is also called
982 whenever an exception is detected. */
983 if (call_trace_protected(tstate->c_tracefunc,
984 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100985 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 /* Trace function raised an error */
987 goto exit_eval_frame;
988 }
989 }
990 if (tstate->c_profilefunc != NULL) {
991 /* Similar for c_profilefunc, except it needn't
992 return itself and isn't called for "line" events */
993 if (call_trace_protected(tstate->c_profilefunc,
994 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100995 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 /* Profile function raised an error */
997 goto exit_eval_frame;
998 }
999 }
1000 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001001
Łukasz Langaa785c872016-09-09 17:37:37 -07001002 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1003 dtrace_function_entry(f);
1004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 co = f->f_code;
1006 names = co->co_names;
1007 consts = co->co_consts;
1008 fastlocals = f->f_localsplus;
1009 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001010 assert(PyBytes_Check(co->co_code));
1011 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001012 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1013 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1014 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001015 /*
1016 f->f_lasti refers to the index of the last instruction,
1017 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001018
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001019 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001020 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 When the PREDICT() macros are enabled, some opcode pairs follow in
1023 direct succession without updating f->f_lasti. A successful
1024 prediction effectively links the two codes together as if they
1025 were a single new opcode; accordingly,f->f_lasti will point to
1026 the first code in the pair (for instance, GET_ITER followed by
1027 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001028 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001030 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001031 next_instr = first_instr;
1032 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001033 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1034 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001035 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 stack_pointer = f->f_stacktop;
1037 assert(stack_pointer != NULL);
1038 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001039 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001040
Yury Selivanoveb636452016-09-08 22:01:51 -07001041 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01001042 if (!throwflag && f->f_exc_type != NULL && f->f_exc_type != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 /* We were in an except handler when we left,
1044 restore the exception state which was put aside
1045 (see YIELD_VALUE). */
Benjamin Peterson87880242011-07-03 16:48:31 -05001046 swap_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 }
Benjamin Peterson87880242011-07-03 16:48:31 -05001048 else
1049 save_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001051
Tim Peters5ca576e2001-06-18 22:08:13 +00001052#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001053 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001054#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 why = WHY_NOT;
Guido van Rossumac7be682001-01-17 15:42:30 +00001057
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001058 if (throwflag) /* support for generator.throw() */
1059 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001060
Victor Stinnerace47d72013-07-18 01:41:08 +02001061#ifdef Py_DEBUG
1062 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001063 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001064 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +02001065 assert(!PyErr_Occurred());
1066#endif
1067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1070 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +02001071 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 /* Do periodic things. Doing this every time through
1074 the loop would add too much overhead, so we do it
1075 only every Nth instruction. We also do it if
1076 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1077 event needs attention (e.g. a signal handler or
1078 async I/O handler); see Py_AddPendingCall() and
1079 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 if (_Py_atomic_load_relaxed(&eval_breaker)) {
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001082 if (_Py_OPCODE(*next_instr) == SETUP_FINALLY ||
1083 _Py_OPCODE(*next_instr) == YIELD_FROM) {
1084 /* Two cases where we skip running signal handlers and other
1085 pending calls:
1086 - If we're about to enter the try: of a try/finally (not
1087 *very* useful, but might help in some cases and it's
1088 traditional)
1089 - If we're resuming a chain of nested 'yield from' or
1090 'await' calls, then each frame is parked with YIELD_FROM
1091 as its next opcode. If the user hit control-C we want to
1092 wait until we've reached the innermost frame before
1093 running the signal handler and raising KeyboardInterrupt
1094 (see bpo-30039).
1095 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 goto fast_next_opcode;
1097 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001099 if (Py_MakePendingCalls() < 0)
1100 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001102#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001103 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 /* Give another thread a chance */
1105 if (PyThreadState_Swap(NULL) != tstate)
1106 Py_FatalError("ceval: tstate mix-up");
1107 drop_gil(tstate);
1108
1109 /* Other threads may run now */
1110
1111 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001112
1113 /* Check if we should make a quick exit. */
1114 if (_Py_Finalizing && _Py_Finalizing != tstate) {
1115 drop_gil(tstate);
1116 PyThread_exit_thread();
1117 }
1118
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 if (PyThreadState_Swap(tstate) != NULL)
1120 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001122#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 /* Check for asynchronous exceptions. */
1124 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001125 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 tstate->async_exc = NULL;
1127 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001128 PyErr_SetNone(exc);
1129 Py_DECREF(exc);
1130 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 }
1132 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 fast_next_opcode:
1135 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001136
Łukasz Langaa785c872016-09-09 17:37:37 -07001137 if (PyDTrace_LINE_ENABLED())
1138 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001143 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001144 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 /* see maybe_call_line_trace
1146 for expository comments */
1147 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 err = maybe_call_line_trace(tstate->c_tracefunc,
1150 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001151 tstate, f,
1152 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 /* Reload possibly changed frame fields */
1154 JUMPTO(f->f_lasti);
1155 if (f->f_stacktop != NULL) {
1156 stack_pointer = f->f_stacktop;
1157 f->f_stacktop = NULL;
1158 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001159 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001161 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001165
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001166 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001167 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001168#ifdef DYNAMIC_EXECUTION_PROFILE
1169#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 dxpairs[lastopcode][opcode]++;
1171 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001172#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001174#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001175
Guido van Rossum96a42c81992-01-12 02:29:51 +00001176#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 if (lltrace) {
1180 if (HAS_ARG(opcode)) {
1181 printf("%d: %d, %d\n",
1182 f->f_lasti, opcode, oparg);
1183 }
1184 else {
1185 printf("%d: %d\n",
1186 f->f_lasti, opcode);
1187 }
1188 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001189#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 /* BEWARE!
1194 It is essential that any operation that fails sets either
1195 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1196 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 TARGET(NOP)
1199 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001200
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001201 TARGET(LOAD_FAST) {
1202 PyObject *value = GETLOCAL(oparg);
1203 if (value == NULL) {
1204 format_exc_check_arg(PyExc_UnboundLocalError,
1205 UNBOUNDLOCAL_ERROR_MSG,
1206 PyTuple_GetItem(co->co_varnames, oparg));
1207 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001209 Py_INCREF(value);
1210 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001212 }
1213
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001214 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001215 TARGET(LOAD_CONST) {
1216 PyObject *value = GETITEM(consts, oparg);
1217 Py_INCREF(value);
1218 PUSH(value);
1219 FAST_DISPATCH();
1220 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001221
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001222 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001223 TARGET(STORE_FAST) {
1224 PyObject *value = POP();
1225 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001227 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001228
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001229 TARGET(POP_TOP) {
1230 PyObject *value = POP();
1231 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001233 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001234
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001235 TARGET(ROT_TWO) {
1236 PyObject *top = TOP();
1237 PyObject *second = SECOND();
1238 SET_TOP(second);
1239 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001241 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001242
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001243 TARGET(ROT_THREE) {
1244 PyObject *top = TOP();
1245 PyObject *second = SECOND();
1246 PyObject *third = THIRD();
1247 SET_TOP(second);
1248 SET_SECOND(third);
1249 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001251 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001252
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001253 TARGET(DUP_TOP) {
1254 PyObject *top = TOP();
1255 Py_INCREF(top);
1256 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001258 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001259
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001260 TARGET(DUP_TOP_TWO) {
1261 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001262 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001263 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001264 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001265 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001266 SET_TOP(top);
1267 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001268 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001269 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001270
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001271 TARGET(UNARY_POSITIVE) {
1272 PyObject *value = TOP();
1273 PyObject *res = PyNumber_Positive(value);
1274 Py_DECREF(value);
1275 SET_TOP(res);
1276 if (res == NULL)
1277 goto error;
1278 DISPATCH();
1279 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001280
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001281 TARGET(UNARY_NEGATIVE) {
1282 PyObject *value = TOP();
1283 PyObject *res = PyNumber_Negative(value);
1284 Py_DECREF(value);
1285 SET_TOP(res);
1286 if (res == NULL)
1287 goto error;
1288 DISPATCH();
1289 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001290
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001291 TARGET(UNARY_NOT) {
1292 PyObject *value = TOP();
1293 int err = PyObject_IsTrue(value);
1294 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 if (err == 0) {
1296 Py_INCREF(Py_True);
1297 SET_TOP(Py_True);
1298 DISPATCH();
1299 }
1300 else if (err > 0) {
1301 Py_INCREF(Py_False);
1302 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 DISPATCH();
1304 }
1305 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001306 goto error;
1307 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001308
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001309 TARGET(UNARY_INVERT) {
1310 PyObject *value = TOP();
1311 PyObject *res = PyNumber_Invert(value);
1312 Py_DECREF(value);
1313 SET_TOP(res);
1314 if (res == NULL)
1315 goto error;
1316 DISPATCH();
1317 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001318
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001319 TARGET(BINARY_POWER) {
1320 PyObject *exp = POP();
1321 PyObject *base = TOP();
1322 PyObject *res = PyNumber_Power(base, exp, Py_None);
1323 Py_DECREF(base);
1324 Py_DECREF(exp);
1325 SET_TOP(res);
1326 if (res == NULL)
1327 goto error;
1328 DISPATCH();
1329 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001330
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001331 TARGET(BINARY_MULTIPLY) {
1332 PyObject *right = POP();
1333 PyObject *left = TOP();
1334 PyObject *res = PyNumber_Multiply(left, right);
1335 Py_DECREF(left);
1336 Py_DECREF(right);
1337 SET_TOP(res);
1338 if (res == NULL)
1339 goto error;
1340 DISPATCH();
1341 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001342
Benjamin Petersond51374e2014-04-09 23:55:56 -04001343 TARGET(BINARY_MATRIX_MULTIPLY) {
1344 PyObject *right = POP();
1345 PyObject *left = TOP();
1346 PyObject *res = PyNumber_MatrixMultiply(left, right);
1347 Py_DECREF(left);
1348 Py_DECREF(right);
1349 SET_TOP(res);
1350 if (res == NULL)
1351 goto error;
1352 DISPATCH();
1353 }
1354
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001355 TARGET(BINARY_TRUE_DIVIDE) {
1356 PyObject *divisor = POP();
1357 PyObject *dividend = TOP();
1358 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1359 Py_DECREF(dividend);
1360 Py_DECREF(divisor);
1361 SET_TOP(quotient);
1362 if (quotient == NULL)
1363 goto error;
1364 DISPATCH();
1365 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001366
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001367 TARGET(BINARY_FLOOR_DIVIDE) {
1368 PyObject *divisor = POP();
1369 PyObject *dividend = TOP();
1370 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1371 Py_DECREF(dividend);
1372 Py_DECREF(divisor);
1373 SET_TOP(quotient);
1374 if (quotient == NULL)
1375 goto error;
1376 DISPATCH();
1377 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001378
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001379 TARGET(BINARY_MODULO) {
1380 PyObject *divisor = POP();
1381 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001382 PyObject *res;
1383 if (PyUnicode_CheckExact(dividend) && (
1384 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1385 // fast path; string formatting, but not if the RHS is a str subclass
1386 // (see issue28598)
1387 res = PyUnicode_Format(dividend, divisor);
1388 } else {
1389 res = PyNumber_Remainder(dividend, divisor);
1390 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001391 Py_DECREF(divisor);
1392 Py_DECREF(dividend);
1393 SET_TOP(res);
1394 if (res == NULL)
1395 goto error;
1396 DISPATCH();
1397 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001398
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001399 TARGET(BINARY_ADD) {
1400 PyObject *right = POP();
1401 PyObject *left = TOP();
1402 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001403 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1404 CPython using bytecode, it is simply worthless.
1405 See http://bugs.python.org/issue21955 and
1406 http://bugs.python.org/issue10044 for the discussion. In short,
1407 no patch shown any impact on a realistic benchmark, only a minor
1408 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001409 if (PyUnicode_CheckExact(left) &&
1410 PyUnicode_CheckExact(right)) {
1411 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001412 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001413 }
1414 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001415 sum = PyNumber_Add(left, right);
1416 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001417 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001418 Py_DECREF(right);
1419 SET_TOP(sum);
1420 if (sum == NULL)
1421 goto error;
1422 DISPATCH();
1423 }
1424
1425 TARGET(BINARY_SUBTRACT) {
1426 PyObject *right = POP();
1427 PyObject *left = TOP();
1428 PyObject *diff = PyNumber_Subtract(left, right);
1429 Py_DECREF(right);
1430 Py_DECREF(left);
1431 SET_TOP(diff);
1432 if (diff == NULL)
1433 goto error;
1434 DISPATCH();
1435 }
1436
1437 TARGET(BINARY_SUBSCR) {
1438 PyObject *sub = POP();
1439 PyObject *container = TOP();
1440 PyObject *res = PyObject_GetItem(container, sub);
1441 Py_DECREF(container);
1442 Py_DECREF(sub);
1443 SET_TOP(res);
1444 if (res == NULL)
1445 goto error;
1446 DISPATCH();
1447 }
1448
1449 TARGET(BINARY_LSHIFT) {
1450 PyObject *right = POP();
1451 PyObject *left = TOP();
1452 PyObject *res = PyNumber_Lshift(left, right);
1453 Py_DECREF(left);
1454 Py_DECREF(right);
1455 SET_TOP(res);
1456 if (res == NULL)
1457 goto error;
1458 DISPATCH();
1459 }
1460
1461 TARGET(BINARY_RSHIFT) {
1462 PyObject *right = POP();
1463 PyObject *left = TOP();
1464 PyObject *res = PyNumber_Rshift(left, right);
1465 Py_DECREF(left);
1466 Py_DECREF(right);
1467 SET_TOP(res);
1468 if (res == NULL)
1469 goto error;
1470 DISPATCH();
1471 }
1472
1473 TARGET(BINARY_AND) {
1474 PyObject *right = POP();
1475 PyObject *left = TOP();
1476 PyObject *res = PyNumber_And(left, right);
1477 Py_DECREF(left);
1478 Py_DECREF(right);
1479 SET_TOP(res);
1480 if (res == NULL)
1481 goto error;
1482 DISPATCH();
1483 }
1484
1485 TARGET(BINARY_XOR) {
1486 PyObject *right = POP();
1487 PyObject *left = TOP();
1488 PyObject *res = PyNumber_Xor(left, right);
1489 Py_DECREF(left);
1490 Py_DECREF(right);
1491 SET_TOP(res);
1492 if (res == NULL)
1493 goto error;
1494 DISPATCH();
1495 }
1496
1497 TARGET(BINARY_OR) {
1498 PyObject *right = POP();
1499 PyObject *left = TOP();
1500 PyObject *res = PyNumber_Or(left, right);
1501 Py_DECREF(left);
1502 Py_DECREF(right);
1503 SET_TOP(res);
1504 if (res == NULL)
1505 goto error;
1506 DISPATCH();
1507 }
1508
1509 TARGET(LIST_APPEND) {
1510 PyObject *v = POP();
1511 PyObject *list = PEEK(oparg);
1512 int err;
1513 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001515 if (err != 0)
1516 goto error;
1517 PREDICT(JUMP_ABSOLUTE);
1518 DISPATCH();
1519 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001520
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001521 TARGET(SET_ADD) {
1522 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001523 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001524 int err;
1525 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001527 if (err != 0)
1528 goto error;
1529 PREDICT(JUMP_ABSOLUTE);
1530 DISPATCH();
1531 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001532
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001533 TARGET(INPLACE_POWER) {
1534 PyObject *exp = POP();
1535 PyObject *base = TOP();
1536 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1537 Py_DECREF(base);
1538 Py_DECREF(exp);
1539 SET_TOP(res);
1540 if (res == NULL)
1541 goto error;
1542 DISPATCH();
1543 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001544
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001545 TARGET(INPLACE_MULTIPLY) {
1546 PyObject *right = POP();
1547 PyObject *left = TOP();
1548 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1549 Py_DECREF(left);
1550 Py_DECREF(right);
1551 SET_TOP(res);
1552 if (res == NULL)
1553 goto error;
1554 DISPATCH();
1555 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001556
Benjamin Petersond51374e2014-04-09 23:55:56 -04001557 TARGET(INPLACE_MATRIX_MULTIPLY) {
1558 PyObject *right = POP();
1559 PyObject *left = TOP();
1560 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1561 Py_DECREF(left);
1562 Py_DECREF(right);
1563 SET_TOP(res);
1564 if (res == NULL)
1565 goto error;
1566 DISPATCH();
1567 }
1568
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001569 TARGET(INPLACE_TRUE_DIVIDE) {
1570 PyObject *divisor = POP();
1571 PyObject *dividend = TOP();
1572 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1573 Py_DECREF(dividend);
1574 Py_DECREF(divisor);
1575 SET_TOP(quotient);
1576 if (quotient == NULL)
1577 goto error;
1578 DISPATCH();
1579 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001580
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001581 TARGET(INPLACE_FLOOR_DIVIDE) {
1582 PyObject *divisor = POP();
1583 PyObject *dividend = TOP();
1584 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1585 Py_DECREF(dividend);
1586 Py_DECREF(divisor);
1587 SET_TOP(quotient);
1588 if (quotient == NULL)
1589 goto error;
1590 DISPATCH();
1591 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001592
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001593 TARGET(INPLACE_MODULO) {
1594 PyObject *right = POP();
1595 PyObject *left = TOP();
1596 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1597 Py_DECREF(left);
1598 Py_DECREF(right);
1599 SET_TOP(mod);
1600 if (mod == NULL)
1601 goto error;
1602 DISPATCH();
1603 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001604
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001605 TARGET(INPLACE_ADD) {
1606 PyObject *right = POP();
1607 PyObject *left = TOP();
1608 PyObject *sum;
1609 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1610 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001611 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001612 }
1613 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001614 sum = PyNumber_InPlaceAdd(left, right);
1615 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001616 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001617 Py_DECREF(right);
1618 SET_TOP(sum);
1619 if (sum == NULL)
1620 goto error;
1621 DISPATCH();
1622 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001623
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001624 TARGET(INPLACE_SUBTRACT) {
1625 PyObject *right = POP();
1626 PyObject *left = TOP();
1627 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1628 Py_DECREF(left);
1629 Py_DECREF(right);
1630 SET_TOP(diff);
1631 if (diff == NULL)
1632 goto error;
1633 DISPATCH();
1634 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001635
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001636 TARGET(INPLACE_LSHIFT) {
1637 PyObject *right = POP();
1638 PyObject *left = TOP();
1639 PyObject *res = PyNumber_InPlaceLshift(left, right);
1640 Py_DECREF(left);
1641 Py_DECREF(right);
1642 SET_TOP(res);
1643 if (res == NULL)
1644 goto error;
1645 DISPATCH();
1646 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001647
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001648 TARGET(INPLACE_RSHIFT) {
1649 PyObject *right = POP();
1650 PyObject *left = TOP();
1651 PyObject *res = PyNumber_InPlaceRshift(left, right);
1652 Py_DECREF(left);
1653 Py_DECREF(right);
1654 SET_TOP(res);
1655 if (res == NULL)
1656 goto error;
1657 DISPATCH();
1658 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001659
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001660 TARGET(INPLACE_AND) {
1661 PyObject *right = POP();
1662 PyObject *left = TOP();
1663 PyObject *res = PyNumber_InPlaceAnd(left, right);
1664 Py_DECREF(left);
1665 Py_DECREF(right);
1666 SET_TOP(res);
1667 if (res == NULL)
1668 goto error;
1669 DISPATCH();
1670 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001671
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001672 TARGET(INPLACE_XOR) {
1673 PyObject *right = POP();
1674 PyObject *left = TOP();
1675 PyObject *res = PyNumber_InPlaceXor(left, right);
1676 Py_DECREF(left);
1677 Py_DECREF(right);
1678 SET_TOP(res);
1679 if (res == NULL)
1680 goto error;
1681 DISPATCH();
1682 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001683
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001684 TARGET(INPLACE_OR) {
1685 PyObject *right = POP();
1686 PyObject *left = TOP();
1687 PyObject *res = PyNumber_InPlaceOr(left, right);
1688 Py_DECREF(left);
1689 Py_DECREF(right);
1690 SET_TOP(res);
1691 if (res == NULL)
1692 goto error;
1693 DISPATCH();
1694 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001695
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001696 TARGET(STORE_SUBSCR) {
1697 PyObject *sub = TOP();
1698 PyObject *container = SECOND();
1699 PyObject *v = THIRD();
1700 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 STACKADJ(-3);
Martin Panter95f53c12016-07-18 08:23:26 +00001702 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001703 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001705 Py_DECREF(container);
1706 Py_DECREF(sub);
1707 if (err != 0)
1708 goto error;
1709 DISPATCH();
1710 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001711
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001712 TARGET(STORE_ANNOTATION) {
1713 _Py_IDENTIFIER(__annotations__);
1714 PyObject *ann_dict;
1715 PyObject *ann = POP();
1716 PyObject *name = GETITEM(names, oparg);
1717 int err;
1718 if (f->f_locals == NULL) {
1719 PyErr_Format(PyExc_SystemError,
1720 "no locals found when storing annotation");
1721 Py_DECREF(ann);
1722 goto error;
1723 }
1724 /* first try to get __annotations__ from locals... */
1725 if (PyDict_CheckExact(f->f_locals)) {
1726 ann_dict = _PyDict_GetItemId(f->f_locals,
1727 &PyId___annotations__);
1728 if (ann_dict == NULL) {
1729 PyErr_SetString(PyExc_NameError,
1730 "__annotations__ not found");
1731 Py_DECREF(ann);
1732 goto error;
1733 }
1734 Py_INCREF(ann_dict);
1735 }
1736 else {
1737 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
1738 if (ann_str == NULL) {
1739 Py_DECREF(ann);
1740 goto error;
1741 }
1742 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
1743 if (ann_dict == NULL) {
1744 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
1745 PyErr_SetString(PyExc_NameError,
1746 "__annotations__ not found");
1747 }
1748 Py_DECREF(ann);
1749 goto error;
1750 }
1751 }
1752 /* ...if succeeded, __annotations__[name] = ann */
1753 if (PyDict_CheckExact(ann_dict)) {
1754 err = PyDict_SetItem(ann_dict, name, ann);
1755 }
1756 else {
1757 err = PyObject_SetItem(ann_dict, name, ann);
1758 }
1759 Py_DECREF(ann_dict);
Yury Selivanov50c584f2016-09-08 23:38:21 -07001760 Py_DECREF(ann);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001761 if (err != 0) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001762 goto error;
1763 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001764 DISPATCH();
1765 }
1766
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001767 TARGET(DELETE_SUBSCR) {
1768 PyObject *sub = TOP();
1769 PyObject *container = SECOND();
1770 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 STACKADJ(-2);
Martin Panter95f53c12016-07-18 08:23:26 +00001772 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001773 err = PyObject_DelItem(container, sub);
1774 Py_DECREF(container);
1775 Py_DECREF(sub);
1776 if (err != 0)
1777 goto error;
1778 DISPATCH();
1779 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001780
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001781 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001782 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001783 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001784 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001785 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001786 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 PyErr_SetString(PyExc_RuntimeError,
1788 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001789 Py_DECREF(value);
1790 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001792 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001793 Py_DECREF(value);
1794 if (res == NULL)
1795 goto error;
1796 Py_DECREF(res);
1797 DISPATCH();
1798 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001799
Thomas Wouters434d0822000-08-24 20:11:32 +00001800#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001802#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001803 TARGET(RAISE_VARARGS) {
1804 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 switch (oparg) {
1806 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001807 cause = POP(); /* cause */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001809 exc = POP(); /* exc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001810 case 0: /* Fallthrough */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001811 if (do_raise(exc, cause)) {
1812 why = WHY_EXCEPTION;
1813 goto fast_block_end;
1814 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815 break;
1816 default:
1817 PyErr_SetString(PyExc_SystemError,
1818 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 break;
1820 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001821 goto error;
1822 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001823
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001824 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 retval = POP();
1826 why = WHY_RETURN;
1827 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001828 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001829
Yury Selivanov75445082015-05-11 22:57:16 -04001830 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001831 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001832 PyObject *iter = NULL;
1833 PyObject *awaitable = NULL;
1834 PyObject *obj = TOP();
1835 PyTypeObject *type = Py_TYPE(obj);
1836
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001837 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001838 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001839 }
Yury Selivanov75445082015-05-11 22:57:16 -04001840
1841 if (getter != NULL) {
1842 iter = (*getter)(obj);
1843 Py_DECREF(obj);
1844 if (iter == NULL) {
1845 SET_TOP(NULL);
1846 goto error;
1847 }
1848 }
1849 else {
1850 SET_TOP(NULL);
1851 PyErr_Format(
1852 PyExc_TypeError,
1853 "'async for' requires an object with "
1854 "__aiter__ method, got %.100s",
1855 type->tp_name);
1856 Py_DECREF(obj);
1857 goto error;
1858 }
1859
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001860 if (Py_TYPE(iter)->tp_as_async != NULL &&
1861 Py_TYPE(iter)->tp_as_async->am_anext != NULL) {
1862
1863 /* Starting with CPython 3.5.2 __aiter__ should return
1864 asynchronous iterators directly (not awaitables that
1865 resolve to asynchronous iterators.)
1866
1867 Therefore, we check if the object that was returned
1868 from __aiter__ has an __anext__ method. If it does,
1869 we wrap it in an awaitable that resolves to `iter`.
1870
1871 See http://bugs.python.org/issue27243 for more
1872 details.
1873 */
1874
1875 PyObject *wrapper = _PyAIterWrapper_New(iter);
1876 Py_DECREF(iter);
1877 SET_TOP(wrapper);
1878 DISPATCH();
1879 }
1880
Yury Selivanov5376ba92015-06-22 12:19:30 -04001881 awaitable = _PyCoro_GetAwaitableIter(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001882 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001883 _PyErr_FormatFromCause(
Yury Selivanov75445082015-05-11 22:57:16 -04001884 PyExc_TypeError,
1885 "'async for' received an invalid object "
1886 "from __aiter__: %.100s",
1887 Py_TYPE(iter)->tp_name);
1888
Yury Selivanov398ff912017-03-02 22:20:00 -05001889 SET_TOP(NULL);
Yury Selivanov75445082015-05-11 22:57:16 -04001890 Py_DECREF(iter);
1891 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001892 } else {
Yury Selivanov75445082015-05-11 22:57:16 -04001893 Py_DECREF(iter);
1894
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001895 if (PyErr_WarnFormat(
Yury Selivanov2edd8a12016-11-08 15:13:07 -05001896 PyExc_DeprecationWarning, 1,
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001897 "'%.100s' implements legacy __aiter__ protocol; "
1898 "__aiter__ should return an asynchronous "
1899 "iterator, not awaitable",
1900 type->tp_name))
1901 {
1902 /* Warning was converted to an error. */
1903 Py_DECREF(awaitable);
1904 SET_TOP(NULL);
1905 goto error;
1906 }
1907 }
1908
Yury Selivanov75445082015-05-11 22:57:16 -04001909 SET_TOP(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001910 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001911 DISPATCH();
1912 }
1913
1914 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001915 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001916 PyObject *next_iter = NULL;
1917 PyObject *awaitable = NULL;
1918 PyObject *aiter = TOP();
1919 PyTypeObject *type = Py_TYPE(aiter);
1920
Yury Selivanoveb636452016-09-08 22:01:51 -07001921 if (PyAsyncGen_CheckExact(aiter)) {
1922 awaitable = type->tp_as_async->am_anext(aiter);
1923 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001924 goto error;
1925 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001926 } else {
1927 if (type->tp_as_async != NULL){
1928 getter = type->tp_as_async->am_anext;
1929 }
Yury Selivanov75445082015-05-11 22:57:16 -04001930
Yury Selivanoveb636452016-09-08 22:01:51 -07001931 if (getter != NULL) {
1932 next_iter = (*getter)(aiter);
1933 if (next_iter == NULL) {
1934 goto error;
1935 }
1936 }
1937 else {
1938 PyErr_Format(
1939 PyExc_TypeError,
1940 "'async for' requires an iterator with "
1941 "__anext__ method, got %.100s",
1942 type->tp_name);
1943 goto error;
1944 }
Yury Selivanov75445082015-05-11 22:57:16 -04001945
Yury Selivanoveb636452016-09-08 22:01:51 -07001946 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1947 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001948 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001949 PyExc_TypeError,
1950 "'async for' received an invalid object "
1951 "from __anext__: %.100s",
1952 Py_TYPE(next_iter)->tp_name);
1953
1954 Py_DECREF(next_iter);
1955 goto error;
1956 } else {
1957 Py_DECREF(next_iter);
1958 }
1959 }
Yury Selivanov75445082015-05-11 22:57:16 -04001960
1961 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001962 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001963 DISPATCH();
1964 }
1965
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001966 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001967 TARGET(GET_AWAITABLE) {
1968 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001969 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001970
1971 Py_DECREF(iterable);
1972
Yury Selivanovc724bae2016-03-02 11:30:46 -05001973 if (iter != NULL && PyCoro_CheckExact(iter)) {
1974 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1975 if (yf != NULL) {
1976 /* `iter` is a coroutine object that is being
1977 awaited, `yf` is a pointer to the current awaitable
1978 being awaited on. */
1979 Py_DECREF(yf);
1980 Py_CLEAR(iter);
1981 PyErr_SetString(
1982 PyExc_RuntimeError,
1983 "coroutine is being awaited already");
1984 /* The code below jumps to `error` if `iter` is NULL. */
1985 }
1986 }
1987
Yury Selivanov75445082015-05-11 22:57:16 -04001988 SET_TOP(iter); /* Even if it's NULL */
1989
1990 if (iter == NULL) {
1991 goto error;
1992 }
1993
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001994 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001995 DISPATCH();
1996 }
1997
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001998 TARGET(YIELD_FROM) {
1999 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002000 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002001 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002002 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2003 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002004 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002005 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002006 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002007 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002008 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002009 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002010 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002011 Py_DECREF(v);
2012 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002013 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002014 if (tstate->c_tracefunc != NULL
2015 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002016 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002017 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002018 if (err < 0)
2019 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002020 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002021 SET_TOP(val);
2022 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002023 }
Martin Panter95f53c12016-07-18 08:23:26 +00002024 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002025 f->f_stacktop = stack_pointer;
2026 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002027 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002028 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002029 f->f_lasti -= sizeof(_Py_CODEUNIT);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002030 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002031 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002032
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002033 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002034 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002035
2036 if (co->co_flags & CO_ASYNC_GENERATOR) {
2037 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2038 Py_DECREF(retval);
2039 if (w == NULL) {
2040 retval = NULL;
2041 goto error;
2042 }
2043 retval = w;
2044 }
2045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002046 f->f_stacktop = stack_pointer;
2047 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002049 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002050
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002051 TARGET(POP_EXCEPT) {
2052 PyTryBlock *b = PyFrame_BlockPop(f);
2053 if (b->b_type != EXCEPT_HANDLER) {
2054 PyErr_SetString(PyExc_SystemError,
2055 "popped block is not an except handler");
2056 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002058 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002060 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002061
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002062 PREDICTED(POP_BLOCK);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002063 TARGET(POP_BLOCK) {
2064 PyTryBlock *b = PyFrame_BlockPop(f);
2065 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002066 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002067 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002070 TARGET(END_FINALLY) {
2071 PyObject *status = POP();
2072 if (PyLong_Check(status)) {
2073 why = (enum why_code) PyLong_AS_LONG(status);
2074 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075 if (why == WHY_RETURN ||
2076 why == WHY_CONTINUE)
2077 retval = POP();
2078 if (why == WHY_SILENCED) {
2079 /* An exception was silenced by 'with', we must
2080 manually unwind the EXCEPT_HANDLER block which was
2081 created when the exception was caught, otherwise
2082 the stack will be in an inconsistent state. */
2083 PyTryBlock *b = PyFrame_BlockPop(f);
2084 assert(b->b_type == EXCEPT_HANDLER);
2085 UNWIND_EXCEPT_HANDLER(b);
2086 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002087 Py_DECREF(status);
2088 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002090 Py_DECREF(status);
2091 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002092 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002093 else if (PyExceptionClass_Check(status)) {
2094 PyObject *exc = POP();
2095 PyObject *tb = POP();
2096 PyErr_Restore(status, exc, tb);
2097 why = WHY_EXCEPTION;
2098 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002099 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002100 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 PyErr_SetString(PyExc_SystemError,
2102 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002103 Py_DECREF(status);
2104 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002106 Py_DECREF(status);
2107 DISPATCH();
2108 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002109
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002110 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002111 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002112
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002113 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002114 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002115 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
2116 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002117 PyErr_SetString(PyExc_NameError,
2118 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002119 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002120 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002121 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002122 }
2123 else {
2124 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2125 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002126 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002127 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2128 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002129 if (PyErr_ExceptionMatches(PyExc_KeyError))
2130 PyErr_SetString(PyExc_NameError,
2131 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002132 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002133 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002134 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002135 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002136 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002137 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002138
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002139 TARGET(STORE_NAME) {
2140 PyObject *name = GETITEM(names, oparg);
2141 PyObject *v = POP();
2142 PyObject *ns = f->f_locals;
2143 int err;
2144 if (ns == NULL) {
2145 PyErr_Format(PyExc_SystemError,
2146 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002148 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002149 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002150 if (PyDict_CheckExact(ns))
2151 err = PyDict_SetItem(ns, name, v);
2152 else
2153 err = PyObject_SetItem(ns, name, v);
2154 Py_DECREF(v);
2155 if (err != 0)
2156 goto error;
2157 DISPATCH();
2158 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002159
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002160 TARGET(DELETE_NAME) {
2161 PyObject *name = GETITEM(names, oparg);
2162 PyObject *ns = f->f_locals;
2163 int err;
2164 if (ns == NULL) {
2165 PyErr_Format(PyExc_SystemError,
2166 "no locals when deleting %R", name);
2167 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002168 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002169 err = PyObject_DelItem(ns, name);
2170 if (err != 0) {
2171 format_exc_check_arg(PyExc_NameError,
2172 NAME_ERROR_MSG,
2173 name);
2174 goto error;
2175 }
2176 DISPATCH();
2177 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002178
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002179 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002180 TARGET(UNPACK_SEQUENCE) {
2181 PyObject *seq = POP(), *item, **items;
2182 if (PyTuple_CheckExact(seq) &&
2183 PyTuple_GET_SIZE(seq) == oparg) {
2184 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002186 item = items[oparg];
2187 Py_INCREF(item);
2188 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002189 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002190 } else if (PyList_CheckExact(seq) &&
2191 PyList_GET_SIZE(seq) == oparg) {
2192 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002193 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002194 item = items[oparg];
2195 Py_INCREF(item);
2196 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002198 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199 stack_pointer + oparg)) {
2200 STACKADJ(oparg);
2201 } else {
2202 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002203 Py_DECREF(seq);
2204 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002205 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002206 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002207 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002209
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002210 TARGET(UNPACK_EX) {
2211 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2212 PyObject *seq = POP();
2213
2214 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2215 stack_pointer + totalargs)) {
2216 stack_pointer += totalargs;
2217 } else {
2218 Py_DECREF(seq);
2219 goto error;
2220 }
2221 Py_DECREF(seq);
2222 DISPATCH();
2223 }
2224
2225 TARGET(STORE_ATTR) {
2226 PyObject *name = GETITEM(names, oparg);
2227 PyObject *owner = TOP();
2228 PyObject *v = SECOND();
2229 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002231 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002233 Py_DECREF(owner);
2234 if (err != 0)
2235 goto error;
2236 DISPATCH();
2237 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002238
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002239 TARGET(DELETE_ATTR) {
2240 PyObject *name = GETITEM(names, oparg);
2241 PyObject *owner = POP();
2242 int err;
2243 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2244 Py_DECREF(owner);
2245 if (err != 0)
2246 goto error;
2247 DISPATCH();
2248 }
2249
2250 TARGET(STORE_GLOBAL) {
2251 PyObject *name = GETITEM(names, oparg);
2252 PyObject *v = POP();
2253 int err;
2254 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002255 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002256 if (err != 0)
2257 goto error;
2258 DISPATCH();
2259 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002260
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002261 TARGET(DELETE_GLOBAL) {
2262 PyObject *name = GETITEM(names, oparg);
2263 int err;
2264 err = PyDict_DelItem(f->f_globals, name);
2265 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002266 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002267 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002268 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002269 }
2270 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002271 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002272
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002273 TARGET(LOAD_NAME) {
2274 PyObject *name = GETITEM(names, oparg);
2275 PyObject *locals = f->f_locals;
2276 PyObject *v;
2277 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002279 "no locals when loading %R", name);
2280 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002282 if (PyDict_CheckExact(locals)) {
2283 v = PyDict_GetItem(locals, name);
2284 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002285 }
2286 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002287 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002288 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002289 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2290 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 PyErr_Clear();
2292 }
2293 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002294 if (v == NULL) {
2295 v = PyDict_GetItem(f->f_globals, name);
2296 Py_XINCREF(v);
2297 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002298 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002299 v = PyDict_GetItem(f->f_builtins, name);
2300 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002301 format_exc_check_arg(
2302 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002303 NAME_ERROR_MSG, name);
2304 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002305 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002306 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002307 }
2308 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002309 v = PyObject_GetItem(f->f_builtins, name);
2310 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002311 if (PyErr_ExceptionMatches(PyExc_KeyError))
2312 format_exc_check_arg(
2313 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002314 NAME_ERROR_MSG, name);
2315 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002316 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002317 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002320 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002321 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002322 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002323
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002324 TARGET(LOAD_GLOBAL) {
2325 PyObject *name = GETITEM(names, oparg);
2326 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002327 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002328 && PyDict_CheckExact(f->f_builtins))
2329 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002330 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002331 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002332 name);
2333 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002334 if (!_PyErr_OCCURRED()) {
2335 /* _PyDict_LoadGlobal() returns NULL without raising
2336 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002337 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002338 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002339 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002340 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002341 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002342 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002343 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002344 else {
2345 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002346
2347 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002348 v = PyObject_GetItem(f->f_globals, name);
2349 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002350 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2351 goto error;
2352 PyErr_Clear();
2353
Victor Stinnerb4efc962015-11-20 09:24:02 +01002354 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002355 v = PyObject_GetItem(f->f_builtins, name);
2356 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002357 if (PyErr_ExceptionMatches(PyExc_KeyError))
2358 format_exc_check_arg(
2359 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002360 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002361 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002362 }
2363 }
2364 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002365 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002367 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002368
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002369 TARGET(DELETE_FAST) {
2370 PyObject *v = GETLOCAL(oparg);
2371 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 SETLOCAL(oparg, NULL);
2373 DISPATCH();
2374 }
2375 format_exc_check_arg(
2376 PyExc_UnboundLocalError,
2377 UNBOUNDLOCAL_ERROR_MSG,
2378 PyTuple_GetItem(co->co_varnames, oparg)
2379 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002380 goto error;
2381 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002382
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002383 TARGET(DELETE_DEREF) {
2384 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002385 PyObject *oldobj = PyCell_GET(cell);
2386 if (oldobj != NULL) {
2387 PyCell_SET(cell, NULL);
2388 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002389 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002390 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002391 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002392 goto error;
2393 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002394
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002395 TARGET(LOAD_CLOSURE) {
2396 PyObject *cell = freevars[oparg];
2397 Py_INCREF(cell);
2398 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002399 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002400 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002401
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002402 TARGET(LOAD_CLASSDEREF) {
2403 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002404 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002405 assert(locals);
2406 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2407 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2408 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2409 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2410 if (PyDict_CheckExact(locals)) {
2411 value = PyDict_GetItem(locals, name);
2412 Py_XINCREF(value);
2413 }
2414 else {
2415 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002416 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002417 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2418 goto error;
2419 PyErr_Clear();
2420 }
2421 }
2422 if (!value) {
2423 PyObject *cell = freevars[oparg];
2424 value = PyCell_GET(cell);
2425 if (value == NULL) {
2426 format_exc_unbound(co, oparg);
2427 goto error;
2428 }
2429 Py_INCREF(value);
2430 }
2431 PUSH(value);
2432 DISPATCH();
2433 }
2434
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002435 TARGET(LOAD_DEREF) {
2436 PyObject *cell = freevars[oparg];
2437 PyObject *value = PyCell_GET(cell);
2438 if (value == NULL) {
2439 format_exc_unbound(co, oparg);
2440 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002441 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002442 Py_INCREF(value);
2443 PUSH(value);
2444 DISPATCH();
2445 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002446
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002447 TARGET(STORE_DEREF) {
2448 PyObject *v = POP();
2449 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002450 PyObject *oldobj = PyCell_GET(cell);
2451 PyCell_SET(cell, v);
2452 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002453 DISPATCH();
2454 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002455
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002456 TARGET(BUILD_STRING) {
2457 PyObject *str;
2458 PyObject *empty = PyUnicode_New(0, 0);
2459 if (empty == NULL) {
2460 goto error;
2461 }
2462 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2463 Py_DECREF(empty);
2464 if (str == NULL)
2465 goto error;
2466 while (--oparg >= 0) {
2467 PyObject *item = POP();
2468 Py_DECREF(item);
2469 }
2470 PUSH(str);
2471 DISPATCH();
2472 }
2473
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002474 TARGET(BUILD_TUPLE) {
2475 PyObject *tup = PyTuple_New(oparg);
2476 if (tup == NULL)
2477 goto error;
2478 while (--oparg >= 0) {
2479 PyObject *item = POP();
2480 PyTuple_SET_ITEM(tup, oparg, item);
2481 }
2482 PUSH(tup);
2483 DISPATCH();
2484 }
2485
2486 TARGET(BUILD_LIST) {
2487 PyObject *list = PyList_New(oparg);
2488 if (list == NULL)
2489 goto error;
2490 while (--oparg >= 0) {
2491 PyObject *item = POP();
2492 PyList_SET_ITEM(list, oparg, item);
2493 }
2494 PUSH(list);
2495 DISPATCH();
2496 }
2497
Serhiy Storchaka73442852016-10-02 10:33:46 +03002498 TARGET(BUILD_TUPLE_UNPACK_WITH_CALL)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002499 TARGET(BUILD_TUPLE_UNPACK)
2500 TARGET(BUILD_LIST_UNPACK) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002501 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002502 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002503 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002504 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002505
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002506 if (sum == NULL)
2507 goto error;
2508
2509 for (i = oparg; i > 0; i--) {
2510 PyObject *none_val;
2511
2512 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2513 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002514 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
2515 PyErr_ExceptionMatches(PyExc_TypeError)) {
2516 PyObject *func = PEEK(1 + oparg);
2517 PyErr_Format(PyExc_TypeError,
2518 "%.200s%.200s argument after * "
2519 "must be an iterable, not %.200s",
2520 PyEval_GetFuncName(func),
2521 PyEval_GetFuncDesc(func),
2522 PEEK(i)->ob_type->tp_name);
2523 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002524 Py_DECREF(sum);
2525 goto error;
2526 }
2527 Py_DECREF(none_val);
2528 }
2529
2530 if (convert_to_tuple) {
2531 return_value = PyList_AsTuple(sum);
2532 Py_DECREF(sum);
2533 if (return_value == NULL)
2534 goto error;
2535 }
2536 else {
2537 return_value = sum;
2538 }
2539
2540 while (oparg--)
2541 Py_DECREF(POP());
2542 PUSH(return_value);
2543 DISPATCH();
2544 }
2545
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002546 TARGET(BUILD_SET) {
2547 PyObject *set = PySet_New(NULL);
2548 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002549 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002550 if (set == NULL)
2551 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002552 for (i = oparg; i > 0; i--) {
2553 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002554 if (err == 0)
2555 err = PySet_Add(set, item);
2556 Py_DECREF(item);
2557 }
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002558 STACKADJ(-oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002559 if (err != 0) {
2560 Py_DECREF(set);
2561 goto error;
2562 }
2563 PUSH(set);
2564 DISPATCH();
2565 }
2566
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002567 TARGET(BUILD_SET_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002568 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002569 PyObject *sum = PySet_New(NULL);
2570 if (sum == NULL)
2571 goto error;
2572
2573 for (i = oparg; i > 0; i--) {
2574 if (_PySet_Update(sum, PEEK(i)) < 0) {
2575 Py_DECREF(sum);
2576 goto error;
2577 }
2578 }
2579
2580 while (oparg--)
2581 Py_DECREF(POP());
2582 PUSH(sum);
2583 DISPATCH();
2584 }
2585
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002586 TARGET(BUILD_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002587 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002588 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2589 if (map == NULL)
2590 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002591 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002592 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002593 PyObject *key = PEEK(2*i);
2594 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002595 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002596 if (err != 0) {
2597 Py_DECREF(map);
2598 goto error;
2599 }
2600 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002601
2602 while (oparg--) {
2603 Py_DECREF(POP());
2604 Py_DECREF(POP());
2605 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002606 PUSH(map);
2607 DISPATCH();
2608 }
2609
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002610 TARGET(SETUP_ANNOTATIONS) {
2611 _Py_IDENTIFIER(__annotations__);
2612 int err;
2613 PyObject *ann_dict;
2614 if (f->f_locals == NULL) {
2615 PyErr_Format(PyExc_SystemError,
2616 "no locals found when setting up annotations");
2617 goto error;
2618 }
2619 /* check if __annotations__ in locals()... */
2620 if (PyDict_CheckExact(f->f_locals)) {
2621 ann_dict = _PyDict_GetItemId(f->f_locals,
2622 &PyId___annotations__);
2623 if (ann_dict == NULL) {
2624 /* ...if not, create a new one */
2625 ann_dict = PyDict_New();
2626 if (ann_dict == NULL) {
2627 goto error;
2628 }
2629 err = _PyDict_SetItemId(f->f_locals,
2630 &PyId___annotations__, ann_dict);
2631 Py_DECREF(ann_dict);
2632 if (err != 0) {
2633 goto error;
2634 }
2635 }
2636 }
2637 else {
2638 /* do the same if locals() is not a dict */
2639 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2640 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002641 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002642 }
2643 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2644 if (ann_dict == NULL) {
2645 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2646 goto error;
2647 }
2648 PyErr_Clear();
2649 ann_dict = PyDict_New();
2650 if (ann_dict == NULL) {
2651 goto error;
2652 }
2653 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2654 Py_DECREF(ann_dict);
2655 if (err != 0) {
2656 goto error;
2657 }
2658 }
2659 else {
2660 Py_DECREF(ann_dict);
2661 }
2662 }
2663 DISPATCH();
2664 }
2665
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002666 TARGET(BUILD_CONST_KEY_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002667 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002668 PyObject *map;
2669 PyObject *keys = TOP();
2670 if (!PyTuple_CheckExact(keys) ||
2671 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2672 PyErr_SetString(PyExc_SystemError,
2673 "bad BUILD_CONST_KEY_MAP keys argument");
2674 goto error;
2675 }
2676 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2677 if (map == NULL) {
2678 goto error;
2679 }
2680 for (i = oparg; i > 0; i--) {
2681 int err;
2682 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2683 PyObject *value = PEEK(i + 1);
2684 err = PyDict_SetItem(map, key, value);
2685 if (err != 0) {
2686 Py_DECREF(map);
2687 goto error;
2688 }
2689 }
2690
2691 Py_DECREF(POP());
2692 while (oparg--) {
2693 Py_DECREF(POP());
2694 }
2695 PUSH(map);
2696 DISPATCH();
2697 }
2698
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002699 TARGET(BUILD_MAP_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002700 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002701 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002702 if (sum == NULL)
2703 goto error;
2704
2705 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002706 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002707 if (PyDict_Update(sum, arg) < 0) {
2708 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2709 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002710 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002711 arg->ob_type->tp_name);
2712 }
2713 Py_DECREF(sum);
2714 goto error;
2715 }
2716 }
2717
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002718 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002719 Py_DECREF(POP());
2720 PUSH(sum);
2721 DISPATCH();
2722 }
2723
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002724 TARGET(BUILD_MAP_UNPACK_WITH_CALL) {
2725 Py_ssize_t i;
2726 PyObject *sum = PyDict_New();
2727 if (sum == NULL)
2728 goto error;
2729
2730 for (i = oparg; i > 0; i--) {
2731 PyObject *arg = PEEK(i);
2732 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
2733 PyObject *func = PEEK(2 + oparg);
2734 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2735 PyErr_Format(PyExc_TypeError,
2736 "%.200s%.200s argument after ** "
2737 "must be a mapping, not %.200s",
2738 PyEval_GetFuncName(func),
2739 PyEval_GetFuncDesc(func),
2740 arg->ob_type->tp_name);
2741 }
2742 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2743 PyObject *exc, *val, *tb;
2744 PyErr_Fetch(&exc, &val, &tb);
2745 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
2746 PyObject *key = PyTuple_GET_ITEM(val, 0);
2747 if (!PyUnicode_Check(key)) {
2748 PyErr_Format(PyExc_TypeError,
2749 "%.200s%.200s keywords must be strings",
2750 PyEval_GetFuncName(func),
2751 PyEval_GetFuncDesc(func));
2752 } else {
2753 PyErr_Format(PyExc_TypeError,
2754 "%.200s%.200s got multiple "
2755 "values for keyword argument '%U'",
2756 PyEval_GetFuncName(func),
2757 PyEval_GetFuncDesc(func),
2758 key);
2759 }
2760 Py_XDECREF(exc);
2761 Py_XDECREF(val);
2762 Py_XDECREF(tb);
2763 }
2764 else {
2765 PyErr_Restore(exc, val, tb);
2766 }
2767 }
2768 Py_DECREF(sum);
2769 goto error;
2770 }
2771 }
2772
2773 while (oparg--)
2774 Py_DECREF(POP());
2775 PUSH(sum);
2776 DISPATCH();
2777 }
2778
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002779 TARGET(MAP_ADD) {
2780 PyObject *key = TOP();
2781 PyObject *value = SECOND();
2782 PyObject *map;
2783 int err;
2784 STACKADJ(-2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002785 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002786 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002787 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002788 Py_DECREF(value);
2789 Py_DECREF(key);
2790 if (err != 0)
2791 goto error;
2792 PREDICT(JUMP_ABSOLUTE);
2793 DISPATCH();
2794 }
2795
2796 TARGET(LOAD_ATTR) {
2797 PyObject *name = GETITEM(names, oparg);
2798 PyObject *owner = TOP();
2799 PyObject *res = PyObject_GetAttr(owner, name);
2800 Py_DECREF(owner);
2801 SET_TOP(res);
2802 if (res == NULL)
2803 goto error;
2804 DISPATCH();
2805 }
2806
2807 TARGET(COMPARE_OP) {
2808 PyObject *right = POP();
2809 PyObject *left = TOP();
2810 PyObject *res = cmp_outcome(oparg, left, right);
2811 Py_DECREF(left);
2812 Py_DECREF(right);
2813 SET_TOP(res);
2814 if (res == NULL)
2815 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002816 PREDICT(POP_JUMP_IF_FALSE);
2817 PREDICT(POP_JUMP_IF_TRUE);
2818 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002819 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002820
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002821 TARGET(IMPORT_NAME) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002822 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002823 PyObject *fromlist = POP();
2824 PyObject *level = TOP();
2825 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002826 res = import_name(f, name, fromlist, level);
2827 Py_DECREF(level);
2828 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002829 SET_TOP(res);
2830 if (res == NULL)
2831 goto error;
2832 DISPATCH();
2833 }
2834
2835 TARGET(IMPORT_STAR) {
2836 PyObject *from = POP(), *locals;
2837 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002838 if (PyFrame_FastToLocalsWithError(f) < 0) {
2839 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002840 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002841 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002842
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002843 locals = f->f_locals;
2844 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002845 PyErr_SetString(PyExc_SystemError,
2846 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002847 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002848 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002849 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002850 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002851 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002852 Py_DECREF(from);
2853 if (err != 0)
2854 goto error;
2855 DISPATCH();
2856 }
Guido van Rossum25831651993-05-19 14:50:45 +00002857
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002858 TARGET(IMPORT_FROM) {
2859 PyObject *name = GETITEM(names, oparg);
2860 PyObject *from = TOP();
2861 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002862 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002863 PUSH(res);
2864 if (res == NULL)
2865 goto error;
2866 DISPATCH();
2867 }
Thomas Wouters52152252000-08-17 22:55:00 +00002868
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002869 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 JUMPBY(oparg);
2871 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002872 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002873
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002874 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002875 TARGET(POP_JUMP_IF_FALSE) {
2876 PyObject *cond = POP();
2877 int err;
2878 if (cond == Py_True) {
2879 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 FAST_DISPATCH();
2881 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002882 if (cond == Py_False) {
2883 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002884 JUMPTO(oparg);
2885 FAST_DISPATCH();
2886 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002887 err = PyObject_IsTrue(cond);
2888 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002889 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002890 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002891 else if (err == 0)
2892 JUMPTO(oparg);
2893 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002894 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002895 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002896 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002897
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002898 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002899 TARGET(POP_JUMP_IF_TRUE) {
2900 PyObject *cond = POP();
2901 int err;
2902 if (cond == Py_False) {
2903 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002904 FAST_DISPATCH();
2905 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002906 if (cond == Py_True) {
2907 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 JUMPTO(oparg);
2909 FAST_DISPATCH();
2910 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002911 err = PyObject_IsTrue(cond);
2912 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002914 JUMPTO(oparg);
2915 }
2916 else if (err == 0)
2917 ;
2918 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002919 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002920 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002921 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002922
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002923 TARGET(JUMP_IF_FALSE_OR_POP) {
2924 PyObject *cond = TOP();
2925 int err;
2926 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002927 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002928 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002929 FAST_DISPATCH();
2930 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002931 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002932 JUMPTO(oparg);
2933 FAST_DISPATCH();
2934 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002935 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 if (err > 0) {
2937 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002938 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002939 }
2940 else if (err == 0)
2941 JUMPTO(oparg);
2942 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002943 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002944 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002945 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002946
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002947 TARGET(JUMP_IF_TRUE_OR_POP) {
2948 PyObject *cond = TOP();
2949 int err;
2950 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002951 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002952 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002953 FAST_DISPATCH();
2954 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002955 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002956 JUMPTO(oparg);
2957 FAST_DISPATCH();
2958 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002959 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002960 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002961 JUMPTO(oparg);
2962 }
2963 else if (err == 0) {
2964 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002965 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002966 }
2967 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002968 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002969 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002970 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002971
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002972 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002973 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002974 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002975#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002976 /* Enabling this path speeds-up all while and for-loops by bypassing
2977 the per-loop checks for signals. By default, this should be turned-off
2978 because it prevents detection of a control-break in tight loops like
2979 "while 1: pass". Compile with this option turned-on when you need
2980 the speed-up and do not need break checking inside tight loops (ones
2981 that contain only instructions ending with FAST_DISPATCH).
2982 */
2983 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002984#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002985 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002986#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002987 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002988
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002989 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002990 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002991 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002992 PyObject *iter = PyObject_GetIter(iterable);
2993 Py_DECREF(iterable);
2994 SET_TOP(iter);
2995 if (iter == NULL)
2996 goto error;
2997 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002998 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002999 DISPATCH();
3000 }
3001
3002 TARGET(GET_YIELD_FROM_ITER) {
3003 /* before: [obj]; after [getiter(obj)] */
3004 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003005 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003006 if (PyCoro_CheckExact(iterable)) {
3007 /* `iterable` is a coroutine */
3008 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3009 /* and it is used in a 'yield from' expression of a
3010 regular generator. */
3011 Py_DECREF(iterable);
3012 SET_TOP(NULL);
3013 PyErr_SetString(PyExc_TypeError,
3014 "cannot 'yield from' a coroutine object "
3015 "in a non-coroutine generator");
3016 goto error;
3017 }
3018 }
3019 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003020 /* `iterable` is not a generator. */
3021 iter = PyObject_GetIter(iterable);
3022 Py_DECREF(iterable);
3023 SET_TOP(iter);
3024 if (iter == NULL)
3025 goto error;
3026 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003027 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003028 DISPATCH();
3029 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003030
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003031 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003032 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003033 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003034 PyObject *iter = TOP();
3035 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3036 if (next != NULL) {
3037 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003038 PREDICT(STORE_FAST);
3039 PREDICT(UNPACK_SEQUENCE);
3040 DISPATCH();
3041 }
3042 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003043 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
3044 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08003045 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003046 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003047 PyErr_Clear();
3048 }
3049 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003050 STACKADJ(-1);
3051 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003052 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003053 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003054 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003055 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003056
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003057 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003058 why = WHY_BREAK;
3059 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003060 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003061
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003062 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003063 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003064 if (retval == NULL)
3065 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003066 why = WHY_CONTINUE;
3067 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003068 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003069
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003070 TARGET(SETUP_LOOP)
3071 TARGET(SETUP_EXCEPT)
3072 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003073 /* NOTE: If you add any new block-setup opcodes that
3074 are not try/except/finally handlers, you may need
3075 to update the PyGen_NeedsFinalizing() function.
3076 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003078 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
3079 STACK_LEVEL());
3080 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003081 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003082
Yury Selivanov75445082015-05-11 22:57:16 -04003083 TARGET(BEFORE_ASYNC_WITH) {
3084 _Py_IDENTIFIER(__aexit__);
3085 _Py_IDENTIFIER(__aenter__);
3086
3087 PyObject *mgr = TOP();
3088 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3089 *enter;
3090 PyObject *res;
3091 if (exit == NULL)
3092 goto error;
3093 SET_TOP(exit);
3094 enter = special_lookup(mgr, &PyId___aenter__);
3095 Py_DECREF(mgr);
3096 if (enter == NULL)
3097 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003098 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003099 Py_DECREF(enter);
3100 if (res == NULL)
3101 goto error;
3102 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003103 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003104 DISPATCH();
3105 }
3106
3107 TARGET(SETUP_ASYNC_WITH) {
3108 PyObject *res = POP();
3109 /* Setup the finally block before pushing the result
3110 of __aenter__ on the stack. */
3111 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3112 STACK_LEVEL());
3113 PUSH(res);
3114 DISPATCH();
3115 }
3116
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003117 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003118 _Py_IDENTIFIER(__exit__);
3119 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003120 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08003121 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003122 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08003123 if (enter == NULL)
3124 goto error;
3125 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003126 if (exit == NULL) {
3127 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003128 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003129 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003130 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003131 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003132 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003133 Py_DECREF(enter);
3134 if (res == NULL)
3135 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136 /* Setup the finally block before pushing the result
3137 of __enter__ on the stack. */
3138 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3139 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003140
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003141 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003142 DISPATCH();
3143 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003144
Yury Selivanov75445082015-05-11 22:57:16 -04003145 TARGET(WITH_CLEANUP_START) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04003146 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003147 how/why we entered the finally clause:
3148 - TOP = None
3149 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
3150 - TOP = WHY_*; no retval below it
3151 - (TOP, SECOND, THIRD) = exc_info()
3152 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
3153 Below them is EXIT, the context.__exit__ bound method.
3154 In the last case, we must call
3155 EXIT(TOP, SECOND, THIRD)
3156 otherwise we must call
3157 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003158
Benjamin Peterson8f169482013-10-29 22:25:06 -04003159 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003160 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04003161 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003162 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 In addition, if the stack represents an exception,
3165 *and* the function call returns a 'true' value, we
3166 push WHY_SILENCED onto the stack. END_FINALLY will
3167 then not re-raise the exception. (But non-local
3168 gotos should still be resumed.)
3169 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003170
Victor Stinner842cfff2016-12-01 14:45:31 +01003171 PyObject* stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003172 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003173 PyObject *exc, *val, *tb, *res;
3174
3175 val = tb = Py_None;
3176 exc = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003177 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003178 (void)POP();
3179 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003180 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003181 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003182 else if (PyLong_Check(exc)) {
3183 STACKADJ(-1);
3184 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003185 case WHY_RETURN:
3186 case WHY_CONTINUE:
3187 /* Retval in TOP. */
3188 exit_func = SECOND();
3189 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003190 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003191 break;
3192 default:
3193 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003194 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003195 break;
3196 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003197 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003198 }
3199 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003200 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003201 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003202 val = SECOND();
3203 tb = THIRD();
3204 tp2 = FOURTH();
3205 exc2 = PEEK(5);
3206 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003207 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003208 SET_VALUE(7, tb2);
3209 SET_VALUE(6, exc2);
3210 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003211 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3212 SET_FOURTH(NULL);
3213 /* We just shifted the stack down, so we have
3214 to tell the except handler block that the
3215 values are lower than it expects. */
3216 block = &f->f_blockstack[f->f_iblock - 1];
3217 assert(block->b_type == EXCEPT_HANDLER);
3218 block->b_level--;
3219 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003220
3221 stack[0] = exc;
3222 stack[1] = val;
3223 stack[2] = tb;
3224 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003226 if (res == NULL)
3227 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003228
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003229 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003230 PUSH(exc);
3231 PUSH(res);
3232 PREDICT(WITH_CLEANUP_FINISH);
3233 DISPATCH();
3234 }
3235
3236 PREDICTED(WITH_CLEANUP_FINISH);
3237 TARGET(WITH_CLEANUP_FINISH) {
3238 PyObject *res = POP();
3239 PyObject *exc = POP();
3240 int err;
3241
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003242 if (exc != Py_None)
3243 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003244 else
3245 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003246
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003247 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003248 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003250 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003251 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003252 else if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003253 /* There was an exception and a True return */
3254 PUSH(PyLong_FromLong((long) WHY_SILENCED));
3255 }
3256 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003257 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003258 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003259
Yury Selivanovf2392132016-12-13 19:03:51 -05003260 TARGET(LOAD_METHOD) {
3261 /* Designed to work in tamdem with CALL_METHOD. */
3262 PyObject *name = GETITEM(names, oparg);
3263 PyObject *obj = TOP();
3264 PyObject *meth = NULL;
3265
3266 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3267
Yury Selivanovf2392132016-12-13 19:03:51 -05003268 if (meth == NULL) {
3269 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003270 goto error;
3271 }
3272
3273 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003274 /* We can bypass temporary bound method object.
3275 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003276
INADA Naoki015bce62017-01-16 17:23:30 +09003277 meth | self | arg1 | ... | argN
3278 */
3279 SET_TOP(meth);
3280 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003281 }
3282 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003283 /* meth is not an unbound method (but a regular attr, or
3284 something was returned by a descriptor protocol). Set
3285 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003286 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003287
3288 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003289 */
INADA Naoki015bce62017-01-16 17:23:30 +09003290 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003291 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003292 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003293 }
3294 DISPATCH();
3295 }
3296
3297 TARGET(CALL_METHOD) {
3298 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003299 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003300
3301 sp = stack_pointer;
3302
INADA Naoki015bce62017-01-16 17:23:30 +09003303 meth = PEEK(oparg + 2);
3304 if (meth == NULL) {
3305 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3306 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003307
3308 Stack layout:
3309
INADA Naoki015bce62017-01-16 17:23:30 +09003310 ... | NULL | callable | arg1 | ... | argN
3311 ^- TOP()
3312 ^- (-oparg)
3313 ^- (-oparg-1)
3314 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003315
INADA Naoki015bce62017-01-16 17:23:30 +09003316 `callable` will be POPed by call_funtion.
3317 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003318 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003319 res = call_function(&sp, oparg, NULL);
3320 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003321 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003322 }
3323 else {
3324 /* This is a method call. Stack layout:
3325
INADA Naoki015bce62017-01-16 17:23:30 +09003326 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003327 ^- TOP()
3328 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003329 ^- (-oparg-1)
3330 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003331
INADA Naoki015bce62017-01-16 17:23:30 +09003332 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003333 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003334 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003335 */
3336 res = call_function(&sp, oparg + 1, NULL);
3337 stack_pointer = sp;
3338 }
3339
3340 PUSH(res);
3341 if (res == NULL)
3342 goto error;
3343 DISPATCH();
3344 }
3345
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003346 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003347 TARGET(CALL_FUNCTION) {
3348 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003349 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003350 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003351 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003352 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003353 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003354 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003355 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003356 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003357 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003358
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003359 TARGET(CALL_FUNCTION_KW) {
3360 PyObject **sp, *res, *names;
3361
3362 names = POP();
3363 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003364 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003365 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003366 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003367 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003368 Py_DECREF(names);
3369
3370 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003371 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003372 }
3373 DISPATCH();
3374 }
3375
3376 TARGET(CALL_FUNCTION_EX) {
3377 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003378 if (oparg & 0x01) {
3379 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003380 if (!PyDict_CheckExact(kwargs)) {
3381 PyObject *d = PyDict_New();
3382 if (d == NULL)
3383 goto error;
3384 if (PyDict_Update(d, kwargs) != 0) {
3385 Py_DECREF(d);
3386 /* PyDict_Update raises attribute
3387 * error (percolated from an attempt
3388 * to get 'keys' attribute) instead of
3389 * a type error if its second argument
3390 * is not a mapping.
3391 */
3392 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3393 func = SECOND();
3394 PyErr_Format(PyExc_TypeError,
3395 "%.200s%.200s argument after ** "
3396 "must be a mapping, not %.200s",
3397 PyEval_GetFuncName(func),
3398 PyEval_GetFuncDesc(func),
3399 kwargs->ob_type->tp_name);
3400 }
Victor Stinnereece2222016-09-12 11:16:37 +02003401 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003402 goto error;
3403 }
3404 Py_DECREF(kwargs);
3405 kwargs = d;
3406 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003407 assert(PyDict_CheckExact(kwargs));
3408 }
3409 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003410 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003411 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003412 if (Py_TYPE(callargs)->tp_iter == NULL &&
3413 !PySequence_Check(callargs)) {
3414 PyErr_Format(PyExc_TypeError,
3415 "%.200s%.200s argument after * "
3416 "must be an iterable, not %.200s",
3417 PyEval_GetFuncName(func),
3418 PyEval_GetFuncDesc(func),
3419 callargs->ob_type->tp_name);
Victor Stinnereece2222016-09-12 11:16:37 +02003420 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003421 goto error;
3422 }
3423 Py_SETREF(callargs, PySequence_Tuple(callargs));
3424 if (callargs == NULL) {
3425 goto error;
3426 }
3427 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003428 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003429
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003430 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003431 Py_DECREF(func);
3432 Py_DECREF(callargs);
3433 Py_XDECREF(kwargs);
3434
3435 SET_TOP(result);
3436 if (result == NULL) {
3437 goto error;
3438 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003439 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003440 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003441
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003442 TARGET(MAKE_FUNCTION) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003443 PyObject *qualname = POP();
3444 PyObject *codeobj = POP();
3445 PyFunctionObject *func = (PyFunctionObject *)
3446 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003447
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003448 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003449 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003450 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003451 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003452 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003453
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003454 if (oparg & 0x08) {
3455 assert(PyTuple_CheckExact(TOP()));
3456 func ->func_closure = POP();
3457 }
3458 if (oparg & 0x04) {
3459 assert(PyDict_CheckExact(TOP()));
3460 func->func_annotations = POP();
3461 }
3462 if (oparg & 0x02) {
3463 assert(PyDict_CheckExact(TOP()));
3464 func->func_kwdefaults = POP();
3465 }
3466 if (oparg & 0x01) {
3467 assert(PyTuple_CheckExact(TOP()));
3468 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003469 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003470
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003471 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003472 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003473 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003474
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003475 TARGET(BUILD_SLICE) {
3476 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003477 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003478 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003479 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003480 step = NULL;
3481 stop = POP();
3482 start = TOP();
3483 slice = PySlice_New(start, stop, step);
3484 Py_DECREF(start);
3485 Py_DECREF(stop);
3486 Py_XDECREF(step);
3487 SET_TOP(slice);
3488 if (slice == NULL)
3489 goto error;
3490 DISPATCH();
3491 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003492
Eric V. Smitha78c7952015-11-03 12:45:05 -05003493 TARGET(FORMAT_VALUE) {
3494 /* Handles f-string value formatting. */
3495 PyObject *result;
3496 PyObject *fmt_spec;
3497 PyObject *value;
3498 PyObject *(*conv_fn)(PyObject *);
3499 int which_conversion = oparg & FVC_MASK;
3500 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3501
3502 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003503 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003504
3505 /* See if any conversion is specified. */
3506 switch (which_conversion) {
3507 case FVC_STR: conv_fn = PyObject_Str; break;
3508 case FVC_REPR: conv_fn = PyObject_Repr; break;
3509 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3510
3511 /* Must be 0 (meaning no conversion), since only four
3512 values are allowed by (oparg & FVC_MASK). */
3513 default: conv_fn = NULL; break;
3514 }
3515
3516 /* If there's a conversion function, call it and replace
3517 value with that result. Otherwise, just use value,
3518 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003519 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003520 result = conv_fn(value);
3521 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003522 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003523 Py_XDECREF(fmt_spec);
3524 goto error;
3525 }
3526 value = result;
3527 }
3528
3529 /* If value is a unicode object, and there's no fmt_spec,
3530 then we know the result of format(value) is value
3531 itself. In that case, skip calling format(). I plan to
3532 move this optimization in to PyObject_Format()
3533 itself. */
3534 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3535 /* Do nothing, just transfer ownership to result. */
3536 result = value;
3537 } else {
3538 /* Actually call format(). */
3539 result = PyObject_Format(value, fmt_spec);
3540 Py_DECREF(value);
3541 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003542 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003543 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003544 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003545 }
3546
Eric V. Smith135d5f42016-02-05 18:23:08 -05003547 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003548 DISPATCH();
3549 }
3550
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003551 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003552 int oldoparg = oparg;
3553 NEXTOPARG();
3554 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003555 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003556 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003557
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003558
Antoine Pitrou042b1282010-08-13 21:15:58 +00003559#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003560 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003561#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003562 default:
3563 fprintf(stderr,
3564 "XXX lineno: %d, opcode: %d\n",
3565 PyFrame_GetLineNumber(f),
3566 opcode);
3567 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003568 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003569
3570#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003571 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003572#endif
3573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003574 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003575
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003576 /* This should never be reached. Every opcode should end with DISPATCH()
3577 or goto error. */
3578 assert(0);
Guido van Rossumac7be682001-01-17 15:42:30 +00003579
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003580error:
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003581
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003582 assert(why == WHY_NOT);
3583 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003584
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003585 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003586#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003587 if (!PyErr_Occurred())
3588 PyErr_SetString(PyExc_SystemError,
3589 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003590#else
3591 assert(PyErr_Occurred());
3592#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003593
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003594 /* Log traceback info. */
3595 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003596
Benjamin Peterson51f46162013-01-23 08:38:47 -05003597 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003598 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3599 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003600
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003601fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003602 assert(why != WHY_NOT);
3603
3604 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003605 while (why != WHY_NOT && f->f_iblock > 0) {
3606 /* Peek at the current block. */
3607 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003609 assert(why != WHY_YIELD);
3610 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3611 why = WHY_NOT;
3612 JUMPTO(PyLong_AS_LONG(retval));
3613 Py_DECREF(retval);
3614 break;
3615 }
3616 /* Now we have to pop the block. */
3617 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003619 if (b->b_type == EXCEPT_HANDLER) {
3620 UNWIND_EXCEPT_HANDLER(b);
3621 continue;
3622 }
3623 UNWIND_BLOCK(b);
3624 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3625 why = WHY_NOT;
3626 JUMPTO(b->b_handler);
3627 break;
3628 }
3629 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3630 || b->b_type == SETUP_FINALLY)) {
3631 PyObject *exc, *val, *tb;
3632 int handler = b->b_handler;
3633 /* Beware, this invalidates all b->b_* fields */
3634 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3635 PUSH(tstate->exc_traceback);
3636 PUSH(tstate->exc_value);
3637 if (tstate->exc_type != NULL) {
3638 PUSH(tstate->exc_type);
3639 }
3640 else {
3641 Py_INCREF(Py_None);
3642 PUSH(Py_None);
3643 }
3644 PyErr_Fetch(&exc, &val, &tb);
3645 /* Make the raw exception data
3646 available to the handler,
3647 so a program can emulate the
3648 Python main loop. */
3649 PyErr_NormalizeException(
3650 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003651 if (tb != NULL)
3652 PyException_SetTraceback(val, tb);
3653 else
3654 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003655 Py_INCREF(exc);
3656 tstate->exc_type = exc;
3657 Py_INCREF(val);
3658 tstate->exc_value = val;
3659 tstate->exc_traceback = tb;
3660 if (tb == NULL)
3661 tb = Py_None;
3662 Py_INCREF(tb);
3663 PUSH(tb);
3664 PUSH(val);
3665 PUSH(exc);
3666 why = WHY_NOT;
3667 JUMPTO(handler);
3668 break;
3669 }
3670 if (b->b_type == SETUP_FINALLY) {
3671 if (why & (WHY_RETURN | WHY_CONTINUE))
3672 PUSH(retval);
3673 PUSH(PyLong_FromLong((long)why));
3674 why = WHY_NOT;
3675 JUMPTO(b->b_handler);
3676 break;
3677 }
3678 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003680 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003682 if (why != WHY_NOT)
3683 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00003684
Victor Stinnerace47d72013-07-18 01:41:08 +02003685 assert(!PyErr_Occurred());
3686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003687 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003689 assert(why != WHY_YIELD);
3690 /* Pop remaining stack entries. */
3691 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003692 PyObject *o = POP();
3693 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003694 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003696 if (why != WHY_RETURN)
3697 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003698
Victor Stinner4a7cc882015-03-06 23:35:27 +01003699 assert((retval != NULL) ^ (PyErr_Occurred() != NULL));
Victor Stinnerace47d72013-07-18 01:41:08 +02003700
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003701fast_yield:
Yury Selivanoveb636452016-09-08 22:01:51 -07003702 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01003703
Benjamin Petersonac913412011-07-03 16:25:11 -05003704 /* The purpose of this block is to put aside the generator's exception
3705 state and restore that of the calling frame. If the current
3706 exception state is from the caller, we clear the exception values
3707 on the generator frame, so they are not swapped back in latter. The
3708 origin of the current exception state is determined by checking for
3709 except handler blocks, which we must be in iff a new exception
3710 state came into existence in this frame. (An uncaught exception
3711 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3712 int i;
Victor Stinner74319ae2016-08-25 00:04:09 +02003713 for (i = 0; i < f->f_iblock; i++) {
3714 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER) {
Benjamin Petersonac913412011-07-03 16:25:11 -05003715 break;
Victor Stinner74319ae2016-08-25 00:04:09 +02003716 }
3717 }
Benjamin Petersonac913412011-07-03 16:25:11 -05003718 if (i == f->f_iblock)
3719 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003720 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003721 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003722 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003723 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003725 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003726 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003727 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003728 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3729 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003730 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003731 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003732 why = WHY_EXCEPTION;
3733 }
3734 }
3735 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003736 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3737 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003738 PyTrace_RETURN, NULL);
3739 }
3740 }
3741 if (tstate->c_profilefunc) {
3742 if (why == WHY_EXCEPTION)
3743 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003744 tstate->c_profileobj,
3745 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003746 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003747 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3748 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003749 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003750 Py_CLEAR(retval);
Brett Cannonb94767f2011-02-22 20:15:44 +00003751 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003752 }
3753 }
3754 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003756 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003757exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003758 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3759 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003760 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003761 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003762 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003763
Victor Stinnerefde1462015-03-21 15:04:43 +01003764 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003765}
3766
Benjamin Petersonb204a422011-06-05 22:04:07 -05003767static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003768format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3769{
3770 int err;
3771 Py_ssize_t len = PyList_GET_SIZE(names);
3772 PyObject *name_str, *comma, *tail, *tmp;
3773
3774 assert(PyList_CheckExact(names));
3775 assert(len >= 1);
3776 /* Deal with the joys of natural language. */
3777 switch (len) {
3778 case 1:
3779 name_str = PyList_GET_ITEM(names, 0);
3780 Py_INCREF(name_str);
3781 break;
3782 case 2:
3783 name_str = PyUnicode_FromFormat("%U and %U",
3784 PyList_GET_ITEM(names, len - 2),
3785 PyList_GET_ITEM(names, len - 1));
3786 break;
3787 default:
3788 tail = PyUnicode_FromFormat(", %U, and %U",
3789 PyList_GET_ITEM(names, len - 2),
3790 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003791 if (tail == NULL)
3792 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003793 /* Chop off the last two objects in the list. This shouldn't actually
3794 fail, but we can't be too careful. */
3795 err = PyList_SetSlice(names, len - 2, len, NULL);
3796 if (err == -1) {
3797 Py_DECREF(tail);
3798 return;
3799 }
3800 /* Stitch everything up into a nice comma-separated list. */
3801 comma = PyUnicode_FromString(", ");
3802 if (comma == NULL) {
3803 Py_DECREF(tail);
3804 return;
3805 }
3806 tmp = PyUnicode_Join(comma, names);
3807 Py_DECREF(comma);
3808 if (tmp == NULL) {
3809 Py_DECREF(tail);
3810 return;
3811 }
3812 name_str = PyUnicode_Concat(tmp, tail);
3813 Py_DECREF(tmp);
3814 Py_DECREF(tail);
3815 break;
3816 }
3817 if (name_str == NULL)
3818 return;
3819 PyErr_Format(PyExc_TypeError,
3820 "%U() missing %i required %s argument%s: %U",
3821 co->co_name,
3822 len,
3823 kind,
3824 len == 1 ? "" : "s",
3825 name_str);
3826 Py_DECREF(name_str);
3827}
3828
3829static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003830missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003831 PyObject **fastlocals)
3832{
Victor Stinner74319ae2016-08-25 00:04:09 +02003833 Py_ssize_t i, j = 0;
3834 Py_ssize_t start, end;
3835 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003836 const char *kind = positional ? "positional" : "keyword-only";
3837 PyObject *missing_names;
3838
3839 /* Compute the names of the arguments that are missing. */
3840 missing_names = PyList_New(missing);
3841 if (missing_names == NULL)
3842 return;
3843 if (positional) {
3844 start = 0;
3845 end = co->co_argcount - defcount;
3846 }
3847 else {
3848 start = co->co_argcount;
3849 end = start + co->co_kwonlyargcount;
3850 }
3851 for (i = start; i < end; i++) {
3852 if (GETLOCAL(i) == NULL) {
3853 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3854 PyObject *name = PyObject_Repr(raw);
3855 if (name == NULL) {
3856 Py_DECREF(missing_names);
3857 return;
3858 }
3859 PyList_SET_ITEM(missing_names, j++, name);
3860 }
3861 }
3862 assert(j == missing);
3863 format_missing(kind, co, missing_names);
3864 Py_DECREF(missing_names);
3865}
3866
3867static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003868too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3869 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003870{
3871 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003872 Py_ssize_t kwonly_given = 0;
3873 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003874 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003875 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003876
Benjamin Petersone109c702011-06-24 09:37:26 -05003877 assert((co->co_flags & CO_VARARGS) == 0);
3878 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003879 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3880 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003881 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003882 }
3883 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003884 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003885 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003886 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003887 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003888 }
3889 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003890 plural = (co_argcount != 1);
3891 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003892 }
3893 if (sig == NULL)
3894 return;
3895 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003896 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3897 kwonly_sig = PyUnicode_FromFormat(format,
3898 given != 1 ? "s" : "",
3899 kwonly_given,
3900 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003901 if (kwonly_sig == NULL) {
3902 Py_DECREF(sig);
3903 return;
3904 }
3905 }
3906 else {
3907 /* This will not fail. */
3908 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003909 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003910 }
3911 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003912 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003913 co->co_name,
3914 sig,
3915 plural ? "s" : "",
3916 given,
3917 kwonly_sig,
3918 given == 1 && !kwonly_given ? "was" : "were");
3919 Py_DECREF(sig);
3920 Py_DECREF(kwonly_sig);
3921}
3922
Guido van Rossumc2e20742006-02-27 22:32:47 +00003923/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003924 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003925 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003926
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003927PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003928_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Victor Stinner74319ae2016-08-25 00:04:09 +02003929 PyObject **args, Py_ssize_t argcount,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003930 PyObject **kwnames, PyObject **kwargs,
3931 Py_ssize_t kwcount, int kwstep,
Victor Stinner74319ae2016-08-25 00:04:09 +02003932 PyObject **defs, Py_ssize_t defcount,
3933 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003934 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003935{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003936 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003937 PyFrameObject *f;
3938 PyObject *retval = NULL;
3939 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003940 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003941 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003942 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3943 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003944 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003946 if (globals == NULL) {
3947 PyErr_SetString(PyExc_SystemError,
3948 "PyEval_EvalCodeEx: NULL globals");
3949 return NULL;
3950 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003951
Victor Stinnerc7020012016-08-16 23:40:29 +02003952 /* Create the frame */
3953 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003954 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003955 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003956 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003957 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003958 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003959 fastlocals = f->f_localsplus;
3960 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003961
Victor Stinnerc7020012016-08-16 23:40:29 +02003962 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003963 if (co->co_flags & CO_VARKEYWORDS) {
3964 kwdict = PyDict_New();
3965 if (kwdict == NULL)
3966 goto fail;
3967 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003968 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003969 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003970 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003971 SETLOCAL(i, kwdict);
3972 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003973 else {
3974 kwdict = NULL;
3975 }
3976
3977 /* Copy positional arguments into local variables */
3978 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003979 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003980 }
3981 else {
3982 n = argcount;
3983 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003984 for (i = 0; i < n; i++) {
3985 x = args[i];
3986 Py_INCREF(x);
3987 SETLOCAL(i, x);
3988 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003989
3990 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003991 if (co->co_flags & CO_VARARGS) {
3992 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003993 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003994 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003995 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003996 SETLOCAL(total_args, u);
3997 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003998 x = args[i];
3999 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004000 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004001 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004002 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004003
Serhiy Storchakab7281052016-09-12 00:52:40 +03004004 /* Handle keyword arguments passed as two strided arrays */
4005 kwcount *= kwstep;
4006 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004007 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004008 PyObject *keyword = kwnames[i];
4009 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004010 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004011
Benjamin Petersonb204a422011-06-05 22:04:07 -05004012 if (keyword == NULL || !PyUnicode_Check(keyword)) {
4013 PyErr_Format(PyExc_TypeError,
4014 "%U() keywords must be strings",
4015 co->co_name);
4016 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004017 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004018
Benjamin Petersonb204a422011-06-05 22:04:07 -05004019 /* Speed hack: do raw pointer compares. As names are
4020 normally interned this should almost always hit. */
4021 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
4022 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004023 PyObject *name = co_varnames[j];
4024 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004025 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004026 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004027 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004028
Benjamin Petersonb204a422011-06-05 22:04:07 -05004029 /* Slow fallback, just in case */
4030 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004031 PyObject *name = co_varnames[j];
4032 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4033 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004034 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004035 }
4036 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004037 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004038 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004039 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004040
Victor Stinner231d1f32017-01-11 02:12:06 +01004041 assert(j >= total_args);
4042 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004043 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02004044 "%U() got an unexpected keyword argument '%S'",
4045 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004046 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004047 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004048
Christian Heimes0bd447f2013-07-20 14:48:10 +02004049 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4050 goto fail;
4051 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004052 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004053
Benjamin Petersonb204a422011-06-05 22:04:07 -05004054 kw_found:
4055 if (GETLOCAL(j) != NULL) {
4056 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02004057 "%U() got multiple values for argument '%S'",
4058 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004059 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004060 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004061 Py_INCREF(value);
4062 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004063 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004064
4065 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004066 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004067 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004068 goto fail;
4069 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004070
4071 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004072 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02004073 Py_ssize_t m = co->co_argcount - defcount;
4074 Py_ssize_t missing = 0;
4075 for (i = argcount; i < m; i++) {
4076 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004077 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004078 }
4079 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004080 if (missing) {
4081 missing_arguments(co, missing, defcount, fastlocals);
4082 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004083 }
4084 if (n > m)
4085 i = n - m;
4086 else
4087 i = 0;
4088 for (; i < defcount; i++) {
4089 if (GETLOCAL(m+i) == NULL) {
4090 PyObject *def = defs[i];
4091 Py_INCREF(def);
4092 SETLOCAL(m+i, def);
4093 }
4094 }
4095 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004096
4097 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004098 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004099 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004100 for (i = co->co_argcount; i < total_args; i++) {
4101 PyObject *name;
4102 if (GETLOCAL(i) != NULL)
4103 continue;
4104 name = PyTuple_GET_ITEM(co->co_varnames, i);
4105 if (kwdefs != NULL) {
4106 PyObject *def = PyDict_GetItem(kwdefs, name);
4107 if (def) {
4108 Py_INCREF(def);
4109 SETLOCAL(i, def);
4110 continue;
4111 }
4112 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004113 missing++;
4114 }
4115 if (missing) {
4116 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004117 goto fail;
4118 }
4119 }
4120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004121 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004122 vars into frame. */
4123 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004124 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004125 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004126 /* Possibly account for the cell variable being an argument. */
4127 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004128 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004129 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004130 /* Clear the local copy. */
4131 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004132 }
4133 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004134 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004135 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004136 if (c == NULL)
4137 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004138 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004139 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004140
4141 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004142 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4143 PyObject *o = PyTuple_GET_ITEM(closure, i);
4144 Py_INCREF(o);
4145 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004146 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004147
Yury Selivanoveb636452016-09-08 22:01:51 -07004148 /* Handle generator/coroutine/asynchronous generator */
4149 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004150 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04004151 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004152 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004153
4154 if (is_coro && tstate->in_coroutine_wrapper) {
4155 assert(coro_wrapper != NULL);
4156 PyErr_Format(PyExc_RuntimeError,
4157 "coroutine wrapper %.200R attempted "
4158 "to recursively wrap %.200R",
4159 coro_wrapper,
4160 co);
4161 goto fail;
4162 }
Yury Selivanov75445082015-05-11 22:57:16 -04004163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004164 /* Don't need to keep the reference to f_back, it will be set
4165 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004166 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004168 /* Create a new generator that owns the ready to run frame
4169 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004170 if (is_coro) {
4171 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004172 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4173 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004174 } else {
4175 gen = PyGen_NewWithQualName(f, name, qualname);
4176 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004177 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004178 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004179 }
INADA Naoki9c157762016-12-26 18:52:46 +09004180
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004181 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004182
Yury Selivanov94c22632015-06-04 10:16:51 -04004183 if (is_coro && coro_wrapper != NULL) {
4184 PyObject *wrapped;
4185 tstate->in_coroutine_wrapper = 1;
4186 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4187 tstate->in_coroutine_wrapper = 0;
4188 return wrapped;
4189 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004190
Yury Selivanov75445082015-05-11 22:57:16 -04004191 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004192 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004193
Victor Stinner59a73272016-12-09 18:51:13 +01004194 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004195
Thomas Woutersce272b62007-09-19 21:19:28 +00004196fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004198 /* decref'ing the frame can cause __del__ methods to get invoked,
4199 which can call back into Python. While we're done with the
4200 current Python frame (f), the associated C stack is still in use,
4201 so recursion_depth must be boosted for the duration.
4202 */
4203 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09004204 if (Py_REFCNT(f) > 1) {
4205 Py_DECREF(f);
4206 _PyObject_GC_TRACK(f);
4207 }
4208 else {
4209 ++tstate->recursion_depth;
4210 Py_DECREF(f);
4211 --tstate->recursion_depth;
4212 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004213 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004214}
4215
Victor Stinner40ee3012014-06-16 15:59:28 +02004216PyObject *
4217PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
4218 PyObject **args, int argcount, PyObject **kws, int kwcount,
4219 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
4220{
4221 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004222 args, argcount,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004223 kws, kws + 1, kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004224 defs, defcount,
4225 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004226 NULL, NULL);
4227}
Tim Peters5ca576e2001-06-18 22:08:13 +00004228
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004229static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004230special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004231{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004232 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004233 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004234 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004235 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004236 return NULL;
4237 }
4238 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004239}
4240
4241
Benjamin Peterson87880242011-07-03 16:48:31 -05004242/* These 3 functions deal with the exception state of generators. */
4243
4244static void
4245save_exc_state(PyThreadState *tstate, PyFrameObject *f)
4246{
4247 PyObject *type, *value, *traceback;
4248 Py_XINCREF(tstate->exc_type);
4249 Py_XINCREF(tstate->exc_value);
4250 Py_XINCREF(tstate->exc_traceback);
4251 type = f->f_exc_type;
4252 value = f->f_exc_value;
4253 traceback = f->f_exc_traceback;
4254 f->f_exc_type = tstate->exc_type;
4255 f->f_exc_value = tstate->exc_value;
4256 f->f_exc_traceback = tstate->exc_traceback;
4257 Py_XDECREF(type);
4258 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004259 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05004260}
4261
4262static void
4263swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
4264{
4265 PyObject *tmp;
4266 tmp = tstate->exc_type;
4267 tstate->exc_type = f->f_exc_type;
4268 f->f_exc_type = tmp;
4269 tmp = tstate->exc_value;
4270 tstate->exc_value = f->f_exc_value;
4271 f->f_exc_value = tmp;
4272 tmp = tstate->exc_traceback;
4273 tstate->exc_traceback = f->f_exc_traceback;
4274 f->f_exc_traceback = tmp;
4275}
4276
4277static void
4278restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
4279{
4280 PyObject *type, *value, *tb;
4281 type = tstate->exc_type;
4282 value = tstate->exc_value;
4283 tb = tstate->exc_traceback;
4284 tstate->exc_type = f->f_exc_type;
4285 tstate->exc_value = f->f_exc_value;
4286 tstate->exc_traceback = f->f_exc_traceback;
4287 f->f_exc_type = NULL;
4288 f->f_exc_value = NULL;
4289 f->f_exc_traceback = NULL;
4290 Py_XDECREF(type);
4291 Py_XDECREF(value);
4292 Py_XDECREF(tb);
4293}
4294
4295
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004296/* Logic for the raise statement (too complicated for inlining).
4297 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004298static int
Collin Winter828f04a2007-08-31 00:04:24 +00004299do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004300{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004301 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004303 if (exc == NULL) {
4304 /* Reraise */
4305 PyThreadState *tstate = PyThreadState_GET();
4306 PyObject *tb;
4307 type = tstate->exc_type;
4308 value = tstate->exc_value;
4309 tb = tstate->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004310 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004311 PyErr_SetString(PyExc_RuntimeError,
4312 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004313 return 0;
4314 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004315 Py_XINCREF(type);
4316 Py_XINCREF(value);
4317 Py_XINCREF(tb);
4318 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004319 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004320 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004322 /* We support the following forms of raise:
4323 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004324 raise <instance>
4325 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004327 if (PyExceptionClass_Check(exc)) {
4328 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004329 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004330 if (value == NULL)
4331 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004332 if (!PyExceptionInstance_Check(value)) {
4333 PyErr_Format(PyExc_TypeError,
4334 "calling %R should have returned an instance of "
4335 "BaseException, not %R",
4336 type, Py_TYPE(value));
4337 goto raise_error;
4338 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004339 }
4340 else if (PyExceptionInstance_Check(exc)) {
4341 value = exc;
4342 type = PyExceptionInstance_Class(exc);
4343 Py_INCREF(type);
4344 }
4345 else {
4346 /* Not something you can raise. You get an exception
4347 anyway, just not what you specified :-) */
4348 Py_DECREF(exc);
4349 PyErr_SetString(PyExc_TypeError,
4350 "exceptions must derive from BaseException");
4351 goto raise_error;
4352 }
Collin Winter828f04a2007-08-31 00:04:24 +00004353
Serhiy Storchakac0191582016-09-27 11:37:10 +03004354 assert(type != NULL);
4355 assert(value != NULL);
4356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004357 if (cause) {
4358 PyObject *fixed_cause;
4359 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004360 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004361 if (fixed_cause == NULL)
4362 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004363 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004364 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004365 else if (PyExceptionInstance_Check(cause)) {
4366 fixed_cause = cause;
4367 }
4368 else if (cause == Py_None) {
4369 Py_DECREF(cause);
4370 fixed_cause = NULL;
4371 }
4372 else {
4373 PyErr_SetString(PyExc_TypeError,
4374 "exception causes must derive from "
4375 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004376 goto raise_error;
4377 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004378 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004379 }
Collin Winter828f04a2007-08-31 00:04:24 +00004380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004381 PyErr_SetObject(type, value);
4382 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004383 Py_DECREF(value);
4384 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004385 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004386
4387raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004388 Py_XDECREF(value);
4389 Py_XDECREF(type);
4390 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004391 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004392}
4393
Tim Petersd6d010b2001-06-21 02:49:55 +00004394/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004395 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004396
Guido van Rossum0368b722007-05-11 16:50:42 +00004397 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4398 with a variable target.
4399*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004400
Barry Warsawe42b18f1997-08-25 22:13:04 +00004401static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004402unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004403{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004404 int i = 0, j = 0;
4405 Py_ssize_t ll = 0;
4406 PyObject *it; /* iter(v) */
4407 PyObject *w;
4408 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004410 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004412 it = PyObject_GetIter(v);
4413 if (it == NULL)
4414 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00004415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004416 for (; i < argcnt; i++) {
4417 w = PyIter_Next(it);
4418 if (w == NULL) {
4419 /* Iterator done, via error or exhaustion. */
4420 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004421 if (argcntafter == -1) {
4422 PyErr_Format(PyExc_ValueError,
4423 "not enough values to unpack (expected %d, got %d)",
4424 argcnt, i);
4425 }
4426 else {
4427 PyErr_Format(PyExc_ValueError,
4428 "not enough values to unpack "
4429 "(expected at least %d, got %d)",
4430 argcnt + argcntafter, i);
4431 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004432 }
4433 goto Error;
4434 }
4435 *--sp = w;
4436 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004438 if (argcntafter == -1) {
4439 /* We better have exhausted the iterator now. */
4440 w = PyIter_Next(it);
4441 if (w == NULL) {
4442 if (PyErr_Occurred())
4443 goto Error;
4444 Py_DECREF(it);
4445 return 1;
4446 }
4447 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004448 PyErr_Format(PyExc_ValueError,
4449 "too many values to unpack (expected %d)",
4450 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004451 goto Error;
4452 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004454 l = PySequence_List(it);
4455 if (l == NULL)
4456 goto Error;
4457 *--sp = l;
4458 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004460 ll = PyList_GET_SIZE(l);
4461 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004462 PyErr_Format(PyExc_ValueError,
4463 "not enough values to unpack (expected at least %d, got %zd)",
4464 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004465 goto Error;
4466 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004468 /* Pop the "after-variable" args off the list. */
4469 for (j = argcntafter; j > 0; j--, i++) {
4470 *--sp = PyList_GET_ITEM(l, ll - j);
4471 }
4472 /* Resize the list. */
4473 Py_SIZE(l) = ll - argcntafter;
4474 Py_DECREF(it);
4475 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004476
Tim Petersd6d010b2001-06-21 02:49:55 +00004477Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004478 for (; i > 0; i--, sp++)
4479 Py_DECREF(*sp);
4480 Py_XDECREF(it);
4481 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004482}
4483
4484
Guido van Rossum96a42c81992-01-12 02:29:51 +00004485#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004486static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004487prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004488{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 printf("%s ", str);
4490 if (PyObject_Print(v, stdout, 0) != 0)
4491 PyErr_Clear(); /* Don't know what else to do */
4492 printf("\n");
4493 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004494}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004495#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004496
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004497static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004498call_exc_trace(Py_tracefunc func, PyObject *self,
4499 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004500{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004501 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004502 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004503 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004504 if (value == NULL) {
4505 value = Py_None;
4506 Py_INCREF(value);
4507 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004508 PyErr_NormalizeException(&type, &value, &orig_traceback);
4509 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004510 arg = PyTuple_Pack(3, type, value, traceback);
4511 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004512 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004513 return;
4514 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004515 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004516 Py_DECREF(arg);
4517 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004518 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004519 else {
4520 Py_XDECREF(type);
4521 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004522 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004523 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004524}
4525
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004526static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004527call_trace_protected(Py_tracefunc func, PyObject *obj,
4528 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004529 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004531 PyObject *type, *value, *traceback;
4532 int err;
4533 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004534 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004535 if (err == 0)
4536 {
4537 PyErr_Restore(type, value, traceback);
4538 return 0;
4539 }
4540 else {
4541 Py_XDECREF(type);
4542 Py_XDECREF(value);
4543 Py_XDECREF(traceback);
4544 return -1;
4545 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004546}
4547
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004548static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004549call_trace(Py_tracefunc func, PyObject *obj,
4550 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004551 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004553 int result;
4554 if (tstate->tracing)
4555 return 0;
4556 tstate->tracing++;
4557 tstate->use_tracing = 0;
4558 result = func(obj, frame, what, arg);
4559 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4560 || (tstate->c_profilefunc != NULL));
4561 tstate->tracing--;
4562 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004563}
4564
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004565PyObject *
4566_PyEval_CallTracing(PyObject *func, PyObject *args)
4567{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004568 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004569 int save_tracing = tstate->tracing;
4570 int save_use_tracing = tstate->use_tracing;
4571 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004573 tstate->tracing = 0;
4574 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4575 || (tstate->c_profilefunc != NULL));
4576 result = PyObject_Call(func, args, NULL);
4577 tstate->tracing = save_tracing;
4578 tstate->use_tracing = save_use_tracing;
4579 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004580}
4581
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004582/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004583static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004584maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004585 PyThreadState *tstate, PyFrameObject *frame,
4586 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004587{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004588 int result = 0;
4589 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004591 /* If the last instruction executed isn't in the current
4592 instruction window, reset the window.
4593 */
4594 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4595 PyAddrPair bounds;
4596 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4597 &bounds);
4598 *instr_lb = bounds.ap_lower;
4599 *instr_ub = bounds.ap_upper;
4600 }
4601 /* If the last instruction falls at the start of a line or if
4602 it represents a jump backwards, update the frame's line
4603 number and call the trace function. */
4604 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
4605 frame->f_lineno = line;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004606 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004607 }
4608 *instr_prev = frame->f_lasti;
4609 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004610}
4611
Fred Drake5755ce62001-06-27 19:19:46 +00004612void
4613PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004614{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004615 PyThreadState *tstate = PyThreadState_GET();
4616 PyObject *temp = tstate->c_profileobj;
4617 Py_XINCREF(arg);
4618 tstate->c_profilefunc = NULL;
4619 tstate->c_profileobj = NULL;
4620 /* Must make sure that tracing is not ignored if 'temp' is freed */
4621 tstate->use_tracing = tstate->c_tracefunc != NULL;
4622 Py_XDECREF(temp);
4623 tstate->c_profilefunc = func;
4624 tstate->c_profileobj = arg;
4625 /* Flag that tracing or profiling is turned on */
4626 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004627}
4628
4629void
4630PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4631{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004632 PyThreadState *tstate = PyThreadState_GET();
4633 PyObject *temp = tstate->c_traceobj;
4634 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4635 Py_XINCREF(arg);
4636 tstate->c_tracefunc = NULL;
4637 tstate->c_traceobj = NULL;
4638 /* Must make sure that profiling is not ignored if 'temp' is freed */
4639 tstate->use_tracing = tstate->c_profilefunc != NULL;
4640 Py_XDECREF(temp);
4641 tstate->c_tracefunc = func;
4642 tstate->c_traceobj = arg;
4643 /* Flag that tracing or profiling is turned on */
4644 tstate->use_tracing = ((func != NULL)
4645 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004646}
4647
Yury Selivanov75445082015-05-11 22:57:16 -04004648void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004649_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004650{
4651 PyThreadState *tstate = PyThreadState_GET();
4652
Yury Selivanov75445082015-05-11 22:57:16 -04004653 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004654 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004655}
4656
4657PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004658_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004659{
4660 PyThreadState *tstate = PyThreadState_GET();
4661 return tstate->coroutine_wrapper;
4662}
4663
Yury Selivanoveb636452016-09-08 22:01:51 -07004664void
4665_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4666{
4667 PyThreadState *tstate = PyThreadState_GET();
4668
4669 Py_XINCREF(firstiter);
4670 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4671}
4672
4673PyObject *
4674_PyEval_GetAsyncGenFirstiter(void)
4675{
4676 PyThreadState *tstate = PyThreadState_GET();
4677 return tstate->async_gen_firstiter;
4678}
4679
4680void
4681_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4682{
4683 PyThreadState *tstate = PyThreadState_GET();
4684
4685 Py_XINCREF(finalizer);
4686 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4687}
4688
4689PyObject *
4690_PyEval_GetAsyncGenFinalizer(void)
4691{
4692 PyThreadState *tstate = PyThreadState_GET();
4693 return tstate->async_gen_finalizer;
4694}
4695
Guido van Rossumb209a111997-04-29 18:18:01 +00004696PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004697PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004698{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004699 PyFrameObject *current_frame = PyEval_GetFrame();
4700 if (current_frame == NULL)
4701 return PyThreadState_GET()->interp->builtins;
4702 else
4703 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004704}
4705
Guido van Rossumb209a111997-04-29 18:18:01 +00004706PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004707PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004708{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004709 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004710 if (current_frame == NULL) {
4711 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004712 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004713 }
4714
4715 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4716 return NULL;
4717
4718 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004719 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004720}
4721
Guido van Rossumb209a111997-04-29 18:18:01 +00004722PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004723PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004724{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004725 PyFrameObject *current_frame = PyEval_GetFrame();
4726 if (current_frame == NULL)
4727 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004728
4729 assert(current_frame->f_globals != NULL);
4730 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004731}
4732
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004733PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004734PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004735{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004736 PyThreadState *tstate = PyThreadState_GET();
4737 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004738}
4739
Guido van Rossum6135a871995-01-09 17:53:26 +00004740int
Tim Peters5ba58662001-07-16 02:29:45 +00004741PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004742{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004743 PyFrameObject *current_frame = PyEval_GetFrame();
4744 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004746 if (current_frame != NULL) {
4747 const int codeflags = current_frame->f_code->co_flags;
4748 const int compilerflags = codeflags & PyCF_MASK;
4749 if (compilerflags) {
4750 result = 1;
4751 cf->cf_flags |= compilerflags;
4752 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004753#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004754 if (codeflags & CO_GENERATOR_ALLOWED) {
4755 result = 1;
4756 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4757 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004758#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004759 }
4760 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004761}
4762
Guido van Rossum3f5da241990-12-20 15:06:42 +00004763
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004764const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004765PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004766{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004767 if (PyMethod_Check(func))
4768 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4769 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004770 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004771 else if (PyCFunction_Check(func))
4772 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4773 else
4774 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004775}
4776
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004777const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004778PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004779{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004780 if (PyMethod_Check(func))
4781 return "()";
4782 else if (PyFunction_Check(func))
4783 return "()";
4784 else if (PyCFunction_Check(func))
4785 return "()";
4786 else
4787 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004788}
4789
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004790#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004791if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004792 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4793 tstate, tstate->frame, \
4794 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004795 x = NULL; \
4796 } \
4797 else { \
4798 x = call; \
4799 if (tstate->c_profilefunc != NULL) { \
4800 if (x == NULL) { \
4801 call_trace_protected(tstate->c_profilefunc, \
4802 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004803 tstate, tstate->frame, \
4804 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004805 /* XXX should pass (type, value, tb) */ \
4806 } else { \
4807 if (call_trace(tstate->c_profilefunc, \
4808 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004809 tstate, tstate->frame, \
4810 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004811 Py_DECREF(x); \
4812 x = NULL; \
4813 } \
4814 } \
4815 } \
4816 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004817} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004818 x = call; \
4819 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004820
Victor Stinner415c5102017-01-11 00:54:57 +01004821/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4822 to reduce the stack consumption. */
4823Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004824call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004825{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004826 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004827 PyObject *func = *pfunc;
4828 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004829 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4830 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004831 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004833 /* Always dispatch PyCFunction first, because these are
4834 presumed to be the most frequent callable object.
4835 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004836 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004837 PyThreadState *tstate = PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004838 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004839 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004840 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
4841 PyThreadState *tstate = PyThreadState_GET();
INADA Naoki93fac8d2017-03-07 14:24:37 +09004842 if (tstate->use_tracing && tstate->c_profilefunc) {
4843 // We need to create PyCFunctionObject for tracing.
4844 PyMethodDescrObject *descr = (PyMethodDescrObject*)func;
4845 func = PyCFunction_NewEx(descr->d_method, stack[0], NULL);
4846 if (func == NULL) {
4847 return NULL;
4848 }
4849 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack+1, nargs-1,
4850 kwnames));
4851 Py_DECREF(func);
4852 }
4853 else {
4854 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4855 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004856 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004857 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004858 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004859 /* Optimize access to bound methods. Reuse the Python stack
4860 to pass 'self' as the first argument, replace 'func'
4861 with 'self'. It avoids the creation of a new temporary tuple
4862 for arguments (to replace func with self) when the method uses
4863 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004864 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004865 Py_INCREF(self);
4866 func = PyMethod_GET_FUNCTION(func);
4867 Py_INCREF(func);
4868 Py_SETREF(*pfunc, self);
4869 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004870 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004871 }
4872 else {
4873 Py_INCREF(func);
4874 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004875
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004876 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004877 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004878 }
4879 else {
4880 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4881 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004882 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004883 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004884
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004885 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4886
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004887 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004888 while ((*pp_stack) > pfunc) {
4889 w = EXT_POP(*pp_stack);
4890 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004891 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004893 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004894}
4895
Jeremy Hylton52820442001-01-03 23:52:36 +00004896static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004897do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004898{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004899 if (PyCFunction_Check(func)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004900 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004901 PyThreadState *tstate = PyThreadState_GET();
4902 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004903 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004904 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004905 else {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004906 return PyObject_Call(func, callargs, kwdict);
Victor Stinner74319ae2016-08-25 00:04:09 +02004907 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004908}
4909
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004910/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004911 nb_index slot defined, and store in *pi.
4912 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004913 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004914 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004915*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004916int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004917_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004918{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004919 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004920 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 "None or have an __index__ method");
4930 return 0;
4931 }
4932 *pi = x;
4933 }
4934 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004935}
4936
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004937int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004938_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004939{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004940 Py_ssize_t x;
4941 if (PyIndex_Check(v)) {
4942 x = PyNumber_AsSsize_t(v, NULL);
4943 if (x == -1 && PyErr_Occurred())
4944 return 0;
4945 }
4946 else {
4947 PyErr_SetString(PyExc_TypeError,
4948 "slice indices must be integers or "
4949 "have an __index__ method");
4950 return 0;
4951 }
4952 *pi = x;
4953 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004954}
4955
4956
Guido van Rossum486364b2007-06-30 05:01:58 +00004957#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004958 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004959
Guido van Rossumb209a111997-04-29 18:18:01 +00004960static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004961cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004962{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004963 int res = 0;
4964 switch (op) {
4965 case PyCmp_IS:
4966 res = (v == w);
4967 break;
4968 case PyCmp_IS_NOT:
4969 res = (v != w);
4970 break;
4971 case PyCmp_IN:
4972 res = PySequence_Contains(w, v);
4973 if (res < 0)
4974 return NULL;
4975 break;
4976 case PyCmp_NOT_IN:
4977 res = PySequence_Contains(w, v);
4978 if (res < 0)
4979 return NULL;
4980 res = !res;
4981 break;
4982 case PyCmp_EXC_MATCH:
4983 if (PyTuple_Check(w)) {
4984 Py_ssize_t i, length;
4985 length = PyTuple_Size(w);
4986 for (i = 0; i < length; i += 1) {
4987 PyObject *exc = PyTuple_GET_ITEM(w, i);
4988 if (!PyExceptionClass_Check(exc)) {
4989 PyErr_SetString(PyExc_TypeError,
4990 CANNOT_CATCH_MSG);
4991 return NULL;
4992 }
4993 }
4994 }
4995 else {
4996 if (!PyExceptionClass_Check(w)) {
4997 PyErr_SetString(PyExc_TypeError,
4998 CANNOT_CATCH_MSG);
4999 return NULL;
5000 }
5001 }
5002 res = PyErr_GivenExceptionMatches(v, w);
5003 break;
5004 default:
5005 return PyObject_RichCompare(v, w, op);
5006 }
5007 v = res ? Py_True : Py_False;
5008 Py_INCREF(v);
5009 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005010}
5011
Thomas Wouters52152252000-08-17 22:55:00 +00005012static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005013import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
5014{
5015 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005016 PyObject *import_func, *res;
5017 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005018
5019 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
5020 if (import_func == NULL) {
5021 PyErr_SetString(PyExc_ImportError, "__import__ not found");
5022 return NULL;
5023 }
5024
5025 /* Fast path for not overloaded __import__. */
5026 if (import_func == PyThreadState_GET()->interp->import_func) {
5027 int ilevel = _PyLong_AsInt(level);
5028 if (ilevel == -1 && PyErr_Occurred()) {
5029 return NULL;
5030 }
5031 res = PyImport_ImportModuleLevelObject(
5032 name,
5033 f->f_globals,
5034 f->f_locals == NULL ? Py_None : f->f_locals,
5035 fromlist,
5036 ilevel);
5037 return res;
5038 }
5039
5040 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005041
5042 stack[0] = name;
5043 stack[1] = f->f_globals;
5044 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5045 stack[3] = fromlist;
5046 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005047 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005048 Py_DECREF(import_func);
5049 return res;
5050}
5051
5052static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00005053import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005054{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005055 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005056 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08005057 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005059 x = PyObject_GetAttr(v, name);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005060 if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
5061 return x;
5062 /* Issue #17636: in case this failed because of a circular relative
5063 import, try to fallback on reading the module directly from
5064 sys.modules. */
5065 PyErr_Clear();
5066 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005067 if (pkgname == NULL) {
5068 goto error;
5069 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005070 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005071 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005072 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005073 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005074 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005075 x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005076 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005077 if (x == NULL) {
5078 goto error;
5079 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005080 Py_DECREF(pkgname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005081 Py_INCREF(x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005082 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005083 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005084 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005085 if (pkgname == NULL) {
5086 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5087 if (pkgname_or_unknown == NULL) {
5088 Py_XDECREF(pkgpath);
5089 return NULL;
5090 }
5091 } else {
5092 pkgname_or_unknown = pkgname;
5093 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005094
5095 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
5096 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08005097 errmsg = PyUnicode_FromFormat(
5098 "cannot import name %R from %R (unknown location)",
5099 name, pkgname_or_unknown
5100 );
5101 /* NULL check for errmsg done by PyErr_SetImportError. */
5102 PyErr_SetImportError(errmsg, pkgname, NULL);
5103 }
5104 else {
5105 errmsg = PyUnicode_FromFormat(
5106 "cannot import name %R from %R (%S)",
5107 name, pkgname_or_unknown, pkgpath
5108 );
5109 /* NULL check for errmsg done by PyErr_SetImportError. */
5110 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005111 }
5112
Xiang Zhang4830f582017-03-21 11:13:42 +08005113 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005114 Py_XDECREF(pkgname_or_unknown);
5115 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005116 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005117}
Guido van Rossumac7be682001-01-17 15:42:30 +00005118
Thomas Wouters52152252000-08-17 22:55:00 +00005119static int
5120import_all_from(PyObject *locals, PyObject *v)
5121{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005122 _Py_IDENTIFIER(__all__);
5123 _Py_IDENTIFIER(__dict__);
5124 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005125 PyObject *dict, *name, *value;
5126 int skip_leading_underscores = 0;
5127 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005129 if (all == NULL) {
5130 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5131 return -1; /* Unexpected error */
5132 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005133 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005134 if (dict == NULL) {
5135 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5136 return -1;
5137 PyErr_SetString(PyExc_ImportError,
5138 "from-import-* object has no __dict__ and no __all__");
5139 return -1;
5140 }
5141 all = PyMapping_Keys(dict);
5142 Py_DECREF(dict);
5143 if (all == NULL)
5144 return -1;
5145 skip_leading_underscores = 1;
5146 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005148 for (pos = 0, err = 0; ; pos++) {
5149 name = PySequence_GetItem(all, pos);
5150 if (name == NULL) {
5151 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5152 err = -1;
5153 else
5154 PyErr_Clear();
5155 break;
5156 }
5157 if (skip_leading_underscores &&
5158 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005159 PyUnicode_READY(name) != -1 &&
5160 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005161 {
5162 Py_DECREF(name);
5163 continue;
5164 }
5165 value = PyObject_GetAttr(v, name);
5166 if (value == NULL)
5167 err = -1;
5168 else if (PyDict_CheckExact(locals))
5169 err = PyDict_SetItem(locals, name, value);
5170 else
5171 err = PyObject_SetItem(locals, name, value);
5172 Py_DECREF(name);
5173 Py_XDECREF(value);
5174 if (err != 0)
5175 break;
5176 }
5177 Py_DECREF(all);
5178 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005179}
5180
Guido van Rossumac7be682001-01-17 15:42:30 +00005181static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005182format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005183{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005184 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005186 if (!obj)
5187 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005188
Serhiy Storchaka06515832016-11-20 09:13:07 +02005189 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005190 if (!obj_str)
5191 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005193 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005194}
Guido van Rossum950361c1997-01-24 13:49:28 +00005195
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005196static void
5197format_exc_unbound(PyCodeObject *co, int oparg)
5198{
5199 PyObject *name;
5200 /* Don't stomp existing exception */
5201 if (PyErr_Occurred())
5202 return;
5203 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5204 name = PyTuple_GET_ITEM(co->co_cellvars,
5205 oparg);
5206 format_exc_check_arg(
5207 PyExc_UnboundLocalError,
5208 UNBOUNDLOCAL_ERROR_MSG,
5209 name);
5210 } else {
5211 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5212 PyTuple_GET_SIZE(co->co_cellvars));
5213 format_exc_check_arg(PyExc_NameError,
5214 UNBOUNDFREE_ERROR_MSG, name);
5215 }
5216}
5217
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005218static PyObject *
5219unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005220 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005221{
5222 PyObject *res;
5223 if (Py_REFCNT(v) == 2) {
5224 /* In the common case, there are 2 references to the value
5225 * stored in 'variable' when the += is performed: one on the
5226 * value stack (in 'v') and one still stored in the
5227 * 'variable'. We try to delete the variable now to reduce
5228 * the refcnt to 1.
5229 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005230 int opcode, oparg;
5231 NEXTOPARG();
5232 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005233 case STORE_FAST:
5234 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005235 PyObject **fastlocals = f->f_localsplus;
5236 if (GETLOCAL(oparg) == v)
5237 SETLOCAL(oparg, NULL);
5238 break;
5239 }
5240 case STORE_DEREF:
5241 {
5242 PyObject **freevars = (f->f_localsplus +
5243 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005244 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005245 if (PyCell_GET(c) == v) {
5246 PyCell_SET(c, NULL);
5247 Py_DECREF(v);
5248 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005249 break;
5250 }
5251 case STORE_NAME:
5252 {
5253 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005254 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005255 PyObject *locals = f->f_locals;
5256 if (PyDict_CheckExact(locals) &&
5257 PyDict_GetItem(locals, name) == v) {
5258 if (PyDict_DelItem(locals, name) != 0) {
5259 PyErr_Clear();
5260 }
5261 }
5262 break;
5263 }
5264 }
5265 }
5266 res = v;
5267 PyUnicode_Append(&res, w);
5268 return res;
5269}
5270
Guido van Rossum950361c1997-01-24 13:49:28 +00005271#ifdef DYNAMIC_EXECUTION_PROFILE
5272
Skip Montanarof118cb12001-10-15 20:51:38 +00005273static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005274getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005275{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005276 int i;
5277 PyObject *l = PyList_New(256);
5278 if (l == NULL) return NULL;
5279 for (i = 0; i < 256; i++) {
5280 PyObject *x = PyLong_FromLong(a[i]);
5281 if (x == NULL) {
5282 Py_DECREF(l);
5283 return NULL;
5284 }
5285 PyList_SetItem(l, i, x);
5286 }
5287 for (i = 0; i < 256; i++)
5288 a[i] = 0;
5289 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005290}
5291
5292PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005293_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005294{
5295#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005296 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005297#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005298 int i;
5299 PyObject *l = PyList_New(257);
5300 if (l == NULL) return NULL;
5301 for (i = 0; i < 257; i++) {
5302 PyObject *x = getarray(dxpairs[i]);
5303 if (x == NULL) {
5304 Py_DECREF(l);
5305 return NULL;
5306 }
5307 PyList_SetItem(l, i, x);
5308 }
5309 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005310#endif
5311}
5312
5313#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005314
5315Py_ssize_t
5316_PyEval_RequestCodeExtraIndex(freefunc free)
5317{
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005318 PyInterpreterState *interp = PyThreadState_Get()->interp;
Brett Cannon5c4de282016-09-07 11:16:41 -07005319 Py_ssize_t new_index;
5320
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005321 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005322 return -1;
5323 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005324 new_index = interp->co_extra_user_count++;
5325 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005326 return new_index;
5327}
Łukasz Langaa785c872016-09-09 17:37:37 -07005328
5329static void
5330dtrace_function_entry(PyFrameObject *f)
5331{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005332 const char *filename;
5333 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005334 int lineno;
5335
5336 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5337 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5338 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5339
5340 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5341}
5342
5343static void
5344dtrace_function_return(PyFrameObject *f)
5345{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005346 const char *filename;
5347 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005348 int lineno;
5349
5350 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5351 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5352 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5353
5354 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5355}
5356
5357/* DTrace equivalent of maybe_call_line_trace. */
5358static void
5359maybe_dtrace_line(PyFrameObject *frame,
5360 int *instr_lb, int *instr_ub, int *instr_prev)
5361{
5362 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005363 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005364
5365 /* If the last instruction executed isn't in the current
5366 instruction window, reset the window.
5367 */
5368 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5369 PyAddrPair bounds;
5370 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5371 &bounds);
5372 *instr_lb = bounds.ap_lower;
5373 *instr_ub = bounds.ap_upper;
5374 }
5375 /* If the last instruction falls at the start of a line or if
5376 it represents a jump backwards, update the frame's line
5377 number and call the trace function. */
5378 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5379 frame->f_lineno = line;
5380 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5381 if (!co_filename)
5382 co_filename = "?";
5383 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5384 if (!co_name)
5385 co_name = "?";
5386 PyDTrace_LINE(co_filename, co_name, line);
5387 }
5388 *instr_prev = frame->f_lasti;
5389}