blob: 8eb78bf4d465882b36faba353b09f0cd90bbe273 [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 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001879 exc = POP(); /* exc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 case 0: /* Fallthrough */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001881 if (do_raise(exc, cause)) {
1882 why = WHY_EXCEPTION;
1883 goto fast_block_end;
1884 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885 break;
1886 default:
1887 PyErr_SetString(PyExc_SystemError,
1888 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 break;
1890 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001891 goto error;
1892 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001893
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001894 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 retval = POP();
1896 why = WHY_RETURN;
1897 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001898 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001899
Yury Selivanov75445082015-05-11 22:57:16 -04001900 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001901 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001902 PyObject *iter = NULL;
1903 PyObject *awaitable = NULL;
1904 PyObject *obj = TOP();
1905 PyTypeObject *type = Py_TYPE(obj);
1906
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001907 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001908 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001909 }
Yury Selivanov75445082015-05-11 22:57:16 -04001910
1911 if (getter != NULL) {
1912 iter = (*getter)(obj);
1913 Py_DECREF(obj);
1914 if (iter == NULL) {
1915 SET_TOP(NULL);
1916 goto error;
1917 }
1918 }
1919 else {
1920 SET_TOP(NULL);
1921 PyErr_Format(
1922 PyExc_TypeError,
1923 "'async for' requires an object with "
1924 "__aiter__ method, got %.100s",
1925 type->tp_name);
1926 Py_DECREF(obj);
1927 goto error;
1928 }
1929
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001930 if (Py_TYPE(iter)->tp_as_async != NULL &&
1931 Py_TYPE(iter)->tp_as_async->am_anext != NULL) {
1932
1933 /* Starting with CPython 3.5.2 __aiter__ should return
1934 asynchronous iterators directly (not awaitables that
1935 resolve to asynchronous iterators.)
1936
1937 Therefore, we check if the object that was returned
1938 from __aiter__ has an __anext__ method. If it does,
1939 we wrap it in an awaitable that resolves to `iter`.
1940
1941 See http://bugs.python.org/issue27243 for more
1942 details.
1943 */
1944
1945 PyObject *wrapper = _PyAIterWrapper_New(iter);
1946 Py_DECREF(iter);
1947 SET_TOP(wrapper);
1948 DISPATCH();
1949 }
1950
Yury Selivanov5376ba92015-06-22 12:19:30 -04001951 awaitable = _PyCoro_GetAwaitableIter(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001952 if (awaitable == NULL) {
Yury Selivanovdea51012017-03-02 22:20:00 -05001953 _PyErr_FormatFromCause(
Yury Selivanov75445082015-05-11 22:57:16 -04001954 PyExc_TypeError,
1955 "'async for' received an invalid object "
1956 "from __aiter__: %.100s",
1957 Py_TYPE(iter)->tp_name);
1958
Yury Selivanovdea51012017-03-02 22:20:00 -05001959 SET_TOP(NULL);
Yury Selivanov75445082015-05-11 22:57:16 -04001960 Py_DECREF(iter);
1961 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001962 } else {
Yury Selivanov75445082015-05-11 22:57:16 -04001963 Py_DECREF(iter);
1964
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001965 if (PyErr_WarnFormat(
Yury Selivanov2edd8a12016-11-08 15:13:07 -05001966 PyExc_DeprecationWarning, 1,
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001967 "'%.100s' implements legacy __aiter__ protocol; "
1968 "__aiter__ should return an asynchronous "
1969 "iterator, not awaitable",
1970 type->tp_name))
1971 {
1972 /* Warning was converted to an error. */
1973 Py_DECREF(awaitable);
1974 SET_TOP(NULL);
1975 goto error;
1976 }
1977 }
1978
Yury Selivanov75445082015-05-11 22:57:16 -04001979 SET_TOP(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001980 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001981 DISPATCH();
1982 }
1983
1984 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001985 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001986 PyObject *next_iter = NULL;
1987 PyObject *awaitable = NULL;
1988 PyObject *aiter = TOP();
1989 PyTypeObject *type = Py_TYPE(aiter);
1990
Yury Selivanoveb636452016-09-08 22:01:51 -07001991 if (PyAsyncGen_CheckExact(aiter)) {
1992 awaitable = type->tp_as_async->am_anext(aiter);
1993 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001994 goto error;
1995 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001996 } else {
1997 if (type->tp_as_async != NULL){
1998 getter = type->tp_as_async->am_anext;
1999 }
Yury Selivanov75445082015-05-11 22:57:16 -04002000
Yury Selivanoveb636452016-09-08 22:01:51 -07002001 if (getter != NULL) {
2002 next_iter = (*getter)(aiter);
2003 if (next_iter == NULL) {
2004 goto error;
2005 }
2006 }
2007 else {
2008 PyErr_Format(
2009 PyExc_TypeError,
2010 "'async for' requires an iterator with "
2011 "__anext__ method, got %.100s",
2012 type->tp_name);
2013 goto error;
2014 }
Yury Selivanov75445082015-05-11 22:57:16 -04002015
Yury Selivanoveb636452016-09-08 22:01:51 -07002016 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2017 if (awaitable == NULL) {
Yury Selivanovdea51012017-03-02 22:20:00 -05002018 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002019 PyExc_TypeError,
2020 "'async for' received an invalid object "
2021 "from __anext__: %.100s",
2022 Py_TYPE(next_iter)->tp_name);
2023
2024 Py_DECREF(next_iter);
2025 goto error;
2026 } else {
2027 Py_DECREF(next_iter);
2028 }
2029 }
Yury Selivanov75445082015-05-11 22:57:16 -04002030
2031 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002032 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002033 DISPATCH();
2034 }
2035
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002036 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002037 TARGET(GET_AWAITABLE) {
2038 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002039 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002040
2041 Py_DECREF(iterable);
2042
Yury Selivanovc724bae2016-03-02 11:30:46 -05002043 if (iter != NULL && PyCoro_CheckExact(iter)) {
2044 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2045 if (yf != NULL) {
2046 /* `iter` is a coroutine object that is being
2047 awaited, `yf` is a pointer to the current awaitable
2048 being awaited on. */
2049 Py_DECREF(yf);
2050 Py_CLEAR(iter);
2051 PyErr_SetString(
2052 PyExc_RuntimeError,
2053 "coroutine is being awaited already");
2054 /* The code below jumps to `error` if `iter` is NULL. */
2055 }
2056 }
2057
Yury Selivanov75445082015-05-11 22:57:16 -04002058 SET_TOP(iter); /* Even if it's NULL */
2059
2060 if (iter == NULL) {
2061 goto error;
2062 }
2063
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002064 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002065 DISPATCH();
2066 }
2067
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002068 TARGET(YIELD_FROM) {
2069 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002070 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002071 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002072 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2073 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002074 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002075 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002076 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002077 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002078 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002079 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002080 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002081 Py_DECREF(v);
2082 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002083 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002084 if (tstate->c_tracefunc != NULL
2085 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002086 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002087 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002088 if (err < 0)
2089 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002090 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002091 SET_TOP(val);
2092 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002093 }
Martin Panter95f53c12016-07-18 08:23:26 +00002094 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002095 f->f_stacktop = stack_pointer;
2096 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002097 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002098 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002099 f->f_lasti -= sizeof(_Py_CODEUNIT);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002100 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002101 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002102
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002103 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002104 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002105
2106 if (co->co_flags & CO_ASYNC_GENERATOR) {
2107 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2108 Py_DECREF(retval);
2109 if (w == NULL) {
2110 retval = NULL;
2111 goto error;
2112 }
2113 retval = w;
2114 }
2115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002116 f->f_stacktop = stack_pointer;
2117 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002118 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002119 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002120
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002121 TARGET(POP_EXCEPT) {
2122 PyTryBlock *b = PyFrame_BlockPop(f);
2123 if (b->b_type != EXCEPT_HANDLER) {
2124 PyErr_SetString(PyExc_SystemError,
2125 "popped block is not an except handler");
2126 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002127 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002128 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002129 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002130 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002131
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002132 PREDICTED(POP_BLOCK);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002133 TARGET(POP_BLOCK) {
2134 PyTryBlock *b = PyFrame_BlockPop(f);
2135 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002136 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002137 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002139 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002140 TARGET(END_FINALLY) {
2141 PyObject *status = POP();
2142 if (PyLong_Check(status)) {
2143 why = (enum why_code) PyLong_AS_LONG(status);
2144 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002145 if (why == WHY_RETURN ||
2146 why == WHY_CONTINUE)
2147 retval = POP();
2148 if (why == WHY_SILENCED) {
2149 /* An exception was silenced by 'with', we must
2150 manually unwind the EXCEPT_HANDLER block which was
2151 created when the exception was caught, otherwise
2152 the stack will be in an inconsistent state. */
2153 PyTryBlock *b = PyFrame_BlockPop(f);
2154 assert(b->b_type == EXCEPT_HANDLER);
2155 UNWIND_EXCEPT_HANDLER(b);
2156 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002157 Py_DECREF(status);
2158 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002159 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002160 Py_DECREF(status);
2161 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002163 else if (PyExceptionClass_Check(status)) {
2164 PyObject *exc = POP();
2165 PyObject *tb = POP();
2166 PyErr_Restore(status, exc, tb);
2167 why = WHY_EXCEPTION;
2168 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002170 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002171 PyErr_SetString(PyExc_SystemError,
2172 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002173 Py_DECREF(status);
2174 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002176 Py_DECREF(status);
2177 DISPATCH();
2178 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002179
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002180 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002181 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002182
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002183 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002184 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002185 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
2186 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002187 PyErr_SetString(PyExc_NameError,
2188 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002189 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002190 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002191 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002192 }
2193 else {
2194 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2195 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002196 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002197 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2198 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002199 if (PyErr_ExceptionMatches(PyExc_KeyError))
2200 PyErr_SetString(PyExc_NameError,
2201 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002202 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002203 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002204 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002205 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002206 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002207 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002208
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002209 TARGET(STORE_NAME) {
2210 PyObject *name = GETITEM(names, oparg);
2211 PyObject *v = POP();
2212 PyObject *ns = f->f_locals;
2213 int err;
2214 if (ns == NULL) {
2215 PyErr_Format(PyExc_SystemError,
2216 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002217 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002218 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002220 if (PyDict_CheckExact(ns))
2221 err = PyDict_SetItem(ns, name, v);
2222 else
2223 err = PyObject_SetItem(ns, name, v);
2224 Py_DECREF(v);
2225 if (err != 0)
2226 goto error;
2227 DISPATCH();
2228 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002229
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002230 TARGET(DELETE_NAME) {
2231 PyObject *name = GETITEM(names, oparg);
2232 PyObject *ns = f->f_locals;
2233 int err;
2234 if (ns == NULL) {
2235 PyErr_Format(PyExc_SystemError,
2236 "no locals when deleting %R", name);
2237 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002238 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002239 err = PyObject_DelItem(ns, name);
2240 if (err != 0) {
2241 format_exc_check_arg(PyExc_NameError,
2242 NAME_ERROR_MSG,
2243 name);
2244 goto error;
2245 }
2246 DISPATCH();
2247 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002248
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002249 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002250 TARGET(UNPACK_SEQUENCE) {
2251 PyObject *seq = POP(), *item, **items;
2252 if (PyTuple_CheckExact(seq) &&
2253 PyTuple_GET_SIZE(seq) == oparg) {
2254 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002255 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002256 item = items[oparg];
2257 Py_INCREF(item);
2258 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002259 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002260 } else if (PyList_CheckExact(seq) &&
2261 PyList_GET_SIZE(seq) == oparg) {
2262 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002264 item = items[oparg];
2265 Py_INCREF(item);
2266 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002267 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002268 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002269 stack_pointer + oparg)) {
2270 STACKADJ(oparg);
2271 } else {
2272 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002273 Py_DECREF(seq);
2274 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002276 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002277 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002279
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002280 TARGET(UNPACK_EX) {
2281 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2282 PyObject *seq = POP();
2283
2284 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2285 stack_pointer + totalargs)) {
2286 stack_pointer += totalargs;
2287 } else {
2288 Py_DECREF(seq);
2289 goto error;
2290 }
2291 Py_DECREF(seq);
2292 DISPATCH();
2293 }
2294
2295 TARGET(STORE_ATTR) {
2296 PyObject *name = GETITEM(names, oparg);
2297 PyObject *owner = TOP();
2298 PyObject *v = SECOND();
2299 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002301 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002303 Py_DECREF(owner);
2304 if (err != 0)
2305 goto error;
2306 DISPATCH();
2307 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002308
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002309 TARGET(DELETE_ATTR) {
2310 PyObject *name = GETITEM(names, oparg);
2311 PyObject *owner = POP();
2312 int err;
2313 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2314 Py_DECREF(owner);
2315 if (err != 0)
2316 goto error;
2317 DISPATCH();
2318 }
2319
2320 TARGET(STORE_GLOBAL) {
2321 PyObject *name = GETITEM(names, oparg);
2322 PyObject *v = POP();
2323 int err;
2324 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002325 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002326 if (err != 0)
2327 goto error;
2328 DISPATCH();
2329 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002330
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002331 TARGET(DELETE_GLOBAL) {
2332 PyObject *name = GETITEM(names, oparg);
2333 int err;
2334 err = PyDict_DelItem(f->f_globals, name);
2335 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002337 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002338 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002339 }
2340 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002341 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002342
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002343 TARGET(LOAD_NAME) {
2344 PyObject *name = GETITEM(names, oparg);
2345 PyObject *locals = f->f_locals;
2346 PyObject *v;
2347 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002348 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002349 "no locals when loading %R", name);
2350 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002352 if (PyDict_CheckExact(locals)) {
2353 v = PyDict_GetItem(locals, name);
2354 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002355 }
2356 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002357 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002358 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002359 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2360 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 PyErr_Clear();
2362 }
2363 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002364 if (v == NULL) {
2365 v = PyDict_GetItem(f->f_globals, name);
2366 Py_XINCREF(v);
2367 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002368 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002369 v = PyDict_GetItem(f->f_builtins, name);
2370 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002371 format_exc_check_arg(
2372 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002373 NAME_ERROR_MSG, name);
2374 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002375 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002376 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002377 }
2378 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002379 v = PyObject_GetItem(f->f_builtins, name);
2380 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002381 if (PyErr_ExceptionMatches(PyExc_KeyError))
2382 format_exc_check_arg(
2383 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002384 NAME_ERROR_MSG, name);
2385 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002386 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002387 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002390 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002392 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002393
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002394 TARGET(LOAD_GLOBAL) {
2395 PyObject *name = GETITEM(names, oparg);
2396 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002397 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002398 && PyDict_CheckExact(f->f_builtins))
2399 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002400 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002401 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002402 name);
2403 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002404 if (!_PyErr_OCCURRED()) {
2405 /* _PyDict_LoadGlobal() returns NULL without raising
2406 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002407 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002408 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002409 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002410 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002412 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002414 else {
2415 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002416
2417 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002418 v = PyObject_GetItem(f->f_globals, name);
2419 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002420 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2421 goto error;
2422 PyErr_Clear();
2423
Victor Stinnerb4efc962015-11-20 09:24:02 +01002424 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002425 v = PyObject_GetItem(f->f_builtins, name);
2426 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002427 if (PyErr_ExceptionMatches(PyExc_KeyError))
2428 format_exc_check_arg(
2429 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002430 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002431 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002432 }
2433 }
2434 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002435 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002437 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002438
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002439 TARGET(DELETE_FAST) {
2440 PyObject *v = GETLOCAL(oparg);
2441 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002442 SETLOCAL(oparg, NULL);
2443 DISPATCH();
2444 }
2445 format_exc_check_arg(
2446 PyExc_UnboundLocalError,
2447 UNBOUNDLOCAL_ERROR_MSG,
2448 PyTuple_GetItem(co->co_varnames, oparg)
2449 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002450 goto error;
2451 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002452
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002453 TARGET(DELETE_DEREF) {
2454 PyObject *cell = freevars[oparg];
2455 if (PyCell_GET(cell) != NULL) {
2456 PyCell_Set(cell, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002457 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002458 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002459 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002460 goto error;
2461 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002462
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002463 TARGET(LOAD_CLOSURE) {
2464 PyObject *cell = freevars[oparg];
2465 Py_INCREF(cell);
2466 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002467 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002468 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002469
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002470 TARGET(LOAD_CLASSDEREF) {
2471 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002472 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002473 assert(locals);
2474 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2475 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2476 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2477 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2478 if (PyDict_CheckExact(locals)) {
2479 value = PyDict_GetItem(locals, name);
2480 Py_XINCREF(value);
2481 }
2482 else {
2483 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002484 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002485 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2486 goto error;
2487 PyErr_Clear();
2488 }
2489 }
2490 if (!value) {
2491 PyObject *cell = freevars[oparg];
2492 value = PyCell_GET(cell);
2493 if (value == NULL) {
2494 format_exc_unbound(co, oparg);
2495 goto error;
2496 }
2497 Py_INCREF(value);
2498 }
2499 PUSH(value);
2500 DISPATCH();
2501 }
2502
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002503 TARGET(LOAD_DEREF) {
2504 PyObject *cell = freevars[oparg];
2505 PyObject *value = PyCell_GET(cell);
2506 if (value == NULL) {
2507 format_exc_unbound(co, oparg);
2508 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002509 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002510 Py_INCREF(value);
2511 PUSH(value);
2512 DISPATCH();
2513 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002514
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002515 TARGET(STORE_DEREF) {
2516 PyObject *v = POP();
2517 PyObject *cell = freevars[oparg];
Raymond Hettinger13527122016-11-11 04:31:18 -08002518 PyObject *oldobj = PyCell_GET(cell);
2519 PyCell_SET(cell, v);
2520 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002521 DISPATCH();
2522 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002523
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002524 TARGET(BUILD_STRING) {
2525 PyObject *str;
2526 PyObject *empty = PyUnicode_New(0, 0);
2527 if (empty == NULL) {
2528 goto error;
2529 }
2530 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2531 Py_DECREF(empty);
2532 if (str == NULL)
2533 goto error;
2534 while (--oparg >= 0) {
2535 PyObject *item = POP();
2536 Py_DECREF(item);
2537 }
2538 PUSH(str);
2539 DISPATCH();
2540 }
2541
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002542 TARGET(BUILD_TUPLE) {
2543 PyObject *tup = PyTuple_New(oparg);
2544 if (tup == NULL)
2545 goto error;
2546 while (--oparg >= 0) {
2547 PyObject *item = POP();
2548 PyTuple_SET_ITEM(tup, oparg, item);
2549 }
2550 PUSH(tup);
2551 DISPATCH();
2552 }
2553
2554 TARGET(BUILD_LIST) {
2555 PyObject *list = PyList_New(oparg);
2556 if (list == NULL)
2557 goto error;
2558 while (--oparg >= 0) {
2559 PyObject *item = POP();
2560 PyList_SET_ITEM(list, oparg, item);
2561 }
2562 PUSH(list);
2563 DISPATCH();
2564 }
2565
Serhiy Storchaka73442852016-10-02 10:33:46 +03002566 TARGET(BUILD_TUPLE_UNPACK_WITH_CALL)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002567 TARGET(BUILD_TUPLE_UNPACK)
2568 TARGET(BUILD_LIST_UNPACK) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002569 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002570 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002571 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002572 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002573
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002574 if (sum == NULL)
2575 goto error;
2576
2577 for (i = oparg; i > 0; i--) {
2578 PyObject *none_val;
2579
2580 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2581 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002582 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka946a0b62017-08-03 12:14:35 +03002583 PyErr_ExceptionMatches(PyExc_TypeError))
2584 {
2585 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002586 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002587 Py_DECREF(sum);
2588 goto error;
2589 }
2590 Py_DECREF(none_val);
2591 }
2592
2593 if (convert_to_tuple) {
2594 return_value = PyList_AsTuple(sum);
2595 Py_DECREF(sum);
2596 if (return_value == NULL)
2597 goto error;
2598 }
2599 else {
2600 return_value = sum;
2601 }
2602
2603 while (oparg--)
2604 Py_DECREF(POP());
2605 PUSH(return_value);
2606 DISPATCH();
2607 }
2608
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002609 TARGET(BUILD_SET) {
2610 PyObject *set = PySet_New(NULL);
2611 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002612 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002613 if (set == NULL)
2614 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002615 for (i = oparg; i > 0; i--) {
2616 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002617 if (err == 0)
2618 err = PySet_Add(set, item);
2619 Py_DECREF(item);
2620 }
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002621 STACKADJ(-oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002622 if (err != 0) {
2623 Py_DECREF(set);
2624 goto error;
2625 }
2626 PUSH(set);
2627 DISPATCH();
2628 }
2629
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002630 TARGET(BUILD_SET_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002631 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002632 PyObject *sum = PySet_New(NULL);
2633 if (sum == NULL)
2634 goto error;
2635
2636 for (i = oparg; i > 0; i--) {
2637 if (_PySet_Update(sum, PEEK(i)) < 0) {
2638 Py_DECREF(sum);
2639 goto error;
2640 }
2641 }
2642
2643 while (oparg--)
2644 Py_DECREF(POP());
2645 PUSH(sum);
2646 DISPATCH();
2647 }
2648
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002649 TARGET(BUILD_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002650 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002651 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2652 if (map == NULL)
2653 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002654 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002655 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002656 PyObject *key = PEEK(2*i);
2657 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002658 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002659 if (err != 0) {
2660 Py_DECREF(map);
2661 goto error;
2662 }
2663 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002664
2665 while (oparg--) {
2666 Py_DECREF(POP());
2667 Py_DECREF(POP());
2668 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002669 PUSH(map);
2670 DISPATCH();
2671 }
2672
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002673 TARGET(SETUP_ANNOTATIONS) {
2674 _Py_IDENTIFIER(__annotations__);
2675 int err;
2676 PyObject *ann_dict;
2677 if (f->f_locals == NULL) {
2678 PyErr_Format(PyExc_SystemError,
2679 "no locals found when setting up annotations");
2680 goto error;
2681 }
2682 /* check if __annotations__ in locals()... */
2683 if (PyDict_CheckExact(f->f_locals)) {
2684 ann_dict = _PyDict_GetItemId(f->f_locals,
2685 &PyId___annotations__);
2686 if (ann_dict == NULL) {
2687 /* ...if not, create a new one */
2688 ann_dict = PyDict_New();
2689 if (ann_dict == NULL) {
2690 goto error;
2691 }
2692 err = _PyDict_SetItemId(f->f_locals,
2693 &PyId___annotations__, ann_dict);
2694 Py_DECREF(ann_dict);
2695 if (err != 0) {
2696 goto error;
2697 }
2698 }
2699 }
2700 else {
2701 /* do the same if locals() is not a dict */
2702 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2703 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002704 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002705 }
2706 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2707 if (ann_dict == NULL) {
2708 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2709 goto error;
2710 }
2711 PyErr_Clear();
2712 ann_dict = PyDict_New();
2713 if (ann_dict == NULL) {
2714 goto error;
2715 }
2716 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2717 Py_DECREF(ann_dict);
2718 if (err != 0) {
2719 goto error;
2720 }
2721 }
2722 else {
2723 Py_DECREF(ann_dict);
2724 }
2725 }
2726 DISPATCH();
2727 }
2728
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002729 TARGET(BUILD_CONST_KEY_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002730 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002731 PyObject *map;
2732 PyObject *keys = TOP();
2733 if (!PyTuple_CheckExact(keys) ||
2734 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2735 PyErr_SetString(PyExc_SystemError,
2736 "bad BUILD_CONST_KEY_MAP keys argument");
2737 goto error;
2738 }
2739 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2740 if (map == NULL) {
2741 goto error;
2742 }
2743 for (i = oparg; i > 0; i--) {
2744 int err;
2745 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2746 PyObject *value = PEEK(i + 1);
2747 err = PyDict_SetItem(map, key, value);
2748 if (err != 0) {
2749 Py_DECREF(map);
2750 goto error;
2751 }
2752 }
2753
2754 Py_DECREF(POP());
2755 while (oparg--) {
2756 Py_DECREF(POP());
2757 }
2758 PUSH(map);
2759 DISPATCH();
2760 }
2761
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002762 TARGET(BUILD_MAP_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002763 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002764 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002765 if (sum == NULL)
2766 goto error;
2767
2768 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002769 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002770 if (PyDict_Update(sum, arg) < 0) {
2771 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2772 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002773 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002774 arg->ob_type->tp_name);
2775 }
2776 Py_DECREF(sum);
2777 goto error;
2778 }
2779 }
2780
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002781 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002782 Py_DECREF(POP());
2783 PUSH(sum);
2784 DISPATCH();
2785 }
2786
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002787 TARGET(BUILD_MAP_UNPACK_WITH_CALL) {
2788 Py_ssize_t i;
2789 PyObject *sum = PyDict_New();
2790 if (sum == NULL)
2791 goto error;
2792
2793 for (i = oparg; i > 0; i--) {
2794 PyObject *arg = PEEK(i);
2795 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
2796 PyObject *func = PEEK(2 + oparg);
2797 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka946a0b62017-08-03 12:14:35 +03002798 format_kwargs_mapping_error(func, arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002799 }
2800 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2801 PyObject *exc, *val, *tb;
2802 PyErr_Fetch(&exc, &val, &tb);
2803 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
2804 PyObject *key = PyTuple_GET_ITEM(val, 0);
2805 if (!PyUnicode_Check(key)) {
2806 PyErr_Format(PyExc_TypeError,
2807 "%.200s%.200s keywords must be strings",
2808 PyEval_GetFuncName(func),
2809 PyEval_GetFuncDesc(func));
2810 } else {
2811 PyErr_Format(PyExc_TypeError,
2812 "%.200s%.200s got multiple "
2813 "values for keyword argument '%U'",
2814 PyEval_GetFuncName(func),
2815 PyEval_GetFuncDesc(func),
2816 key);
2817 }
2818 Py_XDECREF(exc);
2819 Py_XDECREF(val);
2820 Py_XDECREF(tb);
2821 }
2822 else {
2823 PyErr_Restore(exc, val, tb);
2824 }
2825 }
2826 Py_DECREF(sum);
2827 goto error;
2828 }
2829 }
2830
2831 while (oparg--)
2832 Py_DECREF(POP());
2833 PUSH(sum);
2834 DISPATCH();
2835 }
2836
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002837 TARGET(MAP_ADD) {
2838 PyObject *key = TOP();
2839 PyObject *value = SECOND();
2840 PyObject *map;
2841 int err;
2842 STACKADJ(-2);
2843 map = stack_pointer[-oparg]; /* dict */
2844 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002845 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002846 Py_DECREF(value);
2847 Py_DECREF(key);
2848 if (err != 0)
2849 goto error;
2850 PREDICT(JUMP_ABSOLUTE);
2851 DISPATCH();
2852 }
2853
2854 TARGET(LOAD_ATTR) {
2855 PyObject *name = GETITEM(names, oparg);
2856 PyObject *owner = TOP();
2857 PyObject *res = PyObject_GetAttr(owner, name);
2858 Py_DECREF(owner);
2859 SET_TOP(res);
2860 if (res == NULL)
2861 goto error;
2862 DISPATCH();
2863 }
2864
2865 TARGET(COMPARE_OP) {
2866 PyObject *right = POP();
2867 PyObject *left = TOP();
2868 PyObject *res = cmp_outcome(oparg, left, right);
2869 Py_DECREF(left);
2870 Py_DECREF(right);
2871 SET_TOP(res);
2872 if (res == NULL)
2873 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 PREDICT(POP_JUMP_IF_FALSE);
2875 PREDICT(POP_JUMP_IF_TRUE);
2876 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002877 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002878
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002879 TARGET(IMPORT_NAME) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002880 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002881 PyObject *fromlist = POP();
2882 PyObject *level = TOP();
2883 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002884 res = import_name(f, name, fromlist, level);
2885 Py_DECREF(level);
2886 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002887 SET_TOP(res);
2888 if (res == NULL)
2889 goto error;
2890 DISPATCH();
2891 }
2892
2893 TARGET(IMPORT_STAR) {
2894 PyObject *from = POP(), *locals;
2895 int err;
Berker Peksag7accf202017-02-27 20:41:21 +03002896 if (PyFrame_FastToLocalsWithError(f) < 0) {
2897 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002898 goto error;
Berker Peksag7accf202017-02-27 20:41:21 +03002899 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002900
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002901 locals = f->f_locals;
2902 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 PyErr_SetString(PyExc_SystemError,
2904 "no locals found during 'import *'");
Berker Peksag7accf202017-02-27 20:41:21 +03002905 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002906 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002907 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002908 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002909 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002910 Py_DECREF(from);
2911 if (err != 0)
2912 goto error;
2913 DISPATCH();
2914 }
Guido van Rossum25831651993-05-19 14:50:45 +00002915
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002916 TARGET(IMPORT_FROM) {
2917 PyObject *name = GETITEM(names, oparg);
2918 PyObject *from = TOP();
2919 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002920 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002921 PUSH(res);
2922 if (res == NULL)
2923 goto error;
2924 DISPATCH();
2925 }
Thomas Wouters52152252000-08-17 22:55:00 +00002926
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002927 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002928 JUMPBY(oparg);
2929 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002930 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002931
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002932 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002933 TARGET(POP_JUMP_IF_FALSE) {
2934 PyObject *cond = POP();
2935 int err;
2936 if (cond == Py_True) {
2937 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002938 FAST_DISPATCH();
2939 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002940 if (cond == Py_False) {
2941 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002942 JUMPTO(oparg);
2943 FAST_DISPATCH();
2944 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002945 err = PyObject_IsTrue(cond);
2946 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002947 if (err > 0)
2948 err = 0;
2949 else if (err == 0)
2950 JUMPTO(oparg);
2951 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002952 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002953 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002954 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002955
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002956 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002957 TARGET(POP_JUMP_IF_TRUE) {
2958 PyObject *cond = POP();
2959 int err;
2960 if (cond == Py_False) {
2961 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002962 FAST_DISPATCH();
2963 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002964 if (cond == Py_True) {
2965 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002966 JUMPTO(oparg);
2967 FAST_DISPATCH();
2968 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002969 err = PyObject_IsTrue(cond);
2970 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002971 if (err > 0) {
2972 err = 0;
2973 JUMPTO(oparg);
2974 }
2975 else if (err == 0)
2976 ;
2977 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002978 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002979 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002980 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002981
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002982 TARGET(JUMP_IF_FALSE_OR_POP) {
2983 PyObject *cond = TOP();
2984 int err;
2985 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002986 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002987 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002988 FAST_DISPATCH();
2989 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002990 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002991 JUMPTO(oparg);
2992 FAST_DISPATCH();
2993 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002994 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002995 if (err > 0) {
2996 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002997 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002998 err = 0;
2999 }
3000 else if (err == 0)
3001 JUMPTO(oparg);
3002 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003003 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003004 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003005 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003006
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003007 TARGET(JUMP_IF_TRUE_OR_POP) {
3008 PyObject *cond = TOP();
3009 int err;
3010 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003011 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003012 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003013 FAST_DISPATCH();
3014 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003015 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003016 JUMPTO(oparg);
3017 FAST_DISPATCH();
3018 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003019 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003020 if (err > 0) {
3021 err = 0;
3022 JUMPTO(oparg);
3023 }
3024 else if (err == 0) {
3025 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003026 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003027 }
3028 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003029 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003030 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003031 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003032
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003033 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003034 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003035 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003036#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 /* Enabling this path speeds-up all while and for-loops by bypassing
3038 the per-loop checks for signals. By default, this should be turned-off
3039 because it prevents detection of a control-break in tight loops like
3040 "while 1: pass". Compile with this option turned-on when you need
3041 the speed-up and do not need break checking inside tight loops (ones
3042 that contain only instructions ending with FAST_DISPATCH).
3043 */
3044 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003045#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003046 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003047#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003048 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003049
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003050 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003051 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003052 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003053 PyObject *iter = PyObject_GetIter(iterable);
3054 Py_DECREF(iterable);
3055 SET_TOP(iter);
3056 if (iter == NULL)
3057 goto error;
3058 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003059 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003060 DISPATCH();
3061 }
3062
3063 TARGET(GET_YIELD_FROM_ITER) {
3064 /* before: [obj]; after [getiter(obj)] */
3065 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003066 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003067 if (PyCoro_CheckExact(iterable)) {
3068 /* `iterable` is a coroutine */
3069 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3070 /* and it is used in a 'yield from' expression of a
3071 regular generator. */
3072 Py_DECREF(iterable);
3073 SET_TOP(NULL);
3074 PyErr_SetString(PyExc_TypeError,
3075 "cannot 'yield from' a coroutine object "
3076 "in a non-coroutine generator");
3077 goto error;
3078 }
3079 }
3080 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003081 /* `iterable` is not a generator. */
3082 iter = PyObject_GetIter(iterable);
3083 Py_DECREF(iterable);
3084 SET_TOP(iter);
3085 if (iter == NULL)
3086 goto error;
3087 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003088 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003089 DISPATCH();
3090 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003091
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003092 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003093 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003094 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003095 PyObject *iter = TOP();
3096 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3097 if (next != NULL) {
3098 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003099 PREDICT(STORE_FAST);
3100 PREDICT(UNPACK_SEQUENCE);
3101 DISPATCH();
3102 }
3103 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003104 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
3105 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08003106 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003107 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003108 PyErr_Clear();
3109 }
3110 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003111 STACKADJ(-1);
3112 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003113 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003114 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003115 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003116 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003117
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003118 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003119 why = WHY_BREAK;
3120 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003121 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003122
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003123 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003124 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003125 if (retval == NULL)
3126 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003127 why = WHY_CONTINUE;
3128 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003129 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003130
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003131 TARGET(SETUP_LOOP)
3132 TARGET(SETUP_EXCEPT)
3133 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003134 /* NOTE: If you add any new block-setup opcodes that
3135 are not try/except/finally handlers, you may need
3136 to update the PyGen_NeedsFinalizing() function.
3137 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003139 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
3140 STACK_LEVEL());
3141 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003142 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003143
Yury Selivanov75445082015-05-11 22:57:16 -04003144 TARGET(BEFORE_ASYNC_WITH) {
3145 _Py_IDENTIFIER(__aexit__);
3146 _Py_IDENTIFIER(__aenter__);
3147
3148 PyObject *mgr = TOP();
3149 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3150 *enter;
3151 PyObject *res;
3152 if (exit == NULL)
3153 goto error;
3154 SET_TOP(exit);
3155 enter = special_lookup(mgr, &PyId___aenter__);
3156 Py_DECREF(mgr);
3157 if (enter == NULL)
3158 goto error;
3159 res = PyObject_CallFunctionObjArgs(enter, NULL);
3160 Py_DECREF(enter);
3161 if (res == NULL)
3162 goto error;
3163 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003164 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003165 DISPATCH();
3166 }
3167
3168 TARGET(SETUP_ASYNC_WITH) {
3169 PyObject *res = POP();
3170 /* Setup the finally block before pushing the result
3171 of __aenter__ on the stack. */
3172 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3173 STACK_LEVEL());
3174 PUSH(res);
3175 DISPATCH();
3176 }
3177
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003178 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003179 _Py_IDENTIFIER(__exit__);
3180 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003181 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08003182 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003183 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08003184 if (enter == NULL)
3185 goto error;
3186 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003187 if (exit == NULL) {
3188 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003189 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003190 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003191 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003192 Py_DECREF(mgr);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003193 res = PyObject_CallFunctionObjArgs(enter, NULL);
3194 Py_DECREF(enter);
3195 if (res == NULL)
3196 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003197 /* Setup the finally block before pushing the result
3198 of __enter__ on the stack. */
3199 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3200 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003201
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003202 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003203 DISPATCH();
3204 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003205
Yury Selivanov75445082015-05-11 22:57:16 -04003206 TARGET(WITH_CLEANUP_START) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04003207 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003208 how/why we entered the finally clause:
3209 - TOP = None
3210 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
3211 - TOP = WHY_*; no retval below it
3212 - (TOP, SECOND, THIRD) = exc_info()
3213 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
3214 Below them is EXIT, the context.__exit__ bound method.
3215 In the last case, we must call
3216 EXIT(TOP, SECOND, THIRD)
3217 otherwise we must call
3218 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003219
Benjamin Peterson8f169482013-10-29 22:25:06 -04003220 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003221 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04003222 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003223 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225 In addition, if the stack represents an exception,
3226 *and* the function call returns a 'true' value, we
3227 push WHY_SILENCED onto the stack. END_FINALLY will
3228 then not re-raise the exception. (But non-local
3229 gotos should still be resumed.)
3230 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003232 PyObject *exit_func;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003233 PyObject *exc = TOP(), *val = Py_None, *tb = Py_None, *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003234 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003235 (void)POP();
3236 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003237 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003238 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003239 else if (PyLong_Check(exc)) {
3240 STACKADJ(-1);
3241 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003242 case WHY_RETURN:
3243 case WHY_CONTINUE:
3244 /* Retval in TOP. */
3245 exit_func = SECOND();
3246 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003247 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003248 break;
3249 default:
3250 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003251 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003252 break;
3253 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003254 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003255 }
3256 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003257 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003258 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003259 val = SECOND();
3260 tb = THIRD();
3261 tp2 = FOURTH();
3262 exc2 = PEEK(5);
3263 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003264 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003265 SET_VALUE(7, tb2);
3266 SET_VALUE(6, exc2);
3267 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003268 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3269 SET_FOURTH(NULL);
3270 /* We just shifted the stack down, so we have
3271 to tell the except handler block that the
3272 values are lower than it expects. */
3273 block = &f->f_blockstack[f->f_iblock - 1];
3274 assert(block->b_type == EXCEPT_HANDLER);
3275 block->b_level--;
3276 }
3277 /* XXX Not the fastest way to call it... */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003278 res = PyObject_CallFunctionObjArgs(exit_func, exc, val, tb, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003279 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003280 if (res == NULL)
3281 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003282
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003283 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003284 PUSH(exc);
3285 PUSH(res);
3286 PREDICT(WITH_CLEANUP_FINISH);
3287 DISPATCH();
3288 }
3289
3290 PREDICTED(WITH_CLEANUP_FINISH);
3291 TARGET(WITH_CLEANUP_FINISH) {
3292 PyObject *res = POP();
3293 PyObject *exc = POP();
3294 int err;
3295
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003296 if (exc != Py_None)
3297 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003298 else
3299 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003300
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003301 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003302 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003304 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003305 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003306 else if (err > 0) {
3307 err = 0;
3308 /* There was an exception and a True return */
3309 PUSH(PyLong_FromLong((long) WHY_SILENCED));
3310 }
3311 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003312 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003313 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003314
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003315 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003316 TARGET(CALL_FUNCTION) {
3317 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003318 PCALL(PCALL_ALL);
3319 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003320 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003321 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003322 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003323 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003324 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003325 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003326 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003327 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003328
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003329 TARGET(CALL_FUNCTION_KW) {
3330 PyObject **sp, *res, *names;
3331
3332 names = POP();
3333 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003334 PCALL(PCALL_ALL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003335 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003336 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003337 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003338 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003339 Py_DECREF(names);
3340
3341 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003342 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003343 }
3344 DISPATCH();
3345 }
3346
3347 TARGET(CALL_FUNCTION_EX) {
3348 PyObject *func, *callargs, *kwargs = NULL, *result;
3349 PCALL(PCALL_ALL);
3350 if (oparg & 0x01) {
3351 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003352 if (!PyDict_CheckExact(kwargs)) {
3353 PyObject *d = PyDict_New();
3354 if (d == NULL)
3355 goto error;
3356 if (PyDict_Update(d, kwargs) != 0) {
3357 Py_DECREF(d);
3358 /* PyDict_Update raises attribute
3359 * error (percolated from an attempt
3360 * to get 'keys' attribute) instead of
3361 * a type error if its second argument
3362 * is not a mapping.
3363 */
3364 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka946a0b62017-08-03 12:14:35 +03003365 format_kwargs_mapping_error(SECOND(), kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003366 }
Victor Stinnereece2222016-09-12 11:16:37 +02003367 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003368 goto error;
3369 }
3370 Py_DECREF(kwargs);
3371 kwargs = d;
3372 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003373 assert(PyDict_CheckExact(kwargs));
3374 }
3375 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003376 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003377 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka946a0b62017-08-03 12:14:35 +03003378 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003379 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003380 goto error;
3381 }
3382 Py_SETREF(callargs, PySequence_Tuple(callargs));
3383 if (callargs == NULL) {
3384 goto error;
3385 }
3386 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003387 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003388
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003389 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003390 Py_DECREF(func);
3391 Py_DECREF(callargs);
3392 Py_XDECREF(kwargs);
3393
3394 SET_TOP(result);
3395 if (result == NULL) {
3396 goto error;
3397 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003398 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003399 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003400
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003401 TARGET(MAKE_FUNCTION) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003402 PyObject *qualname = POP();
3403 PyObject *codeobj = POP();
3404 PyFunctionObject *func = (PyFunctionObject *)
3405 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003406
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003407 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003408 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003409 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003410 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003411 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003412
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003413 if (oparg & 0x08) {
3414 assert(PyTuple_CheckExact(TOP()));
3415 func ->func_closure = POP();
3416 }
3417 if (oparg & 0x04) {
3418 assert(PyDict_CheckExact(TOP()));
3419 func->func_annotations = POP();
3420 }
3421 if (oparg & 0x02) {
3422 assert(PyDict_CheckExact(TOP()));
3423 func->func_kwdefaults = POP();
3424 }
3425 if (oparg & 0x01) {
3426 assert(PyTuple_CheckExact(TOP()));
3427 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003428 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003429
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003430 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003431 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003432 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003433
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003434 TARGET(BUILD_SLICE) {
3435 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003436 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003437 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003439 step = NULL;
3440 stop = POP();
3441 start = TOP();
3442 slice = PySlice_New(start, stop, step);
3443 Py_DECREF(start);
3444 Py_DECREF(stop);
3445 Py_XDECREF(step);
3446 SET_TOP(slice);
3447 if (slice == NULL)
3448 goto error;
3449 DISPATCH();
3450 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003451
Eric V. Smitha78c7952015-11-03 12:45:05 -05003452 TARGET(FORMAT_VALUE) {
3453 /* Handles f-string value formatting. */
3454 PyObject *result;
3455 PyObject *fmt_spec;
3456 PyObject *value;
3457 PyObject *(*conv_fn)(PyObject *);
3458 int which_conversion = oparg & FVC_MASK;
3459 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3460
3461 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003462 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003463
3464 /* See if any conversion is specified. */
3465 switch (which_conversion) {
3466 case FVC_STR: conv_fn = PyObject_Str; break;
3467 case FVC_REPR: conv_fn = PyObject_Repr; break;
3468 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3469
3470 /* Must be 0 (meaning no conversion), since only four
3471 values are allowed by (oparg & FVC_MASK). */
3472 default: conv_fn = NULL; break;
3473 }
3474
3475 /* If there's a conversion function, call it and replace
3476 value with that result. Otherwise, just use value,
3477 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003478 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003479 result = conv_fn(value);
3480 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003481 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003482 Py_XDECREF(fmt_spec);
3483 goto error;
3484 }
3485 value = result;
3486 }
3487
3488 /* If value is a unicode object, and there's no fmt_spec,
3489 then we know the result of format(value) is value
3490 itself. In that case, skip calling format(). I plan to
3491 move this optimization in to PyObject_Format()
3492 itself. */
3493 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3494 /* Do nothing, just transfer ownership to result. */
3495 result = value;
3496 } else {
3497 /* Actually call format(). */
3498 result = PyObject_Format(value, fmt_spec);
3499 Py_DECREF(value);
3500 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003501 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003502 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003503 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003504 }
3505
Eric V. Smith135d5f42016-02-05 18:23:08 -05003506 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003507 DISPATCH();
3508 }
3509
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003510 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003511 int oldoparg = oparg;
3512 NEXTOPARG();
3513 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003514 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003515 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003516
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003517
Antoine Pitrou042b1282010-08-13 21:15:58 +00003518#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003519 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003520#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003521 default:
3522 fprintf(stderr,
3523 "XXX lineno: %d, opcode: %d\n",
3524 PyFrame_GetLineNumber(f),
3525 opcode);
3526 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003527 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003528
3529#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003530 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003531#endif
3532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003533 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003534
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003535 /* This should never be reached. Every opcode should end with DISPATCH()
3536 or goto error. */
3537 assert(0);
Guido van Rossumac7be682001-01-17 15:42:30 +00003538
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003539error:
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003540
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003541 assert(why == WHY_NOT);
3542 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003543
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003544 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003545#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003546 if (!PyErr_Occurred())
3547 PyErr_SetString(PyExc_SystemError,
3548 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003549#else
3550 assert(PyErr_Occurred());
3551#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003552
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003553 /* Log traceback info. */
3554 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003555
Benjamin Peterson51f46162013-01-23 08:38:47 -05003556 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003557 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3558 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003559
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003560fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003561 assert(why != WHY_NOT);
3562
3563 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003564 while (why != WHY_NOT && f->f_iblock > 0) {
3565 /* Peek at the current block. */
3566 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003568 assert(why != WHY_YIELD);
3569 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3570 why = WHY_NOT;
3571 JUMPTO(PyLong_AS_LONG(retval));
3572 Py_DECREF(retval);
3573 break;
3574 }
3575 /* Now we have to pop the block. */
3576 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003578 if (b->b_type == EXCEPT_HANDLER) {
3579 UNWIND_EXCEPT_HANDLER(b);
3580 continue;
3581 }
3582 UNWIND_BLOCK(b);
3583 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3584 why = WHY_NOT;
3585 JUMPTO(b->b_handler);
3586 break;
3587 }
3588 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3589 || b->b_type == SETUP_FINALLY)) {
3590 PyObject *exc, *val, *tb;
3591 int handler = b->b_handler;
3592 /* Beware, this invalidates all b->b_* fields */
3593 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3594 PUSH(tstate->exc_traceback);
3595 PUSH(tstate->exc_value);
3596 if (tstate->exc_type != NULL) {
3597 PUSH(tstate->exc_type);
3598 }
3599 else {
3600 Py_INCREF(Py_None);
3601 PUSH(Py_None);
3602 }
3603 PyErr_Fetch(&exc, &val, &tb);
3604 /* Make the raw exception data
3605 available to the handler,
3606 so a program can emulate the
3607 Python main loop. */
3608 PyErr_NormalizeException(
3609 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003610 if (tb != NULL)
3611 PyException_SetTraceback(val, tb);
3612 else
3613 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003614 Py_INCREF(exc);
3615 tstate->exc_type = exc;
3616 Py_INCREF(val);
3617 tstate->exc_value = val;
3618 tstate->exc_traceback = tb;
3619 if (tb == NULL)
3620 tb = Py_None;
3621 Py_INCREF(tb);
3622 PUSH(tb);
3623 PUSH(val);
3624 PUSH(exc);
3625 why = WHY_NOT;
3626 JUMPTO(handler);
3627 break;
3628 }
3629 if (b->b_type == SETUP_FINALLY) {
3630 if (why & (WHY_RETURN | WHY_CONTINUE))
3631 PUSH(retval);
3632 PUSH(PyLong_FromLong((long)why));
3633 why = WHY_NOT;
3634 JUMPTO(b->b_handler);
3635 break;
3636 }
3637 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003639 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003641 if (why != WHY_NOT)
3642 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00003643
Victor Stinnerace47d72013-07-18 01:41:08 +02003644 assert(!PyErr_Occurred());
3645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003646 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003648 assert(why != WHY_YIELD);
3649 /* Pop remaining stack entries. */
3650 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003651 PyObject *o = POP();
3652 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003653 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003655 if (why != WHY_RETURN)
3656 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003657
Victor Stinner4a7cc882015-03-06 23:35:27 +01003658 assert((retval != NULL) ^ (PyErr_Occurred() != NULL));
Victor Stinnerace47d72013-07-18 01:41:08 +02003659
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003660fast_yield:
Yury Selivanoveb636452016-09-08 22:01:51 -07003661 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01003662
Benjamin Petersonac913412011-07-03 16:25:11 -05003663 /* The purpose of this block is to put aside the generator's exception
3664 state and restore that of the calling frame. If the current
3665 exception state is from the caller, we clear the exception values
3666 on the generator frame, so they are not swapped back in latter. The
3667 origin of the current exception state is determined by checking for
3668 except handler blocks, which we must be in iff a new exception
3669 state came into existence in this frame. (An uncaught exception
3670 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3671 int i;
Victor Stinner74319ae2016-08-25 00:04:09 +02003672 for (i = 0; i < f->f_iblock; i++) {
3673 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER) {
Benjamin Petersonac913412011-07-03 16:25:11 -05003674 break;
Victor Stinner74319ae2016-08-25 00:04:09 +02003675 }
3676 }
Benjamin Petersonac913412011-07-03 16:25:11 -05003677 if (i == f->f_iblock)
3678 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003679 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003680 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003681 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003682 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003684 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003685 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003686 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003687 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3688 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003689 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003690 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003691 why = WHY_EXCEPTION;
3692 }
3693 }
3694 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003695 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3696 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003697 PyTrace_RETURN, NULL);
3698 }
3699 }
3700 if (tstate->c_profilefunc) {
3701 if (why == WHY_EXCEPTION)
3702 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003703 tstate->c_profileobj,
3704 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003705 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003706 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3707 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003708 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003709 Py_CLEAR(retval);
Brett Cannonb94767f2011-02-22 20:15:44 +00003710 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003711 }
3712 }
3713 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003715 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003716exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003717 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3718 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003719 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003720 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003721 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003722
Victor Stinnerefde1462015-03-21 15:04:43 +01003723 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003724}
3725
Benjamin Petersonb204a422011-06-05 22:04:07 -05003726static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003727format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3728{
3729 int err;
3730 Py_ssize_t len = PyList_GET_SIZE(names);
3731 PyObject *name_str, *comma, *tail, *tmp;
3732
3733 assert(PyList_CheckExact(names));
3734 assert(len >= 1);
3735 /* Deal with the joys of natural language. */
3736 switch (len) {
3737 case 1:
3738 name_str = PyList_GET_ITEM(names, 0);
3739 Py_INCREF(name_str);
3740 break;
3741 case 2:
3742 name_str = PyUnicode_FromFormat("%U and %U",
3743 PyList_GET_ITEM(names, len - 2),
3744 PyList_GET_ITEM(names, len - 1));
3745 break;
3746 default:
3747 tail = PyUnicode_FromFormat(", %U, and %U",
3748 PyList_GET_ITEM(names, len - 2),
3749 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003750 if (tail == NULL)
3751 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003752 /* Chop off the last two objects in the list. This shouldn't actually
3753 fail, but we can't be too careful. */
3754 err = PyList_SetSlice(names, len - 2, len, NULL);
3755 if (err == -1) {
3756 Py_DECREF(tail);
3757 return;
3758 }
3759 /* Stitch everything up into a nice comma-separated list. */
3760 comma = PyUnicode_FromString(", ");
3761 if (comma == NULL) {
3762 Py_DECREF(tail);
3763 return;
3764 }
3765 tmp = PyUnicode_Join(comma, names);
3766 Py_DECREF(comma);
3767 if (tmp == NULL) {
3768 Py_DECREF(tail);
3769 return;
3770 }
3771 name_str = PyUnicode_Concat(tmp, tail);
3772 Py_DECREF(tmp);
3773 Py_DECREF(tail);
3774 break;
3775 }
3776 if (name_str == NULL)
3777 return;
3778 PyErr_Format(PyExc_TypeError,
3779 "%U() missing %i required %s argument%s: %U",
3780 co->co_name,
3781 len,
3782 kind,
3783 len == 1 ? "" : "s",
3784 name_str);
3785 Py_DECREF(name_str);
3786}
3787
3788static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003789missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003790 PyObject **fastlocals)
3791{
Victor Stinner74319ae2016-08-25 00:04:09 +02003792 Py_ssize_t i, j = 0;
3793 Py_ssize_t start, end;
3794 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003795 const char *kind = positional ? "positional" : "keyword-only";
3796 PyObject *missing_names;
3797
3798 /* Compute the names of the arguments that are missing. */
3799 missing_names = PyList_New(missing);
3800 if (missing_names == NULL)
3801 return;
3802 if (positional) {
3803 start = 0;
3804 end = co->co_argcount - defcount;
3805 }
3806 else {
3807 start = co->co_argcount;
3808 end = start + co->co_kwonlyargcount;
3809 }
3810 for (i = start; i < end; i++) {
3811 if (GETLOCAL(i) == NULL) {
3812 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3813 PyObject *name = PyObject_Repr(raw);
3814 if (name == NULL) {
3815 Py_DECREF(missing_names);
3816 return;
3817 }
3818 PyList_SET_ITEM(missing_names, j++, name);
3819 }
3820 }
3821 assert(j == missing);
3822 format_missing(kind, co, missing_names);
3823 Py_DECREF(missing_names);
3824}
3825
3826static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003827too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3828 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003829{
3830 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003831 Py_ssize_t kwonly_given = 0;
3832 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003833 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003834 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003835
Benjamin Petersone109c702011-06-24 09:37:26 -05003836 assert((co->co_flags & CO_VARARGS) == 0);
3837 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003838 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3839 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003840 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003841 }
3842 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003843 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003844 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003845 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003846 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003847 }
3848 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003849 plural = (co_argcount != 1);
3850 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003851 }
3852 if (sig == NULL)
3853 return;
3854 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003855 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3856 kwonly_sig = PyUnicode_FromFormat(format,
3857 given != 1 ? "s" : "",
3858 kwonly_given,
3859 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003860 if (kwonly_sig == NULL) {
3861 Py_DECREF(sig);
3862 return;
3863 }
3864 }
3865 else {
3866 /* This will not fail. */
3867 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003868 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003869 }
3870 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003871 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003872 co->co_name,
3873 sig,
3874 plural ? "s" : "",
3875 given,
3876 kwonly_sig,
3877 given == 1 && !kwonly_given ? "was" : "were");
3878 Py_DECREF(sig);
3879 Py_DECREF(kwonly_sig);
3880}
3881
Guido van Rossumc2e20742006-02-27 22:32:47 +00003882/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003883 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003884 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003885
Victor Stinner40ee3012014-06-16 15:59:28 +02003886static PyObject *
3887_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Victor Stinner74319ae2016-08-25 00:04:09 +02003888 PyObject **args, Py_ssize_t argcount,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003889 PyObject **kwnames, PyObject **kwargs,
3890 Py_ssize_t kwcount, int kwstep,
Victor Stinner74319ae2016-08-25 00:04:09 +02003891 PyObject **defs, Py_ssize_t defcount,
3892 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003893 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003894{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003895 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003896 PyFrameObject *f;
3897 PyObject *retval = NULL;
3898 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003899 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003900 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003901 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3902 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003903 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003905 if (globals == NULL) {
3906 PyErr_SetString(PyExc_SystemError,
3907 "PyEval_EvalCodeEx: NULL globals");
3908 return NULL;
3909 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003910
Victor Stinnerc7020012016-08-16 23:40:29 +02003911 /* Create the frame */
3912 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003913 assert(tstate != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003914 f = PyFrame_New(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003915 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003916 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003917 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003918 fastlocals = f->f_localsplus;
3919 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003920
Victor Stinnerc7020012016-08-16 23:40:29 +02003921 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003922 if (co->co_flags & CO_VARKEYWORDS) {
3923 kwdict = PyDict_New();
3924 if (kwdict == NULL)
3925 goto fail;
3926 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003927 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003928 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003929 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003930 SETLOCAL(i, kwdict);
3931 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003932 else {
3933 kwdict = NULL;
3934 }
3935
3936 /* Copy positional arguments into local variables */
3937 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003938 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003939 }
3940 else {
3941 n = argcount;
3942 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003943 for (i = 0; i < n; i++) {
3944 x = args[i];
3945 Py_INCREF(x);
3946 SETLOCAL(i, x);
3947 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003948
3949 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003950 if (co->co_flags & CO_VARARGS) {
3951 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003952 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003953 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003954 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003955 SETLOCAL(total_args, u);
3956 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003957 x = args[i];
3958 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003959 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003960 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003961 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003962
Serhiy Storchakab7281052016-09-12 00:52:40 +03003963 /* Handle keyword arguments passed as two strided arrays */
3964 kwcount *= kwstep;
3965 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003966 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003967 PyObject *keyword = kwnames[i];
3968 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003969 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003970
Benjamin Petersonb204a422011-06-05 22:04:07 -05003971 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3972 PyErr_Format(PyExc_TypeError,
3973 "%U() keywords must be strings",
3974 co->co_name);
3975 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003976 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003977
Benjamin Petersonb204a422011-06-05 22:04:07 -05003978 /* Speed hack: do raw pointer compares. As names are
3979 normally interned this should almost always hit. */
3980 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3981 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003982 PyObject *name = co_varnames[j];
3983 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003984 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003985 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003986 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003987
Benjamin Petersonb204a422011-06-05 22:04:07 -05003988 /* Slow fallback, just in case */
3989 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003990 PyObject *name = co_varnames[j];
3991 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3992 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003993 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003994 }
3995 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003996 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003997 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003998 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003999
Benjamin Petersonb204a422011-06-05 22:04:07 -05004000 if (j >= total_args && kwdict == NULL) {
4001 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02004002 "%U() got an unexpected keyword argument '%S'",
4003 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004004 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004005 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004006
Christian Heimes0bd447f2013-07-20 14:48:10 +02004007 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4008 goto fail;
4009 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004010 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004011
Benjamin Petersonb204a422011-06-05 22:04:07 -05004012 kw_found:
4013 if (GETLOCAL(j) != NULL) {
4014 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02004015 "%U() got multiple values for argument '%S'",
4016 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004017 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004018 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004019 Py_INCREF(value);
4020 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004021 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004022
4023 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004024 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004025 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004026 goto fail;
4027 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004028
4029 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004030 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02004031 Py_ssize_t m = co->co_argcount - defcount;
4032 Py_ssize_t missing = 0;
4033 for (i = argcount; i < m; i++) {
4034 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004035 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004036 }
4037 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004038 if (missing) {
4039 missing_arguments(co, missing, defcount, fastlocals);
4040 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004041 }
4042 if (n > m)
4043 i = n - m;
4044 else
4045 i = 0;
4046 for (; i < defcount; i++) {
4047 if (GETLOCAL(m+i) == NULL) {
4048 PyObject *def = defs[i];
4049 Py_INCREF(def);
4050 SETLOCAL(m+i, def);
4051 }
4052 }
4053 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004054
4055 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004056 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004057 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004058 for (i = co->co_argcount; i < total_args; i++) {
4059 PyObject *name;
4060 if (GETLOCAL(i) != NULL)
4061 continue;
4062 name = PyTuple_GET_ITEM(co->co_varnames, i);
4063 if (kwdefs != NULL) {
4064 PyObject *def = PyDict_GetItem(kwdefs, name);
4065 if (def) {
4066 Py_INCREF(def);
4067 SETLOCAL(i, def);
4068 continue;
4069 }
4070 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004071 missing++;
4072 }
4073 if (missing) {
4074 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004075 goto fail;
4076 }
4077 }
4078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004079 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004080 vars into frame. */
4081 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004082 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05004083 int arg;
4084 /* Possibly account for the cell variable being an argument. */
4085 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004086 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004087 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004088 /* Clear the local copy. */
4089 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004090 }
4091 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004092 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004093 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004094 if (c == NULL)
4095 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004096 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004097 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004098
4099 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004100 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4101 PyObject *o = PyTuple_GET_ITEM(closure, i);
4102 Py_INCREF(o);
4103 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004104 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004105
Yury Selivanoveb636452016-09-08 22:01:51 -07004106 /* Handle generator/coroutine/asynchronous generator */
4107 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004108 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04004109 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004110 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004111
4112 if (is_coro && tstate->in_coroutine_wrapper) {
4113 assert(coro_wrapper != NULL);
4114 PyErr_Format(PyExc_RuntimeError,
4115 "coroutine wrapper %.200R attempted "
4116 "to recursively wrap %.200R",
4117 coro_wrapper,
4118 co);
4119 goto fail;
4120 }
Yury Selivanov75445082015-05-11 22:57:16 -04004121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004122 /* Don't need to keep the reference to f_back, it will be set
4123 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004124 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004126 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004128 /* Create a new generator that owns the ready to run frame
4129 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004130 if (is_coro) {
4131 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004132 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4133 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004134 } else {
4135 gen = PyGen_NewWithQualName(f, name, qualname);
4136 }
Yury Selivanov75445082015-05-11 22:57:16 -04004137 if (gen == NULL)
4138 return NULL;
4139
Yury Selivanov94c22632015-06-04 10:16:51 -04004140 if (is_coro && coro_wrapper != NULL) {
4141 PyObject *wrapped;
4142 tstate->in_coroutine_wrapper = 1;
4143 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4144 tstate->in_coroutine_wrapper = 0;
4145 return wrapped;
4146 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004147
Yury Selivanov75445082015-05-11 22:57:16 -04004148 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004149 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004151 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004152
Thomas Woutersce272b62007-09-19 21:19:28 +00004153fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004155 /* decref'ing the frame can cause __del__ methods to get invoked,
4156 which can call back into Python. While we're done with the
4157 current Python frame (f), the associated C stack is still in use,
4158 so recursion_depth must be boosted for the duration.
4159 */
4160 assert(tstate != NULL);
4161 ++tstate->recursion_depth;
4162 Py_DECREF(f);
4163 --tstate->recursion_depth;
4164 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004165}
4166
Victor Stinner40ee3012014-06-16 15:59:28 +02004167PyObject *
4168PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
4169 PyObject **args, int argcount, PyObject **kws, int kwcount,
4170 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
4171{
4172 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004173 args, argcount,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004174 kws, kws + 1, kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004175 defs, defcount,
4176 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004177 NULL, NULL);
4178}
Tim Peters5ca576e2001-06-18 22:08:13 +00004179
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004180static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004181special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004183 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004184 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004185 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004186 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004187 return NULL;
4188 }
4189 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004190}
4191
4192
Benjamin Peterson87880242011-07-03 16:48:31 -05004193/* These 3 functions deal with the exception state of generators. */
4194
4195static void
4196save_exc_state(PyThreadState *tstate, PyFrameObject *f)
4197{
4198 PyObject *type, *value, *traceback;
4199 Py_XINCREF(tstate->exc_type);
4200 Py_XINCREF(tstate->exc_value);
4201 Py_XINCREF(tstate->exc_traceback);
4202 type = f->f_exc_type;
4203 value = f->f_exc_value;
4204 traceback = f->f_exc_traceback;
4205 f->f_exc_type = tstate->exc_type;
4206 f->f_exc_value = tstate->exc_value;
4207 f->f_exc_traceback = tstate->exc_traceback;
4208 Py_XDECREF(type);
4209 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004210 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05004211}
4212
4213static void
4214swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
4215{
4216 PyObject *tmp;
4217 tmp = tstate->exc_type;
4218 tstate->exc_type = f->f_exc_type;
4219 f->f_exc_type = tmp;
4220 tmp = tstate->exc_value;
4221 tstate->exc_value = f->f_exc_value;
4222 f->f_exc_value = tmp;
4223 tmp = tstate->exc_traceback;
4224 tstate->exc_traceback = f->f_exc_traceback;
4225 f->f_exc_traceback = tmp;
4226}
4227
4228static void
4229restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
4230{
4231 PyObject *type, *value, *tb;
4232 type = tstate->exc_type;
4233 value = tstate->exc_value;
4234 tb = tstate->exc_traceback;
4235 tstate->exc_type = f->f_exc_type;
4236 tstate->exc_value = f->f_exc_value;
4237 tstate->exc_traceback = f->f_exc_traceback;
4238 f->f_exc_type = NULL;
4239 f->f_exc_value = NULL;
4240 f->f_exc_traceback = NULL;
4241 Py_XDECREF(type);
4242 Py_XDECREF(value);
4243 Py_XDECREF(tb);
4244}
4245
4246
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004247/* Logic for the raise statement (too complicated for inlining).
4248 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004249static int
Collin Winter828f04a2007-08-31 00:04:24 +00004250do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004251{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004252 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004254 if (exc == NULL) {
4255 /* Reraise */
4256 PyThreadState *tstate = PyThreadState_GET();
4257 PyObject *tb;
4258 type = tstate->exc_type;
4259 value = tstate->exc_value;
4260 tb = tstate->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004261 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004262 PyErr_SetString(PyExc_RuntimeError,
4263 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004264 return 0;
4265 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004266 Py_XINCREF(type);
4267 Py_XINCREF(value);
4268 Py_XINCREF(tb);
4269 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004270 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004271 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004273 /* We support the following forms of raise:
4274 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004275 raise <instance>
4276 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004278 if (PyExceptionClass_Check(exc)) {
4279 type = exc;
4280 value = PyObject_CallObject(exc, NULL);
4281 if (value == NULL)
4282 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004283 if (!PyExceptionInstance_Check(value)) {
4284 PyErr_Format(PyExc_TypeError,
4285 "calling %R should have returned an instance of "
4286 "BaseException, not %R",
4287 type, Py_TYPE(value));
4288 goto raise_error;
4289 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004290 }
4291 else if (PyExceptionInstance_Check(exc)) {
4292 value = exc;
4293 type = PyExceptionInstance_Class(exc);
4294 Py_INCREF(type);
4295 }
4296 else {
4297 /* Not something you can raise. You get an exception
4298 anyway, just not what you specified :-) */
4299 Py_DECREF(exc);
4300 PyErr_SetString(PyExc_TypeError,
4301 "exceptions must derive from BaseException");
4302 goto raise_error;
4303 }
Collin Winter828f04a2007-08-31 00:04:24 +00004304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004305 if (cause) {
4306 PyObject *fixed_cause;
4307 if (PyExceptionClass_Check(cause)) {
4308 fixed_cause = PyObject_CallObject(cause, NULL);
4309 if (fixed_cause == NULL)
4310 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004311 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004312 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004313 else if (PyExceptionInstance_Check(cause)) {
4314 fixed_cause = cause;
4315 }
4316 else if (cause == Py_None) {
4317 Py_DECREF(cause);
4318 fixed_cause = NULL;
4319 }
4320 else {
4321 PyErr_SetString(PyExc_TypeError,
4322 "exception causes must derive from "
4323 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004324 goto raise_error;
4325 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004326 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004327 }
Collin Winter828f04a2007-08-31 00:04:24 +00004328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004329 PyErr_SetObject(type, value);
4330 /* PyErr_SetObject incref's its arguments */
4331 Py_XDECREF(value);
4332 Py_XDECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004333 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004334
4335raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004336 Py_XDECREF(value);
4337 Py_XDECREF(type);
4338 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004339 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004340}
4341
Tim Petersd6d010b2001-06-21 02:49:55 +00004342/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004343 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004344
Guido van Rossum0368b722007-05-11 16:50:42 +00004345 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4346 with a variable target.
4347*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004348
Barry Warsawe42b18f1997-08-25 22:13:04 +00004349static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004350unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004352 int i = 0, j = 0;
4353 Py_ssize_t ll = 0;
4354 PyObject *it; /* iter(v) */
4355 PyObject *w;
4356 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004358 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004360 it = PyObject_GetIter(v);
4361 if (it == NULL)
4362 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00004363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004364 for (; i < argcnt; i++) {
4365 w = PyIter_Next(it);
4366 if (w == NULL) {
4367 /* Iterator done, via error or exhaustion. */
4368 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004369 if (argcntafter == -1) {
4370 PyErr_Format(PyExc_ValueError,
4371 "not enough values to unpack (expected %d, got %d)",
4372 argcnt, i);
4373 }
4374 else {
4375 PyErr_Format(PyExc_ValueError,
4376 "not enough values to unpack "
4377 "(expected at least %d, got %d)",
4378 argcnt + argcntafter, i);
4379 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004380 }
4381 goto Error;
4382 }
4383 *--sp = w;
4384 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004386 if (argcntafter == -1) {
4387 /* We better have exhausted the iterator now. */
4388 w = PyIter_Next(it);
4389 if (w == NULL) {
4390 if (PyErr_Occurred())
4391 goto Error;
4392 Py_DECREF(it);
4393 return 1;
4394 }
4395 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004396 PyErr_Format(PyExc_ValueError,
4397 "too many values to unpack (expected %d)",
4398 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004399 goto Error;
4400 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004402 l = PySequence_List(it);
4403 if (l == NULL)
4404 goto Error;
4405 *--sp = l;
4406 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004408 ll = PyList_GET_SIZE(l);
4409 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004410 PyErr_Format(PyExc_ValueError,
4411 "not enough values to unpack (expected at least %d, got %zd)",
4412 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004413 goto Error;
4414 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004416 /* Pop the "after-variable" args off the list. */
4417 for (j = argcntafter; j > 0; j--, i++) {
4418 *--sp = PyList_GET_ITEM(l, ll - j);
4419 }
4420 /* Resize the list. */
4421 Py_SIZE(l) = ll - argcntafter;
4422 Py_DECREF(it);
4423 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004424
Tim Petersd6d010b2001-06-21 02:49:55 +00004425Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004426 for (; i > 0; i--, sp++)
4427 Py_DECREF(*sp);
4428 Py_XDECREF(it);
4429 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004430}
4431
4432
Guido van Rossum96a42c81992-01-12 02:29:51 +00004433#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004434static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004435prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004436{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004437 printf("%s ", str);
4438 if (PyObject_Print(v, stdout, 0) != 0)
4439 PyErr_Clear(); /* Don't know what else to do */
4440 printf("\n");
4441 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004442}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004443#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004444
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004445static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004446call_exc_trace(Py_tracefunc func, PyObject *self,
4447 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004448{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004449 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004450 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004451 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004452 if (value == NULL) {
4453 value = Py_None;
4454 Py_INCREF(value);
4455 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004456 PyErr_NormalizeException(&type, &value, &orig_traceback);
4457 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004458 arg = PyTuple_Pack(3, type, value, traceback);
4459 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004460 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004461 return;
4462 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004463 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004464 Py_DECREF(arg);
4465 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004466 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004467 else {
4468 Py_XDECREF(type);
4469 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004470 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004471 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004472}
4473
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004474static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004475call_trace_protected(Py_tracefunc func, PyObject *obj,
4476 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004477 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004478{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004479 PyObject *type, *value, *traceback;
4480 int err;
4481 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004482 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004483 if (err == 0)
4484 {
4485 PyErr_Restore(type, value, traceback);
4486 return 0;
4487 }
4488 else {
4489 Py_XDECREF(type);
4490 Py_XDECREF(value);
4491 Py_XDECREF(traceback);
4492 return -1;
4493 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004494}
4495
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004496static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004497call_trace(Py_tracefunc func, PyObject *obj,
4498 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004499 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004500{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004501 int result;
4502 if (tstate->tracing)
4503 return 0;
4504 tstate->tracing++;
4505 tstate->use_tracing = 0;
4506 result = func(obj, frame, what, arg);
4507 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4508 || (tstate->c_profilefunc != NULL));
4509 tstate->tracing--;
4510 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004511}
4512
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004513PyObject *
4514_PyEval_CallTracing(PyObject *func, PyObject *args)
4515{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004516 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004517 int save_tracing = tstate->tracing;
4518 int save_use_tracing = tstate->use_tracing;
4519 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004521 tstate->tracing = 0;
4522 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4523 || (tstate->c_profilefunc != NULL));
4524 result = PyObject_Call(func, args, NULL);
4525 tstate->tracing = save_tracing;
4526 tstate->use_tracing = save_use_tracing;
4527 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004528}
4529
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004530/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004531static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004532maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004533 PyThreadState *tstate, PyFrameObject *frame,
4534 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004535{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004536 int result = 0;
4537 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004539 /* If the last instruction executed isn't in the current
4540 instruction window, reset the window.
4541 */
4542 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4543 PyAddrPair bounds;
4544 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4545 &bounds);
4546 *instr_lb = bounds.ap_lower;
4547 *instr_ub = bounds.ap_upper;
4548 }
4549 /* If the last instruction falls at the start of a line or if
4550 it represents a jump backwards, update the frame's line
4551 number and call the trace function. */
4552 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
4553 frame->f_lineno = line;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004554 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004555 }
4556 *instr_prev = frame->f_lasti;
4557 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004558}
4559
Fred Drake5755ce62001-06-27 19:19:46 +00004560void
4561PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004562{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004563 PyThreadState *tstate = PyThreadState_GET();
4564 PyObject *temp = tstate->c_profileobj;
4565 Py_XINCREF(arg);
4566 tstate->c_profilefunc = NULL;
4567 tstate->c_profileobj = NULL;
4568 /* Must make sure that tracing is not ignored if 'temp' is freed */
4569 tstate->use_tracing = tstate->c_tracefunc != NULL;
4570 Py_XDECREF(temp);
4571 tstate->c_profilefunc = func;
4572 tstate->c_profileobj = arg;
4573 /* Flag that tracing or profiling is turned on */
4574 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004575}
4576
4577void
4578PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4579{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004580 PyThreadState *tstate = PyThreadState_GET();
4581 PyObject *temp = tstate->c_traceobj;
4582 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4583 Py_XINCREF(arg);
4584 tstate->c_tracefunc = NULL;
4585 tstate->c_traceobj = NULL;
4586 /* Must make sure that profiling is not ignored if 'temp' is freed */
4587 tstate->use_tracing = tstate->c_profilefunc != NULL;
4588 Py_XDECREF(temp);
4589 tstate->c_tracefunc = func;
4590 tstate->c_traceobj = arg;
4591 /* Flag that tracing or profiling is turned on */
4592 tstate->use_tracing = ((func != NULL)
4593 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004594}
4595
Yury Selivanov75445082015-05-11 22:57:16 -04004596void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004597_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004598{
4599 PyThreadState *tstate = PyThreadState_GET();
4600
Yury Selivanov75445082015-05-11 22:57:16 -04004601 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004602 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004603}
4604
4605PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004606_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004607{
4608 PyThreadState *tstate = PyThreadState_GET();
4609 return tstate->coroutine_wrapper;
4610}
4611
Yury Selivanoveb636452016-09-08 22:01:51 -07004612void
4613_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4614{
4615 PyThreadState *tstate = PyThreadState_GET();
4616
4617 Py_XINCREF(firstiter);
4618 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4619}
4620
4621PyObject *
4622_PyEval_GetAsyncGenFirstiter(void)
4623{
4624 PyThreadState *tstate = PyThreadState_GET();
4625 return tstate->async_gen_firstiter;
4626}
4627
4628void
4629_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4630{
4631 PyThreadState *tstate = PyThreadState_GET();
4632
4633 Py_XINCREF(finalizer);
4634 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4635}
4636
4637PyObject *
4638_PyEval_GetAsyncGenFinalizer(void)
4639{
4640 PyThreadState *tstate = PyThreadState_GET();
4641 return tstate->async_gen_finalizer;
4642}
4643
Guido van Rossumb209a111997-04-29 18:18:01 +00004644PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004645PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004646{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004647 PyFrameObject *current_frame = PyEval_GetFrame();
4648 if (current_frame == NULL)
4649 return PyThreadState_GET()->interp->builtins;
4650 else
4651 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004652}
4653
Guido van Rossumb209a111997-04-29 18:18:01 +00004654PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004655PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004656{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004657 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004658 if (current_frame == NULL) {
4659 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004660 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004661 }
4662
4663 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4664 return NULL;
4665
4666 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004667 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004668}
4669
Guido van Rossumb209a111997-04-29 18:18:01 +00004670PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004671PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004672{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004673 PyFrameObject *current_frame = PyEval_GetFrame();
4674 if (current_frame == NULL)
4675 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004676
4677 assert(current_frame->f_globals != NULL);
4678 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004679}
4680
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004681PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004682PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004683{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004684 PyThreadState *tstate = PyThreadState_GET();
4685 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004686}
4687
Guido van Rossum6135a871995-01-09 17:53:26 +00004688int
Tim Peters5ba58662001-07-16 02:29:45 +00004689PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004690{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004691 PyFrameObject *current_frame = PyEval_GetFrame();
4692 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004694 if (current_frame != NULL) {
4695 const int codeflags = current_frame->f_code->co_flags;
4696 const int compilerflags = codeflags & PyCF_MASK;
4697 if (compilerflags) {
4698 result = 1;
4699 cf->cf_flags |= compilerflags;
4700 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004701#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004702 if (codeflags & CO_GENERATOR_ALLOWED) {
4703 result = 1;
4704 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4705 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004706#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004707 }
4708 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004709}
4710
Guido van Rossum3f5da241990-12-20 15:06:42 +00004711
Guido van Rossum681d79a1995-07-18 14:51:37 +00004712/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00004713 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00004714
Guido van Rossumb209a111997-04-29 18:18:01 +00004715PyObject *
Victor Stinner8a31c822016-08-19 17:12:23 +02004716PyEval_CallObjectWithKeywords(PyObject *func, PyObject *args, PyObject *kwargs)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004717{
Victor Stinner59b356d2015-03-16 11:52:32 +01004718#ifdef Py_DEBUG
4719 /* PyEval_CallObjectWithKeywords() must not be called with an exception
4720 set. It raises a new exception if parameters are invalid or if
4721 PyTuple_New() fails, and so the original exception is lost. */
4722 assert(!PyErr_Occurred());
4723#endif
4724
INADA Naoki023532e2017-03-01 21:14:43 +09004725 if (args != NULL && !PyTuple_Check(args)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004726 PyErr_SetString(PyExc_TypeError,
4727 "argument list must be a tuple");
4728 return NULL;
4729 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00004730
Victor Stinner8a31c822016-08-19 17:12:23 +02004731 if (kwargs != NULL && !PyDict_Check(kwargs)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004732 PyErr_SetString(PyExc_TypeError,
4733 "keyword list must be a dictionary");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004734 return NULL;
4735 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00004736
INADA Naoki023532e2017-03-01 21:14:43 +09004737 if (args == NULL) {
4738 return _PyObject_FastCallDict(func, NULL, 0, kwargs);
4739 }
4740 else {
4741 return PyObject_Call(func, args, kwargs);
4742 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004743}
4744
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004745const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004746PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004747{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004748 if (PyMethod_Check(func))
4749 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4750 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004751 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004752 else if (PyCFunction_Check(func))
4753 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4754 else
4755 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004756}
4757
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004758const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004759PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004761 if (PyMethod_Check(func))
4762 return "()";
4763 else if (PyFunction_Check(func))
4764 return "()";
4765 else if (PyCFunction_Check(func))
4766 return "()";
4767 else
4768 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004769}
4770
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004771#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004772if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004773 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4774 tstate, tstate->frame, \
4775 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004776 x = NULL; \
4777 } \
4778 else { \
4779 x = call; \
4780 if (tstate->c_profilefunc != NULL) { \
4781 if (x == NULL) { \
4782 call_trace_protected(tstate->c_profilefunc, \
4783 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004784 tstate, tstate->frame, \
4785 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004786 /* XXX should pass (type, value, tb) */ \
4787 } else { \
4788 if (call_trace(tstate->c_profilefunc, \
4789 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004790 tstate, tstate->frame, \
4791 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004792 Py_DECREF(x); \
4793 x = NULL; \
4794 } \
4795 } \
4796 } \
4797 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004798} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004799 x = call; \
4800 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004801
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004802static PyObject *
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004803call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004804{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004805 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004806 PyObject *func = *pfunc;
4807 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004808 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4809 Py_ssize_t nargs = oparg - nkwargs;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004810 PyObject **stack;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004812 /* Always dispatch PyCFunction first, because these are
4813 presumed to be the most frequent callable object.
4814 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004815 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004816 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004818 PCALL(PCALL_CFUNCTION);
Victor Stinner4a7cc882015-03-06 23:35:27 +01004819
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004820 stack = (*pp_stack) - nargs - nkwargs;
4821 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004822 }
4823 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004824 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4825 /* optimize access to bound methods */
4826 PyObject *self = PyMethod_GET_SELF(func);
4827 PCALL(PCALL_METHOD);
4828 PCALL(PCALL_BOUND_METHOD);
4829 Py_INCREF(self);
4830 func = PyMethod_GET_FUNCTION(func);
4831 Py_INCREF(func);
4832 Py_SETREF(*pfunc, self);
4833 nargs++;
4834 }
4835 else {
4836 Py_INCREF(func);
4837 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004838
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004839 stack = (*pp_stack) - nargs - nkwargs;
Victor Stinner4a7cc882015-03-06 23:35:27 +01004840
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004841 if (PyFunction_Check(func)) {
4842 x = fast_function(func, stack, nargs, kwnames);
4843 }
4844 else {
4845 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4846 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004847
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004848 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004849 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004850
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004851 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004853 /* Clear the stack of the function object. Also removes
4854 the arguments in case they weren't consumed already
Victor Stinnere90bdb12016-08-25 23:26:50 +02004855 (fast_function() and err_args() leave them on the stack).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004856 */
4857 while ((*pp_stack) > pfunc) {
4858 w = EXT_POP(*pp_stack);
4859 Py_DECREF(w);
4860 PCALL(PCALL_POP);
4861 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004863 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004864}
4865
Victor Stinnere90bdb12016-08-25 23:26:50 +02004866/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004867 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004868 For the simplest case -- a function that takes only positional
4869 arguments and is called with only positional arguments -- it
4870 inlines the most primitive frame setup code from
4871 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4872 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004873*/
4874
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004875static PyObject*
Victor Stinnerd8735722016-09-09 12:36:44 -07004876_PyFunction_FastCall(PyCodeObject *co, PyObject **args, Py_ssize_t nargs,
4877 PyObject *globals)
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004878{
4879 PyFrameObject *f;
4880 PyThreadState *tstate = PyThreadState_GET();
4881 PyObject **fastlocals;
4882 Py_ssize_t i;
4883 PyObject *result;
4884
4885 PCALL(PCALL_FASTER_FUNCTION);
4886 assert(globals != NULL);
4887 /* XXX Perhaps we should create a specialized
4888 PyFrame_New() that doesn't take locals, but does
4889 take builtins without sanity checking them.
4890 */
4891 assert(tstate != NULL);
4892 f = PyFrame_New(tstate, co, globals, NULL);
4893 if (f == NULL) {
4894 return NULL;
4895 }
4896
4897 fastlocals = f->f_localsplus;
4898
Victor Stinner74319ae2016-08-25 00:04:09 +02004899 for (i = 0; i < nargs; i++) {
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004900 Py_INCREF(*args);
4901 fastlocals[i] = *args++;
4902 }
4903 result = PyEval_EvalFrameEx(f,0);
4904
4905 ++tstate->recursion_depth;
4906 Py_DECREF(f);
4907 --tstate->recursion_depth;
4908
4909 return result;
4910}
4911
Victor Stinnere90bdb12016-08-25 23:26:50 +02004912static PyObject *
Victor Stinnerd8735722016-09-09 12:36:44 -07004913fast_function(PyObject *func, PyObject **stack,
4914 Py_ssize_t nargs, PyObject *kwnames)
Jeremy Hylton52820442001-01-03 23:52:36 +00004915{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004916 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4917 PyObject *globals = PyFunction_GET_GLOBALS(func);
4918 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004919 PyObject *kwdefs, *closure, *name, *qualname;
4920 PyObject **d;
Victor Stinnerd8735722016-09-09 12:36:44 -07004921 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004922 Py_ssize_t nd;
Victor Stinnerd8735722016-09-09 12:36:44 -07004923
Victor Stinner57f91ac2016-09-12 13:37:07 +02004924 assert(PyFunction_Check(func));
4925 assert(nargs >= 0);
4926 assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
Victor Stinnerd8735722016-09-09 12:36:44 -07004927 assert((nargs == 0 && nkwargs == 0) || stack != NULL);
Victor Stinner57f91ac2016-09-12 13:37:07 +02004928 /* kwnames must only contains str strings, no subclass, and all keys must
4929 be unique */
Victor Stinner577e1f82016-08-25 00:29:32 +02004930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004931 PCALL(PCALL_FUNCTION);
4932 PCALL(PCALL_FAST_FUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004933
Victor Stinner74319ae2016-08-25 00:04:09 +02004934 if (co->co_kwonlyargcount == 0 && nkwargs == 0 &&
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004935 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
4936 {
Victor Stinner2eedc112016-08-22 12:29:42 +02004937 if (argdefs == NULL && co->co_argcount == nargs) {
Victor Stinnerd8735722016-09-09 12:36:44 -07004938 return _PyFunction_FastCall(co, stack, nargs, globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02004939 }
4940 else if (nargs == 0 && argdefs != NULL
4941 && co->co_argcount == Py_SIZE(argdefs)) {
4942 /* function called with no arguments, but all parameters have
4943 a default value: use default values as arguments .*/
4944 stack = &PyTuple_GET_ITEM(argdefs, 0);
Victor Stinnerd8735722016-09-09 12:36:44 -07004945 return _PyFunction_FastCall(co, stack, Py_SIZE(argdefs), globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02004946 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004947 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004948
4949 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
4950 closure = PyFunction_GET_CLOSURE(func);
4951 name = ((PyFunctionObject *)func) -> func_name;
4952 qualname = ((PyFunctionObject *)func) -> func_qualname;
4953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004954 if (argdefs != NULL) {
4955 d = &PyTuple_GET_ITEM(argdefs, 0);
4956 nd = Py_SIZE(argdefs);
4957 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004958 else {
4959 d = NULL;
4960 nd = 0;
4961 }
4962 return _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
Victor Stinner2eedc112016-08-22 12:29:42 +02004963 stack, nargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004964 nkwargs ? &PyTuple_GET_ITEM(kwnames, 0) : NULL,
4965 stack + nargs,
4966 nkwargs, 1,
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004967 d, (int)nd, kwdefs,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004968 closure, name, qualname);
4969}
4970
4971PyObject *
Victor Stinnerd8735722016-09-09 12:36:44 -07004972_PyFunction_FastCallKeywords(PyObject *func, PyObject **stack,
4973 Py_ssize_t nargs, PyObject *kwnames)
4974{
4975 return fast_function(func, stack, nargs, kwnames);
4976}
4977
4978PyObject *
Victor Stinner74319ae2016-08-25 00:04:09 +02004979_PyFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs,
Victor Stinnerb9009392016-08-22 23:15:44 +02004980 PyObject *kwargs)
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004981{
4982 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4983 PyObject *globals = PyFunction_GET_GLOBALS(func);
4984 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4985 PyObject *kwdefs, *closure, *name, *qualname;
Victor Stinnerb9009392016-08-22 23:15:44 +02004986 PyObject *kwtuple, **k;
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004987 PyObject **d;
Victor Stinner74319ae2016-08-25 00:04:09 +02004988 Py_ssize_t nd, nk;
Victor Stinnerb9009392016-08-22 23:15:44 +02004989 PyObject *result;
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004990
Victor Stinner74319ae2016-08-25 00:04:09 +02004991 assert(func != NULL);
4992 assert(nargs >= 0);
4993 assert(nargs == 0 || args != NULL);
Victor Stinnerb9009392016-08-22 23:15:44 +02004994 assert(kwargs == NULL || PyDict_Check(kwargs));
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004995
Victor Stinner577e1f82016-08-25 00:29:32 +02004996 PCALL(PCALL_FUNCTION);
4997 PCALL(PCALL_FAST_FUNCTION);
4998
Victor Stinnerb9009392016-08-22 23:15:44 +02004999 if (co->co_kwonlyargcount == 0 &&
5000 (kwargs == NULL || PyDict_Size(kwargs) == 0) &&
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005001 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
5002 {
Victor Stinnerb9009392016-08-22 23:15:44 +02005003 /* Fast paths */
Victor Stinner2eedc112016-08-22 12:29:42 +02005004 if (argdefs == NULL && co->co_argcount == nargs) {
Victor Stinnerd8735722016-09-09 12:36:44 -07005005 return _PyFunction_FastCall(co, args, nargs, globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02005006 }
5007 else if (nargs == 0 && argdefs != NULL
5008 && co->co_argcount == Py_SIZE(argdefs)) {
5009 /* function called with no arguments, but all parameters have
5010 a default value: use default values as arguments .*/
5011 args = &PyTuple_GET_ITEM(argdefs, 0);
Victor Stinnerd8735722016-09-09 12:36:44 -07005012 return _PyFunction_FastCall(co, args, Py_SIZE(argdefs), globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02005013 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005014 }
5015
Victor Stinnerb9009392016-08-22 23:15:44 +02005016 if (kwargs != NULL) {
5017 Py_ssize_t pos, i;
5018 nk = PyDict_Size(kwargs);
5019
5020 kwtuple = PyTuple_New(2 * nk);
5021 if (kwtuple == NULL) {
5022 return NULL;
5023 }
5024
5025 k = &PyTuple_GET_ITEM(kwtuple, 0);
5026 pos = i = 0;
5027 while (PyDict_Next(kwargs, &pos, &k[i], &k[i+1])) {
5028 Py_INCREF(k[i]);
5029 Py_INCREF(k[i+1]);
5030 i += 2;
5031 }
5032 nk = i / 2;
5033 }
5034 else {
5035 kwtuple = NULL;
5036 k = NULL;
5037 nk = 0;
5038 }
5039
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005040 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
5041 closure = PyFunction_GET_CLOSURE(func);
5042 name = ((PyFunctionObject *)func) -> func_name;
5043 qualname = ((PyFunctionObject *)func) -> func_qualname;
5044
5045 if (argdefs != NULL) {
5046 d = &PyTuple_GET_ITEM(argdefs, 0);
5047 nd = Py_SIZE(argdefs);
5048 }
5049 else {
5050 d = NULL;
5051 nd = 0;
5052 }
Victor Stinnerb9009392016-08-22 23:15:44 +02005053
5054 result = _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
5055 args, nargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03005056 k, k + 1, nk, 2,
Victor Stinnerb9009392016-08-22 23:15:44 +02005057 d, nd, kwdefs,
5058 closure, name, qualname);
5059 Py_XDECREF(kwtuple);
5060 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00005061}
5062
5063static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005064do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005065{
Jeremy Hylton985eba52003-02-05 23:13:00 +00005066#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005067 /* At this point, we have to look at the type of func to
5068 update the call stats properly. Do it here so as to avoid
5069 exposing the call stats machinery outside ceval.c
5070 */
5071 if (PyFunction_Check(func))
5072 PCALL(PCALL_FUNCTION);
5073 else if (PyMethod_Check(func))
5074 PCALL(PCALL_METHOD);
5075 else if (PyType_Check(func))
5076 PCALL(PCALL_TYPE);
5077 else if (PyCFunction_Check(func))
5078 PCALL(PCALL_CFUNCTION);
5079 else
5080 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00005081#endif
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005083 if (PyCFunction_Check(func)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005084 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005085 PyThreadState *tstate = PyThreadState_GET();
5086 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005087 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005088 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005089 else {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005090 return PyObject_Call(func, callargs, kwdict);
Victor Stinner74319ae2016-08-25 00:04:09 +02005091 }
Jeremy Hylton52820442001-01-03 23:52:36 +00005092}
5093
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005094/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005095 nb_index slot defined, and store in *pi.
5096 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang98b49a02017-05-10 19:00:15 +08005097 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005098 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005099*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005100int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005101_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005102{
Serhiy Storchakabf4bb2e2017-03-30 19:46:59 +03005103 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005104 Py_ssize_t x;
5105 if (PyIndex_Check(v)) {
5106 x = PyNumber_AsSsize_t(v, NULL);
5107 if (x == -1 && PyErr_Occurred())
5108 return 0;
5109 }
5110 else {
5111 PyErr_SetString(PyExc_TypeError,
5112 "slice indices must be integers or "
5113 "None or have an __index__ method");
5114 return 0;
5115 }
5116 *pi = x;
5117 }
5118 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005119}
5120
Serhiy Storchakabf4bb2e2017-03-30 19:46:59 +03005121int
5122_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
5123{
5124 Py_ssize_t x;
5125 if (PyIndex_Check(v)) {
5126 x = PyNumber_AsSsize_t(v, NULL);
5127 if (x == -1 && PyErr_Occurred())
5128 return 0;
5129 }
5130 else {
5131 PyErr_SetString(PyExc_TypeError,
5132 "slice indices must be integers or "
5133 "have an __index__ method");
5134 return 0;
5135 }
5136 *pi = x;
5137 return 1;
5138}
5139
5140
Guido van Rossum486364b2007-06-30 05:01:58 +00005141#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005142 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00005143
Guido van Rossumb209a111997-04-29 18:18:01 +00005144static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02005145cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005146{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005147 int res = 0;
5148 switch (op) {
5149 case PyCmp_IS:
5150 res = (v == w);
5151 break;
5152 case PyCmp_IS_NOT:
5153 res = (v != w);
5154 break;
5155 case PyCmp_IN:
5156 res = PySequence_Contains(w, v);
5157 if (res < 0)
5158 return NULL;
5159 break;
5160 case PyCmp_NOT_IN:
5161 res = PySequence_Contains(w, v);
5162 if (res < 0)
5163 return NULL;
5164 res = !res;
5165 break;
5166 case PyCmp_EXC_MATCH:
5167 if (PyTuple_Check(w)) {
5168 Py_ssize_t i, length;
5169 length = PyTuple_Size(w);
5170 for (i = 0; i < length; i += 1) {
5171 PyObject *exc = PyTuple_GET_ITEM(w, i);
5172 if (!PyExceptionClass_Check(exc)) {
5173 PyErr_SetString(PyExc_TypeError,
5174 CANNOT_CATCH_MSG);
5175 return NULL;
5176 }
5177 }
5178 }
5179 else {
5180 if (!PyExceptionClass_Check(w)) {
5181 PyErr_SetString(PyExc_TypeError,
5182 CANNOT_CATCH_MSG);
5183 return NULL;
5184 }
5185 }
5186 res = PyErr_GivenExceptionMatches(v, w);
5187 break;
5188 default:
5189 return PyObject_RichCompare(v, w, op);
5190 }
5191 v = res ? Py_True : Py_False;
5192 Py_INCREF(v);
5193 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005194}
5195
Thomas Wouters52152252000-08-17 22:55:00 +00005196static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005197import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
5198{
5199 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005200 PyObject *import_func, *res;
5201 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005202
5203 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
5204 if (import_func == NULL) {
5205 PyErr_SetString(PyExc_ImportError, "__import__ not found");
5206 return NULL;
5207 }
5208
5209 /* Fast path for not overloaded __import__. */
5210 if (import_func == PyThreadState_GET()->interp->import_func) {
5211 int ilevel = _PyLong_AsInt(level);
5212 if (ilevel == -1 && PyErr_Occurred()) {
5213 return NULL;
5214 }
5215 res = PyImport_ImportModuleLevelObject(
5216 name,
5217 f->f_globals,
5218 f->f_locals == NULL ? Py_None : f->f_locals,
5219 fromlist,
5220 ilevel);
5221 return res;
5222 }
5223
5224 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005225
5226 stack[0] = name;
5227 stack[1] = f->f_globals;
5228 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5229 stack[3] = fromlist;
5230 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005231 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005232 Py_DECREF(import_func);
5233 return res;
5234}
5235
5236static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00005237import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005238{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005239 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005240 _Py_IDENTIFIER(__name__);
5241 PyObject *fullmodname, *pkgname;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005243 x = PyObject_GetAttr(v, name);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005244 if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
5245 return x;
5246 /* Issue #17636: in case this failed because of a circular relative
5247 import, try to fallback on reading the module directly from
5248 sys.modules. */
5249 PyErr_Clear();
5250 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005251 if (pkgname == NULL) {
5252 goto error;
5253 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005254 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
5255 Py_DECREF(pkgname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005256 if (fullmodname == NULL) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005257 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005258 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005259 x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005260 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005261 if (x == NULL) {
5262 goto error;
5263 }
5264 Py_INCREF(x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005265 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005266 error:
5267 PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
5268 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005269}
Guido van Rossumac7be682001-01-17 15:42:30 +00005270
Thomas Wouters52152252000-08-17 22:55:00 +00005271static int
5272import_all_from(PyObject *locals, PyObject *v)
5273{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005274 _Py_IDENTIFIER(__all__);
5275 _Py_IDENTIFIER(__dict__);
5276 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005277 PyObject *dict, *name, *value;
5278 int skip_leading_underscores = 0;
5279 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005281 if (all == NULL) {
5282 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5283 return -1; /* Unexpected error */
5284 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005285 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005286 if (dict == NULL) {
5287 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5288 return -1;
5289 PyErr_SetString(PyExc_ImportError,
5290 "from-import-* object has no __dict__ and no __all__");
5291 return -1;
5292 }
5293 all = PyMapping_Keys(dict);
5294 Py_DECREF(dict);
5295 if (all == NULL)
5296 return -1;
5297 skip_leading_underscores = 1;
5298 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005300 for (pos = 0, err = 0; ; pos++) {
5301 name = PySequence_GetItem(all, pos);
5302 if (name == NULL) {
5303 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5304 err = -1;
5305 else
5306 PyErr_Clear();
5307 break;
5308 }
5309 if (skip_leading_underscores &&
5310 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005311 PyUnicode_READY(name) != -1 &&
5312 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005313 {
5314 Py_DECREF(name);
5315 continue;
5316 }
5317 value = PyObject_GetAttr(v, name);
5318 if (value == NULL)
5319 err = -1;
5320 else if (PyDict_CheckExact(locals))
5321 err = PyDict_SetItem(locals, name, value);
5322 else
5323 err = PyObject_SetItem(locals, name, value);
5324 Py_DECREF(name);
5325 Py_XDECREF(value);
5326 if (err != 0)
5327 break;
5328 }
5329 Py_DECREF(all);
5330 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005331}
5332
Serhiy Storchaka946a0b62017-08-03 12:14:35 +03005333static int
5334check_args_iterable(PyObject *func, PyObject *args)
5335{
5336 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
5337 PyErr_Format(PyExc_TypeError,
5338 "%.200s%.200s argument after * "
5339 "must be an iterable, not %.200s",
5340 PyEval_GetFuncName(func),
5341 PyEval_GetFuncDesc(func),
5342 args->ob_type->tp_name);
5343 return -1;
5344 }
5345 return 0;
5346}
5347
5348static void
5349format_kwargs_mapping_error(PyObject *func, PyObject *kwargs)
5350{
5351 PyErr_Format(PyExc_TypeError,
5352 "%.200s%.200s argument after ** "
5353 "must be a mapping, not %.200s",
5354 PyEval_GetFuncName(func),
5355 PyEval_GetFuncDesc(func),
5356 kwargs->ob_type->tp_name);
5357}
5358
Guido van Rossumac7be682001-01-17 15:42:30 +00005359static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005360format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005362 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005364 if (!obj)
5365 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005366
Serhiy Storchaka06515832016-11-20 09:13:07 +02005367 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005368 if (!obj_str)
5369 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005371 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005372}
Guido van Rossum950361c1997-01-24 13:49:28 +00005373
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005374static void
5375format_exc_unbound(PyCodeObject *co, int oparg)
5376{
5377 PyObject *name;
5378 /* Don't stomp existing exception */
5379 if (PyErr_Occurred())
5380 return;
5381 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5382 name = PyTuple_GET_ITEM(co->co_cellvars,
5383 oparg);
5384 format_exc_check_arg(
5385 PyExc_UnboundLocalError,
5386 UNBOUNDLOCAL_ERROR_MSG,
5387 name);
5388 } else {
5389 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5390 PyTuple_GET_SIZE(co->co_cellvars));
5391 format_exc_check_arg(PyExc_NameError,
5392 UNBOUNDFREE_ERROR_MSG, name);
5393 }
5394}
5395
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005396static PyObject *
5397unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005398 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005399{
5400 PyObject *res;
5401 if (Py_REFCNT(v) == 2) {
5402 /* In the common case, there are 2 references to the value
5403 * stored in 'variable' when the += is performed: one on the
5404 * value stack (in 'v') and one still stored in the
5405 * 'variable'. We try to delete the variable now to reduce
5406 * the refcnt to 1.
5407 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005408 int opcode, oparg;
5409 NEXTOPARG();
5410 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005411 case STORE_FAST:
5412 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005413 PyObject **fastlocals = f->f_localsplus;
5414 if (GETLOCAL(oparg) == v)
5415 SETLOCAL(oparg, NULL);
5416 break;
5417 }
5418 case STORE_DEREF:
5419 {
5420 PyObject **freevars = (f->f_localsplus +
5421 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005422 PyObject *c = freevars[oparg];
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005423 if (PyCell_GET(c) == v)
5424 PyCell_Set(c, NULL);
5425 break;
5426 }
5427 case STORE_NAME:
5428 {
5429 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005430 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005431 PyObject *locals = f->f_locals;
5432 if (PyDict_CheckExact(locals) &&
5433 PyDict_GetItem(locals, name) == v) {
5434 if (PyDict_DelItem(locals, name) != 0) {
5435 PyErr_Clear();
5436 }
5437 }
5438 break;
5439 }
5440 }
5441 }
5442 res = v;
5443 PyUnicode_Append(&res, w);
5444 return res;
5445}
5446
Guido van Rossum950361c1997-01-24 13:49:28 +00005447#ifdef DYNAMIC_EXECUTION_PROFILE
5448
Skip Montanarof118cb12001-10-15 20:51:38 +00005449static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005450getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005451{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005452 int i;
5453 PyObject *l = PyList_New(256);
5454 if (l == NULL) return NULL;
5455 for (i = 0; i < 256; i++) {
5456 PyObject *x = PyLong_FromLong(a[i]);
5457 if (x == NULL) {
5458 Py_DECREF(l);
5459 return NULL;
5460 }
5461 PyList_SetItem(l, i, x);
5462 }
5463 for (i = 0; i < 256; i++)
5464 a[i] = 0;
5465 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005466}
5467
5468PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005469_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005470{
5471#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005472 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005473#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005474 int i;
5475 PyObject *l = PyList_New(257);
5476 if (l == NULL) return NULL;
5477 for (i = 0; i < 257; i++) {
5478 PyObject *x = getarray(dxpairs[i]);
5479 if (x == NULL) {
5480 Py_DECREF(l);
5481 return NULL;
5482 }
5483 PyList_SetItem(l, i, x);
5484 }
5485 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005486#endif
5487}
5488
5489#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005490
5491Py_ssize_t
5492_PyEval_RequestCodeExtraIndex(freefunc free)
5493{
Dino Viehland2997fec2017-06-12 18:46:35 -07005494 __PyCodeExtraState *state = __PyCodeExtraState_Get();
Brett Cannon5c4de282016-09-07 11:16:41 -07005495 Py_ssize_t new_index;
5496
Dino Viehland2997fec2017-06-12 18:46:35 -07005497 if (state->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005498 return -1;
5499 }
Dino Viehland2997fec2017-06-12 18:46:35 -07005500 new_index = state->co_extra_user_count++;
5501 state->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005502 return new_index;
5503}
Łukasz Langaa785c872016-09-09 17:37:37 -07005504
5505static void
5506dtrace_function_entry(PyFrameObject *f)
5507{
5508 char* filename;
5509 char* funcname;
5510 int lineno;
5511
5512 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5513 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5514 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5515
5516 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5517}
5518
5519static void
5520dtrace_function_return(PyFrameObject *f)
5521{
5522 char* filename;
5523 char* funcname;
5524 int lineno;
5525
5526 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5527 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5528 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5529
5530 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5531}
5532
5533/* DTrace equivalent of maybe_call_line_trace. */
5534static void
5535maybe_dtrace_line(PyFrameObject *frame,
5536 int *instr_lb, int *instr_ub, int *instr_prev)
5537{
5538 int line = frame->f_lineno;
5539 char *co_filename, *co_name;
5540
5541 /* If the last instruction executed isn't in the current
5542 instruction window, reset the window.
5543 */
5544 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5545 PyAddrPair bounds;
5546 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5547 &bounds);
5548 *instr_lb = bounds.ap_lower;
5549 *instr_ub = bounds.ap_upper;
5550 }
5551 /* If the last instruction falls at the start of a line or if
5552 it represents a jump backwards, update the frame's line
5553 number and call the trace function. */
5554 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5555 frame->f_lineno = line;
5556 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5557 if (!co_filename)
5558 co_filename = "?";
5559 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5560 if (!co_name)
5561 co_name = "?";
5562 PyDTrace_LINE(co_filename, co_name, line);
5563 }
5564 *instr_prev = frame->f_lasti;
5565}