blob: 1b8cdfc663a0b086f63eaf9b5d638767aad0f1f0 [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 Stinnerd8735722016-09-09 12:36:44 -070040static PyObject * fast_function(PyObject *, PyObject **, Py_ssize_t, PyObject *);
Victor Stinnerf9b760f2016-09-09 10:17:08 -070041static PyObject * do_call_core(PyObject *, PyObject *, PyObject *);
Jeremy Hylton52820442001-01-03 23:52:36 +000042
Guido van Rossum0a066c01992-03-27 17:29:15 +000043#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000044static int lltrace;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020045static int prtrace(PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000046#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010047static int call_trace(Py_tracefunc, PyObject *,
48 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000050static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010051 PyThreadState *, PyFrameObject *,
52 int, PyObject *);
53static void call_exc_trace(Py_tracefunc, PyObject *,
54 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000055static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010056 PyThreadState *, PyFrameObject *, int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070057static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
58static void dtrace_function_entry(PyFrameObject *);
59static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000060
Thomas Wouters477c8d52006-05-27 19:21:47 +000061static PyObject * cmp_outcome(int, PyObject *, PyObject *);
Serhiy Storchaka133138a2016-08-02 22:51:21 +030062static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *, PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +000063static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +000064static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +000065static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +000066static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +020067static PyObject * unicode_concatenate(PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030068 PyFrameObject *, const _Py_CODEUNIT *);
Benjamin Petersonce798522012-01-22 11:24:29 -050069static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Guido van Rossum374a9221991-04-04 10:40:29 +000070
Paul Prescode68140d2000-08-30 20:25:01 +000071#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000073#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000075#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 "free variable '%.200s' referenced before assignment" \
77 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000078
Guido van Rossum950361c1997-01-24 13:49:28 +000079/* Dynamic execution profile */
80#ifdef DYNAMIC_EXECUTION_PROFILE
81#ifdef DXPAIRS
82static long dxpairs[257][256];
83#define dxp dxpairs[256]
84#else
85static long dxp[256];
86#endif
87#endif
88
Benjamin Petersond2be5b42010-09-10 22:47:02 +000089#ifdef WITH_THREAD
90#define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request)
91#else
92#define GIL_REQUEST 0
93#endif
94
Jeffrey Yasskin39370832010-05-03 19:29:34 +000095/* This can set eval_breaker to 0 even though gil_drop_request became
96 1. We believe this is all right because the eval loop will release
97 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +000098#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 _Py_atomic_store_relaxed( \
100 &eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000101 GIL_REQUEST | \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
103 pending_async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000104
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000105#ifdef WITH_THREAD
106
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000107#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 do { \
109 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
110 _Py_atomic_store_relaxed(&eval_breaker, 1); \
111 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000112
113#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 do { \
115 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
116 COMPUTE_EVAL_BREAKER(); \
117 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000118
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000119#endif
120
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000121/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000122#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 do { \
124 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
125 _Py_atomic_store_relaxed(&eval_breaker, 1); \
126 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000127
128#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 do { \
130 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
131 COMPUTE_EVAL_BREAKER(); \
132 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000133
134#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 do { \
136 pending_async_exc = 1; \
137 _Py_atomic_store_relaxed(&eval_breaker, 1); \
138 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000139
140#define UNSIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000142
143
Guido van Rossume59214e1994-08-30 08:01:59 +0000144#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000145
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000146#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000147#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000148#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000149#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000150
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000151static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000152static long main_thread = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000153/* This single variable consolidates all requests to break out of the fast path
154 in the eval loop. */
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000155static _Py_atomic_int eval_breaker = {0};
156/* Request for dropping the GIL */
157static _Py_atomic_int gil_drop_request = {0};
158/* Request for running pending calls. */
159static _Py_atomic_int pendingcalls_to_do = {0};
160/* Request for looking at the `async_exc` field of the current thread state.
161 Guarded by the GIL. */
162static int pending_async_exc = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000163
164#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000165
Tim Peters7f468f22004-10-11 02:40:51 +0000166int
167PyEval_ThreadsInitialized(void)
168{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000170}
171
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000172void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000173PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 if (gil_created())
176 return;
177 create_gil();
178 take_gil(PyThreadState_GET());
179 main_thread = PyThread_get_thread_ident();
180 if (!pending_lock)
181 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000182}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000183
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000184void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000185_PyEval_FiniThreads(void)
186{
187 if (!gil_created())
188 return;
189 destroy_gil();
190 assert(!gil_created());
191}
192
193void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000194PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 PyThreadState *tstate = PyThreadState_GET();
197 if (tstate == NULL)
198 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
199 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000200}
201
202void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000203PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 /* This function must succeed when the current thread state is NULL.
206 We therefore avoid PyThreadState_GET() which dumps a fatal error
207 in debug mode.
208 */
209 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
210 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000211}
212
213void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000214PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 if (tstate == NULL)
217 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
218 /* Check someone has called PyEval_InitThreads() to create the lock */
219 assert(gil_created());
220 take_gil(tstate);
221 if (PyThreadState_Swap(tstate) != NULL)
222 Py_FatalError(
223 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000224}
225
226void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000227PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000228{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 if (tstate == NULL)
230 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
231 if (PyThreadState_Swap(NULL) != tstate)
232 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
233 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000234}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000235
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200236/* This function is called from PyOS_AfterFork to destroy all threads which are
237 * not running in the child process, and clear internal locks which might be
238 * held by those threads. (This could also be done using pthread_atfork
239 * mechanism, at least for the pthreads implementation.) */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000240
241void
242PyEval_ReInitThreads(void)
243{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200244 _Py_IDENTIFIER(_after_fork);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 PyObject *threading, *result;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200246 PyThreadState *current_tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 if (!gil_created())
249 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 recreate_gil();
251 pending_lock = PyThread_allocate_lock();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200252 take_gil(current_tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 /* Update the threading module with the new state.
256 */
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200257 threading = PyMapping_GetItemString(current_tstate->interp->modules,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 "threading");
259 if (threading == NULL) {
260 /* threading not imported */
261 PyErr_Clear();
262 return;
263 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200264 result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 if (result == NULL)
266 PyErr_WriteUnraisable(threading);
267 else
268 Py_DECREF(result);
269 Py_DECREF(threading);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200270
271 /* Destroy all threads except the current one */
272 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000273}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000274
275#else
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000276static _Py_atomic_int eval_breaker = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000277static int pending_async_exc = 0;
278#endif /* WITH_THREAD */
279
280/* This function is used to signal that async exceptions are waiting to be
281 raised, therefore it is also useful in non-threaded builds. */
282
283void
284_PyEval_SignalAsyncExc(void)
285{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000287}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000288
Guido van Rossumff4949e1992-08-05 19:58:53 +0000289/* Functions save_thread and restore_thread are always defined so
290 dynamically loaded modules needn't be compiled separately for use
291 with and without threads: */
292
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000293PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000294PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000295{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 PyThreadState *tstate = PyThreadState_Swap(NULL);
297 if (tstate == NULL)
298 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000299#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 if (gil_created())
301 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000302#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000304}
305
306void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000307PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000308{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 if (tstate == NULL)
310 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000311#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 if (gil_created()) {
313 int err = errno;
314 take_gil(tstate);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200315 /* _Py_Finalizing is protected by the GIL */
316 if (_Py_Finalizing && tstate != _Py_Finalizing) {
317 drop_gil(tstate);
318 PyThread_exit_thread();
319 assert(0); /* unreachable */
320 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 errno = err;
322 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000323#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000325}
326
327
Guido van Rossuma9672091994-09-14 13:31:22 +0000328/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
329 signal handlers or Mac I/O completion routines) can schedule calls
330 to a function to be called synchronously.
331 The synchronous function is called with one void* argument.
332 It should return 0 for success or -1 for failure -- failure should
333 be accompanied by an exception.
334
335 If registry succeeds, the registry function returns 0; if it fails
336 (e.g. due to too many pending calls) it returns -1 (without setting
337 an exception condition).
338
339 Note that because registry may occur from within signal handlers,
340 or other asynchronous events, calling malloc() is unsafe!
341
342#ifdef WITH_THREAD
343 Any thread can schedule pending calls, but only the main thread
344 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000345 There is no facility to schedule calls to a particular thread, but
346 that should be easy to change, should that ever be required. In
347 that case, the static variables here should go into the python
348 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000349#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000350*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000351
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000352#ifdef WITH_THREAD
353
354/* The WITH_THREAD implementation is thread-safe. It allows
355 scheduling to be made from any thread, and even from an executing
356 callback.
357 */
358
359#define NPENDINGCALLS 32
360static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 int (*func)(void *);
362 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000363} pendingcalls[NPENDINGCALLS];
364static int pendingfirst = 0;
365static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000366
367int
368Py_AddPendingCall(int (*func)(void *), void *arg)
369{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 int i, j, result=0;
371 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 /* try a few times for the lock. Since this mechanism is used
374 * for signal handling (on the main thread), there is a (slim)
375 * chance that a signal is delivered on the same thread while we
376 * hold the lock during the Py_MakePendingCalls() function.
377 * This avoids a deadlock in that case.
378 * Note that signals can be delivered on any thread. In particular,
379 * on Windows, a SIGINT is delivered on a system-created worker
380 * thread.
381 * We also check for lock being NULL, in the unlikely case that
382 * this function is called before any bytecode evaluation takes place.
383 */
384 if (lock != NULL) {
385 for (i = 0; i<100; i++) {
386 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
387 break;
388 }
389 if (i == 100)
390 return -1;
391 }
392
393 i = pendinglast;
394 j = (i + 1) % NPENDINGCALLS;
395 if (j == pendingfirst) {
396 result = -1; /* Queue full */
397 } else {
398 pendingcalls[i].func = func;
399 pendingcalls[i].arg = arg;
400 pendinglast = j;
401 }
402 /* signal main loop */
403 SIGNAL_PENDING_CALLS();
404 if (lock != NULL)
405 PyThread_release_lock(lock);
406 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000407}
408
409int
410Py_MakePendingCalls(void)
411{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200412 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 int i;
414 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 if (!pending_lock) {
417 /* initial allocation of the lock */
418 pending_lock = PyThread_allocate_lock();
419 if (pending_lock == NULL)
420 return -1;
421 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 /* only service pending calls on main thread */
424 if (main_thread && PyThread_get_thread_ident() != main_thread)
425 return 0;
426 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200427 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200429 busy = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 /* perform a bounded number of calls, in case of recursion */
431 for (i=0; i<NPENDINGCALLS; i++) {
432 int j;
433 int (*func)(void *);
434 void *arg = NULL;
435
436 /* pop one item off the queue while holding the lock */
437 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
438 j = pendingfirst;
439 if (j == pendinglast) {
440 func = NULL; /* Queue empty */
441 } else {
442 func = pendingcalls[j].func;
443 arg = pendingcalls[j].arg;
444 pendingfirst = (j + 1) % NPENDINGCALLS;
445 }
446 if (pendingfirst != pendinglast)
447 SIGNAL_PENDING_CALLS();
448 else
449 UNSIGNAL_PENDING_CALLS();
450 PyThread_release_lock(pending_lock);
451 /* having released the lock, perform the callback */
452 if (func == NULL)
453 break;
454 r = func(arg);
455 if (r)
456 break;
457 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200458 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000460}
461
462#else /* if ! defined WITH_THREAD */
463
464/*
465 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
466 This code is used for signal handling in python that isn't built
467 with WITH_THREAD.
468 Don't use this implementation when Py_AddPendingCalls() can happen
469 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470
Guido van Rossuma9672091994-09-14 13:31:22 +0000471 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000472 (1) nested asynchronous calls to Py_AddPendingCall()
473 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000475 (1) is very unlikely because typically signal delivery
476 is blocked during signal handling. So it should be impossible.
477 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000478 The current code is safe against (2), but not against (1).
479 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000480 thread is present, interrupted by signals, and that the critical
481 section is protected with the "busy" variable. On Windows, which
482 delivers SIGINT on a system thread, this does not hold and therefore
483 Windows really shouldn't use this version.
484 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000485*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000486
Guido van Rossuma9672091994-09-14 13:31:22 +0000487#define NPENDINGCALLS 32
488static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 int (*func)(void *);
490 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000491} pendingcalls[NPENDINGCALLS];
492static volatile int pendingfirst = 0;
493static volatile int pendinglast = 0;
Benjamin Peterson08ec84c2010-05-30 14:49:32 +0000494static _Py_atomic_int pendingcalls_to_do = {0};
Guido van Rossuma9672091994-09-14 13:31:22 +0000495
496int
Thomas Wouters334fb892000-07-25 12:56:38 +0000497Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 static volatile int busy = 0;
500 int i, j;
501 /* XXX Begin critical section */
502 if (busy)
503 return -1;
504 busy = 1;
505 i = pendinglast;
506 j = (i + 1) % NPENDINGCALLS;
507 if (j == pendingfirst) {
508 busy = 0;
509 return -1; /* Queue full */
510 }
511 pendingcalls[i].func = func;
512 pendingcalls[i].arg = arg;
513 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 SIGNAL_PENDING_CALLS();
516 busy = 0;
517 /* XXX End critical section */
518 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000519}
520
Guido van Rossum180d7b41994-09-29 09:45:57 +0000521int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000522Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000523{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 static int busy = 0;
525 if (busy)
526 return 0;
527 busy = 1;
528 UNSIGNAL_PENDING_CALLS();
529 for (;;) {
530 int i;
531 int (*func)(void *);
532 void *arg;
533 i = pendingfirst;
534 if (i == pendinglast)
535 break; /* Queue empty */
536 func = pendingcalls[i].func;
537 arg = pendingcalls[i].arg;
538 pendingfirst = (i + 1) % NPENDINGCALLS;
539 if (func(arg) < 0) {
540 busy = 0;
541 SIGNAL_PENDING_CALLS(); /* We're not done yet */
542 return -1;
543 }
544 }
545 busy = 0;
546 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000547}
548
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000549#endif /* WITH_THREAD */
550
Guido van Rossuma9672091994-09-14 13:31:22 +0000551
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000552/* The interpreter's recursion limit */
553
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000554#ifndef Py_DEFAULT_RECURSION_LIMIT
555#define Py_DEFAULT_RECURSION_LIMIT 1000
556#endif
557static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
558int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000559
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000560int
561Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000562{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000564}
565
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000566void
567Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000568{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 recursion_limit = new_limit;
570 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000571}
572
Armin Rigo2b3eb402003-10-28 12:05:48 +0000573/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
574 if the recursion_depth reaches _Py_CheckRecursionLimit.
575 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
576 to guarantee that _Py_CheckRecursiveCall() is regularly called.
577 Without USE_STACKCHECK, there is no need for this. */
578int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300579_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000580{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000582
583#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 if (PyOS_CheckStack()) {
585 --tstate->recursion_depth;
586 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
587 return -1;
588 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000589#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 _Py_CheckRecursionLimit = recursion_limit;
591 if (tstate->recursion_critical)
592 /* Somebody asked that we don't check for recursion. */
593 return 0;
594 if (tstate->overflowed) {
595 if (tstate->recursion_depth > recursion_limit + 50) {
596 /* Overflowing while handling an overflow. Give up. */
597 Py_FatalError("Cannot recover from stack overflow.");
598 }
599 return 0;
600 }
601 if (tstate->recursion_depth > recursion_limit) {
602 --tstate->recursion_depth;
603 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400604 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 "maximum recursion depth exceeded%s",
606 where);
607 return -1;
608 }
609 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000610}
611
Guido van Rossum374a9221991-04-04 10:40:29 +0000612/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000613enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000614 WHY_NOT = 0x0001, /* No error */
615 WHY_EXCEPTION = 0x0002, /* Exception occurred */
Stefan Krahb7e10102010-06-23 18:42:39 +0000616 WHY_RETURN = 0x0008, /* 'return' statement */
617 WHY_BREAK = 0x0010, /* 'break' statement */
618 WHY_CONTINUE = 0x0020, /* 'continue' statement */
619 WHY_YIELD = 0x0040, /* 'yield' operator */
620 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000621};
Guido van Rossum374a9221991-04-04 10:40:29 +0000622
Benjamin Peterson87880242011-07-03 16:48:31 -0500623static void save_exc_state(PyThreadState *, PyFrameObject *);
624static void swap_exc_state(PyThreadState *, PyFrameObject *);
625static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400626static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000627static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000628
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000629/* Records whether tracing is on for any thread. Counts the number of
630 threads for which tstate->c_tracefunc is non-NULL, so if the value
631 is 0, we know we don't have to check this thread's c_tracefunc.
632 This speeds up the if statement in PyEval_EvalFrameEx() after
633 fast_next_opcode*/
634static int _Py_TracingPossible = 0;
635
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000636
Guido van Rossum374a9221991-04-04 10:40:29 +0000637
Guido van Rossumb209a111997-04-29 18:18:01 +0000638PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000639PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000640{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 return PyEval_EvalCodeEx(co,
642 globals, locals,
643 (PyObject **)NULL, 0,
644 (PyObject **)NULL, 0,
645 (PyObject **)NULL, 0,
646 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000647}
648
649
650/* Interpreter main loop */
651
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000652PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000653PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 /* This is for backward compatibility with extension modules that
655 used this API; core interpreter code should call
656 PyEval_EvalFrameEx() */
657 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000658}
659
660PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000661PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000662{
Brett Cannon3cebf932016-09-05 15:33:46 -0700663 PyThreadState *tstate = PyThreadState_GET();
664 return tstate->interp->eval_frame(f, throwflag);
665}
666
Victor Stinnerc6944e72016-11-11 02:13:35 +0100667PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700668_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
669{
Guido van Rossum950361c1997-01-24 13:49:28 +0000670#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000672#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200673 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300674 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200675 int opcode; /* Current opcode */
676 int oparg; /* Current opcode argument, if any */
677 enum why_code why; /* Reason for block stack unwind */
678 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 PyObject *retval = NULL; /* Return value */
680 PyThreadState *tstate = PyThreadState_GET();
681 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 is true when the line being executed has changed. The
688 initial values are such as to make this false the first
689 time it is tested. */
690 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000691
Serhiy Storchakaab874002016-09-11 13:48:15 +0300692 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 PyObject *names;
694 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000695
Brett Cannon368b4b72012-04-02 12:17:59 -0400696#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200697 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400698#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200699
Antoine Pitroub52ec782009-01-25 16:34:23 +0000700/* Computed GOTOs, or
701 the-optimization-commonly-but-improperly-known-as-"threaded code"
702 using gcc's labels-as-values extension
703 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
704
705 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000707 combined with a lookup table of jump addresses. However, since the
708 indirect jump instruction is shared by all opcodes, the CPU will have a
709 hard time making the right prediction for where to jump next (actually,
710 it will be always wrong except in the uncommon case of a sequence of
711 several identical opcodes).
712
713 "Threaded code" in contrast, uses an explicit jump table and an explicit
714 indirect jump instruction at the end of each opcode. Since the jump
715 instruction is at a different address for each opcode, the CPU will make a
716 separate prediction for each of these instructions, which is equivalent to
717 predicting the second opcode of each opcode pair. These predictions have
718 a much better chance to turn out valid, especially in small bytecode loops.
719
720 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000722 and potentially many more instructions (depending on the pipeline width).
723 A correctly predicted branch, however, is nearly free.
724
725 At the time of this writing, the "threaded code" version is up to 15-20%
726 faster than the normal "switch" version, depending on the compiler and the
727 CPU architecture.
728
729 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
730 because it would render the measurements invalid.
731
732
733 NOTE: care must be taken that the compiler doesn't try to "optimize" the
734 indirect jumps by sharing them between all opcodes. Such optimizations
735 can be disabled on gcc by using the -fno-gcse flag (or possibly
736 -fno-crossjumping).
737*/
738
Antoine Pitrou042b1282010-08-13 21:15:58 +0000739#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000740#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000741#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000742#endif
743
Antoine Pitrou042b1282010-08-13 21:15:58 +0000744#ifdef HAVE_COMPUTED_GOTOS
745 #ifndef USE_COMPUTED_GOTOS
746 #define USE_COMPUTED_GOTOS 1
747 #endif
748#else
749 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
750 #error "Computed gotos are not supported on this compiler."
751 #endif
752 #undef USE_COMPUTED_GOTOS
753 #define USE_COMPUTED_GOTOS 0
754#endif
755
756#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000757/* Import the static jump table */
758#include "opcode_targets.h"
759
Antoine Pitroub52ec782009-01-25 16:34:23 +0000760#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 TARGET_##op: \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000763
Antoine Pitroub52ec782009-01-25 16:34:23 +0000764#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 { \
766 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
767 FAST_DISPATCH(); \
768 } \
769 continue; \
770 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000771
772#ifdef LLTRACE
773#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700775 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300777 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300778 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 } \
780 goto fast_next_opcode; \
781 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000782#else
783#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700785 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300787 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300788 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 } \
790 goto fast_next_opcode; \
791 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000792#endif
793
794#else
795#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 case op:
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300797
Antoine Pitroub52ec782009-01-25 16:34:23 +0000798#define DISPATCH() continue
799#define FAST_DISPATCH() goto fast_next_opcode
800#endif
801
802
Neal Norwitza81d2202002-07-14 00:27:26 +0000803/* Tuple access macros */
804
805#ifndef Py_DEBUG
806#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
807#else
808#define GETITEM(v, i) PyTuple_GetItem((v), (i))
809#endif
810
Guido van Rossum374a9221991-04-04 10:40:29 +0000811/* Code access macros */
812
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300813/* The integer overflow is checked by an assertion below. */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300814#define INSTR_OFFSET() (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300815#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300816 _Py_CODEUNIT word = *next_instr; \
817 opcode = _Py_OPCODE(word); \
818 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300819 next_instr++; \
820 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300821#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
822#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000823
Raymond Hettingerf606f872003-03-16 03:11:04 +0000824/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 Some opcodes tend to come in pairs thus making it possible to
826 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300827 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 Verifying the prediction costs a single high-speed test of a register
830 variable against a constant. If the pairing was good, then the
831 processor's own internal branch predication has a high likelihood of
832 success, resulting in a nearly zero-overhead transition to the
833 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300834 including its unpredictable switch-case branch. Combined with the
835 processor's internal branch prediction, a successful PREDICT has the
836 effect of making the two opcodes run as if they were a single new opcode
837 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000838
Georg Brandl86b2fb92008-07-16 03:43:04 +0000839 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 predictions turned-on and interpret the results as if some opcodes
841 had been combined or turn-off predictions so that the opcode frequency
842 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000843
844 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 the CPU to record separate branch prediction information for each
846 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000847
Raymond Hettingerf606f872003-03-16 03:11:04 +0000848*/
849
Antoine Pitrou042b1282010-08-13 21:15:58 +0000850#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000852#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300853#define PREDICT(op) \
854 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300855 _Py_CODEUNIT word = *next_instr; \
856 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300857 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300858 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300859 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300860 goto PRED_##op; \
861 } \
862 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000863#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300864#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000865
Raymond Hettingerf606f872003-03-16 03:11:04 +0000866
Guido van Rossum374a9221991-04-04 10:40:29 +0000867/* Stack manipulation macros */
868
Martin v. Löwis18e16552006-02-15 17:27:45 +0000869/* The stack can grow at most MAXINT deep, as co_nlocals and
870 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000871#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
872#define EMPTY() (STACK_LEVEL() == 0)
873#define TOP() (stack_pointer[-1])
874#define SECOND() (stack_pointer[-2])
875#define THIRD() (stack_pointer[-3])
876#define FOURTH() (stack_pointer[-4])
877#define PEEK(n) (stack_pointer[-(n)])
878#define SET_TOP(v) (stack_pointer[-1] = (v))
879#define SET_SECOND(v) (stack_pointer[-2] = (v))
880#define SET_THIRD(v) (stack_pointer[-3] = (v))
881#define SET_FOURTH(v) (stack_pointer[-4] = (v))
882#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
883#define BASIC_STACKADJ(n) (stack_pointer += n)
884#define BASIC_PUSH(v) (*stack_pointer++ = (v))
885#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000886
Guido van Rossum96a42c81992-01-12 02:29:51 +0000887#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000889 lltrace && prtrace(TOP(), "push")); \
890 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000892 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000894 lltrace && prtrace(TOP(), "stackadj")); \
895 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +0000896#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000897 prtrace((STACK_POINTER)[-1], "ext_pop")), \
898 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000899#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000900#define PUSH(v) BASIC_PUSH(v)
901#define POP() BASIC_POP()
902#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000903#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000904#endif
905
Guido van Rossum681d79a1995-07-18 14:51:37 +0000906/* Local variable macros */
907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000909
910/* The SETLOCAL() macro must not DECREF the local variable in-place and
911 then store the new value; it must copy the old value to a temporary
912 value, then store the new value, and then DECREF the temporary value.
913 This is because it is possible that during the DECREF the frame is
914 accessed by other code (e.g. a __del__ method or gc.collect()) and the
915 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000917 GETLOCAL(i) = value; \
918 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000919
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000920
921#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 while (STACK_LEVEL() > (b)->b_level) { \
923 PyObject *v = POP(); \
924 Py_XDECREF(v); \
925 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000926
927#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300928 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 PyObject *type, *value, *traceback; \
930 assert(STACK_LEVEL() >= (b)->b_level + 3); \
931 while (STACK_LEVEL() > (b)->b_level + 3) { \
932 value = POP(); \
933 Py_XDECREF(value); \
934 } \
935 type = tstate->exc_type; \
936 value = tstate->exc_value; \
937 traceback = tstate->exc_traceback; \
938 tstate->exc_type = POP(); \
939 tstate->exc_value = POP(); \
940 tstate->exc_traceback = POP(); \
941 Py_XDECREF(type); \
942 Py_XDECREF(value); \
943 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300944 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000945
Guido van Rossuma027efa1997-05-05 20:56:21 +0000946/* Start of code */
947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 /* push frame */
949 if (Py_EnterRecursiveCall(""))
950 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 if (tstate->use_tracing) {
955 if (tstate->c_tracefunc != NULL) {
956 /* tstate->c_tracefunc, if defined, is a
957 function that will be called on *every* entry
958 to a code block. Its return value, if not
959 None, is a function that will be called at
960 the start of each executed line of code.
961 (Actually, the function must return itself
962 in order to continue tracing.) The trace
963 functions are called with three arguments:
964 a pointer to the current frame, a string
965 indicating why the function is called, and
966 an argument which depends on the situation.
967 The global trace function is also called
968 whenever an exception is detected. */
969 if (call_trace_protected(tstate->c_tracefunc,
970 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100971 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 /* Trace function raised an error */
973 goto exit_eval_frame;
974 }
975 }
976 if (tstate->c_profilefunc != NULL) {
977 /* Similar for c_profilefunc, except it needn't
978 return itself and isn't called for "line" events */
979 if (call_trace_protected(tstate->c_profilefunc,
980 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100981 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 /* Profile function raised an error */
983 goto exit_eval_frame;
984 }
985 }
986 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000987
Łukasz Langaa785c872016-09-09 17:37:37 -0700988 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
989 dtrace_function_entry(f);
990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 co = f->f_code;
992 names = co->co_names;
993 consts = co->co_consts;
994 fastlocals = f->f_localsplus;
995 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300996 assert(PyBytes_Check(co->co_code));
997 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +0300998 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
999 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1000 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001001 /*
1002 f->f_lasti refers to the index of the last instruction,
1003 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001004
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001005 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001006 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 When the PREDICT() macros are enabled, some opcode pairs follow in
1009 direct succession without updating f->f_lasti. A successful
1010 prediction effectively links the two codes together as if they
1011 were a single new opcode; accordingly,f->f_lasti will point to
1012 the first code in the pair (for instance, GET_ITER followed by
1013 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001014 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001016 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001017 next_instr = first_instr;
1018 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001019 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1020 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001021 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 stack_pointer = f->f_stacktop;
1023 assert(stack_pointer != NULL);
1024 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001025 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001026
Yury Selivanoveb636452016-09-08 22:01:51 -07001027 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01001028 if (!throwflag && f->f_exc_type != NULL && f->f_exc_type != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 /* We were in an except handler when we left,
1030 restore the exception state which was put aside
1031 (see YIELD_VALUE). */
Benjamin Peterson87880242011-07-03 16:48:31 -05001032 swap_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 }
Benjamin Peterson87880242011-07-03 16:48:31 -05001034 else
1035 save_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001037
Tim Peters5ca576e2001-06-18 22:08:13 +00001038#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001039 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001040#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 why = WHY_NOT;
Guido van Rossumac7be682001-01-17 15:42:30 +00001043
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001044 if (throwflag) /* support for generator.throw() */
1045 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001046
Victor Stinnerace47d72013-07-18 01:41:08 +02001047#ifdef Py_DEBUG
1048 /* PyEval_EvalFrameEx() must not be called with an exception set,
1049 because it may clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001050 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +02001051 assert(!PyErr_Occurred());
1052#endif
1053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1056 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +02001057 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 /* Do periodic things. Doing this every time through
1060 the loop would add too much overhead, so we do it
1061 only every Nth instruction. We also do it if
1062 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1063 event needs attention (e.g. a signal handler or
1064 async I/O handler); see Py_AddPendingCall() and
1065 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 if (_Py_atomic_load_relaxed(&eval_breaker)) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001068 if (_Py_OPCODE(*next_instr) == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 /* Make the last opcode before
Ezio Melotti13925002011-03-16 11:05:33 +02001070 a try: finally: block uninterruptible. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 goto fast_next_opcode;
1072 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001074 if (Py_MakePendingCalls() < 0)
1075 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001077#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001078 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 /* Give another thread a chance */
1080 if (PyThreadState_Swap(NULL) != tstate)
1081 Py_FatalError("ceval: tstate mix-up");
1082 drop_gil(tstate);
1083
1084 /* Other threads may run now */
1085
1086 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001087
1088 /* Check if we should make a quick exit. */
1089 if (_Py_Finalizing && _Py_Finalizing != tstate) {
1090 drop_gil(tstate);
1091 PyThread_exit_thread();
1092 }
1093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 if (PyThreadState_Swap(tstate) != NULL)
1095 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001097#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 /* Check for asynchronous exceptions. */
1099 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001100 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 tstate->async_exc = NULL;
1102 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001103 PyErr_SetNone(exc);
1104 Py_DECREF(exc);
1105 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 }
1107 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 fast_next_opcode:
1110 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001111
Łukasz Langaa785c872016-09-09 17:37:37 -07001112 if (PyDTrace_LINE_ENABLED())
1113 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001118 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001119 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 /* see maybe_call_line_trace
1121 for expository comments */
1122 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001123
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 err = maybe_call_line_trace(tstate->c_tracefunc,
1125 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001126 tstate, f,
1127 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 /* Reload possibly changed frame fields */
1129 JUMPTO(f->f_lasti);
1130 if (f->f_stacktop != NULL) {
1131 stack_pointer = f->f_stacktop;
1132 f->f_stacktop = NULL;
1133 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001134 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001136 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001140
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001141 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001142 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001143#ifdef DYNAMIC_EXECUTION_PROFILE
1144#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 dxpairs[lastopcode][opcode]++;
1146 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001147#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001149#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001150
Guido van Rossum96a42c81992-01-12 02:29:51 +00001151#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 if (lltrace) {
1155 if (HAS_ARG(opcode)) {
1156 printf("%d: %d, %d\n",
1157 f->f_lasti, opcode, oparg);
1158 }
1159 else {
1160 printf("%d: %d\n",
1161 f->f_lasti, opcode);
1162 }
1163 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001164#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 /* BEWARE!
1169 It is essential that any operation that fails sets either
1170 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1171 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 TARGET(NOP)
1174 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001175
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001176 TARGET(LOAD_FAST) {
1177 PyObject *value = GETLOCAL(oparg);
1178 if (value == NULL) {
1179 format_exc_check_arg(PyExc_UnboundLocalError,
1180 UNBOUNDLOCAL_ERROR_MSG,
1181 PyTuple_GetItem(co->co_varnames, oparg));
1182 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001184 Py_INCREF(value);
1185 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001187 }
1188
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001189 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001190 TARGET(LOAD_CONST) {
1191 PyObject *value = GETITEM(consts, oparg);
1192 Py_INCREF(value);
1193 PUSH(value);
1194 FAST_DISPATCH();
1195 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001196
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001197 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001198 TARGET(STORE_FAST) {
1199 PyObject *value = POP();
1200 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001202 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001203
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001204 TARGET(POP_TOP) {
1205 PyObject *value = POP();
1206 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001208 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001209
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001210 TARGET(ROT_TWO) {
1211 PyObject *top = TOP();
1212 PyObject *second = SECOND();
1213 SET_TOP(second);
1214 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001216 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001217
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001218 TARGET(ROT_THREE) {
1219 PyObject *top = TOP();
1220 PyObject *second = SECOND();
1221 PyObject *third = THIRD();
1222 SET_TOP(second);
1223 SET_SECOND(third);
1224 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001226 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001227
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001228 TARGET(DUP_TOP) {
1229 PyObject *top = TOP();
1230 Py_INCREF(top);
1231 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001233 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001234
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001235 TARGET(DUP_TOP_TWO) {
1236 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001237 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001238 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001239 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001240 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001241 SET_TOP(top);
1242 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001243 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001244 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001245
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001246 TARGET(UNARY_POSITIVE) {
1247 PyObject *value = TOP();
1248 PyObject *res = PyNumber_Positive(value);
1249 Py_DECREF(value);
1250 SET_TOP(res);
1251 if (res == NULL)
1252 goto error;
1253 DISPATCH();
1254 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001255
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001256 TARGET(UNARY_NEGATIVE) {
1257 PyObject *value = TOP();
1258 PyObject *res = PyNumber_Negative(value);
1259 Py_DECREF(value);
1260 SET_TOP(res);
1261 if (res == NULL)
1262 goto error;
1263 DISPATCH();
1264 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001265
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001266 TARGET(UNARY_NOT) {
1267 PyObject *value = TOP();
1268 int err = PyObject_IsTrue(value);
1269 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 if (err == 0) {
1271 Py_INCREF(Py_True);
1272 SET_TOP(Py_True);
1273 DISPATCH();
1274 }
1275 else if (err > 0) {
1276 Py_INCREF(Py_False);
1277 SET_TOP(Py_False);
1278 err = 0;
1279 DISPATCH();
1280 }
1281 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001282 goto error;
1283 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001284
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001285 TARGET(UNARY_INVERT) {
1286 PyObject *value = TOP();
1287 PyObject *res = PyNumber_Invert(value);
1288 Py_DECREF(value);
1289 SET_TOP(res);
1290 if (res == NULL)
1291 goto error;
1292 DISPATCH();
1293 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001294
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001295 TARGET(BINARY_POWER) {
1296 PyObject *exp = POP();
1297 PyObject *base = TOP();
1298 PyObject *res = PyNumber_Power(base, exp, Py_None);
1299 Py_DECREF(base);
1300 Py_DECREF(exp);
1301 SET_TOP(res);
1302 if (res == NULL)
1303 goto error;
1304 DISPATCH();
1305 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001306
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001307 TARGET(BINARY_MULTIPLY) {
1308 PyObject *right = POP();
1309 PyObject *left = TOP();
1310 PyObject *res = PyNumber_Multiply(left, right);
1311 Py_DECREF(left);
1312 Py_DECREF(right);
1313 SET_TOP(res);
1314 if (res == NULL)
1315 goto error;
1316 DISPATCH();
1317 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001318
Benjamin Petersond51374e2014-04-09 23:55:56 -04001319 TARGET(BINARY_MATRIX_MULTIPLY) {
1320 PyObject *right = POP();
1321 PyObject *left = TOP();
1322 PyObject *res = PyNumber_MatrixMultiply(left, right);
1323 Py_DECREF(left);
1324 Py_DECREF(right);
1325 SET_TOP(res);
1326 if (res == NULL)
1327 goto error;
1328 DISPATCH();
1329 }
1330
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001331 TARGET(BINARY_TRUE_DIVIDE) {
1332 PyObject *divisor = POP();
1333 PyObject *dividend = TOP();
1334 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1335 Py_DECREF(dividend);
1336 Py_DECREF(divisor);
1337 SET_TOP(quotient);
1338 if (quotient == NULL)
1339 goto error;
1340 DISPATCH();
1341 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001342
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001343 TARGET(BINARY_FLOOR_DIVIDE) {
1344 PyObject *divisor = POP();
1345 PyObject *dividend = TOP();
1346 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1347 Py_DECREF(dividend);
1348 Py_DECREF(divisor);
1349 SET_TOP(quotient);
1350 if (quotient == NULL)
1351 goto error;
1352 DISPATCH();
1353 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001354
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001355 TARGET(BINARY_MODULO) {
1356 PyObject *divisor = POP();
1357 PyObject *dividend = TOP();
1358 PyObject *res = PyUnicode_CheckExact(dividend) ?
1359 PyUnicode_Format(dividend, divisor) :
1360 PyNumber_Remainder(dividend, divisor);
1361 Py_DECREF(divisor);
1362 Py_DECREF(dividend);
1363 SET_TOP(res);
1364 if (res == NULL)
1365 goto error;
1366 DISPATCH();
1367 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001368
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001369 TARGET(BINARY_ADD) {
1370 PyObject *right = POP();
1371 PyObject *left = TOP();
1372 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001373 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1374 CPython using bytecode, it is simply worthless.
1375 See http://bugs.python.org/issue21955 and
1376 http://bugs.python.org/issue10044 for the discussion. In short,
1377 no patch shown any impact on a realistic benchmark, only a minor
1378 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001379 if (PyUnicode_CheckExact(left) &&
1380 PyUnicode_CheckExact(right)) {
1381 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001382 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001383 }
1384 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001385 sum = PyNumber_Add(left, right);
1386 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001387 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001388 Py_DECREF(right);
1389 SET_TOP(sum);
1390 if (sum == NULL)
1391 goto error;
1392 DISPATCH();
1393 }
1394
1395 TARGET(BINARY_SUBTRACT) {
1396 PyObject *right = POP();
1397 PyObject *left = TOP();
1398 PyObject *diff = PyNumber_Subtract(left, right);
1399 Py_DECREF(right);
1400 Py_DECREF(left);
1401 SET_TOP(diff);
1402 if (diff == NULL)
1403 goto error;
1404 DISPATCH();
1405 }
1406
1407 TARGET(BINARY_SUBSCR) {
1408 PyObject *sub = POP();
1409 PyObject *container = TOP();
1410 PyObject *res = PyObject_GetItem(container, sub);
1411 Py_DECREF(container);
1412 Py_DECREF(sub);
1413 SET_TOP(res);
1414 if (res == NULL)
1415 goto error;
1416 DISPATCH();
1417 }
1418
1419 TARGET(BINARY_LSHIFT) {
1420 PyObject *right = POP();
1421 PyObject *left = TOP();
1422 PyObject *res = PyNumber_Lshift(left, right);
1423 Py_DECREF(left);
1424 Py_DECREF(right);
1425 SET_TOP(res);
1426 if (res == NULL)
1427 goto error;
1428 DISPATCH();
1429 }
1430
1431 TARGET(BINARY_RSHIFT) {
1432 PyObject *right = POP();
1433 PyObject *left = TOP();
1434 PyObject *res = PyNumber_Rshift(left, right);
1435 Py_DECREF(left);
1436 Py_DECREF(right);
1437 SET_TOP(res);
1438 if (res == NULL)
1439 goto error;
1440 DISPATCH();
1441 }
1442
1443 TARGET(BINARY_AND) {
1444 PyObject *right = POP();
1445 PyObject *left = TOP();
1446 PyObject *res = PyNumber_And(left, right);
1447 Py_DECREF(left);
1448 Py_DECREF(right);
1449 SET_TOP(res);
1450 if (res == NULL)
1451 goto error;
1452 DISPATCH();
1453 }
1454
1455 TARGET(BINARY_XOR) {
1456 PyObject *right = POP();
1457 PyObject *left = TOP();
1458 PyObject *res = PyNumber_Xor(left, right);
1459 Py_DECREF(left);
1460 Py_DECREF(right);
1461 SET_TOP(res);
1462 if (res == NULL)
1463 goto error;
1464 DISPATCH();
1465 }
1466
1467 TARGET(BINARY_OR) {
1468 PyObject *right = POP();
1469 PyObject *left = TOP();
1470 PyObject *res = PyNumber_Or(left, right);
1471 Py_DECREF(left);
1472 Py_DECREF(right);
1473 SET_TOP(res);
1474 if (res == NULL)
1475 goto error;
1476 DISPATCH();
1477 }
1478
1479 TARGET(LIST_APPEND) {
1480 PyObject *v = POP();
1481 PyObject *list = PEEK(oparg);
1482 int err;
1483 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001485 if (err != 0)
1486 goto error;
1487 PREDICT(JUMP_ABSOLUTE);
1488 DISPATCH();
1489 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001490
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001491 TARGET(SET_ADD) {
1492 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001493 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001494 int err;
1495 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001497 if (err != 0)
1498 goto error;
1499 PREDICT(JUMP_ABSOLUTE);
1500 DISPATCH();
1501 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001502
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001503 TARGET(INPLACE_POWER) {
1504 PyObject *exp = POP();
1505 PyObject *base = TOP();
1506 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1507 Py_DECREF(base);
1508 Py_DECREF(exp);
1509 SET_TOP(res);
1510 if (res == NULL)
1511 goto error;
1512 DISPATCH();
1513 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001514
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001515 TARGET(INPLACE_MULTIPLY) {
1516 PyObject *right = POP();
1517 PyObject *left = TOP();
1518 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1519 Py_DECREF(left);
1520 Py_DECREF(right);
1521 SET_TOP(res);
1522 if (res == NULL)
1523 goto error;
1524 DISPATCH();
1525 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001526
Benjamin Petersond51374e2014-04-09 23:55:56 -04001527 TARGET(INPLACE_MATRIX_MULTIPLY) {
1528 PyObject *right = POP();
1529 PyObject *left = TOP();
1530 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1531 Py_DECREF(left);
1532 Py_DECREF(right);
1533 SET_TOP(res);
1534 if (res == NULL)
1535 goto error;
1536 DISPATCH();
1537 }
1538
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001539 TARGET(INPLACE_TRUE_DIVIDE) {
1540 PyObject *divisor = POP();
1541 PyObject *dividend = TOP();
1542 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1543 Py_DECREF(dividend);
1544 Py_DECREF(divisor);
1545 SET_TOP(quotient);
1546 if (quotient == NULL)
1547 goto error;
1548 DISPATCH();
1549 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001550
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001551 TARGET(INPLACE_FLOOR_DIVIDE) {
1552 PyObject *divisor = POP();
1553 PyObject *dividend = TOP();
1554 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1555 Py_DECREF(dividend);
1556 Py_DECREF(divisor);
1557 SET_TOP(quotient);
1558 if (quotient == NULL)
1559 goto error;
1560 DISPATCH();
1561 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001562
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001563 TARGET(INPLACE_MODULO) {
1564 PyObject *right = POP();
1565 PyObject *left = TOP();
1566 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1567 Py_DECREF(left);
1568 Py_DECREF(right);
1569 SET_TOP(mod);
1570 if (mod == NULL)
1571 goto error;
1572 DISPATCH();
1573 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001574
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001575 TARGET(INPLACE_ADD) {
1576 PyObject *right = POP();
1577 PyObject *left = TOP();
1578 PyObject *sum;
1579 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1580 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001581 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001582 }
1583 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001584 sum = PyNumber_InPlaceAdd(left, right);
1585 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001586 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001587 Py_DECREF(right);
1588 SET_TOP(sum);
1589 if (sum == NULL)
1590 goto error;
1591 DISPATCH();
1592 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001593
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001594 TARGET(INPLACE_SUBTRACT) {
1595 PyObject *right = POP();
1596 PyObject *left = TOP();
1597 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1598 Py_DECREF(left);
1599 Py_DECREF(right);
1600 SET_TOP(diff);
1601 if (diff == NULL)
1602 goto error;
1603 DISPATCH();
1604 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001605
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001606 TARGET(INPLACE_LSHIFT) {
1607 PyObject *right = POP();
1608 PyObject *left = TOP();
1609 PyObject *res = PyNumber_InPlaceLshift(left, right);
1610 Py_DECREF(left);
1611 Py_DECREF(right);
1612 SET_TOP(res);
1613 if (res == NULL)
1614 goto error;
1615 DISPATCH();
1616 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001617
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001618 TARGET(INPLACE_RSHIFT) {
1619 PyObject *right = POP();
1620 PyObject *left = TOP();
1621 PyObject *res = PyNumber_InPlaceRshift(left, right);
1622 Py_DECREF(left);
1623 Py_DECREF(right);
1624 SET_TOP(res);
1625 if (res == NULL)
1626 goto error;
1627 DISPATCH();
1628 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001629
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001630 TARGET(INPLACE_AND) {
1631 PyObject *right = POP();
1632 PyObject *left = TOP();
1633 PyObject *res = PyNumber_InPlaceAnd(left, right);
1634 Py_DECREF(left);
1635 Py_DECREF(right);
1636 SET_TOP(res);
1637 if (res == NULL)
1638 goto error;
1639 DISPATCH();
1640 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001641
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001642 TARGET(INPLACE_XOR) {
1643 PyObject *right = POP();
1644 PyObject *left = TOP();
1645 PyObject *res = PyNumber_InPlaceXor(left, right);
1646 Py_DECREF(left);
1647 Py_DECREF(right);
1648 SET_TOP(res);
1649 if (res == NULL)
1650 goto error;
1651 DISPATCH();
1652 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001653
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001654 TARGET(INPLACE_OR) {
1655 PyObject *right = POP();
1656 PyObject *left = TOP();
1657 PyObject *res = PyNumber_InPlaceOr(left, right);
1658 Py_DECREF(left);
1659 Py_DECREF(right);
1660 SET_TOP(res);
1661 if (res == NULL)
1662 goto error;
1663 DISPATCH();
1664 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001665
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001666 TARGET(STORE_SUBSCR) {
1667 PyObject *sub = TOP();
1668 PyObject *container = SECOND();
1669 PyObject *v = THIRD();
1670 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 STACKADJ(-3);
Martin Panter95f53c12016-07-18 08:23:26 +00001672 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001673 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001675 Py_DECREF(container);
1676 Py_DECREF(sub);
1677 if (err != 0)
1678 goto error;
1679 DISPATCH();
1680 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001681
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001682 TARGET(STORE_ANNOTATION) {
1683 _Py_IDENTIFIER(__annotations__);
1684 PyObject *ann_dict;
1685 PyObject *ann = POP();
1686 PyObject *name = GETITEM(names, oparg);
1687 int err;
1688 if (f->f_locals == NULL) {
1689 PyErr_Format(PyExc_SystemError,
1690 "no locals found when storing annotation");
1691 Py_DECREF(ann);
1692 goto error;
1693 }
1694 /* first try to get __annotations__ from locals... */
1695 if (PyDict_CheckExact(f->f_locals)) {
1696 ann_dict = _PyDict_GetItemId(f->f_locals,
1697 &PyId___annotations__);
1698 if (ann_dict == NULL) {
1699 PyErr_SetString(PyExc_NameError,
1700 "__annotations__ not found");
1701 Py_DECREF(ann);
1702 goto error;
1703 }
1704 Py_INCREF(ann_dict);
1705 }
1706 else {
1707 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
1708 if (ann_str == NULL) {
1709 Py_DECREF(ann);
1710 goto error;
1711 }
1712 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
1713 if (ann_dict == NULL) {
1714 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
1715 PyErr_SetString(PyExc_NameError,
1716 "__annotations__ not found");
1717 }
1718 Py_DECREF(ann);
1719 goto error;
1720 }
1721 }
1722 /* ...if succeeded, __annotations__[name] = ann */
1723 if (PyDict_CheckExact(ann_dict)) {
1724 err = PyDict_SetItem(ann_dict, name, ann);
1725 }
1726 else {
1727 err = PyObject_SetItem(ann_dict, name, ann);
1728 }
1729 Py_DECREF(ann_dict);
Yury Selivanov50c584f2016-09-08 23:38:21 -07001730 Py_DECREF(ann);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001731 if (err != 0) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001732 goto error;
1733 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001734 DISPATCH();
1735 }
1736
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001737 TARGET(DELETE_SUBSCR) {
1738 PyObject *sub = TOP();
1739 PyObject *container = SECOND();
1740 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 STACKADJ(-2);
Martin Panter95f53c12016-07-18 08:23:26 +00001742 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001743 err = PyObject_DelItem(container, sub);
1744 Py_DECREF(container);
1745 Py_DECREF(sub);
1746 if (err != 0)
1747 goto error;
1748 DISPATCH();
1749 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001750
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001751 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001752 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001753 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001754 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001755 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001756 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 PyErr_SetString(PyExc_RuntimeError,
1758 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001759 Py_DECREF(value);
1760 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001762 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001763 Py_DECREF(value);
1764 if (res == NULL)
1765 goto error;
1766 Py_DECREF(res);
1767 DISPATCH();
1768 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001769
Thomas Wouters434d0822000-08-24 20:11:32 +00001770#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001772#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001773 TARGET(RAISE_VARARGS) {
1774 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 switch (oparg) {
1776 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001777 cause = POP(); /* cause */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001779 exc = POP(); /* exc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001780 case 0: /* Fallthrough */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001781 if (do_raise(exc, cause)) {
1782 why = WHY_EXCEPTION;
1783 goto fast_block_end;
1784 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 break;
1786 default:
1787 PyErr_SetString(PyExc_SystemError,
1788 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 break;
1790 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001791 goto error;
1792 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001793
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001794 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 retval = POP();
1796 why = WHY_RETURN;
1797 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001798 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001799
Yury Selivanov75445082015-05-11 22:57:16 -04001800 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001801 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001802 PyObject *iter = NULL;
1803 PyObject *awaitable = NULL;
1804 PyObject *obj = TOP();
1805 PyTypeObject *type = Py_TYPE(obj);
1806
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001807 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001808 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001809 }
Yury Selivanov75445082015-05-11 22:57:16 -04001810
1811 if (getter != NULL) {
1812 iter = (*getter)(obj);
1813 Py_DECREF(obj);
1814 if (iter == NULL) {
1815 SET_TOP(NULL);
1816 goto error;
1817 }
1818 }
1819 else {
1820 SET_TOP(NULL);
1821 PyErr_Format(
1822 PyExc_TypeError,
1823 "'async for' requires an object with "
1824 "__aiter__ method, got %.100s",
1825 type->tp_name);
1826 Py_DECREF(obj);
1827 goto error;
1828 }
1829
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001830 if (Py_TYPE(iter)->tp_as_async != NULL &&
1831 Py_TYPE(iter)->tp_as_async->am_anext != NULL) {
1832
1833 /* Starting with CPython 3.5.2 __aiter__ should return
1834 asynchronous iterators directly (not awaitables that
1835 resolve to asynchronous iterators.)
1836
1837 Therefore, we check if the object that was returned
1838 from __aiter__ has an __anext__ method. If it does,
1839 we wrap it in an awaitable that resolves to `iter`.
1840
1841 See http://bugs.python.org/issue27243 for more
1842 details.
1843 */
1844
1845 PyObject *wrapper = _PyAIterWrapper_New(iter);
1846 Py_DECREF(iter);
1847 SET_TOP(wrapper);
1848 DISPATCH();
1849 }
1850
Yury Selivanov5376ba92015-06-22 12:19:30 -04001851 awaitable = _PyCoro_GetAwaitableIter(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001852 if (awaitable == NULL) {
1853 SET_TOP(NULL);
1854 PyErr_Format(
1855 PyExc_TypeError,
1856 "'async for' received an invalid object "
1857 "from __aiter__: %.100s",
1858 Py_TYPE(iter)->tp_name);
1859
1860 Py_DECREF(iter);
1861 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001862 } else {
Yury Selivanov75445082015-05-11 22:57:16 -04001863 Py_DECREF(iter);
1864
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001865 if (PyErr_WarnFormat(
Yury Selivanov2edd8a12016-11-08 15:13:07 -05001866 PyExc_DeprecationWarning, 1,
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001867 "'%.100s' implements legacy __aiter__ protocol; "
1868 "__aiter__ should return an asynchronous "
1869 "iterator, not awaitable",
1870 type->tp_name))
1871 {
1872 /* Warning was converted to an error. */
1873 Py_DECREF(awaitable);
1874 SET_TOP(NULL);
1875 goto error;
1876 }
1877 }
1878
Yury Selivanov75445082015-05-11 22:57:16 -04001879 SET_TOP(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001880 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001881 DISPATCH();
1882 }
1883
1884 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001885 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001886 PyObject *next_iter = NULL;
1887 PyObject *awaitable = NULL;
1888 PyObject *aiter = TOP();
1889 PyTypeObject *type = Py_TYPE(aiter);
1890
Yury Selivanoveb636452016-09-08 22:01:51 -07001891 if (PyAsyncGen_CheckExact(aiter)) {
1892 awaitable = type->tp_as_async->am_anext(aiter);
1893 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001894 goto error;
1895 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001896 } else {
1897 if (type->tp_as_async != NULL){
1898 getter = type->tp_as_async->am_anext;
1899 }
Yury Selivanov75445082015-05-11 22:57:16 -04001900
Yury Selivanoveb636452016-09-08 22:01:51 -07001901 if (getter != NULL) {
1902 next_iter = (*getter)(aiter);
1903 if (next_iter == NULL) {
1904 goto error;
1905 }
1906 }
1907 else {
1908 PyErr_Format(
1909 PyExc_TypeError,
1910 "'async for' requires an iterator with "
1911 "__anext__ method, got %.100s",
1912 type->tp_name);
1913 goto error;
1914 }
Yury Selivanov75445082015-05-11 22:57:16 -04001915
Yury Selivanoveb636452016-09-08 22:01:51 -07001916 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1917 if (awaitable == NULL) {
1918 PyErr_Format(
1919 PyExc_TypeError,
1920 "'async for' received an invalid object "
1921 "from __anext__: %.100s",
1922 Py_TYPE(next_iter)->tp_name);
1923
1924 Py_DECREF(next_iter);
1925 goto error;
1926 } else {
1927 Py_DECREF(next_iter);
1928 }
1929 }
Yury Selivanov75445082015-05-11 22:57:16 -04001930
1931 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001932 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001933 DISPATCH();
1934 }
1935
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001936 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001937 TARGET(GET_AWAITABLE) {
1938 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001939 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001940
1941 Py_DECREF(iterable);
1942
Yury Selivanovc724bae2016-03-02 11:30:46 -05001943 if (iter != NULL && PyCoro_CheckExact(iter)) {
1944 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1945 if (yf != NULL) {
1946 /* `iter` is a coroutine object that is being
1947 awaited, `yf` is a pointer to the current awaitable
1948 being awaited on. */
1949 Py_DECREF(yf);
1950 Py_CLEAR(iter);
1951 PyErr_SetString(
1952 PyExc_RuntimeError,
1953 "coroutine is being awaited already");
1954 /* The code below jumps to `error` if `iter` is NULL. */
1955 }
1956 }
1957
Yury Selivanov75445082015-05-11 22:57:16 -04001958 SET_TOP(iter); /* Even if it's NULL */
1959
1960 if (iter == NULL) {
1961 goto error;
1962 }
1963
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001964 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001965 DISPATCH();
1966 }
1967
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001968 TARGET(YIELD_FROM) {
1969 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001970 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001971 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001972 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1973 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001974 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001975 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001976 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001977 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001978 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001979 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001980 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001981 Py_DECREF(v);
1982 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001983 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001984 if (tstate->c_tracefunc != NULL
1985 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001986 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001987 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001988 if (err < 0)
1989 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001990 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001991 SET_TOP(val);
1992 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001993 }
Martin Panter95f53c12016-07-18 08:23:26 +00001994 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001995 f->f_stacktop = stack_pointer;
1996 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001997 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001998 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001999 f->f_lasti -= sizeof(_Py_CODEUNIT);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002000 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002001 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002002
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002003 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002005
2006 if (co->co_flags & CO_ASYNC_GENERATOR) {
2007 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2008 Py_DECREF(retval);
2009 if (w == NULL) {
2010 retval = NULL;
2011 goto error;
2012 }
2013 retval = w;
2014 }
2015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 f->f_stacktop = stack_pointer;
2017 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002018 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002019 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002020
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002021 TARGET(POP_EXCEPT) {
2022 PyTryBlock *b = PyFrame_BlockPop(f);
2023 if (b->b_type != EXCEPT_HANDLER) {
2024 PyErr_SetString(PyExc_SystemError,
2025 "popped block is not an except handler");
2026 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002028 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002029 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002030 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002031
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002032 PREDICTED(POP_BLOCK);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002033 TARGET(POP_BLOCK) {
2034 PyTryBlock *b = PyFrame_BlockPop(f);
2035 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002036 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002037 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002038
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002039 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002040 TARGET(END_FINALLY) {
2041 PyObject *status = POP();
2042 if (PyLong_Check(status)) {
2043 why = (enum why_code) PyLong_AS_LONG(status);
2044 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 if (why == WHY_RETURN ||
2046 why == WHY_CONTINUE)
2047 retval = POP();
2048 if (why == WHY_SILENCED) {
2049 /* An exception was silenced by 'with', we must
2050 manually unwind the EXCEPT_HANDLER block which was
2051 created when the exception was caught, otherwise
2052 the stack will be in an inconsistent state. */
2053 PyTryBlock *b = PyFrame_BlockPop(f);
2054 assert(b->b_type == EXCEPT_HANDLER);
2055 UNWIND_EXCEPT_HANDLER(b);
2056 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002057 Py_DECREF(status);
2058 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002060 Py_DECREF(status);
2061 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002063 else if (PyExceptionClass_Check(status)) {
2064 PyObject *exc = POP();
2065 PyObject *tb = POP();
2066 PyErr_Restore(status, exc, tb);
2067 why = WHY_EXCEPTION;
2068 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002070 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 PyErr_SetString(PyExc_SystemError,
2072 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002073 Py_DECREF(status);
2074 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002076 Py_DECREF(status);
2077 DISPATCH();
2078 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002079
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002080 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002081 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002082
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002083 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002084 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002085 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
2086 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002087 PyErr_SetString(PyExc_NameError,
2088 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002089 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002090 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002091 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002092 }
2093 else {
2094 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2095 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002096 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002097 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2098 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002099 if (PyErr_ExceptionMatches(PyExc_KeyError))
2100 PyErr_SetString(PyExc_NameError,
2101 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002102 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002103 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002104 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002105 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002106 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002107 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002108
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002109 TARGET(STORE_NAME) {
2110 PyObject *name = GETITEM(names, oparg);
2111 PyObject *v = POP();
2112 PyObject *ns = f->f_locals;
2113 int err;
2114 if (ns == NULL) {
2115 PyErr_Format(PyExc_SystemError,
2116 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002117 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002118 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002119 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002120 if (PyDict_CheckExact(ns))
2121 err = PyDict_SetItem(ns, name, v);
2122 else
2123 err = PyObject_SetItem(ns, name, v);
2124 Py_DECREF(v);
2125 if (err != 0)
2126 goto error;
2127 DISPATCH();
2128 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002129
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002130 TARGET(DELETE_NAME) {
2131 PyObject *name = GETITEM(names, oparg);
2132 PyObject *ns = f->f_locals;
2133 int err;
2134 if (ns == NULL) {
2135 PyErr_Format(PyExc_SystemError,
2136 "no locals when deleting %R", name);
2137 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002139 err = PyObject_DelItem(ns, name);
2140 if (err != 0) {
2141 format_exc_check_arg(PyExc_NameError,
2142 NAME_ERROR_MSG,
2143 name);
2144 goto error;
2145 }
2146 DISPATCH();
2147 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002148
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002149 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002150 TARGET(UNPACK_SEQUENCE) {
2151 PyObject *seq = POP(), *item, **items;
2152 if (PyTuple_CheckExact(seq) &&
2153 PyTuple_GET_SIZE(seq) == oparg) {
2154 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002156 item = items[oparg];
2157 Py_INCREF(item);
2158 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002159 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002160 } else if (PyList_CheckExact(seq) &&
2161 PyList_GET_SIZE(seq) == oparg) {
2162 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002163 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002164 item = items[oparg];
2165 Py_INCREF(item);
2166 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002167 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002168 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 stack_pointer + oparg)) {
2170 STACKADJ(oparg);
2171 } else {
2172 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002173 Py_DECREF(seq);
2174 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002176 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002177 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002178 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002179
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002180 TARGET(UNPACK_EX) {
2181 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2182 PyObject *seq = POP();
2183
2184 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2185 stack_pointer + totalargs)) {
2186 stack_pointer += totalargs;
2187 } else {
2188 Py_DECREF(seq);
2189 goto error;
2190 }
2191 Py_DECREF(seq);
2192 DISPATCH();
2193 }
2194
2195 TARGET(STORE_ATTR) {
2196 PyObject *name = GETITEM(names, oparg);
2197 PyObject *owner = TOP();
2198 PyObject *v = SECOND();
2199 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002200 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002201 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002202 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002203 Py_DECREF(owner);
2204 if (err != 0)
2205 goto error;
2206 DISPATCH();
2207 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002208
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002209 TARGET(DELETE_ATTR) {
2210 PyObject *name = GETITEM(names, oparg);
2211 PyObject *owner = POP();
2212 int err;
2213 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2214 Py_DECREF(owner);
2215 if (err != 0)
2216 goto error;
2217 DISPATCH();
2218 }
2219
2220 TARGET(STORE_GLOBAL) {
2221 PyObject *name = GETITEM(names, oparg);
2222 PyObject *v = POP();
2223 int err;
2224 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002225 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002226 if (err != 0)
2227 goto error;
2228 DISPATCH();
2229 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002230
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002231 TARGET(DELETE_GLOBAL) {
2232 PyObject *name = GETITEM(names, oparg);
2233 int err;
2234 err = PyDict_DelItem(f->f_globals, name);
2235 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002237 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002238 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002239 }
2240 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002241 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002242
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002243 TARGET(LOAD_NAME) {
2244 PyObject *name = GETITEM(names, oparg);
2245 PyObject *locals = f->f_locals;
2246 PyObject *v;
2247 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002248 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002249 "no locals when loading %R", name);
2250 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002252 if (PyDict_CheckExact(locals)) {
2253 v = PyDict_GetItem(locals, name);
2254 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002255 }
2256 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002257 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002258 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002259 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2260 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 PyErr_Clear();
2262 }
2263 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002264 if (v == NULL) {
2265 v = PyDict_GetItem(f->f_globals, name);
2266 Py_XINCREF(v);
2267 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002268 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002269 v = PyDict_GetItem(f->f_builtins, name);
2270 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002271 format_exc_check_arg(
2272 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002273 NAME_ERROR_MSG, name);
2274 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002275 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002276 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002277 }
2278 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002279 v = PyObject_GetItem(f->f_builtins, name);
2280 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002281 if (PyErr_ExceptionMatches(PyExc_KeyError))
2282 format_exc_check_arg(
2283 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002284 NAME_ERROR_MSG, name);
2285 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002286 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002287 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002289 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002290 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002292 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002293
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002294 TARGET(LOAD_GLOBAL) {
2295 PyObject *name = GETITEM(names, oparg);
2296 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002297 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002298 && PyDict_CheckExact(f->f_builtins))
2299 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002300 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002301 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002302 name);
2303 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002304 if (!_PyErr_OCCURRED()) {
2305 /* _PyDict_LoadGlobal() returns NULL without raising
2306 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002307 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002308 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002309 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002310 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002312 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002314 else {
2315 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002316
2317 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002318 v = PyObject_GetItem(f->f_globals, name);
2319 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002320 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2321 goto error;
2322 PyErr_Clear();
2323
Victor Stinnerb4efc962015-11-20 09:24:02 +01002324 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002325 v = PyObject_GetItem(f->f_builtins, name);
2326 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002327 if (PyErr_ExceptionMatches(PyExc_KeyError))
2328 format_exc_check_arg(
2329 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002330 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002331 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002332 }
2333 }
2334 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002335 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002337 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002338
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002339 TARGET(DELETE_FAST) {
2340 PyObject *v = GETLOCAL(oparg);
2341 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002342 SETLOCAL(oparg, NULL);
2343 DISPATCH();
2344 }
2345 format_exc_check_arg(
2346 PyExc_UnboundLocalError,
2347 UNBOUNDLOCAL_ERROR_MSG,
2348 PyTuple_GetItem(co->co_varnames, oparg)
2349 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002350 goto error;
2351 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002352
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002353 TARGET(DELETE_DEREF) {
2354 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002355 PyObject *oldobj = PyCell_GET(cell);
2356 if (oldobj != NULL) {
2357 PyCell_SET(cell, NULL);
2358 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002359 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002360 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002361 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002362 goto error;
2363 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002364
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002365 TARGET(LOAD_CLOSURE) {
2366 PyObject *cell = freevars[oparg];
2367 Py_INCREF(cell);
2368 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002370 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002371
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002372 TARGET(LOAD_CLASSDEREF) {
2373 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002374 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002375 assert(locals);
2376 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2377 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2378 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2379 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2380 if (PyDict_CheckExact(locals)) {
2381 value = PyDict_GetItem(locals, name);
2382 Py_XINCREF(value);
2383 }
2384 else {
2385 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002386 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002387 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2388 goto error;
2389 PyErr_Clear();
2390 }
2391 }
2392 if (!value) {
2393 PyObject *cell = freevars[oparg];
2394 value = PyCell_GET(cell);
2395 if (value == NULL) {
2396 format_exc_unbound(co, oparg);
2397 goto error;
2398 }
2399 Py_INCREF(value);
2400 }
2401 PUSH(value);
2402 DISPATCH();
2403 }
2404
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002405 TARGET(LOAD_DEREF) {
2406 PyObject *cell = freevars[oparg];
2407 PyObject *value = PyCell_GET(cell);
2408 if (value == NULL) {
2409 format_exc_unbound(co, oparg);
2410 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002412 Py_INCREF(value);
2413 PUSH(value);
2414 DISPATCH();
2415 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002416
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002417 TARGET(STORE_DEREF) {
2418 PyObject *v = POP();
2419 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002420 PyObject *oldobj = PyCell_GET(cell);
2421 PyCell_SET(cell, v);
2422 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002423 DISPATCH();
2424 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002425
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002426 TARGET(BUILD_STRING) {
2427 PyObject *str;
2428 PyObject *empty = PyUnicode_New(0, 0);
2429 if (empty == NULL) {
2430 goto error;
2431 }
2432 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2433 Py_DECREF(empty);
2434 if (str == NULL)
2435 goto error;
2436 while (--oparg >= 0) {
2437 PyObject *item = POP();
2438 Py_DECREF(item);
2439 }
2440 PUSH(str);
2441 DISPATCH();
2442 }
2443
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002444 TARGET(BUILD_TUPLE) {
2445 PyObject *tup = PyTuple_New(oparg);
2446 if (tup == NULL)
2447 goto error;
2448 while (--oparg >= 0) {
2449 PyObject *item = POP();
2450 PyTuple_SET_ITEM(tup, oparg, item);
2451 }
2452 PUSH(tup);
2453 DISPATCH();
2454 }
2455
2456 TARGET(BUILD_LIST) {
2457 PyObject *list = PyList_New(oparg);
2458 if (list == NULL)
2459 goto error;
2460 while (--oparg >= 0) {
2461 PyObject *item = POP();
2462 PyList_SET_ITEM(list, oparg, item);
2463 }
2464 PUSH(list);
2465 DISPATCH();
2466 }
2467
Serhiy Storchaka73442852016-10-02 10:33:46 +03002468 TARGET(BUILD_TUPLE_UNPACK_WITH_CALL)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002469 TARGET(BUILD_TUPLE_UNPACK)
2470 TARGET(BUILD_LIST_UNPACK) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002471 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002472 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002473 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002474 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002475
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002476 if (sum == NULL)
2477 goto error;
2478
2479 for (i = oparg; i > 0; i--) {
2480 PyObject *none_val;
2481
2482 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2483 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002484 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
2485 PyErr_ExceptionMatches(PyExc_TypeError)) {
2486 PyObject *func = PEEK(1 + oparg);
2487 PyErr_Format(PyExc_TypeError,
2488 "%.200s%.200s argument after * "
2489 "must be an iterable, not %.200s",
2490 PyEval_GetFuncName(func),
2491 PyEval_GetFuncDesc(func),
2492 PEEK(i)->ob_type->tp_name);
2493 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002494 Py_DECREF(sum);
2495 goto error;
2496 }
2497 Py_DECREF(none_val);
2498 }
2499
2500 if (convert_to_tuple) {
2501 return_value = PyList_AsTuple(sum);
2502 Py_DECREF(sum);
2503 if (return_value == NULL)
2504 goto error;
2505 }
2506 else {
2507 return_value = sum;
2508 }
2509
2510 while (oparg--)
2511 Py_DECREF(POP());
2512 PUSH(return_value);
2513 DISPATCH();
2514 }
2515
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002516 TARGET(BUILD_SET) {
2517 PyObject *set = PySet_New(NULL);
2518 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002519 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002520 if (set == NULL)
2521 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002522 for (i = oparg; i > 0; i--) {
2523 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002524 if (err == 0)
2525 err = PySet_Add(set, item);
2526 Py_DECREF(item);
2527 }
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002528 STACKADJ(-oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002529 if (err != 0) {
2530 Py_DECREF(set);
2531 goto error;
2532 }
2533 PUSH(set);
2534 DISPATCH();
2535 }
2536
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002537 TARGET(BUILD_SET_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002538 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002539 PyObject *sum = PySet_New(NULL);
2540 if (sum == NULL)
2541 goto error;
2542
2543 for (i = oparg; i > 0; i--) {
2544 if (_PySet_Update(sum, PEEK(i)) < 0) {
2545 Py_DECREF(sum);
2546 goto error;
2547 }
2548 }
2549
2550 while (oparg--)
2551 Py_DECREF(POP());
2552 PUSH(sum);
2553 DISPATCH();
2554 }
2555
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002556 TARGET(BUILD_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002557 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002558 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2559 if (map == NULL)
2560 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002561 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002562 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002563 PyObject *key = PEEK(2*i);
2564 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002565 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002566 if (err != 0) {
2567 Py_DECREF(map);
2568 goto error;
2569 }
2570 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002571
2572 while (oparg--) {
2573 Py_DECREF(POP());
2574 Py_DECREF(POP());
2575 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002576 PUSH(map);
2577 DISPATCH();
2578 }
2579
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002580 TARGET(SETUP_ANNOTATIONS) {
2581 _Py_IDENTIFIER(__annotations__);
2582 int err;
2583 PyObject *ann_dict;
2584 if (f->f_locals == NULL) {
2585 PyErr_Format(PyExc_SystemError,
2586 "no locals found when setting up annotations");
2587 goto error;
2588 }
2589 /* check if __annotations__ in locals()... */
2590 if (PyDict_CheckExact(f->f_locals)) {
2591 ann_dict = _PyDict_GetItemId(f->f_locals,
2592 &PyId___annotations__);
2593 if (ann_dict == NULL) {
2594 /* ...if not, create a new one */
2595 ann_dict = PyDict_New();
2596 if (ann_dict == NULL) {
2597 goto error;
2598 }
2599 err = _PyDict_SetItemId(f->f_locals,
2600 &PyId___annotations__, ann_dict);
2601 Py_DECREF(ann_dict);
2602 if (err != 0) {
2603 goto error;
2604 }
2605 }
2606 }
2607 else {
2608 /* do the same if locals() is not a dict */
2609 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2610 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002611 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002612 }
2613 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2614 if (ann_dict == NULL) {
2615 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2616 goto error;
2617 }
2618 PyErr_Clear();
2619 ann_dict = PyDict_New();
2620 if (ann_dict == NULL) {
2621 goto error;
2622 }
2623 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2624 Py_DECREF(ann_dict);
2625 if (err != 0) {
2626 goto error;
2627 }
2628 }
2629 else {
2630 Py_DECREF(ann_dict);
2631 }
2632 }
2633 DISPATCH();
2634 }
2635
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002636 TARGET(BUILD_CONST_KEY_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002637 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002638 PyObject *map;
2639 PyObject *keys = TOP();
2640 if (!PyTuple_CheckExact(keys) ||
2641 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2642 PyErr_SetString(PyExc_SystemError,
2643 "bad BUILD_CONST_KEY_MAP keys argument");
2644 goto error;
2645 }
2646 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2647 if (map == NULL) {
2648 goto error;
2649 }
2650 for (i = oparg; i > 0; i--) {
2651 int err;
2652 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2653 PyObject *value = PEEK(i + 1);
2654 err = PyDict_SetItem(map, key, value);
2655 if (err != 0) {
2656 Py_DECREF(map);
2657 goto error;
2658 }
2659 }
2660
2661 Py_DECREF(POP());
2662 while (oparg--) {
2663 Py_DECREF(POP());
2664 }
2665 PUSH(map);
2666 DISPATCH();
2667 }
2668
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002669 TARGET(BUILD_MAP_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002670 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002671 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002672 if (sum == NULL)
2673 goto error;
2674
2675 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002676 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002677 if (PyDict_Update(sum, arg) < 0) {
2678 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2679 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002680 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002681 arg->ob_type->tp_name);
2682 }
2683 Py_DECREF(sum);
2684 goto error;
2685 }
2686 }
2687
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002688 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002689 Py_DECREF(POP());
2690 PUSH(sum);
2691 DISPATCH();
2692 }
2693
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002694 TARGET(BUILD_MAP_UNPACK_WITH_CALL) {
2695 Py_ssize_t i;
2696 PyObject *sum = PyDict_New();
2697 if (sum == NULL)
2698 goto error;
2699
2700 for (i = oparg; i > 0; i--) {
2701 PyObject *arg = PEEK(i);
2702 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
2703 PyObject *func = PEEK(2 + oparg);
2704 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2705 PyErr_Format(PyExc_TypeError,
2706 "%.200s%.200s argument after ** "
2707 "must be a mapping, not %.200s",
2708 PyEval_GetFuncName(func),
2709 PyEval_GetFuncDesc(func),
2710 arg->ob_type->tp_name);
2711 }
2712 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2713 PyObject *exc, *val, *tb;
2714 PyErr_Fetch(&exc, &val, &tb);
2715 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
2716 PyObject *key = PyTuple_GET_ITEM(val, 0);
2717 if (!PyUnicode_Check(key)) {
2718 PyErr_Format(PyExc_TypeError,
2719 "%.200s%.200s keywords must be strings",
2720 PyEval_GetFuncName(func),
2721 PyEval_GetFuncDesc(func));
2722 } else {
2723 PyErr_Format(PyExc_TypeError,
2724 "%.200s%.200s got multiple "
2725 "values for keyword argument '%U'",
2726 PyEval_GetFuncName(func),
2727 PyEval_GetFuncDesc(func),
2728 key);
2729 }
2730 Py_XDECREF(exc);
2731 Py_XDECREF(val);
2732 Py_XDECREF(tb);
2733 }
2734 else {
2735 PyErr_Restore(exc, val, tb);
2736 }
2737 }
2738 Py_DECREF(sum);
2739 goto error;
2740 }
2741 }
2742
2743 while (oparg--)
2744 Py_DECREF(POP());
2745 PUSH(sum);
2746 DISPATCH();
2747 }
2748
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002749 TARGET(MAP_ADD) {
2750 PyObject *key = TOP();
2751 PyObject *value = SECOND();
2752 PyObject *map;
2753 int err;
2754 STACKADJ(-2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002755 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002756 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002757 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002758 Py_DECREF(value);
2759 Py_DECREF(key);
2760 if (err != 0)
2761 goto error;
2762 PREDICT(JUMP_ABSOLUTE);
2763 DISPATCH();
2764 }
2765
2766 TARGET(LOAD_ATTR) {
2767 PyObject *name = GETITEM(names, oparg);
2768 PyObject *owner = TOP();
2769 PyObject *res = PyObject_GetAttr(owner, name);
2770 Py_DECREF(owner);
2771 SET_TOP(res);
2772 if (res == NULL)
2773 goto error;
2774 DISPATCH();
2775 }
2776
2777 TARGET(COMPARE_OP) {
2778 PyObject *right = POP();
2779 PyObject *left = TOP();
2780 PyObject *res = cmp_outcome(oparg, left, right);
2781 Py_DECREF(left);
2782 Py_DECREF(right);
2783 SET_TOP(res);
2784 if (res == NULL)
2785 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002786 PREDICT(POP_JUMP_IF_FALSE);
2787 PREDICT(POP_JUMP_IF_TRUE);
2788 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002789 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002790
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002791 TARGET(IMPORT_NAME) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002792 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002793 PyObject *fromlist = POP();
2794 PyObject *level = TOP();
2795 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002796 res = import_name(f, name, fromlist, level);
2797 Py_DECREF(level);
2798 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002799 SET_TOP(res);
2800 if (res == NULL)
2801 goto error;
2802 DISPATCH();
2803 }
2804
2805 TARGET(IMPORT_STAR) {
2806 PyObject *from = POP(), *locals;
2807 int err;
Victor Stinner41bb43a2013-10-29 01:19:37 +01002808 if (PyFrame_FastToLocalsWithError(f) < 0)
2809 goto error;
2810
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002811 locals = f->f_locals;
2812 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002813 PyErr_SetString(PyExc_SystemError,
2814 "no locals found during 'import *'");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002815 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002816 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002817 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002818 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002819 Py_DECREF(from);
2820 if (err != 0)
2821 goto error;
2822 DISPATCH();
2823 }
Guido van Rossum25831651993-05-19 14:50:45 +00002824
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002825 TARGET(IMPORT_FROM) {
2826 PyObject *name = GETITEM(names, oparg);
2827 PyObject *from = TOP();
2828 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002829 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002830 PUSH(res);
2831 if (res == NULL)
2832 goto error;
2833 DISPATCH();
2834 }
Thomas Wouters52152252000-08-17 22:55:00 +00002835
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002836 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002837 JUMPBY(oparg);
2838 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002839 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002840
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002841 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002842 TARGET(POP_JUMP_IF_FALSE) {
2843 PyObject *cond = POP();
2844 int err;
2845 if (cond == Py_True) {
2846 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002847 FAST_DISPATCH();
2848 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002849 if (cond == Py_False) {
2850 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002851 JUMPTO(oparg);
2852 FAST_DISPATCH();
2853 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002854 err = PyObject_IsTrue(cond);
2855 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002856 if (err > 0)
2857 err = 0;
2858 else if (err == 0)
2859 JUMPTO(oparg);
2860 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002861 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002862 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002863 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002864
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002865 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002866 TARGET(POP_JUMP_IF_TRUE) {
2867 PyObject *cond = POP();
2868 int err;
2869 if (cond == Py_False) {
2870 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 FAST_DISPATCH();
2872 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002873 if (cond == Py_True) {
2874 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002875 JUMPTO(oparg);
2876 FAST_DISPATCH();
2877 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002878 err = PyObject_IsTrue(cond);
2879 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 if (err > 0) {
2881 err = 0;
2882 JUMPTO(oparg);
2883 }
2884 else if (err == 0)
2885 ;
2886 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002887 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002889 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002890
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002891 TARGET(JUMP_IF_FALSE_OR_POP) {
2892 PyObject *cond = TOP();
2893 int err;
2894 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002895 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002896 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002897 FAST_DISPATCH();
2898 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002899 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002900 JUMPTO(oparg);
2901 FAST_DISPATCH();
2902 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002903 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002904 if (err > 0) {
2905 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002906 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002907 err = 0;
2908 }
2909 else if (err == 0)
2910 JUMPTO(oparg);
2911 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002912 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002914 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002915
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002916 TARGET(JUMP_IF_TRUE_OR_POP) {
2917 PyObject *cond = TOP();
2918 int err;
2919 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002920 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002921 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002922 FAST_DISPATCH();
2923 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002924 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 JUMPTO(oparg);
2926 FAST_DISPATCH();
2927 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002928 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002929 if (err > 0) {
2930 err = 0;
2931 JUMPTO(oparg);
2932 }
2933 else if (err == 0) {
2934 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002935 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 }
2937 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002938 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002939 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002940 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002941
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002942 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002943 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002944 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002945#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 /* Enabling this path speeds-up all while and for-loops by bypassing
2947 the per-loop checks for signals. By default, this should be turned-off
2948 because it prevents detection of a control-break in tight loops like
2949 "while 1: pass". Compile with this option turned-on when you need
2950 the speed-up and do not need break checking inside tight loops (ones
2951 that contain only instructions ending with FAST_DISPATCH).
2952 */
2953 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002954#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002955 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002956#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002957 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002958
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002959 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002960 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002961 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002962 PyObject *iter = PyObject_GetIter(iterable);
2963 Py_DECREF(iterable);
2964 SET_TOP(iter);
2965 if (iter == NULL)
2966 goto error;
2967 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002968 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002969 DISPATCH();
2970 }
2971
2972 TARGET(GET_YIELD_FROM_ITER) {
2973 /* before: [obj]; after [getiter(obj)] */
2974 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002975 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002976 if (PyCoro_CheckExact(iterable)) {
2977 /* `iterable` is a coroutine */
2978 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2979 /* and it is used in a 'yield from' expression of a
2980 regular generator. */
2981 Py_DECREF(iterable);
2982 SET_TOP(NULL);
2983 PyErr_SetString(PyExc_TypeError,
2984 "cannot 'yield from' a coroutine object "
2985 "in a non-coroutine generator");
2986 goto error;
2987 }
2988 }
2989 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002990 /* `iterable` is not a generator. */
2991 iter = PyObject_GetIter(iterable);
2992 Py_DECREF(iterable);
2993 SET_TOP(iter);
2994 if (iter == NULL)
2995 goto error;
2996 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002997 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002998 DISPATCH();
2999 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003000
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003001 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003002 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003003 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003004 PyObject *iter = TOP();
3005 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3006 if (next != NULL) {
3007 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003008 PREDICT(STORE_FAST);
3009 PREDICT(UNPACK_SEQUENCE);
3010 DISPATCH();
3011 }
3012 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003013 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
3014 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08003015 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003016 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003017 PyErr_Clear();
3018 }
3019 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003020 STACKADJ(-1);
3021 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003022 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003023 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003024 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003025 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003026
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003027 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003028 why = WHY_BREAK;
3029 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003030 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003031
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003032 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003033 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003034 if (retval == NULL)
3035 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003036 why = WHY_CONTINUE;
3037 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003038 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003039
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003040 TARGET(SETUP_LOOP)
3041 TARGET(SETUP_EXCEPT)
3042 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003043 /* NOTE: If you add any new block-setup opcodes that
3044 are not try/except/finally handlers, you may need
3045 to update the PyGen_NeedsFinalizing() function.
3046 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003048 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
3049 STACK_LEVEL());
3050 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003051 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003052
Yury Selivanov75445082015-05-11 22:57:16 -04003053 TARGET(BEFORE_ASYNC_WITH) {
3054 _Py_IDENTIFIER(__aexit__);
3055 _Py_IDENTIFIER(__aenter__);
3056
3057 PyObject *mgr = TOP();
3058 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3059 *enter;
3060 PyObject *res;
3061 if (exit == NULL)
3062 goto error;
3063 SET_TOP(exit);
3064 enter = special_lookup(mgr, &PyId___aenter__);
3065 Py_DECREF(mgr);
3066 if (enter == NULL)
3067 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003068 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003069 Py_DECREF(enter);
3070 if (res == NULL)
3071 goto error;
3072 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003073 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003074 DISPATCH();
3075 }
3076
3077 TARGET(SETUP_ASYNC_WITH) {
3078 PyObject *res = POP();
3079 /* Setup the finally block before pushing the result
3080 of __aenter__ on the stack. */
3081 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3082 STACK_LEVEL());
3083 PUSH(res);
3084 DISPATCH();
3085 }
3086
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003087 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003088 _Py_IDENTIFIER(__exit__);
3089 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003090 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08003091 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003092 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08003093 if (enter == NULL)
3094 goto error;
3095 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003096 if (exit == NULL) {
3097 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003098 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003099 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003100 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003101 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003102 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003103 Py_DECREF(enter);
3104 if (res == NULL)
3105 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003106 /* Setup the finally block before pushing the result
3107 of __enter__ on the stack. */
3108 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3109 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003110
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003111 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003112 DISPATCH();
3113 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003114
Yury Selivanov75445082015-05-11 22:57:16 -04003115 TARGET(WITH_CLEANUP_START) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04003116 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003117 how/why we entered the finally clause:
3118 - TOP = None
3119 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
3120 - TOP = WHY_*; no retval below it
3121 - (TOP, SECOND, THIRD) = exc_info()
3122 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
3123 Below them is EXIT, the context.__exit__ bound method.
3124 In the last case, we must call
3125 EXIT(TOP, SECOND, THIRD)
3126 otherwise we must call
3127 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003128
Benjamin Peterson8f169482013-10-29 22:25:06 -04003129 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003130 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04003131 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003132 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003134 In addition, if the stack represents an exception,
3135 *and* the function call returns a 'true' value, we
3136 push WHY_SILENCED onto the stack. END_FINALLY will
3137 then not re-raise the exception. (But non-local
3138 gotos should still be resumed.)
3139 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003140
Victor Stinner842cfff2016-12-01 14:45:31 +01003141 PyObject* stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003142 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003143 PyObject *exc, *val, *tb, *res;
3144
3145 val = tb = Py_None;
3146 exc = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003147 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003148 (void)POP();
3149 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003150 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003151 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003152 else if (PyLong_Check(exc)) {
3153 STACKADJ(-1);
3154 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003155 case WHY_RETURN:
3156 case WHY_CONTINUE:
3157 /* Retval in TOP. */
3158 exit_func = SECOND();
3159 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003160 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003161 break;
3162 default:
3163 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003164 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003165 break;
3166 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003167 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003168 }
3169 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003170 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003171 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003172 val = SECOND();
3173 tb = THIRD();
3174 tp2 = FOURTH();
3175 exc2 = PEEK(5);
3176 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003177 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003178 SET_VALUE(7, tb2);
3179 SET_VALUE(6, exc2);
3180 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003181 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3182 SET_FOURTH(NULL);
3183 /* We just shifted the stack down, so we have
3184 to tell the except handler block that the
3185 values are lower than it expects. */
3186 block = &f->f_blockstack[f->f_iblock - 1];
3187 assert(block->b_type == EXCEPT_HANDLER);
3188 block->b_level--;
3189 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003190
3191 stack[0] = exc;
3192 stack[1] = val;
3193 stack[2] = tb;
3194 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003195 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003196 if (res == NULL)
3197 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003198
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003199 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003200 PUSH(exc);
3201 PUSH(res);
3202 PREDICT(WITH_CLEANUP_FINISH);
3203 DISPATCH();
3204 }
3205
3206 PREDICTED(WITH_CLEANUP_FINISH);
3207 TARGET(WITH_CLEANUP_FINISH) {
3208 PyObject *res = POP();
3209 PyObject *exc = POP();
3210 int err;
3211
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003212 if (exc != Py_None)
3213 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003214 else
3215 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003216
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003217 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003218 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003220 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003221 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003222 else if (err > 0) {
3223 err = 0;
3224 /* There was an exception and a True return */
3225 PUSH(PyLong_FromLong((long) WHY_SILENCED));
3226 }
3227 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003228 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003229 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003230
Yury Selivanovf2392132016-12-13 19:03:51 -05003231 TARGET(LOAD_METHOD) {
3232 /* Designed to work in tamdem with CALL_METHOD. */
3233 PyObject *name = GETITEM(names, oparg);
3234 PyObject *obj = TOP();
3235 PyObject *meth = NULL;
3236
3237 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3238
Yury Selivanovf2392132016-12-13 19:03:51 -05003239 if (meth == NULL) {
3240 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003241 goto error;
3242 }
3243
3244 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003245 /* We can bypass temporary bound method object.
3246 meth is unbound method and obj is self.
3247
3248 meth | self | arg1 | ... | argN
3249 */
3250 SET_TOP(meth);
3251 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003252 }
3253 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003254 /* meth is not an unbound method (but a regular attr, or
3255 something was returned by a descriptor protocol). Set
3256 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003257 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003258
3259 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003260 */
INADA Naoki015bce62017-01-16 17:23:30 +09003261 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003262 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003263 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003264 }
3265 DISPATCH();
3266 }
3267
3268 TARGET(CALL_METHOD) {
3269 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003270 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003271
3272 sp = stack_pointer;
3273
INADA Naoki015bce62017-01-16 17:23:30 +09003274 meth = PEEK(oparg + 2);
3275 if (meth == NULL) {
3276 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3277 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003278
3279 Stack layout:
3280
INADA Naoki015bce62017-01-16 17:23:30 +09003281 ... | NULL | callable | arg1 | ... | argN
3282 ^- TOP()
3283 ^- (-oparg)
3284 ^- (-oparg-1)
3285 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003286
INADA Naoki015bce62017-01-16 17:23:30 +09003287 `callable` will be POPed by call_funtion.
3288 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003289 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003290 res = call_function(&sp, oparg, NULL);
3291 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003292 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003293 }
3294 else {
3295 /* This is a method call. Stack layout:
3296
INADA Naoki015bce62017-01-16 17:23:30 +09003297 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003298 ^- TOP()
3299 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003300 ^- (-oparg-1)
3301 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003302
INADA Naoki015bce62017-01-16 17:23:30 +09003303 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003304 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003305 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003306 */
3307 res = call_function(&sp, oparg + 1, NULL);
3308 stack_pointer = sp;
3309 }
3310
3311 PUSH(res);
3312 if (res == NULL)
3313 goto error;
3314 DISPATCH();
3315 }
3316
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003317 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003318 TARGET(CALL_FUNCTION) {
3319 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003320 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003321 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003322 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003323 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003324 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003325 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003326 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003327 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003328 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003329
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003330 TARGET(CALL_FUNCTION_KW) {
3331 PyObject **sp, *res, *names;
3332
3333 names = POP();
3334 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003335 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003336 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003337 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003338 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003339 Py_DECREF(names);
3340
3341 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003342 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003343 }
3344 DISPATCH();
3345 }
3346
3347 TARGET(CALL_FUNCTION_EX) {
3348 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003349 if (oparg & 0x01) {
3350 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003351 if (!PyDict_CheckExact(kwargs)) {
3352 PyObject *d = PyDict_New();
3353 if (d == NULL)
3354 goto error;
3355 if (PyDict_Update(d, kwargs) != 0) {
3356 Py_DECREF(d);
3357 /* PyDict_Update raises attribute
3358 * error (percolated from an attempt
3359 * to get 'keys' attribute) instead of
3360 * a type error if its second argument
3361 * is not a mapping.
3362 */
3363 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3364 func = SECOND();
3365 PyErr_Format(PyExc_TypeError,
3366 "%.200s%.200s argument after ** "
3367 "must be a mapping, not %.200s",
3368 PyEval_GetFuncName(func),
3369 PyEval_GetFuncDesc(func),
3370 kwargs->ob_type->tp_name);
3371 }
Victor Stinnereece2222016-09-12 11:16:37 +02003372 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003373 goto error;
3374 }
3375 Py_DECREF(kwargs);
3376 kwargs = d;
3377 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003378 assert(PyDict_CheckExact(kwargs));
3379 }
3380 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003381 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003382 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003383 if (Py_TYPE(callargs)->tp_iter == NULL &&
3384 !PySequence_Check(callargs)) {
3385 PyErr_Format(PyExc_TypeError,
3386 "%.200s%.200s argument after * "
3387 "must be an iterable, not %.200s",
3388 PyEval_GetFuncName(func),
3389 PyEval_GetFuncDesc(func),
3390 callargs->ob_type->tp_name);
Victor Stinnereece2222016-09-12 11:16:37 +02003391 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003392 goto error;
3393 }
3394 Py_SETREF(callargs, PySequence_Tuple(callargs));
3395 if (callargs == NULL) {
3396 goto error;
3397 }
3398 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003399 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003400
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003401 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003402 Py_DECREF(func);
3403 Py_DECREF(callargs);
3404 Py_XDECREF(kwargs);
3405
3406 SET_TOP(result);
3407 if (result == NULL) {
3408 goto error;
3409 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003410 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003411 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003412
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003413 TARGET(MAKE_FUNCTION) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003414 PyObject *qualname = POP();
3415 PyObject *codeobj = POP();
3416 PyFunctionObject *func = (PyFunctionObject *)
3417 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003418
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003419 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003420 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003421 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003422 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003423 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003424
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003425 if (oparg & 0x08) {
3426 assert(PyTuple_CheckExact(TOP()));
3427 func ->func_closure = POP();
3428 }
3429 if (oparg & 0x04) {
3430 assert(PyDict_CheckExact(TOP()));
3431 func->func_annotations = POP();
3432 }
3433 if (oparg & 0x02) {
3434 assert(PyDict_CheckExact(TOP()));
3435 func->func_kwdefaults = POP();
3436 }
3437 if (oparg & 0x01) {
3438 assert(PyTuple_CheckExact(TOP()));
3439 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003440 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003441
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003442 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003443 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003444 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003445
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003446 TARGET(BUILD_SLICE) {
3447 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003448 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003449 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003450 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003451 step = NULL;
3452 stop = POP();
3453 start = TOP();
3454 slice = PySlice_New(start, stop, step);
3455 Py_DECREF(start);
3456 Py_DECREF(stop);
3457 Py_XDECREF(step);
3458 SET_TOP(slice);
3459 if (slice == NULL)
3460 goto error;
3461 DISPATCH();
3462 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003463
Eric V. Smitha78c7952015-11-03 12:45:05 -05003464 TARGET(FORMAT_VALUE) {
3465 /* Handles f-string value formatting. */
3466 PyObject *result;
3467 PyObject *fmt_spec;
3468 PyObject *value;
3469 PyObject *(*conv_fn)(PyObject *);
3470 int which_conversion = oparg & FVC_MASK;
3471 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3472
3473 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003474 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003475
3476 /* See if any conversion is specified. */
3477 switch (which_conversion) {
3478 case FVC_STR: conv_fn = PyObject_Str; break;
3479 case FVC_REPR: conv_fn = PyObject_Repr; break;
3480 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3481
3482 /* Must be 0 (meaning no conversion), since only four
3483 values are allowed by (oparg & FVC_MASK). */
3484 default: conv_fn = NULL; break;
3485 }
3486
3487 /* If there's a conversion function, call it and replace
3488 value with that result. Otherwise, just use value,
3489 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003490 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003491 result = conv_fn(value);
3492 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003493 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003494 Py_XDECREF(fmt_spec);
3495 goto error;
3496 }
3497 value = result;
3498 }
3499
3500 /* If value is a unicode object, and there's no fmt_spec,
3501 then we know the result of format(value) is value
3502 itself. In that case, skip calling format(). I plan to
3503 move this optimization in to PyObject_Format()
3504 itself. */
3505 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3506 /* Do nothing, just transfer ownership to result. */
3507 result = value;
3508 } else {
3509 /* Actually call format(). */
3510 result = PyObject_Format(value, fmt_spec);
3511 Py_DECREF(value);
3512 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003513 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003514 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003515 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003516 }
3517
Eric V. Smith135d5f42016-02-05 18:23:08 -05003518 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003519 DISPATCH();
3520 }
3521
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003522 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003523 int oldoparg = oparg;
3524 NEXTOPARG();
3525 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003526 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003527 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003528
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003529
Antoine Pitrou042b1282010-08-13 21:15:58 +00003530#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003531 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003532#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003533 default:
3534 fprintf(stderr,
3535 "XXX lineno: %d, opcode: %d\n",
3536 PyFrame_GetLineNumber(f),
3537 opcode);
3538 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003539 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003540
3541#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003542 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003543#endif
3544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003545 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003546
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003547 /* This should never be reached. Every opcode should end with DISPATCH()
3548 or goto error. */
3549 assert(0);
Guido van Rossumac7be682001-01-17 15:42:30 +00003550
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003551error:
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003552
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003553 assert(why == WHY_NOT);
3554 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003555
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003556 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003557#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003558 if (!PyErr_Occurred())
3559 PyErr_SetString(PyExc_SystemError,
3560 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003561#else
3562 assert(PyErr_Occurred());
3563#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003564
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003565 /* Log traceback info. */
3566 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003567
Benjamin Peterson51f46162013-01-23 08:38:47 -05003568 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003569 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3570 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003571
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003572fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003573 assert(why != WHY_NOT);
3574
3575 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003576 while (why != WHY_NOT && f->f_iblock > 0) {
3577 /* Peek at the current block. */
3578 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003580 assert(why != WHY_YIELD);
3581 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3582 why = WHY_NOT;
3583 JUMPTO(PyLong_AS_LONG(retval));
3584 Py_DECREF(retval);
3585 break;
3586 }
3587 /* Now we have to pop the block. */
3588 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003590 if (b->b_type == EXCEPT_HANDLER) {
3591 UNWIND_EXCEPT_HANDLER(b);
3592 continue;
3593 }
3594 UNWIND_BLOCK(b);
3595 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3596 why = WHY_NOT;
3597 JUMPTO(b->b_handler);
3598 break;
3599 }
3600 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3601 || b->b_type == SETUP_FINALLY)) {
3602 PyObject *exc, *val, *tb;
3603 int handler = b->b_handler;
3604 /* Beware, this invalidates all b->b_* fields */
3605 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3606 PUSH(tstate->exc_traceback);
3607 PUSH(tstate->exc_value);
3608 if (tstate->exc_type != NULL) {
3609 PUSH(tstate->exc_type);
3610 }
3611 else {
3612 Py_INCREF(Py_None);
3613 PUSH(Py_None);
3614 }
3615 PyErr_Fetch(&exc, &val, &tb);
3616 /* Make the raw exception data
3617 available to the handler,
3618 so a program can emulate the
3619 Python main loop. */
3620 PyErr_NormalizeException(
3621 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003622 if (tb != NULL)
3623 PyException_SetTraceback(val, tb);
3624 else
3625 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003626 Py_INCREF(exc);
3627 tstate->exc_type = exc;
3628 Py_INCREF(val);
3629 tstate->exc_value = val;
3630 tstate->exc_traceback = tb;
3631 if (tb == NULL)
3632 tb = Py_None;
3633 Py_INCREF(tb);
3634 PUSH(tb);
3635 PUSH(val);
3636 PUSH(exc);
3637 why = WHY_NOT;
3638 JUMPTO(handler);
3639 break;
3640 }
3641 if (b->b_type == SETUP_FINALLY) {
3642 if (why & (WHY_RETURN | WHY_CONTINUE))
3643 PUSH(retval);
3644 PUSH(PyLong_FromLong((long)why));
3645 why = WHY_NOT;
3646 JUMPTO(b->b_handler);
3647 break;
3648 }
3649 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003651 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003653 if (why != WHY_NOT)
3654 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00003655
Victor Stinnerace47d72013-07-18 01:41:08 +02003656 assert(!PyErr_Occurred());
3657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003658 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003660 assert(why != WHY_YIELD);
3661 /* Pop remaining stack entries. */
3662 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003663 PyObject *o = POP();
3664 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003665 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003667 if (why != WHY_RETURN)
3668 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003669
Victor Stinner4a7cc882015-03-06 23:35:27 +01003670 assert((retval != NULL) ^ (PyErr_Occurred() != NULL));
Victor Stinnerace47d72013-07-18 01:41:08 +02003671
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003672fast_yield:
Yury Selivanoveb636452016-09-08 22:01:51 -07003673 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01003674
Benjamin Petersonac913412011-07-03 16:25:11 -05003675 /* The purpose of this block is to put aside the generator's exception
3676 state and restore that of the calling frame. If the current
3677 exception state is from the caller, we clear the exception values
3678 on the generator frame, so they are not swapped back in latter. The
3679 origin of the current exception state is determined by checking for
3680 except handler blocks, which we must be in iff a new exception
3681 state came into existence in this frame. (An uncaught exception
3682 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3683 int i;
Victor Stinner74319ae2016-08-25 00:04:09 +02003684 for (i = 0; i < f->f_iblock; i++) {
3685 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER) {
Benjamin Petersonac913412011-07-03 16:25:11 -05003686 break;
Victor Stinner74319ae2016-08-25 00:04:09 +02003687 }
3688 }
Benjamin Petersonac913412011-07-03 16:25:11 -05003689 if (i == f->f_iblock)
3690 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003691 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003692 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003693 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003694 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003696 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003697 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003698 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003699 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3700 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003701 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003702 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003703 why = WHY_EXCEPTION;
3704 }
3705 }
3706 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003707 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3708 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003709 PyTrace_RETURN, NULL);
3710 }
3711 }
3712 if (tstate->c_profilefunc) {
3713 if (why == WHY_EXCEPTION)
3714 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003715 tstate->c_profileobj,
3716 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003717 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003718 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3719 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003720 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003721 Py_CLEAR(retval);
Brett Cannonb94767f2011-02-22 20:15:44 +00003722 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003723 }
3724 }
3725 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003727 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003728exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003729 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3730 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003731 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003732 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003733 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003734
Victor Stinnerefde1462015-03-21 15:04:43 +01003735 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003736}
3737
Benjamin Petersonb204a422011-06-05 22:04:07 -05003738static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003739format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3740{
3741 int err;
3742 Py_ssize_t len = PyList_GET_SIZE(names);
3743 PyObject *name_str, *comma, *tail, *tmp;
3744
3745 assert(PyList_CheckExact(names));
3746 assert(len >= 1);
3747 /* Deal with the joys of natural language. */
3748 switch (len) {
3749 case 1:
3750 name_str = PyList_GET_ITEM(names, 0);
3751 Py_INCREF(name_str);
3752 break;
3753 case 2:
3754 name_str = PyUnicode_FromFormat("%U and %U",
3755 PyList_GET_ITEM(names, len - 2),
3756 PyList_GET_ITEM(names, len - 1));
3757 break;
3758 default:
3759 tail = PyUnicode_FromFormat(", %U, and %U",
3760 PyList_GET_ITEM(names, len - 2),
3761 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003762 if (tail == NULL)
3763 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003764 /* Chop off the last two objects in the list. This shouldn't actually
3765 fail, but we can't be too careful. */
3766 err = PyList_SetSlice(names, len - 2, len, NULL);
3767 if (err == -1) {
3768 Py_DECREF(tail);
3769 return;
3770 }
3771 /* Stitch everything up into a nice comma-separated list. */
3772 comma = PyUnicode_FromString(", ");
3773 if (comma == NULL) {
3774 Py_DECREF(tail);
3775 return;
3776 }
3777 tmp = PyUnicode_Join(comma, names);
3778 Py_DECREF(comma);
3779 if (tmp == NULL) {
3780 Py_DECREF(tail);
3781 return;
3782 }
3783 name_str = PyUnicode_Concat(tmp, tail);
3784 Py_DECREF(tmp);
3785 Py_DECREF(tail);
3786 break;
3787 }
3788 if (name_str == NULL)
3789 return;
3790 PyErr_Format(PyExc_TypeError,
3791 "%U() missing %i required %s argument%s: %U",
3792 co->co_name,
3793 len,
3794 kind,
3795 len == 1 ? "" : "s",
3796 name_str);
3797 Py_DECREF(name_str);
3798}
3799
3800static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003801missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003802 PyObject **fastlocals)
3803{
Victor Stinner74319ae2016-08-25 00:04:09 +02003804 Py_ssize_t i, j = 0;
3805 Py_ssize_t start, end;
3806 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003807 const char *kind = positional ? "positional" : "keyword-only";
3808 PyObject *missing_names;
3809
3810 /* Compute the names of the arguments that are missing. */
3811 missing_names = PyList_New(missing);
3812 if (missing_names == NULL)
3813 return;
3814 if (positional) {
3815 start = 0;
3816 end = co->co_argcount - defcount;
3817 }
3818 else {
3819 start = co->co_argcount;
3820 end = start + co->co_kwonlyargcount;
3821 }
3822 for (i = start; i < end; i++) {
3823 if (GETLOCAL(i) == NULL) {
3824 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3825 PyObject *name = PyObject_Repr(raw);
3826 if (name == NULL) {
3827 Py_DECREF(missing_names);
3828 return;
3829 }
3830 PyList_SET_ITEM(missing_names, j++, name);
3831 }
3832 }
3833 assert(j == missing);
3834 format_missing(kind, co, missing_names);
3835 Py_DECREF(missing_names);
3836}
3837
3838static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003839too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3840 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003841{
3842 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003843 Py_ssize_t kwonly_given = 0;
3844 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003845 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003846 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003847
Benjamin Petersone109c702011-06-24 09:37:26 -05003848 assert((co->co_flags & CO_VARARGS) == 0);
3849 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003850 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3851 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003852 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003853 }
3854 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003855 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003856 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003857 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003858 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003859 }
3860 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003861 plural = (co_argcount != 1);
3862 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003863 }
3864 if (sig == NULL)
3865 return;
3866 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003867 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3868 kwonly_sig = PyUnicode_FromFormat(format,
3869 given != 1 ? "s" : "",
3870 kwonly_given,
3871 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003872 if (kwonly_sig == NULL) {
3873 Py_DECREF(sig);
3874 return;
3875 }
3876 }
3877 else {
3878 /* This will not fail. */
3879 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003880 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003881 }
3882 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003883 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003884 co->co_name,
3885 sig,
3886 plural ? "s" : "",
3887 given,
3888 kwonly_sig,
3889 given == 1 && !kwonly_given ? "was" : "were");
3890 Py_DECREF(sig);
3891 Py_DECREF(kwonly_sig);
3892}
3893
Guido van Rossumc2e20742006-02-27 22:32:47 +00003894/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003895 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003896 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003897
Victor Stinner40ee3012014-06-16 15:59:28 +02003898static PyObject *
3899_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Victor Stinner74319ae2016-08-25 00:04:09 +02003900 PyObject **args, Py_ssize_t argcount,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003901 PyObject **kwnames, PyObject **kwargs,
3902 Py_ssize_t kwcount, int kwstep,
Victor Stinner74319ae2016-08-25 00:04:09 +02003903 PyObject **defs, Py_ssize_t defcount,
3904 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003905 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003906{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003907 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003908 PyFrameObject *f;
3909 PyObject *retval = NULL;
3910 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003911 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003912 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003913 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3914 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003915 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003917 if (globals == NULL) {
3918 PyErr_SetString(PyExc_SystemError,
3919 "PyEval_EvalCodeEx: NULL globals");
3920 return NULL;
3921 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003922
Victor Stinnerc7020012016-08-16 23:40:29 +02003923 /* Create the frame */
3924 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003925 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003926 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003927 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003928 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003929 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003930 fastlocals = f->f_localsplus;
3931 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003932
Victor Stinnerc7020012016-08-16 23:40:29 +02003933 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003934 if (co->co_flags & CO_VARKEYWORDS) {
3935 kwdict = PyDict_New();
3936 if (kwdict == NULL)
3937 goto fail;
3938 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003939 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003940 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003941 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003942 SETLOCAL(i, kwdict);
3943 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003944 else {
3945 kwdict = NULL;
3946 }
3947
3948 /* Copy positional arguments into local variables */
3949 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003950 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003951 }
3952 else {
3953 n = argcount;
3954 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003955 for (i = 0; i < n; i++) {
3956 x = args[i];
3957 Py_INCREF(x);
3958 SETLOCAL(i, x);
3959 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003960
3961 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003962 if (co->co_flags & CO_VARARGS) {
3963 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003964 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003965 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003966 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003967 SETLOCAL(total_args, u);
3968 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003969 x = args[i];
3970 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003971 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003972 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003973 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003974
Serhiy Storchakab7281052016-09-12 00:52:40 +03003975 /* Handle keyword arguments passed as two strided arrays */
3976 kwcount *= kwstep;
3977 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003978 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003979 PyObject *keyword = kwnames[i];
3980 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003981 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003982
Benjamin Petersonb204a422011-06-05 22:04:07 -05003983 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3984 PyErr_Format(PyExc_TypeError,
3985 "%U() keywords must be strings",
3986 co->co_name);
3987 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003988 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003989
Benjamin Petersonb204a422011-06-05 22:04:07 -05003990 /* Speed hack: do raw pointer compares. As names are
3991 normally interned this should almost always hit. */
3992 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3993 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003994 PyObject *name = co_varnames[j];
3995 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003996 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003997 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003998 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003999
Benjamin Petersonb204a422011-06-05 22:04:07 -05004000 /* Slow fallback, just in case */
4001 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004002 PyObject *name = co_varnames[j];
4003 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4004 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004005 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004006 }
4007 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004008 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004009 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004010 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004011
Victor Stinner231d1f32017-01-11 02:12:06 +01004012 assert(j >= total_args);
4013 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004014 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02004015 "%U() got an unexpected keyword argument '%S'",
4016 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004017 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004018 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004019
Christian Heimes0bd447f2013-07-20 14:48:10 +02004020 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4021 goto fail;
4022 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004023 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004024
Benjamin Petersonb204a422011-06-05 22:04:07 -05004025 kw_found:
4026 if (GETLOCAL(j) != NULL) {
4027 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02004028 "%U() got multiple values for argument '%S'",
4029 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004030 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004031 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004032 Py_INCREF(value);
4033 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004034 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004035
4036 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004037 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004038 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004039 goto fail;
4040 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004041
4042 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004043 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02004044 Py_ssize_t m = co->co_argcount - defcount;
4045 Py_ssize_t missing = 0;
4046 for (i = argcount; i < m; i++) {
4047 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004048 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004049 }
4050 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004051 if (missing) {
4052 missing_arguments(co, missing, defcount, fastlocals);
4053 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004054 }
4055 if (n > m)
4056 i = n - m;
4057 else
4058 i = 0;
4059 for (; i < defcount; i++) {
4060 if (GETLOCAL(m+i) == NULL) {
4061 PyObject *def = defs[i];
4062 Py_INCREF(def);
4063 SETLOCAL(m+i, def);
4064 }
4065 }
4066 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004067
4068 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004069 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004070 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004071 for (i = co->co_argcount; i < total_args; i++) {
4072 PyObject *name;
4073 if (GETLOCAL(i) != NULL)
4074 continue;
4075 name = PyTuple_GET_ITEM(co->co_varnames, i);
4076 if (kwdefs != NULL) {
4077 PyObject *def = PyDict_GetItem(kwdefs, name);
4078 if (def) {
4079 Py_INCREF(def);
4080 SETLOCAL(i, def);
4081 continue;
4082 }
4083 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004084 missing++;
4085 }
4086 if (missing) {
4087 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004088 goto fail;
4089 }
4090 }
4091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004092 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004093 vars into frame. */
4094 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004095 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004096 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004097 /* Possibly account for the cell variable being an argument. */
4098 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004099 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004100 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004101 /* Clear the local copy. */
4102 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004103 }
4104 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004105 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004106 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004107 if (c == NULL)
4108 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004109 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004110 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004111
4112 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004113 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4114 PyObject *o = PyTuple_GET_ITEM(closure, i);
4115 Py_INCREF(o);
4116 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004117 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004118
Yury Selivanoveb636452016-09-08 22:01:51 -07004119 /* Handle generator/coroutine/asynchronous generator */
4120 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004121 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04004122 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004123 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004124
4125 if (is_coro && tstate->in_coroutine_wrapper) {
4126 assert(coro_wrapper != NULL);
4127 PyErr_Format(PyExc_RuntimeError,
4128 "coroutine wrapper %.200R attempted "
4129 "to recursively wrap %.200R",
4130 coro_wrapper,
4131 co);
4132 goto fail;
4133 }
Yury Selivanov75445082015-05-11 22:57:16 -04004134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004135 /* Don't need to keep the reference to f_back, it will be set
4136 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004137 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004139 /* Create a new generator that owns the ready to run frame
4140 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004141 if (is_coro) {
4142 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004143 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4144 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004145 } else {
4146 gen = PyGen_NewWithQualName(f, name, qualname);
4147 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004148 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004149 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004150 }
INADA Naoki9c157762016-12-26 18:52:46 +09004151
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004152 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004153
Yury Selivanov94c22632015-06-04 10:16:51 -04004154 if (is_coro && coro_wrapper != NULL) {
4155 PyObject *wrapped;
4156 tstate->in_coroutine_wrapper = 1;
4157 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4158 tstate->in_coroutine_wrapper = 0;
4159 return wrapped;
4160 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004161
Yury Selivanov75445082015-05-11 22:57:16 -04004162 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004163 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004164
Victor Stinner59a73272016-12-09 18:51:13 +01004165 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004166
Thomas Woutersce272b62007-09-19 21:19:28 +00004167fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004169 /* decref'ing the frame can cause __del__ methods to get invoked,
4170 which can call back into Python. While we're done with the
4171 current Python frame (f), the associated C stack is still in use,
4172 so recursion_depth must be boosted for the duration.
4173 */
4174 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09004175 if (Py_REFCNT(f) > 1) {
4176 Py_DECREF(f);
4177 _PyObject_GC_TRACK(f);
4178 }
4179 else {
4180 ++tstate->recursion_depth;
4181 Py_DECREF(f);
4182 --tstate->recursion_depth;
4183 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004184 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004185}
4186
Victor Stinner40ee3012014-06-16 15:59:28 +02004187PyObject *
4188PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
4189 PyObject **args, int argcount, PyObject **kws, int kwcount,
4190 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
4191{
4192 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004193 args, argcount,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004194 kws, kws + 1, kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004195 defs, defcount,
4196 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004197 NULL, NULL);
4198}
Tim Peters5ca576e2001-06-18 22:08:13 +00004199
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004200static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004201special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004203 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004204 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004205 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004206 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004207 return NULL;
4208 }
4209 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004210}
4211
4212
Benjamin Peterson87880242011-07-03 16:48:31 -05004213/* These 3 functions deal with the exception state of generators. */
4214
4215static void
4216save_exc_state(PyThreadState *tstate, PyFrameObject *f)
4217{
4218 PyObject *type, *value, *traceback;
4219 Py_XINCREF(tstate->exc_type);
4220 Py_XINCREF(tstate->exc_value);
4221 Py_XINCREF(tstate->exc_traceback);
4222 type = f->f_exc_type;
4223 value = f->f_exc_value;
4224 traceback = f->f_exc_traceback;
4225 f->f_exc_type = tstate->exc_type;
4226 f->f_exc_value = tstate->exc_value;
4227 f->f_exc_traceback = tstate->exc_traceback;
4228 Py_XDECREF(type);
4229 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004230 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05004231}
4232
4233static void
4234swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
4235{
4236 PyObject *tmp;
4237 tmp = tstate->exc_type;
4238 tstate->exc_type = f->f_exc_type;
4239 f->f_exc_type = tmp;
4240 tmp = tstate->exc_value;
4241 tstate->exc_value = f->f_exc_value;
4242 f->f_exc_value = tmp;
4243 tmp = tstate->exc_traceback;
4244 tstate->exc_traceback = f->f_exc_traceback;
4245 f->f_exc_traceback = tmp;
4246}
4247
4248static void
4249restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
4250{
4251 PyObject *type, *value, *tb;
4252 type = tstate->exc_type;
4253 value = tstate->exc_value;
4254 tb = tstate->exc_traceback;
4255 tstate->exc_type = f->f_exc_type;
4256 tstate->exc_value = f->f_exc_value;
4257 tstate->exc_traceback = f->f_exc_traceback;
4258 f->f_exc_type = NULL;
4259 f->f_exc_value = NULL;
4260 f->f_exc_traceback = NULL;
4261 Py_XDECREF(type);
4262 Py_XDECREF(value);
4263 Py_XDECREF(tb);
4264}
4265
4266
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004267/* Logic for the raise statement (too complicated for inlining).
4268 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004269static int
Collin Winter828f04a2007-08-31 00:04:24 +00004270do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004271{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004272 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004274 if (exc == NULL) {
4275 /* Reraise */
4276 PyThreadState *tstate = PyThreadState_GET();
4277 PyObject *tb;
4278 type = tstate->exc_type;
4279 value = tstate->exc_value;
4280 tb = tstate->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004281 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004282 PyErr_SetString(PyExc_RuntimeError,
4283 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004284 return 0;
4285 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004286 Py_XINCREF(type);
4287 Py_XINCREF(value);
4288 Py_XINCREF(tb);
4289 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004290 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004291 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004293 /* We support the following forms of raise:
4294 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004295 raise <instance>
4296 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004298 if (PyExceptionClass_Check(exc)) {
4299 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004300 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004301 if (value == NULL)
4302 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004303 if (!PyExceptionInstance_Check(value)) {
4304 PyErr_Format(PyExc_TypeError,
4305 "calling %R should have returned an instance of "
4306 "BaseException, not %R",
4307 type, Py_TYPE(value));
4308 goto raise_error;
4309 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004310 }
4311 else if (PyExceptionInstance_Check(exc)) {
4312 value = exc;
4313 type = PyExceptionInstance_Class(exc);
4314 Py_INCREF(type);
4315 }
4316 else {
4317 /* Not something you can raise. You get an exception
4318 anyway, just not what you specified :-) */
4319 Py_DECREF(exc);
4320 PyErr_SetString(PyExc_TypeError,
4321 "exceptions must derive from BaseException");
4322 goto raise_error;
4323 }
Collin Winter828f04a2007-08-31 00:04:24 +00004324
Serhiy Storchakac0191582016-09-27 11:37:10 +03004325 assert(type != NULL);
4326 assert(value != NULL);
4327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004328 if (cause) {
4329 PyObject *fixed_cause;
4330 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004331 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004332 if (fixed_cause == NULL)
4333 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004334 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004335 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004336 else if (PyExceptionInstance_Check(cause)) {
4337 fixed_cause = cause;
4338 }
4339 else if (cause == Py_None) {
4340 Py_DECREF(cause);
4341 fixed_cause = NULL;
4342 }
4343 else {
4344 PyErr_SetString(PyExc_TypeError,
4345 "exception causes must derive from "
4346 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004347 goto raise_error;
4348 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004349 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004350 }
Collin Winter828f04a2007-08-31 00:04:24 +00004351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004352 PyErr_SetObject(type, value);
4353 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004354 Py_DECREF(value);
4355 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004356 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004357
4358raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004359 Py_XDECREF(value);
4360 Py_XDECREF(type);
4361 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004362 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004363}
4364
Tim Petersd6d010b2001-06-21 02:49:55 +00004365/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004366 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004367
Guido van Rossum0368b722007-05-11 16:50:42 +00004368 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4369 with a variable target.
4370*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004371
Barry Warsawe42b18f1997-08-25 22:13:04 +00004372static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004373unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004374{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004375 int i = 0, j = 0;
4376 Py_ssize_t ll = 0;
4377 PyObject *it; /* iter(v) */
4378 PyObject *w;
4379 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004381 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004383 it = PyObject_GetIter(v);
4384 if (it == NULL)
4385 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00004386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004387 for (; i < argcnt; i++) {
4388 w = PyIter_Next(it);
4389 if (w == NULL) {
4390 /* Iterator done, via error or exhaustion. */
4391 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004392 if (argcntafter == -1) {
4393 PyErr_Format(PyExc_ValueError,
4394 "not enough values to unpack (expected %d, got %d)",
4395 argcnt, i);
4396 }
4397 else {
4398 PyErr_Format(PyExc_ValueError,
4399 "not enough values to unpack "
4400 "(expected at least %d, got %d)",
4401 argcnt + argcntafter, i);
4402 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004403 }
4404 goto Error;
4405 }
4406 *--sp = w;
4407 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004409 if (argcntafter == -1) {
4410 /* We better have exhausted the iterator now. */
4411 w = PyIter_Next(it);
4412 if (w == NULL) {
4413 if (PyErr_Occurred())
4414 goto Error;
4415 Py_DECREF(it);
4416 return 1;
4417 }
4418 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004419 PyErr_Format(PyExc_ValueError,
4420 "too many values to unpack (expected %d)",
4421 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004422 goto Error;
4423 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004425 l = PySequence_List(it);
4426 if (l == NULL)
4427 goto Error;
4428 *--sp = l;
4429 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004431 ll = PyList_GET_SIZE(l);
4432 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004433 PyErr_Format(PyExc_ValueError,
4434 "not enough values to unpack (expected at least %d, got %zd)",
4435 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004436 goto Error;
4437 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004439 /* Pop the "after-variable" args off the list. */
4440 for (j = argcntafter; j > 0; j--, i++) {
4441 *--sp = PyList_GET_ITEM(l, ll - j);
4442 }
4443 /* Resize the list. */
4444 Py_SIZE(l) = ll - argcntafter;
4445 Py_DECREF(it);
4446 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004447
Tim Petersd6d010b2001-06-21 02:49:55 +00004448Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004449 for (; i > 0; i--, sp++)
4450 Py_DECREF(*sp);
4451 Py_XDECREF(it);
4452 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004453}
4454
4455
Guido van Rossum96a42c81992-01-12 02:29:51 +00004456#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004457static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004458prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004459{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004460 printf("%s ", str);
4461 if (PyObject_Print(v, stdout, 0) != 0)
4462 PyErr_Clear(); /* Don't know what else to do */
4463 printf("\n");
4464 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004465}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004466#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004467
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004468static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004469call_exc_trace(Py_tracefunc func, PyObject *self,
4470 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004471{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004472 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004473 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004474 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004475 if (value == NULL) {
4476 value = Py_None;
4477 Py_INCREF(value);
4478 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004479 PyErr_NormalizeException(&type, &value, &orig_traceback);
4480 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004481 arg = PyTuple_Pack(3, type, value, traceback);
4482 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004483 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004484 return;
4485 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004486 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004487 Py_DECREF(arg);
4488 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004489 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004490 else {
4491 Py_XDECREF(type);
4492 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004493 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004494 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004495}
4496
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004497static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004498call_trace_protected(Py_tracefunc func, PyObject *obj,
4499 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004500 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004502 PyObject *type, *value, *traceback;
4503 int err;
4504 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004505 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004506 if (err == 0)
4507 {
4508 PyErr_Restore(type, value, traceback);
4509 return 0;
4510 }
4511 else {
4512 Py_XDECREF(type);
4513 Py_XDECREF(value);
4514 Py_XDECREF(traceback);
4515 return -1;
4516 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004517}
4518
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004519static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004520call_trace(Py_tracefunc func, PyObject *obj,
4521 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004522 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004523{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004524 int result;
4525 if (tstate->tracing)
4526 return 0;
4527 tstate->tracing++;
4528 tstate->use_tracing = 0;
4529 result = func(obj, frame, what, arg);
4530 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4531 || (tstate->c_profilefunc != NULL));
4532 tstate->tracing--;
4533 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004534}
4535
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004536PyObject *
4537_PyEval_CallTracing(PyObject *func, PyObject *args)
4538{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004539 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004540 int save_tracing = tstate->tracing;
4541 int save_use_tracing = tstate->use_tracing;
4542 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004544 tstate->tracing = 0;
4545 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4546 || (tstate->c_profilefunc != NULL));
4547 result = PyObject_Call(func, args, NULL);
4548 tstate->tracing = save_tracing;
4549 tstate->use_tracing = save_use_tracing;
4550 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004551}
4552
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004553/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004554static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004555maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004556 PyThreadState *tstate, PyFrameObject *frame,
4557 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004558{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004559 int result = 0;
4560 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004562 /* If the last instruction executed isn't in the current
4563 instruction window, reset the window.
4564 */
4565 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4566 PyAddrPair bounds;
4567 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4568 &bounds);
4569 *instr_lb = bounds.ap_lower;
4570 *instr_ub = bounds.ap_upper;
4571 }
4572 /* If the last instruction falls at the start of a line or if
4573 it represents a jump backwards, update the frame's line
4574 number and call the trace function. */
4575 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
4576 frame->f_lineno = line;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004577 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004578 }
4579 *instr_prev = frame->f_lasti;
4580 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004581}
4582
Fred Drake5755ce62001-06-27 19:19:46 +00004583void
4584PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004585{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004586 PyThreadState *tstate = PyThreadState_GET();
4587 PyObject *temp = tstate->c_profileobj;
4588 Py_XINCREF(arg);
4589 tstate->c_profilefunc = NULL;
4590 tstate->c_profileobj = NULL;
4591 /* Must make sure that tracing is not ignored if 'temp' is freed */
4592 tstate->use_tracing = tstate->c_tracefunc != NULL;
4593 Py_XDECREF(temp);
4594 tstate->c_profilefunc = func;
4595 tstate->c_profileobj = arg;
4596 /* Flag that tracing or profiling is turned on */
4597 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004598}
4599
4600void
4601PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4602{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004603 PyThreadState *tstate = PyThreadState_GET();
4604 PyObject *temp = tstate->c_traceobj;
4605 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4606 Py_XINCREF(arg);
4607 tstate->c_tracefunc = NULL;
4608 tstate->c_traceobj = NULL;
4609 /* Must make sure that profiling is not ignored if 'temp' is freed */
4610 tstate->use_tracing = tstate->c_profilefunc != NULL;
4611 Py_XDECREF(temp);
4612 tstate->c_tracefunc = func;
4613 tstate->c_traceobj = arg;
4614 /* Flag that tracing or profiling is turned on */
4615 tstate->use_tracing = ((func != NULL)
4616 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004617}
4618
Yury Selivanov75445082015-05-11 22:57:16 -04004619void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004620_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004621{
4622 PyThreadState *tstate = PyThreadState_GET();
4623
Yury Selivanov75445082015-05-11 22:57:16 -04004624 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004625 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004626}
4627
4628PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004629_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004630{
4631 PyThreadState *tstate = PyThreadState_GET();
4632 return tstate->coroutine_wrapper;
4633}
4634
Yury Selivanoveb636452016-09-08 22:01:51 -07004635void
4636_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4637{
4638 PyThreadState *tstate = PyThreadState_GET();
4639
4640 Py_XINCREF(firstiter);
4641 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4642}
4643
4644PyObject *
4645_PyEval_GetAsyncGenFirstiter(void)
4646{
4647 PyThreadState *tstate = PyThreadState_GET();
4648 return tstate->async_gen_firstiter;
4649}
4650
4651void
4652_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4653{
4654 PyThreadState *tstate = PyThreadState_GET();
4655
4656 Py_XINCREF(finalizer);
4657 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4658}
4659
4660PyObject *
4661_PyEval_GetAsyncGenFinalizer(void)
4662{
4663 PyThreadState *tstate = PyThreadState_GET();
4664 return tstate->async_gen_finalizer;
4665}
4666
Guido van Rossumb209a111997-04-29 18:18:01 +00004667PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004668PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004669{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004670 PyFrameObject *current_frame = PyEval_GetFrame();
4671 if (current_frame == NULL)
4672 return PyThreadState_GET()->interp->builtins;
4673 else
4674 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004675}
4676
Guido van Rossumb209a111997-04-29 18:18:01 +00004677PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004678PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004679{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004680 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004681 if (current_frame == NULL) {
4682 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004683 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004684 }
4685
4686 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4687 return NULL;
4688
4689 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004690 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004691}
4692
Guido van Rossumb209a111997-04-29 18:18:01 +00004693PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004694PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004695{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004696 PyFrameObject *current_frame = PyEval_GetFrame();
4697 if (current_frame == NULL)
4698 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004699
4700 assert(current_frame->f_globals != NULL);
4701 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004702}
4703
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004704PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004705PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004706{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004707 PyThreadState *tstate = PyThreadState_GET();
4708 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004709}
4710
Guido van Rossum6135a871995-01-09 17:53:26 +00004711int
Tim Peters5ba58662001-07-16 02:29:45 +00004712PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004713{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004714 PyFrameObject *current_frame = PyEval_GetFrame();
4715 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004717 if (current_frame != NULL) {
4718 const int codeflags = current_frame->f_code->co_flags;
4719 const int compilerflags = codeflags & PyCF_MASK;
4720 if (compilerflags) {
4721 result = 1;
4722 cf->cf_flags |= compilerflags;
4723 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004724#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004725 if (codeflags & CO_GENERATOR_ALLOWED) {
4726 result = 1;
4727 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4728 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004729#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004730 }
4731 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004732}
4733
Guido van Rossum3f5da241990-12-20 15:06:42 +00004734
Guido van Rossum681d79a1995-07-18 14:51:37 +00004735/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00004736 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00004737
Guido van Rossumb209a111997-04-29 18:18:01 +00004738PyObject *
Victor Stinner2d0eb652016-12-06 16:27:24 +01004739PyEval_CallObjectWithKeywords(PyObject *callable,
4740 PyObject *args, PyObject *kwargs)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004741{
Victor Stinner59b356d2015-03-16 11:52:32 +01004742#ifdef Py_DEBUG
4743 /* PyEval_CallObjectWithKeywords() must not be called with an exception
4744 set. It raises a new exception if parameters are invalid or if
4745 PyTuple_New() fails, and so the original exception is lost. */
4746 assert(!PyErr_Occurred());
4747#endif
4748
Victor Stinner8a31c822016-08-19 17:12:23 +02004749 if (args == NULL) {
Victor Stinner2d0eb652016-12-06 16:27:24 +01004750 return _PyObject_FastCallDict(callable, NULL, 0, kwargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004751 }
Victor Stinner155ea652016-08-22 23:26:00 +02004752
4753 if (!PyTuple_Check(args)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004754 PyErr_SetString(PyExc_TypeError,
4755 "argument list must be a tuple");
4756 return NULL;
4757 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00004758
Victor Stinner8a31c822016-08-19 17:12:23 +02004759 if (kwargs != NULL && !PyDict_Check(kwargs)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004760 PyErr_SetString(PyExc_TypeError,
4761 "keyword list must be a dictionary");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004762 return NULL;
4763 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00004764
Victor Stinner2d0eb652016-12-06 16:27:24 +01004765 return PyObject_Call(callable, args, kwargs);
Jeremy Hylton52820442001-01-03 23:52:36 +00004766}
4767
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004768const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004769PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004770{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004771 if (PyMethod_Check(func))
4772 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4773 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004774 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004775 else if (PyCFunction_Check(func))
4776 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4777 else
4778 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004779}
4780
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004781const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004782PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004783{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004784 if (PyMethod_Check(func))
4785 return "()";
4786 else if (PyFunction_Check(func))
4787 return "()";
4788 else if (PyCFunction_Check(func))
4789 return "()";
4790 else
4791 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004792}
4793
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004794#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004795if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004796 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4797 tstate, tstate->frame, \
4798 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004799 x = NULL; \
4800 } \
4801 else { \
4802 x = call; \
4803 if (tstate->c_profilefunc != NULL) { \
4804 if (x == NULL) { \
4805 call_trace_protected(tstate->c_profilefunc, \
4806 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004807 tstate, tstate->frame, \
4808 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004809 /* XXX should pass (type, value, tb) */ \
4810 } else { \
4811 if (call_trace(tstate->c_profilefunc, \
4812 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004813 tstate, tstate->frame, \
4814 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004815 Py_DECREF(x); \
4816 x = NULL; \
4817 } \
4818 } \
4819 } \
4820 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004821} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004822 x = call; \
4823 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004824
Victor Stinner415c5102017-01-11 00:54:57 +01004825/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4826 to reduce the stack consumption. */
4827Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004828call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004829{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004830 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004831 PyObject *func = *pfunc;
4832 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004833 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4834 Py_ssize_t nargs = oparg - nkwargs;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004835 PyObject **stack;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004837 /* Always dispatch PyCFunction first, because these are
4838 presumed to be the most frequent callable object.
4839 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004840 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004841 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004842
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004843 stack = (*pp_stack) - nargs - nkwargs;
4844 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004845 }
4846 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004847 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004848 /* Optimize access to bound methods. Reuse the Python stack
4849 to pass 'self' as the first argument, replace 'func'
4850 with 'self'. It avoids the creation of a new temporary tuple
4851 for arguments (to replace func with self) when the method uses
4852 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004853 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004854 Py_INCREF(self);
4855 func = PyMethod_GET_FUNCTION(func);
4856 Py_INCREF(func);
4857 Py_SETREF(*pfunc, self);
4858 nargs++;
4859 }
4860 else {
4861 Py_INCREF(func);
4862 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004863
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004864 stack = (*pp_stack) - nargs - nkwargs;
Victor Stinner4a7cc882015-03-06 23:35:27 +01004865
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004866 if (PyFunction_Check(func)) {
4867 x = fast_function(func, stack, nargs, kwnames);
4868 }
4869 else {
4870 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4871 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004872
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004873 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004874 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004875
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004876 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004878 /* Clear the stack of the function object. Also removes
4879 the arguments in case they weren't consumed already
Victor Stinnere90bdb12016-08-25 23:26:50 +02004880 (fast_function() and err_args() leave them on the stack).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004881 */
4882 while ((*pp_stack) > pfunc) {
4883 w = EXT_POP(*pp_stack);
4884 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004885 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004887 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004888}
4889
Victor Stinnere90bdb12016-08-25 23:26:50 +02004890/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004891 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004892 For the simplest case -- a function that takes only positional
4893 arguments and is called with only positional arguments -- it
4894 inlines the most primitive frame setup code from
4895 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4896 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004897*/
4898
Victor Stinnerc6944e72016-11-11 02:13:35 +01004899static PyObject* _Py_HOT_FUNCTION
Victor Stinnerd8735722016-09-09 12:36:44 -07004900_PyFunction_FastCall(PyCodeObject *co, PyObject **args, Py_ssize_t nargs,
4901 PyObject *globals)
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004902{
4903 PyFrameObject *f;
4904 PyThreadState *tstate = PyThreadState_GET();
4905 PyObject **fastlocals;
4906 Py_ssize_t i;
4907 PyObject *result;
4908
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004909 assert(globals != NULL);
4910 /* XXX Perhaps we should create a specialized
INADA Naoki5a625d02016-12-24 20:19:08 +09004911 _PyFrame_New_NoTrack() that doesn't take locals, but does
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004912 take builtins without sanity checking them.
4913 */
4914 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09004915 f = _PyFrame_New_NoTrack(tstate, co, globals, NULL);
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004916 if (f == NULL) {
4917 return NULL;
4918 }
4919
4920 fastlocals = f->f_localsplus;
4921
Victor Stinner74319ae2016-08-25 00:04:09 +02004922 for (i = 0; i < nargs; i++) {
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004923 Py_INCREF(*args);
4924 fastlocals[i] = *args++;
4925 }
Victor Stinner59a73272016-12-09 18:51:13 +01004926 result = PyEval_EvalFrameEx(f,0);
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004927
INADA Naoki5a625d02016-12-24 20:19:08 +09004928 if (Py_REFCNT(f) > 1) {
4929 Py_DECREF(f);
4930 _PyObject_GC_TRACK(f);
4931 }
4932 else {
4933 ++tstate->recursion_depth;
4934 Py_DECREF(f);
4935 --tstate->recursion_depth;
4936 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004937 return result;
4938}
4939
Victor Stinnere90bdb12016-08-25 23:26:50 +02004940static PyObject *
Victor Stinnerd8735722016-09-09 12:36:44 -07004941fast_function(PyObject *func, PyObject **stack,
4942 Py_ssize_t nargs, PyObject *kwnames)
Jeremy Hylton52820442001-01-03 23:52:36 +00004943{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004944 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4945 PyObject *globals = PyFunction_GET_GLOBALS(func);
4946 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004947 PyObject *kwdefs, *closure, *name, *qualname;
4948 PyObject **d;
Victor Stinnerd8735722016-09-09 12:36:44 -07004949 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004950 Py_ssize_t nd;
Victor Stinnerd8735722016-09-09 12:36:44 -07004951
Victor Stinner57f91ac2016-09-12 13:37:07 +02004952 assert(PyFunction_Check(func));
4953 assert(nargs >= 0);
4954 assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
Victor Stinnerd8735722016-09-09 12:36:44 -07004955 assert((nargs == 0 && nkwargs == 0) || stack != NULL);
Victor Stinner57f91ac2016-09-12 13:37:07 +02004956 /* kwnames must only contains str strings, no subclass, and all keys must
4957 be unique */
Victor Stinner577e1f82016-08-25 00:29:32 +02004958
Victor Stinner74319ae2016-08-25 00:04:09 +02004959 if (co->co_kwonlyargcount == 0 && nkwargs == 0 &&
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004960 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
4961 {
Victor Stinner2eedc112016-08-22 12:29:42 +02004962 if (argdefs == NULL && co->co_argcount == nargs) {
Victor Stinnerd8735722016-09-09 12:36:44 -07004963 return _PyFunction_FastCall(co, stack, nargs, globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02004964 }
4965 else if (nargs == 0 && argdefs != NULL
4966 && co->co_argcount == Py_SIZE(argdefs)) {
4967 /* function called with no arguments, but all parameters have
4968 a default value: use default values as arguments .*/
4969 stack = &PyTuple_GET_ITEM(argdefs, 0);
Victor Stinnerd8735722016-09-09 12:36:44 -07004970 return _PyFunction_FastCall(co, stack, Py_SIZE(argdefs), globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02004971 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004972 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004973
4974 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
4975 closure = PyFunction_GET_CLOSURE(func);
4976 name = ((PyFunctionObject *)func) -> func_name;
4977 qualname = ((PyFunctionObject *)func) -> func_qualname;
4978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004979 if (argdefs != NULL) {
4980 d = &PyTuple_GET_ITEM(argdefs, 0);
4981 nd = Py_SIZE(argdefs);
4982 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004983 else {
4984 d = NULL;
4985 nd = 0;
4986 }
4987 return _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
Victor Stinner2eedc112016-08-22 12:29:42 +02004988 stack, nargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004989 nkwargs ? &PyTuple_GET_ITEM(kwnames, 0) : NULL,
4990 stack + nargs,
4991 nkwargs, 1,
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004992 d, (int)nd, kwdefs,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004993 closure, name, qualname);
4994}
4995
4996PyObject *
Victor Stinnerd8735722016-09-09 12:36:44 -07004997_PyFunction_FastCallKeywords(PyObject *func, PyObject **stack,
4998 Py_ssize_t nargs, PyObject *kwnames)
4999{
5000 return fast_function(func, stack, nargs, kwnames);
5001}
5002
5003PyObject *
Victor Stinner74319ae2016-08-25 00:04:09 +02005004_PyFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs,
Victor Stinnerb9009392016-08-22 23:15:44 +02005005 PyObject *kwargs)
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005006{
5007 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
5008 PyObject *globals = PyFunction_GET_GLOBALS(func);
5009 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
5010 PyObject *kwdefs, *closure, *name, *qualname;
Victor Stinnerb9009392016-08-22 23:15:44 +02005011 PyObject *kwtuple, **k;
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005012 PyObject **d;
Victor Stinner74319ae2016-08-25 00:04:09 +02005013 Py_ssize_t nd, nk;
Victor Stinnerb9009392016-08-22 23:15:44 +02005014 PyObject *result;
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005015
Victor Stinner74319ae2016-08-25 00:04:09 +02005016 assert(func != NULL);
5017 assert(nargs >= 0);
5018 assert(nargs == 0 || args != NULL);
Victor Stinnerb9009392016-08-22 23:15:44 +02005019 assert(kwargs == NULL || PyDict_Check(kwargs));
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005020
Victor Stinnerb9009392016-08-22 23:15:44 +02005021 if (co->co_kwonlyargcount == 0 &&
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02005022 (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) &&
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005023 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
5024 {
Victor Stinnerb9009392016-08-22 23:15:44 +02005025 /* Fast paths */
Victor Stinner2eedc112016-08-22 12:29:42 +02005026 if (argdefs == NULL && co->co_argcount == nargs) {
Victor Stinnerd8735722016-09-09 12:36:44 -07005027 return _PyFunction_FastCall(co, args, nargs, globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02005028 }
5029 else if (nargs == 0 && argdefs != NULL
5030 && co->co_argcount == Py_SIZE(argdefs)) {
5031 /* function called with no arguments, but all parameters have
5032 a default value: use default values as arguments .*/
5033 args = &PyTuple_GET_ITEM(argdefs, 0);
Victor Stinnerd8735722016-09-09 12:36:44 -07005034 return _PyFunction_FastCall(co, args, Py_SIZE(argdefs), globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02005035 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005036 }
5037
Victor Stinner865a0f62017-01-03 02:01:42 +01005038 nk = (kwargs != NULL) ? PyDict_GET_SIZE(kwargs) : 0;
5039 if (nk != 0) {
Victor Stinnerb9009392016-08-22 23:15:44 +02005040 Py_ssize_t pos, i;
Victor Stinnerb9009392016-08-22 23:15:44 +02005041
5042 kwtuple = PyTuple_New(2 * nk);
5043 if (kwtuple == NULL) {
5044 return NULL;
5045 }
5046
5047 k = &PyTuple_GET_ITEM(kwtuple, 0);
5048 pos = i = 0;
5049 while (PyDict_Next(kwargs, &pos, &k[i], &k[i+1])) {
Victor Stinner865a0f62017-01-03 02:01:42 +01005050 /* We must hold strong references because keyword arguments can be
5051 indirectly modified while the function is called:
5052 see issue #2016 and test_extcall */
Victor Stinnerb9009392016-08-22 23:15:44 +02005053 Py_INCREF(k[i]);
5054 Py_INCREF(k[i+1]);
5055 i += 2;
5056 }
5057 nk = i / 2;
5058 }
5059 else {
5060 kwtuple = NULL;
5061 k = NULL;
Victor Stinnerb9009392016-08-22 23:15:44 +02005062 }
5063
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005064 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
5065 closure = PyFunction_GET_CLOSURE(func);
5066 name = ((PyFunctionObject *)func) -> func_name;
5067 qualname = ((PyFunctionObject *)func) -> func_qualname;
5068
5069 if (argdefs != NULL) {
5070 d = &PyTuple_GET_ITEM(argdefs, 0);
5071 nd = Py_SIZE(argdefs);
5072 }
5073 else {
5074 d = NULL;
5075 nd = 0;
5076 }
Victor Stinnerb9009392016-08-22 23:15:44 +02005077
5078 result = _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
5079 args, nargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03005080 k, k + 1, nk, 2,
Victor Stinnerb9009392016-08-22 23:15:44 +02005081 d, nd, kwdefs,
5082 closure, name, qualname);
5083 Py_XDECREF(kwtuple);
5084 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00005085}
5086
5087static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005088do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005089{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005090 if (PyCFunction_Check(func)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005091 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005092 PyThreadState *tstate = PyThreadState_GET();
5093 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005094 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005095 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005096 else {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005097 return PyObject_Call(func, callargs, kwdict);
Victor Stinner74319ae2016-08-25 00:04:09 +02005098 }
Jeremy Hylton52820442001-01-03 23:52:36 +00005099}
5100
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005101/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005102 nb_index slot defined, and store in *pi.
5103 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
5104 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 +00005105 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005106*/
Tim Petersb5196382001-12-16 19:44:20 +00005107/* Note: If v is NULL, return success without storing into *pi. This
5108 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
5109 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00005110*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005111int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005112_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005114 if (v != NULL) {
5115 Py_ssize_t x;
5116 if (PyIndex_Check(v)) {
5117 x = PyNumber_AsSsize_t(v, NULL);
5118 if (x == -1 && PyErr_Occurred())
5119 return 0;
5120 }
5121 else {
5122 PyErr_SetString(PyExc_TypeError,
5123 "slice indices must be integers or "
5124 "None or have an __index__ method");
5125 return 0;
5126 }
5127 *pi = x;
5128 }
5129 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005130}
5131
Guido van Rossum486364b2007-06-30 05:01:58 +00005132#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005133 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00005134
Guido van Rossumb209a111997-04-29 18:18:01 +00005135static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02005136cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005137{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005138 int res = 0;
5139 switch (op) {
5140 case PyCmp_IS:
5141 res = (v == w);
5142 break;
5143 case PyCmp_IS_NOT:
5144 res = (v != w);
5145 break;
5146 case PyCmp_IN:
5147 res = PySequence_Contains(w, v);
5148 if (res < 0)
5149 return NULL;
5150 break;
5151 case PyCmp_NOT_IN:
5152 res = PySequence_Contains(w, v);
5153 if (res < 0)
5154 return NULL;
5155 res = !res;
5156 break;
5157 case PyCmp_EXC_MATCH:
5158 if (PyTuple_Check(w)) {
5159 Py_ssize_t i, length;
5160 length = PyTuple_Size(w);
5161 for (i = 0; i < length; i += 1) {
5162 PyObject *exc = PyTuple_GET_ITEM(w, i);
5163 if (!PyExceptionClass_Check(exc)) {
5164 PyErr_SetString(PyExc_TypeError,
5165 CANNOT_CATCH_MSG);
5166 return NULL;
5167 }
5168 }
5169 }
5170 else {
5171 if (!PyExceptionClass_Check(w)) {
5172 PyErr_SetString(PyExc_TypeError,
5173 CANNOT_CATCH_MSG);
5174 return NULL;
5175 }
5176 }
5177 res = PyErr_GivenExceptionMatches(v, w);
5178 break;
5179 default:
5180 return PyObject_RichCompare(v, w, op);
5181 }
5182 v = res ? Py_True : Py_False;
5183 Py_INCREF(v);
5184 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005185}
5186
Thomas Wouters52152252000-08-17 22:55:00 +00005187static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005188import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
5189{
5190 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005191 PyObject *import_func, *res;
5192 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005193
5194 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
5195 if (import_func == NULL) {
5196 PyErr_SetString(PyExc_ImportError, "__import__ not found");
5197 return NULL;
5198 }
5199
5200 /* Fast path for not overloaded __import__. */
5201 if (import_func == PyThreadState_GET()->interp->import_func) {
5202 int ilevel = _PyLong_AsInt(level);
5203 if (ilevel == -1 && PyErr_Occurred()) {
5204 return NULL;
5205 }
5206 res = PyImport_ImportModuleLevelObject(
5207 name,
5208 f->f_globals,
5209 f->f_locals == NULL ? Py_None : f->f_locals,
5210 fromlist,
5211 ilevel);
5212 return res;
5213 }
5214
5215 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005216
5217 stack[0] = name;
5218 stack[1] = f->f_globals;
5219 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5220 stack[3] = fromlist;
5221 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005222 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005223 Py_DECREF(import_func);
5224 return res;
5225}
5226
5227static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00005228import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005229{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005230 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005231 _Py_IDENTIFIER(__name__);
5232 PyObject *fullmodname, *pkgname;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005234 x = PyObject_GetAttr(v, name);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005235 if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
5236 return x;
5237 /* Issue #17636: in case this failed because of a circular relative
5238 import, try to fallback on reading the module directly from
5239 sys.modules. */
5240 PyErr_Clear();
5241 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005242 if (pkgname == NULL) {
5243 goto error;
5244 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005245 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
5246 Py_DECREF(pkgname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005247 if (fullmodname == NULL) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005248 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005249 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005250 x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005251 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005252 if (x == NULL) {
5253 goto error;
5254 }
5255 Py_INCREF(x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005256 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005257 error:
5258 PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
5259 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005260}
Guido van Rossumac7be682001-01-17 15:42:30 +00005261
Thomas Wouters52152252000-08-17 22:55:00 +00005262static int
5263import_all_from(PyObject *locals, PyObject *v)
5264{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005265 _Py_IDENTIFIER(__all__);
5266 _Py_IDENTIFIER(__dict__);
5267 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005268 PyObject *dict, *name, *value;
5269 int skip_leading_underscores = 0;
5270 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005272 if (all == NULL) {
5273 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5274 return -1; /* Unexpected error */
5275 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005276 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005277 if (dict == NULL) {
5278 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5279 return -1;
5280 PyErr_SetString(PyExc_ImportError,
5281 "from-import-* object has no __dict__ and no __all__");
5282 return -1;
5283 }
5284 all = PyMapping_Keys(dict);
5285 Py_DECREF(dict);
5286 if (all == NULL)
5287 return -1;
5288 skip_leading_underscores = 1;
5289 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005291 for (pos = 0, err = 0; ; pos++) {
5292 name = PySequence_GetItem(all, pos);
5293 if (name == NULL) {
5294 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5295 err = -1;
5296 else
5297 PyErr_Clear();
5298 break;
5299 }
5300 if (skip_leading_underscores &&
5301 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005302 PyUnicode_READY(name) != -1 &&
5303 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005304 {
5305 Py_DECREF(name);
5306 continue;
5307 }
5308 value = PyObject_GetAttr(v, name);
5309 if (value == NULL)
5310 err = -1;
5311 else if (PyDict_CheckExact(locals))
5312 err = PyDict_SetItem(locals, name, value);
5313 else
5314 err = PyObject_SetItem(locals, name, value);
5315 Py_DECREF(name);
5316 Py_XDECREF(value);
5317 if (err != 0)
5318 break;
5319 }
5320 Py_DECREF(all);
5321 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005322}
5323
Guido van Rossumac7be682001-01-17 15:42:30 +00005324static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005325format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005327 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005329 if (!obj)
5330 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005331
Serhiy Storchaka06515832016-11-20 09:13:07 +02005332 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005333 if (!obj_str)
5334 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005336 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005337}
Guido van Rossum950361c1997-01-24 13:49:28 +00005338
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005339static void
5340format_exc_unbound(PyCodeObject *co, int oparg)
5341{
5342 PyObject *name;
5343 /* Don't stomp existing exception */
5344 if (PyErr_Occurred())
5345 return;
5346 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5347 name = PyTuple_GET_ITEM(co->co_cellvars,
5348 oparg);
5349 format_exc_check_arg(
5350 PyExc_UnboundLocalError,
5351 UNBOUNDLOCAL_ERROR_MSG,
5352 name);
5353 } else {
5354 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5355 PyTuple_GET_SIZE(co->co_cellvars));
5356 format_exc_check_arg(PyExc_NameError,
5357 UNBOUNDFREE_ERROR_MSG, name);
5358 }
5359}
5360
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005361static PyObject *
5362unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005363 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005364{
5365 PyObject *res;
5366 if (Py_REFCNT(v) == 2) {
5367 /* In the common case, there are 2 references to the value
5368 * stored in 'variable' when the += is performed: one on the
5369 * value stack (in 'v') and one still stored in the
5370 * 'variable'. We try to delete the variable now to reduce
5371 * the refcnt to 1.
5372 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005373 int opcode, oparg;
5374 NEXTOPARG();
5375 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005376 case STORE_FAST:
5377 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005378 PyObject **fastlocals = f->f_localsplus;
5379 if (GETLOCAL(oparg) == v)
5380 SETLOCAL(oparg, NULL);
5381 break;
5382 }
5383 case STORE_DEREF:
5384 {
5385 PyObject **freevars = (f->f_localsplus +
5386 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005387 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005388 if (PyCell_GET(c) == v) {
5389 PyCell_SET(c, NULL);
5390 Py_DECREF(v);
5391 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005392 break;
5393 }
5394 case STORE_NAME:
5395 {
5396 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005397 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005398 PyObject *locals = f->f_locals;
5399 if (PyDict_CheckExact(locals) &&
5400 PyDict_GetItem(locals, name) == v) {
5401 if (PyDict_DelItem(locals, name) != 0) {
5402 PyErr_Clear();
5403 }
5404 }
5405 break;
5406 }
5407 }
5408 }
5409 res = v;
5410 PyUnicode_Append(&res, w);
5411 return res;
5412}
5413
Guido van Rossum950361c1997-01-24 13:49:28 +00005414#ifdef DYNAMIC_EXECUTION_PROFILE
5415
Skip Montanarof118cb12001-10-15 20:51:38 +00005416static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005417getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005418{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005419 int i;
5420 PyObject *l = PyList_New(256);
5421 if (l == NULL) return NULL;
5422 for (i = 0; i < 256; i++) {
5423 PyObject *x = PyLong_FromLong(a[i]);
5424 if (x == NULL) {
5425 Py_DECREF(l);
5426 return NULL;
5427 }
5428 PyList_SetItem(l, i, x);
5429 }
5430 for (i = 0; i < 256; i++)
5431 a[i] = 0;
5432 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005433}
5434
5435PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005436_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005437{
5438#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005439 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005440#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005441 int i;
5442 PyObject *l = PyList_New(257);
5443 if (l == NULL) return NULL;
5444 for (i = 0; i < 257; i++) {
5445 PyObject *x = getarray(dxpairs[i]);
5446 if (x == NULL) {
5447 Py_DECREF(l);
5448 return NULL;
5449 }
5450 PyList_SetItem(l, i, x);
5451 }
5452 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005453#endif
5454}
5455
5456#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005457
5458Py_ssize_t
5459_PyEval_RequestCodeExtraIndex(freefunc free)
5460{
5461 PyThreadState *tstate = PyThreadState_Get();
5462 Py_ssize_t new_index;
5463
5464 if (tstate->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
5465 return -1;
5466 }
5467 new_index = tstate->co_extra_user_count++;
5468 tstate->co_extra_freefuncs[new_index] = free;
5469 return new_index;
5470}
Łukasz Langaa785c872016-09-09 17:37:37 -07005471
5472static void
5473dtrace_function_entry(PyFrameObject *f)
5474{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005475 const char *filename;
5476 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005477 int lineno;
5478
5479 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5480 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5481 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5482
5483 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5484}
5485
5486static void
5487dtrace_function_return(PyFrameObject *f)
5488{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005489 const char *filename;
5490 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005491 int lineno;
5492
5493 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5494 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5495 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5496
5497 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5498}
5499
5500/* DTrace equivalent of maybe_call_line_trace. */
5501static void
5502maybe_dtrace_line(PyFrameObject *frame,
5503 int *instr_lb, int *instr_ub, int *instr_prev)
5504{
5505 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005506 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005507
5508 /* If the last instruction executed isn't in the current
5509 instruction window, reset the window.
5510 */
5511 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5512 PyAddrPair bounds;
5513 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5514 &bounds);
5515 *instr_lb = bounds.ap_lower;
5516 *instr_ub = bounds.ap_upper;
5517 }
5518 /* If the last instruction falls at the start of a line or if
5519 it represents a jump backwards, update the frame's line
5520 number and call the trace function. */
5521 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5522 frame->f_lineno = line;
5523 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5524 if (!co_filename)
5525 co_filename = "?";
5526 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5527 if (!co_name)
5528 co_name = "?";
5529 PyDTrace_LINE(co_filename, co_name, line);
5530 }
5531 *instr_prev = frame->f_lasti;
5532}