blob: 436e5cad25f8c94eabba52fb61fb0d9a72d6bb98 [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 */
Eric Snow05351c12017-09-05 21:43:08 -070039Py_LOCAL_INLINE(PyObject *) call_function(PyObject ***, Py_ssize_t, PyObject *);
Victor Stinnerf9b760f2016-09-09 10:17:08 -070040static PyObject * do_call_core(PyObject *, PyObject *, PyObject *);
Jeremy Hylton52820442001-01-03 23:52:36 +000041
Guido van Rossum0a066c01992-03-27 17:29:15 +000042#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000043static int lltrace;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020044static int prtrace(PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000045#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010046static int call_trace(Py_tracefunc, PyObject *,
47 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000049static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010050 PyThreadState *, PyFrameObject *,
51 int, PyObject *);
52static void call_exc_trace(Py_tracefunc, PyObject *,
53 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000054static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow05351c12017-09-05 21:43:08 -070055 PyThreadState *, PyFrameObject *, int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070056static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
57static void dtrace_function_entry(PyFrameObject *);
58static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000059
Thomas Wouters477c8d52006-05-27 19:21:47 +000060static PyObject * cmp_outcome(int, PyObject *, PyObject *);
Eric Snow05351c12017-09-05 21:43:08 -070061static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *, PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +000062static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +000063static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +000064static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +000065static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +020066static PyObject * unicode_concatenate(PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030067 PyFrameObject *, const _Py_CODEUNIT *);
Benjamin Petersonce798522012-01-22 11:24:29 -050068static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Serhiy Storchaka25e4f772017-08-03 11:37:15 +030069static int check_args_iterable(PyObject *func, PyObject *vararg);
70static void format_kwargs_mapping_error(PyObject *func, PyObject *kwargs);
Guido van Rossum374a9221991-04-04 10:40:29 +000071
Paul Prescode68140d2000-08-30 20:25:01 +000072#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000074#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000076#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 "free variable '%.200s' referenced before assignment" \
78 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000079
Guido van Rossum950361c1997-01-24 13:49:28 +000080/* Dynamic execution profile */
81#ifdef DYNAMIC_EXECUTION_PROFILE
82#ifdef DXPAIRS
83static long dxpairs[257][256];
84#define dxp dxpairs[256]
85#else
86static long dxp[256];
87#endif
88#endif
89
Benjamin Petersond2be5b42010-09-10 22:47:02 +000090#ifdef WITH_THREAD
Eric Snow05351c12017-09-05 21:43:08 -070091#define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request)
Benjamin Petersond2be5b42010-09-10 22:47:02 +000092#else
93#define GIL_REQUEST 0
94#endif
95
Jeffrey Yasskin39370832010-05-03 19:29:34 +000096/* This can set eval_breaker to 0 even though gil_drop_request became
97 1. We believe this is all right because the eval loop will release
98 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +000099#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 _Py_atomic_store_relaxed( \
Eric Snow05351c12017-09-05 21:43:08 -0700101 &eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000102 GIL_REQUEST | \
Eric Snow05351c12017-09-05 21:43:08 -0700103 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
104 pending_async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000105
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000106#ifdef WITH_THREAD
107
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000108#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 do { \
Eric Snow05351c12017-09-05 21:43:08 -0700110 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
111 _Py_atomic_store_relaxed(&eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000113
114#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 do { \
Eric Snow05351c12017-09-05 21:43:08 -0700116 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 COMPUTE_EVAL_BREAKER(); \
118 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000119
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000120#endif
121
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000122/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000123#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 do { \
Eric Snow05351c12017-09-05 21:43:08 -0700125 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
126 _Py_atomic_store_relaxed(&eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000128
129#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 do { \
Eric Snow05351c12017-09-05 21:43:08 -0700131 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 COMPUTE_EVAL_BREAKER(); \
133 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000134
135#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 do { \
Eric Snow05351c12017-09-05 21:43:08 -0700137 pending_async_exc = 1; \
138 _Py_atomic_store_relaxed(&eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000140
141#define UNSIGNAL_ASYNC_EXC() \
Eric Snow05351c12017-09-05 21:43:08 -0700142 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000143
144
Eric Snow05351c12017-09-05 21:43:08 -0700145/* This single variable consolidates all requests to break out of the fast path
146 in the eval loop. */
147static _Py_atomic_int eval_breaker = {0};
148/* Request for running pending calls. */
149static _Py_atomic_int pendingcalls_to_do = {0};
150/* Request for looking at the `async_exc` field of the current thread state.
151 Guarded by the GIL. */
152static int pending_async_exc = 0;
153
Guido van Rossume59214e1994-08-30 08:01:59 +0000154#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000155
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000156#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000157#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000158#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000159#include "pythread.h"
Eric Snow05351c12017-09-05 21:43:08 -0700160
161static PyThread_type_lock pending_lock = 0; /* for pending calls */
162static unsigned long main_thread = 0;
163/* Request for dropping the GIL */
164static _Py_atomic_int gil_drop_request = {0};
165
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000166#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000167
Tim Peters7f468f22004-10-11 02:40:51 +0000168int
169PyEval_ThreadsInitialized(void)
170{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000172}
173
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000174void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000175PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 if (gil_created())
178 return;
179 create_gil();
180 take_gil(PyThreadState_GET());
Eric Snow05351c12017-09-05 21:43:08 -0700181 main_thread = PyThread_get_thread_ident();
182 if (!pending_lock)
183 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000184}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000185
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000186void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000187_PyEval_FiniThreads(void)
188{
189 if (!gil_created())
190 return;
191 destroy_gil();
192 assert(!gil_created());
193}
194
195void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000196PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 PyThreadState *tstate = PyThreadState_GET();
199 if (tstate == NULL)
200 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
201 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000202}
203
204void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000205PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000206{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 /* This function must succeed when the current thread state is NULL.
208 We therefore avoid PyThreadState_GET() which dumps a fatal error
209 in debug mode.
210 */
211 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
212 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000213}
214
215void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000216PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000217{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 if (tstate == NULL)
219 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
220 /* Check someone has called PyEval_InitThreads() to create the lock */
221 assert(gil_created());
222 take_gil(tstate);
223 if (PyThreadState_Swap(tstate) != NULL)
224 Py_FatalError(
225 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000226}
227
228void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000229PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000230{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 if (tstate == NULL)
232 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
233 if (PyThreadState_Swap(NULL) != tstate)
234 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
235 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000236}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000237
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200238/* This function is called from PyOS_AfterFork_Child to destroy all threads
239 * which are not running in the child process, and clear internal locks
240 * which might be held by those threads.
241 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000242
243void
244PyEval_ReInitThreads(void)
245{
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200246 PyThreadState *current_tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 if (!gil_created())
249 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 recreate_gil();
Eric Snow05351c12017-09-05 21:43:08 -0700251 pending_lock = PyThread_allocate_lock();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200252 take_gil(current_tstate);
Eric Snow05351c12017-09-05 21:43:08 -0700253 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000254
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200255 /* Destroy all threads except the current one */
256 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000257}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000258
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000259#endif /* WITH_THREAD */
260
261/* This function is used to signal that async exceptions are waiting to be
262 raised, therefore it is also useful in non-threaded builds. */
263
264void
265_PyEval_SignalAsyncExc(void)
266{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000268}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000269
Guido van Rossumff4949e1992-08-05 19:58:53 +0000270/* Functions save_thread and restore_thread are always defined so
271 dynamically loaded modules needn't be compiled separately for use
272 with and without threads: */
273
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000274PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000275PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 PyThreadState *tstate = PyThreadState_Swap(NULL);
278 if (tstate == NULL)
279 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000280#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 if (gil_created())
282 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000283#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000285}
286
287void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000288PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000289{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 if (tstate == NULL)
291 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000292#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 if (gil_created()) {
294 int err = errno;
295 take_gil(tstate);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200296 /* _Py_Finalizing is protected by the GIL */
Eric Snow05351c12017-09-05 21:43:08 -0700297 if (_Py_Finalizing && tstate != _Py_Finalizing) {
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200298 drop_gil(tstate);
299 PyThread_exit_thread();
300 assert(0); /* unreachable */
301 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 errno = err;
303 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000304#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000306}
307
308
Guido van Rossuma9672091994-09-14 13:31:22 +0000309/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
310 signal handlers or Mac I/O completion routines) can schedule calls
311 to a function to be called synchronously.
312 The synchronous function is called with one void* argument.
313 It should return 0 for success or -1 for failure -- failure should
314 be accompanied by an exception.
315
316 If registry succeeds, the registry function returns 0; if it fails
317 (e.g. due to too many pending calls) it returns -1 (without setting
318 an exception condition).
319
320 Note that because registry may occur from within signal handlers,
321 or other asynchronous events, calling malloc() is unsafe!
322
323#ifdef WITH_THREAD
324 Any thread can schedule pending calls, but only the main thread
325 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000326 There is no facility to schedule calls to a particular thread, but
327 that should be easy to change, should that ever be required. In
328 that case, the static variables here should go into the python
329 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000330#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000331*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000332
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200333void
334_PyEval_SignalReceived(void)
335{
336 /* bpo-30703: Function called when the C signal handler of Python gets a
337 signal. We cannot queue a callback using Py_AddPendingCall() since
338 that function is not async-signal-safe. */
339 SIGNAL_PENDING_CALLS();
340}
341
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000342#ifdef WITH_THREAD
343
344/* The WITH_THREAD implementation is thread-safe. It allows
345 scheduling to be made from any thread, and even from an executing
346 callback.
347 */
348
Eric Snow05351c12017-09-05 21:43:08 -0700349#define NPENDINGCALLS 32
350static struct {
351 int (*func)(void *);
352 void *arg;
353} pendingcalls[NPENDINGCALLS];
354static int pendingfirst = 0;
355static int pendinglast = 0;
356
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000357int
358Py_AddPendingCall(int (*func)(void *), void *arg)
359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 int i, j, result=0;
Eric Snow05351c12017-09-05 21:43:08 -0700361 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 /* try a few times for the lock. Since this mechanism is used
364 * for signal handling (on the main thread), there is a (slim)
365 * chance that a signal is delivered on the same thread while we
366 * hold the lock during the Py_MakePendingCalls() function.
367 * This avoids a deadlock in that case.
368 * Note that signals can be delivered on any thread. In particular,
369 * on Windows, a SIGINT is delivered on a system-created worker
370 * thread.
371 * We also check for lock being NULL, in the unlikely case that
372 * this function is called before any bytecode evaluation takes place.
373 */
374 if (lock != NULL) {
375 for (i = 0; i<100; i++) {
376 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
377 break;
378 }
379 if (i == 100)
380 return -1;
381 }
382
Eric Snow05351c12017-09-05 21:43:08 -0700383 i = pendinglast;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 j = (i + 1) % NPENDINGCALLS;
Eric Snow05351c12017-09-05 21:43:08 -0700385 if (j == pendingfirst) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 result = -1; /* Queue full */
387 } else {
Eric Snow05351c12017-09-05 21:43:08 -0700388 pendingcalls[i].func = func;
389 pendingcalls[i].arg = arg;
390 pendinglast = j;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 }
392 /* signal main loop */
393 SIGNAL_PENDING_CALLS();
394 if (lock != NULL)
395 PyThread_release_lock(lock);
396 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000397}
398
399int
400Py_MakePendingCalls(void)
401{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200402 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 int i;
404 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000405
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200406 assert(PyGILState_Check());
407
Eric Snow05351c12017-09-05 21:43:08 -0700408 if (!pending_lock) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 /* initial allocation of the lock */
Eric Snow05351c12017-09-05 21:43:08 -0700410 pending_lock = PyThread_allocate_lock();
411 if (pending_lock == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 return -1;
413 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 /* only service pending calls on main thread */
Eric Snow05351c12017-09-05 21:43:08 -0700416 if (main_thread && PyThread_get_thread_ident() != main_thread)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 return 0;
418 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200419 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200421 busy = 1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200422 /* unsignal before starting to call callbacks, so that any callback
423 added in-between re-signals */
424 UNSIGNAL_PENDING_CALLS();
425
426 /* Python signal handler doesn't really queue a callback: it only signals
427 that a signal was received, see _PyEval_SignalReceived(). */
428 if (PyErr_CheckSignals() < 0) {
429 goto error;
430 }
431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 /* perform a bounded number of calls, in case of recursion */
433 for (i=0; i<NPENDINGCALLS; i++) {
434 int j;
435 int (*func)(void *);
436 void *arg = NULL;
437
438 /* pop one item off the queue while holding the lock */
Eric Snow05351c12017-09-05 21:43:08 -0700439 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
440 j = pendingfirst;
441 if (j == pendinglast) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 func = NULL; /* Queue empty */
443 } else {
Eric Snow05351c12017-09-05 21:43:08 -0700444 func = pendingcalls[j].func;
445 arg = pendingcalls[j].arg;
446 pendingfirst = (j + 1) % NPENDINGCALLS;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 }
Eric Snow05351c12017-09-05 21:43:08 -0700448 PyThread_release_lock(pending_lock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 /* having released the lock, perform the callback */
450 if (func == NULL)
451 break;
452 r = func(arg);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200453 if (r) {
454 goto error;
455 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200457
Charles-François Natalif23339a2011-07-23 18:15:43 +0200458 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 return r;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200460
461error:
462 busy = 0;
463 SIGNAL_PENDING_CALLS(); /* We're not done yet */
464 return -1;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000465}
466
467#else /* if ! defined WITH_THREAD */
468
469/*
470 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
471 This code is used for signal handling in python that isn't built
472 with WITH_THREAD.
473 Don't use this implementation when Py_AddPendingCalls() can happen
474 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475
Guido van Rossuma9672091994-09-14 13:31:22 +0000476 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000477 (1) nested asynchronous calls to Py_AddPendingCall()
478 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000480 (1) is very unlikely because typically signal delivery
481 is blocked during signal handling. So it should be impossible.
482 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000483 The current code is safe against (2), but not against (1).
484 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000485 thread is present, interrupted by signals, and that the critical
486 section is protected with the "busy" variable. On Windows, which
487 delivers SIGINT on a system thread, this does not hold and therefore
488 Windows really shouldn't use this version.
489 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000490*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000491
Eric Snow05351c12017-09-05 21:43:08 -0700492#define NPENDINGCALLS 32
493static struct {
494 int (*func)(void *);
495 void *arg;
496} pendingcalls[NPENDINGCALLS];
497static volatile int pendingfirst = 0;
498static volatile int pendinglast = 0;
499
Guido van Rossuma9672091994-09-14 13:31:22 +0000500int
Thomas Wouters334fb892000-07-25 12:56:38 +0000501Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000502{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 static volatile int busy = 0;
504 int i, j;
505 /* XXX Begin critical section */
506 if (busy)
507 return -1;
508 busy = 1;
Eric Snow05351c12017-09-05 21:43:08 -0700509 i = pendinglast;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 j = (i + 1) % NPENDINGCALLS;
Eric Snow05351c12017-09-05 21:43:08 -0700511 if (j == pendingfirst) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 busy = 0;
513 return -1; /* Queue full */
514 }
Eric Snow05351c12017-09-05 21:43:08 -0700515 pendingcalls[i].func = func;
516 pendingcalls[i].arg = arg;
517 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 SIGNAL_PENDING_CALLS();
520 busy = 0;
521 /* XXX End critical section */
522 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000523}
524
Guido van Rossum180d7b41994-09-29 09:45:57 +0000525int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000526Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000527{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 static int busy = 0;
529 if (busy)
530 return 0;
531 busy = 1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200532
533 /* unsignal before starting to call callbacks, so that any callback
534 added in-between re-signals */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 UNSIGNAL_PENDING_CALLS();
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200536 /* Python signal handler doesn't really queue a callback: it only signals
537 that a signal was received, see _PyEval_SignalReceived(). */
538 if (PyErr_CheckSignals() < 0) {
539 goto error;
540 }
541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 for (;;) {
543 int i;
544 int (*func)(void *);
545 void *arg;
Eric Snow05351c12017-09-05 21:43:08 -0700546 i = pendingfirst;
547 if (i == pendinglast)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 break; /* Queue empty */
Eric Snow05351c12017-09-05 21:43:08 -0700549 func = pendingcalls[i].func;
550 arg = pendingcalls[i].arg;
551 pendingfirst = (i + 1) % NPENDINGCALLS;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 if (func(arg) < 0) {
Masayuki Yamamoto0c311632017-07-05 17:39:17 +0900553 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 }
555 }
556 busy = 0;
557 return 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200558
559error:
560 busy = 0;
561 SIGNAL_PENDING_CALLS(); /* We're not done yet */
562 return -1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000563}
564
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000565#endif /* WITH_THREAD */
566
Guido van Rossuma9672091994-09-14 13:31:22 +0000567
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000568/* The interpreter's recursion limit */
569
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000570#ifndef Py_DEFAULT_RECURSION_LIMIT
571#define Py_DEFAULT_RECURSION_LIMIT 1000
572#endif
Eric Snow05351c12017-09-05 21:43:08 -0700573static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
574int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000575
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000576int
577Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000578{
Eric Snow05351c12017-09-05 21:43:08 -0700579 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000580}
581
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000582void
583Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000584{
Eric Snow05351c12017-09-05 21:43:08 -0700585 recursion_limit = new_limit;
586 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000587}
588
Armin Rigo2b3eb402003-10-28 12:05:48 +0000589/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
590 if the recursion_depth reaches _Py_CheckRecursionLimit.
591 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
592 to guarantee that _Py_CheckRecursiveCall() is regularly called.
593 Without USE_STACKCHECK, there is no need for this. */
594int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300595_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000596{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000598
599#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 if (PyOS_CheckStack()) {
601 --tstate->recursion_depth;
602 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
603 return -1;
604 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000605#endif
Eric Snow05351c12017-09-05 21:43:08 -0700606 _Py_CheckRecursionLimit = recursion_limit;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 if (tstate->recursion_critical)
608 /* Somebody asked that we don't check for recursion. */
609 return 0;
610 if (tstate->overflowed) {
611 if (tstate->recursion_depth > recursion_limit + 50) {
612 /* Overflowing while handling an overflow. Give up. */
613 Py_FatalError("Cannot recover from stack overflow.");
614 }
615 return 0;
616 }
617 if (tstate->recursion_depth > recursion_limit) {
618 --tstate->recursion_depth;
619 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400620 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 "maximum recursion depth exceeded%s",
622 where);
623 return -1;
624 }
625 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000626}
627
Guido van Rossum374a9221991-04-04 10:40:29 +0000628/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000629enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000630 WHY_NOT = 0x0001, /* No error */
631 WHY_EXCEPTION = 0x0002, /* Exception occurred */
Stefan Krahb7e10102010-06-23 18:42:39 +0000632 WHY_RETURN = 0x0008, /* 'return' statement */
633 WHY_BREAK = 0x0010, /* 'break' statement */
634 WHY_CONTINUE = 0x0020, /* 'continue' statement */
635 WHY_YIELD = 0x0040, /* 'yield' operator */
636 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000637};
Guido van Rossum374a9221991-04-04 10:40:29 +0000638
Benjamin Peterson87880242011-07-03 16:48:31 -0500639static void save_exc_state(PyThreadState *, PyFrameObject *);
640static void swap_exc_state(PyThreadState *, PyFrameObject *);
641static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400642static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000643static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000644
Eric Snow05351c12017-09-05 21:43:08 -0700645/* Records whether tracing is on for any thread. Counts the number of
646 threads for which tstate->c_tracefunc is non-NULL, so if the value
647 is 0, we know we don't have to check this thread's c_tracefunc.
648 This speeds up the if statement in PyEval_EvalFrameEx() after
649 fast_next_opcode*/
650static int _Py_TracingPossible = 0;
651
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000652
Guido van Rossum374a9221991-04-04 10:40:29 +0000653
Guido van Rossumb209a111997-04-29 18:18:01 +0000654PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000655PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000656{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 return PyEval_EvalCodeEx(co,
658 globals, locals,
659 (PyObject **)NULL, 0,
660 (PyObject **)NULL, 0,
661 (PyObject **)NULL, 0,
662 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000663}
664
665
666/* Interpreter main loop */
667
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000668PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000669PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 /* This is for backward compatibility with extension modules that
671 used this API; core interpreter code should call
672 PyEval_EvalFrameEx() */
673 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000674}
675
676PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000677PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000678{
Brett Cannon3cebf932016-09-05 15:33:46 -0700679 PyThreadState *tstate = PyThreadState_GET();
680 return tstate->interp->eval_frame(f, throwflag);
681}
682
Victor Stinnerc6944e72016-11-11 02:13:35 +0100683PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700684_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
685{
Guido van Rossum950361c1997-01-24 13:49:28 +0000686#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000688#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200689 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300690 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200691 int opcode; /* Current opcode */
692 int oparg; /* Current opcode argument, if any */
693 enum why_code why; /* Reason for block stack unwind */
694 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 PyObject *retval = NULL; /* Return value */
696 PyThreadState *tstate = PyThreadState_GET();
697 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 is true when the line being executed has changed. The
704 initial values are such as to make this false the first
705 time it is tested. */
706 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000707
Serhiy Storchakaab874002016-09-11 13:48:15 +0300708 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 PyObject *names;
710 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000711
Brett Cannon368b4b72012-04-02 12:17:59 -0400712#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200713 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400714#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200715
Antoine Pitroub52ec782009-01-25 16:34:23 +0000716/* Computed GOTOs, or
717 the-optimization-commonly-but-improperly-known-as-"threaded code"
718 using gcc's labels-as-values extension
719 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
720
721 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000723 combined with a lookup table of jump addresses. However, since the
724 indirect jump instruction is shared by all opcodes, the CPU will have a
725 hard time making the right prediction for where to jump next (actually,
726 it will be always wrong except in the uncommon case of a sequence of
727 several identical opcodes).
728
729 "Threaded code" in contrast, uses an explicit jump table and an explicit
730 indirect jump instruction at the end of each opcode. Since the jump
731 instruction is at a different address for each opcode, the CPU will make a
732 separate prediction for each of these instructions, which is equivalent to
733 predicting the second opcode of each opcode pair. These predictions have
734 a much better chance to turn out valid, especially in small bytecode loops.
735
736 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000738 and potentially many more instructions (depending on the pipeline width).
739 A correctly predicted branch, however, is nearly free.
740
741 At the time of this writing, the "threaded code" version is up to 15-20%
742 faster than the normal "switch" version, depending on the compiler and the
743 CPU architecture.
744
745 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
746 because it would render the measurements invalid.
747
748
749 NOTE: care must be taken that the compiler doesn't try to "optimize" the
750 indirect jumps by sharing them between all opcodes. Such optimizations
751 can be disabled on gcc by using the -fno-gcse flag (or possibly
752 -fno-crossjumping).
753*/
754
Antoine Pitrou042b1282010-08-13 21:15:58 +0000755#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000756#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000757#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000758#endif
759
Antoine Pitrou042b1282010-08-13 21:15:58 +0000760#ifdef HAVE_COMPUTED_GOTOS
761 #ifndef USE_COMPUTED_GOTOS
762 #define USE_COMPUTED_GOTOS 1
763 #endif
764#else
765 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
766 #error "Computed gotos are not supported on this compiler."
767 #endif
768 #undef USE_COMPUTED_GOTOS
769 #define USE_COMPUTED_GOTOS 0
770#endif
771
772#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000773/* Import the static jump table */
774#include "opcode_targets.h"
775
Antoine Pitroub52ec782009-01-25 16:34:23 +0000776#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 TARGET_##op: \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000779
Antoine Pitroub52ec782009-01-25 16:34:23 +0000780#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 { \
Eric Snow05351c12017-09-05 21:43:08 -0700782 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 FAST_DISPATCH(); \
784 } \
785 continue; \
786 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000787
788#ifdef LLTRACE
789#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700791 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300793 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300794 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 } \
796 goto fast_next_opcode; \
797 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000798#else
799#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700801 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300803 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300804 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 } \
806 goto fast_next_opcode; \
807 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000808#endif
809
810#else
811#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 case op:
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300813
Antoine Pitroub52ec782009-01-25 16:34:23 +0000814#define DISPATCH() continue
815#define FAST_DISPATCH() goto fast_next_opcode
816#endif
817
818
Neal Norwitza81d2202002-07-14 00:27:26 +0000819/* Tuple access macros */
820
821#ifndef Py_DEBUG
822#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
823#else
824#define GETITEM(v, i) PyTuple_GetItem((v), (i))
825#endif
826
Guido van Rossum374a9221991-04-04 10:40:29 +0000827/* Code access macros */
828
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300829/* The integer overflow is checked by an assertion below. */
Eric Snow05351c12017-09-05 21:43:08 -0700830#define INSTR_OFFSET() (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300831#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300832 _Py_CODEUNIT word = *next_instr; \
833 opcode = _Py_OPCODE(word); \
834 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300835 next_instr++; \
836 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300837#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
838#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000839
Raymond Hettingerf606f872003-03-16 03:11:04 +0000840/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 Some opcodes tend to come in pairs thus making it possible to
842 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300843 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000844
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 Verifying the prediction costs a single high-speed test of a register
846 variable against a constant. If the pairing was good, then the
847 processor's own internal branch predication has a high likelihood of
848 success, resulting in a nearly zero-overhead transition to the
849 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300850 including its unpredictable switch-case branch. Combined with the
851 processor's internal branch prediction, a successful PREDICT has the
852 effect of making the two opcodes run as if they were a single new opcode
853 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000854
Georg Brandl86b2fb92008-07-16 03:43:04 +0000855 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 predictions turned-on and interpret the results as if some opcodes
857 had been combined or turn-off predictions so that the opcode frequency
858 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000859
860 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 the CPU to record separate branch prediction information for each
862 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000863
Raymond Hettingerf606f872003-03-16 03:11:04 +0000864*/
865
Antoine Pitrou042b1282010-08-13 21:15:58 +0000866#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000868#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300869#define PREDICT(op) \
870 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300871 _Py_CODEUNIT word = *next_instr; \
872 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300873 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300874 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300875 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300876 goto PRED_##op; \
877 } \
878 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000879#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300880#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000881
Raymond Hettingerf606f872003-03-16 03:11:04 +0000882
Guido van Rossum374a9221991-04-04 10:40:29 +0000883/* Stack manipulation macros */
884
Martin v. Löwis18e16552006-02-15 17:27:45 +0000885/* The stack can grow at most MAXINT deep, as co_nlocals and
886 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000887#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
888#define EMPTY() (STACK_LEVEL() == 0)
889#define TOP() (stack_pointer[-1])
890#define SECOND() (stack_pointer[-2])
891#define THIRD() (stack_pointer[-3])
892#define FOURTH() (stack_pointer[-4])
893#define PEEK(n) (stack_pointer[-(n)])
894#define SET_TOP(v) (stack_pointer[-1] = (v))
895#define SET_SECOND(v) (stack_pointer[-2] = (v))
896#define SET_THIRD(v) (stack_pointer[-3] = (v))
897#define SET_FOURTH(v) (stack_pointer[-4] = (v))
898#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
899#define BASIC_STACKADJ(n) (stack_pointer += n)
900#define BASIC_PUSH(v) (*stack_pointer++ = (v))
901#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000902
Guido van Rossum96a42c81992-01-12 02:29:51 +0000903#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000905 lltrace && prtrace(TOP(), "push")); \
906 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000908 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000910 lltrace && prtrace(TOP(), "stackadj")); \
911 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +0000912#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000913 prtrace((STACK_POINTER)[-1], "ext_pop")), \
914 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000915#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000916#define PUSH(v) BASIC_PUSH(v)
917#define POP() BASIC_POP()
918#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000919#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000920#endif
921
Guido van Rossum681d79a1995-07-18 14:51:37 +0000922/* Local variable macros */
923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000925
926/* The SETLOCAL() macro must not DECREF the local variable in-place and
927 then store the new value; it must copy the old value to a temporary
928 value, then store the new value, and then DECREF the temporary value.
929 This is because it is possible that during the DECREF the frame is
930 accessed by other code (e.g. a __del__ method or gc.collect()) and the
931 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000933 GETLOCAL(i) = value; \
934 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000935
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000936
937#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 while (STACK_LEVEL() > (b)->b_level) { \
939 PyObject *v = POP(); \
940 Py_XDECREF(v); \
941 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000942
943#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300944 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 PyObject *type, *value, *traceback; \
946 assert(STACK_LEVEL() >= (b)->b_level + 3); \
947 while (STACK_LEVEL() > (b)->b_level + 3) { \
948 value = POP(); \
949 Py_XDECREF(value); \
950 } \
951 type = tstate->exc_type; \
952 value = tstate->exc_value; \
953 traceback = tstate->exc_traceback; \
954 tstate->exc_type = POP(); \
955 tstate->exc_value = POP(); \
956 tstate->exc_traceback = POP(); \
957 Py_XDECREF(type); \
958 Py_XDECREF(value); \
959 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300960 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000961
Guido van Rossuma027efa1997-05-05 20:56:21 +0000962/* Start of code */
963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 /* push frame */
965 if (Py_EnterRecursiveCall(""))
966 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 if (tstate->use_tracing) {
971 if (tstate->c_tracefunc != NULL) {
972 /* tstate->c_tracefunc, if defined, is a
973 function that will be called on *every* entry
974 to a code block. Its return value, if not
975 None, is a function that will be called at
976 the start of each executed line of code.
977 (Actually, the function must return itself
978 in order to continue tracing.) The trace
979 functions are called with three arguments:
980 a pointer to the current frame, a string
981 indicating why the function is called, and
982 an argument which depends on the situation.
983 The global trace function is also called
984 whenever an exception is detected. */
985 if (call_trace_protected(tstate->c_tracefunc,
986 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100987 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 /* Trace function raised an error */
989 goto exit_eval_frame;
990 }
991 }
992 if (tstate->c_profilefunc != NULL) {
993 /* Similar for c_profilefunc, except it needn't
994 return itself and isn't called for "line" events */
995 if (call_trace_protected(tstate->c_profilefunc,
996 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100997 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 /* Profile function raised an error */
999 goto exit_eval_frame;
1000 }
1001 }
1002 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001003
Łukasz Langaa785c872016-09-09 17:37:37 -07001004 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1005 dtrace_function_entry(f);
1006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 co = f->f_code;
1008 names = co->co_names;
1009 consts = co->co_consts;
1010 fastlocals = f->f_localsplus;
1011 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001012 assert(PyBytes_Check(co->co_code));
1013 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001014 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1015 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1016 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001017 /*
1018 f->f_lasti refers to the index of the last instruction,
1019 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001020
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001021 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001022 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 When the PREDICT() macros are enabled, some opcode pairs follow in
1025 direct succession without updating f->f_lasti. A successful
1026 prediction effectively links the two codes together as if they
1027 were a single new opcode; accordingly,f->f_lasti will point to
1028 the first code in the pair (for instance, GET_ITER followed by
1029 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001030 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001032 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001033 next_instr = first_instr;
1034 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001035 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1036 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001037 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 stack_pointer = f->f_stacktop;
1039 assert(stack_pointer != NULL);
1040 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001041 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001042
Yury Selivanoveb636452016-09-08 22:01:51 -07001043 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01001044 if (!throwflag && f->f_exc_type != NULL && f->f_exc_type != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 /* We were in an except handler when we left,
1046 restore the exception state which was put aside
1047 (see YIELD_VALUE). */
Benjamin Peterson87880242011-07-03 16:48:31 -05001048 swap_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 }
Benjamin Peterson87880242011-07-03 16:48:31 -05001050 else
1051 save_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001053
Tim Peters5ca576e2001-06-18 22:08:13 +00001054#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001055 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001056#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 why = WHY_NOT;
Guido van Rossumac7be682001-01-17 15:42:30 +00001059
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001060 if (throwflag) /* support for generator.throw() */
1061 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001062
Victor Stinnerace47d72013-07-18 01:41:08 +02001063#ifdef Py_DEBUG
1064 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001065 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001066 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +02001067 assert(!PyErr_Occurred());
1068#endif
1069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1072 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +02001073 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 /* Do periodic things. Doing this every time through
1076 the loop would add too much overhead, so we do it
1077 only every Nth instruction. We also do it if
1078 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1079 event needs attention (e.g. a signal handler or
1080 async I/O handler); see Py_AddPendingCall() and
1081 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001082
Eric Snow05351c12017-09-05 21:43:08 -07001083 if (_Py_atomic_load_relaxed(&eval_breaker)) {
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001084 if (_Py_OPCODE(*next_instr) == SETUP_FINALLY ||
1085 _Py_OPCODE(*next_instr) == YIELD_FROM) {
1086 /* Two cases where we skip running signal handlers and other
1087 pending calls:
1088 - If we're about to enter the try: of a try/finally (not
1089 *very* useful, but might help in some cases and it's
1090 traditional)
1091 - If we're resuming a chain of nested 'yield from' or
1092 'await' calls, then each frame is parked with YIELD_FROM
1093 as its next opcode. If the user hit control-C we want to
1094 wait until we've reached the innermost frame before
1095 running the signal handler and raising KeyboardInterrupt
1096 (see bpo-30039).
1097 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 goto fast_next_opcode;
1099 }
Eric Snow05351c12017-09-05 21:43:08 -07001100 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001101 if (Py_MakePendingCalls() < 0)
1102 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001104#ifdef WITH_THREAD
Eric Snow05351c12017-09-05 21:43:08 -07001105 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 /* Give another thread a chance */
1107 if (PyThreadState_Swap(NULL) != tstate)
1108 Py_FatalError("ceval: tstate mix-up");
1109 drop_gil(tstate);
1110
1111 /* Other threads may run now */
1112
1113 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001114
1115 /* Check if we should make a quick exit. */
Eric Snow05351c12017-09-05 21:43:08 -07001116 if (_Py_Finalizing && _Py_Finalizing != tstate) {
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001117 drop_gil(tstate);
1118 PyThread_exit_thread();
1119 }
1120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 if (PyThreadState_Swap(tstate) != NULL)
1122 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001124#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 /* Check for asynchronous exceptions. */
1126 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001127 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 tstate->async_exc = NULL;
1129 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001130 PyErr_SetNone(exc);
1131 Py_DECREF(exc);
1132 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 }
1134 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 fast_next_opcode:
1137 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001138
Łukasz Langaa785c872016-09-09 17:37:37 -07001139 if (PyDTrace_LINE_ENABLED())
1140 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001143
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001145 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001146 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 /* see maybe_call_line_trace
1148 for expository comments */
1149 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 err = maybe_call_line_trace(tstate->c_tracefunc,
1152 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001153 tstate, f,
1154 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 /* Reload possibly changed frame fields */
1156 JUMPTO(f->f_lasti);
1157 if (f->f_stacktop != NULL) {
1158 stack_pointer = f->f_stacktop;
1159 f->f_stacktop = NULL;
1160 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001161 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001163 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001167
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001168 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001169 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001170#ifdef DYNAMIC_EXECUTION_PROFILE
1171#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 dxpairs[lastopcode][opcode]++;
1173 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001174#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001176#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001177
Guido van Rossum96a42c81992-01-12 02:29:51 +00001178#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 if (lltrace) {
1182 if (HAS_ARG(opcode)) {
1183 printf("%d: %d, %d\n",
1184 f->f_lasti, opcode, oparg);
1185 }
1186 else {
1187 printf("%d: %d\n",
1188 f->f_lasti, opcode);
1189 }
1190 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001191#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 /* BEWARE!
1196 It is essential that any operation that fails sets either
1197 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1198 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 TARGET(NOP)
1201 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001202
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001203 TARGET(LOAD_FAST) {
1204 PyObject *value = GETLOCAL(oparg);
1205 if (value == NULL) {
1206 format_exc_check_arg(PyExc_UnboundLocalError,
1207 UNBOUNDLOCAL_ERROR_MSG,
1208 PyTuple_GetItem(co->co_varnames, oparg));
1209 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001211 Py_INCREF(value);
1212 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001214 }
1215
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001216 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001217 TARGET(LOAD_CONST) {
1218 PyObject *value = GETITEM(consts, oparg);
1219 Py_INCREF(value);
1220 PUSH(value);
1221 FAST_DISPATCH();
1222 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001223
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001224 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001225 TARGET(STORE_FAST) {
1226 PyObject *value = POP();
1227 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001229 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001230
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001231 TARGET(POP_TOP) {
1232 PyObject *value = POP();
1233 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001235 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001236
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001237 TARGET(ROT_TWO) {
1238 PyObject *top = TOP();
1239 PyObject *second = SECOND();
1240 SET_TOP(second);
1241 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001243 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001244
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001245 TARGET(ROT_THREE) {
1246 PyObject *top = TOP();
1247 PyObject *second = SECOND();
1248 PyObject *third = THIRD();
1249 SET_TOP(second);
1250 SET_SECOND(third);
1251 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001253 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001254
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001255 TARGET(DUP_TOP) {
1256 PyObject *top = TOP();
1257 Py_INCREF(top);
1258 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001260 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001261
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001262 TARGET(DUP_TOP_TWO) {
1263 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001264 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001265 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001266 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001267 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001268 SET_TOP(top);
1269 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001270 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001271 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001272
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001273 TARGET(UNARY_POSITIVE) {
1274 PyObject *value = TOP();
1275 PyObject *res = PyNumber_Positive(value);
1276 Py_DECREF(value);
1277 SET_TOP(res);
1278 if (res == NULL)
1279 goto error;
1280 DISPATCH();
1281 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001282
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001283 TARGET(UNARY_NEGATIVE) {
1284 PyObject *value = TOP();
1285 PyObject *res = PyNumber_Negative(value);
1286 Py_DECREF(value);
1287 SET_TOP(res);
1288 if (res == NULL)
1289 goto error;
1290 DISPATCH();
1291 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001292
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001293 TARGET(UNARY_NOT) {
1294 PyObject *value = TOP();
1295 int err = PyObject_IsTrue(value);
1296 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 if (err == 0) {
1298 Py_INCREF(Py_True);
1299 SET_TOP(Py_True);
1300 DISPATCH();
1301 }
1302 else if (err > 0) {
1303 Py_INCREF(Py_False);
1304 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 DISPATCH();
1306 }
1307 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001308 goto error;
1309 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001310
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001311 TARGET(UNARY_INVERT) {
1312 PyObject *value = TOP();
1313 PyObject *res = PyNumber_Invert(value);
1314 Py_DECREF(value);
1315 SET_TOP(res);
1316 if (res == NULL)
1317 goto error;
1318 DISPATCH();
1319 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001320
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001321 TARGET(BINARY_POWER) {
1322 PyObject *exp = POP();
1323 PyObject *base = TOP();
1324 PyObject *res = PyNumber_Power(base, exp, Py_None);
1325 Py_DECREF(base);
1326 Py_DECREF(exp);
1327 SET_TOP(res);
1328 if (res == NULL)
1329 goto error;
1330 DISPATCH();
1331 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001332
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001333 TARGET(BINARY_MULTIPLY) {
1334 PyObject *right = POP();
1335 PyObject *left = TOP();
1336 PyObject *res = PyNumber_Multiply(left, right);
1337 Py_DECREF(left);
1338 Py_DECREF(right);
1339 SET_TOP(res);
1340 if (res == NULL)
1341 goto error;
1342 DISPATCH();
1343 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001344
Benjamin Petersond51374e2014-04-09 23:55:56 -04001345 TARGET(BINARY_MATRIX_MULTIPLY) {
1346 PyObject *right = POP();
1347 PyObject *left = TOP();
1348 PyObject *res = PyNumber_MatrixMultiply(left, right);
1349 Py_DECREF(left);
1350 Py_DECREF(right);
1351 SET_TOP(res);
1352 if (res == NULL)
1353 goto error;
1354 DISPATCH();
1355 }
1356
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001357 TARGET(BINARY_TRUE_DIVIDE) {
1358 PyObject *divisor = POP();
1359 PyObject *dividend = TOP();
1360 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1361 Py_DECREF(dividend);
1362 Py_DECREF(divisor);
1363 SET_TOP(quotient);
1364 if (quotient == NULL)
1365 goto error;
1366 DISPATCH();
1367 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001368
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001369 TARGET(BINARY_FLOOR_DIVIDE) {
1370 PyObject *divisor = POP();
1371 PyObject *dividend = TOP();
1372 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1373 Py_DECREF(dividend);
1374 Py_DECREF(divisor);
1375 SET_TOP(quotient);
1376 if (quotient == NULL)
1377 goto error;
1378 DISPATCH();
1379 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001380
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001381 TARGET(BINARY_MODULO) {
1382 PyObject *divisor = POP();
1383 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001384 PyObject *res;
1385 if (PyUnicode_CheckExact(dividend) && (
1386 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1387 // fast path; string formatting, but not if the RHS is a str subclass
1388 // (see issue28598)
1389 res = PyUnicode_Format(dividend, divisor);
1390 } else {
1391 res = PyNumber_Remainder(dividend, divisor);
1392 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001393 Py_DECREF(divisor);
1394 Py_DECREF(dividend);
1395 SET_TOP(res);
1396 if (res == NULL)
1397 goto error;
1398 DISPATCH();
1399 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001400
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001401 TARGET(BINARY_ADD) {
1402 PyObject *right = POP();
1403 PyObject *left = TOP();
1404 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001405 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1406 CPython using bytecode, it is simply worthless.
1407 See http://bugs.python.org/issue21955 and
1408 http://bugs.python.org/issue10044 for the discussion. In short,
1409 no patch shown any impact on a realistic benchmark, only a minor
1410 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001411 if (PyUnicode_CheckExact(left) &&
1412 PyUnicode_CheckExact(right)) {
1413 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001414 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001415 }
1416 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001417 sum = PyNumber_Add(left, right);
1418 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001419 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001420 Py_DECREF(right);
1421 SET_TOP(sum);
1422 if (sum == NULL)
1423 goto error;
1424 DISPATCH();
1425 }
1426
1427 TARGET(BINARY_SUBTRACT) {
1428 PyObject *right = POP();
1429 PyObject *left = TOP();
1430 PyObject *diff = PyNumber_Subtract(left, right);
1431 Py_DECREF(right);
1432 Py_DECREF(left);
1433 SET_TOP(diff);
1434 if (diff == NULL)
1435 goto error;
1436 DISPATCH();
1437 }
1438
1439 TARGET(BINARY_SUBSCR) {
1440 PyObject *sub = POP();
1441 PyObject *container = TOP();
1442 PyObject *res = PyObject_GetItem(container, sub);
1443 Py_DECREF(container);
1444 Py_DECREF(sub);
1445 SET_TOP(res);
1446 if (res == NULL)
1447 goto error;
1448 DISPATCH();
1449 }
1450
1451 TARGET(BINARY_LSHIFT) {
1452 PyObject *right = POP();
1453 PyObject *left = TOP();
1454 PyObject *res = PyNumber_Lshift(left, right);
1455 Py_DECREF(left);
1456 Py_DECREF(right);
1457 SET_TOP(res);
1458 if (res == NULL)
1459 goto error;
1460 DISPATCH();
1461 }
1462
1463 TARGET(BINARY_RSHIFT) {
1464 PyObject *right = POP();
1465 PyObject *left = TOP();
1466 PyObject *res = PyNumber_Rshift(left, right);
1467 Py_DECREF(left);
1468 Py_DECREF(right);
1469 SET_TOP(res);
1470 if (res == NULL)
1471 goto error;
1472 DISPATCH();
1473 }
1474
1475 TARGET(BINARY_AND) {
1476 PyObject *right = POP();
1477 PyObject *left = TOP();
1478 PyObject *res = PyNumber_And(left, right);
1479 Py_DECREF(left);
1480 Py_DECREF(right);
1481 SET_TOP(res);
1482 if (res == NULL)
1483 goto error;
1484 DISPATCH();
1485 }
1486
1487 TARGET(BINARY_XOR) {
1488 PyObject *right = POP();
1489 PyObject *left = TOP();
1490 PyObject *res = PyNumber_Xor(left, right);
1491 Py_DECREF(left);
1492 Py_DECREF(right);
1493 SET_TOP(res);
1494 if (res == NULL)
1495 goto error;
1496 DISPATCH();
1497 }
1498
1499 TARGET(BINARY_OR) {
1500 PyObject *right = POP();
1501 PyObject *left = TOP();
1502 PyObject *res = PyNumber_Or(left, right);
1503 Py_DECREF(left);
1504 Py_DECREF(right);
1505 SET_TOP(res);
1506 if (res == NULL)
1507 goto error;
1508 DISPATCH();
1509 }
1510
1511 TARGET(LIST_APPEND) {
1512 PyObject *v = POP();
1513 PyObject *list = PEEK(oparg);
1514 int err;
1515 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001517 if (err != 0)
1518 goto error;
1519 PREDICT(JUMP_ABSOLUTE);
1520 DISPATCH();
1521 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001522
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001523 TARGET(SET_ADD) {
1524 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001525 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001526 int err;
1527 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001529 if (err != 0)
1530 goto error;
1531 PREDICT(JUMP_ABSOLUTE);
1532 DISPATCH();
1533 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001534
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001535 TARGET(INPLACE_POWER) {
1536 PyObject *exp = POP();
1537 PyObject *base = TOP();
1538 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1539 Py_DECREF(base);
1540 Py_DECREF(exp);
1541 SET_TOP(res);
1542 if (res == NULL)
1543 goto error;
1544 DISPATCH();
1545 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001546
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001547 TARGET(INPLACE_MULTIPLY) {
1548 PyObject *right = POP();
1549 PyObject *left = TOP();
1550 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1551 Py_DECREF(left);
1552 Py_DECREF(right);
1553 SET_TOP(res);
1554 if (res == NULL)
1555 goto error;
1556 DISPATCH();
1557 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001558
Benjamin Petersond51374e2014-04-09 23:55:56 -04001559 TARGET(INPLACE_MATRIX_MULTIPLY) {
1560 PyObject *right = POP();
1561 PyObject *left = TOP();
1562 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1563 Py_DECREF(left);
1564 Py_DECREF(right);
1565 SET_TOP(res);
1566 if (res == NULL)
1567 goto error;
1568 DISPATCH();
1569 }
1570
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001571 TARGET(INPLACE_TRUE_DIVIDE) {
1572 PyObject *divisor = POP();
1573 PyObject *dividend = TOP();
1574 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1575 Py_DECREF(dividend);
1576 Py_DECREF(divisor);
1577 SET_TOP(quotient);
1578 if (quotient == NULL)
1579 goto error;
1580 DISPATCH();
1581 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001582
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001583 TARGET(INPLACE_FLOOR_DIVIDE) {
1584 PyObject *divisor = POP();
1585 PyObject *dividend = TOP();
1586 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1587 Py_DECREF(dividend);
1588 Py_DECREF(divisor);
1589 SET_TOP(quotient);
1590 if (quotient == NULL)
1591 goto error;
1592 DISPATCH();
1593 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001594
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001595 TARGET(INPLACE_MODULO) {
1596 PyObject *right = POP();
1597 PyObject *left = TOP();
1598 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1599 Py_DECREF(left);
1600 Py_DECREF(right);
1601 SET_TOP(mod);
1602 if (mod == NULL)
1603 goto error;
1604 DISPATCH();
1605 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001606
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001607 TARGET(INPLACE_ADD) {
1608 PyObject *right = POP();
1609 PyObject *left = TOP();
1610 PyObject *sum;
1611 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1612 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001613 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001614 }
1615 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001616 sum = PyNumber_InPlaceAdd(left, right);
1617 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001618 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001619 Py_DECREF(right);
1620 SET_TOP(sum);
1621 if (sum == NULL)
1622 goto error;
1623 DISPATCH();
1624 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001625
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001626 TARGET(INPLACE_SUBTRACT) {
1627 PyObject *right = POP();
1628 PyObject *left = TOP();
1629 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1630 Py_DECREF(left);
1631 Py_DECREF(right);
1632 SET_TOP(diff);
1633 if (diff == NULL)
1634 goto error;
1635 DISPATCH();
1636 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001637
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001638 TARGET(INPLACE_LSHIFT) {
1639 PyObject *right = POP();
1640 PyObject *left = TOP();
1641 PyObject *res = PyNumber_InPlaceLshift(left, right);
1642 Py_DECREF(left);
1643 Py_DECREF(right);
1644 SET_TOP(res);
1645 if (res == NULL)
1646 goto error;
1647 DISPATCH();
1648 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001649
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001650 TARGET(INPLACE_RSHIFT) {
1651 PyObject *right = POP();
1652 PyObject *left = TOP();
1653 PyObject *res = PyNumber_InPlaceRshift(left, right);
1654 Py_DECREF(left);
1655 Py_DECREF(right);
1656 SET_TOP(res);
1657 if (res == NULL)
1658 goto error;
1659 DISPATCH();
1660 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001661
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001662 TARGET(INPLACE_AND) {
1663 PyObject *right = POP();
1664 PyObject *left = TOP();
1665 PyObject *res = PyNumber_InPlaceAnd(left, right);
1666 Py_DECREF(left);
1667 Py_DECREF(right);
1668 SET_TOP(res);
1669 if (res == NULL)
1670 goto error;
1671 DISPATCH();
1672 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001673
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001674 TARGET(INPLACE_XOR) {
1675 PyObject *right = POP();
1676 PyObject *left = TOP();
1677 PyObject *res = PyNumber_InPlaceXor(left, right);
1678 Py_DECREF(left);
1679 Py_DECREF(right);
1680 SET_TOP(res);
1681 if (res == NULL)
1682 goto error;
1683 DISPATCH();
1684 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001685
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001686 TARGET(INPLACE_OR) {
1687 PyObject *right = POP();
1688 PyObject *left = TOP();
1689 PyObject *res = PyNumber_InPlaceOr(left, right);
1690 Py_DECREF(left);
1691 Py_DECREF(right);
1692 SET_TOP(res);
1693 if (res == NULL)
1694 goto error;
1695 DISPATCH();
1696 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001697
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001698 TARGET(STORE_SUBSCR) {
1699 PyObject *sub = TOP();
1700 PyObject *container = SECOND();
1701 PyObject *v = THIRD();
1702 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 STACKADJ(-3);
Martin Panter95f53c12016-07-18 08:23:26 +00001704 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001705 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001707 Py_DECREF(container);
1708 Py_DECREF(sub);
1709 if (err != 0)
1710 goto error;
1711 DISPATCH();
1712 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001713
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001714 TARGET(STORE_ANNOTATION) {
1715 _Py_IDENTIFIER(__annotations__);
1716 PyObject *ann_dict;
1717 PyObject *ann = POP();
1718 PyObject *name = GETITEM(names, oparg);
1719 int err;
1720 if (f->f_locals == NULL) {
1721 PyErr_Format(PyExc_SystemError,
1722 "no locals found when storing annotation");
1723 Py_DECREF(ann);
1724 goto error;
1725 }
1726 /* first try to get __annotations__ from locals... */
1727 if (PyDict_CheckExact(f->f_locals)) {
1728 ann_dict = _PyDict_GetItemId(f->f_locals,
1729 &PyId___annotations__);
1730 if (ann_dict == NULL) {
1731 PyErr_SetString(PyExc_NameError,
1732 "__annotations__ not found");
1733 Py_DECREF(ann);
1734 goto error;
1735 }
1736 Py_INCREF(ann_dict);
1737 }
1738 else {
1739 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
1740 if (ann_str == NULL) {
1741 Py_DECREF(ann);
1742 goto error;
1743 }
1744 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
1745 if (ann_dict == NULL) {
1746 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
1747 PyErr_SetString(PyExc_NameError,
1748 "__annotations__ not found");
1749 }
1750 Py_DECREF(ann);
1751 goto error;
1752 }
1753 }
1754 /* ...if succeeded, __annotations__[name] = ann */
1755 if (PyDict_CheckExact(ann_dict)) {
1756 err = PyDict_SetItem(ann_dict, name, ann);
1757 }
1758 else {
1759 err = PyObject_SetItem(ann_dict, name, ann);
1760 }
1761 Py_DECREF(ann_dict);
Yury Selivanov50c584f2016-09-08 23:38:21 -07001762 Py_DECREF(ann);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001763 if (err != 0) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001764 goto error;
1765 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001766 DISPATCH();
1767 }
1768
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001769 TARGET(DELETE_SUBSCR) {
1770 PyObject *sub = TOP();
1771 PyObject *container = SECOND();
1772 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 STACKADJ(-2);
Martin Panter95f53c12016-07-18 08:23:26 +00001774 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001775 err = PyObject_DelItem(container, sub);
1776 Py_DECREF(container);
1777 Py_DECREF(sub);
1778 if (err != 0)
1779 goto error;
1780 DISPATCH();
1781 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001782
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001783 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001784 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001785 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001786 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001787 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001788 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 PyErr_SetString(PyExc_RuntimeError,
1790 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001791 Py_DECREF(value);
1792 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001793 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001794 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001795 Py_DECREF(value);
1796 if (res == NULL)
1797 goto error;
1798 Py_DECREF(res);
1799 DISPATCH();
1800 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001801
Thomas Wouters434d0822000-08-24 20:11:32 +00001802#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001803 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001804#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001805 TARGET(RAISE_VARARGS) {
1806 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 switch (oparg) {
1808 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001809 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001810 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001812 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001813 /* fall through */
1814 case 0:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001815 if (do_raise(exc, cause)) {
1816 why = WHY_EXCEPTION;
1817 goto fast_block_end;
1818 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 break;
1820 default:
1821 PyErr_SetString(PyExc_SystemError,
1822 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001823 break;
1824 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001825 goto error;
1826 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001827
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001828 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829 retval = POP();
1830 why = WHY_RETURN;
1831 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001832 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001833
Yury Selivanov75445082015-05-11 22:57:16 -04001834 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001835 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001836 PyObject *iter = NULL;
1837 PyObject *awaitable = NULL;
1838 PyObject *obj = TOP();
1839 PyTypeObject *type = Py_TYPE(obj);
1840
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001841 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001842 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001843 }
Yury Selivanov75445082015-05-11 22:57:16 -04001844
1845 if (getter != NULL) {
1846 iter = (*getter)(obj);
1847 Py_DECREF(obj);
1848 if (iter == NULL) {
1849 SET_TOP(NULL);
1850 goto error;
1851 }
1852 }
1853 else {
1854 SET_TOP(NULL);
1855 PyErr_Format(
1856 PyExc_TypeError,
1857 "'async for' requires an object with "
1858 "__aiter__ method, got %.100s",
1859 type->tp_name);
1860 Py_DECREF(obj);
1861 goto error;
1862 }
1863
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001864 if (Py_TYPE(iter)->tp_as_async != NULL &&
1865 Py_TYPE(iter)->tp_as_async->am_anext != NULL) {
1866
1867 /* Starting with CPython 3.5.2 __aiter__ should return
1868 asynchronous iterators directly (not awaitables that
1869 resolve to asynchronous iterators.)
1870
1871 Therefore, we check if the object that was returned
1872 from __aiter__ has an __anext__ method. If it does,
1873 we wrap it in an awaitable that resolves to `iter`.
1874
1875 See http://bugs.python.org/issue27243 for more
1876 details.
1877 */
1878
1879 PyObject *wrapper = _PyAIterWrapper_New(iter);
1880 Py_DECREF(iter);
1881 SET_TOP(wrapper);
1882 DISPATCH();
1883 }
1884
Yury Selivanov5376ba92015-06-22 12:19:30 -04001885 awaitable = _PyCoro_GetAwaitableIter(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001886 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001887 _PyErr_FormatFromCause(
Yury Selivanov75445082015-05-11 22:57:16 -04001888 PyExc_TypeError,
1889 "'async for' received an invalid object "
1890 "from __aiter__: %.100s",
1891 Py_TYPE(iter)->tp_name);
1892
Yury Selivanov398ff912017-03-02 22:20:00 -05001893 SET_TOP(NULL);
Yury Selivanov75445082015-05-11 22:57:16 -04001894 Py_DECREF(iter);
1895 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001896 } else {
Yury Selivanov75445082015-05-11 22:57:16 -04001897 Py_DECREF(iter);
1898
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001899 if (PyErr_WarnFormat(
Yury Selivanov2edd8a12016-11-08 15:13:07 -05001900 PyExc_DeprecationWarning, 1,
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001901 "'%.100s' implements legacy __aiter__ protocol; "
1902 "__aiter__ should return an asynchronous "
1903 "iterator, not awaitable",
1904 type->tp_name))
1905 {
1906 /* Warning was converted to an error. */
1907 Py_DECREF(awaitable);
1908 SET_TOP(NULL);
1909 goto error;
1910 }
1911 }
1912
Yury Selivanov75445082015-05-11 22:57:16 -04001913 SET_TOP(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001914 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001915 DISPATCH();
1916 }
1917
1918 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001919 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001920 PyObject *next_iter = NULL;
1921 PyObject *awaitable = NULL;
1922 PyObject *aiter = TOP();
1923 PyTypeObject *type = Py_TYPE(aiter);
1924
Yury Selivanoveb636452016-09-08 22:01:51 -07001925 if (PyAsyncGen_CheckExact(aiter)) {
1926 awaitable = type->tp_as_async->am_anext(aiter);
1927 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001928 goto error;
1929 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001930 } else {
1931 if (type->tp_as_async != NULL){
1932 getter = type->tp_as_async->am_anext;
1933 }
Yury Selivanov75445082015-05-11 22:57:16 -04001934
Yury Selivanoveb636452016-09-08 22:01:51 -07001935 if (getter != NULL) {
1936 next_iter = (*getter)(aiter);
1937 if (next_iter == NULL) {
1938 goto error;
1939 }
1940 }
1941 else {
1942 PyErr_Format(
1943 PyExc_TypeError,
1944 "'async for' requires an iterator with "
1945 "__anext__ method, got %.100s",
1946 type->tp_name);
1947 goto error;
1948 }
Yury Selivanov75445082015-05-11 22:57:16 -04001949
Yury Selivanoveb636452016-09-08 22:01:51 -07001950 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1951 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001952 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001953 PyExc_TypeError,
1954 "'async for' received an invalid object "
1955 "from __anext__: %.100s",
1956 Py_TYPE(next_iter)->tp_name);
1957
1958 Py_DECREF(next_iter);
1959 goto error;
1960 } else {
1961 Py_DECREF(next_iter);
1962 }
1963 }
Yury Selivanov75445082015-05-11 22:57:16 -04001964
1965 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001966 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001967 DISPATCH();
1968 }
1969
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001970 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001971 TARGET(GET_AWAITABLE) {
1972 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001973 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001974
1975 Py_DECREF(iterable);
1976
Yury Selivanovc724bae2016-03-02 11:30:46 -05001977 if (iter != NULL && PyCoro_CheckExact(iter)) {
1978 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1979 if (yf != NULL) {
1980 /* `iter` is a coroutine object that is being
1981 awaited, `yf` is a pointer to the current awaitable
1982 being awaited on. */
1983 Py_DECREF(yf);
1984 Py_CLEAR(iter);
1985 PyErr_SetString(
1986 PyExc_RuntimeError,
1987 "coroutine is being awaited already");
1988 /* The code below jumps to `error` if `iter` is NULL. */
1989 }
1990 }
1991
Yury Selivanov75445082015-05-11 22:57:16 -04001992 SET_TOP(iter); /* Even if it's NULL */
1993
1994 if (iter == NULL) {
1995 goto error;
1996 }
1997
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001998 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001999 DISPATCH();
2000 }
2001
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002002 TARGET(YIELD_FROM) {
2003 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002004 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002005 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002006 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2007 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002008 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002009 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002010 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002011 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002012 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002013 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002014 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002015 Py_DECREF(v);
2016 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002017 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002018 if (tstate->c_tracefunc != NULL
2019 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002020 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002021 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002022 if (err < 0)
2023 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002024 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002025 SET_TOP(val);
2026 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002027 }
Martin Panter95f53c12016-07-18 08:23:26 +00002028 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002029 f->f_stacktop = stack_pointer;
2030 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002031 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002032 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002033 f->f_lasti -= sizeof(_Py_CODEUNIT);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002034 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002035 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002036
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002037 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002039
2040 if (co->co_flags & CO_ASYNC_GENERATOR) {
2041 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2042 Py_DECREF(retval);
2043 if (w == NULL) {
2044 retval = NULL;
2045 goto error;
2046 }
2047 retval = w;
2048 }
2049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002050 f->f_stacktop = stack_pointer;
2051 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002052 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002053 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002054
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002055 TARGET(POP_EXCEPT) {
2056 PyTryBlock *b = PyFrame_BlockPop(f);
2057 if (b->b_type != EXCEPT_HANDLER) {
2058 PyErr_SetString(PyExc_SystemError,
2059 "popped block is not an except handler");
2060 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002062 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002063 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002064 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002065
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002066 PREDICTED(POP_BLOCK);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002067 TARGET(POP_BLOCK) {
2068 PyTryBlock *b = PyFrame_BlockPop(f);
2069 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002070 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002071 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002074 TARGET(END_FINALLY) {
2075 PyObject *status = POP();
2076 if (PyLong_Check(status)) {
2077 why = (enum why_code) PyLong_AS_LONG(status);
2078 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002079 if (why == WHY_RETURN ||
2080 why == WHY_CONTINUE)
2081 retval = POP();
2082 if (why == WHY_SILENCED) {
2083 /* An exception was silenced by 'with', we must
2084 manually unwind the EXCEPT_HANDLER block which was
2085 created when the exception was caught, otherwise
2086 the stack will be in an inconsistent state. */
2087 PyTryBlock *b = PyFrame_BlockPop(f);
2088 assert(b->b_type == EXCEPT_HANDLER);
2089 UNWIND_EXCEPT_HANDLER(b);
2090 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002091 Py_DECREF(status);
2092 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002093 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002094 Py_DECREF(status);
2095 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002097 else if (PyExceptionClass_Check(status)) {
2098 PyObject *exc = POP();
2099 PyObject *tb = POP();
2100 PyErr_Restore(status, exc, tb);
2101 why = WHY_EXCEPTION;
2102 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002104 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 PyErr_SetString(PyExc_SystemError,
2106 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002107 Py_DECREF(status);
2108 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002109 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002110 Py_DECREF(status);
2111 DISPATCH();
2112 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002113
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002114 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002115 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002116
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002117 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002118 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002119 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
2120 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002121 PyErr_SetString(PyExc_NameError,
2122 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002123 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002124 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002125 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002126 }
2127 else {
2128 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2129 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002130 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002131 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2132 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002133 if (PyErr_ExceptionMatches(PyExc_KeyError))
2134 PyErr_SetString(PyExc_NameError,
2135 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002136 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002137 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002139 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002140 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002141 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002142
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002143 TARGET(STORE_NAME) {
2144 PyObject *name = GETITEM(names, oparg);
2145 PyObject *v = POP();
2146 PyObject *ns = f->f_locals;
2147 int err;
2148 if (ns == NULL) {
2149 PyErr_Format(PyExc_SystemError,
2150 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002151 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002152 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002153 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002154 if (PyDict_CheckExact(ns))
2155 err = PyDict_SetItem(ns, name, v);
2156 else
2157 err = PyObject_SetItem(ns, name, v);
2158 Py_DECREF(v);
2159 if (err != 0)
2160 goto error;
2161 DISPATCH();
2162 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002163
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002164 TARGET(DELETE_NAME) {
2165 PyObject *name = GETITEM(names, oparg);
2166 PyObject *ns = f->f_locals;
2167 int err;
2168 if (ns == NULL) {
2169 PyErr_Format(PyExc_SystemError,
2170 "no locals when deleting %R", name);
2171 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002172 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002173 err = PyObject_DelItem(ns, name);
2174 if (err != 0) {
2175 format_exc_check_arg(PyExc_NameError,
2176 NAME_ERROR_MSG,
2177 name);
2178 goto error;
2179 }
2180 DISPATCH();
2181 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002182
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002183 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002184 TARGET(UNPACK_SEQUENCE) {
2185 PyObject *seq = POP(), *item, **items;
2186 if (PyTuple_CheckExact(seq) &&
2187 PyTuple_GET_SIZE(seq) == oparg) {
2188 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002189 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002190 item = items[oparg];
2191 Py_INCREF(item);
2192 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002193 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002194 } else if (PyList_CheckExact(seq) &&
2195 PyList_GET_SIZE(seq) == oparg) {
2196 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002198 item = items[oparg];
2199 Py_INCREF(item);
2200 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002201 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002202 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002203 stack_pointer + oparg)) {
2204 STACKADJ(oparg);
2205 } else {
2206 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002207 Py_DECREF(seq);
2208 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002209 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002210 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002211 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002212 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002213
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002214 TARGET(UNPACK_EX) {
2215 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2216 PyObject *seq = POP();
2217
2218 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2219 stack_pointer + totalargs)) {
2220 stack_pointer += totalargs;
2221 } else {
2222 Py_DECREF(seq);
2223 goto error;
2224 }
2225 Py_DECREF(seq);
2226 DISPATCH();
2227 }
2228
2229 TARGET(STORE_ATTR) {
2230 PyObject *name = GETITEM(names, oparg);
2231 PyObject *owner = TOP();
2232 PyObject *v = SECOND();
2233 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002234 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002235 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002237 Py_DECREF(owner);
2238 if (err != 0)
2239 goto error;
2240 DISPATCH();
2241 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002242
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002243 TARGET(DELETE_ATTR) {
2244 PyObject *name = GETITEM(names, oparg);
2245 PyObject *owner = POP();
2246 int err;
2247 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2248 Py_DECREF(owner);
2249 if (err != 0)
2250 goto error;
2251 DISPATCH();
2252 }
2253
2254 TARGET(STORE_GLOBAL) {
2255 PyObject *name = GETITEM(names, oparg);
2256 PyObject *v = POP();
2257 int err;
2258 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002259 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002260 if (err != 0)
2261 goto error;
2262 DISPATCH();
2263 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002264
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002265 TARGET(DELETE_GLOBAL) {
2266 PyObject *name = GETITEM(names, oparg);
2267 int err;
2268 err = PyDict_DelItem(f->f_globals, name);
2269 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002271 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002272 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002273 }
2274 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002275 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002276
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002277 TARGET(LOAD_NAME) {
2278 PyObject *name = GETITEM(names, oparg);
2279 PyObject *locals = f->f_locals;
2280 PyObject *v;
2281 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002282 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002283 "no locals when loading %R", name);
2284 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002285 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002286 if (PyDict_CheckExact(locals)) {
2287 v = PyDict_GetItem(locals, name);
2288 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002289 }
2290 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002291 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002292 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002293 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2294 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002295 PyErr_Clear();
2296 }
2297 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002298 if (v == NULL) {
2299 v = PyDict_GetItem(f->f_globals, name);
2300 Py_XINCREF(v);
2301 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002302 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002303 v = PyDict_GetItem(f->f_builtins, name);
2304 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002305 format_exc_check_arg(
2306 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002307 NAME_ERROR_MSG, name);
2308 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002309 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002310 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002311 }
2312 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002313 v = PyObject_GetItem(f->f_builtins, name);
2314 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002315 if (PyErr_ExceptionMatches(PyExc_KeyError))
2316 format_exc_check_arg(
2317 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002318 NAME_ERROR_MSG, name);
2319 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002320 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002321 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002322 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002323 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002324 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002325 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002326 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002327
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002328 TARGET(LOAD_GLOBAL) {
2329 PyObject *name = GETITEM(names, oparg);
2330 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002331 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002332 && PyDict_CheckExact(f->f_builtins))
2333 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002334 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002335 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002336 name);
2337 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002338 if (!_PyErr_OCCURRED()) {
2339 /* _PyDict_LoadGlobal() returns NULL without raising
2340 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002341 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002342 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002343 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002344 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002345 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002346 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002347 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002348 else {
2349 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002350
2351 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002352 v = PyObject_GetItem(f->f_globals, name);
2353 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002354 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2355 goto error;
2356 PyErr_Clear();
2357
Victor Stinnerb4efc962015-11-20 09:24:02 +01002358 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002359 v = PyObject_GetItem(f->f_builtins, name);
2360 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002361 if (PyErr_ExceptionMatches(PyExc_KeyError))
2362 format_exc_check_arg(
2363 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002364 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002365 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002366 }
2367 }
2368 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002369 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002370 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002371 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002372
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002373 TARGET(DELETE_FAST) {
2374 PyObject *v = GETLOCAL(oparg);
2375 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 SETLOCAL(oparg, NULL);
2377 DISPATCH();
2378 }
2379 format_exc_check_arg(
2380 PyExc_UnboundLocalError,
2381 UNBOUNDLOCAL_ERROR_MSG,
2382 PyTuple_GetItem(co->co_varnames, oparg)
2383 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002384 goto error;
2385 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002386
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002387 TARGET(DELETE_DEREF) {
2388 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002389 PyObject *oldobj = PyCell_GET(cell);
2390 if (oldobj != NULL) {
2391 PyCell_SET(cell, NULL);
2392 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002393 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002394 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002395 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002396 goto error;
2397 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002398
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002399 TARGET(LOAD_CLOSURE) {
2400 PyObject *cell = freevars[oparg];
2401 Py_INCREF(cell);
2402 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002404 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002405
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002406 TARGET(LOAD_CLASSDEREF) {
2407 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002408 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002409 assert(locals);
2410 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2411 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2412 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2413 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2414 if (PyDict_CheckExact(locals)) {
2415 value = PyDict_GetItem(locals, name);
2416 Py_XINCREF(value);
2417 }
2418 else {
2419 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002420 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002421 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2422 goto error;
2423 PyErr_Clear();
2424 }
2425 }
2426 if (!value) {
2427 PyObject *cell = freevars[oparg];
2428 value = PyCell_GET(cell);
2429 if (value == NULL) {
2430 format_exc_unbound(co, oparg);
2431 goto error;
2432 }
2433 Py_INCREF(value);
2434 }
2435 PUSH(value);
2436 DISPATCH();
2437 }
2438
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002439 TARGET(LOAD_DEREF) {
2440 PyObject *cell = freevars[oparg];
2441 PyObject *value = PyCell_GET(cell);
2442 if (value == NULL) {
2443 format_exc_unbound(co, oparg);
2444 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002446 Py_INCREF(value);
2447 PUSH(value);
2448 DISPATCH();
2449 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002450
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002451 TARGET(STORE_DEREF) {
2452 PyObject *v = POP();
2453 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002454 PyObject *oldobj = PyCell_GET(cell);
2455 PyCell_SET(cell, v);
2456 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002457 DISPATCH();
2458 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002459
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002460 TARGET(BUILD_STRING) {
2461 PyObject *str;
2462 PyObject *empty = PyUnicode_New(0, 0);
2463 if (empty == NULL) {
2464 goto error;
2465 }
2466 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2467 Py_DECREF(empty);
2468 if (str == NULL)
2469 goto error;
2470 while (--oparg >= 0) {
2471 PyObject *item = POP();
2472 Py_DECREF(item);
2473 }
2474 PUSH(str);
2475 DISPATCH();
2476 }
2477
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002478 TARGET(BUILD_TUPLE) {
2479 PyObject *tup = PyTuple_New(oparg);
2480 if (tup == NULL)
2481 goto error;
2482 while (--oparg >= 0) {
2483 PyObject *item = POP();
2484 PyTuple_SET_ITEM(tup, oparg, item);
2485 }
2486 PUSH(tup);
2487 DISPATCH();
2488 }
2489
2490 TARGET(BUILD_LIST) {
2491 PyObject *list = PyList_New(oparg);
2492 if (list == NULL)
2493 goto error;
2494 while (--oparg >= 0) {
2495 PyObject *item = POP();
2496 PyList_SET_ITEM(list, oparg, item);
2497 }
2498 PUSH(list);
2499 DISPATCH();
2500 }
2501
Serhiy Storchaka73442852016-10-02 10:33:46 +03002502 TARGET(BUILD_TUPLE_UNPACK_WITH_CALL)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002503 TARGET(BUILD_TUPLE_UNPACK)
2504 TARGET(BUILD_LIST_UNPACK) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002505 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002506 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002507 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002508 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002509
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002510 if (sum == NULL)
2511 goto error;
2512
2513 for (i = oparg; i > 0; i--) {
2514 PyObject *none_val;
2515
2516 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2517 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002518 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002519 PyErr_ExceptionMatches(PyExc_TypeError))
2520 {
2521 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002522 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002523 Py_DECREF(sum);
2524 goto error;
2525 }
2526 Py_DECREF(none_val);
2527 }
2528
2529 if (convert_to_tuple) {
2530 return_value = PyList_AsTuple(sum);
2531 Py_DECREF(sum);
2532 if (return_value == NULL)
2533 goto error;
2534 }
2535 else {
2536 return_value = sum;
2537 }
2538
2539 while (oparg--)
2540 Py_DECREF(POP());
2541 PUSH(return_value);
2542 DISPATCH();
2543 }
2544
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002545 TARGET(BUILD_SET) {
2546 PyObject *set = PySet_New(NULL);
2547 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002548 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002549 if (set == NULL)
2550 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002551 for (i = oparg; i > 0; i--) {
2552 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002553 if (err == 0)
2554 err = PySet_Add(set, item);
2555 Py_DECREF(item);
2556 }
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002557 STACKADJ(-oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002558 if (err != 0) {
2559 Py_DECREF(set);
2560 goto error;
2561 }
2562 PUSH(set);
2563 DISPATCH();
2564 }
2565
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002566 TARGET(BUILD_SET_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002567 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002568 PyObject *sum = PySet_New(NULL);
2569 if (sum == NULL)
2570 goto error;
2571
2572 for (i = oparg; i > 0; i--) {
2573 if (_PySet_Update(sum, PEEK(i)) < 0) {
2574 Py_DECREF(sum);
2575 goto error;
2576 }
2577 }
2578
2579 while (oparg--)
2580 Py_DECREF(POP());
2581 PUSH(sum);
2582 DISPATCH();
2583 }
2584
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002585 TARGET(BUILD_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002586 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002587 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2588 if (map == NULL)
2589 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002590 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002591 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002592 PyObject *key = PEEK(2*i);
2593 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002594 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002595 if (err != 0) {
2596 Py_DECREF(map);
2597 goto error;
2598 }
2599 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002600
2601 while (oparg--) {
2602 Py_DECREF(POP());
2603 Py_DECREF(POP());
2604 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002605 PUSH(map);
2606 DISPATCH();
2607 }
2608
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002609 TARGET(SETUP_ANNOTATIONS) {
2610 _Py_IDENTIFIER(__annotations__);
2611 int err;
2612 PyObject *ann_dict;
2613 if (f->f_locals == NULL) {
2614 PyErr_Format(PyExc_SystemError,
2615 "no locals found when setting up annotations");
2616 goto error;
2617 }
2618 /* check if __annotations__ in locals()... */
2619 if (PyDict_CheckExact(f->f_locals)) {
2620 ann_dict = _PyDict_GetItemId(f->f_locals,
2621 &PyId___annotations__);
2622 if (ann_dict == NULL) {
2623 /* ...if not, create a new one */
2624 ann_dict = PyDict_New();
2625 if (ann_dict == NULL) {
2626 goto error;
2627 }
2628 err = _PyDict_SetItemId(f->f_locals,
2629 &PyId___annotations__, ann_dict);
2630 Py_DECREF(ann_dict);
2631 if (err != 0) {
2632 goto error;
2633 }
2634 }
2635 }
2636 else {
2637 /* do the same if locals() is not a dict */
2638 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2639 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002640 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002641 }
2642 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2643 if (ann_dict == NULL) {
2644 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2645 goto error;
2646 }
2647 PyErr_Clear();
2648 ann_dict = PyDict_New();
2649 if (ann_dict == NULL) {
2650 goto error;
2651 }
2652 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2653 Py_DECREF(ann_dict);
2654 if (err != 0) {
2655 goto error;
2656 }
2657 }
2658 else {
2659 Py_DECREF(ann_dict);
2660 }
2661 }
2662 DISPATCH();
2663 }
2664
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002665 TARGET(BUILD_CONST_KEY_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002666 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002667 PyObject *map;
2668 PyObject *keys = TOP();
2669 if (!PyTuple_CheckExact(keys) ||
2670 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2671 PyErr_SetString(PyExc_SystemError,
2672 "bad BUILD_CONST_KEY_MAP keys argument");
2673 goto error;
2674 }
2675 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2676 if (map == NULL) {
2677 goto error;
2678 }
2679 for (i = oparg; i > 0; i--) {
2680 int err;
2681 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2682 PyObject *value = PEEK(i + 1);
2683 err = PyDict_SetItem(map, key, value);
2684 if (err != 0) {
2685 Py_DECREF(map);
2686 goto error;
2687 }
2688 }
2689
2690 Py_DECREF(POP());
2691 while (oparg--) {
2692 Py_DECREF(POP());
2693 }
2694 PUSH(map);
2695 DISPATCH();
2696 }
2697
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002698 TARGET(BUILD_MAP_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002699 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002700 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002701 if (sum == NULL)
2702 goto error;
2703
2704 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002705 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002706 if (PyDict_Update(sum, arg) < 0) {
2707 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2708 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002709 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002710 arg->ob_type->tp_name);
2711 }
2712 Py_DECREF(sum);
2713 goto error;
2714 }
2715 }
2716
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002717 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002718 Py_DECREF(POP());
2719 PUSH(sum);
2720 DISPATCH();
2721 }
2722
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002723 TARGET(BUILD_MAP_UNPACK_WITH_CALL) {
2724 Py_ssize_t i;
2725 PyObject *sum = PyDict_New();
2726 if (sum == NULL)
2727 goto error;
2728
2729 for (i = oparg; i > 0; i--) {
2730 PyObject *arg = PEEK(i);
2731 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
2732 PyObject *func = PEEK(2 + oparg);
2733 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002734 format_kwargs_mapping_error(func, arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002735 }
2736 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2737 PyObject *exc, *val, *tb;
2738 PyErr_Fetch(&exc, &val, &tb);
2739 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
2740 PyObject *key = PyTuple_GET_ITEM(val, 0);
2741 if (!PyUnicode_Check(key)) {
2742 PyErr_Format(PyExc_TypeError,
2743 "%.200s%.200s keywords must be strings",
2744 PyEval_GetFuncName(func),
2745 PyEval_GetFuncDesc(func));
2746 } else {
2747 PyErr_Format(PyExc_TypeError,
2748 "%.200s%.200s got multiple "
2749 "values for keyword argument '%U'",
2750 PyEval_GetFuncName(func),
2751 PyEval_GetFuncDesc(func),
2752 key);
2753 }
2754 Py_XDECREF(exc);
2755 Py_XDECREF(val);
2756 Py_XDECREF(tb);
2757 }
2758 else {
2759 PyErr_Restore(exc, val, tb);
2760 }
2761 }
2762 Py_DECREF(sum);
2763 goto error;
2764 }
2765 }
2766
2767 while (oparg--)
2768 Py_DECREF(POP());
2769 PUSH(sum);
2770 DISPATCH();
2771 }
2772
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002773 TARGET(MAP_ADD) {
2774 PyObject *key = TOP();
2775 PyObject *value = SECOND();
2776 PyObject *map;
2777 int err;
2778 STACKADJ(-2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002779 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002780 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002781 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002782 Py_DECREF(value);
2783 Py_DECREF(key);
2784 if (err != 0)
2785 goto error;
2786 PREDICT(JUMP_ABSOLUTE);
2787 DISPATCH();
2788 }
2789
2790 TARGET(LOAD_ATTR) {
2791 PyObject *name = GETITEM(names, oparg);
2792 PyObject *owner = TOP();
2793 PyObject *res = PyObject_GetAttr(owner, name);
2794 Py_DECREF(owner);
2795 SET_TOP(res);
2796 if (res == NULL)
2797 goto error;
2798 DISPATCH();
2799 }
2800
2801 TARGET(COMPARE_OP) {
2802 PyObject *right = POP();
2803 PyObject *left = TOP();
2804 PyObject *res = cmp_outcome(oparg, left, right);
2805 Py_DECREF(left);
2806 Py_DECREF(right);
2807 SET_TOP(res);
2808 if (res == NULL)
2809 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002810 PREDICT(POP_JUMP_IF_FALSE);
2811 PREDICT(POP_JUMP_IF_TRUE);
2812 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002813 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002814
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002815 TARGET(IMPORT_NAME) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002816 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002817 PyObject *fromlist = POP();
2818 PyObject *level = TOP();
2819 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002820 res = import_name(f, name, fromlist, level);
2821 Py_DECREF(level);
2822 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002823 SET_TOP(res);
2824 if (res == NULL)
2825 goto error;
2826 DISPATCH();
2827 }
2828
2829 TARGET(IMPORT_STAR) {
2830 PyObject *from = POP(), *locals;
2831 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002832 if (PyFrame_FastToLocalsWithError(f) < 0) {
2833 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002834 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002835 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002836
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002837 locals = f->f_locals;
2838 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002839 PyErr_SetString(PyExc_SystemError,
2840 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002841 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002842 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002843 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002844 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002845 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002846 Py_DECREF(from);
2847 if (err != 0)
2848 goto error;
2849 DISPATCH();
2850 }
Guido van Rossum25831651993-05-19 14:50:45 +00002851
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002852 TARGET(IMPORT_FROM) {
2853 PyObject *name = GETITEM(names, oparg);
2854 PyObject *from = TOP();
2855 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002856 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002857 PUSH(res);
2858 if (res == NULL)
2859 goto error;
2860 DISPATCH();
2861 }
Thomas Wouters52152252000-08-17 22:55:00 +00002862
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002863 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002864 JUMPBY(oparg);
2865 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002866 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002867
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002868 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002869 TARGET(POP_JUMP_IF_FALSE) {
2870 PyObject *cond = POP();
2871 int err;
2872 if (cond == Py_True) {
2873 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 FAST_DISPATCH();
2875 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002876 if (cond == Py_False) {
2877 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 JUMPTO(oparg);
2879 FAST_DISPATCH();
2880 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002881 err = PyObject_IsTrue(cond);
2882 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002883 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002884 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002885 else if (err == 0)
2886 JUMPTO(oparg);
2887 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002888 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002889 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002890 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002891
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002892 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002893 TARGET(POP_JUMP_IF_TRUE) {
2894 PyObject *cond = POP();
2895 int err;
2896 if (cond == Py_False) {
2897 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002898 FAST_DISPATCH();
2899 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002900 if (cond == Py_True) {
2901 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002902 JUMPTO(oparg);
2903 FAST_DISPATCH();
2904 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002905 err = PyObject_IsTrue(cond);
2906 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002907 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 JUMPTO(oparg);
2909 }
2910 else if (err == 0)
2911 ;
2912 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002913 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002914 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002915 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002916
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002917 TARGET(JUMP_IF_FALSE_OR_POP) {
2918 PyObject *cond = TOP();
2919 int err;
2920 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002921 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002922 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002923 FAST_DISPATCH();
2924 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002925 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002926 JUMPTO(oparg);
2927 FAST_DISPATCH();
2928 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002929 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002930 if (err > 0) {
2931 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002932 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002933 }
2934 else if (err == 0)
2935 JUMPTO(oparg);
2936 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002937 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002938 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002939 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002940
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002941 TARGET(JUMP_IF_TRUE_OR_POP) {
2942 PyObject *cond = TOP();
2943 int err;
2944 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002945 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002946 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002947 FAST_DISPATCH();
2948 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002949 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002950 JUMPTO(oparg);
2951 FAST_DISPATCH();
2952 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002953 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002954 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002955 JUMPTO(oparg);
2956 }
2957 else if (err == 0) {
2958 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002959 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002960 }
2961 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002962 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002963 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002964 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002965
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002966 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002967 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002968 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002969#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002970 /* Enabling this path speeds-up all while and for-loops by bypassing
2971 the per-loop checks for signals. By default, this should be turned-off
2972 because it prevents detection of a control-break in tight loops like
2973 "while 1: pass". Compile with this option turned-on when you need
2974 the speed-up and do not need break checking inside tight loops (ones
2975 that contain only instructions ending with FAST_DISPATCH).
2976 */
2977 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002978#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002979 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002980#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002981 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002982
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002983 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002984 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002985 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002986 PyObject *iter = PyObject_GetIter(iterable);
2987 Py_DECREF(iterable);
2988 SET_TOP(iter);
2989 if (iter == NULL)
2990 goto error;
2991 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002992 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002993 DISPATCH();
2994 }
2995
2996 TARGET(GET_YIELD_FROM_ITER) {
2997 /* before: [obj]; after [getiter(obj)] */
2998 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002999 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003000 if (PyCoro_CheckExact(iterable)) {
3001 /* `iterable` is a coroutine */
3002 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3003 /* and it is used in a 'yield from' expression of a
3004 regular generator. */
3005 Py_DECREF(iterable);
3006 SET_TOP(NULL);
3007 PyErr_SetString(PyExc_TypeError,
3008 "cannot 'yield from' a coroutine object "
3009 "in a non-coroutine generator");
3010 goto error;
3011 }
3012 }
3013 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003014 /* `iterable` is not a generator. */
3015 iter = PyObject_GetIter(iterable);
3016 Py_DECREF(iterable);
3017 SET_TOP(iter);
3018 if (iter == NULL)
3019 goto error;
3020 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003021 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003022 DISPATCH();
3023 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003024
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003025 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003026 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003027 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003028 PyObject *iter = TOP();
3029 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3030 if (next != NULL) {
3031 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003032 PREDICT(STORE_FAST);
3033 PREDICT(UNPACK_SEQUENCE);
3034 DISPATCH();
3035 }
3036 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003037 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
3038 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08003039 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003040 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003041 PyErr_Clear();
3042 }
3043 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003044 STACKADJ(-1);
3045 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003046 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003047 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003048 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003049 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003050
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003051 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003052 why = WHY_BREAK;
3053 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003054 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003055
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003056 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003057 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003058 if (retval == NULL)
3059 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003060 why = WHY_CONTINUE;
3061 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003062 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003063
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003064 TARGET(SETUP_LOOP)
3065 TARGET(SETUP_EXCEPT)
3066 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003067 /* NOTE: If you add any new block-setup opcodes that
3068 are not try/except/finally handlers, you may need
3069 to update the PyGen_NeedsFinalizing() function.
3070 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003071
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003072 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
3073 STACK_LEVEL());
3074 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003075 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003076
Yury Selivanov75445082015-05-11 22:57:16 -04003077 TARGET(BEFORE_ASYNC_WITH) {
3078 _Py_IDENTIFIER(__aexit__);
3079 _Py_IDENTIFIER(__aenter__);
3080
3081 PyObject *mgr = TOP();
3082 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3083 *enter;
3084 PyObject *res;
3085 if (exit == NULL)
3086 goto error;
3087 SET_TOP(exit);
3088 enter = special_lookup(mgr, &PyId___aenter__);
3089 Py_DECREF(mgr);
3090 if (enter == NULL)
3091 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003092 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003093 Py_DECREF(enter);
3094 if (res == NULL)
3095 goto error;
3096 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003097 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003098 DISPATCH();
3099 }
3100
3101 TARGET(SETUP_ASYNC_WITH) {
3102 PyObject *res = POP();
3103 /* Setup the finally block before pushing the result
3104 of __aenter__ on the stack. */
3105 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3106 STACK_LEVEL());
3107 PUSH(res);
3108 DISPATCH();
3109 }
3110
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003111 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003112 _Py_IDENTIFIER(__exit__);
3113 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003114 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08003115 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003116 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08003117 if (enter == NULL)
3118 goto error;
3119 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003120 if (exit == NULL) {
3121 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003122 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003123 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003124 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003125 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003126 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003127 Py_DECREF(enter);
3128 if (res == NULL)
3129 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003130 /* Setup the finally block before pushing the result
3131 of __enter__ on the stack. */
3132 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3133 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003134
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003135 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136 DISPATCH();
3137 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003138
Yury Selivanov75445082015-05-11 22:57:16 -04003139 TARGET(WITH_CLEANUP_START) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04003140 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003141 how/why we entered the finally clause:
3142 - TOP = None
3143 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
3144 - TOP = WHY_*; no retval below it
3145 - (TOP, SECOND, THIRD) = exc_info()
3146 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
3147 Below them is EXIT, the context.__exit__ bound method.
3148 In the last case, we must call
3149 EXIT(TOP, SECOND, THIRD)
3150 otherwise we must call
3151 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003152
Benjamin Peterson8f169482013-10-29 22:25:06 -04003153 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04003155 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003156 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003158 In addition, if the stack represents an exception,
3159 *and* the function call returns a 'true' value, we
3160 push WHY_SILENCED onto the stack. END_FINALLY will
3161 then not re-raise the exception. (But non-local
3162 gotos should still be resumed.)
3163 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003164
Victor Stinner842cfff2016-12-01 14:45:31 +01003165 PyObject* stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003166 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003167 PyObject *exc, *val, *tb, *res;
3168
3169 val = tb = Py_None;
3170 exc = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003171 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003172 (void)POP();
3173 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003174 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003175 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003176 else if (PyLong_Check(exc)) {
3177 STACKADJ(-1);
3178 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003179 case WHY_RETURN:
3180 case WHY_CONTINUE:
3181 /* Retval in TOP. */
3182 exit_func = SECOND();
3183 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003184 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003185 break;
3186 default:
3187 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003188 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003189 break;
3190 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003191 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003192 }
3193 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003194 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003195 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003196 val = SECOND();
3197 tb = THIRD();
3198 tp2 = FOURTH();
3199 exc2 = PEEK(5);
3200 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003201 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003202 SET_VALUE(7, tb2);
3203 SET_VALUE(6, exc2);
3204 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003205 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3206 SET_FOURTH(NULL);
3207 /* We just shifted the stack down, so we have
3208 to tell the except handler block that the
3209 values are lower than it expects. */
3210 block = &f->f_blockstack[f->f_iblock - 1];
3211 assert(block->b_type == EXCEPT_HANDLER);
3212 block->b_level--;
3213 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003214
3215 stack[0] = exc;
3216 stack[1] = val;
3217 stack[2] = tb;
3218 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003219 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003220 if (res == NULL)
3221 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003222
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003223 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003224 PUSH(exc);
3225 PUSH(res);
3226 PREDICT(WITH_CLEANUP_FINISH);
3227 DISPATCH();
3228 }
3229
3230 PREDICTED(WITH_CLEANUP_FINISH);
3231 TARGET(WITH_CLEANUP_FINISH) {
3232 PyObject *res = POP();
3233 PyObject *exc = POP();
3234 int err;
3235
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003236 if (exc != Py_None)
3237 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003238 else
3239 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003240
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003241 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003242 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003244 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003245 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003246 else if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003247 /* There was an exception and a True return */
3248 PUSH(PyLong_FromLong((long) WHY_SILENCED));
3249 }
3250 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003251 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003252 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003253
Yury Selivanovf2392132016-12-13 19:03:51 -05003254 TARGET(LOAD_METHOD) {
3255 /* Designed to work in tamdem with CALL_METHOD. */
3256 PyObject *name = GETITEM(names, oparg);
3257 PyObject *obj = TOP();
3258 PyObject *meth = NULL;
3259
3260 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3261
Yury Selivanovf2392132016-12-13 19:03:51 -05003262 if (meth == NULL) {
3263 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003264 goto error;
3265 }
3266
3267 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003268 /* We can bypass temporary bound method object.
3269 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003270
INADA Naoki015bce62017-01-16 17:23:30 +09003271 meth | self | arg1 | ... | argN
3272 */
3273 SET_TOP(meth);
3274 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003275 }
3276 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003277 /* meth is not an unbound method (but a regular attr, or
3278 something was returned by a descriptor protocol). Set
3279 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003280 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003281
3282 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003283 */
INADA Naoki015bce62017-01-16 17:23:30 +09003284 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003285 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003286 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003287 }
3288 DISPATCH();
3289 }
3290
3291 TARGET(CALL_METHOD) {
3292 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003293 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003294
3295 sp = stack_pointer;
3296
INADA Naoki015bce62017-01-16 17:23:30 +09003297 meth = PEEK(oparg + 2);
3298 if (meth == NULL) {
3299 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3300 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003301
3302 Stack layout:
3303
INADA Naoki015bce62017-01-16 17:23:30 +09003304 ... | NULL | callable | arg1 | ... | argN
3305 ^- TOP()
3306 ^- (-oparg)
3307 ^- (-oparg-1)
3308 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003309
Ville Skyttä49b27342017-08-03 09:00:59 +03003310 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003311 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003312 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003313 res = call_function(&sp, oparg, NULL);
3314 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003315 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003316 }
3317 else {
3318 /* This is a method call. Stack layout:
3319
INADA Naoki015bce62017-01-16 17:23:30 +09003320 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003321 ^- TOP()
3322 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003323 ^- (-oparg-1)
3324 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003325
INADA Naoki015bce62017-01-16 17:23:30 +09003326 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003327 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003328 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003329 */
3330 res = call_function(&sp, oparg + 1, NULL);
3331 stack_pointer = sp;
3332 }
3333
3334 PUSH(res);
3335 if (res == NULL)
3336 goto error;
3337 DISPATCH();
3338 }
3339
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003340 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003341 TARGET(CALL_FUNCTION) {
3342 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003343 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003344 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003345 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003346 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003347 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003348 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003349 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003350 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003351 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003352
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003353 TARGET(CALL_FUNCTION_KW) {
3354 PyObject **sp, *res, *names;
3355
3356 names = POP();
3357 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003358 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003359 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003360 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003361 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003362 Py_DECREF(names);
3363
3364 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003365 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003366 }
3367 DISPATCH();
3368 }
3369
3370 TARGET(CALL_FUNCTION_EX) {
3371 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003372 if (oparg & 0x01) {
3373 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003374 if (!PyDict_CheckExact(kwargs)) {
3375 PyObject *d = PyDict_New();
3376 if (d == NULL)
3377 goto error;
3378 if (PyDict_Update(d, kwargs) != 0) {
3379 Py_DECREF(d);
3380 /* PyDict_Update raises attribute
3381 * error (percolated from an attempt
3382 * to get 'keys' attribute) instead of
3383 * a type error if its second argument
3384 * is not a mapping.
3385 */
3386 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003387 format_kwargs_mapping_error(SECOND(), kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003388 }
Victor Stinnereece2222016-09-12 11:16:37 +02003389 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003390 goto error;
3391 }
3392 Py_DECREF(kwargs);
3393 kwargs = d;
3394 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003395 assert(PyDict_CheckExact(kwargs));
3396 }
3397 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003398 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003399 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003400 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003401 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003402 goto error;
3403 }
3404 Py_SETREF(callargs, PySequence_Tuple(callargs));
3405 if (callargs == NULL) {
3406 goto error;
3407 }
3408 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003409 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003410
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003411 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003412 Py_DECREF(func);
3413 Py_DECREF(callargs);
3414 Py_XDECREF(kwargs);
3415
3416 SET_TOP(result);
3417 if (result == NULL) {
3418 goto error;
3419 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003420 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003421 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003422
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003423 TARGET(MAKE_FUNCTION) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003424 PyObject *qualname = POP();
3425 PyObject *codeobj = POP();
3426 PyFunctionObject *func = (PyFunctionObject *)
3427 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003428
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003429 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003430 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003431 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003432 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003433 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003434
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003435 if (oparg & 0x08) {
3436 assert(PyTuple_CheckExact(TOP()));
3437 func ->func_closure = POP();
3438 }
3439 if (oparg & 0x04) {
3440 assert(PyDict_CheckExact(TOP()));
3441 func->func_annotations = POP();
3442 }
3443 if (oparg & 0x02) {
3444 assert(PyDict_CheckExact(TOP()));
3445 func->func_kwdefaults = POP();
3446 }
3447 if (oparg & 0x01) {
3448 assert(PyTuple_CheckExact(TOP()));
3449 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003450 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003451
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003452 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003453 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003454 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003455
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003456 TARGET(BUILD_SLICE) {
3457 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003458 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003459 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003460 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003461 step = NULL;
3462 stop = POP();
3463 start = TOP();
3464 slice = PySlice_New(start, stop, step);
3465 Py_DECREF(start);
3466 Py_DECREF(stop);
3467 Py_XDECREF(step);
3468 SET_TOP(slice);
3469 if (slice == NULL)
3470 goto error;
3471 DISPATCH();
3472 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003473
Eric V. Smitha78c7952015-11-03 12:45:05 -05003474 TARGET(FORMAT_VALUE) {
3475 /* Handles f-string value formatting. */
3476 PyObject *result;
3477 PyObject *fmt_spec;
3478 PyObject *value;
3479 PyObject *(*conv_fn)(PyObject *);
3480 int which_conversion = oparg & FVC_MASK;
3481 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3482
3483 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003484 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003485
3486 /* See if any conversion is specified. */
3487 switch (which_conversion) {
3488 case FVC_STR: conv_fn = PyObject_Str; break;
3489 case FVC_REPR: conv_fn = PyObject_Repr; break;
3490 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3491
3492 /* Must be 0 (meaning no conversion), since only four
3493 values are allowed by (oparg & FVC_MASK). */
3494 default: conv_fn = NULL; break;
3495 }
3496
3497 /* If there's a conversion function, call it and replace
3498 value with that result. Otherwise, just use value,
3499 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003500 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003501 result = conv_fn(value);
3502 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003503 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003504 Py_XDECREF(fmt_spec);
3505 goto error;
3506 }
3507 value = result;
3508 }
3509
3510 /* If value is a unicode object, and there's no fmt_spec,
3511 then we know the result of format(value) is value
3512 itself. In that case, skip calling format(). I plan to
3513 move this optimization in to PyObject_Format()
3514 itself. */
3515 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3516 /* Do nothing, just transfer ownership to result. */
3517 result = value;
3518 } else {
3519 /* Actually call format(). */
3520 result = PyObject_Format(value, fmt_spec);
3521 Py_DECREF(value);
3522 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003523 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003524 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003525 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003526 }
3527
Eric V. Smith135d5f42016-02-05 18:23:08 -05003528 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003529 DISPATCH();
3530 }
3531
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003532 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003533 int oldoparg = oparg;
3534 NEXTOPARG();
3535 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003536 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003537 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003538
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003539
Antoine Pitrou042b1282010-08-13 21:15:58 +00003540#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003541 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003542#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003543 default:
3544 fprintf(stderr,
3545 "XXX lineno: %d, opcode: %d\n",
3546 PyFrame_GetLineNumber(f),
3547 opcode);
3548 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003549 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003550
3551#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003552 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003553#endif
3554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003555 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003556
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003557 /* This should never be reached. Every opcode should end with DISPATCH()
3558 or goto error. */
3559 assert(0);
Guido van Rossumac7be682001-01-17 15:42:30 +00003560
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003561error:
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003562
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003563 assert(why == WHY_NOT);
3564 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003565
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003566 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003567#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003568 if (!PyErr_Occurred())
3569 PyErr_SetString(PyExc_SystemError,
3570 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003571#else
3572 assert(PyErr_Occurred());
3573#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003574
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003575 /* Log traceback info. */
3576 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003577
Benjamin Peterson51f46162013-01-23 08:38:47 -05003578 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003579 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3580 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003581
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003582fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003583 assert(why != WHY_NOT);
3584
3585 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003586 while (why != WHY_NOT && f->f_iblock > 0) {
3587 /* Peek at the current block. */
3588 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003590 assert(why != WHY_YIELD);
3591 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3592 why = WHY_NOT;
3593 JUMPTO(PyLong_AS_LONG(retval));
3594 Py_DECREF(retval);
3595 break;
3596 }
3597 /* Now we have to pop the block. */
3598 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003600 if (b->b_type == EXCEPT_HANDLER) {
3601 UNWIND_EXCEPT_HANDLER(b);
3602 continue;
3603 }
3604 UNWIND_BLOCK(b);
3605 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3606 why = WHY_NOT;
3607 JUMPTO(b->b_handler);
3608 break;
3609 }
3610 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3611 || b->b_type == SETUP_FINALLY)) {
3612 PyObject *exc, *val, *tb;
3613 int handler = b->b_handler;
3614 /* Beware, this invalidates all b->b_* fields */
3615 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3616 PUSH(tstate->exc_traceback);
3617 PUSH(tstate->exc_value);
3618 if (tstate->exc_type != NULL) {
3619 PUSH(tstate->exc_type);
3620 }
3621 else {
3622 Py_INCREF(Py_None);
3623 PUSH(Py_None);
3624 }
3625 PyErr_Fetch(&exc, &val, &tb);
3626 /* Make the raw exception data
3627 available to the handler,
3628 so a program can emulate the
3629 Python main loop. */
3630 PyErr_NormalizeException(
3631 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003632 if (tb != NULL)
3633 PyException_SetTraceback(val, tb);
3634 else
3635 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003636 Py_INCREF(exc);
3637 tstate->exc_type = exc;
3638 Py_INCREF(val);
3639 tstate->exc_value = val;
3640 tstate->exc_traceback = tb;
3641 if (tb == NULL)
3642 tb = Py_None;
3643 Py_INCREF(tb);
3644 PUSH(tb);
3645 PUSH(val);
3646 PUSH(exc);
3647 why = WHY_NOT;
3648 JUMPTO(handler);
3649 break;
3650 }
3651 if (b->b_type == SETUP_FINALLY) {
3652 if (why & (WHY_RETURN | WHY_CONTINUE))
3653 PUSH(retval);
3654 PUSH(PyLong_FromLong((long)why));
3655 why = WHY_NOT;
3656 JUMPTO(b->b_handler);
3657 break;
3658 }
3659 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003661 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003663 if (why != WHY_NOT)
3664 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00003665
Victor Stinnerace47d72013-07-18 01:41:08 +02003666 assert(!PyErr_Occurred());
3667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003668 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003670 assert(why != WHY_YIELD);
3671 /* Pop remaining stack entries. */
3672 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003673 PyObject *o = POP();
3674 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003675 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003677 if (why != WHY_RETURN)
3678 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003679
Victor Stinner4a7cc882015-03-06 23:35:27 +01003680 assert((retval != NULL) ^ (PyErr_Occurred() != NULL));
Victor Stinnerace47d72013-07-18 01:41:08 +02003681
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003682fast_yield:
Yury Selivanoveb636452016-09-08 22:01:51 -07003683 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01003684
Benjamin Petersonac913412011-07-03 16:25:11 -05003685 /* The purpose of this block is to put aside the generator's exception
3686 state and restore that of the calling frame. If the current
3687 exception state is from the caller, we clear the exception values
3688 on the generator frame, so they are not swapped back in latter. The
3689 origin of the current exception state is determined by checking for
3690 except handler blocks, which we must be in iff a new exception
3691 state came into existence in this frame. (An uncaught exception
3692 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3693 int i;
Victor Stinner74319ae2016-08-25 00:04:09 +02003694 for (i = 0; i < f->f_iblock; i++) {
3695 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER) {
Benjamin Petersonac913412011-07-03 16:25:11 -05003696 break;
Victor Stinner74319ae2016-08-25 00:04:09 +02003697 }
3698 }
Benjamin Petersonac913412011-07-03 16:25:11 -05003699 if (i == f->f_iblock)
3700 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003701 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003702 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003703 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003704 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003706 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003707 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003708 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003709 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3710 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003711 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003712 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003713 why = WHY_EXCEPTION;
3714 }
3715 }
3716 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003717 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3718 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003719 PyTrace_RETURN, NULL);
3720 }
3721 }
3722 if (tstate->c_profilefunc) {
3723 if (why == WHY_EXCEPTION)
3724 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003725 tstate->c_profileobj,
3726 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003727 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003728 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3729 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003730 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003731 Py_CLEAR(retval);
Brett Cannonb94767f2011-02-22 20:15:44 +00003732 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003733 }
3734 }
3735 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003737 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003738exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003739 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3740 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003741 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003742 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003743 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003744
Victor Stinnerefde1462015-03-21 15:04:43 +01003745 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003746}
3747
Benjamin Petersonb204a422011-06-05 22:04:07 -05003748static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003749format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3750{
3751 int err;
3752 Py_ssize_t len = PyList_GET_SIZE(names);
3753 PyObject *name_str, *comma, *tail, *tmp;
3754
3755 assert(PyList_CheckExact(names));
3756 assert(len >= 1);
3757 /* Deal with the joys of natural language. */
3758 switch (len) {
3759 case 1:
3760 name_str = PyList_GET_ITEM(names, 0);
3761 Py_INCREF(name_str);
3762 break;
3763 case 2:
3764 name_str = PyUnicode_FromFormat("%U and %U",
3765 PyList_GET_ITEM(names, len - 2),
3766 PyList_GET_ITEM(names, len - 1));
3767 break;
3768 default:
3769 tail = PyUnicode_FromFormat(", %U, and %U",
3770 PyList_GET_ITEM(names, len - 2),
3771 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003772 if (tail == NULL)
3773 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003774 /* Chop off the last two objects in the list. This shouldn't actually
3775 fail, but we can't be too careful. */
3776 err = PyList_SetSlice(names, len - 2, len, NULL);
3777 if (err == -1) {
3778 Py_DECREF(tail);
3779 return;
3780 }
3781 /* Stitch everything up into a nice comma-separated list. */
3782 comma = PyUnicode_FromString(", ");
3783 if (comma == NULL) {
3784 Py_DECREF(tail);
3785 return;
3786 }
3787 tmp = PyUnicode_Join(comma, names);
3788 Py_DECREF(comma);
3789 if (tmp == NULL) {
3790 Py_DECREF(tail);
3791 return;
3792 }
3793 name_str = PyUnicode_Concat(tmp, tail);
3794 Py_DECREF(tmp);
3795 Py_DECREF(tail);
3796 break;
3797 }
3798 if (name_str == NULL)
3799 return;
3800 PyErr_Format(PyExc_TypeError,
3801 "%U() missing %i required %s argument%s: %U",
3802 co->co_name,
3803 len,
3804 kind,
3805 len == 1 ? "" : "s",
3806 name_str);
3807 Py_DECREF(name_str);
3808}
3809
3810static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003811missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003812 PyObject **fastlocals)
3813{
Victor Stinner74319ae2016-08-25 00:04:09 +02003814 Py_ssize_t i, j = 0;
3815 Py_ssize_t start, end;
3816 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003817 const char *kind = positional ? "positional" : "keyword-only";
3818 PyObject *missing_names;
3819
3820 /* Compute the names of the arguments that are missing. */
3821 missing_names = PyList_New(missing);
3822 if (missing_names == NULL)
3823 return;
3824 if (positional) {
3825 start = 0;
3826 end = co->co_argcount - defcount;
3827 }
3828 else {
3829 start = co->co_argcount;
3830 end = start + co->co_kwonlyargcount;
3831 }
3832 for (i = start; i < end; i++) {
3833 if (GETLOCAL(i) == NULL) {
3834 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3835 PyObject *name = PyObject_Repr(raw);
3836 if (name == NULL) {
3837 Py_DECREF(missing_names);
3838 return;
3839 }
3840 PyList_SET_ITEM(missing_names, j++, name);
3841 }
3842 }
3843 assert(j == missing);
3844 format_missing(kind, co, missing_names);
3845 Py_DECREF(missing_names);
3846}
3847
3848static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003849too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3850 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003851{
3852 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003853 Py_ssize_t kwonly_given = 0;
3854 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003855 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003856 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003857
Benjamin Petersone109c702011-06-24 09:37:26 -05003858 assert((co->co_flags & CO_VARARGS) == 0);
3859 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003860 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3861 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003862 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003863 }
3864 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003865 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003866 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003867 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003868 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003869 }
3870 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003871 plural = (co_argcount != 1);
3872 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003873 }
3874 if (sig == NULL)
3875 return;
3876 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003877 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3878 kwonly_sig = PyUnicode_FromFormat(format,
3879 given != 1 ? "s" : "",
3880 kwonly_given,
3881 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003882 if (kwonly_sig == NULL) {
3883 Py_DECREF(sig);
3884 return;
3885 }
3886 }
3887 else {
3888 /* This will not fail. */
3889 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003890 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003891 }
3892 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003893 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003894 co->co_name,
3895 sig,
3896 plural ? "s" : "",
3897 given,
3898 kwonly_sig,
3899 given == 1 && !kwonly_given ? "was" : "were");
3900 Py_DECREF(sig);
3901 Py_DECREF(kwonly_sig);
3902}
3903
Guido van Rossumc2e20742006-02-27 22:32:47 +00003904/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003905 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003906 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003907
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003908PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003909_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Victor Stinner74319ae2016-08-25 00:04:09 +02003910 PyObject **args, Py_ssize_t argcount,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003911 PyObject **kwnames, PyObject **kwargs,
3912 Py_ssize_t kwcount, int kwstep,
Victor Stinner74319ae2016-08-25 00:04:09 +02003913 PyObject **defs, Py_ssize_t defcount,
3914 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003915 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003916{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003917 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003918 PyFrameObject *f;
3919 PyObject *retval = NULL;
3920 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003921 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003922 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003923 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3924 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003925 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003927 if (globals == NULL) {
3928 PyErr_SetString(PyExc_SystemError,
3929 "PyEval_EvalCodeEx: NULL globals");
3930 return NULL;
3931 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003932
Victor Stinnerc7020012016-08-16 23:40:29 +02003933 /* Create the frame */
3934 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003935 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003936 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003937 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003938 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003939 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003940 fastlocals = f->f_localsplus;
3941 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003942
Victor Stinnerc7020012016-08-16 23:40:29 +02003943 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003944 if (co->co_flags & CO_VARKEYWORDS) {
3945 kwdict = PyDict_New();
3946 if (kwdict == NULL)
3947 goto fail;
3948 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003949 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003950 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003951 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003952 SETLOCAL(i, kwdict);
3953 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003954 else {
3955 kwdict = NULL;
3956 }
3957
3958 /* Copy positional arguments into local variables */
3959 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003960 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003961 }
3962 else {
3963 n = argcount;
3964 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003965 for (i = 0; i < n; i++) {
3966 x = args[i];
3967 Py_INCREF(x);
3968 SETLOCAL(i, x);
3969 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003970
3971 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003972 if (co->co_flags & CO_VARARGS) {
3973 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003974 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003975 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003976 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003977 SETLOCAL(total_args, u);
3978 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003979 x = args[i];
3980 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003981 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003982 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003983 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003984
Serhiy Storchakab7281052016-09-12 00:52:40 +03003985 /* Handle keyword arguments passed as two strided arrays */
3986 kwcount *= kwstep;
3987 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003988 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003989 PyObject *keyword = kwnames[i];
3990 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003991 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003992
Benjamin Petersonb204a422011-06-05 22:04:07 -05003993 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3994 PyErr_Format(PyExc_TypeError,
3995 "%U() keywords must be strings",
3996 co->co_name);
3997 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003998 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003999
Benjamin Petersonb204a422011-06-05 22:04:07 -05004000 /* Speed hack: do raw pointer compares. As names are
4001 normally interned this should almost always hit. */
4002 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
4003 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004004 PyObject *name = co_varnames[j];
4005 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004006 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004007 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004008 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004009
Benjamin Petersonb204a422011-06-05 22:04:07 -05004010 /* Slow fallback, just in case */
4011 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004012 PyObject *name = co_varnames[j];
4013 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4014 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004015 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004016 }
4017 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004018 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004019 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004020 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004021
Victor Stinner231d1f32017-01-11 02:12:06 +01004022 assert(j >= total_args);
4023 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004024 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02004025 "%U() got an unexpected keyword argument '%S'",
4026 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004027 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004028 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004029
Christian Heimes0bd447f2013-07-20 14:48:10 +02004030 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4031 goto fail;
4032 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004033 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004034
Benjamin Petersonb204a422011-06-05 22:04:07 -05004035 kw_found:
4036 if (GETLOCAL(j) != NULL) {
4037 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02004038 "%U() got multiple values for argument '%S'",
4039 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004040 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004041 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004042 Py_INCREF(value);
4043 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004044 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004045
4046 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004047 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004048 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004049 goto fail;
4050 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004051
4052 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004053 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02004054 Py_ssize_t m = co->co_argcount - defcount;
4055 Py_ssize_t missing = 0;
4056 for (i = argcount; i < m; i++) {
4057 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004058 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004059 }
4060 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004061 if (missing) {
4062 missing_arguments(co, missing, defcount, fastlocals);
4063 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004064 }
4065 if (n > m)
4066 i = n - m;
4067 else
4068 i = 0;
4069 for (; i < defcount; i++) {
4070 if (GETLOCAL(m+i) == NULL) {
4071 PyObject *def = defs[i];
4072 Py_INCREF(def);
4073 SETLOCAL(m+i, def);
4074 }
4075 }
4076 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004077
4078 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004079 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004080 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004081 for (i = co->co_argcount; i < total_args; i++) {
4082 PyObject *name;
4083 if (GETLOCAL(i) != NULL)
4084 continue;
4085 name = PyTuple_GET_ITEM(co->co_varnames, i);
4086 if (kwdefs != NULL) {
4087 PyObject *def = PyDict_GetItem(kwdefs, name);
4088 if (def) {
4089 Py_INCREF(def);
4090 SETLOCAL(i, def);
4091 continue;
4092 }
4093 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004094 missing++;
4095 }
4096 if (missing) {
4097 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004098 goto fail;
4099 }
4100 }
4101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004102 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004103 vars into frame. */
4104 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004105 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004106 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004107 /* Possibly account for the cell variable being an argument. */
4108 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004109 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004110 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004111 /* Clear the local copy. */
4112 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004113 }
4114 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004115 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004116 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004117 if (c == NULL)
4118 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004119 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004120 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004121
4122 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004123 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4124 PyObject *o = PyTuple_GET_ITEM(closure, i);
4125 Py_INCREF(o);
4126 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004127 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004128
Yury Selivanoveb636452016-09-08 22:01:51 -07004129 /* Handle generator/coroutine/asynchronous generator */
4130 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004131 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04004132 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004133 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004134
4135 if (is_coro && tstate->in_coroutine_wrapper) {
4136 assert(coro_wrapper != NULL);
4137 PyErr_Format(PyExc_RuntimeError,
4138 "coroutine wrapper %.200R attempted "
4139 "to recursively wrap %.200R",
4140 coro_wrapper,
4141 co);
4142 goto fail;
4143 }
Yury Selivanov75445082015-05-11 22:57:16 -04004144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004145 /* Don't need to keep the reference to f_back, it will be set
4146 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004147 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004149 /* Create a new generator that owns the ready to run frame
4150 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004151 if (is_coro) {
4152 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004153 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4154 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004155 } else {
4156 gen = PyGen_NewWithQualName(f, name, qualname);
4157 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004158 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004159 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004160 }
INADA Naoki9c157762016-12-26 18:52:46 +09004161
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004162 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004163
Yury Selivanov94c22632015-06-04 10:16:51 -04004164 if (is_coro && coro_wrapper != NULL) {
4165 PyObject *wrapped;
4166 tstate->in_coroutine_wrapper = 1;
4167 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4168 tstate->in_coroutine_wrapper = 0;
4169 return wrapped;
4170 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004171
Yury Selivanov75445082015-05-11 22:57:16 -04004172 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004173 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004174
Victor Stinner59a73272016-12-09 18:51:13 +01004175 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004176
Thomas Woutersce272b62007-09-19 21:19:28 +00004177fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004179 /* decref'ing the frame can cause __del__ methods to get invoked,
4180 which can call back into Python. While we're done with the
4181 current Python frame (f), the associated C stack is still in use,
4182 so recursion_depth must be boosted for the duration.
4183 */
4184 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09004185 if (Py_REFCNT(f) > 1) {
4186 Py_DECREF(f);
4187 _PyObject_GC_TRACK(f);
4188 }
4189 else {
4190 ++tstate->recursion_depth;
4191 Py_DECREF(f);
4192 --tstate->recursion_depth;
4193 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004194 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004195}
4196
Victor Stinner40ee3012014-06-16 15:59:28 +02004197PyObject *
4198PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
4199 PyObject **args, int argcount, PyObject **kws, int kwcount,
4200 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
4201{
4202 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004203 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004204 kws, kws != NULL ? kws + 1 : NULL,
4205 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004206 defs, defcount,
4207 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004208 NULL, NULL);
4209}
Tim Peters5ca576e2001-06-18 22:08:13 +00004210
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004211static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004212special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004214 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004215 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004216 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004217 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004218 return NULL;
4219 }
4220 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004221}
4222
4223
Benjamin Peterson87880242011-07-03 16:48:31 -05004224/* These 3 functions deal with the exception state of generators. */
4225
4226static void
4227save_exc_state(PyThreadState *tstate, PyFrameObject *f)
4228{
4229 PyObject *type, *value, *traceback;
4230 Py_XINCREF(tstate->exc_type);
4231 Py_XINCREF(tstate->exc_value);
4232 Py_XINCREF(tstate->exc_traceback);
4233 type = f->f_exc_type;
4234 value = f->f_exc_value;
4235 traceback = f->f_exc_traceback;
4236 f->f_exc_type = tstate->exc_type;
4237 f->f_exc_value = tstate->exc_value;
4238 f->f_exc_traceback = tstate->exc_traceback;
4239 Py_XDECREF(type);
4240 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004241 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05004242}
4243
4244static void
4245swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
4246{
4247 PyObject *tmp;
4248 tmp = tstate->exc_type;
4249 tstate->exc_type = f->f_exc_type;
4250 f->f_exc_type = tmp;
4251 tmp = tstate->exc_value;
4252 tstate->exc_value = f->f_exc_value;
4253 f->f_exc_value = tmp;
4254 tmp = tstate->exc_traceback;
4255 tstate->exc_traceback = f->f_exc_traceback;
4256 f->f_exc_traceback = tmp;
4257}
4258
4259static void
4260restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
4261{
4262 PyObject *type, *value, *tb;
4263 type = tstate->exc_type;
4264 value = tstate->exc_value;
4265 tb = tstate->exc_traceback;
4266 tstate->exc_type = f->f_exc_type;
4267 tstate->exc_value = f->f_exc_value;
4268 tstate->exc_traceback = f->f_exc_traceback;
4269 f->f_exc_type = NULL;
4270 f->f_exc_value = NULL;
4271 f->f_exc_traceback = NULL;
4272 Py_XDECREF(type);
4273 Py_XDECREF(value);
4274 Py_XDECREF(tb);
4275}
4276
4277
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004278/* Logic for the raise statement (too complicated for inlining).
4279 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004280static int
Collin Winter828f04a2007-08-31 00:04:24 +00004281do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004282{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004283 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004285 if (exc == NULL) {
4286 /* Reraise */
4287 PyThreadState *tstate = PyThreadState_GET();
4288 PyObject *tb;
4289 type = tstate->exc_type;
4290 value = tstate->exc_value;
4291 tb = tstate->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004292 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004293 PyErr_SetString(PyExc_RuntimeError,
4294 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004295 return 0;
4296 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004297 Py_XINCREF(type);
4298 Py_XINCREF(value);
4299 Py_XINCREF(tb);
4300 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004301 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004302 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004304 /* We support the following forms of raise:
4305 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004306 raise <instance>
4307 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004309 if (PyExceptionClass_Check(exc)) {
4310 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004311 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004312 if (value == NULL)
4313 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004314 if (!PyExceptionInstance_Check(value)) {
4315 PyErr_Format(PyExc_TypeError,
4316 "calling %R should have returned an instance of "
4317 "BaseException, not %R",
4318 type, Py_TYPE(value));
4319 goto raise_error;
4320 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004321 }
4322 else if (PyExceptionInstance_Check(exc)) {
4323 value = exc;
4324 type = PyExceptionInstance_Class(exc);
4325 Py_INCREF(type);
4326 }
4327 else {
4328 /* Not something you can raise. You get an exception
4329 anyway, just not what you specified :-) */
4330 Py_DECREF(exc);
4331 PyErr_SetString(PyExc_TypeError,
4332 "exceptions must derive from BaseException");
4333 goto raise_error;
4334 }
Collin Winter828f04a2007-08-31 00:04:24 +00004335
Serhiy Storchakac0191582016-09-27 11:37:10 +03004336 assert(type != NULL);
4337 assert(value != NULL);
4338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004339 if (cause) {
4340 PyObject *fixed_cause;
4341 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004342 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004343 if (fixed_cause == NULL)
4344 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004345 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004346 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004347 else if (PyExceptionInstance_Check(cause)) {
4348 fixed_cause = cause;
4349 }
4350 else if (cause == Py_None) {
4351 Py_DECREF(cause);
4352 fixed_cause = NULL;
4353 }
4354 else {
4355 PyErr_SetString(PyExc_TypeError,
4356 "exception causes must derive from "
4357 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004358 goto raise_error;
4359 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004360 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004361 }
Collin Winter828f04a2007-08-31 00:04:24 +00004362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004363 PyErr_SetObject(type, value);
4364 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004365 Py_DECREF(value);
4366 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004367 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004368
4369raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004370 Py_XDECREF(value);
4371 Py_XDECREF(type);
4372 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004373 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004374}
4375
Tim Petersd6d010b2001-06-21 02:49:55 +00004376/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004377 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004378
Guido van Rossum0368b722007-05-11 16:50:42 +00004379 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4380 with a variable target.
4381*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004382
Barry Warsawe42b18f1997-08-25 22:13:04 +00004383static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004384unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004386 int i = 0, j = 0;
4387 Py_ssize_t ll = 0;
4388 PyObject *it; /* iter(v) */
4389 PyObject *w;
4390 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004392 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004394 it = PyObject_GetIter(v);
4395 if (it == NULL)
4396 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00004397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004398 for (; i < argcnt; i++) {
4399 w = PyIter_Next(it);
4400 if (w == NULL) {
4401 /* Iterator done, via error or exhaustion. */
4402 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004403 if (argcntafter == -1) {
4404 PyErr_Format(PyExc_ValueError,
4405 "not enough values to unpack (expected %d, got %d)",
4406 argcnt, i);
4407 }
4408 else {
4409 PyErr_Format(PyExc_ValueError,
4410 "not enough values to unpack "
4411 "(expected at least %d, got %d)",
4412 argcnt + argcntafter, i);
4413 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414 }
4415 goto Error;
4416 }
4417 *--sp = w;
4418 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004420 if (argcntafter == -1) {
4421 /* We better have exhausted the iterator now. */
4422 w = PyIter_Next(it);
4423 if (w == NULL) {
4424 if (PyErr_Occurred())
4425 goto Error;
4426 Py_DECREF(it);
4427 return 1;
4428 }
4429 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004430 PyErr_Format(PyExc_ValueError,
4431 "too many values to unpack (expected %d)",
4432 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004433 goto Error;
4434 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004436 l = PySequence_List(it);
4437 if (l == NULL)
4438 goto Error;
4439 *--sp = l;
4440 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004442 ll = PyList_GET_SIZE(l);
4443 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004444 PyErr_Format(PyExc_ValueError,
4445 "not enough values to unpack (expected at least %d, got %zd)",
4446 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004447 goto Error;
4448 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004450 /* Pop the "after-variable" args off the list. */
4451 for (j = argcntafter; j > 0; j--, i++) {
4452 *--sp = PyList_GET_ITEM(l, ll - j);
4453 }
4454 /* Resize the list. */
4455 Py_SIZE(l) = ll - argcntafter;
4456 Py_DECREF(it);
4457 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004458
Tim Petersd6d010b2001-06-21 02:49:55 +00004459Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004460 for (; i > 0; i--, sp++)
4461 Py_DECREF(*sp);
4462 Py_XDECREF(it);
4463 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004464}
4465
4466
Guido van Rossum96a42c81992-01-12 02:29:51 +00004467#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004468static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004469prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004470{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004471 printf("%s ", str);
4472 if (PyObject_Print(v, stdout, 0) != 0)
4473 PyErr_Clear(); /* Don't know what else to do */
4474 printf("\n");
4475 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004476}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004477#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004478
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004479static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004480call_exc_trace(Py_tracefunc func, PyObject *self,
4481 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004482{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004483 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004484 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004485 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004486 if (value == NULL) {
4487 value = Py_None;
4488 Py_INCREF(value);
4489 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004490 PyErr_NormalizeException(&type, &value, &orig_traceback);
4491 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004492 arg = PyTuple_Pack(3, type, value, traceback);
4493 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004494 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004495 return;
4496 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004497 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004498 Py_DECREF(arg);
4499 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004500 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004501 else {
4502 Py_XDECREF(type);
4503 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004504 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004505 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004506}
4507
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004508static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004509call_trace_protected(Py_tracefunc func, PyObject *obj,
4510 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004511 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004512{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004513 PyObject *type, *value, *traceback;
4514 int err;
4515 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004516 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004517 if (err == 0)
4518 {
4519 PyErr_Restore(type, value, traceback);
4520 return 0;
4521 }
4522 else {
4523 Py_XDECREF(type);
4524 Py_XDECREF(value);
4525 Py_XDECREF(traceback);
4526 return -1;
4527 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004528}
4529
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004530static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004531call_trace(Py_tracefunc func, PyObject *obj,
4532 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004533 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004534{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004535 int result;
4536 if (tstate->tracing)
4537 return 0;
4538 tstate->tracing++;
4539 tstate->use_tracing = 0;
4540 result = func(obj, frame, what, arg);
4541 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4542 || (tstate->c_profilefunc != NULL));
4543 tstate->tracing--;
4544 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004545}
4546
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004547PyObject *
4548_PyEval_CallTracing(PyObject *func, PyObject *args)
4549{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004550 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004551 int save_tracing = tstate->tracing;
4552 int save_use_tracing = tstate->use_tracing;
4553 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004555 tstate->tracing = 0;
4556 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4557 || (tstate->c_profilefunc != NULL));
4558 result = PyObject_Call(func, args, NULL);
4559 tstate->tracing = save_tracing;
4560 tstate->use_tracing = save_use_tracing;
4561 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004562}
4563
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004564/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004565static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004566maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004567 PyThreadState *tstate, PyFrameObject *frame,
4568 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004569{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004570 int result = 0;
4571 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004573 /* If the last instruction executed isn't in the current
4574 instruction window, reset the window.
4575 */
4576 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4577 PyAddrPair bounds;
4578 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4579 &bounds);
4580 *instr_lb = bounds.ap_lower;
4581 *instr_ub = bounds.ap_upper;
4582 }
4583 /* If the last instruction falls at the start of a line or if
4584 it represents a jump backwards, update the frame's line
4585 number and call the trace function. */
4586 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
4587 frame->f_lineno = line;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004588 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004589 }
4590 *instr_prev = frame->f_lasti;
4591 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004592}
4593
Fred Drake5755ce62001-06-27 19:19:46 +00004594void
4595PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004596{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004597 PyThreadState *tstate = PyThreadState_GET();
4598 PyObject *temp = tstate->c_profileobj;
4599 Py_XINCREF(arg);
4600 tstate->c_profilefunc = NULL;
4601 tstate->c_profileobj = NULL;
4602 /* Must make sure that tracing is not ignored if 'temp' is freed */
4603 tstate->use_tracing = tstate->c_tracefunc != NULL;
4604 Py_XDECREF(temp);
4605 tstate->c_profilefunc = func;
4606 tstate->c_profileobj = arg;
4607 /* Flag that tracing or profiling is turned on */
4608 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004609}
4610
4611void
4612PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4613{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004614 PyThreadState *tstate = PyThreadState_GET();
4615 PyObject *temp = tstate->c_traceobj;
4616 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4617 Py_XINCREF(arg);
4618 tstate->c_tracefunc = NULL;
4619 tstate->c_traceobj = NULL;
4620 /* Must make sure that profiling is not ignored if 'temp' is freed */
4621 tstate->use_tracing = tstate->c_profilefunc != NULL;
4622 Py_XDECREF(temp);
4623 tstate->c_tracefunc = func;
4624 tstate->c_traceobj = arg;
4625 /* Flag that tracing or profiling is turned on */
4626 tstate->use_tracing = ((func != NULL)
4627 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004628}
4629
Yury Selivanov75445082015-05-11 22:57:16 -04004630void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004631_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004632{
4633 PyThreadState *tstate = PyThreadState_GET();
4634
Yury Selivanov75445082015-05-11 22:57:16 -04004635 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004636 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004637}
4638
4639PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004640_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004641{
4642 PyThreadState *tstate = PyThreadState_GET();
4643 return tstate->coroutine_wrapper;
4644}
4645
Yury Selivanoveb636452016-09-08 22:01:51 -07004646void
4647_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4648{
4649 PyThreadState *tstate = PyThreadState_GET();
4650
4651 Py_XINCREF(firstiter);
4652 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4653}
4654
4655PyObject *
4656_PyEval_GetAsyncGenFirstiter(void)
4657{
4658 PyThreadState *tstate = PyThreadState_GET();
4659 return tstate->async_gen_firstiter;
4660}
4661
4662void
4663_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4664{
4665 PyThreadState *tstate = PyThreadState_GET();
4666
4667 Py_XINCREF(finalizer);
4668 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4669}
4670
4671PyObject *
4672_PyEval_GetAsyncGenFinalizer(void)
4673{
4674 PyThreadState *tstate = PyThreadState_GET();
4675 return tstate->async_gen_finalizer;
4676}
4677
Guido van Rossumb209a111997-04-29 18:18:01 +00004678PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004679PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004680{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004681 PyFrameObject *current_frame = PyEval_GetFrame();
4682 if (current_frame == NULL)
4683 return PyThreadState_GET()->interp->builtins;
4684 else
4685 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004686}
4687
Guido van Rossumb209a111997-04-29 18:18:01 +00004688PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004689PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004690{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004691 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004692 if (current_frame == NULL) {
4693 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004694 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004695 }
4696
4697 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4698 return NULL;
4699
4700 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004701 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004702}
4703
Guido van Rossumb209a111997-04-29 18:18:01 +00004704PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004705PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004706{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004707 PyFrameObject *current_frame = PyEval_GetFrame();
4708 if (current_frame == NULL)
4709 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004710
4711 assert(current_frame->f_globals != NULL);
4712 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004713}
4714
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004715PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004716PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004718 PyThreadState *tstate = PyThreadState_GET();
4719 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004720}
4721
Guido van Rossum6135a871995-01-09 17:53:26 +00004722int
Tim Peters5ba58662001-07-16 02:29:45 +00004723PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004724{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004725 PyFrameObject *current_frame = PyEval_GetFrame();
4726 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004728 if (current_frame != NULL) {
4729 const int codeflags = current_frame->f_code->co_flags;
4730 const int compilerflags = codeflags & PyCF_MASK;
4731 if (compilerflags) {
4732 result = 1;
4733 cf->cf_flags |= compilerflags;
4734 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004735#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004736 if (codeflags & CO_GENERATOR_ALLOWED) {
4737 result = 1;
4738 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4739 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004740#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004741 }
4742 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004743}
4744
Guido van Rossum3f5da241990-12-20 15:06:42 +00004745
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004746const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004747PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004748{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004749 if (PyMethod_Check(func))
4750 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4751 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004752 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004753 else if (PyCFunction_Check(func))
4754 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4755 else
4756 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004757}
4758
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004759const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004760PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004761{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004762 if (PyMethod_Check(func))
4763 return "()";
4764 else if (PyFunction_Check(func))
4765 return "()";
4766 else if (PyCFunction_Check(func))
4767 return "()";
4768 else
4769 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004770}
4771
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004772#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004773if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004774 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4775 tstate, tstate->frame, \
4776 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004777 x = NULL; \
4778 } \
4779 else { \
4780 x = call; \
4781 if (tstate->c_profilefunc != NULL) { \
4782 if (x == NULL) { \
4783 call_trace_protected(tstate->c_profilefunc, \
4784 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004785 tstate, tstate->frame, \
4786 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004787 /* XXX should pass (type, value, tb) */ \
4788 } else { \
4789 if (call_trace(tstate->c_profilefunc, \
4790 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004791 tstate, tstate->frame, \
4792 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004793 Py_DECREF(x); \
4794 x = NULL; \
4795 } \
4796 } \
4797 } \
4798 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004799} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004800 x = call; \
4801 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004802
Victor Stinner415c5102017-01-11 00:54:57 +01004803/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4804 to reduce the stack consumption. */
4805Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004806call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004807{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004808 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004809 PyObject *func = *pfunc;
4810 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004811 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4812 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004813 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004815 /* Always dispatch PyCFunction first, because these are
4816 presumed to be the most frequent callable object.
4817 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004818 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004819 PyThreadState *tstate = PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004820 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004821 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004822 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
4823 PyThreadState *tstate = PyThreadState_GET();
INADA Naoki93fac8d2017-03-07 14:24:37 +09004824 if (tstate->use_tracing && tstate->c_profilefunc) {
4825 // We need to create PyCFunctionObject for tracing.
4826 PyMethodDescrObject *descr = (PyMethodDescrObject*)func;
4827 func = PyCFunction_NewEx(descr->d_method, stack[0], NULL);
4828 if (func == NULL) {
4829 return NULL;
4830 }
4831 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack+1, nargs-1,
4832 kwnames));
4833 Py_DECREF(func);
4834 }
4835 else {
4836 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4837 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004838 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004839 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004840 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004841 /* Optimize access to bound methods. Reuse the Python stack
4842 to pass 'self' as the first argument, replace 'func'
4843 with 'self'. It avoids the creation of a new temporary tuple
4844 for arguments (to replace func with self) when the method uses
4845 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004846 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004847 Py_INCREF(self);
4848 func = PyMethod_GET_FUNCTION(func);
4849 Py_INCREF(func);
4850 Py_SETREF(*pfunc, self);
4851 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004852 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004853 }
4854 else {
4855 Py_INCREF(func);
4856 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004857
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004858 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004859 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004860 }
4861 else {
4862 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4863 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004864 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004865 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004866
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004867 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4868
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004869 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004870 while ((*pp_stack) > pfunc) {
4871 w = EXT_POP(*pp_stack);
4872 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004873 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004875 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004876}
4877
Jeremy Hylton52820442001-01-03 23:52:36 +00004878static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004879do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004880{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004881 if (PyCFunction_Check(func)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004882 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004883 PyThreadState *tstate = PyThreadState_GET();
4884 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004885 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004886 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004887 else {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004888 return PyObject_Call(func, callargs, kwdict);
Victor Stinner74319ae2016-08-25 00:04:09 +02004889 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004890}
4891
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004892/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004893 nb_index slot defined, and store in *pi.
4894 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004895 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004896 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004897*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004898int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004899_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004900{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004901 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004902 Py_ssize_t x;
4903 if (PyIndex_Check(v)) {
4904 x = PyNumber_AsSsize_t(v, NULL);
4905 if (x == -1 && PyErr_Occurred())
4906 return 0;
4907 }
4908 else {
4909 PyErr_SetString(PyExc_TypeError,
4910 "slice indices must be integers or "
4911 "None or have an __index__ method");
4912 return 0;
4913 }
4914 *pi = x;
4915 }
4916 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004917}
4918
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004919int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004920_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004921{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004922 Py_ssize_t x;
4923 if (PyIndex_Check(v)) {
4924 x = PyNumber_AsSsize_t(v, NULL);
4925 if (x == -1 && PyErr_Occurred())
4926 return 0;
4927 }
4928 else {
4929 PyErr_SetString(PyExc_TypeError,
4930 "slice indices must be integers or "
4931 "have an __index__ method");
4932 return 0;
4933 }
4934 *pi = x;
4935 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004936}
4937
4938
Guido van Rossum486364b2007-06-30 05:01:58 +00004939#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004940 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004941
Guido van Rossumb209a111997-04-29 18:18:01 +00004942static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004943cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004944{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004945 int res = 0;
4946 switch (op) {
4947 case PyCmp_IS:
4948 res = (v == w);
4949 break;
4950 case PyCmp_IS_NOT:
4951 res = (v != w);
4952 break;
4953 case PyCmp_IN:
4954 res = PySequence_Contains(w, v);
4955 if (res < 0)
4956 return NULL;
4957 break;
4958 case PyCmp_NOT_IN:
4959 res = PySequence_Contains(w, v);
4960 if (res < 0)
4961 return NULL;
4962 res = !res;
4963 break;
4964 case PyCmp_EXC_MATCH:
4965 if (PyTuple_Check(w)) {
4966 Py_ssize_t i, length;
4967 length = PyTuple_Size(w);
4968 for (i = 0; i < length; i += 1) {
4969 PyObject *exc = PyTuple_GET_ITEM(w, i);
4970 if (!PyExceptionClass_Check(exc)) {
4971 PyErr_SetString(PyExc_TypeError,
4972 CANNOT_CATCH_MSG);
4973 return NULL;
4974 }
4975 }
4976 }
4977 else {
4978 if (!PyExceptionClass_Check(w)) {
4979 PyErr_SetString(PyExc_TypeError,
4980 CANNOT_CATCH_MSG);
4981 return NULL;
4982 }
4983 }
4984 res = PyErr_GivenExceptionMatches(v, w);
4985 break;
4986 default:
4987 return PyObject_RichCompare(v, w, op);
4988 }
4989 v = res ? Py_True : Py_False;
4990 Py_INCREF(v);
4991 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004992}
4993
Thomas Wouters52152252000-08-17 22:55:00 +00004994static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004995import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4996{
4997 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004998 PyObject *import_func, *res;
4999 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005000
5001 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
5002 if (import_func == NULL) {
5003 PyErr_SetString(PyExc_ImportError, "__import__ not found");
5004 return NULL;
5005 }
5006
5007 /* Fast path for not overloaded __import__. */
5008 if (import_func == PyThreadState_GET()->interp->import_func) {
5009 int ilevel = _PyLong_AsInt(level);
5010 if (ilevel == -1 && PyErr_Occurred()) {
5011 return NULL;
5012 }
5013 res = PyImport_ImportModuleLevelObject(
5014 name,
5015 f->f_globals,
5016 f->f_locals == NULL ? Py_None : f->f_locals,
5017 fromlist,
5018 ilevel);
5019 return res;
5020 }
5021
5022 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005023
5024 stack[0] = name;
5025 stack[1] = f->f_globals;
5026 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5027 stack[3] = fromlist;
5028 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005029 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005030 Py_DECREF(import_func);
5031 return res;
5032}
5033
5034static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00005035import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005036{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005037 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005038 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08005039 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005041 x = PyObject_GetAttr(v, name);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005042 if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
5043 return x;
5044 /* Issue #17636: in case this failed because of a circular relative
5045 import, try to fallback on reading the module directly from
5046 sys.modules. */
5047 PyErr_Clear();
5048 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005049 if (pkgname == NULL) {
5050 goto error;
5051 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005052 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005053 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005054 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005055 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005056 }
Eric Snow86b7afd2017-09-04 17:54:09 -06005057 x = _PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005058 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005059 if (x == NULL) {
5060 goto error;
5061 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005062 Py_DECREF(pkgname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005063 Py_INCREF(x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005064 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005065 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005066 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005067 if (pkgname == NULL) {
5068 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5069 if (pkgname_or_unknown == NULL) {
5070 Py_XDECREF(pkgpath);
5071 return NULL;
5072 }
5073 } else {
5074 pkgname_or_unknown = pkgname;
5075 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005076
5077 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
5078 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08005079 errmsg = PyUnicode_FromFormat(
5080 "cannot import name %R from %R (unknown location)",
5081 name, pkgname_or_unknown
5082 );
5083 /* NULL check for errmsg done by PyErr_SetImportError. */
5084 PyErr_SetImportError(errmsg, pkgname, NULL);
5085 }
5086 else {
5087 errmsg = PyUnicode_FromFormat(
5088 "cannot import name %R from %R (%S)",
5089 name, pkgname_or_unknown, pkgpath
5090 );
5091 /* NULL check for errmsg done by PyErr_SetImportError. */
5092 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005093 }
5094
Xiang Zhang4830f582017-03-21 11:13:42 +08005095 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005096 Py_XDECREF(pkgname_or_unknown);
5097 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005098 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005099}
Guido van Rossumac7be682001-01-17 15:42:30 +00005100
Thomas Wouters52152252000-08-17 22:55:00 +00005101static int
5102import_all_from(PyObject *locals, PyObject *v)
5103{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005104 _Py_IDENTIFIER(__all__);
5105 _Py_IDENTIFIER(__dict__);
5106 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005107 PyObject *dict, *name, *value;
5108 int skip_leading_underscores = 0;
5109 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005111 if (all == NULL) {
5112 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5113 return -1; /* Unexpected error */
5114 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005115 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005116 if (dict == NULL) {
5117 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5118 return -1;
5119 PyErr_SetString(PyExc_ImportError,
5120 "from-import-* object has no __dict__ and no __all__");
5121 return -1;
5122 }
5123 all = PyMapping_Keys(dict);
5124 Py_DECREF(dict);
5125 if (all == NULL)
5126 return -1;
5127 skip_leading_underscores = 1;
5128 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005129
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005130 for (pos = 0, err = 0; ; pos++) {
5131 name = PySequence_GetItem(all, pos);
5132 if (name == NULL) {
5133 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5134 err = -1;
5135 else
5136 PyErr_Clear();
5137 break;
5138 }
5139 if (skip_leading_underscores &&
5140 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005141 PyUnicode_READY(name) != -1 &&
5142 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005143 {
5144 Py_DECREF(name);
5145 continue;
5146 }
5147 value = PyObject_GetAttr(v, name);
5148 if (value == NULL)
5149 err = -1;
5150 else if (PyDict_CheckExact(locals))
5151 err = PyDict_SetItem(locals, name, value);
5152 else
5153 err = PyObject_SetItem(locals, name, value);
5154 Py_DECREF(name);
5155 Py_XDECREF(value);
5156 if (err != 0)
5157 break;
5158 }
5159 Py_DECREF(all);
5160 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005161}
5162
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005163static int
5164check_args_iterable(PyObject *func, PyObject *args)
5165{
5166 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
5167 PyErr_Format(PyExc_TypeError,
5168 "%.200s%.200s argument after * "
5169 "must be an iterable, not %.200s",
5170 PyEval_GetFuncName(func),
5171 PyEval_GetFuncDesc(func),
5172 args->ob_type->tp_name);
5173 return -1;
5174 }
5175 return 0;
5176}
5177
5178static void
5179format_kwargs_mapping_error(PyObject *func, PyObject *kwargs)
5180{
5181 PyErr_Format(PyExc_TypeError,
5182 "%.200s%.200s argument after ** "
5183 "must be a mapping, not %.200s",
5184 PyEval_GetFuncName(func),
5185 PyEval_GetFuncDesc(func),
5186 kwargs->ob_type->tp_name);
5187}
5188
Guido van Rossumac7be682001-01-17 15:42:30 +00005189static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005190format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005191{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005192 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005194 if (!obj)
5195 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005196
Serhiy Storchaka06515832016-11-20 09:13:07 +02005197 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005198 if (!obj_str)
5199 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005201 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005202}
Guido van Rossum950361c1997-01-24 13:49:28 +00005203
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005204static void
5205format_exc_unbound(PyCodeObject *co, int oparg)
5206{
5207 PyObject *name;
5208 /* Don't stomp existing exception */
5209 if (PyErr_Occurred())
5210 return;
5211 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5212 name = PyTuple_GET_ITEM(co->co_cellvars,
5213 oparg);
5214 format_exc_check_arg(
5215 PyExc_UnboundLocalError,
5216 UNBOUNDLOCAL_ERROR_MSG,
5217 name);
5218 } else {
5219 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5220 PyTuple_GET_SIZE(co->co_cellvars));
5221 format_exc_check_arg(PyExc_NameError,
5222 UNBOUNDFREE_ERROR_MSG, name);
5223 }
5224}
5225
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005226static PyObject *
5227unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005228 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005229{
5230 PyObject *res;
5231 if (Py_REFCNT(v) == 2) {
5232 /* In the common case, there are 2 references to the value
5233 * stored in 'variable' when the += is performed: one on the
5234 * value stack (in 'v') and one still stored in the
5235 * 'variable'. We try to delete the variable now to reduce
5236 * the refcnt to 1.
5237 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005238 int opcode, oparg;
5239 NEXTOPARG();
5240 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005241 case STORE_FAST:
5242 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005243 PyObject **fastlocals = f->f_localsplus;
5244 if (GETLOCAL(oparg) == v)
5245 SETLOCAL(oparg, NULL);
5246 break;
5247 }
5248 case STORE_DEREF:
5249 {
5250 PyObject **freevars = (f->f_localsplus +
5251 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005252 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005253 if (PyCell_GET(c) == v) {
5254 PyCell_SET(c, NULL);
5255 Py_DECREF(v);
5256 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005257 break;
5258 }
5259 case STORE_NAME:
5260 {
5261 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005262 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005263 PyObject *locals = f->f_locals;
5264 if (PyDict_CheckExact(locals) &&
5265 PyDict_GetItem(locals, name) == v) {
5266 if (PyDict_DelItem(locals, name) != 0) {
5267 PyErr_Clear();
5268 }
5269 }
5270 break;
5271 }
5272 }
5273 }
5274 res = v;
5275 PyUnicode_Append(&res, w);
5276 return res;
5277}
5278
Guido van Rossum950361c1997-01-24 13:49:28 +00005279#ifdef DYNAMIC_EXECUTION_PROFILE
5280
Skip Montanarof118cb12001-10-15 20:51:38 +00005281static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005282getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005283{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005284 int i;
5285 PyObject *l = PyList_New(256);
5286 if (l == NULL) return NULL;
5287 for (i = 0; i < 256; i++) {
5288 PyObject *x = PyLong_FromLong(a[i]);
5289 if (x == NULL) {
5290 Py_DECREF(l);
5291 return NULL;
5292 }
5293 PyList_SetItem(l, i, x);
5294 }
5295 for (i = 0; i < 256; i++)
5296 a[i] = 0;
5297 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005298}
5299
5300PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005301_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005302{
5303#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005304 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005305#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005306 int i;
5307 PyObject *l = PyList_New(257);
5308 if (l == NULL) return NULL;
5309 for (i = 0; i < 257; i++) {
5310 PyObject *x = getarray(dxpairs[i]);
5311 if (x == NULL) {
5312 Py_DECREF(l);
5313 return NULL;
5314 }
5315 PyList_SetItem(l, i, x);
5316 }
5317 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005318#endif
5319}
5320
5321#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005322
5323Py_ssize_t
5324_PyEval_RequestCodeExtraIndex(freefunc free)
5325{
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005326 PyInterpreterState *interp = PyThreadState_Get()->interp;
Brett Cannon5c4de282016-09-07 11:16:41 -07005327 Py_ssize_t new_index;
5328
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005329 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005330 return -1;
5331 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005332 new_index = interp->co_extra_user_count++;
5333 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005334 return new_index;
5335}
Łukasz Langaa785c872016-09-09 17:37:37 -07005336
5337static void
5338dtrace_function_entry(PyFrameObject *f)
5339{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005340 const char *filename;
5341 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005342 int lineno;
5343
5344 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5345 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5346 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5347
5348 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5349}
5350
5351static void
5352dtrace_function_return(PyFrameObject *f)
5353{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005354 const char *filename;
5355 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005356 int lineno;
5357
5358 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5359 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5360 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5361
5362 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5363}
5364
5365/* DTrace equivalent of maybe_call_line_trace. */
5366static void
5367maybe_dtrace_line(PyFrameObject *frame,
5368 int *instr_lb, int *instr_ub, int *instr_prev)
5369{
5370 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005371 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005372
5373 /* If the last instruction executed isn't in the current
5374 instruction window, reset the window.
5375 */
5376 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5377 PyAddrPair bounds;
5378 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5379 &bounds);
5380 *instr_lb = bounds.ap_lower;
5381 *instr_ub = bounds.ap_upper;
5382 }
5383 /* If the last instruction falls at the start of a line or if
5384 it represents a jump backwards, update the frame's line
5385 number and call the trace function. */
5386 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5387 frame->f_lineno = line;
5388 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5389 if (!co_filename)
5390 co_filename = "?";
5391 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5392 if (!co_name)
5393 co_name = "?";
5394 PyDTrace_LINE(co_filename, co_name, line);
5395 }
5396 *instr_prev = frame->f_lasti;
5397}