blob: ea79f5f2b57f8f91a4ef02ecad34cdb42faf004d [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040015#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000016#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000017#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070018#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040019#include "setobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000020#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000021
Guido van Rossumc6004111993-11-05 10:22:19 +000022#include <ctype.h>
23
Guido van Rossum04691fc1992-08-12 15:35:34 +000024/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000025/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000026
Guido van Rossum408027e1996-12-30 16:17:54 +000027#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000028/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029#define LLTRACE 1 /* Low-level trace feature */
30#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000031#endif
32
Jeremy Hylton52820442001-01-03 23:52:36 +000033typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000034
Guido van Rossum374a9221991-04-04 10:40:29 +000035/* Forward declarations */
Victor Stinnerf9b760f2016-09-09 10:17:08 -070036static PyObject * call_function(PyObject ***, Py_ssize_t, PyObject *);
Victor Stinnerd8735722016-09-09 12:36:44 -070037static PyObject * fast_function(PyObject *, PyObject **, Py_ssize_t, PyObject *);
Victor Stinnerf9b760f2016-09-09 10:17:08 -070038static PyObject * do_call_core(PyObject *, PyObject *, PyObject *);
Jeremy Hylton52820442001-01-03 23:52:36 +000039
Guido van Rossum0a066c01992-03-27 17:29:15 +000040#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000041static int lltrace;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020042static int prtrace(PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000043#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010044static int call_trace(Py_tracefunc, PyObject *,
45 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000047static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010048 PyThreadState *, PyFrameObject *,
49 int, PyObject *);
50static void call_exc_trace(Py_tracefunc, PyObject *,
51 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000052static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010053 PyThreadState *, PyFrameObject *, int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070054static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
55static void dtrace_function_entry(PyFrameObject *);
56static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000057
Thomas Wouters477c8d52006-05-27 19:21:47 +000058static PyObject * cmp_outcome(int, PyObject *, PyObject *);
Serhiy Storchaka133138a2016-08-02 22:51:21 +030059static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *, PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +000060static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +000061static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +000062static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +000063static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +020064static PyObject * unicode_concatenate(PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030065 PyFrameObject *, const _Py_CODEUNIT *);
Benjamin Petersonce798522012-01-22 11:24:29 -050066static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Guido van Rossum374a9221991-04-04 10:40:29 +000067
Paul Prescode68140d2000-08-30 20:25:01 +000068#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000070#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000072#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 "free variable '%.200s' referenced before assignment" \
74 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000075
Guido van Rossum950361c1997-01-24 13:49:28 +000076/* Dynamic execution profile */
77#ifdef DYNAMIC_EXECUTION_PROFILE
78#ifdef DXPAIRS
79static long dxpairs[257][256];
80#define dxp dxpairs[256]
81#else
82static long dxp[256];
83#endif
84#endif
85
Jeremy Hylton985eba52003-02-05 23:13:00 +000086/* Function call profile */
87#ifdef CALL_PROFILE
88#define PCALL_NUM 11
89static int pcall[PCALL_NUM];
90
91#define PCALL_ALL 0
92#define PCALL_FUNCTION 1
93#define PCALL_FAST_FUNCTION 2
94#define PCALL_FASTER_FUNCTION 3
95#define PCALL_METHOD 4
96#define PCALL_BOUND_METHOD 5
97#define PCALL_CFUNCTION 6
98#define PCALL_TYPE 7
99#define PCALL_GENERATOR 8
100#define PCALL_OTHER 9
101#define PCALL_POP 10
102
103/* Notes about the statistics
104
105 PCALL_FAST stats
106
107 FAST_FUNCTION means no argument tuple needs to be created.
108 FASTER_FUNCTION means that the fast-path frame setup code is used.
109
110 If there is a method call where the call can be optimized by changing
111 the argument tuple and calling the function directly, it gets recorded
112 twice.
113
114 As a result, the relationship among the statistics appears to be
115 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
116 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
117 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
118 PCALL_METHOD > PCALL_BOUND_METHOD
119*/
120
121#define PCALL(POS) pcall[POS]++
122
123PyObject *
124PyEval_GetCallStats(PyObject *self)
125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 return Py_BuildValue("iiiiiiiiiii",
127 pcall[0], pcall[1], pcall[2], pcall[3],
128 pcall[4], pcall[5], pcall[6], pcall[7],
129 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000130}
131#else
132#define PCALL(O)
133
134PyObject *
135PyEval_GetCallStats(PyObject *self)
136{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 Py_INCREF(Py_None);
138 return Py_None;
Jeremy Hylton985eba52003-02-05 23:13:00 +0000139}
140#endif
141
Tim Peters5ca576e2001-06-18 22:08:13 +0000142
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000143#ifdef WITH_THREAD
144#define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request)
145#else
146#define GIL_REQUEST 0
147#endif
148
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000149/* This can set eval_breaker to 0 even though gil_drop_request became
150 1. We believe this is all right because the eval loop will release
151 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000152#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 _Py_atomic_store_relaxed( \
154 &eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000155 GIL_REQUEST | \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
157 pending_async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000158
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000159#ifdef WITH_THREAD
160
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000161#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 do { \
163 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
164 _Py_atomic_store_relaxed(&eval_breaker, 1); \
165 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000166
167#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 do { \
169 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
170 COMPUTE_EVAL_BREAKER(); \
171 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000172
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000173#endif
174
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000175/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000176#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 do { \
178 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
179 _Py_atomic_store_relaxed(&eval_breaker, 1); \
180 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000181
182#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 do { \
184 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
185 COMPUTE_EVAL_BREAKER(); \
186 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000187
188#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 do { \
190 pending_async_exc = 1; \
191 _Py_atomic_store_relaxed(&eval_breaker, 1); \
192 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000193
194#define UNSIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000196
197
Guido van Rossume59214e1994-08-30 08:01:59 +0000198#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000199
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000200#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000201#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000202#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000203#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000204
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000205static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000206static long main_thread = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000207/* This single variable consolidates all requests to break out of the fast path
208 in the eval loop. */
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000209static _Py_atomic_int eval_breaker = {0};
210/* Request for dropping the GIL */
211static _Py_atomic_int gil_drop_request = {0};
212/* Request for running pending calls. */
213static _Py_atomic_int pendingcalls_to_do = {0};
214/* Request for looking at the `async_exc` field of the current thread state.
215 Guarded by the GIL. */
216static int pending_async_exc = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000217
218#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000219
Tim Peters7f468f22004-10-11 02:40:51 +0000220int
221PyEval_ThreadsInitialized(void)
222{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000224}
225
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000226void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000227PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000228{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 if (gil_created())
230 return;
231 create_gil();
232 take_gil(PyThreadState_GET());
233 main_thread = PyThread_get_thread_ident();
234 if (!pending_lock)
235 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000236}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000237
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000238void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000239_PyEval_FiniThreads(void)
240{
241 if (!gil_created())
242 return;
243 destroy_gil();
244 assert(!gil_created());
245}
246
247void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000248PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000249{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 PyThreadState *tstate = PyThreadState_GET();
251 if (tstate == NULL)
252 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
253 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000254}
255
256void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000257PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000258{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 /* This function must succeed when the current thread state is NULL.
260 We therefore avoid PyThreadState_GET() which dumps a fatal error
261 in debug mode.
262 */
263 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
264 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000265}
266
267void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000268PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 if (tstate == NULL)
271 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
272 /* Check someone has called PyEval_InitThreads() to create the lock */
273 assert(gil_created());
274 take_gil(tstate);
275 if (PyThreadState_Swap(tstate) != NULL)
276 Py_FatalError(
277 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000278}
279
280void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000281PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000282{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 if (tstate == NULL)
284 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
285 if (PyThreadState_Swap(NULL) != tstate)
286 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
287 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000288}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000289
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200290/* This function is called from PyOS_AfterFork to destroy all threads which are
291 * not running in the child process, and clear internal locks which might be
292 * held by those threads. (This could also be done using pthread_atfork
293 * mechanism, at least for the pthreads implementation.) */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000294
295void
296PyEval_ReInitThreads(void)
297{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200298 _Py_IDENTIFIER(_after_fork);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 PyObject *threading, *result;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200300 PyThreadState *current_tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 if (!gil_created())
303 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 recreate_gil();
305 pending_lock = PyThread_allocate_lock();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200306 take_gil(current_tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 /* Update the threading module with the new state.
310 */
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200311 threading = PyMapping_GetItemString(current_tstate->interp->modules,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 "threading");
313 if (threading == NULL) {
314 /* threading not imported */
315 PyErr_Clear();
316 return;
317 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200318 result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 if (result == NULL)
320 PyErr_WriteUnraisable(threading);
321 else
322 Py_DECREF(result);
323 Py_DECREF(threading);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200324
325 /* Destroy all threads except the current one */
326 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000327}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000328
329#else
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000330static _Py_atomic_int eval_breaker = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000331static int pending_async_exc = 0;
332#endif /* WITH_THREAD */
333
334/* This function is used to signal that async exceptions are waiting to be
335 raised, therefore it is also useful in non-threaded builds. */
336
337void
338_PyEval_SignalAsyncExc(void)
339{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000341}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000342
Guido van Rossumff4949e1992-08-05 19:58:53 +0000343/* Functions save_thread and restore_thread are always defined so
344 dynamically loaded modules needn't be compiled separately for use
345 with and without threads: */
346
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000347PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000348PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000349{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 PyThreadState *tstate = PyThreadState_Swap(NULL);
351 if (tstate == NULL)
352 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000353#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 if (gil_created())
355 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000356#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000358}
359
360void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000361PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000362{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 if (tstate == NULL)
364 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000365#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 if (gil_created()) {
367 int err = errno;
368 take_gil(tstate);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200369 /* _Py_Finalizing is protected by the GIL */
370 if (_Py_Finalizing && tstate != _Py_Finalizing) {
371 drop_gil(tstate);
372 PyThread_exit_thread();
373 assert(0); /* unreachable */
374 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 errno = err;
376 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000377#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000379}
380
381
Guido van Rossuma9672091994-09-14 13:31:22 +0000382/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
383 signal handlers or Mac I/O completion routines) can schedule calls
384 to a function to be called synchronously.
385 The synchronous function is called with one void* argument.
386 It should return 0 for success or -1 for failure -- failure should
387 be accompanied by an exception.
388
389 If registry succeeds, the registry function returns 0; if it fails
390 (e.g. due to too many pending calls) it returns -1 (without setting
391 an exception condition).
392
393 Note that because registry may occur from within signal handlers,
394 or other asynchronous events, calling malloc() is unsafe!
395
396#ifdef WITH_THREAD
397 Any thread can schedule pending calls, but only the main thread
398 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000399 There is no facility to schedule calls to a particular thread, but
400 that should be easy to change, should that ever be required. In
401 that case, the static variables here should go into the python
402 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000403#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000404*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000405
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000406#ifdef WITH_THREAD
407
408/* The WITH_THREAD implementation is thread-safe. It allows
409 scheduling to be made from any thread, and even from an executing
410 callback.
411 */
412
413#define NPENDINGCALLS 32
414static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 int (*func)(void *);
416 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000417} pendingcalls[NPENDINGCALLS];
418static int pendingfirst = 0;
419static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000420
421int
422Py_AddPendingCall(int (*func)(void *), void *arg)
423{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 int i, j, result=0;
425 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 /* try a few times for the lock. Since this mechanism is used
428 * for signal handling (on the main thread), there is a (slim)
429 * chance that a signal is delivered on the same thread while we
430 * hold the lock during the Py_MakePendingCalls() function.
431 * This avoids a deadlock in that case.
432 * Note that signals can be delivered on any thread. In particular,
433 * on Windows, a SIGINT is delivered on a system-created worker
434 * thread.
435 * We also check for lock being NULL, in the unlikely case that
436 * this function is called before any bytecode evaluation takes place.
437 */
438 if (lock != NULL) {
439 for (i = 0; i<100; i++) {
440 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
441 break;
442 }
443 if (i == 100)
444 return -1;
445 }
446
447 i = pendinglast;
448 j = (i + 1) % NPENDINGCALLS;
449 if (j == pendingfirst) {
450 result = -1; /* Queue full */
451 } else {
452 pendingcalls[i].func = func;
453 pendingcalls[i].arg = arg;
454 pendinglast = j;
455 }
456 /* signal main loop */
457 SIGNAL_PENDING_CALLS();
458 if (lock != NULL)
459 PyThread_release_lock(lock);
460 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000461}
462
463int
464Py_MakePendingCalls(void)
465{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200466 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 int i;
468 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 if (!pending_lock) {
471 /* initial allocation of the lock */
472 pending_lock = PyThread_allocate_lock();
473 if (pending_lock == NULL)
474 return -1;
475 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 /* only service pending calls on main thread */
478 if (main_thread && PyThread_get_thread_ident() != main_thread)
479 return 0;
480 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200481 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200483 busy = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 /* perform a bounded number of calls, in case of recursion */
485 for (i=0; i<NPENDINGCALLS; i++) {
486 int j;
487 int (*func)(void *);
488 void *arg = NULL;
489
490 /* pop one item off the queue while holding the lock */
491 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
492 j = pendingfirst;
493 if (j == pendinglast) {
494 func = NULL; /* Queue empty */
495 } else {
496 func = pendingcalls[j].func;
497 arg = pendingcalls[j].arg;
498 pendingfirst = (j + 1) % NPENDINGCALLS;
499 }
500 if (pendingfirst != pendinglast)
501 SIGNAL_PENDING_CALLS();
502 else
503 UNSIGNAL_PENDING_CALLS();
504 PyThread_release_lock(pending_lock);
505 /* having released the lock, perform the callback */
506 if (func == NULL)
507 break;
508 r = func(arg);
509 if (r)
510 break;
511 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200512 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000514}
515
516#else /* if ! defined WITH_THREAD */
517
518/*
519 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
520 This code is used for signal handling in python that isn't built
521 with WITH_THREAD.
522 Don't use this implementation when Py_AddPendingCalls() can happen
523 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524
Guido van Rossuma9672091994-09-14 13:31:22 +0000525 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000526 (1) nested asynchronous calls to Py_AddPendingCall()
527 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000529 (1) is very unlikely because typically signal delivery
530 is blocked during signal handling. So it should be impossible.
531 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000532 The current code is safe against (2), but not against (1).
533 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000534 thread is present, interrupted by signals, and that the critical
535 section is protected with the "busy" variable. On Windows, which
536 delivers SIGINT on a system thread, this does not hold and therefore
537 Windows really shouldn't use this version.
538 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000539*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000540
Guido van Rossuma9672091994-09-14 13:31:22 +0000541#define NPENDINGCALLS 32
542static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 int (*func)(void *);
544 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000545} pendingcalls[NPENDINGCALLS];
546static volatile int pendingfirst = 0;
547static volatile int pendinglast = 0;
Benjamin Peterson08ec84c2010-05-30 14:49:32 +0000548static _Py_atomic_int pendingcalls_to_do = {0};
Guido van Rossuma9672091994-09-14 13:31:22 +0000549
550int
Thomas Wouters334fb892000-07-25 12:56:38 +0000551Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 static volatile int busy = 0;
554 int i, j;
555 /* XXX Begin critical section */
556 if (busy)
557 return -1;
558 busy = 1;
559 i = pendinglast;
560 j = (i + 1) % NPENDINGCALLS;
561 if (j == pendingfirst) {
562 busy = 0;
563 return -1; /* Queue full */
564 }
565 pendingcalls[i].func = func;
566 pendingcalls[i].arg = arg;
567 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 SIGNAL_PENDING_CALLS();
570 busy = 0;
571 /* XXX End critical section */
572 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000573}
574
Guido van Rossum180d7b41994-09-29 09:45:57 +0000575int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000576Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000577{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 static int busy = 0;
579 if (busy)
580 return 0;
581 busy = 1;
582 UNSIGNAL_PENDING_CALLS();
583 for (;;) {
584 int i;
585 int (*func)(void *);
586 void *arg;
587 i = pendingfirst;
588 if (i == pendinglast)
589 break; /* Queue empty */
590 func = pendingcalls[i].func;
591 arg = pendingcalls[i].arg;
592 pendingfirst = (i + 1) % NPENDINGCALLS;
593 if (func(arg) < 0) {
594 busy = 0;
595 SIGNAL_PENDING_CALLS(); /* We're not done yet */
596 return -1;
597 }
598 }
599 busy = 0;
600 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000601}
602
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000603#endif /* WITH_THREAD */
604
Guido van Rossuma9672091994-09-14 13:31:22 +0000605
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000606/* The interpreter's recursion limit */
607
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000608#ifndef Py_DEFAULT_RECURSION_LIMIT
609#define Py_DEFAULT_RECURSION_LIMIT 1000
610#endif
611static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
612int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000613
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000614int
615Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000618}
619
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000620void
621Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000622{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 recursion_limit = new_limit;
624 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000625}
626
Armin Rigo2b3eb402003-10-28 12:05:48 +0000627/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
628 if the recursion_depth reaches _Py_CheckRecursionLimit.
629 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
630 to guarantee that _Py_CheckRecursiveCall() is regularly called.
631 Without USE_STACKCHECK, there is no need for this. */
632int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300633_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000634{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000636
637#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 if (PyOS_CheckStack()) {
639 --tstate->recursion_depth;
640 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
641 return -1;
642 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000643#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 _Py_CheckRecursionLimit = recursion_limit;
645 if (tstate->recursion_critical)
646 /* Somebody asked that we don't check for recursion. */
647 return 0;
648 if (tstate->overflowed) {
649 if (tstate->recursion_depth > recursion_limit + 50) {
650 /* Overflowing while handling an overflow. Give up. */
651 Py_FatalError("Cannot recover from stack overflow.");
652 }
653 return 0;
654 }
655 if (tstate->recursion_depth > recursion_limit) {
656 --tstate->recursion_depth;
657 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400658 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 "maximum recursion depth exceeded%s",
660 where);
661 return -1;
662 }
663 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000664}
665
Guido van Rossum374a9221991-04-04 10:40:29 +0000666/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000667enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000668 WHY_NOT = 0x0001, /* No error */
669 WHY_EXCEPTION = 0x0002, /* Exception occurred */
Stefan Krahb7e10102010-06-23 18:42:39 +0000670 WHY_RETURN = 0x0008, /* 'return' statement */
671 WHY_BREAK = 0x0010, /* 'break' statement */
672 WHY_CONTINUE = 0x0020, /* 'continue' statement */
673 WHY_YIELD = 0x0040, /* 'yield' operator */
674 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000675};
Guido van Rossum374a9221991-04-04 10:40:29 +0000676
Benjamin Peterson87880242011-07-03 16:48:31 -0500677static void save_exc_state(PyThreadState *, PyFrameObject *);
678static void swap_exc_state(PyThreadState *, PyFrameObject *);
679static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400680static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000681static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000682
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000683/* Records whether tracing is on for any thread. Counts the number of
684 threads for which tstate->c_tracefunc is non-NULL, so if the value
685 is 0, we know we don't have to check this thread's c_tracefunc.
686 This speeds up the if statement in PyEval_EvalFrameEx() after
687 fast_next_opcode*/
688static int _Py_TracingPossible = 0;
689
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000690
Guido van Rossum374a9221991-04-04 10:40:29 +0000691
Guido van Rossumb209a111997-04-29 18:18:01 +0000692PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000693PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000694{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 return PyEval_EvalCodeEx(co,
696 globals, locals,
697 (PyObject **)NULL, 0,
698 (PyObject **)NULL, 0,
699 (PyObject **)NULL, 0,
700 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000701}
702
703
704/* Interpreter main loop */
705
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000706PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000707PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 /* This is for backward compatibility with extension modules that
709 used this API; core interpreter code should call
710 PyEval_EvalFrameEx() */
711 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000712}
713
714PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000715PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000716{
Brett Cannon3cebf932016-09-05 15:33:46 -0700717 PyThreadState *tstate = PyThreadState_GET();
718 return tstate->interp->eval_frame(f, throwflag);
719}
720
721PyObject *
722_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
723{
Guido van Rossum950361c1997-01-24 13:49:28 +0000724#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000726#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200727 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300728 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200729 int opcode; /* Current opcode */
730 int oparg; /* Current opcode argument, if any */
731 enum why_code why; /* Reason for block stack unwind */
732 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 PyObject *retval = NULL; /* Return value */
734 PyThreadState *tstate = PyThreadState_GET();
735 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 is true when the line being executed has changed. The
742 initial values are such as to make this false the first
743 time it is tested. */
744 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000745
Serhiy Storchakaab874002016-09-11 13:48:15 +0300746 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 PyObject *names;
748 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000749
Brett Cannon368b4b72012-04-02 12:17:59 -0400750#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200751 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400752#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200753
Antoine Pitroub52ec782009-01-25 16:34:23 +0000754/* Computed GOTOs, or
755 the-optimization-commonly-but-improperly-known-as-"threaded code"
756 using gcc's labels-as-values extension
757 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
758
759 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000761 combined with a lookup table of jump addresses. However, since the
762 indirect jump instruction is shared by all opcodes, the CPU will have a
763 hard time making the right prediction for where to jump next (actually,
764 it will be always wrong except in the uncommon case of a sequence of
765 several identical opcodes).
766
767 "Threaded code" in contrast, uses an explicit jump table and an explicit
768 indirect jump instruction at the end of each opcode. Since the jump
769 instruction is at a different address for each opcode, the CPU will make a
770 separate prediction for each of these instructions, which is equivalent to
771 predicting the second opcode of each opcode pair. These predictions have
772 a much better chance to turn out valid, especially in small bytecode loops.
773
774 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000776 and potentially many more instructions (depending on the pipeline width).
777 A correctly predicted branch, however, is nearly free.
778
779 At the time of this writing, the "threaded code" version is up to 15-20%
780 faster than the normal "switch" version, depending on the compiler and the
781 CPU architecture.
782
783 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
784 because it would render the measurements invalid.
785
786
787 NOTE: care must be taken that the compiler doesn't try to "optimize" the
788 indirect jumps by sharing them between all opcodes. Such optimizations
789 can be disabled on gcc by using the -fno-gcse flag (or possibly
790 -fno-crossjumping).
791*/
792
Antoine Pitrou042b1282010-08-13 21:15:58 +0000793#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000794#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000795#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000796#endif
797
Antoine Pitrou042b1282010-08-13 21:15:58 +0000798#ifdef HAVE_COMPUTED_GOTOS
799 #ifndef USE_COMPUTED_GOTOS
800 #define USE_COMPUTED_GOTOS 1
801 #endif
802#else
803 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
804 #error "Computed gotos are not supported on this compiler."
805 #endif
806 #undef USE_COMPUTED_GOTOS
807 #define USE_COMPUTED_GOTOS 0
808#endif
809
810#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000811/* Import the static jump table */
812#include "opcode_targets.h"
813
Antoine Pitroub52ec782009-01-25 16:34:23 +0000814#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 TARGET_##op: \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000817
Antoine Pitroub52ec782009-01-25 16:34:23 +0000818#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 { \
820 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
821 FAST_DISPATCH(); \
822 } \
823 continue; \
824 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000825
826#ifdef LLTRACE
827#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700829 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300831 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300832 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 } \
834 goto fast_next_opcode; \
835 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000836#else
837#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700839 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300841 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300842 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 } \
844 goto fast_next_opcode; \
845 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000846#endif
847
848#else
849#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 case op:
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300851
Antoine Pitroub52ec782009-01-25 16:34:23 +0000852#define DISPATCH() continue
853#define FAST_DISPATCH() goto fast_next_opcode
854#endif
855
856
Neal Norwitza81d2202002-07-14 00:27:26 +0000857/* Tuple access macros */
858
859#ifndef Py_DEBUG
860#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
861#else
862#define GETITEM(v, i) PyTuple_GetItem((v), (i))
863#endif
864
Guido van Rossum374a9221991-04-04 10:40:29 +0000865/* Code access macros */
866
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300867/* The integer overflow is checked by an assertion below. */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300868#define INSTR_OFFSET() (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300869#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300870 _Py_CODEUNIT word = *next_instr; \
871 opcode = _Py_OPCODE(word); \
872 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300873 next_instr++; \
874 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300875#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
876#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000877
Raymond Hettingerf606f872003-03-16 03:11:04 +0000878/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 Some opcodes tend to come in pairs thus making it possible to
880 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300881 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 Verifying the prediction costs a single high-speed test of a register
884 variable against a constant. If the pairing was good, then the
885 processor's own internal branch predication has a high likelihood of
886 success, resulting in a nearly zero-overhead transition to the
887 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300888 including its unpredictable switch-case branch. Combined with the
889 processor's internal branch prediction, a successful PREDICT has the
890 effect of making the two opcodes run as if they were a single new opcode
891 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000892
Georg Brandl86b2fb92008-07-16 03:43:04 +0000893 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 predictions turned-on and interpret the results as if some opcodes
895 had been combined or turn-off predictions so that the opcode frequency
896 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000897
898 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 the CPU to record separate branch prediction information for each
900 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000901
Raymond Hettingerf606f872003-03-16 03:11:04 +0000902*/
903
Antoine Pitrou042b1282010-08-13 21:15:58 +0000904#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000906#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300907#define PREDICT(op) \
908 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300909 _Py_CODEUNIT word = *next_instr; \
910 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300911 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300912 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300913 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300914 goto PRED_##op; \
915 } \
916 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000917#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300918#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000919
Raymond Hettingerf606f872003-03-16 03:11:04 +0000920
Guido van Rossum374a9221991-04-04 10:40:29 +0000921/* Stack manipulation macros */
922
Martin v. Löwis18e16552006-02-15 17:27:45 +0000923/* The stack can grow at most MAXINT deep, as co_nlocals and
924 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000925#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
926#define EMPTY() (STACK_LEVEL() == 0)
927#define TOP() (stack_pointer[-1])
928#define SECOND() (stack_pointer[-2])
929#define THIRD() (stack_pointer[-3])
930#define FOURTH() (stack_pointer[-4])
931#define PEEK(n) (stack_pointer[-(n)])
932#define SET_TOP(v) (stack_pointer[-1] = (v))
933#define SET_SECOND(v) (stack_pointer[-2] = (v))
934#define SET_THIRD(v) (stack_pointer[-3] = (v))
935#define SET_FOURTH(v) (stack_pointer[-4] = (v))
936#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
937#define BASIC_STACKADJ(n) (stack_pointer += n)
938#define BASIC_PUSH(v) (*stack_pointer++ = (v))
939#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000940
Guido van Rossum96a42c81992-01-12 02:29:51 +0000941#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000943 lltrace && prtrace(TOP(), "push")); \
944 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000946 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000948 lltrace && prtrace(TOP(), "stackadj")); \
949 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +0000950#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000951 prtrace((STACK_POINTER)[-1], "ext_pop")), \
952 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000953#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000954#define PUSH(v) BASIC_PUSH(v)
955#define POP() BASIC_POP()
956#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000957#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000958#endif
959
Guido van Rossum681d79a1995-07-18 14:51:37 +0000960/* Local variable macros */
961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000963
964/* The SETLOCAL() macro must not DECREF the local variable in-place and
965 then store the new value; it must copy the old value to a temporary
966 value, then store the new value, and then DECREF the temporary value.
967 This is because it is possible that during the DECREF the frame is
968 accessed by other code (e.g. a __del__ method or gc.collect()) and the
969 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000971 GETLOCAL(i) = value; \
972 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000973
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000974
975#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 while (STACK_LEVEL() > (b)->b_level) { \
977 PyObject *v = POP(); \
978 Py_XDECREF(v); \
979 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000980
981#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300982 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 PyObject *type, *value, *traceback; \
984 assert(STACK_LEVEL() >= (b)->b_level + 3); \
985 while (STACK_LEVEL() > (b)->b_level + 3) { \
986 value = POP(); \
987 Py_XDECREF(value); \
988 } \
989 type = tstate->exc_type; \
990 value = tstate->exc_value; \
991 traceback = tstate->exc_traceback; \
992 tstate->exc_type = POP(); \
993 tstate->exc_value = POP(); \
994 tstate->exc_traceback = POP(); \
995 Py_XDECREF(type); \
996 Py_XDECREF(value); \
997 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300998 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000999
Guido van Rossuma027efa1997-05-05 20:56:21 +00001000/* Start of code */
1001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 /* push frame */
1003 if (Py_EnterRecursiveCall(""))
1004 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 if (tstate->use_tracing) {
1009 if (tstate->c_tracefunc != NULL) {
1010 /* tstate->c_tracefunc, if defined, is a
1011 function that will be called on *every* entry
1012 to a code block. Its return value, if not
1013 None, is a function that will be called at
1014 the start of each executed line of code.
1015 (Actually, the function must return itself
1016 in order to continue tracing.) The trace
1017 functions are called with three arguments:
1018 a pointer to the current frame, a string
1019 indicating why the function is called, and
1020 an argument which depends on the situation.
1021 The global trace function is also called
1022 whenever an exception is detected. */
1023 if (call_trace_protected(tstate->c_tracefunc,
1024 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001025 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 /* Trace function raised an error */
1027 goto exit_eval_frame;
1028 }
1029 }
1030 if (tstate->c_profilefunc != NULL) {
1031 /* Similar for c_profilefunc, except it needn't
1032 return itself and isn't called for "line" events */
1033 if (call_trace_protected(tstate->c_profilefunc,
1034 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001035 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 /* Profile function raised an error */
1037 goto exit_eval_frame;
1038 }
1039 }
1040 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001041
Łukasz Langaa785c872016-09-09 17:37:37 -07001042 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1043 dtrace_function_entry(f);
1044
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);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001052 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1053 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1054 first_instr = (_Py_CODEUNIT *) 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 Storchakaab874002016-09-11 13:48:15 +03001070 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001071 next_instr = first_instr;
1072 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001073 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1074 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001075 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 stack_pointer = f->f_stacktop;
1077 assert(stack_pointer != NULL);
1078 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001079 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001080
Yury Selivanoveb636452016-09-08 22:01:51 -07001081 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01001082 if (!throwflag && f->f_exc_type != NULL && f->f_exc_type != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 /* We were in an except handler when we left,
1084 restore the exception state which was put aside
1085 (see YIELD_VALUE). */
Benjamin Peterson87880242011-07-03 16:48:31 -05001086 swap_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 }
Benjamin Peterson87880242011-07-03 16:48:31 -05001088 else
1089 save_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001091
Tim Peters5ca576e2001-06-18 22:08:13 +00001092#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001093 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001094#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 why = WHY_NOT;
Guido van Rossumac7be682001-01-17 15:42:30 +00001097
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001098 if (throwflag) /* support for generator.throw() */
1099 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001100
Victor Stinnerace47d72013-07-18 01:41:08 +02001101#ifdef Py_DEBUG
1102 /* PyEval_EvalFrameEx() must not be called with an exception set,
1103 because it may clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001104 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +02001105 assert(!PyErr_Occurred());
1106#endif
1107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1110 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +02001111 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 /* Do periodic things. Doing this every time through
1114 the loop would add too much overhead, so we do it
1115 only every Nth instruction. We also do it if
1116 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1117 event needs attention (e.g. a signal handler or
1118 async I/O handler); see Py_AddPendingCall() and
1119 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 if (_Py_atomic_load_relaxed(&eval_breaker)) {
Yury Selivanove89f95b2017-06-09 17:06:39 -04001122 if (_Py_OPCODE(*next_instr) == SETUP_FINALLY ||
1123 _Py_OPCODE(*next_instr) == YIELD_FROM) {
1124 /* Two cases where we skip running signal handlers and other
1125 pending calls:
1126 - If we're about to enter the try: of a try/finally (not
1127 *very* useful, but might help in some cases and it's
1128 traditional)
1129 - If we're resuming a chain of nested 'yield from' or
1130 'await' calls, then each frame is parked with YIELD_FROM
1131 as its next opcode. If the user hit control-C we want to
1132 wait until we've reached the innermost frame before
1133 running the signal handler and raising KeyboardInterrupt
1134 (see bpo-30039).
1135 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 goto fast_next_opcode;
1137 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001139 if (Py_MakePendingCalls() < 0)
1140 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001142#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001143 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 /* Give another thread a chance */
1145 if (PyThreadState_Swap(NULL) != tstate)
1146 Py_FatalError("ceval: tstate mix-up");
1147 drop_gil(tstate);
1148
1149 /* Other threads may run now */
1150
1151 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001152
1153 /* Check if we should make a quick exit. */
1154 if (_Py_Finalizing && _Py_Finalizing != tstate) {
1155 drop_gil(tstate);
1156 PyThread_exit_thread();
1157 }
1158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 if (PyThreadState_Swap(tstate) != NULL)
1160 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001162#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 /* Check for asynchronous exceptions. */
1164 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001165 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 tstate->async_exc = NULL;
1167 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001168 PyErr_SetNone(exc);
1169 Py_DECREF(exc);
1170 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 }
1172 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 fast_next_opcode:
1175 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001176
Łukasz Langaa785c872016-09-09 17:37:37 -07001177 if (PyDTrace_LINE_ENABLED())
1178 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001183 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001184 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 /* see maybe_call_line_trace
1186 for expository comments */
1187 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 err = maybe_call_line_trace(tstate->c_tracefunc,
1190 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001191 tstate, f,
1192 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 /* Reload possibly changed frame fields */
1194 JUMPTO(f->f_lasti);
1195 if (f->f_stacktop != NULL) {
1196 stack_pointer = f->f_stacktop;
1197 f->f_stacktop = NULL;
1198 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001199 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001201 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001205
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001206 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001207 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001208#ifdef DYNAMIC_EXECUTION_PROFILE
1209#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 dxpairs[lastopcode][opcode]++;
1211 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001212#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001214#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001215
Guido van Rossum96a42c81992-01-12 02:29:51 +00001216#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 if (lltrace) {
1220 if (HAS_ARG(opcode)) {
1221 printf("%d: %d, %d\n",
1222 f->f_lasti, opcode, oparg);
1223 }
1224 else {
1225 printf("%d: %d\n",
1226 f->f_lasti, opcode);
1227 }
1228 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001229#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 /* BEWARE!
1234 It is essential that any operation that fails sets either
1235 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1236 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 TARGET(NOP)
1239 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001240
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001241 TARGET(LOAD_FAST) {
1242 PyObject *value = GETLOCAL(oparg);
1243 if (value == NULL) {
1244 format_exc_check_arg(PyExc_UnboundLocalError,
1245 UNBOUNDLOCAL_ERROR_MSG,
1246 PyTuple_GetItem(co->co_varnames, oparg));
1247 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001249 Py_INCREF(value);
1250 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001252 }
1253
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001254 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001255 TARGET(LOAD_CONST) {
1256 PyObject *value = GETITEM(consts, oparg);
1257 Py_INCREF(value);
1258 PUSH(value);
1259 FAST_DISPATCH();
1260 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001261
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001262 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001263 TARGET(STORE_FAST) {
1264 PyObject *value = POP();
1265 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001267 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001268
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001269 TARGET(POP_TOP) {
1270 PyObject *value = POP();
1271 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001273 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001274
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001275 TARGET(ROT_TWO) {
1276 PyObject *top = TOP();
1277 PyObject *second = SECOND();
1278 SET_TOP(second);
1279 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001281 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001282
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001283 TARGET(ROT_THREE) {
1284 PyObject *top = TOP();
1285 PyObject *second = SECOND();
1286 PyObject *third = THIRD();
1287 SET_TOP(second);
1288 SET_SECOND(third);
1289 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001291 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001292
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001293 TARGET(DUP_TOP) {
1294 PyObject *top = TOP();
1295 Py_INCREF(top);
1296 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001298 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001299
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001300 TARGET(DUP_TOP_TWO) {
1301 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001302 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001303 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001304 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001305 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001306 SET_TOP(top);
1307 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001308 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001309 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001310
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001311 TARGET(UNARY_POSITIVE) {
1312 PyObject *value = TOP();
1313 PyObject *res = PyNumber_Positive(value);
1314 Py_DECREF(value);
1315 SET_TOP(res);
1316 if (res == NULL)
1317 goto error;
1318 DISPATCH();
1319 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001320
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001321 TARGET(UNARY_NEGATIVE) {
1322 PyObject *value = TOP();
1323 PyObject *res = PyNumber_Negative(value);
1324 Py_DECREF(value);
1325 SET_TOP(res);
1326 if (res == NULL)
1327 goto error;
1328 DISPATCH();
1329 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001330
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001331 TARGET(UNARY_NOT) {
1332 PyObject *value = TOP();
1333 int err = PyObject_IsTrue(value);
1334 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 if (err == 0) {
1336 Py_INCREF(Py_True);
1337 SET_TOP(Py_True);
1338 DISPATCH();
1339 }
1340 else if (err > 0) {
1341 Py_INCREF(Py_False);
1342 SET_TOP(Py_False);
1343 err = 0;
1344 DISPATCH();
1345 }
1346 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001347 goto error;
1348 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001349
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001350 TARGET(UNARY_INVERT) {
1351 PyObject *value = TOP();
1352 PyObject *res = PyNumber_Invert(value);
1353 Py_DECREF(value);
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_POWER) {
1361 PyObject *exp = POP();
1362 PyObject *base = TOP();
1363 PyObject *res = PyNumber_Power(base, exp, Py_None);
1364 Py_DECREF(base);
1365 Py_DECREF(exp);
1366 SET_TOP(res);
1367 if (res == NULL)
1368 goto error;
1369 DISPATCH();
1370 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001371
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001372 TARGET(BINARY_MULTIPLY) {
1373 PyObject *right = POP();
1374 PyObject *left = TOP();
1375 PyObject *res = PyNumber_Multiply(left, right);
1376 Py_DECREF(left);
1377 Py_DECREF(right);
1378 SET_TOP(res);
1379 if (res == NULL)
1380 goto error;
1381 DISPATCH();
1382 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001383
Benjamin Petersond51374e2014-04-09 23:55:56 -04001384 TARGET(BINARY_MATRIX_MULTIPLY) {
1385 PyObject *right = POP();
1386 PyObject *left = TOP();
1387 PyObject *res = PyNumber_MatrixMultiply(left, right);
1388 Py_DECREF(left);
1389 Py_DECREF(right);
1390 SET_TOP(res);
1391 if (res == NULL)
1392 goto error;
1393 DISPATCH();
1394 }
1395
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001396 TARGET(BINARY_TRUE_DIVIDE) {
1397 PyObject *divisor = POP();
1398 PyObject *dividend = TOP();
1399 PyObject *quotient = PyNumber_TrueDivide(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 Rossumac7be682001-01-17 15:42:30 +00001407
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001408 TARGET(BINARY_FLOOR_DIVIDE) {
1409 PyObject *divisor = POP();
1410 PyObject *dividend = TOP();
1411 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1412 Py_DECREF(dividend);
1413 Py_DECREF(divisor);
1414 SET_TOP(quotient);
1415 if (quotient == NULL)
1416 goto error;
1417 DISPATCH();
1418 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001419
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001420 TARGET(BINARY_MODULO) {
1421 PyObject *divisor = POP();
1422 PyObject *dividend = TOP();
Martijn Pieters53039ad2017-02-27 16:08:01 +00001423 PyObject *res;
1424 if (PyUnicode_CheckExact(dividend) && (
1425 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1426 // fast path; string formatting, but not if the RHS is a str subclass
1427 // (see issue28598)
1428 res = PyUnicode_Format(dividend, divisor);
1429 } else {
1430 res = PyNumber_Remainder(dividend, divisor);
1431 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001432 Py_DECREF(divisor);
1433 Py_DECREF(dividend);
1434 SET_TOP(res);
1435 if (res == NULL)
1436 goto error;
1437 DISPATCH();
1438 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001439
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001440 TARGET(BINARY_ADD) {
1441 PyObject *right = POP();
1442 PyObject *left = TOP();
1443 PyObject *sum;
1444 if (PyUnicode_CheckExact(left) &&
1445 PyUnicode_CheckExact(right)) {
1446 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001447 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001448 }
1449 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001450 sum = PyNumber_Add(left, right);
1451 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001452 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001453 Py_DECREF(right);
1454 SET_TOP(sum);
1455 if (sum == NULL)
1456 goto error;
1457 DISPATCH();
1458 }
1459
1460 TARGET(BINARY_SUBTRACT) {
1461 PyObject *right = POP();
1462 PyObject *left = TOP();
1463 PyObject *diff = PyNumber_Subtract(left, right);
1464 Py_DECREF(right);
1465 Py_DECREF(left);
1466 SET_TOP(diff);
1467 if (diff == NULL)
1468 goto error;
1469 DISPATCH();
1470 }
1471
1472 TARGET(BINARY_SUBSCR) {
1473 PyObject *sub = POP();
1474 PyObject *container = TOP();
1475 PyObject *res = PyObject_GetItem(container, sub);
1476 Py_DECREF(container);
1477 Py_DECREF(sub);
1478 SET_TOP(res);
1479 if (res == NULL)
1480 goto error;
1481 DISPATCH();
1482 }
1483
1484 TARGET(BINARY_LSHIFT) {
1485 PyObject *right = POP();
1486 PyObject *left = TOP();
1487 PyObject *res = PyNumber_Lshift(left, right);
1488 Py_DECREF(left);
1489 Py_DECREF(right);
1490 SET_TOP(res);
1491 if (res == NULL)
1492 goto error;
1493 DISPATCH();
1494 }
1495
1496 TARGET(BINARY_RSHIFT) {
1497 PyObject *right = POP();
1498 PyObject *left = TOP();
1499 PyObject *res = PyNumber_Rshift(left, right);
1500 Py_DECREF(left);
1501 Py_DECREF(right);
1502 SET_TOP(res);
1503 if (res == NULL)
1504 goto error;
1505 DISPATCH();
1506 }
1507
1508 TARGET(BINARY_AND) {
1509 PyObject *right = POP();
1510 PyObject *left = TOP();
1511 PyObject *res = PyNumber_And(left, right);
1512 Py_DECREF(left);
1513 Py_DECREF(right);
1514 SET_TOP(res);
1515 if (res == NULL)
1516 goto error;
1517 DISPATCH();
1518 }
1519
1520 TARGET(BINARY_XOR) {
1521 PyObject *right = POP();
1522 PyObject *left = TOP();
1523 PyObject *res = PyNumber_Xor(left, right);
1524 Py_DECREF(left);
1525 Py_DECREF(right);
1526 SET_TOP(res);
1527 if (res == NULL)
1528 goto error;
1529 DISPATCH();
1530 }
1531
1532 TARGET(BINARY_OR) {
1533 PyObject *right = POP();
1534 PyObject *left = TOP();
1535 PyObject *res = PyNumber_Or(left, right);
1536 Py_DECREF(left);
1537 Py_DECREF(right);
1538 SET_TOP(res);
1539 if (res == NULL)
1540 goto error;
1541 DISPATCH();
1542 }
1543
1544 TARGET(LIST_APPEND) {
1545 PyObject *v = POP();
1546 PyObject *list = PEEK(oparg);
1547 int err;
1548 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001550 if (err != 0)
1551 goto error;
1552 PREDICT(JUMP_ABSOLUTE);
1553 DISPATCH();
1554 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001555
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001556 TARGET(SET_ADD) {
1557 PyObject *v = POP();
1558 PyObject *set = stack_pointer[-oparg];
1559 int err;
1560 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001562 if (err != 0)
1563 goto error;
1564 PREDICT(JUMP_ABSOLUTE);
1565 DISPATCH();
1566 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001567
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001568 TARGET(INPLACE_POWER) {
1569 PyObject *exp = POP();
1570 PyObject *base = TOP();
1571 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1572 Py_DECREF(base);
1573 Py_DECREF(exp);
1574 SET_TOP(res);
1575 if (res == NULL)
1576 goto error;
1577 DISPATCH();
1578 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001579
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001580 TARGET(INPLACE_MULTIPLY) {
1581 PyObject *right = POP();
1582 PyObject *left = TOP();
1583 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1584 Py_DECREF(left);
1585 Py_DECREF(right);
1586 SET_TOP(res);
1587 if (res == NULL)
1588 goto error;
1589 DISPATCH();
1590 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001591
Benjamin Petersond51374e2014-04-09 23:55:56 -04001592 TARGET(INPLACE_MATRIX_MULTIPLY) {
1593 PyObject *right = POP();
1594 PyObject *left = TOP();
1595 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1596 Py_DECREF(left);
1597 Py_DECREF(right);
1598 SET_TOP(res);
1599 if (res == NULL)
1600 goto error;
1601 DISPATCH();
1602 }
1603
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001604 TARGET(INPLACE_TRUE_DIVIDE) {
1605 PyObject *divisor = POP();
1606 PyObject *dividend = TOP();
1607 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1608 Py_DECREF(dividend);
1609 Py_DECREF(divisor);
1610 SET_TOP(quotient);
1611 if (quotient == NULL)
1612 goto error;
1613 DISPATCH();
1614 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001615
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001616 TARGET(INPLACE_FLOOR_DIVIDE) {
1617 PyObject *divisor = POP();
1618 PyObject *dividend = TOP();
1619 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1620 Py_DECREF(dividend);
1621 Py_DECREF(divisor);
1622 SET_TOP(quotient);
1623 if (quotient == NULL)
1624 goto error;
1625 DISPATCH();
1626 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001627
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001628 TARGET(INPLACE_MODULO) {
1629 PyObject *right = POP();
1630 PyObject *left = TOP();
1631 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1632 Py_DECREF(left);
1633 Py_DECREF(right);
1634 SET_TOP(mod);
1635 if (mod == NULL)
1636 goto error;
1637 DISPATCH();
1638 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001639
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001640 TARGET(INPLACE_ADD) {
1641 PyObject *right = POP();
1642 PyObject *left = TOP();
1643 PyObject *sum;
1644 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1645 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001646 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001647 }
1648 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001649 sum = PyNumber_InPlaceAdd(left, right);
1650 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001651 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001652 Py_DECREF(right);
1653 SET_TOP(sum);
1654 if (sum == NULL)
1655 goto error;
1656 DISPATCH();
1657 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001658
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001659 TARGET(INPLACE_SUBTRACT) {
1660 PyObject *right = POP();
1661 PyObject *left = TOP();
1662 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1663 Py_DECREF(left);
1664 Py_DECREF(right);
1665 SET_TOP(diff);
1666 if (diff == NULL)
1667 goto error;
1668 DISPATCH();
1669 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001670
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001671 TARGET(INPLACE_LSHIFT) {
1672 PyObject *right = POP();
1673 PyObject *left = TOP();
1674 PyObject *res = PyNumber_InPlaceLshift(left, right);
1675 Py_DECREF(left);
1676 Py_DECREF(right);
1677 SET_TOP(res);
1678 if (res == NULL)
1679 goto error;
1680 DISPATCH();
1681 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001682
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001683 TARGET(INPLACE_RSHIFT) {
1684 PyObject *right = POP();
1685 PyObject *left = TOP();
1686 PyObject *res = PyNumber_InPlaceRshift(left, right);
1687 Py_DECREF(left);
1688 Py_DECREF(right);
1689 SET_TOP(res);
1690 if (res == NULL)
1691 goto error;
1692 DISPATCH();
1693 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001694
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001695 TARGET(INPLACE_AND) {
1696 PyObject *right = POP();
1697 PyObject *left = TOP();
1698 PyObject *res = PyNumber_InPlaceAnd(left, right);
1699 Py_DECREF(left);
1700 Py_DECREF(right);
1701 SET_TOP(res);
1702 if (res == NULL)
1703 goto error;
1704 DISPATCH();
1705 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001706
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001707 TARGET(INPLACE_XOR) {
1708 PyObject *right = POP();
1709 PyObject *left = TOP();
1710 PyObject *res = PyNumber_InPlaceXor(left, right);
1711 Py_DECREF(left);
1712 Py_DECREF(right);
1713 SET_TOP(res);
1714 if (res == NULL)
1715 goto error;
1716 DISPATCH();
1717 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001718
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001719 TARGET(INPLACE_OR) {
1720 PyObject *right = POP();
1721 PyObject *left = TOP();
1722 PyObject *res = PyNumber_InPlaceOr(left, right);
1723 Py_DECREF(left);
1724 Py_DECREF(right);
1725 SET_TOP(res);
1726 if (res == NULL)
1727 goto error;
1728 DISPATCH();
1729 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001730
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001731 TARGET(STORE_SUBSCR) {
1732 PyObject *sub = TOP();
1733 PyObject *container = SECOND();
1734 PyObject *v = THIRD();
1735 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001736 STACKADJ(-3);
Martin Panter95f53c12016-07-18 08:23:26 +00001737 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001738 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001740 Py_DECREF(container);
1741 Py_DECREF(sub);
1742 if (err != 0)
1743 goto error;
1744 DISPATCH();
1745 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001746
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001747 TARGET(STORE_ANNOTATION) {
1748 _Py_IDENTIFIER(__annotations__);
1749 PyObject *ann_dict;
1750 PyObject *ann = POP();
1751 PyObject *name = GETITEM(names, oparg);
1752 int err;
1753 if (f->f_locals == NULL) {
1754 PyErr_Format(PyExc_SystemError,
1755 "no locals found when storing annotation");
1756 Py_DECREF(ann);
1757 goto error;
1758 }
1759 /* first try to get __annotations__ from locals... */
1760 if (PyDict_CheckExact(f->f_locals)) {
1761 ann_dict = _PyDict_GetItemId(f->f_locals,
1762 &PyId___annotations__);
1763 if (ann_dict == NULL) {
1764 PyErr_SetString(PyExc_NameError,
1765 "__annotations__ not found");
1766 Py_DECREF(ann);
1767 goto error;
1768 }
1769 Py_INCREF(ann_dict);
1770 }
1771 else {
1772 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
1773 if (ann_str == NULL) {
1774 Py_DECREF(ann);
1775 goto error;
1776 }
1777 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
1778 if (ann_dict == NULL) {
1779 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
1780 PyErr_SetString(PyExc_NameError,
1781 "__annotations__ not found");
1782 }
1783 Py_DECREF(ann);
1784 goto error;
1785 }
1786 }
1787 /* ...if succeeded, __annotations__[name] = ann */
1788 if (PyDict_CheckExact(ann_dict)) {
1789 err = PyDict_SetItem(ann_dict, name, ann);
1790 }
1791 else {
1792 err = PyObject_SetItem(ann_dict, name, ann);
1793 }
1794 Py_DECREF(ann_dict);
Yury Selivanov50c584f2016-09-08 23:38:21 -07001795 Py_DECREF(ann);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001796 if (err != 0) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001797 goto error;
1798 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001799 DISPATCH();
1800 }
1801
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001802 TARGET(DELETE_SUBSCR) {
1803 PyObject *sub = TOP();
1804 PyObject *container = SECOND();
1805 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001806 STACKADJ(-2);
Martin Panter95f53c12016-07-18 08:23:26 +00001807 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001808 err = PyObject_DelItem(container, sub);
1809 Py_DECREF(container);
1810 Py_DECREF(sub);
1811 if (err != 0)
1812 goto error;
1813 DISPATCH();
1814 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001815
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001816 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001817 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001818 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001819 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001820 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001821 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 PyErr_SetString(PyExc_RuntimeError,
1823 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001824 Py_DECREF(value);
1825 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 }
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001827 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001828 Py_DECREF(value);
1829 if (res == NULL)
1830 goto error;
1831 Py_DECREF(res);
1832 DISPATCH();
1833 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001834
Thomas Wouters434d0822000-08-24 20:11:32 +00001835#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001837#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001838 TARGET(RAISE_VARARGS) {
1839 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840 switch (oparg) {
1841 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001842 cause = POP(); /* cause */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001843 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001844 exc = POP(); /* exc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 case 0: /* Fallthrough */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001846 if (do_raise(exc, cause)) {
1847 why = WHY_EXCEPTION;
1848 goto fast_block_end;
1849 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850 break;
1851 default:
1852 PyErr_SetString(PyExc_SystemError,
1853 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 break;
1855 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001856 goto error;
1857 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001858
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001859 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 retval = POP();
1861 why = WHY_RETURN;
1862 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001863 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001864
Yury Selivanov75445082015-05-11 22:57:16 -04001865 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001866 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001867 PyObject *iter = NULL;
1868 PyObject *awaitable = NULL;
1869 PyObject *obj = TOP();
1870 PyTypeObject *type = Py_TYPE(obj);
1871
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001872 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001873 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001874 }
Yury Selivanov75445082015-05-11 22:57:16 -04001875
1876 if (getter != NULL) {
1877 iter = (*getter)(obj);
1878 Py_DECREF(obj);
1879 if (iter == NULL) {
1880 SET_TOP(NULL);
1881 goto error;
1882 }
1883 }
1884 else {
1885 SET_TOP(NULL);
1886 PyErr_Format(
1887 PyExc_TypeError,
1888 "'async for' requires an object with "
1889 "__aiter__ method, got %.100s",
1890 type->tp_name);
1891 Py_DECREF(obj);
1892 goto error;
1893 }
1894
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001895 if (Py_TYPE(iter)->tp_as_async != NULL &&
1896 Py_TYPE(iter)->tp_as_async->am_anext != NULL) {
1897
1898 /* Starting with CPython 3.5.2 __aiter__ should return
1899 asynchronous iterators directly (not awaitables that
1900 resolve to asynchronous iterators.)
1901
1902 Therefore, we check if the object that was returned
1903 from __aiter__ has an __anext__ method. If it does,
1904 we wrap it in an awaitable that resolves to `iter`.
1905
1906 See http://bugs.python.org/issue27243 for more
1907 details.
1908 */
1909
1910 PyObject *wrapper = _PyAIterWrapper_New(iter);
1911 Py_DECREF(iter);
1912 SET_TOP(wrapper);
1913 DISPATCH();
1914 }
1915
Yury Selivanov5376ba92015-06-22 12:19:30 -04001916 awaitable = _PyCoro_GetAwaitableIter(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001917 if (awaitable == NULL) {
Yury Selivanovdea51012017-03-02 22:20:00 -05001918 _PyErr_FormatFromCause(
Yury Selivanov75445082015-05-11 22:57:16 -04001919 PyExc_TypeError,
1920 "'async for' received an invalid object "
1921 "from __aiter__: %.100s",
1922 Py_TYPE(iter)->tp_name);
1923
Yury Selivanovdea51012017-03-02 22:20:00 -05001924 SET_TOP(NULL);
Yury Selivanov75445082015-05-11 22:57:16 -04001925 Py_DECREF(iter);
1926 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001927 } else {
Yury Selivanov75445082015-05-11 22:57:16 -04001928 Py_DECREF(iter);
1929
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001930 if (PyErr_WarnFormat(
Yury Selivanov2edd8a12016-11-08 15:13:07 -05001931 PyExc_DeprecationWarning, 1,
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001932 "'%.100s' implements legacy __aiter__ protocol; "
1933 "__aiter__ should return an asynchronous "
1934 "iterator, not awaitable",
1935 type->tp_name))
1936 {
1937 /* Warning was converted to an error. */
1938 Py_DECREF(awaitable);
1939 SET_TOP(NULL);
1940 goto error;
1941 }
1942 }
1943
Yury Selivanov75445082015-05-11 22:57:16 -04001944 SET_TOP(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001945 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001946 DISPATCH();
1947 }
1948
1949 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001950 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001951 PyObject *next_iter = NULL;
1952 PyObject *awaitable = NULL;
1953 PyObject *aiter = TOP();
1954 PyTypeObject *type = Py_TYPE(aiter);
1955
Yury Selivanoveb636452016-09-08 22:01:51 -07001956 if (PyAsyncGen_CheckExact(aiter)) {
1957 awaitable = type->tp_as_async->am_anext(aiter);
1958 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001959 goto error;
1960 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001961 } else {
1962 if (type->tp_as_async != NULL){
1963 getter = type->tp_as_async->am_anext;
1964 }
Yury Selivanov75445082015-05-11 22:57:16 -04001965
Yury Selivanoveb636452016-09-08 22:01:51 -07001966 if (getter != NULL) {
1967 next_iter = (*getter)(aiter);
1968 if (next_iter == NULL) {
1969 goto error;
1970 }
1971 }
1972 else {
1973 PyErr_Format(
1974 PyExc_TypeError,
1975 "'async for' requires an iterator with "
1976 "__anext__ method, got %.100s",
1977 type->tp_name);
1978 goto error;
1979 }
Yury Selivanov75445082015-05-11 22:57:16 -04001980
Yury Selivanoveb636452016-09-08 22:01:51 -07001981 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1982 if (awaitable == NULL) {
Yury Selivanovdea51012017-03-02 22:20:00 -05001983 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001984 PyExc_TypeError,
1985 "'async for' received an invalid object "
1986 "from __anext__: %.100s",
1987 Py_TYPE(next_iter)->tp_name);
1988
1989 Py_DECREF(next_iter);
1990 goto error;
1991 } else {
1992 Py_DECREF(next_iter);
1993 }
1994 }
Yury Selivanov75445082015-05-11 22:57:16 -04001995
1996 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001997 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001998 DISPATCH();
1999 }
2000
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002001 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002002 TARGET(GET_AWAITABLE) {
2003 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002004 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002005
2006 Py_DECREF(iterable);
2007
Yury Selivanovc724bae2016-03-02 11:30:46 -05002008 if (iter != NULL && PyCoro_CheckExact(iter)) {
2009 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2010 if (yf != NULL) {
2011 /* `iter` is a coroutine object that is being
2012 awaited, `yf` is a pointer to the current awaitable
2013 being awaited on. */
2014 Py_DECREF(yf);
2015 Py_CLEAR(iter);
2016 PyErr_SetString(
2017 PyExc_RuntimeError,
2018 "coroutine is being awaited already");
2019 /* The code below jumps to `error` if `iter` is NULL. */
2020 }
2021 }
2022
Yury Selivanov75445082015-05-11 22:57:16 -04002023 SET_TOP(iter); /* Even if it's NULL */
2024
2025 if (iter == NULL) {
2026 goto error;
2027 }
2028
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002029 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002030 DISPATCH();
2031 }
2032
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002033 TARGET(YIELD_FROM) {
2034 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002035 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002036 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002037 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2038 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002039 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002040 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002041 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002042 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002043 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002044 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002045 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002046 Py_DECREF(v);
2047 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002048 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002049 if (tstate->c_tracefunc != NULL
2050 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002051 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002052 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002053 if (err < 0)
2054 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002055 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002056 SET_TOP(val);
2057 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002058 }
Martin Panter95f53c12016-07-18 08:23:26 +00002059 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002060 f->f_stacktop = stack_pointer;
2061 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002062 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002063 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002064 f->f_lasti -= sizeof(_Py_CODEUNIT);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002065 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002066 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002067
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002068 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002070
2071 if (co->co_flags & CO_ASYNC_GENERATOR) {
2072 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2073 Py_DECREF(retval);
2074 if (w == NULL) {
2075 retval = NULL;
2076 goto error;
2077 }
2078 retval = w;
2079 }
2080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002081 f->f_stacktop = stack_pointer;
2082 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002083 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002084 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002085
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002086 TARGET(POP_EXCEPT) {
2087 PyTryBlock *b = PyFrame_BlockPop(f);
2088 if (b->b_type != EXCEPT_HANDLER) {
2089 PyErr_SetString(PyExc_SystemError,
2090 "popped block is not an except handler");
2091 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002092 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002093 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002094 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002095 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002096
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002097 PREDICTED(POP_BLOCK);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002098 TARGET(POP_BLOCK) {
2099 PyTryBlock *b = PyFrame_BlockPop(f);
2100 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002102 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002104 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002105 TARGET(END_FINALLY) {
2106 PyObject *status = POP();
2107 if (PyLong_Check(status)) {
2108 why = (enum why_code) PyLong_AS_LONG(status);
2109 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002110 if (why == WHY_RETURN ||
2111 why == WHY_CONTINUE)
2112 retval = POP();
2113 if (why == WHY_SILENCED) {
2114 /* An exception was silenced by 'with', we must
2115 manually unwind the EXCEPT_HANDLER block which was
2116 created when the exception was caught, otherwise
2117 the stack will be in an inconsistent state. */
2118 PyTryBlock *b = PyFrame_BlockPop(f);
2119 assert(b->b_type == EXCEPT_HANDLER);
2120 UNWIND_EXCEPT_HANDLER(b);
2121 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002122 Py_DECREF(status);
2123 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002125 Py_DECREF(status);
2126 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002127 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002128 else if (PyExceptionClass_Check(status)) {
2129 PyObject *exc = POP();
2130 PyObject *tb = POP();
2131 PyErr_Restore(status, exc, tb);
2132 why = WHY_EXCEPTION;
2133 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002134 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002135 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002136 PyErr_SetString(PyExc_SystemError,
2137 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002138 Py_DECREF(status);
2139 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002141 Py_DECREF(status);
2142 DISPATCH();
2143 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002144
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002145 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002146 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002147
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002148 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002149 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002150 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
2151 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002152 PyErr_SetString(PyExc_NameError,
2153 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002154 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002155 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002156 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002157 }
2158 else {
2159 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2160 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002161 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002162 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2163 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002164 if (PyErr_ExceptionMatches(PyExc_KeyError))
2165 PyErr_SetString(PyExc_NameError,
2166 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002167 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002168 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002170 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002171 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002172 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002173
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002174 TARGET(STORE_NAME) {
2175 PyObject *name = GETITEM(names, oparg);
2176 PyObject *v = POP();
2177 PyObject *ns = f->f_locals;
2178 int err;
2179 if (ns == NULL) {
2180 PyErr_Format(PyExc_SystemError,
2181 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002183 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002185 if (PyDict_CheckExact(ns))
2186 err = PyDict_SetItem(ns, name, v);
2187 else
2188 err = PyObject_SetItem(ns, name, v);
2189 Py_DECREF(v);
2190 if (err != 0)
2191 goto error;
2192 DISPATCH();
2193 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002194
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002195 TARGET(DELETE_NAME) {
2196 PyObject *name = GETITEM(names, oparg);
2197 PyObject *ns = f->f_locals;
2198 int err;
2199 if (ns == NULL) {
2200 PyErr_Format(PyExc_SystemError,
2201 "no locals when deleting %R", name);
2202 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002203 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002204 err = PyObject_DelItem(ns, name);
2205 if (err != 0) {
2206 format_exc_check_arg(PyExc_NameError,
2207 NAME_ERROR_MSG,
2208 name);
2209 goto error;
2210 }
2211 DISPATCH();
2212 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002213
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002214 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002215 TARGET(UNPACK_SEQUENCE) {
2216 PyObject *seq = POP(), *item, **items;
2217 if (PyTuple_CheckExact(seq) &&
2218 PyTuple_GET_SIZE(seq) == oparg) {
2219 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002220 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002221 item = items[oparg];
2222 Py_INCREF(item);
2223 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002225 } else if (PyList_CheckExact(seq) &&
2226 PyList_GET_SIZE(seq) == oparg) {
2227 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002229 item = items[oparg];
2230 Py_INCREF(item);
2231 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002233 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002234 stack_pointer + oparg)) {
2235 STACKADJ(oparg);
2236 } else {
2237 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002238 Py_DECREF(seq);
2239 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002240 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002241 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002242 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002243 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002244
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002245 TARGET(UNPACK_EX) {
2246 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2247 PyObject *seq = POP();
2248
2249 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2250 stack_pointer + totalargs)) {
2251 stack_pointer += totalargs;
2252 } else {
2253 Py_DECREF(seq);
2254 goto error;
2255 }
2256 Py_DECREF(seq);
2257 DISPATCH();
2258 }
2259
2260 TARGET(STORE_ATTR) {
2261 PyObject *name = GETITEM(names, oparg);
2262 PyObject *owner = TOP();
2263 PyObject *v = SECOND();
2264 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002266 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002267 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002268 Py_DECREF(owner);
2269 if (err != 0)
2270 goto error;
2271 DISPATCH();
2272 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002273
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002274 TARGET(DELETE_ATTR) {
2275 PyObject *name = GETITEM(names, oparg);
2276 PyObject *owner = POP();
2277 int err;
2278 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2279 Py_DECREF(owner);
2280 if (err != 0)
2281 goto error;
2282 DISPATCH();
2283 }
2284
2285 TARGET(STORE_GLOBAL) {
2286 PyObject *name = GETITEM(names, oparg);
2287 PyObject *v = POP();
2288 int err;
2289 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002290 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002291 if (err != 0)
2292 goto error;
2293 DISPATCH();
2294 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002295
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002296 TARGET(DELETE_GLOBAL) {
2297 PyObject *name = GETITEM(names, oparg);
2298 int err;
2299 err = PyDict_DelItem(f->f_globals, name);
2300 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002302 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002303 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002304 }
2305 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002306 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002307
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002308 TARGET(LOAD_NAME) {
2309 PyObject *name = GETITEM(names, oparg);
2310 PyObject *locals = f->f_locals;
2311 PyObject *v;
2312 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002314 "no locals when loading %R", name);
2315 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002317 if (PyDict_CheckExact(locals)) {
2318 v = PyDict_GetItem(locals, name);
2319 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002320 }
2321 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002322 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002323 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002324 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2325 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326 PyErr_Clear();
2327 }
2328 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002329 if (v == NULL) {
2330 v = PyDict_GetItem(f->f_globals, name);
2331 Py_XINCREF(v);
2332 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002333 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002334 v = PyDict_GetItem(f->f_builtins, name);
2335 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002336 format_exc_check_arg(
2337 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002338 NAME_ERROR_MSG, name);
2339 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002340 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002341 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002342 }
2343 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002344 v = PyObject_GetItem(f->f_builtins, name);
2345 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002346 if (PyErr_ExceptionMatches(PyExc_KeyError))
2347 format_exc_check_arg(
2348 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002349 NAME_ERROR_MSG, name);
2350 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002351 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002352 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002355 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002357 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002358
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002359 TARGET(LOAD_GLOBAL) {
2360 PyObject *name = GETITEM(names, oparg);
2361 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002362 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002363 && PyDict_CheckExact(f->f_builtins))
2364 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002365 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002366 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002367 name);
2368 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002369 if (!_PyErr_OCCURRED()) {
2370 /* _PyDict_LoadGlobal() returns NULL without raising
2371 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002372 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002373 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002374 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002375 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002377 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002378 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002379 else {
2380 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002381
2382 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002383 v = PyObject_GetItem(f->f_globals, name);
2384 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002385 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2386 goto error;
2387 PyErr_Clear();
2388
Victor Stinnerb4efc962015-11-20 09:24:02 +01002389 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002390 v = PyObject_GetItem(f->f_builtins, name);
2391 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002392 if (PyErr_ExceptionMatches(PyExc_KeyError))
2393 format_exc_check_arg(
2394 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002395 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002396 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002397 }
2398 }
2399 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002400 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002402 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002403
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002404 TARGET(DELETE_FAST) {
2405 PyObject *v = GETLOCAL(oparg);
2406 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002407 SETLOCAL(oparg, NULL);
2408 DISPATCH();
2409 }
2410 format_exc_check_arg(
2411 PyExc_UnboundLocalError,
2412 UNBOUNDLOCAL_ERROR_MSG,
2413 PyTuple_GetItem(co->co_varnames, oparg)
2414 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002415 goto error;
2416 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002417
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002418 TARGET(DELETE_DEREF) {
2419 PyObject *cell = freevars[oparg];
2420 if (PyCell_GET(cell) != NULL) {
2421 PyCell_Set(cell, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002422 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002423 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002424 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002425 goto error;
2426 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002427
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002428 TARGET(LOAD_CLOSURE) {
2429 PyObject *cell = freevars[oparg];
2430 Py_INCREF(cell);
2431 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002433 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002434
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002435 TARGET(LOAD_CLASSDEREF) {
2436 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002437 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002438 assert(locals);
2439 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2440 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2441 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2442 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2443 if (PyDict_CheckExact(locals)) {
2444 value = PyDict_GetItem(locals, name);
2445 Py_XINCREF(value);
2446 }
2447 else {
2448 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002449 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002450 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2451 goto error;
2452 PyErr_Clear();
2453 }
2454 }
2455 if (!value) {
2456 PyObject *cell = freevars[oparg];
2457 value = PyCell_GET(cell);
2458 if (value == NULL) {
2459 format_exc_unbound(co, oparg);
2460 goto error;
2461 }
2462 Py_INCREF(value);
2463 }
2464 PUSH(value);
2465 DISPATCH();
2466 }
2467
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002468 TARGET(LOAD_DEREF) {
2469 PyObject *cell = freevars[oparg];
2470 PyObject *value = PyCell_GET(cell);
2471 if (value == NULL) {
2472 format_exc_unbound(co, oparg);
2473 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002474 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002475 Py_INCREF(value);
2476 PUSH(value);
2477 DISPATCH();
2478 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002479
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002480 TARGET(STORE_DEREF) {
2481 PyObject *v = POP();
2482 PyObject *cell = freevars[oparg];
Raymond Hettinger13527122016-11-11 04:31:18 -08002483 PyObject *oldobj = PyCell_GET(cell);
2484 PyCell_SET(cell, v);
2485 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002486 DISPATCH();
2487 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002488
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002489 TARGET(BUILD_STRING) {
2490 PyObject *str;
2491 PyObject *empty = PyUnicode_New(0, 0);
2492 if (empty == NULL) {
2493 goto error;
2494 }
2495 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2496 Py_DECREF(empty);
2497 if (str == NULL)
2498 goto error;
2499 while (--oparg >= 0) {
2500 PyObject *item = POP();
2501 Py_DECREF(item);
2502 }
2503 PUSH(str);
2504 DISPATCH();
2505 }
2506
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002507 TARGET(BUILD_TUPLE) {
2508 PyObject *tup = PyTuple_New(oparg);
2509 if (tup == NULL)
2510 goto error;
2511 while (--oparg >= 0) {
2512 PyObject *item = POP();
2513 PyTuple_SET_ITEM(tup, oparg, item);
2514 }
2515 PUSH(tup);
2516 DISPATCH();
2517 }
2518
2519 TARGET(BUILD_LIST) {
2520 PyObject *list = PyList_New(oparg);
2521 if (list == NULL)
2522 goto error;
2523 while (--oparg >= 0) {
2524 PyObject *item = POP();
2525 PyList_SET_ITEM(list, oparg, item);
2526 }
2527 PUSH(list);
2528 DISPATCH();
2529 }
2530
Serhiy Storchaka73442852016-10-02 10:33:46 +03002531 TARGET(BUILD_TUPLE_UNPACK_WITH_CALL)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002532 TARGET(BUILD_TUPLE_UNPACK)
2533 TARGET(BUILD_LIST_UNPACK) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002534 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002535 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002536 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002537 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002538
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002539 if (sum == NULL)
2540 goto error;
2541
2542 for (i = oparg; i > 0; i--) {
2543 PyObject *none_val;
2544
2545 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2546 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002547 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
2548 PyErr_ExceptionMatches(PyExc_TypeError)) {
2549 PyObject *func = PEEK(1 + oparg);
2550 PyErr_Format(PyExc_TypeError,
2551 "%.200s%.200s argument after * "
2552 "must be an iterable, not %.200s",
2553 PyEval_GetFuncName(func),
2554 PyEval_GetFuncDesc(func),
2555 PEEK(i)->ob_type->tp_name);
2556 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002557 Py_DECREF(sum);
2558 goto error;
2559 }
2560 Py_DECREF(none_val);
2561 }
2562
2563 if (convert_to_tuple) {
2564 return_value = PyList_AsTuple(sum);
2565 Py_DECREF(sum);
2566 if (return_value == NULL)
2567 goto error;
2568 }
2569 else {
2570 return_value = sum;
2571 }
2572
2573 while (oparg--)
2574 Py_DECREF(POP());
2575 PUSH(return_value);
2576 DISPATCH();
2577 }
2578
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002579 TARGET(BUILD_SET) {
2580 PyObject *set = PySet_New(NULL);
2581 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002582 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002583 if (set == NULL)
2584 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002585 for (i = oparg; i > 0; i--) {
2586 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002587 if (err == 0)
2588 err = PySet_Add(set, item);
2589 Py_DECREF(item);
2590 }
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002591 STACKADJ(-oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002592 if (err != 0) {
2593 Py_DECREF(set);
2594 goto error;
2595 }
2596 PUSH(set);
2597 DISPATCH();
2598 }
2599
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002600 TARGET(BUILD_SET_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002601 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002602 PyObject *sum = PySet_New(NULL);
2603 if (sum == NULL)
2604 goto error;
2605
2606 for (i = oparg; i > 0; i--) {
2607 if (_PySet_Update(sum, PEEK(i)) < 0) {
2608 Py_DECREF(sum);
2609 goto error;
2610 }
2611 }
2612
2613 while (oparg--)
2614 Py_DECREF(POP());
2615 PUSH(sum);
2616 DISPATCH();
2617 }
2618
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002619 TARGET(BUILD_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002620 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002621 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2622 if (map == NULL)
2623 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002624 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002625 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002626 PyObject *key = PEEK(2*i);
2627 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002628 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002629 if (err != 0) {
2630 Py_DECREF(map);
2631 goto error;
2632 }
2633 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002634
2635 while (oparg--) {
2636 Py_DECREF(POP());
2637 Py_DECREF(POP());
2638 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002639 PUSH(map);
2640 DISPATCH();
2641 }
2642
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002643 TARGET(SETUP_ANNOTATIONS) {
2644 _Py_IDENTIFIER(__annotations__);
2645 int err;
2646 PyObject *ann_dict;
2647 if (f->f_locals == NULL) {
2648 PyErr_Format(PyExc_SystemError,
2649 "no locals found when setting up annotations");
2650 goto error;
2651 }
2652 /* check if __annotations__ in locals()... */
2653 if (PyDict_CheckExact(f->f_locals)) {
2654 ann_dict = _PyDict_GetItemId(f->f_locals,
2655 &PyId___annotations__);
2656 if (ann_dict == NULL) {
2657 /* ...if not, create a new one */
2658 ann_dict = PyDict_New();
2659 if (ann_dict == NULL) {
2660 goto error;
2661 }
2662 err = _PyDict_SetItemId(f->f_locals,
2663 &PyId___annotations__, ann_dict);
2664 Py_DECREF(ann_dict);
2665 if (err != 0) {
2666 goto error;
2667 }
2668 }
2669 }
2670 else {
2671 /* do the same if locals() is not a dict */
2672 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2673 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002674 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002675 }
2676 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2677 if (ann_dict == NULL) {
2678 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2679 goto error;
2680 }
2681 PyErr_Clear();
2682 ann_dict = PyDict_New();
2683 if (ann_dict == NULL) {
2684 goto error;
2685 }
2686 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2687 Py_DECREF(ann_dict);
2688 if (err != 0) {
2689 goto error;
2690 }
2691 }
2692 else {
2693 Py_DECREF(ann_dict);
2694 }
2695 }
2696 DISPATCH();
2697 }
2698
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002699 TARGET(BUILD_CONST_KEY_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002700 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002701 PyObject *map;
2702 PyObject *keys = TOP();
2703 if (!PyTuple_CheckExact(keys) ||
2704 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2705 PyErr_SetString(PyExc_SystemError,
2706 "bad BUILD_CONST_KEY_MAP keys argument");
2707 goto error;
2708 }
2709 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2710 if (map == NULL) {
2711 goto error;
2712 }
2713 for (i = oparg; i > 0; i--) {
2714 int err;
2715 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2716 PyObject *value = PEEK(i + 1);
2717 err = PyDict_SetItem(map, key, value);
2718 if (err != 0) {
2719 Py_DECREF(map);
2720 goto error;
2721 }
2722 }
2723
2724 Py_DECREF(POP());
2725 while (oparg--) {
2726 Py_DECREF(POP());
2727 }
2728 PUSH(map);
2729 DISPATCH();
2730 }
2731
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002732 TARGET(BUILD_MAP_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002733 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002734 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002735 if (sum == NULL)
2736 goto error;
2737
2738 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002739 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002740 if (PyDict_Update(sum, arg) < 0) {
2741 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2742 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002743 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002744 arg->ob_type->tp_name);
2745 }
2746 Py_DECREF(sum);
2747 goto error;
2748 }
2749 }
2750
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002751 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002752 Py_DECREF(POP());
2753 PUSH(sum);
2754 DISPATCH();
2755 }
2756
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002757 TARGET(BUILD_MAP_UNPACK_WITH_CALL) {
2758 Py_ssize_t i;
2759 PyObject *sum = PyDict_New();
2760 if (sum == NULL)
2761 goto error;
2762
2763 for (i = oparg; i > 0; i--) {
2764 PyObject *arg = PEEK(i);
2765 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
2766 PyObject *func = PEEK(2 + oparg);
2767 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2768 PyErr_Format(PyExc_TypeError,
2769 "%.200s%.200s argument after ** "
2770 "must be a mapping, not %.200s",
2771 PyEval_GetFuncName(func),
2772 PyEval_GetFuncDesc(func),
2773 arg->ob_type->tp_name);
2774 }
2775 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2776 PyObject *exc, *val, *tb;
2777 PyErr_Fetch(&exc, &val, &tb);
2778 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
2779 PyObject *key = PyTuple_GET_ITEM(val, 0);
2780 if (!PyUnicode_Check(key)) {
2781 PyErr_Format(PyExc_TypeError,
2782 "%.200s%.200s keywords must be strings",
2783 PyEval_GetFuncName(func),
2784 PyEval_GetFuncDesc(func));
2785 } else {
2786 PyErr_Format(PyExc_TypeError,
2787 "%.200s%.200s got multiple "
2788 "values for keyword argument '%U'",
2789 PyEval_GetFuncName(func),
2790 PyEval_GetFuncDesc(func),
2791 key);
2792 }
2793 Py_XDECREF(exc);
2794 Py_XDECREF(val);
2795 Py_XDECREF(tb);
2796 }
2797 else {
2798 PyErr_Restore(exc, val, tb);
2799 }
2800 }
2801 Py_DECREF(sum);
2802 goto error;
2803 }
2804 }
2805
2806 while (oparg--)
2807 Py_DECREF(POP());
2808 PUSH(sum);
2809 DISPATCH();
2810 }
2811
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002812 TARGET(MAP_ADD) {
2813 PyObject *key = TOP();
2814 PyObject *value = SECOND();
2815 PyObject *map;
2816 int err;
2817 STACKADJ(-2);
2818 map = stack_pointer[-oparg]; /* dict */
2819 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002820 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002821 Py_DECREF(value);
2822 Py_DECREF(key);
2823 if (err != 0)
2824 goto error;
2825 PREDICT(JUMP_ABSOLUTE);
2826 DISPATCH();
2827 }
2828
2829 TARGET(LOAD_ATTR) {
2830 PyObject *name = GETITEM(names, oparg);
2831 PyObject *owner = TOP();
2832 PyObject *res = PyObject_GetAttr(owner, name);
2833 Py_DECREF(owner);
2834 SET_TOP(res);
2835 if (res == NULL)
2836 goto error;
2837 DISPATCH();
2838 }
2839
2840 TARGET(COMPARE_OP) {
2841 PyObject *right = POP();
2842 PyObject *left = TOP();
2843 PyObject *res = cmp_outcome(oparg, left, right);
2844 Py_DECREF(left);
2845 Py_DECREF(right);
2846 SET_TOP(res);
2847 if (res == NULL)
2848 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002849 PREDICT(POP_JUMP_IF_FALSE);
2850 PREDICT(POP_JUMP_IF_TRUE);
2851 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002852 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002853
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002854 TARGET(IMPORT_NAME) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002855 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002856 PyObject *fromlist = POP();
2857 PyObject *level = TOP();
2858 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002859 res = import_name(f, name, fromlist, level);
2860 Py_DECREF(level);
2861 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002862 SET_TOP(res);
2863 if (res == NULL)
2864 goto error;
2865 DISPATCH();
2866 }
2867
2868 TARGET(IMPORT_STAR) {
2869 PyObject *from = POP(), *locals;
2870 int err;
Berker Peksag7accf202017-02-27 20:41:21 +03002871 if (PyFrame_FastToLocalsWithError(f) < 0) {
2872 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002873 goto error;
Berker Peksag7accf202017-02-27 20:41:21 +03002874 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002875
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002876 locals = f->f_locals;
2877 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 PyErr_SetString(PyExc_SystemError,
2879 "no locals found during 'import *'");
Berker Peksag7accf202017-02-27 20:41:21 +03002880 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002881 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002882 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002883 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002884 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002885 Py_DECREF(from);
2886 if (err != 0)
2887 goto error;
2888 DISPATCH();
2889 }
Guido van Rossum25831651993-05-19 14:50:45 +00002890
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002891 TARGET(IMPORT_FROM) {
2892 PyObject *name = GETITEM(names, oparg);
2893 PyObject *from = TOP();
2894 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002895 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002896 PUSH(res);
2897 if (res == NULL)
2898 goto error;
2899 DISPATCH();
2900 }
Thomas Wouters52152252000-08-17 22:55:00 +00002901
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002902 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 JUMPBY(oparg);
2904 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002905 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002906
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002907 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002908 TARGET(POP_JUMP_IF_FALSE) {
2909 PyObject *cond = POP();
2910 int err;
2911 if (cond == Py_True) {
2912 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 FAST_DISPATCH();
2914 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002915 if (cond == Py_False) {
2916 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 JUMPTO(oparg);
2918 FAST_DISPATCH();
2919 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002920 err = PyObject_IsTrue(cond);
2921 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002922 if (err > 0)
2923 err = 0;
2924 else if (err == 0)
2925 JUMPTO(oparg);
2926 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002927 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002928 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002929 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002930
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002931 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002932 TARGET(POP_JUMP_IF_TRUE) {
2933 PyObject *cond = POP();
2934 int err;
2935 if (cond == Py_False) {
2936 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002937 FAST_DISPATCH();
2938 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002939 if (cond == Py_True) {
2940 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002941 JUMPTO(oparg);
2942 FAST_DISPATCH();
2943 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002944 err = PyObject_IsTrue(cond);
2945 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 if (err > 0) {
2947 err = 0;
2948 JUMPTO(oparg);
2949 }
2950 else if (err == 0)
2951 ;
2952 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002953 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002954 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002955 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002956
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002957 TARGET(JUMP_IF_FALSE_OR_POP) {
2958 PyObject *cond = TOP();
2959 int err;
2960 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002961 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002962 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002963 FAST_DISPATCH();
2964 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002965 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002966 JUMPTO(oparg);
2967 FAST_DISPATCH();
2968 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002969 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002970 if (err > 0) {
2971 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002972 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002973 err = 0;
2974 }
2975 else if (err == 0)
2976 JUMPTO(oparg);
2977 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002978 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002979 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002980 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002981
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002982 TARGET(JUMP_IF_TRUE_OR_POP) {
2983 PyObject *cond = TOP();
2984 int err;
2985 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002986 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002987 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002988 FAST_DISPATCH();
2989 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002990 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002991 JUMPTO(oparg);
2992 FAST_DISPATCH();
2993 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002994 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002995 if (err > 0) {
2996 err = 0;
2997 JUMPTO(oparg);
2998 }
2999 else if (err == 0) {
3000 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003001 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003002 }
3003 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003004 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003005 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003006 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003007
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003008 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003009 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003010 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003011#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003012 /* Enabling this path speeds-up all while and for-loops by bypassing
3013 the per-loop checks for signals. By default, this should be turned-off
3014 because it prevents detection of a control-break in tight loops like
3015 "while 1: pass". Compile with this option turned-on when you need
3016 the speed-up and do not need break checking inside tight loops (ones
3017 that contain only instructions ending with FAST_DISPATCH).
3018 */
3019 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003020#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003021 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003022#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003023 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003024
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003025 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003026 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003027 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003028 PyObject *iter = PyObject_GetIter(iterable);
3029 Py_DECREF(iterable);
3030 SET_TOP(iter);
3031 if (iter == NULL)
3032 goto error;
3033 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003034 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003035 DISPATCH();
3036 }
3037
3038 TARGET(GET_YIELD_FROM_ITER) {
3039 /* before: [obj]; after [getiter(obj)] */
3040 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003041 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003042 if (PyCoro_CheckExact(iterable)) {
3043 /* `iterable` is a coroutine */
3044 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3045 /* and it is used in a 'yield from' expression of a
3046 regular generator. */
3047 Py_DECREF(iterable);
3048 SET_TOP(NULL);
3049 PyErr_SetString(PyExc_TypeError,
3050 "cannot 'yield from' a coroutine object "
3051 "in a non-coroutine generator");
3052 goto error;
3053 }
3054 }
3055 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003056 /* `iterable` is not a generator. */
3057 iter = PyObject_GetIter(iterable);
3058 Py_DECREF(iterable);
3059 SET_TOP(iter);
3060 if (iter == NULL)
3061 goto error;
3062 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003063 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003064 DISPATCH();
3065 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003066
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003067 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003068 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003069 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003070 PyObject *iter = TOP();
3071 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3072 if (next != NULL) {
3073 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003074 PREDICT(STORE_FAST);
3075 PREDICT(UNPACK_SEQUENCE);
3076 DISPATCH();
3077 }
3078 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003079 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
3080 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08003081 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003082 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003083 PyErr_Clear();
3084 }
3085 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003086 STACKADJ(-1);
3087 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003089 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003091 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003092
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003093 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003094 why = WHY_BREAK;
3095 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003096 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003097
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003098 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003099 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003100 if (retval == NULL)
3101 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003102 why = WHY_CONTINUE;
3103 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003104 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003105
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003106 TARGET(SETUP_LOOP)
3107 TARGET(SETUP_EXCEPT)
3108 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003109 /* NOTE: If you add any new block-setup opcodes that
3110 are not try/except/finally handlers, you may need
3111 to update the PyGen_NeedsFinalizing() function.
3112 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003114 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
3115 STACK_LEVEL());
3116 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003117 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003118
Yury Selivanov75445082015-05-11 22:57:16 -04003119 TARGET(BEFORE_ASYNC_WITH) {
3120 _Py_IDENTIFIER(__aexit__);
3121 _Py_IDENTIFIER(__aenter__);
3122
3123 PyObject *mgr = TOP();
3124 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3125 *enter;
3126 PyObject *res;
3127 if (exit == NULL)
3128 goto error;
3129 SET_TOP(exit);
3130 enter = special_lookup(mgr, &PyId___aenter__);
3131 Py_DECREF(mgr);
3132 if (enter == NULL)
3133 goto error;
3134 res = PyObject_CallFunctionObjArgs(enter, NULL);
3135 Py_DECREF(enter);
3136 if (res == NULL)
3137 goto error;
3138 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003139 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003140 DISPATCH();
3141 }
3142
3143 TARGET(SETUP_ASYNC_WITH) {
3144 PyObject *res = POP();
3145 /* Setup the finally block before pushing the result
3146 of __aenter__ on the stack. */
3147 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3148 STACK_LEVEL());
3149 PUSH(res);
3150 DISPATCH();
3151 }
3152
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003153 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003154 _Py_IDENTIFIER(__exit__);
3155 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003156 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08003157 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003158 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08003159 if (enter == NULL)
3160 goto error;
3161 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003162 if (exit == NULL) {
3163 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003164 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003165 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003166 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003167 Py_DECREF(mgr);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003168 res = PyObject_CallFunctionObjArgs(enter, NULL);
3169 Py_DECREF(enter);
3170 if (res == NULL)
3171 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003172 /* Setup the finally block before pushing the result
3173 of __enter__ on the stack. */
3174 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3175 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003176
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003177 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003178 DISPATCH();
3179 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003180
Yury Selivanov75445082015-05-11 22:57:16 -04003181 TARGET(WITH_CLEANUP_START) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04003182 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003183 how/why we entered the finally clause:
3184 - TOP = None
3185 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
3186 - TOP = WHY_*; no retval below it
3187 - (TOP, SECOND, THIRD) = exc_info()
3188 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
3189 Below them is EXIT, the context.__exit__ bound method.
3190 In the last case, we must call
3191 EXIT(TOP, SECOND, THIRD)
3192 otherwise we must call
3193 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003194
Benjamin Peterson8f169482013-10-29 22:25:06 -04003195 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003196 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04003197 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003198 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003200 In addition, if the stack represents an exception,
3201 *and* the function call returns a 'true' value, we
3202 push WHY_SILENCED onto the stack. END_FINALLY will
3203 then not re-raise the exception. (But non-local
3204 gotos should still be resumed.)
3205 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003207 PyObject *exit_func;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003208 PyObject *exc = TOP(), *val = Py_None, *tb = Py_None, *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003209 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003210 (void)POP();
3211 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003212 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003213 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003214 else if (PyLong_Check(exc)) {
3215 STACKADJ(-1);
3216 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003217 case WHY_RETURN:
3218 case WHY_CONTINUE:
3219 /* Retval in TOP. */
3220 exit_func = SECOND();
3221 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003222 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003223 break;
3224 default:
3225 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003226 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003227 break;
3228 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003229 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003230 }
3231 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003232 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003233 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003234 val = SECOND();
3235 tb = THIRD();
3236 tp2 = FOURTH();
3237 exc2 = PEEK(5);
3238 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003239 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003240 SET_VALUE(7, tb2);
3241 SET_VALUE(6, exc2);
3242 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003243 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3244 SET_FOURTH(NULL);
3245 /* We just shifted the stack down, so we have
3246 to tell the except handler block that the
3247 values are lower than it expects. */
3248 block = &f->f_blockstack[f->f_iblock - 1];
3249 assert(block->b_type == EXCEPT_HANDLER);
3250 block->b_level--;
3251 }
3252 /* XXX Not the fastest way to call it... */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003253 res = PyObject_CallFunctionObjArgs(exit_func, exc, val, tb, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003254 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003255 if (res == NULL)
3256 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003257
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003258 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003259 PUSH(exc);
3260 PUSH(res);
3261 PREDICT(WITH_CLEANUP_FINISH);
3262 DISPATCH();
3263 }
3264
3265 PREDICTED(WITH_CLEANUP_FINISH);
3266 TARGET(WITH_CLEANUP_FINISH) {
3267 PyObject *res = POP();
3268 PyObject *exc = POP();
3269 int err;
3270
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003271 if (exc != Py_None)
3272 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003273 else
3274 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003275
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003276 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003277 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003279 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003280 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003281 else if (err > 0) {
3282 err = 0;
3283 /* There was an exception and a True return */
3284 PUSH(PyLong_FromLong((long) WHY_SILENCED));
3285 }
3286 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003287 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003288 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003289
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003290 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003291 TARGET(CALL_FUNCTION) {
3292 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003293 PCALL(PCALL_ALL);
3294 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003295 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003296 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003297 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003298 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003299 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003300 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003301 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003302 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003303
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003304 TARGET(CALL_FUNCTION_KW) {
3305 PyObject **sp, *res, *names;
3306
3307 names = POP();
3308 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003309 PCALL(PCALL_ALL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003310 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003311 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003312 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003313 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003314 Py_DECREF(names);
3315
3316 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003317 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003318 }
3319 DISPATCH();
3320 }
3321
3322 TARGET(CALL_FUNCTION_EX) {
3323 PyObject *func, *callargs, *kwargs = NULL, *result;
3324 PCALL(PCALL_ALL);
3325 if (oparg & 0x01) {
3326 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003327 if (!PyDict_CheckExact(kwargs)) {
3328 PyObject *d = PyDict_New();
3329 if (d == NULL)
3330 goto error;
3331 if (PyDict_Update(d, kwargs) != 0) {
3332 Py_DECREF(d);
3333 /* PyDict_Update raises attribute
3334 * error (percolated from an attempt
3335 * to get 'keys' attribute) instead of
3336 * a type error if its second argument
3337 * is not a mapping.
3338 */
3339 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3340 func = SECOND();
3341 PyErr_Format(PyExc_TypeError,
3342 "%.200s%.200s argument after ** "
3343 "must be a mapping, not %.200s",
3344 PyEval_GetFuncName(func),
3345 PyEval_GetFuncDesc(func),
3346 kwargs->ob_type->tp_name);
3347 }
Victor Stinnereece2222016-09-12 11:16:37 +02003348 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003349 goto error;
3350 }
3351 Py_DECREF(kwargs);
3352 kwargs = d;
3353 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003354 assert(PyDict_CheckExact(kwargs));
3355 }
3356 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003357 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003358 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003359 if (Py_TYPE(callargs)->tp_iter == NULL &&
3360 !PySequence_Check(callargs)) {
3361 PyErr_Format(PyExc_TypeError,
3362 "%.200s%.200s argument after * "
3363 "must be an iterable, not %.200s",
3364 PyEval_GetFuncName(func),
3365 PyEval_GetFuncDesc(func),
3366 callargs->ob_type->tp_name);
Victor Stinnereece2222016-09-12 11:16:37 +02003367 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003368 goto error;
3369 }
3370 Py_SETREF(callargs, PySequence_Tuple(callargs));
3371 if (callargs == NULL) {
3372 goto error;
3373 }
3374 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003375 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003376
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003377 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003378 Py_DECREF(func);
3379 Py_DECREF(callargs);
3380 Py_XDECREF(kwargs);
3381
3382 SET_TOP(result);
3383 if (result == NULL) {
3384 goto error;
3385 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003386 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003387 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003388
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003389 TARGET(MAKE_FUNCTION) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003390 PyObject *qualname = POP();
3391 PyObject *codeobj = POP();
3392 PyFunctionObject *func = (PyFunctionObject *)
3393 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003394
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003395 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003396 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003397 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003398 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003399 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003400
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003401 if (oparg & 0x08) {
3402 assert(PyTuple_CheckExact(TOP()));
3403 func ->func_closure = POP();
3404 }
3405 if (oparg & 0x04) {
3406 assert(PyDict_CheckExact(TOP()));
3407 func->func_annotations = POP();
3408 }
3409 if (oparg & 0x02) {
3410 assert(PyDict_CheckExact(TOP()));
3411 func->func_kwdefaults = POP();
3412 }
3413 if (oparg & 0x01) {
3414 assert(PyTuple_CheckExact(TOP()));
3415 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003416 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003417
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003418 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003419 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003420 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003421
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003422 TARGET(BUILD_SLICE) {
3423 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003424 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003425 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003426 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003427 step = NULL;
3428 stop = POP();
3429 start = TOP();
3430 slice = PySlice_New(start, stop, step);
3431 Py_DECREF(start);
3432 Py_DECREF(stop);
3433 Py_XDECREF(step);
3434 SET_TOP(slice);
3435 if (slice == NULL)
3436 goto error;
3437 DISPATCH();
3438 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003439
Eric V. Smitha78c7952015-11-03 12:45:05 -05003440 TARGET(FORMAT_VALUE) {
3441 /* Handles f-string value formatting. */
3442 PyObject *result;
3443 PyObject *fmt_spec;
3444 PyObject *value;
3445 PyObject *(*conv_fn)(PyObject *);
3446 int which_conversion = oparg & FVC_MASK;
3447 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3448
3449 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003450 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003451
3452 /* See if any conversion is specified. */
3453 switch (which_conversion) {
3454 case FVC_STR: conv_fn = PyObject_Str; break;
3455 case FVC_REPR: conv_fn = PyObject_Repr; break;
3456 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3457
3458 /* Must be 0 (meaning no conversion), since only four
3459 values are allowed by (oparg & FVC_MASK). */
3460 default: conv_fn = NULL; break;
3461 }
3462
3463 /* If there's a conversion function, call it and replace
3464 value with that result. Otherwise, just use value,
3465 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003466 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003467 result = conv_fn(value);
3468 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003469 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003470 Py_XDECREF(fmt_spec);
3471 goto error;
3472 }
3473 value = result;
3474 }
3475
3476 /* If value is a unicode object, and there's no fmt_spec,
3477 then we know the result of format(value) is value
3478 itself. In that case, skip calling format(). I plan to
3479 move this optimization in to PyObject_Format()
3480 itself. */
3481 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3482 /* Do nothing, just transfer ownership to result. */
3483 result = value;
3484 } else {
3485 /* Actually call format(). */
3486 result = PyObject_Format(value, fmt_spec);
3487 Py_DECREF(value);
3488 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003489 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003490 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003491 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003492 }
3493
Eric V. Smith135d5f42016-02-05 18:23:08 -05003494 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003495 DISPATCH();
3496 }
3497
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003498 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003499 int oldoparg = oparg;
3500 NEXTOPARG();
3501 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003502 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003503 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003504
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003505
Antoine Pitrou042b1282010-08-13 21:15:58 +00003506#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003507 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003508#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003509 default:
3510 fprintf(stderr,
3511 "XXX lineno: %d, opcode: %d\n",
3512 PyFrame_GetLineNumber(f),
3513 opcode);
3514 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003515 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003516
3517#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003518 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003519#endif
3520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003521 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003522
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003523 /* This should never be reached. Every opcode should end with DISPATCH()
3524 or goto error. */
3525 assert(0);
Guido van Rossumac7be682001-01-17 15:42:30 +00003526
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003527error:
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003528
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003529 assert(why == WHY_NOT);
3530 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003531
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003532 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003533#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003534 if (!PyErr_Occurred())
3535 PyErr_SetString(PyExc_SystemError,
3536 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003537#else
3538 assert(PyErr_Occurred());
3539#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003540
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003541 /* Log traceback info. */
3542 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003543
Benjamin Peterson51f46162013-01-23 08:38:47 -05003544 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003545 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3546 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003547
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003548fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003549 assert(why != WHY_NOT);
3550
3551 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003552 while (why != WHY_NOT && f->f_iblock > 0) {
3553 /* Peek at the current block. */
3554 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003556 assert(why != WHY_YIELD);
3557 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3558 why = WHY_NOT;
3559 JUMPTO(PyLong_AS_LONG(retval));
3560 Py_DECREF(retval);
3561 break;
3562 }
3563 /* Now we have to pop the block. */
3564 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003566 if (b->b_type == EXCEPT_HANDLER) {
3567 UNWIND_EXCEPT_HANDLER(b);
3568 continue;
3569 }
3570 UNWIND_BLOCK(b);
3571 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3572 why = WHY_NOT;
3573 JUMPTO(b->b_handler);
3574 break;
3575 }
3576 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3577 || b->b_type == SETUP_FINALLY)) {
3578 PyObject *exc, *val, *tb;
3579 int handler = b->b_handler;
3580 /* Beware, this invalidates all b->b_* fields */
3581 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3582 PUSH(tstate->exc_traceback);
3583 PUSH(tstate->exc_value);
3584 if (tstate->exc_type != NULL) {
3585 PUSH(tstate->exc_type);
3586 }
3587 else {
3588 Py_INCREF(Py_None);
3589 PUSH(Py_None);
3590 }
3591 PyErr_Fetch(&exc, &val, &tb);
3592 /* Make the raw exception data
3593 available to the handler,
3594 so a program can emulate the
3595 Python main loop. */
3596 PyErr_NormalizeException(
3597 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003598 if (tb != NULL)
3599 PyException_SetTraceback(val, tb);
3600 else
3601 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003602 Py_INCREF(exc);
3603 tstate->exc_type = exc;
3604 Py_INCREF(val);
3605 tstate->exc_value = val;
3606 tstate->exc_traceback = tb;
3607 if (tb == NULL)
3608 tb = Py_None;
3609 Py_INCREF(tb);
3610 PUSH(tb);
3611 PUSH(val);
3612 PUSH(exc);
3613 why = WHY_NOT;
3614 JUMPTO(handler);
3615 break;
3616 }
3617 if (b->b_type == SETUP_FINALLY) {
3618 if (why & (WHY_RETURN | WHY_CONTINUE))
3619 PUSH(retval);
3620 PUSH(PyLong_FromLong((long)why));
3621 why = WHY_NOT;
3622 JUMPTO(b->b_handler);
3623 break;
3624 }
3625 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003627 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003629 if (why != WHY_NOT)
3630 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00003631
Victor Stinnerace47d72013-07-18 01:41:08 +02003632 assert(!PyErr_Occurred());
3633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003634 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003636 assert(why != WHY_YIELD);
3637 /* Pop remaining stack entries. */
3638 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003639 PyObject *o = POP();
3640 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003641 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003643 if (why != WHY_RETURN)
3644 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003645
Victor Stinner4a7cc882015-03-06 23:35:27 +01003646 assert((retval != NULL) ^ (PyErr_Occurred() != NULL));
Victor Stinnerace47d72013-07-18 01:41:08 +02003647
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003648fast_yield:
Yury Selivanoveb636452016-09-08 22:01:51 -07003649 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01003650
Benjamin Petersonac913412011-07-03 16:25:11 -05003651 /* The purpose of this block is to put aside the generator's exception
3652 state and restore that of the calling frame. If the current
3653 exception state is from the caller, we clear the exception values
3654 on the generator frame, so they are not swapped back in latter. The
3655 origin of the current exception state is determined by checking for
3656 except handler blocks, which we must be in iff a new exception
3657 state came into existence in this frame. (An uncaught exception
3658 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3659 int i;
Victor Stinner74319ae2016-08-25 00:04:09 +02003660 for (i = 0; i < f->f_iblock; i++) {
3661 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER) {
Benjamin Petersonac913412011-07-03 16:25:11 -05003662 break;
Victor Stinner74319ae2016-08-25 00:04:09 +02003663 }
3664 }
Benjamin Petersonac913412011-07-03 16:25:11 -05003665 if (i == f->f_iblock)
3666 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003667 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003668 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003669 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003670 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003672 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003673 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003674 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003675 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3676 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003677 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003678 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003679 why = WHY_EXCEPTION;
3680 }
3681 }
3682 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003683 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3684 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003685 PyTrace_RETURN, NULL);
3686 }
3687 }
3688 if (tstate->c_profilefunc) {
3689 if (why == WHY_EXCEPTION)
3690 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003691 tstate->c_profileobj,
3692 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003693 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003694 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3695 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003696 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003697 Py_CLEAR(retval);
Brett Cannonb94767f2011-02-22 20:15:44 +00003698 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003699 }
3700 }
3701 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003703 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003704exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003705 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3706 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003707 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003708 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003709 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003710
Victor Stinnerefde1462015-03-21 15:04:43 +01003711 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003712}
3713
Benjamin Petersonb204a422011-06-05 22:04:07 -05003714static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003715format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3716{
3717 int err;
3718 Py_ssize_t len = PyList_GET_SIZE(names);
3719 PyObject *name_str, *comma, *tail, *tmp;
3720
3721 assert(PyList_CheckExact(names));
3722 assert(len >= 1);
3723 /* Deal with the joys of natural language. */
3724 switch (len) {
3725 case 1:
3726 name_str = PyList_GET_ITEM(names, 0);
3727 Py_INCREF(name_str);
3728 break;
3729 case 2:
3730 name_str = PyUnicode_FromFormat("%U and %U",
3731 PyList_GET_ITEM(names, len - 2),
3732 PyList_GET_ITEM(names, len - 1));
3733 break;
3734 default:
3735 tail = PyUnicode_FromFormat(", %U, and %U",
3736 PyList_GET_ITEM(names, len - 2),
3737 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003738 if (tail == NULL)
3739 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003740 /* Chop off the last two objects in the list. This shouldn't actually
3741 fail, but we can't be too careful. */
3742 err = PyList_SetSlice(names, len - 2, len, NULL);
3743 if (err == -1) {
3744 Py_DECREF(tail);
3745 return;
3746 }
3747 /* Stitch everything up into a nice comma-separated list. */
3748 comma = PyUnicode_FromString(", ");
3749 if (comma == NULL) {
3750 Py_DECREF(tail);
3751 return;
3752 }
3753 tmp = PyUnicode_Join(comma, names);
3754 Py_DECREF(comma);
3755 if (tmp == NULL) {
3756 Py_DECREF(tail);
3757 return;
3758 }
3759 name_str = PyUnicode_Concat(tmp, tail);
3760 Py_DECREF(tmp);
3761 Py_DECREF(tail);
3762 break;
3763 }
3764 if (name_str == NULL)
3765 return;
3766 PyErr_Format(PyExc_TypeError,
3767 "%U() missing %i required %s argument%s: %U",
3768 co->co_name,
3769 len,
3770 kind,
3771 len == 1 ? "" : "s",
3772 name_str);
3773 Py_DECREF(name_str);
3774}
3775
3776static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003777missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003778 PyObject **fastlocals)
3779{
Victor Stinner74319ae2016-08-25 00:04:09 +02003780 Py_ssize_t i, j = 0;
3781 Py_ssize_t start, end;
3782 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003783 const char *kind = positional ? "positional" : "keyword-only";
3784 PyObject *missing_names;
3785
3786 /* Compute the names of the arguments that are missing. */
3787 missing_names = PyList_New(missing);
3788 if (missing_names == NULL)
3789 return;
3790 if (positional) {
3791 start = 0;
3792 end = co->co_argcount - defcount;
3793 }
3794 else {
3795 start = co->co_argcount;
3796 end = start + co->co_kwonlyargcount;
3797 }
3798 for (i = start; i < end; i++) {
3799 if (GETLOCAL(i) == NULL) {
3800 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3801 PyObject *name = PyObject_Repr(raw);
3802 if (name == NULL) {
3803 Py_DECREF(missing_names);
3804 return;
3805 }
3806 PyList_SET_ITEM(missing_names, j++, name);
3807 }
3808 }
3809 assert(j == missing);
3810 format_missing(kind, co, missing_names);
3811 Py_DECREF(missing_names);
3812}
3813
3814static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003815too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3816 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003817{
3818 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003819 Py_ssize_t kwonly_given = 0;
3820 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003821 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003822 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003823
Benjamin Petersone109c702011-06-24 09:37:26 -05003824 assert((co->co_flags & CO_VARARGS) == 0);
3825 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003826 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3827 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003828 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003829 }
3830 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003831 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003832 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003833 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003834 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003835 }
3836 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003837 plural = (co_argcount != 1);
3838 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003839 }
3840 if (sig == NULL)
3841 return;
3842 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003843 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3844 kwonly_sig = PyUnicode_FromFormat(format,
3845 given != 1 ? "s" : "",
3846 kwonly_given,
3847 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003848 if (kwonly_sig == NULL) {
3849 Py_DECREF(sig);
3850 return;
3851 }
3852 }
3853 else {
3854 /* This will not fail. */
3855 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003856 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003857 }
3858 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003859 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003860 co->co_name,
3861 sig,
3862 plural ? "s" : "",
3863 given,
3864 kwonly_sig,
3865 given == 1 && !kwonly_given ? "was" : "were");
3866 Py_DECREF(sig);
3867 Py_DECREF(kwonly_sig);
3868}
3869
Guido van Rossumc2e20742006-02-27 22:32:47 +00003870/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003871 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003872 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003873
Victor Stinner40ee3012014-06-16 15:59:28 +02003874static PyObject *
3875_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Victor Stinner74319ae2016-08-25 00:04:09 +02003876 PyObject **args, Py_ssize_t argcount,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003877 PyObject **kwnames, PyObject **kwargs,
3878 Py_ssize_t kwcount, int kwstep,
Victor Stinner74319ae2016-08-25 00:04:09 +02003879 PyObject **defs, Py_ssize_t defcount,
3880 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003881 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003882{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003883 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003884 PyFrameObject *f;
3885 PyObject *retval = NULL;
3886 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003887 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003888 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003889 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3890 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003891 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003893 if (globals == NULL) {
3894 PyErr_SetString(PyExc_SystemError,
3895 "PyEval_EvalCodeEx: NULL globals");
3896 return NULL;
3897 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003898
Victor Stinnerc7020012016-08-16 23:40:29 +02003899 /* Create the frame */
3900 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003901 assert(tstate != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003902 f = PyFrame_New(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003903 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003904 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003905 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003906 fastlocals = f->f_localsplus;
3907 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003908
Victor Stinnerc7020012016-08-16 23:40:29 +02003909 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003910 if (co->co_flags & CO_VARKEYWORDS) {
3911 kwdict = PyDict_New();
3912 if (kwdict == NULL)
3913 goto fail;
3914 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003915 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003916 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003917 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003918 SETLOCAL(i, kwdict);
3919 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003920 else {
3921 kwdict = NULL;
3922 }
3923
3924 /* Copy positional arguments into local variables */
3925 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003926 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003927 }
3928 else {
3929 n = argcount;
3930 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003931 for (i = 0; i < n; i++) {
3932 x = args[i];
3933 Py_INCREF(x);
3934 SETLOCAL(i, x);
3935 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003936
3937 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003938 if (co->co_flags & CO_VARARGS) {
3939 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003940 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003941 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003942 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003943 SETLOCAL(total_args, u);
3944 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003945 x = args[i];
3946 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003947 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003948 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003949 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003950
Serhiy Storchakab7281052016-09-12 00:52:40 +03003951 /* Handle keyword arguments passed as two strided arrays */
3952 kwcount *= kwstep;
3953 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003954 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003955 PyObject *keyword = kwnames[i];
3956 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003957 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003958
Benjamin Petersonb204a422011-06-05 22:04:07 -05003959 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3960 PyErr_Format(PyExc_TypeError,
3961 "%U() keywords must be strings",
3962 co->co_name);
3963 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003964 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003965
Benjamin Petersonb204a422011-06-05 22:04:07 -05003966 /* Speed hack: do raw pointer compares. As names are
3967 normally interned this should almost always hit. */
3968 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3969 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003970 PyObject *name = co_varnames[j];
3971 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003972 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003973 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003974 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003975
Benjamin Petersonb204a422011-06-05 22:04:07 -05003976 /* Slow fallback, just in case */
3977 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003978 PyObject *name = co_varnames[j];
3979 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3980 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003981 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003982 }
3983 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003984 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003985 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003986 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003987
Benjamin Petersonb204a422011-06-05 22:04:07 -05003988 if (j >= total_args && kwdict == NULL) {
3989 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003990 "%U() got an unexpected keyword argument '%S'",
3991 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003992 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003993 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003994
Christian Heimes0bd447f2013-07-20 14:48:10 +02003995 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3996 goto fail;
3997 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003998 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003999
Benjamin Petersonb204a422011-06-05 22:04:07 -05004000 kw_found:
4001 if (GETLOCAL(j) != NULL) {
4002 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02004003 "%U() got multiple values for argument '%S'",
4004 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004005 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004006 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004007 Py_INCREF(value);
4008 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004009 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004010
4011 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004012 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004013 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004014 goto fail;
4015 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004016
4017 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004018 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02004019 Py_ssize_t m = co->co_argcount - defcount;
4020 Py_ssize_t missing = 0;
4021 for (i = argcount; i < m; i++) {
4022 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004023 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004024 }
4025 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004026 if (missing) {
4027 missing_arguments(co, missing, defcount, fastlocals);
4028 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004029 }
4030 if (n > m)
4031 i = n - m;
4032 else
4033 i = 0;
4034 for (; i < defcount; i++) {
4035 if (GETLOCAL(m+i) == NULL) {
4036 PyObject *def = defs[i];
4037 Py_INCREF(def);
4038 SETLOCAL(m+i, def);
4039 }
4040 }
4041 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004042
4043 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004044 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004045 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004046 for (i = co->co_argcount; i < total_args; i++) {
4047 PyObject *name;
4048 if (GETLOCAL(i) != NULL)
4049 continue;
4050 name = PyTuple_GET_ITEM(co->co_varnames, i);
4051 if (kwdefs != NULL) {
4052 PyObject *def = PyDict_GetItem(kwdefs, name);
4053 if (def) {
4054 Py_INCREF(def);
4055 SETLOCAL(i, def);
4056 continue;
4057 }
4058 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004059 missing++;
4060 }
4061 if (missing) {
4062 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004063 goto fail;
4064 }
4065 }
4066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004067 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004068 vars into frame. */
4069 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004070 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05004071 int arg;
4072 /* Possibly account for the cell variable being an argument. */
4073 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004074 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004075 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004076 /* Clear the local copy. */
4077 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004078 }
4079 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004080 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004081 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004082 if (c == NULL)
4083 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004084 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004085 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004086
4087 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004088 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4089 PyObject *o = PyTuple_GET_ITEM(closure, i);
4090 Py_INCREF(o);
4091 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004092 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004093
Yury Selivanoveb636452016-09-08 22:01:51 -07004094 /* Handle generator/coroutine/asynchronous generator */
4095 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004096 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04004097 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004098 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004099
4100 if (is_coro && tstate->in_coroutine_wrapper) {
4101 assert(coro_wrapper != NULL);
4102 PyErr_Format(PyExc_RuntimeError,
4103 "coroutine wrapper %.200R attempted "
4104 "to recursively wrap %.200R",
4105 coro_wrapper,
4106 co);
4107 goto fail;
4108 }
Yury Selivanov75445082015-05-11 22:57:16 -04004109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004110 /* Don't need to keep the reference to f_back, it will be set
4111 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004112 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004114 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004116 /* Create a new generator that owns the ready to run frame
4117 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004118 if (is_coro) {
4119 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004120 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4121 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004122 } else {
4123 gen = PyGen_NewWithQualName(f, name, qualname);
4124 }
Yury Selivanov75445082015-05-11 22:57:16 -04004125 if (gen == NULL)
4126 return NULL;
4127
Yury Selivanov94c22632015-06-04 10:16:51 -04004128 if (is_coro && coro_wrapper != NULL) {
4129 PyObject *wrapped;
4130 tstate->in_coroutine_wrapper = 1;
4131 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4132 tstate->in_coroutine_wrapper = 0;
4133 return wrapped;
4134 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004135
Yury Selivanov75445082015-05-11 22:57:16 -04004136 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004137 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004139 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004140
Thomas Woutersce272b62007-09-19 21:19:28 +00004141fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004143 /* decref'ing the frame can cause __del__ methods to get invoked,
4144 which can call back into Python. While we're done with the
4145 current Python frame (f), the associated C stack is still in use,
4146 so recursion_depth must be boosted for the duration.
4147 */
4148 assert(tstate != NULL);
4149 ++tstate->recursion_depth;
4150 Py_DECREF(f);
4151 --tstate->recursion_depth;
4152 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004153}
4154
Victor Stinner40ee3012014-06-16 15:59:28 +02004155PyObject *
4156PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
4157 PyObject **args, int argcount, PyObject **kws, int kwcount,
4158 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
4159{
4160 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004161 args, argcount,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004162 kws, kws + 1, kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004163 defs, defcount,
4164 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004165 NULL, NULL);
4166}
Tim Peters5ca576e2001-06-18 22:08:13 +00004167
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004168static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004169special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004170{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004171 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004172 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004173 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004174 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004175 return NULL;
4176 }
4177 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004178}
4179
4180
Benjamin Peterson87880242011-07-03 16:48:31 -05004181/* These 3 functions deal with the exception state of generators. */
4182
4183static void
4184save_exc_state(PyThreadState *tstate, PyFrameObject *f)
4185{
4186 PyObject *type, *value, *traceback;
4187 Py_XINCREF(tstate->exc_type);
4188 Py_XINCREF(tstate->exc_value);
4189 Py_XINCREF(tstate->exc_traceback);
4190 type = f->f_exc_type;
4191 value = f->f_exc_value;
4192 traceback = f->f_exc_traceback;
4193 f->f_exc_type = tstate->exc_type;
4194 f->f_exc_value = tstate->exc_value;
4195 f->f_exc_traceback = tstate->exc_traceback;
4196 Py_XDECREF(type);
4197 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004198 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05004199}
4200
4201static void
4202swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
4203{
4204 PyObject *tmp;
4205 tmp = tstate->exc_type;
4206 tstate->exc_type = f->f_exc_type;
4207 f->f_exc_type = tmp;
4208 tmp = tstate->exc_value;
4209 tstate->exc_value = f->f_exc_value;
4210 f->f_exc_value = tmp;
4211 tmp = tstate->exc_traceback;
4212 tstate->exc_traceback = f->f_exc_traceback;
4213 f->f_exc_traceback = tmp;
4214}
4215
4216static void
4217restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
4218{
4219 PyObject *type, *value, *tb;
4220 type = tstate->exc_type;
4221 value = tstate->exc_value;
4222 tb = tstate->exc_traceback;
4223 tstate->exc_type = f->f_exc_type;
4224 tstate->exc_value = f->f_exc_value;
4225 tstate->exc_traceback = f->f_exc_traceback;
4226 f->f_exc_type = NULL;
4227 f->f_exc_value = NULL;
4228 f->f_exc_traceback = NULL;
4229 Py_XDECREF(type);
4230 Py_XDECREF(value);
4231 Py_XDECREF(tb);
4232}
4233
4234
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004235/* Logic for the raise statement (too complicated for inlining).
4236 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004237static int
Collin Winter828f04a2007-08-31 00:04:24 +00004238do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004239{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004240 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004242 if (exc == NULL) {
4243 /* Reraise */
4244 PyThreadState *tstate = PyThreadState_GET();
4245 PyObject *tb;
4246 type = tstate->exc_type;
4247 value = tstate->exc_value;
4248 tb = tstate->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004249 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004250 PyErr_SetString(PyExc_RuntimeError,
4251 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004252 return 0;
4253 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004254 Py_XINCREF(type);
4255 Py_XINCREF(value);
4256 Py_XINCREF(tb);
4257 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004258 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004259 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004261 /* We support the following forms of raise:
4262 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004263 raise <instance>
4264 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004266 if (PyExceptionClass_Check(exc)) {
4267 type = exc;
4268 value = PyObject_CallObject(exc, NULL);
4269 if (value == NULL)
4270 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004271 if (!PyExceptionInstance_Check(value)) {
4272 PyErr_Format(PyExc_TypeError,
4273 "calling %R should have returned an instance of "
4274 "BaseException, not %R",
4275 type, Py_TYPE(value));
4276 goto raise_error;
4277 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004278 }
4279 else if (PyExceptionInstance_Check(exc)) {
4280 value = exc;
4281 type = PyExceptionInstance_Class(exc);
4282 Py_INCREF(type);
4283 }
4284 else {
4285 /* Not something you can raise. You get an exception
4286 anyway, just not what you specified :-) */
4287 Py_DECREF(exc);
4288 PyErr_SetString(PyExc_TypeError,
4289 "exceptions must derive from BaseException");
4290 goto raise_error;
4291 }
Collin Winter828f04a2007-08-31 00:04:24 +00004292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004293 if (cause) {
4294 PyObject *fixed_cause;
4295 if (PyExceptionClass_Check(cause)) {
4296 fixed_cause = PyObject_CallObject(cause, NULL);
4297 if (fixed_cause == NULL)
4298 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004299 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004300 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004301 else if (PyExceptionInstance_Check(cause)) {
4302 fixed_cause = cause;
4303 }
4304 else if (cause == Py_None) {
4305 Py_DECREF(cause);
4306 fixed_cause = NULL;
4307 }
4308 else {
4309 PyErr_SetString(PyExc_TypeError,
4310 "exception causes must derive from "
4311 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004312 goto raise_error;
4313 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004314 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004315 }
Collin Winter828f04a2007-08-31 00:04:24 +00004316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004317 PyErr_SetObject(type, value);
4318 /* PyErr_SetObject incref's its arguments */
4319 Py_XDECREF(value);
4320 Py_XDECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004321 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004322
4323raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004324 Py_XDECREF(value);
4325 Py_XDECREF(type);
4326 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004327 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004328}
4329
Tim Petersd6d010b2001-06-21 02:49:55 +00004330/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004331 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004332
Guido van Rossum0368b722007-05-11 16:50:42 +00004333 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4334 with a variable target.
4335*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004336
Barry Warsawe42b18f1997-08-25 22:13:04 +00004337static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004338unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004339{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004340 int i = 0, j = 0;
4341 Py_ssize_t ll = 0;
4342 PyObject *it; /* iter(v) */
4343 PyObject *w;
4344 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004346 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004348 it = PyObject_GetIter(v);
4349 if (it == NULL)
4350 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00004351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004352 for (; i < argcnt; i++) {
4353 w = PyIter_Next(it);
4354 if (w == NULL) {
4355 /* Iterator done, via error or exhaustion. */
4356 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004357 if (argcntafter == -1) {
4358 PyErr_Format(PyExc_ValueError,
4359 "not enough values to unpack (expected %d, got %d)",
4360 argcnt, i);
4361 }
4362 else {
4363 PyErr_Format(PyExc_ValueError,
4364 "not enough values to unpack "
4365 "(expected at least %d, got %d)",
4366 argcnt + argcntafter, i);
4367 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004368 }
4369 goto Error;
4370 }
4371 *--sp = w;
4372 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004374 if (argcntafter == -1) {
4375 /* We better have exhausted the iterator now. */
4376 w = PyIter_Next(it);
4377 if (w == NULL) {
4378 if (PyErr_Occurred())
4379 goto Error;
4380 Py_DECREF(it);
4381 return 1;
4382 }
4383 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004384 PyErr_Format(PyExc_ValueError,
4385 "too many values to unpack (expected %d)",
4386 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004387 goto Error;
4388 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004390 l = PySequence_List(it);
4391 if (l == NULL)
4392 goto Error;
4393 *--sp = l;
4394 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004396 ll = PyList_GET_SIZE(l);
4397 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004398 PyErr_Format(PyExc_ValueError,
4399 "not enough values to unpack (expected at least %d, got %zd)",
4400 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004401 goto Error;
4402 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004404 /* Pop the "after-variable" args off the list. */
4405 for (j = argcntafter; j > 0; j--, i++) {
4406 *--sp = PyList_GET_ITEM(l, ll - j);
4407 }
4408 /* Resize the list. */
4409 Py_SIZE(l) = ll - argcntafter;
4410 Py_DECREF(it);
4411 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004412
Tim Petersd6d010b2001-06-21 02:49:55 +00004413Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414 for (; i > 0; i--, sp++)
4415 Py_DECREF(*sp);
4416 Py_XDECREF(it);
4417 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004418}
4419
4420
Guido van Rossum96a42c81992-01-12 02:29:51 +00004421#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004422static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004423prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004425 printf("%s ", str);
4426 if (PyObject_Print(v, stdout, 0) != 0)
4427 PyErr_Clear(); /* Don't know what else to do */
4428 printf("\n");
4429 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004430}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004431#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004432
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004433static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004434call_exc_trace(Py_tracefunc func, PyObject *self,
4435 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004436{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004437 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004438 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004439 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004440 if (value == NULL) {
4441 value = Py_None;
4442 Py_INCREF(value);
4443 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004444 PyErr_NormalizeException(&type, &value, &orig_traceback);
4445 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004446 arg = PyTuple_Pack(3, type, value, traceback);
4447 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004448 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004449 return;
4450 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004451 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004452 Py_DECREF(arg);
4453 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004454 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004455 else {
4456 Py_XDECREF(type);
4457 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004458 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004459 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004460}
4461
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004462static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004463call_trace_protected(Py_tracefunc func, PyObject *obj,
4464 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004465 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004466{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004467 PyObject *type, *value, *traceback;
4468 int err;
4469 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004470 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004471 if (err == 0)
4472 {
4473 PyErr_Restore(type, value, traceback);
4474 return 0;
4475 }
4476 else {
4477 Py_XDECREF(type);
4478 Py_XDECREF(value);
4479 Py_XDECREF(traceback);
4480 return -1;
4481 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004482}
4483
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004484static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004485call_trace(Py_tracefunc func, PyObject *obj,
4486 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004487 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004488{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 int result;
4490 if (tstate->tracing)
4491 return 0;
4492 tstate->tracing++;
4493 tstate->use_tracing = 0;
4494 result = func(obj, frame, what, arg);
4495 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4496 || (tstate->c_profilefunc != NULL));
4497 tstate->tracing--;
4498 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004499}
4500
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004501PyObject *
4502_PyEval_CallTracing(PyObject *func, PyObject *args)
4503{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004504 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004505 int save_tracing = tstate->tracing;
4506 int save_use_tracing = tstate->use_tracing;
4507 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004509 tstate->tracing = 0;
4510 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4511 || (tstate->c_profilefunc != NULL));
4512 result = PyObject_Call(func, args, NULL);
4513 tstate->tracing = save_tracing;
4514 tstate->use_tracing = save_use_tracing;
4515 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004516}
4517
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004518/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004519static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004520maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004521 PyThreadState *tstate, PyFrameObject *frame,
4522 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004523{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004524 int result = 0;
4525 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004527 /* If the last instruction executed isn't in the current
4528 instruction window, reset the window.
4529 */
4530 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4531 PyAddrPair bounds;
4532 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4533 &bounds);
4534 *instr_lb = bounds.ap_lower;
4535 *instr_ub = bounds.ap_upper;
4536 }
4537 /* If the last instruction falls at the start of a line or if
4538 it represents a jump backwards, update the frame's line
4539 number and call the trace function. */
4540 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
4541 frame->f_lineno = line;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004542 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004543 }
4544 *instr_prev = frame->f_lasti;
4545 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004546}
4547
Fred Drake5755ce62001-06-27 19:19:46 +00004548void
4549PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004551 PyThreadState *tstate = PyThreadState_GET();
4552 PyObject *temp = tstate->c_profileobj;
4553 Py_XINCREF(arg);
4554 tstate->c_profilefunc = NULL;
4555 tstate->c_profileobj = NULL;
4556 /* Must make sure that tracing is not ignored if 'temp' is freed */
4557 tstate->use_tracing = tstate->c_tracefunc != NULL;
4558 Py_XDECREF(temp);
4559 tstate->c_profilefunc = func;
4560 tstate->c_profileobj = arg;
4561 /* Flag that tracing or profiling is turned on */
4562 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004563}
4564
4565void
4566PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4567{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004568 PyThreadState *tstate = PyThreadState_GET();
4569 PyObject *temp = tstate->c_traceobj;
4570 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4571 Py_XINCREF(arg);
4572 tstate->c_tracefunc = NULL;
4573 tstate->c_traceobj = NULL;
4574 /* Must make sure that profiling is not ignored if 'temp' is freed */
4575 tstate->use_tracing = tstate->c_profilefunc != NULL;
4576 Py_XDECREF(temp);
4577 tstate->c_tracefunc = func;
4578 tstate->c_traceobj = arg;
4579 /* Flag that tracing or profiling is turned on */
4580 tstate->use_tracing = ((func != NULL)
4581 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004582}
4583
Yury Selivanov75445082015-05-11 22:57:16 -04004584void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004585_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004586{
4587 PyThreadState *tstate = PyThreadState_GET();
4588
Yury Selivanov75445082015-05-11 22:57:16 -04004589 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004590 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004591}
4592
4593PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004594_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004595{
4596 PyThreadState *tstate = PyThreadState_GET();
4597 return tstate->coroutine_wrapper;
4598}
4599
Yury Selivanoveb636452016-09-08 22:01:51 -07004600void
4601_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4602{
4603 PyThreadState *tstate = PyThreadState_GET();
4604
4605 Py_XINCREF(firstiter);
4606 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4607}
4608
4609PyObject *
4610_PyEval_GetAsyncGenFirstiter(void)
4611{
4612 PyThreadState *tstate = PyThreadState_GET();
4613 return tstate->async_gen_firstiter;
4614}
4615
4616void
4617_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4618{
4619 PyThreadState *tstate = PyThreadState_GET();
4620
4621 Py_XINCREF(finalizer);
4622 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4623}
4624
4625PyObject *
4626_PyEval_GetAsyncGenFinalizer(void)
4627{
4628 PyThreadState *tstate = PyThreadState_GET();
4629 return tstate->async_gen_finalizer;
4630}
4631
Guido van Rossumb209a111997-04-29 18:18:01 +00004632PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004633PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004634{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004635 PyFrameObject *current_frame = PyEval_GetFrame();
4636 if (current_frame == NULL)
4637 return PyThreadState_GET()->interp->builtins;
4638 else
4639 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004640}
4641
Guido van Rossumb209a111997-04-29 18:18:01 +00004642PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004643PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004644{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004645 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004646 if (current_frame == NULL) {
4647 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004648 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004649 }
4650
4651 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4652 return NULL;
4653
4654 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004655 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004656}
4657
Guido van Rossumb209a111997-04-29 18:18:01 +00004658PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004659PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004660{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004661 PyFrameObject *current_frame = PyEval_GetFrame();
4662 if (current_frame == NULL)
4663 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004664
4665 assert(current_frame->f_globals != NULL);
4666 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004667}
4668
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004669PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004670PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004671{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004672 PyThreadState *tstate = PyThreadState_GET();
4673 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004674}
4675
Guido van Rossum6135a871995-01-09 17:53:26 +00004676int
Tim Peters5ba58662001-07-16 02:29:45 +00004677PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004678{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004679 PyFrameObject *current_frame = PyEval_GetFrame();
4680 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004682 if (current_frame != NULL) {
4683 const int codeflags = current_frame->f_code->co_flags;
4684 const int compilerflags = codeflags & PyCF_MASK;
4685 if (compilerflags) {
4686 result = 1;
4687 cf->cf_flags |= compilerflags;
4688 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004689#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004690 if (codeflags & CO_GENERATOR_ALLOWED) {
4691 result = 1;
4692 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4693 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004694#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004695 }
4696 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004697}
4698
Guido van Rossum3f5da241990-12-20 15:06:42 +00004699
Guido van Rossum681d79a1995-07-18 14:51:37 +00004700/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00004701 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00004702
Guido van Rossumb209a111997-04-29 18:18:01 +00004703PyObject *
Victor Stinner8a31c822016-08-19 17:12:23 +02004704PyEval_CallObjectWithKeywords(PyObject *func, PyObject *args, PyObject *kwargs)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004705{
Victor Stinner59b356d2015-03-16 11:52:32 +01004706#ifdef Py_DEBUG
4707 /* PyEval_CallObjectWithKeywords() must not be called with an exception
4708 set. It raises a new exception if parameters are invalid or if
4709 PyTuple_New() fails, and so the original exception is lost. */
4710 assert(!PyErr_Occurred());
4711#endif
4712
INADA Naoki023532e2017-03-01 21:14:43 +09004713 if (args != NULL && !PyTuple_Check(args)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004714 PyErr_SetString(PyExc_TypeError,
4715 "argument list must be a tuple");
4716 return NULL;
4717 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00004718
Victor Stinner8a31c822016-08-19 17:12:23 +02004719 if (kwargs != NULL && !PyDict_Check(kwargs)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004720 PyErr_SetString(PyExc_TypeError,
4721 "keyword list must be a dictionary");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004722 return NULL;
4723 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00004724
INADA Naoki023532e2017-03-01 21:14:43 +09004725 if (args == NULL) {
4726 return _PyObject_FastCallDict(func, NULL, 0, kwargs);
4727 }
4728 else {
4729 return PyObject_Call(func, args, kwargs);
4730 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004731}
4732
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004733const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004734PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004735{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004736 if (PyMethod_Check(func))
4737 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4738 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004739 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004740 else if (PyCFunction_Check(func))
4741 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4742 else
4743 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004744}
4745
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004746const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004747PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004748{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004749 if (PyMethod_Check(func))
4750 return "()";
4751 else if (PyFunction_Check(func))
4752 return "()";
4753 else if (PyCFunction_Check(func))
4754 return "()";
4755 else
4756 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004757}
4758
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004759#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004760if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004761 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4762 tstate, tstate->frame, \
4763 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004764 x = NULL; \
4765 } \
4766 else { \
4767 x = call; \
4768 if (tstate->c_profilefunc != NULL) { \
4769 if (x == NULL) { \
4770 call_trace_protected(tstate->c_profilefunc, \
4771 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004772 tstate, tstate->frame, \
4773 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004774 /* XXX should pass (type, value, tb) */ \
4775 } else { \
4776 if (call_trace(tstate->c_profilefunc, \
4777 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004778 tstate, tstate->frame, \
4779 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004780 Py_DECREF(x); \
4781 x = NULL; \
4782 } \
4783 } \
4784 } \
4785 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004786} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004787 x = call; \
4788 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004789
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004790static PyObject *
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004791call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004792{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004793 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004794 PyObject *func = *pfunc;
4795 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004796 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4797 Py_ssize_t nargs = oparg - nkwargs;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004798 PyObject **stack;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004800 /* Always dispatch PyCFunction first, because these are
4801 presumed to be the most frequent callable object.
4802 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004803 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004804 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004806 PCALL(PCALL_CFUNCTION);
Victor Stinner4a7cc882015-03-06 23:35:27 +01004807
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004808 stack = (*pp_stack) - nargs - nkwargs;
4809 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004810 }
4811 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004812 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4813 /* optimize access to bound methods */
4814 PyObject *self = PyMethod_GET_SELF(func);
4815 PCALL(PCALL_METHOD);
4816 PCALL(PCALL_BOUND_METHOD);
4817 Py_INCREF(self);
4818 func = PyMethod_GET_FUNCTION(func);
4819 Py_INCREF(func);
4820 Py_SETREF(*pfunc, self);
4821 nargs++;
4822 }
4823 else {
4824 Py_INCREF(func);
4825 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004826
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004827 stack = (*pp_stack) - nargs - nkwargs;
Victor Stinner4a7cc882015-03-06 23:35:27 +01004828
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004829 if (PyFunction_Check(func)) {
4830 x = fast_function(func, stack, nargs, kwnames);
4831 }
4832 else {
4833 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4834 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004835
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004836 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004837 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004838
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004839 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4840
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004841 /* Clear the stack of the function object. Also removes
4842 the arguments in case they weren't consumed already
Victor Stinnere90bdb12016-08-25 23:26:50 +02004843 (fast_function() and err_args() leave them on the stack).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004844 */
4845 while ((*pp_stack) > pfunc) {
4846 w = EXT_POP(*pp_stack);
4847 Py_DECREF(w);
4848 PCALL(PCALL_POP);
4849 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004851 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004852}
4853
Victor Stinnere90bdb12016-08-25 23:26:50 +02004854/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004855 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004856 For the simplest case -- a function that takes only positional
4857 arguments and is called with only positional arguments -- it
4858 inlines the most primitive frame setup code from
4859 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4860 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004861*/
4862
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004863static PyObject*
Victor Stinnerd8735722016-09-09 12:36:44 -07004864_PyFunction_FastCall(PyCodeObject *co, PyObject **args, Py_ssize_t nargs,
4865 PyObject *globals)
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004866{
4867 PyFrameObject *f;
4868 PyThreadState *tstate = PyThreadState_GET();
4869 PyObject **fastlocals;
4870 Py_ssize_t i;
4871 PyObject *result;
4872
4873 PCALL(PCALL_FASTER_FUNCTION);
4874 assert(globals != NULL);
4875 /* XXX Perhaps we should create a specialized
4876 PyFrame_New() that doesn't take locals, but does
4877 take builtins without sanity checking them.
4878 */
4879 assert(tstate != NULL);
4880 f = PyFrame_New(tstate, co, globals, NULL);
4881 if (f == NULL) {
4882 return NULL;
4883 }
4884
4885 fastlocals = f->f_localsplus;
4886
Victor Stinner74319ae2016-08-25 00:04:09 +02004887 for (i = 0; i < nargs; i++) {
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004888 Py_INCREF(*args);
4889 fastlocals[i] = *args++;
4890 }
4891 result = PyEval_EvalFrameEx(f,0);
4892
4893 ++tstate->recursion_depth;
4894 Py_DECREF(f);
4895 --tstate->recursion_depth;
4896
4897 return result;
4898}
4899
Victor Stinnere90bdb12016-08-25 23:26:50 +02004900static PyObject *
Victor Stinnerd8735722016-09-09 12:36:44 -07004901fast_function(PyObject *func, PyObject **stack,
4902 Py_ssize_t nargs, PyObject *kwnames)
Jeremy Hylton52820442001-01-03 23:52:36 +00004903{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004904 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4905 PyObject *globals = PyFunction_GET_GLOBALS(func);
4906 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004907 PyObject *kwdefs, *closure, *name, *qualname;
4908 PyObject **d;
Victor Stinnerd8735722016-09-09 12:36:44 -07004909 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004910 Py_ssize_t nd;
Victor Stinnerd8735722016-09-09 12:36:44 -07004911
Victor Stinner57f91ac2016-09-12 13:37:07 +02004912 assert(PyFunction_Check(func));
4913 assert(nargs >= 0);
4914 assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
Victor Stinnerd8735722016-09-09 12:36:44 -07004915 assert((nargs == 0 && nkwargs == 0) || stack != NULL);
Victor Stinner57f91ac2016-09-12 13:37:07 +02004916 /* kwnames must only contains str strings, no subclass, and all keys must
4917 be unique */
Victor Stinner577e1f82016-08-25 00:29:32 +02004918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004919 PCALL(PCALL_FUNCTION);
4920 PCALL(PCALL_FAST_FUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004921
Victor Stinner74319ae2016-08-25 00:04:09 +02004922 if (co->co_kwonlyargcount == 0 && nkwargs == 0 &&
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004923 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
4924 {
Victor Stinner2eedc112016-08-22 12:29:42 +02004925 if (argdefs == NULL && co->co_argcount == nargs) {
Victor Stinnerd8735722016-09-09 12:36:44 -07004926 return _PyFunction_FastCall(co, stack, nargs, globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02004927 }
4928 else if (nargs == 0 && argdefs != NULL
4929 && co->co_argcount == Py_SIZE(argdefs)) {
4930 /* function called with no arguments, but all parameters have
4931 a default value: use default values as arguments .*/
4932 stack = &PyTuple_GET_ITEM(argdefs, 0);
Victor Stinnerd8735722016-09-09 12:36:44 -07004933 return _PyFunction_FastCall(co, stack, Py_SIZE(argdefs), globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02004934 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004935 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004936
4937 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
4938 closure = PyFunction_GET_CLOSURE(func);
4939 name = ((PyFunctionObject *)func) -> func_name;
4940 qualname = ((PyFunctionObject *)func) -> func_qualname;
4941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004942 if (argdefs != NULL) {
4943 d = &PyTuple_GET_ITEM(argdefs, 0);
4944 nd = Py_SIZE(argdefs);
4945 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004946 else {
4947 d = NULL;
4948 nd = 0;
4949 }
4950 return _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
Victor Stinner2eedc112016-08-22 12:29:42 +02004951 stack, nargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004952 nkwargs ? &PyTuple_GET_ITEM(kwnames, 0) : NULL,
4953 stack + nargs,
4954 nkwargs, 1,
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004955 d, (int)nd, kwdefs,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004956 closure, name, qualname);
4957}
4958
4959PyObject *
Victor Stinnerd8735722016-09-09 12:36:44 -07004960_PyFunction_FastCallKeywords(PyObject *func, PyObject **stack,
4961 Py_ssize_t nargs, PyObject *kwnames)
4962{
4963 return fast_function(func, stack, nargs, kwnames);
4964}
4965
4966PyObject *
Victor Stinner74319ae2016-08-25 00:04:09 +02004967_PyFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs,
Victor Stinnerb9009392016-08-22 23:15:44 +02004968 PyObject *kwargs)
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004969{
4970 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4971 PyObject *globals = PyFunction_GET_GLOBALS(func);
4972 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4973 PyObject *kwdefs, *closure, *name, *qualname;
Victor Stinnerb9009392016-08-22 23:15:44 +02004974 PyObject *kwtuple, **k;
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004975 PyObject **d;
Victor Stinner74319ae2016-08-25 00:04:09 +02004976 Py_ssize_t nd, nk;
Victor Stinnerb9009392016-08-22 23:15:44 +02004977 PyObject *result;
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004978
Victor Stinner74319ae2016-08-25 00:04:09 +02004979 assert(func != NULL);
4980 assert(nargs >= 0);
4981 assert(nargs == 0 || args != NULL);
Victor Stinnerb9009392016-08-22 23:15:44 +02004982 assert(kwargs == NULL || PyDict_Check(kwargs));
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004983
Victor Stinner577e1f82016-08-25 00:29:32 +02004984 PCALL(PCALL_FUNCTION);
4985 PCALL(PCALL_FAST_FUNCTION);
4986
Victor Stinnerb9009392016-08-22 23:15:44 +02004987 if (co->co_kwonlyargcount == 0 &&
4988 (kwargs == NULL || PyDict_Size(kwargs) == 0) &&
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004989 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
4990 {
Victor Stinnerb9009392016-08-22 23:15:44 +02004991 /* Fast paths */
Victor Stinner2eedc112016-08-22 12:29:42 +02004992 if (argdefs == NULL && co->co_argcount == nargs) {
Victor Stinnerd8735722016-09-09 12:36:44 -07004993 return _PyFunction_FastCall(co, args, nargs, globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02004994 }
4995 else if (nargs == 0 && argdefs != NULL
4996 && co->co_argcount == Py_SIZE(argdefs)) {
4997 /* function called with no arguments, but all parameters have
4998 a default value: use default values as arguments .*/
4999 args = &PyTuple_GET_ITEM(argdefs, 0);
Victor Stinnerd8735722016-09-09 12:36:44 -07005000 return _PyFunction_FastCall(co, args, Py_SIZE(argdefs), globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02005001 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005002 }
5003
Victor Stinnerb9009392016-08-22 23:15:44 +02005004 if (kwargs != NULL) {
5005 Py_ssize_t pos, i;
5006 nk = PyDict_Size(kwargs);
5007
5008 kwtuple = PyTuple_New(2 * nk);
5009 if (kwtuple == NULL) {
5010 return NULL;
5011 }
5012
5013 k = &PyTuple_GET_ITEM(kwtuple, 0);
5014 pos = i = 0;
5015 while (PyDict_Next(kwargs, &pos, &k[i], &k[i+1])) {
5016 Py_INCREF(k[i]);
5017 Py_INCREF(k[i+1]);
5018 i += 2;
5019 }
5020 nk = i / 2;
5021 }
5022 else {
5023 kwtuple = NULL;
5024 k = NULL;
5025 nk = 0;
5026 }
5027
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005028 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
5029 closure = PyFunction_GET_CLOSURE(func);
5030 name = ((PyFunctionObject *)func) -> func_name;
5031 qualname = ((PyFunctionObject *)func) -> func_qualname;
5032
5033 if (argdefs != NULL) {
5034 d = &PyTuple_GET_ITEM(argdefs, 0);
5035 nd = Py_SIZE(argdefs);
5036 }
5037 else {
5038 d = NULL;
5039 nd = 0;
5040 }
Victor Stinnerb9009392016-08-22 23:15:44 +02005041
5042 result = _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
5043 args, nargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03005044 k, k + 1, nk, 2,
Victor Stinnerb9009392016-08-22 23:15:44 +02005045 d, nd, kwdefs,
5046 closure, name, qualname);
5047 Py_XDECREF(kwtuple);
5048 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00005049}
5050
5051static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005052do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005053{
Jeremy Hylton985eba52003-02-05 23:13:00 +00005054#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005055 /* At this point, we have to look at the type of func to
5056 update the call stats properly. Do it here so as to avoid
5057 exposing the call stats machinery outside ceval.c
5058 */
5059 if (PyFunction_Check(func))
5060 PCALL(PCALL_FUNCTION);
5061 else if (PyMethod_Check(func))
5062 PCALL(PCALL_METHOD);
5063 else if (PyType_Check(func))
5064 PCALL(PCALL_TYPE);
5065 else if (PyCFunction_Check(func))
5066 PCALL(PCALL_CFUNCTION);
5067 else
5068 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00005069#endif
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005071 if (PyCFunction_Check(func)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005072 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005073 PyThreadState *tstate = PyThreadState_GET();
5074 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005075 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005076 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005077 else {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005078 return PyObject_Call(func, callargs, kwdict);
Victor Stinner74319ae2016-08-25 00:04:09 +02005079 }
Jeremy Hylton52820442001-01-03 23:52:36 +00005080}
5081
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005082/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005083 nb_index slot defined, and store in *pi.
5084 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang98b49a02017-05-10 19:00:15 +08005085 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005086 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005087*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005088int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005089_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005090{
Serhiy Storchakabf4bb2e2017-03-30 19:46:59 +03005091 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005092 Py_ssize_t x;
5093 if (PyIndex_Check(v)) {
5094 x = PyNumber_AsSsize_t(v, NULL);
5095 if (x == -1 && PyErr_Occurred())
5096 return 0;
5097 }
5098 else {
5099 PyErr_SetString(PyExc_TypeError,
5100 "slice indices must be integers or "
5101 "None or have an __index__ method");
5102 return 0;
5103 }
5104 *pi = x;
5105 }
5106 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005107}
5108
Serhiy Storchakabf4bb2e2017-03-30 19:46:59 +03005109int
5110_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
5111{
5112 Py_ssize_t x;
5113 if (PyIndex_Check(v)) {
5114 x = PyNumber_AsSsize_t(v, NULL);
5115 if (x == -1 && PyErr_Occurred())
5116 return 0;
5117 }
5118 else {
5119 PyErr_SetString(PyExc_TypeError,
5120 "slice indices must be integers or "
5121 "have an __index__ method");
5122 return 0;
5123 }
5124 *pi = x;
5125 return 1;
5126}
5127
5128
Guido van Rossum486364b2007-06-30 05:01:58 +00005129#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005130 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00005131
Guido van Rossumb209a111997-04-29 18:18:01 +00005132static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02005133cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005134{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005135 int res = 0;
5136 switch (op) {
5137 case PyCmp_IS:
5138 res = (v == w);
5139 break;
5140 case PyCmp_IS_NOT:
5141 res = (v != w);
5142 break;
5143 case PyCmp_IN:
5144 res = PySequence_Contains(w, v);
5145 if (res < 0)
5146 return NULL;
5147 break;
5148 case PyCmp_NOT_IN:
5149 res = PySequence_Contains(w, v);
5150 if (res < 0)
5151 return NULL;
5152 res = !res;
5153 break;
5154 case PyCmp_EXC_MATCH:
5155 if (PyTuple_Check(w)) {
5156 Py_ssize_t i, length;
5157 length = PyTuple_Size(w);
5158 for (i = 0; i < length; i += 1) {
5159 PyObject *exc = PyTuple_GET_ITEM(w, i);
5160 if (!PyExceptionClass_Check(exc)) {
5161 PyErr_SetString(PyExc_TypeError,
5162 CANNOT_CATCH_MSG);
5163 return NULL;
5164 }
5165 }
5166 }
5167 else {
5168 if (!PyExceptionClass_Check(w)) {
5169 PyErr_SetString(PyExc_TypeError,
5170 CANNOT_CATCH_MSG);
5171 return NULL;
5172 }
5173 }
5174 res = PyErr_GivenExceptionMatches(v, w);
5175 break;
5176 default:
5177 return PyObject_RichCompare(v, w, op);
5178 }
5179 v = res ? Py_True : Py_False;
5180 Py_INCREF(v);
5181 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005182}
5183
Thomas Wouters52152252000-08-17 22:55:00 +00005184static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005185import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
5186{
5187 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005188 PyObject *import_func, *res;
5189 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005190
5191 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
5192 if (import_func == NULL) {
5193 PyErr_SetString(PyExc_ImportError, "__import__ not found");
5194 return NULL;
5195 }
5196
5197 /* Fast path for not overloaded __import__. */
5198 if (import_func == PyThreadState_GET()->interp->import_func) {
5199 int ilevel = _PyLong_AsInt(level);
5200 if (ilevel == -1 && PyErr_Occurred()) {
5201 return NULL;
5202 }
5203 res = PyImport_ImportModuleLevelObject(
5204 name,
5205 f->f_globals,
5206 f->f_locals == NULL ? Py_None : f->f_locals,
5207 fromlist,
5208 ilevel);
5209 return res;
5210 }
5211
5212 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005213
5214 stack[0] = name;
5215 stack[1] = f->f_globals;
5216 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5217 stack[3] = fromlist;
5218 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005219 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005220 Py_DECREF(import_func);
5221 return res;
5222}
5223
5224static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00005225import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005226{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005227 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005228 _Py_IDENTIFIER(__name__);
5229 PyObject *fullmodname, *pkgname;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005231 x = PyObject_GetAttr(v, name);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005232 if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
5233 return x;
5234 /* Issue #17636: in case this failed because of a circular relative
5235 import, try to fallback on reading the module directly from
5236 sys.modules. */
5237 PyErr_Clear();
5238 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005239 if (pkgname == NULL) {
5240 goto error;
5241 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005242 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
5243 Py_DECREF(pkgname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005244 if (fullmodname == NULL) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005245 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005246 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005247 x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005248 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005249 if (x == NULL) {
5250 goto error;
5251 }
5252 Py_INCREF(x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005253 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005254 error:
5255 PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
5256 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005257}
Guido van Rossumac7be682001-01-17 15:42:30 +00005258
Thomas Wouters52152252000-08-17 22:55:00 +00005259static int
5260import_all_from(PyObject *locals, PyObject *v)
5261{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005262 _Py_IDENTIFIER(__all__);
5263 _Py_IDENTIFIER(__dict__);
5264 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005265 PyObject *dict, *name, *value;
5266 int skip_leading_underscores = 0;
5267 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005269 if (all == NULL) {
5270 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5271 return -1; /* Unexpected error */
5272 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005273 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005274 if (dict == NULL) {
5275 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5276 return -1;
5277 PyErr_SetString(PyExc_ImportError,
5278 "from-import-* object has no __dict__ and no __all__");
5279 return -1;
5280 }
5281 all = PyMapping_Keys(dict);
5282 Py_DECREF(dict);
5283 if (all == NULL)
5284 return -1;
5285 skip_leading_underscores = 1;
5286 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005288 for (pos = 0, err = 0; ; pos++) {
5289 name = PySequence_GetItem(all, pos);
5290 if (name == NULL) {
5291 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5292 err = -1;
5293 else
5294 PyErr_Clear();
5295 break;
5296 }
5297 if (skip_leading_underscores &&
5298 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005299 PyUnicode_READY(name) != -1 &&
5300 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005301 {
5302 Py_DECREF(name);
5303 continue;
5304 }
5305 value = PyObject_GetAttr(v, name);
5306 if (value == NULL)
5307 err = -1;
5308 else if (PyDict_CheckExact(locals))
5309 err = PyDict_SetItem(locals, name, value);
5310 else
5311 err = PyObject_SetItem(locals, name, value);
5312 Py_DECREF(name);
5313 Py_XDECREF(value);
5314 if (err != 0)
5315 break;
5316 }
5317 Py_DECREF(all);
5318 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005319}
5320
Guido van Rossumac7be682001-01-17 15:42:30 +00005321static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005322format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005324 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005326 if (!obj)
5327 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005328
Serhiy Storchaka06515832016-11-20 09:13:07 +02005329 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005330 if (!obj_str)
5331 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005333 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005334}
Guido van Rossum950361c1997-01-24 13:49:28 +00005335
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005336static void
5337format_exc_unbound(PyCodeObject *co, int oparg)
5338{
5339 PyObject *name;
5340 /* Don't stomp existing exception */
5341 if (PyErr_Occurred())
5342 return;
5343 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5344 name = PyTuple_GET_ITEM(co->co_cellvars,
5345 oparg);
5346 format_exc_check_arg(
5347 PyExc_UnboundLocalError,
5348 UNBOUNDLOCAL_ERROR_MSG,
5349 name);
5350 } else {
5351 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5352 PyTuple_GET_SIZE(co->co_cellvars));
5353 format_exc_check_arg(PyExc_NameError,
5354 UNBOUNDFREE_ERROR_MSG, name);
5355 }
5356}
5357
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005358static PyObject *
5359unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005360 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005361{
5362 PyObject *res;
5363 if (Py_REFCNT(v) == 2) {
5364 /* In the common case, there are 2 references to the value
5365 * stored in 'variable' when the += is performed: one on the
5366 * value stack (in 'v') and one still stored in the
5367 * 'variable'. We try to delete the variable now to reduce
5368 * the refcnt to 1.
5369 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005370 int opcode, oparg;
5371 NEXTOPARG();
5372 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005373 case STORE_FAST:
5374 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005375 PyObject **fastlocals = f->f_localsplus;
5376 if (GETLOCAL(oparg) == v)
5377 SETLOCAL(oparg, NULL);
5378 break;
5379 }
5380 case STORE_DEREF:
5381 {
5382 PyObject **freevars = (f->f_localsplus +
5383 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005384 PyObject *c = freevars[oparg];
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005385 if (PyCell_GET(c) == v)
5386 PyCell_Set(c, NULL);
5387 break;
5388 }
5389 case STORE_NAME:
5390 {
5391 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005392 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005393 PyObject *locals = f->f_locals;
5394 if (PyDict_CheckExact(locals) &&
5395 PyDict_GetItem(locals, name) == v) {
5396 if (PyDict_DelItem(locals, name) != 0) {
5397 PyErr_Clear();
5398 }
5399 }
5400 break;
5401 }
5402 }
5403 }
5404 res = v;
5405 PyUnicode_Append(&res, w);
5406 return res;
5407}
5408
Guido van Rossum950361c1997-01-24 13:49:28 +00005409#ifdef DYNAMIC_EXECUTION_PROFILE
5410
Skip Montanarof118cb12001-10-15 20:51:38 +00005411static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005412getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005413{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005414 int i;
5415 PyObject *l = PyList_New(256);
5416 if (l == NULL) return NULL;
5417 for (i = 0; i < 256; i++) {
5418 PyObject *x = PyLong_FromLong(a[i]);
5419 if (x == NULL) {
5420 Py_DECREF(l);
5421 return NULL;
5422 }
5423 PyList_SetItem(l, i, x);
5424 }
5425 for (i = 0; i < 256; i++)
5426 a[i] = 0;
5427 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005428}
5429
5430PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005431_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005432{
5433#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005434 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005435#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005436 int i;
5437 PyObject *l = PyList_New(257);
5438 if (l == NULL) return NULL;
5439 for (i = 0; i < 257; i++) {
5440 PyObject *x = getarray(dxpairs[i]);
5441 if (x == NULL) {
5442 Py_DECREF(l);
5443 return NULL;
5444 }
5445 PyList_SetItem(l, i, x);
5446 }
5447 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005448#endif
5449}
5450
5451#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005452
5453Py_ssize_t
5454_PyEval_RequestCodeExtraIndex(freefunc free)
5455{
Dino Viehland2997fec2017-06-12 18:46:35 -07005456 __PyCodeExtraState *state = __PyCodeExtraState_Get();
Brett Cannon5c4de282016-09-07 11:16:41 -07005457 Py_ssize_t new_index;
5458
Dino Viehland2997fec2017-06-12 18:46:35 -07005459 if (state->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005460 return -1;
5461 }
Dino Viehland2997fec2017-06-12 18:46:35 -07005462 new_index = state->co_extra_user_count++;
5463 state->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005464 return new_index;
5465}
Łukasz Langaa785c872016-09-09 17:37:37 -07005466
5467static void
5468dtrace_function_entry(PyFrameObject *f)
5469{
5470 char* filename;
5471 char* funcname;
5472 int lineno;
5473
5474 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5475 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5476 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5477
5478 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5479}
5480
5481static void
5482dtrace_function_return(PyFrameObject *f)
5483{
5484 char* filename;
5485 char* funcname;
5486 int lineno;
5487
5488 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5489 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5490 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5491
5492 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5493}
5494
5495/* DTrace equivalent of maybe_call_line_trace. */
5496static void
5497maybe_dtrace_line(PyFrameObject *frame,
5498 int *instr_lb, int *instr_ub, int *instr_prev)
5499{
5500 int line = frame->f_lineno;
5501 char *co_filename, *co_name;
5502
5503 /* If the last instruction executed isn't in the current
5504 instruction window, reset the window.
5505 */
5506 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5507 PyAddrPair bounds;
5508 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5509 &bounds);
5510 *instr_lb = bounds.ap_lower;
5511 *instr_ub = bounds.ap_upper;
5512 }
5513 /* If the last instruction falls at the start of a line or if
5514 it represents a jump backwards, update the frame's line
5515 number and call the trace function. */
5516 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5517 frame->f_lineno = line;
5518 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5519 if (!co_filename)
5520 co_filename = "?";
5521 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5522 if (!co_name)
5523 co_name = "?";
5524 PyDTrace_LINE(co_filename, co_name, line);
5525 }
5526 *instr_prev = frame->f_lasti;
5527}