blob: b6ad444e70be2254b68ba592638571ae61482412 [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 *);
Serhiy Storchaka946a0b62017-08-03 12:14:35 +030067static int check_args_iterable(PyObject *func, PyObject *vararg);
68static void format_kwargs_mapping_error(PyObject *func, PyObject *kwargs);
Guido van Rossum374a9221991-04-04 10:40:29 +000069
Paul Prescode68140d2000-08-30 20:25:01 +000070#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000072#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000074#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 "free variable '%.200s' referenced before assignment" \
76 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000077
Guido van Rossum950361c1997-01-24 13:49:28 +000078/* Dynamic execution profile */
79#ifdef DYNAMIC_EXECUTION_PROFILE
80#ifdef DXPAIRS
81static long dxpairs[257][256];
82#define dxp dxpairs[256]
83#else
84static long dxp[256];
85#endif
86#endif
87
Jeremy Hylton985eba52003-02-05 23:13:00 +000088/* Function call profile */
89#ifdef CALL_PROFILE
90#define PCALL_NUM 11
91static int pcall[PCALL_NUM];
92
93#define PCALL_ALL 0
94#define PCALL_FUNCTION 1
95#define PCALL_FAST_FUNCTION 2
96#define PCALL_FASTER_FUNCTION 3
97#define PCALL_METHOD 4
98#define PCALL_BOUND_METHOD 5
99#define PCALL_CFUNCTION 6
100#define PCALL_TYPE 7
101#define PCALL_GENERATOR 8
102#define PCALL_OTHER 9
103#define PCALL_POP 10
104
105/* Notes about the statistics
106
107 PCALL_FAST stats
108
109 FAST_FUNCTION means no argument tuple needs to be created.
110 FASTER_FUNCTION means that the fast-path frame setup code is used.
111
112 If there is a method call where the call can be optimized by changing
113 the argument tuple and calling the function directly, it gets recorded
114 twice.
115
116 As a result, the relationship among the statistics appears to be
117 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
118 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
119 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
120 PCALL_METHOD > PCALL_BOUND_METHOD
121*/
122
123#define PCALL(POS) pcall[POS]++
124
125PyObject *
126PyEval_GetCallStats(PyObject *self)
127{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 return Py_BuildValue("iiiiiiiiiii",
129 pcall[0], pcall[1], pcall[2], pcall[3],
130 pcall[4], pcall[5], pcall[6], pcall[7],
131 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000132}
133#else
134#define PCALL(O)
135
136PyObject *
137PyEval_GetCallStats(PyObject *self)
138{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 Py_INCREF(Py_None);
140 return Py_None;
Jeremy Hylton985eba52003-02-05 23:13:00 +0000141}
142#endif
143
Tim Peters5ca576e2001-06-18 22:08:13 +0000144
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000145#ifdef WITH_THREAD
146#define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request)
147#else
148#define GIL_REQUEST 0
149#endif
150
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000151/* This can set eval_breaker to 0 even though gil_drop_request became
152 1. We believe this is all right because the eval loop will release
153 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000154#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 _Py_atomic_store_relaxed( \
156 &eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000157 GIL_REQUEST | \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
159 pending_async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000160
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000161#ifdef WITH_THREAD
162
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000163#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 do { \
165 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
166 _Py_atomic_store_relaxed(&eval_breaker, 1); \
167 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000168
169#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 do { \
171 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
172 COMPUTE_EVAL_BREAKER(); \
173 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000174
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000175#endif
176
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000177/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000178#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179 do { \
180 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
181 _Py_atomic_store_relaxed(&eval_breaker, 1); \
182 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000183
184#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 do { \
186 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
187 COMPUTE_EVAL_BREAKER(); \
188 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000189
190#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 do { \
192 pending_async_exc = 1; \
193 _Py_atomic_store_relaxed(&eval_breaker, 1); \
194 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000195
196#define UNSIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000198
199
Antoine Pitrou3024c052017-07-01 19:12:05 +0200200/* This single variable consolidates all requests to break out of the fast path
201 in the eval loop. */
202static _Py_atomic_int eval_breaker = {0};
203/* Request for running pending calls. */
204static _Py_atomic_int pendingcalls_to_do = {0};
205/* Request for looking at the `async_exc` field of the current thread state.
206 Guarded by the GIL. */
207static int pending_async_exc = 0;
208
Guido van Rossume59214e1994-08-30 08:01:59 +0000209#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000210
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000211#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000212#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000213#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000214#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000215
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000216static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000217static long main_thread = 0;
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000218/* Request for dropping the GIL */
219static _Py_atomic_int gil_drop_request = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000220
221#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000222
Tim Peters7f468f22004-10-11 02:40:51 +0000223int
224PyEval_ThreadsInitialized(void)
225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000227}
228
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000229void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000230PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000231{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 if (gil_created())
233 return;
234 create_gil();
235 take_gil(PyThreadState_GET());
236 main_thread = PyThread_get_thread_ident();
237 if (!pending_lock)
238 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000239}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000240
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000241void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000242_PyEval_FiniThreads(void)
243{
244 if (!gil_created())
245 return;
246 destroy_gil();
247 assert(!gil_created());
248}
249
250void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000251PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000252{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 PyThreadState *tstate = PyThreadState_GET();
254 if (tstate == NULL)
255 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
256 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000257}
258
259void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000260PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000261{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 /* This function must succeed when the current thread state is NULL.
263 We therefore avoid PyThreadState_GET() which dumps a fatal error
264 in debug mode.
265 */
266 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
267 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000268}
269
270void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000271PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000272{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 if (tstate == NULL)
274 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
275 /* Check someone has called PyEval_InitThreads() to create the lock */
276 assert(gil_created());
277 take_gil(tstate);
278 if (PyThreadState_Swap(tstate) != NULL)
279 Py_FatalError(
280 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000281}
282
283void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000284PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000285{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 if (tstate == NULL)
287 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
288 if (PyThreadState_Swap(NULL) != tstate)
289 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
290 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000291}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000292
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200293/* This function is called from PyOS_AfterFork to destroy all threads which are
294 * not running in the child process, and clear internal locks which might be
295 * held by those threads. (This could also be done using pthread_atfork
296 * mechanism, at least for the pthreads implementation.) */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000297
298void
299PyEval_ReInitThreads(void)
300{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200301 _Py_IDENTIFIER(_after_fork);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 PyObject *threading, *result;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200303 PyThreadState *current_tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 if (!gil_created())
306 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 recreate_gil();
308 pending_lock = PyThread_allocate_lock();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200309 take_gil(current_tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 /* Update the threading module with the new state.
313 */
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200314 threading = PyMapping_GetItemString(current_tstate->interp->modules,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 "threading");
316 if (threading == NULL) {
317 /* threading not imported */
318 PyErr_Clear();
319 return;
320 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200321 result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 if (result == NULL)
323 PyErr_WriteUnraisable(threading);
324 else
325 Py_DECREF(result);
326 Py_DECREF(threading);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200327
328 /* Destroy all threads except the current one */
329 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000330}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000331
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000332#endif /* WITH_THREAD */
333
334/* This function is used to signal that async exceptions are waiting to be
335 raised, therefore it is also useful in non-threaded builds. */
336
337void
338_PyEval_SignalAsyncExc(void)
339{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000341}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000342
Guido van Rossumff4949e1992-08-05 19:58:53 +0000343/* Functions save_thread and restore_thread are always defined so
344 dynamically loaded modules needn't be compiled separately for use
345 with and without threads: */
346
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000347PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000348PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000349{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 PyThreadState *tstate = PyThreadState_Swap(NULL);
351 if (tstate == NULL)
352 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000353#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 if (gil_created())
355 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000356#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000358}
359
360void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000361PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000362{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 if (tstate == NULL)
364 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000365#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 if (gil_created()) {
367 int err = errno;
368 take_gil(tstate);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200369 /* _Py_Finalizing is protected by the GIL */
370 if (_Py_Finalizing && tstate != _Py_Finalizing) {
371 drop_gil(tstate);
372 PyThread_exit_thread();
373 assert(0); /* unreachable */
374 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 errno = err;
376 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000377#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000379}
380
381
Guido van Rossuma9672091994-09-14 13:31:22 +0000382/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
383 signal handlers or Mac I/O completion routines) can schedule calls
384 to a function to be called synchronously.
385 The synchronous function is called with one void* argument.
386 It should return 0 for success or -1 for failure -- failure should
387 be accompanied by an exception.
388
389 If registry succeeds, the registry function returns 0; if it fails
390 (e.g. due to too many pending calls) it returns -1 (without setting
391 an exception condition).
392
393 Note that because registry may occur from within signal handlers,
394 or other asynchronous events, calling malloc() is unsafe!
395
396#ifdef WITH_THREAD
397 Any thread can schedule pending calls, but only the main thread
398 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000399 There is no facility to schedule calls to a particular thread, but
400 that should be easy to change, should that ever be required. In
401 that case, the static variables here should go into the python
402 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000403#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000404*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000405
Antoine Pitrou3024c052017-07-01 19:12:05 +0200406void
407_PyEval_SignalReceived(void)
408{
409 /* bpo-30703: Function called when the C signal handler of Python gets a
410 signal. We cannot queue a callback using Py_AddPendingCall() since
411 that function is not async-signal-safe. */
412 SIGNAL_PENDING_CALLS();
413}
414
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000415#ifdef WITH_THREAD
416
417/* The WITH_THREAD implementation is thread-safe. It allows
418 scheduling to be made from any thread, and even from an executing
419 callback.
420 */
421
422#define NPENDINGCALLS 32
423static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 int (*func)(void *);
425 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000426} pendingcalls[NPENDINGCALLS];
427static int pendingfirst = 0;
428static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000429
430int
431Py_AddPendingCall(int (*func)(void *), void *arg)
432{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 int i, j, result=0;
434 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 /* try a few times for the lock. Since this mechanism is used
437 * for signal handling (on the main thread), there is a (slim)
438 * chance that a signal is delivered on the same thread while we
439 * hold the lock during the Py_MakePendingCalls() function.
440 * This avoids a deadlock in that case.
441 * Note that signals can be delivered on any thread. In particular,
442 * on Windows, a SIGINT is delivered on a system-created worker
443 * thread.
444 * We also check for lock being NULL, in the unlikely case that
445 * this function is called before any bytecode evaluation takes place.
446 */
447 if (lock != NULL) {
448 for (i = 0; i<100; i++) {
449 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
450 break;
451 }
452 if (i == 100)
453 return -1;
454 }
455
456 i = pendinglast;
457 j = (i + 1) % NPENDINGCALLS;
458 if (j == pendingfirst) {
459 result = -1; /* Queue full */
460 } else {
461 pendingcalls[i].func = func;
462 pendingcalls[i].arg = arg;
463 pendinglast = j;
464 }
465 /* signal main loop */
466 SIGNAL_PENDING_CALLS();
467 if (lock != NULL)
468 PyThread_release_lock(lock);
469 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000470}
471
472int
473Py_MakePendingCalls(void)
474{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200475 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 int i;
477 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000478
Antoine Pitrou3024c052017-07-01 19:12:05 +0200479 assert(PyGILState_Check());
480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 if (!pending_lock) {
482 /* initial allocation of the lock */
483 pending_lock = PyThread_allocate_lock();
484 if (pending_lock == NULL)
485 return -1;
486 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 /* only service pending calls on main thread */
489 if (main_thread && PyThread_get_thread_ident() != main_thread)
490 return 0;
491 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200492 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200494 busy = 1;
Antoine Pitrou3024c052017-07-01 19:12:05 +0200495 /* unsignal before starting to call callbacks, so that any callback
496 added in-between re-signals */
497 UNSIGNAL_PENDING_CALLS();
498
499 /* Python signal handler doesn't really queue a callback: it only signals
500 that a signal was received, see _PyEval_SignalReceived(). */
501 if (PyErr_CheckSignals() < 0) {
502 goto error;
503 }
504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 /* perform a bounded number of calls, in case of recursion */
506 for (i=0; i<NPENDINGCALLS; i++) {
507 int j;
508 int (*func)(void *);
509 void *arg = NULL;
510
511 /* pop one item off the queue while holding the lock */
512 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
513 j = pendingfirst;
514 if (j == pendinglast) {
515 func = NULL; /* Queue empty */
516 } else {
517 func = pendingcalls[j].func;
518 arg = pendingcalls[j].arg;
519 pendingfirst = (j + 1) % NPENDINGCALLS;
520 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 PyThread_release_lock(pending_lock);
522 /* having released the lock, perform the callback */
523 if (func == NULL)
524 break;
525 r = func(arg);
Antoine Pitrou3024c052017-07-01 19:12:05 +0200526 if (r) {
527 goto error;
528 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 }
Antoine Pitrou3024c052017-07-01 19:12:05 +0200530
Charles-François Natalif23339a2011-07-23 18:15:43 +0200531 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 return r;
Antoine Pitrou3024c052017-07-01 19:12:05 +0200533
534error:
535 busy = 0;
536 SIGNAL_PENDING_CALLS(); /* We're not done yet */
537 return -1;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000538}
539
540#else /* if ! defined WITH_THREAD */
541
542/*
543 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
544 This code is used for signal handling in python that isn't built
545 with WITH_THREAD.
546 Don't use this implementation when Py_AddPendingCalls() can happen
547 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548
Guido van Rossuma9672091994-09-14 13:31:22 +0000549 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000550 (1) nested asynchronous calls to Py_AddPendingCall()
551 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000553 (1) is very unlikely because typically signal delivery
554 is blocked during signal handling. So it should be impossible.
555 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000556 The current code is safe against (2), but not against (1).
557 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000558 thread is present, interrupted by signals, and that the critical
559 section is protected with the "busy" variable. On Windows, which
560 delivers SIGINT on a system thread, this does not hold and therefore
561 Windows really shouldn't use this version.
562 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000563*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000564
Guido van Rossuma9672091994-09-14 13:31:22 +0000565#define NPENDINGCALLS 32
566static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 int (*func)(void *);
568 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000569} pendingcalls[NPENDINGCALLS];
570static volatile int pendingfirst = 0;
571static volatile int pendinglast = 0;
572
573int
Thomas Wouters334fb892000-07-25 12:56:38 +0000574Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000575{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 static volatile int busy = 0;
577 int i, j;
578 /* XXX Begin critical section */
579 if (busy)
580 return -1;
581 busy = 1;
582 i = pendinglast;
583 j = (i + 1) % NPENDINGCALLS;
584 if (j == pendingfirst) {
585 busy = 0;
586 return -1; /* Queue full */
587 }
588 pendingcalls[i].func = func;
589 pendingcalls[i].arg = arg;
590 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 SIGNAL_PENDING_CALLS();
593 busy = 0;
594 /* XXX End critical section */
595 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000596}
597
Guido van Rossum180d7b41994-09-29 09:45:57 +0000598int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000599Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000600{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 static int busy = 0;
602 if (busy)
603 return 0;
604 busy = 1;
Antoine Pitrou3024c052017-07-01 19:12:05 +0200605
606 /* unsignal before starting to call callbacks, so that any callback
607 added in-between re-signals */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 UNSIGNAL_PENDING_CALLS();
Antoine Pitrou3024c052017-07-01 19:12:05 +0200609 /* Python signal handler doesn't really queue a callback: it only signals
610 that a signal was received, see _PyEval_SignalReceived(). */
611 if (PyErr_CheckSignals() < 0) {
612 goto error;
613 }
614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 for (;;) {
616 int i;
617 int (*func)(void *);
618 void *arg;
619 i = pendingfirst;
620 if (i == pendinglast)
621 break; /* Queue empty */
622 func = pendingcalls[i].func;
623 arg = pendingcalls[i].arg;
624 pendingfirst = (i + 1) % NPENDINGCALLS;
625 if (func(arg) < 0) {
Masayuki Yamamotoe3a0ff02017-07-05 18:24:46 +0900626 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 }
628 }
629 busy = 0;
630 return 0;
Antoine Pitrou3024c052017-07-01 19:12:05 +0200631
632error:
633 busy = 0;
634 SIGNAL_PENDING_CALLS(); /* We're not done yet */
635 return -1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000636}
637
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000638#endif /* WITH_THREAD */
639
Guido van Rossuma9672091994-09-14 13:31:22 +0000640
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000641/* The interpreter's recursion limit */
642
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000643#ifndef Py_DEFAULT_RECURSION_LIMIT
644#define Py_DEFAULT_RECURSION_LIMIT 1000
645#endif
646static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
647int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000648
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000649int
650Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000651{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000653}
654
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000655void
656Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000657{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 recursion_limit = new_limit;
659 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000660}
661
Armin Rigo2b3eb402003-10-28 12:05:48 +0000662/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
663 if the recursion_depth reaches _Py_CheckRecursionLimit.
664 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
665 to guarantee that _Py_CheckRecursiveCall() is regularly called.
666 Without USE_STACKCHECK, there is no need for this. */
667int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300668_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000669{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000671
672#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 if (PyOS_CheckStack()) {
674 --tstate->recursion_depth;
675 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
676 return -1;
677 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000678#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 _Py_CheckRecursionLimit = recursion_limit;
680 if (tstate->recursion_critical)
681 /* Somebody asked that we don't check for recursion. */
682 return 0;
683 if (tstate->overflowed) {
684 if (tstate->recursion_depth > recursion_limit + 50) {
685 /* Overflowing while handling an overflow. Give up. */
686 Py_FatalError("Cannot recover from stack overflow.");
687 }
688 return 0;
689 }
690 if (tstate->recursion_depth > recursion_limit) {
691 --tstate->recursion_depth;
692 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400693 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 "maximum recursion depth exceeded%s",
695 where);
696 return -1;
697 }
698 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000699}
700
Guido van Rossum374a9221991-04-04 10:40:29 +0000701/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000702enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000703 WHY_NOT = 0x0001, /* No error */
704 WHY_EXCEPTION = 0x0002, /* Exception occurred */
Stefan Krahb7e10102010-06-23 18:42:39 +0000705 WHY_RETURN = 0x0008, /* 'return' statement */
706 WHY_BREAK = 0x0010, /* 'break' statement */
707 WHY_CONTINUE = 0x0020, /* 'continue' statement */
708 WHY_YIELD = 0x0040, /* 'yield' operator */
709 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000710};
Guido van Rossum374a9221991-04-04 10:40:29 +0000711
Benjamin Peterson87880242011-07-03 16:48:31 -0500712static void save_exc_state(PyThreadState *, PyFrameObject *);
713static void swap_exc_state(PyThreadState *, PyFrameObject *);
714static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400715static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000716static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000717
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000718/* Records whether tracing is on for any thread. Counts the number of
719 threads for which tstate->c_tracefunc is non-NULL, so if the value
720 is 0, we know we don't have to check this thread's c_tracefunc.
721 This speeds up the if statement in PyEval_EvalFrameEx() after
722 fast_next_opcode*/
723static int _Py_TracingPossible = 0;
724
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000725
Guido van Rossum374a9221991-04-04 10:40:29 +0000726
Guido van Rossumb209a111997-04-29 18:18:01 +0000727PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000728PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000729{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 return PyEval_EvalCodeEx(co,
731 globals, locals,
732 (PyObject **)NULL, 0,
733 (PyObject **)NULL, 0,
734 (PyObject **)NULL, 0,
735 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000736}
737
738
739/* Interpreter main loop */
740
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000741PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000742PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 /* This is for backward compatibility with extension modules that
744 used this API; core interpreter code should call
745 PyEval_EvalFrameEx() */
746 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000747}
748
749PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000750PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000751{
Brett Cannon3cebf932016-09-05 15:33:46 -0700752 PyThreadState *tstate = PyThreadState_GET();
753 return tstate->interp->eval_frame(f, throwflag);
754}
755
756PyObject *
757_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
758{
Guido van Rossum950361c1997-01-24 13:49:28 +0000759#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000761#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200762 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300763 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200764 int opcode; /* Current opcode */
765 int oparg; /* Current opcode argument, if any */
766 enum why_code why; /* Reason for block stack unwind */
767 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 PyObject *retval = NULL; /* Return value */
769 PyThreadState *tstate = PyThreadState_GET();
770 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 is true when the line being executed has changed. The
777 initial values are such as to make this false the first
778 time it is tested. */
779 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000780
Serhiy Storchakaab874002016-09-11 13:48:15 +0300781 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 PyObject *names;
783 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000784
Brett Cannon368b4b72012-04-02 12:17:59 -0400785#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200786 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400787#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200788
Antoine Pitroub52ec782009-01-25 16:34:23 +0000789/* Computed GOTOs, or
790 the-optimization-commonly-but-improperly-known-as-"threaded code"
791 using gcc's labels-as-values extension
792 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
793
794 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000796 combined with a lookup table of jump addresses. However, since the
797 indirect jump instruction is shared by all opcodes, the CPU will have a
798 hard time making the right prediction for where to jump next (actually,
799 it will be always wrong except in the uncommon case of a sequence of
800 several identical opcodes).
801
802 "Threaded code" in contrast, uses an explicit jump table and an explicit
803 indirect jump instruction at the end of each opcode. Since the jump
804 instruction is at a different address for each opcode, the CPU will make a
805 separate prediction for each of these instructions, which is equivalent to
806 predicting the second opcode of each opcode pair. These predictions have
807 a much better chance to turn out valid, especially in small bytecode loops.
808
809 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000811 and potentially many more instructions (depending on the pipeline width).
812 A correctly predicted branch, however, is nearly free.
813
814 At the time of this writing, the "threaded code" version is up to 15-20%
815 faster than the normal "switch" version, depending on the compiler and the
816 CPU architecture.
817
818 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
819 because it would render the measurements invalid.
820
821
822 NOTE: care must be taken that the compiler doesn't try to "optimize" the
823 indirect jumps by sharing them between all opcodes. Such optimizations
824 can be disabled on gcc by using the -fno-gcse flag (or possibly
825 -fno-crossjumping).
826*/
827
Antoine Pitrou042b1282010-08-13 21:15:58 +0000828#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000829#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000830#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000831#endif
832
Antoine Pitrou042b1282010-08-13 21:15:58 +0000833#ifdef HAVE_COMPUTED_GOTOS
834 #ifndef USE_COMPUTED_GOTOS
835 #define USE_COMPUTED_GOTOS 1
836 #endif
837#else
838 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
839 #error "Computed gotos are not supported on this compiler."
840 #endif
841 #undef USE_COMPUTED_GOTOS
842 #define USE_COMPUTED_GOTOS 0
843#endif
844
845#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000846/* Import the static jump table */
847#include "opcode_targets.h"
848
Antoine Pitroub52ec782009-01-25 16:34:23 +0000849#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 TARGET_##op: \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000852
Antoine Pitroub52ec782009-01-25 16:34:23 +0000853#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 { \
855 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
856 FAST_DISPATCH(); \
857 } \
858 continue; \
859 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000860
861#ifdef LLTRACE
862#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700864 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300866 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300867 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 } \
869 goto fast_next_opcode; \
870 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000871#else
872#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700874 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300876 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300877 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 } \
879 goto fast_next_opcode; \
880 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000881#endif
882
883#else
884#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 case op:
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300886
Antoine Pitroub52ec782009-01-25 16:34:23 +0000887#define DISPATCH() continue
888#define FAST_DISPATCH() goto fast_next_opcode
889#endif
890
891
Neal Norwitza81d2202002-07-14 00:27:26 +0000892/* Tuple access macros */
893
894#ifndef Py_DEBUG
895#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
896#else
897#define GETITEM(v, i) PyTuple_GetItem((v), (i))
898#endif
899
Guido van Rossum374a9221991-04-04 10:40:29 +0000900/* Code access macros */
901
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300902/* The integer overflow is checked by an assertion below. */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300903#define INSTR_OFFSET() (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300904#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300905 _Py_CODEUNIT word = *next_instr; \
906 opcode = _Py_OPCODE(word); \
907 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300908 next_instr++; \
909 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300910#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
911#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000912
Raymond Hettingerf606f872003-03-16 03:11:04 +0000913/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 Some opcodes tend to come in pairs thus making it possible to
915 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300916 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 Verifying the prediction costs a single high-speed test of a register
919 variable against a constant. If the pairing was good, then the
920 processor's own internal branch predication has a high likelihood of
921 success, resulting in a nearly zero-overhead transition to the
922 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300923 including its unpredictable switch-case branch. Combined with the
924 processor's internal branch prediction, a successful PREDICT has the
925 effect of making the two opcodes run as if they were a single new opcode
926 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000927
Georg Brandl86b2fb92008-07-16 03:43:04 +0000928 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 predictions turned-on and interpret the results as if some opcodes
930 had been combined or turn-off predictions so that the opcode frequency
931 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000932
933 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 the CPU to record separate branch prediction information for each
935 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000936
Raymond Hettingerf606f872003-03-16 03:11:04 +0000937*/
938
Antoine Pitrou042b1282010-08-13 21:15:58 +0000939#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000941#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300942#define PREDICT(op) \
943 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300944 _Py_CODEUNIT word = *next_instr; \
945 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300946 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300947 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300948 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300949 goto PRED_##op; \
950 } \
951 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000952#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300953#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000954
Raymond Hettingerf606f872003-03-16 03:11:04 +0000955
Guido van Rossum374a9221991-04-04 10:40:29 +0000956/* Stack manipulation macros */
957
Martin v. Löwis18e16552006-02-15 17:27:45 +0000958/* The stack can grow at most MAXINT deep, as co_nlocals and
959 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000960#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
961#define EMPTY() (STACK_LEVEL() == 0)
962#define TOP() (stack_pointer[-1])
963#define SECOND() (stack_pointer[-2])
964#define THIRD() (stack_pointer[-3])
965#define FOURTH() (stack_pointer[-4])
966#define PEEK(n) (stack_pointer[-(n)])
967#define SET_TOP(v) (stack_pointer[-1] = (v))
968#define SET_SECOND(v) (stack_pointer[-2] = (v))
969#define SET_THIRD(v) (stack_pointer[-3] = (v))
970#define SET_FOURTH(v) (stack_pointer[-4] = (v))
971#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
972#define BASIC_STACKADJ(n) (stack_pointer += n)
973#define BASIC_PUSH(v) (*stack_pointer++ = (v))
974#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000975
Guido van Rossum96a42c81992-01-12 02:29:51 +0000976#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000978 lltrace && prtrace(TOP(), "push")); \
979 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000981 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000983 lltrace && prtrace(TOP(), "stackadj")); \
984 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +0000985#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000986 prtrace((STACK_POINTER)[-1], "ext_pop")), \
987 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000988#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000989#define PUSH(v) BASIC_PUSH(v)
990#define POP() BASIC_POP()
991#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000992#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000993#endif
994
Guido van Rossum681d79a1995-07-18 14:51:37 +0000995/* Local variable macros */
996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000998
999/* The SETLOCAL() macro must not DECREF the local variable in-place and
1000 then store the new value; it must copy the old value to a temporary
1001 value, then store the new value, and then DECREF the temporary value.
1002 This is because it is possible that during the DECREF the frame is
1003 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1004 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001006 GETLOCAL(i) = value; \
1007 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001008
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001009
1010#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 while (STACK_LEVEL() > (b)->b_level) { \
1012 PyObject *v = POP(); \
1013 Py_XDECREF(v); \
1014 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001015
1016#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001017 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 PyObject *type, *value, *traceback; \
1019 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1020 while (STACK_LEVEL() > (b)->b_level + 3) { \
1021 value = POP(); \
1022 Py_XDECREF(value); \
1023 } \
1024 type = tstate->exc_type; \
1025 value = tstate->exc_value; \
1026 traceback = tstate->exc_traceback; \
1027 tstate->exc_type = POP(); \
1028 tstate->exc_value = POP(); \
1029 tstate->exc_traceback = POP(); \
1030 Py_XDECREF(type); \
1031 Py_XDECREF(value); \
1032 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001033 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001034
Guido van Rossuma027efa1997-05-05 20:56:21 +00001035/* Start of code */
1036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 /* push frame */
1038 if (Py_EnterRecursiveCall(""))
1039 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 if (tstate->use_tracing) {
1044 if (tstate->c_tracefunc != NULL) {
1045 /* tstate->c_tracefunc, if defined, is a
1046 function that will be called on *every* entry
1047 to a code block. Its return value, if not
1048 None, is a function that will be called at
1049 the start of each executed line of code.
1050 (Actually, the function must return itself
1051 in order to continue tracing.) The trace
1052 functions are called with three arguments:
1053 a pointer to the current frame, a string
1054 indicating why the function is called, and
1055 an argument which depends on the situation.
1056 The global trace function is also called
1057 whenever an exception is detected. */
1058 if (call_trace_protected(tstate->c_tracefunc,
1059 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001060 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 /* Trace function raised an error */
1062 goto exit_eval_frame;
1063 }
1064 }
1065 if (tstate->c_profilefunc != NULL) {
1066 /* Similar for c_profilefunc, except it needn't
1067 return itself and isn't called for "line" events */
1068 if (call_trace_protected(tstate->c_profilefunc,
1069 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001070 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 /* Profile function raised an error */
1072 goto exit_eval_frame;
1073 }
1074 }
1075 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001076
Łukasz Langaa785c872016-09-09 17:37:37 -07001077 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1078 dtrace_function_entry(f);
1079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 co = f->f_code;
1081 names = co->co_names;
1082 consts = co->co_consts;
1083 fastlocals = f->f_localsplus;
1084 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001085 assert(PyBytes_Check(co->co_code));
1086 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001087 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1088 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1089 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001090 /*
1091 f->f_lasti refers to the index of the last instruction,
1092 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001093
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001094 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001095 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 When the PREDICT() macros are enabled, some opcode pairs follow in
1098 direct succession without updating f->f_lasti. A successful
1099 prediction effectively links the two codes together as if they
1100 were a single new opcode; accordingly,f->f_lasti will point to
1101 the first code in the pair (for instance, GET_ITER followed by
1102 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001103 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001105 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001106 next_instr = first_instr;
1107 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001108 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1109 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001110 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 stack_pointer = f->f_stacktop;
1112 assert(stack_pointer != NULL);
1113 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001114 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001115
Yury Selivanoveb636452016-09-08 22:01:51 -07001116 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01001117 if (!throwflag && f->f_exc_type != NULL && f->f_exc_type != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 /* We were in an except handler when we left,
1119 restore the exception state which was put aside
1120 (see YIELD_VALUE). */
Benjamin Peterson87880242011-07-03 16:48:31 -05001121 swap_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 }
Benjamin Peterson87880242011-07-03 16:48:31 -05001123 else
1124 save_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001126
Tim Peters5ca576e2001-06-18 22:08:13 +00001127#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001128 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001129#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 why = WHY_NOT;
Guido van Rossumac7be682001-01-17 15:42:30 +00001132
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001133 if (throwflag) /* support for generator.throw() */
1134 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001135
Victor Stinnerace47d72013-07-18 01:41:08 +02001136#ifdef Py_DEBUG
1137 /* PyEval_EvalFrameEx() must not be called with an exception set,
1138 because it may clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001139 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +02001140 assert(!PyErr_Occurred());
1141#endif
1142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1145 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +02001146 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 /* Do periodic things. Doing this every time through
1149 the loop would add too much overhead, so we do it
1150 only every Nth instruction. We also do it if
1151 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1152 event needs attention (e.g. a signal handler or
1153 async I/O handler); see Py_AddPendingCall() and
1154 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 if (_Py_atomic_load_relaxed(&eval_breaker)) {
Yury Selivanove89f95b2017-06-09 17:06:39 -04001157 if (_Py_OPCODE(*next_instr) == SETUP_FINALLY ||
1158 _Py_OPCODE(*next_instr) == YIELD_FROM) {
1159 /* Two cases where we skip running signal handlers and other
1160 pending calls:
1161 - If we're about to enter the try: of a try/finally (not
1162 *very* useful, but might help in some cases and it's
1163 traditional)
1164 - If we're resuming a chain of nested 'yield from' or
1165 'await' calls, then each frame is parked with YIELD_FROM
1166 as its next opcode. If the user hit control-C we want to
1167 wait until we've reached the innermost frame before
1168 running the signal handler and raising KeyboardInterrupt
1169 (see bpo-30039).
1170 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 goto fast_next_opcode;
1172 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001174 if (Py_MakePendingCalls() < 0)
1175 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001177#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001178 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 /* Give another thread a chance */
1180 if (PyThreadState_Swap(NULL) != tstate)
1181 Py_FatalError("ceval: tstate mix-up");
1182 drop_gil(tstate);
1183
1184 /* Other threads may run now */
1185
1186 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001187
1188 /* Check if we should make a quick exit. */
1189 if (_Py_Finalizing && _Py_Finalizing != tstate) {
1190 drop_gil(tstate);
1191 PyThread_exit_thread();
1192 }
1193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 if (PyThreadState_Swap(tstate) != NULL)
1195 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001197#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 /* Check for asynchronous exceptions. */
1199 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001200 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 tstate->async_exc = NULL;
1202 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001203 PyErr_SetNone(exc);
1204 Py_DECREF(exc);
1205 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 }
1207 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 fast_next_opcode:
1210 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001211
Łukasz Langaa785c872016-09-09 17:37:37 -07001212 if (PyDTrace_LINE_ENABLED())
1213 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001218 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001219 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 /* see maybe_call_line_trace
1221 for expository comments */
1222 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 err = maybe_call_line_trace(tstate->c_tracefunc,
1225 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001226 tstate, f,
1227 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 /* Reload possibly changed frame fields */
1229 JUMPTO(f->f_lasti);
1230 if (f->f_stacktop != NULL) {
1231 stack_pointer = f->f_stacktop;
1232 f->f_stacktop = NULL;
1233 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001234 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001236 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001240
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001241 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001242 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001243#ifdef DYNAMIC_EXECUTION_PROFILE
1244#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 dxpairs[lastopcode][opcode]++;
1246 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001247#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001249#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001250
Guido van Rossum96a42c81992-01-12 02:29:51 +00001251#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 if (lltrace) {
1255 if (HAS_ARG(opcode)) {
1256 printf("%d: %d, %d\n",
1257 f->f_lasti, opcode, oparg);
1258 }
1259 else {
1260 printf("%d: %d\n",
1261 f->f_lasti, opcode);
1262 }
1263 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001264#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 /* BEWARE!
1269 It is essential that any operation that fails sets either
1270 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1271 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 TARGET(NOP)
1274 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001275
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001276 TARGET(LOAD_FAST) {
1277 PyObject *value = GETLOCAL(oparg);
1278 if (value == NULL) {
1279 format_exc_check_arg(PyExc_UnboundLocalError,
1280 UNBOUNDLOCAL_ERROR_MSG,
1281 PyTuple_GetItem(co->co_varnames, oparg));
1282 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001284 Py_INCREF(value);
1285 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001287 }
1288
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001289 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001290 TARGET(LOAD_CONST) {
1291 PyObject *value = GETITEM(consts, oparg);
1292 Py_INCREF(value);
1293 PUSH(value);
1294 FAST_DISPATCH();
1295 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001296
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001297 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001298 TARGET(STORE_FAST) {
1299 PyObject *value = POP();
1300 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001302 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001303
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001304 TARGET(POP_TOP) {
1305 PyObject *value = POP();
1306 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001308 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001309
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001310 TARGET(ROT_TWO) {
1311 PyObject *top = TOP();
1312 PyObject *second = SECOND();
1313 SET_TOP(second);
1314 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001316 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001317
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001318 TARGET(ROT_THREE) {
1319 PyObject *top = TOP();
1320 PyObject *second = SECOND();
1321 PyObject *third = THIRD();
1322 SET_TOP(second);
1323 SET_SECOND(third);
1324 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001326 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001327
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001328 TARGET(DUP_TOP) {
1329 PyObject *top = TOP();
1330 Py_INCREF(top);
1331 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001333 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001334
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001335 TARGET(DUP_TOP_TWO) {
1336 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001337 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001338 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001339 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001340 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001341 SET_TOP(top);
1342 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001343 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001344 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001345
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001346 TARGET(UNARY_POSITIVE) {
1347 PyObject *value = TOP();
1348 PyObject *res = PyNumber_Positive(value);
1349 Py_DECREF(value);
1350 SET_TOP(res);
1351 if (res == NULL)
1352 goto error;
1353 DISPATCH();
1354 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001355
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001356 TARGET(UNARY_NEGATIVE) {
1357 PyObject *value = TOP();
1358 PyObject *res = PyNumber_Negative(value);
1359 Py_DECREF(value);
1360 SET_TOP(res);
1361 if (res == NULL)
1362 goto error;
1363 DISPATCH();
1364 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001365
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001366 TARGET(UNARY_NOT) {
1367 PyObject *value = TOP();
1368 int err = PyObject_IsTrue(value);
1369 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 if (err == 0) {
1371 Py_INCREF(Py_True);
1372 SET_TOP(Py_True);
1373 DISPATCH();
1374 }
1375 else if (err > 0) {
1376 Py_INCREF(Py_False);
1377 SET_TOP(Py_False);
1378 err = 0;
1379 DISPATCH();
1380 }
1381 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001382 goto error;
1383 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001384
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001385 TARGET(UNARY_INVERT) {
1386 PyObject *value = TOP();
1387 PyObject *res = PyNumber_Invert(value);
1388 Py_DECREF(value);
1389 SET_TOP(res);
1390 if (res == NULL)
1391 goto error;
1392 DISPATCH();
1393 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001394
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001395 TARGET(BINARY_POWER) {
1396 PyObject *exp = POP();
1397 PyObject *base = TOP();
1398 PyObject *res = PyNumber_Power(base, exp, Py_None);
1399 Py_DECREF(base);
1400 Py_DECREF(exp);
1401 SET_TOP(res);
1402 if (res == NULL)
1403 goto error;
1404 DISPATCH();
1405 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001406
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001407 TARGET(BINARY_MULTIPLY) {
1408 PyObject *right = POP();
1409 PyObject *left = TOP();
1410 PyObject *res = PyNumber_Multiply(left, right);
1411 Py_DECREF(left);
1412 Py_DECREF(right);
1413 SET_TOP(res);
1414 if (res == NULL)
1415 goto error;
1416 DISPATCH();
1417 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001418
Benjamin Petersond51374e2014-04-09 23:55:56 -04001419 TARGET(BINARY_MATRIX_MULTIPLY) {
1420 PyObject *right = POP();
1421 PyObject *left = TOP();
1422 PyObject *res = PyNumber_MatrixMultiply(left, right);
1423 Py_DECREF(left);
1424 Py_DECREF(right);
1425 SET_TOP(res);
1426 if (res == NULL)
1427 goto error;
1428 DISPATCH();
1429 }
1430
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001431 TARGET(BINARY_TRUE_DIVIDE) {
1432 PyObject *divisor = POP();
1433 PyObject *dividend = TOP();
1434 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1435 Py_DECREF(dividend);
1436 Py_DECREF(divisor);
1437 SET_TOP(quotient);
1438 if (quotient == NULL)
1439 goto error;
1440 DISPATCH();
1441 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001442
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001443 TARGET(BINARY_FLOOR_DIVIDE) {
1444 PyObject *divisor = POP();
1445 PyObject *dividend = TOP();
1446 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1447 Py_DECREF(dividend);
1448 Py_DECREF(divisor);
1449 SET_TOP(quotient);
1450 if (quotient == NULL)
1451 goto error;
1452 DISPATCH();
1453 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001454
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001455 TARGET(BINARY_MODULO) {
1456 PyObject *divisor = POP();
1457 PyObject *dividend = TOP();
Martijn Pieters53039ad2017-02-27 16:08:01 +00001458 PyObject *res;
1459 if (PyUnicode_CheckExact(dividend) && (
1460 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1461 // fast path; string formatting, but not if the RHS is a str subclass
1462 // (see issue28598)
1463 res = PyUnicode_Format(dividend, divisor);
1464 } else {
1465 res = PyNumber_Remainder(dividend, divisor);
1466 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001467 Py_DECREF(divisor);
1468 Py_DECREF(dividend);
1469 SET_TOP(res);
1470 if (res == NULL)
1471 goto error;
1472 DISPATCH();
1473 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001474
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001475 TARGET(BINARY_ADD) {
1476 PyObject *right = POP();
1477 PyObject *left = TOP();
1478 PyObject *sum;
1479 if (PyUnicode_CheckExact(left) &&
1480 PyUnicode_CheckExact(right)) {
1481 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001482 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001483 }
1484 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001485 sum = PyNumber_Add(left, right);
1486 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001487 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001488 Py_DECREF(right);
1489 SET_TOP(sum);
1490 if (sum == NULL)
1491 goto error;
1492 DISPATCH();
1493 }
1494
1495 TARGET(BINARY_SUBTRACT) {
1496 PyObject *right = POP();
1497 PyObject *left = TOP();
1498 PyObject *diff = PyNumber_Subtract(left, right);
1499 Py_DECREF(right);
1500 Py_DECREF(left);
1501 SET_TOP(diff);
1502 if (diff == NULL)
1503 goto error;
1504 DISPATCH();
1505 }
1506
1507 TARGET(BINARY_SUBSCR) {
1508 PyObject *sub = POP();
1509 PyObject *container = TOP();
1510 PyObject *res = PyObject_GetItem(container, sub);
1511 Py_DECREF(container);
1512 Py_DECREF(sub);
1513 SET_TOP(res);
1514 if (res == NULL)
1515 goto error;
1516 DISPATCH();
1517 }
1518
1519 TARGET(BINARY_LSHIFT) {
1520 PyObject *right = POP();
1521 PyObject *left = TOP();
1522 PyObject *res = PyNumber_Lshift(left, right);
1523 Py_DECREF(left);
1524 Py_DECREF(right);
1525 SET_TOP(res);
1526 if (res == NULL)
1527 goto error;
1528 DISPATCH();
1529 }
1530
1531 TARGET(BINARY_RSHIFT) {
1532 PyObject *right = POP();
1533 PyObject *left = TOP();
1534 PyObject *res = PyNumber_Rshift(left, right);
1535 Py_DECREF(left);
1536 Py_DECREF(right);
1537 SET_TOP(res);
1538 if (res == NULL)
1539 goto error;
1540 DISPATCH();
1541 }
1542
1543 TARGET(BINARY_AND) {
1544 PyObject *right = POP();
1545 PyObject *left = TOP();
1546 PyObject *res = PyNumber_And(left, right);
1547 Py_DECREF(left);
1548 Py_DECREF(right);
1549 SET_TOP(res);
1550 if (res == NULL)
1551 goto error;
1552 DISPATCH();
1553 }
1554
1555 TARGET(BINARY_XOR) {
1556 PyObject *right = POP();
1557 PyObject *left = TOP();
1558 PyObject *res = PyNumber_Xor(left, right);
1559 Py_DECREF(left);
1560 Py_DECREF(right);
1561 SET_TOP(res);
1562 if (res == NULL)
1563 goto error;
1564 DISPATCH();
1565 }
1566
1567 TARGET(BINARY_OR) {
1568 PyObject *right = POP();
1569 PyObject *left = TOP();
1570 PyObject *res = PyNumber_Or(left, right);
1571 Py_DECREF(left);
1572 Py_DECREF(right);
1573 SET_TOP(res);
1574 if (res == NULL)
1575 goto error;
1576 DISPATCH();
1577 }
1578
1579 TARGET(LIST_APPEND) {
1580 PyObject *v = POP();
1581 PyObject *list = PEEK(oparg);
1582 int err;
1583 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001585 if (err != 0)
1586 goto error;
1587 PREDICT(JUMP_ABSOLUTE);
1588 DISPATCH();
1589 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001590
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001591 TARGET(SET_ADD) {
1592 PyObject *v = POP();
1593 PyObject *set = stack_pointer[-oparg];
1594 int err;
1595 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001597 if (err != 0)
1598 goto error;
1599 PREDICT(JUMP_ABSOLUTE);
1600 DISPATCH();
1601 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001602
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001603 TARGET(INPLACE_POWER) {
1604 PyObject *exp = POP();
1605 PyObject *base = TOP();
1606 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1607 Py_DECREF(base);
1608 Py_DECREF(exp);
1609 SET_TOP(res);
1610 if (res == NULL)
1611 goto error;
1612 DISPATCH();
1613 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001614
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001615 TARGET(INPLACE_MULTIPLY) {
1616 PyObject *right = POP();
1617 PyObject *left = TOP();
1618 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1619 Py_DECREF(left);
1620 Py_DECREF(right);
1621 SET_TOP(res);
1622 if (res == NULL)
1623 goto error;
1624 DISPATCH();
1625 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001626
Benjamin Petersond51374e2014-04-09 23:55:56 -04001627 TARGET(INPLACE_MATRIX_MULTIPLY) {
1628 PyObject *right = POP();
1629 PyObject *left = TOP();
1630 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1631 Py_DECREF(left);
1632 Py_DECREF(right);
1633 SET_TOP(res);
1634 if (res == NULL)
1635 goto error;
1636 DISPATCH();
1637 }
1638
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001639 TARGET(INPLACE_TRUE_DIVIDE) {
1640 PyObject *divisor = POP();
1641 PyObject *dividend = TOP();
1642 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1643 Py_DECREF(dividend);
1644 Py_DECREF(divisor);
1645 SET_TOP(quotient);
1646 if (quotient == NULL)
1647 goto error;
1648 DISPATCH();
1649 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001650
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001651 TARGET(INPLACE_FLOOR_DIVIDE) {
1652 PyObject *divisor = POP();
1653 PyObject *dividend = TOP();
1654 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1655 Py_DECREF(dividend);
1656 Py_DECREF(divisor);
1657 SET_TOP(quotient);
1658 if (quotient == NULL)
1659 goto error;
1660 DISPATCH();
1661 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001662
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001663 TARGET(INPLACE_MODULO) {
1664 PyObject *right = POP();
1665 PyObject *left = TOP();
1666 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1667 Py_DECREF(left);
1668 Py_DECREF(right);
1669 SET_TOP(mod);
1670 if (mod == NULL)
1671 goto error;
1672 DISPATCH();
1673 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001674
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001675 TARGET(INPLACE_ADD) {
1676 PyObject *right = POP();
1677 PyObject *left = TOP();
1678 PyObject *sum;
1679 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1680 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001681 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001682 }
1683 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001684 sum = PyNumber_InPlaceAdd(left, right);
1685 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001686 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001687 Py_DECREF(right);
1688 SET_TOP(sum);
1689 if (sum == NULL)
1690 goto error;
1691 DISPATCH();
1692 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001693
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001694 TARGET(INPLACE_SUBTRACT) {
1695 PyObject *right = POP();
1696 PyObject *left = TOP();
1697 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1698 Py_DECREF(left);
1699 Py_DECREF(right);
1700 SET_TOP(diff);
1701 if (diff == NULL)
1702 goto error;
1703 DISPATCH();
1704 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001705
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001706 TARGET(INPLACE_LSHIFT) {
1707 PyObject *right = POP();
1708 PyObject *left = TOP();
1709 PyObject *res = PyNumber_InPlaceLshift(left, right);
1710 Py_DECREF(left);
1711 Py_DECREF(right);
1712 SET_TOP(res);
1713 if (res == NULL)
1714 goto error;
1715 DISPATCH();
1716 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001717
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001718 TARGET(INPLACE_RSHIFT) {
1719 PyObject *right = POP();
1720 PyObject *left = TOP();
1721 PyObject *res = PyNumber_InPlaceRshift(left, right);
1722 Py_DECREF(left);
1723 Py_DECREF(right);
1724 SET_TOP(res);
1725 if (res == NULL)
1726 goto error;
1727 DISPATCH();
1728 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001729
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001730 TARGET(INPLACE_AND) {
1731 PyObject *right = POP();
1732 PyObject *left = TOP();
1733 PyObject *res = PyNumber_InPlaceAnd(left, right);
1734 Py_DECREF(left);
1735 Py_DECREF(right);
1736 SET_TOP(res);
1737 if (res == NULL)
1738 goto error;
1739 DISPATCH();
1740 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001741
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001742 TARGET(INPLACE_XOR) {
1743 PyObject *right = POP();
1744 PyObject *left = TOP();
1745 PyObject *res = PyNumber_InPlaceXor(left, right);
1746 Py_DECREF(left);
1747 Py_DECREF(right);
1748 SET_TOP(res);
1749 if (res == NULL)
1750 goto error;
1751 DISPATCH();
1752 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001753
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001754 TARGET(INPLACE_OR) {
1755 PyObject *right = POP();
1756 PyObject *left = TOP();
1757 PyObject *res = PyNumber_InPlaceOr(left, right);
1758 Py_DECREF(left);
1759 Py_DECREF(right);
1760 SET_TOP(res);
1761 if (res == NULL)
1762 goto error;
1763 DISPATCH();
1764 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001765
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001766 TARGET(STORE_SUBSCR) {
1767 PyObject *sub = TOP();
1768 PyObject *container = SECOND();
1769 PyObject *v = THIRD();
1770 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 STACKADJ(-3);
Martin Panter95f53c12016-07-18 08:23:26 +00001772 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001773 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001775 Py_DECREF(container);
1776 Py_DECREF(sub);
1777 if (err != 0)
1778 goto error;
1779 DISPATCH();
1780 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001781
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001782 TARGET(STORE_ANNOTATION) {
1783 _Py_IDENTIFIER(__annotations__);
1784 PyObject *ann_dict;
1785 PyObject *ann = POP();
1786 PyObject *name = GETITEM(names, oparg);
1787 int err;
1788 if (f->f_locals == NULL) {
1789 PyErr_Format(PyExc_SystemError,
1790 "no locals found when storing annotation");
1791 Py_DECREF(ann);
1792 goto error;
1793 }
1794 /* first try to get __annotations__ from locals... */
1795 if (PyDict_CheckExact(f->f_locals)) {
1796 ann_dict = _PyDict_GetItemId(f->f_locals,
1797 &PyId___annotations__);
1798 if (ann_dict == NULL) {
1799 PyErr_SetString(PyExc_NameError,
1800 "__annotations__ not found");
1801 Py_DECREF(ann);
1802 goto error;
1803 }
1804 Py_INCREF(ann_dict);
1805 }
1806 else {
1807 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
1808 if (ann_str == NULL) {
1809 Py_DECREF(ann);
1810 goto error;
1811 }
1812 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
1813 if (ann_dict == NULL) {
1814 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
1815 PyErr_SetString(PyExc_NameError,
1816 "__annotations__ not found");
1817 }
1818 Py_DECREF(ann);
1819 goto error;
1820 }
1821 }
1822 /* ...if succeeded, __annotations__[name] = ann */
1823 if (PyDict_CheckExact(ann_dict)) {
1824 err = PyDict_SetItem(ann_dict, name, ann);
1825 }
1826 else {
1827 err = PyObject_SetItem(ann_dict, name, ann);
1828 }
1829 Py_DECREF(ann_dict);
Yury Selivanov50c584f2016-09-08 23:38:21 -07001830 Py_DECREF(ann);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001831 if (err != 0) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001832 goto error;
1833 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001834 DISPATCH();
1835 }
1836
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001837 TARGET(DELETE_SUBSCR) {
1838 PyObject *sub = TOP();
1839 PyObject *container = SECOND();
1840 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001841 STACKADJ(-2);
Martin Panter95f53c12016-07-18 08:23:26 +00001842 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001843 err = PyObject_DelItem(container, sub);
1844 Py_DECREF(container);
1845 Py_DECREF(sub);
1846 if (err != 0)
1847 goto error;
1848 DISPATCH();
1849 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001850
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001851 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001852 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001853 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001854 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001855 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001856 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 PyErr_SetString(PyExc_RuntimeError,
1858 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001859 Py_DECREF(value);
1860 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001861 }
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001862 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001863 Py_DECREF(value);
1864 if (res == NULL)
1865 goto error;
1866 Py_DECREF(res);
1867 DISPATCH();
1868 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001869
Thomas Wouters434d0822000-08-24 20:11:32 +00001870#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001872#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001873 TARGET(RAISE_VARARGS) {
1874 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 switch (oparg) {
1876 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001877 cause = POP(); /* cause */
Victor Stinnerc0e77362017-09-12 16:09:44 -07001878 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001880 exc = POP(); /* exc */
Victor Stinnerc0e77362017-09-12 16:09:44 -07001881 /* fall through */
1882 case 0:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001883 if (do_raise(exc, cause)) {
1884 why = WHY_EXCEPTION;
1885 goto fast_block_end;
1886 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 break;
1888 default:
1889 PyErr_SetString(PyExc_SystemError,
1890 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 break;
1892 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001893 goto error;
1894 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001895
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001896 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001897 retval = POP();
1898 why = WHY_RETURN;
1899 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001900 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001901
Yury Selivanov75445082015-05-11 22:57:16 -04001902 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001903 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001904 PyObject *iter = NULL;
1905 PyObject *awaitable = NULL;
1906 PyObject *obj = TOP();
1907 PyTypeObject *type = Py_TYPE(obj);
1908
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001909 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001910 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001911 }
Yury Selivanov75445082015-05-11 22:57:16 -04001912
1913 if (getter != NULL) {
1914 iter = (*getter)(obj);
1915 Py_DECREF(obj);
1916 if (iter == NULL) {
1917 SET_TOP(NULL);
1918 goto error;
1919 }
1920 }
1921 else {
1922 SET_TOP(NULL);
1923 PyErr_Format(
1924 PyExc_TypeError,
1925 "'async for' requires an object with "
1926 "__aiter__ method, got %.100s",
1927 type->tp_name);
1928 Py_DECREF(obj);
1929 goto error;
1930 }
1931
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001932 if (Py_TYPE(iter)->tp_as_async != NULL &&
1933 Py_TYPE(iter)->tp_as_async->am_anext != NULL) {
1934
1935 /* Starting with CPython 3.5.2 __aiter__ should return
1936 asynchronous iterators directly (not awaitables that
1937 resolve to asynchronous iterators.)
1938
1939 Therefore, we check if the object that was returned
1940 from __aiter__ has an __anext__ method. If it does,
1941 we wrap it in an awaitable that resolves to `iter`.
1942
1943 See http://bugs.python.org/issue27243 for more
1944 details.
1945 */
1946
1947 PyObject *wrapper = _PyAIterWrapper_New(iter);
1948 Py_DECREF(iter);
1949 SET_TOP(wrapper);
1950 DISPATCH();
1951 }
1952
Yury Selivanov5376ba92015-06-22 12:19:30 -04001953 awaitable = _PyCoro_GetAwaitableIter(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001954 if (awaitable == NULL) {
Yury Selivanovdea51012017-03-02 22:20:00 -05001955 _PyErr_FormatFromCause(
Yury Selivanov75445082015-05-11 22:57:16 -04001956 PyExc_TypeError,
1957 "'async for' received an invalid object "
1958 "from __aiter__: %.100s",
1959 Py_TYPE(iter)->tp_name);
1960
Yury Selivanovdea51012017-03-02 22:20:00 -05001961 SET_TOP(NULL);
Yury Selivanov75445082015-05-11 22:57:16 -04001962 Py_DECREF(iter);
1963 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001964 } else {
Yury Selivanov75445082015-05-11 22:57:16 -04001965 Py_DECREF(iter);
1966
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001967 if (PyErr_WarnFormat(
Yury Selivanov2edd8a12016-11-08 15:13:07 -05001968 PyExc_DeprecationWarning, 1,
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001969 "'%.100s' implements legacy __aiter__ protocol; "
1970 "__aiter__ should return an asynchronous "
1971 "iterator, not awaitable",
1972 type->tp_name))
1973 {
1974 /* Warning was converted to an error. */
1975 Py_DECREF(awaitable);
1976 SET_TOP(NULL);
1977 goto error;
1978 }
1979 }
1980
Yury Selivanov75445082015-05-11 22:57:16 -04001981 SET_TOP(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001982 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001983 DISPATCH();
1984 }
1985
1986 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001987 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001988 PyObject *next_iter = NULL;
1989 PyObject *awaitable = NULL;
1990 PyObject *aiter = TOP();
1991 PyTypeObject *type = Py_TYPE(aiter);
1992
Yury Selivanoveb636452016-09-08 22:01:51 -07001993 if (PyAsyncGen_CheckExact(aiter)) {
1994 awaitable = type->tp_as_async->am_anext(aiter);
1995 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001996 goto error;
1997 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001998 } else {
1999 if (type->tp_as_async != NULL){
2000 getter = type->tp_as_async->am_anext;
2001 }
Yury Selivanov75445082015-05-11 22:57:16 -04002002
Yury Selivanoveb636452016-09-08 22:01:51 -07002003 if (getter != NULL) {
2004 next_iter = (*getter)(aiter);
2005 if (next_iter == NULL) {
2006 goto error;
2007 }
2008 }
2009 else {
2010 PyErr_Format(
2011 PyExc_TypeError,
2012 "'async for' requires an iterator with "
2013 "__anext__ method, got %.100s",
2014 type->tp_name);
2015 goto error;
2016 }
Yury Selivanov75445082015-05-11 22:57:16 -04002017
Yury Selivanoveb636452016-09-08 22:01:51 -07002018 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2019 if (awaitable == NULL) {
Yury Selivanovdea51012017-03-02 22:20:00 -05002020 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002021 PyExc_TypeError,
2022 "'async for' received an invalid object "
2023 "from __anext__: %.100s",
2024 Py_TYPE(next_iter)->tp_name);
2025
2026 Py_DECREF(next_iter);
2027 goto error;
2028 } else {
2029 Py_DECREF(next_iter);
2030 }
2031 }
Yury Selivanov75445082015-05-11 22:57:16 -04002032
2033 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002034 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002035 DISPATCH();
2036 }
2037
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002038 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002039 TARGET(GET_AWAITABLE) {
2040 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002041 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002042
2043 Py_DECREF(iterable);
2044
Yury Selivanovc724bae2016-03-02 11:30:46 -05002045 if (iter != NULL && PyCoro_CheckExact(iter)) {
2046 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2047 if (yf != NULL) {
2048 /* `iter` is a coroutine object that is being
2049 awaited, `yf` is a pointer to the current awaitable
2050 being awaited on. */
2051 Py_DECREF(yf);
2052 Py_CLEAR(iter);
2053 PyErr_SetString(
2054 PyExc_RuntimeError,
2055 "coroutine is being awaited already");
2056 /* The code below jumps to `error` if `iter` is NULL. */
2057 }
2058 }
2059
Yury Selivanov75445082015-05-11 22:57:16 -04002060 SET_TOP(iter); /* Even if it's NULL */
2061
2062 if (iter == NULL) {
2063 goto error;
2064 }
2065
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002066 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002067 DISPATCH();
2068 }
2069
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002070 TARGET(YIELD_FROM) {
2071 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002072 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002073 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002074 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2075 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002076 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002077 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002078 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002079 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002080 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002081 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002082 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002083 Py_DECREF(v);
2084 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002085 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002086 if (tstate->c_tracefunc != NULL
2087 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002088 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002089 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002090 if (err < 0)
2091 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002092 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002093 SET_TOP(val);
2094 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002095 }
Martin Panter95f53c12016-07-18 08:23:26 +00002096 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002097 f->f_stacktop = stack_pointer;
2098 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002099 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002100 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002101 f->f_lasti -= sizeof(_Py_CODEUNIT);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002102 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002103 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002104
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002105 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002106 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002107
2108 if (co->co_flags & CO_ASYNC_GENERATOR) {
2109 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2110 Py_DECREF(retval);
2111 if (w == NULL) {
2112 retval = NULL;
2113 goto error;
2114 }
2115 retval = w;
2116 }
2117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002118 f->f_stacktop = stack_pointer;
2119 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002120 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002121 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002122
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002123 TARGET(POP_EXCEPT) {
2124 PyTryBlock *b = PyFrame_BlockPop(f);
2125 if (b->b_type != EXCEPT_HANDLER) {
2126 PyErr_SetString(PyExc_SystemError,
2127 "popped block is not an except handler");
2128 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002129 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002130 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002131 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002132 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002133
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002134 PREDICTED(POP_BLOCK);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002135 TARGET(POP_BLOCK) {
2136 PyTryBlock *b = PyFrame_BlockPop(f);
2137 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002139 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002141 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002142 TARGET(END_FINALLY) {
2143 PyObject *status = POP();
2144 if (PyLong_Check(status)) {
2145 why = (enum why_code) PyLong_AS_LONG(status);
2146 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147 if (why == WHY_RETURN ||
2148 why == WHY_CONTINUE)
2149 retval = POP();
2150 if (why == WHY_SILENCED) {
2151 /* An exception was silenced by 'with', we must
2152 manually unwind the EXCEPT_HANDLER block which was
2153 created when the exception was caught, otherwise
2154 the stack will be in an inconsistent state. */
2155 PyTryBlock *b = PyFrame_BlockPop(f);
2156 assert(b->b_type == EXCEPT_HANDLER);
2157 UNWIND_EXCEPT_HANDLER(b);
2158 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002159 Py_DECREF(status);
2160 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002161 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002162 Py_DECREF(status);
2163 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002164 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002165 else if (PyExceptionClass_Check(status)) {
2166 PyObject *exc = POP();
2167 PyObject *tb = POP();
2168 PyErr_Restore(status, exc, tb);
2169 why = WHY_EXCEPTION;
2170 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002171 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002172 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173 PyErr_SetString(PyExc_SystemError,
2174 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002175 Py_DECREF(status);
2176 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002177 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002178 Py_DECREF(status);
2179 DISPATCH();
2180 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002181
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002182 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002183 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002184
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002185 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002186 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002187 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
2188 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002189 PyErr_SetString(PyExc_NameError,
2190 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002191 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002192 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002193 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002194 }
2195 else {
2196 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2197 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002198 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002199 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2200 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002201 if (PyErr_ExceptionMatches(PyExc_KeyError))
2202 PyErr_SetString(PyExc_NameError,
2203 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002204 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002205 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002206 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002207 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002208 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002209 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002210
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002211 TARGET(STORE_NAME) {
2212 PyObject *name = GETITEM(names, oparg);
2213 PyObject *v = POP();
2214 PyObject *ns = f->f_locals;
2215 int err;
2216 if (ns == NULL) {
2217 PyErr_Format(PyExc_SystemError,
2218 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002220 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002221 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002222 if (PyDict_CheckExact(ns))
2223 err = PyDict_SetItem(ns, name, v);
2224 else
2225 err = PyObject_SetItem(ns, name, v);
2226 Py_DECREF(v);
2227 if (err != 0)
2228 goto error;
2229 DISPATCH();
2230 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002231
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002232 TARGET(DELETE_NAME) {
2233 PyObject *name = GETITEM(names, oparg);
2234 PyObject *ns = f->f_locals;
2235 int err;
2236 if (ns == NULL) {
2237 PyErr_Format(PyExc_SystemError,
2238 "no locals when deleting %R", name);
2239 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002240 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002241 err = PyObject_DelItem(ns, name);
2242 if (err != 0) {
2243 format_exc_check_arg(PyExc_NameError,
2244 NAME_ERROR_MSG,
2245 name);
2246 goto error;
2247 }
2248 DISPATCH();
2249 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002250
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002251 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002252 TARGET(UNPACK_SEQUENCE) {
2253 PyObject *seq = POP(), *item, **items;
2254 if (PyTuple_CheckExact(seq) &&
2255 PyTuple_GET_SIZE(seq) == oparg) {
2256 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002258 item = items[oparg];
2259 Py_INCREF(item);
2260 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002262 } else if (PyList_CheckExact(seq) &&
2263 PyList_GET_SIZE(seq) == oparg) {
2264 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002266 item = items[oparg];
2267 Py_INCREF(item);
2268 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002269 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002270 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002271 stack_pointer + oparg)) {
2272 STACKADJ(oparg);
2273 } else {
2274 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002275 Py_DECREF(seq);
2276 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002278 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002279 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002280 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002281
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002282 TARGET(UNPACK_EX) {
2283 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2284 PyObject *seq = POP();
2285
2286 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2287 stack_pointer + totalargs)) {
2288 stack_pointer += totalargs;
2289 } else {
2290 Py_DECREF(seq);
2291 goto error;
2292 }
2293 Py_DECREF(seq);
2294 DISPATCH();
2295 }
2296
2297 TARGET(STORE_ATTR) {
2298 PyObject *name = GETITEM(names, oparg);
2299 PyObject *owner = TOP();
2300 PyObject *v = SECOND();
2301 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002303 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002304 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002305 Py_DECREF(owner);
2306 if (err != 0)
2307 goto error;
2308 DISPATCH();
2309 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002310
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002311 TARGET(DELETE_ATTR) {
2312 PyObject *name = GETITEM(names, oparg);
2313 PyObject *owner = POP();
2314 int err;
2315 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2316 Py_DECREF(owner);
2317 if (err != 0)
2318 goto error;
2319 DISPATCH();
2320 }
2321
2322 TARGET(STORE_GLOBAL) {
2323 PyObject *name = GETITEM(names, oparg);
2324 PyObject *v = POP();
2325 int err;
2326 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002327 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002328 if (err != 0)
2329 goto error;
2330 DISPATCH();
2331 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002332
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002333 TARGET(DELETE_GLOBAL) {
2334 PyObject *name = GETITEM(names, oparg);
2335 int err;
2336 err = PyDict_DelItem(f->f_globals, name);
2337 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002338 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002339 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002340 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002341 }
2342 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002343 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002344
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002345 TARGET(LOAD_NAME) {
2346 PyObject *name = GETITEM(names, oparg);
2347 PyObject *locals = f->f_locals;
2348 PyObject *v;
2349 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002350 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002351 "no locals when loading %R", name);
2352 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002354 if (PyDict_CheckExact(locals)) {
2355 v = PyDict_GetItem(locals, name);
2356 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 }
2358 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002359 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002360 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002361 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2362 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002363 PyErr_Clear();
2364 }
2365 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002366 if (v == NULL) {
2367 v = PyDict_GetItem(f->f_globals, name);
2368 Py_XINCREF(v);
2369 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002370 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002371 v = PyDict_GetItem(f->f_builtins, name);
2372 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002373 format_exc_check_arg(
2374 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002375 NAME_ERROR_MSG, name);
2376 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002377 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002378 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002379 }
2380 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002381 v = PyObject_GetItem(f->f_builtins, name);
2382 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002383 if (PyErr_ExceptionMatches(PyExc_KeyError))
2384 format_exc_check_arg(
2385 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002386 NAME_ERROR_MSG, name);
2387 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002388 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002389 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002392 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002394 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002395
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002396 TARGET(LOAD_GLOBAL) {
2397 PyObject *name = GETITEM(names, oparg);
2398 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002399 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002400 && PyDict_CheckExact(f->f_builtins))
2401 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002402 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002403 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002404 name);
2405 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002406 if (!_PyErr_OCCURRED()) {
2407 /* _PyDict_LoadGlobal() returns NULL without raising
2408 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002409 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002410 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002411 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002412 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002414 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002416 else {
2417 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002418
2419 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002420 v = PyObject_GetItem(f->f_globals, name);
2421 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002422 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2423 goto error;
2424 PyErr_Clear();
2425
Victor Stinnerb4efc962015-11-20 09:24:02 +01002426 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002427 v = PyObject_GetItem(f->f_builtins, name);
2428 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002429 if (PyErr_ExceptionMatches(PyExc_KeyError))
2430 format_exc_check_arg(
2431 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002432 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002433 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002434 }
2435 }
2436 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002437 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002438 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002439 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002440
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002441 TARGET(DELETE_FAST) {
2442 PyObject *v = GETLOCAL(oparg);
2443 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 SETLOCAL(oparg, NULL);
2445 DISPATCH();
2446 }
2447 format_exc_check_arg(
2448 PyExc_UnboundLocalError,
2449 UNBOUNDLOCAL_ERROR_MSG,
2450 PyTuple_GetItem(co->co_varnames, oparg)
2451 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002452 goto error;
2453 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002454
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002455 TARGET(DELETE_DEREF) {
2456 PyObject *cell = freevars[oparg];
2457 if (PyCell_GET(cell) != NULL) {
2458 PyCell_Set(cell, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002459 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002460 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002461 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002462 goto error;
2463 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002464
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002465 TARGET(LOAD_CLOSURE) {
2466 PyObject *cell = freevars[oparg];
2467 Py_INCREF(cell);
2468 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002469 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002470 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002471
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002472 TARGET(LOAD_CLASSDEREF) {
2473 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002474 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002475 assert(locals);
2476 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2477 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2478 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2479 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2480 if (PyDict_CheckExact(locals)) {
2481 value = PyDict_GetItem(locals, name);
2482 Py_XINCREF(value);
2483 }
2484 else {
2485 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002486 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002487 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2488 goto error;
2489 PyErr_Clear();
2490 }
2491 }
2492 if (!value) {
2493 PyObject *cell = freevars[oparg];
2494 value = PyCell_GET(cell);
2495 if (value == NULL) {
2496 format_exc_unbound(co, oparg);
2497 goto error;
2498 }
2499 Py_INCREF(value);
2500 }
2501 PUSH(value);
2502 DISPATCH();
2503 }
2504
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002505 TARGET(LOAD_DEREF) {
2506 PyObject *cell = freevars[oparg];
2507 PyObject *value = PyCell_GET(cell);
2508 if (value == NULL) {
2509 format_exc_unbound(co, oparg);
2510 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002511 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002512 Py_INCREF(value);
2513 PUSH(value);
2514 DISPATCH();
2515 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002516
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002517 TARGET(STORE_DEREF) {
2518 PyObject *v = POP();
2519 PyObject *cell = freevars[oparg];
Raymond Hettinger13527122016-11-11 04:31:18 -08002520 PyObject *oldobj = PyCell_GET(cell);
2521 PyCell_SET(cell, v);
2522 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002523 DISPATCH();
2524 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002525
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002526 TARGET(BUILD_STRING) {
2527 PyObject *str;
2528 PyObject *empty = PyUnicode_New(0, 0);
2529 if (empty == NULL) {
2530 goto error;
2531 }
2532 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2533 Py_DECREF(empty);
2534 if (str == NULL)
2535 goto error;
2536 while (--oparg >= 0) {
2537 PyObject *item = POP();
2538 Py_DECREF(item);
2539 }
2540 PUSH(str);
2541 DISPATCH();
2542 }
2543
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002544 TARGET(BUILD_TUPLE) {
2545 PyObject *tup = PyTuple_New(oparg);
2546 if (tup == NULL)
2547 goto error;
2548 while (--oparg >= 0) {
2549 PyObject *item = POP();
2550 PyTuple_SET_ITEM(tup, oparg, item);
2551 }
2552 PUSH(tup);
2553 DISPATCH();
2554 }
2555
2556 TARGET(BUILD_LIST) {
2557 PyObject *list = PyList_New(oparg);
2558 if (list == NULL)
2559 goto error;
2560 while (--oparg >= 0) {
2561 PyObject *item = POP();
2562 PyList_SET_ITEM(list, oparg, item);
2563 }
2564 PUSH(list);
2565 DISPATCH();
2566 }
2567
Serhiy Storchaka73442852016-10-02 10:33:46 +03002568 TARGET(BUILD_TUPLE_UNPACK_WITH_CALL)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002569 TARGET(BUILD_TUPLE_UNPACK)
2570 TARGET(BUILD_LIST_UNPACK) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002571 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002572 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002573 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002574 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002575
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002576 if (sum == NULL)
2577 goto error;
2578
2579 for (i = oparg; i > 0; i--) {
2580 PyObject *none_val;
2581
2582 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2583 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002584 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka946a0b62017-08-03 12:14:35 +03002585 PyErr_ExceptionMatches(PyExc_TypeError))
2586 {
2587 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002588 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002589 Py_DECREF(sum);
2590 goto error;
2591 }
2592 Py_DECREF(none_val);
2593 }
2594
2595 if (convert_to_tuple) {
2596 return_value = PyList_AsTuple(sum);
2597 Py_DECREF(sum);
2598 if (return_value == NULL)
2599 goto error;
2600 }
2601 else {
2602 return_value = sum;
2603 }
2604
2605 while (oparg--)
2606 Py_DECREF(POP());
2607 PUSH(return_value);
2608 DISPATCH();
2609 }
2610
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002611 TARGET(BUILD_SET) {
2612 PyObject *set = PySet_New(NULL);
2613 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002614 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002615 if (set == NULL)
2616 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002617 for (i = oparg; i > 0; i--) {
2618 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002619 if (err == 0)
2620 err = PySet_Add(set, item);
2621 Py_DECREF(item);
2622 }
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002623 STACKADJ(-oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002624 if (err != 0) {
2625 Py_DECREF(set);
2626 goto error;
2627 }
2628 PUSH(set);
2629 DISPATCH();
2630 }
2631
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002632 TARGET(BUILD_SET_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002633 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002634 PyObject *sum = PySet_New(NULL);
2635 if (sum == NULL)
2636 goto error;
2637
2638 for (i = oparg; i > 0; i--) {
2639 if (_PySet_Update(sum, PEEK(i)) < 0) {
2640 Py_DECREF(sum);
2641 goto error;
2642 }
2643 }
2644
2645 while (oparg--)
2646 Py_DECREF(POP());
2647 PUSH(sum);
2648 DISPATCH();
2649 }
2650
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002651 TARGET(BUILD_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002652 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002653 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2654 if (map == NULL)
2655 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002656 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002657 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002658 PyObject *key = PEEK(2*i);
2659 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002660 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002661 if (err != 0) {
2662 Py_DECREF(map);
2663 goto error;
2664 }
2665 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002666
2667 while (oparg--) {
2668 Py_DECREF(POP());
2669 Py_DECREF(POP());
2670 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002671 PUSH(map);
2672 DISPATCH();
2673 }
2674
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002675 TARGET(SETUP_ANNOTATIONS) {
2676 _Py_IDENTIFIER(__annotations__);
2677 int err;
2678 PyObject *ann_dict;
2679 if (f->f_locals == NULL) {
2680 PyErr_Format(PyExc_SystemError,
2681 "no locals found when setting up annotations");
2682 goto error;
2683 }
2684 /* check if __annotations__ in locals()... */
2685 if (PyDict_CheckExact(f->f_locals)) {
2686 ann_dict = _PyDict_GetItemId(f->f_locals,
2687 &PyId___annotations__);
2688 if (ann_dict == NULL) {
2689 /* ...if not, create a new one */
2690 ann_dict = PyDict_New();
2691 if (ann_dict == NULL) {
2692 goto error;
2693 }
2694 err = _PyDict_SetItemId(f->f_locals,
2695 &PyId___annotations__, ann_dict);
2696 Py_DECREF(ann_dict);
2697 if (err != 0) {
2698 goto error;
2699 }
2700 }
2701 }
2702 else {
2703 /* do the same if locals() is not a dict */
2704 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2705 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002706 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002707 }
2708 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2709 if (ann_dict == NULL) {
2710 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2711 goto error;
2712 }
2713 PyErr_Clear();
2714 ann_dict = PyDict_New();
2715 if (ann_dict == NULL) {
2716 goto error;
2717 }
2718 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2719 Py_DECREF(ann_dict);
2720 if (err != 0) {
2721 goto error;
2722 }
2723 }
2724 else {
2725 Py_DECREF(ann_dict);
2726 }
2727 }
2728 DISPATCH();
2729 }
2730
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002731 TARGET(BUILD_CONST_KEY_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002732 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002733 PyObject *map;
2734 PyObject *keys = TOP();
2735 if (!PyTuple_CheckExact(keys) ||
2736 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2737 PyErr_SetString(PyExc_SystemError,
2738 "bad BUILD_CONST_KEY_MAP keys argument");
2739 goto error;
2740 }
2741 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2742 if (map == NULL) {
2743 goto error;
2744 }
2745 for (i = oparg; i > 0; i--) {
2746 int err;
2747 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2748 PyObject *value = PEEK(i + 1);
2749 err = PyDict_SetItem(map, key, value);
2750 if (err != 0) {
2751 Py_DECREF(map);
2752 goto error;
2753 }
2754 }
2755
2756 Py_DECREF(POP());
2757 while (oparg--) {
2758 Py_DECREF(POP());
2759 }
2760 PUSH(map);
2761 DISPATCH();
2762 }
2763
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002764 TARGET(BUILD_MAP_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002765 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002766 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002767 if (sum == NULL)
2768 goto error;
2769
2770 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002771 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002772 if (PyDict_Update(sum, arg) < 0) {
2773 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2774 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002775 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002776 arg->ob_type->tp_name);
2777 }
2778 Py_DECREF(sum);
2779 goto error;
2780 }
2781 }
2782
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002783 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002784 Py_DECREF(POP());
2785 PUSH(sum);
2786 DISPATCH();
2787 }
2788
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002789 TARGET(BUILD_MAP_UNPACK_WITH_CALL) {
2790 Py_ssize_t i;
2791 PyObject *sum = PyDict_New();
2792 if (sum == NULL)
2793 goto error;
2794
2795 for (i = oparg; i > 0; i--) {
2796 PyObject *arg = PEEK(i);
2797 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
2798 PyObject *func = PEEK(2 + oparg);
2799 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka946a0b62017-08-03 12:14:35 +03002800 format_kwargs_mapping_error(func, arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002801 }
2802 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2803 PyObject *exc, *val, *tb;
2804 PyErr_Fetch(&exc, &val, &tb);
2805 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
2806 PyObject *key = PyTuple_GET_ITEM(val, 0);
2807 if (!PyUnicode_Check(key)) {
2808 PyErr_Format(PyExc_TypeError,
2809 "%.200s%.200s keywords must be strings",
2810 PyEval_GetFuncName(func),
2811 PyEval_GetFuncDesc(func));
2812 } else {
2813 PyErr_Format(PyExc_TypeError,
2814 "%.200s%.200s got multiple "
2815 "values for keyword argument '%U'",
2816 PyEval_GetFuncName(func),
2817 PyEval_GetFuncDesc(func),
2818 key);
2819 }
2820 Py_XDECREF(exc);
2821 Py_XDECREF(val);
2822 Py_XDECREF(tb);
2823 }
2824 else {
2825 PyErr_Restore(exc, val, tb);
2826 }
2827 }
2828 Py_DECREF(sum);
2829 goto error;
2830 }
2831 }
2832
2833 while (oparg--)
2834 Py_DECREF(POP());
2835 PUSH(sum);
2836 DISPATCH();
2837 }
2838
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002839 TARGET(MAP_ADD) {
2840 PyObject *key = TOP();
2841 PyObject *value = SECOND();
2842 PyObject *map;
2843 int err;
2844 STACKADJ(-2);
2845 map = stack_pointer[-oparg]; /* dict */
2846 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002847 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002848 Py_DECREF(value);
2849 Py_DECREF(key);
2850 if (err != 0)
2851 goto error;
2852 PREDICT(JUMP_ABSOLUTE);
2853 DISPATCH();
2854 }
2855
2856 TARGET(LOAD_ATTR) {
2857 PyObject *name = GETITEM(names, oparg);
2858 PyObject *owner = TOP();
2859 PyObject *res = PyObject_GetAttr(owner, name);
2860 Py_DECREF(owner);
2861 SET_TOP(res);
2862 if (res == NULL)
2863 goto error;
2864 DISPATCH();
2865 }
2866
2867 TARGET(COMPARE_OP) {
2868 PyObject *right = POP();
2869 PyObject *left = TOP();
2870 PyObject *res = cmp_outcome(oparg, left, right);
2871 Py_DECREF(left);
2872 Py_DECREF(right);
2873 SET_TOP(res);
2874 if (res == NULL)
2875 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 PREDICT(POP_JUMP_IF_FALSE);
2877 PREDICT(POP_JUMP_IF_TRUE);
2878 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002879 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002880
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002881 TARGET(IMPORT_NAME) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002882 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002883 PyObject *fromlist = POP();
2884 PyObject *level = TOP();
2885 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002886 res = import_name(f, name, fromlist, level);
2887 Py_DECREF(level);
2888 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002889 SET_TOP(res);
2890 if (res == NULL)
2891 goto error;
2892 DISPATCH();
2893 }
2894
2895 TARGET(IMPORT_STAR) {
2896 PyObject *from = POP(), *locals;
2897 int err;
Berker Peksag7accf202017-02-27 20:41:21 +03002898 if (PyFrame_FastToLocalsWithError(f) < 0) {
2899 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002900 goto error;
Berker Peksag7accf202017-02-27 20:41:21 +03002901 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002902
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002903 locals = f->f_locals;
2904 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 PyErr_SetString(PyExc_SystemError,
2906 "no locals found during 'import *'");
Berker Peksag7accf202017-02-27 20:41:21 +03002907 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002908 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002909 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002910 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002911 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002912 Py_DECREF(from);
2913 if (err != 0)
2914 goto error;
2915 DISPATCH();
2916 }
Guido van Rossum25831651993-05-19 14:50:45 +00002917
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002918 TARGET(IMPORT_FROM) {
2919 PyObject *name = GETITEM(names, oparg);
2920 PyObject *from = TOP();
2921 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002922 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002923 PUSH(res);
2924 if (res == NULL)
2925 goto error;
2926 DISPATCH();
2927 }
Thomas Wouters52152252000-08-17 22:55:00 +00002928
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002929 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002930 JUMPBY(oparg);
2931 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002932 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002933
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002934 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002935 TARGET(POP_JUMP_IF_FALSE) {
2936 PyObject *cond = POP();
2937 int err;
2938 if (cond == Py_True) {
2939 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002940 FAST_DISPATCH();
2941 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002942 if (cond == Py_False) {
2943 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002944 JUMPTO(oparg);
2945 FAST_DISPATCH();
2946 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002947 err = PyObject_IsTrue(cond);
2948 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002949 if (err > 0)
2950 err = 0;
2951 else if (err == 0)
2952 JUMPTO(oparg);
2953 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002954 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002955 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002956 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002957
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002958 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002959 TARGET(POP_JUMP_IF_TRUE) {
2960 PyObject *cond = POP();
2961 int err;
2962 if (cond == Py_False) {
2963 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002964 FAST_DISPATCH();
2965 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002966 if (cond == Py_True) {
2967 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002968 JUMPTO(oparg);
2969 FAST_DISPATCH();
2970 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002971 err = PyObject_IsTrue(cond);
2972 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002973 if (err > 0) {
2974 err = 0;
2975 JUMPTO(oparg);
2976 }
2977 else if (err == 0)
2978 ;
2979 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002980 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002981 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002982 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002983
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002984 TARGET(JUMP_IF_FALSE_OR_POP) {
2985 PyObject *cond = TOP();
2986 int err;
2987 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002988 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002989 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002990 FAST_DISPATCH();
2991 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002992 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002993 JUMPTO(oparg);
2994 FAST_DISPATCH();
2995 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002996 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002997 if (err > 0) {
2998 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002999 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003000 err = 0;
3001 }
3002 else if (err == 0)
3003 JUMPTO(oparg);
3004 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003005 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003006 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003007 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003008
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003009 TARGET(JUMP_IF_TRUE_OR_POP) {
3010 PyObject *cond = TOP();
3011 int err;
3012 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003013 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003014 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003015 FAST_DISPATCH();
3016 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003017 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003018 JUMPTO(oparg);
3019 FAST_DISPATCH();
3020 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003021 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003022 if (err > 0) {
3023 err = 0;
3024 JUMPTO(oparg);
3025 }
3026 else if (err == 0) {
3027 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003028 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003029 }
3030 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003031 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003032 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003033 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003034
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003035 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003036 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003038#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003039 /* Enabling this path speeds-up all while and for-loops by bypassing
3040 the per-loop checks for signals. By default, this should be turned-off
3041 because it prevents detection of a control-break in tight loops like
3042 "while 1: pass". Compile with this option turned-on when you need
3043 the speed-up and do not need break checking inside tight loops (ones
3044 that contain only instructions ending with FAST_DISPATCH).
3045 */
3046 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003047#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003048 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003049#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003050 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003051
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003052 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003053 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003054 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003055 PyObject *iter = PyObject_GetIter(iterable);
3056 Py_DECREF(iterable);
3057 SET_TOP(iter);
3058 if (iter == NULL)
3059 goto error;
3060 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003061 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003062 DISPATCH();
3063 }
3064
3065 TARGET(GET_YIELD_FROM_ITER) {
3066 /* before: [obj]; after [getiter(obj)] */
3067 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003068 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003069 if (PyCoro_CheckExact(iterable)) {
3070 /* `iterable` is a coroutine */
3071 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3072 /* and it is used in a 'yield from' expression of a
3073 regular generator. */
3074 Py_DECREF(iterable);
3075 SET_TOP(NULL);
3076 PyErr_SetString(PyExc_TypeError,
3077 "cannot 'yield from' a coroutine object "
3078 "in a non-coroutine generator");
3079 goto error;
3080 }
3081 }
3082 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003083 /* `iterable` is not a generator. */
3084 iter = PyObject_GetIter(iterable);
3085 Py_DECREF(iterable);
3086 SET_TOP(iter);
3087 if (iter == NULL)
3088 goto error;
3089 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003090 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003091 DISPATCH();
3092 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003093
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003094 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003095 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003096 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003097 PyObject *iter = TOP();
3098 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3099 if (next != NULL) {
3100 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003101 PREDICT(STORE_FAST);
3102 PREDICT(UNPACK_SEQUENCE);
3103 DISPATCH();
3104 }
3105 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003106 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
3107 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08003108 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003109 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003110 PyErr_Clear();
3111 }
3112 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003113 STACKADJ(-1);
3114 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003115 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003116 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003117 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003118 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003119
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003120 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003121 why = WHY_BREAK;
3122 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003123 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003124
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003125 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003126 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003127 if (retval == NULL)
3128 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003129 why = WHY_CONTINUE;
3130 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003131 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003132
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003133 TARGET(SETUP_LOOP)
3134 TARGET(SETUP_EXCEPT)
3135 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136 /* NOTE: If you add any new block-setup opcodes that
3137 are not try/except/finally handlers, you may need
3138 to update the PyGen_NeedsFinalizing() function.
3139 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003141 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
3142 STACK_LEVEL());
3143 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003144 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003145
Yury Selivanov75445082015-05-11 22:57:16 -04003146 TARGET(BEFORE_ASYNC_WITH) {
3147 _Py_IDENTIFIER(__aexit__);
3148 _Py_IDENTIFIER(__aenter__);
3149
3150 PyObject *mgr = TOP();
3151 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3152 *enter;
3153 PyObject *res;
3154 if (exit == NULL)
3155 goto error;
3156 SET_TOP(exit);
3157 enter = special_lookup(mgr, &PyId___aenter__);
3158 Py_DECREF(mgr);
3159 if (enter == NULL)
3160 goto error;
3161 res = PyObject_CallFunctionObjArgs(enter, NULL);
3162 Py_DECREF(enter);
3163 if (res == NULL)
3164 goto error;
3165 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003166 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003167 DISPATCH();
3168 }
3169
3170 TARGET(SETUP_ASYNC_WITH) {
3171 PyObject *res = POP();
3172 /* Setup the finally block before pushing the result
3173 of __aenter__ on the stack. */
3174 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3175 STACK_LEVEL());
3176 PUSH(res);
3177 DISPATCH();
3178 }
3179
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003180 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003181 _Py_IDENTIFIER(__exit__);
3182 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003183 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08003184 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003185 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08003186 if (enter == NULL)
3187 goto error;
3188 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003189 if (exit == NULL) {
3190 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003191 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003192 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003193 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003194 Py_DECREF(mgr);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003195 res = PyObject_CallFunctionObjArgs(enter, NULL);
3196 Py_DECREF(enter);
3197 if (res == NULL)
3198 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003199 /* Setup the finally block before pushing the result
3200 of __enter__ on the stack. */
3201 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3202 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003203
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003204 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003205 DISPATCH();
3206 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003207
Yury Selivanov75445082015-05-11 22:57:16 -04003208 TARGET(WITH_CLEANUP_START) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04003209 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003210 how/why we entered the finally clause:
3211 - TOP = None
3212 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
3213 - TOP = WHY_*; no retval below it
3214 - (TOP, SECOND, THIRD) = exc_info()
3215 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
3216 Below them is EXIT, the context.__exit__ bound method.
3217 In the last case, we must call
3218 EXIT(TOP, SECOND, THIRD)
3219 otherwise we must call
3220 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003221
Benjamin Peterson8f169482013-10-29 22:25:06 -04003222 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003223 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04003224 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003227 In addition, if the stack represents an exception,
3228 *and* the function call returns a 'true' value, we
3229 push WHY_SILENCED onto the stack. END_FINALLY will
3230 then not re-raise the exception. (But non-local
3231 gotos should still be resumed.)
3232 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003234 PyObject *exit_func;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003235 PyObject *exc = TOP(), *val = Py_None, *tb = Py_None, *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003236 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003237 (void)POP();
3238 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003239 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003240 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003241 else if (PyLong_Check(exc)) {
3242 STACKADJ(-1);
3243 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003244 case WHY_RETURN:
3245 case WHY_CONTINUE:
3246 /* Retval in TOP. */
3247 exit_func = SECOND();
3248 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003249 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003250 break;
3251 default:
3252 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003253 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003254 break;
3255 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003256 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003257 }
3258 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003259 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003260 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003261 val = SECOND();
3262 tb = THIRD();
3263 tp2 = FOURTH();
3264 exc2 = PEEK(5);
3265 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003266 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003267 SET_VALUE(7, tb2);
3268 SET_VALUE(6, exc2);
3269 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003270 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3271 SET_FOURTH(NULL);
3272 /* We just shifted the stack down, so we have
3273 to tell the except handler block that the
3274 values are lower than it expects. */
3275 block = &f->f_blockstack[f->f_iblock - 1];
3276 assert(block->b_type == EXCEPT_HANDLER);
3277 block->b_level--;
3278 }
3279 /* XXX Not the fastest way to call it... */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003280 res = PyObject_CallFunctionObjArgs(exit_func, exc, val, tb, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003281 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003282 if (res == NULL)
3283 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003284
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003285 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003286 PUSH(exc);
3287 PUSH(res);
3288 PREDICT(WITH_CLEANUP_FINISH);
3289 DISPATCH();
3290 }
3291
3292 PREDICTED(WITH_CLEANUP_FINISH);
3293 TARGET(WITH_CLEANUP_FINISH) {
3294 PyObject *res = POP();
3295 PyObject *exc = POP();
3296 int err;
3297
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003298 if (exc != Py_None)
3299 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003300 else
3301 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003302
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003303 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003304 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003306 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003307 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003308 else if (err > 0) {
3309 err = 0;
3310 /* There was an exception and a True return */
3311 PUSH(PyLong_FromLong((long) WHY_SILENCED));
3312 }
3313 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003314 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003316
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003317 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003318 TARGET(CALL_FUNCTION) {
3319 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003320 PCALL(PCALL_ALL);
3321 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003322 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003323 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003324 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003325 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003326 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003327 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003328 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003329 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003330
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003331 TARGET(CALL_FUNCTION_KW) {
3332 PyObject **sp, *res, *names;
3333
3334 names = POP();
3335 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003336 PCALL(PCALL_ALL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003337 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003338 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003339 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003340 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003341 Py_DECREF(names);
3342
3343 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003344 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003345 }
3346 DISPATCH();
3347 }
3348
3349 TARGET(CALL_FUNCTION_EX) {
3350 PyObject *func, *callargs, *kwargs = NULL, *result;
3351 PCALL(PCALL_ALL);
3352 if (oparg & 0x01) {
3353 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003354 if (!PyDict_CheckExact(kwargs)) {
3355 PyObject *d = PyDict_New();
3356 if (d == NULL)
3357 goto error;
3358 if (PyDict_Update(d, kwargs) != 0) {
3359 Py_DECREF(d);
3360 /* PyDict_Update raises attribute
3361 * error (percolated from an attempt
3362 * to get 'keys' attribute) instead of
3363 * a type error if its second argument
3364 * is not a mapping.
3365 */
3366 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka946a0b62017-08-03 12:14:35 +03003367 format_kwargs_mapping_error(SECOND(), kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003368 }
Victor Stinnereece2222016-09-12 11:16:37 +02003369 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003370 goto error;
3371 }
3372 Py_DECREF(kwargs);
3373 kwargs = d;
3374 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003375 assert(PyDict_CheckExact(kwargs));
3376 }
3377 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003378 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003379 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka946a0b62017-08-03 12:14:35 +03003380 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003381 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003382 goto error;
3383 }
3384 Py_SETREF(callargs, PySequence_Tuple(callargs));
3385 if (callargs == NULL) {
3386 goto error;
3387 }
3388 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003389 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003390
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003391 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003392 Py_DECREF(func);
3393 Py_DECREF(callargs);
3394 Py_XDECREF(kwargs);
3395
3396 SET_TOP(result);
3397 if (result == NULL) {
3398 goto error;
3399 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003400 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003401 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003402
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003403 TARGET(MAKE_FUNCTION) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003404 PyObject *qualname = POP();
3405 PyObject *codeobj = POP();
3406 PyFunctionObject *func = (PyFunctionObject *)
3407 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003408
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003409 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003410 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003411 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003412 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003413 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003414
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003415 if (oparg & 0x08) {
3416 assert(PyTuple_CheckExact(TOP()));
3417 func ->func_closure = POP();
3418 }
3419 if (oparg & 0x04) {
3420 assert(PyDict_CheckExact(TOP()));
3421 func->func_annotations = POP();
3422 }
3423 if (oparg & 0x02) {
3424 assert(PyDict_CheckExact(TOP()));
3425 func->func_kwdefaults = POP();
3426 }
3427 if (oparg & 0x01) {
3428 assert(PyTuple_CheckExact(TOP()));
3429 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003430 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003431
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003432 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003433 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003434 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003435
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003436 TARGET(BUILD_SLICE) {
3437 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003439 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003440 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003441 step = NULL;
3442 stop = POP();
3443 start = TOP();
3444 slice = PySlice_New(start, stop, step);
3445 Py_DECREF(start);
3446 Py_DECREF(stop);
3447 Py_XDECREF(step);
3448 SET_TOP(slice);
3449 if (slice == NULL)
3450 goto error;
3451 DISPATCH();
3452 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003453
Eric V. Smitha78c7952015-11-03 12:45:05 -05003454 TARGET(FORMAT_VALUE) {
3455 /* Handles f-string value formatting. */
3456 PyObject *result;
3457 PyObject *fmt_spec;
3458 PyObject *value;
3459 PyObject *(*conv_fn)(PyObject *);
3460 int which_conversion = oparg & FVC_MASK;
3461 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3462
3463 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003464 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003465
3466 /* See if any conversion is specified. */
3467 switch (which_conversion) {
3468 case FVC_STR: conv_fn = PyObject_Str; break;
3469 case FVC_REPR: conv_fn = PyObject_Repr; break;
3470 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3471
3472 /* Must be 0 (meaning no conversion), since only four
3473 values are allowed by (oparg & FVC_MASK). */
3474 default: conv_fn = NULL; break;
3475 }
3476
3477 /* If there's a conversion function, call it and replace
3478 value with that result. Otherwise, just use value,
3479 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003480 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003481 result = conv_fn(value);
3482 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003483 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003484 Py_XDECREF(fmt_spec);
3485 goto error;
3486 }
3487 value = result;
3488 }
3489
3490 /* If value is a unicode object, and there's no fmt_spec,
3491 then we know the result of format(value) is value
3492 itself. In that case, skip calling format(). I plan to
3493 move this optimization in to PyObject_Format()
3494 itself. */
3495 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3496 /* Do nothing, just transfer ownership to result. */
3497 result = value;
3498 } else {
3499 /* Actually call format(). */
3500 result = PyObject_Format(value, fmt_spec);
3501 Py_DECREF(value);
3502 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003503 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003504 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003505 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003506 }
3507
Eric V. Smith135d5f42016-02-05 18:23:08 -05003508 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003509 DISPATCH();
3510 }
3511
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003512 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003513 int oldoparg = oparg;
3514 NEXTOPARG();
3515 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003516 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003517 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003518
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003519
Antoine Pitrou042b1282010-08-13 21:15:58 +00003520#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003521 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003522#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003523 default:
3524 fprintf(stderr,
3525 "XXX lineno: %d, opcode: %d\n",
3526 PyFrame_GetLineNumber(f),
3527 opcode);
3528 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003529 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003530
3531#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003532 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003533#endif
3534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003535 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003536
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003537 /* This should never be reached. Every opcode should end with DISPATCH()
3538 or goto error. */
3539 assert(0);
Guido van Rossumac7be682001-01-17 15:42:30 +00003540
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003541error:
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003542
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003543 assert(why == WHY_NOT);
3544 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003545
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003546 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003547#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003548 if (!PyErr_Occurred())
3549 PyErr_SetString(PyExc_SystemError,
3550 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003551#else
3552 assert(PyErr_Occurred());
3553#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003554
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003555 /* Log traceback info. */
3556 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003557
Benjamin Peterson51f46162013-01-23 08:38:47 -05003558 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003559 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3560 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003561
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003562fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003563 assert(why != WHY_NOT);
3564
3565 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003566 while (why != WHY_NOT && f->f_iblock > 0) {
3567 /* Peek at the current block. */
3568 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003570 assert(why != WHY_YIELD);
3571 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3572 why = WHY_NOT;
3573 JUMPTO(PyLong_AS_LONG(retval));
3574 Py_DECREF(retval);
3575 break;
3576 }
3577 /* Now we have to pop the block. */
3578 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003580 if (b->b_type == EXCEPT_HANDLER) {
3581 UNWIND_EXCEPT_HANDLER(b);
3582 continue;
3583 }
3584 UNWIND_BLOCK(b);
3585 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3586 why = WHY_NOT;
3587 JUMPTO(b->b_handler);
3588 break;
3589 }
3590 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3591 || b->b_type == SETUP_FINALLY)) {
3592 PyObject *exc, *val, *tb;
3593 int handler = b->b_handler;
3594 /* Beware, this invalidates all b->b_* fields */
3595 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3596 PUSH(tstate->exc_traceback);
3597 PUSH(tstate->exc_value);
3598 if (tstate->exc_type != NULL) {
3599 PUSH(tstate->exc_type);
3600 }
3601 else {
3602 Py_INCREF(Py_None);
3603 PUSH(Py_None);
3604 }
3605 PyErr_Fetch(&exc, &val, &tb);
3606 /* Make the raw exception data
3607 available to the handler,
3608 so a program can emulate the
3609 Python main loop. */
3610 PyErr_NormalizeException(
3611 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003612 if (tb != NULL)
3613 PyException_SetTraceback(val, tb);
3614 else
3615 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003616 Py_INCREF(exc);
3617 tstate->exc_type = exc;
3618 Py_INCREF(val);
3619 tstate->exc_value = val;
3620 tstate->exc_traceback = tb;
3621 if (tb == NULL)
3622 tb = Py_None;
3623 Py_INCREF(tb);
3624 PUSH(tb);
3625 PUSH(val);
3626 PUSH(exc);
3627 why = WHY_NOT;
3628 JUMPTO(handler);
3629 break;
3630 }
3631 if (b->b_type == SETUP_FINALLY) {
3632 if (why & (WHY_RETURN | WHY_CONTINUE))
3633 PUSH(retval);
3634 PUSH(PyLong_FromLong((long)why));
3635 why = WHY_NOT;
3636 JUMPTO(b->b_handler);
3637 break;
3638 }
3639 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003641 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003643 if (why != WHY_NOT)
3644 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00003645
Victor Stinnerace47d72013-07-18 01:41:08 +02003646 assert(!PyErr_Occurred());
3647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003648 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003650 assert(why != WHY_YIELD);
3651 /* Pop remaining stack entries. */
3652 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003653 PyObject *o = POP();
3654 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003655 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003657 if (why != WHY_RETURN)
3658 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003659
Victor Stinner4a7cc882015-03-06 23:35:27 +01003660 assert((retval != NULL) ^ (PyErr_Occurred() != NULL));
Victor Stinnerace47d72013-07-18 01:41:08 +02003661
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003662fast_yield:
Yury Selivanoveb636452016-09-08 22:01:51 -07003663 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01003664
Benjamin Petersonac913412011-07-03 16:25:11 -05003665 /* The purpose of this block is to put aside the generator's exception
3666 state and restore that of the calling frame. If the current
3667 exception state is from the caller, we clear the exception values
3668 on the generator frame, so they are not swapped back in latter. The
3669 origin of the current exception state is determined by checking for
3670 except handler blocks, which we must be in iff a new exception
3671 state came into existence in this frame. (An uncaught exception
3672 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3673 int i;
Victor Stinner74319ae2016-08-25 00:04:09 +02003674 for (i = 0; i < f->f_iblock; i++) {
3675 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER) {
Benjamin Petersonac913412011-07-03 16:25:11 -05003676 break;
Victor Stinner74319ae2016-08-25 00:04:09 +02003677 }
3678 }
Benjamin Petersonac913412011-07-03 16:25:11 -05003679 if (i == f->f_iblock)
3680 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003681 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003682 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003683 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003684 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003686 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003687 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003688 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003689 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3690 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003691 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003692 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003693 why = WHY_EXCEPTION;
3694 }
3695 }
3696 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003697 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3698 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003699 PyTrace_RETURN, NULL);
3700 }
3701 }
3702 if (tstate->c_profilefunc) {
3703 if (why == WHY_EXCEPTION)
3704 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003705 tstate->c_profileobj,
3706 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003707 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003708 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
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);
Brett Cannonb94767f2011-02-22 20:15:44 +00003712 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003713 }
3714 }
3715 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003717 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003718exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003719 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3720 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003721 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003722 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003723 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003724
Victor Stinnerefde1462015-03-21 15:04:43 +01003725 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003726}
3727
Benjamin Petersonb204a422011-06-05 22:04:07 -05003728static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003729format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3730{
3731 int err;
3732 Py_ssize_t len = PyList_GET_SIZE(names);
3733 PyObject *name_str, *comma, *tail, *tmp;
3734
3735 assert(PyList_CheckExact(names));
3736 assert(len >= 1);
3737 /* Deal with the joys of natural language. */
3738 switch (len) {
3739 case 1:
3740 name_str = PyList_GET_ITEM(names, 0);
3741 Py_INCREF(name_str);
3742 break;
3743 case 2:
3744 name_str = PyUnicode_FromFormat("%U and %U",
3745 PyList_GET_ITEM(names, len - 2),
3746 PyList_GET_ITEM(names, len - 1));
3747 break;
3748 default:
3749 tail = PyUnicode_FromFormat(", %U, and %U",
3750 PyList_GET_ITEM(names, len - 2),
3751 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003752 if (tail == NULL)
3753 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003754 /* Chop off the last two objects in the list. This shouldn't actually
3755 fail, but we can't be too careful. */
3756 err = PyList_SetSlice(names, len - 2, len, NULL);
3757 if (err == -1) {
3758 Py_DECREF(tail);
3759 return;
3760 }
3761 /* Stitch everything up into a nice comma-separated list. */
3762 comma = PyUnicode_FromString(", ");
3763 if (comma == NULL) {
3764 Py_DECREF(tail);
3765 return;
3766 }
3767 tmp = PyUnicode_Join(comma, names);
3768 Py_DECREF(comma);
3769 if (tmp == NULL) {
3770 Py_DECREF(tail);
3771 return;
3772 }
3773 name_str = PyUnicode_Concat(tmp, tail);
3774 Py_DECREF(tmp);
3775 Py_DECREF(tail);
3776 break;
3777 }
3778 if (name_str == NULL)
3779 return;
3780 PyErr_Format(PyExc_TypeError,
3781 "%U() missing %i required %s argument%s: %U",
3782 co->co_name,
3783 len,
3784 kind,
3785 len == 1 ? "" : "s",
3786 name_str);
3787 Py_DECREF(name_str);
3788}
3789
3790static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003791missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003792 PyObject **fastlocals)
3793{
Victor Stinner74319ae2016-08-25 00:04:09 +02003794 Py_ssize_t i, j = 0;
3795 Py_ssize_t start, end;
3796 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003797 const char *kind = positional ? "positional" : "keyword-only";
3798 PyObject *missing_names;
3799
3800 /* Compute the names of the arguments that are missing. */
3801 missing_names = PyList_New(missing);
3802 if (missing_names == NULL)
3803 return;
3804 if (positional) {
3805 start = 0;
3806 end = co->co_argcount - defcount;
3807 }
3808 else {
3809 start = co->co_argcount;
3810 end = start + co->co_kwonlyargcount;
3811 }
3812 for (i = start; i < end; i++) {
3813 if (GETLOCAL(i) == NULL) {
3814 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3815 PyObject *name = PyObject_Repr(raw);
3816 if (name == NULL) {
3817 Py_DECREF(missing_names);
3818 return;
3819 }
3820 PyList_SET_ITEM(missing_names, j++, name);
3821 }
3822 }
3823 assert(j == missing);
3824 format_missing(kind, co, missing_names);
3825 Py_DECREF(missing_names);
3826}
3827
3828static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003829too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3830 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003831{
3832 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003833 Py_ssize_t kwonly_given = 0;
3834 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003835 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003836 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003837
Benjamin Petersone109c702011-06-24 09:37:26 -05003838 assert((co->co_flags & CO_VARARGS) == 0);
3839 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003840 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3841 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003842 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003843 }
3844 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003845 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003846 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003847 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003848 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003849 }
3850 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003851 plural = (co_argcount != 1);
3852 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003853 }
3854 if (sig == NULL)
3855 return;
3856 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003857 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3858 kwonly_sig = PyUnicode_FromFormat(format,
3859 given != 1 ? "s" : "",
3860 kwonly_given,
3861 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003862 if (kwonly_sig == NULL) {
3863 Py_DECREF(sig);
3864 return;
3865 }
3866 }
3867 else {
3868 /* This will not fail. */
3869 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003870 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003871 }
3872 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003873 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003874 co->co_name,
3875 sig,
3876 plural ? "s" : "",
3877 given,
3878 kwonly_sig,
3879 given == 1 && !kwonly_given ? "was" : "were");
3880 Py_DECREF(sig);
3881 Py_DECREF(kwonly_sig);
3882}
3883
Guido van Rossumc2e20742006-02-27 22:32:47 +00003884/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003885 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003886 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003887
Victor Stinner40ee3012014-06-16 15:59:28 +02003888static PyObject *
3889_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Victor Stinner74319ae2016-08-25 00:04:09 +02003890 PyObject **args, Py_ssize_t argcount,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003891 PyObject **kwnames, PyObject **kwargs,
3892 Py_ssize_t kwcount, int kwstep,
Victor Stinner74319ae2016-08-25 00:04:09 +02003893 PyObject **defs, Py_ssize_t defcount,
3894 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003895 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003896{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003897 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003898 PyFrameObject *f;
3899 PyObject *retval = NULL;
3900 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003901 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003902 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003903 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3904 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003905 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003906
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003907 if (globals == NULL) {
3908 PyErr_SetString(PyExc_SystemError,
3909 "PyEval_EvalCodeEx: NULL globals");
3910 return NULL;
3911 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003912
Victor Stinnerc7020012016-08-16 23:40:29 +02003913 /* Create the frame */
3914 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003915 assert(tstate != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003916 f = PyFrame_New(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003917 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003918 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003919 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003920 fastlocals = f->f_localsplus;
3921 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003922
Victor Stinnerc7020012016-08-16 23:40:29 +02003923 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003924 if (co->co_flags & CO_VARKEYWORDS) {
3925 kwdict = PyDict_New();
3926 if (kwdict == NULL)
3927 goto fail;
3928 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003929 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003930 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003931 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003932 SETLOCAL(i, kwdict);
3933 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003934 else {
3935 kwdict = NULL;
3936 }
3937
3938 /* Copy positional arguments into local variables */
3939 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003940 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003941 }
3942 else {
3943 n = argcount;
3944 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003945 for (i = 0; i < n; i++) {
3946 x = args[i];
3947 Py_INCREF(x);
3948 SETLOCAL(i, x);
3949 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003950
3951 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003952 if (co->co_flags & CO_VARARGS) {
3953 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003954 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003955 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003956 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003957 SETLOCAL(total_args, u);
3958 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003959 x = args[i];
3960 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003961 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003962 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003963 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003964
Serhiy Storchakab7281052016-09-12 00:52:40 +03003965 /* Handle keyword arguments passed as two strided arrays */
3966 kwcount *= kwstep;
3967 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003968 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003969 PyObject *keyword = kwnames[i];
3970 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003971 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003972
Benjamin Petersonb204a422011-06-05 22:04:07 -05003973 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3974 PyErr_Format(PyExc_TypeError,
3975 "%U() keywords must be strings",
3976 co->co_name);
3977 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003978 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003979
Benjamin Petersonb204a422011-06-05 22:04:07 -05003980 /* Speed hack: do raw pointer compares. As names are
3981 normally interned this should almost always hit. */
3982 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3983 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003984 PyObject *name = co_varnames[j];
3985 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003986 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003987 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003988 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003989
Benjamin Petersonb204a422011-06-05 22:04:07 -05003990 /* Slow fallback, just in case */
3991 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003992 PyObject *name = co_varnames[j];
3993 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3994 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003995 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003996 }
3997 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003998 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003999 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004000 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004001
Benjamin Petersonb204a422011-06-05 22:04:07 -05004002 if (j >= total_args && kwdict == NULL) {
4003 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02004004 "%U() got an unexpected keyword argument '%S'",
4005 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004006 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004007 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004008
Christian Heimes0bd447f2013-07-20 14:48:10 +02004009 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4010 goto fail;
4011 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004012 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004013
Benjamin Petersonb204a422011-06-05 22:04:07 -05004014 kw_found:
4015 if (GETLOCAL(j) != NULL) {
4016 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02004017 "%U() got multiple values for argument '%S'",
4018 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004019 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004020 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004021 Py_INCREF(value);
4022 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004023 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004024
4025 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004026 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004027 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004028 goto fail;
4029 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004030
4031 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004032 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02004033 Py_ssize_t m = co->co_argcount - defcount;
4034 Py_ssize_t missing = 0;
4035 for (i = argcount; i < m; i++) {
4036 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004037 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004038 }
4039 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004040 if (missing) {
4041 missing_arguments(co, missing, defcount, fastlocals);
4042 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004043 }
4044 if (n > m)
4045 i = n - m;
4046 else
4047 i = 0;
4048 for (; i < defcount; i++) {
4049 if (GETLOCAL(m+i) == NULL) {
4050 PyObject *def = defs[i];
4051 Py_INCREF(def);
4052 SETLOCAL(m+i, def);
4053 }
4054 }
4055 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004056
4057 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004058 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004059 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004060 for (i = co->co_argcount; i < total_args; i++) {
4061 PyObject *name;
4062 if (GETLOCAL(i) != NULL)
4063 continue;
4064 name = PyTuple_GET_ITEM(co->co_varnames, i);
4065 if (kwdefs != NULL) {
4066 PyObject *def = PyDict_GetItem(kwdefs, name);
4067 if (def) {
4068 Py_INCREF(def);
4069 SETLOCAL(i, def);
4070 continue;
4071 }
4072 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004073 missing++;
4074 }
4075 if (missing) {
4076 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004077 goto fail;
4078 }
4079 }
4080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004081 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004082 vars into frame. */
4083 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004084 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05004085 int arg;
4086 /* Possibly account for the cell variable being an argument. */
4087 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004088 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004089 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004090 /* Clear the local copy. */
4091 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004092 }
4093 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004094 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004095 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004096 if (c == NULL)
4097 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004098 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004099 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004100
4101 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004102 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4103 PyObject *o = PyTuple_GET_ITEM(closure, i);
4104 Py_INCREF(o);
4105 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004106 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004107
Yury Selivanoveb636452016-09-08 22:01:51 -07004108 /* Handle generator/coroutine/asynchronous generator */
4109 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004110 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04004111 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004112 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004113
4114 if (is_coro && tstate->in_coroutine_wrapper) {
4115 assert(coro_wrapper != NULL);
4116 PyErr_Format(PyExc_RuntimeError,
4117 "coroutine wrapper %.200R attempted "
4118 "to recursively wrap %.200R",
4119 coro_wrapper,
4120 co);
4121 goto fail;
4122 }
Yury Selivanov75445082015-05-11 22:57:16 -04004123
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004124 /* Don't need to keep the reference to f_back, it will be set
4125 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004126 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004128 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004129
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004130 /* Create a new generator that owns the ready to run frame
4131 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004132 if (is_coro) {
4133 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004134 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4135 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004136 } else {
4137 gen = PyGen_NewWithQualName(f, name, qualname);
4138 }
Yury Selivanov75445082015-05-11 22:57:16 -04004139 if (gen == NULL)
4140 return NULL;
4141
Yury Selivanov94c22632015-06-04 10:16:51 -04004142 if (is_coro && coro_wrapper != NULL) {
4143 PyObject *wrapped;
4144 tstate->in_coroutine_wrapper = 1;
4145 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4146 tstate->in_coroutine_wrapper = 0;
4147 return wrapped;
4148 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004149
Yury Selivanov75445082015-05-11 22:57:16 -04004150 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004151 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004153 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004154
Thomas Woutersce272b62007-09-19 21:19:28 +00004155fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004157 /* decref'ing the frame can cause __del__ methods to get invoked,
4158 which can call back into Python. While we're done with the
4159 current Python frame (f), the associated C stack is still in use,
4160 so recursion_depth must be boosted for the duration.
4161 */
4162 assert(tstate != NULL);
4163 ++tstate->recursion_depth;
4164 Py_DECREF(f);
4165 --tstate->recursion_depth;
4166 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004167}
4168
Victor Stinner40ee3012014-06-16 15:59:28 +02004169PyObject *
4170PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
4171 PyObject **args, int argcount, PyObject **kws, int kwcount,
4172 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
4173{
4174 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004175 args, argcount,
Zackery Spytzf032e922017-09-07 19:17:38 -06004176 kws, kws != NULL ? kws + 1 : NULL,
4177 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004178 defs, defcount,
4179 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004180 NULL, NULL);
4181}
Tim Peters5ca576e2001-06-18 22:08:13 +00004182
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004183static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004184special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004185{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004186 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004187 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004188 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004189 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004190 return NULL;
4191 }
4192 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004193}
4194
4195
Benjamin Peterson87880242011-07-03 16:48:31 -05004196/* These 3 functions deal with the exception state of generators. */
4197
4198static void
4199save_exc_state(PyThreadState *tstate, PyFrameObject *f)
4200{
4201 PyObject *type, *value, *traceback;
4202 Py_XINCREF(tstate->exc_type);
4203 Py_XINCREF(tstate->exc_value);
4204 Py_XINCREF(tstate->exc_traceback);
4205 type = f->f_exc_type;
4206 value = f->f_exc_value;
4207 traceback = f->f_exc_traceback;
4208 f->f_exc_type = tstate->exc_type;
4209 f->f_exc_value = tstate->exc_value;
4210 f->f_exc_traceback = tstate->exc_traceback;
4211 Py_XDECREF(type);
4212 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004213 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05004214}
4215
4216static void
4217swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
4218{
4219 PyObject *tmp;
4220 tmp = tstate->exc_type;
4221 tstate->exc_type = f->f_exc_type;
4222 f->f_exc_type = tmp;
4223 tmp = tstate->exc_value;
4224 tstate->exc_value = f->f_exc_value;
4225 f->f_exc_value = tmp;
4226 tmp = tstate->exc_traceback;
4227 tstate->exc_traceback = f->f_exc_traceback;
4228 f->f_exc_traceback = tmp;
4229}
4230
4231static void
4232restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
4233{
4234 PyObject *type, *value, *tb;
4235 type = tstate->exc_type;
4236 value = tstate->exc_value;
4237 tb = tstate->exc_traceback;
4238 tstate->exc_type = f->f_exc_type;
4239 tstate->exc_value = f->f_exc_value;
4240 tstate->exc_traceback = f->f_exc_traceback;
4241 f->f_exc_type = NULL;
4242 f->f_exc_value = NULL;
4243 f->f_exc_traceback = NULL;
4244 Py_XDECREF(type);
4245 Py_XDECREF(value);
4246 Py_XDECREF(tb);
4247}
4248
4249
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004250/* Logic for the raise statement (too complicated for inlining).
4251 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004252static int
Collin Winter828f04a2007-08-31 00:04:24 +00004253do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004255 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004257 if (exc == NULL) {
4258 /* Reraise */
4259 PyThreadState *tstate = PyThreadState_GET();
4260 PyObject *tb;
4261 type = tstate->exc_type;
4262 value = tstate->exc_value;
4263 tb = tstate->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004264 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004265 PyErr_SetString(PyExc_RuntimeError,
4266 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004267 return 0;
4268 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004269 Py_XINCREF(type);
4270 Py_XINCREF(value);
4271 Py_XINCREF(tb);
4272 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004273 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004274 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004276 /* We support the following forms of raise:
4277 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004278 raise <instance>
4279 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004281 if (PyExceptionClass_Check(exc)) {
4282 type = exc;
4283 value = PyObject_CallObject(exc, NULL);
4284 if (value == NULL)
4285 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004286 if (!PyExceptionInstance_Check(value)) {
4287 PyErr_Format(PyExc_TypeError,
4288 "calling %R should have returned an instance of "
4289 "BaseException, not %R",
4290 type, Py_TYPE(value));
4291 goto raise_error;
4292 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004293 }
4294 else if (PyExceptionInstance_Check(exc)) {
4295 value = exc;
4296 type = PyExceptionInstance_Class(exc);
4297 Py_INCREF(type);
4298 }
4299 else {
4300 /* Not something you can raise. You get an exception
4301 anyway, just not what you specified :-) */
4302 Py_DECREF(exc);
4303 PyErr_SetString(PyExc_TypeError,
4304 "exceptions must derive from BaseException");
4305 goto raise_error;
4306 }
Collin Winter828f04a2007-08-31 00:04:24 +00004307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004308 if (cause) {
4309 PyObject *fixed_cause;
4310 if (PyExceptionClass_Check(cause)) {
4311 fixed_cause = PyObject_CallObject(cause, NULL);
4312 if (fixed_cause == NULL)
4313 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004314 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004315 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004316 else if (PyExceptionInstance_Check(cause)) {
4317 fixed_cause = cause;
4318 }
4319 else if (cause == Py_None) {
4320 Py_DECREF(cause);
4321 fixed_cause = NULL;
4322 }
4323 else {
4324 PyErr_SetString(PyExc_TypeError,
4325 "exception causes must derive from "
4326 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004327 goto raise_error;
4328 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004329 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004330 }
Collin Winter828f04a2007-08-31 00:04:24 +00004331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004332 PyErr_SetObject(type, value);
4333 /* PyErr_SetObject incref's its arguments */
4334 Py_XDECREF(value);
4335 Py_XDECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004336 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004337
4338raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004339 Py_XDECREF(value);
4340 Py_XDECREF(type);
4341 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004342 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004343}
4344
Tim Petersd6d010b2001-06-21 02:49:55 +00004345/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004346 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004347
Guido van Rossum0368b722007-05-11 16:50:42 +00004348 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4349 with a variable target.
4350*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004351
Barry Warsawe42b18f1997-08-25 22:13:04 +00004352static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004353unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004354{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004355 int i = 0, j = 0;
4356 Py_ssize_t ll = 0;
4357 PyObject *it; /* iter(v) */
4358 PyObject *w;
4359 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004361 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004363 it = PyObject_GetIter(v);
4364 if (it == NULL)
4365 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00004366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004367 for (; i < argcnt; i++) {
4368 w = PyIter_Next(it);
4369 if (w == NULL) {
4370 /* Iterator done, via error or exhaustion. */
4371 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004372 if (argcntafter == -1) {
4373 PyErr_Format(PyExc_ValueError,
4374 "not enough values to unpack (expected %d, got %d)",
4375 argcnt, i);
4376 }
4377 else {
4378 PyErr_Format(PyExc_ValueError,
4379 "not enough values to unpack "
4380 "(expected at least %d, got %d)",
4381 argcnt + argcntafter, i);
4382 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004383 }
4384 goto Error;
4385 }
4386 *--sp = w;
4387 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004389 if (argcntafter == -1) {
4390 /* We better have exhausted the iterator now. */
4391 w = PyIter_Next(it);
4392 if (w == NULL) {
4393 if (PyErr_Occurred())
4394 goto Error;
4395 Py_DECREF(it);
4396 return 1;
4397 }
4398 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004399 PyErr_Format(PyExc_ValueError,
4400 "too many values to unpack (expected %d)",
4401 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004402 goto Error;
4403 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004405 l = PySequence_List(it);
4406 if (l == NULL)
4407 goto Error;
4408 *--sp = l;
4409 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004411 ll = PyList_GET_SIZE(l);
4412 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004413 PyErr_Format(PyExc_ValueError,
4414 "not enough values to unpack (expected at least %d, got %zd)",
4415 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004416 goto Error;
4417 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004419 /* Pop the "after-variable" args off the list. */
4420 for (j = argcntafter; j > 0; j--, i++) {
4421 *--sp = PyList_GET_ITEM(l, ll - j);
4422 }
4423 /* Resize the list. */
4424 Py_SIZE(l) = ll - argcntafter;
4425 Py_DECREF(it);
4426 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004427
Tim Petersd6d010b2001-06-21 02:49:55 +00004428Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004429 for (; i > 0; i--, sp++)
4430 Py_DECREF(*sp);
4431 Py_XDECREF(it);
4432 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004433}
4434
4435
Guido van Rossum96a42c81992-01-12 02:29:51 +00004436#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004437static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004438prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004439{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004440 printf("%s ", str);
4441 if (PyObject_Print(v, stdout, 0) != 0)
4442 PyErr_Clear(); /* Don't know what else to do */
4443 printf("\n");
4444 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004445}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004446#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004447
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004448static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004449call_exc_trace(Py_tracefunc func, PyObject *self,
4450 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004451{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004452 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004453 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004454 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004455 if (value == NULL) {
4456 value = Py_None;
4457 Py_INCREF(value);
4458 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004459 PyErr_NormalizeException(&type, &value, &orig_traceback);
4460 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004461 arg = PyTuple_Pack(3, type, value, traceback);
4462 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004463 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004464 return;
4465 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004466 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004467 Py_DECREF(arg);
4468 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004469 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004470 else {
4471 Py_XDECREF(type);
4472 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004473 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004474 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004475}
4476
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004477static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004478call_trace_protected(Py_tracefunc func, PyObject *obj,
4479 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004480 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004481{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004482 PyObject *type, *value, *traceback;
4483 int err;
4484 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004485 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004486 if (err == 0)
4487 {
4488 PyErr_Restore(type, value, traceback);
4489 return 0;
4490 }
4491 else {
4492 Py_XDECREF(type);
4493 Py_XDECREF(value);
4494 Py_XDECREF(traceback);
4495 return -1;
4496 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004497}
4498
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004499static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004500call_trace(Py_tracefunc func, PyObject *obj,
4501 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004502 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004503{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004504 int result;
4505 if (tstate->tracing)
4506 return 0;
4507 tstate->tracing++;
4508 tstate->use_tracing = 0;
4509 result = func(obj, frame, what, arg);
4510 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4511 || (tstate->c_profilefunc != NULL));
4512 tstate->tracing--;
4513 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004514}
4515
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004516PyObject *
4517_PyEval_CallTracing(PyObject *func, PyObject *args)
4518{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004519 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004520 int save_tracing = tstate->tracing;
4521 int save_use_tracing = tstate->use_tracing;
4522 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004524 tstate->tracing = 0;
4525 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4526 || (tstate->c_profilefunc != NULL));
4527 result = PyObject_Call(func, args, NULL);
4528 tstate->tracing = save_tracing;
4529 tstate->use_tracing = save_use_tracing;
4530 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004531}
4532
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004533/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004534static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004535maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004536 PyThreadState *tstate, PyFrameObject *frame,
4537 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004538{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004539 int result = 0;
4540 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004542 /* If the last instruction executed isn't in the current
4543 instruction window, reset the window.
4544 */
4545 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4546 PyAddrPair bounds;
4547 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4548 &bounds);
4549 *instr_lb = bounds.ap_lower;
4550 *instr_ub = bounds.ap_upper;
4551 }
4552 /* If the last instruction falls at the start of a line or if
4553 it represents a jump backwards, update the frame's line
4554 number and call the trace function. */
4555 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
4556 frame->f_lineno = line;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004557 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004558 }
4559 *instr_prev = frame->f_lasti;
4560 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004561}
4562
Fred Drake5755ce62001-06-27 19:19:46 +00004563void
4564PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004566 PyThreadState *tstate = PyThreadState_GET();
4567 PyObject *temp = tstate->c_profileobj;
4568 Py_XINCREF(arg);
4569 tstate->c_profilefunc = NULL;
4570 tstate->c_profileobj = NULL;
4571 /* Must make sure that tracing is not ignored if 'temp' is freed */
4572 tstate->use_tracing = tstate->c_tracefunc != NULL;
4573 Py_XDECREF(temp);
4574 tstate->c_profilefunc = func;
4575 tstate->c_profileobj = arg;
4576 /* Flag that tracing or profiling is turned on */
4577 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004578}
4579
4580void
4581PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004583 PyThreadState *tstate = PyThreadState_GET();
4584 PyObject *temp = tstate->c_traceobj;
4585 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4586 Py_XINCREF(arg);
4587 tstate->c_tracefunc = NULL;
4588 tstate->c_traceobj = NULL;
4589 /* Must make sure that profiling is not ignored if 'temp' is freed */
4590 tstate->use_tracing = tstate->c_profilefunc != NULL;
4591 Py_XDECREF(temp);
4592 tstate->c_tracefunc = func;
4593 tstate->c_traceobj = arg;
4594 /* Flag that tracing or profiling is turned on */
4595 tstate->use_tracing = ((func != NULL)
4596 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004597}
4598
Yury Selivanov75445082015-05-11 22:57:16 -04004599void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004600_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004601{
4602 PyThreadState *tstate = PyThreadState_GET();
4603
Yury Selivanov75445082015-05-11 22:57:16 -04004604 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004605 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004606}
4607
4608PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004609_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004610{
4611 PyThreadState *tstate = PyThreadState_GET();
4612 return tstate->coroutine_wrapper;
4613}
4614
Yury Selivanoveb636452016-09-08 22:01:51 -07004615void
4616_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4617{
4618 PyThreadState *tstate = PyThreadState_GET();
4619
4620 Py_XINCREF(firstiter);
4621 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4622}
4623
4624PyObject *
4625_PyEval_GetAsyncGenFirstiter(void)
4626{
4627 PyThreadState *tstate = PyThreadState_GET();
4628 return tstate->async_gen_firstiter;
4629}
4630
4631void
4632_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4633{
4634 PyThreadState *tstate = PyThreadState_GET();
4635
4636 Py_XINCREF(finalizer);
4637 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4638}
4639
4640PyObject *
4641_PyEval_GetAsyncGenFinalizer(void)
4642{
4643 PyThreadState *tstate = PyThreadState_GET();
4644 return tstate->async_gen_finalizer;
4645}
4646
Guido van Rossumb209a111997-04-29 18:18:01 +00004647PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004648PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004649{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004650 PyFrameObject *current_frame = PyEval_GetFrame();
4651 if (current_frame == NULL)
4652 return PyThreadState_GET()->interp->builtins;
4653 else
4654 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004655}
4656
Guido van Rossumb209a111997-04-29 18:18:01 +00004657PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004658PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004659{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004660 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004661 if (current_frame == NULL) {
4662 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004663 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004664 }
4665
4666 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4667 return NULL;
4668
4669 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004670 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004671}
4672
Guido van Rossumb209a111997-04-29 18:18:01 +00004673PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004674PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004675{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004676 PyFrameObject *current_frame = PyEval_GetFrame();
4677 if (current_frame == NULL)
4678 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004679
4680 assert(current_frame->f_globals != NULL);
4681 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004682}
4683
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004684PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004685PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004686{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004687 PyThreadState *tstate = PyThreadState_GET();
4688 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004689}
4690
Guido van Rossum6135a871995-01-09 17:53:26 +00004691int
Tim Peters5ba58662001-07-16 02:29:45 +00004692PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004693{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004694 PyFrameObject *current_frame = PyEval_GetFrame();
4695 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004697 if (current_frame != NULL) {
4698 const int codeflags = current_frame->f_code->co_flags;
4699 const int compilerflags = codeflags & PyCF_MASK;
4700 if (compilerflags) {
4701 result = 1;
4702 cf->cf_flags |= compilerflags;
4703 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004704#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004705 if (codeflags & CO_GENERATOR_ALLOWED) {
4706 result = 1;
4707 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4708 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004709#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004710 }
4711 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004712}
4713
Guido van Rossum3f5da241990-12-20 15:06:42 +00004714
Guido van Rossum681d79a1995-07-18 14:51:37 +00004715/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00004716 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00004717
Guido van Rossumb209a111997-04-29 18:18:01 +00004718PyObject *
Victor Stinner8a31c822016-08-19 17:12:23 +02004719PyEval_CallObjectWithKeywords(PyObject *func, PyObject *args, PyObject *kwargs)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004720{
Victor Stinner59b356d2015-03-16 11:52:32 +01004721#ifdef Py_DEBUG
4722 /* PyEval_CallObjectWithKeywords() must not be called with an exception
4723 set. It raises a new exception if parameters are invalid or if
4724 PyTuple_New() fails, and so the original exception is lost. */
4725 assert(!PyErr_Occurred());
4726#endif
4727
INADA Naoki023532e2017-03-01 21:14:43 +09004728 if (args != NULL && !PyTuple_Check(args)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004729 PyErr_SetString(PyExc_TypeError,
4730 "argument list must be a tuple");
4731 return NULL;
4732 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00004733
Victor Stinner8a31c822016-08-19 17:12:23 +02004734 if (kwargs != NULL && !PyDict_Check(kwargs)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004735 PyErr_SetString(PyExc_TypeError,
4736 "keyword list must be a dictionary");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004737 return NULL;
4738 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00004739
INADA Naoki023532e2017-03-01 21:14:43 +09004740 if (args == NULL) {
4741 return _PyObject_FastCallDict(func, NULL, 0, kwargs);
4742 }
4743 else {
4744 return PyObject_Call(func, args, kwargs);
4745 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004746}
4747
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004748const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004749PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004751 if (PyMethod_Check(func))
4752 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4753 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004754 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004755 else if (PyCFunction_Check(func))
4756 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4757 else
4758 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004759}
4760
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004761const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004762PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004763{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004764 if (PyMethod_Check(func))
4765 return "()";
4766 else if (PyFunction_Check(func))
4767 return "()";
4768 else if (PyCFunction_Check(func))
4769 return "()";
4770 else
4771 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004772}
4773
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004774#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004775if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004776 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4777 tstate, tstate->frame, \
4778 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004779 x = NULL; \
4780 } \
4781 else { \
4782 x = call; \
4783 if (tstate->c_profilefunc != NULL) { \
4784 if (x == NULL) { \
4785 call_trace_protected(tstate->c_profilefunc, \
4786 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004787 tstate, tstate->frame, \
4788 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004789 /* XXX should pass (type, value, tb) */ \
4790 } else { \
4791 if (call_trace(tstate->c_profilefunc, \
4792 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004793 tstate, tstate->frame, \
4794 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004795 Py_DECREF(x); \
4796 x = NULL; \
4797 } \
4798 } \
4799 } \
4800 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004801} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004802 x = call; \
4803 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004804
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004805static PyObject *
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004806call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004807{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004808 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004809 PyObject *func = *pfunc;
4810 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004811 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4812 Py_ssize_t nargs = oparg - nkwargs;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004813 PyObject **stack;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004815 /* Always dispatch PyCFunction first, because these are
4816 presumed to be the most frequent callable object.
4817 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004818 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004819 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004821 PCALL(PCALL_CFUNCTION);
Victor Stinner4a7cc882015-03-06 23:35:27 +01004822
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004823 stack = (*pp_stack) - nargs - nkwargs;
4824 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004825 }
4826 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004827 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4828 /* optimize access to bound methods */
4829 PyObject *self = PyMethod_GET_SELF(func);
4830 PCALL(PCALL_METHOD);
4831 PCALL(PCALL_BOUND_METHOD);
4832 Py_INCREF(self);
4833 func = PyMethod_GET_FUNCTION(func);
4834 Py_INCREF(func);
4835 Py_SETREF(*pfunc, self);
4836 nargs++;
4837 }
4838 else {
4839 Py_INCREF(func);
4840 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004841
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004842 stack = (*pp_stack) - nargs - nkwargs;
Victor Stinner4a7cc882015-03-06 23:35:27 +01004843
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004844 if (PyFunction_Check(func)) {
4845 x = fast_function(func, stack, nargs, kwnames);
4846 }
4847 else {
4848 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4849 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004850
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004851 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004852 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004853
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004854 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004856 /* Clear the stack of the function object. Also removes
4857 the arguments in case they weren't consumed already
Victor Stinnere90bdb12016-08-25 23:26:50 +02004858 (fast_function() and err_args() leave them on the stack).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004859 */
4860 while ((*pp_stack) > pfunc) {
4861 w = EXT_POP(*pp_stack);
4862 Py_DECREF(w);
4863 PCALL(PCALL_POP);
4864 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004866 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004867}
4868
Victor Stinnere90bdb12016-08-25 23:26:50 +02004869/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004870 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004871 For the simplest case -- a function that takes only positional
4872 arguments and is called with only positional arguments -- it
4873 inlines the most primitive frame setup code from
4874 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4875 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004876*/
4877
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004878static PyObject*
Victor Stinnerd8735722016-09-09 12:36:44 -07004879_PyFunction_FastCall(PyCodeObject *co, PyObject **args, Py_ssize_t nargs,
4880 PyObject *globals)
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004881{
4882 PyFrameObject *f;
4883 PyThreadState *tstate = PyThreadState_GET();
4884 PyObject **fastlocals;
4885 Py_ssize_t i;
4886 PyObject *result;
4887
4888 PCALL(PCALL_FASTER_FUNCTION);
4889 assert(globals != NULL);
4890 /* XXX Perhaps we should create a specialized
4891 PyFrame_New() that doesn't take locals, but does
4892 take builtins without sanity checking them.
4893 */
4894 assert(tstate != NULL);
4895 f = PyFrame_New(tstate, co, globals, NULL);
4896 if (f == NULL) {
4897 return NULL;
4898 }
4899
4900 fastlocals = f->f_localsplus;
4901
Victor Stinner74319ae2016-08-25 00:04:09 +02004902 for (i = 0; i < nargs; i++) {
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004903 Py_INCREF(*args);
4904 fastlocals[i] = *args++;
4905 }
4906 result = PyEval_EvalFrameEx(f,0);
4907
4908 ++tstate->recursion_depth;
4909 Py_DECREF(f);
4910 --tstate->recursion_depth;
4911
4912 return result;
4913}
4914
Victor Stinnere90bdb12016-08-25 23:26:50 +02004915static PyObject *
Victor Stinnerd8735722016-09-09 12:36:44 -07004916fast_function(PyObject *func, PyObject **stack,
4917 Py_ssize_t nargs, PyObject *kwnames)
Jeremy Hylton52820442001-01-03 23:52:36 +00004918{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004919 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4920 PyObject *globals = PyFunction_GET_GLOBALS(func);
4921 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004922 PyObject *kwdefs, *closure, *name, *qualname;
4923 PyObject **d;
Victor Stinnerd8735722016-09-09 12:36:44 -07004924 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004925 Py_ssize_t nd;
Victor Stinnerd8735722016-09-09 12:36:44 -07004926
Victor Stinner57f91ac2016-09-12 13:37:07 +02004927 assert(PyFunction_Check(func));
4928 assert(nargs >= 0);
4929 assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
Victor Stinnerd8735722016-09-09 12:36:44 -07004930 assert((nargs == 0 && nkwargs == 0) || stack != NULL);
Victor Stinner57f91ac2016-09-12 13:37:07 +02004931 /* kwnames must only contains str strings, no subclass, and all keys must
4932 be unique */
Victor Stinner577e1f82016-08-25 00:29:32 +02004933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004934 PCALL(PCALL_FUNCTION);
4935 PCALL(PCALL_FAST_FUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004936
Victor Stinner74319ae2016-08-25 00:04:09 +02004937 if (co->co_kwonlyargcount == 0 && nkwargs == 0 &&
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004938 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
4939 {
Victor Stinner2eedc112016-08-22 12:29:42 +02004940 if (argdefs == NULL && co->co_argcount == nargs) {
Victor Stinnerd8735722016-09-09 12:36:44 -07004941 return _PyFunction_FastCall(co, stack, nargs, globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02004942 }
4943 else if (nargs == 0 && argdefs != NULL
4944 && co->co_argcount == Py_SIZE(argdefs)) {
4945 /* function called with no arguments, but all parameters have
4946 a default value: use default values as arguments .*/
4947 stack = &PyTuple_GET_ITEM(argdefs, 0);
Victor Stinnerd8735722016-09-09 12:36:44 -07004948 return _PyFunction_FastCall(co, stack, Py_SIZE(argdefs), globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02004949 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004950 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004951
4952 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
4953 closure = PyFunction_GET_CLOSURE(func);
4954 name = ((PyFunctionObject *)func) -> func_name;
4955 qualname = ((PyFunctionObject *)func) -> func_qualname;
4956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004957 if (argdefs != NULL) {
4958 d = &PyTuple_GET_ITEM(argdefs, 0);
4959 nd = Py_SIZE(argdefs);
4960 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004961 else {
4962 d = NULL;
4963 nd = 0;
4964 }
4965 return _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
Victor Stinner2eedc112016-08-22 12:29:42 +02004966 stack, nargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004967 nkwargs ? &PyTuple_GET_ITEM(kwnames, 0) : NULL,
4968 stack + nargs,
4969 nkwargs, 1,
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004970 d, (int)nd, kwdefs,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004971 closure, name, qualname);
4972}
4973
4974PyObject *
Victor Stinnerd8735722016-09-09 12:36:44 -07004975_PyFunction_FastCallKeywords(PyObject *func, PyObject **stack,
4976 Py_ssize_t nargs, PyObject *kwnames)
4977{
4978 return fast_function(func, stack, nargs, kwnames);
4979}
4980
4981PyObject *
Victor Stinner74319ae2016-08-25 00:04:09 +02004982_PyFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs,
Victor Stinnerb9009392016-08-22 23:15:44 +02004983 PyObject *kwargs)
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004984{
4985 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4986 PyObject *globals = PyFunction_GET_GLOBALS(func);
4987 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4988 PyObject *kwdefs, *closure, *name, *qualname;
Victor Stinnerb9009392016-08-22 23:15:44 +02004989 PyObject *kwtuple, **k;
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004990 PyObject **d;
Victor Stinner74319ae2016-08-25 00:04:09 +02004991 Py_ssize_t nd, nk;
Victor Stinnerb9009392016-08-22 23:15:44 +02004992 PyObject *result;
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004993
Victor Stinner74319ae2016-08-25 00:04:09 +02004994 assert(func != NULL);
4995 assert(nargs >= 0);
4996 assert(nargs == 0 || args != NULL);
Victor Stinnerb9009392016-08-22 23:15:44 +02004997 assert(kwargs == NULL || PyDict_Check(kwargs));
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004998
Victor Stinner577e1f82016-08-25 00:29:32 +02004999 PCALL(PCALL_FUNCTION);
5000 PCALL(PCALL_FAST_FUNCTION);
5001
Victor Stinnerb9009392016-08-22 23:15:44 +02005002 if (co->co_kwonlyargcount == 0 &&
5003 (kwargs == NULL || PyDict_Size(kwargs) == 0) &&
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005004 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
5005 {
Victor Stinnerb9009392016-08-22 23:15:44 +02005006 /* Fast paths */
Victor Stinner2eedc112016-08-22 12:29:42 +02005007 if (argdefs == NULL && co->co_argcount == nargs) {
Victor Stinnerd8735722016-09-09 12:36:44 -07005008 return _PyFunction_FastCall(co, args, nargs, globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02005009 }
5010 else if (nargs == 0 && argdefs != NULL
5011 && co->co_argcount == Py_SIZE(argdefs)) {
5012 /* function called with no arguments, but all parameters have
5013 a default value: use default values as arguments .*/
5014 args = &PyTuple_GET_ITEM(argdefs, 0);
Victor Stinnerd8735722016-09-09 12:36:44 -07005015 return _PyFunction_FastCall(co, args, Py_SIZE(argdefs), globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02005016 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005017 }
5018
Victor Stinnerb9009392016-08-22 23:15:44 +02005019 if (kwargs != NULL) {
5020 Py_ssize_t pos, i;
5021 nk = PyDict_Size(kwargs);
5022
5023 kwtuple = PyTuple_New(2 * nk);
5024 if (kwtuple == NULL) {
5025 return NULL;
5026 }
5027
5028 k = &PyTuple_GET_ITEM(kwtuple, 0);
5029 pos = i = 0;
5030 while (PyDict_Next(kwargs, &pos, &k[i], &k[i+1])) {
5031 Py_INCREF(k[i]);
5032 Py_INCREF(k[i+1]);
5033 i += 2;
5034 }
5035 nk = i / 2;
5036 }
5037 else {
5038 kwtuple = NULL;
5039 k = NULL;
5040 nk = 0;
5041 }
5042
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005043 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
5044 closure = PyFunction_GET_CLOSURE(func);
5045 name = ((PyFunctionObject *)func) -> func_name;
5046 qualname = ((PyFunctionObject *)func) -> func_qualname;
5047
5048 if (argdefs != NULL) {
5049 d = &PyTuple_GET_ITEM(argdefs, 0);
5050 nd = Py_SIZE(argdefs);
5051 }
5052 else {
5053 d = NULL;
5054 nd = 0;
5055 }
Victor Stinnerb9009392016-08-22 23:15:44 +02005056
5057 result = _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
5058 args, nargs,
Zackery Spytzf032e922017-09-07 19:17:38 -06005059 k, k != NULL ? k + 1 : NULL, nk, 2,
Victor Stinnerb9009392016-08-22 23:15:44 +02005060 d, nd, kwdefs,
5061 closure, name, qualname);
5062 Py_XDECREF(kwtuple);
5063 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00005064}
5065
5066static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005067do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005068{
Jeremy Hylton985eba52003-02-05 23:13:00 +00005069#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005070 /* At this point, we have to look at the type of func to
5071 update the call stats properly. Do it here so as to avoid
5072 exposing the call stats machinery outside ceval.c
5073 */
5074 if (PyFunction_Check(func))
5075 PCALL(PCALL_FUNCTION);
5076 else if (PyMethod_Check(func))
5077 PCALL(PCALL_METHOD);
5078 else if (PyType_Check(func))
5079 PCALL(PCALL_TYPE);
5080 else if (PyCFunction_Check(func))
5081 PCALL(PCALL_CFUNCTION);
5082 else
5083 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00005084#endif
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005086 if (PyCFunction_Check(func)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005087 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005088 PyThreadState *tstate = PyThreadState_GET();
5089 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005090 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005091 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005092 else {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005093 return PyObject_Call(func, callargs, kwdict);
Victor Stinner74319ae2016-08-25 00:04:09 +02005094 }
Jeremy Hylton52820442001-01-03 23:52:36 +00005095}
5096
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005097/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005098 nb_index slot defined, and store in *pi.
5099 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang98b49a02017-05-10 19:00:15 +08005100 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005101 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005102*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005103int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005104_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005105{
Serhiy Storchakabf4bb2e2017-03-30 19:46:59 +03005106 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005107 Py_ssize_t x;
5108 if (PyIndex_Check(v)) {
5109 x = PyNumber_AsSsize_t(v, NULL);
5110 if (x == -1 && PyErr_Occurred())
5111 return 0;
5112 }
5113 else {
5114 PyErr_SetString(PyExc_TypeError,
5115 "slice indices must be integers or "
5116 "None or have an __index__ method");
5117 return 0;
5118 }
5119 *pi = x;
5120 }
5121 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005122}
5123
Serhiy Storchakabf4bb2e2017-03-30 19:46:59 +03005124int
5125_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
5126{
5127 Py_ssize_t x;
5128 if (PyIndex_Check(v)) {
5129 x = PyNumber_AsSsize_t(v, NULL);
5130 if (x == -1 && PyErr_Occurred())
5131 return 0;
5132 }
5133 else {
5134 PyErr_SetString(PyExc_TypeError,
5135 "slice indices must be integers or "
5136 "have an __index__ method");
5137 return 0;
5138 }
5139 *pi = x;
5140 return 1;
5141}
5142
5143
Guido van Rossum486364b2007-06-30 05:01:58 +00005144#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005145 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00005146
Guido van Rossumb209a111997-04-29 18:18:01 +00005147static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02005148cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005149{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005150 int res = 0;
5151 switch (op) {
5152 case PyCmp_IS:
5153 res = (v == w);
5154 break;
5155 case PyCmp_IS_NOT:
5156 res = (v != w);
5157 break;
5158 case PyCmp_IN:
5159 res = PySequence_Contains(w, v);
5160 if (res < 0)
5161 return NULL;
5162 break;
5163 case PyCmp_NOT_IN:
5164 res = PySequence_Contains(w, v);
5165 if (res < 0)
5166 return NULL;
5167 res = !res;
5168 break;
5169 case PyCmp_EXC_MATCH:
5170 if (PyTuple_Check(w)) {
5171 Py_ssize_t i, length;
5172 length = PyTuple_Size(w);
5173 for (i = 0; i < length; i += 1) {
5174 PyObject *exc = PyTuple_GET_ITEM(w, i);
5175 if (!PyExceptionClass_Check(exc)) {
5176 PyErr_SetString(PyExc_TypeError,
5177 CANNOT_CATCH_MSG);
5178 return NULL;
5179 }
5180 }
5181 }
5182 else {
5183 if (!PyExceptionClass_Check(w)) {
5184 PyErr_SetString(PyExc_TypeError,
5185 CANNOT_CATCH_MSG);
5186 return NULL;
5187 }
5188 }
5189 res = PyErr_GivenExceptionMatches(v, w);
5190 break;
5191 default:
5192 return PyObject_RichCompare(v, w, op);
5193 }
5194 v = res ? Py_True : Py_False;
5195 Py_INCREF(v);
5196 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005197}
5198
Thomas Wouters52152252000-08-17 22:55:00 +00005199static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005200import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
5201{
5202 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005203 PyObject *import_func, *res;
5204 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005205
5206 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
5207 if (import_func == NULL) {
5208 PyErr_SetString(PyExc_ImportError, "__import__ not found");
5209 return NULL;
5210 }
5211
5212 /* Fast path for not overloaded __import__. */
5213 if (import_func == PyThreadState_GET()->interp->import_func) {
5214 int ilevel = _PyLong_AsInt(level);
5215 if (ilevel == -1 && PyErr_Occurred()) {
5216 return NULL;
5217 }
5218 res = PyImport_ImportModuleLevelObject(
5219 name,
5220 f->f_globals,
5221 f->f_locals == NULL ? Py_None : f->f_locals,
5222 fromlist,
5223 ilevel);
5224 return res;
5225 }
5226
5227 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005228
5229 stack[0] = name;
5230 stack[1] = f->f_globals;
5231 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5232 stack[3] = fromlist;
5233 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005234 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005235 Py_DECREF(import_func);
5236 return res;
5237}
5238
5239static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00005240import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005241{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005242 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005243 _Py_IDENTIFIER(__name__);
5244 PyObject *fullmodname, *pkgname;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005246 x = PyObject_GetAttr(v, name);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005247 if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
5248 return x;
5249 /* Issue #17636: in case this failed because of a circular relative
5250 import, try to fallback on reading the module directly from
5251 sys.modules. */
5252 PyErr_Clear();
5253 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005254 if (pkgname == NULL) {
5255 goto error;
5256 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005257 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
5258 Py_DECREF(pkgname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005259 if (fullmodname == NULL) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005260 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005261 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005262 x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005263 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005264 if (x == NULL) {
5265 goto error;
5266 }
5267 Py_INCREF(x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005268 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005269 error:
5270 PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
5271 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005272}
Guido van Rossumac7be682001-01-17 15:42:30 +00005273
Thomas Wouters52152252000-08-17 22:55:00 +00005274static int
5275import_all_from(PyObject *locals, PyObject *v)
5276{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005277 _Py_IDENTIFIER(__all__);
5278 _Py_IDENTIFIER(__dict__);
5279 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005280 PyObject *dict, *name, *value;
5281 int skip_leading_underscores = 0;
5282 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005284 if (all == NULL) {
5285 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5286 return -1; /* Unexpected error */
5287 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005288 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005289 if (dict == NULL) {
5290 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5291 return -1;
5292 PyErr_SetString(PyExc_ImportError,
5293 "from-import-* object has no __dict__ and no __all__");
5294 return -1;
5295 }
5296 all = PyMapping_Keys(dict);
5297 Py_DECREF(dict);
5298 if (all == NULL)
5299 return -1;
5300 skip_leading_underscores = 1;
5301 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005303 for (pos = 0, err = 0; ; pos++) {
5304 name = PySequence_GetItem(all, pos);
5305 if (name == NULL) {
5306 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5307 err = -1;
5308 else
5309 PyErr_Clear();
5310 break;
5311 }
Serhiy Storchakaddb536b2017-09-08 10:43:54 +03005312 if (skip_leading_underscores && PyUnicode_Check(name)) {
5313 if (PyUnicode_READY(name) == -1) {
5314 Py_DECREF(name);
5315 err = -1;
5316 break;
5317 }
5318 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5319 Py_DECREF(name);
5320 continue;
5321 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005322 }
5323 value = PyObject_GetAttr(v, name);
5324 if (value == NULL)
5325 err = -1;
5326 else if (PyDict_CheckExact(locals))
5327 err = PyDict_SetItem(locals, name, value);
5328 else
5329 err = PyObject_SetItem(locals, name, value);
5330 Py_DECREF(name);
5331 Py_XDECREF(value);
5332 if (err != 0)
5333 break;
5334 }
5335 Py_DECREF(all);
5336 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005337}
5338
Serhiy Storchaka946a0b62017-08-03 12:14:35 +03005339static int
5340check_args_iterable(PyObject *func, PyObject *args)
5341{
5342 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
5343 PyErr_Format(PyExc_TypeError,
5344 "%.200s%.200s argument after * "
5345 "must be an iterable, not %.200s",
5346 PyEval_GetFuncName(func),
5347 PyEval_GetFuncDesc(func),
5348 args->ob_type->tp_name);
5349 return -1;
5350 }
5351 return 0;
5352}
5353
5354static void
5355format_kwargs_mapping_error(PyObject *func, PyObject *kwargs)
5356{
5357 PyErr_Format(PyExc_TypeError,
5358 "%.200s%.200s argument after ** "
5359 "must be a mapping, not %.200s",
5360 PyEval_GetFuncName(func),
5361 PyEval_GetFuncDesc(func),
5362 kwargs->ob_type->tp_name);
5363}
5364
Guido van Rossumac7be682001-01-17 15:42:30 +00005365static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005366format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005367{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005368 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005370 if (!obj)
5371 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005372
Serhiy Storchaka06515832016-11-20 09:13:07 +02005373 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005374 if (!obj_str)
5375 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005377 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005378}
Guido van Rossum950361c1997-01-24 13:49:28 +00005379
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005380static void
5381format_exc_unbound(PyCodeObject *co, int oparg)
5382{
5383 PyObject *name;
5384 /* Don't stomp existing exception */
5385 if (PyErr_Occurred())
5386 return;
5387 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5388 name = PyTuple_GET_ITEM(co->co_cellvars,
5389 oparg);
5390 format_exc_check_arg(
5391 PyExc_UnboundLocalError,
5392 UNBOUNDLOCAL_ERROR_MSG,
5393 name);
5394 } else {
5395 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5396 PyTuple_GET_SIZE(co->co_cellvars));
5397 format_exc_check_arg(PyExc_NameError,
5398 UNBOUNDFREE_ERROR_MSG, name);
5399 }
5400}
5401
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005402static PyObject *
5403unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005404 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005405{
5406 PyObject *res;
5407 if (Py_REFCNT(v) == 2) {
5408 /* In the common case, there are 2 references to the value
5409 * stored in 'variable' when the += is performed: one on the
5410 * value stack (in 'v') and one still stored in the
5411 * 'variable'. We try to delete the variable now to reduce
5412 * the refcnt to 1.
5413 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005414 int opcode, oparg;
5415 NEXTOPARG();
5416 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005417 case STORE_FAST:
5418 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005419 PyObject **fastlocals = f->f_localsplus;
5420 if (GETLOCAL(oparg) == v)
5421 SETLOCAL(oparg, NULL);
5422 break;
5423 }
5424 case STORE_DEREF:
5425 {
5426 PyObject **freevars = (f->f_localsplus +
5427 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005428 PyObject *c = freevars[oparg];
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005429 if (PyCell_GET(c) == v)
5430 PyCell_Set(c, NULL);
5431 break;
5432 }
5433 case STORE_NAME:
5434 {
5435 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005436 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005437 PyObject *locals = f->f_locals;
5438 if (PyDict_CheckExact(locals) &&
5439 PyDict_GetItem(locals, name) == v) {
5440 if (PyDict_DelItem(locals, name) != 0) {
5441 PyErr_Clear();
5442 }
5443 }
5444 break;
5445 }
5446 }
5447 }
5448 res = v;
5449 PyUnicode_Append(&res, w);
5450 return res;
5451}
5452
Guido van Rossum950361c1997-01-24 13:49:28 +00005453#ifdef DYNAMIC_EXECUTION_PROFILE
5454
Skip Montanarof118cb12001-10-15 20:51:38 +00005455static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005456getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005457{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005458 int i;
5459 PyObject *l = PyList_New(256);
5460 if (l == NULL) return NULL;
5461 for (i = 0; i < 256; i++) {
5462 PyObject *x = PyLong_FromLong(a[i]);
5463 if (x == NULL) {
5464 Py_DECREF(l);
5465 return NULL;
5466 }
5467 PyList_SetItem(l, i, x);
5468 }
5469 for (i = 0; i < 256; i++)
5470 a[i] = 0;
5471 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005472}
5473
5474PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005475_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005476{
5477#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005478 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005479#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005480 int i;
5481 PyObject *l = PyList_New(257);
5482 if (l == NULL) return NULL;
5483 for (i = 0; i < 257; i++) {
5484 PyObject *x = getarray(dxpairs[i]);
5485 if (x == NULL) {
5486 Py_DECREF(l);
5487 return NULL;
5488 }
5489 PyList_SetItem(l, i, x);
5490 }
5491 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005492#endif
5493}
5494
5495#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005496
5497Py_ssize_t
5498_PyEval_RequestCodeExtraIndex(freefunc free)
5499{
Dino Viehland2997fec2017-06-12 18:46:35 -07005500 __PyCodeExtraState *state = __PyCodeExtraState_Get();
Brett Cannon5c4de282016-09-07 11:16:41 -07005501 Py_ssize_t new_index;
5502
Dino Viehland2997fec2017-06-12 18:46:35 -07005503 if (state->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005504 return -1;
5505 }
Dino Viehland2997fec2017-06-12 18:46:35 -07005506 new_index = state->co_extra_user_count++;
5507 state->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005508 return new_index;
5509}
Łukasz Langaa785c872016-09-09 17:37:37 -07005510
5511static void
5512dtrace_function_entry(PyFrameObject *f)
5513{
5514 char* filename;
5515 char* funcname;
5516 int lineno;
5517
5518 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5519 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5520 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5521
5522 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5523}
5524
5525static void
5526dtrace_function_return(PyFrameObject *f)
5527{
5528 char* filename;
5529 char* funcname;
5530 int lineno;
5531
5532 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5533 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5534 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5535
5536 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5537}
5538
5539/* DTrace equivalent of maybe_call_line_trace. */
5540static void
5541maybe_dtrace_line(PyFrameObject *frame,
5542 int *instr_lb, int *instr_ub, int *instr_prev)
5543{
5544 int line = frame->f_lineno;
5545 char *co_filename, *co_name;
5546
5547 /* If the last instruction executed isn't in the current
5548 instruction window, reset the window.
5549 */
5550 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5551 PyAddrPair bounds;
5552 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5553 &bounds);
5554 *instr_lb = bounds.ap_lower;
5555 *instr_ub = bounds.ap_upper;
5556 }
5557 /* If the last instruction falls at the start of a line or if
5558 it represents a jump backwards, update the frame's line
5559 number and call the trace function. */
5560 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5561 frame->f_lineno = line;
5562 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5563 if (!co_filename)
5564 co_filename = "?";
5565 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5566 if (!co_name)
5567 co_name = "?";
5568 PyDTrace_LINE(co_filename, co_name, line);
5569 }
5570 *instr_prev = frame->f_lasti;
5571}