blob: d3bd8b569b9aac12c7ccc36e14f7cf087e82a73a [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"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040018#include "setobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000019#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000020
Guido van Rossumc6004111993-11-05 10:22:19 +000021#include <ctype.h>
22
Guido van Rossum04691fc1992-08-12 15:35:34 +000023/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000024/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000025
Guido van Rossum408027e1996-12-30 16:17:54 +000026#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000027/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000028#define LLTRACE 1 /* Low-level trace feature */
29#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000030#endif
31
Jeremy Hylton52820442001-01-03 23:52:36 +000032typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000033
Guido van Rossum374a9221991-04-04 10:40:29 +000034/* Forward declarations */
Victor Stinnerf9b760f2016-09-09 10:17:08 -070035static PyObject * call_function(PyObject ***, Py_ssize_t, PyObject *);
Victor Stinnerd8735722016-09-09 12:36:44 -070036static PyObject * fast_function(PyObject *, PyObject **, Py_ssize_t, PyObject *);
Victor Stinnerf9b760f2016-09-09 10:17:08 -070037static PyObject * do_call_core(PyObject *, PyObject *, PyObject *);
Jeremy Hylton52820442001-01-03 23:52:36 +000038
Guido van Rossum0a066c01992-03-27 17:29:15 +000039#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000040static int lltrace;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020041static int prtrace(PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000042#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010043static int call_trace(Py_tracefunc, PyObject *,
44 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000046static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010047 PyThreadState *, PyFrameObject *,
48 int, PyObject *);
49static void call_exc_trace(Py_tracefunc, PyObject *,
50 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000051static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010052 PyThreadState *, PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000053
Thomas Wouters477c8d52006-05-27 19:21:47 +000054static PyObject * cmp_outcome(int, PyObject *, PyObject *);
Serhiy Storchaka133138a2016-08-02 22:51:21 +030055static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *, PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +000056static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +000057static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +000058static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +000059static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +020060static PyObject * unicode_concatenate(PyObject *, PyObject *,
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +030061 PyFrameObject *, const unsigned short *);
Benjamin Petersonce798522012-01-22 11:24:29 -050062static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Guido van Rossum374a9221991-04-04 10:40:29 +000063
Paul Prescode68140d2000-08-30 20:25:01 +000064#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000066#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000068#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 "free variable '%.200s' referenced before assignment" \
70 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000071
Guido van Rossum950361c1997-01-24 13:49:28 +000072/* Dynamic execution profile */
73#ifdef DYNAMIC_EXECUTION_PROFILE
74#ifdef DXPAIRS
75static long dxpairs[257][256];
76#define dxp dxpairs[256]
77#else
78static long dxp[256];
79#endif
80#endif
81
Jeremy Hylton985eba52003-02-05 23:13:00 +000082/* Function call profile */
83#ifdef CALL_PROFILE
84#define PCALL_NUM 11
85static int pcall[PCALL_NUM];
86
87#define PCALL_ALL 0
88#define PCALL_FUNCTION 1
89#define PCALL_FAST_FUNCTION 2
90#define PCALL_FASTER_FUNCTION 3
91#define PCALL_METHOD 4
92#define PCALL_BOUND_METHOD 5
93#define PCALL_CFUNCTION 6
94#define PCALL_TYPE 7
95#define PCALL_GENERATOR 8
96#define PCALL_OTHER 9
97#define PCALL_POP 10
98
99/* Notes about the statistics
100
101 PCALL_FAST stats
102
103 FAST_FUNCTION means no argument tuple needs to be created.
104 FASTER_FUNCTION means that the fast-path frame setup code is used.
105
106 If there is a method call where the call can be optimized by changing
107 the argument tuple and calling the function directly, it gets recorded
108 twice.
109
110 As a result, the relationship among the statistics appears to be
111 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
112 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
113 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
114 PCALL_METHOD > PCALL_BOUND_METHOD
115*/
116
117#define PCALL(POS) pcall[POS]++
118
119PyObject *
120PyEval_GetCallStats(PyObject *self)
121{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 return Py_BuildValue("iiiiiiiiiii",
123 pcall[0], pcall[1], pcall[2], pcall[3],
124 pcall[4], pcall[5], pcall[6], pcall[7],
125 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000126}
127#else
128#define PCALL(O)
129
130PyObject *
131PyEval_GetCallStats(PyObject *self)
132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 Py_INCREF(Py_None);
134 return Py_None;
Jeremy Hylton985eba52003-02-05 23:13:00 +0000135}
136#endif
137
Tim Peters5ca576e2001-06-18 22:08:13 +0000138
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000139#ifdef WITH_THREAD
140#define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request)
141#else
142#define GIL_REQUEST 0
143#endif
144
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000145/* This can set eval_breaker to 0 even though gil_drop_request became
146 1. We believe this is all right because the eval loop will release
147 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000148#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 _Py_atomic_store_relaxed( \
150 &eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000151 GIL_REQUEST | \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
153 pending_async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000154
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000155#ifdef WITH_THREAD
156
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000157#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 do { \
159 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
160 _Py_atomic_store_relaxed(&eval_breaker, 1); \
161 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000162
163#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 do { \
165 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
166 COMPUTE_EVAL_BREAKER(); \
167 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000168
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000169#endif
170
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000171/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000172#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 do { \
174 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
175 _Py_atomic_store_relaxed(&eval_breaker, 1); \
176 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000177
178#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179 do { \
180 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
181 COMPUTE_EVAL_BREAKER(); \
182 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000183
184#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 do { \
186 pending_async_exc = 1; \
187 _Py_atomic_store_relaxed(&eval_breaker, 1); \
188 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000189
190#define UNSIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000192
193
Guido van Rossume59214e1994-08-30 08:01:59 +0000194#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000195
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000196#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000197#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000198#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000199#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000200
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000201static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000202static long main_thread = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000203/* This single variable consolidates all requests to break out of the fast path
204 in the eval loop. */
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000205static _Py_atomic_int eval_breaker = {0};
206/* Request for dropping the GIL */
207static _Py_atomic_int gil_drop_request = {0};
208/* Request for running pending calls. */
209static _Py_atomic_int pendingcalls_to_do = {0};
210/* Request for looking at the `async_exc` field of the current thread state.
211 Guarded by the GIL. */
212static int pending_async_exc = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000213
214#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000215
Tim Peters7f468f22004-10-11 02:40:51 +0000216int
217PyEval_ThreadsInitialized(void)
218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000220}
221
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000222void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000223PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000224{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 if (gil_created())
226 return;
227 create_gil();
228 take_gil(PyThreadState_GET());
229 main_thread = PyThread_get_thread_ident();
230 if (!pending_lock)
231 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000232}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000233
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000234void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000235_PyEval_FiniThreads(void)
236{
237 if (!gil_created())
238 return;
239 destroy_gil();
240 assert(!gil_created());
241}
242
243void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000244PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000245{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 PyThreadState *tstate = PyThreadState_GET();
247 if (tstate == NULL)
248 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
249 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000250}
251
252void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000253PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 /* This function must succeed when the current thread state is NULL.
256 We therefore avoid PyThreadState_GET() which dumps a fatal error
257 in debug mode.
258 */
259 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
260 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000261}
262
263void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000264PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000265{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 if (tstate == NULL)
267 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
268 /* Check someone has called PyEval_InitThreads() to create the lock */
269 assert(gil_created());
270 take_gil(tstate);
271 if (PyThreadState_Swap(tstate) != NULL)
272 Py_FatalError(
273 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000274}
275
276void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000277PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000278{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 if (tstate == NULL)
280 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
281 if (PyThreadState_Swap(NULL) != tstate)
282 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
283 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000284}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000285
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200286/* This function is called from PyOS_AfterFork to destroy all threads which are
287 * not running in the child process, and clear internal locks which might be
288 * held by those threads. (This could also be done using pthread_atfork
289 * mechanism, at least for the pthreads implementation.) */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000290
291void
292PyEval_ReInitThreads(void)
293{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200294 _Py_IDENTIFIER(_after_fork);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 PyObject *threading, *result;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200296 PyThreadState *current_tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 if (!gil_created())
299 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 recreate_gil();
301 pending_lock = PyThread_allocate_lock();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200302 take_gil(current_tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 /* Update the threading module with the new state.
306 */
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200307 threading = PyMapping_GetItemString(current_tstate->interp->modules,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 "threading");
309 if (threading == NULL) {
310 /* threading not imported */
311 PyErr_Clear();
312 return;
313 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200314 result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 if (result == NULL)
316 PyErr_WriteUnraisable(threading);
317 else
318 Py_DECREF(result);
319 Py_DECREF(threading);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200320
321 /* Destroy all threads except the current one */
322 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000323}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000324
325#else
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000326static _Py_atomic_int eval_breaker = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000327static int pending_async_exc = 0;
328#endif /* WITH_THREAD */
329
330/* This function is used to signal that async exceptions are waiting to be
331 raised, therefore it is also useful in non-threaded builds. */
332
333void
334_PyEval_SignalAsyncExc(void)
335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000337}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000338
Guido van Rossumff4949e1992-08-05 19:58:53 +0000339/* Functions save_thread and restore_thread are always defined so
340 dynamically loaded modules needn't be compiled separately for use
341 with and without threads: */
342
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000343PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000344PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 PyThreadState *tstate = PyThreadState_Swap(NULL);
347 if (tstate == NULL)
348 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000349#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 if (gil_created())
351 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000352#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000354}
355
356void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000357PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000358{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 if (tstate == NULL)
360 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000361#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 if (gil_created()) {
363 int err = errno;
364 take_gil(tstate);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200365 /* _Py_Finalizing is protected by the GIL */
366 if (_Py_Finalizing && tstate != _Py_Finalizing) {
367 drop_gil(tstate);
368 PyThread_exit_thread();
369 assert(0); /* unreachable */
370 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 errno = err;
372 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000373#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000375}
376
377
Guido van Rossuma9672091994-09-14 13:31:22 +0000378/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
379 signal handlers or Mac I/O completion routines) can schedule calls
380 to a function to be called synchronously.
381 The synchronous function is called with one void* argument.
382 It should return 0 for success or -1 for failure -- failure should
383 be accompanied by an exception.
384
385 If registry succeeds, the registry function returns 0; if it fails
386 (e.g. due to too many pending calls) it returns -1 (without setting
387 an exception condition).
388
389 Note that because registry may occur from within signal handlers,
390 or other asynchronous events, calling malloc() is unsafe!
391
392#ifdef WITH_THREAD
393 Any thread can schedule pending calls, but only the main thread
394 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000395 There is no facility to schedule calls to a particular thread, but
396 that should be easy to change, should that ever be required. In
397 that case, the static variables here should go into the python
398 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000399#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000400*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000401
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000402#ifdef WITH_THREAD
403
404/* The WITH_THREAD implementation is thread-safe. It allows
405 scheduling to be made from any thread, and even from an executing
406 callback.
407 */
408
409#define NPENDINGCALLS 32
410static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 int (*func)(void *);
412 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000413} pendingcalls[NPENDINGCALLS];
414static int pendingfirst = 0;
415static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000416
417int
418Py_AddPendingCall(int (*func)(void *), void *arg)
419{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 int i, j, result=0;
421 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 /* try a few times for the lock. Since this mechanism is used
424 * for signal handling (on the main thread), there is a (slim)
425 * chance that a signal is delivered on the same thread while we
426 * hold the lock during the Py_MakePendingCalls() function.
427 * This avoids a deadlock in that case.
428 * Note that signals can be delivered on any thread. In particular,
429 * on Windows, a SIGINT is delivered on a system-created worker
430 * thread.
431 * We also check for lock being NULL, in the unlikely case that
432 * this function is called before any bytecode evaluation takes place.
433 */
434 if (lock != NULL) {
435 for (i = 0; i<100; i++) {
436 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
437 break;
438 }
439 if (i == 100)
440 return -1;
441 }
442
443 i = pendinglast;
444 j = (i + 1) % NPENDINGCALLS;
445 if (j == pendingfirst) {
446 result = -1; /* Queue full */
447 } else {
448 pendingcalls[i].func = func;
449 pendingcalls[i].arg = arg;
450 pendinglast = j;
451 }
452 /* signal main loop */
453 SIGNAL_PENDING_CALLS();
454 if (lock != NULL)
455 PyThread_release_lock(lock);
456 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000457}
458
459int
460Py_MakePendingCalls(void)
461{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200462 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 int i;
464 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 if (!pending_lock) {
467 /* initial allocation of the lock */
468 pending_lock = PyThread_allocate_lock();
469 if (pending_lock == NULL)
470 return -1;
471 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 /* only service pending calls on main thread */
474 if (main_thread && PyThread_get_thread_ident() != main_thread)
475 return 0;
476 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200477 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200479 busy = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 /* perform a bounded number of calls, in case of recursion */
481 for (i=0; i<NPENDINGCALLS; i++) {
482 int j;
483 int (*func)(void *);
484 void *arg = NULL;
485
486 /* pop one item off the queue while holding the lock */
487 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
488 j = pendingfirst;
489 if (j == pendinglast) {
490 func = NULL; /* Queue empty */
491 } else {
492 func = pendingcalls[j].func;
493 arg = pendingcalls[j].arg;
494 pendingfirst = (j + 1) % NPENDINGCALLS;
495 }
496 if (pendingfirst != pendinglast)
497 SIGNAL_PENDING_CALLS();
498 else
499 UNSIGNAL_PENDING_CALLS();
500 PyThread_release_lock(pending_lock);
501 /* having released the lock, perform the callback */
502 if (func == NULL)
503 break;
504 r = func(arg);
505 if (r)
506 break;
507 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200508 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000510}
511
512#else /* if ! defined WITH_THREAD */
513
514/*
515 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
516 This code is used for signal handling in python that isn't built
517 with WITH_THREAD.
518 Don't use this implementation when Py_AddPendingCalls() can happen
519 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520
Guido van Rossuma9672091994-09-14 13:31:22 +0000521 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000522 (1) nested asynchronous calls to Py_AddPendingCall()
523 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000525 (1) is very unlikely because typically signal delivery
526 is blocked during signal handling. So it should be impossible.
527 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000528 The current code is safe against (2), but not against (1).
529 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000530 thread is present, interrupted by signals, and that the critical
531 section is protected with the "busy" variable. On Windows, which
532 delivers SIGINT on a system thread, this does not hold and therefore
533 Windows really shouldn't use this version.
534 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000535*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000536
Guido van Rossuma9672091994-09-14 13:31:22 +0000537#define NPENDINGCALLS 32
538static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 int (*func)(void *);
540 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000541} pendingcalls[NPENDINGCALLS];
542static volatile int pendingfirst = 0;
543static volatile int pendinglast = 0;
Benjamin Peterson08ec84c2010-05-30 14:49:32 +0000544static _Py_atomic_int pendingcalls_to_do = {0};
Guido van Rossuma9672091994-09-14 13:31:22 +0000545
546int
Thomas Wouters334fb892000-07-25 12:56:38 +0000547Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000548{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 static volatile int busy = 0;
550 int i, j;
551 /* XXX Begin critical section */
552 if (busy)
553 return -1;
554 busy = 1;
555 i = pendinglast;
556 j = (i + 1) % NPENDINGCALLS;
557 if (j == pendingfirst) {
558 busy = 0;
559 return -1; /* Queue full */
560 }
561 pendingcalls[i].func = func;
562 pendingcalls[i].arg = arg;
563 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 SIGNAL_PENDING_CALLS();
566 busy = 0;
567 /* XXX End critical section */
568 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000569}
570
Guido van Rossum180d7b41994-09-29 09:45:57 +0000571int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000572Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000573{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 static int busy = 0;
575 if (busy)
576 return 0;
577 busy = 1;
578 UNSIGNAL_PENDING_CALLS();
579 for (;;) {
580 int i;
581 int (*func)(void *);
582 void *arg;
583 i = pendingfirst;
584 if (i == pendinglast)
585 break; /* Queue empty */
586 func = pendingcalls[i].func;
587 arg = pendingcalls[i].arg;
588 pendingfirst = (i + 1) % NPENDINGCALLS;
589 if (func(arg) < 0) {
590 busy = 0;
591 SIGNAL_PENDING_CALLS(); /* We're not done yet */
592 return -1;
593 }
594 }
595 busy = 0;
596 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000597}
598
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000599#endif /* WITH_THREAD */
600
Guido van Rossuma9672091994-09-14 13:31:22 +0000601
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000602/* The interpreter's recursion limit */
603
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000604#ifndef Py_DEFAULT_RECURSION_LIMIT
605#define Py_DEFAULT_RECURSION_LIMIT 1000
606#endif
607static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
608int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000609
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000610int
611Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000612{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000614}
615
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000616void
617Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000618{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 recursion_limit = new_limit;
620 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000621}
622
Armin Rigo2b3eb402003-10-28 12:05:48 +0000623/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
624 if the recursion_depth reaches _Py_CheckRecursionLimit.
625 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
626 to guarantee that _Py_CheckRecursiveCall() is regularly called.
627 Without USE_STACKCHECK, there is no need for this. */
628int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300629_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000632
633#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 if (PyOS_CheckStack()) {
635 --tstate->recursion_depth;
636 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
637 return -1;
638 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000639#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 _Py_CheckRecursionLimit = recursion_limit;
641 if (tstate->recursion_critical)
642 /* Somebody asked that we don't check for recursion. */
643 return 0;
644 if (tstate->overflowed) {
645 if (tstate->recursion_depth > recursion_limit + 50) {
646 /* Overflowing while handling an overflow. Give up. */
647 Py_FatalError("Cannot recover from stack overflow.");
648 }
649 return 0;
650 }
651 if (tstate->recursion_depth > recursion_limit) {
652 --tstate->recursion_depth;
653 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400654 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 "maximum recursion depth exceeded%s",
656 where);
657 return -1;
658 }
659 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000660}
661
Guido van Rossum374a9221991-04-04 10:40:29 +0000662/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000663enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000664 WHY_NOT = 0x0001, /* No error */
665 WHY_EXCEPTION = 0x0002, /* Exception occurred */
Stefan Krahb7e10102010-06-23 18:42:39 +0000666 WHY_RETURN = 0x0008, /* 'return' statement */
667 WHY_BREAK = 0x0010, /* 'break' statement */
668 WHY_CONTINUE = 0x0020, /* 'continue' statement */
669 WHY_YIELD = 0x0040, /* 'yield' operator */
670 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000671};
Guido van Rossum374a9221991-04-04 10:40:29 +0000672
Benjamin Peterson87880242011-07-03 16:48:31 -0500673static void save_exc_state(PyThreadState *, PyFrameObject *);
674static void swap_exc_state(PyThreadState *, PyFrameObject *);
675static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400676static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000677static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000678
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000679/* Records whether tracing is on for any thread. Counts the number of
680 threads for which tstate->c_tracefunc is non-NULL, so if the value
681 is 0, we know we don't have to check this thread's c_tracefunc.
682 This speeds up the if statement in PyEval_EvalFrameEx() after
683 fast_next_opcode*/
684static int _Py_TracingPossible = 0;
685
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000686
Guido van Rossum374a9221991-04-04 10:40:29 +0000687
Guido van Rossumb209a111997-04-29 18:18:01 +0000688PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000689PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000690{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 return PyEval_EvalCodeEx(co,
692 globals, locals,
693 (PyObject **)NULL, 0,
694 (PyObject **)NULL, 0,
695 (PyObject **)NULL, 0,
696 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000697}
698
699
700/* Interpreter main loop */
701
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000702PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000703PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 /* This is for backward compatibility with extension modules that
705 used this API; core interpreter code should call
706 PyEval_EvalFrameEx() */
707 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000708}
709
710PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000711PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000712{
Brett Cannon3cebf932016-09-05 15:33:46 -0700713 PyThreadState *tstate = PyThreadState_GET();
714 return tstate->interp->eval_frame(f, throwflag);
715}
716
717PyObject *
718_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
719{
Guido van Rossum950361c1997-01-24 13:49:28 +0000720#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000722#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200723 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300724 const unsigned short *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200725 int opcode; /* Current opcode */
726 int oparg; /* Current opcode argument, if any */
727 enum why_code why; /* Reason for block stack unwind */
728 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 PyObject *retval = NULL; /* Return value */
730 PyThreadState *tstate = PyThreadState_GET();
731 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 is true when the line being executed has changed. The
738 initial values are such as to make this false the first
739 time it is tested. */
740 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000741
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300742 const unsigned short *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 PyObject *names;
744 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000745
Brett Cannon368b4b72012-04-02 12:17:59 -0400746#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200747 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400748#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200749
Antoine Pitroub52ec782009-01-25 16:34:23 +0000750/* Computed GOTOs, or
751 the-optimization-commonly-but-improperly-known-as-"threaded code"
752 using gcc's labels-as-values extension
753 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
754
755 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000757 combined with a lookup table of jump addresses. However, since the
758 indirect jump instruction is shared by all opcodes, the CPU will have a
759 hard time making the right prediction for where to jump next (actually,
760 it will be always wrong except in the uncommon case of a sequence of
761 several identical opcodes).
762
763 "Threaded code" in contrast, uses an explicit jump table and an explicit
764 indirect jump instruction at the end of each opcode. Since the jump
765 instruction is at a different address for each opcode, the CPU will make a
766 separate prediction for each of these instructions, which is equivalent to
767 predicting the second opcode of each opcode pair. These predictions have
768 a much better chance to turn out valid, especially in small bytecode loops.
769
770 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000772 and potentially many more instructions (depending on the pipeline width).
773 A correctly predicted branch, however, is nearly free.
774
775 At the time of this writing, the "threaded code" version is up to 15-20%
776 faster than the normal "switch" version, depending on the compiler and the
777 CPU architecture.
778
779 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
780 because it would render the measurements invalid.
781
782
783 NOTE: care must be taken that the compiler doesn't try to "optimize" the
784 indirect jumps by sharing them between all opcodes. Such optimizations
785 can be disabled on gcc by using the -fno-gcse flag (or possibly
786 -fno-crossjumping).
787*/
788
Antoine Pitrou042b1282010-08-13 21:15:58 +0000789#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000790#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000791#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000792#endif
793
Antoine Pitrou042b1282010-08-13 21:15:58 +0000794#ifdef HAVE_COMPUTED_GOTOS
795 #ifndef USE_COMPUTED_GOTOS
796 #define USE_COMPUTED_GOTOS 1
797 #endif
798#else
799 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
800 #error "Computed gotos are not supported on this compiler."
801 #endif
802 #undef USE_COMPUTED_GOTOS
803 #define USE_COMPUTED_GOTOS 0
804#endif
805
806#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000807/* Import the static jump table */
808#include "opcode_targets.h"
809
Antoine Pitroub52ec782009-01-25 16:34:23 +0000810#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 TARGET_##op: \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000813
Antoine Pitroub52ec782009-01-25 16:34:23 +0000814#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 { \
816 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
817 FAST_DISPATCH(); \
818 } \
819 continue; \
820 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000821
822#ifdef LLTRACE
823#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 { \
825 if (!lltrace && !_Py_TracingPossible) { \
826 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300827 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300828 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 } \
830 goto fast_next_opcode; \
831 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000832#else
833#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 { \
835 if (!_Py_TracingPossible) { \
836 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300837 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300838 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 } \
840 goto fast_next_opcode; \
841 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000842#endif
843
844#else
845#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 case op:
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300847
Antoine Pitroub52ec782009-01-25 16:34:23 +0000848#define DISPATCH() continue
849#define FAST_DISPATCH() goto fast_next_opcode
850#endif
851
852
Neal Norwitza81d2202002-07-14 00:27:26 +0000853/* Tuple access macros */
854
855#ifndef Py_DEBUG
856#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
857#else
858#define GETITEM(v, i) PyTuple_GetItem((v), (i))
859#endif
860
Guido van Rossum374a9221991-04-04 10:40:29 +0000861/* Code access macros */
862
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300863#ifdef WORDS_BIGENDIAN
864 #define OPCODE(word) ((word) >> 8)
865 #define OPARG(word) ((word) & 255)
866#else
867 #define OPCODE(word) ((word) & 255)
868 #define OPARG(word) ((word) >> 8)
869#endif
870/* The integer overflow is checked by an assertion below. */
871#define INSTR_OFFSET() (2*(int)(next_instr - first_instr))
872#define NEXTOPARG() do { \
873 unsigned short word = *next_instr; \
874 opcode = OPCODE(word); \
875 oparg = OPARG(word); \
876 next_instr++; \
877 } while (0)
878#define JUMPTO(x) (next_instr = first_instr + (x)/2)
879#define JUMPBY(x) (next_instr += (x)/2)
Guido van Rossum374a9221991-04-04 10:40:29 +0000880
Raymond Hettingerf606f872003-03-16 03:11:04 +0000881/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 Some opcodes tend to come in pairs thus making it possible to
883 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300884 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 Verifying the prediction costs a single high-speed test of a register
887 variable against a constant. If the pairing was good, then the
888 processor's own internal branch predication has a high likelihood of
889 success, resulting in a nearly zero-overhead transition to the
890 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300891 including its unpredictable switch-case branch. Combined with the
892 processor's internal branch prediction, a successful PREDICT has the
893 effect of making the two opcodes run as if they were a single new opcode
894 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000895
Georg Brandl86b2fb92008-07-16 03:43:04 +0000896 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 predictions turned-on and interpret the results as if some opcodes
898 had been combined or turn-off predictions so that the opcode frequency
899 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000900
901 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 the CPU to record separate branch prediction information for each
903 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000904
Raymond Hettingerf606f872003-03-16 03:11:04 +0000905*/
906
Antoine Pitrou042b1282010-08-13 21:15:58 +0000907#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000909#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300910#define PREDICT(op) \
911 do{ \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300912 unsigned short word = *next_instr; \
913 opcode = OPCODE(word); \
914 if (opcode == op){ \
915 oparg = OPARG(word); \
916 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300917 goto PRED_##op; \
918 } \
919 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000920#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300921#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000922
Raymond Hettingerf606f872003-03-16 03:11:04 +0000923
Guido van Rossum374a9221991-04-04 10:40:29 +0000924/* Stack manipulation macros */
925
Martin v. Löwis18e16552006-02-15 17:27:45 +0000926/* The stack can grow at most MAXINT deep, as co_nlocals and
927 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000928#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
929#define EMPTY() (STACK_LEVEL() == 0)
930#define TOP() (stack_pointer[-1])
931#define SECOND() (stack_pointer[-2])
932#define THIRD() (stack_pointer[-3])
933#define FOURTH() (stack_pointer[-4])
934#define PEEK(n) (stack_pointer[-(n)])
935#define SET_TOP(v) (stack_pointer[-1] = (v))
936#define SET_SECOND(v) (stack_pointer[-2] = (v))
937#define SET_THIRD(v) (stack_pointer[-3] = (v))
938#define SET_FOURTH(v) (stack_pointer[-4] = (v))
939#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
940#define BASIC_STACKADJ(n) (stack_pointer += n)
941#define BASIC_PUSH(v) (*stack_pointer++ = (v))
942#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000943
Guido van Rossum96a42c81992-01-12 02:29:51 +0000944#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000946 lltrace && prtrace(TOP(), "push")); \
947 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000949 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000951 lltrace && prtrace(TOP(), "stackadj")); \
952 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +0000953#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000954 prtrace((STACK_POINTER)[-1], "ext_pop")), \
955 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000956#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000957#define PUSH(v) BASIC_PUSH(v)
958#define POP() BASIC_POP()
959#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000960#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000961#endif
962
Guido van Rossum681d79a1995-07-18 14:51:37 +0000963/* Local variable macros */
964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000966
967/* The SETLOCAL() macro must not DECREF the local variable in-place and
968 then store the new value; it must copy the old value to a temporary
969 value, then store the new value, and then DECREF the temporary value.
970 This is because it is possible that during the DECREF the frame is
971 accessed by other code (e.g. a __del__ method or gc.collect()) and the
972 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000974 GETLOCAL(i) = value; \
975 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000976
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000977
978#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 while (STACK_LEVEL() > (b)->b_level) { \
980 PyObject *v = POP(); \
981 Py_XDECREF(v); \
982 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000983
984#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300985 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 PyObject *type, *value, *traceback; \
987 assert(STACK_LEVEL() >= (b)->b_level + 3); \
988 while (STACK_LEVEL() > (b)->b_level + 3) { \
989 value = POP(); \
990 Py_XDECREF(value); \
991 } \
992 type = tstate->exc_type; \
993 value = tstate->exc_value; \
994 traceback = tstate->exc_traceback; \
995 tstate->exc_type = POP(); \
996 tstate->exc_value = POP(); \
997 tstate->exc_traceback = POP(); \
998 Py_XDECREF(type); \
999 Py_XDECREF(value); \
1000 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001001 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001002
Guido van Rossuma027efa1997-05-05 20:56:21 +00001003/* Start of code */
1004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 /* push frame */
1006 if (Py_EnterRecursiveCall(""))
1007 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 if (tstate->use_tracing) {
1012 if (tstate->c_tracefunc != NULL) {
1013 /* tstate->c_tracefunc, if defined, is a
1014 function that will be called on *every* entry
1015 to a code block. Its return value, if not
1016 None, is a function that will be called at
1017 the start of each executed line of code.
1018 (Actually, the function must return itself
1019 in order to continue tracing.) The trace
1020 functions are called with three arguments:
1021 a pointer to the current frame, a string
1022 indicating why the function is called, and
1023 an argument which depends on the situation.
1024 The global trace function is also called
1025 whenever an exception is detected. */
1026 if (call_trace_protected(tstate->c_tracefunc,
1027 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001028 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 /* Trace function raised an error */
1030 goto exit_eval_frame;
1031 }
1032 }
1033 if (tstate->c_profilefunc != NULL) {
1034 /* Similar for c_profilefunc, except it needn't
1035 return itself and isn't called for "line" events */
1036 if (call_trace_protected(tstate->c_profilefunc,
1037 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001038 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 /* Profile function raised an error */
1040 goto exit_eval_frame;
1041 }
1042 }
1043 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 co = f->f_code;
1046 names = co->co_names;
1047 consts = co->co_consts;
1048 fastlocals = f->f_localsplus;
1049 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001050 assert(PyBytes_Check(co->co_code));
1051 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
1052 assert(PyBytes_GET_SIZE(co->co_code) % 2 == 0);
Serhiy Storchaka74f2fe62016-05-25 20:35:44 +03001053 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), 2));
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001054 first_instr = (unsigned short*) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001055 /*
1056 f->f_lasti refers to the index of the last instruction,
1057 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001058
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001059 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001060 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 When the PREDICT() macros are enabled, some opcode pairs follow in
1063 direct succession without updating f->f_lasti. A successful
1064 prediction effectively links the two codes together as if they
1065 were a single new opcode; accordingly,f->f_lasti will point to
1066 the first code in the pair (for instance, GET_ITER followed by
1067 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001068 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001070 next_instr = first_instr;
1071 if (f->f_lasti >= 0) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001072 assert(f->f_lasti % 2 == 0);
1073 next_instr += f->f_lasti/2 + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001074 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 stack_pointer = f->f_stacktop;
1076 assert(stack_pointer != NULL);
1077 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001078 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001079
Yury Selivanoveb636452016-09-08 22:01:51 -07001080 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01001081 if (!throwflag && f->f_exc_type != NULL && f->f_exc_type != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 /* We were in an except handler when we left,
1083 restore the exception state which was put aside
1084 (see YIELD_VALUE). */
Benjamin Peterson87880242011-07-03 16:48:31 -05001085 swap_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 }
Benjamin Peterson87880242011-07-03 16:48:31 -05001087 else
1088 save_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001090
Tim Peters5ca576e2001-06-18 22:08:13 +00001091#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001092 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001093#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 why = WHY_NOT;
Guido van Rossumac7be682001-01-17 15:42:30 +00001096
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001097 if (throwflag) /* support for generator.throw() */
1098 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001099
Victor Stinnerace47d72013-07-18 01:41:08 +02001100#ifdef Py_DEBUG
1101 /* PyEval_EvalFrameEx() must not be called with an exception set,
1102 because it may clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001103 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +02001104 assert(!PyErr_Occurred());
1105#endif
1106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1109 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +02001110 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 /* Do periodic things. Doing this every time through
1113 the loop would add too much overhead, so we do it
1114 only every Nth instruction. We also do it if
1115 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1116 event needs attention (e.g. a signal handler or
1117 async I/O handler); see Py_AddPendingCall() and
1118 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 if (_Py_atomic_load_relaxed(&eval_breaker)) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001121 if (OPCODE(*next_instr) == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 /* Make the last opcode before
Ezio Melotti13925002011-03-16 11:05:33 +02001123 a try: finally: block uninterruptible. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 goto fast_next_opcode;
1125 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001127 if (Py_MakePendingCalls() < 0)
1128 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001130#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001131 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 /* Give another thread a chance */
1133 if (PyThreadState_Swap(NULL) != tstate)
1134 Py_FatalError("ceval: tstate mix-up");
1135 drop_gil(tstate);
1136
1137 /* Other threads may run now */
1138
1139 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001140
1141 /* Check if we should make a quick exit. */
1142 if (_Py_Finalizing && _Py_Finalizing != tstate) {
1143 drop_gil(tstate);
1144 PyThread_exit_thread();
1145 }
1146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 if (PyThreadState_Swap(tstate) != NULL)
1148 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001150#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 /* Check for asynchronous exceptions. */
1152 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001153 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 tstate->async_exc = NULL;
1155 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001156 PyErr_SetNone(exc);
1157 Py_DECREF(exc);
1158 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 }
1160 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 fast_next_opcode:
1163 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001168 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001169 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 /* see maybe_call_line_trace
1171 for expository comments */
1172 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 err = maybe_call_line_trace(tstate->c_tracefunc,
1175 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001176 tstate, f,
1177 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 /* Reload possibly changed frame fields */
1179 JUMPTO(f->f_lasti);
1180 if (f->f_stacktop != NULL) {
1181 stack_pointer = f->f_stacktop;
1182 f->f_stacktop = NULL;
1183 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001184 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001186 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001190
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001191 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001192 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001193#ifdef DYNAMIC_EXECUTION_PROFILE
1194#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 dxpairs[lastopcode][opcode]++;
1196 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001197#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001199#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001200
Guido van Rossum96a42c81992-01-12 02:29:51 +00001201#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 if (lltrace) {
1205 if (HAS_ARG(opcode)) {
1206 printf("%d: %d, %d\n",
1207 f->f_lasti, opcode, oparg);
1208 }
1209 else {
1210 printf("%d: %d\n",
1211 f->f_lasti, opcode);
1212 }
1213 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001214#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 /* BEWARE!
1219 It is essential that any operation that fails sets either
1220 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1221 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 TARGET(NOP)
1224 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001225
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001226 TARGET(LOAD_FAST) {
1227 PyObject *value = GETLOCAL(oparg);
1228 if (value == NULL) {
1229 format_exc_check_arg(PyExc_UnboundLocalError,
1230 UNBOUNDLOCAL_ERROR_MSG,
1231 PyTuple_GetItem(co->co_varnames, oparg));
1232 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001234 Py_INCREF(value);
1235 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001237 }
1238
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001239 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001240 TARGET(LOAD_CONST) {
1241 PyObject *value = GETITEM(consts, oparg);
1242 Py_INCREF(value);
1243 PUSH(value);
1244 FAST_DISPATCH();
1245 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001246
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001247 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001248 TARGET(STORE_FAST) {
1249 PyObject *value = POP();
1250 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001252 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001253
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001254 TARGET(POP_TOP) {
1255 PyObject *value = POP();
1256 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001258 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001259
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001260 TARGET(ROT_TWO) {
1261 PyObject *top = TOP();
1262 PyObject *second = SECOND();
1263 SET_TOP(second);
1264 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001266 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001267
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001268 TARGET(ROT_THREE) {
1269 PyObject *top = TOP();
1270 PyObject *second = SECOND();
1271 PyObject *third = THIRD();
1272 SET_TOP(second);
1273 SET_SECOND(third);
1274 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001276 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001277
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001278 TARGET(DUP_TOP) {
1279 PyObject *top = TOP();
1280 Py_INCREF(top);
1281 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001283 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001284
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001285 TARGET(DUP_TOP_TWO) {
1286 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001287 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001288 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001289 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001290 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001291 SET_TOP(top);
1292 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001293 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001294 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001295
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001296 TARGET(UNARY_POSITIVE) {
1297 PyObject *value = TOP();
1298 PyObject *res = PyNumber_Positive(value);
1299 Py_DECREF(value);
1300 SET_TOP(res);
1301 if (res == NULL)
1302 goto error;
1303 DISPATCH();
1304 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001305
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001306 TARGET(UNARY_NEGATIVE) {
1307 PyObject *value = TOP();
1308 PyObject *res = PyNumber_Negative(value);
1309 Py_DECREF(value);
1310 SET_TOP(res);
1311 if (res == NULL)
1312 goto error;
1313 DISPATCH();
1314 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001315
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001316 TARGET(UNARY_NOT) {
1317 PyObject *value = TOP();
1318 int err = PyObject_IsTrue(value);
1319 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 if (err == 0) {
1321 Py_INCREF(Py_True);
1322 SET_TOP(Py_True);
1323 DISPATCH();
1324 }
1325 else if (err > 0) {
1326 Py_INCREF(Py_False);
1327 SET_TOP(Py_False);
1328 err = 0;
1329 DISPATCH();
1330 }
1331 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001332 goto error;
1333 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001334
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001335 TARGET(UNARY_INVERT) {
1336 PyObject *value = TOP();
1337 PyObject *res = PyNumber_Invert(value);
1338 Py_DECREF(value);
1339 SET_TOP(res);
1340 if (res == NULL)
1341 goto error;
1342 DISPATCH();
1343 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001344
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001345 TARGET(BINARY_POWER) {
1346 PyObject *exp = POP();
1347 PyObject *base = TOP();
1348 PyObject *res = PyNumber_Power(base, exp, Py_None);
1349 Py_DECREF(base);
1350 Py_DECREF(exp);
1351 SET_TOP(res);
1352 if (res == NULL)
1353 goto error;
1354 DISPATCH();
1355 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001356
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001357 TARGET(BINARY_MULTIPLY) {
1358 PyObject *right = POP();
1359 PyObject *left = TOP();
1360 PyObject *res = PyNumber_Multiply(left, right);
1361 Py_DECREF(left);
1362 Py_DECREF(right);
1363 SET_TOP(res);
1364 if (res == NULL)
1365 goto error;
1366 DISPATCH();
1367 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001368
Benjamin Petersond51374e2014-04-09 23:55:56 -04001369 TARGET(BINARY_MATRIX_MULTIPLY) {
1370 PyObject *right = POP();
1371 PyObject *left = TOP();
1372 PyObject *res = PyNumber_MatrixMultiply(left, right);
1373 Py_DECREF(left);
1374 Py_DECREF(right);
1375 SET_TOP(res);
1376 if (res == NULL)
1377 goto error;
1378 DISPATCH();
1379 }
1380
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001381 TARGET(BINARY_TRUE_DIVIDE) {
1382 PyObject *divisor = POP();
1383 PyObject *dividend = TOP();
1384 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1385 Py_DECREF(dividend);
1386 Py_DECREF(divisor);
1387 SET_TOP(quotient);
1388 if (quotient == NULL)
1389 goto error;
1390 DISPATCH();
1391 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001392
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001393 TARGET(BINARY_FLOOR_DIVIDE) {
1394 PyObject *divisor = POP();
1395 PyObject *dividend = TOP();
1396 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1397 Py_DECREF(dividend);
1398 Py_DECREF(divisor);
1399 SET_TOP(quotient);
1400 if (quotient == NULL)
1401 goto error;
1402 DISPATCH();
1403 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001404
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001405 TARGET(BINARY_MODULO) {
1406 PyObject *divisor = POP();
1407 PyObject *dividend = TOP();
1408 PyObject *res = PyUnicode_CheckExact(dividend) ?
1409 PyUnicode_Format(dividend, divisor) :
1410 PyNumber_Remainder(dividend, divisor);
1411 Py_DECREF(divisor);
1412 Py_DECREF(dividend);
1413 SET_TOP(res);
1414 if (res == NULL)
1415 goto error;
1416 DISPATCH();
1417 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001418
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001419 TARGET(BINARY_ADD) {
1420 PyObject *right = POP();
1421 PyObject *left = TOP();
1422 PyObject *sum;
1423 if (PyUnicode_CheckExact(left) &&
1424 PyUnicode_CheckExact(right)) {
1425 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001426 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001427 }
1428 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001429 sum = PyNumber_Add(left, right);
1430 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001431 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001432 Py_DECREF(right);
1433 SET_TOP(sum);
1434 if (sum == NULL)
1435 goto error;
1436 DISPATCH();
1437 }
1438
1439 TARGET(BINARY_SUBTRACT) {
1440 PyObject *right = POP();
1441 PyObject *left = TOP();
1442 PyObject *diff = PyNumber_Subtract(left, right);
1443 Py_DECREF(right);
1444 Py_DECREF(left);
1445 SET_TOP(diff);
1446 if (diff == NULL)
1447 goto error;
1448 DISPATCH();
1449 }
1450
1451 TARGET(BINARY_SUBSCR) {
1452 PyObject *sub = POP();
1453 PyObject *container = TOP();
1454 PyObject *res = PyObject_GetItem(container, sub);
1455 Py_DECREF(container);
1456 Py_DECREF(sub);
1457 SET_TOP(res);
1458 if (res == NULL)
1459 goto error;
1460 DISPATCH();
1461 }
1462
1463 TARGET(BINARY_LSHIFT) {
1464 PyObject *right = POP();
1465 PyObject *left = TOP();
1466 PyObject *res = PyNumber_Lshift(left, right);
1467 Py_DECREF(left);
1468 Py_DECREF(right);
1469 SET_TOP(res);
1470 if (res == NULL)
1471 goto error;
1472 DISPATCH();
1473 }
1474
1475 TARGET(BINARY_RSHIFT) {
1476 PyObject *right = POP();
1477 PyObject *left = TOP();
1478 PyObject *res = PyNumber_Rshift(left, right);
1479 Py_DECREF(left);
1480 Py_DECREF(right);
1481 SET_TOP(res);
1482 if (res == NULL)
1483 goto error;
1484 DISPATCH();
1485 }
1486
1487 TARGET(BINARY_AND) {
1488 PyObject *right = POP();
1489 PyObject *left = TOP();
1490 PyObject *res = PyNumber_And(left, right);
1491 Py_DECREF(left);
1492 Py_DECREF(right);
1493 SET_TOP(res);
1494 if (res == NULL)
1495 goto error;
1496 DISPATCH();
1497 }
1498
1499 TARGET(BINARY_XOR) {
1500 PyObject *right = POP();
1501 PyObject *left = TOP();
1502 PyObject *res = PyNumber_Xor(left, right);
1503 Py_DECREF(left);
1504 Py_DECREF(right);
1505 SET_TOP(res);
1506 if (res == NULL)
1507 goto error;
1508 DISPATCH();
1509 }
1510
1511 TARGET(BINARY_OR) {
1512 PyObject *right = POP();
1513 PyObject *left = TOP();
1514 PyObject *res = PyNumber_Or(left, right);
1515 Py_DECREF(left);
1516 Py_DECREF(right);
1517 SET_TOP(res);
1518 if (res == NULL)
1519 goto error;
1520 DISPATCH();
1521 }
1522
1523 TARGET(LIST_APPEND) {
1524 PyObject *v = POP();
1525 PyObject *list = PEEK(oparg);
1526 int err;
1527 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001529 if (err != 0)
1530 goto error;
1531 PREDICT(JUMP_ABSOLUTE);
1532 DISPATCH();
1533 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001534
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001535 TARGET(SET_ADD) {
1536 PyObject *v = POP();
1537 PyObject *set = stack_pointer[-oparg];
1538 int err;
1539 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001541 if (err != 0)
1542 goto error;
1543 PREDICT(JUMP_ABSOLUTE);
1544 DISPATCH();
1545 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001546
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001547 TARGET(INPLACE_POWER) {
1548 PyObject *exp = POP();
1549 PyObject *base = TOP();
1550 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1551 Py_DECREF(base);
1552 Py_DECREF(exp);
1553 SET_TOP(res);
1554 if (res == NULL)
1555 goto error;
1556 DISPATCH();
1557 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001558
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001559 TARGET(INPLACE_MULTIPLY) {
1560 PyObject *right = POP();
1561 PyObject *left = TOP();
1562 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1563 Py_DECREF(left);
1564 Py_DECREF(right);
1565 SET_TOP(res);
1566 if (res == NULL)
1567 goto error;
1568 DISPATCH();
1569 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001570
Benjamin Petersond51374e2014-04-09 23:55:56 -04001571 TARGET(INPLACE_MATRIX_MULTIPLY) {
1572 PyObject *right = POP();
1573 PyObject *left = TOP();
1574 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1575 Py_DECREF(left);
1576 Py_DECREF(right);
1577 SET_TOP(res);
1578 if (res == NULL)
1579 goto error;
1580 DISPATCH();
1581 }
1582
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001583 TARGET(INPLACE_TRUE_DIVIDE) {
1584 PyObject *divisor = POP();
1585 PyObject *dividend = TOP();
1586 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1587 Py_DECREF(dividend);
1588 Py_DECREF(divisor);
1589 SET_TOP(quotient);
1590 if (quotient == NULL)
1591 goto error;
1592 DISPATCH();
1593 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001594
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001595 TARGET(INPLACE_FLOOR_DIVIDE) {
1596 PyObject *divisor = POP();
1597 PyObject *dividend = TOP();
1598 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1599 Py_DECREF(dividend);
1600 Py_DECREF(divisor);
1601 SET_TOP(quotient);
1602 if (quotient == NULL)
1603 goto error;
1604 DISPATCH();
1605 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001606
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001607 TARGET(INPLACE_MODULO) {
1608 PyObject *right = POP();
1609 PyObject *left = TOP();
1610 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1611 Py_DECREF(left);
1612 Py_DECREF(right);
1613 SET_TOP(mod);
1614 if (mod == NULL)
1615 goto error;
1616 DISPATCH();
1617 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001618
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001619 TARGET(INPLACE_ADD) {
1620 PyObject *right = POP();
1621 PyObject *left = TOP();
1622 PyObject *sum;
1623 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1624 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001625 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001626 }
1627 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001628 sum = PyNumber_InPlaceAdd(left, right);
1629 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001630 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001631 Py_DECREF(right);
1632 SET_TOP(sum);
1633 if (sum == NULL)
1634 goto error;
1635 DISPATCH();
1636 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001637
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001638 TARGET(INPLACE_SUBTRACT) {
1639 PyObject *right = POP();
1640 PyObject *left = TOP();
1641 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1642 Py_DECREF(left);
1643 Py_DECREF(right);
1644 SET_TOP(diff);
1645 if (diff == NULL)
1646 goto error;
1647 DISPATCH();
1648 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001649
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001650 TARGET(INPLACE_LSHIFT) {
1651 PyObject *right = POP();
1652 PyObject *left = TOP();
1653 PyObject *res = PyNumber_InPlaceLshift(left, right);
1654 Py_DECREF(left);
1655 Py_DECREF(right);
1656 SET_TOP(res);
1657 if (res == NULL)
1658 goto error;
1659 DISPATCH();
1660 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001661
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001662 TARGET(INPLACE_RSHIFT) {
1663 PyObject *right = POP();
1664 PyObject *left = TOP();
1665 PyObject *res = PyNumber_InPlaceRshift(left, right);
1666 Py_DECREF(left);
1667 Py_DECREF(right);
1668 SET_TOP(res);
1669 if (res == NULL)
1670 goto error;
1671 DISPATCH();
1672 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001673
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001674 TARGET(INPLACE_AND) {
1675 PyObject *right = POP();
1676 PyObject *left = TOP();
1677 PyObject *res = PyNumber_InPlaceAnd(left, right);
1678 Py_DECREF(left);
1679 Py_DECREF(right);
1680 SET_TOP(res);
1681 if (res == NULL)
1682 goto error;
1683 DISPATCH();
1684 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001685
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001686 TARGET(INPLACE_XOR) {
1687 PyObject *right = POP();
1688 PyObject *left = TOP();
1689 PyObject *res = PyNumber_InPlaceXor(left, right);
1690 Py_DECREF(left);
1691 Py_DECREF(right);
1692 SET_TOP(res);
1693 if (res == NULL)
1694 goto error;
1695 DISPATCH();
1696 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001697
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001698 TARGET(INPLACE_OR) {
1699 PyObject *right = POP();
1700 PyObject *left = TOP();
1701 PyObject *res = PyNumber_InPlaceOr(left, right);
1702 Py_DECREF(left);
1703 Py_DECREF(right);
1704 SET_TOP(res);
1705 if (res == NULL)
1706 goto error;
1707 DISPATCH();
1708 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001709
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001710 TARGET(STORE_SUBSCR) {
1711 PyObject *sub = TOP();
1712 PyObject *container = SECOND();
1713 PyObject *v = THIRD();
1714 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 STACKADJ(-3);
Martin Panter95f53c12016-07-18 08:23:26 +00001716 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001717 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001719 Py_DECREF(container);
1720 Py_DECREF(sub);
1721 if (err != 0)
1722 goto error;
1723 DISPATCH();
1724 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001725
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001726 TARGET(STORE_ANNOTATION) {
1727 _Py_IDENTIFIER(__annotations__);
1728 PyObject *ann_dict;
1729 PyObject *ann = POP();
1730 PyObject *name = GETITEM(names, oparg);
1731 int err;
1732 if (f->f_locals == NULL) {
1733 PyErr_Format(PyExc_SystemError,
1734 "no locals found when storing annotation");
1735 Py_DECREF(ann);
1736 goto error;
1737 }
1738 /* first try to get __annotations__ from locals... */
1739 if (PyDict_CheckExact(f->f_locals)) {
1740 ann_dict = _PyDict_GetItemId(f->f_locals,
1741 &PyId___annotations__);
1742 if (ann_dict == NULL) {
1743 PyErr_SetString(PyExc_NameError,
1744 "__annotations__ not found");
1745 Py_DECREF(ann);
1746 goto error;
1747 }
1748 Py_INCREF(ann_dict);
1749 }
1750 else {
1751 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
1752 if (ann_str == NULL) {
1753 Py_DECREF(ann);
1754 goto error;
1755 }
1756 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
1757 if (ann_dict == NULL) {
1758 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
1759 PyErr_SetString(PyExc_NameError,
1760 "__annotations__ not found");
1761 }
1762 Py_DECREF(ann);
1763 goto error;
1764 }
1765 }
1766 /* ...if succeeded, __annotations__[name] = ann */
1767 if (PyDict_CheckExact(ann_dict)) {
1768 err = PyDict_SetItem(ann_dict, name, ann);
1769 }
1770 else {
1771 err = PyObject_SetItem(ann_dict, name, ann);
1772 }
1773 Py_DECREF(ann_dict);
Yury Selivanov50c584f2016-09-08 23:38:21 -07001774 Py_DECREF(ann);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001775 if (err != 0) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001776 goto error;
1777 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001778 DISPATCH();
1779 }
1780
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001781 TARGET(DELETE_SUBSCR) {
1782 PyObject *sub = TOP();
1783 PyObject *container = SECOND();
1784 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 STACKADJ(-2);
Martin Panter95f53c12016-07-18 08:23:26 +00001786 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001787 err = PyObject_DelItem(container, sub);
1788 Py_DECREF(container);
1789 Py_DECREF(sub);
1790 if (err != 0)
1791 goto error;
1792 DISPATCH();
1793 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001794
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001795 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001796 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001797 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001798 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001799 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001800 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 PyErr_SetString(PyExc_RuntimeError,
1802 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001803 Py_DECREF(value);
1804 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 }
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001806 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001807 Py_DECREF(value);
1808 if (res == NULL)
1809 goto error;
1810 Py_DECREF(res);
1811 DISPATCH();
1812 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001813
Thomas Wouters434d0822000-08-24 20:11:32 +00001814#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001816#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001817 TARGET(RAISE_VARARGS) {
1818 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 switch (oparg) {
1820 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001821 cause = POP(); /* cause */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001823 exc = POP(); /* exc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 case 0: /* Fallthrough */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001825 if (do_raise(exc, cause)) {
1826 why = WHY_EXCEPTION;
1827 goto fast_block_end;
1828 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829 break;
1830 default:
1831 PyErr_SetString(PyExc_SystemError,
1832 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001833 break;
1834 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001835 goto error;
1836 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001837
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001838 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001839 retval = POP();
1840 why = WHY_RETURN;
1841 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001842 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001843
Yury Selivanov75445082015-05-11 22:57:16 -04001844 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001845 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001846 PyObject *iter = NULL;
1847 PyObject *awaitable = NULL;
1848 PyObject *obj = TOP();
1849 PyTypeObject *type = Py_TYPE(obj);
1850
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001851 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001852 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001853 }
Yury Selivanov75445082015-05-11 22:57:16 -04001854
1855 if (getter != NULL) {
1856 iter = (*getter)(obj);
1857 Py_DECREF(obj);
1858 if (iter == NULL) {
1859 SET_TOP(NULL);
1860 goto error;
1861 }
1862 }
1863 else {
1864 SET_TOP(NULL);
1865 PyErr_Format(
1866 PyExc_TypeError,
1867 "'async for' requires an object with "
1868 "__aiter__ method, got %.100s",
1869 type->tp_name);
1870 Py_DECREF(obj);
1871 goto error;
1872 }
1873
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001874 if (Py_TYPE(iter)->tp_as_async != NULL &&
1875 Py_TYPE(iter)->tp_as_async->am_anext != NULL) {
1876
1877 /* Starting with CPython 3.5.2 __aiter__ should return
1878 asynchronous iterators directly (not awaitables that
1879 resolve to asynchronous iterators.)
1880
1881 Therefore, we check if the object that was returned
1882 from __aiter__ has an __anext__ method. If it does,
1883 we wrap it in an awaitable that resolves to `iter`.
1884
1885 See http://bugs.python.org/issue27243 for more
1886 details.
1887 */
1888
1889 PyObject *wrapper = _PyAIterWrapper_New(iter);
1890 Py_DECREF(iter);
1891 SET_TOP(wrapper);
1892 DISPATCH();
1893 }
1894
Yury Selivanov5376ba92015-06-22 12:19:30 -04001895 awaitable = _PyCoro_GetAwaitableIter(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001896 if (awaitable == NULL) {
1897 SET_TOP(NULL);
1898 PyErr_Format(
1899 PyExc_TypeError,
1900 "'async for' received an invalid object "
1901 "from __aiter__: %.100s",
1902 Py_TYPE(iter)->tp_name);
1903
1904 Py_DECREF(iter);
1905 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001906 } else {
Yury Selivanov75445082015-05-11 22:57:16 -04001907 Py_DECREF(iter);
1908
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001909 if (PyErr_WarnFormat(
1910 PyExc_PendingDeprecationWarning, 1,
1911 "'%.100s' implements legacy __aiter__ protocol; "
1912 "__aiter__ should return an asynchronous "
1913 "iterator, not awaitable",
1914 type->tp_name))
1915 {
1916 /* Warning was converted to an error. */
1917 Py_DECREF(awaitable);
1918 SET_TOP(NULL);
1919 goto error;
1920 }
1921 }
1922
Yury Selivanov75445082015-05-11 22:57:16 -04001923 SET_TOP(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001924 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001925 DISPATCH();
1926 }
1927
1928 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001929 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001930 PyObject *next_iter = NULL;
1931 PyObject *awaitable = NULL;
1932 PyObject *aiter = TOP();
1933 PyTypeObject *type = Py_TYPE(aiter);
1934
Yury Selivanoveb636452016-09-08 22:01:51 -07001935 if (PyAsyncGen_CheckExact(aiter)) {
1936 awaitable = type->tp_as_async->am_anext(aiter);
1937 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001938 goto error;
1939 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001940 } else {
1941 if (type->tp_as_async != NULL){
1942 getter = type->tp_as_async->am_anext;
1943 }
Yury Selivanov75445082015-05-11 22:57:16 -04001944
Yury Selivanoveb636452016-09-08 22:01:51 -07001945 if (getter != NULL) {
1946 next_iter = (*getter)(aiter);
1947 if (next_iter == NULL) {
1948 goto error;
1949 }
1950 }
1951 else {
1952 PyErr_Format(
1953 PyExc_TypeError,
1954 "'async for' requires an iterator with "
1955 "__anext__ method, got %.100s",
1956 type->tp_name);
1957 goto error;
1958 }
Yury Selivanov75445082015-05-11 22:57:16 -04001959
Yury Selivanoveb636452016-09-08 22:01:51 -07001960 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1961 if (awaitable == NULL) {
1962 PyErr_Format(
1963 PyExc_TypeError,
1964 "'async for' received an invalid object "
1965 "from __anext__: %.100s",
1966 Py_TYPE(next_iter)->tp_name);
1967
1968 Py_DECREF(next_iter);
1969 goto error;
1970 } else {
1971 Py_DECREF(next_iter);
1972 }
1973 }
Yury Selivanov75445082015-05-11 22:57:16 -04001974
1975 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001976 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001977 DISPATCH();
1978 }
1979
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001980 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001981 TARGET(GET_AWAITABLE) {
1982 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001983 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001984
1985 Py_DECREF(iterable);
1986
Yury Selivanovc724bae2016-03-02 11:30:46 -05001987 if (iter != NULL && PyCoro_CheckExact(iter)) {
1988 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1989 if (yf != NULL) {
1990 /* `iter` is a coroutine object that is being
1991 awaited, `yf` is a pointer to the current awaitable
1992 being awaited on. */
1993 Py_DECREF(yf);
1994 Py_CLEAR(iter);
1995 PyErr_SetString(
1996 PyExc_RuntimeError,
1997 "coroutine is being awaited already");
1998 /* The code below jumps to `error` if `iter` is NULL. */
1999 }
2000 }
2001
Yury Selivanov75445082015-05-11 22:57:16 -04002002 SET_TOP(iter); /* Even if it's NULL */
2003
2004 if (iter == NULL) {
2005 goto error;
2006 }
2007
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002008 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002009 DISPATCH();
2010 }
2011
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002012 TARGET(YIELD_FROM) {
2013 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002014 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002015 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002016 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2017 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002018 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002019 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002020 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002021 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002022 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002023 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002024 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002025 Py_DECREF(v);
2026 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002027 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002028 if (tstate->c_tracefunc != NULL
2029 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002030 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002031 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002032 if (err < 0)
2033 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002034 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002035 SET_TOP(val);
2036 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002037 }
Martin Panter95f53c12016-07-18 08:23:26 +00002038 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002039 f->f_stacktop = stack_pointer;
2040 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002041 /* and repeat... */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002042 f->f_lasti -= 2;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002043 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002044 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002045
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002046 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002047 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002048
2049 if (co->co_flags & CO_ASYNC_GENERATOR) {
2050 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2051 Py_DECREF(retval);
2052 if (w == NULL) {
2053 retval = NULL;
2054 goto error;
2055 }
2056 retval = w;
2057 }
2058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 f->f_stacktop = stack_pointer;
2060 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002062 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002063
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002064 TARGET(POP_EXCEPT) {
2065 PyTryBlock *b = PyFrame_BlockPop(f);
2066 if (b->b_type != EXCEPT_HANDLER) {
2067 PyErr_SetString(PyExc_SystemError,
2068 "popped block is not an except handler");
2069 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002070 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002071 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002073 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002074
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002075 PREDICTED(POP_BLOCK);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002076 TARGET(POP_BLOCK) {
2077 PyTryBlock *b = PyFrame_BlockPop(f);
2078 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002079 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002080 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002083 TARGET(END_FINALLY) {
2084 PyObject *status = POP();
2085 if (PyLong_Check(status)) {
2086 why = (enum why_code) PyLong_AS_LONG(status);
2087 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088 if (why == WHY_RETURN ||
2089 why == WHY_CONTINUE)
2090 retval = POP();
2091 if (why == WHY_SILENCED) {
2092 /* An exception was silenced by 'with', we must
2093 manually unwind the EXCEPT_HANDLER block which was
2094 created when the exception was caught, otherwise
2095 the stack will be in an inconsistent state. */
2096 PyTryBlock *b = PyFrame_BlockPop(f);
2097 assert(b->b_type == EXCEPT_HANDLER);
2098 UNWIND_EXCEPT_HANDLER(b);
2099 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002100 Py_DECREF(status);
2101 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002102 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002103 Py_DECREF(status);
2104 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002106 else if (PyExceptionClass_Check(status)) {
2107 PyObject *exc = POP();
2108 PyObject *tb = POP();
2109 PyErr_Restore(status, exc, tb);
2110 why = WHY_EXCEPTION;
2111 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002113 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114 PyErr_SetString(PyExc_SystemError,
2115 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002116 Py_DECREF(status);
2117 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002118 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002119 Py_DECREF(status);
2120 DISPATCH();
2121 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002122
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002123 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002124 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002125
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002126 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002127 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002128 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
2129 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002130 PyErr_SetString(PyExc_NameError,
2131 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002132 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002133 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002134 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002135 }
2136 else {
2137 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2138 if (build_class_str == NULL)
2139 break;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002140 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2141 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002142 if (PyErr_ExceptionMatches(PyExc_KeyError))
2143 PyErr_SetString(PyExc_NameError,
2144 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002145 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002146 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002148 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002149 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002150 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002151
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002152 TARGET(STORE_NAME) {
2153 PyObject *name = GETITEM(names, oparg);
2154 PyObject *v = POP();
2155 PyObject *ns = f->f_locals;
2156 int err;
2157 if (ns == NULL) {
2158 PyErr_Format(PyExc_SystemError,
2159 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002160 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002161 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002163 if (PyDict_CheckExact(ns))
2164 err = PyDict_SetItem(ns, name, v);
2165 else
2166 err = PyObject_SetItem(ns, name, v);
2167 Py_DECREF(v);
2168 if (err != 0)
2169 goto error;
2170 DISPATCH();
2171 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002172
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002173 TARGET(DELETE_NAME) {
2174 PyObject *name = GETITEM(names, oparg);
2175 PyObject *ns = f->f_locals;
2176 int err;
2177 if (ns == NULL) {
2178 PyErr_Format(PyExc_SystemError,
2179 "no locals when deleting %R", name);
2180 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002182 err = PyObject_DelItem(ns, name);
2183 if (err != 0) {
2184 format_exc_check_arg(PyExc_NameError,
2185 NAME_ERROR_MSG,
2186 name);
2187 goto error;
2188 }
2189 DISPATCH();
2190 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002191
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002192 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002193 TARGET(UNPACK_SEQUENCE) {
2194 PyObject *seq = POP(), *item, **items;
2195 if (PyTuple_CheckExact(seq) &&
2196 PyTuple_GET_SIZE(seq) == oparg) {
2197 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002199 item = items[oparg];
2200 Py_INCREF(item);
2201 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002202 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002203 } else if (PyList_CheckExact(seq) &&
2204 PyList_GET_SIZE(seq) == oparg) {
2205 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002206 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002207 item = items[oparg];
2208 Py_INCREF(item);
2209 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002210 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002211 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002212 stack_pointer + oparg)) {
2213 STACKADJ(oparg);
2214 } else {
2215 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002216 Py_DECREF(seq);
2217 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002219 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002220 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002221 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002222
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002223 TARGET(UNPACK_EX) {
2224 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2225 PyObject *seq = POP();
2226
2227 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2228 stack_pointer + totalargs)) {
2229 stack_pointer += totalargs;
2230 } else {
2231 Py_DECREF(seq);
2232 goto error;
2233 }
2234 Py_DECREF(seq);
2235 DISPATCH();
2236 }
2237
2238 TARGET(STORE_ATTR) {
2239 PyObject *name = GETITEM(names, oparg);
2240 PyObject *owner = TOP();
2241 PyObject *v = SECOND();
2242 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002243 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002244 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002246 Py_DECREF(owner);
2247 if (err != 0)
2248 goto error;
2249 DISPATCH();
2250 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002251
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002252 TARGET(DELETE_ATTR) {
2253 PyObject *name = GETITEM(names, oparg);
2254 PyObject *owner = POP();
2255 int err;
2256 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2257 Py_DECREF(owner);
2258 if (err != 0)
2259 goto error;
2260 DISPATCH();
2261 }
2262
2263 TARGET(STORE_GLOBAL) {
2264 PyObject *name = GETITEM(names, oparg);
2265 PyObject *v = POP();
2266 int err;
2267 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002268 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002269 if (err != 0)
2270 goto error;
2271 DISPATCH();
2272 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002273
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002274 TARGET(DELETE_GLOBAL) {
2275 PyObject *name = GETITEM(names, oparg);
2276 int err;
2277 err = PyDict_DelItem(f->f_globals, name);
2278 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002279 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002280 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002281 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002282 }
2283 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002284 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002285
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002286 TARGET(LOAD_NAME) {
2287 PyObject *name = GETITEM(names, oparg);
2288 PyObject *locals = f->f_locals;
2289 PyObject *v;
2290 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002292 "no locals when loading %R", name);
2293 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002295 if (PyDict_CheckExact(locals)) {
2296 v = PyDict_GetItem(locals, name);
2297 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 }
2299 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002300 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002301 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002302 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2303 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002304 PyErr_Clear();
2305 }
2306 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002307 if (v == NULL) {
2308 v = PyDict_GetItem(f->f_globals, name);
2309 Py_XINCREF(v);
2310 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002311 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002312 v = PyDict_GetItem(f->f_builtins, name);
2313 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002314 format_exc_check_arg(
2315 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002316 NAME_ERROR_MSG, name);
2317 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002318 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002319 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002320 }
2321 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002322 v = PyObject_GetItem(f->f_builtins, name);
2323 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002324 if (PyErr_ExceptionMatches(PyExc_KeyError))
2325 format_exc_check_arg(
2326 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002327 NAME_ERROR_MSG, name);
2328 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002329 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002330 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002331 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002333 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002335 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002336
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002337 TARGET(LOAD_GLOBAL) {
2338 PyObject *name = GETITEM(names, oparg);
2339 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002340 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002341 && PyDict_CheckExact(f->f_builtins))
2342 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002343 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002344 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002345 name);
2346 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002347 if (!_PyErr_OCCURRED()) {
2348 /* _PyDict_LoadGlobal() returns NULL without raising
2349 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002350 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002351 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002352 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002353 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002355 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002357 else {
2358 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002359
2360 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002361 v = PyObject_GetItem(f->f_globals, name);
2362 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002363 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2364 goto error;
2365 PyErr_Clear();
2366
Victor Stinnerb4efc962015-11-20 09:24:02 +01002367 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002368 v = PyObject_GetItem(f->f_builtins, name);
2369 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002370 if (PyErr_ExceptionMatches(PyExc_KeyError))
2371 format_exc_check_arg(
2372 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002373 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002374 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002375 }
2376 }
2377 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002378 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002380 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002381
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002382 TARGET(DELETE_FAST) {
2383 PyObject *v = GETLOCAL(oparg);
2384 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385 SETLOCAL(oparg, NULL);
2386 DISPATCH();
2387 }
2388 format_exc_check_arg(
2389 PyExc_UnboundLocalError,
2390 UNBOUNDLOCAL_ERROR_MSG,
2391 PyTuple_GetItem(co->co_varnames, oparg)
2392 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002393 goto error;
2394 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002395
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002396 TARGET(DELETE_DEREF) {
2397 PyObject *cell = freevars[oparg];
2398 if (PyCell_GET(cell) != NULL) {
2399 PyCell_Set(cell, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002400 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002401 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002402 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002403 goto error;
2404 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002405
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002406 TARGET(LOAD_CLOSURE) {
2407 PyObject *cell = freevars[oparg];
2408 Py_INCREF(cell);
2409 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002410 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002411 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002412
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002413 TARGET(LOAD_CLASSDEREF) {
2414 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002415 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002416 assert(locals);
2417 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2418 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2419 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2420 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2421 if (PyDict_CheckExact(locals)) {
2422 value = PyDict_GetItem(locals, name);
2423 Py_XINCREF(value);
2424 }
2425 else {
2426 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002427 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002428 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2429 goto error;
2430 PyErr_Clear();
2431 }
2432 }
2433 if (!value) {
2434 PyObject *cell = freevars[oparg];
2435 value = PyCell_GET(cell);
2436 if (value == NULL) {
2437 format_exc_unbound(co, oparg);
2438 goto error;
2439 }
2440 Py_INCREF(value);
2441 }
2442 PUSH(value);
2443 DISPATCH();
2444 }
2445
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002446 TARGET(LOAD_DEREF) {
2447 PyObject *cell = freevars[oparg];
2448 PyObject *value = PyCell_GET(cell);
2449 if (value == NULL) {
2450 format_exc_unbound(co, oparg);
2451 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002452 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002453 Py_INCREF(value);
2454 PUSH(value);
2455 DISPATCH();
2456 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002457
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002458 TARGET(STORE_DEREF) {
2459 PyObject *v = POP();
2460 PyObject *cell = freevars[oparg];
2461 PyCell_Set(cell, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002462 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002463 DISPATCH();
2464 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002465
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002466 TARGET(BUILD_STRING) {
2467 PyObject *str;
2468 PyObject *empty = PyUnicode_New(0, 0);
2469 if (empty == NULL) {
2470 goto error;
2471 }
2472 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2473 Py_DECREF(empty);
2474 if (str == NULL)
2475 goto error;
2476 while (--oparg >= 0) {
2477 PyObject *item = POP();
2478 Py_DECREF(item);
2479 }
2480 PUSH(str);
2481 DISPATCH();
2482 }
2483
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002484 TARGET(BUILD_TUPLE) {
2485 PyObject *tup = PyTuple_New(oparg);
2486 if (tup == NULL)
2487 goto error;
2488 while (--oparg >= 0) {
2489 PyObject *item = POP();
2490 PyTuple_SET_ITEM(tup, oparg, item);
2491 }
2492 PUSH(tup);
2493 DISPATCH();
2494 }
2495
2496 TARGET(BUILD_LIST) {
2497 PyObject *list = PyList_New(oparg);
2498 if (list == NULL)
2499 goto error;
2500 while (--oparg >= 0) {
2501 PyObject *item = POP();
2502 PyList_SET_ITEM(list, oparg, item);
2503 }
2504 PUSH(list);
2505 DISPATCH();
2506 }
2507
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002508 TARGET(BUILD_TUPLE_UNPACK)
2509 TARGET(BUILD_LIST_UNPACK) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002510 int convert_to_tuple = opcode == BUILD_TUPLE_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002511 Py_ssize_t i;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002512 PyObject *sum;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002513 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002514
2515 if (convert_to_tuple && oparg == 1 && PyTuple_CheckExact(TOP())) {
2516 DISPATCH();
2517 }
2518
2519 sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002520 if (sum == NULL)
2521 goto error;
2522
2523 for (i = oparg; i > 0; i--) {
2524 PyObject *none_val;
2525
2526 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2527 if (none_val == NULL) {
2528 Py_DECREF(sum);
2529 goto error;
2530 }
2531 Py_DECREF(none_val);
2532 }
2533
2534 if (convert_to_tuple) {
2535 return_value = PyList_AsTuple(sum);
2536 Py_DECREF(sum);
2537 if (return_value == NULL)
2538 goto error;
2539 }
2540 else {
2541 return_value = sum;
2542 }
2543
2544 while (oparg--)
2545 Py_DECREF(POP());
2546 PUSH(return_value);
2547 DISPATCH();
2548 }
2549
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002550 TARGET(BUILD_SET) {
2551 PyObject *set = PySet_New(NULL);
2552 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002553 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002554 if (set == NULL)
2555 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002556 for (i = oparg; i > 0; i--) {
2557 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002558 if (err == 0)
2559 err = PySet_Add(set, item);
2560 Py_DECREF(item);
2561 }
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002562 STACKADJ(-oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002563 if (err != 0) {
2564 Py_DECREF(set);
2565 goto error;
2566 }
2567 PUSH(set);
2568 DISPATCH();
2569 }
2570
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002571 TARGET(BUILD_SET_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002572 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002573 PyObject *sum = PySet_New(NULL);
2574 if (sum == NULL)
2575 goto error;
2576
2577 for (i = oparg; i > 0; i--) {
2578 if (_PySet_Update(sum, PEEK(i)) < 0) {
2579 Py_DECREF(sum);
2580 goto error;
2581 }
2582 }
2583
2584 while (oparg--)
2585 Py_DECREF(POP());
2586 PUSH(sum);
2587 DISPATCH();
2588 }
2589
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002590 TARGET(BUILD_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002591 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002592 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2593 if (map == NULL)
2594 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002595 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002596 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002597 PyObject *key = PEEK(2*i);
2598 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002599 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002600 if (err != 0) {
2601 Py_DECREF(map);
2602 goto error;
2603 }
2604 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002605
2606 while (oparg--) {
2607 Py_DECREF(POP());
2608 Py_DECREF(POP());
2609 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002610 PUSH(map);
2611 DISPATCH();
2612 }
2613
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002614 TARGET(SETUP_ANNOTATIONS) {
2615 _Py_IDENTIFIER(__annotations__);
2616 int err;
2617 PyObject *ann_dict;
2618 if (f->f_locals == NULL) {
2619 PyErr_Format(PyExc_SystemError,
2620 "no locals found when setting up annotations");
2621 goto error;
2622 }
2623 /* check if __annotations__ in locals()... */
2624 if (PyDict_CheckExact(f->f_locals)) {
2625 ann_dict = _PyDict_GetItemId(f->f_locals,
2626 &PyId___annotations__);
2627 if (ann_dict == NULL) {
2628 /* ...if not, create a new one */
2629 ann_dict = PyDict_New();
2630 if (ann_dict == NULL) {
2631 goto error;
2632 }
2633 err = _PyDict_SetItemId(f->f_locals,
2634 &PyId___annotations__, ann_dict);
2635 Py_DECREF(ann_dict);
2636 if (err != 0) {
2637 goto error;
2638 }
2639 }
2640 }
2641 else {
2642 /* do the same if locals() is not a dict */
2643 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2644 if (ann_str == NULL) {
2645 break;
2646 }
2647 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2648 if (ann_dict == NULL) {
2649 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2650 goto error;
2651 }
2652 PyErr_Clear();
2653 ann_dict = PyDict_New();
2654 if (ann_dict == NULL) {
2655 goto error;
2656 }
2657 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2658 Py_DECREF(ann_dict);
2659 if (err != 0) {
2660 goto error;
2661 }
2662 }
2663 else {
2664 Py_DECREF(ann_dict);
2665 }
2666 }
2667 DISPATCH();
2668 }
2669
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002670 TARGET(BUILD_CONST_KEY_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002671 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002672 PyObject *map;
2673 PyObject *keys = TOP();
2674 if (!PyTuple_CheckExact(keys) ||
2675 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2676 PyErr_SetString(PyExc_SystemError,
2677 "bad BUILD_CONST_KEY_MAP keys argument");
2678 goto error;
2679 }
2680 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2681 if (map == NULL) {
2682 goto error;
2683 }
2684 for (i = oparg; i > 0; i--) {
2685 int err;
2686 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2687 PyObject *value = PEEK(i + 1);
2688 err = PyDict_SetItem(map, key, value);
2689 if (err != 0) {
2690 Py_DECREF(map);
2691 goto error;
2692 }
2693 }
2694
2695 Py_DECREF(POP());
2696 while (oparg--) {
2697 Py_DECREF(POP());
2698 }
2699 PUSH(map);
2700 DISPATCH();
2701 }
2702
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002703 TARGET(BUILD_MAP_UNPACK_WITH_CALL)
2704 TARGET(BUILD_MAP_UNPACK) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002705 int with_call = opcode == BUILD_MAP_UNPACK_WITH_CALL;
Victor Stinner74319ae2016-08-25 00:04:09 +02002706 Py_ssize_t i;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002707 PyObject *sum;
2708
2709 if (with_call && oparg == 1 && PyDict_CheckExact(TOP())) {
2710 DISPATCH();
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002711 }
2712
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002713 sum = PyDict_New();
2714 if (sum == NULL)
2715 goto error;
2716
2717 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002718 PyObject *arg = PEEK(i);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002719 if (with_call && PyDict_Size(sum)) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002720 PyObject *intersection = _PyDictView_Intersect(sum, arg);
2721
2722 if (intersection == NULL) {
2723 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002724 PyObject *func = PEEK(2 + oparg);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002725 PyErr_Format(PyExc_TypeError,
2726 "%.200s%.200s argument after ** "
2727 "must be a mapping, not %.200s",
2728 PyEval_GetFuncName(func),
2729 PyEval_GetFuncDesc(func),
2730 arg->ob_type->tp_name);
2731 }
2732 Py_DECREF(sum);
2733 goto error;
2734 }
2735
2736 if (PySet_GET_SIZE(intersection)) {
2737 Py_ssize_t idx = 0;
2738 PyObject *key;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002739 PyObject *func = PEEK(2 + oparg);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002740 Py_hash_t hash;
2741 _PySet_NextEntry(intersection, &idx, &key, &hash);
2742 if (!PyUnicode_Check(key)) {
2743 PyErr_Format(PyExc_TypeError,
2744 "%.200s%.200s keywords must be strings",
2745 PyEval_GetFuncName(func),
2746 PyEval_GetFuncDesc(func));
2747 } else {
2748 PyErr_Format(PyExc_TypeError,
2749 "%.200s%.200s got multiple "
2750 "values for keyword argument '%U'",
2751 PyEval_GetFuncName(func),
2752 PyEval_GetFuncDesc(func),
2753 key);
2754 }
2755 Py_DECREF(intersection);
2756 Py_DECREF(sum);
2757 goto error;
2758 }
2759 Py_DECREF(intersection);
2760 }
2761
2762 if (PyDict_Update(sum, arg) < 0) {
2763 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2764 PyErr_Format(PyExc_TypeError,
2765 "'%.200s' object is not a mapping",
2766 arg->ob_type->tp_name);
2767 }
2768 Py_DECREF(sum);
2769 goto error;
2770 }
2771 }
2772
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002773 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002774 Py_DECREF(POP());
2775 PUSH(sum);
2776 DISPATCH();
2777 }
2778
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002779 TARGET(MAP_ADD) {
2780 PyObject *key = TOP();
2781 PyObject *value = SECOND();
2782 PyObject *map;
2783 int err;
2784 STACKADJ(-2);
2785 map = stack_pointer[-oparg]; /* dict */
2786 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002787 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002788 Py_DECREF(value);
2789 Py_DECREF(key);
2790 if (err != 0)
2791 goto error;
2792 PREDICT(JUMP_ABSOLUTE);
2793 DISPATCH();
2794 }
2795
2796 TARGET(LOAD_ATTR) {
2797 PyObject *name = GETITEM(names, oparg);
2798 PyObject *owner = TOP();
2799 PyObject *res = PyObject_GetAttr(owner, name);
2800 Py_DECREF(owner);
2801 SET_TOP(res);
2802 if (res == NULL)
2803 goto error;
2804 DISPATCH();
2805 }
2806
2807 TARGET(COMPARE_OP) {
2808 PyObject *right = POP();
2809 PyObject *left = TOP();
2810 PyObject *res = cmp_outcome(oparg, left, right);
2811 Py_DECREF(left);
2812 Py_DECREF(right);
2813 SET_TOP(res);
2814 if (res == NULL)
2815 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002816 PREDICT(POP_JUMP_IF_FALSE);
2817 PREDICT(POP_JUMP_IF_TRUE);
2818 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002819 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002820
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002821 TARGET(IMPORT_NAME) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002822 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002823 PyObject *fromlist = POP();
2824 PyObject *level = TOP();
2825 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002826 res = import_name(f, name, fromlist, level);
2827 Py_DECREF(level);
2828 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002829 SET_TOP(res);
2830 if (res == NULL)
2831 goto error;
2832 DISPATCH();
2833 }
2834
2835 TARGET(IMPORT_STAR) {
2836 PyObject *from = POP(), *locals;
2837 int err;
Victor Stinner41bb43a2013-10-29 01:19:37 +01002838 if (PyFrame_FastToLocalsWithError(f) < 0)
2839 goto error;
2840
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002841 locals = f->f_locals;
2842 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002843 PyErr_SetString(PyExc_SystemError,
2844 "no locals found during 'import *'");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002845 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002847 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002848 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002849 Py_DECREF(from);
2850 if (err != 0)
2851 goto error;
2852 DISPATCH();
2853 }
Guido van Rossum25831651993-05-19 14:50:45 +00002854
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002855 TARGET(IMPORT_FROM) {
2856 PyObject *name = GETITEM(names, oparg);
2857 PyObject *from = TOP();
2858 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002859 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002860 PUSH(res);
2861 if (res == NULL)
2862 goto error;
2863 DISPATCH();
2864 }
Thomas Wouters52152252000-08-17 22:55:00 +00002865
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002866 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002867 JUMPBY(oparg);
2868 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002869 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002870
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002871 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002872 TARGET(POP_JUMP_IF_FALSE) {
2873 PyObject *cond = POP();
2874 int err;
2875 if (cond == Py_True) {
2876 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002877 FAST_DISPATCH();
2878 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002879 if (cond == Py_False) {
2880 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 JUMPTO(oparg);
2882 FAST_DISPATCH();
2883 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002884 err = PyObject_IsTrue(cond);
2885 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002886 if (err > 0)
2887 err = 0;
2888 else if (err == 0)
2889 JUMPTO(oparg);
2890 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002891 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002892 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002893 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002894
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002895 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002896 TARGET(POP_JUMP_IF_TRUE) {
2897 PyObject *cond = POP();
2898 int err;
2899 if (cond == Py_False) {
2900 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 FAST_DISPATCH();
2902 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002903 if (cond == Py_True) {
2904 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 JUMPTO(oparg);
2906 FAST_DISPATCH();
2907 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002908 err = PyObject_IsTrue(cond);
2909 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002910 if (err > 0) {
2911 err = 0;
2912 JUMPTO(oparg);
2913 }
2914 else if (err == 0)
2915 ;
2916 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002917 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002918 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002919 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002920
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002921 TARGET(JUMP_IF_FALSE_OR_POP) {
2922 PyObject *cond = TOP();
2923 int err;
2924 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002926 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002927 FAST_DISPATCH();
2928 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002929 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002930 JUMPTO(oparg);
2931 FAST_DISPATCH();
2932 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002933 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002934 if (err > 0) {
2935 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002936 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002937 err = 0;
2938 }
2939 else if (err == 0)
2940 JUMPTO(oparg);
2941 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002942 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002943 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002944 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002945
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002946 TARGET(JUMP_IF_TRUE_OR_POP) {
2947 PyObject *cond = TOP();
2948 int err;
2949 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002950 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002951 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002952 FAST_DISPATCH();
2953 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002954 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002955 JUMPTO(oparg);
2956 FAST_DISPATCH();
2957 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002958 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002959 if (err > 0) {
2960 err = 0;
2961 JUMPTO(oparg);
2962 }
2963 else if (err == 0) {
2964 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002965 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002966 }
2967 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002968 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002969 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002970 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002971
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002972 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002973 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002974 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002975#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002976 /* Enabling this path speeds-up all while and for-loops by bypassing
2977 the per-loop checks for signals. By default, this should be turned-off
2978 because it prevents detection of a control-break in tight loops like
2979 "while 1: pass". Compile with this option turned-on when you need
2980 the speed-up and do not need break checking inside tight loops (ones
2981 that contain only instructions ending with FAST_DISPATCH).
2982 */
2983 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002984#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002985 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002986#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002987 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002988
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002989 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002990 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002991 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002992 PyObject *iter = PyObject_GetIter(iterable);
2993 Py_DECREF(iterable);
2994 SET_TOP(iter);
2995 if (iter == NULL)
2996 goto error;
2997 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002998 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002999 DISPATCH();
3000 }
3001
3002 TARGET(GET_YIELD_FROM_ITER) {
3003 /* before: [obj]; after [getiter(obj)] */
3004 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003005 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003006 if (PyCoro_CheckExact(iterable)) {
3007 /* `iterable` is a coroutine */
3008 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3009 /* and it is used in a 'yield from' expression of a
3010 regular generator. */
3011 Py_DECREF(iterable);
3012 SET_TOP(NULL);
3013 PyErr_SetString(PyExc_TypeError,
3014 "cannot 'yield from' a coroutine object "
3015 "in a non-coroutine generator");
3016 goto error;
3017 }
3018 }
3019 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003020 /* `iterable` is not a generator. */
3021 iter = PyObject_GetIter(iterable);
3022 Py_DECREF(iterable);
3023 SET_TOP(iter);
3024 if (iter == NULL)
3025 goto error;
3026 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003027 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003028 DISPATCH();
3029 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003030
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003031 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003032 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003033 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003034 PyObject *iter = TOP();
3035 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3036 if (next != NULL) {
3037 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003038 PREDICT(STORE_FAST);
3039 PREDICT(UNPACK_SEQUENCE);
3040 DISPATCH();
3041 }
3042 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003043 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
3044 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08003045 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003046 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003047 PyErr_Clear();
3048 }
3049 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003050 STACKADJ(-1);
3051 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003052 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003053 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003054 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003055 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003056
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003057 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003058 why = WHY_BREAK;
3059 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003060 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003061
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003062 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003063 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003064 if (retval == NULL)
3065 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003066 why = WHY_CONTINUE;
3067 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003068 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003069
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003070 TARGET(SETUP_LOOP)
3071 TARGET(SETUP_EXCEPT)
3072 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003073 /* NOTE: If you add any new block-setup opcodes that
3074 are not try/except/finally handlers, you may need
3075 to update the PyGen_NeedsFinalizing() function.
3076 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003078 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
3079 STACK_LEVEL());
3080 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003081 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003082
Yury Selivanov75445082015-05-11 22:57:16 -04003083 TARGET(BEFORE_ASYNC_WITH) {
3084 _Py_IDENTIFIER(__aexit__);
3085 _Py_IDENTIFIER(__aenter__);
3086
3087 PyObject *mgr = TOP();
3088 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3089 *enter;
3090 PyObject *res;
3091 if (exit == NULL)
3092 goto error;
3093 SET_TOP(exit);
3094 enter = special_lookup(mgr, &PyId___aenter__);
3095 Py_DECREF(mgr);
3096 if (enter == NULL)
3097 goto error;
3098 res = PyObject_CallFunctionObjArgs(enter, NULL);
3099 Py_DECREF(enter);
3100 if (res == NULL)
3101 goto error;
3102 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003103 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003104 DISPATCH();
3105 }
3106
3107 TARGET(SETUP_ASYNC_WITH) {
3108 PyObject *res = POP();
3109 /* Setup the finally block before pushing the result
3110 of __aenter__ on the stack. */
3111 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3112 STACK_LEVEL());
3113 PUSH(res);
3114 DISPATCH();
3115 }
3116
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003117 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003118 _Py_IDENTIFIER(__exit__);
3119 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003120 PyObject *mgr = TOP();
3121 PyObject *exit = special_lookup(mgr, &PyId___exit__), *enter;
3122 PyObject *res;
3123 if (exit == NULL)
3124 goto error;
3125 SET_TOP(exit);
3126 enter = special_lookup(mgr, &PyId___enter__);
3127 Py_DECREF(mgr);
3128 if (enter == NULL)
3129 goto error;
3130 res = PyObject_CallFunctionObjArgs(enter, NULL);
3131 Py_DECREF(enter);
3132 if (res == NULL)
3133 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003134 /* Setup the finally block before pushing the result
3135 of __enter__ on the stack. */
3136 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3137 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003138
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003139 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003140 DISPATCH();
3141 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003142
Yury Selivanov75445082015-05-11 22:57:16 -04003143 TARGET(WITH_CLEANUP_START) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04003144 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003145 how/why we entered the finally clause:
3146 - TOP = None
3147 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
3148 - TOP = WHY_*; no retval below it
3149 - (TOP, SECOND, THIRD) = exc_info()
3150 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
3151 Below them is EXIT, the context.__exit__ bound method.
3152 In the last case, we must call
3153 EXIT(TOP, SECOND, THIRD)
3154 otherwise we must call
3155 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003156
Benjamin Peterson8f169482013-10-29 22:25:06 -04003157 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003158 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04003159 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003160 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003162 In addition, if the stack represents an exception,
3163 *and* the function call returns a 'true' value, we
3164 push WHY_SILENCED onto the stack. END_FINALLY will
3165 then not re-raise the exception. (But non-local
3166 gotos should still be resumed.)
3167 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003169 PyObject *exit_func;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003170 PyObject *exc = TOP(), *val = Py_None, *tb = Py_None, *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003171 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003172 (void)POP();
3173 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003174 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003175 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003176 else if (PyLong_Check(exc)) {
3177 STACKADJ(-1);
3178 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003179 case WHY_RETURN:
3180 case WHY_CONTINUE:
3181 /* Retval in TOP. */
3182 exit_func = SECOND();
3183 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003184 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003185 break;
3186 default:
3187 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003188 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003189 break;
3190 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003191 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003192 }
3193 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003194 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003195 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003196 val = SECOND();
3197 tb = THIRD();
3198 tp2 = FOURTH();
3199 exc2 = PEEK(5);
3200 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003201 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003202 SET_VALUE(7, tb2);
3203 SET_VALUE(6, exc2);
3204 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003205 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3206 SET_FOURTH(NULL);
3207 /* We just shifted the stack down, so we have
3208 to tell the except handler block that the
3209 values are lower than it expects. */
3210 block = &f->f_blockstack[f->f_iblock - 1];
3211 assert(block->b_type == EXCEPT_HANDLER);
3212 block->b_level--;
3213 }
3214 /* XXX Not the fastest way to call it... */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003215 res = PyObject_CallFunctionObjArgs(exit_func, exc, val, tb, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003216 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003217 if (res == NULL)
3218 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003219
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003220 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003221 PUSH(exc);
3222 PUSH(res);
3223 PREDICT(WITH_CLEANUP_FINISH);
3224 DISPATCH();
3225 }
3226
3227 PREDICTED(WITH_CLEANUP_FINISH);
3228 TARGET(WITH_CLEANUP_FINISH) {
3229 PyObject *res = POP();
3230 PyObject *exc = POP();
3231 int err;
3232
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003233 if (exc != Py_None)
3234 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003235 else
3236 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003237
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003238 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003239 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003241 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003242 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003243 else if (err > 0) {
3244 err = 0;
3245 /* There was an exception and a True return */
3246 PUSH(PyLong_FromLong((long) WHY_SILENCED));
3247 }
3248 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003249 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003250 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003251
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003252 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003253 TARGET(CALL_FUNCTION) {
3254 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003255 PCALL(PCALL_ALL);
3256 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003257 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003258 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003259 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003260 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003261 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003262 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003263 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003264 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003265
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003266 TARGET(CALL_FUNCTION_KW) {
3267 PyObject **sp, *res, *names;
3268
3269 names = POP();
3270 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003271 PCALL(PCALL_ALL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003272 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003273 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003274 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003275 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003276 Py_DECREF(names);
3277
3278 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003279 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003280 }
3281 DISPATCH();
3282 }
3283
3284 TARGET(CALL_FUNCTION_EX) {
3285 PyObject *func, *callargs, *kwargs = NULL, *result;
3286 PCALL(PCALL_ALL);
3287 if (oparg & 0x01) {
3288 kwargs = POP();
3289 assert(PyDict_CheckExact(kwargs));
3290 }
3291 callargs = POP();
3292 assert(PyTuple_CheckExact(callargs));
3293 func = TOP();
3294
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003295 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003296 Py_DECREF(func);
3297 Py_DECREF(callargs);
3298 Py_XDECREF(kwargs);
3299
3300 SET_TOP(result);
3301 if (result == NULL) {
3302 goto error;
3303 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003304 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003305 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003306
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003307 TARGET(MAKE_FUNCTION) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003308 PyObject *qualname = POP();
3309 PyObject *codeobj = POP();
3310 PyFunctionObject *func = (PyFunctionObject *)
3311 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003312
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003313 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003314 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003315 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003316 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003317 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003318
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003319 if (oparg & 0x08) {
3320 assert(PyTuple_CheckExact(TOP()));
3321 func ->func_closure = POP();
3322 }
3323 if (oparg & 0x04) {
3324 assert(PyDict_CheckExact(TOP()));
3325 func->func_annotations = POP();
3326 }
3327 if (oparg & 0x02) {
3328 assert(PyDict_CheckExact(TOP()));
3329 func->func_kwdefaults = POP();
3330 }
3331 if (oparg & 0x01) {
3332 assert(PyTuple_CheckExact(TOP()));
3333 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003334 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003335
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003336 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003337 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003338 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003339
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003340 TARGET(BUILD_SLICE) {
3341 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003342 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003343 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003344 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003345 step = NULL;
3346 stop = POP();
3347 start = TOP();
3348 slice = PySlice_New(start, stop, step);
3349 Py_DECREF(start);
3350 Py_DECREF(stop);
3351 Py_XDECREF(step);
3352 SET_TOP(slice);
3353 if (slice == NULL)
3354 goto error;
3355 DISPATCH();
3356 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003357
Eric V. Smitha78c7952015-11-03 12:45:05 -05003358 TARGET(FORMAT_VALUE) {
3359 /* Handles f-string value formatting. */
3360 PyObject *result;
3361 PyObject *fmt_spec;
3362 PyObject *value;
3363 PyObject *(*conv_fn)(PyObject *);
3364 int which_conversion = oparg & FVC_MASK;
3365 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3366
3367 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003368 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003369
3370 /* See if any conversion is specified. */
3371 switch (which_conversion) {
3372 case FVC_STR: conv_fn = PyObject_Str; break;
3373 case FVC_REPR: conv_fn = PyObject_Repr; break;
3374 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3375
3376 /* Must be 0 (meaning no conversion), since only four
3377 values are allowed by (oparg & FVC_MASK). */
3378 default: conv_fn = NULL; break;
3379 }
3380
3381 /* If there's a conversion function, call it and replace
3382 value with that result. Otherwise, just use value,
3383 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003384 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003385 result = conv_fn(value);
3386 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003387 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003388 Py_XDECREF(fmt_spec);
3389 goto error;
3390 }
3391 value = result;
3392 }
3393
3394 /* If value is a unicode object, and there's no fmt_spec,
3395 then we know the result of format(value) is value
3396 itself. In that case, skip calling format(). I plan to
3397 move this optimization in to PyObject_Format()
3398 itself. */
3399 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3400 /* Do nothing, just transfer ownership to result. */
3401 result = value;
3402 } else {
3403 /* Actually call format(). */
3404 result = PyObject_Format(value, fmt_spec);
3405 Py_DECREF(value);
3406 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003407 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003408 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003409 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003410 }
3411
Eric V. Smith135d5f42016-02-05 18:23:08 -05003412 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003413 DISPATCH();
3414 }
3415
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003416 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003417 int oldoparg = oparg;
3418 NEXTOPARG();
3419 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003420 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003421 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003422
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003423
Antoine Pitrou042b1282010-08-13 21:15:58 +00003424#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003425 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003426#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003427 default:
3428 fprintf(stderr,
3429 "XXX lineno: %d, opcode: %d\n",
3430 PyFrame_GetLineNumber(f),
3431 opcode);
3432 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003433 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003434
3435#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003436 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003437#endif
3438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003439 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003440
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003441 /* This should never be reached. Every opcode should end with DISPATCH()
3442 or goto error. */
3443 assert(0);
Guido van Rossumac7be682001-01-17 15:42:30 +00003444
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003445error:
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003446
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003447 assert(why == WHY_NOT);
3448 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003449
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003450 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003451#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003452 if (!PyErr_Occurred())
3453 PyErr_SetString(PyExc_SystemError,
3454 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003455#else
3456 assert(PyErr_Occurred());
3457#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003458
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003459 /* Log traceback info. */
3460 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003461
Benjamin Peterson51f46162013-01-23 08:38:47 -05003462 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003463 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3464 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003465
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003466fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003467 assert(why != WHY_NOT);
3468
3469 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003470 while (why != WHY_NOT && f->f_iblock > 0) {
3471 /* Peek at the current block. */
3472 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003474 assert(why != WHY_YIELD);
3475 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3476 why = WHY_NOT;
3477 JUMPTO(PyLong_AS_LONG(retval));
3478 Py_DECREF(retval);
3479 break;
3480 }
3481 /* Now we have to pop the block. */
3482 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003484 if (b->b_type == EXCEPT_HANDLER) {
3485 UNWIND_EXCEPT_HANDLER(b);
3486 continue;
3487 }
3488 UNWIND_BLOCK(b);
3489 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3490 why = WHY_NOT;
3491 JUMPTO(b->b_handler);
3492 break;
3493 }
3494 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3495 || b->b_type == SETUP_FINALLY)) {
3496 PyObject *exc, *val, *tb;
3497 int handler = b->b_handler;
3498 /* Beware, this invalidates all b->b_* fields */
3499 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3500 PUSH(tstate->exc_traceback);
3501 PUSH(tstate->exc_value);
3502 if (tstate->exc_type != NULL) {
3503 PUSH(tstate->exc_type);
3504 }
3505 else {
3506 Py_INCREF(Py_None);
3507 PUSH(Py_None);
3508 }
3509 PyErr_Fetch(&exc, &val, &tb);
3510 /* Make the raw exception data
3511 available to the handler,
3512 so a program can emulate the
3513 Python main loop. */
3514 PyErr_NormalizeException(
3515 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003516 if (tb != NULL)
3517 PyException_SetTraceback(val, tb);
3518 else
3519 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003520 Py_INCREF(exc);
3521 tstate->exc_type = exc;
3522 Py_INCREF(val);
3523 tstate->exc_value = val;
3524 tstate->exc_traceback = tb;
3525 if (tb == NULL)
3526 tb = Py_None;
3527 Py_INCREF(tb);
3528 PUSH(tb);
3529 PUSH(val);
3530 PUSH(exc);
3531 why = WHY_NOT;
3532 JUMPTO(handler);
3533 break;
3534 }
3535 if (b->b_type == SETUP_FINALLY) {
3536 if (why & (WHY_RETURN | WHY_CONTINUE))
3537 PUSH(retval);
3538 PUSH(PyLong_FromLong((long)why));
3539 why = WHY_NOT;
3540 JUMPTO(b->b_handler);
3541 break;
3542 }
3543 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003545 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003547 if (why != WHY_NOT)
3548 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00003549
Victor Stinnerace47d72013-07-18 01:41:08 +02003550 assert(!PyErr_Occurred());
3551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003552 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003554 assert(why != WHY_YIELD);
3555 /* Pop remaining stack entries. */
3556 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003557 PyObject *o = POP();
3558 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003559 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003561 if (why != WHY_RETURN)
3562 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003563
Victor Stinner4a7cc882015-03-06 23:35:27 +01003564 assert((retval != NULL) ^ (PyErr_Occurred() != NULL));
Victor Stinnerace47d72013-07-18 01:41:08 +02003565
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003566fast_yield:
Yury Selivanoveb636452016-09-08 22:01:51 -07003567 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01003568
Benjamin Petersonac913412011-07-03 16:25:11 -05003569 /* The purpose of this block is to put aside the generator's exception
3570 state and restore that of the calling frame. If the current
3571 exception state is from the caller, we clear the exception values
3572 on the generator frame, so they are not swapped back in latter. The
3573 origin of the current exception state is determined by checking for
3574 except handler blocks, which we must be in iff a new exception
3575 state came into existence in this frame. (An uncaught exception
3576 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3577 int i;
Victor Stinner74319ae2016-08-25 00:04:09 +02003578 for (i = 0; i < f->f_iblock; i++) {
3579 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER) {
Benjamin Petersonac913412011-07-03 16:25:11 -05003580 break;
Victor Stinner74319ae2016-08-25 00:04:09 +02003581 }
3582 }
Benjamin Petersonac913412011-07-03 16:25:11 -05003583 if (i == f->f_iblock)
3584 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003585 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003586 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003587 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003588 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003590 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003591 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003592 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003593 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3594 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003595 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003596 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003597 why = WHY_EXCEPTION;
3598 }
3599 }
3600 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003601 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3602 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003603 PyTrace_RETURN, NULL);
3604 }
3605 }
3606 if (tstate->c_profilefunc) {
3607 if (why == WHY_EXCEPTION)
3608 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003609 tstate->c_profileobj,
3610 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003611 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003612 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3613 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003614 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003615 Py_CLEAR(retval);
Brett Cannonb94767f2011-02-22 20:15:44 +00003616 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003617 }
3618 }
3619 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003621 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003622exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003623 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003624 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003625 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003626
Victor Stinnerefde1462015-03-21 15:04:43 +01003627 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003628}
3629
Benjamin Petersonb204a422011-06-05 22:04:07 -05003630static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003631format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3632{
3633 int err;
3634 Py_ssize_t len = PyList_GET_SIZE(names);
3635 PyObject *name_str, *comma, *tail, *tmp;
3636
3637 assert(PyList_CheckExact(names));
3638 assert(len >= 1);
3639 /* Deal with the joys of natural language. */
3640 switch (len) {
3641 case 1:
3642 name_str = PyList_GET_ITEM(names, 0);
3643 Py_INCREF(name_str);
3644 break;
3645 case 2:
3646 name_str = PyUnicode_FromFormat("%U and %U",
3647 PyList_GET_ITEM(names, len - 2),
3648 PyList_GET_ITEM(names, len - 1));
3649 break;
3650 default:
3651 tail = PyUnicode_FromFormat(", %U, and %U",
3652 PyList_GET_ITEM(names, len - 2),
3653 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003654 if (tail == NULL)
3655 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003656 /* Chop off the last two objects in the list. This shouldn't actually
3657 fail, but we can't be too careful. */
3658 err = PyList_SetSlice(names, len - 2, len, NULL);
3659 if (err == -1) {
3660 Py_DECREF(tail);
3661 return;
3662 }
3663 /* Stitch everything up into a nice comma-separated list. */
3664 comma = PyUnicode_FromString(", ");
3665 if (comma == NULL) {
3666 Py_DECREF(tail);
3667 return;
3668 }
3669 tmp = PyUnicode_Join(comma, names);
3670 Py_DECREF(comma);
3671 if (tmp == NULL) {
3672 Py_DECREF(tail);
3673 return;
3674 }
3675 name_str = PyUnicode_Concat(tmp, tail);
3676 Py_DECREF(tmp);
3677 Py_DECREF(tail);
3678 break;
3679 }
3680 if (name_str == NULL)
3681 return;
3682 PyErr_Format(PyExc_TypeError,
3683 "%U() missing %i required %s argument%s: %U",
3684 co->co_name,
3685 len,
3686 kind,
3687 len == 1 ? "" : "s",
3688 name_str);
3689 Py_DECREF(name_str);
3690}
3691
3692static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003693missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003694 PyObject **fastlocals)
3695{
Victor Stinner74319ae2016-08-25 00:04:09 +02003696 Py_ssize_t i, j = 0;
3697 Py_ssize_t start, end;
3698 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003699 const char *kind = positional ? "positional" : "keyword-only";
3700 PyObject *missing_names;
3701
3702 /* Compute the names of the arguments that are missing. */
3703 missing_names = PyList_New(missing);
3704 if (missing_names == NULL)
3705 return;
3706 if (positional) {
3707 start = 0;
3708 end = co->co_argcount - defcount;
3709 }
3710 else {
3711 start = co->co_argcount;
3712 end = start + co->co_kwonlyargcount;
3713 }
3714 for (i = start; i < end; i++) {
3715 if (GETLOCAL(i) == NULL) {
3716 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3717 PyObject *name = PyObject_Repr(raw);
3718 if (name == NULL) {
3719 Py_DECREF(missing_names);
3720 return;
3721 }
3722 PyList_SET_ITEM(missing_names, j++, name);
3723 }
3724 }
3725 assert(j == missing);
3726 format_missing(kind, co, missing_names);
3727 Py_DECREF(missing_names);
3728}
3729
3730static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003731too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3732 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003733{
3734 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003735 Py_ssize_t kwonly_given = 0;
3736 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003737 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003738 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003739
Benjamin Petersone109c702011-06-24 09:37:26 -05003740 assert((co->co_flags & CO_VARARGS) == 0);
3741 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003742 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3743 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003744 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003745 }
3746 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003747 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003748 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003749 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003750 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003751 }
3752 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003753 plural = (co_argcount != 1);
3754 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003755 }
3756 if (sig == NULL)
3757 return;
3758 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003759 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3760 kwonly_sig = PyUnicode_FromFormat(format,
3761 given != 1 ? "s" : "",
3762 kwonly_given,
3763 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003764 if (kwonly_sig == NULL) {
3765 Py_DECREF(sig);
3766 return;
3767 }
3768 }
3769 else {
3770 /* This will not fail. */
3771 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003772 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003773 }
3774 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003775 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003776 co->co_name,
3777 sig,
3778 plural ? "s" : "",
3779 given,
3780 kwonly_sig,
3781 given == 1 && !kwonly_given ? "was" : "were");
3782 Py_DECREF(sig);
3783 Py_DECREF(kwonly_sig);
3784}
3785
Guido van Rossumc2e20742006-02-27 22:32:47 +00003786/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003787 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003788 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003789
Victor Stinner40ee3012014-06-16 15:59:28 +02003790static PyObject *
3791_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Victor Stinner74319ae2016-08-25 00:04:09 +02003792 PyObject **args, Py_ssize_t argcount,
3793 PyObject **kws, Py_ssize_t kwcount,
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003794 PyObject *kwnames, PyObject **kwstack,
Victor Stinner74319ae2016-08-25 00:04:09 +02003795 PyObject **defs, Py_ssize_t defcount,
3796 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003797 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003798{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003799 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003800 PyFrameObject *f;
3801 PyObject *retval = NULL;
3802 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003803 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003804 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003805 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3806 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003807 PyObject *kwdict;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003808 Py_ssize_t kwcount2 = kwnames == NULL ? 0 : PyTuple_GET_SIZE(kwnames);
Victor Stinnerc7020012016-08-16 23:40:29 +02003809
3810 assert((kwcount == 0) || (kws != NULL));
Tim Peters5ca576e2001-06-18 22:08:13 +00003811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003812 if (globals == NULL) {
3813 PyErr_SetString(PyExc_SystemError,
3814 "PyEval_EvalCodeEx: NULL globals");
3815 return NULL;
3816 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003817
Victor Stinnerc7020012016-08-16 23:40:29 +02003818 /* Create the frame */
3819 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003820 assert(tstate != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003821 f = PyFrame_New(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003822 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003823 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003824 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003825 fastlocals = f->f_localsplus;
3826 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003827
Victor Stinnerc7020012016-08-16 23:40:29 +02003828 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003829 if (co->co_flags & CO_VARKEYWORDS) {
3830 kwdict = PyDict_New();
3831 if (kwdict == NULL)
3832 goto fail;
3833 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003834 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003835 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003836 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003837 SETLOCAL(i, kwdict);
3838 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003839 else {
3840 kwdict = NULL;
3841 }
3842
3843 /* Copy positional arguments into local variables */
3844 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003845 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003846 }
3847 else {
3848 n = argcount;
3849 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003850 for (i = 0; i < n; i++) {
3851 x = args[i];
3852 Py_INCREF(x);
3853 SETLOCAL(i, x);
3854 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003855
3856 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003857 if (co->co_flags & CO_VARARGS) {
3858 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003859 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003860 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003861 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003862 SETLOCAL(total_args, u);
3863 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003864 x = args[i];
3865 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003866 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003867 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003868 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003869
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003870 /* Handle keyword arguments passed as an array of (key, value) pairs */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003871 for (i = 0; i < kwcount; i++) {
3872 PyObject **co_varnames;
3873 PyObject *keyword = kws[2*i];
3874 PyObject *value = kws[2*i + 1];
Victor Stinner17061a92016-08-16 23:39:42 +02003875 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003876
Benjamin Petersonb204a422011-06-05 22:04:07 -05003877 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3878 PyErr_Format(PyExc_TypeError,
3879 "%U() keywords must be strings",
3880 co->co_name);
3881 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003882 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003883
Benjamin Petersonb204a422011-06-05 22:04:07 -05003884 /* Speed hack: do raw pointer compares. As names are
3885 normally interned this should almost always hit. */
3886 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3887 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003888 PyObject *name = co_varnames[j];
3889 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003890 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003891 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003892 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003893
Benjamin Petersonb204a422011-06-05 22:04:07 -05003894 /* Slow fallback, just in case */
3895 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003896 PyObject *name = co_varnames[j];
3897 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3898 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003899 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003900 }
3901 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003902 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003903 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003904 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003905
Benjamin Petersonb204a422011-06-05 22:04:07 -05003906 if (j >= total_args && kwdict == NULL) {
3907 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003908 "%U() got an unexpected keyword argument '%S'",
3909 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003910 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003911 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003912
Christian Heimes0bd447f2013-07-20 14:48:10 +02003913 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3914 goto fail;
3915 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003916 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003917
Benjamin Petersonb204a422011-06-05 22:04:07 -05003918 kw_found:
3919 if (GETLOCAL(j) != NULL) {
3920 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003921 "%U() got multiple values for argument '%S'",
3922 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003923 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003924 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003925 Py_INCREF(value);
3926 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003927 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003928
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003929 /* Handle keyword arguments passed as keys tuple + values array */
3930 for (i = 0; i < kwcount2; i++) {
3931 PyObject **co_varnames;
3932 PyObject *keyword = PyTuple_GET_ITEM(kwnames, i);
3933 PyObject *value = kwstack[i];
3934 int j;
3935 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3936 PyErr_Format(PyExc_TypeError,
3937 "%U() keywords must be strings",
3938 co->co_name);
3939 goto fail;
3940 }
3941 /* Speed hack: do raw pointer compares. As names are
3942 normally interned this should almost always hit. */
3943 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3944 for (j = 0; j < total_args; j++) {
3945 PyObject *nm = co_varnames[j];
3946 if (nm == keyword)
3947 goto kw_found2;
3948 }
3949 /* Slow fallback, just in case */
3950 for (j = 0; j < total_args; j++) {
3951 PyObject *nm = co_varnames[j];
3952 int cmp = PyObject_RichCompareBool(
3953 keyword, nm, Py_EQ);
3954 if (cmp > 0)
3955 goto kw_found2;
3956 else if (cmp < 0)
3957 goto fail;
3958 }
3959 if (j >= total_args && kwdict == NULL) {
3960 PyErr_Format(PyExc_TypeError,
3961 "%U() got an unexpected "
3962 "keyword argument '%S'",
3963 co->co_name,
3964 keyword);
3965 goto fail;
3966 }
3967 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3968 goto fail;
3969 }
3970 continue;
3971 kw_found2:
3972 if (GETLOCAL(j) != NULL) {
3973 PyErr_Format(PyExc_TypeError,
3974 "%U() got multiple "
3975 "values for argument '%S'",
3976 co->co_name,
3977 keyword);
3978 goto fail;
3979 }
3980 Py_INCREF(value);
3981 SETLOCAL(j, value);
3982 }
3983
Victor Stinnerc7020012016-08-16 23:40:29 +02003984 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003985 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003986 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003987 goto fail;
3988 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003989
3990 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003991 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02003992 Py_ssize_t m = co->co_argcount - defcount;
3993 Py_ssize_t missing = 0;
3994 for (i = argcount; i < m; i++) {
3995 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003996 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02003997 }
3998 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003999 if (missing) {
4000 missing_arguments(co, missing, defcount, fastlocals);
4001 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004002 }
4003 if (n > m)
4004 i = n - m;
4005 else
4006 i = 0;
4007 for (; i < defcount; i++) {
4008 if (GETLOCAL(m+i) == NULL) {
4009 PyObject *def = defs[i];
4010 Py_INCREF(def);
4011 SETLOCAL(m+i, def);
4012 }
4013 }
4014 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004015
4016 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004017 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004018 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004019 for (i = co->co_argcount; i < total_args; i++) {
4020 PyObject *name;
4021 if (GETLOCAL(i) != NULL)
4022 continue;
4023 name = PyTuple_GET_ITEM(co->co_varnames, i);
4024 if (kwdefs != NULL) {
4025 PyObject *def = PyDict_GetItem(kwdefs, name);
4026 if (def) {
4027 Py_INCREF(def);
4028 SETLOCAL(i, def);
4029 continue;
4030 }
4031 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004032 missing++;
4033 }
4034 if (missing) {
4035 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004036 goto fail;
4037 }
4038 }
4039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004040 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004041 vars into frame. */
4042 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004043 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05004044 int arg;
4045 /* Possibly account for the cell variable being an argument. */
4046 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004047 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004048 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004049 /* Clear the local copy. */
4050 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004051 }
4052 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004053 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004054 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004055 if (c == NULL)
4056 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004057 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004058 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004059
4060 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004061 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4062 PyObject *o = PyTuple_GET_ITEM(closure, i);
4063 Py_INCREF(o);
4064 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004065 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004066
Yury Selivanoveb636452016-09-08 22:01:51 -07004067 /* Handle generator/coroutine/asynchronous generator */
4068 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004069 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04004070 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004071 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004072
4073 if (is_coro && tstate->in_coroutine_wrapper) {
4074 assert(coro_wrapper != NULL);
4075 PyErr_Format(PyExc_RuntimeError,
4076 "coroutine wrapper %.200R attempted "
4077 "to recursively wrap %.200R",
4078 coro_wrapper,
4079 co);
4080 goto fail;
4081 }
Yury Selivanov75445082015-05-11 22:57:16 -04004082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004083 /* Don't need to keep the reference to f_back, it will be set
4084 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004085 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004087 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004089 /* Create a new generator that owns the ready to run frame
4090 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004091 if (is_coro) {
4092 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004093 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4094 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004095 } else {
4096 gen = PyGen_NewWithQualName(f, name, qualname);
4097 }
Yury Selivanov75445082015-05-11 22:57:16 -04004098 if (gen == NULL)
4099 return NULL;
4100
Yury Selivanov94c22632015-06-04 10:16:51 -04004101 if (is_coro && coro_wrapper != NULL) {
4102 PyObject *wrapped;
4103 tstate->in_coroutine_wrapper = 1;
4104 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4105 tstate->in_coroutine_wrapper = 0;
4106 return wrapped;
4107 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004108
Yury Selivanov75445082015-05-11 22:57:16 -04004109 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004110 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004112 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004113
Thomas Woutersce272b62007-09-19 21:19:28 +00004114fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004116 /* decref'ing the frame can cause __del__ methods to get invoked,
4117 which can call back into Python. While we're done with the
4118 current Python frame (f), the associated C stack is still in use,
4119 so recursion_depth must be boosted for the duration.
4120 */
4121 assert(tstate != NULL);
4122 ++tstate->recursion_depth;
4123 Py_DECREF(f);
4124 --tstate->recursion_depth;
4125 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004126}
4127
Victor Stinner40ee3012014-06-16 15:59:28 +02004128PyObject *
4129PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
4130 PyObject **args, int argcount, PyObject **kws, int kwcount,
4131 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
4132{
4133 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004134 args, argcount,
4135 kws, kwcount,
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004136 NULL, NULL,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004137 defs, defcount,
4138 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004139 NULL, NULL);
4140}
Tim Peters5ca576e2001-06-18 22:08:13 +00004141
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004142static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004143special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004145 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004146 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004147 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004148 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004149 return NULL;
4150 }
4151 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004152}
4153
4154
Benjamin Peterson87880242011-07-03 16:48:31 -05004155/* These 3 functions deal with the exception state of generators. */
4156
4157static void
4158save_exc_state(PyThreadState *tstate, PyFrameObject *f)
4159{
4160 PyObject *type, *value, *traceback;
4161 Py_XINCREF(tstate->exc_type);
4162 Py_XINCREF(tstate->exc_value);
4163 Py_XINCREF(tstate->exc_traceback);
4164 type = f->f_exc_type;
4165 value = f->f_exc_value;
4166 traceback = f->f_exc_traceback;
4167 f->f_exc_type = tstate->exc_type;
4168 f->f_exc_value = tstate->exc_value;
4169 f->f_exc_traceback = tstate->exc_traceback;
4170 Py_XDECREF(type);
4171 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004172 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05004173}
4174
4175static void
4176swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
4177{
4178 PyObject *tmp;
4179 tmp = tstate->exc_type;
4180 tstate->exc_type = f->f_exc_type;
4181 f->f_exc_type = tmp;
4182 tmp = tstate->exc_value;
4183 tstate->exc_value = f->f_exc_value;
4184 f->f_exc_value = tmp;
4185 tmp = tstate->exc_traceback;
4186 tstate->exc_traceback = f->f_exc_traceback;
4187 f->f_exc_traceback = tmp;
4188}
4189
4190static void
4191restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
4192{
4193 PyObject *type, *value, *tb;
4194 type = tstate->exc_type;
4195 value = tstate->exc_value;
4196 tb = tstate->exc_traceback;
4197 tstate->exc_type = f->f_exc_type;
4198 tstate->exc_value = f->f_exc_value;
4199 tstate->exc_traceback = f->f_exc_traceback;
4200 f->f_exc_type = NULL;
4201 f->f_exc_value = NULL;
4202 f->f_exc_traceback = NULL;
4203 Py_XDECREF(type);
4204 Py_XDECREF(value);
4205 Py_XDECREF(tb);
4206}
4207
4208
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004209/* Logic for the raise statement (too complicated for inlining).
4210 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004211static int
Collin Winter828f04a2007-08-31 00:04:24 +00004212do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004214 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004216 if (exc == NULL) {
4217 /* Reraise */
4218 PyThreadState *tstate = PyThreadState_GET();
4219 PyObject *tb;
4220 type = tstate->exc_type;
4221 value = tstate->exc_value;
4222 tb = tstate->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004223 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004224 PyErr_SetString(PyExc_RuntimeError,
4225 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004226 return 0;
4227 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004228 Py_XINCREF(type);
4229 Py_XINCREF(value);
4230 Py_XINCREF(tb);
4231 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004232 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004233 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004235 /* We support the following forms of raise:
4236 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004237 raise <instance>
4238 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004240 if (PyExceptionClass_Check(exc)) {
4241 type = exc;
4242 value = PyObject_CallObject(exc, NULL);
4243 if (value == NULL)
4244 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004245 if (!PyExceptionInstance_Check(value)) {
4246 PyErr_Format(PyExc_TypeError,
4247 "calling %R should have returned an instance of "
4248 "BaseException, not %R",
4249 type, Py_TYPE(value));
4250 goto raise_error;
4251 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004252 }
4253 else if (PyExceptionInstance_Check(exc)) {
4254 value = exc;
4255 type = PyExceptionInstance_Class(exc);
4256 Py_INCREF(type);
4257 }
4258 else {
4259 /* Not something you can raise. You get an exception
4260 anyway, just not what you specified :-) */
4261 Py_DECREF(exc);
4262 PyErr_SetString(PyExc_TypeError,
4263 "exceptions must derive from BaseException");
4264 goto raise_error;
4265 }
Collin Winter828f04a2007-08-31 00:04:24 +00004266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004267 if (cause) {
4268 PyObject *fixed_cause;
4269 if (PyExceptionClass_Check(cause)) {
4270 fixed_cause = PyObject_CallObject(cause, NULL);
4271 if (fixed_cause == NULL)
4272 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004273 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004274 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004275 else if (PyExceptionInstance_Check(cause)) {
4276 fixed_cause = cause;
4277 }
4278 else if (cause == Py_None) {
4279 Py_DECREF(cause);
4280 fixed_cause = NULL;
4281 }
4282 else {
4283 PyErr_SetString(PyExc_TypeError,
4284 "exception causes must derive from "
4285 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004286 goto raise_error;
4287 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004288 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004289 }
Collin Winter828f04a2007-08-31 00:04:24 +00004290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004291 PyErr_SetObject(type, value);
4292 /* PyErr_SetObject incref's its arguments */
4293 Py_XDECREF(value);
4294 Py_XDECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004295 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004296
4297raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004298 Py_XDECREF(value);
4299 Py_XDECREF(type);
4300 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004301 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004302}
4303
Tim Petersd6d010b2001-06-21 02:49:55 +00004304/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004305 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004306
Guido van Rossum0368b722007-05-11 16:50:42 +00004307 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4308 with a variable target.
4309*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004310
Barry Warsawe42b18f1997-08-25 22:13:04 +00004311static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004312unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004313{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004314 int i = 0, j = 0;
4315 Py_ssize_t ll = 0;
4316 PyObject *it; /* iter(v) */
4317 PyObject *w;
4318 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004320 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004322 it = PyObject_GetIter(v);
4323 if (it == NULL)
4324 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00004325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004326 for (; i < argcnt; i++) {
4327 w = PyIter_Next(it);
4328 if (w == NULL) {
4329 /* Iterator done, via error or exhaustion. */
4330 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004331 if (argcntafter == -1) {
4332 PyErr_Format(PyExc_ValueError,
4333 "not enough values to unpack (expected %d, got %d)",
4334 argcnt, i);
4335 }
4336 else {
4337 PyErr_Format(PyExc_ValueError,
4338 "not enough values to unpack "
4339 "(expected at least %d, got %d)",
4340 argcnt + argcntafter, i);
4341 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004342 }
4343 goto Error;
4344 }
4345 *--sp = w;
4346 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004348 if (argcntafter == -1) {
4349 /* We better have exhausted the iterator now. */
4350 w = PyIter_Next(it);
4351 if (w == NULL) {
4352 if (PyErr_Occurred())
4353 goto Error;
4354 Py_DECREF(it);
4355 return 1;
4356 }
4357 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004358 PyErr_Format(PyExc_ValueError,
4359 "too many values to unpack (expected %d)",
4360 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004361 goto Error;
4362 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004364 l = PySequence_List(it);
4365 if (l == NULL)
4366 goto Error;
4367 *--sp = l;
4368 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004370 ll = PyList_GET_SIZE(l);
4371 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004372 PyErr_Format(PyExc_ValueError,
4373 "not enough values to unpack (expected at least %d, got %zd)",
4374 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004375 goto Error;
4376 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004378 /* Pop the "after-variable" args off the list. */
4379 for (j = argcntafter; j > 0; j--, i++) {
4380 *--sp = PyList_GET_ITEM(l, ll - j);
4381 }
4382 /* Resize the list. */
4383 Py_SIZE(l) = ll - argcntafter;
4384 Py_DECREF(it);
4385 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004386
Tim Petersd6d010b2001-06-21 02:49:55 +00004387Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004388 for (; i > 0; i--, sp++)
4389 Py_DECREF(*sp);
4390 Py_XDECREF(it);
4391 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004392}
4393
4394
Guido van Rossum96a42c81992-01-12 02:29:51 +00004395#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004396static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004397prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004398{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004399 printf("%s ", str);
4400 if (PyObject_Print(v, stdout, 0) != 0)
4401 PyErr_Clear(); /* Don't know what else to do */
4402 printf("\n");
4403 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004404}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004405#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004406
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004407static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004408call_exc_trace(Py_tracefunc func, PyObject *self,
4409 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004410{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004411 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004412 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004413 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414 if (value == NULL) {
4415 value = Py_None;
4416 Py_INCREF(value);
4417 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004418 PyErr_NormalizeException(&type, &value, &orig_traceback);
4419 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004420 arg = PyTuple_Pack(3, type, value, traceback);
4421 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004422 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004423 return;
4424 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004425 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004426 Py_DECREF(arg);
4427 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004428 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004429 else {
4430 Py_XDECREF(type);
4431 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004432 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004433 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004434}
4435
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004436static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004437call_trace_protected(Py_tracefunc func, PyObject *obj,
4438 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004439 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004441 PyObject *type, *value, *traceback;
4442 int err;
4443 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004444 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004445 if (err == 0)
4446 {
4447 PyErr_Restore(type, value, traceback);
4448 return 0;
4449 }
4450 else {
4451 Py_XDECREF(type);
4452 Py_XDECREF(value);
4453 Py_XDECREF(traceback);
4454 return -1;
4455 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004456}
4457
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004458static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004459call_trace(Py_tracefunc func, PyObject *obj,
4460 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004461 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004462{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004463 int result;
4464 if (tstate->tracing)
4465 return 0;
4466 tstate->tracing++;
4467 tstate->use_tracing = 0;
4468 result = func(obj, frame, what, arg);
4469 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4470 || (tstate->c_profilefunc != NULL));
4471 tstate->tracing--;
4472 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004473}
4474
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004475PyObject *
4476_PyEval_CallTracing(PyObject *func, PyObject *args)
4477{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004478 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004479 int save_tracing = tstate->tracing;
4480 int save_use_tracing = tstate->use_tracing;
4481 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004483 tstate->tracing = 0;
4484 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4485 || (tstate->c_profilefunc != NULL));
4486 result = PyObject_Call(func, args, NULL);
4487 tstate->tracing = save_tracing;
4488 tstate->use_tracing = save_use_tracing;
4489 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004490}
4491
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004492/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004493static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004494maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004495 PyThreadState *tstate, PyFrameObject *frame,
4496 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004497{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004498 int result = 0;
4499 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004501 /* If the last instruction executed isn't in the current
4502 instruction window, reset the window.
4503 */
4504 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4505 PyAddrPair bounds;
4506 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4507 &bounds);
4508 *instr_lb = bounds.ap_lower;
4509 *instr_ub = bounds.ap_upper;
4510 }
4511 /* If the last instruction falls at the start of a line or if
4512 it represents a jump backwards, update the frame's line
4513 number and call the trace function. */
4514 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
4515 frame->f_lineno = line;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004516 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004517 }
4518 *instr_prev = frame->f_lasti;
4519 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004520}
4521
Fred Drake5755ce62001-06-27 19:19:46 +00004522void
4523PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004524{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004525 PyThreadState *tstate = PyThreadState_GET();
4526 PyObject *temp = tstate->c_profileobj;
4527 Py_XINCREF(arg);
4528 tstate->c_profilefunc = NULL;
4529 tstate->c_profileobj = NULL;
4530 /* Must make sure that tracing is not ignored if 'temp' is freed */
4531 tstate->use_tracing = tstate->c_tracefunc != NULL;
4532 Py_XDECREF(temp);
4533 tstate->c_profilefunc = func;
4534 tstate->c_profileobj = arg;
4535 /* Flag that tracing or profiling is turned on */
4536 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004537}
4538
4539void
4540PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004542 PyThreadState *tstate = PyThreadState_GET();
4543 PyObject *temp = tstate->c_traceobj;
4544 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4545 Py_XINCREF(arg);
4546 tstate->c_tracefunc = NULL;
4547 tstate->c_traceobj = NULL;
4548 /* Must make sure that profiling is not ignored if 'temp' is freed */
4549 tstate->use_tracing = tstate->c_profilefunc != NULL;
4550 Py_XDECREF(temp);
4551 tstate->c_tracefunc = func;
4552 tstate->c_traceobj = arg;
4553 /* Flag that tracing or profiling is turned on */
4554 tstate->use_tracing = ((func != NULL)
4555 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004556}
4557
Yury Selivanov75445082015-05-11 22:57:16 -04004558void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004559_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004560{
4561 PyThreadState *tstate = PyThreadState_GET();
4562
Yury Selivanov75445082015-05-11 22:57:16 -04004563 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004564 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004565}
4566
4567PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004568_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004569{
4570 PyThreadState *tstate = PyThreadState_GET();
4571 return tstate->coroutine_wrapper;
4572}
4573
Yury Selivanoveb636452016-09-08 22:01:51 -07004574void
4575_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4576{
4577 PyThreadState *tstate = PyThreadState_GET();
4578
4579 Py_XINCREF(firstiter);
4580 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4581}
4582
4583PyObject *
4584_PyEval_GetAsyncGenFirstiter(void)
4585{
4586 PyThreadState *tstate = PyThreadState_GET();
4587 return tstate->async_gen_firstiter;
4588}
4589
4590void
4591_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4592{
4593 PyThreadState *tstate = PyThreadState_GET();
4594
4595 Py_XINCREF(finalizer);
4596 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4597}
4598
4599PyObject *
4600_PyEval_GetAsyncGenFinalizer(void)
4601{
4602 PyThreadState *tstate = PyThreadState_GET();
4603 return tstate->async_gen_finalizer;
4604}
4605
Guido van Rossumb209a111997-04-29 18:18:01 +00004606PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004607PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004608{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004609 PyFrameObject *current_frame = PyEval_GetFrame();
4610 if (current_frame == NULL)
4611 return PyThreadState_GET()->interp->builtins;
4612 else
4613 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004614}
4615
Guido van Rossumb209a111997-04-29 18:18:01 +00004616PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004617PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004618{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004619 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004620 if (current_frame == NULL) {
4621 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004622 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004623 }
4624
4625 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4626 return NULL;
4627
4628 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004629 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004630}
4631
Guido van Rossumb209a111997-04-29 18:18:01 +00004632PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004633PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004634{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004635 PyFrameObject *current_frame = PyEval_GetFrame();
4636 if (current_frame == NULL)
4637 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004638
4639 assert(current_frame->f_globals != NULL);
4640 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004641}
4642
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004643PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004644PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004645{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004646 PyThreadState *tstate = PyThreadState_GET();
4647 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004648}
4649
Guido van Rossum6135a871995-01-09 17:53:26 +00004650int
Tim Peters5ba58662001-07-16 02:29:45 +00004651PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004652{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004653 PyFrameObject *current_frame = PyEval_GetFrame();
4654 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004656 if (current_frame != NULL) {
4657 const int codeflags = current_frame->f_code->co_flags;
4658 const int compilerflags = codeflags & PyCF_MASK;
4659 if (compilerflags) {
4660 result = 1;
4661 cf->cf_flags |= compilerflags;
4662 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004663#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004664 if (codeflags & CO_GENERATOR_ALLOWED) {
4665 result = 1;
4666 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4667 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004668#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004669 }
4670 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004671}
4672
Guido van Rossum3f5da241990-12-20 15:06:42 +00004673
Guido van Rossum681d79a1995-07-18 14:51:37 +00004674/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00004675 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00004676
Guido van Rossumb209a111997-04-29 18:18:01 +00004677PyObject *
Victor Stinner8a31c822016-08-19 17:12:23 +02004678PyEval_CallObjectWithKeywords(PyObject *func, PyObject *args, PyObject *kwargs)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004679{
Victor Stinner59b356d2015-03-16 11:52:32 +01004680#ifdef Py_DEBUG
4681 /* PyEval_CallObjectWithKeywords() must not be called with an exception
4682 set. It raises a new exception if parameters are invalid or if
4683 PyTuple_New() fails, and so the original exception is lost. */
4684 assert(!PyErr_Occurred());
4685#endif
4686
Victor Stinner8a31c822016-08-19 17:12:23 +02004687 if (args == NULL) {
Victor Stinner155ea652016-08-22 23:26:00 +02004688 return _PyObject_FastCallDict(func, NULL, 0, kwargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004689 }
Victor Stinner155ea652016-08-22 23:26:00 +02004690
4691 if (!PyTuple_Check(args)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004692 PyErr_SetString(PyExc_TypeError,
4693 "argument list must be a tuple");
4694 return NULL;
4695 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00004696
Victor Stinner8a31c822016-08-19 17:12:23 +02004697 if (kwargs != NULL && !PyDict_Check(kwargs)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004698 PyErr_SetString(PyExc_TypeError,
4699 "keyword list must be a dictionary");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004700 return NULL;
4701 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00004702
Victor Stinner6e2333d2016-08-23 00:25:01 +02004703 return PyObject_Call(func, args, kwargs);
Jeremy Hylton52820442001-01-03 23:52:36 +00004704}
4705
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004706const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004707PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004708{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004709 if (PyMethod_Check(func))
4710 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4711 else if (PyFunction_Check(func))
4712 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
4713 else if (PyCFunction_Check(func))
4714 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4715 else
4716 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004717}
4718
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004719const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004720PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004721{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004722 if (PyMethod_Check(func))
4723 return "()";
4724 else if (PyFunction_Check(func))
4725 return "()";
4726 else if (PyCFunction_Check(func))
4727 return "()";
4728 else
4729 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004730}
4731
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004732#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004733if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004734 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4735 tstate, tstate->frame, \
4736 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004737 x = NULL; \
4738 } \
4739 else { \
4740 x = call; \
4741 if (tstate->c_profilefunc != NULL) { \
4742 if (x == NULL) { \
4743 call_trace_protected(tstate->c_profilefunc, \
4744 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004745 tstate, tstate->frame, \
4746 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004747 /* XXX should pass (type, value, tb) */ \
4748 } else { \
4749 if (call_trace(tstate->c_profilefunc, \
4750 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004751 tstate, tstate->frame, \
4752 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004753 Py_DECREF(x); \
4754 x = NULL; \
4755 } \
4756 } \
4757 } \
4758 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004759} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004760 x = call; \
4761 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004762
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004763static PyObject *
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004764call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004765{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004766 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004767 PyObject *func = *pfunc;
4768 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004769 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4770 Py_ssize_t nargs = oparg - nkwargs;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004771 PyObject **stack;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004773 /* Always dispatch PyCFunction first, because these are
4774 presumed to be the most frequent callable object.
4775 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004776 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004777 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004779 PCALL(PCALL_CFUNCTION);
Victor Stinner4a7cc882015-03-06 23:35:27 +01004780
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004781 stack = (*pp_stack) - nargs - nkwargs;
4782 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004783 }
4784 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004785 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4786 /* optimize access to bound methods */
4787 PyObject *self = PyMethod_GET_SELF(func);
4788 PCALL(PCALL_METHOD);
4789 PCALL(PCALL_BOUND_METHOD);
4790 Py_INCREF(self);
4791 func = PyMethod_GET_FUNCTION(func);
4792 Py_INCREF(func);
4793 Py_SETREF(*pfunc, self);
4794 nargs++;
4795 }
4796 else {
4797 Py_INCREF(func);
4798 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004799
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004800 stack = (*pp_stack) - nargs - nkwargs;
Victor Stinner4a7cc882015-03-06 23:35:27 +01004801
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004802 if (PyFunction_Check(func)) {
4803 x = fast_function(func, stack, nargs, kwnames);
4804 }
4805 else {
4806 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4807 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004808
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004809 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004810 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004811
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004812 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004814 /* Clear the stack of the function object. Also removes
4815 the arguments in case they weren't consumed already
Victor Stinnere90bdb12016-08-25 23:26:50 +02004816 (fast_function() and err_args() leave them on the stack).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004817 */
4818 while ((*pp_stack) > pfunc) {
4819 w = EXT_POP(*pp_stack);
4820 Py_DECREF(w);
4821 PCALL(PCALL_POP);
4822 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004824 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004825}
4826
Victor Stinnere90bdb12016-08-25 23:26:50 +02004827/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004828 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004829 For the simplest case -- a function that takes only positional
4830 arguments and is called with only positional arguments -- it
4831 inlines the most primitive frame setup code from
4832 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4833 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004834*/
4835
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004836static PyObject*
Victor Stinnerd8735722016-09-09 12:36:44 -07004837_PyFunction_FastCall(PyCodeObject *co, PyObject **args, Py_ssize_t nargs,
4838 PyObject *globals)
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004839{
4840 PyFrameObject *f;
4841 PyThreadState *tstate = PyThreadState_GET();
4842 PyObject **fastlocals;
4843 Py_ssize_t i;
4844 PyObject *result;
4845
4846 PCALL(PCALL_FASTER_FUNCTION);
4847 assert(globals != NULL);
4848 /* XXX Perhaps we should create a specialized
4849 PyFrame_New() that doesn't take locals, but does
4850 take builtins without sanity checking them.
4851 */
4852 assert(tstate != NULL);
4853 f = PyFrame_New(tstate, co, globals, NULL);
4854 if (f == NULL) {
4855 return NULL;
4856 }
4857
4858 fastlocals = f->f_localsplus;
4859
Victor Stinner74319ae2016-08-25 00:04:09 +02004860 for (i = 0; i < nargs; i++) {
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004861 Py_INCREF(*args);
4862 fastlocals[i] = *args++;
4863 }
4864 result = PyEval_EvalFrameEx(f,0);
4865
4866 ++tstate->recursion_depth;
4867 Py_DECREF(f);
4868 --tstate->recursion_depth;
4869
4870 return result;
4871}
4872
Victor Stinnere90bdb12016-08-25 23:26:50 +02004873static PyObject *
Victor Stinnerd8735722016-09-09 12:36:44 -07004874fast_function(PyObject *func, PyObject **stack,
4875 Py_ssize_t nargs, PyObject *kwnames)
Jeremy Hylton52820442001-01-03 23:52:36 +00004876{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004877 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4878 PyObject *globals = PyFunction_GET_GLOBALS(func);
4879 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004880 PyObject *kwdefs, *closure, *name, *qualname;
4881 PyObject **d;
Victor Stinnerd8735722016-09-09 12:36:44 -07004882 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004883 Py_ssize_t nd;
Victor Stinnerd8735722016-09-09 12:36:44 -07004884
4885 assert((nargs == 0 && nkwargs == 0) || stack != NULL);
Victor Stinner577e1f82016-08-25 00:29:32 +02004886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004887 PCALL(PCALL_FUNCTION);
4888 PCALL(PCALL_FAST_FUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004889
Victor Stinner74319ae2016-08-25 00:04:09 +02004890 if (co->co_kwonlyargcount == 0 && nkwargs == 0 &&
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004891 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
4892 {
Victor Stinner2eedc112016-08-22 12:29:42 +02004893 if (argdefs == NULL && co->co_argcount == nargs) {
Victor Stinnerd8735722016-09-09 12:36:44 -07004894 return _PyFunction_FastCall(co, stack, nargs, globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02004895 }
4896 else if (nargs == 0 && argdefs != NULL
4897 && co->co_argcount == Py_SIZE(argdefs)) {
4898 /* function called with no arguments, but all parameters have
4899 a default value: use default values as arguments .*/
4900 stack = &PyTuple_GET_ITEM(argdefs, 0);
Victor Stinnerd8735722016-09-09 12:36:44 -07004901 return _PyFunction_FastCall(co, stack, Py_SIZE(argdefs), globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02004902 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004903 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004904
4905 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
4906 closure = PyFunction_GET_CLOSURE(func);
4907 name = ((PyFunctionObject *)func) -> func_name;
4908 qualname = ((PyFunctionObject *)func) -> func_qualname;
4909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004910 if (argdefs != NULL) {
4911 d = &PyTuple_GET_ITEM(argdefs, 0);
4912 nd = Py_SIZE(argdefs);
4913 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004914 else {
4915 d = NULL;
4916 nd = 0;
4917 }
4918 return _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
Victor Stinner2eedc112016-08-22 12:29:42 +02004919 stack, nargs,
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004920 NULL, 0,
Victor Stinnerd8735722016-09-09 12:36:44 -07004921 kwnames, stack + nargs,
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004922 d, (int)nd, kwdefs,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004923 closure, name, qualname);
4924}
4925
4926PyObject *
Victor Stinnerd8735722016-09-09 12:36:44 -07004927_PyFunction_FastCallKeywords(PyObject *func, PyObject **stack,
4928 Py_ssize_t nargs, PyObject *kwnames)
4929{
4930 return fast_function(func, stack, nargs, kwnames);
4931}
4932
4933PyObject *
Victor Stinner74319ae2016-08-25 00:04:09 +02004934_PyFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs,
Victor Stinnerb9009392016-08-22 23:15:44 +02004935 PyObject *kwargs)
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004936{
4937 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4938 PyObject *globals = PyFunction_GET_GLOBALS(func);
4939 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4940 PyObject *kwdefs, *closure, *name, *qualname;
Victor Stinnerb9009392016-08-22 23:15:44 +02004941 PyObject *kwtuple, **k;
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004942 PyObject **d;
Victor Stinner74319ae2016-08-25 00:04:09 +02004943 Py_ssize_t nd, nk;
Victor Stinnerb9009392016-08-22 23:15:44 +02004944 PyObject *result;
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004945
Victor Stinner74319ae2016-08-25 00:04:09 +02004946 assert(func != NULL);
4947 assert(nargs >= 0);
4948 assert(nargs == 0 || args != NULL);
Victor Stinnerb9009392016-08-22 23:15:44 +02004949 assert(kwargs == NULL || PyDict_Check(kwargs));
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004950
Victor Stinner577e1f82016-08-25 00:29:32 +02004951 PCALL(PCALL_FUNCTION);
4952 PCALL(PCALL_FAST_FUNCTION);
4953
Victor Stinnerb9009392016-08-22 23:15:44 +02004954 if (co->co_kwonlyargcount == 0 &&
4955 (kwargs == NULL || PyDict_Size(kwargs) == 0) &&
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004956 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
4957 {
Victor Stinnerb9009392016-08-22 23:15:44 +02004958 /* Fast paths */
Victor Stinner2eedc112016-08-22 12:29:42 +02004959 if (argdefs == NULL && co->co_argcount == nargs) {
Victor Stinnerd8735722016-09-09 12:36:44 -07004960 return _PyFunction_FastCall(co, args, nargs, globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02004961 }
4962 else if (nargs == 0 && argdefs != NULL
4963 && co->co_argcount == Py_SIZE(argdefs)) {
4964 /* function called with no arguments, but all parameters have
4965 a default value: use default values as arguments .*/
4966 args = &PyTuple_GET_ITEM(argdefs, 0);
Victor Stinnerd8735722016-09-09 12:36:44 -07004967 return _PyFunction_FastCall(co, args, Py_SIZE(argdefs), globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02004968 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004969 }
4970
Victor Stinnerb9009392016-08-22 23:15:44 +02004971 if (kwargs != NULL) {
4972 Py_ssize_t pos, i;
4973 nk = PyDict_Size(kwargs);
4974
4975 kwtuple = PyTuple_New(2 * nk);
4976 if (kwtuple == NULL) {
4977 return NULL;
4978 }
4979
4980 k = &PyTuple_GET_ITEM(kwtuple, 0);
4981 pos = i = 0;
4982 while (PyDict_Next(kwargs, &pos, &k[i], &k[i+1])) {
4983 Py_INCREF(k[i]);
4984 Py_INCREF(k[i+1]);
4985 i += 2;
4986 }
4987 nk = i / 2;
4988 }
4989 else {
4990 kwtuple = NULL;
4991 k = NULL;
4992 nk = 0;
4993 }
4994
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004995 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
4996 closure = PyFunction_GET_CLOSURE(func);
4997 name = ((PyFunctionObject *)func) -> func_name;
4998 qualname = ((PyFunctionObject *)func) -> func_qualname;
4999
5000 if (argdefs != NULL) {
5001 d = &PyTuple_GET_ITEM(argdefs, 0);
5002 nd = Py_SIZE(argdefs);
5003 }
5004 else {
5005 d = NULL;
5006 nd = 0;
5007 }
Victor Stinnerb9009392016-08-22 23:15:44 +02005008
5009 result = _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
5010 args, nargs,
Victor Stinner74319ae2016-08-25 00:04:09 +02005011 k, nk,
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005012 NULL, NULL,
Victor Stinnerb9009392016-08-22 23:15:44 +02005013 d, nd, kwdefs,
5014 closure, name, qualname);
5015 Py_XDECREF(kwtuple);
5016 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00005017}
5018
5019static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005020do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005021{
Jeremy Hylton985eba52003-02-05 23:13:00 +00005022#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005023 /* At this point, we have to look at the type of func to
5024 update the call stats properly. Do it here so as to avoid
5025 exposing the call stats machinery outside ceval.c
5026 */
5027 if (PyFunction_Check(func))
5028 PCALL(PCALL_FUNCTION);
5029 else if (PyMethod_Check(func))
5030 PCALL(PCALL_METHOD);
5031 else if (PyType_Check(func))
5032 PCALL(PCALL_TYPE);
5033 else if (PyCFunction_Check(func))
5034 PCALL(PCALL_CFUNCTION);
5035 else
5036 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00005037#endif
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005038
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005039 if (PyCFunction_Check(func)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005040 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005041 PyThreadState *tstate = PyThreadState_GET();
5042 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005043 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005044 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005045 else {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005046 return PyObject_Call(func, callargs, kwdict);
Victor Stinner74319ae2016-08-25 00:04:09 +02005047 }
Jeremy Hylton52820442001-01-03 23:52:36 +00005048}
5049
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005050/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005051 nb_index slot defined, and store in *pi.
5052 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
5053 and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005054 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005055*/
Tim Petersb5196382001-12-16 19:44:20 +00005056/* Note: If v is NULL, return success without storing into *pi. This
5057 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
5058 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00005059*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005060int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005061_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005062{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005063 if (v != NULL) {
5064 Py_ssize_t x;
5065 if (PyIndex_Check(v)) {
5066 x = PyNumber_AsSsize_t(v, NULL);
5067 if (x == -1 && PyErr_Occurred())
5068 return 0;
5069 }
5070 else {
5071 PyErr_SetString(PyExc_TypeError,
5072 "slice indices must be integers or "
5073 "None or have an __index__ method");
5074 return 0;
5075 }
5076 *pi = x;
5077 }
5078 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005079}
5080
Guido van Rossum486364b2007-06-30 05:01:58 +00005081#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005082 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00005083
Guido van Rossumb209a111997-04-29 18:18:01 +00005084static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02005085cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005086{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005087 int res = 0;
5088 switch (op) {
5089 case PyCmp_IS:
5090 res = (v == w);
5091 break;
5092 case PyCmp_IS_NOT:
5093 res = (v != w);
5094 break;
5095 case PyCmp_IN:
5096 res = PySequence_Contains(w, v);
5097 if (res < 0)
5098 return NULL;
5099 break;
5100 case PyCmp_NOT_IN:
5101 res = PySequence_Contains(w, v);
5102 if (res < 0)
5103 return NULL;
5104 res = !res;
5105 break;
5106 case PyCmp_EXC_MATCH:
5107 if (PyTuple_Check(w)) {
5108 Py_ssize_t i, length;
5109 length = PyTuple_Size(w);
5110 for (i = 0; i < length; i += 1) {
5111 PyObject *exc = PyTuple_GET_ITEM(w, i);
5112 if (!PyExceptionClass_Check(exc)) {
5113 PyErr_SetString(PyExc_TypeError,
5114 CANNOT_CATCH_MSG);
5115 return NULL;
5116 }
5117 }
5118 }
5119 else {
5120 if (!PyExceptionClass_Check(w)) {
5121 PyErr_SetString(PyExc_TypeError,
5122 CANNOT_CATCH_MSG);
5123 return NULL;
5124 }
5125 }
5126 res = PyErr_GivenExceptionMatches(v, w);
5127 break;
5128 default:
5129 return PyObject_RichCompare(v, w, op);
5130 }
5131 v = res ? Py_True : Py_False;
5132 Py_INCREF(v);
5133 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005134}
5135
Thomas Wouters52152252000-08-17 22:55:00 +00005136static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005137import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
5138{
5139 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005140 PyObject *import_func, *res;
5141 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005142
5143 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
5144 if (import_func == NULL) {
5145 PyErr_SetString(PyExc_ImportError, "__import__ not found");
5146 return NULL;
5147 }
5148
5149 /* Fast path for not overloaded __import__. */
5150 if (import_func == PyThreadState_GET()->interp->import_func) {
5151 int ilevel = _PyLong_AsInt(level);
5152 if (ilevel == -1 && PyErr_Occurred()) {
5153 return NULL;
5154 }
5155 res = PyImport_ImportModuleLevelObject(
5156 name,
5157 f->f_globals,
5158 f->f_locals == NULL ? Py_None : f->f_locals,
5159 fromlist,
5160 ilevel);
5161 return res;
5162 }
5163
5164 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005165
5166 stack[0] = name;
5167 stack[1] = f->f_globals;
5168 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5169 stack[3] = fromlist;
5170 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005171 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005172 Py_DECREF(import_func);
5173 return res;
5174}
5175
5176static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00005177import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005178{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005179 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005180 _Py_IDENTIFIER(__name__);
5181 PyObject *fullmodname, *pkgname;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005183 x = PyObject_GetAttr(v, name);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005184 if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
5185 return x;
5186 /* Issue #17636: in case this failed because of a circular relative
5187 import, try to fallback on reading the module directly from
5188 sys.modules. */
5189 PyErr_Clear();
5190 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005191 if (pkgname == NULL) {
5192 goto error;
5193 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005194 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
5195 Py_DECREF(pkgname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005196 if (fullmodname == NULL) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005197 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005198 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005199 x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005200 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005201 if (x == NULL) {
5202 goto error;
5203 }
5204 Py_INCREF(x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005205 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005206 error:
5207 PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
5208 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005209}
Guido van Rossumac7be682001-01-17 15:42:30 +00005210
Thomas Wouters52152252000-08-17 22:55:00 +00005211static int
5212import_all_from(PyObject *locals, PyObject *v)
5213{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005214 _Py_IDENTIFIER(__all__);
5215 _Py_IDENTIFIER(__dict__);
5216 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005217 PyObject *dict, *name, *value;
5218 int skip_leading_underscores = 0;
5219 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005221 if (all == NULL) {
5222 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5223 return -1; /* Unexpected error */
5224 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005225 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005226 if (dict == NULL) {
5227 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5228 return -1;
5229 PyErr_SetString(PyExc_ImportError,
5230 "from-import-* object has no __dict__ and no __all__");
5231 return -1;
5232 }
5233 all = PyMapping_Keys(dict);
5234 Py_DECREF(dict);
5235 if (all == NULL)
5236 return -1;
5237 skip_leading_underscores = 1;
5238 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005240 for (pos = 0, err = 0; ; pos++) {
5241 name = PySequence_GetItem(all, pos);
5242 if (name == NULL) {
5243 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5244 err = -1;
5245 else
5246 PyErr_Clear();
5247 break;
5248 }
5249 if (skip_leading_underscores &&
5250 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005251 PyUnicode_READY(name) != -1 &&
5252 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005253 {
5254 Py_DECREF(name);
5255 continue;
5256 }
5257 value = PyObject_GetAttr(v, name);
5258 if (value == NULL)
5259 err = -1;
5260 else if (PyDict_CheckExact(locals))
5261 err = PyDict_SetItem(locals, name, value);
5262 else
5263 err = PyObject_SetItem(locals, name, value);
5264 Py_DECREF(name);
5265 Py_XDECREF(value);
5266 if (err != 0)
5267 break;
5268 }
5269 Py_DECREF(all);
5270 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005271}
5272
Guido van Rossumac7be682001-01-17 15:42:30 +00005273static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005274format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005275{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005276 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005278 if (!obj)
5279 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005281 obj_str = _PyUnicode_AsString(obj);
5282 if (!obj_str)
5283 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005285 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005286}
Guido van Rossum950361c1997-01-24 13:49:28 +00005287
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005288static void
5289format_exc_unbound(PyCodeObject *co, int oparg)
5290{
5291 PyObject *name;
5292 /* Don't stomp existing exception */
5293 if (PyErr_Occurred())
5294 return;
5295 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5296 name = PyTuple_GET_ITEM(co->co_cellvars,
5297 oparg);
5298 format_exc_check_arg(
5299 PyExc_UnboundLocalError,
5300 UNBOUNDLOCAL_ERROR_MSG,
5301 name);
5302 } else {
5303 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5304 PyTuple_GET_SIZE(co->co_cellvars));
5305 format_exc_check_arg(PyExc_NameError,
5306 UNBOUNDFREE_ERROR_MSG, name);
5307 }
5308}
5309
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005310static PyObject *
5311unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005312 PyFrameObject *f, const unsigned short *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005313{
5314 PyObject *res;
5315 if (Py_REFCNT(v) == 2) {
5316 /* In the common case, there are 2 references to the value
5317 * stored in 'variable' when the += is performed: one on the
5318 * value stack (in 'v') and one still stored in the
5319 * 'variable'. We try to delete the variable now to reduce
5320 * the refcnt to 1.
5321 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005322 int opcode, oparg;
5323 NEXTOPARG();
5324 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005325 case STORE_FAST:
5326 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005327 PyObject **fastlocals = f->f_localsplus;
5328 if (GETLOCAL(oparg) == v)
5329 SETLOCAL(oparg, NULL);
5330 break;
5331 }
5332 case STORE_DEREF:
5333 {
5334 PyObject **freevars = (f->f_localsplus +
5335 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005336 PyObject *c = freevars[oparg];
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005337 if (PyCell_GET(c) == v)
5338 PyCell_Set(c, NULL);
5339 break;
5340 }
5341 case STORE_NAME:
5342 {
5343 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005344 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005345 PyObject *locals = f->f_locals;
5346 if (PyDict_CheckExact(locals) &&
5347 PyDict_GetItem(locals, name) == v) {
5348 if (PyDict_DelItem(locals, name) != 0) {
5349 PyErr_Clear();
5350 }
5351 }
5352 break;
5353 }
5354 }
5355 }
5356 res = v;
5357 PyUnicode_Append(&res, w);
5358 return res;
5359}
5360
Guido van Rossum950361c1997-01-24 13:49:28 +00005361#ifdef DYNAMIC_EXECUTION_PROFILE
5362
Skip Montanarof118cb12001-10-15 20:51:38 +00005363static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005364getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005366 int i;
5367 PyObject *l = PyList_New(256);
5368 if (l == NULL) return NULL;
5369 for (i = 0; i < 256; i++) {
5370 PyObject *x = PyLong_FromLong(a[i]);
5371 if (x == NULL) {
5372 Py_DECREF(l);
5373 return NULL;
5374 }
5375 PyList_SetItem(l, i, x);
5376 }
5377 for (i = 0; i < 256; i++)
5378 a[i] = 0;
5379 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005380}
5381
5382PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005383_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005384{
5385#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005386 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005387#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005388 int i;
5389 PyObject *l = PyList_New(257);
5390 if (l == NULL) return NULL;
5391 for (i = 0; i < 257; i++) {
5392 PyObject *x = getarray(dxpairs[i]);
5393 if (x == NULL) {
5394 Py_DECREF(l);
5395 return NULL;
5396 }
5397 PyList_SetItem(l, i, x);
5398 }
5399 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005400#endif
5401}
5402
5403#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005404
5405Py_ssize_t
5406_PyEval_RequestCodeExtraIndex(freefunc free)
5407{
5408 PyThreadState *tstate = PyThreadState_Get();
5409 Py_ssize_t new_index;
5410
5411 if (tstate->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
5412 return -1;
5413 }
5414 new_index = tstate->co_extra_user_count++;
5415 tstate->co_extra_freefuncs[new_index] = free;
5416 return new_index;
5417}