blob: 770dfcba89e513dc458f1e8d101730dfa75ddc88 [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
Jeremy Hylton52820442001-01-03 23:52:36 +000033typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000034
Guido van Rossum374a9221991-04-04 10:40:29 +000035/* Forward declarations */
Victor Stinnerf9b760f2016-09-09 10:17:08 -070036static PyObject * call_function(PyObject ***, Py_ssize_t, PyObject *);
Victor Stinnerd8735722016-09-09 12:36:44 -070037static PyObject * fast_function(PyObject *, PyObject **, Py_ssize_t, PyObject *);
Victor Stinnerf9b760f2016-09-09 10:17:08 -070038static PyObject * do_call_core(PyObject *, PyObject *, PyObject *);
Jeremy Hylton52820442001-01-03 23:52:36 +000039
Guido van Rossum0a066c01992-03-27 17:29:15 +000040#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000041static int lltrace;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020042static int prtrace(PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000043#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010044static int call_trace(Py_tracefunc, PyObject *,
45 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000047static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010048 PyThreadState *, PyFrameObject *,
49 int, PyObject *);
50static void call_exc_trace(Py_tracefunc, PyObject *,
51 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000052static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010053 PyThreadState *, PyFrameObject *, int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070054static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
55static void dtrace_function_entry(PyFrameObject *);
56static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000057
Thomas Wouters477c8d52006-05-27 19:21:47 +000058static PyObject * cmp_outcome(int, PyObject *, PyObject *);
Serhiy Storchaka133138a2016-08-02 22:51:21 +030059static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *, PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +000060static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +000061static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +000062static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +000063static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +020064static PyObject * unicode_concatenate(PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030065 PyFrameObject *, const _Py_CODEUNIT *);
Benjamin Petersonce798522012-01-22 11:24:29 -050066static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Guido van Rossum374a9221991-04-04 10:40:29 +000067
Paul Prescode68140d2000-08-30 20:25:01 +000068#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000070#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000072#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 "free variable '%.200s' referenced before assignment" \
74 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000075
Guido van Rossum950361c1997-01-24 13:49:28 +000076/* Dynamic execution profile */
77#ifdef DYNAMIC_EXECUTION_PROFILE
78#ifdef DXPAIRS
79static long dxpairs[257][256];
80#define dxp dxpairs[256]
81#else
82static long dxp[256];
83#endif
84#endif
85
Jeremy Hylton985eba52003-02-05 23:13:00 +000086/* Function call profile */
87#ifdef CALL_PROFILE
88#define PCALL_NUM 11
89static int pcall[PCALL_NUM];
90
91#define PCALL_ALL 0
92#define PCALL_FUNCTION 1
93#define PCALL_FAST_FUNCTION 2
94#define PCALL_FASTER_FUNCTION 3
95#define PCALL_METHOD 4
96#define PCALL_BOUND_METHOD 5
97#define PCALL_CFUNCTION 6
98#define PCALL_TYPE 7
99#define PCALL_GENERATOR 8
100#define PCALL_OTHER 9
101#define PCALL_POP 10
102
103/* Notes about the statistics
104
105 PCALL_FAST stats
106
107 FAST_FUNCTION means no argument tuple needs to be created.
108 FASTER_FUNCTION means that the fast-path frame setup code is used.
109
110 If there is a method call where the call can be optimized by changing
111 the argument tuple and calling the function directly, it gets recorded
112 twice.
113
114 As a result, the relationship among the statistics appears to be
115 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
116 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
117 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
118 PCALL_METHOD > PCALL_BOUND_METHOD
119*/
120
121#define PCALL(POS) pcall[POS]++
122
123PyObject *
124PyEval_GetCallStats(PyObject *self)
125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 return Py_BuildValue("iiiiiiiiiii",
127 pcall[0], pcall[1], pcall[2], pcall[3],
128 pcall[4], pcall[5], pcall[6], pcall[7],
129 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000130}
131#else
132#define PCALL(O)
133
134PyObject *
135PyEval_GetCallStats(PyObject *self)
136{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 Py_INCREF(Py_None);
138 return Py_None;
Jeremy Hylton985eba52003-02-05 23:13:00 +0000139}
140#endif
141
Tim Peters5ca576e2001-06-18 22:08:13 +0000142
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000143#ifdef WITH_THREAD
144#define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request)
145#else
146#define GIL_REQUEST 0
147#endif
148
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000149/* This can set eval_breaker to 0 even though gil_drop_request became
150 1. We believe this is all right because the eval loop will release
151 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000152#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 _Py_atomic_store_relaxed( \
154 &eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000155 GIL_REQUEST | \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
157 pending_async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000158
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000159#ifdef WITH_THREAD
160
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000161#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 do { \
163 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
164 _Py_atomic_store_relaxed(&eval_breaker, 1); \
165 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000166
167#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 do { \
169 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
170 COMPUTE_EVAL_BREAKER(); \
171 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000172
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000173#endif
174
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000175/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000176#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 do { \
178 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
179 _Py_atomic_store_relaxed(&eval_breaker, 1); \
180 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000181
182#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 do { \
184 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
185 COMPUTE_EVAL_BREAKER(); \
186 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000187
188#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 do { \
190 pending_async_exc = 1; \
191 _Py_atomic_store_relaxed(&eval_breaker, 1); \
192 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000193
194#define UNSIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000196
197
Antoine Pitrou3024c052017-07-01 19:12:05 +0200198/* This single variable consolidates all requests to break out of the fast path
199 in the eval loop. */
200static _Py_atomic_int eval_breaker = {0};
201/* Request for running pending calls. */
202static _Py_atomic_int pendingcalls_to_do = {0};
203/* Request for looking at the `async_exc` field of the current thread state.
204 Guarded by the GIL. */
205static int pending_async_exc = 0;
206
Guido van Rossume59214e1994-08-30 08:01:59 +0000207#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000208
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000209#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000210#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000211#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000212#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000213
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000214static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000215static long main_thread = 0;
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000216/* Request for dropping the GIL */
217static _Py_atomic_int gil_drop_request = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000218
219#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000220
Tim Peters7f468f22004-10-11 02:40:51 +0000221int
222PyEval_ThreadsInitialized(void)
223{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000225}
226
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000227void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000228PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000229{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 if (gil_created())
231 return;
232 create_gil();
233 take_gil(PyThreadState_GET());
234 main_thread = PyThread_get_thread_ident();
235 if (!pending_lock)
236 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000237}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000238
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000239void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000240_PyEval_FiniThreads(void)
241{
242 if (!gil_created())
243 return;
244 destroy_gil();
245 assert(!gil_created());
246}
247
248void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000249PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000250{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 PyThreadState *tstate = PyThreadState_GET();
252 if (tstate == NULL)
253 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
254 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000255}
256
257void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000258PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000259{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 /* This function must succeed when the current thread state is NULL.
261 We therefore avoid PyThreadState_GET() which dumps a fatal error
262 in debug mode.
263 */
264 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
265 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000266}
267
268void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000269PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000270{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 if (tstate == NULL)
272 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
273 /* Check someone has called PyEval_InitThreads() to create the lock */
274 assert(gil_created());
275 take_gil(tstate);
276 if (PyThreadState_Swap(tstate) != NULL)
277 Py_FatalError(
278 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000279}
280
281void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000282PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000283{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 if (tstate == NULL)
285 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
286 if (PyThreadState_Swap(NULL) != tstate)
287 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
288 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000289}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000290
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200291/* This function is called from PyOS_AfterFork to destroy all threads which are
292 * not running in the child process, and clear internal locks which might be
293 * held by those threads. (This could also be done using pthread_atfork
294 * mechanism, at least for the pthreads implementation.) */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000295
296void
297PyEval_ReInitThreads(void)
298{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200299 _Py_IDENTIFIER(_after_fork);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 PyObject *threading, *result;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200301 PyThreadState *current_tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 if (!gil_created())
304 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 recreate_gil();
306 pending_lock = PyThread_allocate_lock();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200307 take_gil(current_tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 /* Update the threading module with the new state.
311 */
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200312 threading = PyMapping_GetItemString(current_tstate->interp->modules,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 "threading");
314 if (threading == NULL) {
315 /* threading not imported */
316 PyErr_Clear();
317 return;
318 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200319 result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 if (result == NULL)
321 PyErr_WriteUnraisable(threading);
322 else
323 Py_DECREF(result);
324 Py_DECREF(threading);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200325
326 /* Destroy all threads except the current one */
327 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000328}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000329
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000330#endif /* WITH_THREAD */
331
332/* This function is used to signal that async exceptions are waiting to be
333 raised, therefore it is also useful in non-threaded builds. */
334
335void
336_PyEval_SignalAsyncExc(void)
337{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000339}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000340
Guido van Rossumff4949e1992-08-05 19:58:53 +0000341/* Functions save_thread and restore_thread are always defined so
342 dynamically loaded modules needn't be compiled separately for use
343 with and without threads: */
344
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000345PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000346PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000347{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 PyThreadState *tstate = PyThreadState_Swap(NULL);
349 if (tstate == NULL)
350 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000351#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 if (gil_created())
353 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000354#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000356}
357
358void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000359PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 if (tstate == NULL)
362 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000363#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 if (gil_created()) {
365 int err = errno;
366 take_gil(tstate);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200367 /* _Py_Finalizing is protected by the GIL */
368 if (_Py_Finalizing && tstate != _Py_Finalizing) {
369 drop_gil(tstate);
370 PyThread_exit_thread();
371 assert(0); /* unreachable */
372 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 errno = err;
374 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000375#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000377}
378
379
Guido van Rossuma9672091994-09-14 13:31:22 +0000380/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
381 signal handlers or Mac I/O completion routines) can schedule calls
382 to a function to be called synchronously.
383 The synchronous function is called with one void* argument.
384 It should return 0 for success or -1 for failure -- failure should
385 be accompanied by an exception.
386
387 If registry succeeds, the registry function returns 0; if it fails
388 (e.g. due to too many pending calls) it returns -1 (without setting
389 an exception condition).
390
391 Note that because registry may occur from within signal handlers,
392 or other asynchronous events, calling malloc() is unsafe!
393
394#ifdef WITH_THREAD
395 Any thread can schedule pending calls, but only the main thread
396 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000397 There is no facility to schedule calls to a particular thread, but
398 that should be easy to change, should that ever be required. In
399 that case, the static variables here should go into the python
400 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000401#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000402*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000403
Antoine Pitrou3024c052017-07-01 19:12:05 +0200404void
405_PyEval_SignalReceived(void)
406{
407 /* bpo-30703: Function called when the C signal handler of Python gets a
408 signal. We cannot queue a callback using Py_AddPendingCall() since
409 that function is not async-signal-safe. */
410 SIGNAL_PENDING_CALLS();
411}
412
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000413#ifdef WITH_THREAD
414
415/* The WITH_THREAD implementation is thread-safe. It allows
416 scheduling to be made from any thread, and even from an executing
417 callback.
418 */
419
420#define NPENDINGCALLS 32
421static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 int (*func)(void *);
423 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000424} pendingcalls[NPENDINGCALLS];
425static int pendingfirst = 0;
426static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000427
428int
429Py_AddPendingCall(int (*func)(void *), void *arg)
430{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 int i, j, result=0;
432 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 /* try a few times for the lock. Since this mechanism is used
435 * for signal handling (on the main thread), there is a (slim)
436 * chance that a signal is delivered on the same thread while we
437 * hold the lock during the Py_MakePendingCalls() function.
438 * This avoids a deadlock in that case.
439 * Note that signals can be delivered on any thread. In particular,
440 * on Windows, a SIGINT is delivered on a system-created worker
441 * thread.
442 * We also check for lock being NULL, in the unlikely case that
443 * this function is called before any bytecode evaluation takes place.
444 */
445 if (lock != NULL) {
446 for (i = 0; i<100; i++) {
447 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
448 break;
449 }
450 if (i == 100)
451 return -1;
452 }
453
454 i = pendinglast;
455 j = (i + 1) % NPENDINGCALLS;
456 if (j == pendingfirst) {
457 result = -1; /* Queue full */
458 } else {
459 pendingcalls[i].func = func;
460 pendingcalls[i].arg = arg;
461 pendinglast = j;
462 }
463 /* signal main loop */
464 SIGNAL_PENDING_CALLS();
465 if (lock != NULL)
466 PyThread_release_lock(lock);
467 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000468}
469
470int
471Py_MakePendingCalls(void)
472{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200473 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 int i;
475 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000476
Antoine Pitrou3024c052017-07-01 19:12:05 +0200477 assert(PyGILState_Check());
478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 if (!pending_lock) {
480 /* initial allocation of the lock */
481 pending_lock = PyThread_allocate_lock();
482 if (pending_lock == NULL)
483 return -1;
484 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 /* only service pending calls on main thread */
487 if (main_thread && PyThread_get_thread_ident() != main_thread)
488 return 0;
489 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200490 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200492 busy = 1;
Antoine Pitrou3024c052017-07-01 19:12:05 +0200493 /* unsignal before starting to call callbacks, so that any callback
494 added in-between re-signals */
495 UNSIGNAL_PENDING_CALLS();
496
497 /* Python signal handler doesn't really queue a callback: it only signals
498 that a signal was received, see _PyEval_SignalReceived(). */
499 if (PyErr_CheckSignals() < 0) {
500 goto error;
501 }
502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 /* perform a bounded number of calls, in case of recursion */
504 for (i=0; i<NPENDINGCALLS; i++) {
505 int j;
506 int (*func)(void *);
507 void *arg = NULL;
508
509 /* pop one item off the queue while holding the lock */
510 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
511 j = pendingfirst;
512 if (j == pendinglast) {
513 func = NULL; /* Queue empty */
514 } else {
515 func = pendingcalls[j].func;
516 arg = pendingcalls[j].arg;
517 pendingfirst = (j + 1) % NPENDINGCALLS;
518 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 PyThread_release_lock(pending_lock);
520 /* having released the lock, perform the callback */
521 if (func == NULL)
522 break;
523 r = func(arg);
Antoine Pitrou3024c052017-07-01 19:12:05 +0200524 if (r) {
525 goto error;
526 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 }
Antoine Pitrou3024c052017-07-01 19:12:05 +0200528
Charles-François Natalif23339a2011-07-23 18:15:43 +0200529 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 return r;
Antoine Pitrou3024c052017-07-01 19:12:05 +0200531
532error:
533 busy = 0;
534 SIGNAL_PENDING_CALLS(); /* We're not done yet */
535 return -1;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000536}
537
538#else /* if ! defined WITH_THREAD */
539
540/*
541 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
542 This code is used for signal handling in python that isn't built
543 with WITH_THREAD.
544 Don't use this implementation when Py_AddPendingCalls() can happen
545 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546
Guido van Rossuma9672091994-09-14 13:31:22 +0000547 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000548 (1) nested asynchronous calls to Py_AddPendingCall()
549 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000551 (1) is very unlikely because typically signal delivery
552 is blocked during signal handling. So it should be impossible.
553 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000554 The current code is safe against (2), but not against (1).
555 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000556 thread is present, interrupted by signals, and that the critical
557 section is protected with the "busy" variable. On Windows, which
558 delivers SIGINT on a system thread, this does not hold and therefore
559 Windows really shouldn't use this version.
560 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000561*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000562
Guido van Rossuma9672091994-09-14 13:31:22 +0000563#define NPENDINGCALLS 32
564static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 int (*func)(void *);
566 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000567} pendingcalls[NPENDINGCALLS];
568static volatile int pendingfirst = 0;
569static volatile int pendinglast = 0;
570
571int
Thomas Wouters334fb892000-07-25 12:56:38 +0000572Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000573{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 static volatile int busy = 0;
575 int i, j;
576 /* XXX Begin critical section */
577 if (busy)
578 return -1;
579 busy = 1;
580 i = pendinglast;
581 j = (i + 1) % NPENDINGCALLS;
582 if (j == pendingfirst) {
583 busy = 0;
584 return -1; /* Queue full */
585 }
586 pendingcalls[i].func = func;
587 pendingcalls[i].arg = arg;
588 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 SIGNAL_PENDING_CALLS();
591 busy = 0;
592 /* XXX End critical section */
593 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000594}
595
Guido van Rossum180d7b41994-09-29 09:45:57 +0000596int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000597Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000598{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 static int busy = 0;
600 if (busy)
601 return 0;
602 busy = 1;
Antoine Pitrou3024c052017-07-01 19:12:05 +0200603
604 /* unsignal before starting to call callbacks, so that any callback
605 added in-between re-signals */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 UNSIGNAL_PENDING_CALLS();
Antoine Pitrou3024c052017-07-01 19:12:05 +0200607 /* Python signal handler doesn't really queue a callback: it only signals
608 that a signal was received, see _PyEval_SignalReceived(). */
609 if (PyErr_CheckSignals() < 0) {
610 goto error;
611 }
612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 for (;;) {
614 int i;
615 int (*func)(void *);
616 void *arg;
617 i = pendingfirst;
618 if (i == pendinglast)
619 break; /* Queue empty */
620 func = pendingcalls[i].func;
621 arg = pendingcalls[i].arg;
622 pendingfirst = (i + 1) % NPENDINGCALLS;
623 if (func(arg) < 0) {
Masayuki Yamamotoe3a0ff02017-07-05 18:24:46 +0900624 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 }
626 }
627 busy = 0;
628 return 0;
Antoine Pitrou3024c052017-07-01 19:12:05 +0200629
630error:
631 busy = 0;
632 SIGNAL_PENDING_CALLS(); /* We're not done yet */
633 return -1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000634}
635
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000636#endif /* WITH_THREAD */
637
Guido van Rossuma9672091994-09-14 13:31:22 +0000638
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000639/* The interpreter's recursion limit */
640
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000641#ifndef Py_DEFAULT_RECURSION_LIMIT
642#define Py_DEFAULT_RECURSION_LIMIT 1000
643#endif
644static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
645int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000646
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000647int
648Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000649{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000651}
652
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000653void
654Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000655{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 recursion_limit = new_limit;
657 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000658}
659
Armin Rigo2b3eb402003-10-28 12:05:48 +0000660/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
661 if the recursion_depth reaches _Py_CheckRecursionLimit.
662 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
663 to guarantee that _Py_CheckRecursiveCall() is regularly called.
664 Without USE_STACKCHECK, there is no need for this. */
665int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300666_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000667{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000669
670#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 if (PyOS_CheckStack()) {
672 --tstate->recursion_depth;
673 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
674 return -1;
675 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000676#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 _Py_CheckRecursionLimit = recursion_limit;
678 if (tstate->recursion_critical)
679 /* Somebody asked that we don't check for recursion. */
680 return 0;
681 if (tstate->overflowed) {
682 if (tstate->recursion_depth > recursion_limit + 50) {
683 /* Overflowing while handling an overflow. Give up. */
684 Py_FatalError("Cannot recover from stack overflow.");
685 }
686 return 0;
687 }
688 if (tstate->recursion_depth > recursion_limit) {
689 --tstate->recursion_depth;
690 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400691 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 "maximum recursion depth exceeded%s",
693 where);
694 return -1;
695 }
696 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000697}
698
Guido van Rossum374a9221991-04-04 10:40:29 +0000699/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000700enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000701 WHY_NOT = 0x0001, /* No error */
702 WHY_EXCEPTION = 0x0002, /* Exception occurred */
Stefan Krahb7e10102010-06-23 18:42:39 +0000703 WHY_RETURN = 0x0008, /* 'return' statement */
704 WHY_BREAK = 0x0010, /* 'break' statement */
705 WHY_CONTINUE = 0x0020, /* 'continue' statement */
706 WHY_YIELD = 0x0040, /* 'yield' operator */
707 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000708};
Guido van Rossum374a9221991-04-04 10:40:29 +0000709
Benjamin Peterson87880242011-07-03 16:48:31 -0500710static void save_exc_state(PyThreadState *, PyFrameObject *);
711static void swap_exc_state(PyThreadState *, PyFrameObject *);
712static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400713static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000714static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000715
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000716/* Records whether tracing is on for any thread. Counts the number of
717 threads for which tstate->c_tracefunc is non-NULL, so if the value
718 is 0, we know we don't have to check this thread's c_tracefunc.
719 This speeds up the if statement in PyEval_EvalFrameEx() after
720 fast_next_opcode*/
721static int _Py_TracingPossible = 0;
722
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000723
Guido van Rossum374a9221991-04-04 10:40:29 +0000724
Guido van Rossumb209a111997-04-29 18:18:01 +0000725PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000726PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000727{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 return PyEval_EvalCodeEx(co,
729 globals, locals,
730 (PyObject **)NULL, 0,
731 (PyObject **)NULL, 0,
732 (PyObject **)NULL, 0,
733 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000734}
735
736
737/* Interpreter main loop */
738
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000739PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000740PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 /* This is for backward compatibility with extension modules that
742 used this API; core interpreter code should call
743 PyEval_EvalFrameEx() */
744 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000745}
746
747PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000748PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000749{
Brett Cannon3cebf932016-09-05 15:33:46 -0700750 PyThreadState *tstate = PyThreadState_GET();
751 return tstate->interp->eval_frame(f, throwflag);
752}
753
754PyObject *
755_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
756{
Guido van Rossum950361c1997-01-24 13:49:28 +0000757#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000759#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200760 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300761 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200762 int opcode; /* Current opcode */
763 int oparg; /* Current opcode argument, if any */
764 enum why_code why; /* Reason for block stack unwind */
765 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 PyObject *retval = NULL; /* Return value */
767 PyThreadState *tstate = PyThreadState_GET();
768 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 is true when the line being executed has changed. The
775 initial values are such as to make this false the first
776 time it is tested. */
777 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000778
Serhiy Storchakaab874002016-09-11 13:48:15 +0300779 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 PyObject *names;
781 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000782
Brett Cannon368b4b72012-04-02 12:17:59 -0400783#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200784 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400785#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200786
Antoine Pitroub52ec782009-01-25 16:34:23 +0000787/* Computed GOTOs, or
788 the-optimization-commonly-but-improperly-known-as-"threaded code"
789 using gcc's labels-as-values extension
790 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
791
792 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000794 combined with a lookup table of jump addresses. However, since the
795 indirect jump instruction is shared by all opcodes, the CPU will have a
796 hard time making the right prediction for where to jump next (actually,
797 it will be always wrong except in the uncommon case of a sequence of
798 several identical opcodes).
799
800 "Threaded code" in contrast, uses an explicit jump table and an explicit
801 indirect jump instruction at the end of each opcode. Since the jump
802 instruction is at a different address for each opcode, the CPU will make a
803 separate prediction for each of these instructions, which is equivalent to
804 predicting the second opcode of each opcode pair. These predictions have
805 a much better chance to turn out valid, especially in small bytecode loops.
806
807 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000809 and potentially many more instructions (depending on the pipeline width).
810 A correctly predicted branch, however, is nearly free.
811
812 At the time of this writing, the "threaded code" version is up to 15-20%
813 faster than the normal "switch" version, depending on the compiler and the
814 CPU architecture.
815
816 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
817 because it would render the measurements invalid.
818
819
820 NOTE: care must be taken that the compiler doesn't try to "optimize" the
821 indirect jumps by sharing them between all opcodes. Such optimizations
822 can be disabled on gcc by using the -fno-gcse flag (or possibly
823 -fno-crossjumping).
824*/
825
Antoine Pitrou042b1282010-08-13 21:15:58 +0000826#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000827#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000828#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000829#endif
830
Antoine Pitrou042b1282010-08-13 21:15:58 +0000831#ifdef HAVE_COMPUTED_GOTOS
832 #ifndef USE_COMPUTED_GOTOS
833 #define USE_COMPUTED_GOTOS 1
834 #endif
835#else
836 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
837 #error "Computed gotos are not supported on this compiler."
838 #endif
839 #undef USE_COMPUTED_GOTOS
840 #define USE_COMPUTED_GOTOS 0
841#endif
842
843#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000844/* Import the static jump table */
845#include "opcode_targets.h"
846
Antoine Pitroub52ec782009-01-25 16:34:23 +0000847#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 TARGET_##op: \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000850
Antoine Pitroub52ec782009-01-25 16:34:23 +0000851#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 { \
853 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
854 FAST_DISPATCH(); \
855 } \
856 continue; \
857 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000858
859#ifdef LLTRACE
860#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700862 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300864 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300865 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 } \
867 goto fast_next_opcode; \
868 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000869#else
870#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700872 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300874 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300875 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 } \
877 goto fast_next_opcode; \
878 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000879#endif
880
881#else
882#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 case op:
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300884
Antoine Pitroub52ec782009-01-25 16:34:23 +0000885#define DISPATCH() continue
886#define FAST_DISPATCH() goto fast_next_opcode
887#endif
888
889
Neal Norwitza81d2202002-07-14 00:27:26 +0000890/* Tuple access macros */
891
892#ifndef Py_DEBUG
893#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
894#else
895#define GETITEM(v, i) PyTuple_GetItem((v), (i))
896#endif
897
Guido van Rossum374a9221991-04-04 10:40:29 +0000898/* Code access macros */
899
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300900/* The integer overflow is checked by an assertion below. */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300901#define INSTR_OFFSET() (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300902#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300903 _Py_CODEUNIT word = *next_instr; \
904 opcode = _Py_OPCODE(word); \
905 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300906 next_instr++; \
907 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300908#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
909#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000910
Raymond Hettingerf606f872003-03-16 03:11:04 +0000911/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 Some opcodes tend to come in pairs thus making it possible to
913 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300914 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 Verifying the prediction costs a single high-speed test of a register
917 variable against a constant. If the pairing was good, then the
918 processor's own internal branch predication has a high likelihood of
919 success, resulting in a nearly zero-overhead transition to the
920 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300921 including its unpredictable switch-case branch. Combined with the
922 processor's internal branch prediction, a successful PREDICT has the
923 effect of making the two opcodes run as if they were a single new opcode
924 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000925
Georg Brandl86b2fb92008-07-16 03:43:04 +0000926 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 predictions turned-on and interpret the results as if some opcodes
928 had been combined or turn-off predictions so that the opcode frequency
929 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000930
931 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 the CPU to record separate branch prediction information for each
933 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000934
Raymond Hettingerf606f872003-03-16 03:11:04 +0000935*/
936
Antoine Pitrou042b1282010-08-13 21:15:58 +0000937#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000939#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300940#define PREDICT(op) \
941 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300942 _Py_CODEUNIT word = *next_instr; \
943 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300944 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300945 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300946 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300947 goto PRED_##op; \
948 } \
949 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000950#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300951#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000952
Raymond Hettingerf606f872003-03-16 03:11:04 +0000953
Guido van Rossum374a9221991-04-04 10:40:29 +0000954/* Stack manipulation macros */
955
Martin v. Löwis18e16552006-02-15 17:27:45 +0000956/* The stack can grow at most MAXINT deep, as co_nlocals and
957 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000958#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
959#define EMPTY() (STACK_LEVEL() == 0)
960#define TOP() (stack_pointer[-1])
961#define SECOND() (stack_pointer[-2])
962#define THIRD() (stack_pointer[-3])
963#define FOURTH() (stack_pointer[-4])
964#define PEEK(n) (stack_pointer[-(n)])
965#define SET_TOP(v) (stack_pointer[-1] = (v))
966#define SET_SECOND(v) (stack_pointer[-2] = (v))
967#define SET_THIRD(v) (stack_pointer[-3] = (v))
968#define SET_FOURTH(v) (stack_pointer[-4] = (v))
969#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
970#define BASIC_STACKADJ(n) (stack_pointer += n)
971#define BASIC_PUSH(v) (*stack_pointer++ = (v))
972#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000973
Guido van Rossum96a42c81992-01-12 02:29:51 +0000974#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000976 lltrace && prtrace(TOP(), "push")); \
977 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000979 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000981 lltrace && prtrace(TOP(), "stackadj")); \
982 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +0000983#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000984 prtrace((STACK_POINTER)[-1], "ext_pop")), \
985 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000986#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000987#define PUSH(v) BASIC_PUSH(v)
988#define POP() BASIC_POP()
989#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000990#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000991#endif
992
Guido van Rossum681d79a1995-07-18 14:51:37 +0000993/* Local variable macros */
994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000996
997/* The SETLOCAL() macro must not DECREF the local variable in-place and
998 then store the new value; it must copy the old value to a temporary
999 value, then store the new value, and then DECREF the temporary value.
1000 This is because it is possible that during the DECREF the frame is
1001 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1002 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001004 GETLOCAL(i) = value; \
1005 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001006
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001007
1008#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 while (STACK_LEVEL() > (b)->b_level) { \
1010 PyObject *v = POP(); \
1011 Py_XDECREF(v); \
1012 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001013
1014#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001015 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 PyObject *type, *value, *traceback; \
1017 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1018 while (STACK_LEVEL() > (b)->b_level + 3) { \
1019 value = POP(); \
1020 Py_XDECREF(value); \
1021 } \
1022 type = tstate->exc_type; \
1023 value = tstate->exc_value; \
1024 traceback = tstate->exc_traceback; \
1025 tstate->exc_type = POP(); \
1026 tstate->exc_value = POP(); \
1027 tstate->exc_traceback = POP(); \
1028 Py_XDECREF(type); \
1029 Py_XDECREF(value); \
1030 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001031 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001032
Guido van Rossuma027efa1997-05-05 20:56:21 +00001033/* Start of code */
1034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 /* push frame */
1036 if (Py_EnterRecursiveCall(""))
1037 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001038
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 if (tstate->use_tracing) {
1042 if (tstate->c_tracefunc != NULL) {
1043 /* tstate->c_tracefunc, if defined, is a
1044 function that will be called on *every* entry
1045 to a code block. Its return value, if not
1046 None, is a function that will be called at
1047 the start of each executed line of code.
1048 (Actually, the function must return itself
1049 in order to continue tracing.) The trace
1050 functions are called with three arguments:
1051 a pointer to the current frame, a string
1052 indicating why the function is called, and
1053 an argument which depends on the situation.
1054 The global trace function is also called
1055 whenever an exception is detected. */
1056 if (call_trace_protected(tstate->c_tracefunc,
1057 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001058 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 /* Trace function raised an error */
1060 goto exit_eval_frame;
1061 }
1062 }
1063 if (tstate->c_profilefunc != NULL) {
1064 /* Similar for c_profilefunc, except it needn't
1065 return itself and isn't called for "line" events */
1066 if (call_trace_protected(tstate->c_profilefunc,
1067 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001068 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 /* Profile function raised an error */
1070 goto exit_eval_frame;
1071 }
1072 }
1073 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001074
Łukasz Langaa785c872016-09-09 17:37:37 -07001075 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1076 dtrace_function_entry(f);
1077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 co = f->f_code;
1079 names = co->co_names;
1080 consts = co->co_consts;
1081 fastlocals = f->f_localsplus;
1082 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001083 assert(PyBytes_Check(co->co_code));
1084 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001085 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1086 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1087 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001088 /*
1089 f->f_lasti refers to the index of the last instruction,
1090 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001091
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001092 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001093 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 When the PREDICT() macros are enabled, some opcode pairs follow in
1096 direct succession without updating f->f_lasti. A successful
1097 prediction effectively links the two codes together as if they
1098 were a single new opcode; accordingly,f->f_lasti will point to
1099 the first code in the pair (for instance, GET_ITER followed by
1100 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001101 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001103 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001104 next_instr = first_instr;
1105 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001106 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1107 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001108 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 stack_pointer = f->f_stacktop;
1110 assert(stack_pointer != NULL);
1111 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001112 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001113
Yury Selivanoveb636452016-09-08 22:01:51 -07001114 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01001115 if (!throwflag && f->f_exc_type != NULL && f->f_exc_type != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 /* We were in an except handler when we left,
1117 restore the exception state which was put aside
1118 (see YIELD_VALUE). */
Benjamin Peterson87880242011-07-03 16:48:31 -05001119 swap_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 }
Benjamin Peterson87880242011-07-03 16:48:31 -05001121 else
1122 save_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001124
Tim Peters5ca576e2001-06-18 22:08:13 +00001125#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001126 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001127#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 why = WHY_NOT;
Guido van Rossumac7be682001-01-17 15:42:30 +00001130
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001131 if (throwflag) /* support for generator.throw() */
1132 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001133
Victor Stinnerace47d72013-07-18 01:41:08 +02001134#ifdef Py_DEBUG
1135 /* PyEval_EvalFrameEx() must not be called with an exception set,
1136 because it may clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001137 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +02001138 assert(!PyErr_Occurred());
1139#endif
1140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1143 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +02001144 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 /* Do periodic things. Doing this every time through
1147 the loop would add too much overhead, so we do it
1148 only every Nth instruction. We also do it if
1149 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1150 event needs attention (e.g. a signal handler or
1151 async I/O handler); see Py_AddPendingCall() and
1152 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 if (_Py_atomic_load_relaxed(&eval_breaker)) {
Yury Selivanove89f95b2017-06-09 17:06:39 -04001155 if (_Py_OPCODE(*next_instr) == SETUP_FINALLY ||
1156 _Py_OPCODE(*next_instr) == YIELD_FROM) {
1157 /* Two cases where we skip running signal handlers and other
1158 pending calls:
1159 - If we're about to enter the try: of a try/finally (not
1160 *very* useful, but might help in some cases and it's
1161 traditional)
1162 - If we're resuming a chain of nested 'yield from' or
1163 'await' calls, then each frame is parked with YIELD_FROM
1164 as its next opcode. If the user hit control-C we want to
1165 wait until we've reached the innermost frame before
1166 running the signal handler and raising KeyboardInterrupt
1167 (see bpo-30039).
1168 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 goto fast_next_opcode;
1170 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001172 if (Py_MakePendingCalls() < 0)
1173 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001175#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001176 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 /* Give another thread a chance */
1178 if (PyThreadState_Swap(NULL) != tstate)
1179 Py_FatalError("ceval: tstate mix-up");
1180 drop_gil(tstate);
1181
1182 /* Other threads may run now */
1183
1184 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001185
1186 /* Check if we should make a quick exit. */
1187 if (_Py_Finalizing && _Py_Finalizing != tstate) {
1188 drop_gil(tstate);
1189 PyThread_exit_thread();
1190 }
1191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 if (PyThreadState_Swap(tstate) != NULL)
1193 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001195#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 /* Check for asynchronous exceptions. */
1197 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001198 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 tstate->async_exc = NULL;
1200 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001201 PyErr_SetNone(exc);
1202 Py_DECREF(exc);
1203 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 }
1205 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 fast_next_opcode:
1208 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001209
Łukasz Langaa785c872016-09-09 17:37:37 -07001210 if (PyDTrace_LINE_ENABLED())
1211 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001216 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001217 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 /* see maybe_call_line_trace
1219 for expository comments */
1220 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 err = maybe_call_line_trace(tstate->c_tracefunc,
1223 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001224 tstate, f,
1225 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 /* Reload possibly changed frame fields */
1227 JUMPTO(f->f_lasti);
1228 if (f->f_stacktop != NULL) {
1229 stack_pointer = f->f_stacktop;
1230 f->f_stacktop = NULL;
1231 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001232 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001234 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001238
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001239 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001240 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001241#ifdef DYNAMIC_EXECUTION_PROFILE
1242#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 dxpairs[lastopcode][opcode]++;
1244 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001245#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001247#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001248
Guido van Rossum96a42c81992-01-12 02:29:51 +00001249#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 if (lltrace) {
1253 if (HAS_ARG(opcode)) {
1254 printf("%d: %d, %d\n",
1255 f->f_lasti, opcode, oparg);
1256 }
1257 else {
1258 printf("%d: %d\n",
1259 f->f_lasti, opcode);
1260 }
1261 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001262#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 /* BEWARE!
1267 It is essential that any operation that fails sets either
1268 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1269 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 TARGET(NOP)
1272 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001273
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001274 TARGET(LOAD_FAST) {
1275 PyObject *value = GETLOCAL(oparg);
1276 if (value == NULL) {
1277 format_exc_check_arg(PyExc_UnboundLocalError,
1278 UNBOUNDLOCAL_ERROR_MSG,
1279 PyTuple_GetItem(co->co_varnames, oparg));
1280 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001282 Py_INCREF(value);
1283 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001285 }
1286
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001287 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001288 TARGET(LOAD_CONST) {
1289 PyObject *value = GETITEM(consts, oparg);
1290 Py_INCREF(value);
1291 PUSH(value);
1292 FAST_DISPATCH();
1293 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001294
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001295 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001296 TARGET(STORE_FAST) {
1297 PyObject *value = POP();
1298 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001300 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001301
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001302 TARGET(POP_TOP) {
1303 PyObject *value = POP();
1304 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001306 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001307
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001308 TARGET(ROT_TWO) {
1309 PyObject *top = TOP();
1310 PyObject *second = SECOND();
1311 SET_TOP(second);
1312 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001314 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001315
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001316 TARGET(ROT_THREE) {
1317 PyObject *top = TOP();
1318 PyObject *second = SECOND();
1319 PyObject *third = THIRD();
1320 SET_TOP(second);
1321 SET_SECOND(third);
1322 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001324 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001325
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001326 TARGET(DUP_TOP) {
1327 PyObject *top = TOP();
1328 Py_INCREF(top);
1329 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001331 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001332
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001333 TARGET(DUP_TOP_TWO) {
1334 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001335 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001336 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001337 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001338 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001339 SET_TOP(top);
1340 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001341 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001342 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001343
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001344 TARGET(UNARY_POSITIVE) {
1345 PyObject *value = TOP();
1346 PyObject *res = PyNumber_Positive(value);
1347 Py_DECREF(value);
1348 SET_TOP(res);
1349 if (res == NULL)
1350 goto error;
1351 DISPATCH();
1352 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001353
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001354 TARGET(UNARY_NEGATIVE) {
1355 PyObject *value = TOP();
1356 PyObject *res = PyNumber_Negative(value);
1357 Py_DECREF(value);
1358 SET_TOP(res);
1359 if (res == NULL)
1360 goto error;
1361 DISPATCH();
1362 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001363
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001364 TARGET(UNARY_NOT) {
1365 PyObject *value = TOP();
1366 int err = PyObject_IsTrue(value);
1367 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 if (err == 0) {
1369 Py_INCREF(Py_True);
1370 SET_TOP(Py_True);
1371 DISPATCH();
1372 }
1373 else if (err > 0) {
1374 Py_INCREF(Py_False);
1375 SET_TOP(Py_False);
1376 err = 0;
1377 DISPATCH();
1378 }
1379 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001380 goto error;
1381 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001382
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001383 TARGET(UNARY_INVERT) {
1384 PyObject *value = TOP();
1385 PyObject *res = PyNumber_Invert(value);
1386 Py_DECREF(value);
1387 SET_TOP(res);
1388 if (res == NULL)
1389 goto error;
1390 DISPATCH();
1391 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001392
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001393 TARGET(BINARY_POWER) {
1394 PyObject *exp = POP();
1395 PyObject *base = TOP();
1396 PyObject *res = PyNumber_Power(base, exp, Py_None);
1397 Py_DECREF(base);
1398 Py_DECREF(exp);
1399 SET_TOP(res);
1400 if (res == NULL)
1401 goto error;
1402 DISPATCH();
1403 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001404
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001405 TARGET(BINARY_MULTIPLY) {
1406 PyObject *right = POP();
1407 PyObject *left = TOP();
1408 PyObject *res = PyNumber_Multiply(left, right);
1409 Py_DECREF(left);
1410 Py_DECREF(right);
1411 SET_TOP(res);
1412 if (res == NULL)
1413 goto error;
1414 DISPATCH();
1415 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001416
Benjamin Petersond51374e2014-04-09 23:55:56 -04001417 TARGET(BINARY_MATRIX_MULTIPLY) {
1418 PyObject *right = POP();
1419 PyObject *left = TOP();
1420 PyObject *res = PyNumber_MatrixMultiply(left, right);
1421 Py_DECREF(left);
1422 Py_DECREF(right);
1423 SET_TOP(res);
1424 if (res == NULL)
1425 goto error;
1426 DISPATCH();
1427 }
1428
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001429 TARGET(BINARY_TRUE_DIVIDE) {
1430 PyObject *divisor = POP();
1431 PyObject *dividend = TOP();
1432 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1433 Py_DECREF(dividend);
1434 Py_DECREF(divisor);
1435 SET_TOP(quotient);
1436 if (quotient == NULL)
1437 goto error;
1438 DISPATCH();
1439 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001440
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001441 TARGET(BINARY_FLOOR_DIVIDE) {
1442 PyObject *divisor = POP();
1443 PyObject *dividend = TOP();
1444 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1445 Py_DECREF(dividend);
1446 Py_DECREF(divisor);
1447 SET_TOP(quotient);
1448 if (quotient == NULL)
1449 goto error;
1450 DISPATCH();
1451 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001452
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001453 TARGET(BINARY_MODULO) {
1454 PyObject *divisor = POP();
1455 PyObject *dividend = TOP();
Martijn Pieters53039ad2017-02-27 16:08:01 +00001456 PyObject *res;
1457 if (PyUnicode_CheckExact(dividend) && (
1458 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1459 // fast path; string formatting, but not if the RHS is a str subclass
1460 // (see issue28598)
1461 res = PyUnicode_Format(dividend, divisor);
1462 } else {
1463 res = PyNumber_Remainder(dividend, divisor);
1464 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001465 Py_DECREF(divisor);
1466 Py_DECREF(dividend);
1467 SET_TOP(res);
1468 if (res == NULL)
1469 goto error;
1470 DISPATCH();
1471 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001472
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001473 TARGET(BINARY_ADD) {
1474 PyObject *right = POP();
1475 PyObject *left = TOP();
1476 PyObject *sum;
1477 if (PyUnicode_CheckExact(left) &&
1478 PyUnicode_CheckExact(right)) {
1479 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001480 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001481 }
1482 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001483 sum = PyNumber_Add(left, right);
1484 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001485 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001486 Py_DECREF(right);
1487 SET_TOP(sum);
1488 if (sum == NULL)
1489 goto error;
1490 DISPATCH();
1491 }
1492
1493 TARGET(BINARY_SUBTRACT) {
1494 PyObject *right = POP();
1495 PyObject *left = TOP();
1496 PyObject *diff = PyNumber_Subtract(left, right);
1497 Py_DECREF(right);
1498 Py_DECREF(left);
1499 SET_TOP(diff);
1500 if (diff == NULL)
1501 goto error;
1502 DISPATCH();
1503 }
1504
1505 TARGET(BINARY_SUBSCR) {
1506 PyObject *sub = POP();
1507 PyObject *container = TOP();
1508 PyObject *res = PyObject_GetItem(container, sub);
1509 Py_DECREF(container);
1510 Py_DECREF(sub);
1511 SET_TOP(res);
1512 if (res == NULL)
1513 goto error;
1514 DISPATCH();
1515 }
1516
1517 TARGET(BINARY_LSHIFT) {
1518 PyObject *right = POP();
1519 PyObject *left = TOP();
1520 PyObject *res = PyNumber_Lshift(left, right);
1521 Py_DECREF(left);
1522 Py_DECREF(right);
1523 SET_TOP(res);
1524 if (res == NULL)
1525 goto error;
1526 DISPATCH();
1527 }
1528
1529 TARGET(BINARY_RSHIFT) {
1530 PyObject *right = POP();
1531 PyObject *left = TOP();
1532 PyObject *res = PyNumber_Rshift(left, right);
1533 Py_DECREF(left);
1534 Py_DECREF(right);
1535 SET_TOP(res);
1536 if (res == NULL)
1537 goto error;
1538 DISPATCH();
1539 }
1540
1541 TARGET(BINARY_AND) {
1542 PyObject *right = POP();
1543 PyObject *left = TOP();
1544 PyObject *res = PyNumber_And(left, right);
1545 Py_DECREF(left);
1546 Py_DECREF(right);
1547 SET_TOP(res);
1548 if (res == NULL)
1549 goto error;
1550 DISPATCH();
1551 }
1552
1553 TARGET(BINARY_XOR) {
1554 PyObject *right = POP();
1555 PyObject *left = TOP();
1556 PyObject *res = PyNumber_Xor(left, right);
1557 Py_DECREF(left);
1558 Py_DECREF(right);
1559 SET_TOP(res);
1560 if (res == NULL)
1561 goto error;
1562 DISPATCH();
1563 }
1564
1565 TARGET(BINARY_OR) {
1566 PyObject *right = POP();
1567 PyObject *left = TOP();
1568 PyObject *res = PyNumber_Or(left, right);
1569 Py_DECREF(left);
1570 Py_DECREF(right);
1571 SET_TOP(res);
1572 if (res == NULL)
1573 goto error;
1574 DISPATCH();
1575 }
1576
1577 TARGET(LIST_APPEND) {
1578 PyObject *v = POP();
1579 PyObject *list = PEEK(oparg);
1580 int err;
1581 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001583 if (err != 0)
1584 goto error;
1585 PREDICT(JUMP_ABSOLUTE);
1586 DISPATCH();
1587 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001588
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001589 TARGET(SET_ADD) {
1590 PyObject *v = POP();
1591 PyObject *set = stack_pointer[-oparg];
1592 int err;
1593 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001595 if (err != 0)
1596 goto error;
1597 PREDICT(JUMP_ABSOLUTE);
1598 DISPATCH();
1599 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001600
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001601 TARGET(INPLACE_POWER) {
1602 PyObject *exp = POP();
1603 PyObject *base = TOP();
1604 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1605 Py_DECREF(base);
1606 Py_DECREF(exp);
1607 SET_TOP(res);
1608 if (res == NULL)
1609 goto error;
1610 DISPATCH();
1611 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001612
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001613 TARGET(INPLACE_MULTIPLY) {
1614 PyObject *right = POP();
1615 PyObject *left = TOP();
1616 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1617 Py_DECREF(left);
1618 Py_DECREF(right);
1619 SET_TOP(res);
1620 if (res == NULL)
1621 goto error;
1622 DISPATCH();
1623 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001624
Benjamin Petersond51374e2014-04-09 23:55:56 -04001625 TARGET(INPLACE_MATRIX_MULTIPLY) {
1626 PyObject *right = POP();
1627 PyObject *left = TOP();
1628 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1629 Py_DECREF(left);
1630 Py_DECREF(right);
1631 SET_TOP(res);
1632 if (res == NULL)
1633 goto error;
1634 DISPATCH();
1635 }
1636
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001637 TARGET(INPLACE_TRUE_DIVIDE) {
1638 PyObject *divisor = POP();
1639 PyObject *dividend = TOP();
1640 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1641 Py_DECREF(dividend);
1642 Py_DECREF(divisor);
1643 SET_TOP(quotient);
1644 if (quotient == NULL)
1645 goto error;
1646 DISPATCH();
1647 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001648
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001649 TARGET(INPLACE_FLOOR_DIVIDE) {
1650 PyObject *divisor = POP();
1651 PyObject *dividend = TOP();
1652 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1653 Py_DECREF(dividend);
1654 Py_DECREF(divisor);
1655 SET_TOP(quotient);
1656 if (quotient == NULL)
1657 goto error;
1658 DISPATCH();
1659 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001660
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001661 TARGET(INPLACE_MODULO) {
1662 PyObject *right = POP();
1663 PyObject *left = TOP();
1664 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1665 Py_DECREF(left);
1666 Py_DECREF(right);
1667 SET_TOP(mod);
1668 if (mod == NULL)
1669 goto error;
1670 DISPATCH();
1671 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001672
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001673 TARGET(INPLACE_ADD) {
1674 PyObject *right = POP();
1675 PyObject *left = TOP();
1676 PyObject *sum;
1677 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1678 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001679 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001680 }
1681 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001682 sum = PyNumber_InPlaceAdd(left, right);
1683 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001684 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001685 Py_DECREF(right);
1686 SET_TOP(sum);
1687 if (sum == NULL)
1688 goto error;
1689 DISPATCH();
1690 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001691
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001692 TARGET(INPLACE_SUBTRACT) {
1693 PyObject *right = POP();
1694 PyObject *left = TOP();
1695 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1696 Py_DECREF(left);
1697 Py_DECREF(right);
1698 SET_TOP(diff);
1699 if (diff == NULL)
1700 goto error;
1701 DISPATCH();
1702 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001703
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001704 TARGET(INPLACE_LSHIFT) {
1705 PyObject *right = POP();
1706 PyObject *left = TOP();
1707 PyObject *res = PyNumber_InPlaceLshift(left, right);
1708 Py_DECREF(left);
1709 Py_DECREF(right);
1710 SET_TOP(res);
1711 if (res == NULL)
1712 goto error;
1713 DISPATCH();
1714 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001715
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001716 TARGET(INPLACE_RSHIFT) {
1717 PyObject *right = POP();
1718 PyObject *left = TOP();
1719 PyObject *res = PyNumber_InPlaceRshift(left, right);
1720 Py_DECREF(left);
1721 Py_DECREF(right);
1722 SET_TOP(res);
1723 if (res == NULL)
1724 goto error;
1725 DISPATCH();
1726 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001727
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001728 TARGET(INPLACE_AND) {
1729 PyObject *right = POP();
1730 PyObject *left = TOP();
1731 PyObject *res = PyNumber_InPlaceAnd(left, right);
1732 Py_DECREF(left);
1733 Py_DECREF(right);
1734 SET_TOP(res);
1735 if (res == NULL)
1736 goto error;
1737 DISPATCH();
1738 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001739
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001740 TARGET(INPLACE_XOR) {
1741 PyObject *right = POP();
1742 PyObject *left = TOP();
1743 PyObject *res = PyNumber_InPlaceXor(left, right);
1744 Py_DECREF(left);
1745 Py_DECREF(right);
1746 SET_TOP(res);
1747 if (res == NULL)
1748 goto error;
1749 DISPATCH();
1750 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001751
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001752 TARGET(INPLACE_OR) {
1753 PyObject *right = POP();
1754 PyObject *left = TOP();
1755 PyObject *res = PyNumber_InPlaceOr(left, right);
1756 Py_DECREF(left);
1757 Py_DECREF(right);
1758 SET_TOP(res);
1759 if (res == NULL)
1760 goto error;
1761 DISPATCH();
1762 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001763
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001764 TARGET(STORE_SUBSCR) {
1765 PyObject *sub = TOP();
1766 PyObject *container = SECOND();
1767 PyObject *v = THIRD();
1768 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 STACKADJ(-3);
Martin Panter95f53c12016-07-18 08:23:26 +00001770 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001771 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001773 Py_DECREF(container);
1774 Py_DECREF(sub);
1775 if (err != 0)
1776 goto error;
1777 DISPATCH();
1778 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001779
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001780 TARGET(STORE_ANNOTATION) {
1781 _Py_IDENTIFIER(__annotations__);
1782 PyObject *ann_dict;
1783 PyObject *ann = POP();
1784 PyObject *name = GETITEM(names, oparg);
1785 int err;
1786 if (f->f_locals == NULL) {
1787 PyErr_Format(PyExc_SystemError,
1788 "no locals found when storing annotation");
1789 Py_DECREF(ann);
1790 goto error;
1791 }
1792 /* first try to get __annotations__ from locals... */
1793 if (PyDict_CheckExact(f->f_locals)) {
1794 ann_dict = _PyDict_GetItemId(f->f_locals,
1795 &PyId___annotations__);
1796 if (ann_dict == NULL) {
1797 PyErr_SetString(PyExc_NameError,
1798 "__annotations__ not found");
1799 Py_DECREF(ann);
1800 goto error;
1801 }
1802 Py_INCREF(ann_dict);
1803 }
1804 else {
1805 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
1806 if (ann_str == NULL) {
1807 Py_DECREF(ann);
1808 goto error;
1809 }
1810 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
1811 if (ann_dict == NULL) {
1812 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
1813 PyErr_SetString(PyExc_NameError,
1814 "__annotations__ not found");
1815 }
1816 Py_DECREF(ann);
1817 goto error;
1818 }
1819 }
1820 /* ...if succeeded, __annotations__[name] = ann */
1821 if (PyDict_CheckExact(ann_dict)) {
1822 err = PyDict_SetItem(ann_dict, name, ann);
1823 }
1824 else {
1825 err = PyObject_SetItem(ann_dict, name, ann);
1826 }
1827 Py_DECREF(ann_dict);
Yury Selivanov50c584f2016-09-08 23:38:21 -07001828 Py_DECREF(ann);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001829 if (err != 0) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001830 goto error;
1831 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001832 DISPATCH();
1833 }
1834
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001835 TARGET(DELETE_SUBSCR) {
1836 PyObject *sub = TOP();
1837 PyObject *container = SECOND();
1838 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001839 STACKADJ(-2);
Martin Panter95f53c12016-07-18 08:23:26 +00001840 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001841 err = PyObject_DelItem(container, sub);
1842 Py_DECREF(container);
1843 Py_DECREF(sub);
1844 if (err != 0)
1845 goto error;
1846 DISPATCH();
1847 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001848
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001849 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001850 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001851 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001852 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001853 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001854 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 PyErr_SetString(PyExc_RuntimeError,
1856 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001857 Py_DECREF(value);
1858 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 }
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001860 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001861 Py_DECREF(value);
1862 if (res == NULL)
1863 goto error;
1864 Py_DECREF(res);
1865 DISPATCH();
1866 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001867
Thomas Wouters434d0822000-08-24 20:11:32 +00001868#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001870#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001871 TARGET(RAISE_VARARGS) {
1872 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 switch (oparg) {
1874 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001875 cause = POP(); /* cause */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001877 exc = POP(); /* exc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 case 0: /* Fallthrough */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001879 if (do_raise(exc, cause)) {
1880 why = WHY_EXCEPTION;
1881 goto fast_block_end;
1882 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 break;
1884 default:
1885 PyErr_SetString(PyExc_SystemError,
1886 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 break;
1888 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001889 goto error;
1890 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001891
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001892 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 retval = POP();
1894 why = WHY_RETURN;
1895 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001896 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001897
Yury Selivanov75445082015-05-11 22:57:16 -04001898 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001899 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001900 PyObject *iter = NULL;
1901 PyObject *awaitable = NULL;
1902 PyObject *obj = TOP();
1903 PyTypeObject *type = Py_TYPE(obj);
1904
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001905 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001906 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001907 }
Yury Selivanov75445082015-05-11 22:57:16 -04001908
1909 if (getter != NULL) {
1910 iter = (*getter)(obj);
1911 Py_DECREF(obj);
1912 if (iter == NULL) {
1913 SET_TOP(NULL);
1914 goto error;
1915 }
1916 }
1917 else {
1918 SET_TOP(NULL);
1919 PyErr_Format(
1920 PyExc_TypeError,
1921 "'async for' requires an object with "
1922 "__aiter__ method, got %.100s",
1923 type->tp_name);
1924 Py_DECREF(obj);
1925 goto error;
1926 }
1927
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001928 if (Py_TYPE(iter)->tp_as_async != NULL &&
1929 Py_TYPE(iter)->tp_as_async->am_anext != NULL) {
1930
1931 /* Starting with CPython 3.5.2 __aiter__ should return
1932 asynchronous iterators directly (not awaitables that
1933 resolve to asynchronous iterators.)
1934
1935 Therefore, we check if the object that was returned
1936 from __aiter__ has an __anext__ method. If it does,
1937 we wrap it in an awaitable that resolves to `iter`.
1938
1939 See http://bugs.python.org/issue27243 for more
1940 details.
1941 */
1942
1943 PyObject *wrapper = _PyAIterWrapper_New(iter);
1944 Py_DECREF(iter);
1945 SET_TOP(wrapper);
1946 DISPATCH();
1947 }
1948
Yury Selivanov5376ba92015-06-22 12:19:30 -04001949 awaitable = _PyCoro_GetAwaitableIter(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001950 if (awaitable == NULL) {
Yury Selivanovdea51012017-03-02 22:20:00 -05001951 _PyErr_FormatFromCause(
Yury Selivanov75445082015-05-11 22:57:16 -04001952 PyExc_TypeError,
1953 "'async for' received an invalid object "
1954 "from __aiter__: %.100s",
1955 Py_TYPE(iter)->tp_name);
1956
Yury Selivanovdea51012017-03-02 22:20:00 -05001957 SET_TOP(NULL);
Yury Selivanov75445082015-05-11 22:57:16 -04001958 Py_DECREF(iter);
1959 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001960 } else {
Yury Selivanov75445082015-05-11 22:57:16 -04001961 Py_DECREF(iter);
1962
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001963 if (PyErr_WarnFormat(
Yury Selivanov2edd8a12016-11-08 15:13:07 -05001964 PyExc_DeprecationWarning, 1,
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001965 "'%.100s' implements legacy __aiter__ protocol; "
1966 "__aiter__ should return an asynchronous "
1967 "iterator, not awaitable",
1968 type->tp_name))
1969 {
1970 /* Warning was converted to an error. */
1971 Py_DECREF(awaitable);
1972 SET_TOP(NULL);
1973 goto error;
1974 }
1975 }
1976
Yury Selivanov75445082015-05-11 22:57:16 -04001977 SET_TOP(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001978 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001979 DISPATCH();
1980 }
1981
1982 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001983 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001984 PyObject *next_iter = NULL;
1985 PyObject *awaitable = NULL;
1986 PyObject *aiter = TOP();
1987 PyTypeObject *type = Py_TYPE(aiter);
1988
Yury Selivanoveb636452016-09-08 22:01:51 -07001989 if (PyAsyncGen_CheckExact(aiter)) {
1990 awaitable = type->tp_as_async->am_anext(aiter);
1991 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001992 goto error;
1993 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001994 } else {
1995 if (type->tp_as_async != NULL){
1996 getter = type->tp_as_async->am_anext;
1997 }
Yury Selivanov75445082015-05-11 22:57:16 -04001998
Yury Selivanoveb636452016-09-08 22:01:51 -07001999 if (getter != NULL) {
2000 next_iter = (*getter)(aiter);
2001 if (next_iter == NULL) {
2002 goto error;
2003 }
2004 }
2005 else {
2006 PyErr_Format(
2007 PyExc_TypeError,
2008 "'async for' requires an iterator with "
2009 "__anext__ method, got %.100s",
2010 type->tp_name);
2011 goto error;
2012 }
Yury Selivanov75445082015-05-11 22:57:16 -04002013
Yury Selivanoveb636452016-09-08 22:01:51 -07002014 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2015 if (awaitable == NULL) {
Yury Selivanovdea51012017-03-02 22:20:00 -05002016 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002017 PyExc_TypeError,
2018 "'async for' received an invalid object "
2019 "from __anext__: %.100s",
2020 Py_TYPE(next_iter)->tp_name);
2021
2022 Py_DECREF(next_iter);
2023 goto error;
2024 } else {
2025 Py_DECREF(next_iter);
2026 }
2027 }
Yury Selivanov75445082015-05-11 22:57:16 -04002028
2029 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002030 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002031 DISPATCH();
2032 }
2033
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002034 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002035 TARGET(GET_AWAITABLE) {
2036 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002037 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002038
2039 Py_DECREF(iterable);
2040
Yury Selivanovc724bae2016-03-02 11:30:46 -05002041 if (iter != NULL && PyCoro_CheckExact(iter)) {
2042 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2043 if (yf != NULL) {
2044 /* `iter` is a coroutine object that is being
2045 awaited, `yf` is a pointer to the current awaitable
2046 being awaited on. */
2047 Py_DECREF(yf);
2048 Py_CLEAR(iter);
2049 PyErr_SetString(
2050 PyExc_RuntimeError,
2051 "coroutine is being awaited already");
2052 /* The code below jumps to `error` if `iter` is NULL. */
2053 }
2054 }
2055
Yury Selivanov75445082015-05-11 22:57:16 -04002056 SET_TOP(iter); /* Even if it's NULL */
2057
2058 if (iter == NULL) {
2059 goto error;
2060 }
2061
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002062 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002063 DISPATCH();
2064 }
2065
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002066 TARGET(YIELD_FROM) {
2067 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002068 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002069 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002070 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2071 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002072 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002073 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002074 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002075 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002076 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002077 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002078 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002079 Py_DECREF(v);
2080 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002081 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002082 if (tstate->c_tracefunc != NULL
2083 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002084 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002085 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002086 if (err < 0)
2087 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002088 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002089 SET_TOP(val);
2090 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002091 }
Martin Panter95f53c12016-07-18 08:23:26 +00002092 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002093 f->f_stacktop = stack_pointer;
2094 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002095 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002096 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002097 f->f_lasti -= sizeof(_Py_CODEUNIT);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002098 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002099 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002100
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002101 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002102 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002103
2104 if (co->co_flags & CO_ASYNC_GENERATOR) {
2105 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2106 Py_DECREF(retval);
2107 if (w == NULL) {
2108 retval = NULL;
2109 goto error;
2110 }
2111 retval = w;
2112 }
2113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114 f->f_stacktop = stack_pointer;
2115 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002116 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002117 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002118
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002119 TARGET(POP_EXCEPT) {
2120 PyTryBlock *b = PyFrame_BlockPop(f);
2121 if (b->b_type != EXCEPT_HANDLER) {
2122 PyErr_SetString(PyExc_SystemError,
2123 "popped block is not an except handler");
2124 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002125 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002126 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002127 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002128 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002129
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002130 PREDICTED(POP_BLOCK);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002131 TARGET(POP_BLOCK) {
2132 PyTryBlock *b = PyFrame_BlockPop(f);
2133 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002134 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002135 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002137 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002138 TARGET(END_FINALLY) {
2139 PyObject *status = POP();
2140 if (PyLong_Check(status)) {
2141 why = (enum why_code) PyLong_AS_LONG(status);
2142 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002143 if (why == WHY_RETURN ||
2144 why == WHY_CONTINUE)
2145 retval = POP();
2146 if (why == WHY_SILENCED) {
2147 /* An exception was silenced by 'with', we must
2148 manually unwind the EXCEPT_HANDLER block which was
2149 created when the exception was caught, otherwise
2150 the stack will be in an inconsistent state. */
2151 PyTryBlock *b = PyFrame_BlockPop(f);
2152 assert(b->b_type == EXCEPT_HANDLER);
2153 UNWIND_EXCEPT_HANDLER(b);
2154 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002155 Py_DECREF(status);
2156 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002157 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002158 Py_DECREF(status);
2159 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002160 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002161 else if (PyExceptionClass_Check(status)) {
2162 PyObject *exc = POP();
2163 PyObject *tb = POP();
2164 PyErr_Restore(status, exc, tb);
2165 why = WHY_EXCEPTION;
2166 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002167 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002168 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 PyErr_SetString(PyExc_SystemError,
2170 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002171 Py_DECREF(status);
2172 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002174 Py_DECREF(status);
2175 DISPATCH();
2176 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002177
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002178 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002179 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002180
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002181 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002182 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002183 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
2184 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002185 PyErr_SetString(PyExc_NameError,
2186 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002187 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002188 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002189 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002190 }
2191 else {
2192 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2193 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002194 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002195 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2196 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002197 if (PyErr_ExceptionMatches(PyExc_KeyError))
2198 PyErr_SetString(PyExc_NameError,
2199 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002200 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002201 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002202 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002203 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002204 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002205 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002206
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002207 TARGET(STORE_NAME) {
2208 PyObject *name = GETITEM(names, oparg);
2209 PyObject *v = POP();
2210 PyObject *ns = f->f_locals;
2211 int err;
2212 if (ns == NULL) {
2213 PyErr_Format(PyExc_SystemError,
2214 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002215 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002216 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002217 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002218 if (PyDict_CheckExact(ns))
2219 err = PyDict_SetItem(ns, name, v);
2220 else
2221 err = PyObject_SetItem(ns, name, v);
2222 Py_DECREF(v);
2223 if (err != 0)
2224 goto error;
2225 DISPATCH();
2226 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002227
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002228 TARGET(DELETE_NAME) {
2229 PyObject *name = GETITEM(names, oparg);
2230 PyObject *ns = f->f_locals;
2231 int err;
2232 if (ns == NULL) {
2233 PyErr_Format(PyExc_SystemError,
2234 "no locals when deleting %R", name);
2235 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002237 err = PyObject_DelItem(ns, name);
2238 if (err != 0) {
2239 format_exc_check_arg(PyExc_NameError,
2240 NAME_ERROR_MSG,
2241 name);
2242 goto error;
2243 }
2244 DISPATCH();
2245 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002246
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002247 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002248 TARGET(UNPACK_SEQUENCE) {
2249 PyObject *seq = POP(), *item, **items;
2250 if (PyTuple_CheckExact(seq) &&
2251 PyTuple_GET_SIZE(seq) == oparg) {
2252 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002254 item = items[oparg];
2255 Py_INCREF(item);
2256 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002258 } else if (PyList_CheckExact(seq) &&
2259 PyList_GET_SIZE(seq) == oparg) {
2260 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002262 item = items[oparg];
2263 Py_INCREF(item);
2264 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002266 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002267 stack_pointer + oparg)) {
2268 STACKADJ(oparg);
2269 } else {
2270 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002271 Py_DECREF(seq);
2272 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002273 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002274 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002275 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002276 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002277
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002278 TARGET(UNPACK_EX) {
2279 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2280 PyObject *seq = POP();
2281
2282 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2283 stack_pointer + totalargs)) {
2284 stack_pointer += totalargs;
2285 } else {
2286 Py_DECREF(seq);
2287 goto error;
2288 }
2289 Py_DECREF(seq);
2290 DISPATCH();
2291 }
2292
2293 TARGET(STORE_ATTR) {
2294 PyObject *name = GETITEM(names, oparg);
2295 PyObject *owner = TOP();
2296 PyObject *v = SECOND();
2297 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002299 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002301 Py_DECREF(owner);
2302 if (err != 0)
2303 goto error;
2304 DISPATCH();
2305 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002306
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002307 TARGET(DELETE_ATTR) {
2308 PyObject *name = GETITEM(names, oparg);
2309 PyObject *owner = POP();
2310 int err;
2311 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2312 Py_DECREF(owner);
2313 if (err != 0)
2314 goto error;
2315 DISPATCH();
2316 }
2317
2318 TARGET(STORE_GLOBAL) {
2319 PyObject *name = GETITEM(names, oparg);
2320 PyObject *v = POP();
2321 int err;
2322 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002323 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002324 if (err != 0)
2325 goto error;
2326 DISPATCH();
2327 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002328
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002329 TARGET(DELETE_GLOBAL) {
2330 PyObject *name = GETITEM(names, oparg);
2331 int err;
2332 err = PyDict_DelItem(f->f_globals, name);
2333 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002335 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002336 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002337 }
2338 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002339 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002340
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002341 TARGET(LOAD_NAME) {
2342 PyObject *name = GETITEM(names, oparg);
2343 PyObject *locals = f->f_locals;
2344 PyObject *v;
2345 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002347 "no locals when loading %R", name);
2348 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002349 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002350 if (PyDict_CheckExact(locals)) {
2351 v = PyDict_GetItem(locals, name);
2352 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353 }
2354 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002355 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002356 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002357 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2358 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 PyErr_Clear();
2360 }
2361 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002362 if (v == NULL) {
2363 v = PyDict_GetItem(f->f_globals, name);
2364 Py_XINCREF(v);
2365 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002366 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002367 v = PyDict_GetItem(f->f_builtins, name);
2368 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002369 format_exc_check_arg(
2370 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002371 NAME_ERROR_MSG, name);
2372 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002373 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002374 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002375 }
2376 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002377 v = PyObject_GetItem(f->f_builtins, name);
2378 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002379 if (PyErr_ExceptionMatches(PyExc_KeyError))
2380 format_exc_check_arg(
2381 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002382 NAME_ERROR_MSG, name);
2383 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002384 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002385 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002387 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002388 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002390 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002391
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002392 TARGET(LOAD_GLOBAL) {
2393 PyObject *name = GETITEM(names, oparg);
2394 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002395 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002396 && PyDict_CheckExact(f->f_builtins))
2397 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002398 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002399 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002400 name);
2401 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002402 if (!_PyErr_OCCURRED()) {
2403 /* _PyDict_LoadGlobal() returns NULL without raising
2404 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002405 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002406 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002407 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002408 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002409 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002410 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002412 else {
2413 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002414
2415 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002416 v = PyObject_GetItem(f->f_globals, name);
2417 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002418 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2419 goto error;
2420 PyErr_Clear();
2421
Victor Stinnerb4efc962015-11-20 09:24:02 +01002422 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002423 v = PyObject_GetItem(f->f_builtins, name);
2424 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002425 if (PyErr_ExceptionMatches(PyExc_KeyError))
2426 format_exc_check_arg(
2427 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002428 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002429 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002430 }
2431 }
2432 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002433 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002434 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002435 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002436
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002437 TARGET(DELETE_FAST) {
2438 PyObject *v = GETLOCAL(oparg);
2439 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 SETLOCAL(oparg, NULL);
2441 DISPATCH();
2442 }
2443 format_exc_check_arg(
2444 PyExc_UnboundLocalError,
2445 UNBOUNDLOCAL_ERROR_MSG,
2446 PyTuple_GetItem(co->co_varnames, oparg)
2447 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002448 goto error;
2449 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002450
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002451 TARGET(DELETE_DEREF) {
2452 PyObject *cell = freevars[oparg];
2453 if (PyCell_GET(cell) != NULL) {
2454 PyCell_Set(cell, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002455 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002456 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002457 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002458 goto error;
2459 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002460
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002461 TARGET(LOAD_CLOSURE) {
2462 PyObject *cell = freevars[oparg];
2463 Py_INCREF(cell);
2464 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002465 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002466 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002467
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002468 TARGET(LOAD_CLASSDEREF) {
2469 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002470 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002471 assert(locals);
2472 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2473 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2474 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2475 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2476 if (PyDict_CheckExact(locals)) {
2477 value = PyDict_GetItem(locals, name);
2478 Py_XINCREF(value);
2479 }
2480 else {
2481 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002482 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002483 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2484 goto error;
2485 PyErr_Clear();
2486 }
2487 }
2488 if (!value) {
2489 PyObject *cell = freevars[oparg];
2490 value = PyCell_GET(cell);
2491 if (value == NULL) {
2492 format_exc_unbound(co, oparg);
2493 goto error;
2494 }
2495 Py_INCREF(value);
2496 }
2497 PUSH(value);
2498 DISPATCH();
2499 }
2500
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002501 TARGET(LOAD_DEREF) {
2502 PyObject *cell = freevars[oparg];
2503 PyObject *value = PyCell_GET(cell);
2504 if (value == NULL) {
2505 format_exc_unbound(co, oparg);
2506 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002507 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002508 Py_INCREF(value);
2509 PUSH(value);
2510 DISPATCH();
2511 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002512
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002513 TARGET(STORE_DEREF) {
2514 PyObject *v = POP();
2515 PyObject *cell = freevars[oparg];
Raymond Hettinger13527122016-11-11 04:31:18 -08002516 PyObject *oldobj = PyCell_GET(cell);
2517 PyCell_SET(cell, v);
2518 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002519 DISPATCH();
2520 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002521
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002522 TARGET(BUILD_STRING) {
2523 PyObject *str;
2524 PyObject *empty = PyUnicode_New(0, 0);
2525 if (empty == NULL) {
2526 goto error;
2527 }
2528 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2529 Py_DECREF(empty);
2530 if (str == NULL)
2531 goto error;
2532 while (--oparg >= 0) {
2533 PyObject *item = POP();
2534 Py_DECREF(item);
2535 }
2536 PUSH(str);
2537 DISPATCH();
2538 }
2539
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002540 TARGET(BUILD_TUPLE) {
2541 PyObject *tup = PyTuple_New(oparg);
2542 if (tup == NULL)
2543 goto error;
2544 while (--oparg >= 0) {
2545 PyObject *item = POP();
2546 PyTuple_SET_ITEM(tup, oparg, item);
2547 }
2548 PUSH(tup);
2549 DISPATCH();
2550 }
2551
2552 TARGET(BUILD_LIST) {
2553 PyObject *list = PyList_New(oparg);
2554 if (list == NULL)
2555 goto error;
2556 while (--oparg >= 0) {
2557 PyObject *item = POP();
2558 PyList_SET_ITEM(list, oparg, item);
2559 }
2560 PUSH(list);
2561 DISPATCH();
2562 }
2563
Serhiy Storchaka73442852016-10-02 10:33:46 +03002564 TARGET(BUILD_TUPLE_UNPACK_WITH_CALL)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002565 TARGET(BUILD_TUPLE_UNPACK)
2566 TARGET(BUILD_LIST_UNPACK) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002567 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002568 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002569 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002570 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002571
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002572 if (sum == NULL)
2573 goto error;
2574
2575 for (i = oparg; i > 0; i--) {
2576 PyObject *none_val;
2577
2578 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2579 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002580 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
2581 PyErr_ExceptionMatches(PyExc_TypeError)) {
2582 PyObject *func = PEEK(1 + oparg);
2583 PyErr_Format(PyExc_TypeError,
2584 "%.200s%.200s argument after * "
2585 "must be an iterable, not %.200s",
2586 PyEval_GetFuncName(func),
2587 PyEval_GetFuncDesc(func),
2588 PEEK(i)->ob_type->tp_name);
2589 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002590 Py_DECREF(sum);
2591 goto error;
2592 }
2593 Py_DECREF(none_val);
2594 }
2595
2596 if (convert_to_tuple) {
2597 return_value = PyList_AsTuple(sum);
2598 Py_DECREF(sum);
2599 if (return_value == NULL)
2600 goto error;
2601 }
2602 else {
2603 return_value = sum;
2604 }
2605
2606 while (oparg--)
2607 Py_DECREF(POP());
2608 PUSH(return_value);
2609 DISPATCH();
2610 }
2611
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002612 TARGET(BUILD_SET) {
2613 PyObject *set = PySet_New(NULL);
2614 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002615 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002616 if (set == NULL)
2617 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002618 for (i = oparg; i > 0; i--) {
2619 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002620 if (err == 0)
2621 err = PySet_Add(set, item);
2622 Py_DECREF(item);
2623 }
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002624 STACKADJ(-oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002625 if (err != 0) {
2626 Py_DECREF(set);
2627 goto error;
2628 }
2629 PUSH(set);
2630 DISPATCH();
2631 }
2632
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002633 TARGET(BUILD_SET_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002634 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002635 PyObject *sum = PySet_New(NULL);
2636 if (sum == NULL)
2637 goto error;
2638
2639 for (i = oparg; i > 0; i--) {
2640 if (_PySet_Update(sum, PEEK(i)) < 0) {
2641 Py_DECREF(sum);
2642 goto error;
2643 }
2644 }
2645
2646 while (oparg--)
2647 Py_DECREF(POP());
2648 PUSH(sum);
2649 DISPATCH();
2650 }
2651
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002652 TARGET(BUILD_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002653 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002654 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2655 if (map == NULL)
2656 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002657 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002658 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002659 PyObject *key = PEEK(2*i);
2660 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002661 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002662 if (err != 0) {
2663 Py_DECREF(map);
2664 goto error;
2665 }
2666 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002667
2668 while (oparg--) {
2669 Py_DECREF(POP());
2670 Py_DECREF(POP());
2671 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002672 PUSH(map);
2673 DISPATCH();
2674 }
2675
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002676 TARGET(SETUP_ANNOTATIONS) {
2677 _Py_IDENTIFIER(__annotations__);
2678 int err;
2679 PyObject *ann_dict;
2680 if (f->f_locals == NULL) {
2681 PyErr_Format(PyExc_SystemError,
2682 "no locals found when setting up annotations");
2683 goto error;
2684 }
2685 /* check if __annotations__ in locals()... */
2686 if (PyDict_CheckExact(f->f_locals)) {
2687 ann_dict = _PyDict_GetItemId(f->f_locals,
2688 &PyId___annotations__);
2689 if (ann_dict == NULL) {
2690 /* ...if not, create a new one */
2691 ann_dict = PyDict_New();
2692 if (ann_dict == NULL) {
2693 goto error;
2694 }
2695 err = _PyDict_SetItemId(f->f_locals,
2696 &PyId___annotations__, ann_dict);
2697 Py_DECREF(ann_dict);
2698 if (err != 0) {
2699 goto error;
2700 }
2701 }
2702 }
2703 else {
2704 /* do the same if locals() is not a dict */
2705 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2706 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002707 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002708 }
2709 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2710 if (ann_dict == NULL) {
2711 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2712 goto error;
2713 }
2714 PyErr_Clear();
2715 ann_dict = PyDict_New();
2716 if (ann_dict == NULL) {
2717 goto error;
2718 }
2719 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2720 Py_DECREF(ann_dict);
2721 if (err != 0) {
2722 goto error;
2723 }
2724 }
2725 else {
2726 Py_DECREF(ann_dict);
2727 }
2728 }
2729 DISPATCH();
2730 }
2731
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002732 TARGET(BUILD_CONST_KEY_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002733 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002734 PyObject *map;
2735 PyObject *keys = TOP();
2736 if (!PyTuple_CheckExact(keys) ||
2737 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2738 PyErr_SetString(PyExc_SystemError,
2739 "bad BUILD_CONST_KEY_MAP keys argument");
2740 goto error;
2741 }
2742 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2743 if (map == NULL) {
2744 goto error;
2745 }
2746 for (i = oparg; i > 0; i--) {
2747 int err;
2748 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2749 PyObject *value = PEEK(i + 1);
2750 err = PyDict_SetItem(map, key, value);
2751 if (err != 0) {
2752 Py_DECREF(map);
2753 goto error;
2754 }
2755 }
2756
2757 Py_DECREF(POP());
2758 while (oparg--) {
2759 Py_DECREF(POP());
2760 }
2761 PUSH(map);
2762 DISPATCH();
2763 }
2764
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002765 TARGET(BUILD_MAP_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002766 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002767 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002768 if (sum == NULL)
2769 goto error;
2770
2771 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002772 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002773 if (PyDict_Update(sum, arg) < 0) {
2774 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2775 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002776 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002777 arg->ob_type->tp_name);
2778 }
2779 Py_DECREF(sum);
2780 goto error;
2781 }
2782 }
2783
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002784 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002785 Py_DECREF(POP());
2786 PUSH(sum);
2787 DISPATCH();
2788 }
2789
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002790 TARGET(BUILD_MAP_UNPACK_WITH_CALL) {
2791 Py_ssize_t i;
2792 PyObject *sum = PyDict_New();
2793 if (sum == NULL)
2794 goto error;
2795
2796 for (i = oparg; i > 0; i--) {
2797 PyObject *arg = PEEK(i);
2798 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
2799 PyObject *func = PEEK(2 + oparg);
2800 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2801 PyErr_Format(PyExc_TypeError,
2802 "%.200s%.200s argument after ** "
2803 "must be a mapping, not %.200s",
2804 PyEval_GetFuncName(func),
2805 PyEval_GetFuncDesc(func),
2806 arg->ob_type->tp_name);
2807 }
2808 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2809 PyObject *exc, *val, *tb;
2810 PyErr_Fetch(&exc, &val, &tb);
2811 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
2812 PyObject *key = PyTuple_GET_ITEM(val, 0);
2813 if (!PyUnicode_Check(key)) {
2814 PyErr_Format(PyExc_TypeError,
2815 "%.200s%.200s keywords must be strings",
2816 PyEval_GetFuncName(func),
2817 PyEval_GetFuncDesc(func));
2818 } else {
2819 PyErr_Format(PyExc_TypeError,
2820 "%.200s%.200s got multiple "
2821 "values for keyword argument '%U'",
2822 PyEval_GetFuncName(func),
2823 PyEval_GetFuncDesc(func),
2824 key);
2825 }
2826 Py_XDECREF(exc);
2827 Py_XDECREF(val);
2828 Py_XDECREF(tb);
2829 }
2830 else {
2831 PyErr_Restore(exc, val, tb);
2832 }
2833 }
2834 Py_DECREF(sum);
2835 goto error;
2836 }
2837 }
2838
2839 while (oparg--)
2840 Py_DECREF(POP());
2841 PUSH(sum);
2842 DISPATCH();
2843 }
2844
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002845 TARGET(MAP_ADD) {
2846 PyObject *key = TOP();
2847 PyObject *value = SECOND();
2848 PyObject *map;
2849 int err;
2850 STACKADJ(-2);
2851 map = stack_pointer[-oparg]; /* dict */
2852 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002853 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002854 Py_DECREF(value);
2855 Py_DECREF(key);
2856 if (err != 0)
2857 goto error;
2858 PREDICT(JUMP_ABSOLUTE);
2859 DISPATCH();
2860 }
2861
2862 TARGET(LOAD_ATTR) {
2863 PyObject *name = GETITEM(names, oparg);
2864 PyObject *owner = TOP();
2865 PyObject *res = PyObject_GetAttr(owner, name);
2866 Py_DECREF(owner);
2867 SET_TOP(res);
2868 if (res == NULL)
2869 goto error;
2870 DISPATCH();
2871 }
2872
2873 TARGET(COMPARE_OP) {
2874 PyObject *right = POP();
2875 PyObject *left = TOP();
2876 PyObject *res = cmp_outcome(oparg, left, right);
2877 Py_DECREF(left);
2878 Py_DECREF(right);
2879 SET_TOP(res);
2880 if (res == NULL)
2881 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002882 PREDICT(POP_JUMP_IF_FALSE);
2883 PREDICT(POP_JUMP_IF_TRUE);
2884 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002885 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002886
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002887 TARGET(IMPORT_NAME) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002888 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002889 PyObject *fromlist = POP();
2890 PyObject *level = TOP();
2891 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002892 res = import_name(f, name, fromlist, level);
2893 Py_DECREF(level);
2894 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002895 SET_TOP(res);
2896 if (res == NULL)
2897 goto error;
2898 DISPATCH();
2899 }
2900
2901 TARGET(IMPORT_STAR) {
2902 PyObject *from = POP(), *locals;
2903 int err;
Berker Peksag7accf202017-02-27 20:41:21 +03002904 if (PyFrame_FastToLocalsWithError(f) < 0) {
2905 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002906 goto error;
Berker Peksag7accf202017-02-27 20:41:21 +03002907 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002908
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002909 locals = f->f_locals;
2910 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002911 PyErr_SetString(PyExc_SystemError,
2912 "no locals found during 'import *'");
Berker Peksag7accf202017-02-27 20:41:21 +03002913 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002914 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002915 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002916 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002918 Py_DECREF(from);
2919 if (err != 0)
2920 goto error;
2921 DISPATCH();
2922 }
Guido van Rossum25831651993-05-19 14:50:45 +00002923
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002924 TARGET(IMPORT_FROM) {
2925 PyObject *name = GETITEM(names, oparg);
2926 PyObject *from = TOP();
2927 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002928 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002929 PUSH(res);
2930 if (res == NULL)
2931 goto error;
2932 DISPATCH();
2933 }
Thomas Wouters52152252000-08-17 22:55:00 +00002934
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002935 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 JUMPBY(oparg);
2937 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002938 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002939
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002940 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002941 TARGET(POP_JUMP_IF_FALSE) {
2942 PyObject *cond = POP();
2943 int err;
2944 if (cond == Py_True) {
2945 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 FAST_DISPATCH();
2947 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002948 if (cond == Py_False) {
2949 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002950 JUMPTO(oparg);
2951 FAST_DISPATCH();
2952 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002953 err = PyObject_IsTrue(cond);
2954 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002955 if (err > 0)
2956 err = 0;
2957 else if (err == 0)
2958 JUMPTO(oparg);
2959 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002960 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002961 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002962 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002963
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002964 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002965 TARGET(POP_JUMP_IF_TRUE) {
2966 PyObject *cond = POP();
2967 int err;
2968 if (cond == Py_False) {
2969 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002970 FAST_DISPATCH();
2971 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002972 if (cond == Py_True) {
2973 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002974 JUMPTO(oparg);
2975 FAST_DISPATCH();
2976 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002977 err = PyObject_IsTrue(cond);
2978 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002979 if (err > 0) {
2980 err = 0;
2981 JUMPTO(oparg);
2982 }
2983 else if (err == 0)
2984 ;
2985 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002986 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002987 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002988 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002989
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002990 TARGET(JUMP_IF_FALSE_OR_POP) {
2991 PyObject *cond = TOP();
2992 int err;
2993 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002994 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002995 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 FAST_DISPATCH();
2997 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002998 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002999 JUMPTO(oparg);
3000 FAST_DISPATCH();
3001 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003002 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003003 if (err > 0) {
3004 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003005 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003006 err = 0;
3007 }
3008 else if (err == 0)
3009 JUMPTO(oparg);
3010 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003011 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003012 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003013 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003014
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003015 TARGET(JUMP_IF_TRUE_OR_POP) {
3016 PyObject *cond = TOP();
3017 int err;
3018 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003019 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003020 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003021 FAST_DISPATCH();
3022 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003023 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003024 JUMPTO(oparg);
3025 FAST_DISPATCH();
3026 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003027 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003028 if (err > 0) {
3029 err = 0;
3030 JUMPTO(oparg);
3031 }
3032 else if (err == 0) {
3033 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003034 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003035 }
3036 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003037 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003038 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003039 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003040
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003041 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003042 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003043 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003044#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003045 /* Enabling this path speeds-up all while and for-loops by bypassing
3046 the per-loop checks for signals. By default, this should be turned-off
3047 because it prevents detection of a control-break in tight loops like
3048 "while 1: pass". Compile with this option turned-on when you need
3049 the speed-up and do not need break checking inside tight loops (ones
3050 that contain only instructions ending with FAST_DISPATCH).
3051 */
3052 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003053#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003054 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003055#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003056 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003057
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003058 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003059 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003060 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003061 PyObject *iter = PyObject_GetIter(iterable);
3062 Py_DECREF(iterable);
3063 SET_TOP(iter);
3064 if (iter == NULL)
3065 goto error;
3066 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003067 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003068 DISPATCH();
3069 }
3070
3071 TARGET(GET_YIELD_FROM_ITER) {
3072 /* before: [obj]; after [getiter(obj)] */
3073 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003074 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003075 if (PyCoro_CheckExact(iterable)) {
3076 /* `iterable` is a coroutine */
3077 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3078 /* and it is used in a 'yield from' expression of a
3079 regular generator. */
3080 Py_DECREF(iterable);
3081 SET_TOP(NULL);
3082 PyErr_SetString(PyExc_TypeError,
3083 "cannot 'yield from' a coroutine object "
3084 "in a non-coroutine generator");
3085 goto error;
3086 }
3087 }
3088 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003089 /* `iterable` is not a generator. */
3090 iter = PyObject_GetIter(iterable);
3091 Py_DECREF(iterable);
3092 SET_TOP(iter);
3093 if (iter == NULL)
3094 goto error;
3095 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003096 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003097 DISPATCH();
3098 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003099
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003100 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003101 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003102 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003103 PyObject *iter = TOP();
3104 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3105 if (next != NULL) {
3106 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003107 PREDICT(STORE_FAST);
3108 PREDICT(UNPACK_SEQUENCE);
3109 DISPATCH();
3110 }
3111 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003112 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
3113 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08003114 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003115 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003116 PyErr_Clear();
3117 }
3118 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003119 STACKADJ(-1);
3120 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003121 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003122 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003123 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003124 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003125
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003126 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003127 why = WHY_BREAK;
3128 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003129 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003130
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003131 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003132 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003133 if (retval == NULL)
3134 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003135 why = WHY_CONTINUE;
3136 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003137 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003138
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003139 TARGET(SETUP_LOOP)
3140 TARGET(SETUP_EXCEPT)
3141 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003142 /* NOTE: If you add any new block-setup opcodes that
3143 are not try/except/finally handlers, you may need
3144 to update the PyGen_NeedsFinalizing() function.
3145 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003147 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
3148 STACK_LEVEL());
3149 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003150 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003151
Yury Selivanov75445082015-05-11 22:57:16 -04003152 TARGET(BEFORE_ASYNC_WITH) {
3153 _Py_IDENTIFIER(__aexit__);
3154 _Py_IDENTIFIER(__aenter__);
3155
3156 PyObject *mgr = TOP();
3157 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3158 *enter;
3159 PyObject *res;
3160 if (exit == NULL)
3161 goto error;
3162 SET_TOP(exit);
3163 enter = special_lookup(mgr, &PyId___aenter__);
3164 Py_DECREF(mgr);
3165 if (enter == NULL)
3166 goto error;
3167 res = PyObject_CallFunctionObjArgs(enter, NULL);
3168 Py_DECREF(enter);
3169 if (res == NULL)
3170 goto error;
3171 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003172 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003173 DISPATCH();
3174 }
3175
3176 TARGET(SETUP_ASYNC_WITH) {
3177 PyObject *res = POP();
3178 /* Setup the finally block before pushing the result
3179 of __aenter__ on the stack. */
3180 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3181 STACK_LEVEL());
3182 PUSH(res);
3183 DISPATCH();
3184 }
3185
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003186 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003187 _Py_IDENTIFIER(__exit__);
3188 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003189 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08003190 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003191 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08003192 if (enter == NULL)
3193 goto error;
3194 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003195 if (exit == NULL) {
3196 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003197 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003198 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003199 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003200 Py_DECREF(mgr);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003201 res = PyObject_CallFunctionObjArgs(enter, NULL);
3202 Py_DECREF(enter);
3203 if (res == NULL)
3204 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003205 /* Setup the finally block before pushing the result
3206 of __enter__ on the stack. */
3207 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3208 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003209
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003210 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003211 DISPATCH();
3212 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003213
Yury Selivanov75445082015-05-11 22:57:16 -04003214 TARGET(WITH_CLEANUP_START) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04003215 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003216 how/why we entered the finally clause:
3217 - TOP = None
3218 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
3219 - TOP = WHY_*; no retval below it
3220 - (TOP, SECOND, THIRD) = exc_info()
3221 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
3222 Below them is EXIT, the context.__exit__ bound method.
3223 In the last case, we must call
3224 EXIT(TOP, SECOND, THIRD)
3225 otherwise we must call
3226 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003227
Benjamin Peterson8f169482013-10-29 22:25:06 -04003228 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003229 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04003230 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003231 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003233 In addition, if the stack represents an exception,
3234 *and* the function call returns a 'true' value, we
3235 push WHY_SILENCED onto the stack. END_FINALLY will
3236 then not re-raise the exception. (But non-local
3237 gotos should still be resumed.)
3238 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003240 PyObject *exit_func;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003241 PyObject *exc = TOP(), *val = Py_None, *tb = Py_None, *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003242 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003243 (void)POP();
3244 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003245 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003246 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003247 else if (PyLong_Check(exc)) {
3248 STACKADJ(-1);
3249 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003250 case WHY_RETURN:
3251 case WHY_CONTINUE:
3252 /* Retval in TOP. */
3253 exit_func = SECOND();
3254 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003255 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003256 break;
3257 default:
3258 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003259 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003260 break;
3261 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003262 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003263 }
3264 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003265 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003266 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003267 val = SECOND();
3268 tb = THIRD();
3269 tp2 = FOURTH();
3270 exc2 = PEEK(5);
3271 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003272 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003273 SET_VALUE(7, tb2);
3274 SET_VALUE(6, exc2);
3275 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003276 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3277 SET_FOURTH(NULL);
3278 /* We just shifted the stack down, so we have
3279 to tell the except handler block that the
3280 values are lower than it expects. */
3281 block = &f->f_blockstack[f->f_iblock - 1];
3282 assert(block->b_type == EXCEPT_HANDLER);
3283 block->b_level--;
3284 }
3285 /* XXX Not the fastest way to call it... */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003286 res = PyObject_CallFunctionObjArgs(exit_func, exc, val, tb, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003287 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003288 if (res == NULL)
3289 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003290
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003291 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003292 PUSH(exc);
3293 PUSH(res);
3294 PREDICT(WITH_CLEANUP_FINISH);
3295 DISPATCH();
3296 }
3297
3298 PREDICTED(WITH_CLEANUP_FINISH);
3299 TARGET(WITH_CLEANUP_FINISH) {
3300 PyObject *res = POP();
3301 PyObject *exc = POP();
3302 int err;
3303
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003304 if (exc != Py_None)
3305 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003306 else
3307 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003308
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003309 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003310 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003312 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003313 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003314 else if (err > 0) {
3315 err = 0;
3316 /* There was an exception and a True return */
3317 PUSH(PyLong_FromLong((long) WHY_SILENCED));
3318 }
3319 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003320 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003321 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003322
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003323 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003324 TARGET(CALL_FUNCTION) {
3325 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003326 PCALL(PCALL_ALL);
3327 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003328 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003329 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003330 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003331 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003332 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003333 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003334 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003335 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003336
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003337 TARGET(CALL_FUNCTION_KW) {
3338 PyObject **sp, *res, *names;
3339
3340 names = POP();
3341 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003342 PCALL(PCALL_ALL);
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;
3357 PCALL(PCALL_ALL);
3358 if (oparg & 0x01) {
3359 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003360 if (!PyDict_CheckExact(kwargs)) {
3361 PyObject *d = PyDict_New();
3362 if (d == NULL)
3363 goto error;
3364 if (PyDict_Update(d, kwargs) != 0) {
3365 Py_DECREF(d);
3366 /* PyDict_Update raises attribute
3367 * error (percolated from an attempt
3368 * to get 'keys' attribute) instead of
3369 * a type error if its second argument
3370 * is not a mapping.
3371 */
3372 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3373 func = SECOND();
3374 PyErr_Format(PyExc_TypeError,
3375 "%.200s%.200s argument after ** "
3376 "must be a mapping, not %.200s",
3377 PyEval_GetFuncName(func),
3378 PyEval_GetFuncDesc(func),
3379 kwargs->ob_type->tp_name);
3380 }
Victor Stinnereece2222016-09-12 11:16:37 +02003381 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003382 goto error;
3383 }
3384 Py_DECREF(kwargs);
3385 kwargs = d;
3386 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003387 assert(PyDict_CheckExact(kwargs));
3388 }
3389 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003390 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003391 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003392 if (Py_TYPE(callargs)->tp_iter == NULL &&
3393 !PySequence_Check(callargs)) {
3394 PyErr_Format(PyExc_TypeError,
3395 "%.200s%.200s argument after * "
3396 "must be an iterable, not %.200s",
3397 PyEval_GetFuncName(func),
3398 PyEval_GetFuncDesc(func),
3399 callargs->ob_type->tp_name);
Victor Stinnereece2222016-09-12 11:16:37 +02003400 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003401 goto error;
3402 }
3403 Py_SETREF(callargs, PySequence_Tuple(callargs));
3404 if (callargs == NULL) {
3405 goto error;
3406 }
3407 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003408 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003409
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003410 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003411 Py_DECREF(func);
3412 Py_DECREF(callargs);
3413 Py_XDECREF(kwargs);
3414
3415 SET_TOP(result);
3416 if (result == NULL) {
3417 goto error;
3418 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003419 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003420 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003421
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003422 TARGET(MAKE_FUNCTION) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003423 PyObject *qualname = POP();
3424 PyObject *codeobj = POP();
3425 PyFunctionObject *func = (PyFunctionObject *)
3426 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003427
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003428 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003429 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003430 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003431 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003432 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003433
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003434 if (oparg & 0x08) {
3435 assert(PyTuple_CheckExact(TOP()));
3436 func ->func_closure = POP();
3437 }
3438 if (oparg & 0x04) {
3439 assert(PyDict_CheckExact(TOP()));
3440 func->func_annotations = POP();
3441 }
3442 if (oparg & 0x02) {
3443 assert(PyDict_CheckExact(TOP()));
3444 func->func_kwdefaults = POP();
3445 }
3446 if (oparg & 0x01) {
3447 assert(PyTuple_CheckExact(TOP()));
3448 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003449 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003450
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003451 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003452 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003453 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003454
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003455 TARGET(BUILD_SLICE) {
3456 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003457 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003458 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003459 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003460 step = NULL;
3461 stop = POP();
3462 start = TOP();
3463 slice = PySlice_New(start, stop, step);
3464 Py_DECREF(start);
3465 Py_DECREF(stop);
3466 Py_XDECREF(step);
3467 SET_TOP(slice);
3468 if (slice == NULL)
3469 goto error;
3470 DISPATCH();
3471 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003472
Eric V. Smitha78c7952015-11-03 12:45:05 -05003473 TARGET(FORMAT_VALUE) {
3474 /* Handles f-string value formatting. */
3475 PyObject *result;
3476 PyObject *fmt_spec;
3477 PyObject *value;
3478 PyObject *(*conv_fn)(PyObject *);
3479 int which_conversion = oparg & FVC_MASK;
3480 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3481
3482 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003483 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003484
3485 /* See if any conversion is specified. */
3486 switch (which_conversion) {
3487 case FVC_STR: conv_fn = PyObject_Str; break;
3488 case FVC_REPR: conv_fn = PyObject_Repr; break;
3489 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3490
3491 /* Must be 0 (meaning no conversion), since only four
3492 values are allowed by (oparg & FVC_MASK). */
3493 default: conv_fn = NULL; break;
3494 }
3495
3496 /* If there's a conversion function, call it and replace
3497 value with that result. Otherwise, just use value,
3498 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003499 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003500 result = conv_fn(value);
3501 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003502 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003503 Py_XDECREF(fmt_spec);
3504 goto error;
3505 }
3506 value = result;
3507 }
3508
3509 /* If value is a unicode object, and there's no fmt_spec,
3510 then we know the result of format(value) is value
3511 itself. In that case, skip calling format(). I plan to
3512 move this optimization in to PyObject_Format()
3513 itself. */
3514 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3515 /* Do nothing, just transfer ownership to result. */
3516 result = value;
3517 } else {
3518 /* Actually call format(). */
3519 result = PyObject_Format(value, fmt_spec);
3520 Py_DECREF(value);
3521 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003522 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003523 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003524 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003525 }
3526
Eric V. Smith135d5f42016-02-05 18:23:08 -05003527 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003528 DISPATCH();
3529 }
3530
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003531 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003532 int oldoparg = oparg;
3533 NEXTOPARG();
3534 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003535 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003536 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003537
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003538
Antoine Pitrou042b1282010-08-13 21:15:58 +00003539#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003540 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003541#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003542 default:
3543 fprintf(stderr,
3544 "XXX lineno: %d, opcode: %d\n",
3545 PyFrame_GetLineNumber(f),
3546 opcode);
3547 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003548 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003549
3550#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003551 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003552#endif
3553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003554 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003555
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003556 /* This should never be reached. Every opcode should end with DISPATCH()
3557 or goto error. */
3558 assert(0);
Guido van Rossumac7be682001-01-17 15:42:30 +00003559
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003560error:
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003561
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003562 assert(why == WHY_NOT);
3563 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003564
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003565 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003566#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003567 if (!PyErr_Occurred())
3568 PyErr_SetString(PyExc_SystemError,
3569 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003570#else
3571 assert(PyErr_Occurred());
3572#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003573
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003574 /* Log traceback info. */
3575 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003576
Benjamin Peterson51f46162013-01-23 08:38:47 -05003577 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003578 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3579 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003580
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003581fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003582 assert(why != WHY_NOT);
3583
3584 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003585 while (why != WHY_NOT && f->f_iblock > 0) {
3586 /* Peek at the current block. */
3587 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003589 assert(why != WHY_YIELD);
3590 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3591 why = WHY_NOT;
3592 JUMPTO(PyLong_AS_LONG(retval));
3593 Py_DECREF(retval);
3594 break;
3595 }
3596 /* Now we have to pop the block. */
3597 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003599 if (b->b_type == EXCEPT_HANDLER) {
3600 UNWIND_EXCEPT_HANDLER(b);
3601 continue;
3602 }
3603 UNWIND_BLOCK(b);
3604 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3605 why = WHY_NOT;
3606 JUMPTO(b->b_handler);
3607 break;
3608 }
3609 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3610 || b->b_type == SETUP_FINALLY)) {
3611 PyObject *exc, *val, *tb;
3612 int handler = b->b_handler;
3613 /* Beware, this invalidates all b->b_* fields */
3614 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3615 PUSH(tstate->exc_traceback);
3616 PUSH(tstate->exc_value);
3617 if (tstate->exc_type != NULL) {
3618 PUSH(tstate->exc_type);
3619 }
3620 else {
3621 Py_INCREF(Py_None);
3622 PUSH(Py_None);
3623 }
3624 PyErr_Fetch(&exc, &val, &tb);
3625 /* Make the raw exception data
3626 available to the handler,
3627 so a program can emulate the
3628 Python main loop. */
3629 PyErr_NormalizeException(
3630 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003631 if (tb != NULL)
3632 PyException_SetTraceback(val, tb);
3633 else
3634 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003635 Py_INCREF(exc);
3636 tstate->exc_type = exc;
3637 Py_INCREF(val);
3638 tstate->exc_value = val;
3639 tstate->exc_traceback = tb;
3640 if (tb == NULL)
3641 tb = Py_None;
3642 Py_INCREF(tb);
3643 PUSH(tb);
3644 PUSH(val);
3645 PUSH(exc);
3646 why = WHY_NOT;
3647 JUMPTO(handler);
3648 break;
3649 }
3650 if (b->b_type == SETUP_FINALLY) {
3651 if (why & (WHY_RETURN | WHY_CONTINUE))
3652 PUSH(retval);
3653 PUSH(PyLong_FromLong((long)why));
3654 why = WHY_NOT;
3655 JUMPTO(b->b_handler);
3656 break;
3657 }
3658 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003660 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003662 if (why != WHY_NOT)
3663 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00003664
Victor Stinnerace47d72013-07-18 01:41:08 +02003665 assert(!PyErr_Occurred());
3666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003667 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003669 assert(why != WHY_YIELD);
3670 /* Pop remaining stack entries. */
3671 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003672 PyObject *o = POP();
3673 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003674 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003676 if (why != WHY_RETURN)
3677 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003678
Victor Stinner4a7cc882015-03-06 23:35:27 +01003679 assert((retval != NULL) ^ (PyErr_Occurred() != NULL));
Victor Stinnerace47d72013-07-18 01:41:08 +02003680
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003681fast_yield:
Yury Selivanoveb636452016-09-08 22:01:51 -07003682 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01003683
Benjamin Petersonac913412011-07-03 16:25:11 -05003684 /* The purpose of this block is to put aside the generator's exception
3685 state and restore that of the calling frame. If the current
3686 exception state is from the caller, we clear the exception values
3687 on the generator frame, so they are not swapped back in latter. The
3688 origin of the current exception state is determined by checking for
3689 except handler blocks, which we must be in iff a new exception
3690 state came into existence in this frame. (An uncaught exception
3691 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3692 int i;
Victor Stinner74319ae2016-08-25 00:04:09 +02003693 for (i = 0; i < f->f_iblock; i++) {
3694 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER) {
Benjamin Petersonac913412011-07-03 16:25:11 -05003695 break;
Victor Stinner74319ae2016-08-25 00:04:09 +02003696 }
3697 }
Benjamin Petersonac913412011-07-03 16:25:11 -05003698 if (i == f->f_iblock)
3699 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003700 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003701 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003702 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003703 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003705 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003706 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003707 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003708 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3709 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003710 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003711 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003712 why = WHY_EXCEPTION;
3713 }
3714 }
3715 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003716 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3717 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003718 PyTrace_RETURN, NULL);
3719 }
3720 }
3721 if (tstate->c_profilefunc) {
3722 if (why == WHY_EXCEPTION)
3723 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003724 tstate->c_profileobj,
3725 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003726 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003727 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3728 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003729 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003730 Py_CLEAR(retval);
Brett Cannonb94767f2011-02-22 20:15:44 +00003731 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003732 }
3733 }
3734 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003736 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003737exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003738 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3739 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003740 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003741 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003742 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003743
Victor Stinnerefde1462015-03-21 15:04:43 +01003744 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003745}
3746
Benjamin Petersonb204a422011-06-05 22:04:07 -05003747static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003748format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3749{
3750 int err;
3751 Py_ssize_t len = PyList_GET_SIZE(names);
3752 PyObject *name_str, *comma, *tail, *tmp;
3753
3754 assert(PyList_CheckExact(names));
3755 assert(len >= 1);
3756 /* Deal with the joys of natural language. */
3757 switch (len) {
3758 case 1:
3759 name_str = PyList_GET_ITEM(names, 0);
3760 Py_INCREF(name_str);
3761 break;
3762 case 2:
3763 name_str = PyUnicode_FromFormat("%U and %U",
3764 PyList_GET_ITEM(names, len - 2),
3765 PyList_GET_ITEM(names, len - 1));
3766 break;
3767 default:
3768 tail = PyUnicode_FromFormat(", %U, and %U",
3769 PyList_GET_ITEM(names, len - 2),
3770 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003771 if (tail == NULL)
3772 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003773 /* Chop off the last two objects in the list. This shouldn't actually
3774 fail, but we can't be too careful. */
3775 err = PyList_SetSlice(names, len - 2, len, NULL);
3776 if (err == -1) {
3777 Py_DECREF(tail);
3778 return;
3779 }
3780 /* Stitch everything up into a nice comma-separated list. */
3781 comma = PyUnicode_FromString(", ");
3782 if (comma == NULL) {
3783 Py_DECREF(tail);
3784 return;
3785 }
3786 tmp = PyUnicode_Join(comma, names);
3787 Py_DECREF(comma);
3788 if (tmp == NULL) {
3789 Py_DECREF(tail);
3790 return;
3791 }
3792 name_str = PyUnicode_Concat(tmp, tail);
3793 Py_DECREF(tmp);
3794 Py_DECREF(tail);
3795 break;
3796 }
3797 if (name_str == NULL)
3798 return;
3799 PyErr_Format(PyExc_TypeError,
3800 "%U() missing %i required %s argument%s: %U",
3801 co->co_name,
3802 len,
3803 kind,
3804 len == 1 ? "" : "s",
3805 name_str);
3806 Py_DECREF(name_str);
3807}
3808
3809static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003810missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003811 PyObject **fastlocals)
3812{
Victor Stinner74319ae2016-08-25 00:04:09 +02003813 Py_ssize_t i, j = 0;
3814 Py_ssize_t start, end;
3815 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003816 const char *kind = positional ? "positional" : "keyword-only";
3817 PyObject *missing_names;
3818
3819 /* Compute the names of the arguments that are missing. */
3820 missing_names = PyList_New(missing);
3821 if (missing_names == NULL)
3822 return;
3823 if (positional) {
3824 start = 0;
3825 end = co->co_argcount - defcount;
3826 }
3827 else {
3828 start = co->co_argcount;
3829 end = start + co->co_kwonlyargcount;
3830 }
3831 for (i = start; i < end; i++) {
3832 if (GETLOCAL(i) == NULL) {
3833 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3834 PyObject *name = PyObject_Repr(raw);
3835 if (name == NULL) {
3836 Py_DECREF(missing_names);
3837 return;
3838 }
3839 PyList_SET_ITEM(missing_names, j++, name);
3840 }
3841 }
3842 assert(j == missing);
3843 format_missing(kind, co, missing_names);
3844 Py_DECREF(missing_names);
3845}
3846
3847static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003848too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3849 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003850{
3851 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003852 Py_ssize_t kwonly_given = 0;
3853 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003854 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003855 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003856
Benjamin Petersone109c702011-06-24 09:37:26 -05003857 assert((co->co_flags & CO_VARARGS) == 0);
3858 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003859 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3860 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003861 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003862 }
3863 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003864 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003865 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003866 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003867 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003868 }
3869 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003870 plural = (co_argcount != 1);
3871 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003872 }
3873 if (sig == NULL)
3874 return;
3875 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003876 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3877 kwonly_sig = PyUnicode_FromFormat(format,
3878 given != 1 ? "s" : "",
3879 kwonly_given,
3880 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003881 if (kwonly_sig == NULL) {
3882 Py_DECREF(sig);
3883 return;
3884 }
3885 }
3886 else {
3887 /* This will not fail. */
3888 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003889 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003890 }
3891 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003892 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003893 co->co_name,
3894 sig,
3895 plural ? "s" : "",
3896 given,
3897 kwonly_sig,
3898 given == 1 && !kwonly_given ? "was" : "were");
3899 Py_DECREF(sig);
3900 Py_DECREF(kwonly_sig);
3901}
3902
Guido van Rossumc2e20742006-02-27 22:32:47 +00003903/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003904 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003905 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003906
Victor Stinner40ee3012014-06-16 15:59:28 +02003907static PyObject *
3908_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Victor Stinner74319ae2016-08-25 00:04:09 +02003909 PyObject **args, Py_ssize_t argcount,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003910 PyObject **kwnames, PyObject **kwargs,
3911 Py_ssize_t kwcount, int kwstep,
Victor Stinner74319ae2016-08-25 00:04:09 +02003912 PyObject **defs, Py_ssize_t defcount,
3913 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003914 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003915{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003916 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003917 PyFrameObject *f;
3918 PyObject *retval = NULL;
3919 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003920 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003921 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003922 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3923 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003924 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003926 if (globals == NULL) {
3927 PyErr_SetString(PyExc_SystemError,
3928 "PyEval_EvalCodeEx: NULL globals");
3929 return NULL;
3930 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003931
Victor Stinnerc7020012016-08-16 23:40:29 +02003932 /* Create the frame */
3933 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003934 assert(tstate != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003935 f = PyFrame_New(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003936 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003937 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003938 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003939 fastlocals = f->f_localsplus;
3940 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003941
Victor Stinnerc7020012016-08-16 23:40:29 +02003942 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003943 if (co->co_flags & CO_VARKEYWORDS) {
3944 kwdict = PyDict_New();
3945 if (kwdict == NULL)
3946 goto fail;
3947 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003948 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003949 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003950 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003951 SETLOCAL(i, kwdict);
3952 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003953 else {
3954 kwdict = NULL;
3955 }
3956
3957 /* Copy positional arguments into local variables */
3958 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003959 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003960 }
3961 else {
3962 n = argcount;
3963 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003964 for (i = 0; i < n; i++) {
3965 x = args[i];
3966 Py_INCREF(x);
3967 SETLOCAL(i, x);
3968 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003969
3970 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003971 if (co->co_flags & CO_VARARGS) {
3972 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003973 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003974 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003975 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003976 SETLOCAL(total_args, u);
3977 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003978 x = args[i];
3979 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003980 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003981 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003982 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003983
Serhiy Storchakab7281052016-09-12 00:52:40 +03003984 /* Handle keyword arguments passed as two strided arrays */
3985 kwcount *= kwstep;
3986 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003987 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003988 PyObject *keyword = kwnames[i];
3989 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003990 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003991
Benjamin Petersonb204a422011-06-05 22:04:07 -05003992 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3993 PyErr_Format(PyExc_TypeError,
3994 "%U() keywords must be strings",
3995 co->co_name);
3996 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003997 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003998
Benjamin Petersonb204a422011-06-05 22:04:07 -05003999 /* Speed hack: do raw pointer compares. As names are
4000 normally interned this should almost always hit. */
4001 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
4002 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004003 PyObject *name = co_varnames[j];
4004 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004005 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004006 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004007 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004008
Benjamin Petersonb204a422011-06-05 22:04:07 -05004009 /* Slow fallback, just in case */
4010 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004011 PyObject *name = co_varnames[j];
4012 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4013 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004014 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004015 }
4016 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004017 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004018 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004019 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004020
Benjamin Petersonb204a422011-06-05 22:04:07 -05004021 if (j >= total_args && kwdict == NULL) {
4022 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02004023 "%U() got an unexpected keyword argument '%S'",
4024 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004025 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004026 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004027
Christian Heimes0bd447f2013-07-20 14:48:10 +02004028 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4029 goto fail;
4030 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004031 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004032
Benjamin Petersonb204a422011-06-05 22:04:07 -05004033 kw_found:
4034 if (GETLOCAL(j) != NULL) {
4035 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02004036 "%U() got multiple values for argument '%S'",
4037 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004038 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004039 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004040 Py_INCREF(value);
4041 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004042 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004043
4044 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004045 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004046 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004047 goto fail;
4048 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004049
4050 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004051 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02004052 Py_ssize_t m = co->co_argcount - defcount;
4053 Py_ssize_t missing = 0;
4054 for (i = argcount; i < m; i++) {
4055 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004056 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004057 }
4058 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004059 if (missing) {
4060 missing_arguments(co, missing, defcount, fastlocals);
4061 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004062 }
4063 if (n > m)
4064 i = n - m;
4065 else
4066 i = 0;
4067 for (; i < defcount; i++) {
4068 if (GETLOCAL(m+i) == NULL) {
4069 PyObject *def = defs[i];
4070 Py_INCREF(def);
4071 SETLOCAL(m+i, def);
4072 }
4073 }
4074 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004075
4076 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004077 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004078 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004079 for (i = co->co_argcount; i < total_args; i++) {
4080 PyObject *name;
4081 if (GETLOCAL(i) != NULL)
4082 continue;
4083 name = PyTuple_GET_ITEM(co->co_varnames, i);
4084 if (kwdefs != NULL) {
4085 PyObject *def = PyDict_GetItem(kwdefs, name);
4086 if (def) {
4087 Py_INCREF(def);
4088 SETLOCAL(i, def);
4089 continue;
4090 }
4091 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004092 missing++;
4093 }
4094 if (missing) {
4095 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004096 goto fail;
4097 }
4098 }
4099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004100 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004101 vars into frame. */
4102 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004103 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05004104 int arg;
4105 /* Possibly account for the cell variable being an argument. */
4106 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004107 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004108 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004109 /* Clear the local copy. */
4110 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004111 }
4112 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004113 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004114 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004115 if (c == NULL)
4116 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004117 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004118 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004119
4120 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004121 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4122 PyObject *o = PyTuple_GET_ITEM(closure, i);
4123 Py_INCREF(o);
4124 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004125 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004126
Yury Selivanoveb636452016-09-08 22:01:51 -07004127 /* Handle generator/coroutine/asynchronous generator */
4128 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004129 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04004130 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004131 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004132
4133 if (is_coro && tstate->in_coroutine_wrapper) {
4134 assert(coro_wrapper != NULL);
4135 PyErr_Format(PyExc_RuntimeError,
4136 "coroutine wrapper %.200R attempted "
4137 "to recursively wrap %.200R",
4138 coro_wrapper,
4139 co);
4140 goto fail;
4141 }
Yury Selivanov75445082015-05-11 22:57:16 -04004142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004143 /* Don't need to keep the reference to f_back, it will be set
4144 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004145 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004147 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004149 /* Create a new generator that owns the ready to run frame
4150 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004151 if (is_coro) {
4152 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004153 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4154 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004155 } else {
4156 gen = PyGen_NewWithQualName(f, name, qualname);
4157 }
Yury Selivanov75445082015-05-11 22:57:16 -04004158 if (gen == NULL)
4159 return NULL;
4160
Yury Selivanov94c22632015-06-04 10:16:51 -04004161 if (is_coro && coro_wrapper != NULL) {
4162 PyObject *wrapped;
4163 tstate->in_coroutine_wrapper = 1;
4164 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4165 tstate->in_coroutine_wrapper = 0;
4166 return wrapped;
4167 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004168
Yury Selivanov75445082015-05-11 22:57:16 -04004169 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004170 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004172 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004173
Thomas Woutersce272b62007-09-19 21:19:28 +00004174fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004176 /* decref'ing the frame can cause __del__ methods to get invoked,
4177 which can call back into Python. While we're done with the
4178 current Python frame (f), the associated C stack is still in use,
4179 so recursion_depth must be boosted for the duration.
4180 */
4181 assert(tstate != NULL);
4182 ++tstate->recursion_depth;
4183 Py_DECREF(f);
4184 --tstate->recursion_depth;
4185 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004186}
4187
Victor Stinner40ee3012014-06-16 15:59:28 +02004188PyObject *
4189PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
4190 PyObject **args, int argcount, PyObject **kws, int kwcount,
4191 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
4192{
4193 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004194 args, argcount,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004195 kws, kws + 1, kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004196 defs, defcount,
4197 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004198 NULL, NULL);
4199}
Tim Peters5ca576e2001-06-18 22:08:13 +00004200
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004201static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004202special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004204 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004205 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004206 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004207 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004208 return NULL;
4209 }
4210 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004211}
4212
4213
Benjamin Peterson87880242011-07-03 16:48:31 -05004214/* These 3 functions deal with the exception state of generators. */
4215
4216static void
4217save_exc_state(PyThreadState *tstate, PyFrameObject *f)
4218{
4219 PyObject *type, *value, *traceback;
4220 Py_XINCREF(tstate->exc_type);
4221 Py_XINCREF(tstate->exc_value);
4222 Py_XINCREF(tstate->exc_traceback);
4223 type = f->f_exc_type;
4224 value = f->f_exc_value;
4225 traceback = f->f_exc_traceback;
4226 f->f_exc_type = tstate->exc_type;
4227 f->f_exc_value = tstate->exc_value;
4228 f->f_exc_traceback = tstate->exc_traceback;
4229 Py_XDECREF(type);
4230 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004231 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05004232}
4233
4234static void
4235swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
4236{
4237 PyObject *tmp;
4238 tmp = tstate->exc_type;
4239 tstate->exc_type = f->f_exc_type;
4240 f->f_exc_type = tmp;
4241 tmp = tstate->exc_value;
4242 tstate->exc_value = f->f_exc_value;
4243 f->f_exc_value = tmp;
4244 tmp = tstate->exc_traceback;
4245 tstate->exc_traceback = f->f_exc_traceback;
4246 f->f_exc_traceback = tmp;
4247}
4248
4249static void
4250restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
4251{
4252 PyObject *type, *value, *tb;
4253 type = tstate->exc_type;
4254 value = tstate->exc_value;
4255 tb = tstate->exc_traceback;
4256 tstate->exc_type = f->f_exc_type;
4257 tstate->exc_value = f->f_exc_value;
4258 tstate->exc_traceback = f->f_exc_traceback;
4259 f->f_exc_type = NULL;
4260 f->f_exc_value = NULL;
4261 f->f_exc_traceback = NULL;
4262 Py_XDECREF(type);
4263 Py_XDECREF(value);
4264 Py_XDECREF(tb);
4265}
4266
4267
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004268/* Logic for the raise statement (too complicated for inlining).
4269 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004270static int
Collin Winter828f04a2007-08-31 00:04:24 +00004271do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004272{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004273 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004275 if (exc == NULL) {
4276 /* Reraise */
4277 PyThreadState *tstate = PyThreadState_GET();
4278 PyObject *tb;
4279 type = tstate->exc_type;
4280 value = tstate->exc_value;
4281 tb = tstate->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004282 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004283 PyErr_SetString(PyExc_RuntimeError,
4284 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004285 return 0;
4286 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004287 Py_XINCREF(type);
4288 Py_XINCREF(value);
4289 Py_XINCREF(tb);
4290 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004291 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004292 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004294 /* We support the following forms of raise:
4295 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004296 raise <instance>
4297 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004299 if (PyExceptionClass_Check(exc)) {
4300 type = exc;
4301 value = PyObject_CallObject(exc, NULL);
4302 if (value == NULL)
4303 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004304 if (!PyExceptionInstance_Check(value)) {
4305 PyErr_Format(PyExc_TypeError,
4306 "calling %R should have returned an instance of "
4307 "BaseException, not %R",
4308 type, Py_TYPE(value));
4309 goto raise_error;
4310 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004311 }
4312 else if (PyExceptionInstance_Check(exc)) {
4313 value = exc;
4314 type = PyExceptionInstance_Class(exc);
4315 Py_INCREF(type);
4316 }
4317 else {
4318 /* Not something you can raise. You get an exception
4319 anyway, just not what you specified :-) */
4320 Py_DECREF(exc);
4321 PyErr_SetString(PyExc_TypeError,
4322 "exceptions must derive from BaseException");
4323 goto raise_error;
4324 }
Collin Winter828f04a2007-08-31 00:04:24 +00004325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004326 if (cause) {
4327 PyObject *fixed_cause;
4328 if (PyExceptionClass_Check(cause)) {
4329 fixed_cause = PyObject_CallObject(cause, NULL);
4330 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 */
4352 Py_XDECREF(value);
4353 Py_XDECREF(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 Stinner8a31c822016-08-19 17:12:23 +02004737PyEval_CallObjectWithKeywords(PyObject *func, PyObject *args, PyObject *kwargs)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004738{
Victor Stinner59b356d2015-03-16 11:52:32 +01004739#ifdef Py_DEBUG
4740 /* PyEval_CallObjectWithKeywords() must not be called with an exception
4741 set. It raises a new exception if parameters are invalid or if
4742 PyTuple_New() fails, and so the original exception is lost. */
4743 assert(!PyErr_Occurred());
4744#endif
4745
INADA Naoki023532e2017-03-01 21:14:43 +09004746 if (args != NULL && !PyTuple_Check(args)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004747 PyErr_SetString(PyExc_TypeError,
4748 "argument list must be a tuple");
4749 return NULL;
4750 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00004751
Victor Stinner8a31c822016-08-19 17:12:23 +02004752 if (kwargs != NULL && !PyDict_Check(kwargs)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004753 PyErr_SetString(PyExc_TypeError,
4754 "keyword list must be a dictionary");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004755 return NULL;
4756 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00004757
INADA Naoki023532e2017-03-01 21:14:43 +09004758 if (args == NULL) {
4759 return _PyObject_FastCallDict(func, NULL, 0, kwargs);
4760 }
4761 else {
4762 return PyObject_Call(func, args, kwargs);
4763 }
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
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004823static PyObject *
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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004839 PCALL(PCALL_CFUNCTION);
Victor Stinner4a7cc882015-03-06 23:35:27 +01004840
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004841 stack = (*pp_stack) - nargs - nkwargs;
4842 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004843 }
4844 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004845 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4846 /* optimize access to bound methods */
4847 PyObject *self = PyMethod_GET_SELF(func);
4848 PCALL(PCALL_METHOD);
4849 PCALL(PCALL_BOUND_METHOD);
4850 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);
4881 PCALL(PCALL_POP);
4882 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004884 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004885}
4886
Victor Stinnere90bdb12016-08-25 23:26:50 +02004887/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004888 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004889 For the simplest case -- a function that takes only positional
4890 arguments and is called with only positional arguments -- it
4891 inlines the most primitive frame setup code from
4892 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4893 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004894*/
4895
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004896static PyObject*
Victor Stinnerd8735722016-09-09 12:36:44 -07004897_PyFunction_FastCall(PyCodeObject *co, PyObject **args, Py_ssize_t nargs,
4898 PyObject *globals)
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004899{
4900 PyFrameObject *f;
4901 PyThreadState *tstate = PyThreadState_GET();
4902 PyObject **fastlocals;
4903 Py_ssize_t i;
4904 PyObject *result;
4905
4906 PCALL(PCALL_FASTER_FUNCTION);
4907 assert(globals != NULL);
4908 /* XXX Perhaps we should create a specialized
4909 PyFrame_New() that doesn't take locals, but does
4910 take builtins without sanity checking them.
4911 */
4912 assert(tstate != NULL);
4913 f = PyFrame_New(tstate, co, globals, NULL);
4914 if (f == NULL) {
4915 return NULL;
4916 }
4917
4918 fastlocals = f->f_localsplus;
4919
Victor Stinner74319ae2016-08-25 00:04:09 +02004920 for (i = 0; i < nargs; i++) {
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004921 Py_INCREF(*args);
4922 fastlocals[i] = *args++;
4923 }
4924 result = PyEval_EvalFrameEx(f,0);
4925
4926 ++tstate->recursion_depth;
4927 Py_DECREF(f);
4928 --tstate->recursion_depth;
4929
4930 return result;
4931}
4932
Victor Stinnere90bdb12016-08-25 23:26:50 +02004933static PyObject *
Victor Stinnerd8735722016-09-09 12:36:44 -07004934fast_function(PyObject *func, PyObject **stack,
4935 Py_ssize_t nargs, PyObject *kwnames)
Jeremy Hylton52820442001-01-03 23:52:36 +00004936{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004937 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4938 PyObject *globals = PyFunction_GET_GLOBALS(func);
4939 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004940 PyObject *kwdefs, *closure, *name, *qualname;
4941 PyObject **d;
Victor Stinnerd8735722016-09-09 12:36:44 -07004942 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004943 Py_ssize_t nd;
Victor Stinnerd8735722016-09-09 12:36:44 -07004944
Victor Stinner57f91ac2016-09-12 13:37:07 +02004945 assert(PyFunction_Check(func));
4946 assert(nargs >= 0);
4947 assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
Victor Stinnerd8735722016-09-09 12:36:44 -07004948 assert((nargs == 0 && nkwargs == 0) || stack != NULL);
Victor Stinner57f91ac2016-09-12 13:37:07 +02004949 /* kwnames must only contains str strings, no subclass, and all keys must
4950 be unique */
Victor Stinner577e1f82016-08-25 00:29:32 +02004951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004952 PCALL(PCALL_FUNCTION);
4953 PCALL(PCALL_FAST_FUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004954
Victor Stinner74319ae2016-08-25 00:04:09 +02004955 if (co->co_kwonlyargcount == 0 && nkwargs == 0 &&
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004956 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
4957 {
Victor Stinner2eedc112016-08-22 12:29:42 +02004958 if (argdefs == NULL && co->co_argcount == nargs) {
Victor Stinnerd8735722016-09-09 12:36:44 -07004959 return _PyFunction_FastCall(co, stack, nargs, globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02004960 }
4961 else if (nargs == 0 && argdefs != NULL
4962 && co->co_argcount == Py_SIZE(argdefs)) {
4963 /* function called with no arguments, but all parameters have
4964 a default value: use default values as arguments .*/
4965 stack = &PyTuple_GET_ITEM(argdefs, 0);
Victor Stinnerd8735722016-09-09 12:36:44 -07004966 return _PyFunction_FastCall(co, stack, Py_SIZE(argdefs), globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02004967 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004968 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004969
4970 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
4971 closure = PyFunction_GET_CLOSURE(func);
4972 name = ((PyFunctionObject *)func) -> func_name;
4973 qualname = ((PyFunctionObject *)func) -> func_qualname;
4974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004975 if (argdefs != NULL) {
4976 d = &PyTuple_GET_ITEM(argdefs, 0);
4977 nd = Py_SIZE(argdefs);
4978 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004979 else {
4980 d = NULL;
4981 nd = 0;
4982 }
4983 return _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
Victor Stinner2eedc112016-08-22 12:29:42 +02004984 stack, nargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004985 nkwargs ? &PyTuple_GET_ITEM(kwnames, 0) : NULL,
4986 stack + nargs,
4987 nkwargs, 1,
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004988 d, (int)nd, kwdefs,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004989 closure, name, qualname);
4990}
4991
4992PyObject *
Victor Stinnerd8735722016-09-09 12:36:44 -07004993_PyFunction_FastCallKeywords(PyObject *func, PyObject **stack,
4994 Py_ssize_t nargs, PyObject *kwnames)
4995{
4996 return fast_function(func, stack, nargs, kwnames);
4997}
4998
4999PyObject *
Victor Stinner74319ae2016-08-25 00:04:09 +02005000_PyFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs,
Victor Stinnerb9009392016-08-22 23:15:44 +02005001 PyObject *kwargs)
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005002{
5003 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
5004 PyObject *globals = PyFunction_GET_GLOBALS(func);
5005 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
5006 PyObject *kwdefs, *closure, *name, *qualname;
Victor Stinnerb9009392016-08-22 23:15:44 +02005007 PyObject *kwtuple, **k;
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005008 PyObject **d;
Victor Stinner74319ae2016-08-25 00:04:09 +02005009 Py_ssize_t nd, nk;
Victor Stinnerb9009392016-08-22 23:15:44 +02005010 PyObject *result;
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005011
Victor Stinner74319ae2016-08-25 00:04:09 +02005012 assert(func != NULL);
5013 assert(nargs >= 0);
5014 assert(nargs == 0 || args != NULL);
Victor Stinnerb9009392016-08-22 23:15:44 +02005015 assert(kwargs == NULL || PyDict_Check(kwargs));
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005016
Victor Stinner577e1f82016-08-25 00:29:32 +02005017 PCALL(PCALL_FUNCTION);
5018 PCALL(PCALL_FAST_FUNCTION);
5019
Victor Stinnerb9009392016-08-22 23:15:44 +02005020 if (co->co_kwonlyargcount == 0 &&
5021 (kwargs == NULL || PyDict_Size(kwargs) == 0) &&
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005022 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
5023 {
Victor Stinnerb9009392016-08-22 23:15:44 +02005024 /* Fast paths */
Victor Stinner2eedc112016-08-22 12:29:42 +02005025 if (argdefs == NULL && co->co_argcount == nargs) {
Victor Stinnerd8735722016-09-09 12:36:44 -07005026 return _PyFunction_FastCall(co, args, nargs, globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02005027 }
5028 else if (nargs == 0 && argdefs != NULL
5029 && co->co_argcount == Py_SIZE(argdefs)) {
5030 /* function called with no arguments, but all parameters have
5031 a default value: use default values as arguments .*/
5032 args = &PyTuple_GET_ITEM(argdefs, 0);
Victor Stinnerd8735722016-09-09 12:36:44 -07005033 return _PyFunction_FastCall(co, args, Py_SIZE(argdefs), globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02005034 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005035 }
5036
Victor Stinnerb9009392016-08-22 23:15:44 +02005037 if (kwargs != NULL) {
5038 Py_ssize_t pos, i;
5039 nk = PyDict_Size(kwargs);
5040
5041 kwtuple = PyTuple_New(2 * nk);
5042 if (kwtuple == NULL) {
5043 return NULL;
5044 }
5045
5046 k = &PyTuple_GET_ITEM(kwtuple, 0);
5047 pos = i = 0;
5048 while (PyDict_Next(kwargs, &pos, &k[i], &k[i+1])) {
5049 Py_INCREF(k[i]);
5050 Py_INCREF(k[i+1]);
5051 i += 2;
5052 }
5053 nk = i / 2;
5054 }
5055 else {
5056 kwtuple = NULL;
5057 k = NULL;
5058 nk = 0;
5059 }
5060
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005061 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
5062 closure = PyFunction_GET_CLOSURE(func);
5063 name = ((PyFunctionObject *)func) -> func_name;
5064 qualname = ((PyFunctionObject *)func) -> func_qualname;
5065
5066 if (argdefs != NULL) {
5067 d = &PyTuple_GET_ITEM(argdefs, 0);
5068 nd = Py_SIZE(argdefs);
5069 }
5070 else {
5071 d = NULL;
5072 nd = 0;
5073 }
Victor Stinnerb9009392016-08-22 23:15:44 +02005074
5075 result = _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
5076 args, nargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03005077 k, k + 1, nk, 2,
Victor Stinnerb9009392016-08-22 23:15:44 +02005078 d, nd, kwdefs,
5079 closure, name, qualname);
5080 Py_XDECREF(kwtuple);
5081 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00005082}
5083
5084static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005085do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005086{
Jeremy Hylton985eba52003-02-05 23:13:00 +00005087#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005088 /* At this point, we have to look at the type of func to
5089 update the call stats properly. Do it here so as to avoid
5090 exposing the call stats machinery outside ceval.c
5091 */
5092 if (PyFunction_Check(func))
5093 PCALL(PCALL_FUNCTION);
5094 else if (PyMethod_Check(func))
5095 PCALL(PCALL_METHOD);
5096 else if (PyType_Check(func))
5097 PCALL(PCALL_TYPE);
5098 else if (PyCFunction_Check(func))
5099 PCALL(PCALL_CFUNCTION);
5100 else
5101 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00005102#endif
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005104 if (PyCFunction_Check(func)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005105 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005106 PyThreadState *tstate = PyThreadState_GET();
5107 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005108 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005109 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005110 else {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005111 return PyObject_Call(func, callargs, kwdict);
Victor Stinner74319ae2016-08-25 00:04:09 +02005112 }
Jeremy Hylton52820442001-01-03 23:52:36 +00005113}
5114
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005115/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005116 nb_index slot defined, and store in *pi.
5117 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang98b49a02017-05-10 19:00:15 +08005118 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005119 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005120*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005121int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005122_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005123{
Serhiy Storchakabf4bb2e2017-03-30 19:46:59 +03005124 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005125 Py_ssize_t x;
5126 if (PyIndex_Check(v)) {
5127 x = PyNumber_AsSsize_t(v, NULL);
5128 if (x == -1 && PyErr_Occurred())
5129 return 0;
5130 }
5131 else {
5132 PyErr_SetString(PyExc_TypeError,
5133 "slice indices must be integers or "
5134 "None or have an __index__ method");
5135 return 0;
5136 }
5137 *pi = x;
5138 }
5139 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005140}
5141
Serhiy Storchakabf4bb2e2017-03-30 19:46:59 +03005142int
5143_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
5144{
5145 Py_ssize_t x;
5146 if (PyIndex_Check(v)) {
5147 x = PyNumber_AsSsize_t(v, NULL);
5148 if (x == -1 && PyErr_Occurred())
5149 return 0;
5150 }
5151 else {
5152 PyErr_SetString(PyExc_TypeError,
5153 "slice indices must be integers or "
5154 "have an __index__ method");
5155 return 0;
5156 }
5157 *pi = x;
5158 return 1;
5159}
5160
5161
Guido van Rossum486364b2007-06-30 05:01:58 +00005162#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005163 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00005164
Guido van Rossumb209a111997-04-29 18:18:01 +00005165static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02005166cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005167{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005168 int res = 0;
5169 switch (op) {
5170 case PyCmp_IS:
5171 res = (v == w);
5172 break;
5173 case PyCmp_IS_NOT:
5174 res = (v != w);
5175 break;
5176 case PyCmp_IN:
5177 res = PySequence_Contains(w, v);
5178 if (res < 0)
5179 return NULL;
5180 break;
5181 case PyCmp_NOT_IN:
5182 res = PySequence_Contains(w, v);
5183 if (res < 0)
5184 return NULL;
5185 res = !res;
5186 break;
5187 case PyCmp_EXC_MATCH:
5188 if (PyTuple_Check(w)) {
5189 Py_ssize_t i, length;
5190 length = PyTuple_Size(w);
5191 for (i = 0; i < length; i += 1) {
5192 PyObject *exc = PyTuple_GET_ITEM(w, i);
5193 if (!PyExceptionClass_Check(exc)) {
5194 PyErr_SetString(PyExc_TypeError,
5195 CANNOT_CATCH_MSG);
5196 return NULL;
5197 }
5198 }
5199 }
5200 else {
5201 if (!PyExceptionClass_Check(w)) {
5202 PyErr_SetString(PyExc_TypeError,
5203 CANNOT_CATCH_MSG);
5204 return NULL;
5205 }
5206 }
5207 res = PyErr_GivenExceptionMatches(v, w);
5208 break;
5209 default:
5210 return PyObject_RichCompare(v, w, op);
5211 }
5212 v = res ? Py_True : Py_False;
5213 Py_INCREF(v);
5214 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005215}
5216
Thomas Wouters52152252000-08-17 22:55:00 +00005217static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005218import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
5219{
5220 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005221 PyObject *import_func, *res;
5222 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005223
5224 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
5225 if (import_func == NULL) {
5226 PyErr_SetString(PyExc_ImportError, "__import__ not found");
5227 return NULL;
5228 }
5229
5230 /* Fast path for not overloaded __import__. */
5231 if (import_func == PyThreadState_GET()->interp->import_func) {
5232 int ilevel = _PyLong_AsInt(level);
5233 if (ilevel == -1 && PyErr_Occurred()) {
5234 return NULL;
5235 }
5236 res = PyImport_ImportModuleLevelObject(
5237 name,
5238 f->f_globals,
5239 f->f_locals == NULL ? Py_None : f->f_locals,
5240 fromlist,
5241 ilevel);
5242 return res;
5243 }
5244
5245 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005246
5247 stack[0] = name;
5248 stack[1] = f->f_globals;
5249 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5250 stack[3] = fromlist;
5251 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005252 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005253 Py_DECREF(import_func);
5254 return res;
5255}
5256
5257static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00005258import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005259{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005260 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005261 _Py_IDENTIFIER(__name__);
5262 PyObject *fullmodname, *pkgname;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005264 x = PyObject_GetAttr(v, name);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005265 if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
5266 return x;
5267 /* Issue #17636: in case this failed because of a circular relative
5268 import, try to fallback on reading the module directly from
5269 sys.modules. */
5270 PyErr_Clear();
5271 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005272 if (pkgname == NULL) {
5273 goto error;
5274 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005275 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
5276 Py_DECREF(pkgname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005277 if (fullmodname == NULL) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005278 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005279 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005280 x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005281 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005282 if (x == NULL) {
5283 goto error;
5284 }
5285 Py_INCREF(x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005286 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005287 error:
5288 PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
5289 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005290}
Guido van Rossumac7be682001-01-17 15:42:30 +00005291
Thomas Wouters52152252000-08-17 22:55:00 +00005292static int
5293import_all_from(PyObject *locals, PyObject *v)
5294{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005295 _Py_IDENTIFIER(__all__);
5296 _Py_IDENTIFIER(__dict__);
5297 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005298 PyObject *dict, *name, *value;
5299 int skip_leading_underscores = 0;
5300 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005302 if (all == NULL) {
5303 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5304 return -1; /* Unexpected error */
5305 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005306 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005307 if (dict == NULL) {
5308 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5309 return -1;
5310 PyErr_SetString(PyExc_ImportError,
5311 "from-import-* object has no __dict__ and no __all__");
5312 return -1;
5313 }
5314 all = PyMapping_Keys(dict);
5315 Py_DECREF(dict);
5316 if (all == NULL)
5317 return -1;
5318 skip_leading_underscores = 1;
5319 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005321 for (pos = 0, err = 0; ; pos++) {
5322 name = PySequence_GetItem(all, pos);
5323 if (name == NULL) {
5324 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5325 err = -1;
5326 else
5327 PyErr_Clear();
5328 break;
5329 }
5330 if (skip_leading_underscores &&
5331 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005332 PyUnicode_READY(name) != -1 &&
5333 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005334 {
5335 Py_DECREF(name);
5336 continue;
5337 }
5338 value = PyObject_GetAttr(v, name);
5339 if (value == NULL)
5340 err = -1;
5341 else if (PyDict_CheckExact(locals))
5342 err = PyDict_SetItem(locals, name, value);
5343 else
5344 err = PyObject_SetItem(locals, name, value);
5345 Py_DECREF(name);
5346 Py_XDECREF(value);
5347 if (err != 0)
5348 break;
5349 }
5350 Py_DECREF(all);
5351 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005352}
5353
Guido van Rossumac7be682001-01-17 15:42:30 +00005354static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005355format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005356{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005357 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005359 if (!obj)
5360 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005361
Serhiy Storchaka06515832016-11-20 09:13:07 +02005362 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005363 if (!obj_str)
5364 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005366 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005367}
Guido van Rossum950361c1997-01-24 13:49:28 +00005368
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005369static void
5370format_exc_unbound(PyCodeObject *co, int oparg)
5371{
5372 PyObject *name;
5373 /* Don't stomp existing exception */
5374 if (PyErr_Occurred())
5375 return;
5376 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5377 name = PyTuple_GET_ITEM(co->co_cellvars,
5378 oparg);
5379 format_exc_check_arg(
5380 PyExc_UnboundLocalError,
5381 UNBOUNDLOCAL_ERROR_MSG,
5382 name);
5383 } else {
5384 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5385 PyTuple_GET_SIZE(co->co_cellvars));
5386 format_exc_check_arg(PyExc_NameError,
5387 UNBOUNDFREE_ERROR_MSG, name);
5388 }
5389}
5390
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005391static PyObject *
5392unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005393 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005394{
5395 PyObject *res;
5396 if (Py_REFCNT(v) == 2) {
5397 /* In the common case, there are 2 references to the value
5398 * stored in 'variable' when the += is performed: one on the
5399 * value stack (in 'v') and one still stored in the
5400 * 'variable'. We try to delete the variable now to reduce
5401 * the refcnt to 1.
5402 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005403 int opcode, oparg;
5404 NEXTOPARG();
5405 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005406 case STORE_FAST:
5407 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005408 PyObject **fastlocals = f->f_localsplus;
5409 if (GETLOCAL(oparg) == v)
5410 SETLOCAL(oparg, NULL);
5411 break;
5412 }
5413 case STORE_DEREF:
5414 {
5415 PyObject **freevars = (f->f_localsplus +
5416 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005417 PyObject *c = freevars[oparg];
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005418 if (PyCell_GET(c) == v)
5419 PyCell_Set(c, NULL);
5420 break;
5421 }
5422 case STORE_NAME:
5423 {
5424 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005425 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005426 PyObject *locals = f->f_locals;
5427 if (PyDict_CheckExact(locals) &&
5428 PyDict_GetItem(locals, name) == v) {
5429 if (PyDict_DelItem(locals, name) != 0) {
5430 PyErr_Clear();
5431 }
5432 }
5433 break;
5434 }
5435 }
5436 }
5437 res = v;
5438 PyUnicode_Append(&res, w);
5439 return res;
5440}
5441
Guido van Rossum950361c1997-01-24 13:49:28 +00005442#ifdef DYNAMIC_EXECUTION_PROFILE
5443
Skip Montanarof118cb12001-10-15 20:51:38 +00005444static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005445getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005446{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005447 int i;
5448 PyObject *l = PyList_New(256);
5449 if (l == NULL) return NULL;
5450 for (i = 0; i < 256; i++) {
5451 PyObject *x = PyLong_FromLong(a[i]);
5452 if (x == NULL) {
5453 Py_DECREF(l);
5454 return NULL;
5455 }
5456 PyList_SetItem(l, i, x);
5457 }
5458 for (i = 0; i < 256; i++)
5459 a[i] = 0;
5460 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005461}
5462
5463PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005464_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005465{
5466#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005467 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005468#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005469 int i;
5470 PyObject *l = PyList_New(257);
5471 if (l == NULL) return NULL;
5472 for (i = 0; i < 257; i++) {
5473 PyObject *x = getarray(dxpairs[i]);
5474 if (x == NULL) {
5475 Py_DECREF(l);
5476 return NULL;
5477 }
5478 PyList_SetItem(l, i, x);
5479 }
5480 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005481#endif
5482}
5483
5484#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005485
5486Py_ssize_t
5487_PyEval_RequestCodeExtraIndex(freefunc free)
5488{
Dino Viehland2997fec2017-06-12 18:46:35 -07005489 __PyCodeExtraState *state = __PyCodeExtraState_Get();
Brett Cannon5c4de282016-09-07 11:16:41 -07005490 Py_ssize_t new_index;
5491
Dino Viehland2997fec2017-06-12 18:46:35 -07005492 if (state->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005493 return -1;
5494 }
Dino Viehland2997fec2017-06-12 18:46:35 -07005495 new_index = state->co_extra_user_count++;
5496 state->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005497 return new_index;
5498}
Łukasz Langaa785c872016-09-09 17:37:37 -07005499
5500static void
5501dtrace_function_entry(PyFrameObject *f)
5502{
5503 char* filename;
5504 char* funcname;
5505 int lineno;
5506
5507 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5508 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5509 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5510
5511 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5512}
5513
5514static void
5515dtrace_function_return(PyFrameObject *f)
5516{
5517 char* filename;
5518 char* funcname;
5519 int lineno;
5520
5521 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5522 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5523 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5524
5525 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5526}
5527
5528/* DTrace equivalent of maybe_call_line_trace. */
5529static void
5530maybe_dtrace_line(PyFrameObject *frame,
5531 int *instr_lb, int *instr_ub, int *instr_prev)
5532{
5533 int line = frame->f_lineno;
5534 char *co_filename, *co_name;
5535
5536 /* If the last instruction executed isn't in the current
5537 instruction window, reset the window.
5538 */
5539 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5540 PyAddrPair bounds;
5541 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5542 &bounds);
5543 *instr_lb = bounds.ap_lower;
5544 *instr_ub = bounds.ap_upper;
5545 }
5546 /* If the last instruction falls at the start of a line or if
5547 it represents a jump backwards, update the frame's line
5548 number and call the trace function. */
5549 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5550 frame->f_lineno = line;
5551 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5552 if (!co_filename)
5553 co_filename = "?";
5554 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5555 if (!co_name)
5556 co_name = "?";
5557 PyDTrace_LINE(co_filename, co_name, line);
5558 }
5559 *instr_prev = frame->f_lasti;
5560}