blob: 2d42fe28ee9a71b65df71909cf035775c5004409 [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 /* Main switch on opcode */
1217 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 /* BEWARE!
1222 It is essential that any operation that fails sets either
1223 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1224 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 TARGET(NOP)
1227 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001228
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001229 TARGET(LOAD_FAST) {
1230 PyObject *value = GETLOCAL(oparg);
1231 if (value == NULL) {
1232 format_exc_check_arg(PyExc_UnboundLocalError,
1233 UNBOUNDLOCAL_ERROR_MSG,
1234 PyTuple_GetItem(co->co_varnames, oparg));
1235 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001237 Py_INCREF(value);
1238 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001240 }
1241
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001242 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001243 TARGET(LOAD_CONST) {
1244 PyObject *value = GETITEM(consts, oparg);
1245 Py_INCREF(value);
1246 PUSH(value);
1247 FAST_DISPATCH();
1248 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001249
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001250 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001251 TARGET(STORE_FAST) {
1252 PyObject *value = POP();
1253 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001255 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001256
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001257 TARGET(POP_TOP) {
1258 PyObject *value = POP();
1259 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001261 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001262
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001263 TARGET(ROT_TWO) {
1264 PyObject *top = TOP();
1265 PyObject *second = SECOND();
1266 SET_TOP(second);
1267 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001269 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001270
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001271 TARGET(ROT_THREE) {
1272 PyObject *top = TOP();
1273 PyObject *second = SECOND();
1274 PyObject *third = THIRD();
1275 SET_TOP(second);
1276 SET_SECOND(third);
1277 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001279 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001280
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001281 TARGET(DUP_TOP) {
1282 PyObject *top = TOP();
1283 Py_INCREF(top);
1284 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001286 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001287
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001288 TARGET(DUP_TOP_TWO) {
1289 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001290 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001291 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001292 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001293 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001294 SET_TOP(top);
1295 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001296 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001297 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001298
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001299 TARGET(UNARY_POSITIVE) {
1300 PyObject *value = TOP();
1301 PyObject *res = PyNumber_Positive(value);
1302 Py_DECREF(value);
1303 SET_TOP(res);
1304 if (res == NULL)
1305 goto error;
1306 DISPATCH();
1307 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001308
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001309 TARGET(UNARY_NEGATIVE) {
1310 PyObject *value = TOP();
1311 PyObject *res = PyNumber_Negative(value);
1312 Py_DECREF(value);
1313 SET_TOP(res);
1314 if (res == NULL)
1315 goto error;
1316 DISPATCH();
1317 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001318
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001319 TARGET(UNARY_NOT) {
1320 PyObject *value = TOP();
1321 int err = PyObject_IsTrue(value);
1322 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 if (err == 0) {
1324 Py_INCREF(Py_True);
1325 SET_TOP(Py_True);
1326 DISPATCH();
1327 }
1328 else if (err > 0) {
1329 Py_INCREF(Py_False);
1330 SET_TOP(Py_False);
1331 err = 0;
1332 DISPATCH();
1333 }
1334 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001335 goto error;
1336 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001337
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001338 TARGET(UNARY_INVERT) {
1339 PyObject *value = TOP();
1340 PyObject *res = PyNumber_Invert(value);
1341 Py_DECREF(value);
1342 SET_TOP(res);
1343 if (res == NULL)
1344 goto error;
1345 DISPATCH();
1346 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001347
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001348 TARGET(BINARY_POWER) {
1349 PyObject *exp = POP();
1350 PyObject *base = TOP();
1351 PyObject *res = PyNumber_Power(base, exp, Py_None);
1352 Py_DECREF(base);
1353 Py_DECREF(exp);
1354 SET_TOP(res);
1355 if (res == NULL)
1356 goto error;
1357 DISPATCH();
1358 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001359
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001360 TARGET(BINARY_MULTIPLY) {
1361 PyObject *right = POP();
1362 PyObject *left = TOP();
1363 PyObject *res = PyNumber_Multiply(left, right);
1364 Py_DECREF(left);
1365 Py_DECREF(right);
1366 SET_TOP(res);
1367 if (res == NULL)
1368 goto error;
1369 DISPATCH();
1370 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001371
Benjamin Petersond51374e2014-04-09 23:55:56 -04001372 TARGET(BINARY_MATRIX_MULTIPLY) {
1373 PyObject *right = POP();
1374 PyObject *left = TOP();
1375 PyObject *res = PyNumber_MatrixMultiply(left, right);
1376 Py_DECREF(left);
1377 Py_DECREF(right);
1378 SET_TOP(res);
1379 if (res == NULL)
1380 goto error;
1381 DISPATCH();
1382 }
1383
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001384 TARGET(BINARY_TRUE_DIVIDE) {
1385 PyObject *divisor = POP();
1386 PyObject *dividend = TOP();
1387 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1388 Py_DECREF(dividend);
1389 Py_DECREF(divisor);
1390 SET_TOP(quotient);
1391 if (quotient == NULL)
1392 goto error;
1393 DISPATCH();
1394 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001395
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001396 TARGET(BINARY_FLOOR_DIVIDE) {
1397 PyObject *divisor = POP();
1398 PyObject *dividend = TOP();
1399 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1400 Py_DECREF(dividend);
1401 Py_DECREF(divisor);
1402 SET_TOP(quotient);
1403 if (quotient == NULL)
1404 goto error;
1405 DISPATCH();
1406 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001407
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001408 TARGET(BINARY_MODULO) {
1409 PyObject *divisor = POP();
1410 PyObject *dividend = TOP();
1411 PyObject *res = PyUnicode_CheckExact(dividend) ?
1412 PyUnicode_Format(dividend, divisor) :
1413 PyNumber_Remainder(dividend, divisor);
1414 Py_DECREF(divisor);
1415 Py_DECREF(dividend);
1416 SET_TOP(res);
1417 if (res == NULL)
1418 goto error;
1419 DISPATCH();
1420 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001421
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001422 TARGET(BINARY_ADD) {
1423 PyObject *right = POP();
1424 PyObject *left = TOP();
1425 PyObject *sum;
1426 if (PyUnicode_CheckExact(left) &&
1427 PyUnicode_CheckExact(right)) {
1428 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001429 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001430 }
1431 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001432 sum = PyNumber_Add(left, right);
1433 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001434 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001435 Py_DECREF(right);
1436 SET_TOP(sum);
1437 if (sum == NULL)
1438 goto error;
1439 DISPATCH();
1440 }
1441
1442 TARGET(BINARY_SUBTRACT) {
1443 PyObject *right = POP();
1444 PyObject *left = TOP();
1445 PyObject *diff = PyNumber_Subtract(left, right);
1446 Py_DECREF(right);
1447 Py_DECREF(left);
1448 SET_TOP(diff);
1449 if (diff == NULL)
1450 goto error;
1451 DISPATCH();
1452 }
1453
1454 TARGET(BINARY_SUBSCR) {
1455 PyObject *sub = POP();
1456 PyObject *container = TOP();
1457 PyObject *res = PyObject_GetItem(container, sub);
1458 Py_DECREF(container);
1459 Py_DECREF(sub);
1460 SET_TOP(res);
1461 if (res == NULL)
1462 goto error;
1463 DISPATCH();
1464 }
1465
1466 TARGET(BINARY_LSHIFT) {
1467 PyObject *right = POP();
1468 PyObject *left = TOP();
1469 PyObject *res = PyNumber_Lshift(left, right);
1470 Py_DECREF(left);
1471 Py_DECREF(right);
1472 SET_TOP(res);
1473 if (res == NULL)
1474 goto error;
1475 DISPATCH();
1476 }
1477
1478 TARGET(BINARY_RSHIFT) {
1479 PyObject *right = POP();
1480 PyObject *left = TOP();
1481 PyObject *res = PyNumber_Rshift(left, right);
1482 Py_DECREF(left);
1483 Py_DECREF(right);
1484 SET_TOP(res);
1485 if (res == NULL)
1486 goto error;
1487 DISPATCH();
1488 }
1489
1490 TARGET(BINARY_AND) {
1491 PyObject *right = POP();
1492 PyObject *left = TOP();
1493 PyObject *res = PyNumber_And(left, right);
1494 Py_DECREF(left);
1495 Py_DECREF(right);
1496 SET_TOP(res);
1497 if (res == NULL)
1498 goto error;
1499 DISPATCH();
1500 }
1501
1502 TARGET(BINARY_XOR) {
1503 PyObject *right = POP();
1504 PyObject *left = TOP();
1505 PyObject *res = PyNumber_Xor(left, right);
1506 Py_DECREF(left);
1507 Py_DECREF(right);
1508 SET_TOP(res);
1509 if (res == NULL)
1510 goto error;
1511 DISPATCH();
1512 }
1513
1514 TARGET(BINARY_OR) {
1515 PyObject *right = POP();
1516 PyObject *left = TOP();
1517 PyObject *res = PyNumber_Or(left, right);
1518 Py_DECREF(left);
1519 Py_DECREF(right);
1520 SET_TOP(res);
1521 if (res == NULL)
1522 goto error;
1523 DISPATCH();
1524 }
1525
1526 TARGET(LIST_APPEND) {
1527 PyObject *v = POP();
1528 PyObject *list = PEEK(oparg);
1529 int err;
1530 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001531 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001532 if (err != 0)
1533 goto error;
1534 PREDICT(JUMP_ABSOLUTE);
1535 DISPATCH();
1536 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001537
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001538 TARGET(SET_ADD) {
1539 PyObject *v = POP();
1540 PyObject *set = stack_pointer[-oparg];
1541 int err;
1542 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001544 if (err != 0)
1545 goto error;
1546 PREDICT(JUMP_ABSOLUTE);
1547 DISPATCH();
1548 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001549
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001550 TARGET(INPLACE_POWER) {
1551 PyObject *exp = POP();
1552 PyObject *base = TOP();
1553 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1554 Py_DECREF(base);
1555 Py_DECREF(exp);
1556 SET_TOP(res);
1557 if (res == NULL)
1558 goto error;
1559 DISPATCH();
1560 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001561
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001562 TARGET(INPLACE_MULTIPLY) {
1563 PyObject *right = POP();
1564 PyObject *left = TOP();
1565 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1566 Py_DECREF(left);
1567 Py_DECREF(right);
1568 SET_TOP(res);
1569 if (res == NULL)
1570 goto error;
1571 DISPATCH();
1572 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001573
Benjamin Petersond51374e2014-04-09 23:55:56 -04001574 TARGET(INPLACE_MATRIX_MULTIPLY) {
1575 PyObject *right = POP();
1576 PyObject *left = TOP();
1577 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1578 Py_DECREF(left);
1579 Py_DECREF(right);
1580 SET_TOP(res);
1581 if (res == NULL)
1582 goto error;
1583 DISPATCH();
1584 }
1585
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001586 TARGET(INPLACE_TRUE_DIVIDE) {
1587 PyObject *divisor = POP();
1588 PyObject *dividend = TOP();
1589 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1590 Py_DECREF(dividend);
1591 Py_DECREF(divisor);
1592 SET_TOP(quotient);
1593 if (quotient == NULL)
1594 goto error;
1595 DISPATCH();
1596 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001597
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001598 TARGET(INPLACE_FLOOR_DIVIDE) {
1599 PyObject *divisor = POP();
1600 PyObject *dividend = TOP();
1601 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1602 Py_DECREF(dividend);
1603 Py_DECREF(divisor);
1604 SET_TOP(quotient);
1605 if (quotient == NULL)
1606 goto error;
1607 DISPATCH();
1608 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001609
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001610 TARGET(INPLACE_MODULO) {
1611 PyObject *right = POP();
1612 PyObject *left = TOP();
1613 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1614 Py_DECREF(left);
1615 Py_DECREF(right);
1616 SET_TOP(mod);
1617 if (mod == NULL)
1618 goto error;
1619 DISPATCH();
1620 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001621
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001622 TARGET(INPLACE_ADD) {
1623 PyObject *right = POP();
1624 PyObject *left = TOP();
1625 PyObject *sum;
1626 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1627 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001628 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001629 }
1630 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001631 sum = PyNumber_InPlaceAdd(left, right);
1632 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001633 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001634 Py_DECREF(right);
1635 SET_TOP(sum);
1636 if (sum == NULL)
1637 goto error;
1638 DISPATCH();
1639 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001640
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001641 TARGET(INPLACE_SUBTRACT) {
1642 PyObject *right = POP();
1643 PyObject *left = TOP();
1644 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1645 Py_DECREF(left);
1646 Py_DECREF(right);
1647 SET_TOP(diff);
1648 if (diff == NULL)
1649 goto error;
1650 DISPATCH();
1651 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001652
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001653 TARGET(INPLACE_LSHIFT) {
1654 PyObject *right = POP();
1655 PyObject *left = TOP();
1656 PyObject *res = PyNumber_InPlaceLshift(left, right);
1657 Py_DECREF(left);
1658 Py_DECREF(right);
1659 SET_TOP(res);
1660 if (res == NULL)
1661 goto error;
1662 DISPATCH();
1663 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001664
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001665 TARGET(INPLACE_RSHIFT) {
1666 PyObject *right = POP();
1667 PyObject *left = TOP();
1668 PyObject *res = PyNumber_InPlaceRshift(left, right);
1669 Py_DECREF(left);
1670 Py_DECREF(right);
1671 SET_TOP(res);
1672 if (res == NULL)
1673 goto error;
1674 DISPATCH();
1675 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001676
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001677 TARGET(INPLACE_AND) {
1678 PyObject *right = POP();
1679 PyObject *left = TOP();
1680 PyObject *res = PyNumber_InPlaceAnd(left, right);
1681 Py_DECREF(left);
1682 Py_DECREF(right);
1683 SET_TOP(res);
1684 if (res == NULL)
1685 goto error;
1686 DISPATCH();
1687 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001688
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001689 TARGET(INPLACE_XOR) {
1690 PyObject *right = POP();
1691 PyObject *left = TOP();
1692 PyObject *res = PyNumber_InPlaceXor(left, right);
1693 Py_DECREF(left);
1694 Py_DECREF(right);
1695 SET_TOP(res);
1696 if (res == NULL)
1697 goto error;
1698 DISPATCH();
1699 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001700
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001701 TARGET(INPLACE_OR) {
1702 PyObject *right = POP();
1703 PyObject *left = TOP();
1704 PyObject *res = PyNumber_InPlaceOr(left, right);
1705 Py_DECREF(left);
1706 Py_DECREF(right);
1707 SET_TOP(res);
1708 if (res == NULL)
1709 goto error;
1710 DISPATCH();
1711 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001712
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001713 TARGET(STORE_SUBSCR) {
1714 PyObject *sub = TOP();
1715 PyObject *container = SECOND();
1716 PyObject *v = THIRD();
1717 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 STACKADJ(-3);
Martin Panter95f53c12016-07-18 08:23:26 +00001719 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001720 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001722 Py_DECREF(container);
1723 Py_DECREF(sub);
1724 if (err != 0)
1725 goto error;
1726 DISPATCH();
1727 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001728
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001729 TARGET(STORE_ANNOTATION) {
1730 _Py_IDENTIFIER(__annotations__);
1731 PyObject *ann_dict;
1732 PyObject *ann = POP();
1733 PyObject *name = GETITEM(names, oparg);
1734 int err;
1735 if (f->f_locals == NULL) {
1736 PyErr_Format(PyExc_SystemError,
1737 "no locals found when storing annotation");
1738 Py_DECREF(ann);
1739 goto error;
1740 }
1741 /* first try to get __annotations__ from locals... */
1742 if (PyDict_CheckExact(f->f_locals)) {
1743 ann_dict = _PyDict_GetItemId(f->f_locals,
1744 &PyId___annotations__);
1745 if (ann_dict == NULL) {
1746 PyErr_SetString(PyExc_NameError,
1747 "__annotations__ not found");
1748 Py_DECREF(ann);
1749 goto error;
1750 }
1751 Py_INCREF(ann_dict);
1752 }
1753 else {
1754 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
1755 if (ann_str == NULL) {
1756 Py_DECREF(ann);
1757 goto error;
1758 }
1759 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
1760 if (ann_dict == NULL) {
1761 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
1762 PyErr_SetString(PyExc_NameError,
1763 "__annotations__ not found");
1764 }
1765 Py_DECREF(ann);
1766 goto error;
1767 }
1768 }
1769 /* ...if succeeded, __annotations__[name] = ann */
1770 if (PyDict_CheckExact(ann_dict)) {
1771 err = PyDict_SetItem(ann_dict, name, ann);
1772 }
1773 else {
1774 err = PyObject_SetItem(ann_dict, name, ann);
1775 }
1776 Py_DECREF(ann_dict);
Yury Selivanov50c584f2016-09-08 23:38:21 -07001777 Py_DECREF(ann);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001778 if (err != 0) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001779 goto error;
1780 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001781 DISPATCH();
1782 }
1783
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001784 TARGET(DELETE_SUBSCR) {
1785 PyObject *sub = TOP();
1786 PyObject *container = SECOND();
1787 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 STACKADJ(-2);
Martin Panter95f53c12016-07-18 08:23:26 +00001789 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001790 err = PyObject_DelItem(container, sub);
1791 Py_DECREF(container);
1792 Py_DECREF(sub);
1793 if (err != 0)
1794 goto error;
1795 DISPATCH();
1796 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001797
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001798 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001799 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001800 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001801 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001802 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001803 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001804 PyErr_SetString(PyExc_RuntimeError,
1805 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001806 Py_DECREF(value);
1807 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 }
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001809 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001810 Py_DECREF(value);
1811 if (res == NULL)
1812 goto error;
1813 Py_DECREF(res);
1814 DISPATCH();
1815 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001816
Thomas Wouters434d0822000-08-24 20:11:32 +00001817#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001819#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001820 TARGET(RAISE_VARARGS) {
1821 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 switch (oparg) {
1823 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001824 cause = POP(); /* cause */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001826 exc = POP(); /* exc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827 case 0: /* Fallthrough */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001828 if (do_raise(exc, cause)) {
1829 why = WHY_EXCEPTION;
1830 goto fast_block_end;
1831 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 break;
1833 default:
1834 PyErr_SetString(PyExc_SystemError,
1835 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 break;
1837 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001838 goto error;
1839 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001840
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001841 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001842 retval = POP();
1843 why = WHY_RETURN;
1844 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001845 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001846
Yury Selivanov75445082015-05-11 22:57:16 -04001847 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001848 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001849 PyObject *iter = NULL;
1850 PyObject *awaitable = NULL;
1851 PyObject *obj = TOP();
1852 PyTypeObject *type = Py_TYPE(obj);
1853
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001854 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001855 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001856 }
Yury Selivanov75445082015-05-11 22:57:16 -04001857
1858 if (getter != NULL) {
1859 iter = (*getter)(obj);
1860 Py_DECREF(obj);
1861 if (iter == NULL) {
1862 SET_TOP(NULL);
1863 goto error;
1864 }
1865 }
1866 else {
1867 SET_TOP(NULL);
1868 PyErr_Format(
1869 PyExc_TypeError,
1870 "'async for' requires an object with "
1871 "__aiter__ method, got %.100s",
1872 type->tp_name);
1873 Py_DECREF(obj);
1874 goto error;
1875 }
1876
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001877 if (Py_TYPE(iter)->tp_as_async != NULL &&
1878 Py_TYPE(iter)->tp_as_async->am_anext != NULL) {
1879
1880 /* Starting with CPython 3.5.2 __aiter__ should return
1881 asynchronous iterators directly (not awaitables that
1882 resolve to asynchronous iterators.)
1883
1884 Therefore, we check if the object that was returned
1885 from __aiter__ has an __anext__ method. If it does,
1886 we wrap it in an awaitable that resolves to `iter`.
1887
1888 See http://bugs.python.org/issue27243 for more
1889 details.
1890 */
1891
1892 PyObject *wrapper = _PyAIterWrapper_New(iter);
1893 Py_DECREF(iter);
1894 SET_TOP(wrapper);
1895 DISPATCH();
1896 }
1897
Yury Selivanov5376ba92015-06-22 12:19:30 -04001898 awaitable = _PyCoro_GetAwaitableIter(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001899 if (awaitable == NULL) {
1900 SET_TOP(NULL);
1901 PyErr_Format(
1902 PyExc_TypeError,
1903 "'async for' received an invalid object "
1904 "from __aiter__: %.100s",
1905 Py_TYPE(iter)->tp_name);
1906
1907 Py_DECREF(iter);
1908 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001909 } else {
Yury Selivanov75445082015-05-11 22:57:16 -04001910 Py_DECREF(iter);
1911
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001912 if (PyErr_WarnFormat(
1913 PyExc_PendingDeprecationWarning, 1,
1914 "'%.100s' implements legacy __aiter__ protocol; "
1915 "__aiter__ should return an asynchronous "
1916 "iterator, not awaitable",
1917 type->tp_name))
1918 {
1919 /* Warning was converted to an error. */
1920 Py_DECREF(awaitable);
1921 SET_TOP(NULL);
1922 goto error;
1923 }
1924 }
1925
Yury Selivanov75445082015-05-11 22:57:16 -04001926 SET_TOP(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001927 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001928 DISPATCH();
1929 }
1930
1931 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001932 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001933 PyObject *next_iter = NULL;
1934 PyObject *awaitable = NULL;
1935 PyObject *aiter = TOP();
1936 PyTypeObject *type = Py_TYPE(aiter);
1937
Yury Selivanoveb636452016-09-08 22:01:51 -07001938 if (PyAsyncGen_CheckExact(aiter)) {
1939 awaitable = type->tp_as_async->am_anext(aiter);
1940 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001941 goto error;
1942 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001943 } else {
1944 if (type->tp_as_async != NULL){
1945 getter = type->tp_as_async->am_anext;
1946 }
Yury Selivanov75445082015-05-11 22:57:16 -04001947
Yury Selivanoveb636452016-09-08 22:01:51 -07001948 if (getter != NULL) {
1949 next_iter = (*getter)(aiter);
1950 if (next_iter == NULL) {
1951 goto error;
1952 }
1953 }
1954 else {
1955 PyErr_Format(
1956 PyExc_TypeError,
1957 "'async for' requires an iterator with "
1958 "__anext__ method, got %.100s",
1959 type->tp_name);
1960 goto error;
1961 }
Yury Selivanov75445082015-05-11 22:57:16 -04001962
Yury Selivanoveb636452016-09-08 22:01:51 -07001963 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1964 if (awaitable == NULL) {
1965 PyErr_Format(
1966 PyExc_TypeError,
1967 "'async for' received an invalid object "
1968 "from __anext__: %.100s",
1969 Py_TYPE(next_iter)->tp_name);
1970
1971 Py_DECREF(next_iter);
1972 goto error;
1973 } else {
1974 Py_DECREF(next_iter);
1975 }
1976 }
Yury Selivanov75445082015-05-11 22:57:16 -04001977
1978 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001979 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001980 DISPATCH();
1981 }
1982
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001983 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001984 TARGET(GET_AWAITABLE) {
1985 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001986 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001987
1988 Py_DECREF(iterable);
1989
Yury Selivanovc724bae2016-03-02 11:30:46 -05001990 if (iter != NULL && PyCoro_CheckExact(iter)) {
1991 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1992 if (yf != NULL) {
1993 /* `iter` is a coroutine object that is being
1994 awaited, `yf` is a pointer to the current awaitable
1995 being awaited on. */
1996 Py_DECREF(yf);
1997 Py_CLEAR(iter);
1998 PyErr_SetString(
1999 PyExc_RuntimeError,
2000 "coroutine is being awaited already");
2001 /* The code below jumps to `error` if `iter` is NULL. */
2002 }
2003 }
2004
Yury Selivanov75445082015-05-11 22:57:16 -04002005 SET_TOP(iter); /* Even if it's NULL */
2006
2007 if (iter == NULL) {
2008 goto error;
2009 }
2010
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002011 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002012 DISPATCH();
2013 }
2014
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002015 TARGET(YIELD_FROM) {
2016 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002017 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002018 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002019 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2020 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002021 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002022 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002023 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002024 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002025 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002026 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002027 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002028 Py_DECREF(v);
2029 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002030 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002031 if (tstate->c_tracefunc != NULL
2032 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002033 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002034 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002035 if (err < 0)
2036 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002037 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002038 SET_TOP(val);
2039 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002040 }
Martin Panter95f53c12016-07-18 08:23:26 +00002041 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002042 f->f_stacktop = stack_pointer;
2043 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002044 /* and repeat... */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002045 f->f_lasti -= 2;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002046 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002047 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002048
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002049 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002050 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002051
2052 if (co->co_flags & CO_ASYNC_GENERATOR) {
2053 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2054 Py_DECREF(retval);
2055 if (w == NULL) {
2056 retval = NULL;
2057 goto error;
2058 }
2059 retval = w;
2060 }
2061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 f->f_stacktop = stack_pointer;
2063 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002064 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002065 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002066
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002067 TARGET(POP_EXCEPT) {
2068 PyTryBlock *b = PyFrame_BlockPop(f);
2069 if (b->b_type != EXCEPT_HANDLER) {
2070 PyErr_SetString(PyExc_SystemError,
2071 "popped block is not an except handler");
2072 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002074 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002076 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002077
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002078 PREDICTED(POP_BLOCK);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002079 TARGET(POP_BLOCK) {
2080 PyTryBlock *b = PyFrame_BlockPop(f);
2081 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002083 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002086 TARGET(END_FINALLY) {
2087 PyObject *status = POP();
2088 if (PyLong_Check(status)) {
2089 why = (enum why_code) PyLong_AS_LONG(status);
2090 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091 if (why == WHY_RETURN ||
2092 why == WHY_CONTINUE)
2093 retval = POP();
2094 if (why == WHY_SILENCED) {
2095 /* An exception was silenced by 'with', we must
2096 manually unwind the EXCEPT_HANDLER block which was
2097 created when the exception was caught, otherwise
2098 the stack will be in an inconsistent state. */
2099 PyTryBlock *b = PyFrame_BlockPop(f);
2100 assert(b->b_type == EXCEPT_HANDLER);
2101 UNWIND_EXCEPT_HANDLER(b);
2102 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002103 Py_DECREF(status);
2104 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002106 Py_DECREF(status);
2107 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002108 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002109 else if (PyExceptionClass_Check(status)) {
2110 PyObject *exc = POP();
2111 PyObject *tb = POP();
2112 PyErr_Restore(status, exc, tb);
2113 why = WHY_EXCEPTION;
2114 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002115 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002116 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002117 PyErr_SetString(PyExc_SystemError,
2118 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002119 Py_DECREF(status);
2120 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002122 Py_DECREF(status);
2123 DISPATCH();
2124 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002125
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002126 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002127 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002128
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002129 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002130 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002131 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
2132 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002133 PyErr_SetString(PyExc_NameError,
2134 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002135 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002136 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002137 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002138 }
2139 else {
2140 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2141 if (build_class_str == NULL)
2142 break;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002143 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2144 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002145 if (PyErr_ExceptionMatches(PyExc_KeyError))
2146 PyErr_SetString(PyExc_NameError,
2147 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002148 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002149 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002150 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002151 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002152 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002153 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002154
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002155 TARGET(STORE_NAME) {
2156 PyObject *name = GETITEM(names, oparg);
2157 PyObject *v = POP();
2158 PyObject *ns = f->f_locals;
2159 int err;
2160 if (ns == NULL) {
2161 PyErr_Format(PyExc_SystemError,
2162 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002163 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002164 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002166 if (PyDict_CheckExact(ns))
2167 err = PyDict_SetItem(ns, name, v);
2168 else
2169 err = PyObject_SetItem(ns, name, v);
2170 Py_DECREF(v);
2171 if (err != 0)
2172 goto error;
2173 DISPATCH();
2174 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002175
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002176 TARGET(DELETE_NAME) {
2177 PyObject *name = GETITEM(names, oparg);
2178 PyObject *ns = f->f_locals;
2179 int err;
2180 if (ns == NULL) {
2181 PyErr_Format(PyExc_SystemError,
2182 "no locals when deleting %R", name);
2183 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002185 err = PyObject_DelItem(ns, name);
2186 if (err != 0) {
2187 format_exc_check_arg(PyExc_NameError,
2188 NAME_ERROR_MSG,
2189 name);
2190 goto error;
2191 }
2192 DISPATCH();
2193 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002194
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002195 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002196 TARGET(UNPACK_SEQUENCE) {
2197 PyObject *seq = POP(), *item, **items;
2198 if (PyTuple_CheckExact(seq) &&
2199 PyTuple_GET_SIZE(seq) == oparg) {
2200 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002201 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002202 item = items[oparg];
2203 Py_INCREF(item);
2204 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002205 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002206 } else if (PyList_CheckExact(seq) &&
2207 PyList_GET_SIZE(seq) == oparg) {
2208 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002209 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002210 item = items[oparg];
2211 Py_INCREF(item);
2212 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002214 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002215 stack_pointer + oparg)) {
2216 STACKADJ(oparg);
2217 } else {
2218 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002219 Py_DECREF(seq);
2220 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002221 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002222 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002223 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002225
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002226 TARGET(UNPACK_EX) {
2227 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2228 PyObject *seq = POP();
2229
2230 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2231 stack_pointer + totalargs)) {
2232 stack_pointer += totalargs;
2233 } else {
2234 Py_DECREF(seq);
2235 goto error;
2236 }
2237 Py_DECREF(seq);
2238 DISPATCH();
2239 }
2240
2241 TARGET(STORE_ATTR) {
2242 PyObject *name = GETITEM(names, oparg);
2243 PyObject *owner = TOP();
2244 PyObject *v = SECOND();
2245 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002246 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002247 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002248 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002249 Py_DECREF(owner);
2250 if (err != 0)
2251 goto error;
2252 DISPATCH();
2253 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002254
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002255 TARGET(DELETE_ATTR) {
2256 PyObject *name = GETITEM(names, oparg);
2257 PyObject *owner = POP();
2258 int err;
2259 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2260 Py_DECREF(owner);
2261 if (err != 0)
2262 goto error;
2263 DISPATCH();
2264 }
2265
2266 TARGET(STORE_GLOBAL) {
2267 PyObject *name = GETITEM(names, oparg);
2268 PyObject *v = POP();
2269 int err;
2270 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002271 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002272 if (err != 0)
2273 goto error;
2274 DISPATCH();
2275 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002276
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002277 TARGET(DELETE_GLOBAL) {
2278 PyObject *name = GETITEM(names, oparg);
2279 int err;
2280 err = PyDict_DelItem(f->f_globals, name);
2281 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002282 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002283 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002284 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002285 }
2286 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002287 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002288
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002289 TARGET(LOAD_NAME) {
2290 PyObject *name = GETITEM(names, oparg);
2291 PyObject *locals = f->f_locals;
2292 PyObject *v;
2293 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002295 "no locals when loading %R", name);
2296 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002297 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002298 if (PyDict_CheckExact(locals)) {
2299 v = PyDict_GetItem(locals, name);
2300 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 }
2302 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002303 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002304 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002305 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2306 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 PyErr_Clear();
2308 }
2309 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002310 if (v == NULL) {
2311 v = PyDict_GetItem(f->f_globals, name);
2312 Py_XINCREF(v);
2313 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002314 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002315 v = PyDict_GetItem(f->f_builtins, name);
2316 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002317 format_exc_check_arg(
2318 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002319 NAME_ERROR_MSG, name);
2320 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002321 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002322 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002323 }
2324 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002325 v = PyObject_GetItem(f->f_builtins, name);
2326 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002327 if (PyErr_ExceptionMatches(PyExc_KeyError))
2328 format_exc_check_arg(
2329 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002330 NAME_ERROR_MSG, name);
2331 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002332 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002333 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002335 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002336 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002337 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002338 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002339
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002340 TARGET(LOAD_GLOBAL) {
2341 PyObject *name = GETITEM(names, oparg);
2342 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002343 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002344 && PyDict_CheckExact(f->f_builtins))
2345 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002346 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002347 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002348 name);
2349 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002350 if (!_PyErr_OCCURRED()) {
2351 /* _PyDict_LoadGlobal() returns NULL without raising
2352 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002353 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002354 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002355 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002356 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002358 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002360 else {
2361 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002362
2363 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002364 v = PyObject_GetItem(f->f_globals, name);
2365 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002366 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2367 goto error;
2368 PyErr_Clear();
2369
Victor Stinnerb4efc962015-11-20 09:24:02 +01002370 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002371 v = PyObject_GetItem(f->f_builtins, name);
2372 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002373 if (PyErr_ExceptionMatches(PyExc_KeyError))
2374 format_exc_check_arg(
2375 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002376 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002377 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002378 }
2379 }
2380 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002381 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002382 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002383 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002384
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002385 TARGET(DELETE_FAST) {
2386 PyObject *v = GETLOCAL(oparg);
2387 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 SETLOCAL(oparg, NULL);
2389 DISPATCH();
2390 }
2391 format_exc_check_arg(
2392 PyExc_UnboundLocalError,
2393 UNBOUNDLOCAL_ERROR_MSG,
2394 PyTuple_GetItem(co->co_varnames, oparg)
2395 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002396 goto error;
2397 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002398
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002399 TARGET(DELETE_DEREF) {
2400 PyObject *cell = freevars[oparg];
2401 if (PyCell_GET(cell) != NULL) {
2402 PyCell_Set(cell, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002403 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002404 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002405 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002406 goto error;
2407 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002408
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002409 TARGET(LOAD_CLOSURE) {
2410 PyObject *cell = freevars[oparg];
2411 Py_INCREF(cell);
2412 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002414 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002415
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002416 TARGET(LOAD_CLASSDEREF) {
2417 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002418 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002419 assert(locals);
2420 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2421 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2422 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2423 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2424 if (PyDict_CheckExact(locals)) {
2425 value = PyDict_GetItem(locals, name);
2426 Py_XINCREF(value);
2427 }
2428 else {
2429 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002430 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002431 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2432 goto error;
2433 PyErr_Clear();
2434 }
2435 }
2436 if (!value) {
2437 PyObject *cell = freevars[oparg];
2438 value = PyCell_GET(cell);
2439 if (value == NULL) {
2440 format_exc_unbound(co, oparg);
2441 goto error;
2442 }
2443 Py_INCREF(value);
2444 }
2445 PUSH(value);
2446 DISPATCH();
2447 }
2448
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002449 TARGET(LOAD_DEREF) {
2450 PyObject *cell = freevars[oparg];
2451 PyObject *value = PyCell_GET(cell);
2452 if (value == NULL) {
2453 format_exc_unbound(co, oparg);
2454 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002455 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002456 Py_INCREF(value);
2457 PUSH(value);
2458 DISPATCH();
2459 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002460
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002461 TARGET(STORE_DEREF) {
2462 PyObject *v = POP();
2463 PyObject *cell = freevars[oparg];
2464 PyCell_Set(cell, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002465 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002466 DISPATCH();
2467 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002468
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002469 TARGET(BUILD_STRING) {
2470 PyObject *str;
2471 PyObject *empty = PyUnicode_New(0, 0);
2472 if (empty == NULL) {
2473 goto error;
2474 }
2475 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2476 Py_DECREF(empty);
2477 if (str == NULL)
2478 goto error;
2479 while (--oparg >= 0) {
2480 PyObject *item = POP();
2481 Py_DECREF(item);
2482 }
2483 PUSH(str);
2484 DISPATCH();
2485 }
2486
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002487 TARGET(BUILD_TUPLE) {
2488 PyObject *tup = PyTuple_New(oparg);
2489 if (tup == NULL)
2490 goto error;
2491 while (--oparg >= 0) {
2492 PyObject *item = POP();
2493 PyTuple_SET_ITEM(tup, oparg, item);
2494 }
2495 PUSH(tup);
2496 DISPATCH();
2497 }
2498
2499 TARGET(BUILD_LIST) {
2500 PyObject *list = PyList_New(oparg);
2501 if (list == NULL)
2502 goto error;
2503 while (--oparg >= 0) {
2504 PyObject *item = POP();
2505 PyList_SET_ITEM(list, oparg, item);
2506 }
2507 PUSH(list);
2508 DISPATCH();
2509 }
2510
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002511 TARGET(BUILD_TUPLE_UNPACK)
2512 TARGET(BUILD_LIST_UNPACK) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002513 int convert_to_tuple = opcode == BUILD_TUPLE_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002514 Py_ssize_t i;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002515 PyObject *sum;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002516 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002517
2518 if (convert_to_tuple && oparg == 1 && PyTuple_CheckExact(TOP())) {
2519 DISPATCH();
2520 }
2521
2522 sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002523 if (sum == NULL)
2524 goto error;
2525
2526 for (i = oparg; i > 0; i--) {
2527 PyObject *none_val;
2528
2529 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2530 if (none_val == NULL) {
2531 Py_DECREF(sum);
2532 goto error;
2533 }
2534 Py_DECREF(none_val);
2535 }
2536
2537 if (convert_to_tuple) {
2538 return_value = PyList_AsTuple(sum);
2539 Py_DECREF(sum);
2540 if (return_value == NULL)
2541 goto error;
2542 }
2543 else {
2544 return_value = sum;
2545 }
2546
2547 while (oparg--)
2548 Py_DECREF(POP());
2549 PUSH(return_value);
2550 DISPATCH();
2551 }
2552
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002553 TARGET(BUILD_SET) {
2554 PyObject *set = PySet_New(NULL);
2555 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002556 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002557 if (set == NULL)
2558 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002559 for (i = oparg; i > 0; i--) {
2560 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002561 if (err == 0)
2562 err = PySet_Add(set, item);
2563 Py_DECREF(item);
2564 }
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002565 STACKADJ(-oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002566 if (err != 0) {
2567 Py_DECREF(set);
2568 goto error;
2569 }
2570 PUSH(set);
2571 DISPATCH();
2572 }
2573
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002574 TARGET(BUILD_SET_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002575 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002576 PyObject *sum = PySet_New(NULL);
2577 if (sum == NULL)
2578 goto error;
2579
2580 for (i = oparg; i > 0; i--) {
2581 if (_PySet_Update(sum, PEEK(i)) < 0) {
2582 Py_DECREF(sum);
2583 goto error;
2584 }
2585 }
2586
2587 while (oparg--)
2588 Py_DECREF(POP());
2589 PUSH(sum);
2590 DISPATCH();
2591 }
2592
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002593 TARGET(BUILD_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002594 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002595 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2596 if (map == NULL)
2597 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002598 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002599 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002600 PyObject *key = PEEK(2*i);
2601 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002602 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002603 if (err != 0) {
2604 Py_DECREF(map);
2605 goto error;
2606 }
2607 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002608
2609 while (oparg--) {
2610 Py_DECREF(POP());
2611 Py_DECREF(POP());
2612 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002613 PUSH(map);
2614 DISPATCH();
2615 }
2616
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002617 TARGET(SETUP_ANNOTATIONS) {
2618 _Py_IDENTIFIER(__annotations__);
2619 int err;
2620 PyObject *ann_dict;
2621 if (f->f_locals == NULL) {
2622 PyErr_Format(PyExc_SystemError,
2623 "no locals found when setting up annotations");
2624 goto error;
2625 }
2626 /* check if __annotations__ in locals()... */
2627 if (PyDict_CheckExact(f->f_locals)) {
2628 ann_dict = _PyDict_GetItemId(f->f_locals,
2629 &PyId___annotations__);
2630 if (ann_dict == NULL) {
2631 /* ...if not, create a new one */
2632 ann_dict = PyDict_New();
2633 if (ann_dict == NULL) {
2634 goto error;
2635 }
2636 err = _PyDict_SetItemId(f->f_locals,
2637 &PyId___annotations__, ann_dict);
2638 Py_DECREF(ann_dict);
2639 if (err != 0) {
2640 goto error;
2641 }
2642 }
2643 }
2644 else {
2645 /* do the same if locals() is not a dict */
2646 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2647 if (ann_str == NULL) {
2648 break;
2649 }
2650 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2651 if (ann_dict == NULL) {
2652 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2653 goto error;
2654 }
2655 PyErr_Clear();
2656 ann_dict = PyDict_New();
2657 if (ann_dict == NULL) {
2658 goto error;
2659 }
2660 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2661 Py_DECREF(ann_dict);
2662 if (err != 0) {
2663 goto error;
2664 }
2665 }
2666 else {
2667 Py_DECREF(ann_dict);
2668 }
2669 }
2670 DISPATCH();
2671 }
2672
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002673 TARGET(BUILD_CONST_KEY_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002674 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002675 PyObject *map;
2676 PyObject *keys = TOP();
2677 if (!PyTuple_CheckExact(keys) ||
2678 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2679 PyErr_SetString(PyExc_SystemError,
2680 "bad BUILD_CONST_KEY_MAP keys argument");
2681 goto error;
2682 }
2683 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2684 if (map == NULL) {
2685 goto error;
2686 }
2687 for (i = oparg; i > 0; i--) {
2688 int err;
2689 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2690 PyObject *value = PEEK(i + 1);
2691 err = PyDict_SetItem(map, key, value);
2692 if (err != 0) {
2693 Py_DECREF(map);
2694 goto error;
2695 }
2696 }
2697
2698 Py_DECREF(POP());
2699 while (oparg--) {
2700 Py_DECREF(POP());
2701 }
2702 PUSH(map);
2703 DISPATCH();
2704 }
2705
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002706 TARGET(BUILD_MAP_UNPACK_WITH_CALL)
2707 TARGET(BUILD_MAP_UNPACK) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002708 int with_call = opcode == BUILD_MAP_UNPACK_WITH_CALL;
Victor Stinner74319ae2016-08-25 00:04:09 +02002709 Py_ssize_t i;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002710 PyObject *sum;
2711
2712 if (with_call && oparg == 1 && PyDict_CheckExact(TOP())) {
2713 DISPATCH();
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002714 }
2715
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002716 sum = PyDict_New();
2717 if (sum == NULL)
2718 goto error;
2719
2720 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002721 PyObject *arg = PEEK(i);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002722 if (with_call && PyDict_Size(sum)) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002723 PyObject *intersection = _PyDictView_Intersect(sum, arg);
2724
2725 if (intersection == NULL) {
2726 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002727 PyObject *func = PEEK(2 + oparg);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002728 PyErr_Format(PyExc_TypeError,
2729 "%.200s%.200s argument after ** "
2730 "must be a mapping, not %.200s",
2731 PyEval_GetFuncName(func),
2732 PyEval_GetFuncDesc(func),
2733 arg->ob_type->tp_name);
2734 }
2735 Py_DECREF(sum);
2736 goto error;
2737 }
2738
2739 if (PySet_GET_SIZE(intersection)) {
2740 Py_ssize_t idx = 0;
2741 PyObject *key;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002742 PyObject *func = PEEK(2 + oparg);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002743 Py_hash_t hash;
2744 _PySet_NextEntry(intersection, &idx, &key, &hash);
2745 if (!PyUnicode_Check(key)) {
2746 PyErr_Format(PyExc_TypeError,
2747 "%.200s%.200s keywords must be strings",
2748 PyEval_GetFuncName(func),
2749 PyEval_GetFuncDesc(func));
2750 } else {
2751 PyErr_Format(PyExc_TypeError,
2752 "%.200s%.200s got multiple "
2753 "values for keyword argument '%U'",
2754 PyEval_GetFuncName(func),
2755 PyEval_GetFuncDesc(func),
2756 key);
2757 }
2758 Py_DECREF(intersection);
2759 Py_DECREF(sum);
2760 goto error;
2761 }
2762 Py_DECREF(intersection);
2763 }
2764
2765 if (PyDict_Update(sum, arg) < 0) {
2766 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2767 PyErr_Format(PyExc_TypeError,
2768 "'%.200s' object is not a mapping",
2769 arg->ob_type->tp_name);
2770 }
2771 Py_DECREF(sum);
2772 goto error;
2773 }
2774 }
2775
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002776 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002777 Py_DECREF(POP());
2778 PUSH(sum);
2779 DISPATCH();
2780 }
2781
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002782 TARGET(MAP_ADD) {
2783 PyObject *key = TOP();
2784 PyObject *value = SECOND();
2785 PyObject *map;
2786 int err;
2787 STACKADJ(-2);
2788 map = stack_pointer[-oparg]; /* dict */
2789 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002790 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002791 Py_DECREF(value);
2792 Py_DECREF(key);
2793 if (err != 0)
2794 goto error;
2795 PREDICT(JUMP_ABSOLUTE);
2796 DISPATCH();
2797 }
2798
2799 TARGET(LOAD_ATTR) {
2800 PyObject *name = GETITEM(names, oparg);
2801 PyObject *owner = TOP();
2802 PyObject *res = PyObject_GetAttr(owner, name);
2803 Py_DECREF(owner);
2804 SET_TOP(res);
2805 if (res == NULL)
2806 goto error;
2807 DISPATCH();
2808 }
2809
2810 TARGET(COMPARE_OP) {
2811 PyObject *right = POP();
2812 PyObject *left = TOP();
2813 PyObject *res = cmp_outcome(oparg, left, right);
2814 Py_DECREF(left);
2815 Py_DECREF(right);
2816 SET_TOP(res);
2817 if (res == NULL)
2818 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002819 PREDICT(POP_JUMP_IF_FALSE);
2820 PREDICT(POP_JUMP_IF_TRUE);
2821 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002822 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002823
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002824 TARGET(IMPORT_NAME) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002825 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002826 PyObject *fromlist = POP();
2827 PyObject *level = TOP();
2828 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002829 READ_TIMESTAMP(intr0);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002830 res = import_name(f, name, fromlist, level);
2831 Py_DECREF(level);
2832 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002833 READ_TIMESTAMP(intr1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002834 SET_TOP(res);
2835 if (res == NULL)
2836 goto error;
2837 DISPATCH();
2838 }
2839
2840 TARGET(IMPORT_STAR) {
2841 PyObject *from = POP(), *locals;
2842 int err;
Victor Stinner41bb43a2013-10-29 01:19:37 +01002843 if (PyFrame_FastToLocalsWithError(f) < 0)
2844 goto error;
2845
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002846 locals = f->f_locals;
2847 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002848 PyErr_SetString(PyExc_SystemError,
2849 "no locals found during 'import *'");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002850 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002851 }
2852 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002853 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002854 READ_TIMESTAMP(intr1);
2855 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002856 Py_DECREF(from);
2857 if (err != 0)
2858 goto error;
2859 DISPATCH();
2860 }
Guido van Rossum25831651993-05-19 14:50:45 +00002861
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002862 TARGET(IMPORT_FROM) {
2863 PyObject *name = GETITEM(names, oparg);
2864 PyObject *from = TOP();
2865 PyObject *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002866 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002867 res = import_from(from, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 READ_TIMESTAMP(intr1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002869 PUSH(res);
2870 if (res == NULL)
2871 goto error;
2872 DISPATCH();
2873 }
Thomas Wouters52152252000-08-17 22:55:00 +00002874
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002875 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 JUMPBY(oparg);
2877 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002878 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002879
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002880 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002881 TARGET(POP_JUMP_IF_FALSE) {
2882 PyObject *cond = POP();
2883 int err;
2884 if (cond == Py_True) {
2885 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002886 FAST_DISPATCH();
2887 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002888 if (cond == Py_False) {
2889 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002890 JUMPTO(oparg);
2891 FAST_DISPATCH();
2892 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002893 err = PyObject_IsTrue(cond);
2894 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002895 if (err > 0)
2896 err = 0;
2897 else if (err == 0)
2898 JUMPTO(oparg);
2899 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002900 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002902 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002903
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002904 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002905 TARGET(POP_JUMP_IF_TRUE) {
2906 PyObject *cond = POP();
2907 int err;
2908 if (cond == Py_False) {
2909 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002910 FAST_DISPATCH();
2911 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002912 if (cond == Py_True) {
2913 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002914 JUMPTO(oparg);
2915 FAST_DISPATCH();
2916 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002917 err = PyObject_IsTrue(cond);
2918 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002919 if (err > 0) {
2920 err = 0;
2921 JUMPTO(oparg);
2922 }
2923 else if (err == 0)
2924 ;
2925 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002926 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002927 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002928 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002929
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002930 TARGET(JUMP_IF_FALSE_OR_POP) {
2931 PyObject *cond = TOP();
2932 int err;
2933 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002934 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002935 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 FAST_DISPATCH();
2937 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002938 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002939 JUMPTO(oparg);
2940 FAST_DISPATCH();
2941 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002942 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002943 if (err > 0) {
2944 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002945 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 err = 0;
2947 }
2948 else if (err == 0)
2949 JUMPTO(oparg);
2950 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002951 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002952 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002953 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002954
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002955 TARGET(JUMP_IF_TRUE_OR_POP) {
2956 PyObject *cond = TOP();
2957 int err;
2958 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002959 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002960 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002961 FAST_DISPATCH();
2962 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002963 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002964 JUMPTO(oparg);
2965 FAST_DISPATCH();
2966 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002967 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002968 if (err > 0) {
2969 err = 0;
2970 JUMPTO(oparg);
2971 }
2972 else if (err == 0) {
2973 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002974 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002975 }
2976 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002977 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002978 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002979 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002980
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002981 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002982 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002983 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002984#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002985 /* Enabling this path speeds-up all while and for-loops by bypassing
2986 the per-loop checks for signals. By default, this should be turned-off
2987 because it prevents detection of a control-break in tight loops like
2988 "while 1: pass". Compile with this option turned-on when you need
2989 the speed-up and do not need break checking inside tight loops (ones
2990 that contain only instructions ending with FAST_DISPATCH).
2991 */
2992 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002993#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002994 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002995#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002996 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002997
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002998 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002999 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003000 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003001 PyObject *iter = PyObject_GetIter(iterable);
3002 Py_DECREF(iterable);
3003 SET_TOP(iter);
3004 if (iter == NULL)
3005 goto error;
3006 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003007 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003008 DISPATCH();
3009 }
3010
3011 TARGET(GET_YIELD_FROM_ITER) {
3012 /* before: [obj]; after [getiter(obj)] */
3013 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003014 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003015 if (PyCoro_CheckExact(iterable)) {
3016 /* `iterable` is a coroutine */
3017 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3018 /* and it is used in a 'yield from' expression of a
3019 regular generator. */
3020 Py_DECREF(iterable);
3021 SET_TOP(NULL);
3022 PyErr_SetString(PyExc_TypeError,
3023 "cannot 'yield from' a coroutine object "
3024 "in a non-coroutine generator");
3025 goto error;
3026 }
3027 }
3028 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003029 /* `iterable` is not a generator. */
3030 iter = PyObject_GetIter(iterable);
3031 Py_DECREF(iterable);
3032 SET_TOP(iter);
3033 if (iter == NULL)
3034 goto error;
3035 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003036 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003037 DISPATCH();
3038 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003039
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003040 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003041 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003042 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003043 PyObject *iter = TOP();
3044 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3045 if (next != NULL) {
3046 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003047 PREDICT(STORE_FAST);
3048 PREDICT(UNPACK_SEQUENCE);
3049 DISPATCH();
3050 }
3051 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003052 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
3053 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08003054 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003055 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003056 PyErr_Clear();
3057 }
3058 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003059 STACKADJ(-1);
3060 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003061 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003062 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003063 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003064 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003065
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003066 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003067 why = WHY_BREAK;
3068 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003069 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003070
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003071 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003072 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003073 if (retval == NULL)
3074 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003075 why = WHY_CONTINUE;
3076 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003077 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003078
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003079 TARGET(SETUP_LOOP)
3080 TARGET(SETUP_EXCEPT)
3081 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003082 /* NOTE: If you add any new block-setup opcodes that
3083 are not try/except/finally handlers, you may need
3084 to update the PyGen_NeedsFinalizing() function.
3085 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003087 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
3088 STACK_LEVEL());
3089 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003090 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003091
Yury Selivanov75445082015-05-11 22:57:16 -04003092 TARGET(BEFORE_ASYNC_WITH) {
3093 _Py_IDENTIFIER(__aexit__);
3094 _Py_IDENTIFIER(__aenter__);
3095
3096 PyObject *mgr = TOP();
3097 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3098 *enter;
3099 PyObject *res;
3100 if (exit == NULL)
3101 goto error;
3102 SET_TOP(exit);
3103 enter = special_lookup(mgr, &PyId___aenter__);
3104 Py_DECREF(mgr);
3105 if (enter == NULL)
3106 goto error;
3107 res = PyObject_CallFunctionObjArgs(enter, NULL);
3108 Py_DECREF(enter);
3109 if (res == NULL)
3110 goto error;
3111 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003112 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003113 DISPATCH();
3114 }
3115
3116 TARGET(SETUP_ASYNC_WITH) {
3117 PyObject *res = POP();
3118 /* Setup the finally block before pushing the result
3119 of __aenter__ on the stack. */
3120 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3121 STACK_LEVEL());
3122 PUSH(res);
3123 DISPATCH();
3124 }
3125
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003126 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003127 _Py_IDENTIFIER(__exit__);
3128 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003129 PyObject *mgr = TOP();
3130 PyObject *exit = special_lookup(mgr, &PyId___exit__), *enter;
3131 PyObject *res;
3132 if (exit == NULL)
3133 goto error;
3134 SET_TOP(exit);
3135 enter = special_lookup(mgr, &PyId___enter__);
3136 Py_DECREF(mgr);
3137 if (enter == NULL)
3138 goto error;
3139 res = PyObject_CallFunctionObjArgs(enter, NULL);
3140 Py_DECREF(enter);
3141 if (res == NULL)
3142 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003143 /* Setup the finally block before pushing the result
3144 of __enter__ on the stack. */
3145 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3146 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003147
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003148 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003149 DISPATCH();
3150 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003151
Yury Selivanov75445082015-05-11 22:57:16 -04003152 TARGET(WITH_CLEANUP_START) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04003153 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154 how/why we entered the finally clause:
3155 - TOP = None
3156 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
3157 - TOP = WHY_*; no retval below it
3158 - (TOP, SECOND, THIRD) = exc_info()
3159 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
3160 Below them is EXIT, the context.__exit__ bound method.
3161 In the last case, we must call
3162 EXIT(TOP, SECOND, THIRD)
3163 otherwise we must call
3164 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003165
Benjamin Peterson8f169482013-10-29 22:25:06 -04003166 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003167 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04003168 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003169 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003171 In addition, if the stack represents an exception,
3172 *and* the function call returns a 'true' value, we
3173 push WHY_SILENCED onto the stack. END_FINALLY will
3174 then not re-raise the exception. (But non-local
3175 gotos should still be resumed.)
3176 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003178 PyObject *exit_func;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003179 PyObject *exc = TOP(), *val = Py_None, *tb = Py_None, *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003180 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003181 (void)POP();
3182 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003183 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003184 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003185 else if (PyLong_Check(exc)) {
3186 STACKADJ(-1);
3187 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003188 case WHY_RETURN:
3189 case WHY_CONTINUE:
3190 /* Retval in TOP. */
3191 exit_func = SECOND();
3192 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003193 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003194 break;
3195 default:
3196 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003197 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003198 break;
3199 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003200 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003201 }
3202 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003203 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003204 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003205 val = SECOND();
3206 tb = THIRD();
3207 tp2 = FOURTH();
3208 exc2 = PEEK(5);
3209 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003210 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003211 SET_VALUE(7, tb2);
3212 SET_VALUE(6, exc2);
3213 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003214 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3215 SET_FOURTH(NULL);
3216 /* We just shifted the stack down, so we have
3217 to tell the except handler block that the
3218 values are lower than it expects. */
3219 block = &f->f_blockstack[f->f_iblock - 1];
3220 assert(block->b_type == EXCEPT_HANDLER);
3221 block->b_level--;
3222 }
3223 /* XXX Not the fastest way to call it... */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003224 res = PyObject_CallFunctionObjArgs(exit_func, exc, val, tb, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003226 if (res == NULL)
3227 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003228
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003229 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003230 PUSH(exc);
3231 PUSH(res);
3232 PREDICT(WITH_CLEANUP_FINISH);
3233 DISPATCH();
3234 }
3235
3236 PREDICTED(WITH_CLEANUP_FINISH);
3237 TARGET(WITH_CLEANUP_FINISH) {
3238 PyObject *res = POP();
3239 PyObject *exc = POP();
3240 int err;
3241
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003242 if (exc != Py_None)
3243 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003244 else
3245 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003246
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003247 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003248 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003250 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003251 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003252 else if (err > 0) {
3253 err = 0;
3254 /* There was an exception and a True return */
3255 PUSH(PyLong_FromLong((long) WHY_SILENCED));
3256 }
3257 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003258 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003259 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003260
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003261 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003262 TARGET(CALL_FUNCTION) {
3263 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003264 PCALL(PCALL_ALL);
3265 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003266 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003267 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003268 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003269 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003270 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003271 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003272 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003273 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003274
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003275 TARGET(CALL_FUNCTION_KW) {
3276 PyObject **sp, *res, *names;
3277
3278 names = POP();
3279 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003280 PCALL(PCALL_ALL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003281 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003282 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003283 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003284 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003285 Py_DECREF(names);
3286
3287 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003288 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003289 }
3290 DISPATCH();
3291 }
3292
3293 TARGET(CALL_FUNCTION_EX) {
3294 PyObject *func, *callargs, *kwargs = NULL, *result;
3295 PCALL(PCALL_ALL);
3296 if (oparg & 0x01) {
3297 kwargs = POP();
3298 assert(PyDict_CheckExact(kwargs));
3299 }
3300 callargs = POP();
3301 assert(PyTuple_CheckExact(callargs));
3302 func = TOP();
3303
3304 READ_TIMESTAMP(intr0);
3305 result = do_call_core(func, callargs, kwargs);
3306 READ_TIMESTAMP(intr1);
3307 Py_DECREF(func);
3308 Py_DECREF(callargs);
3309 Py_XDECREF(kwargs);
3310
3311 SET_TOP(result);
3312 if (result == NULL) {
3313 goto error;
3314 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003315 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003316 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003317
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003318 TARGET(MAKE_FUNCTION) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003319 PyObject *qualname = POP();
3320 PyObject *codeobj = POP();
3321 PyFunctionObject *func = (PyFunctionObject *)
3322 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003323
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003324 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003325 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003326 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003327 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003328 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003329
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003330 if (oparg & 0x08) {
3331 assert(PyTuple_CheckExact(TOP()));
3332 func ->func_closure = POP();
3333 }
3334 if (oparg & 0x04) {
3335 assert(PyDict_CheckExact(TOP()));
3336 func->func_annotations = POP();
3337 }
3338 if (oparg & 0x02) {
3339 assert(PyDict_CheckExact(TOP()));
3340 func->func_kwdefaults = POP();
3341 }
3342 if (oparg & 0x01) {
3343 assert(PyTuple_CheckExact(TOP()));
3344 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003345 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003346
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003347 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003348 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003349 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003350
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003351 TARGET(BUILD_SLICE) {
3352 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003353 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003354 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003355 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003356 step = NULL;
3357 stop = POP();
3358 start = TOP();
3359 slice = PySlice_New(start, stop, step);
3360 Py_DECREF(start);
3361 Py_DECREF(stop);
3362 Py_XDECREF(step);
3363 SET_TOP(slice);
3364 if (slice == NULL)
3365 goto error;
3366 DISPATCH();
3367 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003368
Eric V. Smitha78c7952015-11-03 12:45:05 -05003369 TARGET(FORMAT_VALUE) {
3370 /* Handles f-string value formatting. */
3371 PyObject *result;
3372 PyObject *fmt_spec;
3373 PyObject *value;
3374 PyObject *(*conv_fn)(PyObject *);
3375 int which_conversion = oparg & FVC_MASK;
3376 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3377
3378 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003379 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003380
3381 /* See if any conversion is specified. */
3382 switch (which_conversion) {
3383 case FVC_STR: conv_fn = PyObject_Str; break;
3384 case FVC_REPR: conv_fn = PyObject_Repr; break;
3385 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3386
3387 /* Must be 0 (meaning no conversion), since only four
3388 values are allowed by (oparg & FVC_MASK). */
3389 default: conv_fn = NULL; break;
3390 }
3391
3392 /* If there's a conversion function, call it and replace
3393 value with that result. Otherwise, just use value,
3394 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003395 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003396 result = conv_fn(value);
3397 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003398 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003399 Py_XDECREF(fmt_spec);
3400 goto error;
3401 }
3402 value = result;
3403 }
3404
3405 /* If value is a unicode object, and there's no fmt_spec,
3406 then we know the result of format(value) is value
3407 itself. In that case, skip calling format(). I plan to
3408 move this optimization in to PyObject_Format()
3409 itself. */
3410 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3411 /* Do nothing, just transfer ownership to result. */
3412 result = value;
3413 } else {
3414 /* Actually call format(). */
3415 result = PyObject_Format(value, fmt_spec);
3416 Py_DECREF(value);
3417 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003418 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003419 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003420 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003421 }
3422
Eric V. Smith135d5f42016-02-05 18:23:08 -05003423 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003424 DISPATCH();
3425 }
3426
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003427 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003428 int oldoparg = oparg;
3429 NEXTOPARG();
3430 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003431 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003432 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003433
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003434
Antoine Pitrou042b1282010-08-13 21:15:58 +00003435#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003436 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003437#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 default:
3439 fprintf(stderr,
3440 "XXX lineno: %d, opcode: %d\n",
3441 PyFrame_GetLineNumber(f),
3442 opcode);
3443 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003444 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003445
3446#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003447 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003448#endif
3449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003450 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003451
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003452 /* This should never be reached. Every opcode should end with DISPATCH()
3453 or goto error. */
3454 assert(0);
Guido van Rossumac7be682001-01-17 15:42:30 +00003455
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003456error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003457 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003458
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003459 assert(why == WHY_NOT);
3460 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003461
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003462 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003463#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003464 if (!PyErr_Occurred())
3465 PyErr_SetString(PyExc_SystemError,
3466 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003467#else
3468 assert(PyErr_Occurred());
3469#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003470
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003471 /* Log traceback info. */
3472 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003473
Benjamin Peterson51f46162013-01-23 08:38:47 -05003474 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003475 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3476 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003477
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003478fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003479 assert(why != WHY_NOT);
3480
3481 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003482 while (why != WHY_NOT && f->f_iblock > 0) {
3483 /* Peek at the current block. */
3484 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003486 assert(why != WHY_YIELD);
3487 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3488 why = WHY_NOT;
3489 JUMPTO(PyLong_AS_LONG(retval));
3490 Py_DECREF(retval);
3491 break;
3492 }
3493 /* Now we have to pop the block. */
3494 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003496 if (b->b_type == EXCEPT_HANDLER) {
3497 UNWIND_EXCEPT_HANDLER(b);
3498 continue;
3499 }
3500 UNWIND_BLOCK(b);
3501 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3502 why = WHY_NOT;
3503 JUMPTO(b->b_handler);
3504 break;
3505 }
3506 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3507 || b->b_type == SETUP_FINALLY)) {
3508 PyObject *exc, *val, *tb;
3509 int handler = b->b_handler;
3510 /* Beware, this invalidates all b->b_* fields */
3511 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3512 PUSH(tstate->exc_traceback);
3513 PUSH(tstate->exc_value);
3514 if (tstate->exc_type != NULL) {
3515 PUSH(tstate->exc_type);
3516 }
3517 else {
3518 Py_INCREF(Py_None);
3519 PUSH(Py_None);
3520 }
3521 PyErr_Fetch(&exc, &val, &tb);
3522 /* Make the raw exception data
3523 available to the handler,
3524 so a program can emulate the
3525 Python main loop. */
3526 PyErr_NormalizeException(
3527 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003528 if (tb != NULL)
3529 PyException_SetTraceback(val, tb);
3530 else
3531 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003532 Py_INCREF(exc);
3533 tstate->exc_type = exc;
3534 Py_INCREF(val);
3535 tstate->exc_value = val;
3536 tstate->exc_traceback = tb;
3537 if (tb == NULL)
3538 tb = Py_None;
3539 Py_INCREF(tb);
3540 PUSH(tb);
3541 PUSH(val);
3542 PUSH(exc);
3543 why = WHY_NOT;
3544 JUMPTO(handler);
3545 break;
3546 }
3547 if (b->b_type == SETUP_FINALLY) {
3548 if (why & (WHY_RETURN | WHY_CONTINUE))
3549 PUSH(retval);
3550 PUSH(PyLong_FromLong((long)why));
3551 why = WHY_NOT;
3552 JUMPTO(b->b_handler);
3553 break;
3554 }
3555 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003557 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003559 if (why != WHY_NOT)
3560 break;
3561 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003562
Victor Stinnerace47d72013-07-18 01:41:08 +02003563 assert(!PyErr_Occurred());
3564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003565 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003567 assert(why != WHY_YIELD);
3568 /* Pop remaining stack entries. */
3569 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003570 PyObject *o = POP();
3571 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003572 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003574 if (why != WHY_RETURN)
3575 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003576
Victor Stinner4a7cc882015-03-06 23:35:27 +01003577 assert((retval != NULL) ^ (PyErr_Occurred() != NULL));
Victor Stinnerace47d72013-07-18 01:41:08 +02003578
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003579fast_yield:
Yury Selivanoveb636452016-09-08 22:01:51 -07003580 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01003581
Benjamin Petersonac913412011-07-03 16:25:11 -05003582 /* The purpose of this block is to put aside the generator's exception
3583 state and restore that of the calling frame. If the current
3584 exception state is from the caller, we clear the exception values
3585 on the generator frame, so they are not swapped back in latter. The
3586 origin of the current exception state is determined by checking for
3587 except handler blocks, which we must be in iff a new exception
3588 state came into existence in this frame. (An uncaught exception
3589 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3590 int i;
Victor Stinner74319ae2016-08-25 00:04:09 +02003591 for (i = 0; i < f->f_iblock; i++) {
3592 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER) {
Benjamin Petersonac913412011-07-03 16:25:11 -05003593 break;
Victor Stinner74319ae2016-08-25 00:04:09 +02003594 }
3595 }
Benjamin Petersonac913412011-07-03 16:25:11 -05003596 if (i == f->f_iblock)
3597 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003598 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003599 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003600 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003601 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003603 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003604 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003605 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003606 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3607 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003608 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003609 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003610 why = WHY_EXCEPTION;
3611 }
3612 }
3613 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003614 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3615 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003616 PyTrace_RETURN, NULL);
3617 }
3618 }
3619 if (tstate->c_profilefunc) {
3620 if (why == WHY_EXCEPTION)
3621 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003622 tstate->c_profileobj,
3623 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003624 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003625 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3626 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003627 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003628 Py_CLEAR(retval);
Brett Cannonb94767f2011-02-22 20:15:44 +00003629 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003630 }
3631 }
3632 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003634 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003635exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003636 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003637 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003638 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003639
Victor Stinnerefde1462015-03-21 15:04:43 +01003640 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003641}
3642
Benjamin Petersonb204a422011-06-05 22:04:07 -05003643static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003644format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3645{
3646 int err;
3647 Py_ssize_t len = PyList_GET_SIZE(names);
3648 PyObject *name_str, *comma, *tail, *tmp;
3649
3650 assert(PyList_CheckExact(names));
3651 assert(len >= 1);
3652 /* Deal with the joys of natural language. */
3653 switch (len) {
3654 case 1:
3655 name_str = PyList_GET_ITEM(names, 0);
3656 Py_INCREF(name_str);
3657 break;
3658 case 2:
3659 name_str = PyUnicode_FromFormat("%U and %U",
3660 PyList_GET_ITEM(names, len - 2),
3661 PyList_GET_ITEM(names, len - 1));
3662 break;
3663 default:
3664 tail = PyUnicode_FromFormat(", %U, and %U",
3665 PyList_GET_ITEM(names, len - 2),
3666 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003667 if (tail == NULL)
3668 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003669 /* Chop off the last two objects in the list. This shouldn't actually
3670 fail, but we can't be too careful. */
3671 err = PyList_SetSlice(names, len - 2, len, NULL);
3672 if (err == -1) {
3673 Py_DECREF(tail);
3674 return;
3675 }
3676 /* Stitch everything up into a nice comma-separated list. */
3677 comma = PyUnicode_FromString(", ");
3678 if (comma == NULL) {
3679 Py_DECREF(tail);
3680 return;
3681 }
3682 tmp = PyUnicode_Join(comma, names);
3683 Py_DECREF(comma);
3684 if (tmp == NULL) {
3685 Py_DECREF(tail);
3686 return;
3687 }
3688 name_str = PyUnicode_Concat(tmp, tail);
3689 Py_DECREF(tmp);
3690 Py_DECREF(tail);
3691 break;
3692 }
3693 if (name_str == NULL)
3694 return;
3695 PyErr_Format(PyExc_TypeError,
3696 "%U() missing %i required %s argument%s: %U",
3697 co->co_name,
3698 len,
3699 kind,
3700 len == 1 ? "" : "s",
3701 name_str);
3702 Py_DECREF(name_str);
3703}
3704
3705static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003706missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003707 PyObject **fastlocals)
3708{
Victor Stinner74319ae2016-08-25 00:04:09 +02003709 Py_ssize_t i, j = 0;
3710 Py_ssize_t start, end;
3711 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003712 const char *kind = positional ? "positional" : "keyword-only";
3713 PyObject *missing_names;
3714
3715 /* Compute the names of the arguments that are missing. */
3716 missing_names = PyList_New(missing);
3717 if (missing_names == NULL)
3718 return;
3719 if (positional) {
3720 start = 0;
3721 end = co->co_argcount - defcount;
3722 }
3723 else {
3724 start = co->co_argcount;
3725 end = start + co->co_kwonlyargcount;
3726 }
3727 for (i = start; i < end; i++) {
3728 if (GETLOCAL(i) == NULL) {
3729 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3730 PyObject *name = PyObject_Repr(raw);
3731 if (name == NULL) {
3732 Py_DECREF(missing_names);
3733 return;
3734 }
3735 PyList_SET_ITEM(missing_names, j++, name);
3736 }
3737 }
3738 assert(j == missing);
3739 format_missing(kind, co, missing_names);
3740 Py_DECREF(missing_names);
3741}
3742
3743static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003744too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3745 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003746{
3747 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003748 Py_ssize_t kwonly_given = 0;
3749 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003750 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003751 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003752
Benjamin Petersone109c702011-06-24 09:37:26 -05003753 assert((co->co_flags & CO_VARARGS) == 0);
3754 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003755 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3756 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003757 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003758 }
3759 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003760 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003761 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003762 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003763 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003764 }
3765 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003766 plural = (co_argcount != 1);
3767 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003768 }
3769 if (sig == NULL)
3770 return;
3771 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003772 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3773 kwonly_sig = PyUnicode_FromFormat(format,
3774 given != 1 ? "s" : "",
3775 kwonly_given,
3776 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003777 if (kwonly_sig == NULL) {
3778 Py_DECREF(sig);
3779 return;
3780 }
3781 }
3782 else {
3783 /* This will not fail. */
3784 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003785 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003786 }
3787 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003788 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003789 co->co_name,
3790 sig,
3791 plural ? "s" : "",
3792 given,
3793 kwonly_sig,
3794 given == 1 && !kwonly_given ? "was" : "were");
3795 Py_DECREF(sig);
3796 Py_DECREF(kwonly_sig);
3797}
3798
Guido van Rossumc2e20742006-02-27 22:32:47 +00003799/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003800 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003801 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003802
Victor Stinner40ee3012014-06-16 15:59:28 +02003803static PyObject *
3804_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Victor Stinner74319ae2016-08-25 00:04:09 +02003805 PyObject **args, Py_ssize_t argcount,
3806 PyObject **kws, Py_ssize_t kwcount,
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003807 PyObject *kwnames, PyObject **kwstack,
Victor Stinner74319ae2016-08-25 00:04:09 +02003808 PyObject **defs, Py_ssize_t defcount,
3809 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003810 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003811{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003812 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003813 PyFrameObject *f;
3814 PyObject *retval = NULL;
3815 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003816 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003817 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003818 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3819 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003820 PyObject *kwdict;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003821 Py_ssize_t kwcount2 = kwnames == NULL ? 0 : PyTuple_GET_SIZE(kwnames);
Victor Stinnerc7020012016-08-16 23:40:29 +02003822
3823 assert((kwcount == 0) || (kws != NULL));
Tim Peters5ca576e2001-06-18 22:08:13 +00003824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003825 if (globals == NULL) {
3826 PyErr_SetString(PyExc_SystemError,
3827 "PyEval_EvalCodeEx: NULL globals");
3828 return NULL;
3829 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003830
Victor Stinnerc7020012016-08-16 23:40:29 +02003831 /* Create the frame */
3832 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003833 assert(tstate != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003834 f = PyFrame_New(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003835 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003836 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003837 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003838 fastlocals = f->f_localsplus;
3839 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003840
Victor Stinnerc7020012016-08-16 23:40:29 +02003841 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003842 if (co->co_flags & CO_VARKEYWORDS) {
3843 kwdict = PyDict_New();
3844 if (kwdict == NULL)
3845 goto fail;
3846 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003847 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003848 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003849 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003850 SETLOCAL(i, kwdict);
3851 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003852 else {
3853 kwdict = NULL;
3854 }
3855
3856 /* Copy positional arguments into local variables */
3857 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003858 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003859 }
3860 else {
3861 n = argcount;
3862 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003863 for (i = 0; i < n; i++) {
3864 x = args[i];
3865 Py_INCREF(x);
3866 SETLOCAL(i, x);
3867 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003868
3869 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003870 if (co->co_flags & CO_VARARGS) {
3871 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003872 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003873 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003874 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003875 SETLOCAL(total_args, u);
3876 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003877 x = args[i];
3878 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003879 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003880 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003881 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003882
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003883 /* Handle keyword arguments passed as an array of (key, value) pairs */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003884 for (i = 0; i < kwcount; i++) {
3885 PyObject **co_varnames;
3886 PyObject *keyword = kws[2*i];
3887 PyObject *value = kws[2*i + 1];
Victor Stinner17061a92016-08-16 23:39:42 +02003888 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003889
Benjamin Petersonb204a422011-06-05 22:04:07 -05003890 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3891 PyErr_Format(PyExc_TypeError,
3892 "%U() keywords must be strings",
3893 co->co_name);
3894 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003895 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003896
Benjamin Petersonb204a422011-06-05 22:04:07 -05003897 /* Speed hack: do raw pointer compares. As names are
3898 normally interned this should almost always hit. */
3899 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3900 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003901 PyObject *name = co_varnames[j];
3902 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003903 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003904 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003905 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003906
Benjamin Petersonb204a422011-06-05 22:04:07 -05003907 /* Slow fallback, just in case */
3908 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003909 PyObject *name = co_varnames[j];
3910 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3911 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003912 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003913 }
3914 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003915 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003916 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003917 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003918
Benjamin Petersonb204a422011-06-05 22:04:07 -05003919 if (j >= total_args && kwdict == NULL) {
3920 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003921 "%U() got an unexpected keyword 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 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003925
Christian Heimes0bd447f2013-07-20 14:48:10 +02003926 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3927 goto fail;
3928 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003929 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003930
Benjamin Petersonb204a422011-06-05 22:04:07 -05003931 kw_found:
3932 if (GETLOCAL(j) != NULL) {
3933 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003934 "%U() got multiple values for argument '%S'",
3935 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003936 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003937 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003938 Py_INCREF(value);
3939 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003940 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003941
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003942 /* Handle keyword arguments passed as keys tuple + values array */
3943 for (i = 0; i < kwcount2; i++) {
3944 PyObject **co_varnames;
3945 PyObject *keyword = PyTuple_GET_ITEM(kwnames, i);
3946 PyObject *value = kwstack[i];
3947 int j;
3948 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3949 PyErr_Format(PyExc_TypeError,
3950 "%U() keywords must be strings",
3951 co->co_name);
3952 goto fail;
3953 }
3954 /* Speed hack: do raw pointer compares. As names are
3955 normally interned this should almost always hit. */
3956 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3957 for (j = 0; j < total_args; j++) {
3958 PyObject *nm = co_varnames[j];
3959 if (nm == keyword)
3960 goto kw_found2;
3961 }
3962 /* Slow fallback, just in case */
3963 for (j = 0; j < total_args; j++) {
3964 PyObject *nm = co_varnames[j];
3965 int cmp = PyObject_RichCompareBool(
3966 keyword, nm, Py_EQ);
3967 if (cmp > 0)
3968 goto kw_found2;
3969 else if (cmp < 0)
3970 goto fail;
3971 }
3972 if (j >= total_args && kwdict == NULL) {
3973 PyErr_Format(PyExc_TypeError,
3974 "%U() got an unexpected "
3975 "keyword argument '%S'",
3976 co->co_name,
3977 keyword);
3978 goto fail;
3979 }
3980 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3981 goto fail;
3982 }
3983 continue;
3984 kw_found2:
3985 if (GETLOCAL(j) != NULL) {
3986 PyErr_Format(PyExc_TypeError,
3987 "%U() got multiple "
3988 "values for argument '%S'",
3989 co->co_name,
3990 keyword);
3991 goto fail;
3992 }
3993 Py_INCREF(value);
3994 SETLOCAL(j, value);
3995 }
3996
Victor Stinnerc7020012016-08-16 23:40:29 +02003997 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003998 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003999 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004000 goto fail;
4001 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004002
4003 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004004 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02004005 Py_ssize_t m = co->co_argcount - defcount;
4006 Py_ssize_t missing = 0;
4007 for (i = argcount; i < m; i++) {
4008 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004009 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004010 }
4011 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004012 if (missing) {
4013 missing_arguments(co, missing, defcount, fastlocals);
4014 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004015 }
4016 if (n > m)
4017 i = n - m;
4018 else
4019 i = 0;
4020 for (; i < defcount; i++) {
4021 if (GETLOCAL(m+i) == NULL) {
4022 PyObject *def = defs[i];
4023 Py_INCREF(def);
4024 SETLOCAL(m+i, def);
4025 }
4026 }
4027 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004028
4029 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004030 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004031 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004032 for (i = co->co_argcount; i < total_args; i++) {
4033 PyObject *name;
4034 if (GETLOCAL(i) != NULL)
4035 continue;
4036 name = PyTuple_GET_ITEM(co->co_varnames, i);
4037 if (kwdefs != NULL) {
4038 PyObject *def = PyDict_GetItem(kwdefs, name);
4039 if (def) {
4040 Py_INCREF(def);
4041 SETLOCAL(i, def);
4042 continue;
4043 }
4044 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004045 missing++;
4046 }
4047 if (missing) {
4048 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004049 goto fail;
4050 }
4051 }
4052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004053 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004054 vars into frame. */
4055 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004056 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05004057 int arg;
4058 /* Possibly account for the cell variable being an argument. */
4059 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004060 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004061 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004062 /* Clear the local copy. */
4063 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004064 }
4065 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004066 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004067 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004068 if (c == NULL)
4069 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004070 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004071 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004072
4073 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004074 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4075 PyObject *o = PyTuple_GET_ITEM(closure, i);
4076 Py_INCREF(o);
4077 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004078 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004079
Yury Selivanoveb636452016-09-08 22:01:51 -07004080 /* Handle generator/coroutine/asynchronous generator */
4081 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004082 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04004083 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004084 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004085
4086 if (is_coro && tstate->in_coroutine_wrapper) {
4087 assert(coro_wrapper != NULL);
4088 PyErr_Format(PyExc_RuntimeError,
4089 "coroutine wrapper %.200R attempted "
4090 "to recursively wrap %.200R",
4091 coro_wrapper,
4092 co);
4093 goto fail;
4094 }
Yury Selivanov75445082015-05-11 22:57:16 -04004095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004096 /* Don't need to keep the reference to f_back, it will be set
4097 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004098 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004100 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004102 /* Create a new generator that owns the ready to run frame
4103 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004104 if (is_coro) {
4105 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004106 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4107 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004108 } else {
4109 gen = PyGen_NewWithQualName(f, name, qualname);
4110 }
Yury Selivanov75445082015-05-11 22:57:16 -04004111 if (gen == NULL)
4112 return NULL;
4113
Yury Selivanov94c22632015-06-04 10:16:51 -04004114 if (is_coro && coro_wrapper != NULL) {
4115 PyObject *wrapped;
4116 tstate->in_coroutine_wrapper = 1;
4117 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4118 tstate->in_coroutine_wrapper = 0;
4119 return wrapped;
4120 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004121
Yury Selivanov75445082015-05-11 22:57:16 -04004122 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004123 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004125 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004126
Thomas Woutersce272b62007-09-19 21:19:28 +00004127fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004129 /* decref'ing the frame can cause __del__ methods to get invoked,
4130 which can call back into Python. While we're done with the
4131 current Python frame (f), the associated C stack is still in use,
4132 so recursion_depth must be boosted for the duration.
4133 */
4134 assert(tstate != NULL);
4135 ++tstate->recursion_depth;
4136 Py_DECREF(f);
4137 --tstate->recursion_depth;
4138 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004139}
4140
Victor Stinner40ee3012014-06-16 15:59:28 +02004141PyObject *
4142PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
4143 PyObject **args, int argcount, PyObject **kws, int kwcount,
4144 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
4145{
4146 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004147 args, argcount,
4148 kws, kwcount,
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004149 NULL, NULL,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004150 defs, defcount,
4151 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004152 NULL, NULL);
4153}
Tim Peters5ca576e2001-06-18 22:08:13 +00004154
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004155static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004156special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004157{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004158 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004159 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004160 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004161 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004162 return NULL;
4163 }
4164 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004165}
4166
4167
Benjamin Peterson87880242011-07-03 16:48:31 -05004168/* These 3 functions deal with the exception state of generators. */
4169
4170static void
4171save_exc_state(PyThreadState *tstate, PyFrameObject *f)
4172{
4173 PyObject *type, *value, *traceback;
4174 Py_XINCREF(tstate->exc_type);
4175 Py_XINCREF(tstate->exc_value);
4176 Py_XINCREF(tstate->exc_traceback);
4177 type = f->f_exc_type;
4178 value = f->f_exc_value;
4179 traceback = f->f_exc_traceback;
4180 f->f_exc_type = tstate->exc_type;
4181 f->f_exc_value = tstate->exc_value;
4182 f->f_exc_traceback = tstate->exc_traceback;
4183 Py_XDECREF(type);
4184 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004185 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05004186}
4187
4188static void
4189swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
4190{
4191 PyObject *tmp;
4192 tmp = tstate->exc_type;
4193 tstate->exc_type = f->f_exc_type;
4194 f->f_exc_type = tmp;
4195 tmp = tstate->exc_value;
4196 tstate->exc_value = f->f_exc_value;
4197 f->f_exc_value = tmp;
4198 tmp = tstate->exc_traceback;
4199 tstate->exc_traceback = f->f_exc_traceback;
4200 f->f_exc_traceback = tmp;
4201}
4202
4203static void
4204restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
4205{
4206 PyObject *type, *value, *tb;
4207 type = tstate->exc_type;
4208 value = tstate->exc_value;
4209 tb = tstate->exc_traceback;
4210 tstate->exc_type = f->f_exc_type;
4211 tstate->exc_value = f->f_exc_value;
4212 tstate->exc_traceback = f->f_exc_traceback;
4213 f->f_exc_type = NULL;
4214 f->f_exc_value = NULL;
4215 f->f_exc_traceback = NULL;
4216 Py_XDECREF(type);
4217 Py_XDECREF(value);
4218 Py_XDECREF(tb);
4219}
4220
4221
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004222/* Logic for the raise statement (too complicated for inlining).
4223 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004224static int
Collin Winter828f04a2007-08-31 00:04:24 +00004225do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004226{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004227 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004229 if (exc == NULL) {
4230 /* Reraise */
4231 PyThreadState *tstate = PyThreadState_GET();
4232 PyObject *tb;
4233 type = tstate->exc_type;
4234 value = tstate->exc_value;
4235 tb = tstate->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004236 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004237 PyErr_SetString(PyExc_RuntimeError,
4238 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004239 return 0;
4240 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004241 Py_XINCREF(type);
4242 Py_XINCREF(value);
4243 Py_XINCREF(tb);
4244 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004245 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004246 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004248 /* We support the following forms of raise:
4249 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004250 raise <instance>
4251 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004253 if (PyExceptionClass_Check(exc)) {
4254 type = exc;
4255 value = PyObject_CallObject(exc, NULL);
4256 if (value == NULL)
4257 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004258 if (!PyExceptionInstance_Check(value)) {
4259 PyErr_Format(PyExc_TypeError,
4260 "calling %R should have returned an instance of "
4261 "BaseException, not %R",
4262 type, Py_TYPE(value));
4263 goto raise_error;
4264 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004265 }
4266 else if (PyExceptionInstance_Check(exc)) {
4267 value = exc;
4268 type = PyExceptionInstance_Class(exc);
4269 Py_INCREF(type);
4270 }
4271 else {
4272 /* Not something you can raise. You get an exception
4273 anyway, just not what you specified :-) */
4274 Py_DECREF(exc);
4275 PyErr_SetString(PyExc_TypeError,
4276 "exceptions must derive from BaseException");
4277 goto raise_error;
4278 }
Collin Winter828f04a2007-08-31 00:04:24 +00004279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004280 if (cause) {
4281 PyObject *fixed_cause;
4282 if (PyExceptionClass_Check(cause)) {
4283 fixed_cause = PyObject_CallObject(cause, NULL);
4284 if (fixed_cause == NULL)
4285 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004286 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004287 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004288 else if (PyExceptionInstance_Check(cause)) {
4289 fixed_cause = cause;
4290 }
4291 else if (cause == Py_None) {
4292 Py_DECREF(cause);
4293 fixed_cause = NULL;
4294 }
4295 else {
4296 PyErr_SetString(PyExc_TypeError,
4297 "exception causes must derive from "
4298 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004299 goto raise_error;
4300 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004301 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004302 }
Collin Winter828f04a2007-08-31 00:04:24 +00004303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004304 PyErr_SetObject(type, value);
4305 /* PyErr_SetObject incref's its arguments */
4306 Py_XDECREF(value);
4307 Py_XDECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004308 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004309
4310raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004311 Py_XDECREF(value);
4312 Py_XDECREF(type);
4313 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004314 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004315}
4316
Tim Petersd6d010b2001-06-21 02:49:55 +00004317/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004318 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004319
Guido van Rossum0368b722007-05-11 16:50:42 +00004320 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4321 with a variable target.
4322*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004323
Barry Warsawe42b18f1997-08-25 22:13:04 +00004324static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004325unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004327 int i = 0, j = 0;
4328 Py_ssize_t ll = 0;
4329 PyObject *it; /* iter(v) */
4330 PyObject *w;
4331 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004333 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004335 it = PyObject_GetIter(v);
4336 if (it == NULL)
4337 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00004338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004339 for (; i < argcnt; i++) {
4340 w = PyIter_Next(it);
4341 if (w == NULL) {
4342 /* Iterator done, via error or exhaustion. */
4343 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004344 if (argcntafter == -1) {
4345 PyErr_Format(PyExc_ValueError,
4346 "not enough values to unpack (expected %d, got %d)",
4347 argcnt, i);
4348 }
4349 else {
4350 PyErr_Format(PyExc_ValueError,
4351 "not enough values to unpack "
4352 "(expected at least %d, got %d)",
4353 argcnt + argcntafter, i);
4354 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004355 }
4356 goto Error;
4357 }
4358 *--sp = w;
4359 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004361 if (argcntafter == -1) {
4362 /* We better have exhausted the iterator now. */
4363 w = PyIter_Next(it);
4364 if (w == NULL) {
4365 if (PyErr_Occurred())
4366 goto Error;
4367 Py_DECREF(it);
4368 return 1;
4369 }
4370 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004371 PyErr_Format(PyExc_ValueError,
4372 "too many values to unpack (expected %d)",
4373 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004374 goto Error;
4375 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004377 l = PySequence_List(it);
4378 if (l == NULL)
4379 goto Error;
4380 *--sp = l;
4381 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004383 ll = PyList_GET_SIZE(l);
4384 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004385 PyErr_Format(PyExc_ValueError,
4386 "not enough values to unpack (expected at least %d, got %zd)",
4387 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004388 goto Error;
4389 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004391 /* Pop the "after-variable" args off the list. */
4392 for (j = argcntafter; j > 0; j--, i++) {
4393 *--sp = PyList_GET_ITEM(l, ll - j);
4394 }
4395 /* Resize the list. */
4396 Py_SIZE(l) = ll - argcntafter;
4397 Py_DECREF(it);
4398 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004399
Tim Petersd6d010b2001-06-21 02:49:55 +00004400Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004401 for (; i > 0; i--, sp++)
4402 Py_DECREF(*sp);
4403 Py_XDECREF(it);
4404 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004405}
4406
4407
Guido van Rossum96a42c81992-01-12 02:29:51 +00004408#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004409static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004410prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004412 printf("%s ", str);
4413 if (PyObject_Print(v, stdout, 0) != 0)
4414 PyErr_Clear(); /* Don't know what else to do */
4415 printf("\n");
4416 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004417}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004418#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004419
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004420static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004421call_exc_trace(Py_tracefunc func, PyObject *self,
4422 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004423{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004424 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004425 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004426 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004427 if (value == NULL) {
4428 value = Py_None;
4429 Py_INCREF(value);
4430 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004431 PyErr_NormalizeException(&type, &value, &orig_traceback);
4432 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004433 arg = PyTuple_Pack(3, type, value, traceback);
4434 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004435 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004436 return;
4437 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004438 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004439 Py_DECREF(arg);
4440 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004441 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004442 else {
4443 Py_XDECREF(type);
4444 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004445 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004446 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004447}
4448
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004449static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004450call_trace_protected(Py_tracefunc func, PyObject *obj,
4451 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004452 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004453{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004454 PyObject *type, *value, *traceback;
4455 int err;
4456 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004457 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004458 if (err == 0)
4459 {
4460 PyErr_Restore(type, value, traceback);
4461 return 0;
4462 }
4463 else {
4464 Py_XDECREF(type);
4465 Py_XDECREF(value);
4466 Py_XDECREF(traceback);
4467 return -1;
4468 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004469}
4470
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004471static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004472call_trace(Py_tracefunc func, PyObject *obj,
4473 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004474 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004475{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004476 int result;
4477 if (tstate->tracing)
4478 return 0;
4479 tstate->tracing++;
4480 tstate->use_tracing = 0;
4481 result = func(obj, frame, what, arg);
4482 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4483 || (tstate->c_profilefunc != NULL));
4484 tstate->tracing--;
4485 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004486}
4487
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004488PyObject *
4489_PyEval_CallTracing(PyObject *func, PyObject *args)
4490{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004491 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004492 int save_tracing = tstate->tracing;
4493 int save_use_tracing = tstate->use_tracing;
4494 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004496 tstate->tracing = 0;
4497 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4498 || (tstate->c_profilefunc != NULL));
4499 result = PyObject_Call(func, args, NULL);
4500 tstate->tracing = save_tracing;
4501 tstate->use_tracing = save_use_tracing;
4502 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004503}
4504
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004505/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004506static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004507maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004508 PyThreadState *tstate, PyFrameObject *frame,
4509 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004510{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004511 int result = 0;
4512 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004514 /* If the last instruction executed isn't in the current
4515 instruction window, reset the window.
4516 */
4517 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4518 PyAddrPair bounds;
4519 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4520 &bounds);
4521 *instr_lb = bounds.ap_lower;
4522 *instr_ub = bounds.ap_upper;
4523 }
4524 /* If the last instruction falls at the start of a line or if
4525 it represents a jump backwards, update the frame's line
4526 number and call the trace function. */
4527 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
4528 frame->f_lineno = line;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004529 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004530 }
4531 *instr_prev = frame->f_lasti;
4532 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004533}
4534
Fred Drake5755ce62001-06-27 19:19:46 +00004535void
4536PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004537{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004538 PyThreadState *tstate = PyThreadState_GET();
4539 PyObject *temp = tstate->c_profileobj;
4540 Py_XINCREF(arg);
4541 tstate->c_profilefunc = NULL;
4542 tstate->c_profileobj = NULL;
4543 /* Must make sure that tracing is not ignored if 'temp' is freed */
4544 tstate->use_tracing = tstate->c_tracefunc != NULL;
4545 Py_XDECREF(temp);
4546 tstate->c_profilefunc = func;
4547 tstate->c_profileobj = arg;
4548 /* Flag that tracing or profiling is turned on */
4549 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004550}
4551
4552void
4553PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4554{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004555 PyThreadState *tstate = PyThreadState_GET();
4556 PyObject *temp = tstate->c_traceobj;
4557 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4558 Py_XINCREF(arg);
4559 tstate->c_tracefunc = NULL;
4560 tstate->c_traceobj = NULL;
4561 /* Must make sure that profiling is not ignored if 'temp' is freed */
4562 tstate->use_tracing = tstate->c_profilefunc != NULL;
4563 Py_XDECREF(temp);
4564 tstate->c_tracefunc = func;
4565 tstate->c_traceobj = arg;
4566 /* Flag that tracing or profiling is turned on */
4567 tstate->use_tracing = ((func != NULL)
4568 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004569}
4570
Yury Selivanov75445082015-05-11 22:57:16 -04004571void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004572_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004573{
4574 PyThreadState *tstate = PyThreadState_GET();
4575
Yury Selivanov75445082015-05-11 22:57:16 -04004576 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004577 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004578}
4579
4580PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004581_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004582{
4583 PyThreadState *tstate = PyThreadState_GET();
4584 return tstate->coroutine_wrapper;
4585}
4586
Yury Selivanoveb636452016-09-08 22:01:51 -07004587void
4588_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4589{
4590 PyThreadState *tstate = PyThreadState_GET();
4591
4592 Py_XINCREF(firstiter);
4593 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4594}
4595
4596PyObject *
4597_PyEval_GetAsyncGenFirstiter(void)
4598{
4599 PyThreadState *tstate = PyThreadState_GET();
4600 return tstate->async_gen_firstiter;
4601}
4602
4603void
4604_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4605{
4606 PyThreadState *tstate = PyThreadState_GET();
4607
4608 Py_XINCREF(finalizer);
4609 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4610}
4611
4612PyObject *
4613_PyEval_GetAsyncGenFinalizer(void)
4614{
4615 PyThreadState *tstate = PyThreadState_GET();
4616 return tstate->async_gen_finalizer;
4617}
4618
Guido van Rossumb209a111997-04-29 18:18:01 +00004619PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004620PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004621{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004622 PyFrameObject *current_frame = PyEval_GetFrame();
4623 if (current_frame == NULL)
4624 return PyThreadState_GET()->interp->builtins;
4625 else
4626 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004627}
4628
Guido van Rossumb209a111997-04-29 18:18:01 +00004629PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004630PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004631{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004632 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004633 if (current_frame == NULL) {
4634 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004635 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004636 }
4637
4638 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4639 return NULL;
4640
4641 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004642 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004643}
4644
Guido van Rossumb209a111997-04-29 18:18:01 +00004645PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004646PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004647{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004648 PyFrameObject *current_frame = PyEval_GetFrame();
4649 if (current_frame == NULL)
4650 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004651
4652 assert(current_frame->f_globals != NULL);
4653 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004654}
4655
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004656PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004657PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004658{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004659 PyThreadState *tstate = PyThreadState_GET();
4660 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004661}
4662
Guido van Rossum6135a871995-01-09 17:53:26 +00004663int
Tim Peters5ba58662001-07-16 02:29:45 +00004664PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004665{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004666 PyFrameObject *current_frame = PyEval_GetFrame();
4667 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004669 if (current_frame != NULL) {
4670 const int codeflags = current_frame->f_code->co_flags;
4671 const int compilerflags = codeflags & PyCF_MASK;
4672 if (compilerflags) {
4673 result = 1;
4674 cf->cf_flags |= compilerflags;
4675 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004676#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004677 if (codeflags & CO_GENERATOR_ALLOWED) {
4678 result = 1;
4679 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4680 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004681#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004682 }
4683 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004684}
4685
Guido van Rossum3f5da241990-12-20 15:06:42 +00004686
Guido van Rossum681d79a1995-07-18 14:51:37 +00004687/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00004688 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00004689
Guido van Rossumb209a111997-04-29 18:18:01 +00004690PyObject *
Victor Stinner8a31c822016-08-19 17:12:23 +02004691PyEval_CallObjectWithKeywords(PyObject *func, PyObject *args, PyObject *kwargs)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004692{
Victor Stinner59b356d2015-03-16 11:52:32 +01004693#ifdef Py_DEBUG
4694 /* PyEval_CallObjectWithKeywords() must not be called with an exception
4695 set. It raises a new exception if parameters are invalid or if
4696 PyTuple_New() fails, and so the original exception is lost. */
4697 assert(!PyErr_Occurred());
4698#endif
4699
Victor Stinner8a31c822016-08-19 17:12:23 +02004700 if (args == NULL) {
Victor Stinner155ea652016-08-22 23:26:00 +02004701 return _PyObject_FastCallDict(func, NULL, 0, kwargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004702 }
Victor Stinner155ea652016-08-22 23:26:00 +02004703
4704 if (!PyTuple_Check(args)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004705 PyErr_SetString(PyExc_TypeError,
4706 "argument list must be a tuple");
4707 return NULL;
4708 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00004709
Victor Stinner8a31c822016-08-19 17:12:23 +02004710 if (kwargs != NULL && !PyDict_Check(kwargs)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004711 PyErr_SetString(PyExc_TypeError,
4712 "keyword list must be a dictionary");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004713 return NULL;
4714 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00004715
Victor Stinner6e2333d2016-08-23 00:25:01 +02004716 return PyObject_Call(func, args, kwargs);
Jeremy Hylton52820442001-01-03 23:52:36 +00004717}
4718
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004719const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004720PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004721{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004722 if (PyMethod_Check(func))
4723 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4724 else if (PyFunction_Check(func))
4725 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
4726 else if (PyCFunction_Check(func))
4727 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4728 else
4729 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004730}
4731
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004732const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004733PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004734{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004735 if (PyMethod_Check(func))
4736 return "()";
4737 else if (PyFunction_Check(func))
4738 return "()";
4739 else if (PyCFunction_Check(func))
4740 return "()";
4741 else
4742 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004743}
4744
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004745#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004746if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004747 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4748 tstate, tstate->frame, \
4749 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004750 x = NULL; \
4751 } \
4752 else { \
4753 x = call; \
4754 if (tstate->c_profilefunc != NULL) { \
4755 if (x == NULL) { \
4756 call_trace_protected(tstate->c_profilefunc, \
4757 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004758 tstate, tstate->frame, \
4759 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004760 /* XXX should pass (type, value, tb) */ \
4761 } else { \
4762 if (call_trace(tstate->c_profilefunc, \
4763 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004764 tstate, tstate->frame, \
4765 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004766 Py_DECREF(x); \
4767 x = NULL; \
4768 } \
4769 } \
4770 } \
4771 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004772} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004773 x = call; \
4774 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004775
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004776static PyObject *
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004777call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004778{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004779 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004780 PyObject *func = *pfunc;
4781 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004782 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4783 Py_ssize_t nargs = oparg - nkwargs;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004784 PyObject **stack;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004786 /* Always dispatch PyCFunction first, because these are
4787 presumed to be the most frequent callable object.
4788 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004789 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004790 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004792 PCALL(PCALL_CFUNCTION);
Victor Stinner4a7cc882015-03-06 23:35:27 +01004793
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004794 stack = (*pp_stack) - nargs - nkwargs;
4795 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004796 }
4797 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004798 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4799 /* optimize access to bound methods */
4800 PyObject *self = PyMethod_GET_SELF(func);
4801 PCALL(PCALL_METHOD);
4802 PCALL(PCALL_BOUND_METHOD);
4803 Py_INCREF(self);
4804 func = PyMethod_GET_FUNCTION(func);
4805 Py_INCREF(func);
4806 Py_SETREF(*pfunc, self);
4807 nargs++;
4808 }
4809 else {
4810 Py_INCREF(func);
4811 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004812
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004813 stack = (*pp_stack) - nargs - nkwargs;
Victor Stinner4a7cc882015-03-06 23:35:27 +01004814
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004815 READ_TIMESTAMP(*pintr0);
4816 if (PyFunction_Check(func)) {
4817 x = fast_function(func, stack, nargs, kwnames);
4818 }
4819 else {
4820 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4821 }
4822 READ_TIMESTAMP(*pintr1);
Victor Stinnerd8735722016-09-09 12:36:44 -07004823
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004824 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004825 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004826
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004827 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004829 /* Clear the stack of the function object. Also removes
4830 the arguments in case they weren't consumed already
Victor Stinnere90bdb12016-08-25 23:26:50 +02004831 (fast_function() and err_args() leave them on the stack).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004832 */
4833 while ((*pp_stack) > pfunc) {
4834 w = EXT_POP(*pp_stack);
4835 Py_DECREF(w);
4836 PCALL(PCALL_POP);
4837 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004839 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004840}
4841
Victor Stinnere90bdb12016-08-25 23:26:50 +02004842/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004843 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004844 For the simplest case -- a function that takes only positional
4845 arguments and is called with only positional arguments -- it
4846 inlines the most primitive frame setup code from
4847 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4848 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004849*/
4850
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004851static PyObject*
Victor Stinnerd8735722016-09-09 12:36:44 -07004852_PyFunction_FastCall(PyCodeObject *co, PyObject **args, Py_ssize_t nargs,
4853 PyObject *globals)
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004854{
4855 PyFrameObject *f;
4856 PyThreadState *tstate = PyThreadState_GET();
4857 PyObject **fastlocals;
4858 Py_ssize_t i;
4859 PyObject *result;
4860
4861 PCALL(PCALL_FASTER_FUNCTION);
4862 assert(globals != NULL);
4863 /* XXX Perhaps we should create a specialized
4864 PyFrame_New() that doesn't take locals, but does
4865 take builtins without sanity checking them.
4866 */
4867 assert(tstate != NULL);
4868 f = PyFrame_New(tstate, co, globals, NULL);
4869 if (f == NULL) {
4870 return NULL;
4871 }
4872
4873 fastlocals = f->f_localsplus;
4874
Victor Stinner74319ae2016-08-25 00:04:09 +02004875 for (i = 0; i < nargs; i++) {
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004876 Py_INCREF(*args);
4877 fastlocals[i] = *args++;
4878 }
4879 result = PyEval_EvalFrameEx(f,0);
4880
4881 ++tstate->recursion_depth;
4882 Py_DECREF(f);
4883 --tstate->recursion_depth;
4884
4885 return result;
4886}
4887
Victor Stinnere90bdb12016-08-25 23:26:50 +02004888static PyObject *
Victor Stinnerd8735722016-09-09 12:36:44 -07004889fast_function(PyObject *func, PyObject **stack,
4890 Py_ssize_t nargs, PyObject *kwnames)
Jeremy Hylton52820442001-01-03 23:52:36 +00004891{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004892 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4893 PyObject *globals = PyFunction_GET_GLOBALS(func);
4894 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004895 PyObject *kwdefs, *closure, *name, *qualname;
4896 PyObject **d;
Victor Stinnerd8735722016-09-09 12:36:44 -07004897 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004898 Py_ssize_t nd;
Victor Stinnerd8735722016-09-09 12:36:44 -07004899
4900 assert((nargs == 0 && nkwargs == 0) || stack != NULL);
Victor Stinner577e1f82016-08-25 00:29:32 +02004901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004902 PCALL(PCALL_FUNCTION);
4903 PCALL(PCALL_FAST_FUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004904
Victor Stinner74319ae2016-08-25 00:04:09 +02004905 if (co->co_kwonlyargcount == 0 && nkwargs == 0 &&
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004906 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
4907 {
Victor Stinner2eedc112016-08-22 12:29:42 +02004908 if (argdefs == NULL && co->co_argcount == nargs) {
Victor Stinnerd8735722016-09-09 12:36:44 -07004909 return _PyFunction_FastCall(co, stack, nargs, globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02004910 }
4911 else if (nargs == 0 && argdefs != NULL
4912 && co->co_argcount == Py_SIZE(argdefs)) {
4913 /* function called with no arguments, but all parameters have
4914 a default value: use default values as arguments .*/
4915 stack = &PyTuple_GET_ITEM(argdefs, 0);
Victor Stinnerd8735722016-09-09 12:36:44 -07004916 return _PyFunction_FastCall(co, stack, Py_SIZE(argdefs), globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02004917 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004918 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004919
4920 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
4921 closure = PyFunction_GET_CLOSURE(func);
4922 name = ((PyFunctionObject *)func) -> func_name;
4923 qualname = ((PyFunctionObject *)func) -> func_qualname;
4924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004925 if (argdefs != NULL) {
4926 d = &PyTuple_GET_ITEM(argdefs, 0);
4927 nd = Py_SIZE(argdefs);
4928 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004929 else {
4930 d = NULL;
4931 nd = 0;
4932 }
4933 return _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
Victor Stinner2eedc112016-08-22 12:29:42 +02004934 stack, nargs,
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004935 NULL, 0,
Victor Stinnerd8735722016-09-09 12:36:44 -07004936 kwnames, stack + nargs,
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004937 d, (int)nd, kwdefs,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004938 closure, name, qualname);
4939}
4940
4941PyObject *
Victor Stinnerd8735722016-09-09 12:36:44 -07004942_PyFunction_FastCallKeywords(PyObject *func, PyObject **stack,
4943 Py_ssize_t nargs, PyObject *kwnames)
4944{
4945 return fast_function(func, stack, nargs, kwnames);
4946}
4947
4948PyObject *
Victor Stinner74319ae2016-08-25 00:04:09 +02004949_PyFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs,
Victor Stinnerb9009392016-08-22 23:15:44 +02004950 PyObject *kwargs)
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004951{
4952 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4953 PyObject *globals = PyFunction_GET_GLOBALS(func);
4954 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4955 PyObject *kwdefs, *closure, *name, *qualname;
Victor Stinnerb9009392016-08-22 23:15:44 +02004956 PyObject *kwtuple, **k;
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004957 PyObject **d;
Victor Stinner74319ae2016-08-25 00:04:09 +02004958 Py_ssize_t nd, nk;
Victor Stinnerb9009392016-08-22 23:15:44 +02004959 PyObject *result;
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004960
Victor Stinner74319ae2016-08-25 00:04:09 +02004961 assert(func != NULL);
4962 assert(nargs >= 0);
4963 assert(nargs == 0 || args != NULL);
Victor Stinnerb9009392016-08-22 23:15:44 +02004964 assert(kwargs == NULL || PyDict_Check(kwargs));
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004965
Victor Stinner577e1f82016-08-25 00:29:32 +02004966 PCALL(PCALL_FUNCTION);
4967 PCALL(PCALL_FAST_FUNCTION);
4968
Victor Stinnerb9009392016-08-22 23:15:44 +02004969 if (co->co_kwonlyargcount == 0 &&
4970 (kwargs == NULL || PyDict_Size(kwargs) == 0) &&
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004971 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
4972 {
Victor Stinnerb9009392016-08-22 23:15:44 +02004973 /* Fast paths */
Victor Stinner2eedc112016-08-22 12:29:42 +02004974 if (argdefs == NULL && co->co_argcount == nargs) {
Victor Stinnerd8735722016-09-09 12:36:44 -07004975 return _PyFunction_FastCall(co, args, nargs, globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02004976 }
4977 else if (nargs == 0 && argdefs != NULL
4978 && co->co_argcount == Py_SIZE(argdefs)) {
4979 /* function called with no arguments, but all parameters have
4980 a default value: use default values as arguments .*/
4981 args = &PyTuple_GET_ITEM(argdefs, 0);
Victor Stinnerd8735722016-09-09 12:36:44 -07004982 return _PyFunction_FastCall(co, args, Py_SIZE(argdefs), globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02004983 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004984 }
4985
Victor Stinnerb9009392016-08-22 23:15:44 +02004986 if (kwargs != NULL) {
4987 Py_ssize_t pos, i;
4988 nk = PyDict_Size(kwargs);
4989
4990 kwtuple = PyTuple_New(2 * nk);
4991 if (kwtuple == NULL) {
4992 return NULL;
4993 }
4994
4995 k = &PyTuple_GET_ITEM(kwtuple, 0);
4996 pos = i = 0;
4997 while (PyDict_Next(kwargs, &pos, &k[i], &k[i+1])) {
4998 Py_INCREF(k[i]);
4999 Py_INCREF(k[i+1]);
5000 i += 2;
5001 }
5002 nk = i / 2;
5003 }
5004 else {
5005 kwtuple = NULL;
5006 k = NULL;
5007 nk = 0;
5008 }
5009
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005010 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
5011 closure = PyFunction_GET_CLOSURE(func);
5012 name = ((PyFunctionObject *)func) -> func_name;
5013 qualname = ((PyFunctionObject *)func) -> func_qualname;
5014
5015 if (argdefs != NULL) {
5016 d = &PyTuple_GET_ITEM(argdefs, 0);
5017 nd = Py_SIZE(argdefs);
5018 }
5019 else {
5020 d = NULL;
5021 nd = 0;
5022 }
Victor Stinnerb9009392016-08-22 23:15:44 +02005023
5024 result = _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
5025 args, nargs,
Victor Stinner74319ae2016-08-25 00:04:09 +02005026 k, nk,
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005027 NULL, NULL,
Victor Stinnerb9009392016-08-22 23:15:44 +02005028 d, nd, kwdefs,
5029 closure, name, qualname);
5030 Py_XDECREF(kwtuple);
5031 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00005032}
5033
5034static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005035do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005036{
Jeremy Hylton985eba52003-02-05 23:13:00 +00005037#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005038 /* At this point, we have to look at the type of func to
5039 update the call stats properly. Do it here so as to avoid
5040 exposing the call stats machinery outside ceval.c
5041 */
5042 if (PyFunction_Check(func))
5043 PCALL(PCALL_FUNCTION);
5044 else if (PyMethod_Check(func))
5045 PCALL(PCALL_METHOD);
5046 else if (PyType_Check(func))
5047 PCALL(PCALL_TYPE);
5048 else if (PyCFunction_Check(func))
5049 PCALL(PCALL_CFUNCTION);
5050 else
5051 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00005052#endif
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005054 if (PyCFunction_Check(func)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005055 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005056 PyThreadState *tstate = PyThreadState_GET();
5057 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005058 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005059 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005060 else {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005061 return PyObject_Call(func, callargs, kwdict);
Victor Stinner74319ae2016-08-25 00:04:09 +02005062 }
Jeremy Hylton52820442001-01-03 23:52:36 +00005063}
5064
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005065/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005066 nb_index slot defined, and store in *pi.
5067 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
5068 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 +00005069 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005070*/
Tim Petersb5196382001-12-16 19:44:20 +00005071/* Note: If v is NULL, return success without storing into *pi. This
5072 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
5073 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00005074*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005075int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005076_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005077{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005078 if (v != NULL) {
5079 Py_ssize_t x;
5080 if (PyIndex_Check(v)) {
5081 x = PyNumber_AsSsize_t(v, NULL);
5082 if (x == -1 && PyErr_Occurred())
5083 return 0;
5084 }
5085 else {
5086 PyErr_SetString(PyExc_TypeError,
5087 "slice indices must be integers or "
5088 "None or have an __index__ method");
5089 return 0;
5090 }
5091 *pi = x;
5092 }
5093 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005094}
5095
Guido van Rossum486364b2007-06-30 05:01:58 +00005096#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005097 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00005098
Guido van Rossumb209a111997-04-29 18:18:01 +00005099static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02005100cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005101{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005102 int res = 0;
5103 switch (op) {
5104 case PyCmp_IS:
5105 res = (v == w);
5106 break;
5107 case PyCmp_IS_NOT:
5108 res = (v != w);
5109 break;
5110 case PyCmp_IN:
5111 res = PySequence_Contains(w, v);
5112 if (res < 0)
5113 return NULL;
5114 break;
5115 case PyCmp_NOT_IN:
5116 res = PySequence_Contains(w, v);
5117 if (res < 0)
5118 return NULL;
5119 res = !res;
5120 break;
5121 case PyCmp_EXC_MATCH:
5122 if (PyTuple_Check(w)) {
5123 Py_ssize_t i, length;
5124 length = PyTuple_Size(w);
5125 for (i = 0; i < length; i += 1) {
5126 PyObject *exc = PyTuple_GET_ITEM(w, i);
5127 if (!PyExceptionClass_Check(exc)) {
5128 PyErr_SetString(PyExc_TypeError,
5129 CANNOT_CATCH_MSG);
5130 return NULL;
5131 }
5132 }
5133 }
5134 else {
5135 if (!PyExceptionClass_Check(w)) {
5136 PyErr_SetString(PyExc_TypeError,
5137 CANNOT_CATCH_MSG);
5138 return NULL;
5139 }
5140 }
5141 res = PyErr_GivenExceptionMatches(v, w);
5142 break;
5143 default:
5144 return PyObject_RichCompare(v, w, op);
5145 }
5146 v = res ? Py_True : Py_False;
5147 Py_INCREF(v);
5148 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005149}
5150
Thomas Wouters52152252000-08-17 22:55:00 +00005151static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005152import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
5153{
5154 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005155 PyObject *import_func, *res;
5156 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005157
5158 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
5159 if (import_func == NULL) {
5160 PyErr_SetString(PyExc_ImportError, "__import__ not found");
5161 return NULL;
5162 }
5163
5164 /* Fast path for not overloaded __import__. */
5165 if (import_func == PyThreadState_GET()->interp->import_func) {
5166 int ilevel = _PyLong_AsInt(level);
5167 if (ilevel == -1 && PyErr_Occurred()) {
5168 return NULL;
5169 }
5170 res = PyImport_ImportModuleLevelObject(
5171 name,
5172 f->f_globals,
5173 f->f_locals == NULL ? Py_None : f->f_locals,
5174 fromlist,
5175 ilevel);
5176 return res;
5177 }
5178
5179 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005180
5181 stack[0] = name;
5182 stack[1] = f->f_globals;
5183 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5184 stack[3] = fromlist;
5185 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005186 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005187 Py_DECREF(import_func);
5188 return res;
5189}
5190
5191static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00005192import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005193{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005194 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005195 _Py_IDENTIFIER(__name__);
5196 PyObject *fullmodname, *pkgname;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005198 x = PyObject_GetAttr(v, name);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005199 if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
5200 return x;
5201 /* Issue #17636: in case this failed because of a circular relative
5202 import, try to fallback on reading the module directly from
5203 sys.modules. */
5204 PyErr_Clear();
5205 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005206 if (pkgname == NULL) {
5207 goto error;
5208 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005209 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
5210 Py_DECREF(pkgname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005211 if (fullmodname == NULL) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005212 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005213 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005214 x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005215 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005216 if (x == NULL) {
5217 goto error;
5218 }
5219 Py_INCREF(x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005220 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005221 error:
5222 PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
5223 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005224}
Guido van Rossumac7be682001-01-17 15:42:30 +00005225
Thomas Wouters52152252000-08-17 22:55:00 +00005226static int
5227import_all_from(PyObject *locals, PyObject *v)
5228{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005229 _Py_IDENTIFIER(__all__);
5230 _Py_IDENTIFIER(__dict__);
5231 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005232 PyObject *dict, *name, *value;
5233 int skip_leading_underscores = 0;
5234 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005236 if (all == NULL) {
5237 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5238 return -1; /* Unexpected error */
5239 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005240 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005241 if (dict == NULL) {
5242 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5243 return -1;
5244 PyErr_SetString(PyExc_ImportError,
5245 "from-import-* object has no __dict__ and no __all__");
5246 return -1;
5247 }
5248 all = PyMapping_Keys(dict);
5249 Py_DECREF(dict);
5250 if (all == NULL)
5251 return -1;
5252 skip_leading_underscores = 1;
5253 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005255 for (pos = 0, err = 0; ; pos++) {
5256 name = PySequence_GetItem(all, pos);
5257 if (name == NULL) {
5258 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5259 err = -1;
5260 else
5261 PyErr_Clear();
5262 break;
5263 }
5264 if (skip_leading_underscores &&
5265 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005266 PyUnicode_READY(name) != -1 &&
5267 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005268 {
5269 Py_DECREF(name);
5270 continue;
5271 }
5272 value = PyObject_GetAttr(v, name);
5273 if (value == NULL)
5274 err = -1;
5275 else if (PyDict_CheckExact(locals))
5276 err = PyDict_SetItem(locals, name, value);
5277 else
5278 err = PyObject_SetItem(locals, name, value);
5279 Py_DECREF(name);
5280 Py_XDECREF(value);
5281 if (err != 0)
5282 break;
5283 }
5284 Py_DECREF(all);
5285 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005286}
5287
Guido van Rossumac7be682001-01-17 15:42:30 +00005288static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005289format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005291 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005293 if (!obj)
5294 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005296 obj_str = _PyUnicode_AsString(obj);
5297 if (!obj_str)
5298 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005300 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005301}
Guido van Rossum950361c1997-01-24 13:49:28 +00005302
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005303static void
5304format_exc_unbound(PyCodeObject *co, int oparg)
5305{
5306 PyObject *name;
5307 /* Don't stomp existing exception */
5308 if (PyErr_Occurred())
5309 return;
5310 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5311 name = PyTuple_GET_ITEM(co->co_cellvars,
5312 oparg);
5313 format_exc_check_arg(
5314 PyExc_UnboundLocalError,
5315 UNBOUNDLOCAL_ERROR_MSG,
5316 name);
5317 } else {
5318 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5319 PyTuple_GET_SIZE(co->co_cellvars));
5320 format_exc_check_arg(PyExc_NameError,
5321 UNBOUNDFREE_ERROR_MSG, name);
5322 }
5323}
5324
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005325static PyObject *
5326unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005327 PyFrameObject *f, const unsigned short *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005328{
5329 PyObject *res;
5330 if (Py_REFCNT(v) == 2) {
5331 /* In the common case, there are 2 references to the value
5332 * stored in 'variable' when the += is performed: one on the
5333 * value stack (in 'v') and one still stored in the
5334 * 'variable'. We try to delete the variable now to reduce
5335 * the refcnt to 1.
5336 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005337 int opcode, oparg;
5338 NEXTOPARG();
5339 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005340 case STORE_FAST:
5341 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005342 PyObject **fastlocals = f->f_localsplus;
5343 if (GETLOCAL(oparg) == v)
5344 SETLOCAL(oparg, NULL);
5345 break;
5346 }
5347 case STORE_DEREF:
5348 {
5349 PyObject **freevars = (f->f_localsplus +
5350 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005351 PyObject *c = freevars[oparg];
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005352 if (PyCell_GET(c) == v)
5353 PyCell_Set(c, NULL);
5354 break;
5355 }
5356 case STORE_NAME:
5357 {
5358 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005359 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005360 PyObject *locals = f->f_locals;
5361 if (PyDict_CheckExact(locals) &&
5362 PyDict_GetItem(locals, name) == v) {
5363 if (PyDict_DelItem(locals, name) != 0) {
5364 PyErr_Clear();
5365 }
5366 }
5367 break;
5368 }
5369 }
5370 }
5371 res = v;
5372 PyUnicode_Append(&res, w);
5373 return res;
5374}
5375
Guido van Rossum950361c1997-01-24 13:49:28 +00005376#ifdef DYNAMIC_EXECUTION_PROFILE
5377
Skip Montanarof118cb12001-10-15 20:51:38 +00005378static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005379getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005380{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005381 int i;
5382 PyObject *l = PyList_New(256);
5383 if (l == NULL) return NULL;
5384 for (i = 0; i < 256; i++) {
5385 PyObject *x = PyLong_FromLong(a[i]);
5386 if (x == NULL) {
5387 Py_DECREF(l);
5388 return NULL;
5389 }
5390 PyList_SetItem(l, i, x);
5391 }
5392 for (i = 0; i < 256; i++)
5393 a[i] = 0;
5394 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005395}
5396
5397PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005398_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005399{
5400#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005401 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005402#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005403 int i;
5404 PyObject *l = PyList_New(257);
5405 if (l == NULL) return NULL;
5406 for (i = 0; i < 257; i++) {
5407 PyObject *x = getarray(dxpairs[i]);
5408 if (x == NULL) {
5409 Py_DECREF(l);
5410 return NULL;
5411 }
5412 PyList_SetItem(l, i, x);
5413 }
5414 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005415#endif
5416}
5417
5418#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005419
5420Py_ssize_t
5421_PyEval_RequestCodeExtraIndex(freefunc free)
5422{
5423 PyThreadState *tstate = PyThreadState_Get();
5424 Py_ssize_t new_index;
5425
5426 if (tstate->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
5427 return -1;
5428 }
5429 new_index = tstate->co_extra_user_count++;
5430 tstate->co_extra_freefuncs[new_index] = free;
5431 return new_index;
5432}