blob: e682fc1afd0ed0ec73e8d15c4f3b2407ed459f9c [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
Guido van Rossume59214e1994-08-30 08:01:59 +0000143#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000144
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000145#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000146#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000147#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000148#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000149
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000150static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000151static long main_thread = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000152/* This single variable consolidates all requests to break out of the fast path
153 in the eval loop. */
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000154static _Py_atomic_int eval_breaker = {0};
155/* Request for dropping the GIL */
156static _Py_atomic_int gil_drop_request = {0};
157/* Request for running pending calls. */
158static _Py_atomic_int pendingcalls_to_do = {0};
159/* Request for looking at the `async_exc` field of the current thread state.
160 Guarded by the GIL. */
161static int pending_async_exc = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000162
163#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000164
Tim Peters7f468f22004-10-11 02:40:51 +0000165int
166PyEval_ThreadsInitialized(void)
167{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000169}
170
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000171void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000172PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000173{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 if (gil_created())
175 return;
176 create_gil();
177 take_gil(PyThreadState_GET());
178 main_thread = PyThread_get_thread_ident();
179 if (!pending_lock)
180 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000181}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000182
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000183void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000184_PyEval_FiniThreads(void)
185{
186 if (!gil_created())
187 return;
188 destroy_gil();
189 assert(!gil_created());
190}
191
192void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000193PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000194{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 PyThreadState *tstate = PyThreadState_GET();
196 if (tstate == NULL)
197 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
198 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000199}
200
201void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000202PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 /* This function must succeed when the current thread state is NULL.
205 We therefore avoid PyThreadState_GET() which dumps a fatal error
206 in debug mode.
207 */
208 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
209 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000210}
211
212void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000213PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000214{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 if (tstate == NULL)
216 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
217 /* Check someone has called PyEval_InitThreads() to create the lock */
218 assert(gil_created());
219 take_gil(tstate);
220 if (PyThreadState_Swap(tstate) != NULL)
221 Py_FatalError(
222 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000223}
224
225void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000226PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000227{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 if (tstate == NULL)
229 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
230 if (PyThreadState_Swap(NULL) != tstate)
231 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
232 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000233}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000234
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200235/* This function is called from PyOS_AfterFork to destroy all threads which are
236 * not running in the child process, and clear internal locks which might be
237 * held by those threads. (This could also be done using pthread_atfork
238 * mechanism, at least for the pthreads implementation.) */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000239
240void
241PyEval_ReInitThreads(void)
242{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200243 _Py_IDENTIFIER(_after_fork);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 PyObject *threading, *result;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200245 PyThreadState *current_tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 if (!gil_created())
248 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 recreate_gil();
250 pending_lock = PyThread_allocate_lock();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200251 take_gil(current_tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 /* Update the threading module with the new state.
255 */
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200256 threading = PyMapping_GetItemString(current_tstate->interp->modules,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 "threading");
258 if (threading == NULL) {
259 /* threading not imported */
260 PyErr_Clear();
261 return;
262 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200263 result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 if (result == NULL)
265 PyErr_WriteUnraisable(threading);
266 else
267 Py_DECREF(result);
268 Py_DECREF(threading);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200269
270 /* Destroy all threads except the current one */
271 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000272}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000273
274#else
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000275static _Py_atomic_int eval_breaker = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000276static int pending_async_exc = 0;
277#endif /* WITH_THREAD */
278
279/* This function is used to signal that async exceptions are waiting to be
280 raised, therefore it is also useful in non-threaded builds. */
281
282void
283_PyEval_SignalAsyncExc(void)
284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000286}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000287
Guido van Rossumff4949e1992-08-05 19:58:53 +0000288/* Functions save_thread and restore_thread are always defined so
289 dynamically loaded modules needn't be compiled separately for use
290 with and without threads: */
291
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000292PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000293PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000294{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 PyThreadState *tstate = PyThreadState_Swap(NULL);
296 if (tstate == NULL)
297 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000298#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 if (gil_created())
300 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000301#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000303}
304
305void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000306PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000307{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 if (tstate == NULL)
309 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000310#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 if (gil_created()) {
312 int err = errno;
313 take_gil(tstate);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200314 /* _Py_Finalizing is protected by the GIL */
315 if (_Py_Finalizing && tstate != _Py_Finalizing) {
316 drop_gil(tstate);
317 PyThread_exit_thread();
318 assert(0); /* unreachable */
319 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 errno = err;
321 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000322#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000324}
325
326
Guido van Rossuma9672091994-09-14 13:31:22 +0000327/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
328 signal handlers or Mac I/O completion routines) can schedule calls
329 to a function to be called synchronously.
330 The synchronous function is called with one void* argument.
331 It should return 0 for success or -1 for failure -- failure should
332 be accompanied by an exception.
333
334 If registry succeeds, the registry function returns 0; if it fails
335 (e.g. due to too many pending calls) it returns -1 (without setting
336 an exception condition).
337
338 Note that because registry may occur from within signal handlers,
339 or other asynchronous events, calling malloc() is unsafe!
340
341#ifdef WITH_THREAD
342 Any thread can schedule pending calls, but only the main thread
343 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000344 There is no facility to schedule calls to a particular thread, but
345 that should be easy to change, should that ever be required. In
346 that case, the static variables here should go into the python
347 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000348#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000349*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000350
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000351#ifdef WITH_THREAD
352
353/* The WITH_THREAD implementation is thread-safe. It allows
354 scheduling to be made from any thread, and even from an executing
355 callback.
356 */
357
358#define NPENDINGCALLS 32
359static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 int (*func)(void *);
361 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000362} pendingcalls[NPENDINGCALLS];
363static int pendingfirst = 0;
364static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000365
366int
367Py_AddPendingCall(int (*func)(void *), void *arg)
368{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 int i, j, result=0;
370 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 /* try a few times for the lock. Since this mechanism is used
373 * for signal handling (on the main thread), there is a (slim)
374 * chance that a signal is delivered on the same thread while we
375 * hold the lock during the Py_MakePendingCalls() function.
376 * This avoids a deadlock in that case.
377 * Note that signals can be delivered on any thread. In particular,
378 * on Windows, a SIGINT is delivered on a system-created worker
379 * thread.
380 * We also check for lock being NULL, in the unlikely case that
381 * this function is called before any bytecode evaluation takes place.
382 */
383 if (lock != NULL) {
384 for (i = 0; i<100; i++) {
385 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
386 break;
387 }
388 if (i == 100)
389 return -1;
390 }
391
392 i = pendinglast;
393 j = (i + 1) % NPENDINGCALLS;
394 if (j == pendingfirst) {
395 result = -1; /* Queue full */
396 } else {
397 pendingcalls[i].func = func;
398 pendingcalls[i].arg = arg;
399 pendinglast = j;
400 }
401 /* signal main loop */
402 SIGNAL_PENDING_CALLS();
403 if (lock != NULL)
404 PyThread_release_lock(lock);
405 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000406}
407
408int
409Py_MakePendingCalls(void)
410{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200411 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 int i;
413 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 if (!pending_lock) {
416 /* initial allocation of the lock */
417 pending_lock = PyThread_allocate_lock();
418 if (pending_lock == NULL)
419 return -1;
420 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 /* only service pending calls on main thread */
423 if (main_thread && PyThread_get_thread_ident() != main_thread)
424 return 0;
425 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200426 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200428 busy = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 /* perform a bounded number of calls, in case of recursion */
430 for (i=0; i<NPENDINGCALLS; i++) {
431 int j;
432 int (*func)(void *);
433 void *arg = NULL;
434
435 /* pop one item off the queue while holding the lock */
436 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
437 j = pendingfirst;
438 if (j == pendinglast) {
439 func = NULL; /* Queue empty */
440 } else {
441 func = pendingcalls[j].func;
442 arg = pendingcalls[j].arg;
443 pendingfirst = (j + 1) % NPENDINGCALLS;
444 }
445 if (pendingfirst != pendinglast)
446 SIGNAL_PENDING_CALLS();
447 else
448 UNSIGNAL_PENDING_CALLS();
449 PyThread_release_lock(pending_lock);
450 /* having released the lock, perform the callback */
451 if (func == NULL)
452 break;
453 r = func(arg);
454 if (r)
455 break;
456 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200457 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000459}
460
461#else /* if ! defined WITH_THREAD */
462
463/*
464 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
465 This code is used for signal handling in python that isn't built
466 with WITH_THREAD.
467 Don't use this implementation when Py_AddPendingCalls() can happen
468 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469
Guido van Rossuma9672091994-09-14 13:31:22 +0000470 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000471 (1) nested asynchronous calls to Py_AddPendingCall()
472 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000474 (1) is very unlikely because typically signal delivery
475 is blocked during signal handling. So it should be impossible.
476 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000477 The current code is safe against (2), but not against (1).
478 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000479 thread is present, interrupted by signals, and that the critical
480 section is protected with the "busy" variable. On Windows, which
481 delivers SIGINT on a system thread, this does not hold and therefore
482 Windows really shouldn't use this version.
483 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000484*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000485
Guido van Rossuma9672091994-09-14 13:31:22 +0000486#define NPENDINGCALLS 32
487static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 int (*func)(void *);
489 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000490} pendingcalls[NPENDINGCALLS];
491static volatile int pendingfirst = 0;
492static volatile int pendinglast = 0;
Benjamin Peterson08ec84c2010-05-30 14:49:32 +0000493static _Py_atomic_int pendingcalls_to_do = {0};
Guido van Rossuma9672091994-09-14 13:31:22 +0000494
495int
Thomas Wouters334fb892000-07-25 12:56:38 +0000496Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000497{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 static volatile int busy = 0;
499 int i, j;
500 /* XXX Begin critical section */
501 if (busy)
502 return -1;
503 busy = 1;
504 i = pendinglast;
505 j = (i + 1) % NPENDINGCALLS;
506 if (j == pendingfirst) {
507 busy = 0;
508 return -1; /* Queue full */
509 }
510 pendingcalls[i].func = func;
511 pendingcalls[i].arg = arg;
512 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 SIGNAL_PENDING_CALLS();
515 busy = 0;
516 /* XXX End critical section */
517 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000518}
519
Guido van Rossum180d7b41994-09-29 09:45:57 +0000520int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000521Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000522{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 static int busy = 0;
524 if (busy)
525 return 0;
526 busy = 1;
527 UNSIGNAL_PENDING_CALLS();
528 for (;;) {
529 int i;
530 int (*func)(void *);
531 void *arg;
532 i = pendingfirst;
533 if (i == pendinglast)
534 break; /* Queue empty */
535 func = pendingcalls[i].func;
536 arg = pendingcalls[i].arg;
537 pendingfirst = (i + 1) % NPENDINGCALLS;
538 if (func(arg) < 0) {
539 busy = 0;
540 SIGNAL_PENDING_CALLS(); /* We're not done yet */
541 return -1;
542 }
543 }
544 busy = 0;
545 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000546}
547
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000548#endif /* WITH_THREAD */
549
Guido van Rossuma9672091994-09-14 13:31:22 +0000550
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000551/* The interpreter's recursion limit */
552
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000553#ifndef Py_DEFAULT_RECURSION_LIMIT
554#define Py_DEFAULT_RECURSION_LIMIT 1000
555#endif
556static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
557int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000558
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000559int
560Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000561{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000563}
564
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000565void
566Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000567{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 recursion_limit = new_limit;
569 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000570}
571
Armin Rigo2b3eb402003-10-28 12:05:48 +0000572/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
573 if the recursion_depth reaches _Py_CheckRecursionLimit.
574 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
575 to guarantee that _Py_CheckRecursiveCall() is regularly called.
576 Without USE_STACKCHECK, there is no need for this. */
577int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300578_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000579{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000581
582#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 if (PyOS_CheckStack()) {
584 --tstate->recursion_depth;
585 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
586 return -1;
587 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000588#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 _Py_CheckRecursionLimit = recursion_limit;
590 if (tstate->recursion_critical)
591 /* Somebody asked that we don't check for recursion. */
592 return 0;
593 if (tstate->overflowed) {
594 if (tstate->recursion_depth > recursion_limit + 50) {
595 /* Overflowing while handling an overflow. Give up. */
596 Py_FatalError("Cannot recover from stack overflow.");
597 }
598 return 0;
599 }
600 if (tstate->recursion_depth > recursion_limit) {
601 --tstate->recursion_depth;
602 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400603 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 "maximum recursion depth exceeded%s",
605 where);
606 return -1;
607 }
608 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000609}
610
Guido van Rossum374a9221991-04-04 10:40:29 +0000611/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000612enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000613 WHY_NOT = 0x0001, /* No error */
614 WHY_EXCEPTION = 0x0002, /* Exception occurred */
Stefan Krahb7e10102010-06-23 18:42:39 +0000615 WHY_RETURN = 0x0008, /* 'return' statement */
616 WHY_BREAK = 0x0010, /* 'break' statement */
617 WHY_CONTINUE = 0x0020, /* 'continue' statement */
618 WHY_YIELD = 0x0040, /* 'yield' operator */
619 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000620};
Guido van Rossum374a9221991-04-04 10:40:29 +0000621
Benjamin Peterson87880242011-07-03 16:48:31 -0500622static void save_exc_state(PyThreadState *, PyFrameObject *);
623static void swap_exc_state(PyThreadState *, PyFrameObject *);
624static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400625static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000626static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000627
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000628/* Records whether tracing is on for any thread. Counts the number of
629 threads for which tstate->c_tracefunc is non-NULL, so if the value
630 is 0, we know we don't have to check this thread's c_tracefunc.
631 This speeds up the if statement in PyEval_EvalFrameEx() after
632 fast_next_opcode*/
633static int _Py_TracingPossible = 0;
634
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000635
Guido van Rossum374a9221991-04-04 10:40:29 +0000636
Guido van Rossumb209a111997-04-29 18:18:01 +0000637PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000638PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000639{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 return PyEval_EvalCodeEx(co,
641 globals, locals,
642 (PyObject **)NULL, 0,
643 (PyObject **)NULL, 0,
644 (PyObject **)NULL, 0,
645 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000646}
647
648
649/* Interpreter main loop */
650
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000651PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000652PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 /* This is for backward compatibility with extension modules that
654 used this API; core interpreter code should call
655 PyEval_EvalFrameEx() */
656 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000657}
658
659PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000660PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000661{
Brett Cannon3cebf932016-09-05 15:33:46 -0700662 PyThreadState *tstate = PyThreadState_GET();
663 return tstate->interp->eval_frame(f, throwflag);
664}
665
Victor Stinnerc6944e72016-11-11 02:13:35 +0100666PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700667_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
668{
Guido van Rossum950361c1997-01-24 13:49:28 +0000669#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000671#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200672 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300673 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200674 int opcode; /* Current opcode */
675 int oparg; /* Current opcode argument, if any */
676 enum why_code why; /* Reason for block stack unwind */
677 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 PyObject *retval = NULL; /* Return value */
679 PyThreadState *tstate = PyThreadState_GET();
680 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 is true when the line being executed has changed. The
687 initial values are such as to make this false the first
688 time it is tested. */
689 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000690
Serhiy Storchakaab874002016-09-11 13:48:15 +0300691 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 PyObject *names;
693 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000694
Brett Cannon368b4b72012-04-02 12:17:59 -0400695#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200696 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400697#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200698
Antoine Pitroub52ec782009-01-25 16:34:23 +0000699/* Computed GOTOs, or
700 the-optimization-commonly-but-improperly-known-as-"threaded code"
701 using gcc's labels-as-values extension
702 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
703
704 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000706 combined with a lookup table of jump addresses. However, since the
707 indirect jump instruction is shared by all opcodes, the CPU will have a
708 hard time making the right prediction for where to jump next (actually,
709 it will be always wrong except in the uncommon case of a sequence of
710 several identical opcodes).
711
712 "Threaded code" in contrast, uses an explicit jump table and an explicit
713 indirect jump instruction at the end of each opcode. Since the jump
714 instruction is at a different address for each opcode, the CPU will make a
715 separate prediction for each of these instructions, which is equivalent to
716 predicting the second opcode of each opcode pair. These predictions have
717 a much better chance to turn out valid, especially in small bytecode loops.
718
719 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000721 and potentially many more instructions (depending on the pipeline width).
722 A correctly predicted branch, however, is nearly free.
723
724 At the time of this writing, the "threaded code" version is up to 15-20%
725 faster than the normal "switch" version, depending on the compiler and the
726 CPU architecture.
727
728 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
729 because it would render the measurements invalid.
730
731
732 NOTE: care must be taken that the compiler doesn't try to "optimize" the
733 indirect jumps by sharing them between all opcodes. Such optimizations
734 can be disabled on gcc by using the -fno-gcse flag (or possibly
735 -fno-crossjumping).
736*/
737
Antoine Pitrou042b1282010-08-13 21:15:58 +0000738#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000739#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000740#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000741#endif
742
Antoine Pitrou042b1282010-08-13 21:15:58 +0000743#ifdef HAVE_COMPUTED_GOTOS
744 #ifndef USE_COMPUTED_GOTOS
745 #define USE_COMPUTED_GOTOS 1
746 #endif
747#else
748 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
749 #error "Computed gotos are not supported on this compiler."
750 #endif
751 #undef USE_COMPUTED_GOTOS
752 #define USE_COMPUTED_GOTOS 0
753#endif
754
755#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000756/* Import the static jump table */
757#include "opcode_targets.h"
758
Antoine Pitroub52ec782009-01-25 16:34:23 +0000759#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 TARGET_##op: \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000762
Antoine Pitroub52ec782009-01-25 16:34:23 +0000763#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 { \
765 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
766 FAST_DISPATCH(); \
767 } \
768 continue; \
769 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000770
771#ifdef LLTRACE
772#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700774 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300776 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300777 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 } \
779 goto fast_next_opcode; \
780 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000781#else
782#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700784 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300786 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300787 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 } \
789 goto fast_next_opcode; \
790 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000791#endif
792
793#else
794#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 case op:
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300796
Antoine Pitroub52ec782009-01-25 16:34:23 +0000797#define DISPATCH() continue
798#define FAST_DISPATCH() goto fast_next_opcode
799#endif
800
801
Neal Norwitza81d2202002-07-14 00:27:26 +0000802/* Tuple access macros */
803
804#ifndef Py_DEBUG
805#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
806#else
807#define GETITEM(v, i) PyTuple_GetItem((v), (i))
808#endif
809
Guido van Rossum374a9221991-04-04 10:40:29 +0000810/* Code access macros */
811
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300812/* The integer overflow is checked by an assertion below. */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300813#define INSTR_OFFSET() (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300814#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300815 _Py_CODEUNIT word = *next_instr; \
816 opcode = _Py_OPCODE(word); \
817 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300818 next_instr++; \
819 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300820#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
821#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000822
Raymond Hettingerf606f872003-03-16 03:11:04 +0000823/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 Some opcodes tend to come in pairs thus making it possible to
825 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300826 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 Verifying the prediction costs a single high-speed test of a register
829 variable against a constant. If the pairing was good, then the
830 processor's own internal branch predication has a high likelihood of
831 success, resulting in a nearly zero-overhead transition to the
832 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300833 including its unpredictable switch-case branch. Combined with the
834 processor's internal branch prediction, a successful PREDICT has the
835 effect of making the two opcodes run as if they were a single new opcode
836 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000837
Georg Brandl86b2fb92008-07-16 03:43:04 +0000838 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 predictions turned-on and interpret the results as if some opcodes
840 had been combined or turn-off predictions so that the opcode frequency
841 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000842
843 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 the CPU to record separate branch prediction information for each
845 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000846
Raymond Hettingerf606f872003-03-16 03:11:04 +0000847*/
848
Antoine Pitrou042b1282010-08-13 21:15:58 +0000849#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000851#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300852#define PREDICT(op) \
853 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300854 _Py_CODEUNIT word = *next_instr; \
855 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300856 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300857 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300858 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300859 goto PRED_##op; \
860 } \
861 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000862#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300863#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000864
Raymond Hettingerf606f872003-03-16 03:11:04 +0000865
Guido van Rossum374a9221991-04-04 10:40:29 +0000866/* Stack manipulation macros */
867
Martin v. Löwis18e16552006-02-15 17:27:45 +0000868/* The stack can grow at most MAXINT deep, as co_nlocals and
869 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000870#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
871#define EMPTY() (STACK_LEVEL() == 0)
872#define TOP() (stack_pointer[-1])
873#define SECOND() (stack_pointer[-2])
874#define THIRD() (stack_pointer[-3])
875#define FOURTH() (stack_pointer[-4])
876#define PEEK(n) (stack_pointer[-(n)])
877#define SET_TOP(v) (stack_pointer[-1] = (v))
878#define SET_SECOND(v) (stack_pointer[-2] = (v))
879#define SET_THIRD(v) (stack_pointer[-3] = (v))
880#define SET_FOURTH(v) (stack_pointer[-4] = (v))
881#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
882#define BASIC_STACKADJ(n) (stack_pointer += n)
883#define BASIC_PUSH(v) (*stack_pointer++ = (v))
884#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000885
Guido van Rossum96a42c81992-01-12 02:29:51 +0000886#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000888 lltrace && prtrace(TOP(), "push")); \
889 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000891 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000893 lltrace && prtrace(TOP(), "stackadj")); \
894 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +0000895#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000896 prtrace((STACK_POINTER)[-1], "ext_pop")), \
897 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000898#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000899#define PUSH(v) BASIC_PUSH(v)
900#define POP() BASIC_POP()
901#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000902#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000903#endif
904
Guido van Rossum681d79a1995-07-18 14:51:37 +0000905/* Local variable macros */
906
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000908
909/* The SETLOCAL() macro must not DECREF the local variable in-place and
910 then store the new value; it must copy the old value to a temporary
911 value, then store the new value, and then DECREF the temporary value.
912 This is because it is possible that during the DECREF the frame is
913 accessed by other code (e.g. a __del__ method or gc.collect()) and the
914 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000916 GETLOCAL(i) = value; \
917 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000918
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000919
920#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 while (STACK_LEVEL() > (b)->b_level) { \
922 PyObject *v = POP(); \
923 Py_XDECREF(v); \
924 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000925
926#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300927 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 PyObject *type, *value, *traceback; \
929 assert(STACK_LEVEL() >= (b)->b_level + 3); \
930 while (STACK_LEVEL() > (b)->b_level + 3) { \
931 value = POP(); \
932 Py_XDECREF(value); \
933 } \
934 type = tstate->exc_type; \
935 value = tstate->exc_value; \
936 traceback = tstate->exc_traceback; \
937 tstate->exc_type = POP(); \
938 tstate->exc_value = POP(); \
939 tstate->exc_traceback = POP(); \
940 Py_XDECREF(type); \
941 Py_XDECREF(value); \
942 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300943 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000944
Guido van Rossuma027efa1997-05-05 20:56:21 +0000945/* Start of code */
946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 /* push frame */
948 if (Py_EnterRecursiveCall(""))
949 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 if (tstate->use_tracing) {
954 if (tstate->c_tracefunc != NULL) {
955 /* tstate->c_tracefunc, if defined, is a
956 function that will be called on *every* entry
957 to a code block. Its return value, if not
958 None, is a function that will be called at
959 the start of each executed line of code.
960 (Actually, the function must return itself
961 in order to continue tracing.) The trace
962 functions are called with three arguments:
963 a pointer to the current frame, a string
964 indicating why the function is called, and
965 an argument which depends on the situation.
966 The global trace function is also called
967 whenever an exception is detected. */
968 if (call_trace_protected(tstate->c_tracefunc,
969 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100970 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 /* Trace function raised an error */
972 goto exit_eval_frame;
973 }
974 }
975 if (tstate->c_profilefunc != NULL) {
976 /* Similar for c_profilefunc, except it needn't
977 return itself and isn't called for "line" events */
978 if (call_trace_protected(tstate->c_profilefunc,
979 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100980 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 /* Profile function raised an error */
982 goto exit_eval_frame;
983 }
984 }
985 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000986
Łukasz Langaa785c872016-09-09 17:37:37 -0700987 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
988 dtrace_function_entry(f);
989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 co = f->f_code;
991 names = co->co_names;
992 consts = co->co_consts;
993 fastlocals = f->f_localsplus;
994 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300995 assert(PyBytes_Check(co->co_code));
996 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +0300997 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
998 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
999 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001000 /*
1001 f->f_lasti refers to the index of the last instruction,
1002 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001003
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001004 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001005 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 When the PREDICT() macros are enabled, some opcode pairs follow in
1008 direct succession without updating f->f_lasti. A successful
1009 prediction effectively links the two codes together as if they
1010 were a single new opcode; accordingly,f->f_lasti will point to
1011 the first code in the pair (for instance, GET_ITER followed by
1012 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001013 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001015 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001016 next_instr = first_instr;
1017 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001018 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1019 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001020 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 stack_pointer = f->f_stacktop;
1022 assert(stack_pointer != NULL);
1023 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001024 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001025
Yury Selivanoveb636452016-09-08 22:01:51 -07001026 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01001027 if (!throwflag && f->f_exc_type != NULL && f->f_exc_type != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 /* We were in an except handler when we left,
1029 restore the exception state which was put aside
1030 (see YIELD_VALUE). */
Benjamin Peterson87880242011-07-03 16:48:31 -05001031 swap_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 }
Benjamin Peterson87880242011-07-03 16:48:31 -05001033 else
1034 save_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001036
Tim Peters5ca576e2001-06-18 22:08:13 +00001037#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001038 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001039#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 why = WHY_NOT;
Guido van Rossumac7be682001-01-17 15:42:30 +00001042
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001043 if (throwflag) /* support for generator.throw() */
1044 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001045
Victor Stinnerace47d72013-07-18 01:41:08 +02001046#ifdef Py_DEBUG
1047 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001048 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001049 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +02001050 assert(!PyErr_Occurred());
1051#endif
1052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1055 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +02001056 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 /* Do periodic things. Doing this every time through
1059 the loop would add too much overhead, so we do it
1060 only every Nth instruction. We also do it if
1061 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1062 event needs attention (e.g. a signal handler or
1063 async I/O handler); see Py_AddPendingCall() and
1064 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 if (_Py_atomic_load_relaxed(&eval_breaker)) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001067 if (_Py_OPCODE(*next_instr) == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 /* Make the last opcode before
Ezio Melotti13925002011-03-16 11:05:33 +02001069 a try: finally: block uninterruptible. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 goto fast_next_opcode;
1071 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001073 if (Py_MakePendingCalls() < 0)
1074 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001076#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001077 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 /* Give another thread a chance */
1079 if (PyThreadState_Swap(NULL) != tstate)
1080 Py_FatalError("ceval: tstate mix-up");
1081 drop_gil(tstate);
1082
1083 /* Other threads may run now */
1084
1085 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001086
1087 /* Check if we should make a quick exit. */
1088 if (_Py_Finalizing && _Py_Finalizing != tstate) {
1089 drop_gil(tstate);
1090 PyThread_exit_thread();
1091 }
1092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 if (PyThreadState_Swap(tstate) != NULL)
1094 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001096#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 /* Check for asynchronous exceptions. */
1098 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001099 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 tstate->async_exc = NULL;
1101 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001102 PyErr_SetNone(exc);
1103 Py_DECREF(exc);
1104 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 }
1106 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 fast_next_opcode:
1109 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001110
Łukasz Langaa785c872016-09-09 17:37:37 -07001111 if (PyDTrace_LINE_ENABLED())
1112 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001117 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001118 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 /* see maybe_call_line_trace
1120 for expository comments */
1121 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 err = maybe_call_line_trace(tstate->c_tracefunc,
1124 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001125 tstate, f,
1126 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 /* Reload possibly changed frame fields */
1128 JUMPTO(f->f_lasti);
1129 if (f->f_stacktop != NULL) {
1130 stack_pointer = f->f_stacktop;
1131 f->f_stacktop = NULL;
1132 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001133 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001135 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001139
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001140 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001141 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001142#ifdef DYNAMIC_EXECUTION_PROFILE
1143#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 dxpairs[lastopcode][opcode]++;
1145 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001146#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001148#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001149
Guido van Rossum96a42c81992-01-12 02:29:51 +00001150#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 if (lltrace) {
1154 if (HAS_ARG(opcode)) {
1155 printf("%d: %d, %d\n",
1156 f->f_lasti, opcode, oparg);
1157 }
1158 else {
1159 printf("%d: %d\n",
1160 f->f_lasti, opcode);
1161 }
1162 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001163#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 /* BEWARE!
1168 It is essential that any operation that fails sets either
1169 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1170 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 TARGET(NOP)
1173 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001174
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001175 TARGET(LOAD_FAST) {
1176 PyObject *value = GETLOCAL(oparg);
1177 if (value == NULL) {
1178 format_exc_check_arg(PyExc_UnboundLocalError,
1179 UNBOUNDLOCAL_ERROR_MSG,
1180 PyTuple_GetItem(co->co_varnames, oparg));
1181 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001183 Py_INCREF(value);
1184 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001186 }
1187
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001188 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001189 TARGET(LOAD_CONST) {
1190 PyObject *value = GETITEM(consts, oparg);
1191 Py_INCREF(value);
1192 PUSH(value);
1193 FAST_DISPATCH();
1194 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001195
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001196 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001197 TARGET(STORE_FAST) {
1198 PyObject *value = POP();
1199 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001201 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001202
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001203 TARGET(POP_TOP) {
1204 PyObject *value = POP();
1205 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001207 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001208
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001209 TARGET(ROT_TWO) {
1210 PyObject *top = TOP();
1211 PyObject *second = SECOND();
1212 SET_TOP(second);
1213 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001215 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001216
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001217 TARGET(ROT_THREE) {
1218 PyObject *top = TOP();
1219 PyObject *second = SECOND();
1220 PyObject *third = THIRD();
1221 SET_TOP(second);
1222 SET_SECOND(third);
1223 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001225 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001226
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001227 TARGET(DUP_TOP) {
1228 PyObject *top = TOP();
1229 Py_INCREF(top);
1230 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001232 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001233
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001234 TARGET(DUP_TOP_TWO) {
1235 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001236 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001237 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001238 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001239 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001240 SET_TOP(top);
1241 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001242 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001243 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001244
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001245 TARGET(UNARY_POSITIVE) {
1246 PyObject *value = TOP();
1247 PyObject *res = PyNumber_Positive(value);
1248 Py_DECREF(value);
1249 SET_TOP(res);
1250 if (res == NULL)
1251 goto error;
1252 DISPATCH();
1253 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001254
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001255 TARGET(UNARY_NEGATIVE) {
1256 PyObject *value = TOP();
1257 PyObject *res = PyNumber_Negative(value);
1258 Py_DECREF(value);
1259 SET_TOP(res);
1260 if (res == NULL)
1261 goto error;
1262 DISPATCH();
1263 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001264
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001265 TARGET(UNARY_NOT) {
1266 PyObject *value = TOP();
1267 int err = PyObject_IsTrue(value);
1268 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 if (err == 0) {
1270 Py_INCREF(Py_True);
1271 SET_TOP(Py_True);
1272 DISPATCH();
1273 }
1274 else if (err > 0) {
1275 Py_INCREF(Py_False);
1276 SET_TOP(Py_False);
1277 err = 0;
1278 DISPATCH();
1279 }
1280 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001281 goto error;
1282 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001283
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001284 TARGET(UNARY_INVERT) {
1285 PyObject *value = TOP();
1286 PyObject *res = PyNumber_Invert(value);
1287 Py_DECREF(value);
1288 SET_TOP(res);
1289 if (res == NULL)
1290 goto error;
1291 DISPATCH();
1292 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001293
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001294 TARGET(BINARY_POWER) {
1295 PyObject *exp = POP();
1296 PyObject *base = TOP();
1297 PyObject *res = PyNumber_Power(base, exp, Py_None);
1298 Py_DECREF(base);
1299 Py_DECREF(exp);
1300 SET_TOP(res);
1301 if (res == NULL)
1302 goto error;
1303 DISPATCH();
1304 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001305
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001306 TARGET(BINARY_MULTIPLY) {
1307 PyObject *right = POP();
1308 PyObject *left = TOP();
1309 PyObject *res = PyNumber_Multiply(left, right);
1310 Py_DECREF(left);
1311 Py_DECREF(right);
1312 SET_TOP(res);
1313 if (res == NULL)
1314 goto error;
1315 DISPATCH();
1316 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001317
Benjamin Petersond51374e2014-04-09 23:55:56 -04001318 TARGET(BINARY_MATRIX_MULTIPLY) {
1319 PyObject *right = POP();
1320 PyObject *left = TOP();
1321 PyObject *res = PyNumber_MatrixMultiply(left, right);
1322 Py_DECREF(left);
1323 Py_DECREF(right);
1324 SET_TOP(res);
1325 if (res == NULL)
1326 goto error;
1327 DISPATCH();
1328 }
1329
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001330 TARGET(BINARY_TRUE_DIVIDE) {
1331 PyObject *divisor = POP();
1332 PyObject *dividend = TOP();
1333 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1334 Py_DECREF(dividend);
1335 Py_DECREF(divisor);
1336 SET_TOP(quotient);
1337 if (quotient == NULL)
1338 goto error;
1339 DISPATCH();
1340 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001341
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001342 TARGET(BINARY_FLOOR_DIVIDE) {
1343 PyObject *divisor = POP();
1344 PyObject *dividend = TOP();
1345 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1346 Py_DECREF(dividend);
1347 Py_DECREF(divisor);
1348 SET_TOP(quotient);
1349 if (quotient == NULL)
1350 goto error;
1351 DISPATCH();
1352 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001353
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001354 TARGET(BINARY_MODULO) {
1355 PyObject *divisor = POP();
1356 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001357 PyObject *res;
1358 if (PyUnicode_CheckExact(dividend) && (
1359 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1360 // fast path; string formatting, but not if the RHS is a str subclass
1361 // (see issue28598)
1362 res = PyUnicode_Format(dividend, divisor);
1363 } else {
1364 res = PyNumber_Remainder(dividend, divisor);
1365 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001366 Py_DECREF(divisor);
1367 Py_DECREF(dividend);
1368 SET_TOP(res);
1369 if (res == NULL)
1370 goto error;
1371 DISPATCH();
1372 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001373
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001374 TARGET(BINARY_ADD) {
1375 PyObject *right = POP();
1376 PyObject *left = TOP();
1377 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001378 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1379 CPython using bytecode, it is simply worthless.
1380 See http://bugs.python.org/issue21955 and
1381 http://bugs.python.org/issue10044 for the discussion. In short,
1382 no patch shown any impact on a realistic benchmark, only a minor
1383 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001384 if (PyUnicode_CheckExact(left) &&
1385 PyUnicode_CheckExact(right)) {
1386 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001387 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001388 }
1389 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001390 sum = PyNumber_Add(left, right);
1391 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001392 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001393 Py_DECREF(right);
1394 SET_TOP(sum);
1395 if (sum == NULL)
1396 goto error;
1397 DISPATCH();
1398 }
1399
1400 TARGET(BINARY_SUBTRACT) {
1401 PyObject *right = POP();
1402 PyObject *left = TOP();
1403 PyObject *diff = PyNumber_Subtract(left, right);
1404 Py_DECREF(right);
1405 Py_DECREF(left);
1406 SET_TOP(diff);
1407 if (diff == NULL)
1408 goto error;
1409 DISPATCH();
1410 }
1411
1412 TARGET(BINARY_SUBSCR) {
1413 PyObject *sub = POP();
1414 PyObject *container = TOP();
1415 PyObject *res = PyObject_GetItem(container, sub);
1416 Py_DECREF(container);
1417 Py_DECREF(sub);
1418 SET_TOP(res);
1419 if (res == NULL)
1420 goto error;
1421 DISPATCH();
1422 }
1423
1424 TARGET(BINARY_LSHIFT) {
1425 PyObject *right = POP();
1426 PyObject *left = TOP();
1427 PyObject *res = PyNumber_Lshift(left, right);
1428 Py_DECREF(left);
1429 Py_DECREF(right);
1430 SET_TOP(res);
1431 if (res == NULL)
1432 goto error;
1433 DISPATCH();
1434 }
1435
1436 TARGET(BINARY_RSHIFT) {
1437 PyObject *right = POP();
1438 PyObject *left = TOP();
1439 PyObject *res = PyNumber_Rshift(left, right);
1440 Py_DECREF(left);
1441 Py_DECREF(right);
1442 SET_TOP(res);
1443 if (res == NULL)
1444 goto error;
1445 DISPATCH();
1446 }
1447
1448 TARGET(BINARY_AND) {
1449 PyObject *right = POP();
1450 PyObject *left = TOP();
1451 PyObject *res = PyNumber_And(left, right);
1452 Py_DECREF(left);
1453 Py_DECREF(right);
1454 SET_TOP(res);
1455 if (res == NULL)
1456 goto error;
1457 DISPATCH();
1458 }
1459
1460 TARGET(BINARY_XOR) {
1461 PyObject *right = POP();
1462 PyObject *left = TOP();
1463 PyObject *res = PyNumber_Xor(left, right);
1464 Py_DECREF(left);
1465 Py_DECREF(right);
1466 SET_TOP(res);
1467 if (res == NULL)
1468 goto error;
1469 DISPATCH();
1470 }
1471
1472 TARGET(BINARY_OR) {
1473 PyObject *right = POP();
1474 PyObject *left = TOP();
1475 PyObject *res = PyNumber_Or(left, right);
1476 Py_DECREF(left);
1477 Py_DECREF(right);
1478 SET_TOP(res);
1479 if (res == NULL)
1480 goto error;
1481 DISPATCH();
1482 }
1483
1484 TARGET(LIST_APPEND) {
1485 PyObject *v = POP();
1486 PyObject *list = PEEK(oparg);
1487 int err;
1488 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001490 if (err != 0)
1491 goto error;
1492 PREDICT(JUMP_ABSOLUTE);
1493 DISPATCH();
1494 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001495
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001496 TARGET(SET_ADD) {
1497 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001498 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001499 int err;
1500 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001502 if (err != 0)
1503 goto error;
1504 PREDICT(JUMP_ABSOLUTE);
1505 DISPATCH();
1506 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001507
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001508 TARGET(INPLACE_POWER) {
1509 PyObject *exp = POP();
1510 PyObject *base = TOP();
1511 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1512 Py_DECREF(base);
1513 Py_DECREF(exp);
1514 SET_TOP(res);
1515 if (res == NULL)
1516 goto error;
1517 DISPATCH();
1518 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001519
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001520 TARGET(INPLACE_MULTIPLY) {
1521 PyObject *right = POP();
1522 PyObject *left = TOP();
1523 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1524 Py_DECREF(left);
1525 Py_DECREF(right);
1526 SET_TOP(res);
1527 if (res == NULL)
1528 goto error;
1529 DISPATCH();
1530 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001531
Benjamin Petersond51374e2014-04-09 23:55:56 -04001532 TARGET(INPLACE_MATRIX_MULTIPLY) {
1533 PyObject *right = POP();
1534 PyObject *left = TOP();
1535 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1536 Py_DECREF(left);
1537 Py_DECREF(right);
1538 SET_TOP(res);
1539 if (res == NULL)
1540 goto error;
1541 DISPATCH();
1542 }
1543
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001544 TARGET(INPLACE_TRUE_DIVIDE) {
1545 PyObject *divisor = POP();
1546 PyObject *dividend = TOP();
1547 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1548 Py_DECREF(dividend);
1549 Py_DECREF(divisor);
1550 SET_TOP(quotient);
1551 if (quotient == NULL)
1552 goto error;
1553 DISPATCH();
1554 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001555
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001556 TARGET(INPLACE_FLOOR_DIVIDE) {
1557 PyObject *divisor = POP();
1558 PyObject *dividend = TOP();
1559 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1560 Py_DECREF(dividend);
1561 Py_DECREF(divisor);
1562 SET_TOP(quotient);
1563 if (quotient == NULL)
1564 goto error;
1565 DISPATCH();
1566 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001567
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001568 TARGET(INPLACE_MODULO) {
1569 PyObject *right = POP();
1570 PyObject *left = TOP();
1571 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1572 Py_DECREF(left);
1573 Py_DECREF(right);
1574 SET_TOP(mod);
1575 if (mod == NULL)
1576 goto error;
1577 DISPATCH();
1578 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001579
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001580 TARGET(INPLACE_ADD) {
1581 PyObject *right = POP();
1582 PyObject *left = TOP();
1583 PyObject *sum;
1584 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1585 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001586 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001587 }
1588 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001589 sum = PyNumber_InPlaceAdd(left, right);
1590 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001591 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001592 Py_DECREF(right);
1593 SET_TOP(sum);
1594 if (sum == NULL)
1595 goto error;
1596 DISPATCH();
1597 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001598
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001599 TARGET(INPLACE_SUBTRACT) {
1600 PyObject *right = POP();
1601 PyObject *left = TOP();
1602 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1603 Py_DECREF(left);
1604 Py_DECREF(right);
1605 SET_TOP(diff);
1606 if (diff == NULL)
1607 goto error;
1608 DISPATCH();
1609 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001610
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001611 TARGET(INPLACE_LSHIFT) {
1612 PyObject *right = POP();
1613 PyObject *left = TOP();
1614 PyObject *res = PyNumber_InPlaceLshift(left, right);
1615 Py_DECREF(left);
1616 Py_DECREF(right);
1617 SET_TOP(res);
1618 if (res == NULL)
1619 goto error;
1620 DISPATCH();
1621 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001622
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001623 TARGET(INPLACE_RSHIFT) {
1624 PyObject *right = POP();
1625 PyObject *left = TOP();
1626 PyObject *res = PyNumber_InPlaceRshift(left, right);
1627 Py_DECREF(left);
1628 Py_DECREF(right);
1629 SET_TOP(res);
1630 if (res == NULL)
1631 goto error;
1632 DISPATCH();
1633 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001634
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001635 TARGET(INPLACE_AND) {
1636 PyObject *right = POP();
1637 PyObject *left = TOP();
1638 PyObject *res = PyNumber_InPlaceAnd(left, right);
1639 Py_DECREF(left);
1640 Py_DECREF(right);
1641 SET_TOP(res);
1642 if (res == NULL)
1643 goto error;
1644 DISPATCH();
1645 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001646
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001647 TARGET(INPLACE_XOR) {
1648 PyObject *right = POP();
1649 PyObject *left = TOP();
1650 PyObject *res = PyNumber_InPlaceXor(left, right);
1651 Py_DECREF(left);
1652 Py_DECREF(right);
1653 SET_TOP(res);
1654 if (res == NULL)
1655 goto error;
1656 DISPATCH();
1657 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001658
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001659 TARGET(INPLACE_OR) {
1660 PyObject *right = POP();
1661 PyObject *left = TOP();
1662 PyObject *res = PyNumber_InPlaceOr(left, right);
1663 Py_DECREF(left);
1664 Py_DECREF(right);
1665 SET_TOP(res);
1666 if (res == NULL)
1667 goto error;
1668 DISPATCH();
1669 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001670
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001671 TARGET(STORE_SUBSCR) {
1672 PyObject *sub = TOP();
1673 PyObject *container = SECOND();
1674 PyObject *v = THIRD();
1675 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 STACKADJ(-3);
Martin Panter95f53c12016-07-18 08:23:26 +00001677 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001678 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001680 Py_DECREF(container);
1681 Py_DECREF(sub);
1682 if (err != 0)
1683 goto error;
1684 DISPATCH();
1685 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001686
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001687 TARGET(STORE_ANNOTATION) {
1688 _Py_IDENTIFIER(__annotations__);
1689 PyObject *ann_dict;
1690 PyObject *ann = POP();
1691 PyObject *name = GETITEM(names, oparg);
1692 int err;
1693 if (f->f_locals == NULL) {
1694 PyErr_Format(PyExc_SystemError,
1695 "no locals found when storing annotation");
1696 Py_DECREF(ann);
1697 goto error;
1698 }
1699 /* first try to get __annotations__ from locals... */
1700 if (PyDict_CheckExact(f->f_locals)) {
1701 ann_dict = _PyDict_GetItemId(f->f_locals,
1702 &PyId___annotations__);
1703 if (ann_dict == NULL) {
1704 PyErr_SetString(PyExc_NameError,
1705 "__annotations__ not found");
1706 Py_DECREF(ann);
1707 goto error;
1708 }
1709 Py_INCREF(ann_dict);
1710 }
1711 else {
1712 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
1713 if (ann_str == NULL) {
1714 Py_DECREF(ann);
1715 goto error;
1716 }
1717 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
1718 if (ann_dict == NULL) {
1719 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
1720 PyErr_SetString(PyExc_NameError,
1721 "__annotations__ not found");
1722 }
1723 Py_DECREF(ann);
1724 goto error;
1725 }
1726 }
1727 /* ...if succeeded, __annotations__[name] = ann */
1728 if (PyDict_CheckExact(ann_dict)) {
1729 err = PyDict_SetItem(ann_dict, name, ann);
1730 }
1731 else {
1732 err = PyObject_SetItem(ann_dict, name, ann);
1733 }
1734 Py_DECREF(ann_dict);
Yury Selivanov50c584f2016-09-08 23:38:21 -07001735 Py_DECREF(ann);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001736 if (err != 0) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001737 goto error;
1738 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001739 DISPATCH();
1740 }
1741
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001742 TARGET(DELETE_SUBSCR) {
1743 PyObject *sub = TOP();
1744 PyObject *container = SECOND();
1745 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 STACKADJ(-2);
Martin Panter95f53c12016-07-18 08:23:26 +00001747 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001748 err = PyObject_DelItem(container, sub);
1749 Py_DECREF(container);
1750 Py_DECREF(sub);
1751 if (err != 0)
1752 goto error;
1753 DISPATCH();
1754 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001755
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001756 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001757 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001758 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001759 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001760 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001761 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 PyErr_SetString(PyExc_RuntimeError,
1763 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001764 Py_DECREF(value);
1765 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001767 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001768 Py_DECREF(value);
1769 if (res == NULL)
1770 goto error;
1771 Py_DECREF(res);
1772 DISPATCH();
1773 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001774
Thomas Wouters434d0822000-08-24 20:11:32 +00001775#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001777#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001778 TARGET(RAISE_VARARGS) {
1779 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001780 switch (oparg) {
1781 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001782 cause = POP(); /* cause */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001784 exc = POP(); /* exc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 case 0: /* Fallthrough */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001786 if (do_raise(exc, cause)) {
1787 why = WHY_EXCEPTION;
1788 goto fast_block_end;
1789 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 break;
1791 default:
1792 PyErr_SetString(PyExc_SystemError,
1793 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 break;
1795 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001796 goto error;
1797 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001798
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001799 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 retval = POP();
1801 why = WHY_RETURN;
1802 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001803 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001804
Yury Selivanov75445082015-05-11 22:57:16 -04001805 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001806 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001807 PyObject *iter = NULL;
1808 PyObject *awaitable = NULL;
1809 PyObject *obj = TOP();
1810 PyTypeObject *type = Py_TYPE(obj);
1811
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001812 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001813 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001814 }
Yury Selivanov75445082015-05-11 22:57:16 -04001815
1816 if (getter != NULL) {
1817 iter = (*getter)(obj);
1818 Py_DECREF(obj);
1819 if (iter == NULL) {
1820 SET_TOP(NULL);
1821 goto error;
1822 }
1823 }
1824 else {
1825 SET_TOP(NULL);
1826 PyErr_Format(
1827 PyExc_TypeError,
1828 "'async for' requires an object with "
1829 "__aiter__ method, got %.100s",
1830 type->tp_name);
1831 Py_DECREF(obj);
1832 goto error;
1833 }
1834
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001835 if (Py_TYPE(iter)->tp_as_async != NULL &&
1836 Py_TYPE(iter)->tp_as_async->am_anext != NULL) {
1837
1838 /* Starting with CPython 3.5.2 __aiter__ should return
1839 asynchronous iterators directly (not awaitables that
1840 resolve to asynchronous iterators.)
1841
1842 Therefore, we check if the object that was returned
1843 from __aiter__ has an __anext__ method. If it does,
1844 we wrap it in an awaitable that resolves to `iter`.
1845
1846 See http://bugs.python.org/issue27243 for more
1847 details.
1848 */
1849
1850 PyObject *wrapper = _PyAIterWrapper_New(iter);
1851 Py_DECREF(iter);
1852 SET_TOP(wrapper);
1853 DISPATCH();
1854 }
1855
Yury Selivanov5376ba92015-06-22 12:19:30 -04001856 awaitable = _PyCoro_GetAwaitableIter(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001857 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001858 _PyErr_FormatFromCause(
Yury Selivanov75445082015-05-11 22:57:16 -04001859 PyExc_TypeError,
1860 "'async for' received an invalid object "
1861 "from __aiter__: %.100s",
1862 Py_TYPE(iter)->tp_name);
1863
Yury Selivanov398ff912017-03-02 22:20:00 -05001864 SET_TOP(NULL);
Yury Selivanov75445082015-05-11 22:57:16 -04001865 Py_DECREF(iter);
1866 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001867 } else {
Yury Selivanov75445082015-05-11 22:57:16 -04001868 Py_DECREF(iter);
1869
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001870 if (PyErr_WarnFormat(
Yury Selivanov2edd8a12016-11-08 15:13:07 -05001871 PyExc_DeprecationWarning, 1,
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001872 "'%.100s' implements legacy __aiter__ protocol; "
1873 "__aiter__ should return an asynchronous "
1874 "iterator, not awaitable",
1875 type->tp_name))
1876 {
1877 /* Warning was converted to an error. */
1878 Py_DECREF(awaitable);
1879 SET_TOP(NULL);
1880 goto error;
1881 }
1882 }
1883
Yury Selivanov75445082015-05-11 22:57:16 -04001884 SET_TOP(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001885 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001886 DISPATCH();
1887 }
1888
1889 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001890 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001891 PyObject *next_iter = NULL;
1892 PyObject *awaitable = NULL;
1893 PyObject *aiter = TOP();
1894 PyTypeObject *type = Py_TYPE(aiter);
1895
Yury Selivanoveb636452016-09-08 22:01:51 -07001896 if (PyAsyncGen_CheckExact(aiter)) {
1897 awaitable = type->tp_as_async->am_anext(aiter);
1898 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001899 goto error;
1900 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001901 } else {
1902 if (type->tp_as_async != NULL){
1903 getter = type->tp_as_async->am_anext;
1904 }
Yury Selivanov75445082015-05-11 22:57:16 -04001905
Yury Selivanoveb636452016-09-08 22:01:51 -07001906 if (getter != NULL) {
1907 next_iter = (*getter)(aiter);
1908 if (next_iter == NULL) {
1909 goto error;
1910 }
1911 }
1912 else {
1913 PyErr_Format(
1914 PyExc_TypeError,
1915 "'async for' requires an iterator with "
1916 "__anext__ method, got %.100s",
1917 type->tp_name);
1918 goto error;
1919 }
Yury Selivanov75445082015-05-11 22:57:16 -04001920
Yury Selivanoveb636452016-09-08 22:01:51 -07001921 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1922 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001923 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001924 PyExc_TypeError,
1925 "'async for' received an invalid object "
1926 "from __anext__: %.100s",
1927 Py_TYPE(next_iter)->tp_name);
1928
1929 Py_DECREF(next_iter);
1930 goto error;
1931 } else {
1932 Py_DECREF(next_iter);
1933 }
1934 }
Yury Selivanov75445082015-05-11 22:57:16 -04001935
1936 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001937 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001938 DISPATCH();
1939 }
1940
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001941 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001942 TARGET(GET_AWAITABLE) {
1943 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001944 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001945
1946 Py_DECREF(iterable);
1947
Yury Selivanovc724bae2016-03-02 11:30:46 -05001948 if (iter != NULL && PyCoro_CheckExact(iter)) {
1949 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1950 if (yf != NULL) {
1951 /* `iter` is a coroutine object that is being
1952 awaited, `yf` is a pointer to the current awaitable
1953 being awaited on. */
1954 Py_DECREF(yf);
1955 Py_CLEAR(iter);
1956 PyErr_SetString(
1957 PyExc_RuntimeError,
1958 "coroutine is being awaited already");
1959 /* The code below jumps to `error` if `iter` is NULL. */
1960 }
1961 }
1962
Yury Selivanov75445082015-05-11 22:57:16 -04001963 SET_TOP(iter); /* Even if it's NULL */
1964
1965 if (iter == NULL) {
1966 goto error;
1967 }
1968
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001969 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001970 DISPATCH();
1971 }
1972
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001973 TARGET(YIELD_FROM) {
1974 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001975 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001976 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001977 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1978 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001979 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001980 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001981 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001982 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001983 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001984 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001985 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001986 Py_DECREF(v);
1987 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001988 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001989 if (tstate->c_tracefunc != NULL
1990 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001991 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001992 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001993 if (err < 0)
1994 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001995 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001996 SET_TOP(val);
1997 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001998 }
Martin Panter95f53c12016-07-18 08:23:26 +00001999 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002000 f->f_stacktop = stack_pointer;
2001 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002002 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002003 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002004 f->f_lasti -= sizeof(_Py_CODEUNIT);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002005 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002006 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002007
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002008 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002010
2011 if (co->co_flags & CO_ASYNC_GENERATOR) {
2012 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2013 Py_DECREF(retval);
2014 if (w == NULL) {
2015 retval = NULL;
2016 goto error;
2017 }
2018 retval = w;
2019 }
2020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 f->f_stacktop = stack_pointer;
2022 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002023 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002024 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002025
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002026 TARGET(POP_EXCEPT) {
2027 PyTryBlock *b = PyFrame_BlockPop(f);
2028 if (b->b_type != EXCEPT_HANDLER) {
2029 PyErr_SetString(PyExc_SystemError,
2030 "popped block is not an except handler");
2031 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002032 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002033 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002034 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002035 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002036
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002037 PREDICTED(POP_BLOCK);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002038 TARGET(POP_BLOCK) {
2039 PyTryBlock *b = PyFrame_BlockPop(f);
2040 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002041 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002042 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002044 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002045 TARGET(END_FINALLY) {
2046 PyObject *status = POP();
2047 if (PyLong_Check(status)) {
2048 why = (enum why_code) PyLong_AS_LONG(status);
2049 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002050 if (why == WHY_RETURN ||
2051 why == WHY_CONTINUE)
2052 retval = POP();
2053 if (why == WHY_SILENCED) {
2054 /* An exception was silenced by 'with', we must
2055 manually unwind the EXCEPT_HANDLER block which was
2056 created when the exception was caught, otherwise
2057 the stack will be in an inconsistent state. */
2058 PyTryBlock *b = PyFrame_BlockPop(f);
2059 assert(b->b_type == EXCEPT_HANDLER);
2060 UNWIND_EXCEPT_HANDLER(b);
2061 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002062 Py_DECREF(status);
2063 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002064 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002065 Py_DECREF(status);
2066 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002068 else if (PyExceptionClass_Check(status)) {
2069 PyObject *exc = POP();
2070 PyObject *tb = POP();
2071 PyErr_Restore(status, exc, tb);
2072 why = WHY_EXCEPTION;
2073 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002075 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 PyErr_SetString(PyExc_SystemError,
2077 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002078 Py_DECREF(status);
2079 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002080 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002081 Py_DECREF(status);
2082 DISPATCH();
2083 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002084
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002085 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002086 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002087
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002088 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002089 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002090 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
2091 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002092 PyErr_SetString(PyExc_NameError,
2093 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002094 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002095 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002096 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002097 }
2098 else {
2099 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2100 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002101 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002102 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2103 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002104 if (PyErr_ExceptionMatches(PyExc_KeyError))
2105 PyErr_SetString(PyExc_NameError,
2106 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002107 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002108 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002109 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002110 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002111 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002112 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002113
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002114 TARGET(STORE_NAME) {
2115 PyObject *name = GETITEM(names, oparg);
2116 PyObject *v = POP();
2117 PyObject *ns = f->f_locals;
2118 int err;
2119 if (ns == NULL) {
2120 PyErr_Format(PyExc_SystemError,
2121 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002123 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002125 if (PyDict_CheckExact(ns))
2126 err = PyDict_SetItem(ns, name, v);
2127 else
2128 err = PyObject_SetItem(ns, name, v);
2129 Py_DECREF(v);
2130 if (err != 0)
2131 goto error;
2132 DISPATCH();
2133 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002134
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002135 TARGET(DELETE_NAME) {
2136 PyObject *name = GETITEM(names, oparg);
2137 PyObject *ns = f->f_locals;
2138 int err;
2139 if (ns == NULL) {
2140 PyErr_Format(PyExc_SystemError,
2141 "no locals when deleting %R", name);
2142 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002143 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002144 err = PyObject_DelItem(ns, name);
2145 if (err != 0) {
2146 format_exc_check_arg(PyExc_NameError,
2147 NAME_ERROR_MSG,
2148 name);
2149 goto error;
2150 }
2151 DISPATCH();
2152 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002153
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002154 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002155 TARGET(UNPACK_SEQUENCE) {
2156 PyObject *seq = POP(), *item, **items;
2157 if (PyTuple_CheckExact(seq) &&
2158 PyTuple_GET_SIZE(seq) == oparg) {
2159 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002160 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002161 item = items[oparg];
2162 Py_INCREF(item);
2163 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002164 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002165 } else if (PyList_CheckExact(seq) &&
2166 PyList_GET_SIZE(seq) == oparg) {
2167 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002168 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002169 item = items[oparg];
2170 Py_INCREF(item);
2171 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002172 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002173 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002174 stack_pointer + oparg)) {
2175 STACKADJ(oparg);
2176 } else {
2177 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002178 Py_DECREF(seq);
2179 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002180 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002181 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002182 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002183 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002184
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002185 TARGET(UNPACK_EX) {
2186 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2187 PyObject *seq = POP();
2188
2189 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2190 stack_pointer + totalargs)) {
2191 stack_pointer += totalargs;
2192 } else {
2193 Py_DECREF(seq);
2194 goto error;
2195 }
2196 Py_DECREF(seq);
2197 DISPATCH();
2198 }
2199
2200 TARGET(STORE_ATTR) {
2201 PyObject *name = GETITEM(names, oparg);
2202 PyObject *owner = TOP();
2203 PyObject *v = SECOND();
2204 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002205 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002206 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002207 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002208 Py_DECREF(owner);
2209 if (err != 0)
2210 goto error;
2211 DISPATCH();
2212 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002213
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002214 TARGET(DELETE_ATTR) {
2215 PyObject *name = GETITEM(names, oparg);
2216 PyObject *owner = POP();
2217 int err;
2218 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2219 Py_DECREF(owner);
2220 if (err != 0)
2221 goto error;
2222 DISPATCH();
2223 }
2224
2225 TARGET(STORE_GLOBAL) {
2226 PyObject *name = GETITEM(names, oparg);
2227 PyObject *v = POP();
2228 int err;
2229 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002231 if (err != 0)
2232 goto error;
2233 DISPATCH();
2234 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002235
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002236 TARGET(DELETE_GLOBAL) {
2237 PyObject *name = GETITEM(names, oparg);
2238 int err;
2239 err = PyDict_DelItem(f->f_globals, name);
2240 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002241 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002242 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002243 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002244 }
2245 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002246 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002247
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002248 TARGET(LOAD_NAME) {
2249 PyObject *name = GETITEM(names, oparg);
2250 PyObject *locals = f->f_locals;
2251 PyObject *v;
2252 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002254 "no locals when loading %R", name);
2255 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002256 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002257 if (PyDict_CheckExact(locals)) {
2258 v = PyDict_GetItem(locals, name);
2259 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002260 }
2261 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002262 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002263 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002264 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2265 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002266 PyErr_Clear();
2267 }
2268 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002269 if (v == NULL) {
2270 v = PyDict_GetItem(f->f_globals, name);
2271 Py_XINCREF(v);
2272 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002273 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002274 v = PyDict_GetItem(f->f_builtins, name);
2275 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002276 format_exc_check_arg(
2277 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002278 NAME_ERROR_MSG, name);
2279 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002280 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002281 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002282 }
2283 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002284 v = PyObject_GetItem(f->f_builtins, name);
2285 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002286 if (PyErr_ExceptionMatches(PyExc_KeyError))
2287 format_exc_check_arg(
2288 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002289 NAME_ERROR_MSG, name);
2290 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002291 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002292 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002293 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002295 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002296 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002297 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002298
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002299 TARGET(LOAD_GLOBAL) {
2300 PyObject *name = GETITEM(names, oparg);
2301 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002302 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002303 && PyDict_CheckExact(f->f_builtins))
2304 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002305 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002306 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002307 name);
2308 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002309 if (!_PyErr_OCCURRED()) {
2310 /* _PyDict_LoadGlobal() returns NULL without raising
2311 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002312 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002313 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002314 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002315 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002317 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002319 else {
2320 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002321
2322 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002323 v = PyObject_GetItem(f->f_globals, name);
2324 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002325 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2326 goto error;
2327 PyErr_Clear();
2328
Victor Stinnerb4efc962015-11-20 09:24:02 +01002329 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002330 v = PyObject_GetItem(f->f_builtins, name);
2331 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002332 if (PyErr_ExceptionMatches(PyExc_KeyError))
2333 format_exc_check_arg(
2334 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002335 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002336 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002337 }
2338 }
2339 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002340 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002341 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002342 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002343
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002344 TARGET(DELETE_FAST) {
2345 PyObject *v = GETLOCAL(oparg);
2346 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002347 SETLOCAL(oparg, NULL);
2348 DISPATCH();
2349 }
2350 format_exc_check_arg(
2351 PyExc_UnboundLocalError,
2352 UNBOUNDLOCAL_ERROR_MSG,
2353 PyTuple_GetItem(co->co_varnames, oparg)
2354 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002355 goto error;
2356 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002357
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002358 TARGET(DELETE_DEREF) {
2359 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002360 PyObject *oldobj = PyCell_GET(cell);
2361 if (oldobj != NULL) {
2362 PyCell_SET(cell, NULL);
2363 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002364 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002365 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002366 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002367 goto error;
2368 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002369
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002370 TARGET(LOAD_CLOSURE) {
2371 PyObject *cell = freevars[oparg];
2372 Py_INCREF(cell);
2373 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002374 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002375 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002376
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002377 TARGET(LOAD_CLASSDEREF) {
2378 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002379 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002380 assert(locals);
2381 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2382 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2383 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2384 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2385 if (PyDict_CheckExact(locals)) {
2386 value = PyDict_GetItem(locals, name);
2387 Py_XINCREF(value);
2388 }
2389 else {
2390 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002391 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002392 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2393 goto error;
2394 PyErr_Clear();
2395 }
2396 }
2397 if (!value) {
2398 PyObject *cell = freevars[oparg];
2399 value = PyCell_GET(cell);
2400 if (value == NULL) {
2401 format_exc_unbound(co, oparg);
2402 goto error;
2403 }
2404 Py_INCREF(value);
2405 }
2406 PUSH(value);
2407 DISPATCH();
2408 }
2409
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002410 TARGET(LOAD_DEREF) {
2411 PyObject *cell = freevars[oparg];
2412 PyObject *value = PyCell_GET(cell);
2413 if (value == NULL) {
2414 format_exc_unbound(co, oparg);
2415 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002417 Py_INCREF(value);
2418 PUSH(value);
2419 DISPATCH();
2420 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002421
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002422 TARGET(STORE_DEREF) {
2423 PyObject *v = POP();
2424 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002425 PyObject *oldobj = PyCell_GET(cell);
2426 PyCell_SET(cell, v);
2427 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002428 DISPATCH();
2429 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002430
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002431 TARGET(BUILD_STRING) {
2432 PyObject *str;
2433 PyObject *empty = PyUnicode_New(0, 0);
2434 if (empty == NULL) {
2435 goto error;
2436 }
2437 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2438 Py_DECREF(empty);
2439 if (str == NULL)
2440 goto error;
2441 while (--oparg >= 0) {
2442 PyObject *item = POP();
2443 Py_DECREF(item);
2444 }
2445 PUSH(str);
2446 DISPATCH();
2447 }
2448
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002449 TARGET(BUILD_TUPLE) {
2450 PyObject *tup = PyTuple_New(oparg);
2451 if (tup == NULL)
2452 goto error;
2453 while (--oparg >= 0) {
2454 PyObject *item = POP();
2455 PyTuple_SET_ITEM(tup, oparg, item);
2456 }
2457 PUSH(tup);
2458 DISPATCH();
2459 }
2460
2461 TARGET(BUILD_LIST) {
2462 PyObject *list = PyList_New(oparg);
2463 if (list == NULL)
2464 goto error;
2465 while (--oparg >= 0) {
2466 PyObject *item = POP();
2467 PyList_SET_ITEM(list, oparg, item);
2468 }
2469 PUSH(list);
2470 DISPATCH();
2471 }
2472
Serhiy Storchaka73442852016-10-02 10:33:46 +03002473 TARGET(BUILD_TUPLE_UNPACK_WITH_CALL)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002474 TARGET(BUILD_TUPLE_UNPACK)
2475 TARGET(BUILD_LIST_UNPACK) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002476 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002477 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002478 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002479 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002480
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002481 if (sum == NULL)
2482 goto error;
2483
2484 for (i = oparg; i > 0; i--) {
2485 PyObject *none_val;
2486
2487 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2488 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002489 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
2490 PyErr_ExceptionMatches(PyExc_TypeError)) {
2491 PyObject *func = PEEK(1 + oparg);
2492 PyErr_Format(PyExc_TypeError,
2493 "%.200s%.200s argument after * "
2494 "must be an iterable, not %.200s",
2495 PyEval_GetFuncName(func),
2496 PyEval_GetFuncDesc(func),
2497 PEEK(i)->ob_type->tp_name);
2498 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002499 Py_DECREF(sum);
2500 goto error;
2501 }
2502 Py_DECREF(none_val);
2503 }
2504
2505 if (convert_to_tuple) {
2506 return_value = PyList_AsTuple(sum);
2507 Py_DECREF(sum);
2508 if (return_value == NULL)
2509 goto error;
2510 }
2511 else {
2512 return_value = sum;
2513 }
2514
2515 while (oparg--)
2516 Py_DECREF(POP());
2517 PUSH(return_value);
2518 DISPATCH();
2519 }
2520
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002521 TARGET(BUILD_SET) {
2522 PyObject *set = PySet_New(NULL);
2523 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002524 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002525 if (set == NULL)
2526 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002527 for (i = oparg; i > 0; i--) {
2528 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002529 if (err == 0)
2530 err = PySet_Add(set, item);
2531 Py_DECREF(item);
2532 }
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002533 STACKADJ(-oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002534 if (err != 0) {
2535 Py_DECREF(set);
2536 goto error;
2537 }
2538 PUSH(set);
2539 DISPATCH();
2540 }
2541
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002542 TARGET(BUILD_SET_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002543 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002544 PyObject *sum = PySet_New(NULL);
2545 if (sum == NULL)
2546 goto error;
2547
2548 for (i = oparg; i > 0; i--) {
2549 if (_PySet_Update(sum, PEEK(i)) < 0) {
2550 Py_DECREF(sum);
2551 goto error;
2552 }
2553 }
2554
2555 while (oparg--)
2556 Py_DECREF(POP());
2557 PUSH(sum);
2558 DISPATCH();
2559 }
2560
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002561 TARGET(BUILD_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002562 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002563 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2564 if (map == NULL)
2565 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002566 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002567 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002568 PyObject *key = PEEK(2*i);
2569 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002570 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002571 if (err != 0) {
2572 Py_DECREF(map);
2573 goto error;
2574 }
2575 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002576
2577 while (oparg--) {
2578 Py_DECREF(POP());
2579 Py_DECREF(POP());
2580 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002581 PUSH(map);
2582 DISPATCH();
2583 }
2584
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002585 TARGET(SETUP_ANNOTATIONS) {
2586 _Py_IDENTIFIER(__annotations__);
2587 int err;
2588 PyObject *ann_dict;
2589 if (f->f_locals == NULL) {
2590 PyErr_Format(PyExc_SystemError,
2591 "no locals found when setting up annotations");
2592 goto error;
2593 }
2594 /* check if __annotations__ in locals()... */
2595 if (PyDict_CheckExact(f->f_locals)) {
2596 ann_dict = _PyDict_GetItemId(f->f_locals,
2597 &PyId___annotations__);
2598 if (ann_dict == NULL) {
2599 /* ...if not, create a new one */
2600 ann_dict = PyDict_New();
2601 if (ann_dict == NULL) {
2602 goto error;
2603 }
2604 err = _PyDict_SetItemId(f->f_locals,
2605 &PyId___annotations__, ann_dict);
2606 Py_DECREF(ann_dict);
2607 if (err != 0) {
2608 goto error;
2609 }
2610 }
2611 }
2612 else {
2613 /* do the same if locals() is not a dict */
2614 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2615 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002616 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002617 }
2618 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2619 if (ann_dict == NULL) {
2620 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2621 goto error;
2622 }
2623 PyErr_Clear();
2624 ann_dict = PyDict_New();
2625 if (ann_dict == NULL) {
2626 goto error;
2627 }
2628 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2629 Py_DECREF(ann_dict);
2630 if (err != 0) {
2631 goto error;
2632 }
2633 }
2634 else {
2635 Py_DECREF(ann_dict);
2636 }
2637 }
2638 DISPATCH();
2639 }
2640
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002641 TARGET(BUILD_CONST_KEY_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002642 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002643 PyObject *map;
2644 PyObject *keys = TOP();
2645 if (!PyTuple_CheckExact(keys) ||
2646 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2647 PyErr_SetString(PyExc_SystemError,
2648 "bad BUILD_CONST_KEY_MAP keys argument");
2649 goto error;
2650 }
2651 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2652 if (map == NULL) {
2653 goto error;
2654 }
2655 for (i = oparg; i > 0; i--) {
2656 int err;
2657 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2658 PyObject *value = PEEK(i + 1);
2659 err = PyDict_SetItem(map, key, value);
2660 if (err != 0) {
2661 Py_DECREF(map);
2662 goto error;
2663 }
2664 }
2665
2666 Py_DECREF(POP());
2667 while (oparg--) {
2668 Py_DECREF(POP());
2669 }
2670 PUSH(map);
2671 DISPATCH();
2672 }
2673
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002674 TARGET(BUILD_MAP_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002675 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002676 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002677 if (sum == NULL)
2678 goto error;
2679
2680 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002681 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002682 if (PyDict_Update(sum, arg) < 0) {
2683 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2684 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002685 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002686 arg->ob_type->tp_name);
2687 }
2688 Py_DECREF(sum);
2689 goto error;
2690 }
2691 }
2692
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002693 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002694 Py_DECREF(POP());
2695 PUSH(sum);
2696 DISPATCH();
2697 }
2698
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002699 TARGET(BUILD_MAP_UNPACK_WITH_CALL) {
2700 Py_ssize_t i;
2701 PyObject *sum = PyDict_New();
2702 if (sum == NULL)
2703 goto error;
2704
2705 for (i = oparg; i > 0; i--) {
2706 PyObject *arg = PEEK(i);
2707 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
2708 PyObject *func = PEEK(2 + oparg);
2709 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2710 PyErr_Format(PyExc_TypeError,
2711 "%.200s%.200s argument after ** "
2712 "must be a mapping, not %.200s",
2713 PyEval_GetFuncName(func),
2714 PyEval_GetFuncDesc(func),
2715 arg->ob_type->tp_name);
2716 }
2717 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2718 PyObject *exc, *val, *tb;
2719 PyErr_Fetch(&exc, &val, &tb);
2720 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
2721 PyObject *key = PyTuple_GET_ITEM(val, 0);
2722 if (!PyUnicode_Check(key)) {
2723 PyErr_Format(PyExc_TypeError,
2724 "%.200s%.200s keywords must be strings",
2725 PyEval_GetFuncName(func),
2726 PyEval_GetFuncDesc(func));
2727 } else {
2728 PyErr_Format(PyExc_TypeError,
2729 "%.200s%.200s got multiple "
2730 "values for keyword argument '%U'",
2731 PyEval_GetFuncName(func),
2732 PyEval_GetFuncDesc(func),
2733 key);
2734 }
2735 Py_XDECREF(exc);
2736 Py_XDECREF(val);
2737 Py_XDECREF(tb);
2738 }
2739 else {
2740 PyErr_Restore(exc, val, tb);
2741 }
2742 }
2743 Py_DECREF(sum);
2744 goto error;
2745 }
2746 }
2747
2748 while (oparg--)
2749 Py_DECREF(POP());
2750 PUSH(sum);
2751 DISPATCH();
2752 }
2753
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002754 TARGET(MAP_ADD) {
2755 PyObject *key = TOP();
2756 PyObject *value = SECOND();
2757 PyObject *map;
2758 int err;
2759 STACKADJ(-2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002760 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002761 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002762 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002763 Py_DECREF(value);
2764 Py_DECREF(key);
2765 if (err != 0)
2766 goto error;
2767 PREDICT(JUMP_ABSOLUTE);
2768 DISPATCH();
2769 }
2770
2771 TARGET(LOAD_ATTR) {
2772 PyObject *name = GETITEM(names, oparg);
2773 PyObject *owner = TOP();
2774 PyObject *res = PyObject_GetAttr(owner, name);
2775 Py_DECREF(owner);
2776 SET_TOP(res);
2777 if (res == NULL)
2778 goto error;
2779 DISPATCH();
2780 }
2781
2782 TARGET(COMPARE_OP) {
2783 PyObject *right = POP();
2784 PyObject *left = TOP();
2785 PyObject *res = cmp_outcome(oparg, left, right);
2786 Py_DECREF(left);
2787 Py_DECREF(right);
2788 SET_TOP(res);
2789 if (res == NULL)
2790 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002791 PREDICT(POP_JUMP_IF_FALSE);
2792 PREDICT(POP_JUMP_IF_TRUE);
2793 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002794 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002795
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002796 TARGET(IMPORT_NAME) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002797 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002798 PyObject *fromlist = POP();
2799 PyObject *level = TOP();
2800 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002801 res = import_name(f, name, fromlist, level);
2802 Py_DECREF(level);
2803 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002804 SET_TOP(res);
2805 if (res == NULL)
2806 goto error;
2807 DISPATCH();
2808 }
2809
2810 TARGET(IMPORT_STAR) {
2811 PyObject *from = POP(), *locals;
2812 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002813 if (PyFrame_FastToLocalsWithError(f) < 0) {
2814 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002815 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002816 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002817
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002818 locals = f->f_locals;
2819 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002820 PyErr_SetString(PyExc_SystemError,
2821 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002822 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002823 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002825 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002826 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002827 Py_DECREF(from);
2828 if (err != 0)
2829 goto error;
2830 DISPATCH();
2831 }
Guido van Rossum25831651993-05-19 14:50:45 +00002832
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002833 TARGET(IMPORT_FROM) {
2834 PyObject *name = GETITEM(names, oparg);
2835 PyObject *from = TOP();
2836 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002837 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002838 PUSH(res);
2839 if (res == NULL)
2840 goto error;
2841 DISPATCH();
2842 }
Thomas Wouters52152252000-08-17 22:55:00 +00002843
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002844 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002845 JUMPBY(oparg);
2846 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002847 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002848
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002849 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002850 TARGET(POP_JUMP_IF_FALSE) {
2851 PyObject *cond = POP();
2852 int err;
2853 if (cond == Py_True) {
2854 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002855 FAST_DISPATCH();
2856 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002857 if (cond == Py_False) {
2858 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002859 JUMPTO(oparg);
2860 FAST_DISPATCH();
2861 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002862 err = PyObject_IsTrue(cond);
2863 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002864 if (err > 0)
2865 err = 0;
2866 else if (err == 0)
2867 JUMPTO(oparg);
2868 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002869 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002871 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002872
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002873 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002874 TARGET(POP_JUMP_IF_TRUE) {
2875 PyObject *cond = POP();
2876 int err;
2877 if (cond == Py_False) {
2878 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 FAST_DISPATCH();
2880 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002881 if (cond == Py_True) {
2882 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002883 JUMPTO(oparg);
2884 FAST_DISPATCH();
2885 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002886 err = PyObject_IsTrue(cond);
2887 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 if (err > 0) {
2889 err = 0;
2890 JUMPTO(oparg);
2891 }
2892 else if (err == 0)
2893 ;
2894 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002895 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002896 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002897 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002898
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002899 TARGET(JUMP_IF_FALSE_OR_POP) {
2900 PyObject *cond = TOP();
2901 int err;
2902 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002904 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 FAST_DISPATCH();
2906 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002907 if (cond == Py_False) {
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);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002912 if (err > 0) {
2913 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002914 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002915 err = 0;
2916 }
2917 else if (err == 0)
2918 JUMPTO(oparg);
2919 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002920 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002921 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002922 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002923
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002924 TARGET(JUMP_IF_TRUE_OR_POP) {
2925 PyObject *cond = TOP();
2926 int err;
2927 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002928 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002929 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002930 FAST_DISPATCH();
2931 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002932 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002933 JUMPTO(oparg);
2934 FAST_DISPATCH();
2935 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002936 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002937 if (err > 0) {
2938 err = 0;
2939 JUMPTO(oparg);
2940 }
2941 else if (err == 0) {
2942 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002943 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002944 }
2945 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002946 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002947 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002948 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002949
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002950 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002951 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002952 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002953#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002954 /* Enabling this path speeds-up all while and for-loops by bypassing
2955 the per-loop checks for signals. By default, this should be turned-off
2956 because it prevents detection of a control-break in tight loops like
2957 "while 1: pass". Compile with this option turned-on when you need
2958 the speed-up and do not need break checking inside tight loops (ones
2959 that contain only instructions ending with FAST_DISPATCH).
2960 */
2961 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002962#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002963 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002964#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002965 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002966
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002967 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002968 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002969 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002970 PyObject *iter = PyObject_GetIter(iterable);
2971 Py_DECREF(iterable);
2972 SET_TOP(iter);
2973 if (iter == NULL)
2974 goto error;
2975 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002976 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002977 DISPATCH();
2978 }
2979
2980 TARGET(GET_YIELD_FROM_ITER) {
2981 /* before: [obj]; after [getiter(obj)] */
2982 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002983 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002984 if (PyCoro_CheckExact(iterable)) {
2985 /* `iterable` is a coroutine */
2986 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2987 /* and it is used in a 'yield from' expression of a
2988 regular generator. */
2989 Py_DECREF(iterable);
2990 SET_TOP(NULL);
2991 PyErr_SetString(PyExc_TypeError,
2992 "cannot 'yield from' a coroutine object "
2993 "in a non-coroutine generator");
2994 goto error;
2995 }
2996 }
2997 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002998 /* `iterable` is not a generator. */
2999 iter = PyObject_GetIter(iterable);
3000 Py_DECREF(iterable);
3001 SET_TOP(iter);
3002 if (iter == NULL)
3003 goto error;
3004 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003005 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003006 DISPATCH();
3007 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003008
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003009 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003010 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003011 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003012 PyObject *iter = TOP();
3013 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3014 if (next != NULL) {
3015 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003016 PREDICT(STORE_FAST);
3017 PREDICT(UNPACK_SEQUENCE);
3018 DISPATCH();
3019 }
3020 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003021 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
3022 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08003023 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003024 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003025 PyErr_Clear();
3026 }
3027 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003028 STACKADJ(-1);
3029 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003030 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003031 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003032 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003033 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003034
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003035 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003036 why = WHY_BREAK;
3037 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003038 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003039
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003040 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003041 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003042 if (retval == NULL)
3043 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003044 why = WHY_CONTINUE;
3045 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003046 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003047
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003048 TARGET(SETUP_LOOP)
3049 TARGET(SETUP_EXCEPT)
3050 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003051 /* NOTE: If you add any new block-setup opcodes that
3052 are not try/except/finally handlers, you may need
3053 to update the PyGen_NeedsFinalizing() function.
3054 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003056 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
3057 STACK_LEVEL());
3058 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003059 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003060
Yury Selivanov75445082015-05-11 22:57:16 -04003061 TARGET(BEFORE_ASYNC_WITH) {
3062 _Py_IDENTIFIER(__aexit__);
3063 _Py_IDENTIFIER(__aenter__);
3064
3065 PyObject *mgr = TOP();
3066 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3067 *enter;
3068 PyObject *res;
3069 if (exit == NULL)
3070 goto error;
3071 SET_TOP(exit);
3072 enter = special_lookup(mgr, &PyId___aenter__);
3073 Py_DECREF(mgr);
3074 if (enter == NULL)
3075 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003076 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003077 Py_DECREF(enter);
3078 if (res == NULL)
3079 goto error;
3080 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003081 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003082 DISPATCH();
3083 }
3084
3085 TARGET(SETUP_ASYNC_WITH) {
3086 PyObject *res = POP();
3087 /* Setup the finally block before pushing the result
3088 of __aenter__ on the stack. */
3089 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3090 STACK_LEVEL());
3091 PUSH(res);
3092 DISPATCH();
3093 }
3094
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003095 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003096 _Py_IDENTIFIER(__exit__);
3097 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003098 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08003099 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003100 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08003101 if (enter == NULL)
3102 goto error;
3103 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003104 if (exit == NULL) {
3105 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003106 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003107 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003108 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003109 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003110 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003111 Py_DECREF(enter);
3112 if (res == NULL)
3113 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003114 /* Setup the finally block before pushing the result
3115 of __enter__ on the stack. */
3116 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3117 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003118
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003119 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003120 DISPATCH();
3121 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003122
Yury Selivanov75445082015-05-11 22:57:16 -04003123 TARGET(WITH_CLEANUP_START) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04003124 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003125 how/why we entered the finally clause:
3126 - TOP = None
3127 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
3128 - TOP = WHY_*; no retval below it
3129 - (TOP, SECOND, THIRD) = exc_info()
3130 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
3131 Below them is EXIT, the context.__exit__ bound method.
3132 In the last case, we must call
3133 EXIT(TOP, SECOND, THIRD)
3134 otherwise we must call
3135 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003136
Benjamin Peterson8f169482013-10-29 22:25:06 -04003137 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003138 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04003139 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003140 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003142 In addition, if the stack represents an exception,
3143 *and* the function call returns a 'true' value, we
3144 push WHY_SILENCED onto the stack. END_FINALLY will
3145 then not re-raise the exception. (But non-local
3146 gotos should still be resumed.)
3147 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003148
Victor Stinner842cfff2016-12-01 14:45:31 +01003149 PyObject* stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003150 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003151 PyObject *exc, *val, *tb, *res;
3152
3153 val = tb = Py_None;
3154 exc = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003155 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003156 (void)POP();
3157 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003158 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003159 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003160 else if (PyLong_Check(exc)) {
3161 STACKADJ(-1);
3162 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003163 case WHY_RETURN:
3164 case WHY_CONTINUE:
3165 /* Retval in TOP. */
3166 exit_func = SECOND();
3167 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003168 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003169 break;
3170 default:
3171 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003172 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003173 break;
3174 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003175 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003176 }
3177 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003178 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003179 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003180 val = SECOND();
3181 tb = THIRD();
3182 tp2 = FOURTH();
3183 exc2 = PEEK(5);
3184 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003185 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003186 SET_VALUE(7, tb2);
3187 SET_VALUE(6, exc2);
3188 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003189 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3190 SET_FOURTH(NULL);
3191 /* We just shifted the stack down, so we have
3192 to tell the except handler block that the
3193 values are lower than it expects. */
3194 block = &f->f_blockstack[f->f_iblock - 1];
3195 assert(block->b_type == EXCEPT_HANDLER);
3196 block->b_level--;
3197 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003198
3199 stack[0] = exc;
3200 stack[1] = val;
3201 stack[2] = tb;
3202 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003203 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003204 if (res == NULL)
3205 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003206
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003207 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003208 PUSH(exc);
3209 PUSH(res);
3210 PREDICT(WITH_CLEANUP_FINISH);
3211 DISPATCH();
3212 }
3213
3214 PREDICTED(WITH_CLEANUP_FINISH);
3215 TARGET(WITH_CLEANUP_FINISH) {
3216 PyObject *res = POP();
3217 PyObject *exc = POP();
3218 int err;
3219
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003220 if (exc != Py_None)
3221 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003222 else
3223 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003224
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003225 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003226 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003228 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003229 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003230 else if (err > 0) {
3231 err = 0;
3232 /* There was an exception and a True return */
3233 PUSH(PyLong_FromLong((long) WHY_SILENCED));
3234 }
3235 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003236 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003237 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003238
Yury Selivanovf2392132016-12-13 19:03:51 -05003239 TARGET(LOAD_METHOD) {
3240 /* Designed to work in tamdem with CALL_METHOD. */
3241 PyObject *name = GETITEM(names, oparg);
3242 PyObject *obj = TOP();
3243 PyObject *meth = NULL;
3244
3245 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3246
Yury Selivanovf2392132016-12-13 19:03:51 -05003247 if (meth == NULL) {
3248 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003249 goto error;
3250 }
3251
3252 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003253 /* We can bypass temporary bound method object.
3254 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003255
INADA Naoki015bce62017-01-16 17:23:30 +09003256 meth | self | arg1 | ... | argN
3257 */
3258 SET_TOP(meth);
3259 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003260 }
3261 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003262 /* meth is not an unbound method (but a regular attr, or
3263 something was returned by a descriptor protocol). Set
3264 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003265 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003266
3267 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003268 */
INADA Naoki015bce62017-01-16 17:23:30 +09003269 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003270 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003271 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003272 }
3273 DISPATCH();
3274 }
3275
3276 TARGET(CALL_METHOD) {
3277 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003278 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003279
3280 sp = stack_pointer;
3281
INADA Naoki015bce62017-01-16 17:23:30 +09003282 meth = PEEK(oparg + 2);
3283 if (meth == NULL) {
3284 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3285 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003286
3287 Stack layout:
3288
INADA Naoki015bce62017-01-16 17:23:30 +09003289 ... | NULL | callable | arg1 | ... | argN
3290 ^- TOP()
3291 ^- (-oparg)
3292 ^- (-oparg-1)
3293 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003294
INADA Naoki015bce62017-01-16 17:23:30 +09003295 `callable` will be POPed by call_funtion.
3296 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003297 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003298 res = call_function(&sp, oparg, NULL);
3299 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003300 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003301 }
3302 else {
3303 /* This is a method call. Stack layout:
3304
INADA Naoki015bce62017-01-16 17:23:30 +09003305 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003306 ^- TOP()
3307 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003308 ^- (-oparg-1)
3309 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003310
INADA Naoki015bce62017-01-16 17:23:30 +09003311 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003312 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003313 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003314 */
3315 res = call_function(&sp, oparg + 1, NULL);
3316 stack_pointer = sp;
3317 }
3318
3319 PUSH(res);
3320 if (res == NULL)
3321 goto error;
3322 DISPATCH();
3323 }
3324
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003325 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003326 TARGET(CALL_FUNCTION) {
3327 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003328 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003329 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003330 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003331 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003332 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003333 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003334 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003335 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003336 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003337
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003338 TARGET(CALL_FUNCTION_KW) {
3339 PyObject **sp, *res, *names;
3340
3341 names = POP();
3342 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003343 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003344 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003345 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003346 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003347 Py_DECREF(names);
3348
3349 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003350 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003351 }
3352 DISPATCH();
3353 }
3354
3355 TARGET(CALL_FUNCTION_EX) {
3356 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003357 if (oparg & 0x01) {
3358 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003359 if (!PyDict_CheckExact(kwargs)) {
3360 PyObject *d = PyDict_New();
3361 if (d == NULL)
3362 goto error;
3363 if (PyDict_Update(d, kwargs) != 0) {
3364 Py_DECREF(d);
3365 /* PyDict_Update raises attribute
3366 * error (percolated from an attempt
3367 * to get 'keys' attribute) instead of
3368 * a type error if its second argument
3369 * is not a mapping.
3370 */
3371 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3372 func = SECOND();
3373 PyErr_Format(PyExc_TypeError,
3374 "%.200s%.200s argument after ** "
3375 "must be a mapping, not %.200s",
3376 PyEval_GetFuncName(func),
3377 PyEval_GetFuncDesc(func),
3378 kwargs->ob_type->tp_name);
3379 }
Victor Stinnereece2222016-09-12 11:16:37 +02003380 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003381 goto error;
3382 }
3383 Py_DECREF(kwargs);
3384 kwargs = d;
3385 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003386 assert(PyDict_CheckExact(kwargs));
3387 }
3388 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003389 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003390 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003391 if (Py_TYPE(callargs)->tp_iter == NULL &&
3392 !PySequence_Check(callargs)) {
3393 PyErr_Format(PyExc_TypeError,
3394 "%.200s%.200s argument after * "
3395 "must be an iterable, not %.200s",
3396 PyEval_GetFuncName(func),
3397 PyEval_GetFuncDesc(func),
3398 callargs->ob_type->tp_name);
Victor Stinnereece2222016-09-12 11:16:37 +02003399 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003400 goto error;
3401 }
3402 Py_SETREF(callargs, PySequence_Tuple(callargs));
3403 if (callargs == NULL) {
3404 goto error;
3405 }
3406 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003407 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003408
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003409 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003410 Py_DECREF(func);
3411 Py_DECREF(callargs);
3412 Py_XDECREF(kwargs);
3413
3414 SET_TOP(result);
3415 if (result == NULL) {
3416 goto error;
3417 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003418 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003419 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003420
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003421 TARGET(MAKE_FUNCTION) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003422 PyObject *qualname = POP();
3423 PyObject *codeobj = POP();
3424 PyFunctionObject *func = (PyFunctionObject *)
3425 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003426
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003427 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003428 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003429 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003430 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003431 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003432
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003433 if (oparg & 0x08) {
3434 assert(PyTuple_CheckExact(TOP()));
3435 func ->func_closure = POP();
3436 }
3437 if (oparg & 0x04) {
3438 assert(PyDict_CheckExact(TOP()));
3439 func->func_annotations = POP();
3440 }
3441 if (oparg & 0x02) {
3442 assert(PyDict_CheckExact(TOP()));
3443 func->func_kwdefaults = POP();
3444 }
3445 if (oparg & 0x01) {
3446 assert(PyTuple_CheckExact(TOP()));
3447 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003448 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003449
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003450 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003451 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003452 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003453
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003454 TARGET(BUILD_SLICE) {
3455 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003456 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003457 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003458 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003459 step = NULL;
3460 stop = POP();
3461 start = TOP();
3462 slice = PySlice_New(start, stop, step);
3463 Py_DECREF(start);
3464 Py_DECREF(stop);
3465 Py_XDECREF(step);
3466 SET_TOP(slice);
3467 if (slice == NULL)
3468 goto error;
3469 DISPATCH();
3470 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003471
Eric V. Smitha78c7952015-11-03 12:45:05 -05003472 TARGET(FORMAT_VALUE) {
3473 /* Handles f-string value formatting. */
3474 PyObject *result;
3475 PyObject *fmt_spec;
3476 PyObject *value;
3477 PyObject *(*conv_fn)(PyObject *);
3478 int which_conversion = oparg & FVC_MASK;
3479 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3480
3481 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003482 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003483
3484 /* See if any conversion is specified. */
3485 switch (which_conversion) {
3486 case FVC_STR: conv_fn = PyObject_Str; break;
3487 case FVC_REPR: conv_fn = PyObject_Repr; break;
3488 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3489
3490 /* Must be 0 (meaning no conversion), since only four
3491 values are allowed by (oparg & FVC_MASK). */
3492 default: conv_fn = NULL; break;
3493 }
3494
3495 /* If there's a conversion function, call it and replace
3496 value with that result. Otherwise, just use value,
3497 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003498 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003499 result = conv_fn(value);
3500 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003501 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003502 Py_XDECREF(fmt_spec);
3503 goto error;
3504 }
3505 value = result;
3506 }
3507
3508 /* If value is a unicode object, and there's no fmt_spec,
3509 then we know the result of format(value) is value
3510 itself. In that case, skip calling format(). I plan to
3511 move this optimization in to PyObject_Format()
3512 itself. */
3513 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3514 /* Do nothing, just transfer ownership to result. */
3515 result = value;
3516 } else {
3517 /* Actually call format(). */
3518 result = PyObject_Format(value, fmt_spec);
3519 Py_DECREF(value);
3520 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003521 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003522 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003523 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003524 }
3525
Eric V. Smith135d5f42016-02-05 18:23:08 -05003526 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003527 DISPATCH();
3528 }
3529
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003530 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003531 int oldoparg = oparg;
3532 NEXTOPARG();
3533 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003534 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003535 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003536
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003537
Antoine Pitrou042b1282010-08-13 21:15:58 +00003538#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003539 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003540#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003541 default:
3542 fprintf(stderr,
3543 "XXX lineno: %d, opcode: %d\n",
3544 PyFrame_GetLineNumber(f),
3545 opcode);
3546 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003547 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003548
3549#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003550 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003551#endif
3552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003553 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003554
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003555 /* This should never be reached. Every opcode should end with DISPATCH()
3556 or goto error. */
3557 assert(0);
Guido van Rossumac7be682001-01-17 15:42:30 +00003558
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003559error:
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003560
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003561 assert(why == WHY_NOT);
3562 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003563
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003564 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003565#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003566 if (!PyErr_Occurred())
3567 PyErr_SetString(PyExc_SystemError,
3568 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003569#else
3570 assert(PyErr_Occurred());
3571#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003572
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003573 /* Log traceback info. */
3574 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003575
Benjamin Peterson51f46162013-01-23 08:38:47 -05003576 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003577 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3578 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003579
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003580fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003581 assert(why != WHY_NOT);
3582
3583 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003584 while (why != WHY_NOT && f->f_iblock > 0) {
3585 /* Peek at the current block. */
3586 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003588 assert(why != WHY_YIELD);
3589 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3590 why = WHY_NOT;
3591 JUMPTO(PyLong_AS_LONG(retval));
3592 Py_DECREF(retval);
3593 break;
3594 }
3595 /* Now we have to pop the block. */
3596 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003598 if (b->b_type == EXCEPT_HANDLER) {
3599 UNWIND_EXCEPT_HANDLER(b);
3600 continue;
3601 }
3602 UNWIND_BLOCK(b);
3603 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3604 why = WHY_NOT;
3605 JUMPTO(b->b_handler);
3606 break;
3607 }
3608 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3609 || b->b_type == SETUP_FINALLY)) {
3610 PyObject *exc, *val, *tb;
3611 int handler = b->b_handler;
3612 /* Beware, this invalidates all b->b_* fields */
3613 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3614 PUSH(tstate->exc_traceback);
3615 PUSH(tstate->exc_value);
3616 if (tstate->exc_type != NULL) {
3617 PUSH(tstate->exc_type);
3618 }
3619 else {
3620 Py_INCREF(Py_None);
3621 PUSH(Py_None);
3622 }
3623 PyErr_Fetch(&exc, &val, &tb);
3624 /* Make the raw exception data
3625 available to the handler,
3626 so a program can emulate the
3627 Python main loop. */
3628 PyErr_NormalizeException(
3629 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003630 if (tb != NULL)
3631 PyException_SetTraceback(val, tb);
3632 else
3633 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003634 Py_INCREF(exc);
3635 tstate->exc_type = exc;
3636 Py_INCREF(val);
3637 tstate->exc_value = val;
3638 tstate->exc_traceback = tb;
3639 if (tb == NULL)
3640 tb = Py_None;
3641 Py_INCREF(tb);
3642 PUSH(tb);
3643 PUSH(val);
3644 PUSH(exc);
3645 why = WHY_NOT;
3646 JUMPTO(handler);
3647 break;
3648 }
3649 if (b->b_type == SETUP_FINALLY) {
3650 if (why & (WHY_RETURN | WHY_CONTINUE))
3651 PUSH(retval);
3652 PUSH(PyLong_FromLong((long)why));
3653 why = WHY_NOT;
3654 JUMPTO(b->b_handler);
3655 break;
3656 }
3657 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003659 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003661 if (why != WHY_NOT)
3662 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00003663
Victor Stinnerace47d72013-07-18 01:41:08 +02003664 assert(!PyErr_Occurred());
3665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003666 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003668 assert(why != WHY_YIELD);
3669 /* Pop remaining stack entries. */
3670 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003671 PyObject *o = POP();
3672 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003673 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003675 if (why != WHY_RETURN)
3676 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003677
Victor Stinner4a7cc882015-03-06 23:35:27 +01003678 assert((retval != NULL) ^ (PyErr_Occurred() != NULL));
Victor Stinnerace47d72013-07-18 01:41:08 +02003679
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003680fast_yield:
Yury Selivanoveb636452016-09-08 22:01:51 -07003681 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01003682
Benjamin Petersonac913412011-07-03 16:25:11 -05003683 /* The purpose of this block is to put aside the generator's exception
3684 state and restore that of the calling frame. If the current
3685 exception state is from the caller, we clear the exception values
3686 on the generator frame, so they are not swapped back in latter. The
3687 origin of the current exception state is determined by checking for
3688 except handler blocks, which we must be in iff a new exception
3689 state came into existence in this frame. (An uncaught exception
3690 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3691 int i;
Victor Stinner74319ae2016-08-25 00:04:09 +02003692 for (i = 0; i < f->f_iblock; i++) {
3693 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER) {
Benjamin Petersonac913412011-07-03 16:25:11 -05003694 break;
Victor Stinner74319ae2016-08-25 00:04:09 +02003695 }
3696 }
Benjamin Petersonac913412011-07-03 16:25:11 -05003697 if (i == f->f_iblock)
3698 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003699 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003700 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003701 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003702 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003704 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003705 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003706 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003707 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3708 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003709 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003710 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003711 why = WHY_EXCEPTION;
3712 }
3713 }
3714 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003715 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3716 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003717 PyTrace_RETURN, NULL);
3718 }
3719 }
3720 if (tstate->c_profilefunc) {
3721 if (why == WHY_EXCEPTION)
3722 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003723 tstate->c_profileobj,
3724 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003725 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003726 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3727 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003728 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003729 Py_CLEAR(retval);
Brett Cannonb94767f2011-02-22 20:15:44 +00003730 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003731 }
3732 }
3733 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003735 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003736exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003737 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3738 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003739 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003740 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003741 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003742
Victor Stinnerefde1462015-03-21 15:04:43 +01003743 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003744}
3745
Benjamin Petersonb204a422011-06-05 22:04:07 -05003746static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003747format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3748{
3749 int err;
3750 Py_ssize_t len = PyList_GET_SIZE(names);
3751 PyObject *name_str, *comma, *tail, *tmp;
3752
3753 assert(PyList_CheckExact(names));
3754 assert(len >= 1);
3755 /* Deal with the joys of natural language. */
3756 switch (len) {
3757 case 1:
3758 name_str = PyList_GET_ITEM(names, 0);
3759 Py_INCREF(name_str);
3760 break;
3761 case 2:
3762 name_str = PyUnicode_FromFormat("%U and %U",
3763 PyList_GET_ITEM(names, len - 2),
3764 PyList_GET_ITEM(names, len - 1));
3765 break;
3766 default:
3767 tail = PyUnicode_FromFormat(", %U, and %U",
3768 PyList_GET_ITEM(names, len - 2),
3769 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003770 if (tail == NULL)
3771 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003772 /* Chop off the last two objects in the list. This shouldn't actually
3773 fail, but we can't be too careful. */
3774 err = PyList_SetSlice(names, len - 2, len, NULL);
3775 if (err == -1) {
3776 Py_DECREF(tail);
3777 return;
3778 }
3779 /* Stitch everything up into a nice comma-separated list. */
3780 comma = PyUnicode_FromString(", ");
3781 if (comma == NULL) {
3782 Py_DECREF(tail);
3783 return;
3784 }
3785 tmp = PyUnicode_Join(comma, names);
3786 Py_DECREF(comma);
3787 if (tmp == NULL) {
3788 Py_DECREF(tail);
3789 return;
3790 }
3791 name_str = PyUnicode_Concat(tmp, tail);
3792 Py_DECREF(tmp);
3793 Py_DECREF(tail);
3794 break;
3795 }
3796 if (name_str == NULL)
3797 return;
3798 PyErr_Format(PyExc_TypeError,
3799 "%U() missing %i required %s argument%s: %U",
3800 co->co_name,
3801 len,
3802 kind,
3803 len == 1 ? "" : "s",
3804 name_str);
3805 Py_DECREF(name_str);
3806}
3807
3808static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003809missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003810 PyObject **fastlocals)
3811{
Victor Stinner74319ae2016-08-25 00:04:09 +02003812 Py_ssize_t i, j = 0;
3813 Py_ssize_t start, end;
3814 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003815 const char *kind = positional ? "positional" : "keyword-only";
3816 PyObject *missing_names;
3817
3818 /* Compute the names of the arguments that are missing. */
3819 missing_names = PyList_New(missing);
3820 if (missing_names == NULL)
3821 return;
3822 if (positional) {
3823 start = 0;
3824 end = co->co_argcount - defcount;
3825 }
3826 else {
3827 start = co->co_argcount;
3828 end = start + co->co_kwonlyargcount;
3829 }
3830 for (i = start; i < end; i++) {
3831 if (GETLOCAL(i) == NULL) {
3832 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3833 PyObject *name = PyObject_Repr(raw);
3834 if (name == NULL) {
3835 Py_DECREF(missing_names);
3836 return;
3837 }
3838 PyList_SET_ITEM(missing_names, j++, name);
3839 }
3840 }
3841 assert(j == missing);
3842 format_missing(kind, co, missing_names);
3843 Py_DECREF(missing_names);
3844}
3845
3846static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003847too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3848 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003849{
3850 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003851 Py_ssize_t kwonly_given = 0;
3852 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003853 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003854 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003855
Benjamin Petersone109c702011-06-24 09:37:26 -05003856 assert((co->co_flags & CO_VARARGS) == 0);
3857 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003858 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3859 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003860 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003861 }
3862 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003863 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003864 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003865 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003866 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003867 }
3868 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003869 plural = (co_argcount != 1);
3870 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003871 }
3872 if (sig == NULL)
3873 return;
3874 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003875 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3876 kwonly_sig = PyUnicode_FromFormat(format,
3877 given != 1 ? "s" : "",
3878 kwonly_given,
3879 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003880 if (kwonly_sig == NULL) {
3881 Py_DECREF(sig);
3882 return;
3883 }
3884 }
3885 else {
3886 /* This will not fail. */
3887 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003888 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003889 }
3890 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003891 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003892 co->co_name,
3893 sig,
3894 plural ? "s" : "",
3895 given,
3896 kwonly_sig,
3897 given == 1 && !kwonly_given ? "was" : "were");
3898 Py_DECREF(sig);
3899 Py_DECREF(kwonly_sig);
3900}
3901
Guido van Rossumc2e20742006-02-27 22:32:47 +00003902/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003903 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003904 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003905
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003906PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003907_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Victor Stinner74319ae2016-08-25 00:04:09 +02003908 PyObject **args, Py_ssize_t argcount,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003909 PyObject **kwnames, PyObject **kwargs,
3910 Py_ssize_t kwcount, int kwstep,
Victor Stinner74319ae2016-08-25 00:04:09 +02003911 PyObject **defs, Py_ssize_t defcount,
3912 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003913 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003914{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003915 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003916 PyFrameObject *f;
3917 PyObject *retval = NULL;
3918 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003919 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003920 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003921 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3922 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003923 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003925 if (globals == NULL) {
3926 PyErr_SetString(PyExc_SystemError,
3927 "PyEval_EvalCodeEx: NULL globals");
3928 return NULL;
3929 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003930
Victor Stinnerc7020012016-08-16 23:40:29 +02003931 /* Create the frame */
3932 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003933 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003934 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003935 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003936 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003937 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003938 fastlocals = f->f_localsplus;
3939 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003940
Victor Stinnerc7020012016-08-16 23:40:29 +02003941 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003942 if (co->co_flags & CO_VARKEYWORDS) {
3943 kwdict = PyDict_New();
3944 if (kwdict == NULL)
3945 goto fail;
3946 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003947 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003948 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003949 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003950 SETLOCAL(i, kwdict);
3951 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003952 else {
3953 kwdict = NULL;
3954 }
3955
3956 /* Copy positional arguments into local variables */
3957 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003958 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003959 }
3960 else {
3961 n = argcount;
3962 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003963 for (i = 0; i < n; i++) {
3964 x = args[i];
3965 Py_INCREF(x);
3966 SETLOCAL(i, x);
3967 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003968
3969 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003970 if (co->co_flags & CO_VARARGS) {
3971 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003972 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003973 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003974 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003975 SETLOCAL(total_args, u);
3976 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003977 x = args[i];
3978 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003979 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003980 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003981 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003982
Serhiy Storchakab7281052016-09-12 00:52:40 +03003983 /* Handle keyword arguments passed as two strided arrays */
3984 kwcount *= kwstep;
3985 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003986 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003987 PyObject *keyword = kwnames[i];
3988 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003989 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003990
Benjamin Petersonb204a422011-06-05 22:04:07 -05003991 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3992 PyErr_Format(PyExc_TypeError,
3993 "%U() keywords must be strings",
3994 co->co_name);
3995 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003996 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003997
Benjamin Petersonb204a422011-06-05 22:04:07 -05003998 /* Speed hack: do raw pointer compares. As names are
3999 normally interned this should almost always hit. */
4000 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
4001 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004002 PyObject *name = co_varnames[j];
4003 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004004 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004005 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004006 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004007
Benjamin Petersonb204a422011-06-05 22:04:07 -05004008 /* Slow fallback, just in case */
4009 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004010 PyObject *name = co_varnames[j];
4011 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4012 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004013 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004014 }
4015 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004016 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004017 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004018 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004019
Victor Stinner231d1f32017-01-11 02:12:06 +01004020 assert(j >= total_args);
4021 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004022 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02004023 "%U() got an unexpected keyword argument '%S'",
4024 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004025 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004026 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004027
Christian Heimes0bd447f2013-07-20 14:48:10 +02004028 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4029 goto fail;
4030 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004031 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004032
Benjamin Petersonb204a422011-06-05 22:04:07 -05004033 kw_found:
4034 if (GETLOCAL(j) != NULL) {
4035 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02004036 "%U() got multiple values for argument '%S'",
4037 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004038 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004039 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004040 Py_INCREF(value);
4041 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004042 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004043
4044 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004045 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004046 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004047 goto fail;
4048 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004049
4050 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004051 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02004052 Py_ssize_t m = co->co_argcount - defcount;
4053 Py_ssize_t missing = 0;
4054 for (i = argcount; i < m; i++) {
4055 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004056 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004057 }
4058 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004059 if (missing) {
4060 missing_arguments(co, missing, defcount, fastlocals);
4061 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004062 }
4063 if (n > m)
4064 i = n - m;
4065 else
4066 i = 0;
4067 for (; i < defcount; i++) {
4068 if (GETLOCAL(m+i) == NULL) {
4069 PyObject *def = defs[i];
4070 Py_INCREF(def);
4071 SETLOCAL(m+i, def);
4072 }
4073 }
4074 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004075
4076 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004077 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004078 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004079 for (i = co->co_argcount; i < total_args; i++) {
4080 PyObject *name;
4081 if (GETLOCAL(i) != NULL)
4082 continue;
4083 name = PyTuple_GET_ITEM(co->co_varnames, i);
4084 if (kwdefs != NULL) {
4085 PyObject *def = PyDict_GetItem(kwdefs, name);
4086 if (def) {
4087 Py_INCREF(def);
4088 SETLOCAL(i, def);
4089 continue;
4090 }
4091 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004092 missing++;
4093 }
4094 if (missing) {
4095 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004096 goto fail;
4097 }
4098 }
4099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004100 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004101 vars into frame. */
4102 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004103 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004104 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004105 /* Possibly account for the cell variable being an argument. */
4106 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004107 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004108 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004109 /* Clear the local copy. */
4110 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004111 }
4112 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004113 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004114 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004115 if (c == NULL)
4116 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004117 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004118 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004119
4120 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004121 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4122 PyObject *o = PyTuple_GET_ITEM(closure, i);
4123 Py_INCREF(o);
4124 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004125 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004126
Yury Selivanoveb636452016-09-08 22:01:51 -07004127 /* Handle generator/coroutine/asynchronous generator */
4128 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004129 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04004130 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004131 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004132
4133 if (is_coro && tstate->in_coroutine_wrapper) {
4134 assert(coro_wrapper != NULL);
4135 PyErr_Format(PyExc_RuntimeError,
4136 "coroutine wrapper %.200R attempted "
4137 "to recursively wrap %.200R",
4138 coro_wrapper,
4139 co);
4140 goto fail;
4141 }
Yury Selivanov75445082015-05-11 22:57:16 -04004142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004143 /* Don't need to keep the reference to f_back, it will be set
4144 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004145 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004147 /* Create a new generator that owns the ready to run frame
4148 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004149 if (is_coro) {
4150 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004151 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4152 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004153 } else {
4154 gen = PyGen_NewWithQualName(f, name, qualname);
4155 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004156 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004157 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004158 }
INADA Naoki9c157762016-12-26 18:52:46 +09004159
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004160 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004161
Yury Selivanov94c22632015-06-04 10:16:51 -04004162 if (is_coro && coro_wrapper != NULL) {
4163 PyObject *wrapped;
4164 tstate->in_coroutine_wrapper = 1;
4165 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4166 tstate->in_coroutine_wrapper = 0;
4167 return wrapped;
4168 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004169
Yury Selivanov75445082015-05-11 22:57:16 -04004170 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004171 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004172
Victor Stinner59a73272016-12-09 18:51:13 +01004173 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004174
Thomas Woutersce272b62007-09-19 21:19:28 +00004175fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004177 /* decref'ing the frame can cause __del__ methods to get invoked,
4178 which can call back into Python. While we're done with the
4179 current Python frame (f), the associated C stack is still in use,
4180 so recursion_depth must be boosted for the duration.
4181 */
4182 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09004183 if (Py_REFCNT(f) > 1) {
4184 Py_DECREF(f);
4185 _PyObject_GC_TRACK(f);
4186 }
4187 else {
4188 ++tstate->recursion_depth;
4189 Py_DECREF(f);
4190 --tstate->recursion_depth;
4191 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004192 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004193}
4194
Victor Stinner40ee3012014-06-16 15:59:28 +02004195PyObject *
4196PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
4197 PyObject **args, int argcount, PyObject **kws, int kwcount,
4198 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
4199{
4200 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004201 args, argcount,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004202 kws, kws + 1, kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004203 defs, defcount,
4204 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004205 NULL, NULL);
4206}
Tim Peters5ca576e2001-06-18 22:08:13 +00004207
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004208static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004209special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004210{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004211 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004212 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004213 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004214 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004215 return NULL;
4216 }
4217 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004218}
4219
4220
Benjamin Peterson87880242011-07-03 16:48:31 -05004221/* These 3 functions deal with the exception state of generators. */
4222
4223static void
4224save_exc_state(PyThreadState *tstate, PyFrameObject *f)
4225{
4226 PyObject *type, *value, *traceback;
4227 Py_XINCREF(tstate->exc_type);
4228 Py_XINCREF(tstate->exc_value);
4229 Py_XINCREF(tstate->exc_traceback);
4230 type = f->f_exc_type;
4231 value = f->f_exc_value;
4232 traceback = f->f_exc_traceback;
4233 f->f_exc_type = tstate->exc_type;
4234 f->f_exc_value = tstate->exc_value;
4235 f->f_exc_traceback = tstate->exc_traceback;
4236 Py_XDECREF(type);
4237 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004238 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05004239}
4240
4241static void
4242swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
4243{
4244 PyObject *tmp;
4245 tmp = tstate->exc_type;
4246 tstate->exc_type = f->f_exc_type;
4247 f->f_exc_type = tmp;
4248 tmp = tstate->exc_value;
4249 tstate->exc_value = f->f_exc_value;
4250 f->f_exc_value = tmp;
4251 tmp = tstate->exc_traceback;
4252 tstate->exc_traceback = f->f_exc_traceback;
4253 f->f_exc_traceback = tmp;
4254}
4255
4256static void
4257restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
4258{
4259 PyObject *type, *value, *tb;
4260 type = tstate->exc_type;
4261 value = tstate->exc_value;
4262 tb = tstate->exc_traceback;
4263 tstate->exc_type = f->f_exc_type;
4264 tstate->exc_value = f->f_exc_value;
4265 tstate->exc_traceback = f->f_exc_traceback;
4266 f->f_exc_type = NULL;
4267 f->f_exc_value = NULL;
4268 f->f_exc_traceback = NULL;
4269 Py_XDECREF(type);
4270 Py_XDECREF(value);
4271 Py_XDECREF(tb);
4272}
4273
4274
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004275/* Logic for the raise statement (too complicated for inlining).
4276 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004277static int
Collin Winter828f04a2007-08-31 00:04:24 +00004278do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004279{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004280 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004282 if (exc == NULL) {
4283 /* Reraise */
4284 PyThreadState *tstate = PyThreadState_GET();
4285 PyObject *tb;
4286 type = tstate->exc_type;
4287 value = tstate->exc_value;
4288 tb = tstate->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004289 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004290 PyErr_SetString(PyExc_RuntimeError,
4291 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004292 return 0;
4293 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004294 Py_XINCREF(type);
4295 Py_XINCREF(value);
4296 Py_XINCREF(tb);
4297 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004298 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004299 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004301 /* We support the following forms of raise:
4302 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004303 raise <instance>
4304 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004306 if (PyExceptionClass_Check(exc)) {
4307 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004308 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004309 if (value == NULL)
4310 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004311 if (!PyExceptionInstance_Check(value)) {
4312 PyErr_Format(PyExc_TypeError,
4313 "calling %R should have returned an instance of "
4314 "BaseException, not %R",
4315 type, Py_TYPE(value));
4316 goto raise_error;
4317 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004318 }
4319 else if (PyExceptionInstance_Check(exc)) {
4320 value = exc;
4321 type = PyExceptionInstance_Class(exc);
4322 Py_INCREF(type);
4323 }
4324 else {
4325 /* Not something you can raise. You get an exception
4326 anyway, just not what you specified :-) */
4327 Py_DECREF(exc);
4328 PyErr_SetString(PyExc_TypeError,
4329 "exceptions must derive from BaseException");
4330 goto raise_error;
4331 }
Collin Winter828f04a2007-08-31 00:04:24 +00004332
Serhiy Storchakac0191582016-09-27 11:37:10 +03004333 assert(type != NULL);
4334 assert(value != NULL);
4335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004336 if (cause) {
4337 PyObject *fixed_cause;
4338 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004339 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004340 if (fixed_cause == NULL)
4341 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004342 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004343 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004344 else if (PyExceptionInstance_Check(cause)) {
4345 fixed_cause = cause;
4346 }
4347 else if (cause == Py_None) {
4348 Py_DECREF(cause);
4349 fixed_cause = NULL;
4350 }
4351 else {
4352 PyErr_SetString(PyExc_TypeError,
4353 "exception causes must derive from "
4354 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004355 goto raise_error;
4356 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004357 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004358 }
Collin Winter828f04a2007-08-31 00:04:24 +00004359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004360 PyErr_SetObject(type, value);
4361 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004362 Py_DECREF(value);
4363 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004364 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004365
4366raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004367 Py_XDECREF(value);
4368 Py_XDECREF(type);
4369 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004370 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004371}
4372
Tim Petersd6d010b2001-06-21 02:49:55 +00004373/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004374 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004375
Guido van Rossum0368b722007-05-11 16:50:42 +00004376 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4377 with a variable target.
4378*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004379
Barry Warsawe42b18f1997-08-25 22:13:04 +00004380static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004381unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004382{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004383 int i = 0, j = 0;
4384 Py_ssize_t ll = 0;
4385 PyObject *it; /* iter(v) */
4386 PyObject *w;
4387 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004389 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004391 it = PyObject_GetIter(v);
4392 if (it == NULL)
4393 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00004394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004395 for (; i < argcnt; i++) {
4396 w = PyIter_Next(it);
4397 if (w == NULL) {
4398 /* Iterator done, via error or exhaustion. */
4399 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004400 if (argcntafter == -1) {
4401 PyErr_Format(PyExc_ValueError,
4402 "not enough values to unpack (expected %d, got %d)",
4403 argcnt, i);
4404 }
4405 else {
4406 PyErr_Format(PyExc_ValueError,
4407 "not enough values to unpack "
4408 "(expected at least %d, got %d)",
4409 argcnt + argcntafter, i);
4410 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004411 }
4412 goto Error;
4413 }
4414 *--sp = w;
4415 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004417 if (argcntafter == -1) {
4418 /* We better have exhausted the iterator now. */
4419 w = PyIter_Next(it);
4420 if (w == NULL) {
4421 if (PyErr_Occurred())
4422 goto Error;
4423 Py_DECREF(it);
4424 return 1;
4425 }
4426 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004427 PyErr_Format(PyExc_ValueError,
4428 "too many values to unpack (expected %d)",
4429 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004430 goto Error;
4431 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004433 l = PySequence_List(it);
4434 if (l == NULL)
4435 goto Error;
4436 *--sp = l;
4437 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004439 ll = PyList_GET_SIZE(l);
4440 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004441 PyErr_Format(PyExc_ValueError,
4442 "not enough values to unpack (expected at least %d, got %zd)",
4443 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004444 goto Error;
4445 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004447 /* Pop the "after-variable" args off the list. */
4448 for (j = argcntafter; j > 0; j--, i++) {
4449 *--sp = PyList_GET_ITEM(l, ll - j);
4450 }
4451 /* Resize the list. */
4452 Py_SIZE(l) = ll - argcntafter;
4453 Py_DECREF(it);
4454 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004455
Tim Petersd6d010b2001-06-21 02:49:55 +00004456Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004457 for (; i > 0; i--, sp++)
4458 Py_DECREF(*sp);
4459 Py_XDECREF(it);
4460 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004461}
4462
4463
Guido van Rossum96a42c81992-01-12 02:29:51 +00004464#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004465static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004466prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004467{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004468 printf("%s ", str);
4469 if (PyObject_Print(v, stdout, 0) != 0)
4470 PyErr_Clear(); /* Don't know what else to do */
4471 printf("\n");
4472 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004473}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004474#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004475
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004476static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004477call_exc_trace(Py_tracefunc func, PyObject *self,
4478 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004479{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004480 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004481 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004482 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004483 if (value == NULL) {
4484 value = Py_None;
4485 Py_INCREF(value);
4486 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004487 PyErr_NormalizeException(&type, &value, &orig_traceback);
4488 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 arg = PyTuple_Pack(3, type, value, traceback);
4490 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004491 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004492 return;
4493 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004494 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004495 Py_DECREF(arg);
4496 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004497 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004498 else {
4499 Py_XDECREF(type);
4500 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004501 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004502 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004503}
4504
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004505static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004506call_trace_protected(Py_tracefunc func, PyObject *obj,
4507 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004508 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004509{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004510 PyObject *type, *value, *traceback;
4511 int err;
4512 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004513 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004514 if (err == 0)
4515 {
4516 PyErr_Restore(type, value, traceback);
4517 return 0;
4518 }
4519 else {
4520 Py_XDECREF(type);
4521 Py_XDECREF(value);
4522 Py_XDECREF(traceback);
4523 return -1;
4524 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004525}
4526
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004527static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004528call_trace(Py_tracefunc func, PyObject *obj,
4529 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004530 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004531{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004532 int result;
4533 if (tstate->tracing)
4534 return 0;
4535 tstate->tracing++;
4536 tstate->use_tracing = 0;
4537 result = func(obj, frame, what, arg);
4538 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4539 || (tstate->c_profilefunc != NULL));
4540 tstate->tracing--;
4541 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004542}
4543
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004544PyObject *
4545_PyEval_CallTracing(PyObject *func, PyObject *args)
4546{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004547 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004548 int save_tracing = tstate->tracing;
4549 int save_use_tracing = tstate->use_tracing;
4550 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004552 tstate->tracing = 0;
4553 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4554 || (tstate->c_profilefunc != NULL));
4555 result = PyObject_Call(func, args, NULL);
4556 tstate->tracing = save_tracing;
4557 tstate->use_tracing = save_use_tracing;
4558 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004559}
4560
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004561/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004562static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004563maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004564 PyThreadState *tstate, PyFrameObject *frame,
4565 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004566{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004567 int result = 0;
4568 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004570 /* If the last instruction executed isn't in the current
4571 instruction window, reset the window.
4572 */
4573 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4574 PyAddrPair bounds;
4575 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4576 &bounds);
4577 *instr_lb = bounds.ap_lower;
4578 *instr_ub = bounds.ap_upper;
4579 }
4580 /* If the last instruction falls at the start of a line or if
4581 it represents a jump backwards, update the frame's line
4582 number and call the trace function. */
4583 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
4584 frame->f_lineno = line;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004585 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004586 }
4587 *instr_prev = frame->f_lasti;
4588 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004589}
4590
Fred Drake5755ce62001-06-27 19:19:46 +00004591void
4592PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004593{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004594 PyThreadState *tstate = PyThreadState_GET();
4595 PyObject *temp = tstate->c_profileobj;
4596 Py_XINCREF(arg);
4597 tstate->c_profilefunc = NULL;
4598 tstate->c_profileobj = NULL;
4599 /* Must make sure that tracing is not ignored if 'temp' is freed */
4600 tstate->use_tracing = tstate->c_tracefunc != NULL;
4601 Py_XDECREF(temp);
4602 tstate->c_profilefunc = func;
4603 tstate->c_profileobj = arg;
4604 /* Flag that tracing or profiling is turned on */
4605 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004606}
4607
4608void
4609PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4610{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004611 PyThreadState *tstate = PyThreadState_GET();
4612 PyObject *temp = tstate->c_traceobj;
4613 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4614 Py_XINCREF(arg);
4615 tstate->c_tracefunc = NULL;
4616 tstate->c_traceobj = NULL;
4617 /* Must make sure that profiling is not ignored if 'temp' is freed */
4618 tstate->use_tracing = tstate->c_profilefunc != NULL;
4619 Py_XDECREF(temp);
4620 tstate->c_tracefunc = func;
4621 tstate->c_traceobj = arg;
4622 /* Flag that tracing or profiling is turned on */
4623 tstate->use_tracing = ((func != NULL)
4624 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004625}
4626
Yury Selivanov75445082015-05-11 22:57:16 -04004627void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004628_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004629{
4630 PyThreadState *tstate = PyThreadState_GET();
4631
Yury Selivanov75445082015-05-11 22:57:16 -04004632 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004633 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004634}
4635
4636PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004637_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004638{
4639 PyThreadState *tstate = PyThreadState_GET();
4640 return tstate->coroutine_wrapper;
4641}
4642
Yury Selivanoveb636452016-09-08 22:01:51 -07004643void
4644_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4645{
4646 PyThreadState *tstate = PyThreadState_GET();
4647
4648 Py_XINCREF(firstiter);
4649 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4650}
4651
4652PyObject *
4653_PyEval_GetAsyncGenFirstiter(void)
4654{
4655 PyThreadState *tstate = PyThreadState_GET();
4656 return tstate->async_gen_firstiter;
4657}
4658
4659void
4660_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4661{
4662 PyThreadState *tstate = PyThreadState_GET();
4663
4664 Py_XINCREF(finalizer);
4665 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4666}
4667
4668PyObject *
4669_PyEval_GetAsyncGenFinalizer(void)
4670{
4671 PyThreadState *tstate = PyThreadState_GET();
4672 return tstate->async_gen_finalizer;
4673}
4674
Guido van Rossumb209a111997-04-29 18:18:01 +00004675PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004676PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004677{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004678 PyFrameObject *current_frame = PyEval_GetFrame();
4679 if (current_frame == NULL)
4680 return PyThreadState_GET()->interp->builtins;
4681 else
4682 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004683}
4684
Guido van Rossumb209a111997-04-29 18:18:01 +00004685PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004686PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004687{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004688 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004689 if (current_frame == NULL) {
4690 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004691 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004692 }
4693
4694 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4695 return NULL;
4696
4697 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004698 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004699}
4700
Guido van Rossumb209a111997-04-29 18:18:01 +00004701PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004702PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004703{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004704 PyFrameObject *current_frame = PyEval_GetFrame();
4705 if (current_frame == NULL)
4706 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004707
4708 assert(current_frame->f_globals != NULL);
4709 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004710}
4711
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004712PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004713PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004714{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004715 PyThreadState *tstate = PyThreadState_GET();
4716 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004717}
4718
Guido van Rossum6135a871995-01-09 17:53:26 +00004719int
Tim Peters5ba58662001-07-16 02:29:45 +00004720PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004721{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004722 PyFrameObject *current_frame = PyEval_GetFrame();
4723 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004725 if (current_frame != NULL) {
4726 const int codeflags = current_frame->f_code->co_flags;
4727 const int compilerflags = codeflags & PyCF_MASK;
4728 if (compilerflags) {
4729 result = 1;
4730 cf->cf_flags |= compilerflags;
4731 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004732#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004733 if (codeflags & CO_GENERATOR_ALLOWED) {
4734 result = 1;
4735 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4736 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004737#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004738 }
4739 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004740}
4741
Guido van Rossum3f5da241990-12-20 15:06:42 +00004742
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004743const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004744PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004745{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004746 if (PyMethod_Check(func))
4747 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4748 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004749 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004750 else if (PyCFunction_Check(func))
4751 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4752 else
4753 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004754}
4755
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004756const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004757PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004758{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004759 if (PyMethod_Check(func))
4760 return "()";
4761 else if (PyFunction_Check(func))
4762 return "()";
4763 else if (PyCFunction_Check(func))
4764 return "()";
4765 else
4766 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004767}
4768
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004769#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004770if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004771 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4772 tstate, tstate->frame, \
4773 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004774 x = NULL; \
4775 } \
4776 else { \
4777 x = call; \
4778 if (tstate->c_profilefunc != NULL) { \
4779 if (x == NULL) { \
4780 call_trace_protected(tstate->c_profilefunc, \
4781 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004782 tstate, tstate->frame, \
4783 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004784 /* XXX should pass (type, value, tb) */ \
4785 } else { \
4786 if (call_trace(tstate->c_profilefunc, \
4787 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004788 tstate, tstate->frame, \
4789 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004790 Py_DECREF(x); \
4791 x = NULL; \
4792 } \
4793 } \
4794 } \
4795 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004796} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004797 x = call; \
4798 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004799
Victor Stinner415c5102017-01-11 00:54:57 +01004800/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4801 to reduce the stack consumption. */
4802Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004803call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004804{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004805 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004806 PyObject *func = *pfunc;
4807 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004808 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4809 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004810 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004812 /* Always dispatch PyCFunction first, because these are
4813 presumed to be the most frequent callable object.
4814 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004815 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004816 PyThreadState *tstate = PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004817 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004818 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004819 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
4820 PyThreadState *tstate = PyThreadState_GET();
INADA Naoki93fac8d2017-03-07 14:24:37 +09004821 if (tstate->use_tracing && tstate->c_profilefunc) {
4822 // We need to create PyCFunctionObject for tracing.
4823 PyMethodDescrObject *descr = (PyMethodDescrObject*)func;
4824 func = PyCFunction_NewEx(descr->d_method, stack[0], NULL);
4825 if (func == NULL) {
4826 return NULL;
4827 }
4828 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack+1, nargs-1,
4829 kwnames));
4830 Py_DECREF(func);
4831 }
4832 else {
4833 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4834 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004835 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004836 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004837 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004838 /* Optimize access to bound methods. Reuse the Python stack
4839 to pass 'self' as the first argument, replace 'func'
4840 with 'self'. It avoids the creation of a new temporary tuple
4841 for arguments (to replace func with self) when the method uses
4842 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004843 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004844 Py_INCREF(self);
4845 func = PyMethod_GET_FUNCTION(func);
4846 Py_INCREF(func);
4847 Py_SETREF(*pfunc, self);
4848 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004849 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004850 }
4851 else {
4852 Py_INCREF(func);
4853 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004854
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004855 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004856 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004857 }
4858 else {
4859 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4860 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004861 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004862 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004863
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004864 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4865
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004866 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004867 while ((*pp_stack) > pfunc) {
4868 w = EXT_POP(*pp_stack);
4869 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004870 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004872 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004873}
4874
Jeremy Hylton52820442001-01-03 23:52:36 +00004875static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004876do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004877{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004878 if (PyCFunction_Check(func)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004879 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004880 PyThreadState *tstate = PyThreadState_GET();
4881 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004882 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004883 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004884 else {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004885 return PyObject_Call(func, callargs, kwdict);
Victor Stinner74319ae2016-08-25 00:04:09 +02004886 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004887}
4888
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004889/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004890 nb_index slot defined, and store in *pi.
4891 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4892 and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004893 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004894*/
Tim Petersb5196382001-12-16 19:44:20 +00004895/* Note: If v is NULL, return success without storing into *pi. This
4896 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4897 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004898*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004899int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004900_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004901{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004902 if (v != NULL) {
4903 Py_ssize_t x;
4904 if (PyIndex_Check(v)) {
4905 x = PyNumber_AsSsize_t(v, NULL);
4906 if (x == -1 && PyErr_Occurred())
4907 return 0;
4908 }
4909 else {
4910 PyErr_SetString(PyExc_TypeError,
4911 "slice indices must be integers or "
4912 "None or have an __index__ method");
4913 return 0;
4914 }
4915 *pi = x;
4916 }
4917 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004918}
4919
Guido van Rossum486364b2007-06-30 05:01:58 +00004920#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004921 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004922
Guido van Rossumb209a111997-04-29 18:18:01 +00004923static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004924cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004925{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004926 int res = 0;
4927 switch (op) {
4928 case PyCmp_IS:
4929 res = (v == w);
4930 break;
4931 case PyCmp_IS_NOT:
4932 res = (v != w);
4933 break;
4934 case PyCmp_IN:
4935 res = PySequence_Contains(w, v);
4936 if (res < 0)
4937 return NULL;
4938 break;
4939 case PyCmp_NOT_IN:
4940 res = PySequence_Contains(w, v);
4941 if (res < 0)
4942 return NULL;
4943 res = !res;
4944 break;
4945 case PyCmp_EXC_MATCH:
4946 if (PyTuple_Check(w)) {
4947 Py_ssize_t i, length;
4948 length = PyTuple_Size(w);
4949 for (i = 0; i < length; i += 1) {
4950 PyObject *exc = PyTuple_GET_ITEM(w, i);
4951 if (!PyExceptionClass_Check(exc)) {
4952 PyErr_SetString(PyExc_TypeError,
4953 CANNOT_CATCH_MSG);
4954 return NULL;
4955 }
4956 }
4957 }
4958 else {
4959 if (!PyExceptionClass_Check(w)) {
4960 PyErr_SetString(PyExc_TypeError,
4961 CANNOT_CATCH_MSG);
4962 return NULL;
4963 }
4964 }
4965 res = PyErr_GivenExceptionMatches(v, w);
4966 break;
4967 default:
4968 return PyObject_RichCompare(v, w, op);
4969 }
4970 v = res ? Py_True : Py_False;
4971 Py_INCREF(v);
4972 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004973}
4974
Thomas Wouters52152252000-08-17 22:55:00 +00004975static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004976import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4977{
4978 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004979 PyObject *import_func, *res;
4980 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004981
4982 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
4983 if (import_func == NULL) {
4984 PyErr_SetString(PyExc_ImportError, "__import__ not found");
4985 return NULL;
4986 }
4987
4988 /* Fast path for not overloaded __import__. */
4989 if (import_func == PyThreadState_GET()->interp->import_func) {
4990 int ilevel = _PyLong_AsInt(level);
4991 if (ilevel == -1 && PyErr_Occurred()) {
4992 return NULL;
4993 }
4994 res = PyImport_ImportModuleLevelObject(
4995 name,
4996 f->f_globals,
4997 f->f_locals == NULL ? Py_None : f->f_locals,
4998 fromlist,
4999 ilevel);
5000 return res;
5001 }
5002
5003 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005004
5005 stack[0] = name;
5006 stack[1] = f->f_globals;
5007 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5008 stack[3] = fromlist;
5009 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005010 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005011 Py_DECREF(import_func);
5012 return res;
5013}
5014
5015static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00005016import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005017{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005018 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005019 _Py_IDENTIFIER(__name__);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005020 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005022 x = PyObject_GetAttr(v, name);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005023 if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
5024 return x;
5025 /* Issue #17636: in case this failed because of a circular relative
5026 import, try to fallback on reading the module directly from
5027 sys.modules. */
5028 PyErr_Clear();
5029 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005030 if (pkgname == NULL) {
5031 goto error;
5032 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005033 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005034 if (fullmodname == NULL) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005035 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005036 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005037 x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005038 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005039 if (x == NULL) {
5040 goto error;
5041 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005042 Py_DECREF(pkgname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005043 Py_INCREF(x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005044 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005045 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005046 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005047 if (pkgname == NULL) {
5048 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5049 if (pkgname_or_unknown == NULL) {
5050 Py_XDECREF(pkgpath);
5051 return NULL;
5052 }
5053 } else {
5054 pkgname_or_unknown = pkgname;
5055 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005056
5057 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
5058 PyErr_Clear();
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005059 PyErr_SetImportError(
5060 PyUnicode_FromFormat("cannot import name %R from %R (unknown location)",
5061 name, pkgname_or_unknown),
5062 pkgname, NULL);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005063 } else {
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005064 PyErr_SetImportError(
5065 PyUnicode_FromFormat("cannot import name %R from %R (%S)",
5066 name, pkgname_or_unknown, pkgpath),
5067 pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005068 }
5069
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005070 Py_XDECREF(pkgname_or_unknown);
5071 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005072 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005073}
Guido van Rossumac7be682001-01-17 15:42:30 +00005074
Thomas Wouters52152252000-08-17 22:55:00 +00005075static int
5076import_all_from(PyObject *locals, PyObject *v)
5077{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005078 _Py_IDENTIFIER(__all__);
5079 _Py_IDENTIFIER(__dict__);
5080 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005081 PyObject *dict, *name, *value;
5082 int skip_leading_underscores = 0;
5083 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005085 if (all == NULL) {
5086 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5087 return -1; /* Unexpected error */
5088 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005089 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005090 if (dict == NULL) {
5091 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5092 return -1;
5093 PyErr_SetString(PyExc_ImportError,
5094 "from-import-* object has no __dict__ and no __all__");
5095 return -1;
5096 }
5097 all = PyMapping_Keys(dict);
5098 Py_DECREF(dict);
5099 if (all == NULL)
5100 return -1;
5101 skip_leading_underscores = 1;
5102 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005104 for (pos = 0, err = 0; ; pos++) {
5105 name = PySequence_GetItem(all, pos);
5106 if (name == NULL) {
5107 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5108 err = -1;
5109 else
5110 PyErr_Clear();
5111 break;
5112 }
5113 if (skip_leading_underscores &&
5114 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005115 PyUnicode_READY(name) != -1 &&
5116 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005117 {
5118 Py_DECREF(name);
5119 continue;
5120 }
5121 value = PyObject_GetAttr(v, name);
5122 if (value == NULL)
5123 err = -1;
5124 else if (PyDict_CheckExact(locals))
5125 err = PyDict_SetItem(locals, name, value);
5126 else
5127 err = PyObject_SetItem(locals, name, value);
5128 Py_DECREF(name);
5129 Py_XDECREF(value);
5130 if (err != 0)
5131 break;
5132 }
5133 Py_DECREF(all);
5134 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005135}
5136
Guido van Rossumac7be682001-01-17 15:42:30 +00005137static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005138format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005140 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005142 if (!obj)
5143 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005144
Serhiy Storchaka06515832016-11-20 09:13:07 +02005145 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005146 if (!obj_str)
5147 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005149 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005150}
Guido van Rossum950361c1997-01-24 13:49:28 +00005151
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005152static void
5153format_exc_unbound(PyCodeObject *co, int oparg)
5154{
5155 PyObject *name;
5156 /* Don't stomp existing exception */
5157 if (PyErr_Occurred())
5158 return;
5159 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5160 name = PyTuple_GET_ITEM(co->co_cellvars,
5161 oparg);
5162 format_exc_check_arg(
5163 PyExc_UnboundLocalError,
5164 UNBOUNDLOCAL_ERROR_MSG,
5165 name);
5166 } else {
5167 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5168 PyTuple_GET_SIZE(co->co_cellvars));
5169 format_exc_check_arg(PyExc_NameError,
5170 UNBOUNDFREE_ERROR_MSG, name);
5171 }
5172}
5173
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005174static PyObject *
5175unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005176 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005177{
5178 PyObject *res;
5179 if (Py_REFCNT(v) == 2) {
5180 /* In the common case, there are 2 references to the value
5181 * stored in 'variable' when the += is performed: one on the
5182 * value stack (in 'v') and one still stored in the
5183 * 'variable'. We try to delete the variable now to reduce
5184 * the refcnt to 1.
5185 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005186 int opcode, oparg;
5187 NEXTOPARG();
5188 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005189 case STORE_FAST:
5190 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005191 PyObject **fastlocals = f->f_localsplus;
5192 if (GETLOCAL(oparg) == v)
5193 SETLOCAL(oparg, NULL);
5194 break;
5195 }
5196 case STORE_DEREF:
5197 {
5198 PyObject **freevars = (f->f_localsplus +
5199 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005200 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005201 if (PyCell_GET(c) == v) {
5202 PyCell_SET(c, NULL);
5203 Py_DECREF(v);
5204 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005205 break;
5206 }
5207 case STORE_NAME:
5208 {
5209 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005210 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005211 PyObject *locals = f->f_locals;
5212 if (PyDict_CheckExact(locals) &&
5213 PyDict_GetItem(locals, name) == v) {
5214 if (PyDict_DelItem(locals, name) != 0) {
5215 PyErr_Clear();
5216 }
5217 }
5218 break;
5219 }
5220 }
5221 }
5222 res = v;
5223 PyUnicode_Append(&res, w);
5224 return res;
5225}
5226
Guido van Rossum950361c1997-01-24 13:49:28 +00005227#ifdef DYNAMIC_EXECUTION_PROFILE
5228
Skip Montanarof118cb12001-10-15 20:51:38 +00005229static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005230getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005231{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005232 int i;
5233 PyObject *l = PyList_New(256);
5234 if (l == NULL) return NULL;
5235 for (i = 0; i < 256; i++) {
5236 PyObject *x = PyLong_FromLong(a[i]);
5237 if (x == NULL) {
5238 Py_DECREF(l);
5239 return NULL;
5240 }
5241 PyList_SetItem(l, i, x);
5242 }
5243 for (i = 0; i < 256; i++)
5244 a[i] = 0;
5245 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005246}
5247
5248PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005249_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005250{
5251#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005252 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005253#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005254 int i;
5255 PyObject *l = PyList_New(257);
5256 if (l == NULL) return NULL;
5257 for (i = 0; i < 257; i++) {
5258 PyObject *x = getarray(dxpairs[i]);
5259 if (x == NULL) {
5260 Py_DECREF(l);
5261 return NULL;
5262 }
5263 PyList_SetItem(l, i, x);
5264 }
5265 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005266#endif
5267}
5268
5269#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005270
5271Py_ssize_t
5272_PyEval_RequestCodeExtraIndex(freefunc free)
5273{
5274 PyThreadState *tstate = PyThreadState_Get();
5275 Py_ssize_t new_index;
5276
5277 if (tstate->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
5278 return -1;
5279 }
5280 new_index = tstate->co_extra_user_count++;
5281 tstate->co_extra_freefuncs[new_index] = free;
5282 return new_index;
5283}
Łukasz Langaa785c872016-09-09 17:37:37 -07005284
5285static void
5286dtrace_function_entry(PyFrameObject *f)
5287{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005288 const char *filename;
5289 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005290 int lineno;
5291
5292 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5293 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5294 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5295
5296 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5297}
5298
5299static void
5300dtrace_function_return(PyFrameObject *f)
5301{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005302 const char *filename;
5303 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005304 int lineno;
5305
5306 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5307 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5308 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5309
5310 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5311}
5312
5313/* DTrace equivalent of maybe_call_line_trace. */
5314static void
5315maybe_dtrace_line(PyFrameObject *frame,
5316 int *instr_lb, int *instr_ub, int *instr_prev)
5317{
5318 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005319 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005320
5321 /* If the last instruction executed isn't in the current
5322 instruction window, reset the window.
5323 */
5324 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5325 PyAddrPair bounds;
5326 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5327 &bounds);
5328 *instr_lb = bounds.ap_lower;
5329 *instr_ub = bounds.ap_upper;
5330 }
5331 /* If the last instruction falls at the start of a line or if
5332 it represents a jump backwards, update the frame's line
5333 number and call the trace function. */
5334 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5335 frame->f_lineno = line;
5336 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5337 if (!co_filename)
5338 co_filename = "?";
5339 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5340 if (!co_name)
5341 co_name = "?";
5342 PyDTrace_LINE(co_filename, co_name, line);
5343 }
5344 *instr_prev = frame->f_lasti;
5345}