blob: 2df94cfa8068f15d460c6f023dae78ccd2832ed4 [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)) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001122 if (_Py_OPCODE(*next_instr) == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 /* Make the last opcode before
Ezio Melotti13925002011-03-16 11:05:33 +02001124 a try: finally: block uninterruptible. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 goto fast_next_opcode;
1126 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001128 if (Py_MakePendingCalls() < 0)
1129 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001131#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001132 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 /* Give another thread a chance */
1134 if (PyThreadState_Swap(NULL) != tstate)
1135 Py_FatalError("ceval: tstate mix-up");
1136 drop_gil(tstate);
1137
1138 /* Other threads may run now */
1139
1140 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001141
1142 /* Check if we should make a quick exit. */
1143 if (_Py_Finalizing && _Py_Finalizing != tstate) {
1144 drop_gil(tstate);
1145 PyThread_exit_thread();
1146 }
1147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 if (PyThreadState_Swap(tstate) != NULL)
1149 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001151#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 /* Check for asynchronous exceptions. */
1153 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001154 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 tstate->async_exc = NULL;
1156 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001157 PyErr_SetNone(exc);
1158 Py_DECREF(exc);
1159 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 }
1161 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 fast_next_opcode:
1164 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001165
Łukasz Langaa785c872016-09-09 17:37:37 -07001166 if (PyDTrace_LINE_ENABLED())
1167 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001172 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001173 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 /* see maybe_call_line_trace
1175 for expository comments */
1176 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 err = maybe_call_line_trace(tstate->c_tracefunc,
1179 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001180 tstate, f,
1181 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 /* Reload possibly changed frame fields */
1183 JUMPTO(f->f_lasti);
1184 if (f->f_stacktop != NULL) {
1185 stack_pointer = f->f_stacktop;
1186 f->f_stacktop = NULL;
1187 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001188 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001190 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001194
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001195 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001196 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001197#ifdef DYNAMIC_EXECUTION_PROFILE
1198#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 dxpairs[lastopcode][opcode]++;
1200 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001201#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001203#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001204
Guido van Rossum96a42c81992-01-12 02:29:51 +00001205#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 if (lltrace) {
1209 if (HAS_ARG(opcode)) {
1210 printf("%d: %d, %d\n",
1211 f->f_lasti, opcode, oparg);
1212 }
1213 else {
1214 printf("%d: %d\n",
1215 f->f_lasti, opcode);
1216 }
1217 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001218#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 /* BEWARE!
1223 It is essential that any operation that fails sets either
1224 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1225 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 TARGET(NOP)
1228 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001229
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001230 TARGET(LOAD_FAST) {
1231 PyObject *value = GETLOCAL(oparg);
1232 if (value == NULL) {
1233 format_exc_check_arg(PyExc_UnboundLocalError,
1234 UNBOUNDLOCAL_ERROR_MSG,
1235 PyTuple_GetItem(co->co_varnames, oparg));
1236 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001238 Py_INCREF(value);
1239 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001241 }
1242
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001243 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001244 TARGET(LOAD_CONST) {
1245 PyObject *value = GETITEM(consts, oparg);
1246 Py_INCREF(value);
1247 PUSH(value);
1248 FAST_DISPATCH();
1249 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001250
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001251 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001252 TARGET(STORE_FAST) {
1253 PyObject *value = POP();
1254 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001256 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001257
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001258 TARGET(POP_TOP) {
1259 PyObject *value = POP();
1260 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001262 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001263
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001264 TARGET(ROT_TWO) {
1265 PyObject *top = TOP();
1266 PyObject *second = SECOND();
1267 SET_TOP(second);
1268 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001270 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001271
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001272 TARGET(ROT_THREE) {
1273 PyObject *top = TOP();
1274 PyObject *second = SECOND();
1275 PyObject *third = THIRD();
1276 SET_TOP(second);
1277 SET_SECOND(third);
1278 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001280 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001281
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001282 TARGET(DUP_TOP) {
1283 PyObject *top = TOP();
1284 Py_INCREF(top);
1285 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001287 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001288
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001289 TARGET(DUP_TOP_TWO) {
1290 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001291 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001292 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001293 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001294 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001295 SET_TOP(top);
1296 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001297 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001298 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001299
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001300 TARGET(UNARY_POSITIVE) {
1301 PyObject *value = TOP();
1302 PyObject *res = PyNumber_Positive(value);
1303 Py_DECREF(value);
1304 SET_TOP(res);
1305 if (res == NULL)
1306 goto error;
1307 DISPATCH();
1308 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001309
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001310 TARGET(UNARY_NEGATIVE) {
1311 PyObject *value = TOP();
1312 PyObject *res = PyNumber_Negative(value);
1313 Py_DECREF(value);
1314 SET_TOP(res);
1315 if (res == NULL)
1316 goto error;
1317 DISPATCH();
1318 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001319
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001320 TARGET(UNARY_NOT) {
1321 PyObject *value = TOP();
1322 int err = PyObject_IsTrue(value);
1323 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 if (err == 0) {
1325 Py_INCREF(Py_True);
1326 SET_TOP(Py_True);
1327 DISPATCH();
1328 }
1329 else if (err > 0) {
1330 Py_INCREF(Py_False);
1331 SET_TOP(Py_False);
1332 err = 0;
1333 DISPATCH();
1334 }
1335 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001336 goto error;
1337 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001338
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001339 TARGET(UNARY_INVERT) {
1340 PyObject *value = TOP();
1341 PyObject *res = PyNumber_Invert(value);
1342 Py_DECREF(value);
1343 SET_TOP(res);
1344 if (res == NULL)
1345 goto error;
1346 DISPATCH();
1347 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001348
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001349 TARGET(BINARY_POWER) {
1350 PyObject *exp = POP();
1351 PyObject *base = TOP();
1352 PyObject *res = PyNumber_Power(base, exp, Py_None);
1353 Py_DECREF(base);
1354 Py_DECREF(exp);
1355 SET_TOP(res);
1356 if (res == NULL)
1357 goto error;
1358 DISPATCH();
1359 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001360
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001361 TARGET(BINARY_MULTIPLY) {
1362 PyObject *right = POP();
1363 PyObject *left = TOP();
1364 PyObject *res = PyNumber_Multiply(left, right);
1365 Py_DECREF(left);
1366 Py_DECREF(right);
1367 SET_TOP(res);
1368 if (res == NULL)
1369 goto error;
1370 DISPATCH();
1371 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001372
Benjamin Petersond51374e2014-04-09 23:55:56 -04001373 TARGET(BINARY_MATRIX_MULTIPLY) {
1374 PyObject *right = POP();
1375 PyObject *left = TOP();
1376 PyObject *res = PyNumber_MatrixMultiply(left, right);
1377 Py_DECREF(left);
1378 Py_DECREF(right);
1379 SET_TOP(res);
1380 if (res == NULL)
1381 goto error;
1382 DISPATCH();
1383 }
1384
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001385 TARGET(BINARY_TRUE_DIVIDE) {
1386 PyObject *divisor = POP();
1387 PyObject *dividend = TOP();
1388 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1389 Py_DECREF(dividend);
1390 Py_DECREF(divisor);
1391 SET_TOP(quotient);
1392 if (quotient == NULL)
1393 goto error;
1394 DISPATCH();
1395 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001396
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001397 TARGET(BINARY_FLOOR_DIVIDE) {
1398 PyObject *divisor = POP();
1399 PyObject *dividend = TOP();
1400 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1401 Py_DECREF(dividend);
1402 Py_DECREF(divisor);
1403 SET_TOP(quotient);
1404 if (quotient == NULL)
1405 goto error;
1406 DISPATCH();
1407 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001408
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001409 TARGET(BINARY_MODULO) {
1410 PyObject *divisor = POP();
1411 PyObject *dividend = TOP();
1412 PyObject *res = PyUnicode_CheckExact(dividend) ?
1413 PyUnicode_Format(dividend, divisor) :
1414 PyNumber_Remainder(dividend, divisor);
1415 Py_DECREF(divisor);
1416 Py_DECREF(dividend);
1417 SET_TOP(res);
1418 if (res == NULL)
1419 goto error;
1420 DISPATCH();
1421 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001422
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001423 TARGET(BINARY_ADD) {
1424 PyObject *right = POP();
1425 PyObject *left = TOP();
1426 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001427 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1428 CPython using bytecode, it is simply worthless.
1429 See http://bugs.python.org/issue21955 and
1430 http://bugs.python.org/issue10044 for the discussion. In short,
1431 no patch shown any impact on a realistic benchmark, only a minor
1432 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001433 if (PyUnicode_CheckExact(left) &&
1434 PyUnicode_CheckExact(right)) {
1435 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001436 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001437 }
1438 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001439 sum = PyNumber_Add(left, right);
1440 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001441 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001442 Py_DECREF(right);
1443 SET_TOP(sum);
1444 if (sum == NULL)
1445 goto error;
1446 DISPATCH();
1447 }
1448
1449 TARGET(BINARY_SUBTRACT) {
1450 PyObject *right = POP();
1451 PyObject *left = TOP();
1452 PyObject *diff = PyNumber_Subtract(left, right);
1453 Py_DECREF(right);
1454 Py_DECREF(left);
1455 SET_TOP(diff);
1456 if (diff == NULL)
1457 goto error;
1458 DISPATCH();
1459 }
1460
1461 TARGET(BINARY_SUBSCR) {
1462 PyObject *sub = POP();
1463 PyObject *container = TOP();
1464 PyObject *res = PyObject_GetItem(container, sub);
1465 Py_DECREF(container);
1466 Py_DECREF(sub);
1467 SET_TOP(res);
1468 if (res == NULL)
1469 goto error;
1470 DISPATCH();
1471 }
1472
1473 TARGET(BINARY_LSHIFT) {
1474 PyObject *right = POP();
1475 PyObject *left = TOP();
1476 PyObject *res = PyNumber_Lshift(left, right);
1477 Py_DECREF(left);
1478 Py_DECREF(right);
1479 SET_TOP(res);
1480 if (res == NULL)
1481 goto error;
1482 DISPATCH();
1483 }
1484
1485 TARGET(BINARY_RSHIFT) {
1486 PyObject *right = POP();
1487 PyObject *left = TOP();
1488 PyObject *res = PyNumber_Rshift(left, right);
1489 Py_DECREF(left);
1490 Py_DECREF(right);
1491 SET_TOP(res);
1492 if (res == NULL)
1493 goto error;
1494 DISPATCH();
1495 }
1496
1497 TARGET(BINARY_AND) {
1498 PyObject *right = POP();
1499 PyObject *left = TOP();
1500 PyObject *res = PyNumber_And(left, right);
1501 Py_DECREF(left);
1502 Py_DECREF(right);
1503 SET_TOP(res);
1504 if (res == NULL)
1505 goto error;
1506 DISPATCH();
1507 }
1508
1509 TARGET(BINARY_XOR) {
1510 PyObject *right = POP();
1511 PyObject *left = TOP();
1512 PyObject *res = PyNumber_Xor(left, right);
1513 Py_DECREF(left);
1514 Py_DECREF(right);
1515 SET_TOP(res);
1516 if (res == NULL)
1517 goto error;
1518 DISPATCH();
1519 }
1520
1521 TARGET(BINARY_OR) {
1522 PyObject *right = POP();
1523 PyObject *left = TOP();
1524 PyObject *res = PyNumber_Or(left, right);
1525 Py_DECREF(left);
1526 Py_DECREF(right);
1527 SET_TOP(res);
1528 if (res == NULL)
1529 goto error;
1530 DISPATCH();
1531 }
1532
1533 TARGET(LIST_APPEND) {
1534 PyObject *v = POP();
1535 PyObject *list = PEEK(oparg);
1536 int err;
1537 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001539 if (err != 0)
1540 goto error;
1541 PREDICT(JUMP_ABSOLUTE);
1542 DISPATCH();
1543 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001544
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001545 TARGET(SET_ADD) {
1546 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001547 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001548 int err;
1549 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001551 if (err != 0)
1552 goto error;
1553 PREDICT(JUMP_ABSOLUTE);
1554 DISPATCH();
1555 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001556
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001557 TARGET(INPLACE_POWER) {
1558 PyObject *exp = POP();
1559 PyObject *base = TOP();
1560 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1561 Py_DECREF(base);
1562 Py_DECREF(exp);
1563 SET_TOP(res);
1564 if (res == NULL)
1565 goto error;
1566 DISPATCH();
1567 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001568
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001569 TARGET(INPLACE_MULTIPLY) {
1570 PyObject *right = POP();
1571 PyObject *left = TOP();
1572 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1573 Py_DECREF(left);
1574 Py_DECREF(right);
1575 SET_TOP(res);
1576 if (res == NULL)
1577 goto error;
1578 DISPATCH();
1579 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001580
Benjamin Petersond51374e2014-04-09 23:55:56 -04001581 TARGET(INPLACE_MATRIX_MULTIPLY) {
1582 PyObject *right = POP();
1583 PyObject *left = TOP();
1584 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1585 Py_DECREF(left);
1586 Py_DECREF(right);
1587 SET_TOP(res);
1588 if (res == NULL)
1589 goto error;
1590 DISPATCH();
1591 }
1592
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001593 TARGET(INPLACE_TRUE_DIVIDE) {
1594 PyObject *divisor = POP();
1595 PyObject *dividend = TOP();
1596 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1597 Py_DECREF(dividend);
1598 Py_DECREF(divisor);
1599 SET_TOP(quotient);
1600 if (quotient == NULL)
1601 goto error;
1602 DISPATCH();
1603 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001604
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001605 TARGET(INPLACE_FLOOR_DIVIDE) {
1606 PyObject *divisor = POP();
1607 PyObject *dividend = TOP();
1608 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1609 Py_DECREF(dividend);
1610 Py_DECREF(divisor);
1611 SET_TOP(quotient);
1612 if (quotient == NULL)
1613 goto error;
1614 DISPATCH();
1615 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001616
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001617 TARGET(INPLACE_MODULO) {
1618 PyObject *right = POP();
1619 PyObject *left = TOP();
1620 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1621 Py_DECREF(left);
1622 Py_DECREF(right);
1623 SET_TOP(mod);
1624 if (mod == NULL)
1625 goto error;
1626 DISPATCH();
1627 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001628
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001629 TARGET(INPLACE_ADD) {
1630 PyObject *right = POP();
1631 PyObject *left = TOP();
1632 PyObject *sum;
1633 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1634 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001635 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001636 }
1637 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001638 sum = PyNumber_InPlaceAdd(left, right);
1639 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001640 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001641 Py_DECREF(right);
1642 SET_TOP(sum);
1643 if (sum == NULL)
1644 goto error;
1645 DISPATCH();
1646 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001647
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001648 TARGET(INPLACE_SUBTRACT) {
1649 PyObject *right = POP();
1650 PyObject *left = TOP();
1651 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1652 Py_DECREF(left);
1653 Py_DECREF(right);
1654 SET_TOP(diff);
1655 if (diff == NULL)
1656 goto error;
1657 DISPATCH();
1658 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001659
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001660 TARGET(INPLACE_LSHIFT) {
1661 PyObject *right = POP();
1662 PyObject *left = TOP();
1663 PyObject *res = PyNumber_InPlaceLshift(left, right);
1664 Py_DECREF(left);
1665 Py_DECREF(right);
1666 SET_TOP(res);
1667 if (res == NULL)
1668 goto error;
1669 DISPATCH();
1670 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001671
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001672 TARGET(INPLACE_RSHIFT) {
1673 PyObject *right = POP();
1674 PyObject *left = TOP();
1675 PyObject *res = PyNumber_InPlaceRshift(left, right);
1676 Py_DECREF(left);
1677 Py_DECREF(right);
1678 SET_TOP(res);
1679 if (res == NULL)
1680 goto error;
1681 DISPATCH();
1682 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001683
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001684 TARGET(INPLACE_AND) {
1685 PyObject *right = POP();
1686 PyObject *left = TOP();
1687 PyObject *res = PyNumber_InPlaceAnd(left, right);
1688 Py_DECREF(left);
1689 Py_DECREF(right);
1690 SET_TOP(res);
1691 if (res == NULL)
1692 goto error;
1693 DISPATCH();
1694 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001695
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001696 TARGET(INPLACE_XOR) {
1697 PyObject *right = POP();
1698 PyObject *left = TOP();
1699 PyObject *res = PyNumber_InPlaceXor(left, right);
1700 Py_DECREF(left);
1701 Py_DECREF(right);
1702 SET_TOP(res);
1703 if (res == NULL)
1704 goto error;
1705 DISPATCH();
1706 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001707
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001708 TARGET(INPLACE_OR) {
1709 PyObject *right = POP();
1710 PyObject *left = TOP();
1711 PyObject *res = PyNumber_InPlaceOr(left, right);
1712 Py_DECREF(left);
1713 Py_DECREF(right);
1714 SET_TOP(res);
1715 if (res == NULL)
1716 goto error;
1717 DISPATCH();
1718 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001719
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001720 TARGET(STORE_SUBSCR) {
1721 PyObject *sub = TOP();
1722 PyObject *container = SECOND();
1723 PyObject *v = THIRD();
1724 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 STACKADJ(-3);
Martin Panter95f53c12016-07-18 08:23:26 +00001726 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001727 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001729 Py_DECREF(container);
1730 Py_DECREF(sub);
1731 if (err != 0)
1732 goto error;
1733 DISPATCH();
1734 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001735
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001736 TARGET(STORE_ANNOTATION) {
1737 _Py_IDENTIFIER(__annotations__);
1738 PyObject *ann_dict;
1739 PyObject *ann = POP();
1740 PyObject *name = GETITEM(names, oparg);
1741 int err;
1742 if (f->f_locals == NULL) {
1743 PyErr_Format(PyExc_SystemError,
1744 "no locals found when storing annotation");
1745 Py_DECREF(ann);
1746 goto error;
1747 }
1748 /* first try to get __annotations__ from locals... */
1749 if (PyDict_CheckExact(f->f_locals)) {
1750 ann_dict = _PyDict_GetItemId(f->f_locals,
1751 &PyId___annotations__);
1752 if (ann_dict == NULL) {
1753 PyErr_SetString(PyExc_NameError,
1754 "__annotations__ not found");
1755 Py_DECREF(ann);
1756 goto error;
1757 }
1758 Py_INCREF(ann_dict);
1759 }
1760 else {
1761 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
1762 if (ann_str == NULL) {
1763 Py_DECREF(ann);
1764 goto error;
1765 }
1766 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
1767 if (ann_dict == NULL) {
1768 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
1769 PyErr_SetString(PyExc_NameError,
1770 "__annotations__ not found");
1771 }
1772 Py_DECREF(ann);
1773 goto error;
1774 }
1775 }
1776 /* ...if succeeded, __annotations__[name] = ann */
1777 if (PyDict_CheckExact(ann_dict)) {
1778 err = PyDict_SetItem(ann_dict, name, ann);
1779 }
1780 else {
1781 err = PyObject_SetItem(ann_dict, name, ann);
1782 }
1783 Py_DECREF(ann_dict);
Yury Selivanov50c584f2016-09-08 23:38:21 -07001784 Py_DECREF(ann);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001785 if (err != 0) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001786 goto error;
1787 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001788 DISPATCH();
1789 }
1790
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001791 TARGET(DELETE_SUBSCR) {
1792 PyObject *sub = TOP();
1793 PyObject *container = SECOND();
1794 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 STACKADJ(-2);
Martin Panter95f53c12016-07-18 08:23:26 +00001796 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001797 err = PyObject_DelItem(container, sub);
1798 Py_DECREF(container);
1799 Py_DECREF(sub);
1800 if (err != 0)
1801 goto error;
1802 DISPATCH();
1803 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001804
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001805 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001806 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001807 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001808 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001809 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001810 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 PyErr_SetString(PyExc_RuntimeError,
1812 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001813 Py_DECREF(value);
1814 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815 }
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001816 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001817 Py_DECREF(value);
1818 if (res == NULL)
1819 goto error;
1820 Py_DECREF(res);
1821 DISPATCH();
1822 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001823
Thomas Wouters434d0822000-08-24 20:11:32 +00001824#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001826#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001827 TARGET(RAISE_VARARGS) {
1828 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829 switch (oparg) {
1830 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001831 cause = POP(); /* cause */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001833 exc = POP(); /* exc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 case 0: /* Fallthrough */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001835 if (do_raise(exc, cause)) {
1836 why = WHY_EXCEPTION;
1837 goto fast_block_end;
1838 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001839 break;
1840 default:
1841 PyErr_SetString(PyExc_SystemError,
1842 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001843 break;
1844 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001845 goto error;
1846 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001847
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001848 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849 retval = POP();
1850 why = WHY_RETURN;
1851 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001852 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001853
Yury Selivanov75445082015-05-11 22:57:16 -04001854 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001855 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001856 PyObject *iter = NULL;
1857 PyObject *awaitable = NULL;
1858 PyObject *obj = TOP();
1859 PyTypeObject *type = Py_TYPE(obj);
1860
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001861 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001862 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001863 }
Yury Selivanov75445082015-05-11 22:57:16 -04001864
1865 if (getter != NULL) {
1866 iter = (*getter)(obj);
1867 Py_DECREF(obj);
1868 if (iter == NULL) {
1869 SET_TOP(NULL);
1870 goto error;
1871 }
1872 }
1873 else {
1874 SET_TOP(NULL);
1875 PyErr_Format(
1876 PyExc_TypeError,
1877 "'async for' requires an object with "
1878 "__aiter__ method, got %.100s",
1879 type->tp_name);
1880 Py_DECREF(obj);
1881 goto error;
1882 }
1883
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001884 if (Py_TYPE(iter)->tp_as_async != NULL &&
1885 Py_TYPE(iter)->tp_as_async->am_anext != NULL) {
1886
1887 /* Starting with CPython 3.5.2 __aiter__ should return
1888 asynchronous iterators directly (not awaitables that
1889 resolve to asynchronous iterators.)
1890
1891 Therefore, we check if the object that was returned
1892 from __aiter__ has an __anext__ method. If it does,
1893 we wrap it in an awaitable that resolves to `iter`.
1894
1895 See http://bugs.python.org/issue27243 for more
1896 details.
1897 */
1898
1899 PyObject *wrapper = _PyAIterWrapper_New(iter);
1900 Py_DECREF(iter);
1901 SET_TOP(wrapper);
1902 DISPATCH();
1903 }
1904
Yury Selivanov5376ba92015-06-22 12:19:30 -04001905 awaitable = _PyCoro_GetAwaitableIter(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001906 if (awaitable == NULL) {
1907 SET_TOP(NULL);
1908 PyErr_Format(
1909 PyExc_TypeError,
1910 "'async for' received an invalid object "
1911 "from __aiter__: %.100s",
1912 Py_TYPE(iter)->tp_name);
1913
1914 Py_DECREF(iter);
1915 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001916 } else {
Yury Selivanov75445082015-05-11 22:57:16 -04001917 Py_DECREF(iter);
1918
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001919 if (PyErr_WarnFormat(
1920 PyExc_PendingDeprecationWarning, 1,
1921 "'%.100s' implements legacy __aiter__ protocol; "
1922 "__aiter__ should return an asynchronous "
1923 "iterator, not awaitable",
1924 type->tp_name))
1925 {
1926 /* Warning was converted to an error. */
1927 Py_DECREF(awaitable);
1928 SET_TOP(NULL);
1929 goto error;
1930 }
1931 }
1932
Yury Selivanov75445082015-05-11 22:57:16 -04001933 SET_TOP(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001934 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001935 DISPATCH();
1936 }
1937
1938 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001939 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001940 PyObject *next_iter = NULL;
1941 PyObject *awaitable = NULL;
1942 PyObject *aiter = TOP();
1943 PyTypeObject *type = Py_TYPE(aiter);
1944
Yury Selivanoveb636452016-09-08 22:01:51 -07001945 if (PyAsyncGen_CheckExact(aiter)) {
1946 awaitable = type->tp_as_async->am_anext(aiter);
1947 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001948 goto error;
1949 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001950 } else {
1951 if (type->tp_as_async != NULL){
1952 getter = type->tp_as_async->am_anext;
1953 }
Yury Selivanov75445082015-05-11 22:57:16 -04001954
Yury Selivanoveb636452016-09-08 22:01:51 -07001955 if (getter != NULL) {
1956 next_iter = (*getter)(aiter);
1957 if (next_iter == NULL) {
1958 goto error;
1959 }
1960 }
1961 else {
1962 PyErr_Format(
1963 PyExc_TypeError,
1964 "'async for' requires an iterator with "
1965 "__anext__ method, got %.100s",
1966 type->tp_name);
1967 goto error;
1968 }
Yury Selivanov75445082015-05-11 22:57:16 -04001969
Yury Selivanoveb636452016-09-08 22:01:51 -07001970 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1971 if (awaitable == NULL) {
1972 PyErr_Format(
1973 PyExc_TypeError,
1974 "'async for' received an invalid object "
1975 "from __anext__: %.100s",
1976 Py_TYPE(next_iter)->tp_name);
1977
1978 Py_DECREF(next_iter);
1979 goto error;
1980 } else {
1981 Py_DECREF(next_iter);
1982 }
1983 }
Yury Selivanov75445082015-05-11 22:57:16 -04001984
1985 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001986 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001987 DISPATCH();
1988 }
1989
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001990 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001991 TARGET(GET_AWAITABLE) {
1992 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001993 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001994
1995 Py_DECREF(iterable);
1996
Yury Selivanovc724bae2016-03-02 11:30:46 -05001997 if (iter != NULL && PyCoro_CheckExact(iter)) {
1998 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1999 if (yf != NULL) {
2000 /* `iter` is a coroutine object that is being
2001 awaited, `yf` is a pointer to the current awaitable
2002 being awaited on. */
2003 Py_DECREF(yf);
2004 Py_CLEAR(iter);
2005 PyErr_SetString(
2006 PyExc_RuntimeError,
2007 "coroutine is being awaited already");
2008 /* The code below jumps to `error` if `iter` is NULL. */
2009 }
2010 }
2011
Yury Selivanov75445082015-05-11 22:57:16 -04002012 SET_TOP(iter); /* Even if it's NULL */
2013
2014 if (iter == NULL) {
2015 goto error;
2016 }
2017
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002018 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002019 DISPATCH();
2020 }
2021
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002022 TARGET(YIELD_FROM) {
2023 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002024 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002025 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002026 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2027 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002028 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002029 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002030 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002031 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002032 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002033 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002034 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002035 Py_DECREF(v);
2036 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002037 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002038 if (tstate->c_tracefunc != NULL
2039 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002040 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002041 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002042 if (err < 0)
2043 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002044 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002045 SET_TOP(val);
2046 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002047 }
Martin Panter95f53c12016-07-18 08:23:26 +00002048 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002049 f->f_stacktop = stack_pointer;
2050 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002051 /* and repeat... */
Serhiy Storchakaab874002016-09-11 13:48:15 +03002052 f->f_lasti -= sizeof(_Py_CODEUNIT);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002053 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002054 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002055
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002056 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002058
2059 if (co->co_flags & CO_ASYNC_GENERATOR) {
2060 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2061 Py_DECREF(retval);
2062 if (w == NULL) {
2063 retval = NULL;
2064 goto error;
2065 }
2066 retval = w;
2067 }
2068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 f->f_stacktop = stack_pointer;
2070 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002072 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002073
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002074 TARGET(POP_EXCEPT) {
2075 PyTryBlock *b = PyFrame_BlockPop(f);
2076 if (b->b_type != EXCEPT_HANDLER) {
2077 PyErr_SetString(PyExc_SystemError,
2078 "popped block is not an except handler");
2079 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002080 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002081 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002083 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002084
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002085 PREDICTED(POP_BLOCK);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002086 TARGET(POP_BLOCK) {
2087 PyTryBlock *b = PyFrame_BlockPop(f);
2088 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002090 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002092 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002093 TARGET(END_FINALLY) {
2094 PyObject *status = POP();
2095 if (PyLong_Check(status)) {
2096 why = (enum why_code) PyLong_AS_LONG(status);
2097 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002098 if (why == WHY_RETURN ||
2099 why == WHY_CONTINUE)
2100 retval = POP();
2101 if (why == WHY_SILENCED) {
2102 /* An exception was silenced by 'with', we must
2103 manually unwind the EXCEPT_HANDLER block which was
2104 created when the exception was caught, otherwise
2105 the stack will be in an inconsistent state. */
2106 PyTryBlock *b = PyFrame_BlockPop(f);
2107 assert(b->b_type == EXCEPT_HANDLER);
2108 UNWIND_EXCEPT_HANDLER(b);
2109 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002110 Py_DECREF(status);
2111 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002113 Py_DECREF(status);
2114 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002115 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002116 else if (PyExceptionClass_Check(status)) {
2117 PyObject *exc = POP();
2118 PyObject *tb = POP();
2119 PyErr_Restore(status, exc, tb);
2120 why = WHY_EXCEPTION;
2121 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002123 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124 PyErr_SetString(PyExc_SystemError,
2125 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002126 Py_DECREF(status);
2127 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002128 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002129 Py_DECREF(status);
2130 DISPATCH();
2131 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002132
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002133 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002134 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002135
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002136 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002137 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002138 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
2139 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002140 PyErr_SetString(PyExc_NameError,
2141 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002142 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002143 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002144 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002145 }
2146 else {
2147 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2148 if (build_class_str == NULL)
2149 break;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002150 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2151 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002152 if (PyErr_ExceptionMatches(PyExc_KeyError))
2153 PyErr_SetString(PyExc_NameError,
2154 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002155 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002156 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002157 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002158 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002159 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002160 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002161
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002162 TARGET(STORE_NAME) {
2163 PyObject *name = GETITEM(names, oparg);
2164 PyObject *v = POP();
2165 PyObject *ns = f->f_locals;
2166 int err;
2167 if (ns == NULL) {
2168 PyErr_Format(PyExc_SystemError,
2169 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002171 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002172 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002173 if (PyDict_CheckExact(ns))
2174 err = PyDict_SetItem(ns, name, v);
2175 else
2176 err = PyObject_SetItem(ns, name, v);
2177 Py_DECREF(v);
2178 if (err != 0)
2179 goto error;
2180 DISPATCH();
2181 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002182
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002183 TARGET(DELETE_NAME) {
2184 PyObject *name = GETITEM(names, oparg);
2185 PyObject *ns = f->f_locals;
2186 int err;
2187 if (ns == NULL) {
2188 PyErr_Format(PyExc_SystemError,
2189 "no locals when deleting %R", name);
2190 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002192 err = PyObject_DelItem(ns, name);
2193 if (err != 0) {
2194 format_exc_check_arg(PyExc_NameError,
2195 NAME_ERROR_MSG,
2196 name);
2197 goto error;
2198 }
2199 DISPATCH();
2200 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002201
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002202 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002203 TARGET(UNPACK_SEQUENCE) {
2204 PyObject *seq = POP(), *item, **items;
2205 if (PyTuple_CheckExact(seq) &&
2206 PyTuple_GET_SIZE(seq) == oparg) {
2207 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002209 item = items[oparg];
2210 Py_INCREF(item);
2211 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002212 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002213 } else if (PyList_CheckExact(seq) &&
2214 PyList_GET_SIZE(seq) == oparg) {
2215 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002217 item = items[oparg];
2218 Py_INCREF(item);
2219 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002220 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002221 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222 stack_pointer + oparg)) {
2223 STACKADJ(oparg);
2224 } else {
2225 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002226 Py_DECREF(seq);
2227 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002229 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002230 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002231 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002232
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002233 TARGET(UNPACK_EX) {
2234 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2235 PyObject *seq = POP();
2236
2237 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2238 stack_pointer + totalargs)) {
2239 stack_pointer += totalargs;
2240 } else {
2241 Py_DECREF(seq);
2242 goto error;
2243 }
2244 Py_DECREF(seq);
2245 DISPATCH();
2246 }
2247
2248 TARGET(STORE_ATTR) {
2249 PyObject *name = GETITEM(names, oparg);
2250 PyObject *owner = TOP();
2251 PyObject *v = SECOND();
2252 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002254 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002255 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002256 Py_DECREF(owner);
2257 if (err != 0)
2258 goto error;
2259 DISPATCH();
2260 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002261
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002262 TARGET(DELETE_ATTR) {
2263 PyObject *name = GETITEM(names, oparg);
2264 PyObject *owner = POP();
2265 int err;
2266 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2267 Py_DECREF(owner);
2268 if (err != 0)
2269 goto error;
2270 DISPATCH();
2271 }
2272
2273 TARGET(STORE_GLOBAL) {
2274 PyObject *name = GETITEM(names, oparg);
2275 PyObject *v = POP();
2276 int err;
2277 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002279 if (err != 0)
2280 goto error;
2281 DISPATCH();
2282 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002283
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002284 TARGET(DELETE_GLOBAL) {
2285 PyObject *name = GETITEM(names, oparg);
2286 int err;
2287 err = PyDict_DelItem(f->f_globals, name);
2288 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002289 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002290 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002291 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002292 }
2293 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002294 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002295
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002296 TARGET(LOAD_NAME) {
2297 PyObject *name = GETITEM(names, oparg);
2298 PyObject *locals = f->f_locals;
2299 PyObject *v;
2300 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002302 "no locals when loading %R", name);
2303 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002304 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002305 if (PyDict_CheckExact(locals)) {
2306 v = PyDict_GetItem(locals, name);
2307 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 }
2309 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002310 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002311 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002312 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2313 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002314 PyErr_Clear();
2315 }
2316 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002317 if (v == NULL) {
2318 v = PyDict_GetItem(f->f_globals, name);
2319 Py_XINCREF(v);
2320 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002321 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002322 v = PyDict_GetItem(f->f_builtins, name);
2323 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002324 format_exc_check_arg(
2325 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002326 NAME_ERROR_MSG, name);
2327 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002328 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002329 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002330 }
2331 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002332 v = PyObject_GetItem(f->f_builtins, name);
2333 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002334 if (PyErr_ExceptionMatches(PyExc_KeyError))
2335 format_exc_check_arg(
2336 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002337 NAME_ERROR_MSG, name);
2338 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002339 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002340 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002341 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002342 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002343 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002345 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002346
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002347 TARGET(LOAD_GLOBAL) {
2348 PyObject *name = GETITEM(names, oparg);
2349 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002350 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002351 && PyDict_CheckExact(f->f_builtins))
2352 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002353 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002354 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002355 name);
2356 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002357 if (!_PyErr_OCCURRED()) {
2358 /* _PyDict_LoadGlobal() returns NULL without raising
2359 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002360 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002361 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002362 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002363 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002365 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002367 else {
2368 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002369
2370 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002371 v = PyObject_GetItem(f->f_globals, name);
2372 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002373 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2374 goto error;
2375 PyErr_Clear();
2376
Victor Stinnerb4efc962015-11-20 09:24:02 +01002377 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002378 v = PyObject_GetItem(f->f_builtins, name);
2379 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002380 if (PyErr_ExceptionMatches(PyExc_KeyError))
2381 format_exc_check_arg(
2382 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002383 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002384 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002385 }
2386 }
2387 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002388 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002390 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002391
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002392 TARGET(DELETE_FAST) {
2393 PyObject *v = GETLOCAL(oparg);
2394 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 SETLOCAL(oparg, NULL);
2396 DISPATCH();
2397 }
2398 format_exc_check_arg(
2399 PyExc_UnboundLocalError,
2400 UNBOUNDLOCAL_ERROR_MSG,
2401 PyTuple_GetItem(co->co_varnames, oparg)
2402 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002403 goto error;
2404 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002405
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002406 TARGET(DELETE_DEREF) {
2407 PyObject *cell = freevars[oparg];
2408 if (PyCell_GET(cell) != NULL) {
2409 PyCell_Set(cell, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002410 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002411 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002412 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002413 goto error;
2414 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002415
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002416 TARGET(LOAD_CLOSURE) {
2417 PyObject *cell = freevars[oparg];
2418 Py_INCREF(cell);
2419 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002421 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002422
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002423 TARGET(LOAD_CLASSDEREF) {
2424 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002425 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002426 assert(locals);
2427 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2428 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2429 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2430 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2431 if (PyDict_CheckExact(locals)) {
2432 value = PyDict_GetItem(locals, name);
2433 Py_XINCREF(value);
2434 }
2435 else {
2436 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002437 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002438 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2439 goto error;
2440 PyErr_Clear();
2441 }
2442 }
2443 if (!value) {
2444 PyObject *cell = freevars[oparg];
2445 value = PyCell_GET(cell);
2446 if (value == NULL) {
2447 format_exc_unbound(co, oparg);
2448 goto error;
2449 }
2450 Py_INCREF(value);
2451 }
2452 PUSH(value);
2453 DISPATCH();
2454 }
2455
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002456 TARGET(LOAD_DEREF) {
2457 PyObject *cell = freevars[oparg];
2458 PyObject *value = PyCell_GET(cell);
2459 if (value == NULL) {
2460 format_exc_unbound(co, oparg);
2461 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002462 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002463 Py_INCREF(value);
2464 PUSH(value);
2465 DISPATCH();
2466 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002467
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002468 TARGET(STORE_DEREF) {
2469 PyObject *v = POP();
2470 PyObject *cell = freevars[oparg];
2471 PyCell_Set(cell, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002472 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002473 DISPATCH();
2474 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002475
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002476 TARGET(BUILD_STRING) {
2477 PyObject *str;
2478 PyObject *empty = PyUnicode_New(0, 0);
2479 if (empty == NULL) {
2480 goto error;
2481 }
2482 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2483 Py_DECREF(empty);
2484 if (str == NULL)
2485 goto error;
2486 while (--oparg >= 0) {
2487 PyObject *item = POP();
2488 Py_DECREF(item);
2489 }
2490 PUSH(str);
2491 DISPATCH();
2492 }
2493
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002494 TARGET(BUILD_TUPLE) {
2495 PyObject *tup = PyTuple_New(oparg);
2496 if (tup == NULL)
2497 goto error;
2498 while (--oparg >= 0) {
2499 PyObject *item = POP();
2500 PyTuple_SET_ITEM(tup, oparg, item);
2501 }
2502 PUSH(tup);
2503 DISPATCH();
2504 }
2505
2506 TARGET(BUILD_LIST) {
2507 PyObject *list = PyList_New(oparg);
2508 if (list == NULL)
2509 goto error;
2510 while (--oparg >= 0) {
2511 PyObject *item = POP();
2512 PyList_SET_ITEM(list, oparg, item);
2513 }
2514 PUSH(list);
2515 DISPATCH();
2516 }
2517
Serhiy Storchaka73442852016-10-02 10:33:46 +03002518 TARGET(BUILD_TUPLE_UNPACK_WITH_CALL)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002519 TARGET(BUILD_TUPLE_UNPACK)
2520 TARGET(BUILD_LIST_UNPACK) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002521 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002522 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002523 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002524 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002525
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002526 if (sum == NULL)
2527 goto error;
2528
2529 for (i = oparg; i > 0; i--) {
2530 PyObject *none_val;
2531
2532 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2533 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002534 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
2535 PyErr_ExceptionMatches(PyExc_TypeError)) {
2536 PyObject *func = PEEK(1 + oparg);
2537 PyErr_Format(PyExc_TypeError,
2538 "%.200s%.200s argument after * "
2539 "must be an iterable, not %.200s",
2540 PyEval_GetFuncName(func),
2541 PyEval_GetFuncDesc(func),
2542 PEEK(i)->ob_type->tp_name);
2543 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002544 Py_DECREF(sum);
2545 goto error;
2546 }
2547 Py_DECREF(none_val);
2548 }
2549
2550 if (convert_to_tuple) {
2551 return_value = PyList_AsTuple(sum);
2552 Py_DECREF(sum);
2553 if (return_value == NULL)
2554 goto error;
2555 }
2556 else {
2557 return_value = sum;
2558 }
2559
2560 while (oparg--)
2561 Py_DECREF(POP());
2562 PUSH(return_value);
2563 DISPATCH();
2564 }
2565
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002566 TARGET(BUILD_SET) {
2567 PyObject *set = PySet_New(NULL);
2568 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002569 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002570 if (set == NULL)
2571 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002572 for (i = oparg; i > 0; i--) {
2573 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002574 if (err == 0)
2575 err = PySet_Add(set, item);
2576 Py_DECREF(item);
2577 }
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002578 STACKADJ(-oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002579 if (err != 0) {
2580 Py_DECREF(set);
2581 goto error;
2582 }
2583 PUSH(set);
2584 DISPATCH();
2585 }
2586
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002587 TARGET(BUILD_SET_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002588 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002589 PyObject *sum = PySet_New(NULL);
2590 if (sum == NULL)
2591 goto error;
2592
2593 for (i = oparg; i > 0; i--) {
2594 if (_PySet_Update(sum, PEEK(i)) < 0) {
2595 Py_DECREF(sum);
2596 goto error;
2597 }
2598 }
2599
2600 while (oparg--)
2601 Py_DECREF(POP());
2602 PUSH(sum);
2603 DISPATCH();
2604 }
2605
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002606 TARGET(BUILD_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002607 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002608 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2609 if (map == NULL)
2610 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002611 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002612 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002613 PyObject *key = PEEK(2*i);
2614 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002615 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002616 if (err != 0) {
2617 Py_DECREF(map);
2618 goto error;
2619 }
2620 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002621
2622 while (oparg--) {
2623 Py_DECREF(POP());
2624 Py_DECREF(POP());
2625 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002626 PUSH(map);
2627 DISPATCH();
2628 }
2629
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002630 TARGET(SETUP_ANNOTATIONS) {
2631 _Py_IDENTIFIER(__annotations__);
2632 int err;
2633 PyObject *ann_dict;
2634 if (f->f_locals == NULL) {
2635 PyErr_Format(PyExc_SystemError,
2636 "no locals found when setting up annotations");
2637 goto error;
2638 }
2639 /* check if __annotations__ in locals()... */
2640 if (PyDict_CheckExact(f->f_locals)) {
2641 ann_dict = _PyDict_GetItemId(f->f_locals,
2642 &PyId___annotations__);
2643 if (ann_dict == NULL) {
2644 /* ...if not, create a new one */
2645 ann_dict = PyDict_New();
2646 if (ann_dict == NULL) {
2647 goto error;
2648 }
2649 err = _PyDict_SetItemId(f->f_locals,
2650 &PyId___annotations__, ann_dict);
2651 Py_DECREF(ann_dict);
2652 if (err != 0) {
2653 goto error;
2654 }
2655 }
2656 }
2657 else {
2658 /* do the same if locals() is not a dict */
2659 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2660 if (ann_str == NULL) {
2661 break;
2662 }
2663 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2664 if (ann_dict == NULL) {
2665 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2666 goto error;
2667 }
2668 PyErr_Clear();
2669 ann_dict = PyDict_New();
2670 if (ann_dict == NULL) {
2671 goto error;
2672 }
2673 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2674 Py_DECREF(ann_dict);
2675 if (err != 0) {
2676 goto error;
2677 }
2678 }
2679 else {
2680 Py_DECREF(ann_dict);
2681 }
2682 }
2683 DISPATCH();
2684 }
2685
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002686 TARGET(BUILD_CONST_KEY_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002687 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002688 PyObject *map;
2689 PyObject *keys = TOP();
2690 if (!PyTuple_CheckExact(keys) ||
2691 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2692 PyErr_SetString(PyExc_SystemError,
2693 "bad BUILD_CONST_KEY_MAP keys argument");
2694 goto error;
2695 }
2696 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2697 if (map == NULL) {
2698 goto error;
2699 }
2700 for (i = oparg; i > 0; i--) {
2701 int err;
2702 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2703 PyObject *value = PEEK(i + 1);
2704 err = PyDict_SetItem(map, key, value);
2705 if (err != 0) {
2706 Py_DECREF(map);
2707 goto error;
2708 }
2709 }
2710
2711 Py_DECREF(POP());
2712 while (oparg--) {
2713 Py_DECREF(POP());
2714 }
2715 PUSH(map);
2716 DISPATCH();
2717 }
2718
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002719 TARGET(BUILD_MAP_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002720 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002721 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002722 if (sum == NULL)
2723 goto error;
2724
2725 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002726 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002727 if (PyDict_Update(sum, arg) < 0) {
2728 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2729 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002730 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002731 arg->ob_type->tp_name);
2732 }
2733 Py_DECREF(sum);
2734 goto error;
2735 }
2736 }
2737
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002738 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002739 Py_DECREF(POP());
2740 PUSH(sum);
2741 DISPATCH();
2742 }
2743
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002744 TARGET(BUILD_MAP_UNPACK_WITH_CALL) {
2745 Py_ssize_t i;
2746 PyObject *sum = PyDict_New();
2747 if (sum == NULL)
2748 goto error;
2749
2750 for (i = oparg; i > 0; i--) {
2751 PyObject *arg = PEEK(i);
2752 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
2753 PyObject *func = PEEK(2 + oparg);
2754 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2755 PyErr_Format(PyExc_TypeError,
2756 "%.200s%.200s argument after ** "
2757 "must be a mapping, not %.200s",
2758 PyEval_GetFuncName(func),
2759 PyEval_GetFuncDesc(func),
2760 arg->ob_type->tp_name);
2761 }
2762 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2763 PyObject *exc, *val, *tb;
2764 PyErr_Fetch(&exc, &val, &tb);
2765 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
2766 PyObject *key = PyTuple_GET_ITEM(val, 0);
2767 if (!PyUnicode_Check(key)) {
2768 PyErr_Format(PyExc_TypeError,
2769 "%.200s%.200s keywords must be strings",
2770 PyEval_GetFuncName(func),
2771 PyEval_GetFuncDesc(func));
2772 } else {
2773 PyErr_Format(PyExc_TypeError,
2774 "%.200s%.200s got multiple "
2775 "values for keyword argument '%U'",
2776 PyEval_GetFuncName(func),
2777 PyEval_GetFuncDesc(func),
2778 key);
2779 }
2780 Py_XDECREF(exc);
2781 Py_XDECREF(val);
2782 Py_XDECREF(tb);
2783 }
2784 else {
2785 PyErr_Restore(exc, val, tb);
2786 }
2787 }
2788 Py_DECREF(sum);
2789 goto error;
2790 }
2791 }
2792
2793 while (oparg--)
2794 Py_DECREF(POP());
2795 PUSH(sum);
2796 DISPATCH();
2797 }
2798
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002799 TARGET(MAP_ADD) {
2800 PyObject *key = TOP();
2801 PyObject *value = SECOND();
2802 PyObject *map;
2803 int err;
2804 STACKADJ(-2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002805 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002806 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002807 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002808 Py_DECREF(value);
2809 Py_DECREF(key);
2810 if (err != 0)
2811 goto error;
2812 PREDICT(JUMP_ABSOLUTE);
2813 DISPATCH();
2814 }
2815
2816 TARGET(LOAD_ATTR) {
2817 PyObject *name = GETITEM(names, oparg);
2818 PyObject *owner = TOP();
2819 PyObject *res = PyObject_GetAttr(owner, name);
2820 Py_DECREF(owner);
2821 SET_TOP(res);
2822 if (res == NULL)
2823 goto error;
2824 DISPATCH();
2825 }
2826
2827 TARGET(COMPARE_OP) {
2828 PyObject *right = POP();
2829 PyObject *left = TOP();
2830 PyObject *res = cmp_outcome(oparg, left, right);
2831 Py_DECREF(left);
2832 Py_DECREF(right);
2833 SET_TOP(res);
2834 if (res == NULL)
2835 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002836 PREDICT(POP_JUMP_IF_FALSE);
2837 PREDICT(POP_JUMP_IF_TRUE);
2838 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002839 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002840
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002841 TARGET(IMPORT_NAME) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002842 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002843 PyObject *fromlist = POP();
2844 PyObject *level = TOP();
2845 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002846 res = import_name(f, name, fromlist, level);
2847 Py_DECREF(level);
2848 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002849 SET_TOP(res);
2850 if (res == NULL)
2851 goto error;
2852 DISPATCH();
2853 }
2854
2855 TARGET(IMPORT_STAR) {
2856 PyObject *from = POP(), *locals;
2857 int err;
Victor Stinner41bb43a2013-10-29 01:19:37 +01002858 if (PyFrame_FastToLocalsWithError(f) < 0)
2859 goto error;
2860
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002861 locals = f->f_locals;
2862 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 PyErr_SetString(PyExc_SystemError,
2864 "no locals found during 'import *'");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002865 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002866 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002867 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002869 Py_DECREF(from);
2870 if (err != 0)
2871 goto error;
2872 DISPATCH();
2873 }
Guido van Rossum25831651993-05-19 14:50:45 +00002874
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002875 TARGET(IMPORT_FROM) {
2876 PyObject *name = GETITEM(names, oparg);
2877 PyObject *from = TOP();
2878 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002879 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002880 PUSH(res);
2881 if (res == NULL)
2882 goto error;
2883 DISPATCH();
2884 }
Thomas Wouters52152252000-08-17 22:55:00 +00002885
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002886 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002887 JUMPBY(oparg);
2888 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002889 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002890
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002891 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002892 TARGET(POP_JUMP_IF_FALSE) {
2893 PyObject *cond = POP();
2894 int err;
2895 if (cond == Py_True) {
2896 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002897 FAST_DISPATCH();
2898 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002899 if (cond == Py_False) {
2900 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 JUMPTO(oparg);
2902 FAST_DISPATCH();
2903 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002904 err = PyObject_IsTrue(cond);
2905 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002906 if (err > 0)
2907 err = 0;
2908 else if (err == 0)
2909 JUMPTO(oparg);
2910 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002911 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002912 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002913 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002914
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002915 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002916 TARGET(POP_JUMP_IF_TRUE) {
2917 PyObject *cond = POP();
2918 int err;
2919 if (cond == Py_False) {
2920 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002921 FAST_DISPATCH();
2922 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002923 if (cond == Py_True) {
2924 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 JUMPTO(oparg);
2926 FAST_DISPATCH();
2927 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002928 err = PyObject_IsTrue(cond);
2929 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002930 if (err > 0) {
2931 err = 0;
2932 JUMPTO(oparg);
2933 }
2934 else if (err == 0)
2935 ;
2936 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002937 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002938 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002939 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002940
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002941 TARGET(JUMP_IF_FALSE_OR_POP) {
2942 PyObject *cond = TOP();
2943 int err;
2944 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002945 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002946 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002947 FAST_DISPATCH();
2948 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002949 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002950 JUMPTO(oparg);
2951 FAST_DISPATCH();
2952 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002953 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002954 if (err > 0) {
2955 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002956 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002957 err = 0;
2958 }
2959 else if (err == 0)
2960 JUMPTO(oparg);
2961 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002962 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002963 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002964 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002965
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002966 TARGET(JUMP_IF_TRUE_OR_POP) {
2967 PyObject *cond = TOP();
2968 int err;
2969 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002970 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002971 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002972 FAST_DISPATCH();
2973 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002974 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002975 JUMPTO(oparg);
2976 FAST_DISPATCH();
2977 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002978 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002979 if (err > 0) {
2980 err = 0;
2981 JUMPTO(oparg);
2982 }
2983 else if (err == 0) {
2984 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002985 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002986 }
2987 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002988 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002989 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002990 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002991
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002992 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002993 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002994 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002995#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 /* Enabling this path speeds-up all while and for-loops by bypassing
2997 the per-loop checks for signals. By default, this should be turned-off
2998 because it prevents detection of a control-break in tight loops like
2999 "while 1: pass". Compile with this option turned-on when you need
3000 the speed-up and do not need break checking inside tight loops (ones
3001 that contain only instructions ending with FAST_DISPATCH).
3002 */
3003 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003004#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003005 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003006#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003007 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003008
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003009 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003010 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003011 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003012 PyObject *iter = PyObject_GetIter(iterable);
3013 Py_DECREF(iterable);
3014 SET_TOP(iter);
3015 if (iter == NULL)
3016 goto error;
3017 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003018 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003019 DISPATCH();
3020 }
3021
3022 TARGET(GET_YIELD_FROM_ITER) {
3023 /* before: [obj]; after [getiter(obj)] */
3024 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003025 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003026 if (PyCoro_CheckExact(iterable)) {
3027 /* `iterable` is a coroutine */
3028 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3029 /* and it is used in a 'yield from' expression of a
3030 regular generator. */
3031 Py_DECREF(iterable);
3032 SET_TOP(NULL);
3033 PyErr_SetString(PyExc_TypeError,
3034 "cannot 'yield from' a coroutine object "
3035 "in a non-coroutine generator");
3036 goto error;
3037 }
3038 }
3039 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003040 /* `iterable` is not a generator. */
3041 iter = PyObject_GetIter(iterable);
3042 Py_DECREF(iterable);
3043 SET_TOP(iter);
3044 if (iter == NULL)
3045 goto error;
3046 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003047 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003048 DISPATCH();
3049 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003050
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003051 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003052 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003053 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003054 PyObject *iter = TOP();
3055 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3056 if (next != NULL) {
3057 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003058 PREDICT(STORE_FAST);
3059 PREDICT(UNPACK_SEQUENCE);
3060 DISPATCH();
3061 }
3062 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003063 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
3064 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08003065 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003066 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003067 PyErr_Clear();
3068 }
3069 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003070 STACKADJ(-1);
3071 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003072 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003073 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003074 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003075 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003076
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003077 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003078 why = WHY_BREAK;
3079 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003080 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003081
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003082 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003083 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003084 if (retval == NULL)
3085 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003086 why = WHY_CONTINUE;
3087 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003088 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003089
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003090 TARGET(SETUP_LOOP)
3091 TARGET(SETUP_EXCEPT)
3092 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003093 /* NOTE: If you add any new block-setup opcodes that
3094 are not try/except/finally handlers, you may need
3095 to update the PyGen_NeedsFinalizing() function.
3096 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003098 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
3099 STACK_LEVEL());
3100 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003101 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003102
Yury Selivanov75445082015-05-11 22:57:16 -04003103 TARGET(BEFORE_ASYNC_WITH) {
3104 _Py_IDENTIFIER(__aexit__);
3105 _Py_IDENTIFIER(__aenter__);
3106
3107 PyObject *mgr = TOP();
3108 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3109 *enter;
3110 PyObject *res;
3111 if (exit == NULL)
3112 goto error;
3113 SET_TOP(exit);
3114 enter = special_lookup(mgr, &PyId___aenter__);
3115 Py_DECREF(mgr);
3116 if (enter == NULL)
3117 goto error;
3118 res = PyObject_CallFunctionObjArgs(enter, NULL);
3119 Py_DECREF(enter);
3120 if (res == NULL)
3121 goto error;
3122 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003123 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003124 DISPATCH();
3125 }
3126
3127 TARGET(SETUP_ASYNC_WITH) {
3128 PyObject *res = POP();
3129 /* Setup the finally block before pushing the result
3130 of __aenter__ on the stack. */
3131 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3132 STACK_LEVEL());
3133 PUSH(res);
3134 DISPATCH();
3135 }
3136
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003137 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003138 _Py_IDENTIFIER(__exit__);
3139 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003140 PyObject *mgr = TOP();
3141 PyObject *exit = special_lookup(mgr, &PyId___exit__), *enter;
3142 PyObject *res;
3143 if (exit == NULL)
3144 goto error;
3145 SET_TOP(exit);
3146 enter = special_lookup(mgr, &PyId___enter__);
3147 Py_DECREF(mgr);
3148 if (enter == NULL)
3149 goto error;
3150 res = PyObject_CallFunctionObjArgs(enter, NULL);
3151 Py_DECREF(enter);
3152 if (res == NULL)
3153 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154 /* Setup the finally block before pushing the result
3155 of __enter__ on the stack. */
3156 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3157 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003158
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003159 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003160 DISPATCH();
3161 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003162
Yury Selivanov75445082015-05-11 22:57:16 -04003163 TARGET(WITH_CLEANUP_START) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04003164 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003165 how/why we entered the finally clause:
3166 - TOP = None
3167 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
3168 - TOP = WHY_*; no retval below it
3169 - (TOP, SECOND, THIRD) = exc_info()
3170 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
3171 Below them is EXIT, the context.__exit__ bound method.
3172 In the last case, we must call
3173 EXIT(TOP, SECOND, THIRD)
3174 otherwise we must call
3175 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003176
Benjamin Peterson8f169482013-10-29 22:25:06 -04003177 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003178 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04003179 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003180 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003182 In addition, if the stack represents an exception,
3183 *and* the function call returns a 'true' value, we
3184 push WHY_SILENCED onto the stack. END_FINALLY will
3185 then not re-raise the exception. (But non-local
3186 gotos should still be resumed.)
3187 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003189 PyObject *exit_func;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003190 PyObject *exc = TOP(), *val = Py_None, *tb = Py_None, *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003191 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003192 (void)POP();
3193 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003194 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003195 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003196 else if (PyLong_Check(exc)) {
3197 STACKADJ(-1);
3198 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003199 case WHY_RETURN:
3200 case WHY_CONTINUE:
3201 /* Retval in TOP. */
3202 exit_func = SECOND();
3203 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003204 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003205 break;
3206 default:
3207 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003208 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003209 break;
3210 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003211 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003212 }
3213 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003214 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003215 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003216 val = SECOND();
3217 tb = THIRD();
3218 tp2 = FOURTH();
3219 exc2 = PEEK(5);
3220 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003221 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003222 SET_VALUE(7, tb2);
3223 SET_VALUE(6, exc2);
3224 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3226 SET_FOURTH(NULL);
3227 /* We just shifted the stack down, so we have
3228 to tell the except handler block that the
3229 values are lower than it expects. */
3230 block = &f->f_blockstack[f->f_iblock - 1];
3231 assert(block->b_type == EXCEPT_HANDLER);
3232 block->b_level--;
3233 }
3234 /* XXX Not the fastest way to call it... */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003235 res = PyObject_CallFunctionObjArgs(exit_func, exc, val, tb, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003236 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003237 if (res == NULL)
3238 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003239
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003240 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003241 PUSH(exc);
3242 PUSH(res);
3243 PREDICT(WITH_CLEANUP_FINISH);
3244 DISPATCH();
3245 }
3246
3247 PREDICTED(WITH_CLEANUP_FINISH);
3248 TARGET(WITH_CLEANUP_FINISH) {
3249 PyObject *res = POP();
3250 PyObject *exc = POP();
3251 int err;
3252
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003253 if (exc != Py_None)
3254 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003255 else
3256 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003257
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003258 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003259 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003261 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003262 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003263 else if (err > 0) {
3264 err = 0;
3265 /* There was an exception and a True return */
3266 PUSH(PyLong_FromLong((long) WHY_SILENCED));
3267 }
3268 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003269 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003270 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003271
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003272 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003273 TARGET(CALL_FUNCTION) {
3274 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003275 PCALL(PCALL_ALL);
3276 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003277 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003278 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003279 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003280 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003281 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003282 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003283 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003284 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003285
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003286 TARGET(CALL_FUNCTION_KW) {
3287 PyObject **sp, *res, *names;
3288
3289 names = POP();
3290 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003291 PCALL(PCALL_ALL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003292 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003293 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003294 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003295 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003296 Py_DECREF(names);
3297
3298 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003299 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003300 }
3301 DISPATCH();
3302 }
3303
3304 TARGET(CALL_FUNCTION_EX) {
3305 PyObject *func, *callargs, *kwargs = NULL, *result;
3306 PCALL(PCALL_ALL);
3307 if (oparg & 0x01) {
3308 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003309 if (!PyDict_CheckExact(kwargs)) {
3310 PyObject *d = PyDict_New();
3311 if (d == NULL)
3312 goto error;
3313 if (PyDict_Update(d, kwargs) != 0) {
3314 Py_DECREF(d);
3315 /* PyDict_Update raises attribute
3316 * error (percolated from an attempt
3317 * to get 'keys' attribute) instead of
3318 * a type error if its second argument
3319 * is not a mapping.
3320 */
3321 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3322 func = SECOND();
3323 PyErr_Format(PyExc_TypeError,
3324 "%.200s%.200s argument after ** "
3325 "must be a mapping, not %.200s",
3326 PyEval_GetFuncName(func),
3327 PyEval_GetFuncDesc(func),
3328 kwargs->ob_type->tp_name);
3329 }
Victor Stinnereece2222016-09-12 11:16:37 +02003330 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003331 goto error;
3332 }
3333 Py_DECREF(kwargs);
3334 kwargs = d;
3335 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003336 assert(PyDict_CheckExact(kwargs));
3337 }
3338 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003339 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003340 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003341 if (Py_TYPE(callargs)->tp_iter == NULL &&
3342 !PySequence_Check(callargs)) {
3343 PyErr_Format(PyExc_TypeError,
3344 "%.200s%.200s argument after * "
3345 "must be an iterable, not %.200s",
3346 PyEval_GetFuncName(func),
3347 PyEval_GetFuncDesc(func),
3348 callargs->ob_type->tp_name);
Victor Stinnereece2222016-09-12 11:16:37 +02003349 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003350 goto error;
3351 }
3352 Py_SETREF(callargs, PySequence_Tuple(callargs));
3353 if (callargs == NULL) {
3354 goto error;
3355 }
3356 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003357 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003358
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003359 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003360 Py_DECREF(func);
3361 Py_DECREF(callargs);
3362 Py_XDECREF(kwargs);
3363
3364 SET_TOP(result);
3365 if (result == NULL) {
3366 goto error;
3367 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003368 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003369 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003370
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003371 TARGET(MAKE_FUNCTION) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003372 PyObject *qualname = POP();
3373 PyObject *codeobj = POP();
3374 PyFunctionObject *func = (PyFunctionObject *)
3375 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003376
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003377 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003378 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003379 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003380 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003381 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003382
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003383 if (oparg & 0x08) {
3384 assert(PyTuple_CheckExact(TOP()));
3385 func ->func_closure = POP();
3386 }
3387 if (oparg & 0x04) {
3388 assert(PyDict_CheckExact(TOP()));
3389 func->func_annotations = POP();
3390 }
3391 if (oparg & 0x02) {
3392 assert(PyDict_CheckExact(TOP()));
3393 func->func_kwdefaults = POP();
3394 }
3395 if (oparg & 0x01) {
3396 assert(PyTuple_CheckExact(TOP()));
3397 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003398 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003399
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003400 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003401 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003402 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003403
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003404 TARGET(BUILD_SLICE) {
3405 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003406 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003407 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003408 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003409 step = NULL;
3410 stop = POP();
3411 start = TOP();
3412 slice = PySlice_New(start, stop, step);
3413 Py_DECREF(start);
3414 Py_DECREF(stop);
3415 Py_XDECREF(step);
3416 SET_TOP(slice);
3417 if (slice == NULL)
3418 goto error;
3419 DISPATCH();
3420 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003421
Eric V. Smitha78c7952015-11-03 12:45:05 -05003422 TARGET(FORMAT_VALUE) {
3423 /* Handles f-string value formatting. */
3424 PyObject *result;
3425 PyObject *fmt_spec;
3426 PyObject *value;
3427 PyObject *(*conv_fn)(PyObject *);
3428 int which_conversion = oparg & FVC_MASK;
3429 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3430
3431 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003432 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003433
3434 /* See if any conversion is specified. */
3435 switch (which_conversion) {
3436 case FVC_STR: conv_fn = PyObject_Str; break;
3437 case FVC_REPR: conv_fn = PyObject_Repr; break;
3438 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3439
3440 /* Must be 0 (meaning no conversion), since only four
3441 values are allowed by (oparg & FVC_MASK). */
3442 default: conv_fn = NULL; break;
3443 }
3444
3445 /* If there's a conversion function, call it and replace
3446 value with that result. Otherwise, just use value,
3447 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003448 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003449 result = conv_fn(value);
3450 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003451 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003452 Py_XDECREF(fmt_spec);
3453 goto error;
3454 }
3455 value = result;
3456 }
3457
3458 /* If value is a unicode object, and there's no fmt_spec,
3459 then we know the result of format(value) is value
3460 itself. In that case, skip calling format(). I plan to
3461 move this optimization in to PyObject_Format()
3462 itself. */
3463 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3464 /* Do nothing, just transfer ownership to result. */
3465 result = value;
3466 } else {
3467 /* Actually call format(). */
3468 result = PyObject_Format(value, fmt_spec);
3469 Py_DECREF(value);
3470 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003471 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003472 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003473 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003474 }
3475
Eric V. Smith135d5f42016-02-05 18:23:08 -05003476 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003477 DISPATCH();
3478 }
3479
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003480 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003481 int oldoparg = oparg;
3482 NEXTOPARG();
3483 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003484 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003485 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003486
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003487
Antoine Pitrou042b1282010-08-13 21:15:58 +00003488#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003489 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003490#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003491 default:
3492 fprintf(stderr,
3493 "XXX lineno: %d, opcode: %d\n",
3494 PyFrame_GetLineNumber(f),
3495 opcode);
3496 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003497 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003498
3499#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003500 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003501#endif
3502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003503 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003504
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003505 /* This should never be reached. Every opcode should end with DISPATCH()
3506 or goto error. */
3507 assert(0);
Guido van Rossumac7be682001-01-17 15:42:30 +00003508
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003509error:
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003510
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003511 assert(why == WHY_NOT);
3512 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003513
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003514 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003515#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003516 if (!PyErr_Occurred())
3517 PyErr_SetString(PyExc_SystemError,
3518 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003519#else
3520 assert(PyErr_Occurred());
3521#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003522
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003523 /* Log traceback info. */
3524 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003525
Benjamin Peterson51f46162013-01-23 08:38:47 -05003526 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003527 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3528 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003529
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003530fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003531 assert(why != WHY_NOT);
3532
3533 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003534 while (why != WHY_NOT && f->f_iblock > 0) {
3535 /* Peek at the current block. */
3536 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003538 assert(why != WHY_YIELD);
3539 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3540 why = WHY_NOT;
3541 JUMPTO(PyLong_AS_LONG(retval));
3542 Py_DECREF(retval);
3543 break;
3544 }
3545 /* Now we have to pop the block. */
3546 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003548 if (b->b_type == EXCEPT_HANDLER) {
3549 UNWIND_EXCEPT_HANDLER(b);
3550 continue;
3551 }
3552 UNWIND_BLOCK(b);
3553 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3554 why = WHY_NOT;
3555 JUMPTO(b->b_handler);
3556 break;
3557 }
3558 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3559 || b->b_type == SETUP_FINALLY)) {
3560 PyObject *exc, *val, *tb;
3561 int handler = b->b_handler;
3562 /* Beware, this invalidates all b->b_* fields */
3563 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3564 PUSH(tstate->exc_traceback);
3565 PUSH(tstate->exc_value);
3566 if (tstate->exc_type != NULL) {
3567 PUSH(tstate->exc_type);
3568 }
3569 else {
3570 Py_INCREF(Py_None);
3571 PUSH(Py_None);
3572 }
3573 PyErr_Fetch(&exc, &val, &tb);
3574 /* Make the raw exception data
3575 available to the handler,
3576 so a program can emulate the
3577 Python main loop. */
3578 PyErr_NormalizeException(
3579 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003580 if (tb != NULL)
3581 PyException_SetTraceback(val, tb);
3582 else
3583 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003584 Py_INCREF(exc);
3585 tstate->exc_type = exc;
3586 Py_INCREF(val);
3587 tstate->exc_value = val;
3588 tstate->exc_traceback = tb;
3589 if (tb == NULL)
3590 tb = Py_None;
3591 Py_INCREF(tb);
3592 PUSH(tb);
3593 PUSH(val);
3594 PUSH(exc);
3595 why = WHY_NOT;
3596 JUMPTO(handler);
3597 break;
3598 }
3599 if (b->b_type == SETUP_FINALLY) {
3600 if (why & (WHY_RETURN | WHY_CONTINUE))
3601 PUSH(retval);
3602 PUSH(PyLong_FromLong((long)why));
3603 why = WHY_NOT;
3604 JUMPTO(b->b_handler);
3605 break;
3606 }
3607 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003609 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003611 if (why != WHY_NOT)
3612 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00003613
Victor Stinnerace47d72013-07-18 01:41:08 +02003614 assert(!PyErr_Occurred());
3615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003616 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003618 assert(why != WHY_YIELD);
3619 /* Pop remaining stack entries. */
3620 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003621 PyObject *o = POP();
3622 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003623 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003625 if (why != WHY_RETURN)
3626 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003627
Victor Stinner4a7cc882015-03-06 23:35:27 +01003628 assert((retval != NULL) ^ (PyErr_Occurred() != NULL));
Victor Stinnerace47d72013-07-18 01:41:08 +02003629
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003630fast_yield:
Yury Selivanoveb636452016-09-08 22:01:51 -07003631 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01003632
Benjamin Petersonac913412011-07-03 16:25:11 -05003633 /* The purpose of this block is to put aside the generator's exception
3634 state and restore that of the calling frame. If the current
3635 exception state is from the caller, we clear the exception values
3636 on the generator frame, so they are not swapped back in latter. The
3637 origin of the current exception state is determined by checking for
3638 except handler blocks, which we must be in iff a new exception
3639 state came into existence in this frame. (An uncaught exception
3640 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3641 int i;
Victor Stinner74319ae2016-08-25 00:04:09 +02003642 for (i = 0; i < f->f_iblock; i++) {
3643 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER) {
Benjamin Petersonac913412011-07-03 16:25:11 -05003644 break;
Victor Stinner74319ae2016-08-25 00:04:09 +02003645 }
3646 }
Benjamin Petersonac913412011-07-03 16:25:11 -05003647 if (i == f->f_iblock)
3648 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003649 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003650 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003651 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003652 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003654 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003655 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003656 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003657 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3658 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003659 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003660 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003661 why = WHY_EXCEPTION;
3662 }
3663 }
3664 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003665 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3666 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003667 PyTrace_RETURN, NULL);
3668 }
3669 }
3670 if (tstate->c_profilefunc) {
3671 if (why == WHY_EXCEPTION)
3672 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003673 tstate->c_profileobj,
3674 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003675 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003676 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3677 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003678 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003679 Py_CLEAR(retval);
Brett Cannonb94767f2011-02-22 20:15:44 +00003680 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003681 }
3682 }
3683 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003685 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003686exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003687 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3688 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003689 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003690 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003691 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003692
Victor Stinnerefde1462015-03-21 15:04:43 +01003693 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003694}
3695
Benjamin Petersonb204a422011-06-05 22:04:07 -05003696static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003697format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3698{
3699 int err;
3700 Py_ssize_t len = PyList_GET_SIZE(names);
3701 PyObject *name_str, *comma, *tail, *tmp;
3702
3703 assert(PyList_CheckExact(names));
3704 assert(len >= 1);
3705 /* Deal with the joys of natural language. */
3706 switch (len) {
3707 case 1:
3708 name_str = PyList_GET_ITEM(names, 0);
3709 Py_INCREF(name_str);
3710 break;
3711 case 2:
3712 name_str = PyUnicode_FromFormat("%U and %U",
3713 PyList_GET_ITEM(names, len - 2),
3714 PyList_GET_ITEM(names, len - 1));
3715 break;
3716 default:
3717 tail = PyUnicode_FromFormat(", %U, and %U",
3718 PyList_GET_ITEM(names, len - 2),
3719 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003720 if (tail == NULL)
3721 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003722 /* Chop off the last two objects in the list. This shouldn't actually
3723 fail, but we can't be too careful. */
3724 err = PyList_SetSlice(names, len - 2, len, NULL);
3725 if (err == -1) {
3726 Py_DECREF(tail);
3727 return;
3728 }
3729 /* Stitch everything up into a nice comma-separated list. */
3730 comma = PyUnicode_FromString(", ");
3731 if (comma == NULL) {
3732 Py_DECREF(tail);
3733 return;
3734 }
3735 tmp = PyUnicode_Join(comma, names);
3736 Py_DECREF(comma);
3737 if (tmp == NULL) {
3738 Py_DECREF(tail);
3739 return;
3740 }
3741 name_str = PyUnicode_Concat(tmp, tail);
3742 Py_DECREF(tmp);
3743 Py_DECREF(tail);
3744 break;
3745 }
3746 if (name_str == NULL)
3747 return;
3748 PyErr_Format(PyExc_TypeError,
3749 "%U() missing %i required %s argument%s: %U",
3750 co->co_name,
3751 len,
3752 kind,
3753 len == 1 ? "" : "s",
3754 name_str);
3755 Py_DECREF(name_str);
3756}
3757
3758static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003759missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003760 PyObject **fastlocals)
3761{
Victor Stinner74319ae2016-08-25 00:04:09 +02003762 Py_ssize_t i, j = 0;
3763 Py_ssize_t start, end;
3764 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003765 const char *kind = positional ? "positional" : "keyword-only";
3766 PyObject *missing_names;
3767
3768 /* Compute the names of the arguments that are missing. */
3769 missing_names = PyList_New(missing);
3770 if (missing_names == NULL)
3771 return;
3772 if (positional) {
3773 start = 0;
3774 end = co->co_argcount - defcount;
3775 }
3776 else {
3777 start = co->co_argcount;
3778 end = start + co->co_kwonlyargcount;
3779 }
3780 for (i = start; i < end; i++) {
3781 if (GETLOCAL(i) == NULL) {
3782 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3783 PyObject *name = PyObject_Repr(raw);
3784 if (name == NULL) {
3785 Py_DECREF(missing_names);
3786 return;
3787 }
3788 PyList_SET_ITEM(missing_names, j++, name);
3789 }
3790 }
3791 assert(j == missing);
3792 format_missing(kind, co, missing_names);
3793 Py_DECREF(missing_names);
3794}
3795
3796static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003797too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3798 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003799{
3800 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003801 Py_ssize_t kwonly_given = 0;
3802 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003803 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003804 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003805
Benjamin Petersone109c702011-06-24 09:37:26 -05003806 assert((co->co_flags & CO_VARARGS) == 0);
3807 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003808 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3809 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003810 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003811 }
3812 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003813 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003814 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003815 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003816 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003817 }
3818 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003819 plural = (co_argcount != 1);
3820 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003821 }
3822 if (sig == NULL)
3823 return;
3824 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003825 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3826 kwonly_sig = PyUnicode_FromFormat(format,
3827 given != 1 ? "s" : "",
3828 kwonly_given,
3829 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003830 if (kwonly_sig == NULL) {
3831 Py_DECREF(sig);
3832 return;
3833 }
3834 }
3835 else {
3836 /* This will not fail. */
3837 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003838 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003839 }
3840 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003841 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003842 co->co_name,
3843 sig,
3844 plural ? "s" : "",
3845 given,
3846 kwonly_sig,
3847 given == 1 && !kwonly_given ? "was" : "were");
3848 Py_DECREF(sig);
3849 Py_DECREF(kwonly_sig);
3850}
3851
Guido van Rossumc2e20742006-02-27 22:32:47 +00003852/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003853 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003854 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003855
Victor Stinner40ee3012014-06-16 15:59:28 +02003856static PyObject *
3857_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Victor Stinner74319ae2016-08-25 00:04:09 +02003858 PyObject **args, Py_ssize_t argcount,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003859 PyObject **kwnames, PyObject **kwargs,
3860 Py_ssize_t kwcount, int kwstep,
Victor Stinner74319ae2016-08-25 00:04:09 +02003861 PyObject **defs, Py_ssize_t defcount,
3862 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003863 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003864{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003865 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003866 PyFrameObject *f;
3867 PyObject *retval = NULL;
3868 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003869 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003870 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003871 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3872 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003873 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003875 if (globals == NULL) {
3876 PyErr_SetString(PyExc_SystemError,
3877 "PyEval_EvalCodeEx: NULL globals");
3878 return NULL;
3879 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003880
Victor Stinnerc7020012016-08-16 23:40:29 +02003881 /* Create the frame */
3882 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003883 assert(tstate != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003884 f = PyFrame_New(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003885 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003886 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003887 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003888 fastlocals = f->f_localsplus;
3889 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003890
Victor Stinnerc7020012016-08-16 23:40:29 +02003891 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003892 if (co->co_flags & CO_VARKEYWORDS) {
3893 kwdict = PyDict_New();
3894 if (kwdict == NULL)
3895 goto fail;
3896 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003897 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003898 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003899 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003900 SETLOCAL(i, kwdict);
3901 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003902 else {
3903 kwdict = NULL;
3904 }
3905
3906 /* Copy positional arguments into local variables */
3907 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003908 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003909 }
3910 else {
3911 n = argcount;
3912 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003913 for (i = 0; i < n; i++) {
3914 x = args[i];
3915 Py_INCREF(x);
3916 SETLOCAL(i, x);
3917 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003918
3919 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003920 if (co->co_flags & CO_VARARGS) {
3921 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003922 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003923 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003924 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003925 SETLOCAL(total_args, u);
3926 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003927 x = args[i];
3928 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003929 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003930 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003931 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003932
Serhiy Storchakab7281052016-09-12 00:52:40 +03003933 /* Handle keyword arguments passed as two strided arrays */
3934 kwcount *= kwstep;
3935 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003936 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003937 PyObject *keyword = kwnames[i];
3938 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003939 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003940
Benjamin Petersonb204a422011-06-05 22:04:07 -05003941 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3942 PyErr_Format(PyExc_TypeError,
3943 "%U() keywords must be strings",
3944 co->co_name);
3945 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003946 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003947
Benjamin Petersonb204a422011-06-05 22:04:07 -05003948 /* Speed hack: do raw pointer compares. As names are
3949 normally interned this should almost always hit. */
3950 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3951 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003952 PyObject *name = co_varnames[j];
3953 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003954 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003955 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003956 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003957
Benjamin Petersonb204a422011-06-05 22:04:07 -05003958 /* Slow fallback, just in case */
3959 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003960 PyObject *name = co_varnames[j];
3961 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3962 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003963 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003964 }
3965 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003966 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003967 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003968 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003969
Benjamin Petersonb204a422011-06-05 22:04:07 -05003970 if (j >= total_args && kwdict == NULL) {
3971 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003972 "%U() got an unexpected keyword argument '%S'",
3973 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003974 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003975 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003976
Christian Heimes0bd447f2013-07-20 14:48:10 +02003977 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3978 goto fail;
3979 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003980 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003981
Benjamin Petersonb204a422011-06-05 22:04:07 -05003982 kw_found:
3983 if (GETLOCAL(j) != NULL) {
3984 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003985 "%U() got multiple values for argument '%S'",
3986 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003987 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003988 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003989 Py_INCREF(value);
3990 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003991 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003992
3993 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003994 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003995 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003996 goto fail;
3997 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003998
3999 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004000 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02004001 Py_ssize_t m = co->co_argcount - defcount;
4002 Py_ssize_t missing = 0;
4003 for (i = argcount; i < m; i++) {
4004 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004005 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004006 }
4007 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004008 if (missing) {
4009 missing_arguments(co, missing, defcount, fastlocals);
4010 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004011 }
4012 if (n > m)
4013 i = n - m;
4014 else
4015 i = 0;
4016 for (; i < defcount; i++) {
4017 if (GETLOCAL(m+i) == NULL) {
4018 PyObject *def = defs[i];
4019 Py_INCREF(def);
4020 SETLOCAL(m+i, def);
4021 }
4022 }
4023 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004024
4025 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004026 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004027 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004028 for (i = co->co_argcount; i < total_args; i++) {
4029 PyObject *name;
4030 if (GETLOCAL(i) != NULL)
4031 continue;
4032 name = PyTuple_GET_ITEM(co->co_varnames, i);
4033 if (kwdefs != NULL) {
4034 PyObject *def = PyDict_GetItem(kwdefs, name);
4035 if (def) {
4036 Py_INCREF(def);
4037 SETLOCAL(i, def);
4038 continue;
4039 }
4040 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004041 missing++;
4042 }
4043 if (missing) {
4044 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004045 goto fail;
4046 }
4047 }
4048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004049 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004050 vars into frame. */
4051 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004052 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05004053 int arg;
4054 /* Possibly account for the cell variable being an argument. */
4055 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004056 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004057 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004058 /* Clear the local copy. */
4059 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004060 }
4061 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004062 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004063 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004064 if (c == NULL)
4065 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004066 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004067 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004068
4069 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004070 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4071 PyObject *o = PyTuple_GET_ITEM(closure, i);
4072 Py_INCREF(o);
4073 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004074 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004075
Yury Selivanoveb636452016-09-08 22:01:51 -07004076 /* Handle generator/coroutine/asynchronous generator */
4077 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004078 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04004079 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004080 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004081
4082 if (is_coro && tstate->in_coroutine_wrapper) {
4083 assert(coro_wrapper != NULL);
4084 PyErr_Format(PyExc_RuntimeError,
4085 "coroutine wrapper %.200R attempted "
4086 "to recursively wrap %.200R",
4087 coro_wrapper,
4088 co);
4089 goto fail;
4090 }
Yury Selivanov75445082015-05-11 22:57:16 -04004091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004092 /* Don't need to keep the reference to f_back, it will be set
4093 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004094 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004096 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004098 /* Create a new generator that owns the ready to run frame
4099 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004100 if (is_coro) {
4101 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004102 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4103 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004104 } else {
4105 gen = PyGen_NewWithQualName(f, name, qualname);
4106 }
Yury Selivanov75445082015-05-11 22:57:16 -04004107 if (gen == NULL)
4108 return NULL;
4109
Yury Selivanov94c22632015-06-04 10:16:51 -04004110 if (is_coro && coro_wrapper != NULL) {
4111 PyObject *wrapped;
4112 tstate->in_coroutine_wrapper = 1;
4113 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4114 tstate->in_coroutine_wrapper = 0;
4115 return wrapped;
4116 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004117
Yury Selivanov75445082015-05-11 22:57:16 -04004118 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004119 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004121 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004122
Thomas Woutersce272b62007-09-19 21:19:28 +00004123fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004125 /* decref'ing the frame can cause __del__ methods to get invoked,
4126 which can call back into Python. While we're done with the
4127 current Python frame (f), the associated C stack is still in use,
4128 so recursion_depth must be boosted for the duration.
4129 */
4130 assert(tstate != NULL);
4131 ++tstate->recursion_depth;
4132 Py_DECREF(f);
4133 --tstate->recursion_depth;
4134 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004135}
4136
Victor Stinner40ee3012014-06-16 15:59:28 +02004137PyObject *
4138PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
4139 PyObject **args, int argcount, PyObject **kws, int kwcount,
4140 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
4141{
4142 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004143 args, argcount,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004144 kws, kws + 1, kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004145 defs, defcount,
4146 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004147 NULL, NULL);
4148}
Tim Peters5ca576e2001-06-18 22:08:13 +00004149
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004150static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004151special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004152{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004153 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004154 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004155 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004156 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004157 return NULL;
4158 }
4159 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004160}
4161
4162
Benjamin Peterson87880242011-07-03 16:48:31 -05004163/* These 3 functions deal with the exception state of generators. */
4164
4165static void
4166save_exc_state(PyThreadState *tstate, PyFrameObject *f)
4167{
4168 PyObject *type, *value, *traceback;
4169 Py_XINCREF(tstate->exc_type);
4170 Py_XINCREF(tstate->exc_value);
4171 Py_XINCREF(tstate->exc_traceback);
4172 type = f->f_exc_type;
4173 value = f->f_exc_value;
4174 traceback = f->f_exc_traceback;
4175 f->f_exc_type = tstate->exc_type;
4176 f->f_exc_value = tstate->exc_value;
4177 f->f_exc_traceback = tstate->exc_traceback;
4178 Py_XDECREF(type);
4179 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004180 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05004181}
4182
4183static void
4184swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
4185{
4186 PyObject *tmp;
4187 tmp = tstate->exc_type;
4188 tstate->exc_type = f->f_exc_type;
4189 f->f_exc_type = tmp;
4190 tmp = tstate->exc_value;
4191 tstate->exc_value = f->f_exc_value;
4192 f->f_exc_value = tmp;
4193 tmp = tstate->exc_traceback;
4194 tstate->exc_traceback = f->f_exc_traceback;
4195 f->f_exc_traceback = tmp;
4196}
4197
4198static void
4199restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
4200{
4201 PyObject *type, *value, *tb;
4202 type = tstate->exc_type;
4203 value = tstate->exc_value;
4204 tb = tstate->exc_traceback;
4205 tstate->exc_type = f->f_exc_type;
4206 tstate->exc_value = f->f_exc_value;
4207 tstate->exc_traceback = f->f_exc_traceback;
4208 f->f_exc_type = NULL;
4209 f->f_exc_value = NULL;
4210 f->f_exc_traceback = NULL;
4211 Py_XDECREF(type);
4212 Py_XDECREF(value);
4213 Py_XDECREF(tb);
4214}
4215
4216
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004217/* Logic for the raise statement (too complicated for inlining).
4218 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004219static int
Collin Winter828f04a2007-08-31 00:04:24 +00004220do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004221{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004222 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004224 if (exc == NULL) {
4225 /* Reraise */
4226 PyThreadState *tstate = PyThreadState_GET();
4227 PyObject *tb;
4228 type = tstate->exc_type;
4229 value = tstate->exc_value;
4230 tb = tstate->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004231 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004232 PyErr_SetString(PyExc_RuntimeError,
4233 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004234 return 0;
4235 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004236 Py_XINCREF(type);
4237 Py_XINCREF(value);
4238 Py_XINCREF(tb);
4239 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004240 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004241 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004243 /* We support the following forms of raise:
4244 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004245 raise <instance>
4246 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004248 if (PyExceptionClass_Check(exc)) {
4249 type = exc;
4250 value = PyObject_CallObject(exc, NULL);
4251 if (value == NULL)
4252 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004253 if (!PyExceptionInstance_Check(value)) {
4254 PyErr_Format(PyExc_TypeError,
4255 "calling %R should have returned an instance of "
4256 "BaseException, not %R",
4257 type, Py_TYPE(value));
4258 goto raise_error;
4259 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004260 }
4261 else if (PyExceptionInstance_Check(exc)) {
4262 value = exc;
4263 type = PyExceptionInstance_Class(exc);
4264 Py_INCREF(type);
4265 }
4266 else {
4267 /* Not something you can raise. You get an exception
4268 anyway, just not what you specified :-) */
4269 Py_DECREF(exc);
4270 PyErr_SetString(PyExc_TypeError,
4271 "exceptions must derive from BaseException");
4272 goto raise_error;
4273 }
Collin Winter828f04a2007-08-31 00:04:24 +00004274
Serhiy Storchakac0191582016-09-27 11:37:10 +03004275 assert(type != NULL);
4276 assert(value != NULL);
4277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004278 if (cause) {
4279 PyObject *fixed_cause;
4280 if (PyExceptionClass_Check(cause)) {
4281 fixed_cause = PyObject_CallObject(cause, NULL);
4282 if (fixed_cause == NULL)
4283 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004284 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004285 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004286 else if (PyExceptionInstance_Check(cause)) {
4287 fixed_cause = cause;
4288 }
4289 else if (cause == Py_None) {
4290 Py_DECREF(cause);
4291 fixed_cause = NULL;
4292 }
4293 else {
4294 PyErr_SetString(PyExc_TypeError,
4295 "exception causes must derive from "
4296 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004297 goto raise_error;
4298 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004299 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004300 }
Collin Winter828f04a2007-08-31 00:04:24 +00004301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004302 PyErr_SetObject(type, value);
4303 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004304 Py_DECREF(value);
4305 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004306 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004307
4308raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004309 Py_XDECREF(value);
4310 Py_XDECREF(type);
4311 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004312 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004313}
4314
Tim Petersd6d010b2001-06-21 02:49:55 +00004315/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004316 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004317
Guido van Rossum0368b722007-05-11 16:50:42 +00004318 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4319 with a variable target.
4320*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004321
Barry Warsawe42b18f1997-08-25 22:13:04 +00004322static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004323unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004324{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004325 int i = 0, j = 0;
4326 Py_ssize_t ll = 0;
4327 PyObject *it; /* iter(v) */
4328 PyObject *w;
4329 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004331 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004333 it = PyObject_GetIter(v);
4334 if (it == NULL)
4335 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00004336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004337 for (; i < argcnt; i++) {
4338 w = PyIter_Next(it);
4339 if (w == NULL) {
4340 /* Iterator done, via error or exhaustion. */
4341 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004342 if (argcntafter == -1) {
4343 PyErr_Format(PyExc_ValueError,
4344 "not enough values to unpack (expected %d, got %d)",
4345 argcnt, i);
4346 }
4347 else {
4348 PyErr_Format(PyExc_ValueError,
4349 "not enough values to unpack "
4350 "(expected at least %d, got %d)",
4351 argcnt + argcntafter, i);
4352 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004353 }
4354 goto Error;
4355 }
4356 *--sp = w;
4357 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004359 if (argcntafter == -1) {
4360 /* We better have exhausted the iterator now. */
4361 w = PyIter_Next(it);
4362 if (w == NULL) {
4363 if (PyErr_Occurred())
4364 goto Error;
4365 Py_DECREF(it);
4366 return 1;
4367 }
4368 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004369 PyErr_Format(PyExc_ValueError,
4370 "too many values to unpack (expected %d)",
4371 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004372 goto Error;
4373 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004375 l = PySequence_List(it);
4376 if (l == NULL)
4377 goto Error;
4378 *--sp = l;
4379 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004381 ll = PyList_GET_SIZE(l);
4382 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004383 PyErr_Format(PyExc_ValueError,
4384 "not enough values to unpack (expected at least %d, got %zd)",
4385 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004386 goto Error;
4387 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004389 /* Pop the "after-variable" args off the list. */
4390 for (j = argcntafter; j > 0; j--, i++) {
4391 *--sp = PyList_GET_ITEM(l, ll - j);
4392 }
4393 /* Resize the list. */
4394 Py_SIZE(l) = ll - argcntafter;
4395 Py_DECREF(it);
4396 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004397
Tim Petersd6d010b2001-06-21 02:49:55 +00004398Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004399 for (; i > 0; i--, sp++)
4400 Py_DECREF(*sp);
4401 Py_XDECREF(it);
4402 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004403}
4404
4405
Guido van Rossum96a42c81992-01-12 02:29:51 +00004406#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004407static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004408prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004409{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004410 printf("%s ", str);
4411 if (PyObject_Print(v, stdout, 0) != 0)
4412 PyErr_Clear(); /* Don't know what else to do */
4413 printf("\n");
4414 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004415}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004416#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004417
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004418static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004419call_exc_trace(Py_tracefunc func, PyObject *self,
4420 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004421{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004422 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004423 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004424 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004425 if (value == NULL) {
4426 value = Py_None;
4427 Py_INCREF(value);
4428 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004429 PyErr_NormalizeException(&type, &value, &orig_traceback);
4430 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004431 arg = PyTuple_Pack(3, type, value, traceback);
4432 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004433 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004434 return;
4435 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004436 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004437 Py_DECREF(arg);
4438 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004439 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004440 else {
4441 Py_XDECREF(type);
4442 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004443 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004444 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004445}
4446
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004447static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004448call_trace_protected(Py_tracefunc func, PyObject *obj,
4449 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004450 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004451{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004452 PyObject *type, *value, *traceback;
4453 int err;
4454 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004455 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004456 if (err == 0)
4457 {
4458 PyErr_Restore(type, value, traceback);
4459 return 0;
4460 }
4461 else {
4462 Py_XDECREF(type);
4463 Py_XDECREF(value);
4464 Py_XDECREF(traceback);
4465 return -1;
4466 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004467}
4468
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004469static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004470call_trace(Py_tracefunc func, PyObject *obj,
4471 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004472 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004473{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004474 int result;
4475 if (tstate->tracing)
4476 return 0;
4477 tstate->tracing++;
4478 tstate->use_tracing = 0;
4479 result = func(obj, frame, what, arg);
4480 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4481 || (tstate->c_profilefunc != NULL));
4482 tstate->tracing--;
4483 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004484}
4485
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004486PyObject *
4487_PyEval_CallTracing(PyObject *func, PyObject *args)
4488{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004489 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004490 int save_tracing = tstate->tracing;
4491 int save_use_tracing = tstate->use_tracing;
4492 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004494 tstate->tracing = 0;
4495 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4496 || (tstate->c_profilefunc != NULL));
4497 result = PyObject_Call(func, args, NULL);
4498 tstate->tracing = save_tracing;
4499 tstate->use_tracing = save_use_tracing;
4500 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004501}
4502
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004503/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004504static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004505maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004506 PyThreadState *tstate, PyFrameObject *frame,
4507 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004508{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004509 int result = 0;
4510 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004512 /* If the last instruction executed isn't in the current
4513 instruction window, reset the window.
4514 */
4515 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4516 PyAddrPair bounds;
4517 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4518 &bounds);
4519 *instr_lb = bounds.ap_lower;
4520 *instr_ub = bounds.ap_upper;
4521 }
4522 /* If the last instruction falls at the start of a line or if
4523 it represents a jump backwards, update the frame's line
4524 number and call the trace function. */
4525 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
4526 frame->f_lineno = line;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004527 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004528 }
4529 *instr_prev = frame->f_lasti;
4530 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004531}
4532
Fred Drake5755ce62001-06-27 19:19:46 +00004533void
4534PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004535{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004536 PyThreadState *tstate = PyThreadState_GET();
4537 PyObject *temp = tstate->c_profileobj;
4538 Py_XINCREF(arg);
4539 tstate->c_profilefunc = NULL;
4540 tstate->c_profileobj = NULL;
4541 /* Must make sure that tracing is not ignored if 'temp' is freed */
4542 tstate->use_tracing = tstate->c_tracefunc != NULL;
4543 Py_XDECREF(temp);
4544 tstate->c_profilefunc = func;
4545 tstate->c_profileobj = arg;
4546 /* Flag that tracing or profiling is turned on */
4547 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004548}
4549
4550void
4551PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004553 PyThreadState *tstate = PyThreadState_GET();
4554 PyObject *temp = tstate->c_traceobj;
4555 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4556 Py_XINCREF(arg);
4557 tstate->c_tracefunc = NULL;
4558 tstate->c_traceobj = NULL;
4559 /* Must make sure that profiling is not ignored if 'temp' is freed */
4560 tstate->use_tracing = tstate->c_profilefunc != NULL;
4561 Py_XDECREF(temp);
4562 tstate->c_tracefunc = func;
4563 tstate->c_traceobj = arg;
4564 /* Flag that tracing or profiling is turned on */
4565 tstate->use_tracing = ((func != NULL)
4566 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004567}
4568
Yury Selivanov75445082015-05-11 22:57:16 -04004569void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004570_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004571{
4572 PyThreadState *tstate = PyThreadState_GET();
4573
Yury Selivanov75445082015-05-11 22:57:16 -04004574 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004575 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004576}
4577
4578PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004579_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004580{
4581 PyThreadState *tstate = PyThreadState_GET();
4582 return tstate->coroutine_wrapper;
4583}
4584
Yury Selivanoveb636452016-09-08 22:01:51 -07004585void
4586_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4587{
4588 PyThreadState *tstate = PyThreadState_GET();
4589
4590 Py_XINCREF(firstiter);
4591 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4592}
4593
4594PyObject *
4595_PyEval_GetAsyncGenFirstiter(void)
4596{
4597 PyThreadState *tstate = PyThreadState_GET();
4598 return tstate->async_gen_firstiter;
4599}
4600
4601void
4602_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4603{
4604 PyThreadState *tstate = PyThreadState_GET();
4605
4606 Py_XINCREF(finalizer);
4607 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4608}
4609
4610PyObject *
4611_PyEval_GetAsyncGenFinalizer(void)
4612{
4613 PyThreadState *tstate = PyThreadState_GET();
4614 return tstate->async_gen_finalizer;
4615}
4616
Guido van Rossumb209a111997-04-29 18:18:01 +00004617PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004618PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004619{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004620 PyFrameObject *current_frame = PyEval_GetFrame();
4621 if (current_frame == NULL)
4622 return PyThreadState_GET()->interp->builtins;
4623 else
4624 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004625}
4626
Guido van Rossumb209a111997-04-29 18:18:01 +00004627PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004628PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004629{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004630 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004631 if (current_frame == NULL) {
4632 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004633 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004634 }
4635
4636 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4637 return NULL;
4638
4639 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004640 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004641}
4642
Guido van Rossumb209a111997-04-29 18:18:01 +00004643PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004644PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004645{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004646 PyFrameObject *current_frame = PyEval_GetFrame();
4647 if (current_frame == NULL)
4648 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004649
4650 assert(current_frame->f_globals != NULL);
4651 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004652}
4653
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004654PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004655PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004656{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004657 PyThreadState *tstate = PyThreadState_GET();
4658 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004659}
4660
Guido van Rossum6135a871995-01-09 17:53:26 +00004661int
Tim Peters5ba58662001-07-16 02:29:45 +00004662PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004663{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004664 PyFrameObject *current_frame = PyEval_GetFrame();
4665 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004667 if (current_frame != NULL) {
4668 const int codeflags = current_frame->f_code->co_flags;
4669 const int compilerflags = codeflags & PyCF_MASK;
4670 if (compilerflags) {
4671 result = 1;
4672 cf->cf_flags |= compilerflags;
4673 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004674#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004675 if (codeflags & CO_GENERATOR_ALLOWED) {
4676 result = 1;
4677 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4678 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004679#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004680 }
4681 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004682}
4683
Guido van Rossum3f5da241990-12-20 15:06:42 +00004684
Guido van Rossum681d79a1995-07-18 14:51:37 +00004685/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00004686 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00004687
Guido van Rossumb209a111997-04-29 18:18:01 +00004688PyObject *
Victor Stinner8a31c822016-08-19 17:12:23 +02004689PyEval_CallObjectWithKeywords(PyObject *func, PyObject *args, PyObject *kwargs)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004690{
Victor Stinner59b356d2015-03-16 11:52:32 +01004691#ifdef Py_DEBUG
4692 /* PyEval_CallObjectWithKeywords() must not be called with an exception
4693 set. It raises a new exception if parameters are invalid or if
4694 PyTuple_New() fails, and so the original exception is lost. */
4695 assert(!PyErr_Occurred());
4696#endif
4697
Victor Stinner8a31c822016-08-19 17:12:23 +02004698 if (args == NULL) {
Victor Stinner155ea652016-08-22 23:26:00 +02004699 return _PyObject_FastCallDict(func, NULL, 0, kwargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004700 }
Victor Stinner155ea652016-08-22 23:26:00 +02004701
4702 if (!PyTuple_Check(args)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004703 PyErr_SetString(PyExc_TypeError,
4704 "argument list must be a tuple");
4705 return NULL;
4706 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00004707
Victor Stinner8a31c822016-08-19 17:12:23 +02004708 if (kwargs != NULL && !PyDict_Check(kwargs)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004709 PyErr_SetString(PyExc_TypeError,
4710 "keyword list must be a dictionary");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004711 return NULL;
4712 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00004713
Victor Stinner6e2333d2016-08-23 00:25:01 +02004714 return PyObject_Call(func, args, kwargs);
Jeremy Hylton52820442001-01-03 23:52:36 +00004715}
4716
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004717const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004718PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004719{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004720 if (PyMethod_Check(func))
4721 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4722 else if (PyFunction_Check(func))
4723 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
4724 else if (PyCFunction_Check(func))
4725 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4726 else
4727 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004728}
4729
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004730const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004731PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004732{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004733 if (PyMethod_Check(func))
4734 return "()";
4735 else if (PyFunction_Check(func))
4736 return "()";
4737 else if (PyCFunction_Check(func))
4738 return "()";
4739 else
4740 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004741}
4742
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004743#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004744if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004745 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4746 tstate, tstate->frame, \
4747 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004748 x = NULL; \
4749 } \
4750 else { \
4751 x = call; \
4752 if (tstate->c_profilefunc != NULL) { \
4753 if (x == NULL) { \
4754 call_trace_protected(tstate->c_profilefunc, \
4755 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004756 tstate, tstate->frame, \
4757 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004758 /* XXX should pass (type, value, tb) */ \
4759 } else { \
4760 if (call_trace(tstate->c_profilefunc, \
4761 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004762 tstate, tstate->frame, \
4763 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004764 Py_DECREF(x); \
4765 x = NULL; \
4766 } \
4767 } \
4768 } \
4769 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004770} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004771 x = call; \
4772 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004773
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004774static PyObject *
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004775call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004776{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004777 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004778 PyObject *func = *pfunc;
4779 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004780 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4781 Py_ssize_t nargs = oparg - nkwargs;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004782 PyObject **stack;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004784 /* Always dispatch PyCFunction first, because these are
4785 presumed to be the most frequent callable object.
4786 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004787 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004788 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004790 PCALL(PCALL_CFUNCTION);
Victor Stinner4a7cc882015-03-06 23:35:27 +01004791
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004792 stack = (*pp_stack) - nargs - nkwargs;
4793 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004794 }
4795 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004796 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4797 /* optimize access to bound methods */
4798 PyObject *self = PyMethod_GET_SELF(func);
4799 PCALL(PCALL_METHOD);
4800 PCALL(PCALL_BOUND_METHOD);
4801 Py_INCREF(self);
4802 func = PyMethod_GET_FUNCTION(func);
4803 Py_INCREF(func);
4804 Py_SETREF(*pfunc, self);
4805 nargs++;
4806 }
4807 else {
4808 Py_INCREF(func);
4809 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004810
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004811 stack = (*pp_stack) - nargs - nkwargs;
Victor Stinner4a7cc882015-03-06 23:35:27 +01004812
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004813 if (PyFunction_Check(func)) {
4814 x = fast_function(func, stack, nargs, kwnames);
4815 }
4816 else {
4817 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4818 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004819
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004820 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004821 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004822
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004823 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004825 /* Clear the stack of the function object. Also removes
4826 the arguments in case they weren't consumed already
Victor Stinnere90bdb12016-08-25 23:26:50 +02004827 (fast_function() and err_args() leave them on the stack).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004828 */
4829 while ((*pp_stack) > pfunc) {
4830 w = EXT_POP(*pp_stack);
4831 Py_DECREF(w);
4832 PCALL(PCALL_POP);
4833 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004835 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004836}
4837
Victor Stinnere90bdb12016-08-25 23:26:50 +02004838/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004839 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004840 For the simplest case -- a function that takes only positional
4841 arguments and is called with only positional arguments -- it
4842 inlines the most primitive frame setup code from
4843 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4844 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004845*/
4846
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004847static PyObject*
Victor Stinnerd8735722016-09-09 12:36:44 -07004848_PyFunction_FastCall(PyCodeObject *co, PyObject **args, Py_ssize_t nargs,
4849 PyObject *globals)
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004850{
4851 PyFrameObject *f;
4852 PyThreadState *tstate = PyThreadState_GET();
4853 PyObject **fastlocals;
4854 Py_ssize_t i;
4855 PyObject *result;
4856
4857 PCALL(PCALL_FASTER_FUNCTION);
4858 assert(globals != NULL);
4859 /* XXX Perhaps we should create a specialized
4860 PyFrame_New() that doesn't take locals, but does
4861 take builtins without sanity checking them.
4862 */
4863 assert(tstate != NULL);
4864 f = PyFrame_New(tstate, co, globals, NULL);
4865 if (f == NULL) {
4866 return NULL;
4867 }
4868
4869 fastlocals = f->f_localsplus;
4870
Victor Stinner74319ae2016-08-25 00:04:09 +02004871 for (i = 0; i < nargs; i++) {
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004872 Py_INCREF(*args);
4873 fastlocals[i] = *args++;
4874 }
4875 result = PyEval_EvalFrameEx(f,0);
4876
4877 ++tstate->recursion_depth;
4878 Py_DECREF(f);
4879 --tstate->recursion_depth;
4880
4881 return result;
4882}
4883
Victor Stinnere90bdb12016-08-25 23:26:50 +02004884static PyObject *
Victor Stinnerd8735722016-09-09 12:36:44 -07004885fast_function(PyObject *func, PyObject **stack,
4886 Py_ssize_t nargs, PyObject *kwnames)
Jeremy Hylton52820442001-01-03 23:52:36 +00004887{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004888 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4889 PyObject *globals = PyFunction_GET_GLOBALS(func);
4890 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004891 PyObject *kwdefs, *closure, *name, *qualname;
4892 PyObject **d;
Victor Stinnerd8735722016-09-09 12:36:44 -07004893 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004894 Py_ssize_t nd;
Victor Stinnerd8735722016-09-09 12:36:44 -07004895
Victor Stinner57f91ac2016-09-12 13:37:07 +02004896 assert(PyFunction_Check(func));
4897 assert(nargs >= 0);
4898 assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
Victor Stinnerd8735722016-09-09 12:36:44 -07004899 assert((nargs == 0 && nkwargs == 0) || stack != NULL);
Victor Stinner57f91ac2016-09-12 13:37:07 +02004900 /* kwnames must only contains str strings, no subclass, and all keys must
4901 be unique */
Victor Stinner577e1f82016-08-25 00:29:32 +02004902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004903 PCALL(PCALL_FUNCTION);
4904 PCALL(PCALL_FAST_FUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004905
Victor Stinner74319ae2016-08-25 00:04:09 +02004906 if (co->co_kwonlyargcount == 0 && nkwargs == 0 &&
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004907 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
4908 {
Victor Stinner2eedc112016-08-22 12:29:42 +02004909 if (argdefs == NULL && co->co_argcount == nargs) {
Victor Stinnerd8735722016-09-09 12:36:44 -07004910 return _PyFunction_FastCall(co, stack, nargs, globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02004911 }
4912 else if (nargs == 0 && argdefs != NULL
4913 && co->co_argcount == Py_SIZE(argdefs)) {
4914 /* function called with no arguments, but all parameters have
4915 a default value: use default values as arguments .*/
4916 stack = &PyTuple_GET_ITEM(argdefs, 0);
Victor Stinnerd8735722016-09-09 12:36:44 -07004917 return _PyFunction_FastCall(co, stack, Py_SIZE(argdefs), globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02004918 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004919 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004920
4921 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
4922 closure = PyFunction_GET_CLOSURE(func);
4923 name = ((PyFunctionObject *)func) -> func_name;
4924 qualname = ((PyFunctionObject *)func) -> func_qualname;
4925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004926 if (argdefs != NULL) {
4927 d = &PyTuple_GET_ITEM(argdefs, 0);
4928 nd = Py_SIZE(argdefs);
4929 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004930 else {
4931 d = NULL;
4932 nd = 0;
4933 }
4934 return _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
Victor Stinner2eedc112016-08-22 12:29:42 +02004935 stack, nargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004936 nkwargs ? &PyTuple_GET_ITEM(kwnames, 0) : NULL,
4937 stack + nargs,
4938 nkwargs, 1,
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004939 d, (int)nd, kwdefs,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004940 closure, name, qualname);
4941}
4942
4943PyObject *
Victor Stinnerd8735722016-09-09 12:36:44 -07004944_PyFunction_FastCallKeywords(PyObject *func, PyObject **stack,
4945 Py_ssize_t nargs, PyObject *kwnames)
4946{
4947 return fast_function(func, stack, nargs, kwnames);
4948}
4949
4950PyObject *
Victor Stinner74319ae2016-08-25 00:04:09 +02004951_PyFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs,
Victor Stinnerb9009392016-08-22 23:15:44 +02004952 PyObject *kwargs)
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004953{
4954 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4955 PyObject *globals = PyFunction_GET_GLOBALS(func);
4956 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4957 PyObject *kwdefs, *closure, *name, *qualname;
Victor Stinnerb9009392016-08-22 23:15:44 +02004958 PyObject *kwtuple, **k;
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004959 PyObject **d;
Victor Stinner74319ae2016-08-25 00:04:09 +02004960 Py_ssize_t nd, nk;
Victor Stinnerb9009392016-08-22 23:15:44 +02004961 PyObject *result;
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004962
Victor Stinner74319ae2016-08-25 00:04:09 +02004963 assert(func != NULL);
4964 assert(nargs >= 0);
4965 assert(nargs == 0 || args != NULL);
Victor Stinnerb9009392016-08-22 23:15:44 +02004966 assert(kwargs == NULL || PyDict_Check(kwargs));
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004967
Victor Stinner577e1f82016-08-25 00:29:32 +02004968 PCALL(PCALL_FUNCTION);
4969 PCALL(PCALL_FAST_FUNCTION);
4970
Victor Stinnerb9009392016-08-22 23:15:44 +02004971 if (co->co_kwonlyargcount == 0 &&
4972 (kwargs == NULL || PyDict_Size(kwargs) == 0) &&
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004973 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
4974 {
Victor Stinnerb9009392016-08-22 23:15:44 +02004975 /* Fast paths */
Victor Stinner2eedc112016-08-22 12:29:42 +02004976 if (argdefs == NULL && co->co_argcount == nargs) {
Victor Stinnerd8735722016-09-09 12:36:44 -07004977 return _PyFunction_FastCall(co, args, nargs, globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02004978 }
4979 else if (nargs == 0 && argdefs != NULL
4980 && co->co_argcount == Py_SIZE(argdefs)) {
4981 /* function called with no arguments, but all parameters have
4982 a default value: use default values as arguments .*/
4983 args = &PyTuple_GET_ITEM(argdefs, 0);
Victor Stinnerd8735722016-09-09 12:36:44 -07004984 return _PyFunction_FastCall(co, args, Py_SIZE(argdefs), globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02004985 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004986 }
4987
Victor Stinnerb9009392016-08-22 23:15:44 +02004988 if (kwargs != NULL) {
4989 Py_ssize_t pos, i;
4990 nk = PyDict_Size(kwargs);
4991
4992 kwtuple = PyTuple_New(2 * nk);
4993 if (kwtuple == NULL) {
4994 return NULL;
4995 }
4996
4997 k = &PyTuple_GET_ITEM(kwtuple, 0);
4998 pos = i = 0;
4999 while (PyDict_Next(kwargs, &pos, &k[i], &k[i+1])) {
5000 Py_INCREF(k[i]);
5001 Py_INCREF(k[i+1]);
5002 i += 2;
5003 }
5004 nk = i / 2;
5005 }
5006 else {
5007 kwtuple = NULL;
5008 k = NULL;
5009 nk = 0;
5010 }
5011
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005012 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
5013 closure = PyFunction_GET_CLOSURE(func);
5014 name = ((PyFunctionObject *)func) -> func_name;
5015 qualname = ((PyFunctionObject *)func) -> func_qualname;
5016
5017 if (argdefs != NULL) {
5018 d = &PyTuple_GET_ITEM(argdefs, 0);
5019 nd = Py_SIZE(argdefs);
5020 }
5021 else {
5022 d = NULL;
5023 nd = 0;
5024 }
Victor Stinnerb9009392016-08-22 23:15:44 +02005025
5026 result = _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
5027 args, nargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03005028 k, k + 1, nk, 2,
Victor Stinnerb9009392016-08-22 23:15:44 +02005029 d, nd, kwdefs,
5030 closure, name, qualname);
5031 Py_XDECREF(kwtuple);
5032 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00005033}
5034
5035static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005036do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005037{
Jeremy Hylton985eba52003-02-05 23:13:00 +00005038#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005039 /* At this point, we have to look at the type of func to
5040 update the call stats properly. Do it here so as to avoid
5041 exposing the call stats machinery outside ceval.c
5042 */
5043 if (PyFunction_Check(func))
5044 PCALL(PCALL_FUNCTION);
5045 else if (PyMethod_Check(func))
5046 PCALL(PCALL_METHOD);
5047 else if (PyType_Check(func))
5048 PCALL(PCALL_TYPE);
5049 else if (PyCFunction_Check(func))
5050 PCALL(PCALL_CFUNCTION);
5051 else
5052 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00005053#endif
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005055 if (PyCFunction_Check(func)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005056 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005057 PyThreadState *tstate = PyThreadState_GET();
5058 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005059 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005060 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005061 else {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005062 return PyObject_Call(func, callargs, kwdict);
Victor Stinner74319ae2016-08-25 00:04:09 +02005063 }
Jeremy Hylton52820442001-01-03 23:52:36 +00005064}
5065
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005066/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005067 nb_index slot defined, and store in *pi.
5068 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
5069 and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005070 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005071*/
Tim Petersb5196382001-12-16 19:44:20 +00005072/* Note: If v is NULL, return success without storing into *pi. This
5073 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
5074 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00005075*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005076int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005077_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005078{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005079 if (v != NULL) {
5080 Py_ssize_t x;
5081 if (PyIndex_Check(v)) {
5082 x = PyNumber_AsSsize_t(v, NULL);
5083 if (x == -1 && PyErr_Occurred())
5084 return 0;
5085 }
5086 else {
5087 PyErr_SetString(PyExc_TypeError,
5088 "slice indices must be integers or "
5089 "None or have an __index__ method");
5090 return 0;
5091 }
5092 *pi = x;
5093 }
5094 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005095}
5096
Guido van Rossum486364b2007-06-30 05:01:58 +00005097#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005098 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00005099
Guido van Rossumb209a111997-04-29 18:18:01 +00005100static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02005101cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005102{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005103 int res = 0;
5104 switch (op) {
5105 case PyCmp_IS:
5106 res = (v == w);
5107 break;
5108 case PyCmp_IS_NOT:
5109 res = (v != w);
5110 break;
5111 case PyCmp_IN:
5112 res = PySequence_Contains(w, v);
5113 if (res < 0)
5114 return NULL;
5115 break;
5116 case PyCmp_NOT_IN:
5117 res = PySequence_Contains(w, v);
5118 if (res < 0)
5119 return NULL;
5120 res = !res;
5121 break;
5122 case PyCmp_EXC_MATCH:
5123 if (PyTuple_Check(w)) {
5124 Py_ssize_t i, length;
5125 length = PyTuple_Size(w);
5126 for (i = 0; i < length; i += 1) {
5127 PyObject *exc = PyTuple_GET_ITEM(w, i);
5128 if (!PyExceptionClass_Check(exc)) {
5129 PyErr_SetString(PyExc_TypeError,
5130 CANNOT_CATCH_MSG);
5131 return NULL;
5132 }
5133 }
5134 }
5135 else {
5136 if (!PyExceptionClass_Check(w)) {
5137 PyErr_SetString(PyExc_TypeError,
5138 CANNOT_CATCH_MSG);
5139 return NULL;
5140 }
5141 }
5142 res = PyErr_GivenExceptionMatches(v, w);
5143 break;
5144 default:
5145 return PyObject_RichCompare(v, w, op);
5146 }
5147 v = res ? Py_True : Py_False;
5148 Py_INCREF(v);
5149 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005150}
5151
Thomas Wouters52152252000-08-17 22:55:00 +00005152static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005153import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
5154{
5155 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005156 PyObject *import_func, *res;
5157 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005158
5159 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
5160 if (import_func == NULL) {
5161 PyErr_SetString(PyExc_ImportError, "__import__ not found");
5162 return NULL;
5163 }
5164
5165 /* Fast path for not overloaded __import__. */
5166 if (import_func == PyThreadState_GET()->interp->import_func) {
5167 int ilevel = _PyLong_AsInt(level);
5168 if (ilevel == -1 && PyErr_Occurred()) {
5169 return NULL;
5170 }
5171 res = PyImport_ImportModuleLevelObject(
5172 name,
5173 f->f_globals,
5174 f->f_locals == NULL ? Py_None : f->f_locals,
5175 fromlist,
5176 ilevel);
5177 return res;
5178 }
5179
5180 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005181
5182 stack[0] = name;
5183 stack[1] = f->f_globals;
5184 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5185 stack[3] = fromlist;
5186 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005187 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005188 Py_DECREF(import_func);
5189 return res;
5190}
5191
5192static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00005193import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005194{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005195 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005196 _Py_IDENTIFIER(__name__);
5197 PyObject *fullmodname, *pkgname;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005199 x = PyObject_GetAttr(v, name);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005200 if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
5201 return x;
5202 /* Issue #17636: in case this failed because of a circular relative
5203 import, try to fallback on reading the module directly from
5204 sys.modules. */
5205 PyErr_Clear();
5206 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005207 if (pkgname == NULL) {
5208 goto error;
5209 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005210 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
5211 Py_DECREF(pkgname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005212 if (fullmodname == NULL) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005213 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005214 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005215 x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005216 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005217 if (x == NULL) {
5218 goto error;
5219 }
5220 Py_INCREF(x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005221 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005222 error:
5223 PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
5224 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005225}
Guido van Rossumac7be682001-01-17 15:42:30 +00005226
Thomas Wouters52152252000-08-17 22:55:00 +00005227static int
5228import_all_from(PyObject *locals, PyObject *v)
5229{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005230 _Py_IDENTIFIER(__all__);
5231 _Py_IDENTIFIER(__dict__);
5232 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005233 PyObject *dict, *name, *value;
5234 int skip_leading_underscores = 0;
5235 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005237 if (all == NULL) {
5238 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5239 return -1; /* Unexpected error */
5240 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005241 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005242 if (dict == NULL) {
5243 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5244 return -1;
5245 PyErr_SetString(PyExc_ImportError,
5246 "from-import-* object has no __dict__ and no __all__");
5247 return -1;
5248 }
5249 all = PyMapping_Keys(dict);
5250 Py_DECREF(dict);
5251 if (all == NULL)
5252 return -1;
5253 skip_leading_underscores = 1;
5254 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005256 for (pos = 0, err = 0; ; pos++) {
5257 name = PySequence_GetItem(all, pos);
5258 if (name == NULL) {
5259 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5260 err = -1;
5261 else
5262 PyErr_Clear();
5263 break;
5264 }
5265 if (skip_leading_underscores &&
5266 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005267 PyUnicode_READY(name) != -1 &&
5268 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005269 {
5270 Py_DECREF(name);
5271 continue;
5272 }
5273 value = PyObject_GetAttr(v, name);
5274 if (value == NULL)
5275 err = -1;
5276 else if (PyDict_CheckExact(locals))
5277 err = PyDict_SetItem(locals, name, value);
5278 else
5279 err = PyObject_SetItem(locals, name, value);
5280 Py_DECREF(name);
5281 Py_XDECREF(value);
5282 if (err != 0)
5283 break;
5284 }
5285 Py_DECREF(all);
5286 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005287}
5288
Guido van Rossumac7be682001-01-17 15:42:30 +00005289static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005290format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005291{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005292 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005294 if (!obj)
5295 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005297 obj_str = _PyUnicode_AsString(obj);
5298 if (!obj_str)
5299 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005301 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005302}
Guido van Rossum950361c1997-01-24 13:49:28 +00005303
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005304static void
5305format_exc_unbound(PyCodeObject *co, int oparg)
5306{
5307 PyObject *name;
5308 /* Don't stomp existing exception */
5309 if (PyErr_Occurred())
5310 return;
5311 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5312 name = PyTuple_GET_ITEM(co->co_cellvars,
5313 oparg);
5314 format_exc_check_arg(
5315 PyExc_UnboundLocalError,
5316 UNBOUNDLOCAL_ERROR_MSG,
5317 name);
5318 } else {
5319 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5320 PyTuple_GET_SIZE(co->co_cellvars));
5321 format_exc_check_arg(PyExc_NameError,
5322 UNBOUNDFREE_ERROR_MSG, name);
5323 }
5324}
5325
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005326static PyObject *
5327unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005328 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005329{
5330 PyObject *res;
5331 if (Py_REFCNT(v) == 2) {
5332 /* In the common case, there are 2 references to the value
5333 * stored in 'variable' when the += is performed: one on the
5334 * value stack (in 'v') and one still stored in the
5335 * 'variable'. We try to delete the variable now to reduce
5336 * the refcnt to 1.
5337 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005338 int opcode, oparg;
5339 NEXTOPARG();
5340 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005341 case STORE_FAST:
5342 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005343 PyObject **fastlocals = f->f_localsplus;
5344 if (GETLOCAL(oparg) == v)
5345 SETLOCAL(oparg, NULL);
5346 break;
5347 }
5348 case STORE_DEREF:
5349 {
5350 PyObject **freevars = (f->f_localsplus +
5351 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005352 PyObject *c = freevars[oparg];
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005353 if (PyCell_GET(c) == v)
5354 PyCell_Set(c, NULL);
5355 break;
5356 }
5357 case STORE_NAME:
5358 {
5359 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005360 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005361 PyObject *locals = f->f_locals;
5362 if (PyDict_CheckExact(locals) &&
5363 PyDict_GetItem(locals, name) == v) {
5364 if (PyDict_DelItem(locals, name) != 0) {
5365 PyErr_Clear();
5366 }
5367 }
5368 break;
5369 }
5370 }
5371 }
5372 res = v;
5373 PyUnicode_Append(&res, w);
5374 return res;
5375}
5376
Guido van Rossum950361c1997-01-24 13:49:28 +00005377#ifdef DYNAMIC_EXECUTION_PROFILE
5378
Skip Montanarof118cb12001-10-15 20:51:38 +00005379static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005380getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005381{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005382 int i;
5383 PyObject *l = PyList_New(256);
5384 if (l == NULL) return NULL;
5385 for (i = 0; i < 256; i++) {
5386 PyObject *x = PyLong_FromLong(a[i]);
5387 if (x == NULL) {
5388 Py_DECREF(l);
5389 return NULL;
5390 }
5391 PyList_SetItem(l, i, x);
5392 }
5393 for (i = 0; i < 256; i++)
5394 a[i] = 0;
5395 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005396}
5397
5398PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005399_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005400{
5401#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005402 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005403#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005404 int i;
5405 PyObject *l = PyList_New(257);
5406 if (l == NULL) return NULL;
5407 for (i = 0; i < 257; i++) {
5408 PyObject *x = getarray(dxpairs[i]);
5409 if (x == NULL) {
5410 Py_DECREF(l);
5411 return NULL;
5412 }
5413 PyList_SetItem(l, i, x);
5414 }
5415 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005416#endif
5417}
5418
5419#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005420
5421Py_ssize_t
5422_PyEval_RequestCodeExtraIndex(freefunc free)
5423{
5424 PyThreadState *tstate = PyThreadState_Get();
5425 Py_ssize_t new_index;
5426
5427 if (tstate->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
5428 return -1;
5429 }
5430 new_index = tstate->co_extra_user_count++;
5431 tstate->co_extra_freefuncs[new_index] = free;
5432 return new_index;
5433}
Łukasz Langaa785c872016-09-09 17:37:37 -07005434
5435static void
5436dtrace_function_entry(PyFrameObject *f)
5437{
5438 char* filename;
5439 char* funcname;
5440 int lineno;
5441
5442 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5443 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5444 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5445
5446 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5447}
5448
5449static void
5450dtrace_function_return(PyFrameObject *f)
5451{
5452 char* filename;
5453 char* funcname;
5454 int lineno;
5455
5456 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5457 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5458 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5459
5460 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5461}
5462
5463/* DTrace equivalent of maybe_call_line_trace. */
5464static void
5465maybe_dtrace_line(PyFrameObject *frame,
5466 int *instr_lb, int *instr_ub, int *instr_prev)
5467{
5468 int line = frame->f_lineno;
5469 char *co_filename, *co_name;
5470
5471 /* If the last instruction executed isn't in the current
5472 instruction window, reset the window.
5473 */
5474 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5475 PyAddrPair bounds;
5476 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5477 &bounds);
5478 *instr_lb = bounds.ap_lower;
5479 *instr_ub = bounds.ap_upper;
5480 }
5481 /* If the last instruction falls at the start of a line or if
5482 it represents a jump backwards, update the frame's line
5483 number and call the trace function. */
5484 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5485 frame->f_lineno = line;
5486 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5487 if (!co_filename)
5488 co_filename = "?";
5489 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5490 if (!co_name)
5491 co_name = "?";
5492 PyDTrace_LINE(co_filename, co_name, line);
5493 }
5494 *instr_prev = frame->f_lasti;
5495}