blob: 9741c15b892377753025459c2e1e22cf2e558e51 [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 Snow76d5abc2017-09-05 18:26:16 -070039Py_LOCAL_INLINE(PyObject *) call_function(PyObject ***, Py_ssize_t,
40 PyObject *);
Victor Stinnerf9b760f2016-09-09 10:17:08 -070041static PyObject * do_call_core(PyObject *, PyObject *, PyObject *);
Jeremy Hylton52820442001-01-03 23:52:36 +000042
Guido van Rossum0a066c01992-03-27 17:29:15 +000043#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000044static int lltrace;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020045static int prtrace(PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000046#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010047static int call_trace(Py_tracefunc, PyObject *,
48 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000050static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010051 PyThreadState *, PyFrameObject *,
52 int, PyObject *);
53static void call_exc_trace(Py_tracefunc, PyObject *,
54 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000055static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow76d5abc2017-09-05 18:26:16 -070056 PyThreadState *, PyFrameObject *,
57 int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070058static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
59static void dtrace_function_entry(PyFrameObject *);
60static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000061
Thomas Wouters477c8d52006-05-27 19:21:47 +000062static PyObject * cmp_outcome(int, PyObject *, PyObject *);
Eric Snow76d5abc2017-09-05 18:26:16 -070063static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *,
64 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +000065static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +000066static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +000067static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +000068static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +020069static PyObject * unicode_concatenate(PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030070 PyFrameObject *, const _Py_CODEUNIT *);
Benjamin Petersonce798522012-01-22 11:24:29 -050071static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Serhiy Storchaka25e4f772017-08-03 11:37:15 +030072static int check_args_iterable(PyObject *func, PyObject *vararg);
73static void format_kwargs_mapping_error(PyObject *func, PyObject *kwargs);
Guido van Rossum374a9221991-04-04 10:40:29 +000074
Paul Prescode68140d2000-08-30 20:25:01 +000075#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000077#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000079#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 "free variable '%.200s' referenced before assignment" \
81 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000082
Guido van Rossum950361c1997-01-24 13:49:28 +000083/* Dynamic execution profile */
84#ifdef DYNAMIC_EXECUTION_PROFILE
85#ifdef DXPAIRS
86static long dxpairs[257][256];
87#define dxp dxpairs[256]
88#else
89static long dxp[256];
90#endif
91#endif
92
Benjamin Petersond2be5b42010-09-10 22:47:02 +000093#ifdef WITH_THREAD
Eric Snow76d5abc2017-09-05 18:26:16 -070094#define GIL_REQUEST _Py_atomic_load_relaxed(&_PyRuntime.ceval.gil_drop_request)
Benjamin Petersond2be5b42010-09-10 22:47:02 +000095#else
96#define GIL_REQUEST 0
97#endif
98
Jeffrey Yasskin39370832010-05-03 19:29:34 +000099/* This can set eval_breaker to 0 even though gil_drop_request became
100 1. We believe this is all right because the eval loop will release
101 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000102#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 _Py_atomic_store_relaxed( \
Eric Snow76d5abc2017-09-05 18:26:16 -0700104 &_PyRuntime.ceval.eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000105 GIL_REQUEST | \
Eric Snow76d5abc2017-09-05 18:26:16 -0700106 _Py_atomic_load_relaxed(&_PyRuntime.ceval.pending.calls_to_do) | \
107 _PyRuntime.ceval.pending.async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000108
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000109#ifdef WITH_THREAD
110
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000111#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 do { \
Eric Snow76d5abc2017-09-05 18:26:16 -0700113 _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 1); \
114 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000116
117#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 do { \
Eric Snow76d5abc2017-09-05 18:26:16 -0700119 _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 COMPUTE_EVAL_BREAKER(); \
121 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000122
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000123#endif
124
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000125/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000126#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 do { \
Eric Snow76d5abc2017-09-05 18:26:16 -0700128 _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 1); \
129 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000131
132#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 do { \
Eric Snow76d5abc2017-09-05 18:26:16 -0700134 _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 COMPUTE_EVAL_BREAKER(); \
136 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000137
138#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 do { \
Eric Snow76d5abc2017-09-05 18:26:16 -0700140 _PyRuntime.ceval.pending.async_exc = 1; \
141 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000143
144#define UNSIGNAL_ASYNC_EXC() \
Eric Snow76d5abc2017-09-05 18:26:16 -0700145 do { \
146 _PyRuntime.ceval.pending.async_exc = 0; \
147 COMPUTE_EVAL_BREAKER(); \
148 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000149
150
Guido van Rossume59214e1994-08-30 08:01:59 +0000151#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000152
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000153#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000154#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000155#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000156#include "pythread.h"
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000157#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000158
Tim Peters7f468f22004-10-11 02:40:51 +0000159int
160PyEval_ThreadsInitialized(void)
161{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000163}
164
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000165void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000166PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000167{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 if (gil_created())
169 return;
170 create_gil();
171 take_gil(PyThreadState_GET());
Eric Snow76d5abc2017-09-05 18:26:16 -0700172 _PyRuntime.ceval.pending.main_thread = PyThread_get_thread_ident();
173 if (!_PyRuntime.ceval.pending.lock)
174 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000175}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000176
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000177void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000178_PyEval_FiniThreads(void)
179{
180 if (!gil_created())
181 return;
182 destroy_gil();
183 assert(!gil_created());
184}
185
186void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000187PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000188{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 PyThreadState *tstate = PyThreadState_GET();
190 if (tstate == NULL)
191 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
192 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000193}
194
195void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000196PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 /* This function must succeed when the current thread state is NULL.
199 We therefore avoid PyThreadState_GET() which dumps a fatal error
200 in debug mode.
201 */
202 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
203 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000204}
205
206void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000207PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000208{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 if (tstate == NULL)
210 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
211 /* Check someone has called PyEval_InitThreads() to create the lock */
212 assert(gil_created());
213 take_gil(tstate);
214 if (PyThreadState_Swap(tstate) != NULL)
215 Py_FatalError(
216 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000217}
218
219void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000220PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000221{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 if (tstate == NULL)
223 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
224 if (PyThreadState_Swap(NULL) != tstate)
225 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
226 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000227}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000228
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200229/* This function is called from PyOS_AfterFork_Child to destroy all threads
230 * which are not running in the child process, and clear internal locks
231 * which might be held by those threads.
232 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000233
234void
235PyEval_ReInitThreads(void)
236{
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200237 PyThreadState *current_tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 if (!gil_created())
240 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 recreate_gil();
Eric Snow76d5abc2017-09-05 18:26:16 -0700242 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200243 take_gil(current_tstate);
Eric Snow76d5abc2017-09-05 18:26:16 -0700244 _PyRuntime.ceval.pending.main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000245
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200246 /* Destroy all threads except the current one */
247 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000248}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000249
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000250#endif /* WITH_THREAD */
251
252/* This function is used to signal that async exceptions are waiting to be
253 raised, therefore it is also useful in non-threaded builds. */
254
255void
256_PyEval_SignalAsyncExc(void)
257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000259}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000260
Guido van Rossumff4949e1992-08-05 19:58:53 +0000261/* Functions save_thread and restore_thread are always defined so
262 dynamically loaded modules needn't be compiled separately for use
263 with and without threads: */
264
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000265PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000266PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000267{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 PyThreadState *tstate = PyThreadState_Swap(NULL);
269 if (tstate == NULL)
270 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000271#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 if (gil_created())
273 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000274#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000276}
277
278void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000279PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000280{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 if (tstate == NULL)
282 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000283#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 if (gil_created()) {
285 int err = errno;
286 take_gil(tstate);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200287 /* _Py_Finalizing is protected by the GIL */
Eric Snow76d5abc2017-09-05 18:26:16 -0700288 if (_Py_IS_FINALIZING() && !_Py_CURRENTLY_FINALIZING(tstate)) {
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200289 drop_gil(tstate);
290 PyThread_exit_thread();
291 assert(0); /* unreachable */
292 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 errno = err;
294 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000295#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000297}
298
299
Guido van Rossuma9672091994-09-14 13:31:22 +0000300/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
301 signal handlers or Mac I/O completion routines) can schedule calls
302 to a function to be called synchronously.
303 The synchronous function is called with one void* argument.
304 It should return 0 for success or -1 for failure -- failure should
305 be accompanied by an exception.
306
307 If registry succeeds, the registry function returns 0; if it fails
308 (e.g. due to too many pending calls) it returns -1 (without setting
309 an exception condition).
310
311 Note that because registry may occur from within signal handlers,
312 or other asynchronous events, calling malloc() is unsafe!
313
314#ifdef WITH_THREAD
315 Any thread can schedule pending calls, but only the main thread
316 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000317 There is no facility to schedule calls to a particular thread, but
318 that should be easy to change, should that ever be required. In
319 that case, the static variables here should go into the python
320 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000321#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000322*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000323
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200324void
325_PyEval_SignalReceived(void)
326{
327 /* bpo-30703: Function called when the C signal handler of Python gets a
328 signal. We cannot queue a callback using Py_AddPendingCall() since
329 that function is not async-signal-safe. */
330 SIGNAL_PENDING_CALLS();
331}
332
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000333#ifdef WITH_THREAD
334
335/* The WITH_THREAD implementation is thread-safe. It allows
336 scheduling to be made from any thread, and even from an executing
337 callback.
338 */
339
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000340int
341Py_AddPendingCall(int (*func)(void *), void *arg)
342{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 int i, j, result=0;
Eric Snow76d5abc2017-09-05 18:26:16 -0700344 PyThread_type_lock lock = _PyRuntime.ceval.pending.lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 /* try a few times for the lock. Since this mechanism is used
347 * for signal handling (on the main thread), there is a (slim)
348 * chance that a signal is delivered on the same thread while we
349 * hold the lock during the Py_MakePendingCalls() function.
350 * This avoids a deadlock in that case.
351 * Note that signals can be delivered on any thread. In particular,
352 * on Windows, a SIGINT is delivered on a system-created worker
353 * thread.
354 * We also check for lock being NULL, in the unlikely case that
355 * this function is called before any bytecode evaluation takes place.
356 */
357 if (lock != NULL) {
358 for (i = 0; i<100; i++) {
359 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
360 break;
361 }
362 if (i == 100)
363 return -1;
364 }
365
Eric Snow76d5abc2017-09-05 18:26:16 -0700366 i = _PyRuntime.ceval.pending.last;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 j = (i + 1) % NPENDINGCALLS;
Eric Snow76d5abc2017-09-05 18:26:16 -0700368 if (j == _PyRuntime.ceval.pending.first) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 result = -1; /* Queue full */
370 } else {
Eric Snow76d5abc2017-09-05 18:26:16 -0700371 _PyRuntime.ceval.pending.calls[i].func = func;
372 _PyRuntime.ceval.pending.calls[i].arg = arg;
373 _PyRuntime.ceval.pending.last = j;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 }
375 /* signal main loop */
376 SIGNAL_PENDING_CALLS();
377 if (lock != NULL)
378 PyThread_release_lock(lock);
379 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000380}
381
382int
383Py_MakePendingCalls(void)
384{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200385 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 int i;
387 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000388
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200389 assert(PyGILState_Check());
390
Eric Snow76d5abc2017-09-05 18:26:16 -0700391 if (!_PyRuntime.ceval.pending.lock) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 /* initial allocation of the lock */
Eric Snow76d5abc2017-09-05 18:26:16 -0700393 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
394 if (_PyRuntime.ceval.pending.lock == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 return -1;
396 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 /* only service pending calls on main thread */
Eric Snow76d5abc2017-09-05 18:26:16 -0700399 if (_PyRuntime.ceval.pending.main_thread &&
400 PyThread_get_thread_ident() != _PyRuntime.ceval.pending.main_thread)
401 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 return 0;
Eric Snow76d5abc2017-09-05 18:26:16 -0700403 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200405 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200407 busy = 1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200408 /* unsignal before starting to call callbacks, so that any callback
409 added in-between re-signals */
410 UNSIGNAL_PENDING_CALLS();
411
412 /* Python signal handler doesn't really queue a callback: it only signals
413 that a signal was received, see _PyEval_SignalReceived(). */
414 if (PyErr_CheckSignals() < 0) {
415 goto error;
416 }
417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 /* perform a bounded number of calls, in case of recursion */
419 for (i=0; i<NPENDINGCALLS; i++) {
420 int j;
421 int (*func)(void *);
422 void *arg = NULL;
423
424 /* pop one item off the queue while holding the lock */
Eric Snow76d5abc2017-09-05 18:26:16 -0700425 PyThread_acquire_lock(_PyRuntime.ceval.pending.lock, WAIT_LOCK);
426 j = _PyRuntime.ceval.pending.first;
427 if (j == _PyRuntime.ceval.pending.last) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 func = NULL; /* Queue empty */
429 } else {
Eric Snow76d5abc2017-09-05 18:26:16 -0700430 func = _PyRuntime.ceval.pending.calls[j].func;
431 arg = _PyRuntime.ceval.pending.calls[j].arg;
432 _PyRuntime.ceval.pending.first = (j + 1) % NPENDINGCALLS;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 }
Eric Snow76d5abc2017-09-05 18:26:16 -0700434 PyThread_release_lock(_PyRuntime.ceval.pending.lock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 /* having released the lock, perform the callback */
436 if (func == NULL)
437 break;
438 r = func(arg);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200439 if (r) {
440 goto error;
441 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200443
Charles-François Natalif23339a2011-07-23 18:15:43 +0200444 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 return r;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200446
447error:
448 busy = 0;
449 SIGNAL_PENDING_CALLS(); /* We're not done yet */
450 return -1;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000451}
452
453#else /* if ! defined WITH_THREAD */
454
455/*
456 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
457 This code is used for signal handling in python that isn't built
458 with WITH_THREAD.
459 Don't use this implementation when Py_AddPendingCalls() can happen
460 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461
Guido van Rossuma9672091994-09-14 13:31:22 +0000462 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000463 (1) nested asynchronous calls to Py_AddPendingCall()
464 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000466 (1) is very unlikely because typically signal delivery
467 is blocked during signal handling. So it should be impossible.
468 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000469 The current code is safe against (2), but not against (1).
470 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000471 thread is present, interrupted by signals, and that the critical
472 section is protected with the "busy" variable. On Windows, which
473 delivers SIGINT on a system thread, this does not hold and therefore
474 Windows really shouldn't use this version.
475 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000476*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000477
Guido van Rossuma9672091994-09-14 13:31:22 +0000478int
Thomas Wouters334fb892000-07-25 12:56:38 +0000479Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000480{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 static volatile int busy = 0;
482 int i, j;
483 /* XXX Begin critical section */
484 if (busy)
485 return -1;
486 busy = 1;
Eric Snow76d5abc2017-09-05 18:26:16 -0700487 i = _PyRuntime.ceval.pending.last;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 j = (i + 1) % NPENDINGCALLS;
Eric Snow76d5abc2017-09-05 18:26:16 -0700489 if (j == _PyRuntime.ceval.pending.first) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 busy = 0;
491 return -1; /* Queue full */
492 }
Eric Snow76d5abc2017-09-05 18:26:16 -0700493 _PyRuntime.ceval.pending.calls[i].func = func;
494 _PyRuntime.ceval.pending.calls[i].arg = arg;
495 _PyRuntime.ceval.pending.last = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 SIGNAL_PENDING_CALLS();
498 busy = 0;
499 /* XXX End critical section */
500 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000501}
502
Guido van Rossum180d7b41994-09-29 09:45:57 +0000503int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000504Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000505{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 static int busy = 0;
507 if (busy)
508 return 0;
509 busy = 1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200510
511 /* unsignal before starting to call callbacks, so that any callback
512 added in-between re-signals */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 UNSIGNAL_PENDING_CALLS();
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200514 /* Python signal handler doesn't really queue a callback: it only signals
515 that a signal was received, see _PyEval_SignalReceived(). */
516 if (PyErr_CheckSignals() < 0) {
517 goto error;
518 }
519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 for (;;) {
521 int i;
522 int (*func)(void *);
523 void *arg;
Eric Snow76d5abc2017-09-05 18:26:16 -0700524 i = _PyRuntime.ceval.pending.first;
525 if (i == _PyRuntime.ceval.pending.last)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 break; /* Queue empty */
Eric Snow76d5abc2017-09-05 18:26:16 -0700527 func = _PyRuntime.ceval.pending.calls[i].func;
528 arg = _PyRuntime.ceval.pending.calls[i].arg;
529 _PyRuntime.ceval.pending.first = (i + 1) % NPENDINGCALLS;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 if (func(arg) < 0) {
Masayuki Yamamoto0c311632017-07-05 17:39:17 +0900531 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 }
533 }
534 busy = 0;
535 return 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200536
537error:
538 busy = 0;
539 SIGNAL_PENDING_CALLS(); /* We're not done yet */
540 return -1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000541}
542
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000543#endif /* WITH_THREAD */
544
Guido van Rossuma9672091994-09-14 13:31:22 +0000545
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000546/* The interpreter's recursion limit */
547
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000548#ifndef Py_DEFAULT_RECURSION_LIMIT
549#define Py_DEFAULT_RECURSION_LIMIT 1000
550#endif
Eric Snow76d5abc2017-09-05 18:26:16 -0700551
552void
553_PyEval_Initialize(struct _ceval_runtime_state *state)
554{
555 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
556 state->check_recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
557 _gil_initialize(&state->gil);
558}
559
560int
561_PyEval_CheckRecursionLimit(void)
562{
563 return _PyRuntime.ceval.check_recursion_limit;
564}
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000565
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000566int
567Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000568{
Eric Snow76d5abc2017-09-05 18:26:16 -0700569 return _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000570}
571
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000572void
573Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000574{
Eric Snow76d5abc2017-09-05 18:26:16 -0700575 _PyRuntime.ceval.recursion_limit = new_limit;
576 _PyRuntime.ceval.check_recursion_limit = _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000577}
578
Armin Rigo2b3eb402003-10-28 12:05:48 +0000579/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
580 if the recursion_depth reaches _Py_CheckRecursionLimit.
581 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
582 to guarantee that _Py_CheckRecursiveCall() is regularly called.
583 Without USE_STACKCHECK, there is no need for this. */
584int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300585_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000586{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 PyThreadState *tstate = PyThreadState_GET();
Eric Snow76d5abc2017-09-05 18:26:16 -0700588 int recursion_limit = _PyRuntime.ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000589
590#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 if (PyOS_CheckStack()) {
592 --tstate->recursion_depth;
593 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
594 return -1;
595 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000596#endif
Eric Snow76d5abc2017-09-05 18:26:16 -0700597 _PyRuntime.ceval.check_recursion_limit = recursion_limit;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 if (tstate->recursion_critical)
599 /* Somebody asked that we don't check for recursion. */
600 return 0;
601 if (tstate->overflowed) {
602 if (tstate->recursion_depth > recursion_limit + 50) {
603 /* Overflowing while handling an overflow. Give up. */
604 Py_FatalError("Cannot recover from stack overflow.");
605 }
606 return 0;
607 }
608 if (tstate->recursion_depth > recursion_limit) {
609 --tstate->recursion_depth;
610 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400611 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 "maximum recursion depth exceeded%s",
613 where);
614 return -1;
615 }
616 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000617}
618
Guido van Rossum374a9221991-04-04 10:40:29 +0000619/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000620enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000621 WHY_NOT = 0x0001, /* No error */
622 WHY_EXCEPTION = 0x0002, /* Exception occurred */
Stefan Krahb7e10102010-06-23 18:42:39 +0000623 WHY_RETURN = 0x0008, /* 'return' statement */
624 WHY_BREAK = 0x0010, /* 'break' statement */
625 WHY_CONTINUE = 0x0020, /* 'continue' statement */
626 WHY_YIELD = 0x0040, /* 'yield' operator */
627 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000628};
Guido van Rossum374a9221991-04-04 10:40:29 +0000629
Benjamin Peterson87880242011-07-03 16:48:31 -0500630static void save_exc_state(PyThreadState *, PyFrameObject *);
631static void swap_exc_state(PyThreadState *, PyFrameObject *);
632static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400633static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000634static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000635
Eric Snow76d5abc2017-09-05 18:26:16 -0700636#define _Py_TracingPossible _PyRuntime.ceval.tracing_possible
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000637
Guido van Rossum374a9221991-04-04 10:40:29 +0000638
Guido van Rossumb209a111997-04-29 18:18:01 +0000639PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000640PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000641{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 return PyEval_EvalCodeEx(co,
643 globals, locals,
644 (PyObject **)NULL, 0,
645 (PyObject **)NULL, 0,
646 (PyObject **)NULL, 0,
647 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000648}
649
650
651/* Interpreter main loop */
652
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000653PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000654PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 /* This is for backward compatibility with extension modules that
656 used this API; core interpreter code should call
657 PyEval_EvalFrameEx() */
658 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000659}
660
661PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000662PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000663{
Brett Cannon3cebf932016-09-05 15:33:46 -0700664 PyThreadState *tstate = PyThreadState_GET();
665 return tstate->interp->eval_frame(f, throwflag);
666}
667
Victor Stinnerc6944e72016-11-11 02:13:35 +0100668PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700669_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
670{
Guido van Rossum950361c1997-01-24 13:49:28 +0000671#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000673#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200674 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300675 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200676 int opcode; /* Current opcode */
677 int oparg; /* Current opcode argument, if any */
678 enum why_code why; /* Reason for block stack unwind */
679 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 PyObject *retval = NULL; /* Return value */
681 PyThreadState *tstate = PyThreadState_GET();
682 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 is true when the line being executed has changed. The
689 initial values are such as to make this false the first
690 time it is tested. */
691 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000692
Serhiy Storchakaab874002016-09-11 13:48:15 +0300693 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 PyObject *names;
695 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000696
Brett Cannon368b4b72012-04-02 12:17:59 -0400697#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200698 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400699#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200700
Antoine Pitroub52ec782009-01-25 16:34:23 +0000701/* Computed GOTOs, or
702 the-optimization-commonly-but-improperly-known-as-"threaded code"
703 using gcc's labels-as-values extension
704 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
705
706 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000708 combined with a lookup table of jump addresses. However, since the
709 indirect jump instruction is shared by all opcodes, the CPU will have a
710 hard time making the right prediction for where to jump next (actually,
711 it will be always wrong except in the uncommon case of a sequence of
712 several identical opcodes).
713
714 "Threaded code" in contrast, uses an explicit jump table and an explicit
715 indirect jump instruction at the end of each opcode. Since the jump
716 instruction is at a different address for each opcode, the CPU will make a
717 separate prediction for each of these instructions, which is equivalent to
718 predicting the second opcode of each opcode pair. These predictions have
719 a much better chance to turn out valid, especially in small bytecode loops.
720
721 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000723 and potentially many more instructions (depending on the pipeline width).
724 A correctly predicted branch, however, is nearly free.
725
726 At the time of this writing, the "threaded code" version is up to 15-20%
727 faster than the normal "switch" version, depending on the compiler and the
728 CPU architecture.
729
730 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
731 because it would render the measurements invalid.
732
733
734 NOTE: care must be taken that the compiler doesn't try to "optimize" the
735 indirect jumps by sharing them between all opcodes. Such optimizations
736 can be disabled on gcc by using the -fno-gcse flag (or possibly
737 -fno-crossjumping).
738*/
739
Antoine Pitrou042b1282010-08-13 21:15:58 +0000740#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000741#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000742#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000743#endif
744
Antoine Pitrou042b1282010-08-13 21:15:58 +0000745#ifdef HAVE_COMPUTED_GOTOS
746 #ifndef USE_COMPUTED_GOTOS
747 #define USE_COMPUTED_GOTOS 1
748 #endif
749#else
750 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
751 #error "Computed gotos are not supported on this compiler."
752 #endif
753 #undef USE_COMPUTED_GOTOS
754 #define USE_COMPUTED_GOTOS 0
755#endif
756
757#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000758/* Import the static jump table */
759#include "opcode_targets.h"
760
Antoine Pitroub52ec782009-01-25 16:34:23 +0000761#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 TARGET_##op: \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000764
Antoine Pitroub52ec782009-01-25 16:34:23 +0000765#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 { \
Eric Snow76d5abc2017-09-05 18:26:16 -0700767 if (!_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 FAST_DISPATCH(); \
769 } \
770 continue; \
771 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000772
773#ifdef LLTRACE
774#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700776 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300778 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300779 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 } \
781 goto fast_next_opcode; \
782 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000783#else
784#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700786 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300788 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300789 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 } \
791 goto fast_next_opcode; \
792 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000793#endif
794
795#else
796#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 case op:
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300798
Antoine Pitroub52ec782009-01-25 16:34:23 +0000799#define DISPATCH() continue
800#define FAST_DISPATCH() goto fast_next_opcode
801#endif
802
803
Neal Norwitza81d2202002-07-14 00:27:26 +0000804/* Tuple access macros */
805
806#ifndef Py_DEBUG
807#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
808#else
809#define GETITEM(v, i) PyTuple_GetItem((v), (i))
810#endif
811
Guido van Rossum374a9221991-04-04 10:40:29 +0000812/* Code access macros */
813
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300814/* The integer overflow is checked by an assertion below. */
Eric Snow76d5abc2017-09-05 18:26:16 -0700815#define INSTR_OFFSET() \
816 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300817#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300818 _Py_CODEUNIT word = *next_instr; \
819 opcode = _Py_OPCODE(word); \
820 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300821 next_instr++; \
822 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300823#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
824#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000825
Raymond Hettingerf606f872003-03-16 03:11:04 +0000826/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 Some opcodes tend to come in pairs thus making it possible to
828 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300829 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 Verifying the prediction costs a single high-speed test of a register
832 variable against a constant. If the pairing was good, then the
833 processor's own internal branch predication has a high likelihood of
834 success, resulting in a nearly zero-overhead transition to the
835 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300836 including its unpredictable switch-case branch. Combined with the
837 processor's internal branch prediction, a successful PREDICT has the
838 effect of making the two opcodes run as if they were a single new opcode
839 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000840
Georg Brandl86b2fb92008-07-16 03:43:04 +0000841 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 predictions turned-on and interpret the results as if some opcodes
843 had been combined or turn-off predictions so that the opcode frequency
844 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000845
846 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 the CPU to record separate branch prediction information for each
848 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000849
Raymond Hettingerf606f872003-03-16 03:11:04 +0000850*/
851
Antoine Pitrou042b1282010-08-13 21:15:58 +0000852#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000854#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300855#define PREDICT(op) \
856 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300857 _Py_CODEUNIT word = *next_instr; \
858 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300859 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300860 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300861 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300862 goto PRED_##op; \
863 } \
864 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000865#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300866#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000867
Raymond Hettingerf606f872003-03-16 03:11:04 +0000868
Guido van Rossum374a9221991-04-04 10:40:29 +0000869/* Stack manipulation macros */
870
Martin v. Löwis18e16552006-02-15 17:27:45 +0000871/* The stack can grow at most MAXINT deep, as co_nlocals and
872 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000873#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
874#define EMPTY() (STACK_LEVEL() == 0)
875#define TOP() (stack_pointer[-1])
876#define SECOND() (stack_pointer[-2])
877#define THIRD() (stack_pointer[-3])
878#define FOURTH() (stack_pointer[-4])
879#define PEEK(n) (stack_pointer[-(n)])
880#define SET_TOP(v) (stack_pointer[-1] = (v))
881#define SET_SECOND(v) (stack_pointer[-2] = (v))
882#define SET_THIRD(v) (stack_pointer[-3] = (v))
883#define SET_FOURTH(v) (stack_pointer[-4] = (v))
884#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
885#define BASIC_STACKADJ(n) (stack_pointer += n)
886#define BASIC_PUSH(v) (*stack_pointer++ = (v))
887#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000888
Guido van Rossum96a42c81992-01-12 02:29:51 +0000889#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000891 lltrace && prtrace(TOP(), "push")); \
892 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000894 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000896 lltrace && prtrace(TOP(), "stackadj")); \
897 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +0000898#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000899 prtrace((STACK_POINTER)[-1], "ext_pop")), \
900 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000901#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000902#define PUSH(v) BASIC_PUSH(v)
903#define POP() BASIC_POP()
904#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000905#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000906#endif
907
Guido van Rossum681d79a1995-07-18 14:51:37 +0000908/* Local variable macros */
909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000911
912/* The SETLOCAL() macro must not DECREF the local variable in-place and
913 then store the new value; it must copy the old value to a temporary
914 value, then store the new value, and then DECREF the temporary value.
915 This is because it is possible that during the DECREF the frame is
916 accessed by other code (e.g. a __del__ method or gc.collect()) and the
917 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000919 GETLOCAL(i) = value; \
920 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000921
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000922
923#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 while (STACK_LEVEL() > (b)->b_level) { \
925 PyObject *v = POP(); \
926 Py_XDECREF(v); \
927 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000928
929#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300930 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 PyObject *type, *value, *traceback; \
932 assert(STACK_LEVEL() >= (b)->b_level + 3); \
933 while (STACK_LEVEL() > (b)->b_level + 3) { \
934 value = POP(); \
935 Py_XDECREF(value); \
936 } \
937 type = tstate->exc_type; \
938 value = tstate->exc_value; \
939 traceback = tstate->exc_traceback; \
940 tstate->exc_type = POP(); \
941 tstate->exc_value = POP(); \
942 tstate->exc_traceback = POP(); \
943 Py_XDECREF(type); \
944 Py_XDECREF(value); \
945 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300946 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000947
Guido van Rossuma027efa1997-05-05 20:56:21 +0000948/* Start of code */
949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 /* push frame */
951 if (Py_EnterRecursiveCall(""))
952 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 if (tstate->use_tracing) {
957 if (tstate->c_tracefunc != NULL) {
958 /* tstate->c_tracefunc, if defined, is a
959 function that will be called on *every* entry
960 to a code block. Its return value, if not
961 None, is a function that will be called at
962 the start of each executed line of code.
963 (Actually, the function must return itself
964 in order to continue tracing.) The trace
965 functions are called with three arguments:
966 a pointer to the current frame, a string
967 indicating why the function is called, and
968 an argument which depends on the situation.
969 The global trace function is also called
970 whenever an exception is detected. */
971 if (call_trace_protected(tstate->c_tracefunc,
972 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100973 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 /* Trace function raised an error */
975 goto exit_eval_frame;
976 }
977 }
978 if (tstate->c_profilefunc != NULL) {
979 /* Similar for c_profilefunc, except it needn't
980 return itself and isn't called for "line" events */
981 if (call_trace_protected(tstate->c_profilefunc,
982 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100983 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 /* Profile function raised an error */
985 goto exit_eval_frame;
986 }
987 }
988 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000989
Łukasz Langaa785c872016-09-09 17:37:37 -0700990 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
991 dtrace_function_entry(f);
992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 co = f->f_code;
994 names = co->co_names;
995 consts = co->co_consts;
996 fastlocals = f->f_localsplus;
997 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300998 assert(PyBytes_Check(co->co_code));
999 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001000 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1001 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1002 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001003 /*
1004 f->f_lasti refers to the index of the last instruction,
1005 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001006
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001007 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001008 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 When the PREDICT() macros are enabled, some opcode pairs follow in
1011 direct succession without updating f->f_lasti. A successful
1012 prediction effectively links the two codes together as if they
1013 were a single new opcode; accordingly,f->f_lasti will point to
1014 the first code in the pair (for instance, GET_ITER followed by
1015 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001016 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001018 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001019 next_instr = first_instr;
1020 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001021 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1022 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001023 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 stack_pointer = f->f_stacktop;
1025 assert(stack_pointer != NULL);
1026 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001027 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001028
Yury Selivanoveb636452016-09-08 22:01:51 -07001029 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01001030 if (!throwflag && f->f_exc_type != NULL && f->f_exc_type != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 /* We were in an except handler when we left,
1032 restore the exception state which was put aside
1033 (see YIELD_VALUE). */
Benjamin Peterson87880242011-07-03 16:48:31 -05001034 swap_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 }
Benjamin Peterson87880242011-07-03 16:48:31 -05001036 else
1037 save_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001039
Tim Peters5ca576e2001-06-18 22:08:13 +00001040#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001041 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001042#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 why = WHY_NOT;
Guido van Rossumac7be682001-01-17 15:42:30 +00001045
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001046 if (throwflag) /* support for generator.throw() */
1047 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001048
Victor Stinnerace47d72013-07-18 01:41:08 +02001049#ifdef Py_DEBUG
1050 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001051 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001052 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +02001053 assert(!PyErr_Occurred());
1054#endif
1055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1058 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +02001059 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 /* Do periodic things. Doing this every time through
1062 the loop would add too much overhead, so we do it
1063 only every Nth instruction. We also do it if
1064 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1065 event needs attention (e.g. a signal handler or
1066 async I/O handler); see Py_AddPendingCall() and
1067 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001068
Eric Snow76d5abc2017-09-05 18:26:16 -07001069 if (_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) {
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001070 if (_Py_OPCODE(*next_instr) == SETUP_FINALLY ||
1071 _Py_OPCODE(*next_instr) == YIELD_FROM) {
1072 /* Two cases where we skip running signal handlers and other
1073 pending calls:
1074 - If we're about to enter the try: of a try/finally (not
1075 *very* useful, but might help in some cases and it's
1076 traditional)
1077 - If we're resuming a chain of nested 'yield from' or
1078 'await' calls, then each frame is parked with YIELD_FROM
1079 as its next opcode. If the user hit control-C we want to
1080 wait until we've reached the innermost frame before
1081 running the signal handler and raising KeyboardInterrupt
1082 (see bpo-30039).
1083 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 goto fast_next_opcode;
1085 }
Eric Snow76d5abc2017-09-05 18:26:16 -07001086 if (_Py_atomic_load_relaxed(
1087 &_PyRuntime.ceval.pending.calls_to_do))
1088 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001089 if (Py_MakePendingCalls() < 0)
1090 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001092#ifdef WITH_THREAD
Eric Snow76d5abc2017-09-05 18:26:16 -07001093 if (_Py_atomic_load_relaxed(
1094 &_PyRuntime.ceval.gil_drop_request))
1095 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 /* Give another thread a chance */
1097 if (PyThreadState_Swap(NULL) != tstate)
1098 Py_FatalError("ceval: tstate mix-up");
1099 drop_gil(tstate);
1100
1101 /* Other threads may run now */
1102
1103 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001104
1105 /* Check if we should make a quick exit. */
Eric Snow76d5abc2017-09-05 18:26:16 -07001106 if (_Py_IS_FINALIZING() &&
1107 !_Py_CURRENTLY_FINALIZING(tstate))
1108 {
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001109 drop_gil(tstate);
1110 PyThread_exit_thread();
1111 }
1112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 if (PyThreadState_Swap(tstate) != NULL)
1114 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001116#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 /* Check for asynchronous exceptions. */
1118 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001119 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 tstate->async_exc = NULL;
1121 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001122 PyErr_SetNone(exc);
1123 Py_DECREF(exc);
1124 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 }
1126 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 fast_next_opcode:
1129 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001130
Łukasz Langaa785c872016-09-09 17:37:37 -07001131 if (PyDTrace_LINE_ENABLED())
1132 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001137 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001138 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 /* see maybe_call_line_trace
1140 for expository comments */
1141 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 err = maybe_call_line_trace(tstate->c_tracefunc,
1144 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001145 tstate, f,
1146 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 /* Reload possibly changed frame fields */
1148 JUMPTO(f->f_lasti);
1149 if (f->f_stacktop != NULL) {
1150 stack_pointer = f->f_stacktop;
1151 f->f_stacktop = NULL;
1152 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001153 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001155 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001159
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001160 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001161 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001162#ifdef DYNAMIC_EXECUTION_PROFILE
1163#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 dxpairs[lastopcode][opcode]++;
1165 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001166#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001168#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001169
Guido van Rossum96a42c81992-01-12 02:29:51 +00001170#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 if (lltrace) {
1174 if (HAS_ARG(opcode)) {
1175 printf("%d: %d, %d\n",
1176 f->f_lasti, opcode, oparg);
1177 }
1178 else {
1179 printf("%d: %d\n",
1180 f->f_lasti, opcode);
1181 }
1182 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001183#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001186
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 /* BEWARE!
1188 It is essential that any operation that fails sets either
1189 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1190 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 TARGET(NOP)
1193 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001194
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001195 TARGET(LOAD_FAST) {
1196 PyObject *value = GETLOCAL(oparg);
1197 if (value == NULL) {
1198 format_exc_check_arg(PyExc_UnboundLocalError,
1199 UNBOUNDLOCAL_ERROR_MSG,
1200 PyTuple_GetItem(co->co_varnames, oparg));
1201 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001203 Py_INCREF(value);
1204 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001206 }
1207
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001208 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001209 TARGET(LOAD_CONST) {
1210 PyObject *value = GETITEM(consts, oparg);
1211 Py_INCREF(value);
1212 PUSH(value);
1213 FAST_DISPATCH();
1214 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001215
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001216 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001217 TARGET(STORE_FAST) {
1218 PyObject *value = POP();
1219 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001221 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001222
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001223 TARGET(POP_TOP) {
1224 PyObject *value = POP();
1225 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001227 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001228
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001229 TARGET(ROT_TWO) {
1230 PyObject *top = TOP();
1231 PyObject *second = SECOND();
1232 SET_TOP(second);
1233 SET_SECOND(top);
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_THREE) {
1238 PyObject *top = TOP();
1239 PyObject *second = SECOND();
1240 PyObject *third = THIRD();
1241 SET_TOP(second);
1242 SET_SECOND(third);
1243 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001245 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001246
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001247 TARGET(DUP_TOP) {
1248 PyObject *top = TOP();
1249 Py_INCREF(top);
1250 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001252 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001253
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001254 TARGET(DUP_TOP_TWO) {
1255 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001256 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001257 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001258 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001259 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001260 SET_TOP(top);
1261 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001262 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001263 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001264
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001265 TARGET(UNARY_POSITIVE) {
1266 PyObject *value = TOP();
1267 PyObject *res = PyNumber_Positive(value);
1268 Py_DECREF(value);
1269 SET_TOP(res);
1270 if (res == NULL)
1271 goto error;
1272 DISPATCH();
1273 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001274
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001275 TARGET(UNARY_NEGATIVE) {
1276 PyObject *value = TOP();
1277 PyObject *res = PyNumber_Negative(value);
1278 Py_DECREF(value);
1279 SET_TOP(res);
1280 if (res == NULL)
1281 goto error;
1282 DISPATCH();
1283 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001284
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001285 TARGET(UNARY_NOT) {
1286 PyObject *value = TOP();
1287 int err = PyObject_IsTrue(value);
1288 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 if (err == 0) {
1290 Py_INCREF(Py_True);
1291 SET_TOP(Py_True);
1292 DISPATCH();
1293 }
1294 else if (err > 0) {
1295 Py_INCREF(Py_False);
1296 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 DISPATCH();
1298 }
1299 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001300 goto error;
1301 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001302
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001303 TARGET(UNARY_INVERT) {
1304 PyObject *value = TOP();
1305 PyObject *res = PyNumber_Invert(value);
1306 Py_DECREF(value);
1307 SET_TOP(res);
1308 if (res == NULL)
1309 goto error;
1310 DISPATCH();
1311 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001312
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001313 TARGET(BINARY_POWER) {
1314 PyObject *exp = POP();
1315 PyObject *base = TOP();
1316 PyObject *res = PyNumber_Power(base, exp, Py_None);
1317 Py_DECREF(base);
1318 Py_DECREF(exp);
1319 SET_TOP(res);
1320 if (res == NULL)
1321 goto error;
1322 DISPATCH();
1323 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001324
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001325 TARGET(BINARY_MULTIPLY) {
1326 PyObject *right = POP();
1327 PyObject *left = TOP();
1328 PyObject *res = PyNumber_Multiply(left, right);
1329 Py_DECREF(left);
1330 Py_DECREF(right);
1331 SET_TOP(res);
1332 if (res == NULL)
1333 goto error;
1334 DISPATCH();
1335 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001336
Benjamin Petersond51374e2014-04-09 23:55:56 -04001337 TARGET(BINARY_MATRIX_MULTIPLY) {
1338 PyObject *right = POP();
1339 PyObject *left = TOP();
1340 PyObject *res = PyNumber_MatrixMultiply(left, right);
1341 Py_DECREF(left);
1342 Py_DECREF(right);
1343 SET_TOP(res);
1344 if (res == NULL)
1345 goto error;
1346 DISPATCH();
1347 }
1348
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001349 TARGET(BINARY_TRUE_DIVIDE) {
1350 PyObject *divisor = POP();
1351 PyObject *dividend = TOP();
1352 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1353 Py_DECREF(dividend);
1354 Py_DECREF(divisor);
1355 SET_TOP(quotient);
1356 if (quotient == NULL)
1357 goto error;
1358 DISPATCH();
1359 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001360
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001361 TARGET(BINARY_FLOOR_DIVIDE) {
1362 PyObject *divisor = POP();
1363 PyObject *dividend = TOP();
1364 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1365 Py_DECREF(dividend);
1366 Py_DECREF(divisor);
1367 SET_TOP(quotient);
1368 if (quotient == NULL)
1369 goto error;
1370 DISPATCH();
1371 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001372
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001373 TARGET(BINARY_MODULO) {
1374 PyObject *divisor = POP();
1375 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001376 PyObject *res;
1377 if (PyUnicode_CheckExact(dividend) && (
1378 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1379 // fast path; string formatting, but not if the RHS is a str subclass
1380 // (see issue28598)
1381 res = PyUnicode_Format(dividend, divisor);
1382 } else {
1383 res = PyNumber_Remainder(dividend, divisor);
1384 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001385 Py_DECREF(divisor);
1386 Py_DECREF(dividend);
1387 SET_TOP(res);
1388 if (res == NULL)
1389 goto error;
1390 DISPATCH();
1391 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001392
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001393 TARGET(BINARY_ADD) {
1394 PyObject *right = POP();
1395 PyObject *left = TOP();
1396 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001397 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1398 CPython using bytecode, it is simply worthless.
1399 See http://bugs.python.org/issue21955 and
1400 http://bugs.python.org/issue10044 for the discussion. In short,
1401 no patch shown any impact on a realistic benchmark, only a minor
1402 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001403 if (PyUnicode_CheckExact(left) &&
1404 PyUnicode_CheckExact(right)) {
1405 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001406 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001407 }
1408 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001409 sum = PyNumber_Add(left, right);
1410 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001411 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001412 Py_DECREF(right);
1413 SET_TOP(sum);
1414 if (sum == NULL)
1415 goto error;
1416 DISPATCH();
1417 }
1418
1419 TARGET(BINARY_SUBTRACT) {
1420 PyObject *right = POP();
1421 PyObject *left = TOP();
1422 PyObject *diff = PyNumber_Subtract(left, right);
1423 Py_DECREF(right);
1424 Py_DECREF(left);
1425 SET_TOP(diff);
1426 if (diff == NULL)
1427 goto error;
1428 DISPATCH();
1429 }
1430
1431 TARGET(BINARY_SUBSCR) {
1432 PyObject *sub = POP();
1433 PyObject *container = TOP();
1434 PyObject *res = PyObject_GetItem(container, sub);
1435 Py_DECREF(container);
1436 Py_DECREF(sub);
1437 SET_TOP(res);
1438 if (res == NULL)
1439 goto error;
1440 DISPATCH();
1441 }
1442
1443 TARGET(BINARY_LSHIFT) {
1444 PyObject *right = POP();
1445 PyObject *left = TOP();
1446 PyObject *res = PyNumber_Lshift(left, right);
1447 Py_DECREF(left);
1448 Py_DECREF(right);
1449 SET_TOP(res);
1450 if (res == NULL)
1451 goto error;
1452 DISPATCH();
1453 }
1454
1455 TARGET(BINARY_RSHIFT) {
1456 PyObject *right = POP();
1457 PyObject *left = TOP();
1458 PyObject *res = PyNumber_Rshift(left, right);
1459 Py_DECREF(left);
1460 Py_DECREF(right);
1461 SET_TOP(res);
1462 if (res == NULL)
1463 goto error;
1464 DISPATCH();
1465 }
1466
1467 TARGET(BINARY_AND) {
1468 PyObject *right = POP();
1469 PyObject *left = TOP();
1470 PyObject *res = PyNumber_And(left, right);
1471 Py_DECREF(left);
1472 Py_DECREF(right);
1473 SET_TOP(res);
1474 if (res == NULL)
1475 goto error;
1476 DISPATCH();
1477 }
1478
1479 TARGET(BINARY_XOR) {
1480 PyObject *right = POP();
1481 PyObject *left = TOP();
1482 PyObject *res = PyNumber_Xor(left, right);
1483 Py_DECREF(left);
1484 Py_DECREF(right);
1485 SET_TOP(res);
1486 if (res == NULL)
1487 goto error;
1488 DISPATCH();
1489 }
1490
1491 TARGET(BINARY_OR) {
1492 PyObject *right = POP();
1493 PyObject *left = TOP();
1494 PyObject *res = PyNumber_Or(left, right);
1495 Py_DECREF(left);
1496 Py_DECREF(right);
1497 SET_TOP(res);
1498 if (res == NULL)
1499 goto error;
1500 DISPATCH();
1501 }
1502
1503 TARGET(LIST_APPEND) {
1504 PyObject *v = POP();
1505 PyObject *list = PEEK(oparg);
1506 int err;
1507 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001508 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001509 if (err != 0)
1510 goto error;
1511 PREDICT(JUMP_ABSOLUTE);
1512 DISPATCH();
1513 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001514
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001515 TARGET(SET_ADD) {
1516 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001517 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001518 int err;
1519 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001521 if (err != 0)
1522 goto error;
1523 PREDICT(JUMP_ABSOLUTE);
1524 DISPATCH();
1525 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001526
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001527 TARGET(INPLACE_POWER) {
1528 PyObject *exp = POP();
1529 PyObject *base = TOP();
1530 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1531 Py_DECREF(base);
1532 Py_DECREF(exp);
1533 SET_TOP(res);
1534 if (res == NULL)
1535 goto error;
1536 DISPATCH();
1537 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001538
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001539 TARGET(INPLACE_MULTIPLY) {
1540 PyObject *right = POP();
1541 PyObject *left = TOP();
1542 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1543 Py_DECREF(left);
1544 Py_DECREF(right);
1545 SET_TOP(res);
1546 if (res == NULL)
1547 goto error;
1548 DISPATCH();
1549 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001550
Benjamin Petersond51374e2014-04-09 23:55:56 -04001551 TARGET(INPLACE_MATRIX_MULTIPLY) {
1552 PyObject *right = POP();
1553 PyObject *left = TOP();
1554 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1555 Py_DECREF(left);
1556 Py_DECREF(right);
1557 SET_TOP(res);
1558 if (res == NULL)
1559 goto error;
1560 DISPATCH();
1561 }
1562
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001563 TARGET(INPLACE_TRUE_DIVIDE) {
1564 PyObject *divisor = POP();
1565 PyObject *dividend = TOP();
1566 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1567 Py_DECREF(dividend);
1568 Py_DECREF(divisor);
1569 SET_TOP(quotient);
1570 if (quotient == NULL)
1571 goto error;
1572 DISPATCH();
1573 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001574
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001575 TARGET(INPLACE_FLOOR_DIVIDE) {
1576 PyObject *divisor = POP();
1577 PyObject *dividend = TOP();
1578 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1579 Py_DECREF(dividend);
1580 Py_DECREF(divisor);
1581 SET_TOP(quotient);
1582 if (quotient == NULL)
1583 goto error;
1584 DISPATCH();
1585 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001586
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001587 TARGET(INPLACE_MODULO) {
1588 PyObject *right = POP();
1589 PyObject *left = TOP();
1590 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1591 Py_DECREF(left);
1592 Py_DECREF(right);
1593 SET_TOP(mod);
1594 if (mod == NULL)
1595 goto error;
1596 DISPATCH();
1597 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001598
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001599 TARGET(INPLACE_ADD) {
1600 PyObject *right = POP();
1601 PyObject *left = TOP();
1602 PyObject *sum;
1603 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1604 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001605 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001606 }
1607 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001608 sum = PyNumber_InPlaceAdd(left, right);
1609 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001610 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001611 Py_DECREF(right);
1612 SET_TOP(sum);
1613 if (sum == NULL)
1614 goto error;
1615 DISPATCH();
1616 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001617
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001618 TARGET(INPLACE_SUBTRACT) {
1619 PyObject *right = POP();
1620 PyObject *left = TOP();
1621 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1622 Py_DECREF(left);
1623 Py_DECREF(right);
1624 SET_TOP(diff);
1625 if (diff == NULL)
1626 goto error;
1627 DISPATCH();
1628 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001629
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001630 TARGET(INPLACE_LSHIFT) {
1631 PyObject *right = POP();
1632 PyObject *left = TOP();
1633 PyObject *res = PyNumber_InPlaceLshift(left, right);
1634 Py_DECREF(left);
1635 Py_DECREF(right);
1636 SET_TOP(res);
1637 if (res == NULL)
1638 goto error;
1639 DISPATCH();
1640 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001641
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001642 TARGET(INPLACE_RSHIFT) {
1643 PyObject *right = POP();
1644 PyObject *left = TOP();
1645 PyObject *res = PyNumber_InPlaceRshift(left, right);
1646 Py_DECREF(left);
1647 Py_DECREF(right);
1648 SET_TOP(res);
1649 if (res == NULL)
1650 goto error;
1651 DISPATCH();
1652 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001653
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001654 TARGET(INPLACE_AND) {
1655 PyObject *right = POP();
1656 PyObject *left = TOP();
1657 PyObject *res = PyNumber_InPlaceAnd(left, right);
1658 Py_DECREF(left);
1659 Py_DECREF(right);
1660 SET_TOP(res);
1661 if (res == NULL)
1662 goto error;
1663 DISPATCH();
1664 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001665
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001666 TARGET(INPLACE_XOR) {
1667 PyObject *right = POP();
1668 PyObject *left = TOP();
1669 PyObject *res = PyNumber_InPlaceXor(left, right);
1670 Py_DECREF(left);
1671 Py_DECREF(right);
1672 SET_TOP(res);
1673 if (res == NULL)
1674 goto error;
1675 DISPATCH();
1676 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001677
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001678 TARGET(INPLACE_OR) {
1679 PyObject *right = POP();
1680 PyObject *left = TOP();
1681 PyObject *res = PyNumber_InPlaceOr(left, right);
1682 Py_DECREF(left);
1683 Py_DECREF(right);
1684 SET_TOP(res);
1685 if (res == NULL)
1686 goto error;
1687 DISPATCH();
1688 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001689
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001690 TARGET(STORE_SUBSCR) {
1691 PyObject *sub = TOP();
1692 PyObject *container = SECOND();
1693 PyObject *v = THIRD();
1694 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 STACKADJ(-3);
Martin Panter95f53c12016-07-18 08:23:26 +00001696 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001697 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001699 Py_DECREF(container);
1700 Py_DECREF(sub);
1701 if (err != 0)
1702 goto error;
1703 DISPATCH();
1704 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001705
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001706 TARGET(STORE_ANNOTATION) {
1707 _Py_IDENTIFIER(__annotations__);
1708 PyObject *ann_dict;
1709 PyObject *ann = POP();
1710 PyObject *name = GETITEM(names, oparg);
1711 int err;
1712 if (f->f_locals == NULL) {
1713 PyErr_Format(PyExc_SystemError,
1714 "no locals found when storing annotation");
1715 Py_DECREF(ann);
1716 goto error;
1717 }
1718 /* first try to get __annotations__ from locals... */
1719 if (PyDict_CheckExact(f->f_locals)) {
1720 ann_dict = _PyDict_GetItemId(f->f_locals,
1721 &PyId___annotations__);
1722 if (ann_dict == NULL) {
1723 PyErr_SetString(PyExc_NameError,
1724 "__annotations__ not found");
1725 Py_DECREF(ann);
1726 goto error;
1727 }
1728 Py_INCREF(ann_dict);
1729 }
1730 else {
1731 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
1732 if (ann_str == NULL) {
1733 Py_DECREF(ann);
1734 goto error;
1735 }
1736 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
1737 if (ann_dict == NULL) {
1738 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
1739 PyErr_SetString(PyExc_NameError,
1740 "__annotations__ not found");
1741 }
1742 Py_DECREF(ann);
1743 goto error;
1744 }
1745 }
1746 /* ...if succeeded, __annotations__[name] = ann */
1747 if (PyDict_CheckExact(ann_dict)) {
1748 err = PyDict_SetItem(ann_dict, name, ann);
1749 }
1750 else {
1751 err = PyObject_SetItem(ann_dict, name, ann);
1752 }
1753 Py_DECREF(ann_dict);
Yury Selivanov50c584f2016-09-08 23:38:21 -07001754 Py_DECREF(ann);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001755 if (err != 0) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001756 goto error;
1757 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001758 DISPATCH();
1759 }
1760
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001761 TARGET(DELETE_SUBSCR) {
1762 PyObject *sub = TOP();
1763 PyObject *container = SECOND();
1764 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001765 STACKADJ(-2);
Martin Panter95f53c12016-07-18 08:23:26 +00001766 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001767 err = PyObject_DelItem(container, sub);
1768 Py_DECREF(container);
1769 Py_DECREF(sub);
1770 if (err != 0)
1771 goto error;
1772 DISPATCH();
1773 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001774
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001775 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001776 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001777 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001778 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001779 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001780 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 PyErr_SetString(PyExc_RuntimeError,
1782 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001783 Py_DECREF(value);
1784 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001786 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001787 Py_DECREF(value);
1788 if (res == NULL)
1789 goto error;
1790 Py_DECREF(res);
1791 DISPATCH();
1792 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001793
Thomas Wouters434d0822000-08-24 20:11:32 +00001794#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001796#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001797 TARGET(RAISE_VARARGS) {
1798 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 switch (oparg) {
1800 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001801 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001802 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001803 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001804 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001805 /* fall through */
1806 case 0:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001807 if (do_raise(exc, cause)) {
1808 why = WHY_EXCEPTION;
1809 goto fast_block_end;
1810 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 break;
1812 default:
1813 PyErr_SetString(PyExc_SystemError,
1814 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815 break;
1816 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001817 goto error;
1818 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001819
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001820 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001821 retval = POP();
1822 why = WHY_RETURN;
1823 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001824 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001825
Yury Selivanov75445082015-05-11 22:57:16 -04001826 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001827 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001828 PyObject *iter = NULL;
1829 PyObject *awaitable = NULL;
1830 PyObject *obj = TOP();
1831 PyTypeObject *type = Py_TYPE(obj);
1832
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001833 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001834 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001835 }
Yury Selivanov75445082015-05-11 22:57:16 -04001836
1837 if (getter != NULL) {
1838 iter = (*getter)(obj);
1839 Py_DECREF(obj);
1840 if (iter == NULL) {
1841 SET_TOP(NULL);
1842 goto error;
1843 }
1844 }
1845 else {
1846 SET_TOP(NULL);
1847 PyErr_Format(
1848 PyExc_TypeError,
1849 "'async for' requires an object with "
1850 "__aiter__ method, got %.100s",
1851 type->tp_name);
1852 Py_DECREF(obj);
1853 goto error;
1854 }
1855
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001856 if (Py_TYPE(iter)->tp_as_async != NULL &&
1857 Py_TYPE(iter)->tp_as_async->am_anext != NULL) {
1858
1859 /* Starting with CPython 3.5.2 __aiter__ should return
1860 asynchronous iterators directly (not awaitables that
1861 resolve to asynchronous iterators.)
1862
1863 Therefore, we check if the object that was returned
1864 from __aiter__ has an __anext__ method. If it does,
1865 we wrap it in an awaitable that resolves to `iter`.
1866
1867 See http://bugs.python.org/issue27243 for more
1868 details.
1869 */
1870
1871 PyObject *wrapper = _PyAIterWrapper_New(iter);
1872 Py_DECREF(iter);
1873 SET_TOP(wrapper);
1874 DISPATCH();
1875 }
1876
Yury Selivanov5376ba92015-06-22 12:19:30 -04001877 awaitable = _PyCoro_GetAwaitableIter(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001878 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001879 _PyErr_FormatFromCause(
Yury Selivanov75445082015-05-11 22:57:16 -04001880 PyExc_TypeError,
1881 "'async for' received an invalid object "
1882 "from __aiter__: %.100s",
1883 Py_TYPE(iter)->tp_name);
1884
Yury Selivanov398ff912017-03-02 22:20:00 -05001885 SET_TOP(NULL);
Yury Selivanov75445082015-05-11 22:57:16 -04001886 Py_DECREF(iter);
1887 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001888 } else {
Yury Selivanov75445082015-05-11 22:57:16 -04001889 Py_DECREF(iter);
1890
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001891 if (PyErr_WarnFormat(
Yury Selivanov2edd8a12016-11-08 15:13:07 -05001892 PyExc_DeprecationWarning, 1,
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001893 "'%.100s' implements legacy __aiter__ protocol; "
1894 "__aiter__ should return an asynchronous "
1895 "iterator, not awaitable",
1896 type->tp_name))
1897 {
1898 /* Warning was converted to an error. */
1899 Py_DECREF(awaitable);
1900 SET_TOP(NULL);
1901 goto error;
1902 }
1903 }
1904
Yury Selivanov75445082015-05-11 22:57:16 -04001905 SET_TOP(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001906 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001907 DISPATCH();
1908 }
1909
1910 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001911 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001912 PyObject *next_iter = NULL;
1913 PyObject *awaitable = NULL;
1914 PyObject *aiter = TOP();
1915 PyTypeObject *type = Py_TYPE(aiter);
1916
Yury Selivanoveb636452016-09-08 22:01:51 -07001917 if (PyAsyncGen_CheckExact(aiter)) {
1918 awaitable = type->tp_as_async->am_anext(aiter);
1919 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001920 goto error;
1921 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001922 } else {
1923 if (type->tp_as_async != NULL){
1924 getter = type->tp_as_async->am_anext;
1925 }
Yury Selivanov75445082015-05-11 22:57:16 -04001926
Yury Selivanoveb636452016-09-08 22:01:51 -07001927 if (getter != NULL) {
1928 next_iter = (*getter)(aiter);
1929 if (next_iter == NULL) {
1930 goto error;
1931 }
1932 }
1933 else {
1934 PyErr_Format(
1935 PyExc_TypeError,
1936 "'async for' requires an iterator with "
1937 "__anext__ method, got %.100s",
1938 type->tp_name);
1939 goto error;
1940 }
Yury Selivanov75445082015-05-11 22:57:16 -04001941
Yury Selivanoveb636452016-09-08 22:01:51 -07001942 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1943 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001944 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001945 PyExc_TypeError,
1946 "'async for' received an invalid object "
1947 "from __anext__: %.100s",
1948 Py_TYPE(next_iter)->tp_name);
1949
1950 Py_DECREF(next_iter);
1951 goto error;
1952 } else {
1953 Py_DECREF(next_iter);
1954 }
1955 }
Yury Selivanov75445082015-05-11 22:57:16 -04001956
1957 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001958 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001959 DISPATCH();
1960 }
1961
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001962 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001963 TARGET(GET_AWAITABLE) {
1964 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001965 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001966
1967 Py_DECREF(iterable);
1968
Yury Selivanovc724bae2016-03-02 11:30:46 -05001969 if (iter != NULL && PyCoro_CheckExact(iter)) {
1970 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1971 if (yf != NULL) {
1972 /* `iter` is a coroutine object that is being
1973 awaited, `yf` is a pointer to the current awaitable
1974 being awaited on. */
1975 Py_DECREF(yf);
1976 Py_CLEAR(iter);
1977 PyErr_SetString(
1978 PyExc_RuntimeError,
1979 "coroutine is being awaited already");
1980 /* The code below jumps to `error` if `iter` is NULL. */
1981 }
1982 }
1983
Yury Selivanov75445082015-05-11 22:57:16 -04001984 SET_TOP(iter); /* Even if it's NULL */
1985
1986 if (iter == NULL) {
1987 goto error;
1988 }
1989
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001990 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001991 DISPATCH();
1992 }
1993
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001994 TARGET(YIELD_FROM) {
1995 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001996 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001997 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001998 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1999 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002000 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002001 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002002 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002003 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002004 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002005 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002006 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002007 Py_DECREF(v);
2008 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002009 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002010 if (tstate->c_tracefunc != NULL
2011 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002012 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002013 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002014 if (err < 0)
2015 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002016 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002017 SET_TOP(val);
2018 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002019 }
Martin Panter95f53c12016-07-18 08:23:26 +00002020 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002021 f->f_stacktop = stack_pointer;
2022 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002023 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002024 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002025 f->f_lasti -= sizeof(_Py_CODEUNIT);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002026 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002027 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002028
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002029 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002030 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002031
2032 if (co->co_flags & CO_ASYNC_GENERATOR) {
2033 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2034 Py_DECREF(retval);
2035 if (w == NULL) {
2036 retval = NULL;
2037 goto error;
2038 }
2039 retval = w;
2040 }
2041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002042 f->f_stacktop = stack_pointer;
2043 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002044 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002045 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002046
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002047 TARGET(POP_EXCEPT) {
2048 PyTryBlock *b = PyFrame_BlockPop(f);
2049 if (b->b_type != EXCEPT_HANDLER) {
2050 PyErr_SetString(PyExc_SystemError,
2051 "popped block is not an except handler");
2052 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002054 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002055 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002056 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002057
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002058 PREDICTED(POP_BLOCK);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002059 TARGET(POP_BLOCK) {
2060 PyTryBlock *b = PyFrame_BlockPop(f);
2061 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002063 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002065 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002066 TARGET(END_FINALLY) {
2067 PyObject *status = POP();
2068 if (PyLong_Check(status)) {
2069 why = (enum why_code) PyLong_AS_LONG(status);
2070 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 if (why == WHY_RETURN ||
2072 why == WHY_CONTINUE)
2073 retval = POP();
2074 if (why == WHY_SILENCED) {
2075 /* An exception was silenced by 'with', we must
2076 manually unwind the EXCEPT_HANDLER block which was
2077 created when the exception was caught, otherwise
2078 the stack will be in an inconsistent state. */
2079 PyTryBlock *b = PyFrame_BlockPop(f);
2080 assert(b->b_type == EXCEPT_HANDLER);
2081 UNWIND_EXCEPT_HANDLER(b);
2082 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002083 Py_DECREF(status);
2084 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002086 Py_DECREF(status);
2087 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002089 else if (PyExceptionClass_Check(status)) {
2090 PyObject *exc = POP();
2091 PyObject *tb = POP();
2092 PyErr_Restore(status, exc, tb);
2093 why = WHY_EXCEPTION;
2094 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002096 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097 PyErr_SetString(PyExc_SystemError,
2098 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002099 Py_DECREF(status);
2100 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002102 Py_DECREF(status);
2103 DISPATCH();
2104 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002105
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002106 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002107 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002108
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002109 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002110 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002111 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
2112 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002113 PyErr_SetString(PyExc_NameError,
2114 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002115 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002116 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002117 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002118 }
2119 else {
2120 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2121 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002122 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002123 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2124 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002125 if (PyErr_ExceptionMatches(PyExc_KeyError))
2126 PyErr_SetString(PyExc_NameError,
2127 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002128 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002129 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002130 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002131 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002132 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002133 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002134
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002135 TARGET(STORE_NAME) {
2136 PyObject *name = GETITEM(names, oparg);
2137 PyObject *v = POP();
2138 PyObject *ns = f->f_locals;
2139 int err;
2140 if (ns == NULL) {
2141 PyErr_Format(PyExc_SystemError,
2142 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002143 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002144 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002145 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002146 if (PyDict_CheckExact(ns))
2147 err = PyDict_SetItem(ns, name, v);
2148 else
2149 err = PyObject_SetItem(ns, name, v);
2150 Py_DECREF(v);
2151 if (err != 0)
2152 goto error;
2153 DISPATCH();
2154 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002155
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002156 TARGET(DELETE_NAME) {
2157 PyObject *name = GETITEM(names, oparg);
2158 PyObject *ns = f->f_locals;
2159 int err;
2160 if (ns == NULL) {
2161 PyErr_Format(PyExc_SystemError,
2162 "no locals when deleting %R", name);
2163 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002164 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002165 err = PyObject_DelItem(ns, name);
2166 if (err != 0) {
2167 format_exc_check_arg(PyExc_NameError,
2168 NAME_ERROR_MSG,
2169 name);
2170 goto error;
2171 }
2172 DISPATCH();
2173 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002174
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002175 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002176 TARGET(UNPACK_SEQUENCE) {
2177 PyObject *seq = POP(), *item, **items;
2178 if (PyTuple_CheckExact(seq) &&
2179 PyTuple_GET_SIZE(seq) == oparg) {
2180 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002182 item = items[oparg];
2183 Py_INCREF(item);
2184 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002186 } else if (PyList_CheckExact(seq) &&
2187 PyList_GET_SIZE(seq) == oparg) {
2188 items = ((PyListObject *)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 (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 stack_pointer + oparg)) {
2196 STACKADJ(oparg);
2197 } else {
2198 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002199 Py_DECREF(seq);
2200 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002201 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002202 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002203 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002204 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002205
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002206 TARGET(UNPACK_EX) {
2207 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2208 PyObject *seq = POP();
2209
2210 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2211 stack_pointer + totalargs)) {
2212 stack_pointer += totalargs;
2213 } else {
2214 Py_DECREF(seq);
2215 goto error;
2216 }
2217 Py_DECREF(seq);
2218 DISPATCH();
2219 }
2220
2221 TARGET(STORE_ATTR) {
2222 PyObject *name = GETITEM(names, oparg);
2223 PyObject *owner = TOP();
2224 PyObject *v = SECOND();
2225 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002227 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002229 Py_DECREF(owner);
2230 if (err != 0)
2231 goto error;
2232 DISPATCH();
2233 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002234
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002235 TARGET(DELETE_ATTR) {
2236 PyObject *name = GETITEM(names, oparg);
2237 PyObject *owner = POP();
2238 int err;
2239 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2240 Py_DECREF(owner);
2241 if (err != 0)
2242 goto error;
2243 DISPATCH();
2244 }
2245
2246 TARGET(STORE_GLOBAL) {
2247 PyObject *name = GETITEM(names, oparg);
2248 PyObject *v = POP();
2249 int err;
2250 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002252 if (err != 0)
2253 goto error;
2254 DISPATCH();
2255 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002256
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002257 TARGET(DELETE_GLOBAL) {
2258 PyObject *name = GETITEM(names, oparg);
2259 int err;
2260 err = PyDict_DelItem(f->f_globals, name);
2261 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002263 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002264 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002265 }
2266 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002267 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002268
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002269 TARGET(LOAD_NAME) {
2270 PyObject *name = GETITEM(names, oparg);
2271 PyObject *locals = f->f_locals;
2272 PyObject *v;
2273 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002274 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002275 "no locals when loading %R", name);
2276 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002278 if (PyDict_CheckExact(locals)) {
2279 v = PyDict_GetItem(locals, name);
2280 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 }
2282 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002283 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002284 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002285 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2286 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 PyErr_Clear();
2288 }
2289 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002290 if (v == NULL) {
2291 v = PyDict_GetItem(f->f_globals, name);
2292 Py_XINCREF(v);
2293 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002294 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002295 v = PyDict_GetItem(f->f_builtins, name);
2296 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002297 format_exc_check_arg(
2298 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002299 NAME_ERROR_MSG, name);
2300 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002301 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002302 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002303 }
2304 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002305 v = PyObject_GetItem(f->f_builtins, name);
2306 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002307 if (PyErr_ExceptionMatches(PyExc_KeyError))
2308 format_exc_check_arg(
2309 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002310 NAME_ERROR_MSG, name);
2311 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002312 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002313 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002314 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002316 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002318 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002319
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002320 TARGET(LOAD_GLOBAL) {
2321 PyObject *name = GETITEM(names, oparg);
2322 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002323 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002324 && PyDict_CheckExact(f->f_builtins))
2325 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002326 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002327 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002328 name);
2329 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002330 if (!_PyErr_OCCURRED()) {
2331 /* _PyDict_LoadGlobal() returns NULL without raising
2332 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002333 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002334 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002335 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002336 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002337 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002338 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002339 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002340 else {
2341 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002342
2343 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002344 v = PyObject_GetItem(f->f_globals, name);
2345 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002346 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2347 goto error;
2348 PyErr_Clear();
2349
Victor Stinnerb4efc962015-11-20 09:24:02 +01002350 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002351 v = PyObject_GetItem(f->f_builtins, name);
2352 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002353 if (PyErr_ExceptionMatches(PyExc_KeyError))
2354 format_exc_check_arg(
2355 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002356 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002357 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002358 }
2359 }
2360 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002361 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002363 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002364
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002365 TARGET(DELETE_FAST) {
2366 PyObject *v = GETLOCAL(oparg);
2367 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 SETLOCAL(oparg, NULL);
2369 DISPATCH();
2370 }
2371 format_exc_check_arg(
2372 PyExc_UnboundLocalError,
2373 UNBOUNDLOCAL_ERROR_MSG,
2374 PyTuple_GetItem(co->co_varnames, oparg)
2375 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002376 goto error;
2377 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002378
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002379 TARGET(DELETE_DEREF) {
2380 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002381 PyObject *oldobj = PyCell_GET(cell);
2382 if (oldobj != NULL) {
2383 PyCell_SET(cell, NULL);
2384 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002385 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002386 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002387 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002388 goto error;
2389 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002390
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002391 TARGET(LOAD_CLOSURE) {
2392 PyObject *cell = freevars[oparg];
2393 Py_INCREF(cell);
2394 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002396 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002397
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002398 TARGET(LOAD_CLASSDEREF) {
2399 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002400 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002401 assert(locals);
2402 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2403 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2404 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2405 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2406 if (PyDict_CheckExact(locals)) {
2407 value = PyDict_GetItem(locals, name);
2408 Py_XINCREF(value);
2409 }
2410 else {
2411 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002412 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002413 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2414 goto error;
2415 PyErr_Clear();
2416 }
2417 }
2418 if (!value) {
2419 PyObject *cell = freevars[oparg];
2420 value = PyCell_GET(cell);
2421 if (value == NULL) {
2422 format_exc_unbound(co, oparg);
2423 goto error;
2424 }
2425 Py_INCREF(value);
2426 }
2427 PUSH(value);
2428 DISPATCH();
2429 }
2430
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002431 TARGET(LOAD_DEREF) {
2432 PyObject *cell = freevars[oparg];
2433 PyObject *value = PyCell_GET(cell);
2434 if (value == NULL) {
2435 format_exc_unbound(co, oparg);
2436 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002437 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002438 Py_INCREF(value);
2439 PUSH(value);
2440 DISPATCH();
2441 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002442
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002443 TARGET(STORE_DEREF) {
2444 PyObject *v = POP();
2445 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002446 PyObject *oldobj = PyCell_GET(cell);
2447 PyCell_SET(cell, v);
2448 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002449 DISPATCH();
2450 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002451
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002452 TARGET(BUILD_STRING) {
2453 PyObject *str;
2454 PyObject *empty = PyUnicode_New(0, 0);
2455 if (empty == NULL) {
2456 goto error;
2457 }
2458 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2459 Py_DECREF(empty);
2460 if (str == NULL)
2461 goto error;
2462 while (--oparg >= 0) {
2463 PyObject *item = POP();
2464 Py_DECREF(item);
2465 }
2466 PUSH(str);
2467 DISPATCH();
2468 }
2469
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002470 TARGET(BUILD_TUPLE) {
2471 PyObject *tup = PyTuple_New(oparg);
2472 if (tup == NULL)
2473 goto error;
2474 while (--oparg >= 0) {
2475 PyObject *item = POP();
2476 PyTuple_SET_ITEM(tup, oparg, item);
2477 }
2478 PUSH(tup);
2479 DISPATCH();
2480 }
2481
2482 TARGET(BUILD_LIST) {
2483 PyObject *list = PyList_New(oparg);
2484 if (list == NULL)
2485 goto error;
2486 while (--oparg >= 0) {
2487 PyObject *item = POP();
2488 PyList_SET_ITEM(list, oparg, item);
2489 }
2490 PUSH(list);
2491 DISPATCH();
2492 }
2493
Serhiy Storchaka73442852016-10-02 10:33:46 +03002494 TARGET(BUILD_TUPLE_UNPACK_WITH_CALL)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002495 TARGET(BUILD_TUPLE_UNPACK)
2496 TARGET(BUILD_LIST_UNPACK) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002497 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002498 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002499 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002500 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002501
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002502 if (sum == NULL)
2503 goto error;
2504
2505 for (i = oparg; i > 0; i--) {
2506 PyObject *none_val;
2507
2508 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2509 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002510 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002511 PyErr_ExceptionMatches(PyExc_TypeError))
2512 {
2513 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002514 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002515 Py_DECREF(sum);
2516 goto error;
2517 }
2518 Py_DECREF(none_val);
2519 }
2520
2521 if (convert_to_tuple) {
2522 return_value = PyList_AsTuple(sum);
2523 Py_DECREF(sum);
2524 if (return_value == NULL)
2525 goto error;
2526 }
2527 else {
2528 return_value = sum;
2529 }
2530
2531 while (oparg--)
2532 Py_DECREF(POP());
2533 PUSH(return_value);
2534 DISPATCH();
2535 }
2536
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002537 TARGET(BUILD_SET) {
2538 PyObject *set = PySet_New(NULL);
2539 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002540 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002541 if (set == NULL)
2542 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002543 for (i = oparg; i > 0; i--) {
2544 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002545 if (err == 0)
2546 err = PySet_Add(set, item);
2547 Py_DECREF(item);
2548 }
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002549 STACKADJ(-oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002550 if (err != 0) {
2551 Py_DECREF(set);
2552 goto error;
2553 }
2554 PUSH(set);
2555 DISPATCH();
2556 }
2557
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002558 TARGET(BUILD_SET_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002559 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002560 PyObject *sum = PySet_New(NULL);
2561 if (sum == NULL)
2562 goto error;
2563
2564 for (i = oparg; i > 0; i--) {
2565 if (_PySet_Update(sum, PEEK(i)) < 0) {
2566 Py_DECREF(sum);
2567 goto error;
2568 }
2569 }
2570
2571 while (oparg--)
2572 Py_DECREF(POP());
2573 PUSH(sum);
2574 DISPATCH();
2575 }
2576
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002577 TARGET(BUILD_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002578 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002579 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2580 if (map == NULL)
2581 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002582 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002583 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002584 PyObject *key = PEEK(2*i);
2585 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002586 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002587 if (err != 0) {
2588 Py_DECREF(map);
2589 goto error;
2590 }
2591 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002592
2593 while (oparg--) {
2594 Py_DECREF(POP());
2595 Py_DECREF(POP());
2596 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002597 PUSH(map);
2598 DISPATCH();
2599 }
2600
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002601 TARGET(SETUP_ANNOTATIONS) {
2602 _Py_IDENTIFIER(__annotations__);
2603 int err;
2604 PyObject *ann_dict;
2605 if (f->f_locals == NULL) {
2606 PyErr_Format(PyExc_SystemError,
2607 "no locals found when setting up annotations");
2608 goto error;
2609 }
2610 /* check if __annotations__ in locals()... */
2611 if (PyDict_CheckExact(f->f_locals)) {
2612 ann_dict = _PyDict_GetItemId(f->f_locals,
2613 &PyId___annotations__);
2614 if (ann_dict == NULL) {
2615 /* ...if not, create a new one */
2616 ann_dict = PyDict_New();
2617 if (ann_dict == NULL) {
2618 goto error;
2619 }
2620 err = _PyDict_SetItemId(f->f_locals,
2621 &PyId___annotations__, ann_dict);
2622 Py_DECREF(ann_dict);
2623 if (err != 0) {
2624 goto error;
2625 }
2626 }
2627 }
2628 else {
2629 /* do the same if locals() is not a dict */
2630 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2631 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002632 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002633 }
2634 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2635 if (ann_dict == NULL) {
2636 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2637 goto error;
2638 }
2639 PyErr_Clear();
2640 ann_dict = PyDict_New();
2641 if (ann_dict == NULL) {
2642 goto error;
2643 }
2644 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2645 Py_DECREF(ann_dict);
2646 if (err != 0) {
2647 goto error;
2648 }
2649 }
2650 else {
2651 Py_DECREF(ann_dict);
2652 }
2653 }
2654 DISPATCH();
2655 }
2656
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002657 TARGET(BUILD_CONST_KEY_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002658 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002659 PyObject *map;
2660 PyObject *keys = TOP();
2661 if (!PyTuple_CheckExact(keys) ||
2662 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2663 PyErr_SetString(PyExc_SystemError,
2664 "bad BUILD_CONST_KEY_MAP keys argument");
2665 goto error;
2666 }
2667 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2668 if (map == NULL) {
2669 goto error;
2670 }
2671 for (i = oparg; i > 0; i--) {
2672 int err;
2673 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2674 PyObject *value = PEEK(i + 1);
2675 err = PyDict_SetItem(map, key, value);
2676 if (err != 0) {
2677 Py_DECREF(map);
2678 goto error;
2679 }
2680 }
2681
2682 Py_DECREF(POP());
2683 while (oparg--) {
2684 Py_DECREF(POP());
2685 }
2686 PUSH(map);
2687 DISPATCH();
2688 }
2689
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002690 TARGET(BUILD_MAP_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002691 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002692 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002693 if (sum == NULL)
2694 goto error;
2695
2696 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002697 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002698 if (PyDict_Update(sum, arg) < 0) {
2699 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2700 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002701 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002702 arg->ob_type->tp_name);
2703 }
2704 Py_DECREF(sum);
2705 goto error;
2706 }
2707 }
2708
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002709 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002710 Py_DECREF(POP());
2711 PUSH(sum);
2712 DISPATCH();
2713 }
2714
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002715 TARGET(BUILD_MAP_UNPACK_WITH_CALL) {
2716 Py_ssize_t i;
2717 PyObject *sum = PyDict_New();
2718 if (sum == NULL)
2719 goto error;
2720
2721 for (i = oparg; i > 0; i--) {
2722 PyObject *arg = PEEK(i);
2723 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
2724 PyObject *func = PEEK(2 + oparg);
2725 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002726 format_kwargs_mapping_error(func, arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002727 }
2728 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2729 PyObject *exc, *val, *tb;
2730 PyErr_Fetch(&exc, &val, &tb);
2731 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
2732 PyObject *key = PyTuple_GET_ITEM(val, 0);
2733 if (!PyUnicode_Check(key)) {
2734 PyErr_Format(PyExc_TypeError,
2735 "%.200s%.200s keywords must be strings",
2736 PyEval_GetFuncName(func),
2737 PyEval_GetFuncDesc(func));
2738 } else {
2739 PyErr_Format(PyExc_TypeError,
2740 "%.200s%.200s got multiple "
2741 "values for keyword argument '%U'",
2742 PyEval_GetFuncName(func),
2743 PyEval_GetFuncDesc(func),
2744 key);
2745 }
2746 Py_XDECREF(exc);
2747 Py_XDECREF(val);
2748 Py_XDECREF(tb);
2749 }
2750 else {
2751 PyErr_Restore(exc, val, tb);
2752 }
2753 }
2754 Py_DECREF(sum);
2755 goto error;
2756 }
2757 }
2758
2759 while (oparg--)
2760 Py_DECREF(POP());
2761 PUSH(sum);
2762 DISPATCH();
2763 }
2764
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002765 TARGET(MAP_ADD) {
2766 PyObject *key = TOP();
2767 PyObject *value = SECOND();
2768 PyObject *map;
2769 int err;
2770 STACKADJ(-2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002771 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002772 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002773 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002774 Py_DECREF(value);
2775 Py_DECREF(key);
2776 if (err != 0)
2777 goto error;
2778 PREDICT(JUMP_ABSOLUTE);
2779 DISPATCH();
2780 }
2781
2782 TARGET(LOAD_ATTR) {
2783 PyObject *name = GETITEM(names, oparg);
2784 PyObject *owner = TOP();
2785 PyObject *res = PyObject_GetAttr(owner, name);
2786 Py_DECREF(owner);
2787 SET_TOP(res);
2788 if (res == NULL)
2789 goto error;
2790 DISPATCH();
2791 }
2792
2793 TARGET(COMPARE_OP) {
2794 PyObject *right = POP();
2795 PyObject *left = TOP();
2796 PyObject *res = cmp_outcome(oparg, left, right);
2797 Py_DECREF(left);
2798 Py_DECREF(right);
2799 SET_TOP(res);
2800 if (res == NULL)
2801 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002802 PREDICT(POP_JUMP_IF_FALSE);
2803 PREDICT(POP_JUMP_IF_TRUE);
2804 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002805 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002806
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002807 TARGET(IMPORT_NAME) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002808 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002809 PyObject *fromlist = POP();
2810 PyObject *level = TOP();
2811 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002812 res = import_name(f, name, fromlist, level);
2813 Py_DECREF(level);
2814 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002815 SET_TOP(res);
2816 if (res == NULL)
2817 goto error;
2818 DISPATCH();
2819 }
2820
2821 TARGET(IMPORT_STAR) {
2822 PyObject *from = POP(), *locals;
2823 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002824 if (PyFrame_FastToLocalsWithError(f) < 0) {
2825 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002826 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002827 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002828
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002829 locals = f->f_locals;
2830 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002831 PyErr_SetString(PyExc_SystemError,
2832 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002833 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002834 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002835 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002836 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002837 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002838 Py_DECREF(from);
2839 if (err != 0)
2840 goto error;
2841 DISPATCH();
2842 }
Guido van Rossum25831651993-05-19 14:50:45 +00002843
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002844 TARGET(IMPORT_FROM) {
2845 PyObject *name = GETITEM(names, oparg);
2846 PyObject *from = TOP();
2847 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002848 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002849 PUSH(res);
2850 if (res == NULL)
2851 goto error;
2852 DISPATCH();
2853 }
Thomas Wouters52152252000-08-17 22:55:00 +00002854
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002855 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002856 JUMPBY(oparg);
2857 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002858 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002859
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002860 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002861 TARGET(POP_JUMP_IF_FALSE) {
2862 PyObject *cond = POP();
2863 int err;
2864 if (cond == Py_True) {
2865 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002866 FAST_DISPATCH();
2867 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002868 if (cond == Py_False) {
2869 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 JUMPTO(oparg);
2871 FAST_DISPATCH();
2872 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002873 err = PyObject_IsTrue(cond);
2874 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002875 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002876 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002877 else if (err == 0)
2878 JUMPTO(oparg);
2879 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002880 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002882 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002883
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002884 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002885 TARGET(POP_JUMP_IF_TRUE) {
2886 PyObject *cond = POP();
2887 int err;
2888 if (cond == Py_False) {
2889 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002890 FAST_DISPATCH();
2891 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002892 if (cond == Py_True) {
2893 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002894 JUMPTO(oparg);
2895 FAST_DISPATCH();
2896 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002897 err = PyObject_IsTrue(cond);
2898 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002899 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002900 JUMPTO(oparg);
2901 }
2902 else if (err == 0)
2903 ;
2904 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002905 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002906 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002907 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002908
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002909 TARGET(JUMP_IF_FALSE_OR_POP) {
2910 PyObject *cond = TOP();
2911 int err;
2912 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002914 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002915 FAST_DISPATCH();
2916 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002917 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002918 JUMPTO(oparg);
2919 FAST_DISPATCH();
2920 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002921 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002922 if (err > 0) {
2923 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002924 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 }
2926 else if (err == 0)
2927 JUMPTO(oparg);
2928 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002929 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002930 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002931 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002932
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002933 TARGET(JUMP_IF_TRUE_OR_POP) {
2934 PyObject *cond = TOP();
2935 int err;
2936 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002937 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002938 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002939 FAST_DISPATCH();
2940 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002941 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002942 JUMPTO(oparg);
2943 FAST_DISPATCH();
2944 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002945 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002947 JUMPTO(oparg);
2948 }
2949 else if (err == 0) {
2950 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002951 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002952 }
2953 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002954 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002955 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002956 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002957
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002958 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002959 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002960 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002961#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002962 /* Enabling this path speeds-up all while and for-loops by bypassing
2963 the per-loop checks for signals. By default, this should be turned-off
2964 because it prevents detection of a control-break in tight loops like
2965 "while 1: pass". Compile with this option turned-on when you need
2966 the speed-up and do not need break checking inside tight loops (ones
2967 that contain only instructions ending with FAST_DISPATCH).
2968 */
2969 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002970#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002971 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002972#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002973 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002974
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002975 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002976 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002977 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002978 PyObject *iter = PyObject_GetIter(iterable);
2979 Py_DECREF(iterable);
2980 SET_TOP(iter);
2981 if (iter == NULL)
2982 goto error;
2983 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002984 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002985 DISPATCH();
2986 }
2987
2988 TARGET(GET_YIELD_FROM_ITER) {
2989 /* before: [obj]; after [getiter(obj)] */
2990 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002991 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002992 if (PyCoro_CheckExact(iterable)) {
2993 /* `iterable` is a coroutine */
2994 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2995 /* and it is used in a 'yield from' expression of a
2996 regular generator. */
2997 Py_DECREF(iterable);
2998 SET_TOP(NULL);
2999 PyErr_SetString(PyExc_TypeError,
3000 "cannot 'yield from' a coroutine object "
3001 "in a non-coroutine generator");
3002 goto error;
3003 }
3004 }
3005 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003006 /* `iterable` is not a generator. */
3007 iter = PyObject_GetIter(iterable);
3008 Py_DECREF(iterable);
3009 SET_TOP(iter);
3010 if (iter == NULL)
3011 goto error;
3012 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003013 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003014 DISPATCH();
3015 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003016
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003017 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003018 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003019 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003020 PyObject *iter = TOP();
3021 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3022 if (next != NULL) {
3023 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003024 PREDICT(STORE_FAST);
3025 PREDICT(UNPACK_SEQUENCE);
3026 DISPATCH();
3027 }
3028 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003029 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
3030 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08003031 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003032 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003033 PyErr_Clear();
3034 }
3035 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003036 STACKADJ(-1);
3037 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003038 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003039 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003040 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003041 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003042
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003043 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003044 why = WHY_BREAK;
3045 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003046 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003047
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003048 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003049 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003050 if (retval == NULL)
3051 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003052 why = WHY_CONTINUE;
3053 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003054 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003055
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003056 TARGET(SETUP_LOOP)
3057 TARGET(SETUP_EXCEPT)
3058 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003059 /* NOTE: If you add any new block-setup opcodes that
3060 are not try/except/finally handlers, you may need
3061 to update the PyGen_NeedsFinalizing() function.
3062 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003064 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
3065 STACK_LEVEL());
3066 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003067 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003068
Yury Selivanov75445082015-05-11 22:57:16 -04003069 TARGET(BEFORE_ASYNC_WITH) {
3070 _Py_IDENTIFIER(__aexit__);
3071 _Py_IDENTIFIER(__aenter__);
3072
3073 PyObject *mgr = TOP();
3074 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3075 *enter;
3076 PyObject *res;
3077 if (exit == NULL)
3078 goto error;
3079 SET_TOP(exit);
3080 enter = special_lookup(mgr, &PyId___aenter__);
3081 Py_DECREF(mgr);
3082 if (enter == NULL)
3083 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003084 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003085 Py_DECREF(enter);
3086 if (res == NULL)
3087 goto error;
3088 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003089 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003090 DISPATCH();
3091 }
3092
3093 TARGET(SETUP_ASYNC_WITH) {
3094 PyObject *res = POP();
3095 /* Setup the finally block before pushing the result
3096 of __aenter__ on the stack. */
3097 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3098 STACK_LEVEL());
3099 PUSH(res);
3100 DISPATCH();
3101 }
3102
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003103 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003104 _Py_IDENTIFIER(__exit__);
3105 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003106 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08003107 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003108 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08003109 if (enter == NULL)
3110 goto error;
3111 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003112 if (exit == NULL) {
3113 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003114 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003115 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003116 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003117 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003118 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003119 Py_DECREF(enter);
3120 if (res == NULL)
3121 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003122 /* Setup the finally block before pushing the result
3123 of __enter__ on the stack. */
3124 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3125 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003126
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003127 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003128 DISPATCH();
3129 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003130
Yury Selivanov75445082015-05-11 22:57:16 -04003131 TARGET(WITH_CLEANUP_START) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04003132 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003133 how/why we entered the finally clause:
3134 - TOP = None
3135 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
3136 - TOP = WHY_*; no retval below it
3137 - (TOP, SECOND, THIRD) = exc_info()
3138 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
3139 Below them is EXIT, the context.__exit__ bound method.
3140 In the last case, we must call
3141 EXIT(TOP, SECOND, THIRD)
3142 otherwise we must call
3143 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003144
Benjamin Peterson8f169482013-10-29 22:25:06 -04003145 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003146 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04003147 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003148 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003150 In addition, if the stack represents an exception,
3151 *and* the function call returns a 'true' value, we
3152 push WHY_SILENCED onto the stack. END_FINALLY will
3153 then not re-raise the exception. (But non-local
3154 gotos should still be resumed.)
3155 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003156
Victor Stinner842cfff2016-12-01 14:45:31 +01003157 PyObject* stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003158 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003159 PyObject *exc, *val, *tb, *res;
3160
3161 val = tb = Py_None;
3162 exc = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003163 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 (void)POP();
3165 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003166 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003167 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003168 else if (PyLong_Check(exc)) {
3169 STACKADJ(-1);
3170 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003171 case WHY_RETURN:
3172 case WHY_CONTINUE:
3173 /* Retval in TOP. */
3174 exit_func = SECOND();
3175 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003176 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003177 break;
3178 default:
3179 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003180 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003181 break;
3182 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003183 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003184 }
3185 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003186 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003187 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003188 val = SECOND();
3189 tb = THIRD();
3190 tp2 = FOURTH();
3191 exc2 = PEEK(5);
3192 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003193 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003194 SET_VALUE(7, tb2);
3195 SET_VALUE(6, exc2);
3196 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003197 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3198 SET_FOURTH(NULL);
3199 /* We just shifted the stack down, so we have
3200 to tell the except handler block that the
3201 values are lower than it expects. */
3202 block = &f->f_blockstack[f->f_iblock - 1];
3203 assert(block->b_type == EXCEPT_HANDLER);
3204 block->b_level--;
3205 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003206
3207 stack[0] = exc;
3208 stack[1] = val;
3209 stack[2] = tb;
3210 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003211 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003212 if (res == NULL)
3213 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003214
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003215 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003216 PUSH(exc);
3217 PUSH(res);
3218 PREDICT(WITH_CLEANUP_FINISH);
3219 DISPATCH();
3220 }
3221
3222 PREDICTED(WITH_CLEANUP_FINISH);
3223 TARGET(WITH_CLEANUP_FINISH) {
3224 PyObject *res = POP();
3225 PyObject *exc = POP();
3226 int err;
3227
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003228 if (exc != Py_None)
3229 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003230 else
3231 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003232
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003233 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003234 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003236 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003237 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003238 else if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003239 /* There was an exception and a True return */
3240 PUSH(PyLong_FromLong((long) WHY_SILENCED));
3241 }
3242 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003243 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003244 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003245
Yury Selivanovf2392132016-12-13 19:03:51 -05003246 TARGET(LOAD_METHOD) {
3247 /* Designed to work in tamdem with CALL_METHOD. */
3248 PyObject *name = GETITEM(names, oparg);
3249 PyObject *obj = TOP();
3250 PyObject *meth = NULL;
3251
3252 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3253
Yury Selivanovf2392132016-12-13 19:03:51 -05003254 if (meth == NULL) {
3255 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003256 goto error;
3257 }
3258
3259 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003260 /* We can bypass temporary bound method object.
3261 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003262
INADA Naoki015bce62017-01-16 17:23:30 +09003263 meth | self | arg1 | ... | argN
3264 */
3265 SET_TOP(meth);
3266 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003267 }
3268 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003269 /* meth is not an unbound method (but a regular attr, or
3270 something was returned by a descriptor protocol). Set
3271 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003272 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003273
3274 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003275 */
INADA Naoki015bce62017-01-16 17:23:30 +09003276 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003277 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003278 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003279 }
3280 DISPATCH();
3281 }
3282
3283 TARGET(CALL_METHOD) {
3284 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003285 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003286
3287 sp = stack_pointer;
3288
INADA Naoki015bce62017-01-16 17:23:30 +09003289 meth = PEEK(oparg + 2);
3290 if (meth == NULL) {
3291 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3292 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003293
3294 Stack layout:
3295
INADA Naoki015bce62017-01-16 17:23:30 +09003296 ... | NULL | callable | arg1 | ... | argN
3297 ^- TOP()
3298 ^- (-oparg)
3299 ^- (-oparg-1)
3300 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003301
Ville Skyttä49b27342017-08-03 09:00:59 +03003302 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003303 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003304 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003305 res = call_function(&sp, oparg, NULL);
3306 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003307 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003308 }
3309 else {
3310 /* This is a method call. Stack layout:
3311
INADA Naoki015bce62017-01-16 17:23:30 +09003312 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003313 ^- TOP()
3314 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003315 ^- (-oparg-1)
3316 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003317
INADA Naoki015bce62017-01-16 17:23:30 +09003318 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003319 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003320 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003321 */
3322 res = call_function(&sp, oparg + 1, NULL);
3323 stack_pointer = sp;
3324 }
3325
3326 PUSH(res);
3327 if (res == NULL)
3328 goto error;
3329 DISPATCH();
3330 }
3331
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003332 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003333 TARGET(CALL_FUNCTION) {
3334 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003335 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003336 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003337 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003338 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003339 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003340 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003341 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003342 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003343 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003344
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003345 TARGET(CALL_FUNCTION_KW) {
3346 PyObject **sp, *res, *names;
3347
3348 names = POP();
3349 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003350 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003351 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003352 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003353 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003354 Py_DECREF(names);
3355
3356 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003357 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003358 }
3359 DISPATCH();
3360 }
3361
3362 TARGET(CALL_FUNCTION_EX) {
3363 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003364 if (oparg & 0x01) {
3365 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003366 if (!PyDict_CheckExact(kwargs)) {
3367 PyObject *d = PyDict_New();
3368 if (d == NULL)
3369 goto error;
3370 if (PyDict_Update(d, kwargs) != 0) {
3371 Py_DECREF(d);
3372 /* PyDict_Update raises attribute
3373 * error (percolated from an attempt
3374 * to get 'keys' attribute) instead of
3375 * a type error if its second argument
3376 * is not a mapping.
3377 */
3378 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003379 format_kwargs_mapping_error(SECOND(), kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003380 }
Victor Stinnereece2222016-09-12 11:16:37 +02003381 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003382 goto error;
3383 }
3384 Py_DECREF(kwargs);
3385 kwargs = d;
3386 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003387 assert(PyDict_CheckExact(kwargs));
3388 }
3389 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003390 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003391 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003392 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003393 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003394 goto error;
3395 }
3396 Py_SETREF(callargs, PySequence_Tuple(callargs));
3397 if (callargs == NULL) {
3398 goto error;
3399 }
3400 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003401 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003402
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003403 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003404 Py_DECREF(func);
3405 Py_DECREF(callargs);
3406 Py_XDECREF(kwargs);
3407
3408 SET_TOP(result);
3409 if (result == NULL) {
3410 goto error;
3411 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003412 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003413 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003414
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003415 TARGET(MAKE_FUNCTION) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003416 PyObject *qualname = POP();
3417 PyObject *codeobj = POP();
3418 PyFunctionObject *func = (PyFunctionObject *)
3419 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003420
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003421 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003422 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003423 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003424 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003425 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003426
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003427 if (oparg & 0x08) {
3428 assert(PyTuple_CheckExact(TOP()));
3429 func ->func_closure = POP();
3430 }
3431 if (oparg & 0x04) {
3432 assert(PyDict_CheckExact(TOP()));
3433 func->func_annotations = POP();
3434 }
3435 if (oparg & 0x02) {
3436 assert(PyDict_CheckExact(TOP()));
3437 func->func_kwdefaults = POP();
3438 }
3439 if (oparg & 0x01) {
3440 assert(PyTuple_CheckExact(TOP()));
3441 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003442 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003443
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003444 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003445 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003446 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003447
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003448 TARGET(BUILD_SLICE) {
3449 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003450 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003451 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003452 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003453 step = NULL;
3454 stop = POP();
3455 start = TOP();
3456 slice = PySlice_New(start, stop, step);
3457 Py_DECREF(start);
3458 Py_DECREF(stop);
3459 Py_XDECREF(step);
3460 SET_TOP(slice);
3461 if (slice == NULL)
3462 goto error;
3463 DISPATCH();
3464 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003465
Eric V. Smitha78c7952015-11-03 12:45:05 -05003466 TARGET(FORMAT_VALUE) {
3467 /* Handles f-string value formatting. */
3468 PyObject *result;
3469 PyObject *fmt_spec;
3470 PyObject *value;
3471 PyObject *(*conv_fn)(PyObject *);
3472 int which_conversion = oparg & FVC_MASK;
3473 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3474
3475 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003476 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003477
3478 /* See if any conversion is specified. */
3479 switch (which_conversion) {
3480 case FVC_STR: conv_fn = PyObject_Str; break;
3481 case FVC_REPR: conv_fn = PyObject_Repr; break;
3482 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3483
3484 /* Must be 0 (meaning no conversion), since only four
3485 values are allowed by (oparg & FVC_MASK). */
3486 default: conv_fn = NULL; break;
3487 }
3488
3489 /* If there's a conversion function, call it and replace
3490 value with that result. Otherwise, just use value,
3491 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003492 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003493 result = conv_fn(value);
3494 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003495 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003496 Py_XDECREF(fmt_spec);
3497 goto error;
3498 }
3499 value = result;
3500 }
3501
3502 /* If value is a unicode object, and there's no fmt_spec,
3503 then we know the result of format(value) is value
3504 itself. In that case, skip calling format(). I plan to
3505 move this optimization in to PyObject_Format()
3506 itself. */
3507 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3508 /* Do nothing, just transfer ownership to result. */
3509 result = value;
3510 } else {
3511 /* Actually call format(). */
3512 result = PyObject_Format(value, fmt_spec);
3513 Py_DECREF(value);
3514 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003515 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003516 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003517 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003518 }
3519
Eric V. Smith135d5f42016-02-05 18:23:08 -05003520 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003521 DISPATCH();
3522 }
3523
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003524 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003525 int oldoparg = oparg;
3526 NEXTOPARG();
3527 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003528 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003529 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003530
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003531
Antoine Pitrou042b1282010-08-13 21:15:58 +00003532#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003533 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003534#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003535 default:
3536 fprintf(stderr,
3537 "XXX lineno: %d, opcode: %d\n",
3538 PyFrame_GetLineNumber(f),
3539 opcode);
3540 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003541 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003542
3543#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003544 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003545#endif
3546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003547 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003548
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003549 /* This should never be reached. Every opcode should end with DISPATCH()
3550 or goto error. */
3551 assert(0);
Guido van Rossumac7be682001-01-17 15:42:30 +00003552
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003553error:
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003554
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003555 assert(why == WHY_NOT);
3556 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003557
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003558 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003559#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003560 if (!PyErr_Occurred())
3561 PyErr_SetString(PyExc_SystemError,
3562 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003563#else
3564 assert(PyErr_Occurred());
3565#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003566
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003567 /* Log traceback info. */
3568 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003569
Benjamin Peterson51f46162013-01-23 08:38:47 -05003570 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003571 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3572 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003573
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003574fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003575 assert(why != WHY_NOT);
3576
3577 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003578 while (why != WHY_NOT && f->f_iblock > 0) {
3579 /* Peek at the current block. */
3580 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003582 assert(why != WHY_YIELD);
3583 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3584 why = WHY_NOT;
3585 JUMPTO(PyLong_AS_LONG(retval));
3586 Py_DECREF(retval);
3587 break;
3588 }
3589 /* Now we have to pop the block. */
3590 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003592 if (b->b_type == EXCEPT_HANDLER) {
3593 UNWIND_EXCEPT_HANDLER(b);
3594 continue;
3595 }
3596 UNWIND_BLOCK(b);
3597 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3598 why = WHY_NOT;
3599 JUMPTO(b->b_handler);
3600 break;
3601 }
3602 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3603 || b->b_type == SETUP_FINALLY)) {
3604 PyObject *exc, *val, *tb;
3605 int handler = b->b_handler;
3606 /* Beware, this invalidates all b->b_* fields */
3607 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3608 PUSH(tstate->exc_traceback);
3609 PUSH(tstate->exc_value);
3610 if (tstate->exc_type != NULL) {
3611 PUSH(tstate->exc_type);
3612 }
3613 else {
3614 Py_INCREF(Py_None);
3615 PUSH(Py_None);
3616 }
3617 PyErr_Fetch(&exc, &val, &tb);
3618 /* Make the raw exception data
3619 available to the handler,
3620 so a program can emulate the
3621 Python main loop. */
3622 PyErr_NormalizeException(
3623 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003624 if (tb != NULL)
3625 PyException_SetTraceback(val, tb);
3626 else
3627 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003628 Py_INCREF(exc);
3629 tstate->exc_type = exc;
3630 Py_INCREF(val);
3631 tstate->exc_value = val;
3632 tstate->exc_traceback = tb;
3633 if (tb == NULL)
3634 tb = Py_None;
3635 Py_INCREF(tb);
3636 PUSH(tb);
3637 PUSH(val);
3638 PUSH(exc);
3639 why = WHY_NOT;
3640 JUMPTO(handler);
3641 break;
3642 }
3643 if (b->b_type == SETUP_FINALLY) {
3644 if (why & (WHY_RETURN | WHY_CONTINUE))
3645 PUSH(retval);
3646 PUSH(PyLong_FromLong((long)why));
3647 why = WHY_NOT;
3648 JUMPTO(b->b_handler);
3649 break;
3650 }
3651 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003653 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003655 if (why != WHY_NOT)
3656 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00003657
Victor Stinnerace47d72013-07-18 01:41:08 +02003658 assert(!PyErr_Occurred());
3659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003660 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003662 assert(why != WHY_YIELD);
3663 /* Pop remaining stack entries. */
3664 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003665 PyObject *o = POP();
3666 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003667 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003669 if (why != WHY_RETURN)
3670 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003671
Victor Stinner4a7cc882015-03-06 23:35:27 +01003672 assert((retval != NULL) ^ (PyErr_Occurred() != NULL));
Victor Stinnerace47d72013-07-18 01:41:08 +02003673
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003674fast_yield:
Yury Selivanoveb636452016-09-08 22:01:51 -07003675 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01003676
Benjamin Petersonac913412011-07-03 16:25:11 -05003677 /* The purpose of this block is to put aside the generator's exception
3678 state and restore that of the calling frame. If the current
3679 exception state is from the caller, we clear the exception values
3680 on the generator frame, so they are not swapped back in latter. The
3681 origin of the current exception state is determined by checking for
3682 except handler blocks, which we must be in iff a new exception
3683 state came into existence in this frame. (An uncaught exception
3684 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3685 int i;
Victor Stinner74319ae2016-08-25 00:04:09 +02003686 for (i = 0; i < f->f_iblock; i++) {
3687 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER) {
Benjamin Petersonac913412011-07-03 16:25:11 -05003688 break;
Victor Stinner74319ae2016-08-25 00:04:09 +02003689 }
3690 }
Benjamin Petersonac913412011-07-03 16:25:11 -05003691 if (i == f->f_iblock)
3692 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003693 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003694 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003695 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003696 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003698 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003699 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003700 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003701 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3702 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003703 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003704 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003705 why = WHY_EXCEPTION;
3706 }
3707 }
3708 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003709 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3710 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003711 PyTrace_RETURN, NULL);
3712 }
3713 }
3714 if (tstate->c_profilefunc) {
3715 if (why == WHY_EXCEPTION)
3716 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003717 tstate->c_profileobj,
3718 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003719 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003720 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3721 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003722 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003723 Py_CLEAR(retval);
Brett Cannonb94767f2011-02-22 20:15:44 +00003724 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003725 }
3726 }
3727 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003729 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003730exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003731 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3732 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003733 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003734 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003735 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003736
Victor Stinnerefde1462015-03-21 15:04:43 +01003737 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003738}
3739
Benjamin Petersonb204a422011-06-05 22:04:07 -05003740static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003741format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3742{
3743 int err;
3744 Py_ssize_t len = PyList_GET_SIZE(names);
3745 PyObject *name_str, *comma, *tail, *tmp;
3746
3747 assert(PyList_CheckExact(names));
3748 assert(len >= 1);
3749 /* Deal with the joys of natural language. */
3750 switch (len) {
3751 case 1:
3752 name_str = PyList_GET_ITEM(names, 0);
3753 Py_INCREF(name_str);
3754 break;
3755 case 2:
3756 name_str = PyUnicode_FromFormat("%U and %U",
3757 PyList_GET_ITEM(names, len - 2),
3758 PyList_GET_ITEM(names, len - 1));
3759 break;
3760 default:
3761 tail = PyUnicode_FromFormat(", %U, and %U",
3762 PyList_GET_ITEM(names, len - 2),
3763 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003764 if (tail == NULL)
3765 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003766 /* Chop off the last two objects in the list. This shouldn't actually
3767 fail, but we can't be too careful. */
3768 err = PyList_SetSlice(names, len - 2, len, NULL);
3769 if (err == -1) {
3770 Py_DECREF(tail);
3771 return;
3772 }
3773 /* Stitch everything up into a nice comma-separated list. */
3774 comma = PyUnicode_FromString(", ");
3775 if (comma == NULL) {
3776 Py_DECREF(tail);
3777 return;
3778 }
3779 tmp = PyUnicode_Join(comma, names);
3780 Py_DECREF(comma);
3781 if (tmp == NULL) {
3782 Py_DECREF(tail);
3783 return;
3784 }
3785 name_str = PyUnicode_Concat(tmp, tail);
3786 Py_DECREF(tmp);
3787 Py_DECREF(tail);
3788 break;
3789 }
3790 if (name_str == NULL)
3791 return;
3792 PyErr_Format(PyExc_TypeError,
3793 "%U() missing %i required %s argument%s: %U",
3794 co->co_name,
3795 len,
3796 kind,
3797 len == 1 ? "" : "s",
3798 name_str);
3799 Py_DECREF(name_str);
3800}
3801
3802static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003803missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003804 PyObject **fastlocals)
3805{
Victor Stinner74319ae2016-08-25 00:04:09 +02003806 Py_ssize_t i, j = 0;
3807 Py_ssize_t start, end;
3808 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003809 const char *kind = positional ? "positional" : "keyword-only";
3810 PyObject *missing_names;
3811
3812 /* Compute the names of the arguments that are missing. */
3813 missing_names = PyList_New(missing);
3814 if (missing_names == NULL)
3815 return;
3816 if (positional) {
3817 start = 0;
3818 end = co->co_argcount - defcount;
3819 }
3820 else {
3821 start = co->co_argcount;
3822 end = start + co->co_kwonlyargcount;
3823 }
3824 for (i = start; i < end; i++) {
3825 if (GETLOCAL(i) == NULL) {
3826 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3827 PyObject *name = PyObject_Repr(raw);
3828 if (name == NULL) {
3829 Py_DECREF(missing_names);
3830 return;
3831 }
3832 PyList_SET_ITEM(missing_names, j++, name);
3833 }
3834 }
3835 assert(j == missing);
3836 format_missing(kind, co, missing_names);
3837 Py_DECREF(missing_names);
3838}
3839
3840static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003841too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3842 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003843{
3844 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003845 Py_ssize_t kwonly_given = 0;
3846 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003847 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003848 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003849
Benjamin Petersone109c702011-06-24 09:37:26 -05003850 assert((co->co_flags & CO_VARARGS) == 0);
3851 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003852 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3853 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003854 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003855 }
3856 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003857 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003858 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003859 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003860 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003861 }
3862 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003863 plural = (co_argcount != 1);
3864 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003865 }
3866 if (sig == NULL)
3867 return;
3868 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003869 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3870 kwonly_sig = PyUnicode_FromFormat(format,
3871 given != 1 ? "s" : "",
3872 kwonly_given,
3873 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003874 if (kwonly_sig == NULL) {
3875 Py_DECREF(sig);
3876 return;
3877 }
3878 }
3879 else {
3880 /* This will not fail. */
3881 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003882 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003883 }
3884 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003885 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003886 co->co_name,
3887 sig,
3888 plural ? "s" : "",
3889 given,
3890 kwonly_sig,
3891 given == 1 && !kwonly_given ? "was" : "were");
3892 Py_DECREF(sig);
3893 Py_DECREF(kwonly_sig);
3894}
3895
Guido van Rossumc2e20742006-02-27 22:32:47 +00003896/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003897 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003898 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003899
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003900PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003901_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Victor Stinner74319ae2016-08-25 00:04:09 +02003902 PyObject **args, Py_ssize_t argcount,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003903 PyObject **kwnames, PyObject **kwargs,
3904 Py_ssize_t kwcount, int kwstep,
Victor Stinner74319ae2016-08-25 00:04:09 +02003905 PyObject **defs, Py_ssize_t defcount,
3906 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003907 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003908{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003909 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003910 PyFrameObject *f;
3911 PyObject *retval = NULL;
3912 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003913 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003914 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003915 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3916 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003917 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003919 if (globals == NULL) {
3920 PyErr_SetString(PyExc_SystemError,
3921 "PyEval_EvalCodeEx: NULL globals");
3922 return NULL;
3923 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003924
Victor Stinnerc7020012016-08-16 23:40:29 +02003925 /* Create the frame */
3926 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003927 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003928 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003929 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003930 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003931 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003932 fastlocals = f->f_localsplus;
3933 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003934
Victor Stinnerc7020012016-08-16 23:40:29 +02003935 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003936 if (co->co_flags & CO_VARKEYWORDS) {
3937 kwdict = PyDict_New();
3938 if (kwdict == NULL)
3939 goto fail;
3940 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003941 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003942 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003943 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003944 SETLOCAL(i, kwdict);
3945 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003946 else {
3947 kwdict = NULL;
3948 }
3949
3950 /* Copy positional arguments into local variables */
3951 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003952 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003953 }
3954 else {
3955 n = argcount;
3956 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003957 for (i = 0; i < n; i++) {
3958 x = args[i];
3959 Py_INCREF(x);
3960 SETLOCAL(i, x);
3961 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003962
3963 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003964 if (co->co_flags & CO_VARARGS) {
3965 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003966 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003967 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003968 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003969 SETLOCAL(total_args, u);
3970 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003971 x = args[i];
3972 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003973 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003974 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003975 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003976
Serhiy Storchakab7281052016-09-12 00:52:40 +03003977 /* Handle keyword arguments passed as two strided arrays */
3978 kwcount *= kwstep;
3979 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003980 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003981 PyObject *keyword = kwnames[i];
3982 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003983 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003984
Benjamin Petersonb204a422011-06-05 22:04:07 -05003985 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3986 PyErr_Format(PyExc_TypeError,
3987 "%U() keywords must be strings",
3988 co->co_name);
3989 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003990 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003991
Benjamin Petersonb204a422011-06-05 22:04:07 -05003992 /* Speed hack: do raw pointer compares. As names are
3993 normally interned this should almost always hit. */
3994 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3995 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003996 PyObject *name = co_varnames[j];
3997 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003998 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003999 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004000 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004001
Benjamin Petersonb204a422011-06-05 22:04:07 -05004002 /* Slow fallback, just in case */
4003 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004004 PyObject *name = co_varnames[j];
4005 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4006 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004007 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004008 }
4009 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004010 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004011 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004012 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004013
Victor Stinner231d1f32017-01-11 02:12:06 +01004014 assert(j >= total_args);
4015 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004016 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02004017 "%U() got an unexpected keyword argument '%S'",
4018 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004019 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004020 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004021
Christian Heimes0bd447f2013-07-20 14:48:10 +02004022 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4023 goto fail;
4024 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004025 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004026
Benjamin Petersonb204a422011-06-05 22:04:07 -05004027 kw_found:
4028 if (GETLOCAL(j) != NULL) {
4029 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02004030 "%U() got multiple values for argument '%S'",
4031 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004032 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004033 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004034 Py_INCREF(value);
4035 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004036 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004037
4038 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004039 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004040 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004041 goto fail;
4042 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004043
4044 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004045 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02004046 Py_ssize_t m = co->co_argcount - defcount;
4047 Py_ssize_t missing = 0;
4048 for (i = argcount; i < m; i++) {
4049 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004050 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004051 }
4052 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004053 if (missing) {
4054 missing_arguments(co, missing, defcount, fastlocals);
4055 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004056 }
4057 if (n > m)
4058 i = n - m;
4059 else
4060 i = 0;
4061 for (; i < defcount; i++) {
4062 if (GETLOCAL(m+i) == NULL) {
4063 PyObject *def = defs[i];
4064 Py_INCREF(def);
4065 SETLOCAL(m+i, def);
4066 }
4067 }
4068 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004069
4070 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004071 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004072 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004073 for (i = co->co_argcount; i < total_args; i++) {
4074 PyObject *name;
4075 if (GETLOCAL(i) != NULL)
4076 continue;
4077 name = PyTuple_GET_ITEM(co->co_varnames, i);
4078 if (kwdefs != NULL) {
4079 PyObject *def = PyDict_GetItem(kwdefs, name);
4080 if (def) {
4081 Py_INCREF(def);
4082 SETLOCAL(i, def);
4083 continue;
4084 }
4085 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004086 missing++;
4087 }
4088 if (missing) {
4089 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004090 goto fail;
4091 }
4092 }
4093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004094 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004095 vars into frame. */
4096 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004097 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004098 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004099 /* Possibly account for the cell variable being an argument. */
4100 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004101 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004102 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004103 /* Clear the local copy. */
4104 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004105 }
4106 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004107 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004108 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004109 if (c == NULL)
4110 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004111 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004112 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004113
4114 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004115 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4116 PyObject *o = PyTuple_GET_ITEM(closure, i);
4117 Py_INCREF(o);
4118 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004119 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004120
Yury Selivanoveb636452016-09-08 22:01:51 -07004121 /* Handle generator/coroutine/asynchronous generator */
4122 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004123 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04004124 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004125 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004126
4127 if (is_coro && tstate->in_coroutine_wrapper) {
4128 assert(coro_wrapper != NULL);
4129 PyErr_Format(PyExc_RuntimeError,
4130 "coroutine wrapper %.200R attempted "
4131 "to recursively wrap %.200R",
4132 coro_wrapper,
4133 co);
4134 goto fail;
4135 }
Yury Selivanov75445082015-05-11 22:57:16 -04004136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004137 /* Don't need to keep the reference to f_back, it will be set
4138 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004139 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004141 /* Create a new generator that owns the ready to run frame
4142 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004143 if (is_coro) {
4144 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004145 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4146 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004147 } else {
4148 gen = PyGen_NewWithQualName(f, name, qualname);
4149 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004150 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004151 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004152 }
INADA Naoki9c157762016-12-26 18:52:46 +09004153
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004154 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004155
Yury Selivanov94c22632015-06-04 10:16:51 -04004156 if (is_coro && coro_wrapper != NULL) {
4157 PyObject *wrapped;
4158 tstate->in_coroutine_wrapper = 1;
4159 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4160 tstate->in_coroutine_wrapper = 0;
4161 return wrapped;
4162 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004163
Yury Selivanov75445082015-05-11 22:57:16 -04004164 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004165 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004166
Victor Stinner59a73272016-12-09 18:51:13 +01004167 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004168
Thomas Woutersce272b62007-09-19 21:19:28 +00004169fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004171 /* decref'ing the frame can cause __del__ methods to get invoked,
4172 which can call back into Python. While we're done with the
4173 current Python frame (f), the associated C stack is still in use,
4174 so recursion_depth must be boosted for the duration.
4175 */
4176 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09004177 if (Py_REFCNT(f) > 1) {
4178 Py_DECREF(f);
4179 _PyObject_GC_TRACK(f);
4180 }
4181 else {
4182 ++tstate->recursion_depth;
4183 Py_DECREF(f);
4184 --tstate->recursion_depth;
4185 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004186 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004187}
4188
Victor Stinner40ee3012014-06-16 15:59:28 +02004189PyObject *
4190PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
4191 PyObject **args, int argcount, PyObject **kws, int kwcount,
4192 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
4193{
4194 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004195 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004196 kws, kws != NULL ? kws + 1 : NULL,
4197 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004198 defs, defcount,
4199 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004200 NULL, NULL);
4201}
Tim Peters5ca576e2001-06-18 22:08:13 +00004202
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004203static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004204special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004206 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004207 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004208 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004209 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004210 return NULL;
4211 }
4212 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004213}
4214
4215
Benjamin Peterson87880242011-07-03 16:48:31 -05004216/* These 3 functions deal with the exception state of generators. */
4217
4218static void
4219save_exc_state(PyThreadState *tstate, PyFrameObject *f)
4220{
4221 PyObject *type, *value, *traceback;
4222 Py_XINCREF(tstate->exc_type);
4223 Py_XINCREF(tstate->exc_value);
4224 Py_XINCREF(tstate->exc_traceback);
4225 type = f->f_exc_type;
4226 value = f->f_exc_value;
4227 traceback = f->f_exc_traceback;
4228 f->f_exc_type = tstate->exc_type;
4229 f->f_exc_value = tstate->exc_value;
4230 f->f_exc_traceback = tstate->exc_traceback;
4231 Py_XDECREF(type);
4232 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004233 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05004234}
4235
4236static void
4237swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
4238{
4239 PyObject *tmp;
4240 tmp = tstate->exc_type;
4241 tstate->exc_type = f->f_exc_type;
4242 f->f_exc_type = tmp;
4243 tmp = tstate->exc_value;
4244 tstate->exc_value = f->f_exc_value;
4245 f->f_exc_value = tmp;
4246 tmp = tstate->exc_traceback;
4247 tstate->exc_traceback = f->f_exc_traceback;
4248 f->f_exc_traceback = tmp;
4249}
4250
4251static void
4252restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
4253{
4254 PyObject *type, *value, *tb;
4255 type = tstate->exc_type;
4256 value = tstate->exc_value;
4257 tb = tstate->exc_traceback;
4258 tstate->exc_type = f->f_exc_type;
4259 tstate->exc_value = f->f_exc_value;
4260 tstate->exc_traceback = f->f_exc_traceback;
4261 f->f_exc_type = NULL;
4262 f->f_exc_value = NULL;
4263 f->f_exc_traceback = NULL;
4264 Py_XDECREF(type);
4265 Py_XDECREF(value);
4266 Py_XDECREF(tb);
4267}
4268
4269
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004270/* Logic for the raise statement (too complicated for inlining).
4271 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004272static int
Collin Winter828f04a2007-08-31 00:04:24 +00004273do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004275 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004277 if (exc == NULL) {
4278 /* Reraise */
4279 PyThreadState *tstate = PyThreadState_GET();
4280 PyObject *tb;
4281 type = tstate->exc_type;
4282 value = tstate->exc_value;
4283 tb = tstate->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004284 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004285 PyErr_SetString(PyExc_RuntimeError,
4286 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004287 return 0;
4288 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004289 Py_XINCREF(type);
4290 Py_XINCREF(value);
4291 Py_XINCREF(tb);
4292 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004293 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004294 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004296 /* We support the following forms of raise:
4297 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004298 raise <instance>
4299 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004301 if (PyExceptionClass_Check(exc)) {
4302 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004303 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004304 if (value == NULL)
4305 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004306 if (!PyExceptionInstance_Check(value)) {
4307 PyErr_Format(PyExc_TypeError,
4308 "calling %R should have returned an instance of "
4309 "BaseException, not %R",
4310 type, Py_TYPE(value));
4311 goto raise_error;
4312 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004313 }
4314 else if (PyExceptionInstance_Check(exc)) {
4315 value = exc;
4316 type = PyExceptionInstance_Class(exc);
4317 Py_INCREF(type);
4318 }
4319 else {
4320 /* Not something you can raise. You get an exception
4321 anyway, just not what you specified :-) */
4322 Py_DECREF(exc);
4323 PyErr_SetString(PyExc_TypeError,
4324 "exceptions must derive from BaseException");
4325 goto raise_error;
4326 }
Collin Winter828f04a2007-08-31 00:04:24 +00004327
Serhiy Storchakac0191582016-09-27 11:37:10 +03004328 assert(type != NULL);
4329 assert(value != NULL);
4330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004331 if (cause) {
4332 PyObject *fixed_cause;
4333 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004334 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004335 if (fixed_cause == NULL)
4336 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004337 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004338 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004339 else if (PyExceptionInstance_Check(cause)) {
4340 fixed_cause = cause;
4341 }
4342 else if (cause == Py_None) {
4343 Py_DECREF(cause);
4344 fixed_cause = NULL;
4345 }
4346 else {
4347 PyErr_SetString(PyExc_TypeError,
4348 "exception causes must derive from "
4349 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004350 goto raise_error;
4351 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004352 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004353 }
Collin Winter828f04a2007-08-31 00:04:24 +00004354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004355 PyErr_SetObject(type, value);
4356 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004357 Py_DECREF(value);
4358 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004359 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004360
4361raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004362 Py_XDECREF(value);
4363 Py_XDECREF(type);
4364 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004365 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004366}
4367
Tim Petersd6d010b2001-06-21 02:49:55 +00004368/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004369 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004370
Guido van Rossum0368b722007-05-11 16:50:42 +00004371 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4372 with a variable target.
4373*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004374
Barry Warsawe42b18f1997-08-25 22:13:04 +00004375static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004376unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004377{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004378 int i = 0, j = 0;
4379 Py_ssize_t ll = 0;
4380 PyObject *it; /* iter(v) */
4381 PyObject *w;
4382 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004384 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004386 it = PyObject_GetIter(v);
4387 if (it == NULL)
4388 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00004389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004390 for (; i < argcnt; i++) {
4391 w = PyIter_Next(it);
4392 if (w == NULL) {
4393 /* Iterator done, via error or exhaustion. */
4394 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004395 if (argcntafter == -1) {
4396 PyErr_Format(PyExc_ValueError,
4397 "not enough values to unpack (expected %d, got %d)",
4398 argcnt, i);
4399 }
4400 else {
4401 PyErr_Format(PyExc_ValueError,
4402 "not enough values to unpack "
4403 "(expected at least %d, got %d)",
4404 argcnt + argcntafter, i);
4405 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004406 }
4407 goto Error;
4408 }
4409 *--sp = w;
4410 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004412 if (argcntafter == -1) {
4413 /* We better have exhausted the iterator now. */
4414 w = PyIter_Next(it);
4415 if (w == NULL) {
4416 if (PyErr_Occurred())
4417 goto Error;
4418 Py_DECREF(it);
4419 return 1;
4420 }
4421 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004422 PyErr_Format(PyExc_ValueError,
4423 "too many values to unpack (expected %d)",
4424 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004425 goto Error;
4426 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004428 l = PySequence_List(it);
4429 if (l == NULL)
4430 goto Error;
4431 *--sp = l;
4432 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004434 ll = PyList_GET_SIZE(l);
4435 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004436 PyErr_Format(PyExc_ValueError,
4437 "not enough values to unpack (expected at least %d, got %zd)",
4438 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004439 goto Error;
4440 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004442 /* Pop the "after-variable" args off the list. */
4443 for (j = argcntafter; j > 0; j--, i++) {
4444 *--sp = PyList_GET_ITEM(l, ll - j);
4445 }
4446 /* Resize the list. */
4447 Py_SIZE(l) = ll - argcntafter;
4448 Py_DECREF(it);
4449 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004450
Tim Petersd6d010b2001-06-21 02:49:55 +00004451Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004452 for (; i > 0; i--, sp++)
4453 Py_DECREF(*sp);
4454 Py_XDECREF(it);
4455 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004456}
4457
4458
Guido van Rossum96a42c81992-01-12 02:29:51 +00004459#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004460static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004461prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004462{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004463 printf("%s ", str);
4464 if (PyObject_Print(v, stdout, 0) != 0)
4465 PyErr_Clear(); /* Don't know what else to do */
4466 printf("\n");
4467 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004468}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004469#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004470
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004471static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004472call_exc_trace(Py_tracefunc func, PyObject *self,
4473 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004474{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004475 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004476 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004477 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004478 if (value == NULL) {
4479 value = Py_None;
4480 Py_INCREF(value);
4481 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004482 PyErr_NormalizeException(&type, &value, &orig_traceback);
4483 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004484 arg = PyTuple_Pack(3, type, value, traceback);
4485 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004486 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004487 return;
4488 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004489 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004490 Py_DECREF(arg);
4491 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004492 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004493 else {
4494 Py_XDECREF(type);
4495 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004496 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004497 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004498}
4499
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004500static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004501call_trace_protected(Py_tracefunc func, PyObject *obj,
4502 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004503 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004504{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004505 PyObject *type, *value, *traceback;
4506 int err;
4507 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004508 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004509 if (err == 0)
4510 {
4511 PyErr_Restore(type, value, traceback);
4512 return 0;
4513 }
4514 else {
4515 Py_XDECREF(type);
4516 Py_XDECREF(value);
4517 Py_XDECREF(traceback);
4518 return -1;
4519 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004520}
4521
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004522static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004523call_trace(Py_tracefunc func, PyObject *obj,
4524 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004525 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004526{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004527 int result;
4528 if (tstate->tracing)
4529 return 0;
4530 tstate->tracing++;
4531 tstate->use_tracing = 0;
4532 result = func(obj, frame, what, arg);
4533 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4534 || (tstate->c_profilefunc != NULL));
4535 tstate->tracing--;
4536 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004537}
4538
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004539PyObject *
4540_PyEval_CallTracing(PyObject *func, PyObject *args)
4541{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004542 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004543 int save_tracing = tstate->tracing;
4544 int save_use_tracing = tstate->use_tracing;
4545 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004547 tstate->tracing = 0;
4548 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4549 || (tstate->c_profilefunc != NULL));
4550 result = PyObject_Call(func, args, NULL);
4551 tstate->tracing = save_tracing;
4552 tstate->use_tracing = save_use_tracing;
4553 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004554}
4555
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004556/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004557static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004558maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004559 PyThreadState *tstate, PyFrameObject *frame,
4560 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004561{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004562 int result = 0;
4563 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004565 /* If the last instruction executed isn't in the current
4566 instruction window, reset the window.
4567 */
4568 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4569 PyAddrPair bounds;
4570 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4571 &bounds);
4572 *instr_lb = bounds.ap_lower;
4573 *instr_ub = bounds.ap_upper;
4574 }
4575 /* If the last instruction falls at the start of a line or if
4576 it represents a jump backwards, update the frame's line
4577 number and call the trace function. */
4578 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
4579 frame->f_lineno = line;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004580 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004581 }
4582 *instr_prev = frame->f_lasti;
4583 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004584}
4585
Fred Drake5755ce62001-06-27 19:19:46 +00004586void
4587PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004588{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004589 PyThreadState *tstate = PyThreadState_GET();
4590 PyObject *temp = tstate->c_profileobj;
4591 Py_XINCREF(arg);
4592 tstate->c_profilefunc = NULL;
4593 tstate->c_profileobj = NULL;
4594 /* Must make sure that tracing is not ignored if 'temp' is freed */
4595 tstate->use_tracing = tstate->c_tracefunc != NULL;
4596 Py_XDECREF(temp);
4597 tstate->c_profilefunc = func;
4598 tstate->c_profileobj = arg;
4599 /* Flag that tracing or profiling is turned on */
4600 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004601}
4602
4603void
4604PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4605{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004606 PyThreadState *tstate = PyThreadState_GET();
4607 PyObject *temp = tstate->c_traceobj;
4608 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4609 Py_XINCREF(arg);
4610 tstate->c_tracefunc = NULL;
4611 tstate->c_traceobj = NULL;
4612 /* Must make sure that profiling is not ignored if 'temp' is freed */
4613 tstate->use_tracing = tstate->c_profilefunc != NULL;
4614 Py_XDECREF(temp);
4615 tstate->c_tracefunc = func;
4616 tstate->c_traceobj = arg;
4617 /* Flag that tracing or profiling is turned on */
4618 tstate->use_tracing = ((func != NULL)
4619 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004620}
4621
Yury Selivanov75445082015-05-11 22:57:16 -04004622void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004623_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004624{
4625 PyThreadState *tstate = PyThreadState_GET();
4626
Yury Selivanov75445082015-05-11 22:57:16 -04004627 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004628 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004629}
4630
4631PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004632_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004633{
4634 PyThreadState *tstate = PyThreadState_GET();
4635 return tstate->coroutine_wrapper;
4636}
4637
Yury Selivanoveb636452016-09-08 22:01:51 -07004638void
4639_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4640{
4641 PyThreadState *tstate = PyThreadState_GET();
4642
4643 Py_XINCREF(firstiter);
4644 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4645}
4646
4647PyObject *
4648_PyEval_GetAsyncGenFirstiter(void)
4649{
4650 PyThreadState *tstate = PyThreadState_GET();
4651 return tstate->async_gen_firstiter;
4652}
4653
4654void
4655_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4656{
4657 PyThreadState *tstate = PyThreadState_GET();
4658
4659 Py_XINCREF(finalizer);
4660 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4661}
4662
4663PyObject *
4664_PyEval_GetAsyncGenFinalizer(void)
4665{
4666 PyThreadState *tstate = PyThreadState_GET();
4667 return tstate->async_gen_finalizer;
4668}
4669
Guido van Rossumb209a111997-04-29 18:18:01 +00004670PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004671PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004672{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004673 PyFrameObject *current_frame = PyEval_GetFrame();
4674 if (current_frame == NULL)
4675 return PyThreadState_GET()->interp->builtins;
4676 else
4677 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004678}
4679
Guido van Rossumb209a111997-04-29 18:18:01 +00004680PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004681PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004682{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004683 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004684 if (current_frame == NULL) {
4685 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004686 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004687 }
4688
4689 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4690 return NULL;
4691
4692 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004693 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004694}
4695
Guido van Rossumb209a111997-04-29 18:18:01 +00004696PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004697PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004698{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004699 PyFrameObject *current_frame = PyEval_GetFrame();
4700 if (current_frame == NULL)
4701 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004702
4703 assert(current_frame->f_globals != NULL);
4704 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004705}
4706
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004707PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004708PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004709{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004710 PyThreadState *tstate = PyThreadState_GET();
4711 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004712}
4713
Guido van Rossum6135a871995-01-09 17:53:26 +00004714int
Tim Peters5ba58662001-07-16 02:29:45 +00004715PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004716{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004717 PyFrameObject *current_frame = PyEval_GetFrame();
4718 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004719
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004720 if (current_frame != NULL) {
4721 const int codeflags = current_frame->f_code->co_flags;
4722 const int compilerflags = codeflags & PyCF_MASK;
4723 if (compilerflags) {
4724 result = 1;
4725 cf->cf_flags |= compilerflags;
4726 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004727#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004728 if (codeflags & CO_GENERATOR_ALLOWED) {
4729 result = 1;
4730 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4731 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004732#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004733 }
4734 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004735}
4736
Guido van Rossum3f5da241990-12-20 15:06:42 +00004737
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004738const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004739PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004740{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004741 if (PyMethod_Check(func))
4742 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4743 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004744 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004745 else if (PyCFunction_Check(func))
4746 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4747 else
4748 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004749}
4750
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004751const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004752PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004753{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004754 if (PyMethod_Check(func))
4755 return "()";
4756 else if (PyFunction_Check(func))
4757 return "()";
4758 else if (PyCFunction_Check(func))
4759 return "()";
4760 else
4761 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004762}
4763
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004764#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004765if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004766 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4767 tstate, tstate->frame, \
4768 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004769 x = NULL; \
4770 } \
4771 else { \
4772 x = call; \
4773 if (tstate->c_profilefunc != NULL) { \
4774 if (x == NULL) { \
4775 call_trace_protected(tstate->c_profilefunc, \
4776 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004777 tstate, tstate->frame, \
4778 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004779 /* XXX should pass (type, value, tb) */ \
4780 } else { \
4781 if (call_trace(tstate->c_profilefunc, \
4782 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004783 tstate, tstate->frame, \
4784 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004785 Py_DECREF(x); \
4786 x = NULL; \
4787 } \
4788 } \
4789 } \
4790 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004791} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004792 x = call; \
4793 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004794
Victor Stinner415c5102017-01-11 00:54:57 +01004795/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4796 to reduce the stack consumption. */
4797Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004798call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004799{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004800 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004801 PyObject *func = *pfunc;
4802 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004803 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4804 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004805 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004807 /* Always dispatch PyCFunction first, because these are
4808 presumed to be the most frequent callable object.
4809 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004810 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004811 PyThreadState *tstate = PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004812 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004813 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004814 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
4815 PyThreadState *tstate = PyThreadState_GET();
INADA Naoki93fac8d2017-03-07 14:24:37 +09004816 if (tstate->use_tracing && tstate->c_profilefunc) {
4817 // We need to create PyCFunctionObject for tracing.
4818 PyMethodDescrObject *descr = (PyMethodDescrObject*)func;
4819 func = PyCFunction_NewEx(descr->d_method, stack[0], NULL);
4820 if (func == NULL) {
4821 return NULL;
4822 }
4823 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack+1, nargs-1,
4824 kwnames));
4825 Py_DECREF(func);
4826 }
4827 else {
4828 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4829 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004830 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004831 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004832 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004833 /* Optimize access to bound methods. Reuse the Python stack
4834 to pass 'self' as the first argument, replace 'func'
4835 with 'self'. It avoids the creation of a new temporary tuple
4836 for arguments (to replace func with self) when the method uses
4837 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004838 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004839 Py_INCREF(self);
4840 func = PyMethod_GET_FUNCTION(func);
4841 Py_INCREF(func);
4842 Py_SETREF(*pfunc, self);
4843 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004844 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004845 }
4846 else {
4847 Py_INCREF(func);
4848 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004849
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004850 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004851 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004852 }
4853 else {
4854 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4855 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004856 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004857 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004858
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004859 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4860
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004861 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004862 while ((*pp_stack) > pfunc) {
4863 w = EXT_POP(*pp_stack);
4864 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004865 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004867 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004868}
4869
Jeremy Hylton52820442001-01-03 23:52:36 +00004870static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004871do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004872{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004873 if (PyCFunction_Check(func)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004874 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004875 PyThreadState *tstate = PyThreadState_GET();
4876 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004877 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004878 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004879 else {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004880 return PyObject_Call(func, callargs, kwdict);
Victor Stinner74319ae2016-08-25 00:04:09 +02004881 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004882}
4883
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004884/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004885 nb_index slot defined, and store in *pi.
4886 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004887 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004888 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004889*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004890int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004891_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004892{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004893 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004894 Py_ssize_t x;
4895 if (PyIndex_Check(v)) {
4896 x = PyNumber_AsSsize_t(v, NULL);
4897 if (x == -1 && PyErr_Occurred())
4898 return 0;
4899 }
4900 else {
4901 PyErr_SetString(PyExc_TypeError,
4902 "slice indices must be integers or "
4903 "None or have an __index__ method");
4904 return 0;
4905 }
4906 *pi = x;
4907 }
4908 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004909}
4910
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004911int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004912_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004913{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004914 Py_ssize_t x;
4915 if (PyIndex_Check(v)) {
4916 x = PyNumber_AsSsize_t(v, NULL);
4917 if (x == -1 && PyErr_Occurred())
4918 return 0;
4919 }
4920 else {
4921 PyErr_SetString(PyExc_TypeError,
4922 "slice indices must be integers or "
4923 "have an __index__ method");
4924 return 0;
4925 }
4926 *pi = x;
4927 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004928}
4929
4930
Guido van Rossum486364b2007-06-30 05:01:58 +00004931#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004932 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004933
Guido van Rossumb209a111997-04-29 18:18:01 +00004934static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004935cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004936{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004937 int res = 0;
4938 switch (op) {
4939 case PyCmp_IS:
4940 res = (v == w);
4941 break;
4942 case PyCmp_IS_NOT:
4943 res = (v != w);
4944 break;
4945 case PyCmp_IN:
4946 res = PySequence_Contains(w, v);
4947 if (res < 0)
4948 return NULL;
4949 break;
4950 case PyCmp_NOT_IN:
4951 res = PySequence_Contains(w, v);
4952 if (res < 0)
4953 return NULL;
4954 res = !res;
4955 break;
4956 case PyCmp_EXC_MATCH:
4957 if (PyTuple_Check(w)) {
4958 Py_ssize_t i, length;
4959 length = PyTuple_Size(w);
4960 for (i = 0; i < length; i += 1) {
4961 PyObject *exc = PyTuple_GET_ITEM(w, i);
4962 if (!PyExceptionClass_Check(exc)) {
4963 PyErr_SetString(PyExc_TypeError,
4964 CANNOT_CATCH_MSG);
4965 return NULL;
4966 }
4967 }
4968 }
4969 else {
4970 if (!PyExceptionClass_Check(w)) {
4971 PyErr_SetString(PyExc_TypeError,
4972 CANNOT_CATCH_MSG);
4973 return NULL;
4974 }
4975 }
4976 res = PyErr_GivenExceptionMatches(v, w);
4977 break;
4978 default:
4979 return PyObject_RichCompare(v, w, op);
4980 }
4981 v = res ? Py_True : Py_False;
4982 Py_INCREF(v);
4983 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004984}
4985
Thomas Wouters52152252000-08-17 22:55:00 +00004986static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004987import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4988{
4989 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004990 PyObject *import_func, *res;
4991 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004992
4993 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
4994 if (import_func == NULL) {
4995 PyErr_SetString(PyExc_ImportError, "__import__ not found");
4996 return NULL;
4997 }
4998
4999 /* Fast path for not overloaded __import__. */
5000 if (import_func == PyThreadState_GET()->interp->import_func) {
5001 int ilevel = _PyLong_AsInt(level);
5002 if (ilevel == -1 && PyErr_Occurred()) {
5003 return NULL;
5004 }
5005 res = PyImport_ImportModuleLevelObject(
5006 name,
5007 f->f_globals,
5008 f->f_locals == NULL ? Py_None : f->f_locals,
5009 fromlist,
5010 ilevel);
5011 return res;
5012 }
5013
5014 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005015
5016 stack[0] = name;
5017 stack[1] = f->f_globals;
5018 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5019 stack[3] = fromlist;
5020 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005021 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005022 Py_DECREF(import_func);
5023 return res;
5024}
5025
5026static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00005027import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005028{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005029 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005030 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08005031 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005032
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005033 x = PyObject_GetAttr(v, name);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005034 if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
5035 return x;
5036 /* Issue #17636: in case this failed because of a circular relative
5037 import, try to fallback on reading the module directly from
5038 sys.modules. */
5039 PyErr_Clear();
5040 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005041 if (pkgname == NULL) {
5042 goto error;
5043 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005044 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005045 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005046 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005047 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005048 }
Eric Snow86b7afd2017-09-04 17:54:09 -06005049 x = _PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005050 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005051 if (x == NULL) {
5052 goto error;
5053 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005054 Py_DECREF(pkgname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005055 Py_INCREF(x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005056 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005057 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005058 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005059 if (pkgname == NULL) {
5060 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5061 if (pkgname_or_unknown == NULL) {
5062 Py_XDECREF(pkgpath);
5063 return NULL;
5064 }
5065 } else {
5066 pkgname_or_unknown = pkgname;
5067 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005068
5069 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
5070 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08005071 errmsg = PyUnicode_FromFormat(
5072 "cannot import name %R from %R (unknown location)",
5073 name, pkgname_or_unknown
5074 );
5075 /* NULL check for errmsg done by PyErr_SetImportError. */
5076 PyErr_SetImportError(errmsg, pkgname, NULL);
5077 }
5078 else {
5079 errmsg = PyUnicode_FromFormat(
5080 "cannot import name %R from %R (%S)",
5081 name, pkgname_or_unknown, pkgpath
5082 );
5083 /* NULL check for errmsg done by PyErr_SetImportError. */
5084 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005085 }
5086
Xiang Zhang4830f582017-03-21 11:13:42 +08005087 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005088 Py_XDECREF(pkgname_or_unknown);
5089 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005090 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005091}
Guido van Rossumac7be682001-01-17 15:42:30 +00005092
Thomas Wouters52152252000-08-17 22:55:00 +00005093static int
5094import_all_from(PyObject *locals, PyObject *v)
5095{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005096 _Py_IDENTIFIER(__all__);
5097 _Py_IDENTIFIER(__dict__);
5098 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005099 PyObject *dict, *name, *value;
5100 int skip_leading_underscores = 0;
5101 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005102
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005103 if (all == NULL) {
5104 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5105 return -1; /* Unexpected error */
5106 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005107 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005108 if (dict == NULL) {
5109 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5110 return -1;
5111 PyErr_SetString(PyExc_ImportError,
5112 "from-import-* object has no __dict__ and no __all__");
5113 return -1;
5114 }
5115 all = PyMapping_Keys(dict);
5116 Py_DECREF(dict);
5117 if (all == NULL)
5118 return -1;
5119 skip_leading_underscores = 1;
5120 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005122 for (pos = 0, err = 0; ; pos++) {
5123 name = PySequence_GetItem(all, pos);
5124 if (name == NULL) {
5125 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5126 err = -1;
5127 else
5128 PyErr_Clear();
5129 break;
5130 }
5131 if (skip_leading_underscores &&
5132 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005133 PyUnicode_READY(name) != -1 &&
5134 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005135 {
5136 Py_DECREF(name);
5137 continue;
5138 }
5139 value = PyObject_GetAttr(v, name);
5140 if (value == NULL)
5141 err = -1;
5142 else if (PyDict_CheckExact(locals))
5143 err = PyDict_SetItem(locals, name, value);
5144 else
5145 err = PyObject_SetItem(locals, name, value);
5146 Py_DECREF(name);
5147 Py_XDECREF(value);
5148 if (err != 0)
5149 break;
5150 }
5151 Py_DECREF(all);
5152 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005153}
5154
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005155static int
5156check_args_iterable(PyObject *func, PyObject *args)
5157{
5158 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
5159 PyErr_Format(PyExc_TypeError,
5160 "%.200s%.200s argument after * "
5161 "must be an iterable, not %.200s",
5162 PyEval_GetFuncName(func),
5163 PyEval_GetFuncDesc(func),
5164 args->ob_type->tp_name);
5165 return -1;
5166 }
5167 return 0;
5168}
5169
5170static void
5171format_kwargs_mapping_error(PyObject *func, PyObject *kwargs)
5172{
5173 PyErr_Format(PyExc_TypeError,
5174 "%.200s%.200s argument after ** "
5175 "must be a mapping, not %.200s",
5176 PyEval_GetFuncName(func),
5177 PyEval_GetFuncDesc(func),
5178 kwargs->ob_type->tp_name);
5179}
5180
Guido van Rossumac7be682001-01-17 15:42:30 +00005181static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005182format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005183{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005184 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005186 if (!obj)
5187 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005188
Serhiy Storchaka06515832016-11-20 09:13:07 +02005189 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005190 if (!obj_str)
5191 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005193 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005194}
Guido van Rossum950361c1997-01-24 13:49:28 +00005195
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005196static void
5197format_exc_unbound(PyCodeObject *co, int oparg)
5198{
5199 PyObject *name;
5200 /* Don't stomp existing exception */
5201 if (PyErr_Occurred())
5202 return;
5203 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5204 name = PyTuple_GET_ITEM(co->co_cellvars,
5205 oparg);
5206 format_exc_check_arg(
5207 PyExc_UnboundLocalError,
5208 UNBOUNDLOCAL_ERROR_MSG,
5209 name);
5210 } else {
5211 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5212 PyTuple_GET_SIZE(co->co_cellvars));
5213 format_exc_check_arg(PyExc_NameError,
5214 UNBOUNDFREE_ERROR_MSG, name);
5215 }
5216}
5217
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005218static PyObject *
5219unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005220 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005221{
5222 PyObject *res;
5223 if (Py_REFCNT(v) == 2) {
5224 /* In the common case, there are 2 references to the value
5225 * stored in 'variable' when the += is performed: one on the
5226 * value stack (in 'v') and one still stored in the
5227 * 'variable'. We try to delete the variable now to reduce
5228 * the refcnt to 1.
5229 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005230 int opcode, oparg;
5231 NEXTOPARG();
5232 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005233 case STORE_FAST:
5234 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005235 PyObject **fastlocals = f->f_localsplus;
5236 if (GETLOCAL(oparg) == v)
5237 SETLOCAL(oparg, NULL);
5238 break;
5239 }
5240 case STORE_DEREF:
5241 {
5242 PyObject **freevars = (f->f_localsplus +
5243 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005244 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005245 if (PyCell_GET(c) == v) {
5246 PyCell_SET(c, NULL);
5247 Py_DECREF(v);
5248 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005249 break;
5250 }
5251 case STORE_NAME:
5252 {
5253 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005254 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005255 PyObject *locals = f->f_locals;
5256 if (PyDict_CheckExact(locals) &&
5257 PyDict_GetItem(locals, name) == v) {
5258 if (PyDict_DelItem(locals, name) != 0) {
5259 PyErr_Clear();
5260 }
5261 }
5262 break;
5263 }
5264 }
5265 }
5266 res = v;
5267 PyUnicode_Append(&res, w);
5268 return res;
5269}
5270
Guido van Rossum950361c1997-01-24 13:49:28 +00005271#ifdef DYNAMIC_EXECUTION_PROFILE
5272
Skip Montanarof118cb12001-10-15 20:51:38 +00005273static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005274getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005275{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005276 int i;
5277 PyObject *l = PyList_New(256);
5278 if (l == NULL) return NULL;
5279 for (i = 0; i < 256; i++) {
5280 PyObject *x = PyLong_FromLong(a[i]);
5281 if (x == NULL) {
5282 Py_DECREF(l);
5283 return NULL;
5284 }
5285 PyList_SetItem(l, i, x);
5286 }
5287 for (i = 0; i < 256; i++)
5288 a[i] = 0;
5289 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005290}
5291
5292PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005293_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005294{
5295#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005296 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005297#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005298 int i;
5299 PyObject *l = PyList_New(257);
5300 if (l == NULL) return NULL;
5301 for (i = 0; i < 257; i++) {
5302 PyObject *x = getarray(dxpairs[i]);
5303 if (x == NULL) {
5304 Py_DECREF(l);
5305 return NULL;
5306 }
5307 PyList_SetItem(l, i, x);
5308 }
5309 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005310#endif
5311}
5312
5313#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005314
5315Py_ssize_t
5316_PyEval_RequestCodeExtraIndex(freefunc free)
5317{
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005318 PyInterpreterState *interp = PyThreadState_Get()->interp;
Brett Cannon5c4de282016-09-07 11:16:41 -07005319 Py_ssize_t new_index;
5320
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005321 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005322 return -1;
5323 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005324 new_index = interp->co_extra_user_count++;
5325 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005326 return new_index;
5327}
Łukasz Langaa785c872016-09-09 17:37:37 -07005328
5329static void
5330dtrace_function_entry(PyFrameObject *f)
5331{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005332 const char *filename;
5333 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005334 int lineno;
5335
5336 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5337 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5338 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5339
5340 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5341}
5342
5343static void
5344dtrace_function_return(PyFrameObject *f)
5345{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005346 const char *filename;
5347 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005348 int lineno;
5349
5350 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5351 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5352 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5353
5354 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5355}
5356
5357/* DTrace equivalent of maybe_call_line_trace. */
5358static void
5359maybe_dtrace_line(PyFrameObject *frame,
5360 int *instr_lb, int *instr_ub, int *instr_prev)
5361{
5362 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005363 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005364
5365 /* If the last instruction executed isn't in the current
5366 instruction window, reset the window.
5367 */
5368 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5369 PyAddrPair bounds;
5370 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5371 &bounds);
5372 *instr_lb = bounds.ap_lower;
5373 *instr_ub = bounds.ap_upper;
5374 }
5375 /* If the last instruction falls at the start of a line or if
5376 it represents a jump backwards, update the frame's line
5377 number and call the trace function. */
5378 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5379 frame->f_lineno = line;
5380 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5381 if (!co_filename)
5382 co_filename = "?";
5383 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5384 if (!co_name)
5385 co_name = "?";
5386 PyDTrace_LINE(co_filename, co_name, line);
5387 }
5388 *instr_prev = frame->f_lasti;
5389}