blob: fc11117109cebb6cdcbc6e383cd0e8b8692440b5 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040015#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000016#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000017#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070018#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040019#include "setobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000020#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000021
Guido van Rossumc6004111993-11-05 10:22:19 +000022#include <ctype.h>
23
Guido van Rossum04691fc1992-08-12 15:35:34 +000024/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000025/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000026
Guido van Rossum408027e1996-12-30 16:17:54 +000027#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000028/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029#define LLTRACE 1 /* Low-level trace feature */
30#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000031#endif
32
Yury Selivanovf2392132016-12-13 19:03:51 -050033/* Private API for the LOAD_METHOD opcode. */
34extern int _PyObject_GetMethod(PyObject *, PyObject *, PyObject **);
35
Jeremy Hylton52820442001-01-03 23:52:36 +000036typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000037
Guido van Rossum374a9221991-04-04 10:40:29 +000038/* Forward declarations */
Victor Stinnerf9b760f2016-09-09 10:17:08 -070039static PyObject * call_function(PyObject ***, Py_ssize_t, PyObject *);
Victor Stinnerd8735722016-09-09 12:36:44 -070040static PyObject * fast_function(PyObject *, PyObject **, Py_ssize_t, PyObject *);
Victor Stinnerf9b760f2016-09-09 10:17:08 -070041static PyObject * do_call_core(PyObject *, PyObject *, PyObject *);
Jeremy Hylton52820442001-01-03 23:52:36 +000042
Guido van Rossum0a066c01992-03-27 17:29:15 +000043#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000044static int lltrace;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020045static int prtrace(PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000046#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010047static int call_trace(Py_tracefunc, PyObject *,
48 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000050static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010051 PyThreadState *, PyFrameObject *,
52 int, PyObject *);
53static void call_exc_trace(Py_tracefunc, PyObject *,
54 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000055static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010056 PyThreadState *, PyFrameObject *, int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070057static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
58static void dtrace_function_entry(PyFrameObject *);
59static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000060
Thomas Wouters477c8d52006-05-27 19:21:47 +000061static PyObject * cmp_outcome(int, PyObject *, PyObject *);
Serhiy Storchaka133138a2016-08-02 22:51:21 +030062static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *, PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +000063static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +000064static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +000065static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +000066static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +020067static PyObject * unicode_concatenate(PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030068 PyFrameObject *, const _Py_CODEUNIT *);
Benjamin Petersonce798522012-01-22 11:24:29 -050069static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Guido van Rossum374a9221991-04-04 10:40:29 +000070
Paul Prescode68140d2000-08-30 20:25:01 +000071#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000073#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000075#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 "free variable '%.200s' referenced before assignment" \
77 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000078
Guido van Rossum950361c1997-01-24 13:49:28 +000079/* Dynamic execution profile */
80#ifdef DYNAMIC_EXECUTION_PROFILE
81#ifdef DXPAIRS
82static long dxpairs[257][256];
83#define dxp dxpairs[256]
84#else
85static long dxp[256];
86#endif
87#endif
88
Benjamin Petersond2be5b42010-09-10 22:47:02 +000089#ifdef WITH_THREAD
90#define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request)
91#else
92#define GIL_REQUEST 0
93#endif
94
Jeffrey Yasskin39370832010-05-03 19:29:34 +000095/* This can set eval_breaker to 0 even though gil_drop_request became
96 1. We believe this is all right because the eval loop will release
97 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +000098#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 _Py_atomic_store_relaxed( \
100 &eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000101 GIL_REQUEST | \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
103 pending_async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000104
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000105#ifdef WITH_THREAD
106
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000107#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 do { \
109 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
110 _Py_atomic_store_relaxed(&eval_breaker, 1); \
111 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000112
113#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 do { \
115 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
116 COMPUTE_EVAL_BREAKER(); \
117 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000118
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000119#endif
120
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000121/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000122#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 do { \
124 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
125 _Py_atomic_store_relaxed(&eval_breaker, 1); \
126 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000127
128#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 do { \
130 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
131 COMPUTE_EVAL_BREAKER(); \
132 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000133
134#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 do { \
136 pending_async_exc = 1; \
137 _Py_atomic_store_relaxed(&eval_breaker, 1); \
138 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000139
140#define UNSIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000142
143
Guido van Rossume59214e1994-08-30 08:01:59 +0000144#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000145
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000146#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000147#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000148#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000149#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000150
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000151static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000152static long main_thread = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000153/* This single variable consolidates all requests to break out of the fast path
154 in the eval loop. */
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000155static _Py_atomic_int eval_breaker = {0};
156/* Request for dropping the GIL */
157static _Py_atomic_int gil_drop_request = {0};
158/* Request for running pending calls. */
159static _Py_atomic_int pendingcalls_to_do = {0};
160/* Request for looking at the `async_exc` field of the current thread state.
161 Guarded by the GIL. */
162static int pending_async_exc = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000163
164#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000165
Tim Peters7f468f22004-10-11 02:40:51 +0000166int
167PyEval_ThreadsInitialized(void)
168{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000170}
171
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000172void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000173PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 if (gil_created())
176 return;
177 create_gil();
178 take_gil(PyThreadState_GET());
179 main_thread = PyThread_get_thread_ident();
180 if (!pending_lock)
181 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000182}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000183
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000184void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000185_PyEval_FiniThreads(void)
186{
187 if (!gil_created())
188 return;
189 destroy_gil();
190 assert(!gil_created());
191}
192
193void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000194PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 PyThreadState *tstate = PyThreadState_GET();
197 if (tstate == NULL)
198 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
199 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000200}
201
202void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000203PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 /* This function must succeed when the current thread state is NULL.
206 We therefore avoid PyThreadState_GET() which dumps a fatal error
207 in debug mode.
208 */
209 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
210 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000211}
212
213void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000214PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 if (tstate == NULL)
217 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
218 /* Check someone has called PyEval_InitThreads() to create the lock */
219 assert(gil_created());
220 take_gil(tstate);
221 if (PyThreadState_Swap(tstate) != NULL)
222 Py_FatalError(
223 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000224}
225
226void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000227PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000228{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 if (tstate == NULL)
230 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
231 if (PyThreadState_Swap(NULL) != tstate)
232 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
233 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000234}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000235
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200236/* This function is called from PyOS_AfterFork to destroy all threads which are
237 * not running in the child process, and clear internal locks which might be
238 * held by those threads. (This could also be done using pthread_atfork
239 * mechanism, at least for the pthreads implementation.) */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000240
241void
242PyEval_ReInitThreads(void)
243{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200244 _Py_IDENTIFIER(_after_fork);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 PyObject *threading, *result;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200246 PyThreadState *current_tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 if (!gil_created())
249 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 recreate_gil();
251 pending_lock = PyThread_allocate_lock();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200252 take_gil(current_tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 /* Update the threading module with the new state.
256 */
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200257 threading = PyMapping_GetItemString(current_tstate->interp->modules,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 "threading");
259 if (threading == NULL) {
260 /* threading not imported */
261 PyErr_Clear();
262 return;
263 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200264 result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 if (result == NULL)
266 PyErr_WriteUnraisable(threading);
267 else
268 Py_DECREF(result);
269 Py_DECREF(threading);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200270
271 /* Destroy all threads except the current one */
272 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000273}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000274
275#else
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000276static _Py_atomic_int eval_breaker = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000277static int pending_async_exc = 0;
278#endif /* WITH_THREAD */
279
280/* This function is used to signal that async exceptions are waiting to be
281 raised, therefore it is also useful in non-threaded builds. */
282
283void
284_PyEval_SignalAsyncExc(void)
285{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000287}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000288
Guido van Rossumff4949e1992-08-05 19:58:53 +0000289/* Functions save_thread and restore_thread are always defined so
290 dynamically loaded modules needn't be compiled separately for use
291 with and without threads: */
292
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000293PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000294PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000295{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 PyThreadState *tstate = PyThreadState_Swap(NULL);
297 if (tstate == NULL)
298 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000299#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 if (gil_created())
301 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000302#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000304}
305
306void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000307PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000308{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 if (tstate == NULL)
310 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000311#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 if (gil_created()) {
313 int err = errno;
314 take_gil(tstate);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200315 /* _Py_Finalizing is protected by the GIL */
316 if (_Py_Finalizing && tstate != _Py_Finalizing) {
317 drop_gil(tstate);
318 PyThread_exit_thread();
319 assert(0); /* unreachable */
320 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 errno = err;
322 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000323#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000325}
326
327
Guido van Rossuma9672091994-09-14 13:31:22 +0000328/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
329 signal handlers or Mac I/O completion routines) can schedule calls
330 to a function to be called synchronously.
331 The synchronous function is called with one void* argument.
332 It should return 0 for success or -1 for failure -- failure should
333 be accompanied by an exception.
334
335 If registry succeeds, the registry function returns 0; if it fails
336 (e.g. due to too many pending calls) it returns -1 (without setting
337 an exception condition).
338
339 Note that because registry may occur from within signal handlers,
340 or other asynchronous events, calling malloc() is unsafe!
341
342#ifdef WITH_THREAD
343 Any thread can schedule pending calls, but only the main thread
344 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000345 There is no facility to schedule calls to a particular thread, but
346 that should be easy to change, should that ever be required. In
347 that case, the static variables here should go into the python
348 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000349#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000350*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000351
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000352#ifdef WITH_THREAD
353
354/* The WITH_THREAD implementation is thread-safe. It allows
355 scheduling to be made from any thread, and even from an executing
356 callback.
357 */
358
359#define NPENDINGCALLS 32
360static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 int (*func)(void *);
362 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000363} pendingcalls[NPENDINGCALLS];
364static int pendingfirst = 0;
365static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000366
367int
368Py_AddPendingCall(int (*func)(void *), void *arg)
369{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 int i, j, result=0;
371 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 /* try a few times for the lock. Since this mechanism is used
374 * for signal handling (on the main thread), there is a (slim)
375 * chance that a signal is delivered on the same thread while we
376 * hold the lock during the Py_MakePendingCalls() function.
377 * This avoids a deadlock in that case.
378 * Note that signals can be delivered on any thread. In particular,
379 * on Windows, a SIGINT is delivered on a system-created worker
380 * thread.
381 * We also check for lock being NULL, in the unlikely case that
382 * this function is called before any bytecode evaluation takes place.
383 */
384 if (lock != NULL) {
385 for (i = 0; i<100; i++) {
386 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
387 break;
388 }
389 if (i == 100)
390 return -1;
391 }
392
393 i = pendinglast;
394 j = (i + 1) % NPENDINGCALLS;
395 if (j == pendingfirst) {
396 result = -1; /* Queue full */
397 } else {
398 pendingcalls[i].func = func;
399 pendingcalls[i].arg = arg;
400 pendinglast = j;
401 }
402 /* signal main loop */
403 SIGNAL_PENDING_CALLS();
404 if (lock != NULL)
405 PyThread_release_lock(lock);
406 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000407}
408
409int
410Py_MakePendingCalls(void)
411{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200412 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 int i;
414 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 if (!pending_lock) {
417 /* initial allocation of the lock */
418 pending_lock = PyThread_allocate_lock();
419 if (pending_lock == NULL)
420 return -1;
421 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 /* only service pending calls on main thread */
424 if (main_thread && PyThread_get_thread_ident() != main_thread)
425 return 0;
426 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200427 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200429 busy = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 /* perform a bounded number of calls, in case of recursion */
431 for (i=0; i<NPENDINGCALLS; i++) {
432 int j;
433 int (*func)(void *);
434 void *arg = NULL;
435
436 /* pop one item off the queue while holding the lock */
437 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
438 j = pendingfirst;
439 if (j == pendinglast) {
440 func = NULL; /* Queue empty */
441 } else {
442 func = pendingcalls[j].func;
443 arg = pendingcalls[j].arg;
444 pendingfirst = (j + 1) % NPENDINGCALLS;
445 }
446 if (pendingfirst != pendinglast)
447 SIGNAL_PENDING_CALLS();
448 else
449 UNSIGNAL_PENDING_CALLS();
450 PyThread_release_lock(pending_lock);
451 /* having released the lock, perform the callback */
452 if (func == NULL)
453 break;
454 r = func(arg);
455 if (r)
456 break;
457 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200458 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000460}
461
462#else /* if ! defined WITH_THREAD */
463
464/*
465 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
466 This code is used for signal handling in python that isn't built
467 with WITH_THREAD.
468 Don't use this implementation when Py_AddPendingCalls() can happen
469 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470
Guido van Rossuma9672091994-09-14 13:31:22 +0000471 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000472 (1) nested asynchronous calls to Py_AddPendingCall()
473 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000475 (1) is very unlikely because typically signal delivery
476 is blocked during signal handling. So it should be impossible.
477 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000478 The current code is safe against (2), but not against (1).
479 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000480 thread is present, interrupted by signals, and that the critical
481 section is protected with the "busy" variable. On Windows, which
482 delivers SIGINT on a system thread, this does not hold and therefore
483 Windows really shouldn't use this version.
484 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000485*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000486
Guido van Rossuma9672091994-09-14 13:31:22 +0000487#define NPENDINGCALLS 32
488static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 int (*func)(void *);
490 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000491} pendingcalls[NPENDINGCALLS];
492static volatile int pendingfirst = 0;
493static volatile int pendinglast = 0;
Benjamin Peterson08ec84c2010-05-30 14:49:32 +0000494static _Py_atomic_int pendingcalls_to_do = {0};
Guido van Rossuma9672091994-09-14 13:31:22 +0000495
496int
Thomas Wouters334fb892000-07-25 12:56:38 +0000497Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 static volatile int busy = 0;
500 int i, j;
501 /* XXX Begin critical section */
502 if (busy)
503 return -1;
504 busy = 1;
505 i = pendinglast;
506 j = (i + 1) % NPENDINGCALLS;
507 if (j == pendingfirst) {
508 busy = 0;
509 return -1; /* Queue full */
510 }
511 pendingcalls[i].func = func;
512 pendingcalls[i].arg = arg;
513 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 SIGNAL_PENDING_CALLS();
516 busy = 0;
517 /* XXX End critical section */
518 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000519}
520
Guido van Rossum180d7b41994-09-29 09:45:57 +0000521int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000522Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000523{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 static int busy = 0;
525 if (busy)
526 return 0;
527 busy = 1;
528 UNSIGNAL_PENDING_CALLS();
529 for (;;) {
530 int i;
531 int (*func)(void *);
532 void *arg;
533 i = pendingfirst;
534 if (i == pendinglast)
535 break; /* Queue empty */
536 func = pendingcalls[i].func;
537 arg = pendingcalls[i].arg;
538 pendingfirst = (i + 1) % NPENDINGCALLS;
539 if (func(arg) < 0) {
540 busy = 0;
541 SIGNAL_PENDING_CALLS(); /* We're not done yet */
542 return -1;
543 }
544 }
545 busy = 0;
546 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000547}
548
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000549#endif /* WITH_THREAD */
550
Guido van Rossuma9672091994-09-14 13:31:22 +0000551
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000552/* The interpreter's recursion limit */
553
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000554#ifndef Py_DEFAULT_RECURSION_LIMIT
555#define Py_DEFAULT_RECURSION_LIMIT 1000
556#endif
557static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
558int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000559
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000560int
561Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000562{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000564}
565
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000566void
567Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000568{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 recursion_limit = new_limit;
570 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000571}
572
Armin Rigo2b3eb402003-10-28 12:05:48 +0000573/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
574 if the recursion_depth reaches _Py_CheckRecursionLimit.
575 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
576 to guarantee that _Py_CheckRecursiveCall() is regularly called.
577 Without USE_STACKCHECK, there is no need for this. */
578int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300579_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000580{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000582
583#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 if (PyOS_CheckStack()) {
585 --tstate->recursion_depth;
586 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
587 return -1;
588 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000589#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 _Py_CheckRecursionLimit = recursion_limit;
591 if (tstate->recursion_critical)
592 /* Somebody asked that we don't check for recursion. */
593 return 0;
594 if (tstate->overflowed) {
595 if (tstate->recursion_depth > recursion_limit + 50) {
596 /* Overflowing while handling an overflow. Give up. */
597 Py_FatalError("Cannot recover from stack overflow.");
598 }
599 return 0;
600 }
601 if (tstate->recursion_depth > recursion_limit) {
602 --tstate->recursion_depth;
603 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400604 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 "maximum recursion depth exceeded%s",
606 where);
607 return -1;
608 }
609 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000610}
611
Guido van Rossum374a9221991-04-04 10:40:29 +0000612/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000613enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000614 WHY_NOT = 0x0001, /* No error */
615 WHY_EXCEPTION = 0x0002, /* Exception occurred */
Stefan Krahb7e10102010-06-23 18:42:39 +0000616 WHY_RETURN = 0x0008, /* 'return' statement */
617 WHY_BREAK = 0x0010, /* 'break' statement */
618 WHY_CONTINUE = 0x0020, /* 'continue' statement */
619 WHY_YIELD = 0x0040, /* 'yield' operator */
620 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000621};
Guido van Rossum374a9221991-04-04 10:40:29 +0000622
Benjamin Peterson87880242011-07-03 16:48:31 -0500623static void save_exc_state(PyThreadState *, PyFrameObject *);
624static void swap_exc_state(PyThreadState *, PyFrameObject *);
625static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400626static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000627static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000628
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000629/* Records whether tracing is on for any thread. Counts the number of
630 threads for which tstate->c_tracefunc is non-NULL, so if the value
631 is 0, we know we don't have to check this thread's c_tracefunc.
632 This speeds up the if statement in PyEval_EvalFrameEx() after
633 fast_next_opcode*/
634static int _Py_TracingPossible = 0;
635
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000636
Guido van Rossum374a9221991-04-04 10:40:29 +0000637
Guido van Rossumb209a111997-04-29 18:18:01 +0000638PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000639PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000640{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 return PyEval_EvalCodeEx(co,
642 globals, locals,
643 (PyObject **)NULL, 0,
644 (PyObject **)NULL, 0,
645 (PyObject **)NULL, 0,
646 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000647}
648
649
650/* Interpreter main loop */
651
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000652PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000653PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 /* This is for backward compatibility with extension modules that
655 used this API; core interpreter code should call
656 PyEval_EvalFrameEx() */
657 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000658}
659
660PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000661PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000662{
Brett Cannon3cebf932016-09-05 15:33:46 -0700663 PyThreadState *tstate = PyThreadState_GET();
664 return tstate->interp->eval_frame(f, throwflag);
665}
666
Victor Stinnerc6944e72016-11-11 02:13:35 +0100667PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700668_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
669{
Guido van Rossum950361c1997-01-24 13:49:28 +0000670#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000672#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200673 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300674 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200675 int opcode; /* Current opcode */
676 int oparg; /* Current opcode argument, if any */
677 enum why_code why; /* Reason for block stack unwind */
678 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 PyObject *retval = NULL; /* Return value */
680 PyThreadState *tstate = PyThreadState_GET();
681 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 is true when the line being executed has changed. The
688 initial values are such as to make this false the first
689 time it is tested. */
690 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000691
Serhiy Storchakaab874002016-09-11 13:48:15 +0300692 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 PyObject *names;
694 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000695
Brett Cannon368b4b72012-04-02 12:17:59 -0400696#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200697 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400698#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200699
Antoine Pitroub52ec782009-01-25 16:34:23 +0000700/* Computed GOTOs, or
701 the-optimization-commonly-but-improperly-known-as-"threaded code"
702 using gcc's labels-as-values extension
703 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
704
705 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000707 combined with a lookup table of jump addresses. However, since the
708 indirect jump instruction is shared by all opcodes, the CPU will have a
709 hard time making the right prediction for where to jump next (actually,
710 it will be always wrong except in the uncommon case of a sequence of
711 several identical opcodes).
712
713 "Threaded code" in contrast, uses an explicit jump table and an explicit
714 indirect jump instruction at the end of each opcode. Since the jump
715 instruction is at a different address for each opcode, the CPU will make a
716 separate prediction for each of these instructions, which is equivalent to
717 predicting the second opcode of each opcode pair. These predictions have
718 a much better chance to turn out valid, especially in small bytecode loops.
719
720 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000722 and potentially many more instructions (depending on the pipeline width).
723 A correctly predicted branch, however, is nearly free.
724
725 At the time of this writing, the "threaded code" version is up to 15-20%
726 faster than the normal "switch" version, depending on the compiler and the
727 CPU architecture.
728
729 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
730 because it would render the measurements invalid.
731
732
733 NOTE: care must be taken that the compiler doesn't try to "optimize" the
734 indirect jumps by sharing them between all opcodes. Such optimizations
735 can be disabled on gcc by using the -fno-gcse flag (or possibly
736 -fno-crossjumping).
737*/
738
Antoine Pitrou042b1282010-08-13 21:15:58 +0000739#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000740#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000741#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000742#endif
743
Antoine Pitrou042b1282010-08-13 21:15:58 +0000744#ifdef HAVE_COMPUTED_GOTOS
745 #ifndef USE_COMPUTED_GOTOS
746 #define USE_COMPUTED_GOTOS 1
747 #endif
748#else
749 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
750 #error "Computed gotos are not supported on this compiler."
751 #endif
752 #undef USE_COMPUTED_GOTOS
753 #define USE_COMPUTED_GOTOS 0
754#endif
755
756#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000757/* Import the static jump table */
758#include "opcode_targets.h"
759
Antoine Pitroub52ec782009-01-25 16:34:23 +0000760#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 TARGET_##op: \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000763
Antoine Pitroub52ec782009-01-25 16:34:23 +0000764#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 { \
766 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
767 FAST_DISPATCH(); \
768 } \
769 continue; \
770 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000771
772#ifdef LLTRACE
773#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700775 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300777 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300778 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 } \
780 goto fast_next_opcode; \
781 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000782#else
783#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700785 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300787 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300788 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 } \
790 goto fast_next_opcode; \
791 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000792#endif
793
794#else
795#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 case op:
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300797
Antoine Pitroub52ec782009-01-25 16:34:23 +0000798#define DISPATCH() continue
799#define FAST_DISPATCH() goto fast_next_opcode
800#endif
801
802
Neal Norwitza81d2202002-07-14 00:27:26 +0000803/* Tuple access macros */
804
805#ifndef Py_DEBUG
806#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
807#else
808#define GETITEM(v, i) PyTuple_GetItem((v), (i))
809#endif
810
Guido van Rossum374a9221991-04-04 10:40:29 +0000811/* Code access macros */
812
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300813/* The integer overflow is checked by an assertion below. */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300814#define INSTR_OFFSET() (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300815#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300816 _Py_CODEUNIT word = *next_instr; \
817 opcode = _Py_OPCODE(word); \
818 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300819 next_instr++; \
820 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300821#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
822#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000823
Raymond Hettingerf606f872003-03-16 03:11:04 +0000824/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 Some opcodes tend to come in pairs thus making it possible to
826 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300827 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 Verifying the prediction costs a single high-speed test of a register
830 variable against a constant. If the pairing was good, then the
831 processor's own internal branch predication has a high likelihood of
832 success, resulting in a nearly zero-overhead transition to the
833 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300834 including its unpredictable switch-case branch. Combined with the
835 processor's internal branch prediction, a successful PREDICT has the
836 effect of making the two opcodes run as if they were a single new opcode
837 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000838
Georg Brandl86b2fb92008-07-16 03:43:04 +0000839 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 predictions turned-on and interpret the results as if some opcodes
841 had been combined or turn-off predictions so that the opcode frequency
842 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000843
844 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 the CPU to record separate branch prediction information for each
846 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000847
Raymond Hettingerf606f872003-03-16 03:11:04 +0000848*/
849
Antoine Pitrou042b1282010-08-13 21:15:58 +0000850#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000852#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300853#define PREDICT(op) \
854 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300855 _Py_CODEUNIT word = *next_instr; \
856 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300857 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300858 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300859 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300860 goto PRED_##op; \
861 } \
862 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000863#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300864#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000865
Raymond Hettingerf606f872003-03-16 03:11:04 +0000866
Guido van Rossum374a9221991-04-04 10:40:29 +0000867/* Stack manipulation macros */
868
Martin v. Löwis18e16552006-02-15 17:27:45 +0000869/* The stack can grow at most MAXINT deep, as co_nlocals and
870 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000871#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
872#define EMPTY() (STACK_LEVEL() == 0)
873#define TOP() (stack_pointer[-1])
874#define SECOND() (stack_pointer[-2])
875#define THIRD() (stack_pointer[-3])
876#define FOURTH() (stack_pointer[-4])
877#define PEEK(n) (stack_pointer[-(n)])
878#define SET_TOP(v) (stack_pointer[-1] = (v))
879#define SET_SECOND(v) (stack_pointer[-2] = (v))
880#define SET_THIRD(v) (stack_pointer[-3] = (v))
881#define SET_FOURTH(v) (stack_pointer[-4] = (v))
882#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
883#define BASIC_STACKADJ(n) (stack_pointer += n)
884#define BASIC_PUSH(v) (*stack_pointer++ = (v))
885#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000886
Guido van Rossum96a42c81992-01-12 02:29:51 +0000887#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000889 lltrace && prtrace(TOP(), "push")); \
890 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000892 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000894 lltrace && prtrace(TOP(), "stackadj")); \
895 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +0000896#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000897 prtrace((STACK_POINTER)[-1], "ext_pop")), \
898 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000899#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000900#define PUSH(v) BASIC_PUSH(v)
901#define POP() BASIC_POP()
902#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000903#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000904#endif
905
Guido van Rossum681d79a1995-07-18 14:51:37 +0000906/* Local variable macros */
907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000909
910/* The SETLOCAL() macro must not DECREF the local variable in-place and
911 then store the new value; it must copy the old value to a temporary
912 value, then store the new value, and then DECREF the temporary value.
913 This is because it is possible that during the DECREF the frame is
914 accessed by other code (e.g. a __del__ method or gc.collect()) and the
915 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000917 GETLOCAL(i) = value; \
918 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000919
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000920
921#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 while (STACK_LEVEL() > (b)->b_level) { \
923 PyObject *v = POP(); \
924 Py_XDECREF(v); \
925 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000926
927#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300928 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 PyObject *type, *value, *traceback; \
930 assert(STACK_LEVEL() >= (b)->b_level + 3); \
931 while (STACK_LEVEL() > (b)->b_level + 3) { \
932 value = POP(); \
933 Py_XDECREF(value); \
934 } \
935 type = tstate->exc_type; \
936 value = tstate->exc_value; \
937 traceback = tstate->exc_traceback; \
938 tstate->exc_type = POP(); \
939 tstate->exc_value = POP(); \
940 tstate->exc_traceback = POP(); \
941 Py_XDECREF(type); \
942 Py_XDECREF(value); \
943 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300944 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000945
Guido van Rossuma027efa1997-05-05 20:56:21 +0000946/* Start of code */
947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 /* push frame */
949 if (Py_EnterRecursiveCall(""))
950 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 if (tstate->use_tracing) {
955 if (tstate->c_tracefunc != NULL) {
956 /* tstate->c_tracefunc, if defined, is a
957 function that will be called on *every* entry
958 to a code block. Its return value, if not
959 None, is a function that will be called at
960 the start of each executed line of code.
961 (Actually, the function must return itself
962 in order to continue tracing.) The trace
963 functions are called with three arguments:
964 a pointer to the current frame, a string
965 indicating why the function is called, and
966 an argument which depends on the situation.
967 The global trace function is also called
968 whenever an exception is detected. */
969 if (call_trace_protected(tstate->c_tracefunc,
970 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100971 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 /* Trace function raised an error */
973 goto exit_eval_frame;
974 }
975 }
976 if (tstate->c_profilefunc != NULL) {
977 /* Similar for c_profilefunc, except it needn't
978 return itself and isn't called for "line" events */
979 if (call_trace_protected(tstate->c_profilefunc,
980 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100981 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 /* Profile function raised an error */
983 goto exit_eval_frame;
984 }
985 }
986 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000987
Łukasz Langaa785c872016-09-09 17:37:37 -0700988 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
989 dtrace_function_entry(f);
990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 co = f->f_code;
992 names = co->co_names;
993 consts = co->co_consts;
994 fastlocals = f->f_localsplus;
995 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300996 assert(PyBytes_Check(co->co_code));
997 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +0300998 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
999 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1000 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001001 /*
1002 f->f_lasti refers to the index of the last instruction,
1003 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001004
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001005 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001006 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 When the PREDICT() macros are enabled, some opcode pairs follow in
1009 direct succession without updating f->f_lasti. A successful
1010 prediction effectively links the two codes together as if they
1011 were a single new opcode; accordingly,f->f_lasti will point to
1012 the first code in the pair (for instance, GET_ITER followed by
1013 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001014 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001016 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001017 next_instr = first_instr;
1018 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001019 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1020 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001021 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 stack_pointer = f->f_stacktop;
1023 assert(stack_pointer != NULL);
1024 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001025 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001026
Yury Selivanoveb636452016-09-08 22:01:51 -07001027 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01001028 if (!throwflag && f->f_exc_type != NULL && f->f_exc_type != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 /* We were in an except handler when we left,
1030 restore the exception state which was put aside
1031 (see YIELD_VALUE). */
Benjamin Peterson87880242011-07-03 16:48:31 -05001032 swap_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 }
Benjamin Peterson87880242011-07-03 16:48:31 -05001034 else
1035 save_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001037
Tim Peters5ca576e2001-06-18 22:08:13 +00001038#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001039 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001040#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 why = WHY_NOT;
Guido van Rossumac7be682001-01-17 15:42:30 +00001043
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001044 if (throwflag) /* support for generator.throw() */
1045 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001046
Victor Stinnerace47d72013-07-18 01:41:08 +02001047#ifdef Py_DEBUG
1048 /* PyEval_EvalFrameEx() must not be called with an exception set,
1049 because it may clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001050 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +02001051 assert(!PyErr_Occurred());
1052#endif
1053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1056 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +02001057 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 /* Do periodic things. Doing this every time through
1060 the loop would add too much overhead, so we do it
1061 only every Nth instruction. We also do it if
1062 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1063 event needs attention (e.g. a signal handler or
1064 async I/O handler); see Py_AddPendingCall() and
1065 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 if (_Py_atomic_load_relaxed(&eval_breaker)) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001068 if (_Py_OPCODE(*next_instr) == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 /* Make the last opcode before
Ezio Melotti13925002011-03-16 11:05:33 +02001070 a try: finally: block uninterruptible. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 goto fast_next_opcode;
1072 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001074 if (Py_MakePendingCalls() < 0)
1075 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001077#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001078 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 /* Give another thread a chance */
1080 if (PyThreadState_Swap(NULL) != tstate)
1081 Py_FatalError("ceval: tstate mix-up");
1082 drop_gil(tstate);
1083
1084 /* Other threads may run now */
1085
1086 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001087
1088 /* Check if we should make a quick exit. */
1089 if (_Py_Finalizing && _Py_Finalizing != tstate) {
1090 drop_gil(tstate);
1091 PyThread_exit_thread();
1092 }
1093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 if (PyThreadState_Swap(tstate) != NULL)
1095 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001097#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 /* Check for asynchronous exceptions. */
1099 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001100 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 tstate->async_exc = NULL;
1102 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001103 PyErr_SetNone(exc);
1104 Py_DECREF(exc);
1105 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 }
1107 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 fast_next_opcode:
1110 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001111
Łukasz Langaa785c872016-09-09 17:37:37 -07001112 if (PyDTrace_LINE_ENABLED())
1113 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001118 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001119 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 /* see maybe_call_line_trace
1121 for expository comments */
1122 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001123
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 err = maybe_call_line_trace(tstate->c_tracefunc,
1125 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001126 tstate, f,
1127 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 /* Reload possibly changed frame fields */
1129 JUMPTO(f->f_lasti);
1130 if (f->f_stacktop != NULL) {
1131 stack_pointer = f->f_stacktop;
1132 f->f_stacktop = NULL;
1133 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001134 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001136 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001140
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001141 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001142 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001143#ifdef DYNAMIC_EXECUTION_PROFILE
1144#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 dxpairs[lastopcode][opcode]++;
1146 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001147#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001149#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001150
Guido van Rossum96a42c81992-01-12 02:29:51 +00001151#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 if (lltrace) {
1155 if (HAS_ARG(opcode)) {
1156 printf("%d: %d, %d\n",
1157 f->f_lasti, opcode, oparg);
1158 }
1159 else {
1160 printf("%d: %d\n",
1161 f->f_lasti, opcode);
1162 }
1163 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001164#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 /* BEWARE!
1169 It is essential that any operation that fails sets either
1170 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1171 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 TARGET(NOP)
1174 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001175
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001176 TARGET(LOAD_FAST) {
1177 PyObject *value = GETLOCAL(oparg);
1178 if (value == NULL) {
1179 format_exc_check_arg(PyExc_UnboundLocalError,
1180 UNBOUNDLOCAL_ERROR_MSG,
1181 PyTuple_GetItem(co->co_varnames, oparg));
1182 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001184 Py_INCREF(value);
1185 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001187 }
1188
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001189 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001190 TARGET(LOAD_CONST) {
1191 PyObject *value = GETITEM(consts, oparg);
1192 Py_INCREF(value);
1193 PUSH(value);
1194 FAST_DISPATCH();
1195 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001196
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001197 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001198 TARGET(STORE_FAST) {
1199 PyObject *value = POP();
1200 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001202 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001203
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001204 TARGET(POP_TOP) {
1205 PyObject *value = POP();
1206 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001208 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001209
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001210 TARGET(ROT_TWO) {
1211 PyObject *top = TOP();
1212 PyObject *second = SECOND();
1213 SET_TOP(second);
1214 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001216 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001217
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001218 TARGET(ROT_THREE) {
1219 PyObject *top = TOP();
1220 PyObject *second = SECOND();
1221 PyObject *third = THIRD();
1222 SET_TOP(second);
1223 SET_SECOND(third);
1224 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001226 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001227
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001228 TARGET(DUP_TOP) {
1229 PyObject *top = TOP();
1230 Py_INCREF(top);
1231 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001233 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001234
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001235 TARGET(DUP_TOP_TWO) {
1236 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001237 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001238 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001239 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001240 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001241 SET_TOP(top);
1242 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001243 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001244 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001245
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001246 TARGET(UNARY_POSITIVE) {
1247 PyObject *value = TOP();
1248 PyObject *res = PyNumber_Positive(value);
1249 Py_DECREF(value);
1250 SET_TOP(res);
1251 if (res == NULL)
1252 goto error;
1253 DISPATCH();
1254 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001255
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001256 TARGET(UNARY_NEGATIVE) {
1257 PyObject *value = TOP();
1258 PyObject *res = PyNumber_Negative(value);
1259 Py_DECREF(value);
1260 SET_TOP(res);
1261 if (res == NULL)
1262 goto error;
1263 DISPATCH();
1264 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001265
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001266 TARGET(UNARY_NOT) {
1267 PyObject *value = TOP();
1268 int err = PyObject_IsTrue(value);
1269 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 if (err == 0) {
1271 Py_INCREF(Py_True);
1272 SET_TOP(Py_True);
1273 DISPATCH();
1274 }
1275 else if (err > 0) {
1276 Py_INCREF(Py_False);
1277 SET_TOP(Py_False);
1278 err = 0;
1279 DISPATCH();
1280 }
1281 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001282 goto error;
1283 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001284
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001285 TARGET(UNARY_INVERT) {
1286 PyObject *value = TOP();
1287 PyObject *res = PyNumber_Invert(value);
1288 Py_DECREF(value);
1289 SET_TOP(res);
1290 if (res == NULL)
1291 goto error;
1292 DISPATCH();
1293 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001294
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001295 TARGET(BINARY_POWER) {
1296 PyObject *exp = POP();
1297 PyObject *base = TOP();
1298 PyObject *res = PyNumber_Power(base, exp, Py_None);
1299 Py_DECREF(base);
1300 Py_DECREF(exp);
1301 SET_TOP(res);
1302 if (res == NULL)
1303 goto error;
1304 DISPATCH();
1305 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001306
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001307 TARGET(BINARY_MULTIPLY) {
1308 PyObject *right = POP();
1309 PyObject *left = TOP();
1310 PyObject *res = PyNumber_Multiply(left, right);
1311 Py_DECREF(left);
1312 Py_DECREF(right);
1313 SET_TOP(res);
1314 if (res == NULL)
1315 goto error;
1316 DISPATCH();
1317 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001318
Benjamin Petersond51374e2014-04-09 23:55:56 -04001319 TARGET(BINARY_MATRIX_MULTIPLY) {
1320 PyObject *right = POP();
1321 PyObject *left = TOP();
1322 PyObject *res = PyNumber_MatrixMultiply(left, right);
1323 Py_DECREF(left);
1324 Py_DECREF(right);
1325 SET_TOP(res);
1326 if (res == NULL)
1327 goto error;
1328 DISPATCH();
1329 }
1330
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001331 TARGET(BINARY_TRUE_DIVIDE) {
1332 PyObject *divisor = POP();
1333 PyObject *dividend = TOP();
1334 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1335 Py_DECREF(dividend);
1336 Py_DECREF(divisor);
1337 SET_TOP(quotient);
1338 if (quotient == NULL)
1339 goto error;
1340 DISPATCH();
1341 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001342
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001343 TARGET(BINARY_FLOOR_DIVIDE) {
1344 PyObject *divisor = POP();
1345 PyObject *dividend = TOP();
1346 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1347 Py_DECREF(dividend);
1348 Py_DECREF(divisor);
1349 SET_TOP(quotient);
1350 if (quotient == NULL)
1351 goto error;
1352 DISPATCH();
1353 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001354
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001355 TARGET(BINARY_MODULO) {
1356 PyObject *divisor = POP();
1357 PyObject *dividend = TOP();
1358 PyObject *res = PyUnicode_CheckExact(dividend) ?
1359 PyUnicode_Format(dividend, divisor) :
1360 PyNumber_Remainder(dividend, divisor);
1361 Py_DECREF(divisor);
1362 Py_DECREF(dividend);
1363 SET_TOP(res);
1364 if (res == NULL)
1365 goto error;
1366 DISPATCH();
1367 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001368
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001369 TARGET(BINARY_ADD) {
1370 PyObject *right = POP();
1371 PyObject *left = TOP();
1372 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001373 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1374 CPython using bytecode, it is simply worthless.
1375 See http://bugs.python.org/issue21955 and
1376 http://bugs.python.org/issue10044 for the discussion. In short,
1377 no patch shown any impact on a realistic benchmark, only a minor
1378 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001379 if (PyUnicode_CheckExact(left) &&
1380 PyUnicode_CheckExact(right)) {
1381 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001382 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001383 }
1384 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001385 sum = PyNumber_Add(left, right);
1386 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001387 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001388 Py_DECREF(right);
1389 SET_TOP(sum);
1390 if (sum == NULL)
1391 goto error;
1392 DISPATCH();
1393 }
1394
1395 TARGET(BINARY_SUBTRACT) {
1396 PyObject *right = POP();
1397 PyObject *left = TOP();
1398 PyObject *diff = PyNumber_Subtract(left, right);
1399 Py_DECREF(right);
1400 Py_DECREF(left);
1401 SET_TOP(diff);
1402 if (diff == NULL)
1403 goto error;
1404 DISPATCH();
1405 }
1406
1407 TARGET(BINARY_SUBSCR) {
1408 PyObject *sub = POP();
1409 PyObject *container = TOP();
1410 PyObject *res = PyObject_GetItem(container, sub);
1411 Py_DECREF(container);
1412 Py_DECREF(sub);
1413 SET_TOP(res);
1414 if (res == NULL)
1415 goto error;
1416 DISPATCH();
1417 }
1418
1419 TARGET(BINARY_LSHIFT) {
1420 PyObject *right = POP();
1421 PyObject *left = TOP();
1422 PyObject *res = PyNumber_Lshift(left, right);
1423 Py_DECREF(left);
1424 Py_DECREF(right);
1425 SET_TOP(res);
1426 if (res == NULL)
1427 goto error;
1428 DISPATCH();
1429 }
1430
1431 TARGET(BINARY_RSHIFT) {
1432 PyObject *right = POP();
1433 PyObject *left = TOP();
1434 PyObject *res = PyNumber_Rshift(left, right);
1435 Py_DECREF(left);
1436 Py_DECREF(right);
1437 SET_TOP(res);
1438 if (res == NULL)
1439 goto error;
1440 DISPATCH();
1441 }
1442
1443 TARGET(BINARY_AND) {
1444 PyObject *right = POP();
1445 PyObject *left = TOP();
1446 PyObject *res = PyNumber_And(left, right);
1447 Py_DECREF(left);
1448 Py_DECREF(right);
1449 SET_TOP(res);
1450 if (res == NULL)
1451 goto error;
1452 DISPATCH();
1453 }
1454
1455 TARGET(BINARY_XOR) {
1456 PyObject *right = POP();
1457 PyObject *left = TOP();
1458 PyObject *res = PyNumber_Xor(left, right);
1459 Py_DECREF(left);
1460 Py_DECREF(right);
1461 SET_TOP(res);
1462 if (res == NULL)
1463 goto error;
1464 DISPATCH();
1465 }
1466
1467 TARGET(BINARY_OR) {
1468 PyObject *right = POP();
1469 PyObject *left = TOP();
1470 PyObject *res = PyNumber_Or(left, right);
1471 Py_DECREF(left);
1472 Py_DECREF(right);
1473 SET_TOP(res);
1474 if (res == NULL)
1475 goto error;
1476 DISPATCH();
1477 }
1478
1479 TARGET(LIST_APPEND) {
1480 PyObject *v = POP();
1481 PyObject *list = PEEK(oparg);
1482 int err;
1483 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001485 if (err != 0)
1486 goto error;
1487 PREDICT(JUMP_ABSOLUTE);
1488 DISPATCH();
1489 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001490
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001491 TARGET(SET_ADD) {
1492 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001493 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001494 int err;
1495 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001497 if (err != 0)
1498 goto error;
1499 PREDICT(JUMP_ABSOLUTE);
1500 DISPATCH();
1501 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001502
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001503 TARGET(INPLACE_POWER) {
1504 PyObject *exp = POP();
1505 PyObject *base = TOP();
1506 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1507 Py_DECREF(base);
1508 Py_DECREF(exp);
1509 SET_TOP(res);
1510 if (res == NULL)
1511 goto error;
1512 DISPATCH();
1513 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001514
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001515 TARGET(INPLACE_MULTIPLY) {
1516 PyObject *right = POP();
1517 PyObject *left = TOP();
1518 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1519 Py_DECREF(left);
1520 Py_DECREF(right);
1521 SET_TOP(res);
1522 if (res == NULL)
1523 goto error;
1524 DISPATCH();
1525 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001526
Benjamin Petersond51374e2014-04-09 23:55:56 -04001527 TARGET(INPLACE_MATRIX_MULTIPLY) {
1528 PyObject *right = POP();
1529 PyObject *left = TOP();
1530 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1531 Py_DECREF(left);
1532 Py_DECREF(right);
1533 SET_TOP(res);
1534 if (res == NULL)
1535 goto error;
1536 DISPATCH();
1537 }
1538
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001539 TARGET(INPLACE_TRUE_DIVIDE) {
1540 PyObject *divisor = POP();
1541 PyObject *dividend = TOP();
1542 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1543 Py_DECREF(dividend);
1544 Py_DECREF(divisor);
1545 SET_TOP(quotient);
1546 if (quotient == NULL)
1547 goto error;
1548 DISPATCH();
1549 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001550
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001551 TARGET(INPLACE_FLOOR_DIVIDE) {
1552 PyObject *divisor = POP();
1553 PyObject *dividend = TOP();
1554 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1555 Py_DECREF(dividend);
1556 Py_DECREF(divisor);
1557 SET_TOP(quotient);
1558 if (quotient == NULL)
1559 goto error;
1560 DISPATCH();
1561 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001562
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001563 TARGET(INPLACE_MODULO) {
1564 PyObject *right = POP();
1565 PyObject *left = TOP();
1566 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1567 Py_DECREF(left);
1568 Py_DECREF(right);
1569 SET_TOP(mod);
1570 if (mod == NULL)
1571 goto error;
1572 DISPATCH();
1573 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001574
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001575 TARGET(INPLACE_ADD) {
1576 PyObject *right = POP();
1577 PyObject *left = TOP();
1578 PyObject *sum;
1579 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1580 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001581 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001582 }
1583 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001584 sum = PyNumber_InPlaceAdd(left, right);
1585 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001586 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001587 Py_DECREF(right);
1588 SET_TOP(sum);
1589 if (sum == NULL)
1590 goto error;
1591 DISPATCH();
1592 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001593
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001594 TARGET(INPLACE_SUBTRACT) {
1595 PyObject *right = POP();
1596 PyObject *left = TOP();
1597 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1598 Py_DECREF(left);
1599 Py_DECREF(right);
1600 SET_TOP(diff);
1601 if (diff == NULL)
1602 goto error;
1603 DISPATCH();
1604 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001605
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001606 TARGET(INPLACE_LSHIFT) {
1607 PyObject *right = POP();
1608 PyObject *left = TOP();
1609 PyObject *res = PyNumber_InPlaceLshift(left, right);
1610 Py_DECREF(left);
1611 Py_DECREF(right);
1612 SET_TOP(res);
1613 if (res == NULL)
1614 goto error;
1615 DISPATCH();
1616 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001617
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001618 TARGET(INPLACE_RSHIFT) {
1619 PyObject *right = POP();
1620 PyObject *left = TOP();
1621 PyObject *res = PyNumber_InPlaceRshift(left, right);
1622 Py_DECREF(left);
1623 Py_DECREF(right);
1624 SET_TOP(res);
1625 if (res == NULL)
1626 goto error;
1627 DISPATCH();
1628 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001629
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001630 TARGET(INPLACE_AND) {
1631 PyObject *right = POP();
1632 PyObject *left = TOP();
1633 PyObject *res = PyNumber_InPlaceAnd(left, right);
1634 Py_DECREF(left);
1635 Py_DECREF(right);
1636 SET_TOP(res);
1637 if (res == NULL)
1638 goto error;
1639 DISPATCH();
1640 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001641
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001642 TARGET(INPLACE_XOR) {
1643 PyObject *right = POP();
1644 PyObject *left = TOP();
1645 PyObject *res = PyNumber_InPlaceXor(left, right);
1646 Py_DECREF(left);
1647 Py_DECREF(right);
1648 SET_TOP(res);
1649 if (res == NULL)
1650 goto error;
1651 DISPATCH();
1652 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001653
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001654 TARGET(INPLACE_OR) {
1655 PyObject *right = POP();
1656 PyObject *left = TOP();
1657 PyObject *res = PyNumber_InPlaceOr(left, right);
1658 Py_DECREF(left);
1659 Py_DECREF(right);
1660 SET_TOP(res);
1661 if (res == NULL)
1662 goto error;
1663 DISPATCH();
1664 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001665
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001666 TARGET(STORE_SUBSCR) {
1667 PyObject *sub = TOP();
1668 PyObject *container = SECOND();
1669 PyObject *v = THIRD();
1670 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 STACKADJ(-3);
Martin Panter95f53c12016-07-18 08:23:26 +00001672 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001673 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001675 Py_DECREF(container);
1676 Py_DECREF(sub);
1677 if (err != 0)
1678 goto error;
1679 DISPATCH();
1680 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001681
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001682 TARGET(STORE_ANNOTATION) {
1683 _Py_IDENTIFIER(__annotations__);
1684 PyObject *ann_dict;
1685 PyObject *ann = POP();
1686 PyObject *name = GETITEM(names, oparg);
1687 int err;
1688 if (f->f_locals == NULL) {
1689 PyErr_Format(PyExc_SystemError,
1690 "no locals found when storing annotation");
1691 Py_DECREF(ann);
1692 goto error;
1693 }
1694 /* first try to get __annotations__ from locals... */
1695 if (PyDict_CheckExact(f->f_locals)) {
1696 ann_dict = _PyDict_GetItemId(f->f_locals,
1697 &PyId___annotations__);
1698 if (ann_dict == NULL) {
1699 PyErr_SetString(PyExc_NameError,
1700 "__annotations__ not found");
1701 Py_DECREF(ann);
1702 goto error;
1703 }
1704 Py_INCREF(ann_dict);
1705 }
1706 else {
1707 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
1708 if (ann_str == NULL) {
1709 Py_DECREF(ann);
1710 goto error;
1711 }
1712 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
1713 if (ann_dict == NULL) {
1714 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
1715 PyErr_SetString(PyExc_NameError,
1716 "__annotations__ not found");
1717 }
1718 Py_DECREF(ann);
1719 goto error;
1720 }
1721 }
1722 /* ...if succeeded, __annotations__[name] = ann */
1723 if (PyDict_CheckExact(ann_dict)) {
1724 err = PyDict_SetItem(ann_dict, name, ann);
1725 }
1726 else {
1727 err = PyObject_SetItem(ann_dict, name, ann);
1728 }
1729 Py_DECREF(ann_dict);
Yury Selivanov50c584f2016-09-08 23:38:21 -07001730 Py_DECREF(ann);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001731 if (err != 0) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001732 goto error;
1733 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001734 DISPATCH();
1735 }
1736
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001737 TARGET(DELETE_SUBSCR) {
1738 PyObject *sub = TOP();
1739 PyObject *container = SECOND();
1740 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 STACKADJ(-2);
Martin Panter95f53c12016-07-18 08:23:26 +00001742 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001743 err = PyObject_DelItem(container, sub);
1744 Py_DECREF(container);
1745 Py_DECREF(sub);
1746 if (err != 0)
1747 goto error;
1748 DISPATCH();
1749 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001750
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001751 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001752 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001753 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001754 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001755 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001756 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 PyErr_SetString(PyExc_RuntimeError,
1758 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001759 Py_DECREF(value);
1760 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001762 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001763 Py_DECREF(value);
1764 if (res == NULL)
1765 goto error;
1766 Py_DECREF(res);
1767 DISPATCH();
1768 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001769
Thomas Wouters434d0822000-08-24 20:11:32 +00001770#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001772#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001773 TARGET(RAISE_VARARGS) {
1774 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 switch (oparg) {
1776 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001777 cause = POP(); /* cause */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001779 exc = POP(); /* exc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001780 case 0: /* Fallthrough */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001781 if (do_raise(exc, cause)) {
1782 why = WHY_EXCEPTION;
1783 goto fast_block_end;
1784 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 break;
1786 default:
1787 PyErr_SetString(PyExc_SystemError,
1788 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 break;
1790 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001791 goto error;
1792 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001793
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001794 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 retval = POP();
1796 why = WHY_RETURN;
1797 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001798 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001799
Yury Selivanov75445082015-05-11 22:57:16 -04001800 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001801 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001802 PyObject *iter = NULL;
1803 PyObject *awaitable = NULL;
1804 PyObject *obj = TOP();
1805 PyTypeObject *type = Py_TYPE(obj);
1806
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001807 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001808 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001809 }
Yury Selivanov75445082015-05-11 22:57:16 -04001810
1811 if (getter != NULL) {
1812 iter = (*getter)(obj);
1813 Py_DECREF(obj);
1814 if (iter == NULL) {
1815 SET_TOP(NULL);
1816 goto error;
1817 }
1818 }
1819 else {
1820 SET_TOP(NULL);
1821 PyErr_Format(
1822 PyExc_TypeError,
1823 "'async for' requires an object with "
1824 "__aiter__ method, got %.100s",
1825 type->tp_name);
1826 Py_DECREF(obj);
1827 goto error;
1828 }
1829
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001830 if (Py_TYPE(iter)->tp_as_async != NULL &&
1831 Py_TYPE(iter)->tp_as_async->am_anext != NULL) {
1832
1833 /* Starting with CPython 3.5.2 __aiter__ should return
1834 asynchronous iterators directly (not awaitables that
1835 resolve to asynchronous iterators.)
1836
1837 Therefore, we check if the object that was returned
1838 from __aiter__ has an __anext__ method. If it does,
1839 we wrap it in an awaitable that resolves to `iter`.
1840
1841 See http://bugs.python.org/issue27243 for more
1842 details.
1843 */
1844
1845 PyObject *wrapper = _PyAIterWrapper_New(iter);
1846 Py_DECREF(iter);
1847 SET_TOP(wrapper);
1848 DISPATCH();
1849 }
1850
Yury Selivanov5376ba92015-06-22 12:19:30 -04001851 awaitable = _PyCoro_GetAwaitableIter(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001852 if (awaitable == NULL) {
1853 SET_TOP(NULL);
1854 PyErr_Format(
1855 PyExc_TypeError,
1856 "'async for' received an invalid object "
1857 "from __aiter__: %.100s",
1858 Py_TYPE(iter)->tp_name);
1859
1860 Py_DECREF(iter);
1861 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001862 } else {
Yury Selivanov75445082015-05-11 22:57:16 -04001863 Py_DECREF(iter);
1864
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001865 if (PyErr_WarnFormat(
Yury Selivanov2edd8a12016-11-08 15:13:07 -05001866 PyExc_DeprecationWarning, 1,
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001867 "'%.100s' implements legacy __aiter__ protocol; "
1868 "__aiter__ should return an asynchronous "
1869 "iterator, not awaitable",
1870 type->tp_name))
1871 {
1872 /* Warning was converted to an error. */
1873 Py_DECREF(awaitable);
1874 SET_TOP(NULL);
1875 goto error;
1876 }
1877 }
1878
Yury Selivanov75445082015-05-11 22:57:16 -04001879 SET_TOP(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001880 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001881 DISPATCH();
1882 }
1883
1884 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001885 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001886 PyObject *next_iter = NULL;
1887 PyObject *awaitable = NULL;
1888 PyObject *aiter = TOP();
1889 PyTypeObject *type = Py_TYPE(aiter);
1890
Yury Selivanoveb636452016-09-08 22:01:51 -07001891 if (PyAsyncGen_CheckExact(aiter)) {
1892 awaitable = type->tp_as_async->am_anext(aiter);
1893 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001894 goto error;
1895 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001896 } else {
1897 if (type->tp_as_async != NULL){
1898 getter = type->tp_as_async->am_anext;
1899 }
Yury Selivanov75445082015-05-11 22:57:16 -04001900
Yury Selivanoveb636452016-09-08 22:01:51 -07001901 if (getter != NULL) {
1902 next_iter = (*getter)(aiter);
1903 if (next_iter == NULL) {
1904 goto error;
1905 }
1906 }
1907 else {
1908 PyErr_Format(
1909 PyExc_TypeError,
1910 "'async for' requires an iterator with "
1911 "__anext__ method, got %.100s",
1912 type->tp_name);
1913 goto error;
1914 }
Yury Selivanov75445082015-05-11 22:57:16 -04001915
Yury Selivanoveb636452016-09-08 22:01:51 -07001916 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1917 if (awaitable == NULL) {
1918 PyErr_Format(
1919 PyExc_TypeError,
1920 "'async for' received an invalid object "
1921 "from __anext__: %.100s",
1922 Py_TYPE(next_iter)->tp_name);
1923
1924 Py_DECREF(next_iter);
1925 goto error;
1926 } else {
1927 Py_DECREF(next_iter);
1928 }
1929 }
Yury Selivanov75445082015-05-11 22:57:16 -04001930
1931 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001932 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001933 DISPATCH();
1934 }
1935
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001936 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001937 TARGET(GET_AWAITABLE) {
1938 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001939 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001940
1941 Py_DECREF(iterable);
1942
Yury Selivanovc724bae2016-03-02 11:30:46 -05001943 if (iter != NULL && PyCoro_CheckExact(iter)) {
1944 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1945 if (yf != NULL) {
1946 /* `iter` is a coroutine object that is being
1947 awaited, `yf` is a pointer to the current awaitable
1948 being awaited on. */
1949 Py_DECREF(yf);
1950 Py_CLEAR(iter);
1951 PyErr_SetString(
1952 PyExc_RuntimeError,
1953 "coroutine is being awaited already");
1954 /* The code below jumps to `error` if `iter` is NULL. */
1955 }
1956 }
1957
Yury Selivanov75445082015-05-11 22:57:16 -04001958 SET_TOP(iter); /* Even if it's NULL */
1959
1960 if (iter == NULL) {
1961 goto error;
1962 }
1963
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001964 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001965 DISPATCH();
1966 }
1967
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001968 TARGET(YIELD_FROM) {
1969 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001970 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001971 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001972 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1973 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001974 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001975 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001976 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001977 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001978 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001979 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001980 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001981 Py_DECREF(v);
1982 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001983 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001984 if (tstate->c_tracefunc != NULL
1985 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001986 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001987 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001988 if (err < 0)
1989 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001990 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001991 SET_TOP(val);
1992 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001993 }
Martin Panter95f53c12016-07-18 08:23:26 +00001994 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001995 f->f_stacktop = stack_pointer;
1996 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001997 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001998 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001999 f->f_lasti -= sizeof(_Py_CODEUNIT);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002000 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002001 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002002
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002003 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002005
2006 if (co->co_flags & CO_ASYNC_GENERATOR) {
2007 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2008 Py_DECREF(retval);
2009 if (w == NULL) {
2010 retval = NULL;
2011 goto error;
2012 }
2013 retval = w;
2014 }
2015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 f->f_stacktop = stack_pointer;
2017 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002018 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002019 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002020
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002021 TARGET(POP_EXCEPT) {
2022 PyTryBlock *b = PyFrame_BlockPop(f);
2023 if (b->b_type != EXCEPT_HANDLER) {
2024 PyErr_SetString(PyExc_SystemError,
2025 "popped block is not an except handler");
2026 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002028 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002029 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002030 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002031
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002032 PREDICTED(POP_BLOCK);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002033 TARGET(POP_BLOCK) {
2034 PyTryBlock *b = PyFrame_BlockPop(f);
2035 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002036 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002037 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002038
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002039 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002040 TARGET(END_FINALLY) {
2041 PyObject *status = POP();
2042 if (PyLong_Check(status)) {
2043 why = (enum why_code) PyLong_AS_LONG(status);
2044 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 if (why == WHY_RETURN ||
2046 why == WHY_CONTINUE)
2047 retval = POP();
2048 if (why == WHY_SILENCED) {
2049 /* An exception was silenced by 'with', we must
2050 manually unwind the EXCEPT_HANDLER block which was
2051 created when the exception was caught, otherwise
2052 the stack will be in an inconsistent state. */
2053 PyTryBlock *b = PyFrame_BlockPop(f);
2054 assert(b->b_type == EXCEPT_HANDLER);
2055 UNWIND_EXCEPT_HANDLER(b);
2056 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002057 Py_DECREF(status);
2058 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002060 Py_DECREF(status);
2061 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002063 else if (PyExceptionClass_Check(status)) {
2064 PyObject *exc = POP();
2065 PyObject *tb = POP();
2066 PyErr_Restore(status, exc, tb);
2067 why = WHY_EXCEPTION;
2068 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002070 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 PyErr_SetString(PyExc_SystemError,
2072 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002073 Py_DECREF(status);
2074 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002076 Py_DECREF(status);
2077 DISPATCH();
2078 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002079
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002080 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002081 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002082
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002083 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002084 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002085 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
2086 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002087 PyErr_SetString(PyExc_NameError,
2088 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002089 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002090 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002091 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002092 }
2093 else {
2094 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2095 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002096 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002097 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2098 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002099 if (PyErr_ExceptionMatches(PyExc_KeyError))
2100 PyErr_SetString(PyExc_NameError,
2101 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002102 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002103 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002104 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002105 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002106 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002107 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002108
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002109 TARGET(STORE_NAME) {
2110 PyObject *name = GETITEM(names, oparg);
2111 PyObject *v = POP();
2112 PyObject *ns = f->f_locals;
2113 int err;
2114 if (ns == NULL) {
2115 PyErr_Format(PyExc_SystemError,
2116 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002117 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002118 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002119 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002120 if (PyDict_CheckExact(ns))
2121 err = PyDict_SetItem(ns, name, v);
2122 else
2123 err = PyObject_SetItem(ns, name, v);
2124 Py_DECREF(v);
2125 if (err != 0)
2126 goto error;
2127 DISPATCH();
2128 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002129
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002130 TARGET(DELETE_NAME) {
2131 PyObject *name = GETITEM(names, oparg);
2132 PyObject *ns = f->f_locals;
2133 int err;
2134 if (ns == NULL) {
2135 PyErr_Format(PyExc_SystemError,
2136 "no locals when deleting %R", name);
2137 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002139 err = PyObject_DelItem(ns, name);
2140 if (err != 0) {
2141 format_exc_check_arg(PyExc_NameError,
2142 NAME_ERROR_MSG,
2143 name);
2144 goto error;
2145 }
2146 DISPATCH();
2147 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002148
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002149 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002150 TARGET(UNPACK_SEQUENCE) {
2151 PyObject *seq = POP(), *item, **items;
2152 if (PyTuple_CheckExact(seq) &&
2153 PyTuple_GET_SIZE(seq) == oparg) {
2154 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002156 item = items[oparg];
2157 Py_INCREF(item);
2158 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002159 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002160 } else if (PyList_CheckExact(seq) &&
2161 PyList_GET_SIZE(seq) == oparg) {
2162 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002163 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002164 item = items[oparg];
2165 Py_INCREF(item);
2166 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002167 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002168 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 stack_pointer + oparg)) {
2170 STACKADJ(oparg);
2171 } else {
2172 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002173 Py_DECREF(seq);
2174 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002176 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002177 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002178 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002179
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002180 TARGET(UNPACK_EX) {
2181 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2182 PyObject *seq = POP();
2183
2184 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2185 stack_pointer + totalargs)) {
2186 stack_pointer += totalargs;
2187 } else {
2188 Py_DECREF(seq);
2189 goto error;
2190 }
2191 Py_DECREF(seq);
2192 DISPATCH();
2193 }
2194
2195 TARGET(STORE_ATTR) {
2196 PyObject *name = GETITEM(names, oparg);
2197 PyObject *owner = TOP();
2198 PyObject *v = SECOND();
2199 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002200 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002201 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002202 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002203 Py_DECREF(owner);
2204 if (err != 0)
2205 goto error;
2206 DISPATCH();
2207 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002208
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002209 TARGET(DELETE_ATTR) {
2210 PyObject *name = GETITEM(names, oparg);
2211 PyObject *owner = POP();
2212 int err;
2213 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2214 Py_DECREF(owner);
2215 if (err != 0)
2216 goto error;
2217 DISPATCH();
2218 }
2219
2220 TARGET(STORE_GLOBAL) {
2221 PyObject *name = GETITEM(names, oparg);
2222 PyObject *v = POP();
2223 int err;
2224 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002225 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002226 if (err != 0)
2227 goto error;
2228 DISPATCH();
2229 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002230
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002231 TARGET(DELETE_GLOBAL) {
2232 PyObject *name = GETITEM(names, oparg);
2233 int err;
2234 err = PyDict_DelItem(f->f_globals, name);
2235 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002237 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002238 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002239 }
2240 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002241 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002242
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002243 TARGET(LOAD_NAME) {
2244 PyObject *name = GETITEM(names, oparg);
2245 PyObject *locals = f->f_locals;
2246 PyObject *v;
2247 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002248 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002249 "no locals when loading %R", name);
2250 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002252 if (PyDict_CheckExact(locals)) {
2253 v = PyDict_GetItem(locals, name);
2254 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002255 }
2256 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002257 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002258 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002259 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2260 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 PyErr_Clear();
2262 }
2263 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002264 if (v == NULL) {
2265 v = PyDict_GetItem(f->f_globals, name);
2266 Py_XINCREF(v);
2267 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002268 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002269 v = PyDict_GetItem(f->f_builtins, name);
2270 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002271 format_exc_check_arg(
2272 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002273 NAME_ERROR_MSG, name);
2274 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002275 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002276 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002277 }
2278 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002279 v = PyObject_GetItem(f->f_builtins, name);
2280 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002281 if (PyErr_ExceptionMatches(PyExc_KeyError))
2282 format_exc_check_arg(
2283 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002284 NAME_ERROR_MSG, name);
2285 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002286 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002287 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002289 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002290 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002292 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002293
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002294 TARGET(LOAD_GLOBAL) {
2295 PyObject *name = GETITEM(names, oparg);
2296 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002297 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002298 && PyDict_CheckExact(f->f_builtins))
2299 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002300 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002301 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002302 name);
2303 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002304 if (!_PyErr_OCCURRED()) {
2305 /* _PyDict_LoadGlobal() returns NULL without raising
2306 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002307 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002308 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002309 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002310 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002312 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002314 else {
2315 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002316
2317 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002318 v = PyObject_GetItem(f->f_globals, name);
2319 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002320 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2321 goto error;
2322 PyErr_Clear();
2323
Victor Stinnerb4efc962015-11-20 09:24:02 +01002324 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002325 v = PyObject_GetItem(f->f_builtins, name);
2326 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002327 if (PyErr_ExceptionMatches(PyExc_KeyError))
2328 format_exc_check_arg(
2329 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002330 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002331 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002332 }
2333 }
2334 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002335 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002337 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002338
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002339 TARGET(DELETE_FAST) {
2340 PyObject *v = GETLOCAL(oparg);
2341 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002342 SETLOCAL(oparg, NULL);
2343 DISPATCH();
2344 }
2345 format_exc_check_arg(
2346 PyExc_UnboundLocalError,
2347 UNBOUNDLOCAL_ERROR_MSG,
2348 PyTuple_GetItem(co->co_varnames, oparg)
2349 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002350 goto error;
2351 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002352
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002353 TARGET(DELETE_DEREF) {
2354 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002355 PyObject *oldobj = PyCell_GET(cell);
2356 if (oldobj != NULL) {
2357 PyCell_SET(cell, NULL);
2358 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002359 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002360 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002361 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002362 goto error;
2363 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002364
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002365 TARGET(LOAD_CLOSURE) {
2366 PyObject *cell = freevars[oparg];
2367 Py_INCREF(cell);
2368 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002370 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002371
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002372 TARGET(LOAD_CLASSDEREF) {
2373 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002374 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002375 assert(locals);
2376 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2377 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2378 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2379 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2380 if (PyDict_CheckExact(locals)) {
2381 value = PyDict_GetItem(locals, name);
2382 Py_XINCREF(value);
2383 }
2384 else {
2385 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002386 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002387 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2388 goto error;
2389 PyErr_Clear();
2390 }
2391 }
2392 if (!value) {
2393 PyObject *cell = freevars[oparg];
2394 value = PyCell_GET(cell);
2395 if (value == NULL) {
2396 format_exc_unbound(co, oparg);
2397 goto error;
2398 }
2399 Py_INCREF(value);
2400 }
2401 PUSH(value);
2402 DISPATCH();
2403 }
2404
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002405 TARGET(LOAD_DEREF) {
2406 PyObject *cell = freevars[oparg];
2407 PyObject *value = PyCell_GET(cell);
2408 if (value == NULL) {
2409 format_exc_unbound(co, oparg);
2410 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002412 Py_INCREF(value);
2413 PUSH(value);
2414 DISPATCH();
2415 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002416
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002417 TARGET(STORE_DEREF) {
2418 PyObject *v = POP();
2419 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002420 PyObject *oldobj = PyCell_GET(cell);
2421 PyCell_SET(cell, v);
2422 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002423 DISPATCH();
2424 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002425
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002426 TARGET(BUILD_STRING) {
2427 PyObject *str;
2428 PyObject *empty = PyUnicode_New(0, 0);
2429 if (empty == NULL) {
2430 goto error;
2431 }
2432 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2433 Py_DECREF(empty);
2434 if (str == NULL)
2435 goto error;
2436 while (--oparg >= 0) {
2437 PyObject *item = POP();
2438 Py_DECREF(item);
2439 }
2440 PUSH(str);
2441 DISPATCH();
2442 }
2443
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002444 TARGET(BUILD_TUPLE) {
2445 PyObject *tup = PyTuple_New(oparg);
2446 if (tup == NULL)
2447 goto error;
2448 while (--oparg >= 0) {
2449 PyObject *item = POP();
2450 PyTuple_SET_ITEM(tup, oparg, item);
2451 }
2452 PUSH(tup);
2453 DISPATCH();
2454 }
2455
2456 TARGET(BUILD_LIST) {
2457 PyObject *list = PyList_New(oparg);
2458 if (list == NULL)
2459 goto error;
2460 while (--oparg >= 0) {
2461 PyObject *item = POP();
2462 PyList_SET_ITEM(list, oparg, item);
2463 }
2464 PUSH(list);
2465 DISPATCH();
2466 }
2467
Serhiy Storchaka73442852016-10-02 10:33:46 +03002468 TARGET(BUILD_TUPLE_UNPACK_WITH_CALL)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002469 TARGET(BUILD_TUPLE_UNPACK)
2470 TARGET(BUILD_LIST_UNPACK) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002471 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002472 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002473 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002474 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002475
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002476 if (sum == NULL)
2477 goto error;
2478
2479 for (i = oparg; i > 0; i--) {
2480 PyObject *none_val;
2481
2482 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2483 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002484 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
2485 PyErr_ExceptionMatches(PyExc_TypeError)) {
2486 PyObject *func = PEEK(1 + oparg);
2487 PyErr_Format(PyExc_TypeError,
2488 "%.200s%.200s argument after * "
2489 "must be an iterable, not %.200s",
2490 PyEval_GetFuncName(func),
2491 PyEval_GetFuncDesc(func),
2492 PEEK(i)->ob_type->tp_name);
2493 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002494 Py_DECREF(sum);
2495 goto error;
2496 }
2497 Py_DECREF(none_val);
2498 }
2499
2500 if (convert_to_tuple) {
2501 return_value = PyList_AsTuple(sum);
2502 Py_DECREF(sum);
2503 if (return_value == NULL)
2504 goto error;
2505 }
2506 else {
2507 return_value = sum;
2508 }
2509
2510 while (oparg--)
2511 Py_DECREF(POP());
2512 PUSH(return_value);
2513 DISPATCH();
2514 }
2515
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002516 TARGET(BUILD_SET) {
2517 PyObject *set = PySet_New(NULL);
2518 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002519 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002520 if (set == NULL)
2521 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002522 for (i = oparg; i > 0; i--) {
2523 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002524 if (err == 0)
2525 err = PySet_Add(set, item);
2526 Py_DECREF(item);
2527 }
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002528 STACKADJ(-oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002529 if (err != 0) {
2530 Py_DECREF(set);
2531 goto error;
2532 }
2533 PUSH(set);
2534 DISPATCH();
2535 }
2536
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002537 TARGET(BUILD_SET_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002538 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002539 PyObject *sum = PySet_New(NULL);
2540 if (sum == NULL)
2541 goto error;
2542
2543 for (i = oparg; i > 0; i--) {
2544 if (_PySet_Update(sum, PEEK(i)) < 0) {
2545 Py_DECREF(sum);
2546 goto error;
2547 }
2548 }
2549
2550 while (oparg--)
2551 Py_DECREF(POP());
2552 PUSH(sum);
2553 DISPATCH();
2554 }
2555
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002556 TARGET(BUILD_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002557 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002558 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2559 if (map == NULL)
2560 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002561 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002562 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002563 PyObject *key = PEEK(2*i);
2564 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002565 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002566 if (err != 0) {
2567 Py_DECREF(map);
2568 goto error;
2569 }
2570 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002571
2572 while (oparg--) {
2573 Py_DECREF(POP());
2574 Py_DECREF(POP());
2575 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002576 PUSH(map);
2577 DISPATCH();
2578 }
2579
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002580 TARGET(SETUP_ANNOTATIONS) {
2581 _Py_IDENTIFIER(__annotations__);
2582 int err;
2583 PyObject *ann_dict;
2584 if (f->f_locals == NULL) {
2585 PyErr_Format(PyExc_SystemError,
2586 "no locals found when setting up annotations");
2587 goto error;
2588 }
2589 /* check if __annotations__ in locals()... */
2590 if (PyDict_CheckExact(f->f_locals)) {
2591 ann_dict = _PyDict_GetItemId(f->f_locals,
2592 &PyId___annotations__);
2593 if (ann_dict == NULL) {
2594 /* ...if not, create a new one */
2595 ann_dict = PyDict_New();
2596 if (ann_dict == NULL) {
2597 goto error;
2598 }
2599 err = _PyDict_SetItemId(f->f_locals,
2600 &PyId___annotations__, ann_dict);
2601 Py_DECREF(ann_dict);
2602 if (err != 0) {
2603 goto error;
2604 }
2605 }
2606 }
2607 else {
2608 /* do the same if locals() is not a dict */
2609 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2610 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002611 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002612 }
2613 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2614 if (ann_dict == NULL) {
2615 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2616 goto error;
2617 }
2618 PyErr_Clear();
2619 ann_dict = PyDict_New();
2620 if (ann_dict == NULL) {
2621 goto error;
2622 }
2623 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2624 Py_DECREF(ann_dict);
2625 if (err != 0) {
2626 goto error;
2627 }
2628 }
2629 else {
2630 Py_DECREF(ann_dict);
2631 }
2632 }
2633 DISPATCH();
2634 }
2635
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002636 TARGET(BUILD_CONST_KEY_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002637 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002638 PyObject *map;
2639 PyObject *keys = TOP();
2640 if (!PyTuple_CheckExact(keys) ||
2641 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2642 PyErr_SetString(PyExc_SystemError,
2643 "bad BUILD_CONST_KEY_MAP keys argument");
2644 goto error;
2645 }
2646 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2647 if (map == NULL) {
2648 goto error;
2649 }
2650 for (i = oparg; i > 0; i--) {
2651 int err;
2652 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2653 PyObject *value = PEEK(i + 1);
2654 err = PyDict_SetItem(map, key, value);
2655 if (err != 0) {
2656 Py_DECREF(map);
2657 goto error;
2658 }
2659 }
2660
2661 Py_DECREF(POP());
2662 while (oparg--) {
2663 Py_DECREF(POP());
2664 }
2665 PUSH(map);
2666 DISPATCH();
2667 }
2668
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002669 TARGET(BUILD_MAP_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002670 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002671 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002672 if (sum == NULL)
2673 goto error;
2674
2675 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002676 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002677 if (PyDict_Update(sum, arg) < 0) {
2678 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2679 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002680 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002681 arg->ob_type->tp_name);
2682 }
2683 Py_DECREF(sum);
2684 goto error;
2685 }
2686 }
2687
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002688 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002689 Py_DECREF(POP());
2690 PUSH(sum);
2691 DISPATCH();
2692 }
2693
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002694 TARGET(BUILD_MAP_UNPACK_WITH_CALL) {
2695 Py_ssize_t i;
2696 PyObject *sum = PyDict_New();
2697 if (sum == NULL)
2698 goto error;
2699
2700 for (i = oparg; i > 0; i--) {
2701 PyObject *arg = PEEK(i);
2702 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
2703 PyObject *func = PEEK(2 + oparg);
2704 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2705 PyErr_Format(PyExc_TypeError,
2706 "%.200s%.200s argument after ** "
2707 "must be a mapping, not %.200s",
2708 PyEval_GetFuncName(func),
2709 PyEval_GetFuncDesc(func),
2710 arg->ob_type->tp_name);
2711 }
2712 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2713 PyObject *exc, *val, *tb;
2714 PyErr_Fetch(&exc, &val, &tb);
2715 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
2716 PyObject *key = PyTuple_GET_ITEM(val, 0);
2717 if (!PyUnicode_Check(key)) {
2718 PyErr_Format(PyExc_TypeError,
2719 "%.200s%.200s keywords must be strings",
2720 PyEval_GetFuncName(func),
2721 PyEval_GetFuncDesc(func));
2722 } else {
2723 PyErr_Format(PyExc_TypeError,
2724 "%.200s%.200s got multiple "
2725 "values for keyword argument '%U'",
2726 PyEval_GetFuncName(func),
2727 PyEval_GetFuncDesc(func),
2728 key);
2729 }
2730 Py_XDECREF(exc);
2731 Py_XDECREF(val);
2732 Py_XDECREF(tb);
2733 }
2734 else {
2735 PyErr_Restore(exc, val, tb);
2736 }
2737 }
2738 Py_DECREF(sum);
2739 goto error;
2740 }
2741 }
2742
2743 while (oparg--)
2744 Py_DECREF(POP());
2745 PUSH(sum);
2746 DISPATCH();
2747 }
2748
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002749 TARGET(MAP_ADD) {
2750 PyObject *key = TOP();
2751 PyObject *value = SECOND();
2752 PyObject *map;
2753 int err;
2754 STACKADJ(-2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002755 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002756 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002757 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002758 Py_DECREF(value);
2759 Py_DECREF(key);
2760 if (err != 0)
2761 goto error;
2762 PREDICT(JUMP_ABSOLUTE);
2763 DISPATCH();
2764 }
2765
2766 TARGET(LOAD_ATTR) {
2767 PyObject *name = GETITEM(names, oparg);
2768 PyObject *owner = TOP();
2769 PyObject *res = PyObject_GetAttr(owner, name);
2770 Py_DECREF(owner);
2771 SET_TOP(res);
2772 if (res == NULL)
2773 goto error;
2774 DISPATCH();
2775 }
2776
2777 TARGET(COMPARE_OP) {
2778 PyObject *right = POP();
2779 PyObject *left = TOP();
2780 PyObject *res = cmp_outcome(oparg, left, right);
2781 Py_DECREF(left);
2782 Py_DECREF(right);
2783 SET_TOP(res);
2784 if (res == NULL)
2785 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002786 PREDICT(POP_JUMP_IF_FALSE);
2787 PREDICT(POP_JUMP_IF_TRUE);
2788 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002789 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002790
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002791 TARGET(IMPORT_NAME) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002792 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002793 PyObject *fromlist = POP();
2794 PyObject *level = TOP();
2795 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002796 res = import_name(f, name, fromlist, level);
2797 Py_DECREF(level);
2798 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002799 SET_TOP(res);
2800 if (res == NULL)
2801 goto error;
2802 DISPATCH();
2803 }
2804
2805 TARGET(IMPORT_STAR) {
2806 PyObject *from = POP(), *locals;
2807 int err;
Victor Stinner41bb43a2013-10-29 01:19:37 +01002808 if (PyFrame_FastToLocalsWithError(f) < 0)
2809 goto error;
2810
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002811 locals = f->f_locals;
2812 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002813 PyErr_SetString(PyExc_SystemError,
2814 "no locals found during 'import *'");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002815 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002816 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002817 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002818 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002819 Py_DECREF(from);
2820 if (err != 0)
2821 goto error;
2822 DISPATCH();
2823 }
Guido van Rossum25831651993-05-19 14:50:45 +00002824
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002825 TARGET(IMPORT_FROM) {
2826 PyObject *name = GETITEM(names, oparg);
2827 PyObject *from = TOP();
2828 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002829 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002830 PUSH(res);
2831 if (res == NULL)
2832 goto error;
2833 DISPATCH();
2834 }
Thomas Wouters52152252000-08-17 22:55:00 +00002835
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002836 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002837 JUMPBY(oparg);
2838 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002839 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002840
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002841 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002842 TARGET(POP_JUMP_IF_FALSE) {
2843 PyObject *cond = POP();
2844 int err;
2845 if (cond == Py_True) {
2846 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002847 FAST_DISPATCH();
2848 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002849 if (cond == Py_False) {
2850 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002851 JUMPTO(oparg);
2852 FAST_DISPATCH();
2853 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002854 err = PyObject_IsTrue(cond);
2855 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002856 if (err > 0)
2857 err = 0;
2858 else if (err == 0)
2859 JUMPTO(oparg);
2860 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002861 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002862 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002863 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002864
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002865 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002866 TARGET(POP_JUMP_IF_TRUE) {
2867 PyObject *cond = POP();
2868 int err;
2869 if (cond == Py_False) {
2870 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 FAST_DISPATCH();
2872 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002873 if (cond == Py_True) {
2874 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002875 JUMPTO(oparg);
2876 FAST_DISPATCH();
2877 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002878 err = PyObject_IsTrue(cond);
2879 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 if (err > 0) {
2881 err = 0;
2882 JUMPTO(oparg);
2883 }
2884 else if (err == 0)
2885 ;
2886 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002887 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002889 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002890
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002891 TARGET(JUMP_IF_FALSE_OR_POP) {
2892 PyObject *cond = TOP();
2893 int err;
2894 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002895 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002896 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002897 FAST_DISPATCH();
2898 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002899 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002900 JUMPTO(oparg);
2901 FAST_DISPATCH();
2902 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002903 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002904 if (err > 0) {
2905 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002906 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002907 err = 0;
2908 }
2909 else if (err == 0)
2910 JUMPTO(oparg);
2911 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002912 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002914 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002915
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002916 TARGET(JUMP_IF_TRUE_OR_POP) {
2917 PyObject *cond = TOP();
2918 int err;
2919 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002920 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002921 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002922 FAST_DISPATCH();
2923 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002924 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 JUMPTO(oparg);
2926 FAST_DISPATCH();
2927 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002928 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002929 if (err > 0) {
2930 err = 0;
2931 JUMPTO(oparg);
2932 }
2933 else if (err == 0) {
2934 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002935 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 }
2937 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002938 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002939 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002940 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002941
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002942 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002943 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002944 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002945#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 /* Enabling this path speeds-up all while and for-loops by bypassing
2947 the per-loop checks for signals. By default, this should be turned-off
2948 because it prevents detection of a control-break in tight loops like
2949 "while 1: pass". Compile with this option turned-on when you need
2950 the speed-up and do not need break checking inside tight loops (ones
2951 that contain only instructions ending with FAST_DISPATCH).
2952 */
2953 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002954#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002955 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002956#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002957 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002958
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002959 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002960 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002961 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002962 PyObject *iter = PyObject_GetIter(iterable);
2963 Py_DECREF(iterable);
2964 SET_TOP(iter);
2965 if (iter == NULL)
2966 goto error;
2967 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002968 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002969 DISPATCH();
2970 }
2971
2972 TARGET(GET_YIELD_FROM_ITER) {
2973 /* before: [obj]; after [getiter(obj)] */
2974 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002975 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002976 if (PyCoro_CheckExact(iterable)) {
2977 /* `iterable` is a coroutine */
2978 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2979 /* and it is used in a 'yield from' expression of a
2980 regular generator. */
2981 Py_DECREF(iterable);
2982 SET_TOP(NULL);
2983 PyErr_SetString(PyExc_TypeError,
2984 "cannot 'yield from' a coroutine object "
2985 "in a non-coroutine generator");
2986 goto error;
2987 }
2988 }
2989 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002990 /* `iterable` is not a generator. */
2991 iter = PyObject_GetIter(iterable);
2992 Py_DECREF(iterable);
2993 SET_TOP(iter);
2994 if (iter == NULL)
2995 goto error;
2996 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002997 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002998 DISPATCH();
2999 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003000
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003001 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003002 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003003 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003004 PyObject *iter = TOP();
3005 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3006 if (next != NULL) {
3007 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003008 PREDICT(STORE_FAST);
3009 PREDICT(UNPACK_SEQUENCE);
3010 DISPATCH();
3011 }
3012 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003013 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
3014 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08003015 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003016 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003017 PyErr_Clear();
3018 }
3019 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003020 STACKADJ(-1);
3021 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003022 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003023 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003024 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003025 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003026
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003027 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003028 why = WHY_BREAK;
3029 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003030 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003031
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003032 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003033 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003034 if (retval == NULL)
3035 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003036 why = WHY_CONTINUE;
3037 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003038 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003039
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003040 TARGET(SETUP_LOOP)
3041 TARGET(SETUP_EXCEPT)
3042 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003043 /* NOTE: If you add any new block-setup opcodes that
3044 are not try/except/finally handlers, you may need
3045 to update the PyGen_NeedsFinalizing() function.
3046 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003048 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
3049 STACK_LEVEL());
3050 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003051 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003052
Yury Selivanov75445082015-05-11 22:57:16 -04003053 TARGET(BEFORE_ASYNC_WITH) {
3054 _Py_IDENTIFIER(__aexit__);
3055 _Py_IDENTIFIER(__aenter__);
3056
3057 PyObject *mgr = TOP();
3058 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3059 *enter;
3060 PyObject *res;
3061 if (exit == NULL)
3062 goto error;
3063 SET_TOP(exit);
3064 enter = special_lookup(mgr, &PyId___aenter__);
3065 Py_DECREF(mgr);
3066 if (enter == NULL)
3067 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003068 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003069 Py_DECREF(enter);
3070 if (res == NULL)
3071 goto error;
3072 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003073 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003074 DISPATCH();
3075 }
3076
3077 TARGET(SETUP_ASYNC_WITH) {
3078 PyObject *res = POP();
3079 /* Setup the finally block before pushing the result
3080 of __aenter__ on the stack. */
3081 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3082 STACK_LEVEL());
3083 PUSH(res);
3084 DISPATCH();
3085 }
3086
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003087 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003088 _Py_IDENTIFIER(__exit__);
3089 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003090 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08003091 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003092 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08003093 if (enter == NULL)
3094 goto error;
3095 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003096 if (exit == NULL) {
3097 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003098 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003099 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003100 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003101 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003102 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003103 Py_DECREF(enter);
3104 if (res == NULL)
3105 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003106 /* Setup the finally block before pushing the result
3107 of __enter__ on the stack. */
3108 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3109 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003110
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003111 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003112 DISPATCH();
3113 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003114
Yury Selivanov75445082015-05-11 22:57:16 -04003115 TARGET(WITH_CLEANUP_START) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04003116 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003117 how/why we entered the finally clause:
3118 - TOP = None
3119 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
3120 - TOP = WHY_*; no retval below it
3121 - (TOP, SECOND, THIRD) = exc_info()
3122 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
3123 Below them is EXIT, the context.__exit__ bound method.
3124 In the last case, we must call
3125 EXIT(TOP, SECOND, THIRD)
3126 otherwise we must call
3127 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003128
Benjamin Peterson8f169482013-10-29 22:25:06 -04003129 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003130 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04003131 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003132 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003134 In addition, if the stack represents an exception,
3135 *and* the function call returns a 'true' value, we
3136 push WHY_SILENCED onto the stack. END_FINALLY will
3137 then not re-raise the exception. (But non-local
3138 gotos should still be resumed.)
3139 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003140
Victor Stinner842cfff2016-12-01 14:45:31 +01003141 PyObject* stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003142 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003143 PyObject *exc, *val, *tb, *res;
3144
3145 val = tb = Py_None;
3146 exc = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003147 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003148 (void)POP();
3149 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003150 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003151 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003152 else if (PyLong_Check(exc)) {
3153 STACKADJ(-1);
3154 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003155 case WHY_RETURN:
3156 case WHY_CONTINUE:
3157 /* Retval in TOP. */
3158 exit_func = SECOND();
3159 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003160 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003161 break;
3162 default:
3163 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003164 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003165 break;
3166 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003167 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003168 }
3169 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003170 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003171 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003172 val = SECOND();
3173 tb = THIRD();
3174 tp2 = FOURTH();
3175 exc2 = PEEK(5);
3176 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003177 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003178 SET_VALUE(7, tb2);
3179 SET_VALUE(6, exc2);
3180 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003181 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3182 SET_FOURTH(NULL);
3183 /* We just shifted the stack down, so we have
3184 to tell the except handler block that the
3185 values are lower than it expects. */
3186 block = &f->f_blockstack[f->f_iblock - 1];
3187 assert(block->b_type == EXCEPT_HANDLER);
3188 block->b_level--;
3189 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003190
3191 stack[0] = exc;
3192 stack[1] = val;
3193 stack[2] = tb;
3194 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003195 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003196 if (res == NULL)
3197 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003198
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003199 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003200 PUSH(exc);
3201 PUSH(res);
3202 PREDICT(WITH_CLEANUP_FINISH);
3203 DISPATCH();
3204 }
3205
3206 PREDICTED(WITH_CLEANUP_FINISH);
3207 TARGET(WITH_CLEANUP_FINISH) {
3208 PyObject *res = POP();
3209 PyObject *exc = POP();
3210 int err;
3211
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003212 if (exc != Py_None)
3213 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003214 else
3215 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003216
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003217 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003218 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003220 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003221 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003222 else if (err > 0) {
3223 err = 0;
3224 /* There was an exception and a True return */
3225 PUSH(PyLong_FromLong((long) WHY_SILENCED));
3226 }
3227 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003228 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003229 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003230
Yury Selivanovf2392132016-12-13 19:03:51 -05003231 TARGET(LOAD_METHOD) {
3232 /* Designed to work in tamdem with CALL_METHOD. */
3233 PyObject *name = GETITEM(names, oparg);
3234 PyObject *obj = TOP();
3235 PyObject *meth = NULL;
3236
3237 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3238
3239 SET_TOP(meth); /* Replace `obj` on top; OK if NULL. */
3240 if (meth == NULL) {
3241 /* Most likely attribute wasn't found. */
3242 Py_DECREF(obj);
3243 goto error;
3244 }
3245
3246 if (meth_found) {
3247 /* The method object is now on top of the stack.
3248 Push `obj` back to the stack, so that the stack
3249 layout would be:
3250
3251 method | obj | arg1 | ... | argN
3252 */
3253 PUSH(obj);
3254 }
3255 else {
3256 /* Not a method (but a regular attr, or something
3257 was returned by a descriptor protocol). Push
3258 NULL to the top of the stack, to signal
3259 CALL_METHOD that it's not a method call.
3260 */
3261 Py_DECREF(obj);
3262 PUSH(NULL);
3263 }
3264 DISPATCH();
3265 }
3266
3267 TARGET(CALL_METHOD) {
3268 /* Designed to work in tamdem with LOAD_METHOD. */
3269 PyObject **sp, *res, *obj;
3270
3271 sp = stack_pointer;
3272
3273 obj = PEEK(oparg + 1);
3274 if (obj == NULL) {
3275 /* `obj` is NULL when LOAD_METHOD thinks that it's not
3276 a method call. Swap the NULL and callable.
3277
3278 Stack layout:
3279
3280 ... | callable | NULL | arg1 | ... | argN
3281 ^- TOP()
3282 ^- (-oparg)
3283 ^- (-oparg-1)
3284 ^- (-oparg-2)
3285
3286 after the next line it will be:
3287
3288 ... | callable | callable | arg1 | ... | argN
3289 ^- TOP()
3290 ^- (-oparg)
3291 ^- (-oparg-1)
3292 ^- (-oparg-2)
3293
3294 Right side `callable` will be POPed by call_funtion.
3295 Left side `callable` will be POPed manually later
3296 (one of "callbale" refs on the stack is borrowed.)
3297 */
3298 SET_VALUE(oparg + 1, PEEK(oparg + 2));
3299 res = call_function(&sp, oparg, NULL);
3300 stack_pointer = sp;
3301 (void)POP(); /* POP the left side callable. */
3302 }
3303 else {
3304 /* This is a method call. Stack layout:
3305
3306 ... | method | obj | arg1 | ... | argN
3307 ^- TOP()
3308 ^- (-oparg)
3309 ^- (-oparg-1)
3310
3311 `obj` and `method` will be POPed by call_function.
3312 We'll be passing `oparg + 1` to call_function, to
3313 make it accept the `obj` as a first argument.
3314 */
3315 res = call_function(&sp, oparg + 1, NULL);
3316 stack_pointer = sp;
3317 }
3318
3319 PUSH(res);
3320 if (res == NULL)
3321 goto error;
3322 DISPATCH();
3323 }
3324
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003325 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003326 TARGET(CALL_FUNCTION) {
3327 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003328 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003329 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003330 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003331 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003332 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003333 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003334 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003335 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003336 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003337
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003338 TARGET(CALL_FUNCTION_KW) {
3339 PyObject **sp, *res, *names;
3340
3341 names = POP();
3342 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003343 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003344 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003345 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003346 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003347 Py_DECREF(names);
3348
3349 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003350 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003351 }
3352 DISPATCH();
3353 }
3354
3355 TARGET(CALL_FUNCTION_EX) {
3356 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003357 if (oparg & 0x01) {
3358 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003359 if (!PyDict_CheckExact(kwargs)) {
3360 PyObject *d = PyDict_New();
3361 if (d == NULL)
3362 goto error;
3363 if (PyDict_Update(d, kwargs) != 0) {
3364 Py_DECREF(d);
3365 /* PyDict_Update raises attribute
3366 * error (percolated from an attempt
3367 * to get 'keys' attribute) instead of
3368 * a type error if its second argument
3369 * is not a mapping.
3370 */
3371 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3372 func = SECOND();
3373 PyErr_Format(PyExc_TypeError,
3374 "%.200s%.200s argument after ** "
3375 "must be a mapping, not %.200s",
3376 PyEval_GetFuncName(func),
3377 PyEval_GetFuncDesc(func),
3378 kwargs->ob_type->tp_name);
3379 }
Victor Stinnereece2222016-09-12 11:16:37 +02003380 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003381 goto error;
3382 }
3383 Py_DECREF(kwargs);
3384 kwargs = d;
3385 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003386 assert(PyDict_CheckExact(kwargs));
3387 }
3388 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003389 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003390 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003391 if (Py_TYPE(callargs)->tp_iter == NULL &&
3392 !PySequence_Check(callargs)) {
3393 PyErr_Format(PyExc_TypeError,
3394 "%.200s%.200s argument after * "
3395 "must be an iterable, not %.200s",
3396 PyEval_GetFuncName(func),
3397 PyEval_GetFuncDesc(func),
3398 callargs->ob_type->tp_name);
Victor Stinnereece2222016-09-12 11:16:37 +02003399 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003400 goto error;
3401 }
3402 Py_SETREF(callargs, PySequence_Tuple(callargs));
3403 if (callargs == NULL) {
3404 goto error;
3405 }
3406 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003407 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003408
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003409 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003410 Py_DECREF(func);
3411 Py_DECREF(callargs);
3412 Py_XDECREF(kwargs);
3413
3414 SET_TOP(result);
3415 if (result == NULL) {
3416 goto error;
3417 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003418 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003419 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003420
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003421 TARGET(MAKE_FUNCTION) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003422 PyObject *qualname = POP();
3423 PyObject *codeobj = POP();
3424 PyFunctionObject *func = (PyFunctionObject *)
3425 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003426
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003427 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003428 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003429 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003430 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003431 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003432
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003433 if (oparg & 0x08) {
3434 assert(PyTuple_CheckExact(TOP()));
3435 func ->func_closure = POP();
3436 }
3437 if (oparg & 0x04) {
3438 assert(PyDict_CheckExact(TOP()));
3439 func->func_annotations = POP();
3440 }
3441 if (oparg & 0x02) {
3442 assert(PyDict_CheckExact(TOP()));
3443 func->func_kwdefaults = POP();
3444 }
3445 if (oparg & 0x01) {
3446 assert(PyTuple_CheckExact(TOP()));
3447 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003448 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003449
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003450 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003451 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003452 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003453
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003454 TARGET(BUILD_SLICE) {
3455 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003456 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003457 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003458 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003459 step = NULL;
3460 stop = POP();
3461 start = TOP();
3462 slice = PySlice_New(start, stop, step);
3463 Py_DECREF(start);
3464 Py_DECREF(stop);
3465 Py_XDECREF(step);
3466 SET_TOP(slice);
3467 if (slice == NULL)
3468 goto error;
3469 DISPATCH();
3470 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003471
Eric V. Smitha78c7952015-11-03 12:45:05 -05003472 TARGET(FORMAT_VALUE) {
3473 /* Handles f-string value formatting. */
3474 PyObject *result;
3475 PyObject *fmt_spec;
3476 PyObject *value;
3477 PyObject *(*conv_fn)(PyObject *);
3478 int which_conversion = oparg & FVC_MASK;
3479 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3480
3481 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003482 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003483
3484 /* See if any conversion is specified. */
3485 switch (which_conversion) {
3486 case FVC_STR: conv_fn = PyObject_Str; break;
3487 case FVC_REPR: conv_fn = PyObject_Repr; break;
3488 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3489
3490 /* Must be 0 (meaning no conversion), since only four
3491 values are allowed by (oparg & FVC_MASK). */
3492 default: conv_fn = NULL; break;
3493 }
3494
3495 /* If there's a conversion function, call it and replace
3496 value with that result. Otherwise, just use value,
3497 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003498 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003499 result = conv_fn(value);
3500 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003501 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003502 Py_XDECREF(fmt_spec);
3503 goto error;
3504 }
3505 value = result;
3506 }
3507
3508 /* If value is a unicode object, and there's no fmt_spec,
3509 then we know the result of format(value) is value
3510 itself. In that case, skip calling format(). I plan to
3511 move this optimization in to PyObject_Format()
3512 itself. */
3513 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3514 /* Do nothing, just transfer ownership to result. */
3515 result = value;
3516 } else {
3517 /* Actually call format(). */
3518 result = PyObject_Format(value, fmt_spec);
3519 Py_DECREF(value);
3520 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003521 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003522 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003523 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003524 }
3525
Eric V. Smith135d5f42016-02-05 18:23:08 -05003526 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003527 DISPATCH();
3528 }
3529
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003530 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003531 int oldoparg = oparg;
3532 NEXTOPARG();
3533 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003534 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003535 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003536
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003537
Antoine Pitrou042b1282010-08-13 21:15:58 +00003538#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003539 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003540#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003541 default:
3542 fprintf(stderr,
3543 "XXX lineno: %d, opcode: %d\n",
3544 PyFrame_GetLineNumber(f),
3545 opcode);
3546 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003547 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003548
3549#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003550 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003551#endif
3552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003553 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003554
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003555 /* This should never be reached. Every opcode should end with DISPATCH()
3556 or goto error. */
3557 assert(0);
Guido van Rossumac7be682001-01-17 15:42:30 +00003558
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003559error:
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003560
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003561 assert(why == WHY_NOT);
3562 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003563
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003564 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003565#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003566 if (!PyErr_Occurred())
3567 PyErr_SetString(PyExc_SystemError,
3568 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003569#else
3570 assert(PyErr_Occurred());
3571#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003572
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003573 /* Log traceback info. */
3574 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003575
Benjamin Peterson51f46162013-01-23 08:38:47 -05003576 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003577 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3578 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003579
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003580fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003581 assert(why != WHY_NOT);
3582
3583 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003584 while (why != WHY_NOT && f->f_iblock > 0) {
3585 /* Peek at the current block. */
3586 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003588 assert(why != WHY_YIELD);
3589 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3590 why = WHY_NOT;
3591 JUMPTO(PyLong_AS_LONG(retval));
3592 Py_DECREF(retval);
3593 break;
3594 }
3595 /* Now we have to pop the block. */
3596 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003598 if (b->b_type == EXCEPT_HANDLER) {
3599 UNWIND_EXCEPT_HANDLER(b);
3600 continue;
3601 }
3602 UNWIND_BLOCK(b);
3603 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3604 why = WHY_NOT;
3605 JUMPTO(b->b_handler);
3606 break;
3607 }
3608 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3609 || b->b_type == SETUP_FINALLY)) {
3610 PyObject *exc, *val, *tb;
3611 int handler = b->b_handler;
3612 /* Beware, this invalidates all b->b_* fields */
3613 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3614 PUSH(tstate->exc_traceback);
3615 PUSH(tstate->exc_value);
3616 if (tstate->exc_type != NULL) {
3617 PUSH(tstate->exc_type);
3618 }
3619 else {
3620 Py_INCREF(Py_None);
3621 PUSH(Py_None);
3622 }
3623 PyErr_Fetch(&exc, &val, &tb);
3624 /* Make the raw exception data
3625 available to the handler,
3626 so a program can emulate the
3627 Python main loop. */
3628 PyErr_NormalizeException(
3629 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003630 if (tb != NULL)
3631 PyException_SetTraceback(val, tb);
3632 else
3633 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003634 Py_INCREF(exc);
3635 tstate->exc_type = exc;
3636 Py_INCREF(val);
3637 tstate->exc_value = val;
3638 tstate->exc_traceback = tb;
3639 if (tb == NULL)
3640 tb = Py_None;
3641 Py_INCREF(tb);
3642 PUSH(tb);
3643 PUSH(val);
3644 PUSH(exc);
3645 why = WHY_NOT;
3646 JUMPTO(handler);
3647 break;
3648 }
3649 if (b->b_type == SETUP_FINALLY) {
3650 if (why & (WHY_RETURN | WHY_CONTINUE))
3651 PUSH(retval);
3652 PUSH(PyLong_FromLong((long)why));
3653 why = WHY_NOT;
3654 JUMPTO(b->b_handler);
3655 break;
3656 }
3657 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003659 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003661 if (why != WHY_NOT)
3662 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00003663
Victor Stinnerace47d72013-07-18 01:41:08 +02003664 assert(!PyErr_Occurred());
3665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003666 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003668 assert(why != WHY_YIELD);
3669 /* Pop remaining stack entries. */
3670 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003671 PyObject *o = POP();
3672 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003673 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003675 if (why != WHY_RETURN)
3676 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003677
Victor Stinner4a7cc882015-03-06 23:35:27 +01003678 assert((retval != NULL) ^ (PyErr_Occurred() != NULL));
Victor Stinnerace47d72013-07-18 01:41:08 +02003679
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003680fast_yield:
Yury Selivanoveb636452016-09-08 22:01:51 -07003681 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01003682
Benjamin Petersonac913412011-07-03 16:25:11 -05003683 /* The purpose of this block is to put aside the generator's exception
3684 state and restore that of the calling frame. If the current
3685 exception state is from the caller, we clear the exception values
3686 on the generator frame, so they are not swapped back in latter. The
3687 origin of the current exception state is determined by checking for
3688 except handler blocks, which we must be in iff a new exception
3689 state came into existence in this frame. (An uncaught exception
3690 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3691 int i;
Victor Stinner74319ae2016-08-25 00:04:09 +02003692 for (i = 0; i < f->f_iblock; i++) {
3693 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER) {
Benjamin Petersonac913412011-07-03 16:25:11 -05003694 break;
Victor Stinner74319ae2016-08-25 00:04:09 +02003695 }
3696 }
Benjamin Petersonac913412011-07-03 16:25:11 -05003697 if (i == f->f_iblock)
3698 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003699 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003700 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003701 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003702 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003704 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003705 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003706 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003707 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3708 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003709 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003710 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003711 why = WHY_EXCEPTION;
3712 }
3713 }
3714 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003715 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3716 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003717 PyTrace_RETURN, NULL);
3718 }
3719 }
3720 if (tstate->c_profilefunc) {
3721 if (why == WHY_EXCEPTION)
3722 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003723 tstate->c_profileobj,
3724 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003725 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003726 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3727 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003728 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003729 Py_CLEAR(retval);
Brett Cannonb94767f2011-02-22 20:15:44 +00003730 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003731 }
3732 }
3733 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003735 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003736exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003737 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3738 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003739 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003740 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003741 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003742
Victor Stinnerefde1462015-03-21 15:04:43 +01003743 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003744}
3745
Benjamin Petersonb204a422011-06-05 22:04:07 -05003746static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003747format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3748{
3749 int err;
3750 Py_ssize_t len = PyList_GET_SIZE(names);
3751 PyObject *name_str, *comma, *tail, *tmp;
3752
3753 assert(PyList_CheckExact(names));
3754 assert(len >= 1);
3755 /* Deal with the joys of natural language. */
3756 switch (len) {
3757 case 1:
3758 name_str = PyList_GET_ITEM(names, 0);
3759 Py_INCREF(name_str);
3760 break;
3761 case 2:
3762 name_str = PyUnicode_FromFormat("%U and %U",
3763 PyList_GET_ITEM(names, len - 2),
3764 PyList_GET_ITEM(names, len - 1));
3765 break;
3766 default:
3767 tail = PyUnicode_FromFormat(", %U, and %U",
3768 PyList_GET_ITEM(names, len - 2),
3769 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003770 if (tail == NULL)
3771 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003772 /* Chop off the last two objects in the list. This shouldn't actually
3773 fail, but we can't be too careful. */
3774 err = PyList_SetSlice(names, len - 2, len, NULL);
3775 if (err == -1) {
3776 Py_DECREF(tail);
3777 return;
3778 }
3779 /* Stitch everything up into a nice comma-separated list. */
3780 comma = PyUnicode_FromString(", ");
3781 if (comma == NULL) {
3782 Py_DECREF(tail);
3783 return;
3784 }
3785 tmp = PyUnicode_Join(comma, names);
3786 Py_DECREF(comma);
3787 if (tmp == NULL) {
3788 Py_DECREF(tail);
3789 return;
3790 }
3791 name_str = PyUnicode_Concat(tmp, tail);
3792 Py_DECREF(tmp);
3793 Py_DECREF(tail);
3794 break;
3795 }
3796 if (name_str == NULL)
3797 return;
3798 PyErr_Format(PyExc_TypeError,
3799 "%U() missing %i required %s argument%s: %U",
3800 co->co_name,
3801 len,
3802 kind,
3803 len == 1 ? "" : "s",
3804 name_str);
3805 Py_DECREF(name_str);
3806}
3807
3808static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003809missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003810 PyObject **fastlocals)
3811{
Victor Stinner74319ae2016-08-25 00:04:09 +02003812 Py_ssize_t i, j = 0;
3813 Py_ssize_t start, end;
3814 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003815 const char *kind = positional ? "positional" : "keyword-only";
3816 PyObject *missing_names;
3817
3818 /* Compute the names of the arguments that are missing. */
3819 missing_names = PyList_New(missing);
3820 if (missing_names == NULL)
3821 return;
3822 if (positional) {
3823 start = 0;
3824 end = co->co_argcount - defcount;
3825 }
3826 else {
3827 start = co->co_argcount;
3828 end = start + co->co_kwonlyargcount;
3829 }
3830 for (i = start; i < end; i++) {
3831 if (GETLOCAL(i) == NULL) {
3832 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3833 PyObject *name = PyObject_Repr(raw);
3834 if (name == NULL) {
3835 Py_DECREF(missing_names);
3836 return;
3837 }
3838 PyList_SET_ITEM(missing_names, j++, name);
3839 }
3840 }
3841 assert(j == missing);
3842 format_missing(kind, co, missing_names);
3843 Py_DECREF(missing_names);
3844}
3845
3846static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003847too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3848 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003849{
3850 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003851 Py_ssize_t kwonly_given = 0;
3852 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003853 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003854 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003855
Benjamin Petersone109c702011-06-24 09:37:26 -05003856 assert((co->co_flags & CO_VARARGS) == 0);
3857 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003858 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3859 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003860 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003861 }
3862 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003863 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003864 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003865 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003866 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003867 }
3868 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003869 plural = (co_argcount != 1);
3870 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003871 }
3872 if (sig == NULL)
3873 return;
3874 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003875 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3876 kwonly_sig = PyUnicode_FromFormat(format,
3877 given != 1 ? "s" : "",
3878 kwonly_given,
3879 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003880 if (kwonly_sig == NULL) {
3881 Py_DECREF(sig);
3882 return;
3883 }
3884 }
3885 else {
3886 /* This will not fail. */
3887 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003888 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003889 }
3890 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003891 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003892 co->co_name,
3893 sig,
3894 plural ? "s" : "",
3895 given,
3896 kwonly_sig,
3897 given == 1 && !kwonly_given ? "was" : "were");
3898 Py_DECREF(sig);
3899 Py_DECREF(kwonly_sig);
3900}
3901
Guido van Rossumc2e20742006-02-27 22:32:47 +00003902/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003903 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003904 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003905
Victor Stinner40ee3012014-06-16 15:59:28 +02003906static PyObject *
3907_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Victor Stinner74319ae2016-08-25 00:04:09 +02003908 PyObject **args, Py_ssize_t argcount,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003909 PyObject **kwnames, PyObject **kwargs,
3910 Py_ssize_t kwcount, int kwstep,
Victor Stinner74319ae2016-08-25 00:04:09 +02003911 PyObject **defs, Py_ssize_t defcount,
3912 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003913 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003914{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003915 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003916 PyFrameObject *f;
3917 PyObject *retval = NULL;
3918 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003919 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003920 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003921 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3922 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003923 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003925 if (globals == NULL) {
3926 PyErr_SetString(PyExc_SystemError,
3927 "PyEval_EvalCodeEx: NULL globals");
3928 return NULL;
3929 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003930
Victor Stinnerc7020012016-08-16 23:40:29 +02003931 /* Create the frame */
3932 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003933 assert(tstate != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003934 f = PyFrame_New(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003935 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003936 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003937 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003938 fastlocals = f->f_localsplus;
3939 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003940
Victor Stinnerc7020012016-08-16 23:40:29 +02003941 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003942 if (co->co_flags & CO_VARKEYWORDS) {
3943 kwdict = PyDict_New();
3944 if (kwdict == NULL)
3945 goto fail;
3946 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003947 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003948 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003949 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003950 SETLOCAL(i, kwdict);
3951 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003952 else {
3953 kwdict = NULL;
3954 }
3955
3956 /* Copy positional arguments into local variables */
3957 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003958 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003959 }
3960 else {
3961 n = argcount;
3962 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003963 for (i = 0; i < n; i++) {
3964 x = args[i];
3965 Py_INCREF(x);
3966 SETLOCAL(i, x);
3967 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003968
3969 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003970 if (co->co_flags & CO_VARARGS) {
3971 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003972 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003973 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003974 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003975 SETLOCAL(total_args, u);
3976 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003977 x = args[i];
3978 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003979 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003980 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003981 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003982
Serhiy Storchakab7281052016-09-12 00:52:40 +03003983 /* Handle keyword arguments passed as two strided arrays */
3984 kwcount *= kwstep;
3985 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003986 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003987 PyObject *keyword = kwnames[i];
3988 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003989 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003990
Benjamin Petersonb204a422011-06-05 22:04:07 -05003991 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3992 PyErr_Format(PyExc_TypeError,
3993 "%U() keywords must be strings",
3994 co->co_name);
3995 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003996 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003997
Benjamin Petersonb204a422011-06-05 22:04:07 -05003998 /* Speed hack: do raw pointer compares. As names are
3999 normally interned this should almost always hit. */
4000 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
4001 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004002 PyObject *name = co_varnames[j];
4003 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004004 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004005 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004006 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004007
Benjamin Petersonb204a422011-06-05 22:04:07 -05004008 /* Slow fallback, just in case */
4009 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004010 PyObject *name = co_varnames[j];
4011 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4012 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004013 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004014 }
4015 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004016 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004017 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004018 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004019
Benjamin Petersonb204a422011-06-05 22:04:07 -05004020 if (j >= total_args && kwdict == NULL) {
4021 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02004022 "%U() got an unexpected keyword argument '%S'",
4023 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004024 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004025 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004026
Christian Heimes0bd447f2013-07-20 14:48:10 +02004027 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4028 goto fail;
4029 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004030 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004031
Benjamin Petersonb204a422011-06-05 22:04:07 -05004032 kw_found:
4033 if (GETLOCAL(j) != NULL) {
4034 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02004035 "%U() got multiple values for argument '%S'",
4036 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004037 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004038 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004039 Py_INCREF(value);
4040 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004041 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004042
4043 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004044 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004045 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004046 goto fail;
4047 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004048
4049 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004050 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02004051 Py_ssize_t m = co->co_argcount - defcount;
4052 Py_ssize_t missing = 0;
4053 for (i = argcount; i < m; i++) {
4054 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004055 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004056 }
4057 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004058 if (missing) {
4059 missing_arguments(co, missing, defcount, fastlocals);
4060 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004061 }
4062 if (n > m)
4063 i = n - m;
4064 else
4065 i = 0;
4066 for (; i < defcount; i++) {
4067 if (GETLOCAL(m+i) == NULL) {
4068 PyObject *def = defs[i];
4069 Py_INCREF(def);
4070 SETLOCAL(m+i, def);
4071 }
4072 }
4073 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004074
4075 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004076 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004077 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004078 for (i = co->co_argcount; i < total_args; i++) {
4079 PyObject *name;
4080 if (GETLOCAL(i) != NULL)
4081 continue;
4082 name = PyTuple_GET_ITEM(co->co_varnames, i);
4083 if (kwdefs != NULL) {
4084 PyObject *def = PyDict_GetItem(kwdefs, name);
4085 if (def) {
4086 Py_INCREF(def);
4087 SETLOCAL(i, def);
4088 continue;
4089 }
4090 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004091 missing++;
4092 }
4093 if (missing) {
4094 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004095 goto fail;
4096 }
4097 }
4098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004099 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004100 vars into frame. */
4101 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004102 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05004103 int arg;
4104 /* Possibly account for the cell variable being an argument. */
4105 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004106 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004107 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004108 /* Clear the local copy. */
4109 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004110 }
4111 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004112 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004113 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004114 if (c == NULL)
4115 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004116 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004117 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004118
4119 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004120 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4121 PyObject *o = PyTuple_GET_ITEM(closure, i);
4122 Py_INCREF(o);
4123 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004124 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004125
Yury Selivanoveb636452016-09-08 22:01:51 -07004126 /* Handle generator/coroutine/asynchronous generator */
4127 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004128 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04004129 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004130 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004131
4132 if (is_coro && tstate->in_coroutine_wrapper) {
4133 assert(coro_wrapper != NULL);
4134 PyErr_Format(PyExc_RuntimeError,
4135 "coroutine wrapper %.200R attempted "
4136 "to recursively wrap %.200R",
4137 coro_wrapper,
4138 co);
4139 goto fail;
4140 }
Yury Selivanov75445082015-05-11 22:57:16 -04004141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004142 /* Don't need to keep the reference to f_back, it will be set
4143 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004144 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004146 /* Create a new generator that owns the ready to run frame
4147 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004148 if (is_coro) {
4149 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004150 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4151 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004152 } else {
4153 gen = PyGen_NewWithQualName(f, name, qualname);
4154 }
Yury Selivanov75445082015-05-11 22:57:16 -04004155 if (gen == NULL)
4156 return NULL;
4157
Yury Selivanov94c22632015-06-04 10:16:51 -04004158 if (is_coro && coro_wrapper != NULL) {
4159 PyObject *wrapped;
4160 tstate->in_coroutine_wrapper = 1;
4161 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4162 tstate->in_coroutine_wrapper = 0;
4163 return wrapped;
4164 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004165
Yury Selivanov75445082015-05-11 22:57:16 -04004166 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004167 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004168
Victor Stinner59a73272016-12-09 18:51:13 +01004169 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004170
Thomas Woutersce272b62007-09-19 21:19:28 +00004171fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004173 /* decref'ing the frame can cause __del__ methods to get invoked,
4174 which can call back into Python. While we're done with the
4175 current Python frame (f), the associated C stack is still in use,
4176 so recursion_depth must be boosted for the duration.
4177 */
4178 assert(tstate != NULL);
4179 ++tstate->recursion_depth;
4180 Py_DECREF(f);
4181 --tstate->recursion_depth;
4182 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004183}
4184
Victor Stinner40ee3012014-06-16 15:59:28 +02004185PyObject *
4186PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
4187 PyObject **args, int argcount, PyObject **kws, int kwcount,
4188 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
4189{
4190 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004191 args, argcount,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004192 kws, kws + 1, kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004193 defs, defcount,
4194 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004195 NULL, NULL);
4196}
Tim Peters5ca576e2001-06-18 22:08:13 +00004197
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004198static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004199special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004200{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004201 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004202 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004203 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004204 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004205 return NULL;
4206 }
4207 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004208}
4209
4210
Benjamin Peterson87880242011-07-03 16:48:31 -05004211/* These 3 functions deal with the exception state of generators. */
4212
4213static void
4214save_exc_state(PyThreadState *tstate, PyFrameObject *f)
4215{
4216 PyObject *type, *value, *traceback;
4217 Py_XINCREF(tstate->exc_type);
4218 Py_XINCREF(tstate->exc_value);
4219 Py_XINCREF(tstate->exc_traceback);
4220 type = f->f_exc_type;
4221 value = f->f_exc_value;
4222 traceback = f->f_exc_traceback;
4223 f->f_exc_type = tstate->exc_type;
4224 f->f_exc_value = tstate->exc_value;
4225 f->f_exc_traceback = tstate->exc_traceback;
4226 Py_XDECREF(type);
4227 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004228 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05004229}
4230
4231static void
4232swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
4233{
4234 PyObject *tmp;
4235 tmp = tstate->exc_type;
4236 tstate->exc_type = f->f_exc_type;
4237 f->f_exc_type = tmp;
4238 tmp = tstate->exc_value;
4239 tstate->exc_value = f->f_exc_value;
4240 f->f_exc_value = tmp;
4241 tmp = tstate->exc_traceback;
4242 tstate->exc_traceback = f->f_exc_traceback;
4243 f->f_exc_traceback = tmp;
4244}
4245
4246static void
4247restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
4248{
4249 PyObject *type, *value, *tb;
4250 type = tstate->exc_type;
4251 value = tstate->exc_value;
4252 tb = tstate->exc_traceback;
4253 tstate->exc_type = f->f_exc_type;
4254 tstate->exc_value = f->f_exc_value;
4255 tstate->exc_traceback = f->f_exc_traceback;
4256 f->f_exc_type = NULL;
4257 f->f_exc_value = NULL;
4258 f->f_exc_traceback = NULL;
4259 Py_XDECREF(type);
4260 Py_XDECREF(value);
4261 Py_XDECREF(tb);
4262}
4263
4264
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004265/* Logic for the raise statement (too complicated for inlining).
4266 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004267static int
Collin Winter828f04a2007-08-31 00:04:24 +00004268do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004270 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004272 if (exc == NULL) {
4273 /* Reraise */
4274 PyThreadState *tstate = PyThreadState_GET();
4275 PyObject *tb;
4276 type = tstate->exc_type;
4277 value = tstate->exc_value;
4278 tb = tstate->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004279 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004280 PyErr_SetString(PyExc_RuntimeError,
4281 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004282 return 0;
4283 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004284 Py_XINCREF(type);
4285 Py_XINCREF(value);
4286 Py_XINCREF(tb);
4287 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004288 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004289 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004291 /* We support the following forms of raise:
4292 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004293 raise <instance>
4294 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004296 if (PyExceptionClass_Check(exc)) {
4297 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004298 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004299 if (value == NULL)
4300 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004301 if (!PyExceptionInstance_Check(value)) {
4302 PyErr_Format(PyExc_TypeError,
4303 "calling %R should have returned an instance of "
4304 "BaseException, not %R",
4305 type, Py_TYPE(value));
4306 goto raise_error;
4307 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004308 }
4309 else if (PyExceptionInstance_Check(exc)) {
4310 value = exc;
4311 type = PyExceptionInstance_Class(exc);
4312 Py_INCREF(type);
4313 }
4314 else {
4315 /* Not something you can raise. You get an exception
4316 anyway, just not what you specified :-) */
4317 Py_DECREF(exc);
4318 PyErr_SetString(PyExc_TypeError,
4319 "exceptions must derive from BaseException");
4320 goto raise_error;
4321 }
Collin Winter828f04a2007-08-31 00:04:24 +00004322
Serhiy Storchakac0191582016-09-27 11:37:10 +03004323 assert(type != NULL);
4324 assert(value != NULL);
4325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004326 if (cause) {
4327 PyObject *fixed_cause;
4328 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004329 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004330 if (fixed_cause == NULL)
4331 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004332 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004333 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004334 else if (PyExceptionInstance_Check(cause)) {
4335 fixed_cause = cause;
4336 }
4337 else if (cause == Py_None) {
4338 Py_DECREF(cause);
4339 fixed_cause = NULL;
4340 }
4341 else {
4342 PyErr_SetString(PyExc_TypeError,
4343 "exception causes must derive from "
4344 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004345 goto raise_error;
4346 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004347 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004348 }
Collin Winter828f04a2007-08-31 00:04:24 +00004349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004350 PyErr_SetObject(type, value);
4351 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004352 Py_DECREF(value);
4353 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004354 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004355
4356raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004357 Py_XDECREF(value);
4358 Py_XDECREF(type);
4359 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004360 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004361}
4362
Tim Petersd6d010b2001-06-21 02:49:55 +00004363/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004364 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004365
Guido van Rossum0368b722007-05-11 16:50:42 +00004366 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4367 with a variable target.
4368*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004369
Barry Warsawe42b18f1997-08-25 22:13:04 +00004370static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004371unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004372{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004373 int i = 0, j = 0;
4374 Py_ssize_t ll = 0;
4375 PyObject *it; /* iter(v) */
4376 PyObject *w;
4377 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004379 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004381 it = PyObject_GetIter(v);
4382 if (it == NULL)
4383 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00004384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004385 for (; i < argcnt; i++) {
4386 w = PyIter_Next(it);
4387 if (w == NULL) {
4388 /* Iterator done, via error or exhaustion. */
4389 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004390 if (argcntafter == -1) {
4391 PyErr_Format(PyExc_ValueError,
4392 "not enough values to unpack (expected %d, got %d)",
4393 argcnt, i);
4394 }
4395 else {
4396 PyErr_Format(PyExc_ValueError,
4397 "not enough values to unpack "
4398 "(expected at least %d, got %d)",
4399 argcnt + argcntafter, i);
4400 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004401 }
4402 goto Error;
4403 }
4404 *--sp = w;
4405 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004407 if (argcntafter == -1) {
4408 /* We better have exhausted the iterator now. */
4409 w = PyIter_Next(it);
4410 if (w == NULL) {
4411 if (PyErr_Occurred())
4412 goto Error;
4413 Py_DECREF(it);
4414 return 1;
4415 }
4416 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004417 PyErr_Format(PyExc_ValueError,
4418 "too many values to unpack (expected %d)",
4419 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004420 goto Error;
4421 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004423 l = PySequence_List(it);
4424 if (l == NULL)
4425 goto Error;
4426 *--sp = l;
4427 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004429 ll = PyList_GET_SIZE(l);
4430 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004431 PyErr_Format(PyExc_ValueError,
4432 "not enough values to unpack (expected at least %d, got %zd)",
4433 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004434 goto Error;
4435 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004437 /* Pop the "after-variable" args off the list. */
4438 for (j = argcntafter; j > 0; j--, i++) {
4439 *--sp = PyList_GET_ITEM(l, ll - j);
4440 }
4441 /* Resize the list. */
4442 Py_SIZE(l) = ll - argcntafter;
4443 Py_DECREF(it);
4444 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004445
Tim Petersd6d010b2001-06-21 02:49:55 +00004446Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004447 for (; i > 0; i--, sp++)
4448 Py_DECREF(*sp);
4449 Py_XDECREF(it);
4450 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004451}
4452
4453
Guido van Rossum96a42c81992-01-12 02:29:51 +00004454#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004455static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004456prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004457{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004458 printf("%s ", str);
4459 if (PyObject_Print(v, stdout, 0) != 0)
4460 PyErr_Clear(); /* Don't know what else to do */
4461 printf("\n");
4462 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004463}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004464#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004465
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004466static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004467call_exc_trace(Py_tracefunc func, PyObject *self,
4468 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004469{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004470 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004471 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004472 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004473 if (value == NULL) {
4474 value = Py_None;
4475 Py_INCREF(value);
4476 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004477 PyErr_NormalizeException(&type, &value, &orig_traceback);
4478 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004479 arg = PyTuple_Pack(3, type, value, traceback);
4480 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004481 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004482 return;
4483 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004484 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004485 Py_DECREF(arg);
4486 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004487 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004488 else {
4489 Py_XDECREF(type);
4490 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004491 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004492 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004493}
4494
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004495static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004496call_trace_protected(Py_tracefunc func, PyObject *obj,
4497 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004498 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004499{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004500 PyObject *type, *value, *traceback;
4501 int err;
4502 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004503 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004504 if (err == 0)
4505 {
4506 PyErr_Restore(type, value, traceback);
4507 return 0;
4508 }
4509 else {
4510 Py_XDECREF(type);
4511 Py_XDECREF(value);
4512 Py_XDECREF(traceback);
4513 return -1;
4514 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004515}
4516
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004517static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004518call_trace(Py_tracefunc func, PyObject *obj,
4519 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004520 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004521{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004522 int result;
4523 if (tstate->tracing)
4524 return 0;
4525 tstate->tracing++;
4526 tstate->use_tracing = 0;
4527 result = func(obj, frame, what, arg);
4528 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4529 || (tstate->c_profilefunc != NULL));
4530 tstate->tracing--;
4531 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004532}
4533
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004534PyObject *
4535_PyEval_CallTracing(PyObject *func, PyObject *args)
4536{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004537 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004538 int save_tracing = tstate->tracing;
4539 int save_use_tracing = tstate->use_tracing;
4540 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004542 tstate->tracing = 0;
4543 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4544 || (tstate->c_profilefunc != NULL));
4545 result = PyObject_Call(func, args, NULL);
4546 tstate->tracing = save_tracing;
4547 tstate->use_tracing = save_use_tracing;
4548 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004549}
4550
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004551/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004552static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004553maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004554 PyThreadState *tstate, PyFrameObject *frame,
4555 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004556{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004557 int result = 0;
4558 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004560 /* If the last instruction executed isn't in the current
4561 instruction window, reset the window.
4562 */
4563 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4564 PyAddrPair bounds;
4565 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4566 &bounds);
4567 *instr_lb = bounds.ap_lower;
4568 *instr_ub = bounds.ap_upper;
4569 }
4570 /* If the last instruction falls at the start of a line or if
4571 it represents a jump backwards, update the frame's line
4572 number and call the trace function. */
4573 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
4574 frame->f_lineno = line;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004575 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004576 }
4577 *instr_prev = frame->f_lasti;
4578 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004579}
4580
Fred Drake5755ce62001-06-27 19:19:46 +00004581void
4582PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004583{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004584 PyThreadState *tstate = PyThreadState_GET();
4585 PyObject *temp = tstate->c_profileobj;
4586 Py_XINCREF(arg);
4587 tstate->c_profilefunc = NULL;
4588 tstate->c_profileobj = NULL;
4589 /* Must make sure that tracing is not ignored if 'temp' is freed */
4590 tstate->use_tracing = tstate->c_tracefunc != NULL;
4591 Py_XDECREF(temp);
4592 tstate->c_profilefunc = func;
4593 tstate->c_profileobj = arg;
4594 /* Flag that tracing or profiling is turned on */
4595 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004596}
4597
4598void
4599PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4600{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004601 PyThreadState *tstate = PyThreadState_GET();
4602 PyObject *temp = tstate->c_traceobj;
4603 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4604 Py_XINCREF(arg);
4605 tstate->c_tracefunc = NULL;
4606 tstate->c_traceobj = NULL;
4607 /* Must make sure that profiling is not ignored if 'temp' is freed */
4608 tstate->use_tracing = tstate->c_profilefunc != NULL;
4609 Py_XDECREF(temp);
4610 tstate->c_tracefunc = func;
4611 tstate->c_traceobj = arg;
4612 /* Flag that tracing or profiling is turned on */
4613 tstate->use_tracing = ((func != NULL)
4614 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004615}
4616
Yury Selivanov75445082015-05-11 22:57:16 -04004617void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004618_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004619{
4620 PyThreadState *tstate = PyThreadState_GET();
4621
Yury Selivanov75445082015-05-11 22:57:16 -04004622 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004623 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004624}
4625
4626PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004627_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004628{
4629 PyThreadState *tstate = PyThreadState_GET();
4630 return tstate->coroutine_wrapper;
4631}
4632
Yury Selivanoveb636452016-09-08 22:01:51 -07004633void
4634_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4635{
4636 PyThreadState *tstate = PyThreadState_GET();
4637
4638 Py_XINCREF(firstiter);
4639 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4640}
4641
4642PyObject *
4643_PyEval_GetAsyncGenFirstiter(void)
4644{
4645 PyThreadState *tstate = PyThreadState_GET();
4646 return tstate->async_gen_firstiter;
4647}
4648
4649void
4650_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4651{
4652 PyThreadState *tstate = PyThreadState_GET();
4653
4654 Py_XINCREF(finalizer);
4655 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4656}
4657
4658PyObject *
4659_PyEval_GetAsyncGenFinalizer(void)
4660{
4661 PyThreadState *tstate = PyThreadState_GET();
4662 return tstate->async_gen_finalizer;
4663}
4664
Guido van Rossumb209a111997-04-29 18:18:01 +00004665PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004666PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004667{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004668 PyFrameObject *current_frame = PyEval_GetFrame();
4669 if (current_frame == NULL)
4670 return PyThreadState_GET()->interp->builtins;
4671 else
4672 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004673}
4674
Guido van Rossumb209a111997-04-29 18:18:01 +00004675PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004676PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004677{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004678 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004679 if (current_frame == NULL) {
4680 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004681 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004682 }
4683
4684 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4685 return NULL;
4686
4687 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004688 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004689}
4690
Guido van Rossumb209a111997-04-29 18:18:01 +00004691PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004692PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004693{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004694 PyFrameObject *current_frame = PyEval_GetFrame();
4695 if (current_frame == NULL)
4696 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004697
4698 assert(current_frame->f_globals != NULL);
4699 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004700}
4701
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004702PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004703PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004704{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004705 PyThreadState *tstate = PyThreadState_GET();
4706 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004707}
4708
Guido van Rossum6135a871995-01-09 17:53:26 +00004709int
Tim Peters5ba58662001-07-16 02:29:45 +00004710PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004711{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004712 PyFrameObject *current_frame = PyEval_GetFrame();
4713 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004715 if (current_frame != NULL) {
4716 const int codeflags = current_frame->f_code->co_flags;
4717 const int compilerflags = codeflags & PyCF_MASK;
4718 if (compilerflags) {
4719 result = 1;
4720 cf->cf_flags |= compilerflags;
4721 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004722#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004723 if (codeflags & CO_GENERATOR_ALLOWED) {
4724 result = 1;
4725 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4726 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004727#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004728 }
4729 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004730}
4731
Guido van Rossum3f5da241990-12-20 15:06:42 +00004732
Guido van Rossum681d79a1995-07-18 14:51:37 +00004733/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00004734 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00004735
Guido van Rossumb209a111997-04-29 18:18:01 +00004736PyObject *
Victor Stinner2d0eb652016-12-06 16:27:24 +01004737PyEval_CallObjectWithKeywords(PyObject *callable,
4738 PyObject *args, PyObject *kwargs)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004739{
Victor Stinner59b356d2015-03-16 11:52:32 +01004740#ifdef Py_DEBUG
4741 /* PyEval_CallObjectWithKeywords() must not be called with an exception
4742 set. It raises a new exception if parameters are invalid or if
4743 PyTuple_New() fails, and so the original exception is lost. */
4744 assert(!PyErr_Occurred());
4745#endif
4746
Victor Stinner8a31c822016-08-19 17:12:23 +02004747 if (args == NULL) {
Victor Stinner2d0eb652016-12-06 16:27:24 +01004748 return _PyObject_FastCallDict(callable, NULL, 0, kwargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004749 }
Victor Stinner155ea652016-08-22 23:26:00 +02004750
4751 if (!PyTuple_Check(args)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004752 PyErr_SetString(PyExc_TypeError,
4753 "argument list must be a tuple");
4754 return NULL;
4755 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00004756
Victor Stinner8a31c822016-08-19 17:12:23 +02004757 if (kwargs != NULL && !PyDict_Check(kwargs)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004758 PyErr_SetString(PyExc_TypeError,
4759 "keyword list must be a dictionary");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004760 return NULL;
4761 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00004762
Victor Stinner2d0eb652016-12-06 16:27:24 +01004763 return PyObject_Call(callable, args, kwargs);
Jeremy Hylton52820442001-01-03 23:52:36 +00004764}
4765
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004766const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004767PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004768{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004769 if (PyMethod_Check(func))
4770 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4771 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004772 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004773 else if (PyCFunction_Check(func))
4774 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4775 else
4776 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004777}
4778
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004779const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004780PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004781{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004782 if (PyMethod_Check(func))
4783 return "()";
4784 else if (PyFunction_Check(func))
4785 return "()";
4786 else if (PyCFunction_Check(func))
4787 return "()";
4788 else
4789 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004790}
4791
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004792#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004793if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004794 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4795 tstate, tstate->frame, \
4796 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004797 x = NULL; \
4798 } \
4799 else { \
4800 x = call; \
4801 if (tstate->c_profilefunc != NULL) { \
4802 if (x == NULL) { \
4803 call_trace_protected(tstate->c_profilefunc, \
4804 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004805 tstate, tstate->frame, \
4806 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004807 /* XXX should pass (type, value, tb) */ \
4808 } else { \
4809 if (call_trace(tstate->c_profilefunc, \
4810 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004811 tstate, tstate->frame, \
4812 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004813 Py_DECREF(x); \
4814 x = NULL; \
4815 } \
4816 } \
4817 } \
4818 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004819} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004820 x = call; \
4821 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004822
Victor Stinnerc6944e72016-11-11 02:13:35 +01004823static PyObject* _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004824call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004825{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004826 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004827 PyObject *func = *pfunc;
4828 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004829 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4830 Py_ssize_t nargs = oparg - nkwargs;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004831 PyObject **stack;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004833 /* Always dispatch PyCFunction first, because these are
4834 presumed to be the most frequent callable object.
4835 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004836 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004837 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004838
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004839 stack = (*pp_stack) - nargs - nkwargs;
4840 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004841 }
4842 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004843 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004844 /* Optimize access to bound methods. Reuse the Python stack
4845 to pass 'self' as the first argument, replace 'func'
4846 with 'self'. It avoids the creation of a new temporary tuple
4847 for arguments (to replace func with self) when the method uses
4848 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004849 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004850 Py_INCREF(self);
4851 func = PyMethod_GET_FUNCTION(func);
4852 Py_INCREF(func);
4853 Py_SETREF(*pfunc, self);
4854 nargs++;
4855 }
4856 else {
4857 Py_INCREF(func);
4858 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004859
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004860 stack = (*pp_stack) - nargs - nkwargs;
Victor Stinner4a7cc882015-03-06 23:35:27 +01004861
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004862 if (PyFunction_Check(func)) {
4863 x = fast_function(func, stack, nargs, kwnames);
4864 }
4865 else {
4866 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4867 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004868
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004869 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004870 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004871
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004872 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004874 /* Clear the stack of the function object. Also removes
4875 the arguments in case they weren't consumed already
Victor Stinnere90bdb12016-08-25 23:26:50 +02004876 (fast_function() and err_args() leave them on the stack).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004877 */
4878 while ((*pp_stack) > pfunc) {
4879 w = EXT_POP(*pp_stack);
4880 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004881 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004883 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004884}
4885
Victor Stinnere90bdb12016-08-25 23:26:50 +02004886/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004887 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004888 For the simplest case -- a function that takes only positional
4889 arguments and is called with only positional arguments -- it
4890 inlines the most primitive frame setup code from
4891 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4892 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004893*/
4894
Victor Stinnerc6944e72016-11-11 02:13:35 +01004895static PyObject* _Py_HOT_FUNCTION
Victor Stinnerd8735722016-09-09 12:36:44 -07004896_PyFunction_FastCall(PyCodeObject *co, PyObject **args, Py_ssize_t nargs,
4897 PyObject *globals)
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004898{
4899 PyFrameObject *f;
4900 PyThreadState *tstate = PyThreadState_GET();
4901 PyObject **fastlocals;
4902 Py_ssize_t i;
4903 PyObject *result;
4904
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004905 assert(globals != NULL);
4906 /* XXX Perhaps we should create a specialized
4907 PyFrame_New() that doesn't take locals, but does
4908 take builtins without sanity checking them.
4909 */
4910 assert(tstate != NULL);
4911 f = PyFrame_New(tstate, co, globals, NULL);
4912 if (f == NULL) {
4913 return NULL;
4914 }
4915
4916 fastlocals = f->f_localsplus;
4917
Victor Stinner74319ae2016-08-25 00:04:09 +02004918 for (i = 0; i < nargs; i++) {
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004919 Py_INCREF(*args);
4920 fastlocals[i] = *args++;
4921 }
Victor Stinner59a73272016-12-09 18:51:13 +01004922 result = PyEval_EvalFrameEx(f,0);
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004923
4924 ++tstate->recursion_depth;
4925 Py_DECREF(f);
4926 --tstate->recursion_depth;
4927
4928 return result;
4929}
4930
Victor Stinnere90bdb12016-08-25 23:26:50 +02004931static PyObject *
Victor Stinnerd8735722016-09-09 12:36:44 -07004932fast_function(PyObject *func, PyObject **stack,
4933 Py_ssize_t nargs, PyObject *kwnames)
Jeremy Hylton52820442001-01-03 23:52:36 +00004934{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004935 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4936 PyObject *globals = PyFunction_GET_GLOBALS(func);
4937 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004938 PyObject *kwdefs, *closure, *name, *qualname;
4939 PyObject **d;
Victor Stinnerd8735722016-09-09 12:36:44 -07004940 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004941 Py_ssize_t nd;
Victor Stinnerd8735722016-09-09 12:36:44 -07004942
Victor Stinner57f91ac2016-09-12 13:37:07 +02004943 assert(PyFunction_Check(func));
4944 assert(nargs >= 0);
4945 assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
Victor Stinnerd8735722016-09-09 12:36:44 -07004946 assert((nargs == 0 && nkwargs == 0) || stack != NULL);
Victor Stinner57f91ac2016-09-12 13:37:07 +02004947 /* kwnames must only contains str strings, no subclass, and all keys must
4948 be unique */
Victor Stinner577e1f82016-08-25 00:29:32 +02004949
Victor Stinner74319ae2016-08-25 00:04:09 +02004950 if (co->co_kwonlyargcount == 0 && nkwargs == 0 &&
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004951 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
4952 {
Victor Stinner2eedc112016-08-22 12:29:42 +02004953 if (argdefs == NULL && co->co_argcount == nargs) {
Victor Stinnerd8735722016-09-09 12:36:44 -07004954 return _PyFunction_FastCall(co, stack, nargs, globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02004955 }
4956 else if (nargs == 0 && argdefs != NULL
4957 && co->co_argcount == Py_SIZE(argdefs)) {
4958 /* function called with no arguments, but all parameters have
4959 a default value: use default values as arguments .*/
4960 stack = &PyTuple_GET_ITEM(argdefs, 0);
Victor Stinnerd8735722016-09-09 12:36:44 -07004961 return _PyFunction_FastCall(co, stack, Py_SIZE(argdefs), globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02004962 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004963 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004964
4965 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
4966 closure = PyFunction_GET_CLOSURE(func);
4967 name = ((PyFunctionObject *)func) -> func_name;
4968 qualname = ((PyFunctionObject *)func) -> func_qualname;
4969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004970 if (argdefs != NULL) {
4971 d = &PyTuple_GET_ITEM(argdefs, 0);
4972 nd = Py_SIZE(argdefs);
4973 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004974 else {
4975 d = NULL;
4976 nd = 0;
4977 }
4978 return _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
Victor Stinner2eedc112016-08-22 12:29:42 +02004979 stack, nargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004980 nkwargs ? &PyTuple_GET_ITEM(kwnames, 0) : NULL,
4981 stack + nargs,
4982 nkwargs, 1,
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004983 d, (int)nd, kwdefs,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004984 closure, name, qualname);
4985}
4986
4987PyObject *
Victor Stinnerd8735722016-09-09 12:36:44 -07004988_PyFunction_FastCallKeywords(PyObject *func, PyObject **stack,
4989 Py_ssize_t nargs, PyObject *kwnames)
4990{
4991 return fast_function(func, stack, nargs, kwnames);
4992}
4993
4994PyObject *
Victor Stinner74319ae2016-08-25 00:04:09 +02004995_PyFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs,
Victor Stinnerb9009392016-08-22 23:15:44 +02004996 PyObject *kwargs)
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004997{
4998 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4999 PyObject *globals = PyFunction_GET_GLOBALS(func);
5000 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
5001 PyObject *kwdefs, *closure, *name, *qualname;
Victor Stinnerb9009392016-08-22 23:15:44 +02005002 PyObject *kwtuple, **k;
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005003 PyObject **d;
Victor Stinner74319ae2016-08-25 00:04:09 +02005004 Py_ssize_t nd, nk;
Victor Stinnerb9009392016-08-22 23:15:44 +02005005 PyObject *result;
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005006
Victor Stinner74319ae2016-08-25 00:04:09 +02005007 assert(func != NULL);
5008 assert(nargs >= 0);
5009 assert(nargs == 0 || args != NULL);
Victor Stinnerb9009392016-08-22 23:15:44 +02005010 assert(kwargs == NULL || PyDict_Check(kwargs));
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005011
Victor Stinnerb9009392016-08-22 23:15:44 +02005012 if (co->co_kwonlyargcount == 0 &&
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02005013 (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) &&
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005014 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
5015 {
Victor Stinnerb9009392016-08-22 23:15:44 +02005016 /* Fast paths */
Victor Stinner2eedc112016-08-22 12:29:42 +02005017 if (argdefs == NULL && co->co_argcount == nargs) {
Victor Stinnerd8735722016-09-09 12:36:44 -07005018 return _PyFunction_FastCall(co, args, nargs, globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02005019 }
5020 else if (nargs == 0 && argdefs != NULL
5021 && co->co_argcount == Py_SIZE(argdefs)) {
5022 /* function called with no arguments, but all parameters have
5023 a default value: use default values as arguments .*/
5024 args = &PyTuple_GET_ITEM(argdefs, 0);
Victor Stinnerd8735722016-09-09 12:36:44 -07005025 return _PyFunction_FastCall(co, args, Py_SIZE(argdefs), globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02005026 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005027 }
5028
Victor Stinnerb9009392016-08-22 23:15:44 +02005029 if (kwargs != NULL) {
5030 Py_ssize_t pos, i;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02005031 nk = PyDict_GET_SIZE(kwargs);
Victor Stinnerb9009392016-08-22 23:15:44 +02005032
5033 kwtuple = PyTuple_New(2 * nk);
5034 if (kwtuple == NULL) {
5035 return NULL;
5036 }
5037
5038 k = &PyTuple_GET_ITEM(kwtuple, 0);
5039 pos = i = 0;
5040 while (PyDict_Next(kwargs, &pos, &k[i], &k[i+1])) {
5041 Py_INCREF(k[i]);
5042 Py_INCREF(k[i+1]);
5043 i += 2;
5044 }
5045 nk = i / 2;
5046 }
5047 else {
5048 kwtuple = NULL;
5049 k = NULL;
5050 nk = 0;
5051 }
5052
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005053 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
5054 closure = PyFunction_GET_CLOSURE(func);
5055 name = ((PyFunctionObject *)func) -> func_name;
5056 qualname = ((PyFunctionObject *)func) -> func_qualname;
5057
5058 if (argdefs != NULL) {
5059 d = &PyTuple_GET_ITEM(argdefs, 0);
5060 nd = Py_SIZE(argdefs);
5061 }
5062 else {
5063 d = NULL;
5064 nd = 0;
5065 }
Victor Stinnerb9009392016-08-22 23:15:44 +02005066
5067 result = _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
5068 args, nargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03005069 k, k + 1, nk, 2,
Victor Stinnerb9009392016-08-22 23:15:44 +02005070 d, nd, kwdefs,
5071 closure, name, qualname);
5072 Py_XDECREF(kwtuple);
5073 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00005074}
5075
5076static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005077do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005078{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005079 if (PyCFunction_Check(func)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005080 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005081 PyThreadState *tstate = PyThreadState_GET();
5082 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005083 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005084 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005085 else {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005086 return PyObject_Call(func, callargs, kwdict);
Victor Stinner74319ae2016-08-25 00:04:09 +02005087 }
Jeremy Hylton52820442001-01-03 23:52:36 +00005088}
5089
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005090/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005091 nb_index slot defined, and store in *pi.
5092 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
5093 and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005094 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005095*/
Tim Petersb5196382001-12-16 19:44:20 +00005096/* Note: If v is NULL, return success without storing into *pi. This
5097 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
5098 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00005099*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005100int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005101_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005102{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005103 if (v != NULL) {
5104 Py_ssize_t x;
5105 if (PyIndex_Check(v)) {
5106 x = PyNumber_AsSsize_t(v, NULL);
5107 if (x == -1 && PyErr_Occurred())
5108 return 0;
5109 }
5110 else {
5111 PyErr_SetString(PyExc_TypeError,
5112 "slice indices must be integers or "
5113 "None or have an __index__ method");
5114 return 0;
5115 }
5116 *pi = x;
5117 }
5118 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005119}
5120
Guido van Rossum486364b2007-06-30 05:01:58 +00005121#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005122 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00005123
Guido van Rossumb209a111997-04-29 18:18:01 +00005124static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02005125cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005126{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005127 int res = 0;
5128 switch (op) {
5129 case PyCmp_IS:
5130 res = (v == w);
5131 break;
5132 case PyCmp_IS_NOT:
5133 res = (v != w);
5134 break;
5135 case PyCmp_IN:
5136 res = PySequence_Contains(w, v);
5137 if (res < 0)
5138 return NULL;
5139 break;
5140 case PyCmp_NOT_IN:
5141 res = PySequence_Contains(w, v);
5142 if (res < 0)
5143 return NULL;
5144 res = !res;
5145 break;
5146 case PyCmp_EXC_MATCH:
5147 if (PyTuple_Check(w)) {
5148 Py_ssize_t i, length;
5149 length = PyTuple_Size(w);
5150 for (i = 0; i < length; i += 1) {
5151 PyObject *exc = PyTuple_GET_ITEM(w, i);
5152 if (!PyExceptionClass_Check(exc)) {
5153 PyErr_SetString(PyExc_TypeError,
5154 CANNOT_CATCH_MSG);
5155 return NULL;
5156 }
5157 }
5158 }
5159 else {
5160 if (!PyExceptionClass_Check(w)) {
5161 PyErr_SetString(PyExc_TypeError,
5162 CANNOT_CATCH_MSG);
5163 return NULL;
5164 }
5165 }
5166 res = PyErr_GivenExceptionMatches(v, w);
5167 break;
5168 default:
5169 return PyObject_RichCompare(v, w, op);
5170 }
5171 v = res ? Py_True : Py_False;
5172 Py_INCREF(v);
5173 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005174}
5175
Thomas Wouters52152252000-08-17 22:55:00 +00005176static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005177import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
5178{
5179 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005180 PyObject *import_func, *res;
5181 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005182
5183 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
5184 if (import_func == NULL) {
5185 PyErr_SetString(PyExc_ImportError, "__import__ not found");
5186 return NULL;
5187 }
5188
5189 /* Fast path for not overloaded __import__. */
5190 if (import_func == PyThreadState_GET()->interp->import_func) {
5191 int ilevel = _PyLong_AsInt(level);
5192 if (ilevel == -1 && PyErr_Occurred()) {
5193 return NULL;
5194 }
5195 res = PyImport_ImportModuleLevelObject(
5196 name,
5197 f->f_globals,
5198 f->f_locals == NULL ? Py_None : f->f_locals,
5199 fromlist,
5200 ilevel);
5201 return res;
5202 }
5203
5204 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005205
5206 stack[0] = name;
5207 stack[1] = f->f_globals;
5208 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5209 stack[3] = fromlist;
5210 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005211 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005212 Py_DECREF(import_func);
5213 return res;
5214}
5215
5216static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00005217import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005219 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005220 _Py_IDENTIFIER(__name__);
5221 PyObject *fullmodname, *pkgname;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005223 x = PyObject_GetAttr(v, name);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005224 if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
5225 return x;
5226 /* Issue #17636: in case this failed because of a circular relative
5227 import, try to fallback on reading the module directly from
5228 sys.modules. */
5229 PyErr_Clear();
5230 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005231 if (pkgname == NULL) {
5232 goto error;
5233 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005234 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
5235 Py_DECREF(pkgname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005236 if (fullmodname == NULL) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005237 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005238 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005239 x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005240 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005241 if (x == NULL) {
5242 goto error;
5243 }
5244 Py_INCREF(x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005245 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005246 error:
5247 PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
5248 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005249}
Guido van Rossumac7be682001-01-17 15:42:30 +00005250
Thomas Wouters52152252000-08-17 22:55:00 +00005251static int
5252import_all_from(PyObject *locals, PyObject *v)
5253{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005254 _Py_IDENTIFIER(__all__);
5255 _Py_IDENTIFIER(__dict__);
5256 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005257 PyObject *dict, *name, *value;
5258 int skip_leading_underscores = 0;
5259 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005261 if (all == NULL) {
5262 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5263 return -1; /* Unexpected error */
5264 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005265 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005266 if (dict == NULL) {
5267 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5268 return -1;
5269 PyErr_SetString(PyExc_ImportError,
5270 "from-import-* object has no __dict__ and no __all__");
5271 return -1;
5272 }
5273 all = PyMapping_Keys(dict);
5274 Py_DECREF(dict);
5275 if (all == NULL)
5276 return -1;
5277 skip_leading_underscores = 1;
5278 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005280 for (pos = 0, err = 0; ; pos++) {
5281 name = PySequence_GetItem(all, pos);
5282 if (name == NULL) {
5283 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5284 err = -1;
5285 else
5286 PyErr_Clear();
5287 break;
5288 }
5289 if (skip_leading_underscores &&
5290 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005291 PyUnicode_READY(name) != -1 &&
5292 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005293 {
5294 Py_DECREF(name);
5295 continue;
5296 }
5297 value = PyObject_GetAttr(v, name);
5298 if (value == NULL)
5299 err = -1;
5300 else if (PyDict_CheckExact(locals))
5301 err = PyDict_SetItem(locals, name, value);
5302 else
5303 err = PyObject_SetItem(locals, name, value);
5304 Py_DECREF(name);
5305 Py_XDECREF(value);
5306 if (err != 0)
5307 break;
5308 }
5309 Py_DECREF(all);
5310 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005311}
5312
Guido van Rossumac7be682001-01-17 15:42:30 +00005313static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005314format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005316 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005318 if (!obj)
5319 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005320
Serhiy Storchaka06515832016-11-20 09:13:07 +02005321 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005322 if (!obj_str)
5323 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005325 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005326}
Guido van Rossum950361c1997-01-24 13:49:28 +00005327
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005328static void
5329format_exc_unbound(PyCodeObject *co, int oparg)
5330{
5331 PyObject *name;
5332 /* Don't stomp existing exception */
5333 if (PyErr_Occurred())
5334 return;
5335 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5336 name = PyTuple_GET_ITEM(co->co_cellvars,
5337 oparg);
5338 format_exc_check_arg(
5339 PyExc_UnboundLocalError,
5340 UNBOUNDLOCAL_ERROR_MSG,
5341 name);
5342 } else {
5343 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5344 PyTuple_GET_SIZE(co->co_cellvars));
5345 format_exc_check_arg(PyExc_NameError,
5346 UNBOUNDFREE_ERROR_MSG, name);
5347 }
5348}
5349
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005350static PyObject *
5351unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005352 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005353{
5354 PyObject *res;
5355 if (Py_REFCNT(v) == 2) {
5356 /* In the common case, there are 2 references to the value
5357 * stored in 'variable' when the += is performed: one on the
5358 * value stack (in 'v') and one still stored in the
5359 * 'variable'. We try to delete the variable now to reduce
5360 * the refcnt to 1.
5361 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005362 int opcode, oparg;
5363 NEXTOPARG();
5364 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005365 case STORE_FAST:
5366 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005367 PyObject **fastlocals = f->f_localsplus;
5368 if (GETLOCAL(oparg) == v)
5369 SETLOCAL(oparg, NULL);
5370 break;
5371 }
5372 case STORE_DEREF:
5373 {
5374 PyObject **freevars = (f->f_localsplus +
5375 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005376 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005377 if (PyCell_GET(c) == v) {
5378 PyCell_SET(c, NULL);
5379 Py_DECREF(v);
5380 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005381 break;
5382 }
5383 case STORE_NAME:
5384 {
5385 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005386 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005387 PyObject *locals = f->f_locals;
5388 if (PyDict_CheckExact(locals) &&
5389 PyDict_GetItem(locals, name) == v) {
5390 if (PyDict_DelItem(locals, name) != 0) {
5391 PyErr_Clear();
5392 }
5393 }
5394 break;
5395 }
5396 }
5397 }
5398 res = v;
5399 PyUnicode_Append(&res, w);
5400 return res;
5401}
5402
Guido van Rossum950361c1997-01-24 13:49:28 +00005403#ifdef DYNAMIC_EXECUTION_PROFILE
5404
Skip Montanarof118cb12001-10-15 20:51:38 +00005405static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005406getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005407{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005408 int i;
5409 PyObject *l = PyList_New(256);
5410 if (l == NULL) return NULL;
5411 for (i = 0; i < 256; i++) {
5412 PyObject *x = PyLong_FromLong(a[i]);
5413 if (x == NULL) {
5414 Py_DECREF(l);
5415 return NULL;
5416 }
5417 PyList_SetItem(l, i, x);
5418 }
5419 for (i = 0; i < 256; i++)
5420 a[i] = 0;
5421 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005422}
5423
5424PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005425_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005426{
5427#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005428 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005429#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005430 int i;
5431 PyObject *l = PyList_New(257);
5432 if (l == NULL) return NULL;
5433 for (i = 0; i < 257; i++) {
5434 PyObject *x = getarray(dxpairs[i]);
5435 if (x == NULL) {
5436 Py_DECREF(l);
5437 return NULL;
5438 }
5439 PyList_SetItem(l, i, x);
5440 }
5441 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005442#endif
5443}
5444
5445#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005446
5447Py_ssize_t
5448_PyEval_RequestCodeExtraIndex(freefunc free)
5449{
5450 PyThreadState *tstate = PyThreadState_Get();
5451 Py_ssize_t new_index;
5452
5453 if (tstate->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
5454 return -1;
5455 }
5456 new_index = tstate->co_extra_user_count++;
5457 tstate->co_extra_freefuncs[new_index] = free;
5458 return new_index;
5459}
Łukasz Langaa785c872016-09-09 17:37:37 -07005460
5461static void
5462dtrace_function_entry(PyFrameObject *f)
5463{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005464 const char *filename;
5465 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005466 int lineno;
5467
5468 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5469 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5470 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5471
5472 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5473}
5474
5475static void
5476dtrace_function_return(PyFrameObject *f)
5477{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005478 const char *filename;
5479 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005480 int lineno;
5481
5482 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5483 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5484 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5485
5486 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5487}
5488
5489/* DTrace equivalent of maybe_call_line_trace. */
5490static void
5491maybe_dtrace_line(PyFrameObject *frame,
5492 int *instr_lb, int *instr_ub, int *instr_prev)
5493{
5494 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005495 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005496
5497 /* If the last instruction executed isn't in the current
5498 instruction window, reset the window.
5499 */
5500 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5501 PyAddrPair bounds;
5502 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5503 &bounds);
5504 *instr_lb = bounds.ap_lower;
5505 *instr_ub = bounds.ap_upper;
5506 }
5507 /* If the last instruction falls at the start of a line or if
5508 it represents a jump backwards, update the frame's line
5509 number and call the trace function. */
5510 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5511 frame->f_lineno = line;
5512 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5513 if (!co_filename)
5514 co_filename = "?";
5515 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5516 if (!co_name)
5517 co_name = "?";
5518 PyDTrace_LINE(co_filename, co_name, line);
5519 }
5520 *instr_prev = frame->f_lasti;
5521}