blob: 42c0c614b1d7086831694f46795ded4d6cecda33 [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
Benjamin Petersond2be5b42010-09-10 22:47:02 +000086#ifdef WITH_THREAD
87#define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request)
88#else
89#define GIL_REQUEST 0
90#endif
91
Jeffrey Yasskin39370832010-05-03 19:29:34 +000092/* This can set eval_breaker to 0 even though gil_drop_request became
93 1. We believe this is all right because the eval loop will release
94 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +000095#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 _Py_atomic_store_relaxed( \
97 &eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +000098 GIL_REQUEST | \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
100 pending_async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000101
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000102#ifdef WITH_THREAD
103
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000104#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 do { \
106 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
107 _Py_atomic_store_relaxed(&eval_breaker, 1); \
108 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000109
110#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 do { \
112 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
113 COMPUTE_EVAL_BREAKER(); \
114 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000115
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000116#endif
117
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000118/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000119#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 do { \
121 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
122 _Py_atomic_store_relaxed(&eval_breaker, 1); \
123 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000124
125#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 do { \
127 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
128 COMPUTE_EVAL_BREAKER(); \
129 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000130
131#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 do { \
133 pending_async_exc = 1; \
134 _Py_atomic_store_relaxed(&eval_breaker, 1); \
135 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000136
137#define UNSIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000139
140
Guido van Rossume59214e1994-08-30 08:01:59 +0000141#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000142
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000143#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000144#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000145#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000146#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000147
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000148static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000149static long main_thread = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000150/* This single variable consolidates all requests to break out of the fast path
151 in the eval loop. */
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000152static _Py_atomic_int eval_breaker = {0};
153/* Request for dropping the GIL */
154static _Py_atomic_int gil_drop_request = {0};
155/* Request for running pending calls. */
156static _Py_atomic_int pendingcalls_to_do = {0};
157/* Request for looking at the `async_exc` field of the current thread state.
158 Guarded by the GIL. */
159static int pending_async_exc = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000160
161#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000162
Tim Peters7f468f22004-10-11 02:40:51 +0000163int
164PyEval_ThreadsInitialized(void)
165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000167}
168
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000169void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000170PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000171{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 if (gil_created())
173 return;
174 create_gil();
175 take_gil(PyThreadState_GET());
176 main_thread = PyThread_get_thread_ident();
177 if (!pending_lock)
178 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000179}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000180
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000181void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000182_PyEval_FiniThreads(void)
183{
184 if (!gil_created())
185 return;
186 destroy_gil();
187 assert(!gil_created());
188}
189
190void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000191PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000192{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 PyThreadState *tstate = PyThreadState_GET();
194 if (tstate == NULL)
195 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
196 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000197}
198
199void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000200PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 /* This function must succeed when the current thread state is NULL.
203 We therefore avoid PyThreadState_GET() which dumps a fatal error
204 in debug mode.
205 */
206 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
207 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000208}
209
210void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000211PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 if (tstate == NULL)
214 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
215 /* Check someone has called PyEval_InitThreads() to create the lock */
216 assert(gil_created());
217 take_gil(tstate);
218 if (PyThreadState_Swap(tstate) != NULL)
219 Py_FatalError(
220 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000221}
222
223void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000224PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 if (tstate == NULL)
227 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
228 if (PyThreadState_Swap(NULL) != tstate)
229 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
230 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000231}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000232
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200233/* This function is called from PyOS_AfterFork to destroy all threads which are
234 * not running in the child process, and clear internal locks which might be
235 * held by those threads. (This could also be done using pthread_atfork
236 * mechanism, at least for the pthreads implementation.) */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000237
238void
239PyEval_ReInitThreads(void)
240{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200241 _Py_IDENTIFIER(_after_fork);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 PyObject *threading, *result;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200243 PyThreadState *current_tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 if (!gil_created())
246 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 recreate_gil();
248 pending_lock = PyThread_allocate_lock();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200249 take_gil(current_tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 /* Update the threading module with the new state.
253 */
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200254 threading = PyMapping_GetItemString(current_tstate->interp->modules,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 "threading");
256 if (threading == NULL) {
257 /* threading not imported */
258 PyErr_Clear();
259 return;
260 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200261 result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 if (result == NULL)
263 PyErr_WriteUnraisable(threading);
264 else
265 Py_DECREF(result);
266 Py_DECREF(threading);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200267
268 /* Destroy all threads except the current one */
269 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000270}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000271
272#else
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000273static _Py_atomic_int eval_breaker = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000274static int pending_async_exc = 0;
275#endif /* WITH_THREAD */
276
277/* This function is used to signal that async exceptions are waiting to be
278 raised, therefore it is also useful in non-threaded builds. */
279
280void
281_PyEval_SignalAsyncExc(void)
282{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000284}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000285
Guido van Rossumff4949e1992-08-05 19:58:53 +0000286/* Functions save_thread and restore_thread are always defined so
287 dynamically loaded modules needn't be compiled separately for use
288 with and without threads: */
289
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000290PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000291PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000292{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 PyThreadState *tstate = PyThreadState_Swap(NULL);
294 if (tstate == NULL)
295 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000296#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 if (gil_created())
298 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000299#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000301}
302
303void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000304PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 if (tstate == NULL)
307 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000308#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 if (gil_created()) {
310 int err = errno;
311 take_gil(tstate);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200312 /* _Py_Finalizing is protected by the GIL */
313 if (_Py_Finalizing && tstate != _Py_Finalizing) {
314 drop_gil(tstate);
315 PyThread_exit_thread();
316 assert(0); /* unreachable */
317 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 errno = err;
319 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000320#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000322}
323
324
Guido van Rossuma9672091994-09-14 13:31:22 +0000325/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
326 signal handlers or Mac I/O completion routines) can schedule calls
327 to a function to be called synchronously.
328 The synchronous function is called with one void* argument.
329 It should return 0 for success or -1 for failure -- failure should
330 be accompanied by an exception.
331
332 If registry succeeds, the registry function returns 0; if it fails
333 (e.g. due to too many pending calls) it returns -1 (without setting
334 an exception condition).
335
336 Note that because registry may occur from within signal handlers,
337 or other asynchronous events, calling malloc() is unsafe!
338
339#ifdef WITH_THREAD
340 Any thread can schedule pending calls, but only the main thread
341 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000342 There is no facility to schedule calls to a particular thread, but
343 that should be easy to change, should that ever be required. In
344 that case, the static variables here should go into the python
345 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000346#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000347*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000348
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000349#ifdef WITH_THREAD
350
351/* The WITH_THREAD implementation is thread-safe. It allows
352 scheduling to be made from any thread, and even from an executing
353 callback.
354 */
355
356#define NPENDINGCALLS 32
357static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 int (*func)(void *);
359 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000360} pendingcalls[NPENDINGCALLS];
361static int pendingfirst = 0;
362static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000363
364int
365Py_AddPendingCall(int (*func)(void *), void *arg)
366{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 int i, j, result=0;
368 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 /* try a few times for the lock. Since this mechanism is used
371 * for signal handling (on the main thread), there is a (slim)
372 * chance that a signal is delivered on the same thread while we
373 * hold the lock during the Py_MakePendingCalls() function.
374 * This avoids a deadlock in that case.
375 * Note that signals can be delivered on any thread. In particular,
376 * on Windows, a SIGINT is delivered on a system-created worker
377 * thread.
378 * We also check for lock being NULL, in the unlikely case that
379 * this function is called before any bytecode evaluation takes place.
380 */
381 if (lock != NULL) {
382 for (i = 0; i<100; i++) {
383 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
384 break;
385 }
386 if (i == 100)
387 return -1;
388 }
389
390 i = pendinglast;
391 j = (i + 1) % NPENDINGCALLS;
392 if (j == pendingfirst) {
393 result = -1; /* Queue full */
394 } else {
395 pendingcalls[i].func = func;
396 pendingcalls[i].arg = arg;
397 pendinglast = j;
398 }
399 /* signal main loop */
400 SIGNAL_PENDING_CALLS();
401 if (lock != NULL)
402 PyThread_release_lock(lock);
403 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000404}
405
406int
407Py_MakePendingCalls(void)
408{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200409 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 int i;
411 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 if (!pending_lock) {
414 /* initial allocation of the lock */
415 pending_lock = PyThread_allocate_lock();
416 if (pending_lock == NULL)
417 return -1;
418 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 /* only service pending calls on main thread */
421 if (main_thread && PyThread_get_thread_ident() != main_thread)
422 return 0;
423 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200424 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200426 busy = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 /* perform a bounded number of calls, in case of recursion */
428 for (i=0; i<NPENDINGCALLS; i++) {
429 int j;
430 int (*func)(void *);
431 void *arg = NULL;
432
433 /* pop one item off the queue while holding the lock */
434 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
435 j = pendingfirst;
436 if (j == pendinglast) {
437 func = NULL; /* Queue empty */
438 } else {
439 func = pendingcalls[j].func;
440 arg = pendingcalls[j].arg;
441 pendingfirst = (j + 1) % NPENDINGCALLS;
442 }
443 if (pendingfirst != pendinglast)
444 SIGNAL_PENDING_CALLS();
445 else
446 UNSIGNAL_PENDING_CALLS();
447 PyThread_release_lock(pending_lock);
448 /* having released the lock, perform the callback */
449 if (func == NULL)
450 break;
451 r = func(arg);
452 if (r)
453 break;
454 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200455 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000457}
458
459#else /* if ! defined WITH_THREAD */
460
461/*
462 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
463 This code is used for signal handling in python that isn't built
464 with WITH_THREAD.
465 Don't use this implementation when Py_AddPendingCalls() can happen
466 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467
Guido van Rossuma9672091994-09-14 13:31:22 +0000468 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000469 (1) nested asynchronous calls to Py_AddPendingCall()
470 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000472 (1) is very unlikely because typically signal delivery
473 is blocked during signal handling. So it should be impossible.
474 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000475 The current code is safe against (2), but not against (1).
476 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000477 thread is present, interrupted by signals, and that the critical
478 section is protected with the "busy" variable. On Windows, which
479 delivers SIGINT on a system thread, this does not hold and therefore
480 Windows really shouldn't use this version.
481 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000482*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000483
Guido van Rossuma9672091994-09-14 13:31:22 +0000484#define NPENDINGCALLS 32
485static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 int (*func)(void *);
487 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000488} pendingcalls[NPENDINGCALLS];
489static volatile int pendingfirst = 0;
490static volatile int pendinglast = 0;
Benjamin Peterson08ec84c2010-05-30 14:49:32 +0000491static _Py_atomic_int pendingcalls_to_do = {0};
Guido van Rossuma9672091994-09-14 13:31:22 +0000492
493int
Thomas Wouters334fb892000-07-25 12:56:38 +0000494Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000495{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 static volatile int busy = 0;
497 int i, j;
498 /* XXX Begin critical section */
499 if (busy)
500 return -1;
501 busy = 1;
502 i = pendinglast;
503 j = (i + 1) % NPENDINGCALLS;
504 if (j == pendingfirst) {
505 busy = 0;
506 return -1; /* Queue full */
507 }
508 pendingcalls[i].func = func;
509 pendingcalls[i].arg = arg;
510 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 SIGNAL_PENDING_CALLS();
513 busy = 0;
514 /* XXX End critical section */
515 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000516}
517
Guido van Rossum180d7b41994-09-29 09:45:57 +0000518int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000519Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000520{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 static int busy = 0;
522 if (busy)
523 return 0;
524 busy = 1;
525 UNSIGNAL_PENDING_CALLS();
526 for (;;) {
527 int i;
528 int (*func)(void *);
529 void *arg;
530 i = pendingfirst;
531 if (i == pendinglast)
532 break; /* Queue empty */
533 func = pendingcalls[i].func;
534 arg = pendingcalls[i].arg;
535 pendingfirst = (i + 1) % NPENDINGCALLS;
536 if (func(arg) < 0) {
537 busy = 0;
538 SIGNAL_PENDING_CALLS(); /* We're not done yet */
539 return -1;
540 }
541 }
542 busy = 0;
543 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000544}
545
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000546#endif /* WITH_THREAD */
547
Guido van Rossuma9672091994-09-14 13:31:22 +0000548
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000549/* The interpreter's recursion limit */
550
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000551#ifndef Py_DEFAULT_RECURSION_LIMIT
552#define Py_DEFAULT_RECURSION_LIMIT 1000
553#endif
554static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
555int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000556
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000557int
558Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000561}
562
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000563void
564Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 recursion_limit = new_limit;
567 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000568}
569
Armin Rigo2b3eb402003-10-28 12:05:48 +0000570/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
571 if the recursion_depth reaches _Py_CheckRecursionLimit.
572 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
573 to guarantee that _Py_CheckRecursiveCall() is regularly called.
574 Without USE_STACKCHECK, there is no need for this. */
575int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300576_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000577{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000579
580#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 if (PyOS_CheckStack()) {
582 --tstate->recursion_depth;
583 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
584 return -1;
585 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000586#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 _Py_CheckRecursionLimit = recursion_limit;
588 if (tstate->recursion_critical)
589 /* Somebody asked that we don't check for recursion. */
590 return 0;
591 if (tstate->overflowed) {
592 if (tstate->recursion_depth > recursion_limit + 50) {
593 /* Overflowing while handling an overflow. Give up. */
594 Py_FatalError("Cannot recover from stack overflow.");
595 }
596 return 0;
597 }
598 if (tstate->recursion_depth > recursion_limit) {
599 --tstate->recursion_depth;
600 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400601 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 "maximum recursion depth exceeded%s",
603 where);
604 return -1;
605 }
606 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000607}
608
Guido van Rossum374a9221991-04-04 10:40:29 +0000609/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000610enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000611 WHY_NOT = 0x0001, /* No error */
612 WHY_EXCEPTION = 0x0002, /* Exception occurred */
Stefan Krahb7e10102010-06-23 18:42:39 +0000613 WHY_RETURN = 0x0008, /* 'return' statement */
614 WHY_BREAK = 0x0010, /* 'break' statement */
615 WHY_CONTINUE = 0x0020, /* 'continue' statement */
616 WHY_YIELD = 0x0040, /* 'yield' operator */
617 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000618};
Guido van Rossum374a9221991-04-04 10:40:29 +0000619
Benjamin Peterson87880242011-07-03 16:48:31 -0500620static void save_exc_state(PyThreadState *, PyFrameObject *);
621static void swap_exc_state(PyThreadState *, PyFrameObject *);
622static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400623static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000624static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000625
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000626/* Records whether tracing is on for any thread. Counts the number of
627 threads for which tstate->c_tracefunc is non-NULL, so if the value
628 is 0, we know we don't have to check this thread's c_tracefunc.
629 This speeds up the if statement in PyEval_EvalFrameEx() after
630 fast_next_opcode*/
631static int _Py_TracingPossible = 0;
632
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000633
Guido van Rossum374a9221991-04-04 10:40:29 +0000634
Guido van Rossumb209a111997-04-29 18:18:01 +0000635PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000636PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000637{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 return PyEval_EvalCodeEx(co,
639 globals, locals,
640 (PyObject **)NULL, 0,
641 (PyObject **)NULL, 0,
642 (PyObject **)NULL, 0,
643 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000644}
645
646
647/* Interpreter main loop */
648
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000649PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000650PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 /* This is for backward compatibility with extension modules that
652 used this API; core interpreter code should call
653 PyEval_EvalFrameEx() */
654 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000655}
656
657PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000658PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000659{
Brett Cannon3cebf932016-09-05 15:33:46 -0700660 PyThreadState *tstate = PyThreadState_GET();
661 return tstate->interp->eval_frame(f, throwflag);
662}
663
Victor Stinnerc6944e72016-11-11 02:13:35 +0100664PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700665_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
666{
Guido van Rossum950361c1997-01-24 13:49:28 +0000667#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000669#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200670 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300671 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200672 int opcode; /* Current opcode */
673 int oparg; /* Current opcode argument, if any */
674 enum why_code why; /* Reason for block stack unwind */
675 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 PyObject *retval = NULL; /* Return value */
677 PyThreadState *tstate = PyThreadState_GET();
678 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 is true when the line being executed has changed. The
685 initial values are such as to make this false the first
686 time it is tested. */
687 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000688
Serhiy Storchakaab874002016-09-11 13:48:15 +0300689 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 PyObject *names;
691 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000692
Brett Cannon368b4b72012-04-02 12:17:59 -0400693#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200694 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400695#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200696
Antoine Pitroub52ec782009-01-25 16:34:23 +0000697/* Computed GOTOs, or
698 the-optimization-commonly-but-improperly-known-as-"threaded code"
699 using gcc's labels-as-values extension
700 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
701
702 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000704 combined with a lookup table of jump addresses. However, since the
705 indirect jump instruction is shared by all opcodes, the CPU will have a
706 hard time making the right prediction for where to jump next (actually,
707 it will be always wrong except in the uncommon case of a sequence of
708 several identical opcodes).
709
710 "Threaded code" in contrast, uses an explicit jump table and an explicit
711 indirect jump instruction at the end of each opcode. Since the jump
712 instruction is at a different address for each opcode, the CPU will make a
713 separate prediction for each of these instructions, which is equivalent to
714 predicting the second opcode of each opcode pair. These predictions have
715 a much better chance to turn out valid, especially in small bytecode loops.
716
717 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000719 and potentially many more instructions (depending on the pipeline width).
720 A correctly predicted branch, however, is nearly free.
721
722 At the time of this writing, the "threaded code" version is up to 15-20%
723 faster than the normal "switch" version, depending on the compiler and the
724 CPU architecture.
725
726 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
727 because it would render the measurements invalid.
728
729
730 NOTE: care must be taken that the compiler doesn't try to "optimize" the
731 indirect jumps by sharing them between all opcodes. Such optimizations
732 can be disabled on gcc by using the -fno-gcse flag (or possibly
733 -fno-crossjumping).
734*/
735
Antoine Pitrou042b1282010-08-13 21:15:58 +0000736#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000737#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000738#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000739#endif
740
Antoine Pitrou042b1282010-08-13 21:15:58 +0000741#ifdef HAVE_COMPUTED_GOTOS
742 #ifndef USE_COMPUTED_GOTOS
743 #define USE_COMPUTED_GOTOS 1
744 #endif
745#else
746 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
747 #error "Computed gotos are not supported on this compiler."
748 #endif
749 #undef USE_COMPUTED_GOTOS
750 #define USE_COMPUTED_GOTOS 0
751#endif
752
753#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000754/* Import the static jump table */
755#include "opcode_targets.h"
756
Antoine Pitroub52ec782009-01-25 16:34:23 +0000757#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 TARGET_##op: \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000760
Antoine Pitroub52ec782009-01-25 16:34:23 +0000761#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 { \
763 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
764 FAST_DISPATCH(); \
765 } \
766 continue; \
767 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000768
769#ifdef LLTRACE
770#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700772 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300774 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300775 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 } \
777 goto fast_next_opcode; \
778 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000779#else
780#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700782 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300784 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300785 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 } \
787 goto fast_next_opcode; \
788 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000789#endif
790
791#else
792#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 case op:
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300794
Antoine Pitroub52ec782009-01-25 16:34:23 +0000795#define DISPATCH() continue
796#define FAST_DISPATCH() goto fast_next_opcode
797#endif
798
799
Neal Norwitza81d2202002-07-14 00:27:26 +0000800/* Tuple access macros */
801
802#ifndef Py_DEBUG
803#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
804#else
805#define GETITEM(v, i) PyTuple_GetItem((v), (i))
806#endif
807
Guido van Rossum374a9221991-04-04 10:40:29 +0000808/* Code access macros */
809
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300810/* The integer overflow is checked by an assertion below. */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300811#define INSTR_OFFSET() (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300812#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300813 _Py_CODEUNIT word = *next_instr; \
814 opcode = _Py_OPCODE(word); \
815 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300816 next_instr++; \
817 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300818#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
819#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000820
Raymond Hettingerf606f872003-03-16 03:11:04 +0000821/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 Some opcodes tend to come in pairs thus making it possible to
823 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300824 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 Verifying the prediction costs a single high-speed test of a register
827 variable against a constant. If the pairing was good, then the
828 processor's own internal branch predication has a high likelihood of
829 success, resulting in a nearly zero-overhead transition to the
830 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300831 including its unpredictable switch-case branch. Combined with the
832 processor's internal branch prediction, a successful PREDICT has the
833 effect of making the two opcodes run as if they were a single new opcode
834 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000835
Georg Brandl86b2fb92008-07-16 03:43:04 +0000836 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 predictions turned-on and interpret the results as if some opcodes
838 had been combined or turn-off predictions so that the opcode frequency
839 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000840
841 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 the CPU to record separate branch prediction information for each
843 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000844
Raymond Hettingerf606f872003-03-16 03:11:04 +0000845*/
846
Antoine Pitrou042b1282010-08-13 21:15:58 +0000847#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000849#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300850#define PREDICT(op) \
851 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300852 _Py_CODEUNIT word = *next_instr; \
853 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300854 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300855 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300856 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300857 goto PRED_##op; \
858 } \
859 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000860#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300861#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000862
Raymond Hettingerf606f872003-03-16 03:11:04 +0000863
Guido van Rossum374a9221991-04-04 10:40:29 +0000864/* Stack manipulation macros */
865
Martin v. Löwis18e16552006-02-15 17:27:45 +0000866/* The stack can grow at most MAXINT deep, as co_nlocals and
867 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000868#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
869#define EMPTY() (STACK_LEVEL() == 0)
870#define TOP() (stack_pointer[-1])
871#define SECOND() (stack_pointer[-2])
872#define THIRD() (stack_pointer[-3])
873#define FOURTH() (stack_pointer[-4])
874#define PEEK(n) (stack_pointer[-(n)])
875#define SET_TOP(v) (stack_pointer[-1] = (v))
876#define SET_SECOND(v) (stack_pointer[-2] = (v))
877#define SET_THIRD(v) (stack_pointer[-3] = (v))
878#define SET_FOURTH(v) (stack_pointer[-4] = (v))
879#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
880#define BASIC_STACKADJ(n) (stack_pointer += n)
881#define BASIC_PUSH(v) (*stack_pointer++ = (v))
882#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000883
Guido van Rossum96a42c81992-01-12 02:29:51 +0000884#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000886 lltrace && prtrace(TOP(), "push")); \
887 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000889 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000891 lltrace && prtrace(TOP(), "stackadj")); \
892 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +0000893#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000894 prtrace((STACK_POINTER)[-1], "ext_pop")), \
895 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000896#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000897#define PUSH(v) BASIC_PUSH(v)
898#define POP() BASIC_POP()
899#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000900#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000901#endif
902
Guido van Rossum681d79a1995-07-18 14:51:37 +0000903/* Local variable macros */
904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000906
907/* The SETLOCAL() macro must not DECREF the local variable in-place and
908 then store the new value; it must copy the old value to a temporary
909 value, then store the new value, and then DECREF the temporary value.
910 This is because it is possible that during the DECREF the frame is
911 accessed by other code (e.g. a __del__ method or gc.collect()) and the
912 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000914 GETLOCAL(i) = value; \
915 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000916
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000917
918#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 while (STACK_LEVEL() > (b)->b_level) { \
920 PyObject *v = POP(); \
921 Py_XDECREF(v); \
922 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000923
924#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300925 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 PyObject *type, *value, *traceback; \
927 assert(STACK_LEVEL() >= (b)->b_level + 3); \
928 while (STACK_LEVEL() > (b)->b_level + 3) { \
929 value = POP(); \
930 Py_XDECREF(value); \
931 } \
932 type = tstate->exc_type; \
933 value = tstate->exc_value; \
934 traceback = tstate->exc_traceback; \
935 tstate->exc_type = POP(); \
936 tstate->exc_value = POP(); \
937 tstate->exc_traceback = POP(); \
938 Py_XDECREF(type); \
939 Py_XDECREF(value); \
940 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300941 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000942
Guido van Rossuma027efa1997-05-05 20:56:21 +0000943/* Start of code */
944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 /* push frame */
946 if (Py_EnterRecursiveCall(""))
947 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 if (tstate->use_tracing) {
952 if (tstate->c_tracefunc != NULL) {
953 /* tstate->c_tracefunc, if defined, is a
954 function that will be called on *every* entry
955 to a code block. Its return value, if not
956 None, is a function that will be called at
957 the start of each executed line of code.
958 (Actually, the function must return itself
959 in order to continue tracing.) The trace
960 functions are called with three arguments:
961 a pointer to the current frame, a string
962 indicating why the function is called, and
963 an argument which depends on the situation.
964 The global trace function is also called
965 whenever an exception is detected. */
966 if (call_trace_protected(tstate->c_tracefunc,
967 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100968 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 /* Trace function raised an error */
970 goto exit_eval_frame;
971 }
972 }
973 if (tstate->c_profilefunc != NULL) {
974 /* Similar for c_profilefunc, except it needn't
975 return itself and isn't called for "line" events */
976 if (call_trace_protected(tstate->c_profilefunc,
977 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100978 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 /* Profile function raised an error */
980 goto exit_eval_frame;
981 }
982 }
983 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000984
Łukasz Langaa785c872016-09-09 17:37:37 -0700985 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
986 dtrace_function_entry(f);
987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 co = f->f_code;
989 names = co->co_names;
990 consts = co->co_consts;
991 fastlocals = f->f_localsplus;
992 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300993 assert(PyBytes_Check(co->co_code));
994 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +0300995 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
996 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
997 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300998 /*
999 f->f_lasti refers to the index of the last instruction,
1000 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001001
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001002 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001003 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 When the PREDICT() macros are enabled, some opcode pairs follow in
1006 direct succession without updating f->f_lasti. A successful
1007 prediction effectively links the two codes together as if they
1008 were a single new opcode; accordingly,f->f_lasti will point to
1009 the first code in the pair (for instance, GET_ITER followed by
1010 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001011 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001013 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001014 next_instr = first_instr;
1015 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001016 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1017 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001018 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 stack_pointer = f->f_stacktop;
1020 assert(stack_pointer != NULL);
1021 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001022 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001023
Yury Selivanoveb636452016-09-08 22:01:51 -07001024 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01001025 if (!throwflag && f->f_exc_type != NULL && f->f_exc_type != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 /* We were in an except handler when we left,
1027 restore the exception state which was put aside
1028 (see YIELD_VALUE). */
Benjamin Peterson87880242011-07-03 16:48:31 -05001029 swap_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 }
Benjamin Peterson87880242011-07-03 16:48:31 -05001031 else
1032 save_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001034
Tim Peters5ca576e2001-06-18 22:08:13 +00001035#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001036 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001037#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001038
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 why = WHY_NOT;
Guido van Rossumac7be682001-01-17 15:42:30 +00001040
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001041 if (throwflag) /* support for generator.throw() */
1042 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001043
Victor Stinnerace47d72013-07-18 01:41:08 +02001044#ifdef Py_DEBUG
1045 /* PyEval_EvalFrameEx() must not be called with an exception set,
1046 because it may clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001047 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +02001048 assert(!PyErr_Occurred());
1049#endif
1050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1053 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +02001054 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 /* Do periodic things. Doing this every time through
1057 the loop would add too much overhead, so we do it
1058 only every Nth instruction. We also do it if
1059 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1060 event needs attention (e.g. a signal handler or
1061 async I/O handler); see Py_AddPendingCall() and
1062 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 if (_Py_atomic_load_relaxed(&eval_breaker)) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001065 if (_Py_OPCODE(*next_instr) == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 /* Make the last opcode before
Ezio Melotti13925002011-03-16 11:05:33 +02001067 a try: finally: block uninterruptible. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 goto fast_next_opcode;
1069 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001071 if (Py_MakePendingCalls() < 0)
1072 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001074#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001075 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 /* Give another thread a chance */
1077 if (PyThreadState_Swap(NULL) != tstate)
1078 Py_FatalError("ceval: tstate mix-up");
1079 drop_gil(tstate);
1080
1081 /* Other threads may run now */
1082
1083 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001084
1085 /* Check if we should make a quick exit. */
1086 if (_Py_Finalizing && _Py_Finalizing != tstate) {
1087 drop_gil(tstate);
1088 PyThread_exit_thread();
1089 }
1090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 if (PyThreadState_Swap(tstate) != NULL)
1092 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001094#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 /* Check for asynchronous exceptions. */
1096 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001097 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 tstate->async_exc = NULL;
1099 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001100 PyErr_SetNone(exc);
1101 Py_DECREF(exc);
1102 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 }
1104 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 fast_next_opcode:
1107 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001108
Łukasz Langaa785c872016-09-09 17:37:37 -07001109 if (PyDTrace_LINE_ENABLED())
1110 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001115 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001116 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 /* see maybe_call_line_trace
1118 for expository comments */
1119 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 err = maybe_call_line_trace(tstate->c_tracefunc,
1122 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001123 tstate, f,
1124 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 /* Reload possibly changed frame fields */
1126 JUMPTO(f->f_lasti);
1127 if (f->f_stacktop != NULL) {
1128 stack_pointer = f->f_stacktop;
1129 f->f_stacktop = NULL;
1130 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001131 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001133 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001137
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001138 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001139 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001140#ifdef DYNAMIC_EXECUTION_PROFILE
1141#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 dxpairs[lastopcode][opcode]++;
1143 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001144#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001146#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001147
Guido van Rossum96a42c81992-01-12 02:29:51 +00001148#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 if (lltrace) {
1152 if (HAS_ARG(opcode)) {
1153 printf("%d: %d, %d\n",
1154 f->f_lasti, opcode, oparg);
1155 }
1156 else {
1157 printf("%d: %d\n",
1158 f->f_lasti, opcode);
1159 }
1160 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001161#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 /* BEWARE!
1166 It is essential that any operation that fails sets either
1167 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1168 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 TARGET(NOP)
1171 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001172
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001173 TARGET(LOAD_FAST) {
1174 PyObject *value = GETLOCAL(oparg);
1175 if (value == NULL) {
1176 format_exc_check_arg(PyExc_UnboundLocalError,
1177 UNBOUNDLOCAL_ERROR_MSG,
1178 PyTuple_GetItem(co->co_varnames, oparg));
1179 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001181 Py_INCREF(value);
1182 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001184 }
1185
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001186 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001187 TARGET(LOAD_CONST) {
1188 PyObject *value = GETITEM(consts, oparg);
1189 Py_INCREF(value);
1190 PUSH(value);
1191 FAST_DISPATCH();
1192 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001193
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001194 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001195 TARGET(STORE_FAST) {
1196 PyObject *value = POP();
1197 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001199 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001200
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001201 TARGET(POP_TOP) {
1202 PyObject *value = POP();
1203 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001205 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001206
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001207 TARGET(ROT_TWO) {
1208 PyObject *top = TOP();
1209 PyObject *second = SECOND();
1210 SET_TOP(second);
1211 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001213 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001214
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001215 TARGET(ROT_THREE) {
1216 PyObject *top = TOP();
1217 PyObject *second = SECOND();
1218 PyObject *third = THIRD();
1219 SET_TOP(second);
1220 SET_SECOND(third);
1221 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001223 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001224
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001225 TARGET(DUP_TOP) {
1226 PyObject *top = TOP();
1227 Py_INCREF(top);
1228 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001230 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001231
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001232 TARGET(DUP_TOP_TWO) {
1233 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001234 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001235 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001236 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001237 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001238 SET_TOP(top);
1239 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001240 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001241 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001242
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001243 TARGET(UNARY_POSITIVE) {
1244 PyObject *value = TOP();
1245 PyObject *res = PyNumber_Positive(value);
1246 Py_DECREF(value);
1247 SET_TOP(res);
1248 if (res == NULL)
1249 goto error;
1250 DISPATCH();
1251 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001252
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001253 TARGET(UNARY_NEGATIVE) {
1254 PyObject *value = TOP();
1255 PyObject *res = PyNumber_Negative(value);
1256 Py_DECREF(value);
1257 SET_TOP(res);
1258 if (res == NULL)
1259 goto error;
1260 DISPATCH();
1261 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001262
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001263 TARGET(UNARY_NOT) {
1264 PyObject *value = TOP();
1265 int err = PyObject_IsTrue(value);
1266 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 if (err == 0) {
1268 Py_INCREF(Py_True);
1269 SET_TOP(Py_True);
1270 DISPATCH();
1271 }
1272 else if (err > 0) {
1273 Py_INCREF(Py_False);
1274 SET_TOP(Py_False);
1275 err = 0;
1276 DISPATCH();
1277 }
1278 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001279 goto error;
1280 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001281
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001282 TARGET(UNARY_INVERT) {
1283 PyObject *value = TOP();
1284 PyObject *res = PyNumber_Invert(value);
1285 Py_DECREF(value);
1286 SET_TOP(res);
1287 if (res == NULL)
1288 goto error;
1289 DISPATCH();
1290 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001291
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001292 TARGET(BINARY_POWER) {
1293 PyObject *exp = POP();
1294 PyObject *base = TOP();
1295 PyObject *res = PyNumber_Power(base, exp, Py_None);
1296 Py_DECREF(base);
1297 Py_DECREF(exp);
1298 SET_TOP(res);
1299 if (res == NULL)
1300 goto error;
1301 DISPATCH();
1302 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001303
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001304 TARGET(BINARY_MULTIPLY) {
1305 PyObject *right = POP();
1306 PyObject *left = TOP();
1307 PyObject *res = PyNumber_Multiply(left, right);
1308 Py_DECREF(left);
1309 Py_DECREF(right);
1310 SET_TOP(res);
1311 if (res == NULL)
1312 goto error;
1313 DISPATCH();
1314 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001315
Benjamin Petersond51374e2014-04-09 23:55:56 -04001316 TARGET(BINARY_MATRIX_MULTIPLY) {
1317 PyObject *right = POP();
1318 PyObject *left = TOP();
1319 PyObject *res = PyNumber_MatrixMultiply(left, right);
1320 Py_DECREF(left);
1321 Py_DECREF(right);
1322 SET_TOP(res);
1323 if (res == NULL)
1324 goto error;
1325 DISPATCH();
1326 }
1327
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001328 TARGET(BINARY_TRUE_DIVIDE) {
1329 PyObject *divisor = POP();
1330 PyObject *dividend = TOP();
1331 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1332 Py_DECREF(dividend);
1333 Py_DECREF(divisor);
1334 SET_TOP(quotient);
1335 if (quotient == NULL)
1336 goto error;
1337 DISPATCH();
1338 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001339
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001340 TARGET(BINARY_FLOOR_DIVIDE) {
1341 PyObject *divisor = POP();
1342 PyObject *dividend = TOP();
1343 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1344 Py_DECREF(dividend);
1345 Py_DECREF(divisor);
1346 SET_TOP(quotient);
1347 if (quotient == NULL)
1348 goto error;
1349 DISPATCH();
1350 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001351
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001352 TARGET(BINARY_MODULO) {
1353 PyObject *divisor = POP();
1354 PyObject *dividend = TOP();
1355 PyObject *res = PyUnicode_CheckExact(dividend) ?
1356 PyUnicode_Format(dividend, divisor) :
1357 PyNumber_Remainder(dividend, divisor);
1358 Py_DECREF(divisor);
1359 Py_DECREF(dividend);
1360 SET_TOP(res);
1361 if (res == NULL)
1362 goto error;
1363 DISPATCH();
1364 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001365
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001366 TARGET(BINARY_ADD) {
1367 PyObject *right = POP();
1368 PyObject *left = TOP();
1369 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001370 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1371 CPython using bytecode, it is simply worthless.
1372 See http://bugs.python.org/issue21955 and
1373 http://bugs.python.org/issue10044 for the discussion. In short,
1374 no patch shown any impact on a realistic benchmark, only a minor
1375 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001376 if (PyUnicode_CheckExact(left) &&
1377 PyUnicode_CheckExact(right)) {
1378 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001379 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001380 }
1381 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001382 sum = PyNumber_Add(left, right);
1383 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001384 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001385 Py_DECREF(right);
1386 SET_TOP(sum);
1387 if (sum == NULL)
1388 goto error;
1389 DISPATCH();
1390 }
1391
1392 TARGET(BINARY_SUBTRACT) {
1393 PyObject *right = POP();
1394 PyObject *left = TOP();
1395 PyObject *diff = PyNumber_Subtract(left, right);
1396 Py_DECREF(right);
1397 Py_DECREF(left);
1398 SET_TOP(diff);
1399 if (diff == NULL)
1400 goto error;
1401 DISPATCH();
1402 }
1403
1404 TARGET(BINARY_SUBSCR) {
1405 PyObject *sub = POP();
1406 PyObject *container = TOP();
1407 PyObject *res = PyObject_GetItem(container, sub);
1408 Py_DECREF(container);
1409 Py_DECREF(sub);
1410 SET_TOP(res);
1411 if (res == NULL)
1412 goto error;
1413 DISPATCH();
1414 }
1415
1416 TARGET(BINARY_LSHIFT) {
1417 PyObject *right = POP();
1418 PyObject *left = TOP();
1419 PyObject *res = PyNumber_Lshift(left, right);
1420 Py_DECREF(left);
1421 Py_DECREF(right);
1422 SET_TOP(res);
1423 if (res == NULL)
1424 goto error;
1425 DISPATCH();
1426 }
1427
1428 TARGET(BINARY_RSHIFT) {
1429 PyObject *right = POP();
1430 PyObject *left = TOP();
1431 PyObject *res = PyNumber_Rshift(left, right);
1432 Py_DECREF(left);
1433 Py_DECREF(right);
1434 SET_TOP(res);
1435 if (res == NULL)
1436 goto error;
1437 DISPATCH();
1438 }
1439
1440 TARGET(BINARY_AND) {
1441 PyObject *right = POP();
1442 PyObject *left = TOP();
1443 PyObject *res = PyNumber_And(left, right);
1444 Py_DECREF(left);
1445 Py_DECREF(right);
1446 SET_TOP(res);
1447 if (res == NULL)
1448 goto error;
1449 DISPATCH();
1450 }
1451
1452 TARGET(BINARY_XOR) {
1453 PyObject *right = POP();
1454 PyObject *left = TOP();
1455 PyObject *res = PyNumber_Xor(left, right);
1456 Py_DECREF(left);
1457 Py_DECREF(right);
1458 SET_TOP(res);
1459 if (res == NULL)
1460 goto error;
1461 DISPATCH();
1462 }
1463
1464 TARGET(BINARY_OR) {
1465 PyObject *right = POP();
1466 PyObject *left = TOP();
1467 PyObject *res = PyNumber_Or(left, right);
1468 Py_DECREF(left);
1469 Py_DECREF(right);
1470 SET_TOP(res);
1471 if (res == NULL)
1472 goto error;
1473 DISPATCH();
1474 }
1475
1476 TARGET(LIST_APPEND) {
1477 PyObject *v = POP();
1478 PyObject *list = PEEK(oparg);
1479 int err;
1480 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001482 if (err != 0)
1483 goto error;
1484 PREDICT(JUMP_ABSOLUTE);
1485 DISPATCH();
1486 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001487
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001488 TARGET(SET_ADD) {
1489 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001490 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001491 int err;
1492 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001494 if (err != 0)
1495 goto error;
1496 PREDICT(JUMP_ABSOLUTE);
1497 DISPATCH();
1498 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001499
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001500 TARGET(INPLACE_POWER) {
1501 PyObject *exp = POP();
1502 PyObject *base = TOP();
1503 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1504 Py_DECREF(base);
1505 Py_DECREF(exp);
1506 SET_TOP(res);
1507 if (res == NULL)
1508 goto error;
1509 DISPATCH();
1510 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001511
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001512 TARGET(INPLACE_MULTIPLY) {
1513 PyObject *right = POP();
1514 PyObject *left = TOP();
1515 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1516 Py_DECREF(left);
1517 Py_DECREF(right);
1518 SET_TOP(res);
1519 if (res == NULL)
1520 goto error;
1521 DISPATCH();
1522 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001523
Benjamin Petersond51374e2014-04-09 23:55:56 -04001524 TARGET(INPLACE_MATRIX_MULTIPLY) {
1525 PyObject *right = POP();
1526 PyObject *left = TOP();
1527 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1528 Py_DECREF(left);
1529 Py_DECREF(right);
1530 SET_TOP(res);
1531 if (res == NULL)
1532 goto error;
1533 DISPATCH();
1534 }
1535
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001536 TARGET(INPLACE_TRUE_DIVIDE) {
1537 PyObject *divisor = POP();
1538 PyObject *dividend = TOP();
1539 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1540 Py_DECREF(dividend);
1541 Py_DECREF(divisor);
1542 SET_TOP(quotient);
1543 if (quotient == NULL)
1544 goto error;
1545 DISPATCH();
1546 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001547
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001548 TARGET(INPLACE_FLOOR_DIVIDE) {
1549 PyObject *divisor = POP();
1550 PyObject *dividend = TOP();
1551 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1552 Py_DECREF(dividend);
1553 Py_DECREF(divisor);
1554 SET_TOP(quotient);
1555 if (quotient == NULL)
1556 goto error;
1557 DISPATCH();
1558 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001559
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001560 TARGET(INPLACE_MODULO) {
1561 PyObject *right = POP();
1562 PyObject *left = TOP();
1563 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1564 Py_DECREF(left);
1565 Py_DECREF(right);
1566 SET_TOP(mod);
1567 if (mod == NULL)
1568 goto error;
1569 DISPATCH();
1570 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001571
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001572 TARGET(INPLACE_ADD) {
1573 PyObject *right = POP();
1574 PyObject *left = TOP();
1575 PyObject *sum;
1576 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1577 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001578 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001579 }
1580 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001581 sum = PyNumber_InPlaceAdd(left, right);
1582 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001583 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001584 Py_DECREF(right);
1585 SET_TOP(sum);
1586 if (sum == NULL)
1587 goto error;
1588 DISPATCH();
1589 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001590
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001591 TARGET(INPLACE_SUBTRACT) {
1592 PyObject *right = POP();
1593 PyObject *left = TOP();
1594 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1595 Py_DECREF(left);
1596 Py_DECREF(right);
1597 SET_TOP(diff);
1598 if (diff == NULL)
1599 goto error;
1600 DISPATCH();
1601 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001602
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001603 TARGET(INPLACE_LSHIFT) {
1604 PyObject *right = POP();
1605 PyObject *left = TOP();
1606 PyObject *res = PyNumber_InPlaceLshift(left, right);
1607 Py_DECREF(left);
1608 Py_DECREF(right);
1609 SET_TOP(res);
1610 if (res == NULL)
1611 goto error;
1612 DISPATCH();
1613 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001614
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001615 TARGET(INPLACE_RSHIFT) {
1616 PyObject *right = POP();
1617 PyObject *left = TOP();
1618 PyObject *res = PyNumber_InPlaceRshift(left, right);
1619 Py_DECREF(left);
1620 Py_DECREF(right);
1621 SET_TOP(res);
1622 if (res == NULL)
1623 goto error;
1624 DISPATCH();
1625 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001626
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001627 TARGET(INPLACE_AND) {
1628 PyObject *right = POP();
1629 PyObject *left = TOP();
1630 PyObject *res = PyNumber_InPlaceAnd(left, right);
1631 Py_DECREF(left);
1632 Py_DECREF(right);
1633 SET_TOP(res);
1634 if (res == NULL)
1635 goto error;
1636 DISPATCH();
1637 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001638
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001639 TARGET(INPLACE_XOR) {
1640 PyObject *right = POP();
1641 PyObject *left = TOP();
1642 PyObject *res = PyNumber_InPlaceXor(left, right);
1643 Py_DECREF(left);
1644 Py_DECREF(right);
1645 SET_TOP(res);
1646 if (res == NULL)
1647 goto error;
1648 DISPATCH();
1649 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001650
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001651 TARGET(INPLACE_OR) {
1652 PyObject *right = POP();
1653 PyObject *left = TOP();
1654 PyObject *res = PyNumber_InPlaceOr(left, right);
1655 Py_DECREF(left);
1656 Py_DECREF(right);
1657 SET_TOP(res);
1658 if (res == NULL)
1659 goto error;
1660 DISPATCH();
1661 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001662
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001663 TARGET(STORE_SUBSCR) {
1664 PyObject *sub = TOP();
1665 PyObject *container = SECOND();
1666 PyObject *v = THIRD();
1667 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 STACKADJ(-3);
Martin Panter95f53c12016-07-18 08:23:26 +00001669 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001670 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001672 Py_DECREF(container);
1673 Py_DECREF(sub);
1674 if (err != 0)
1675 goto error;
1676 DISPATCH();
1677 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001678
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001679 TARGET(STORE_ANNOTATION) {
1680 _Py_IDENTIFIER(__annotations__);
1681 PyObject *ann_dict;
1682 PyObject *ann = POP();
1683 PyObject *name = GETITEM(names, oparg);
1684 int err;
1685 if (f->f_locals == NULL) {
1686 PyErr_Format(PyExc_SystemError,
1687 "no locals found when storing annotation");
1688 Py_DECREF(ann);
1689 goto error;
1690 }
1691 /* first try to get __annotations__ from locals... */
1692 if (PyDict_CheckExact(f->f_locals)) {
1693 ann_dict = _PyDict_GetItemId(f->f_locals,
1694 &PyId___annotations__);
1695 if (ann_dict == NULL) {
1696 PyErr_SetString(PyExc_NameError,
1697 "__annotations__ not found");
1698 Py_DECREF(ann);
1699 goto error;
1700 }
1701 Py_INCREF(ann_dict);
1702 }
1703 else {
1704 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
1705 if (ann_str == NULL) {
1706 Py_DECREF(ann);
1707 goto error;
1708 }
1709 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
1710 if (ann_dict == NULL) {
1711 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
1712 PyErr_SetString(PyExc_NameError,
1713 "__annotations__ not found");
1714 }
1715 Py_DECREF(ann);
1716 goto error;
1717 }
1718 }
1719 /* ...if succeeded, __annotations__[name] = ann */
1720 if (PyDict_CheckExact(ann_dict)) {
1721 err = PyDict_SetItem(ann_dict, name, ann);
1722 }
1723 else {
1724 err = PyObject_SetItem(ann_dict, name, ann);
1725 }
1726 Py_DECREF(ann_dict);
Yury Selivanov50c584f2016-09-08 23:38:21 -07001727 Py_DECREF(ann);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001728 if (err != 0) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001729 goto error;
1730 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001731 DISPATCH();
1732 }
1733
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001734 TARGET(DELETE_SUBSCR) {
1735 PyObject *sub = TOP();
1736 PyObject *container = SECOND();
1737 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 STACKADJ(-2);
Martin Panter95f53c12016-07-18 08:23:26 +00001739 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001740 err = PyObject_DelItem(container, sub);
1741 Py_DECREF(container);
1742 Py_DECREF(sub);
1743 if (err != 0)
1744 goto error;
1745 DISPATCH();
1746 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001747
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001748 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001749 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001750 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001751 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001752 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001753 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 PyErr_SetString(PyExc_RuntimeError,
1755 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001756 Py_DECREF(value);
1757 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 }
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001759 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001760 Py_DECREF(value);
1761 if (res == NULL)
1762 goto error;
1763 Py_DECREF(res);
1764 DISPATCH();
1765 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001766
Thomas Wouters434d0822000-08-24 20:11:32 +00001767#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001769#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001770 TARGET(RAISE_VARARGS) {
1771 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 switch (oparg) {
1773 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001774 cause = POP(); /* cause */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001776 exc = POP(); /* exc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 case 0: /* Fallthrough */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001778 if (do_raise(exc, cause)) {
1779 why = WHY_EXCEPTION;
1780 goto fast_block_end;
1781 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 break;
1783 default:
1784 PyErr_SetString(PyExc_SystemError,
1785 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 break;
1787 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001788 goto error;
1789 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001790
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001791 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 retval = POP();
1793 why = WHY_RETURN;
1794 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001795 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001796
Yury Selivanov75445082015-05-11 22:57:16 -04001797 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001798 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001799 PyObject *iter = NULL;
1800 PyObject *awaitable = NULL;
1801 PyObject *obj = TOP();
1802 PyTypeObject *type = Py_TYPE(obj);
1803
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001804 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001805 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001806 }
Yury Selivanov75445082015-05-11 22:57:16 -04001807
1808 if (getter != NULL) {
1809 iter = (*getter)(obj);
1810 Py_DECREF(obj);
1811 if (iter == NULL) {
1812 SET_TOP(NULL);
1813 goto error;
1814 }
1815 }
1816 else {
1817 SET_TOP(NULL);
1818 PyErr_Format(
1819 PyExc_TypeError,
1820 "'async for' requires an object with "
1821 "__aiter__ method, got %.100s",
1822 type->tp_name);
1823 Py_DECREF(obj);
1824 goto error;
1825 }
1826
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001827 if (Py_TYPE(iter)->tp_as_async != NULL &&
1828 Py_TYPE(iter)->tp_as_async->am_anext != NULL) {
1829
1830 /* Starting with CPython 3.5.2 __aiter__ should return
1831 asynchronous iterators directly (not awaitables that
1832 resolve to asynchronous iterators.)
1833
1834 Therefore, we check if the object that was returned
1835 from __aiter__ has an __anext__ method. If it does,
1836 we wrap it in an awaitable that resolves to `iter`.
1837
1838 See http://bugs.python.org/issue27243 for more
1839 details.
1840 */
1841
1842 PyObject *wrapper = _PyAIterWrapper_New(iter);
1843 Py_DECREF(iter);
1844 SET_TOP(wrapper);
1845 DISPATCH();
1846 }
1847
Yury Selivanov5376ba92015-06-22 12:19:30 -04001848 awaitable = _PyCoro_GetAwaitableIter(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001849 if (awaitable == NULL) {
1850 SET_TOP(NULL);
1851 PyErr_Format(
1852 PyExc_TypeError,
1853 "'async for' received an invalid object "
1854 "from __aiter__: %.100s",
1855 Py_TYPE(iter)->tp_name);
1856
1857 Py_DECREF(iter);
1858 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001859 } else {
Yury Selivanov75445082015-05-11 22:57:16 -04001860 Py_DECREF(iter);
1861
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001862 if (PyErr_WarnFormat(
Yury Selivanov2edd8a12016-11-08 15:13:07 -05001863 PyExc_DeprecationWarning, 1,
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001864 "'%.100s' implements legacy __aiter__ protocol; "
1865 "__aiter__ should return an asynchronous "
1866 "iterator, not awaitable",
1867 type->tp_name))
1868 {
1869 /* Warning was converted to an error. */
1870 Py_DECREF(awaitable);
1871 SET_TOP(NULL);
1872 goto error;
1873 }
1874 }
1875
Yury Selivanov75445082015-05-11 22:57:16 -04001876 SET_TOP(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001877 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001878 DISPATCH();
1879 }
1880
1881 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001882 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001883 PyObject *next_iter = NULL;
1884 PyObject *awaitable = NULL;
1885 PyObject *aiter = TOP();
1886 PyTypeObject *type = Py_TYPE(aiter);
1887
Yury Selivanoveb636452016-09-08 22:01:51 -07001888 if (PyAsyncGen_CheckExact(aiter)) {
1889 awaitable = type->tp_as_async->am_anext(aiter);
1890 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001891 goto error;
1892 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001893 } else {
1894 if (type->tp_as_async != NULL){
1895 getter = type->tp_as_async->am_anext;
1896 }
Yury Selivanov75445082015-05-11 22:57:16 -04001897
Yury Selivanoveb636452016-09-08 22:01:51 -07001898 if (getter != NULL) {
1899 next_iter = (*getter)(aiter);
1900 if (next_iter == NULL) {
1901 goto error;
1902 }
1903 }
1904 else {
1905 PyErr_Format(
1906 PyExc_TypeError,
1907 "'async for' requires an iterator with "
1908 "__anext__ method, got %.100s",
1909 type->tp_name);
1910 goto error;
1911 }
Yury Selivanov75445082015-05-11 22:57:16 -04001912
Yury Selivanoveb636452016-09-08 22:01:51 -07001913 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1914 if (awaitable == NULL) {
1915 PyErr_Format(
1916 PyExc_TypeError,
1917 "'async for' received an invalid object "
1918 "from __anext__: %.100s",
1919 Py_TYPE(next_iter)->tp_name);
1920
1921 Py_DECREF(next_iter);
1922 goto error;
1923 } else {
1924 Py_DECREF(next_iter);
1925 }
1926 }
Yury Selivanov75445082015-05-11 22:57:16 -04001927
1928 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001929 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001930 DISPATCH();
1931 }
1932
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001933 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001934 TARGET(GET_AWAITABLE) {
1935 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001936 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001937
1938 Py_DECREF(iterable);
1939
Yury Selivanovc724bae2016-03-02 11:30:46 -05001940 if (iter != NULL && PyCoro_CheckExact(iter)) {
1941 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1942 if (yf != NULL) {
1943 /* `iter` is a coroutine object that is being
1944 awaited, `yf` is a pointer to the current awaitable
1945 being awaited on. */
1946 Py_DECREF(yf);
1947 Py_CLEAR(iter);
1948 PyErr_SetString(
1949 PyExc_RuntimeError,
1950 "coroutine is being awaited already");
1951 /* The code below jumps to `error` if `iter` is NULL. */
1952 }
1953 }
1954
Yury Selivanov75445082015-05-11 22:57:16 -04001955 SET_TOP(iter); /* Even if it's NULL */
1956
1957 if (iter == NULL) {
1958 goto error;
1959 }
1960
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001961 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001962 DISPATCH();
1963 }
1964
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001965 TARGET(YIELD_FROM) {
1966 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001967 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001968 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001969 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1970 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001971 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001972 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001973 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001974 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001975 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001976 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001977 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001978 Py_DECREF(v);
1979 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001980 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001981 if (tstate->c_tracefunc != NULL
1982 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001983 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001984 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001985 if (err < 0)
1986 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001987 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001988 SET_TOP(val);
1989 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001990 }
Martin Panter95f53c12016-07-18 08:23:26 +00001991 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001992 f->f_stacktop = stack_pointer;
1993 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001994 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001995 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001996 f->f_lasti -= sizeof(_Py_CODEUNIT);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001997 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001998 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001999
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002000 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002001 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002002
2003 if (co->co_flags & CO_ASYNC_GENERATOR) {
2004 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2005 Py_DECREF(retval);
2006 if (w == NULL) {
2007 retval = NULL;
2008 goto error;
2009 }
2010 retval = w;
2011 }
2012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002013 f->f_stacktop = stack_pointer;
2014 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002016 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002017
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002018 TARGET(POP_EXCEPT) {
2019 PyTryBlock *b = PyFrame_BlockPop(f);
2020 if (b->b_type != EXCEPT_HANDLER) {
2021 PyErr_SetString(PyExc_SystemError,
2022 "popped block is not an except handler");
2023 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002024 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002025 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002026 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002027 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002028
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002029 PREDICTED(POP_BLOCK);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002030 TARGET(POP_BLOCK) {
2031 PyTryBlock *b = PyFrame_BlockPop(f);
2032 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002033 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002034 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002036 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002037 TARGET(END_FINALLY) {
2038 PyObject *status = POP();
2039 if (PyLong_Check(status)) {
2040 why = (enum why_code) PyLong_AS_LONG(status);
2041 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002042 if (why == WHY_RETURN ||
2043 why == WHY_CONTINUE)
2044 retval = POP();
2045 if (why == WHY_SILENCED) {
2046 /* An exception was silenced by 'with', we must
2047 manually unwind the EXCEPT_HANDLER block which was
2048 created when the exception was caught, otherwise
2049 the stack will be in an inconsistent state. */
2050 PyTryBlock *b = PyFrame_BlockPop(f);
2051 assert(b->b_type == EXCEPT_HANDLER);
2052 UNWIND_EXCEPT_HANDLER(b);
2053 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002054 Py_DECREF(status);
2055 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002056 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002057 Py_DECREF(status);
2058 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002060 else if (PyExceptionClass_Check(status)) {
2061 PyObject *exc = POP();
2062 PyObject *tb = POP();
2063 PyErr_Restore(status, exc, tb);
2064 why = WHY_EXCEPTION;
2065 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002066 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002067 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002068 PyErr_SetString(PyExc_SystemError,
2069 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002070 Py_DECREF(status);
2071 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002073 Py_DECREF(status);
2074 DISPATCH();
2075 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002076
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002077 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002078 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002079
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002080 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002081 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002082 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
2083 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002084 PyErr_SetString(PyExc_NameError,
2085 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002086 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002087 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002088 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002089 }
2090 else {
2091 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2092 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002093 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002094 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2095 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002096 if (PyErr_ExceptionMatches(PyExc_KeyError))
2097 PyErr_SetString(PyExc_NameError,
2098 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002099 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002100 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002102 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002103 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002104 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002105
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002106 TARGET(STORE_NAME) {
2107 PyObject *name = GETITEM(names, oparg);
2108 PyObject *v = POP();
2109 PyObject *ns = f->f_locals;
2110 int err;
2111 if (ns == NULL) {
2112 PyErr_Format(PyExc_SystemError,
2113 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002115 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002116 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002117 if (PyDict_CheckExact(ns))
2118 err = PyDict_SetItem(ns, name, v);
2119 else
2120 err = PyObject_SetItem(ns, name, v);
2121 Py_DECREF(v);
2122 if (err != 0)
2123 goto error;
2124 DISPATCH();
2125 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002126
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002127 TARGET(DELETE_NAME) {
2128 PyObject *name = GETITEM(names, oparg);
2129 PyObject *ns = f->f_locals;
2130 int err;
2131 if (ns == NULL) {
2132 PyErr_Format(PyExc_SystemError,
2133 "no locals when deleting %R", name);
2134 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002135 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002136 err = PyObject_DelItem(ns, name);
2137 if (err != 0) {
2138 format_exc_check_arg(PyExc_NameError,
2139 NAME_ERROR_MSG,
2140 name);
2141 goto error;
2142 }
2143 DISPATCH();
2144 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002145
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002146 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002147 TARGET(UNPACK_SEQUENCE) {
2148 PyObject *seq = POP(), *item, **items;
2149 if (PyTuple_CheckExact(seq) &&
2150 PyTuple_GET_SIZE(seq) == oparg) {
2151 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002152 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002153 item = items[oparg];
2154 Py_INCREF(item);
2155 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002156 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002157 } else if (PyList_CheckExact(seq) &&
2158 PyList_GET_SIZE(seq) == oparg) {
2159 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002160 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002161 item = items[oparg];
2162 Py_INCREF(item);
2163 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002164 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002165 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002166 stack_pointer + oparg)) {
2167 STACKADJ(oparg);
2168 } else {
2169 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002170 Py_DECREF(seq);
2171 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002172 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002173 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002174 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002176
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002177 TARGET(UNPACK_EX) {
2178 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2179 PyObject *seq = POP();
2180
2181 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2182 stack_pointer + totalargs)) {
2183 stack_pointer += totalargs;
2184 } else {
2185 Py_DECREF(seq);
2186 goto error;
2187 }
2188 Py_DECREF(seq);
2189 DISPATCH();
2190 }
2191
2192 TARGET(STORE_ATTR) {
2193 PyObject *name = GETITEM(names, oparg);
2194 PyObject *owner = TOP();
2195 PyObject *v = SECOND();
2196 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002198 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002200 Py_DECREF(owner);
2201 if (err != 0)
2202 goto error;
2203 DISPATCH();
2204 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002205
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002206 TARGET(DELETE_ATTR) {
2207 PyObject *name = GETITEM(names, oparg);
2208 PyObject *owner = POP();
2209 int err;
2210 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2211 Py_DECREF(owner);
2212 if (err != 0)
2213 goto error;
2214 DISPATCH();
2215 }
2216
2217 TARGET(STORE_GLOBAL) {
2218 PyObject *name = GETITEM(names, oparg);
2219 PyObject *v = POP();
2220 int err;
2221 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002223 if (err != 0)
2224 goto error;
2225 DISPATCH();
2226 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002227
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002228 TARGET(DELETE_GLOBAL) {
2229 PyObject *name = GETITEM(names, oparg);
2230 int err;
2231 err = PyDict_DelItem(f->f_globals, name);
2232 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002233 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002234 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002235 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002236 }
2237 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002238 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002239
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002240 TARGET(LOAD_NAME) {
2241 PyObject *name = GETITEM(names, oparg);
2242 PyObject *locals = f->f_locals;
2243 PyObject *v;
2244 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002246 "no locals when loading %R", name);
2247 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002248 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002249 if (PyDict_CheckExact(locals)) {
2250 v = PyDict_GetItem(locals, name);
2251 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002252 }
2253 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002254 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002255 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002256 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2257 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002258 PyErr_Clear();
2259 }
2260 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002261 if (v == NULL) {
2262 v = PyDict_GetItem(f->f_globals, name);
2263 Py_XINCREF(v);
2264 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002265 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002266 v = PyDict_GetItem(f->f_builtins, name);
2267 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002268 format_exc_check_arg(
2269 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002270 NAME_ERROR_MSG, name);
2271 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002272 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002273 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002274 }
2275 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002276 v = PyObject_GetItem(f->f_builtins, name);
2277 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002278 if (PyErr_ExceptionMatches(PyExc_KeyError))
2279 format_exc_check_arg(
2280 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002281 NAME_ERROR_MSG, name);
2282 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002283 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002284 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002285 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002286 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002287 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002289 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002290
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002291 TARGET(LOAD_GLOBAL) {
2292 PyObject *name = GETITEM(names, oparg);
2293 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002294 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002295 && PyDict_CheckExact(f->f_builtins))
2296 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002297 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002298 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002299 name);
2300 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002301 if (!_PyErr_OCCURRED()) {
2302 /* _PyDict_LoadGlobal() returns NULL without raising
2303 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002304 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002305 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002306 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002307 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002309 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002311 else {
2312 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002313
2314 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002315 v = PyObject_GetItem(f->f_globals, name);
2316 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002317 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2318 goto error;
2319 PyErr_Clear();
2320
Victor Stinnerb4efc962015-11-20 09:24:02 +01002321 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002322 v = PyObject_GetItem(f->f_builtins, name);
2323 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002324 if (PyErr_ExceptionMatches(PyExc_KeyError))
2325 format_exc_check_arg(
2326 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002327 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002328 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002329 }
2330 }
2331 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002332 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002333 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002334 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002335
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002336 TARGET(DELETE_FAST) {
2337 PyObject *v = GETLOCAL(oparg);
2338 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002339 SETLOCAL(oparg, NULL);
2340 DISPATCH();
2341 }
2342 format_exc_check_arg(
2343 PyExc_UnboundLocalError,
2344 UNBOUNDLOCAL_ERROR_MSG,
2345 PyTuple_GetItem(co->co_varnames, oparg)
2346 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002347 goto error;
2348 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002349
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002350 TARGET(DELETE_DEREF) {
2351 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002352 PyObject *oldobj = PyCell_GET(cell);
2353 if (oldobj != NULL) {
2354 PyCell_SET(cell, NULL);
2355 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002356 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002357 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002358 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002359 goto error;
2360 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002361
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002362 TARGET(LOAD_CLOSURE) {
2363 PyObject *cell = freevars[oparg];
2364 Py_INCREF(cell);
2365 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002367 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002368
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002369 TARGET(LOAD_CLASSDEREF) {
2370 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002371 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002372 assert(locals);
2373 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2374 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2375 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2376 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2377 if (PyDict_CheckExact(locals)) {
2378 value = PyDict_GetItem(locals, name);
2379 Py_XINCREF(value);
2380 }
2381 else {
2382 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002383 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002384 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2385 goto error;
2386 PyErr_Clear();
2387 }
2388 }
2389 if (!value) {
2390 PyObject *cell = freevars[oparg];
2391 value = PyCell_GET(cell);
2392 if (value == NULL) {
2393 format_exc_unbound(co, oparg);
2394 goto error;
2395 }
2396 Py_INCREF(value);
2397 }
2398 PUSH(value);
2399 DISPATCH();
2400 }
2401
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002402 TARGET(LOAD_DEREF) {
2403 PyObject *cell = freevars[oparg];
2404 PyObject *value = PyCell_GET(cell);
2405 if (value == NULL) {
2406 format_exc_unbound(co, oparg);
2407 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002408 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002409 Py_INCREF(value);
2410 PUSH(value);
2411 DISPATCH();
2412 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002413
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002414 TARGET(STORE_DEREF) {
2415 PyObject *v = POP();
2416 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002417 PyObject *oldobj = PyCell_GET(cell);
2418 PyCell_SET(cell, v);
2419 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002420 DISPATCH();
2421 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002422
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002423 TARGET(BUILD_STRING) {
2424 PyObject *str;
2425 PyObject *empty = PyUnicode_New(0, 0);
2426 if (empty == NULL) {
2427 goto error;
2428 }
2429 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2430 Py_DECREF(empty);
2431 if (str == NULL)
2432 goto error;
2433 while (--oparg >= 0) {
2434 PyObject *item = POP();
2435 Py_DECREF(item);
2436 }
2437 PUSH(str);
2438 DISPATCH();
2439 }
2440
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002441 TARGET(BUILD_TUPLE) {
2442 PyObject *tup = PyTuple_New(oparg);
2443 if (tup == NULL)
2444 goto error;
2445 while (--oparg >= 0) {
2446 PyObject *item = POP();
2447 PyTuple_SET_ITEM(tup, oparg, item);
2448 }
2449 PUSH(tup);
2450 DISPATCH();
2451 }
2452
2453 TARGET(BUILD_LIST) {
2454 PyObject *list = PyList_New(oparg);
2455 if (list == NULL)
2456 goto error;
2457 while (--oparg >= 0) {
2458 PyObject *item = POP();
2459 PyList_SET_ITEM(list, oparg, item);
2460 }
2461 PUSH(list);
2462 DISPATCH();
2463 }
2464
Serhiy Storchaka73442852016-10-02 10:33:46 +03002465 TARGET(BUILD_TUPLE_UNPACK_WITH_CALL)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002466 TARGET(BUILD_TUPLE_UNPACK)
2467 TARGET(BUILD_LIST_UNPACK) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002468 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002469 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002470 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002471 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002472
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002473 if (sum == NULL)
2474 goto error;
2475
2476 for (i = oparg; i > 0; i--) {
2477 PyObject *none_val;
2478
2479 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2480 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002481 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
2482 PyErr_ExceptionMatches(PyExc_TypeError)) {
2483 PyObject *func = PEEK(1 + oparg);
2484 PyErr_Format(PyExc_TypeError,
2485 "%.200s%.200s argument after * "
2486 "must be an iterable, not %.200s",
2487 PyEval_GetFuncName(func),
2488 PyEval_GetFuncDesc(func),
2489 PEEK(i)->ob_type->tp_name);
2490 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002491 Py_DECREF(sum);
2492 goto error;
2493 }
2494 Py_DECREF(none_val);
2495 }
2496
2497 if (convert_to_tuple) {
2498 return_value = PyList_AsTuple(sum);
2499 Py_DECREF(sum);
2500 if (return_value == NULL)
2501 goto error;
2502 }
2503 else {
2504 return_value = sum;
2505 }
2506
2507 while (oparg--)
2508 Py_DECREF(POP());
2509 PUSH(return_value);
2510 DISPATCH();
2511 }
2512
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002513 TARGET(BUILD_SET) {
2514 PyObject *set = PySet_New(NULL);
2515 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002516 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002517 if (set == NULL)
2518 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002519 for (i = oparg; i > 0; i--) {
2520 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002521 if (err == 0)
2522 err = PySet_Add(set, item);
2523 Py_DECREF(item);
2524 }
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002525 STACKADJ(-oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002526 if (err != 0) {
2527 Py_DECREF(set);
2528 goto error;
2529 }
2530 PUSH(set);
2531 DISPATCH();
2532 }
2533
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002534 TARGET(BUILD_SET_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002535 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002536 PyObject *sum = PySet_New(NULL);
2537 if (sum == NULL)
2538 goto error;
2539
2540 for (i = oparg; i > 0; i--) {
2541 if (_PySet_Update(sum, PEEK(i)) < 0) {
2542 Py_DECREF(sum);
2543 goto error;
2544 }
2545 }
2546
2547 while (oparg--)
2548 Py_DECREF(POP());
2549 PUSH(sum);
2550 DISPATCH();
2551 }
2552
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002553 TARGET(BUILD_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002554 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002555 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2556 if (map == NULL)
2557 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002558 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002559 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002560 PyObject *key = PEEK(2*i);
2561 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002562 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002563 if (err != 0) {
2564 Py_DECREF(map);
2565 goto error;
2566 }
2567 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002568
2569 while (oparg--) {
2570 Py_DECREF(POP());
2571 Py_DECREF(POP());
2572 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002573 PUSH(map);
2574 DISPATCH();
2575 }
2576
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002577 TARGET(SETUP_ANNOTATIONS) {
2578 _Py_IDENTIFIER(__annotations__);
2579 int err;
2580 PyObject *ann_dict;
2581 if (f->f_locals == NULL) {
2582 PyErr_Format(PyExc_SystemError,
2583 "no locals found when setting up annotations");
2584 goto error;
2585 }
2586 /* check if __annotations__ in locals()... */
2587 if (PyDict_CheckExact(f->f_locals)) {
2588 ann_dict = _PyDict_GetItemId(f->f_locals,
2589 &PyId___annotations__);
2590 if (ann_dict == NULL) {
2591 /* ...if not, create a new one */
2592 ann_dict = PyDict_New();
2593 if (ann_dict == NULL) {
2594 goto error;
2595 }
2596 err = _PyDict_SetItemId(f->f_locals,
2597 &PyId___annotations__, ann_dict);
2598 Py_DECREF(ann_dict);
2599 if (err != 0) {
2600 goto error;
2601 }
2602 }
2603 }
2604 else {
2605 /* do the same if locals() is not a dict */
2606 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2607 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002608 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002609 }
2610 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2611 if (ann_dict == NULL) {
2612 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2613 goto error;
2614 }
2615 PyErr_Clear();
2616 ann_dict = PyDict_New();
2617 if (ann_dict == NULL) {
2618 goto error;
2619 }
2620 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2621 Py_DECREF(ann_dict);
2622 if (err != 0) {
2623 goto error;
2624 }
2625 }
2626 else {
2627 Py_DECREF(ann_dict);
2628 }
2629 }
2630 DISPATCH();
2631 }
2632
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002633 TARGET(BUILD_CONST_KEY_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002634 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002635 PyObject *map;
2636 PyObject *keys = TOP();
2637 if (!PyTuple_CheckExact(keys) ||
2638 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2639 PyErr_SetString(PyExc_SystemError,
2640 "bad BUILD_CONST_KEY_MAP keys argument");
2641 goto error;
2642 }
2643 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2644 if (map == NULL) {
2645 goto error;
2646 }
2647 for (i = oparg; i > 0; i--) {
2648 int err;
2649 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2650 PyObject *value = PEEK(i + 1);
2651 err = PyDict_SetItem(map, key, value);
2652 if (err != 0) {
2653 Py_DECREF(map);
2654 goto error;
2655 }
2656 }
2657
2658 Py_DECREF(POP());
2659 while (oparg--) {
2660 Py_DECREF(POP());
2661 }
2662 PUSH(map);
2663 DISPATCH();
2664 }
2665
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002666 TARGET(BUILD_MAP_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002667 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002668 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002669 if (sum == NULL)
2670 goto error;
2671
2672 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002673 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002674 if (PyDict_Update(sum, arg) < 0) {
2675 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2676 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002677 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002678 arg->ob_type->tp_name);
2679 }
2680 Py_DECREF(sum);
2681 goto error;
2682 }
2683 }
2684
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002685 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002686 Py_DECREF(POP());
2687 PUSH(sum);
2688 DISPATCH();
2689 }
2690
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002691 TARGET(BUILD_MAP_UNPACK_WITH_CALL) {
2692 Py_ssize_t i;
2693 PyObject *sum = PyDict_New();
2694 if (sum == NULL)
2695 goto error;
2696
2697 for (i = oparg; i > 0; i--) {
2698 PyObject *arg = PEEK(i);
2699 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
2700 PyObject *func = PEEK(2 + oparg);
2701 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2702 PyErr_Format(PyExc_TypeError,
2703 "%.200s%.200s argument after ** "
2704 "must be a mapping, not %.200s",
2705 PyEval_GetFuncName(func),
2706 PyEval_GetFuncDesc(func),
2707 arg->ob_type->tp_name);
2708 }
2709 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2710 PyObject *exc, *val, *tb;
2711 PyErr_Fetch(&exc, &val, &tb);
2712 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
2713 PyObject *key = PyTuple_GET_ITEM(val, 0);
2714 if (!PyUnicode_Check(key)) {
2715 PyErr_Format(PyExc_TypeError,
2716 "%.200s%.200s keywords must be strings",
2717 PyEval_GetFuncName(func),
2718 PyEval_GetFuncDesc(func));
2719 } else {
2720 PyErr_Format(PyExc_TypeError,
2721 "%.200s%.200s got multiple "
2722 "values for keyword argument '%U'",
2723 PyEval_GetFuncName(func),
2724 PyEval_GetFuncDesc(func),
2725 key);
2726 }
2727 Py_XDECREF(exc);
2728 Py_XDECREF(val);
2729 Py_XDECREF(tb);
2730 }
2731 else {
2732 PyErr_Restore(exc, val, tb);
2733 }
2734 }
2735 Py_DECREF(sum);
2736 goto error;
2737 }
2738 }
2739
2740 while (oparg--)
2741 Py_DECREF(POP());
2742 PUSH(sum);
2743 DISPATCH();
2744 }
2745
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002746 TARGET(MAP_ADD) {
2747 PyObject *key = TOP();
2748 PyObject *value = SECOND();
2749 PyObject *map;
2750 int err;
2751 STACKADJ(-2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002752 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002753 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002754 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002755 Py_DECREF(value);
2756 Py_DECREF(key);
2757 if (err != 0)
2758 goto error;
2759 PREDICT(JUMP_ABSOLUTE);
2760 DISPATCH();
2761 }
2762
2763 TARGET(LOAD_ATTR) {
2764 PyObject *name = GETITEM(names, oparg);
2765 PyObject *owner = TOP();
2766 PyObject *res = PyObject_GetAttr(owner, name);
2767 Py_DECREF(owner);
2768 SET_TOP(res);
2769 if (res == NULL)
2770 goto error;
2771 DISPATCH();
2772 }
2773
2774 TARGET(COMPARE_OP) {
2775 PyObject *right = POP();
2776 PyObject *left = TOP();
2777 PyObject *res = cmp_outcome(oparg, left, right);
2778 Py_DECREF(left);
2779 Py_DECREF(right);
2780 SET_TOP(res);
2781 if (res == NULL)
2782 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002783 PREDICT(POP_JUMP_IF_FALSE);
2784 PREDICT(POP_JUMP_IF_TRUE);
2785 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002786 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002787
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002788 TARGET(IMPORT_NAME) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002789 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002790 PyObject *fromlist = POP();
2791 PyObject *level = TOP();
2792 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002793 res = import_name(f, name, fromlist, level);
2794 Py_DECREF(level);
2795 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002796 SET_TOP(res);
2797 if (res == NULL)
2798 goto error;
2799 DISPATCH();
2800 }
2801
2802 TARGET(IMPORT_STAR) {
2803 PyObject *from = POP(), *locals;
2804 int err;
Victor Stinner41bb43a2013-10-29 01:19:37 +01002805 if (PyFrame_FastToLocalsWithError(f) < 0)
2806 goto error;
2807
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002808 locals = f->f_locals;
2809 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002810 PyErr_SetString(PyExc_SystemError,
2811 "no locals found during 'import *'");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002812 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002813 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002814 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002815 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002816 Py_DECREF(from);
2817 if (err != 0)
2818 goto error;
2819 DISPATCH();
2820 }
Guido van Rossum25831651993-05-19 14:50:45 +00002821
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002822 TARGET(IMPORT_FROM) {
2823 PyObject *name = GETITEM(names, oparg);
2824 PyObject *from = TOP();
2825 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002826 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002827 PUSH(res);
2828 if (res == NULL)
2829 goto error;
2830 DISPATCH();
2831 }
Thomas Wouters52152252000-08-17 22:55:00 +00002832
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002833 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002834 JUMPBY(oparg);
2835 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002836 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002837
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002838 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002839 TARGET(POP_JUMP_IF_FALSE) {
2840 PyObject *cond = POP();
2841 int err;
2842 if (cond == Py_True) {
2843 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002844 FAST_DISPATCH();
2845 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002846 if (cond == Py_False) {
2847 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002848 JUMPTO(oparg);
2849 FAST_DISPATCH();
2850 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002851 err = PyObject_IsTrue(cond);
2852 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 if (err > 0)
2854 err = 0;
2855 else if (err == 0)
2856 JUMPTO(oparg);
2857 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002858 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002859 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002860 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002861
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002862 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002863 TARGET(POP_JUMP_IF_TRUE) {
2864 PyObject *cond = POP();
2865 int err;
2866 if (cond == Py_False) {
2867 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 FAST_DISPATCH();
2869 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002870 if (cond == Py_True) {
2871 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 JUMPTO(oparg);
2873 FAST_DISPATCH();
2874 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002875 err = PyObject_IsTrue(cond);
2876 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002877 if (err > 0) {
2878 err = 0;
2879 JUMPTO(oparg);
2880 }
2881 else if (err == 0)
2882 ;
2883 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002884 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002885 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002886 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002887
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002888 TARGET(JUMP_IF_FALSE_OR_POP) {
2889 PyObject *cond = TOP();
2890 int err;
2891 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002892 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002893 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002894 FAST_DISPATCH();
2895 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002896 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002897 JUMPTO(oparg);
2898 FAST_DISPATCH();
2899 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002900 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 if (err > 0) {
2902 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002903 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002904 err = 0;
2905 }
2906 else if (err == 0)
2907 JUMPTO(oparg);
2908 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002909 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002910 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002911 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002912
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002913 TARGET(JUMP_IF_TRUE_OR_POP) {
2914 PyObject *cond = TOP();
2915 int err;
2916 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002918 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002919 FAST_DISPATCH();
2920 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002921 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002922 JUMPTO(oparg);
2923 FAST_DISPATCH();
2924 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002925 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002926 if (err > 0) {
2927 err = 0;
2928 JUMPTO(oparg);
2929 }
2930 else if (err == 0) {
2931 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002932 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002933 }
2934 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002935 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002937 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002938
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002939 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002940 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002941 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002942#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002943 /* Enabling this path speeds-up all while and for-loops by bypassing
2944 the per-loop checks for signals. By default, this should be turned-off
2945 because it prevents detection of a control-break in tight loops like
2946 "while 1: pass". Compile with this option turned-on when you need
2947 the speed-up and do not need break checking inside tight loops (ones
2948 that contain only instructions ending with FAST_DISPATCH).
2949 */
2950 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002951#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002952 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002953#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002954 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002955
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002956 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002957 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002958 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002959 PyObject *iter = PyObject_GetIter(iterable);
2960 Py_DECREF(iterable);
2961 SET_TOP(iter);
2962 if (iter == NULL)
2963 goto error;
2964 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002965 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002966 DISPATCH();
2967 }
2968
2969 TARGET(GET_YIELD_FROM_ITER) {
2970 /* before: [obj]; after [getiter(obj)] */
2971 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002972 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002973 if (PyCoro_CheckExact(iterable)) {
2974 /* `iterable` is a coroutine */
2975 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2976 /* and it is used in a 'yield from' expression of a
2977 regular generator. */
2978 Py_DECREF(iterable);
2979 SET_TOP(NULL);
2980 PyErr_SetString(PyExc_TypeError,
2981 "cannot 'yield from' a coroutine object "
2982 "in a non-coroutine generator");
2983 goto error;
2984 }
2985 }
2986 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002987 /* `iterable` is not a generator. */
2988 iter = PyObject_GetIter(iterable);
2989 Py_DECREF(iterable);
2990 SET_TOP(iter);
2991 if (iter == NULL)
2992 goto error;
2993 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002994 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002995 DISPATCH();
2996 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002997
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002998 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002999 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003000 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003001 PyObject *iter = TOP();
3002 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3003 if (next != NULL) {
3004 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003005 PREDICT(STORE_FAST);
3006 PREDICT(UNPACK_SEQUENCE);
3007 DISPATCH();
3008 }
3009 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003010 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
3011 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08003012 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003013 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003014 PyErr_Clear();
3015 }
3016 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003017 STACKADJ(-1);
3018 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003019 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003020 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003021 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003022 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003023
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003024 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003025 why = WHY_BREAK;
3026 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003027 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003028
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003029 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003030 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003031 if (retval == NULL)
3032 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003033 why = WHY_CONTINUE;
3034 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003035 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003036
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003037 TARGET(SETUP_LOOP)
3038 TARGET(SETUP_EXCEPT)
3039 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003040 /* NOTE: If you add any new block-setup opcodes that
3041 are not try/except/finally handlers, you may need
3042 to update the PyGen_NeedsFinalizing() function.
3043 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003045 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
3046 STACK_LEVEL());
3047 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003048 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003049
Yury Selivanov75445082015-05-11 22:57:16 -04003050 TARGET(BEFORE_ASYNC_WITH) {
3051 _Py_IDENTIFIER(__aexit__);
3052 _Py_IDENTIFIER(__aenter__);
3053
3054 PyObject *mgr = TOP();
3055 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3056 *enter;
3057 PyObject *res;
3058 if (exit == NULL)
3059 goto error;
3060 SET_TOP(exit);
3061 enter = special_lookup(mgr, &PyId___aenter__);
3062 Py_DECREF(mgr);
3063 if (enter == NULL)
3064 goto error;
3065 res = PyObject_CallFunctionObjArgs(enter, NULL);
3066 Py_DECREF(enter);
3067 if (res == NULL)
3068 goto error;
3069 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003070 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003071 DISPATCH();
3072 }
3073
3074 TARGET(SETUP_ASYNC_WITH) {
3075 PyObject *res = POP();
3076 /* Setup the finally block before pushing the result
3077 of __aenter__ on the stack. */
3078 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3079 STACK_LEVEL());
3080 PUSH(res);
3081 DISPATCH();
3082 }
3083
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003084 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003085 _Py_IDENTIFIER(__exit__);
3086 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003087 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08003088 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003089 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08003090 if (enter == NULL)
3091 goto error;
3092 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003093 if (exit == NULL) {
3094 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003095 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003096 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003097 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003098 Py_DECREF(mgr);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003099 res = PyObject_CallFunctionObjArgs(enter, NULL);
3100 Py_DECREF(enter);
3101 if (res == NULL)
3102 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003103 /* Setup the finally block before pushing the result
3104 of __enter__ on the stack. */
3105 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3106 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003107
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003108 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003109 DISPATCH();
3110 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003111
Yury Selivanov75445082015-05-11 22:57:16 -04003112 TARGET(WITH_CLEANUP_START) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04003113 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003114 how/why we entered the finally clause:
3115 - TOP = None
3116 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
3117 - TOP = WHY_*; no retval below it
3118 - (TOP, SECOND, THIRD) = exc_info()
3119 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
3120 Below them is EXIT, the context.__exit__ bound method.
3121 In the last case, we must call
3122 EXIT(TOP, SECOND, THIRD)
3123 otherwise we must call
3124 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003125
Benjamin Peterson8f169482013-10-29 22:25:06 -04003126 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003127 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04003128 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003129 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003131 In addition, if the stack represents an exception,
3132 *and* the function call returns a 'true' value, we
3133 push WHY_SILENCED onto the stack. END_FINALLY will
3134 then not re-raise the exception. (But non-local
3135 gotos should still be resumed.)
3136 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003138 PyObject *exit_func;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003139 PyObject *exc = TOP(), *val = Py_None, *tb = Py_None, *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003140 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003141 (void)POP();
3142 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003143 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003144 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003145 else if (PyLong_Check(exc)) {
3146 STACKADJ(-1);
3147 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003148 case WHY_RETURN:
3149 case WHY_CONTINUE:
3150 /* Retval in TOP. */
3151 exit_func = SECOND();
3152 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003153 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154 break;
3155 default:
3156 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003157 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003158 break;
3159 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003160 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003161 }
3162 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003163 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003165 val = SECOND();
3166 tb = THIRD();
3167 tp2 = FOURTH();
3168 exc2 = PEEK(5);
3169 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003170 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003171 SET_VALUE(7, tb2);
3172 SET_VALUE(6, exc2);
3173 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003174 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3175 SET_FOURTH(NULL);
3176 /* We just shifted the stack down, so we have
3177 to tell the except handler block that the
3178 values are lower than it expects. */
3179 block = &f->f_blockstack[f->f_iblock - 1];
3180 assert(block->b_type == EXCEPT_HANDLER);
3181 block->b_level--;
3182 }
3183 /* XXX Not the fastest way to call it... */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003184 res = PyObject_CallFunctionObjArgs(exit_func, exc, val, tb, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003185 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003186 if (res == NULL)
3187 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003188
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003189 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003190 PUSH(exc);
3191 PUSH(res);
3192 PREDICT(WITH_CLEANUP_FINISH);
3193 DISPATCH();
3194 }
3195
3196 PREDICTED(WITH_CLEANUP_FINISH);
3197 TARGET(WITH_CLEANUP_FINISH) {
3198 PyObject *res = POP();
3199 PyObject *exc = POP();
3200 int err;
3201
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003202 if (exc != Py_None)
3203 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003204 else
3205 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003206
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003207 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003208 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003210 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003211 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003212 else if (err > 0) {
3213 err = 0;
3214 /* There was an exception and a True return */
3215 PUSH(PyLong_FromLong((long) WHY_SILENCED));
3216 }
3217 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003218 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003219 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003220
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003221 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003222 TARGET(CALL_FUNCTION) {
3223 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003224 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003225 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003226 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003227 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003228 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003229 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003230 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003231 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003232 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003233
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003234 TARGET(CALL_FUNCTION_KW) {
3235 PyObject **sp, *res, *names;
3236
3237 names = POP();
3238 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003239 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003240 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003241 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003242 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003243 Py_DECREF(names);
3244
3245 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003246 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003247 }
3248 DISPATCH();
3249 }
3250
3251 TARGET(CALL_FUNCTION_EX) {
3252 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003253 if (oparg & 0x01) {
3254 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003255 if (!PyDict_CheckExact(kwargs)) {
3256 PyObject *d = PyDict_New();
3257 if (d == NULL)
3258 goto error;
3259 if (PyDict_Update(d, kwargs) != 0) {
3260 Py_DECREF(d);
3261 /* PyDict_Update raises attribute
3262 * error (percolated from an attempt
3263 * to get 'keys' attribute) instead of
3264 * a type error if its second argument
3265 * is not a mapping.
3266 */
3267 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3268 func = SECOND();
3269 PyErr_Format(PyExc_TypeError,
3270 "%.200s%.200s argument after ** "
3271 "must be a mapping, not %.200s",
3272 PyEval_GetFuncName(func),
3273 PyEval_GetFuncDesc(func),
3274 kwargs->ob_type->tp_name);
3275 }
Victor Stinnereece2222016-09-12 11:16:37 +02003276 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003277 goto error;
3278 }
3279 Py_DECREF(kwargs);
3280 kwargs = d;
3281 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003282 assert(PyDict_CheckExact(kwargs));
3283 }
3284 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003285 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003286 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003287 if (Py_TYPE(callargs)->tp_iter == NULL &&
3288 !PySequence_Check(callargs)) {
3289 PyErr_Format(PyExc_TypeError,
3290 "%.200s%.200s argument after * "
3291 "must be an iterable, not %.200s",
3292 PyEval_GetFuncName(func),
3293 PyEval_GetFuncDesc(func),
3294 callargs->ob_type->tp_name);
Victor Stinnereece2222016-09-12 11:16:37 +02003295 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003296 goto error;
3297 }
3298 Py_SETREF(callargs, PySequence_Tuple(callargs));
3299 if (callargs == NULL) {
3300 goto error;
3301 }
3302 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003303 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003304
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003305 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003306 Py_DECREF(func);
3307 Py_DECREF(callargs);
3308 Py_XDECREF(kwargs);
3309
3310 SET_TOP(result);
3311 if (result == NULL) {
3312 goto error;
3313 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003314 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003316
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003317 TARGET(MAKE_FUNCTION) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003318 PyObject *qualname = POP();
3319 PyObject *codeobj = POP();
3320 PyFunctionObject *func = (PyFunctionObject *)
3321 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003322
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003323 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003324 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003325 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003326 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003327 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003328
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003329 if (oparg & 0x08) {
3330 assert(PyTuple_CheckExact(TOP()));
3331 func ->func_closure = POP();
3332 }
3333 if (oparg & 0x04) {
3334 assert(PyDict_CheckExact(TOP()));
3335 func->func_annotations = POP();
3336 }
3337 if (oparg & 0x02) {
3338 assert(PyDict_CheckExact(TOP()));
3339 func->func_kwdefaults = POP();
3340 }
3341 if (oparg & 0x01) {
3342 assert(PyTuple_CheckExact(TOP()));
3343 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003344 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003345
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003346 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003347 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003348 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003349
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003350 TARGET(BUILD_SLICE) {
3351 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003352 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003353 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003354 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003355 step = NULL;
3356 stop = POP();
3357 start = TOP();
3358 slice = PySlice_New(start, stop, step);
3359 Py_DECREF(start);
3360 Py_DECREF(stop);
3361 Py_XDECREF(step);
3362 SET_TOP(slice);
3363 if (slice == NULL)
3364 goto error;
3365 DISPATCH();
3366 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003367
Eric V. Smitha78c7952015-11-03 12:45:05 -05003368 TARGET(FORMAT_VALUE) {
3369 /* Handles f-string value formatting. */
3370 PyObject *result;
3371 PyObject *fmt_spec;
3372 PyObject *value;
3373 PyObject *(*conv_fn)(PyObject *);
3374 int which_conversion = oparg & FVC_MASK;
3375 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3376
3377 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003378 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003379
3380 /* See if any conversion is specified. */
3381 switch (which_conversion) {
3382 case FVC_STR: conv_fn = PyObject_Str; break;
3383 case FVC_REPR: conv_fn = PyObject_Repr; break;
3384 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3385
3386 /* Must be 0 (meaning no conversion), since only four
3387 values are allowed by (oparg & FVC_MASK). */
3388 default: conv_fn = NULL; break;
3389 }
3390
3391 /* If there's a conversion function, call it and replace
3392 value with that result. Otherwise, just use value,
3393 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003394 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003395 result = conv_fn(value);
3396 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003397 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003398 Py_XDECREF(fmt_spec);
3399 goto error;
3400 }
3401 value = result;
3402 }
3403
3404 /* If value is a unicode object, and there's no fmt_spec,
3405 then we know the result of format(value) is value
3406 itself. In that case, skip calling format(). I plan to
3407 move this optimization in to PyObject_Format()
3408 itself. */
3409 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3410 /* Do nothing, just transfer ownership to result. */
3411 result = value;
3412 } else {
3413 /* Actually call format(). */
3414 result = PyObject_Format(value, fmt_spec);
3415 Py_DECREF(value);
3416 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003417 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003418 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003419 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003420 }
3421
Eric V. Smith135d5f42016-02-05 18:23:08 -05003422 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003423 DISPATCH();
3424 }
3425
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003426 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003427 int oldoparg = oparg;
3428 NEXTOPARG();
3429 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003430 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003431 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003432
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003433
Antoine Pitrou042b1282010-08-13 21:15:58 +00003434#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003435 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003436#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003437 default:
3438 fprintf(stderr,
3439 "XXX lineno: %d, opcode: %d\n",
3440 PyFrame_GetLineNumber(f),
3441 opcode);
3442 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003443 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003444
3445#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003446 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003447#endif
3448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003449 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003450
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003451 /* This should never be reached. Every opcode should end with DISPATCH()
3452 or goto error. */
3453 assert(0);
Guido van Rossumac7be682001-01-17 15:42:30 +00003454
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003455error:
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003456
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003457 assert(why == WHY_NOT);
3458 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003459
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003460 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003461#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003462 if (!PyErr_Occurred())
3463 PyErr_SetString(PyExc_SystemError,
3464 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003465#else
3466 assert(PyErr_Occurred());
3467#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003468
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003469 /* Log traceback info. */
3470 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003471
Benjamin Peterson51f46162013-01-23 08:38:47 -05003472 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003473 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3474 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003475
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003476fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003477 assert(why != WHY_NOT);
3478
3479 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003480 while (why != WHY_NOT && f->f_iblock > 0) {
3481 /* Peek at the current block. */
3482 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003484 assert(why != WHY_YIELD);
3485 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3486 why = WHY_NOT;
3487 JUMPTO(PyLong_AS_LONG(retval));
3488 Py_DECREF(retval);
3489 break;
3490 }
3491 /* Now we have to pop the block. */
3492 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003494 if (b->b_type == EXCEPT_HANDLER) {
3495 UNWIND_EXCEPT_HANDLER(b);
3496 continue;
3497 }
3498 UNWIND_BLOCK(b);
3499 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3500 why = WHY_NOT;
3501 JUMPTO(b->b_handler);
3502 break;
3503 }
3504 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3505 || b->b_type == SETUP_FINALLY)) {
3506 PyObject *exc, *val, *tb;
3507 int handler = b->b_handler;
3508 /* Beware, this invalidates all b->b_* fields */
3509 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3510 PUSH(tstate->exc_traceback);
3511 PUSH(tstate->exc_value);
3512 if (tstate->exc_type != NULL) {
3513 PUSH(tstate->exc_type);
3514 }
3515 else {
3516 Py_INCREF(Py_None);
3517 PUSH(Py_None);
3518 }
3519 PyErr_Fetch(&exc, &val, &tb);
3520 /* Make the raw exception data
3521 available to the handler,
3522 so a program can emulate the
3523 Python main loop. */
3524 PyErr_NormalizeException(
3525 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003526 if (tb != NULL)
3527 PyException_SetTraceback(val, tb);
3528 else
3529 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003530 Py_INCREF(exc);
3531 tstate->exc_type = exc;
3532 Py_INCREF(val);
3533 tstate->exc_value = val;
3534 tstate->exc_traceback = tb;
3535 if (tb == NULL)
3536 tb = Py_None;
3537 Py_INCREF(tb);
3538 PUSH(tb);
3539 PUSH(val);
3540 PUSH(exc);
3541 why = WHY_NOT;
3542 JUMPTO(handler);
3543 break;
3544 }
3545 if (b->b_type == SETUP_FINALLY) {
3546 if (why & (WHY_RETURN | WHY_CONTINUE))
3547 PUSH(retval);
3548 PUSH(PyLong_FromLong((long)why));
3549 why = WHY_NOT;
3550 JUMPTO(b->b_handler);
3551 break;
3552 }
3553 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003555 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003557 if (why != WHY_NOT)
3558 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00003559
Victor Stinnerace47d72013-07-18 01:41:08 +02003560 assert(!PyErr_Occurred());
3561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003562 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003564 assert(why != WHY_YIELD);
3565 /* Pop remaining stack entries. */
3566 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003567 PyObject *o = POP();
3568 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003569 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003571 if (why != WHY_RETURN)
3572 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003573
Victor Stinner4a7cc882015-03-06 23:35:27 +01003574 assert((retval != NULL) ^ (PyErr_Occurred() != NULL));
Victor Stinnerace47d72013-07-18 01:41:08 +02003575
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003576fast_yield:
Yury Selivanoveb636452016-09-08 22:01:51 -07003577 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01003578
Benjamin Petersonac913412011-07-03 16:25:11 -05003579 /* The purpose of this block is to put aside the generator's exception
3580 state and restore that of the calling frame. If the current
3581 exception state is from the caller, we clear the exception values
3582 on the generator frame, so they are not swapped back in latter. The
3583 origin of the current exception state is determined by checking for
3584 except handler blocks, which we must be in iff a new exception
3585 state came into existence in this frame. (An uncaught exception
3586 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3587 int i;
Victor Stinner74319ae2016-08-25 00:04:09 +02003588 for (i = 0; i < f->f_iblock; i++) {
3589 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER) {
Benjamin Petersonac913412011-07-03 16:25:11 -05003590 break;
Victor Stinner74319ae2016-08-25 00:04:09 +02003591 }
3592 }
Benjamin Petersonac913412011-07-03 16:25:11 -05003593 if (i == f->f_iblock)
3594 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003595 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003596 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003597 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003598 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003600 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003601 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003602 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003603 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3604 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003605 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003606 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003607 why = WHY_EXCEPTION;
3608 }
3609 }
3610 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003611 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3612 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003613 PyTrace_RETURN, NULL);
3614 }
3615 }
3616 if (tstate->c_profilefunc) {
3617 if (why == WHY_EXCEPTION)
3618 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003619 tstate->c_profileobj,
3620 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003621 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003622 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3623 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003624 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003625 Py_CLEAR(retval);
Brett Cannonb94767f2011-02-22 20:15:44 +00003626 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003627 }
3628 }
3629 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003631 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003632exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003633 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3634 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003635 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003636 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003637 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003638
Victor Stinnerefde1462015-03-21 15:04:43 +01003639 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003640}
3641
Benjamin Petersonb204a422011-06-05 22:04:07 -05003642static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003643format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3644{
3645 int err;
3646 Py_ssize_t len = PyList_GET_SIZE(names);
3647 PyObject *name_str, *comma, *tail, *tmp;
3648
3649 assert(PyList_CheckExact(names));
3650 assert(len >= 1);
3651 /* Deal with the joys of natural language. */
3652 switch (len) {
3653 case 1:
3654 name_str = PyList_GET_ITEM(names, 0);
3655 Py_INCREF(name_str);
3656 break;
3657 case 2:
3658 name_str = PyUnicode_FromFormat("%U and %U",
3659 PyList_GET_ITEM(names, len - 2),
3660 PyList_GET_ITEM(names, len - 1));
3661 break;
3662 default:
3663 tail = PyUnicode_FromFormat(", %U, and %U",
3664 PyList_GET_ITEM(names, len - 2),
3665 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003666 if (tail == NULL)
3667 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003668 /* Chop off the last two objects in the list. This shouldn't actually
3669 fail, but we can't be too careful. */
3670 err = PyList_SetSlice(names, len - 2, len, NULL);
3671 if (err == -1) {
3672 Py_DECREF(tail);
3673 return;
3674 }
3675 /* Stitch everything up into a nice comma-separated list. */
3676 comma = PyUnicode_FromString(", ");
3677 if (comma == NULL) {
3678 Py_DECREF(tail);
3679 return;
3680 }
3681 tmp = PyUnicode_Join(comma, names);
3682 Py_DECREF(comma);
3683 if (tmp == NULL) {
3684 Py_DECREF(tail);
3685 return;
3686 }
3687 name_str = PyUnicode_Concat(tmp, tail);
3688 Py_DECREF(tmp);
3689 Py_DECREF(tail);
3690 break;
3691 }
3692 if (name_str == NULL)
3693 return;
3694 PyErr_Format(PyExc_TypeError,
3695 "%U() missing %i required %s argument%s: %U",
3696 co->co_name,
3697 len,
3698 kind,
3699 len == 1 ? "" : "s",
3700 name_str);
3701 Py_DECREF(name_str);
3702}
3703
3704static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003705missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003706 PyObject **fastlocals)
3707{
Victor Stinner74319ae2016-08-25 00:04:09 +02003708 Py_ssize_t i, j = 0;
3709 Py_ssize_t start, end;
3710 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003711 const char *kind = positional ? "positional" : "keyword-only";
3712 PyObject *missing_names;
3713
3714 /* Compute the names of the arguments that are missing. */
3715 missing_names = PyList_New(missing);
3716 if (missing_names == NULL)
3717 return;
3718 if (positional) {
3719 start = 0;
3720 end = co->co_argcount - defcount;
3721 }
3722 else {
3723 start = co->co_argcount;
3724 end = start + co->co_kwonlyargcount;
3725 }
3726 for (i = start; i < end; i++) {
3727 if (GETLOCAL(i) == NULL) {
3728 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3729 PyObject *name = PyObject_Repr(raw);
3730 if (name == NULL) {
3731 Py_DECREF(missing_names);
3732 return;
3733 }
3734 PyList_SET_ITEM(missing_names, j++, name);
3735 }
3736 }
3737 assert(j == missing);
3738 format_missing(kind, co, missing_names);
3739 Py_DECREF(missing_names);
3740}
3741
3742static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003743too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3744 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003745{
3746 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003747 Py_ssize_t kwonly_given = 0;
3748 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003749 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003750 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003751
Benjamin Petersone109c702011-06-24 09:37:26 -05003752 assert((co->co_flags & CO_VARARGS) == 0);
3753 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003754 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3755 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003756 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003757 }
3758 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003759 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003760 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003761 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003762 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003763 }
3764 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003765 plural = (co_argcount != 1);
3766 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003767 }
3768 if (sig == NULL)
3769 return;
3770 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003771 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3772 kwonly_sig = PyUnicode_FromFormat(format,
3773 given != 1 ? "s" : "",
3774 kwonly_given,
3775 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003776 if (kwonly_sig == NULL) {
3777 Py_DECREF(sig);
3778 return;
3779 }
3780 }
3781 else {
3782 /* This will not fail. */
3783 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003784 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003785 }
3786 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003787 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003788 co->co_name,
3789 sig,
3790 plural ? "s" : "",
3791 given,
3792 kwonly_sig,
3793 given == 1 && !kwonly_given ? "was" : "were");
3794 Py_DECREF(sig);
3795 Py_DECREF(kwonly_sig);
3796}
3797
Guido van Rossumc2e20742006-02-27 22:32:47 +00003798/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003799 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003800 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003801
Victor Stinner40ee3012014-06-16 15:59:28 +02003802static PyObject *
3803_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Victor Stinner74319ae2016-08-25 00:04:09 +02003804 PyObject **args, Py_ssize_t argcount,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003805 PyObject **kwnames, PyObject **kwargs,
3806 Py_ssize_t kwcount, int kwstep,
Victor Stinner74319ae2016-08-25 00:04:09 +02003807 PyObject **defs, Py_ssize_t defcount,
3808 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003809 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003810{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003811 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003812 PyFrameObject *f;
3813 PyObject *retval = NULL;
3814 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003815 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003816 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003817 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3818 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003819 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003821 if (globals == NULL) {
3822 PyErr_SetString(PyExc_SystemError,
3823 "PyEval_EvalCodeEx: NULL globals");
3824 return NULL;
3825 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003826
Victor Stinnerc7020012016-08-16 23:40:29 +02003827 /* Create the frame */
3828 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003829 assert(tstate != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003830 f = PyFrame_New(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003831 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003832 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003833 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003834 fastlocals = f->f_localsplus;
3835 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003836
Victor Stinnerc7020012016-08-16 23:40:29 +02003837 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003838 if (co->co_flags & CO_VARKEYWORDS) {
3839 kwdict = PyDict_New();
3840 if (kwdict == NULL)
3841 goto fail;
3842 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003843 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003844 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003845 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003846 SETLOCAL(i, kwdict);
3847 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003848 else {
3849 kwdict = NULL;
3850 }
3851
3852 /* Copy positional arguments into local variables */
3853 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003854 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003855 }
3856 else {
3857 n = argcount;
3858 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003859 for (i = 0; i < n; i++) {
3860 x = args[i];
3861 Py_INCREF(x);
3862 SETLOCAL(i, x);
3863 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003864
3865 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003866 if (co->co_flags & CO_VARARGS) {
3867 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003868 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003869 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003870 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003871 SETLOCAL(total_args, u);
3872 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003873 x = args[i];
3874 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003875 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003876 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003877 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003878
Serhiy Storchakab7281052016-09-12 00:52:40 +03003879 /* Handle keyword arguments passed as two strided arrays */
3880 kwcount *= kwstep;
3881 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003882 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003883 PyObject *keyword = kwnames[i];
3884 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003885 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003886
Benjamin Petersonb204a422011-06-05 22:04:07 -05003887 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3888 PyErr_Format(PyExc_TypeError,
3889 "%U() keywords must be strings",
3890 co->co_name);
3891 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003892 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003893
Benjamin Petersonb204a422011-06-05 22:04:07 -05003894 /* Speed hack: do raw pointer compares. As names are
3895 normally interned this should almost always hit. */
3896 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3897 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003898 PyObject *name = co_varnames[j];
3899 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003900 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003901 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003902 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003903
Benjamin Petersonb204a422011-06-05 22:04:07 -05003904 /* Slow fallback, just in case */
3905 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003906 PyObject *name = co_varnames[j];
3907 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3908 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003909 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003910 }
3911 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003912 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003913 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003914 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003915
Benjamin Petersonb204a422011-06-05 22:04:07 -05003916 if (j >= total_args && kwdict == NULL) {
3917 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003918 "%U() got an unexpected keyword argument '%S'",
3919 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003920 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003921 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003922
Christian Heimes0bd447f2013-07-20 14:48:10 +02003923 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3924 goto fail;
3925 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003926 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003927
Benjamin Petersonb204a422011-06-05 22:04:07 -05003928 kw_found:
3929 if (GETLOCAL(j) != NULL) {
3930 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003931 "%U() got multiple values for argument '%S'",
3932 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003933 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003934 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003935 Py_INCREF(value);
3936 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003937 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003938
3939 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003940 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003941 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003942 goto fail;
3943 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003944
3945 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003946 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02003947 Py_ssize_t m = co->co_argcount - defcount;
3948 Py_ssize_t missing = 0;
3949 for (i = argcount; i < m; i++) {
3950 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003951 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02003952 }
3953 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003954 if (missing) {
3955 missing_arguments(co, missing, defcount, fastlocals);
3956 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003957 }
3958 if (n > m)
3959 i = n - m;
3960 else
3961 i = 0;
3962 for (; i < defcount; i++) {
3963 if (GETLOCAL(m+i) == NULL) {
3964 PyObject *def = defs[i];
3965 Py_INCREF(def);
3966 SETLOCAL(m+i, def);
3967 }
3968 }
3969 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003970
3971 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003972 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02003973 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003974 for (i = co->co_argcount; i < total_args; i++) {
3975 PyObject *name;
3976 if (GETLOCAL(i) != NULL)
3977 continue;
3978 name = PyTuple_GET_ITEM(co->co_varnames, i);
3979 if (kwdefs != NULL) {
3980 PyObject *def = PyDict_GetItem(kwdefs, name);
3981 if (def) {
3982 Py_INCREF(def);
3983 SETLOCAL(i, def);
3984 continue;
3985 }
3986 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003987 missing++;
3988 }
3989 if (missing) {
3990 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003991 goto fail;
3992 }
3993 }
3994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003995 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003996 vars into frame. */
3997 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003998 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05003999 int arg;
4000 /* Possibly account for the cell variable being an argument. */
4001 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004002 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004003 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004004 /* Clear the local copy. */
4005 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004006 }
4007 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004008 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004009 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004010 if (c == NULL)
4011 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004012 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004013 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004014
4015 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004016 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4017 PyObject *o = PyTuple_GET_ITEM(closure, i);
4018 Py_INCREF(o);
4019 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004020 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004021
Yury Selivanoveb636452016-09-08 22:01:51 -07004022 /* Handle generator/coroutine/asynchronous generator */
4023 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004024 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04004025 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004026 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004027
4028 if (is_coro && tstate->in_coroutine_wrapper) {
4029 assert(coro_wrapper != NULL);
4030 PyErr_Format(PyExc_RuntimeError,
4031 "coroutine wrapper %.200R attempted "
4032 "to recursively wrap %.200R",
4033 coro_wrapper,
4034 co);
4035 goto fail;
4036 }
Yury Selivanov75445082015-05-11 22:57:16 -04004037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004038 /* Don't need to keep the reference to f_back, it will be set
4039 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004040 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004042 /* Create a new generator that owns the ready to run frame
4043 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004044 if (is_coro) {
4045 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004046 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4047 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004048 } else {
4049 gen = PyGen_NewWithQualName(f, name, qualname);
4050 }
Yury Selivanov75445082015-05-11 22:57:16 -04004051 if (gen == NULL)
4052 return NULL;
4053
Yury Selivanov94c22632015-06-04 10:16:51 -04004054 if (is_coro && coro_wrapper != NULL) {
4055 PyObject *wrapped;
4056 tstate->in_coroutine_wrapper = 1;
4057 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4058 tstate->in_coroutine_wrapper = 0;
4059 return wrapped;
4060 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004061
Yury Selivanov75445082015-05-11 22:57:16 -04004062 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004063 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004065 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004066
Thomas Woutersce272b62007-09-19 21:19:28 +00004067fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004069 /* decref'ing the frame can cause __del__ methods to get invoked,
4070 which can call back into Python. While we're done with the
4071 current Python frame (f), the associated C stack is still in use,
4072 so recursion_depth must be boosted for the duration.
4073 */
4074 assert(tstate != NULL);
4075 ++tstate->recursion_depth;
4076 Py_DECREF(f);
4077 --tstate->recursion_depth;
4078 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004079}
4080
Victor Stinner40ee3012014-06-16 15:59:28 +02004081PyObject *
4082PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
4083 PyObject **args, int argcount, PyObject **kws, int kwcount,
4084 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
4085{
4086 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004087 args, argcount,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004088 kws, kws + 1, kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004089 defs, defcount,
4090 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004091 NULL, NULL);
4092}
Tim Peters5ca576e2001-06-18 22:08:13 +00004093
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004094static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004095special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004096{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004097 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004098 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004099 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004100 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004101 return NULL;
4102 }
4103 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004104}
4105
4106
Benjamin Peterson87880242011-07-03 16:48:31 -05004107/* These 3 functions deal with the exception state of generators. */
4108
4109static void
4110save_exc_state(PyThreadState *tstate, PyFrameObject *f)
4111{
4112 PyObject *type, *value, *traceback;
4113 Py_XINCREF(tstate->exc_type);
4114 Py_XINCREF(tstate->exc_value);
4115 Py_XINCREF(tstate->exc_traceback);
4116 type = f->f_exc_type;
4117 value = f->f_exc_value;
4118 traceback = f->f_exc_traceback;
4119 f->f_exc_type = tstate->exc_type;
4120 f->f_exc_value = tstate->exc_value;
4121 f->f_exc_traceback = tstate->exc_traceback;
4122 Py_XDECREF(type);
4123 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004124 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05004125}
4126
4127static void
4128swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
4129{
4130 PyObject *tmp;
4131 tmp = tstate->exc_type;
4132 tstate->exc_type = f->f_exc_type;
4133 f->f_exc_type = tmp;
4134 tmp = tstate->exc_value;
4135 tstate->exc_value = f->f_exc_value;
4136 f->f_exc_value = tmp;
4137 tmp = tstate->exc_traceback;
4138 tstate->exc_traceback = f->f_exc_traceback;
4139 f->f_exc_traceback = tmp;
4140}
4141
4142static void
4143restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
4144{
4145 PyObject *type, *value, *tb;
4146 type = tstate->exc_type;
4147 value = tstate->exc_value;
4148 tb = tstate->exc_traceback;
4149 tstate->exc_type = f->f_exc_type;
4150 tstate->exc_value = f->f_exc_value;
4151 tstate->exc_traceback = f->f_exc_traceback;
4152 f->f_exc_type = NULL;
4153 f->f_exc_value = NULL;
4154 f->f_exc_traceback = NULL;
4155 Py_XDECREF(type);
4156 Py_XDECREF(value);
4157 Py_XDECREF(tb);
4158}
4159
4160
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004161/* Logic for the raise statement (too complicated for inlining).
4162 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004163static int
Collin Winter828f04a2007-08-31 00:04:24 +00004164do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004166 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004168 if (exc == NULL) {
4169 /* Reraise */
4170 PyThreadState *tstate = PyThreadState_GET();
4171 PyObject *tb;
4172 type = tstate->exc_type;
4173 value = tstate->exc_value;
4174 tb = tstate->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004175 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004176 PyErr_SetString(PyExc_RuntimeError,
4177 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004178 return 0;
4179 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004180 Py_XINCREF(type);
4181 Py_XINCREF(value);
4182 Py_XINCREF(tb);
4183 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004184 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004185 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004186
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004187 /* We support the following forms of raise:
4188 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004189 raise <instance>
4190 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004192 if (PyExceptionClass_Check(exc)) {
4193 type = exc;
4194 value = PyObject_CallObject(exc, NULL);
4195 if (value == NULL)
4196 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004197 if (!PyExceptionInstance_Check(value)) {
4198 PyErr_Format(PyExc_TypeError,
4199 "calling %R should have returned an instance of "
4200 "BaseException, not %R",
4201 type, Py_TYPE(value));
4202 goto raise_error;
4203 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004204 }
4205 else if (PyExceptionInstance_Check(exc)) {
4206 value = exc;
4207 type = PyExceptionInstance_Class(exc);
4208 Py_INCREF(type);
4209 }
4210 else {
4211 /* Not something you can raise. You get an exception
4212 anyway, just not what you specified :-) */
4213 Py_DECREF(exc);
4214 PyErr_SetString(PyExc_TypeError,
4215 "exceptions must derive from BaseException");
4216 goto raise_error;
4217 }
Collin Winter828f04a2007-08-31 00:04:24 +00004218
Serhiy Storchakac0191582016-09-27 11:37:10 +03004219 assert(type != NULL);
4220 assert(value != NULL);
4221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004222 if (cause) {
4223 PyObject *fixed_cause;
4224 if (PyExceptionClass_Check(cause)) {
4225 fixed_cause = PyObject_CallObject(cause, NULL);
4226 if (fixed_cause == NULL)
4227 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004228 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004229 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004230 else if (PyExceptionInstance_Check(cause)) {
4231 fixed_cause = cause;
4232 }
4233 else if (cause == Py_None) {
4234 Py_DECREF(cause);
4235 fixed_cause = NULL;
4236 }
4237 else {
4238 PyErr_SetString(PyExc_TypeError,
4239 "exception causes must derive from "
4240 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004241 goto raise_error;
4242 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004243 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004244 }
Collin Winter828f04a2007-08-31 00:04:24 +00004245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004246 PyErr_SetObject(type, value);
4247 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004248 Py_DECREF(value);
4249 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004250 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004251
4252raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004253 Py_XDECREF(value);
4254 Py_XDECREF(type);
4255 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004256 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004257}
4258
Tim Petersd6d010b2001-06-21 02:49:55 +00004259/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004260 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004261
Guido van Rossum0368b722007-05-11 16:50:42 +00004262 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4263 with a variable target.
4264*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004265
Barry Warsawe42b18f1997-08-25 22:13:04 +00004266static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004267unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004268{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004269 int i = 0, j = 0;
4270 Py_ssize_t ll = 0;
4271 PyObject *it; /* iter(v) */
4272 PyObject *w;
4273 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004275 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004277 it = PyObject_GetIter(v);
4278 if (it == NULL)
4279 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00004280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004281 for (; i < argcnt; i++) {
4282 w = PyIter_Next(it);
4283 if (w == NULL) {
4284 /* Iterator done, via error or exhaustion. */
4285 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004286 if (argcntafter == -1) {
4287 PyErr_Format(PyExc_ValueError,
4288 "not enough values to unpack (expected %d, got %d)",
4289 argcnt, i);
4290 }
4291 else {
4292 PyErr_Format(PyExc_ValueError,
4293 "not enough values to unpack "
4294 "(expected at least %d, got %d)",
4295 argcnt + argcntafter, i);
4296 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004297 }
4298 goto Error;
4299 }
4300 *--sp = w;
4301 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004303 if (argcntafter == -1) {
4304 /* We better have exhausted the iterator now. */
4305 w = PyIter_Next(it);
4306 if (w == NULL) {
4307 if (PyErr_Occurred())
4308 goto Error;
4309 Py_DECREF(it);
4310 return 1;
4311 }
4312 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004313 PyErr_Format(PyExc_ValueError,
4314 "too many values to unpack (expected %d)",
4315 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004316 goto Error;
4317 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004319 l = PySequence_List(it);
4320 if (l == NULL)
4321 goto Error;
4322 *--sp = l;
4323 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004325 ll = PyList_GET_SIZE(l);
4326 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004327 PyErr_Format(PyExc_ValueError,
4328 "not enough values to unpack (expected at least %d, got %zd)",
4329 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004330 goto Error;
4331 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004333 /* Pop the "after-variable" args off the list. */
4334 for (j = argcntafter; j > 0; j--, i++) {
4335 *--sp = PyList_GET_ITEM(l, ll - j);
4336 }
4337 /* Resize the list. */
4338 Py_SIZE(l) = ll - argcntafter;
4339 Py_DECREF(it);
4340 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004341
Tim Petersd6d010b2001-06-21 02:49:55 +00004342Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004343 for (; i > 0; i--, sp++)
4344 Py_DECREF(*sp);
4345 Py_XDECREF(it);
4346 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004347}
4348
4349
Guido van Rossum96a42c81992-01-12 02:29:51 +00004350#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004351static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004352prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004353{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004354 printf("%s ", str);
4355 if (PyObject_Print(v, stdout, 0) != 0)
4356 PyErr_Clear(); /* Don't know what else to do */
4357 printf("\n");
4358 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004359}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004360#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004361
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004362static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004363call_exc_trace(Py_tracefunc func, PyObject *self,
4364 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004365{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004366 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004367 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004368 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004369 if (value == NULL) {
4370 value = Py_None;
4371 Py_INCREF(value);
4372 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004373 PyErr_NormalizeException(&type, &value, &orig_traceback);
4374 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004375 arg = PyTuple_Pack(3, type, value, traceback);
4376 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004377 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004378 return;
4379 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004380 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004381 Py_DECREF(arg);
4382 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004383 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004384 else {
4385 Py_XDECREF(type);
4386 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004387 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004388 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004389}
4390
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004391static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004392call_trace_protected(Py_tracefunc func, PyObject *obj,
4393 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004394 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004396 PyObject *type, *value, *traceback;
4397 int err;
4398 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004399 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004400 if (err == 0)
4401 {
4402 PyErr_Restore(type, value, traceback);
4403 return 0;
4404 }
4405 else {
4406 Py_XDECREF(type);
4407 Py_XDECREF(value);
4408 Py_XDECREF(traceback);
4409 return -1;
4410 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004411}
4412
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004413static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004414call_trace(Py_tracefunc func, PyObject *obj,
4415 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004416 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004417{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004418 int result;
4419 if (tstate->tracing)
4420 return 0;
4421 tstate->tracing++;
4422 tstate->use_tracing = 0;
4423 result = func(obj, frame, what, arg);
4424 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4425 || (tstate->c_profilefunc != NULL));
4426 tstate->tracing--;
4427 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004428}
4429
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004430PyObject *
4431_PyEval_CallTracing(PyObject *func, PyObject *args)
4432{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004433 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004434 int save_tracing = tstate->tracing;
4435 int save_use_tracing = tstate->use_tracing;
4436 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004438 tstate->tracing = 0;
4439 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4440 || (tstate->c_profilefunc != NULL));
4441 result = PyObject_Call(func, args, NULL);
4442 tstate->tracing = save_tracing;
4443 tstate->use_tracing = save_use_tracing;
4444 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004445}
4446
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004447/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004448static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004449maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004450 PyThreadState *tstate, PyFrameObject *frame,
4451 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004452{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004453 int result = 0;
4454 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004456 /* If the last instruction executed isn't in the current
4457 instruction window, reset the window.
4458 */
4459 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4460 PyAddrPair bounds;
4461 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4462 &bounds);
4463 *instr_lb = bounds.ap_lower;
4464 *instr_ub = bounds.ap_upper;
4465 }
4466 /* If the last instruction falls at the start of a line or if
4467 it represents a jump backwards, update the frame's line
4468 number and call the trace function. */
4469 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
4470 frame->f_lineno = line;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004471 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004472 }
4473 *instr_prev = frame->f_lasti;
4474 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004475}
4476
Fred Drake5755ce62001-06-27 19:19:46 +00004477void
4478PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004479{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004480 PyThreadState *tstate = PyThreadState_GET();
4481 PyObject *temp = tstate->c_profileobj;
4482 Py_XINCREF(arg);
4483 tstate->c_profilefunc = NULL;
4484 tstate->c_profileobj = NULL;
4485 /* Must make sure that tracing is not ignored if 'temp' is freed */
4486 tstate->use_tracing = tstate->c_tracefunc != NULL;
4487 Py_XDECREF(temp);
4488 tstate->c_profilefunc = func;
4489 tstate->c_profileobj = arg;
4490 /* Flag that tracing or profiling is turned on */
4491 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004492}
4493
4494void
4495PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4496{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004497 PyThreadState *tstate = PyThreadState_GET();
4498 PyObject *temp = tstate->c_traceobj;
4499 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4500 Py_XINCREF(arg);
4501 tstate->c_tracefunc = NULL;
4502 tstate->c_traceobj = NULL;
4503 /* Must make sure that profiling is not ignored if 'temp' is freed */
4504 tstate->use_tracing = tstate->c_profilefunc != NULL;
4505 Py_XDECREF(temp);
4506 tstate->c_tracefunc = func;
4507 tstate->c_traceobj = arg;
4508 /* Flag that tracing or profiling is turned on */
4509 tstate->use_tracing = ((func != NULL)
4510 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004511}
4512
Yury Selivanov75445082015-05-11 22:57:16 -04004513void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004514_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004515{
4516 PyThreadState *tstate = PyThreadState_GET();
4517
Yury Selivanov75445082015-05-11 22:57:16 -04004518 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004519 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004520}
4521
4522PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004523_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004524{
4525 PyThreadState *tstate = PyThreadState_GET();
4526 return tstate->coroutine_wrapper;
4527}
4528
Yury Selivanoveb636452016-09-08 22:01:51 -07004529void
4530_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4531{
4532 PyThreadState *tstate = PyThreadState_GET();
4533
4534 Py_XINCREF(firstiter);
4535 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4536}
4537
4538PyObject *
4539_PyEval_GetAsyncGenFirstiter(void)
4540{
4541 PyThreadState *tstate = PyThreadState_GET();
4542 return tstate->async_gen_firstiter;
4543}
4544
4545void
4546_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4547{
4548 PyThreadState *tstate = PyThreadState_GET();
4549
4550 Py_XINCREF(finalizer);
4551 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4552}
4553
4554PyObject *
4555_PyEval_GetAsyncGenFinalizer(void)
4556{
4557 PyThreadState *tstate = PyThreadState_GET();
4558 return tstate->async_gen_finalizer;
4559}
4560
Guido van Rossumb209a111997-04-29 18:18:01 +00004561PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004562PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004563{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004564 PyFrameObject *current_frame = PyEval_GetFrame();
4565 if (current_frame == NULL)
4566 return PyThreadState_GET()->interp->builtins;
4567 else
4568 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004569}
4570
Guido van Rossumb209a111997-04-29 18:18:01 +00004571PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004572PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004573{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004574 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004575 if (current_frame == NULL) {
4576 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004577 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004578 }
4579
4580 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4581 return NULL;
4582
4583 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004584 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004585}
4586
Guido van Rossumb209a111997-04-29 18:18:01 +00004587PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004588PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004589{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004590 PyFrameObject *current_frame = PyEval_GetFrame();
4591 if (current_frame == NULL)
4592 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004593
4594 assert(current_frame->f_globals != NULL);
4595 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004596}
4597
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004598PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004599PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004600{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004601 PyThreadState *tstate = PyThreadState_GET();
4602 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004603}
4604
Guido van Rossum6135a871995-01-09 17:53:26 +00004605int
Tim Peters5ba58662001-07-16 02:29:45 +00004606PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004607{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004608 PyFrameObject *current_frame = PyEval_GetFrame();
4609 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004611 if (current_frame != NULL) {
4612 const int codeflags = current_frame->f_code->co_flags;
4613 const int compilerflags = codeflags & PyCF_MASK;
4614 if (compilerflags) {
4615 result = 1;
4616 cf->cf_flags |= compilerflags;
4617 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004618#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004619 if (codeflags & CO_GENERATOR_ALLOWED) {
4620 result = 1;
4621 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4622 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004623#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004624 }
4625 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004626}
4627
Guido van Rossum3f5da241990-12-20 15:06:42 +00004628
Guido van Rossum681d79a1995-07-18 14:51:37 +00004629/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00004630 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00004631
Guido van Rossumb209a111997-04-29 18:18:01 +00004632PyObject *
Victor Stinner8a31c822016-08-19 17:12:23 +02004633PyEval_CallObjectWithKeywords(PyObject *func, PyObject *args, PyObject *kwargs)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004634{
Victor Stinner59b356d2015-03-16 11:52:32 +01004635#ifdef Py_DEBUG
4636 /* PyEval_CallObjectWithKeywords() must not be called with an exception
4637 set. It raises a new exception if parameters are invalid or if
4638 PyTuple_New() fails, and so the original exception is lost. */
4639 assert(!PyErr_Occurred());
4640#endif
4641
Victor Stinner8a31c822016-08-19 17:12:23 +02004642 if (args == NULL) {
Victor Stinner155ea652016-08-22 23:26:00 +02004643 return _PyObject_FastCallDict(func, NULL, 0, kwargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004644 }
Victor Stinner155ea652016-08-22 23:26:00 +02004645
4646 if (!PyTuple_Check(args)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004647 PyErr_SetString(PyExc_TypeError,
4648 "argument list must be a tuple");
4649 return NULL;
4650 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00004651
Victor Stinner8a31c822016-08-19 17:12:23 +02004652 if (kwargs != NULL && !PyDict_Check(kwargs)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004653 PyErr_SetString(PyExc_TypeError,
4654 "keyword list must be a dictionary");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004655 return NULL;
4656 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00004657
Victor Stinner6e2333d2016-08-23 00:25:01 +02004658 return PyObject_Call(func, args, kwargs);
Jeremy Hylton52820442001-01-03 23:52:36 +00004659}
4660
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004661const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004662PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004663{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004664 if (PyMethod_Check(func))
4665 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4666 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004667 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004668 else if (PyCFunction_Check(func))
4669 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4670 else
4671 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004672}
4673
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004674const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004675PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004676{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004677 if (PyMethod_Check(func))
4678 return "()";
4679 else if (PyFunction_Check(func))
4680 return "()";
4681 else if (PyCFunction_Check(func))
4682 return "()";
4683 else
4684 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004685}
4686
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004687#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004688if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004689 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4690 tstate, tstate->frame, \
4691 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004692 x = NULL; \
4693 } \
4694 else { \
4695 x = call; \
4696 if (tstate->c_profilefunc != NULL) { \
4697 if (x == NULL) { \
4698 call_trace_protected(tstate->c_profilefunc, \
4699 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004700 tstate, tstate->frame, \
4701 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004702 /* XXX should pass (type, value, tb) */ \
4703 } else { \
4704 if (call_trace(tstate->c_profilefunc, \
4705 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004706 tstate, tstate->frame, \
4707 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004708 Py_DECREF(x); \
4709 x = NULL; \
4710 } \
4711 } \
4712 } \
4713 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004714} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004715 x = call; \
4716 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004717
Victor Stinnerc6944e72016-11-11 02:13:35 +01004718static PyObject* _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004719call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004720{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004721 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004722 PyObject *func = *pfunc;
4723 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004724 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4725 Py_ssize_t nargs = oparg - nkwargs;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004726 PyObject **stack;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004728 /* Always dispatch PyCFunction first, because these are
4729 presumed to be the most frequent callable object.
4730 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004731 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004732 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004733
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004734 stack = (*pp_stack) - nargs - nkwargs;
4735 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004736 }
4737 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004738 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004739 /* Optimize access to bound methods. Reuse the Python stack
4740 to pass 'self' as the first argument, replace 'func'
4741 with 'self'. It avoids the creation of a new temporary tuple
4742 for arguments (to replace func with self) when the method uses
4743 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004744 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004745 Py_INCREF(self);
4746 func = PyMethod_GET_FUNCTION(func);
4747 Py_INCREF(func);
4748 Py_SETREF(*pfunc, self);
4749 nargs++;
4750 }
4751 else {
4752 Py_INCREF(func);
4753 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004754
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004755 stack = (*pp_stack) - nargs - nkwargs;
Victor Stinner4a7cc882015-03-06 23:35:27 +01004756
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004757 if (PyFunction_Check(func)) {
4758 x = fast_function(func, stack, nargs, kwnames);
4759 }
4760 else {
4761 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4762 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004763
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004764 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004765 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004766
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004767 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004769 /* Clear the stack of the function object. Also removes
4770 the arguments in case they weren't consumed already
Victor Stinnere90bdb12016-08-25 23:26:50 +02004771 (fast_function() and err_args() leave them on the stack).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004772 */
4773 while ((*pp_stack) > pfunc) {
4774 w = EXT_POP(*pp_stack);
4775 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004776 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004778 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004779}
4780
Victor Stinnere90bdb12016-08-25 23:26:50 +02004781/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004782 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004783 For the simplest case -- a function that takes only positional
4784 arguments and is called with only positional arguments -- it
4785 inlines the most primitive frame setup code from
4786 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4787 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004788*/
4789
Victor Stinnerc6944e72016-11-11 02:13:35 +01004790static PyObject* _Py_HOT_FUNCTION
Victor Stinnerd8735722016-09-09 12:36:44 -07004791_PyFunction_FastCall(PyCodeObject *co, PyObject **args, Py_ssize_t nargs,
4792 PyObject *globals)
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004793{
4794 PyFrameObject *f;
4795 PyThreadState *tstate = PyThreadState_GET();
4796 PyObject **fastlocals;
4797 Py_ssize_t i;
4798 PyObject *result;
4799
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004800 assert(globals != NULL);
4801 /* XXX Perhaps we should create a specialized
4802 PyFrame_New() that doesn't take locals, but does
4803 take builtins without sanity checking them.
4804 */
4805 assert(tstate != NULL);
4806 f = PyFrame_New(tstate, co, globals, NULL);
4807 if (f == NULL) {
4808 return NULL;
4809 }
4810
4811 fastlocals = f->f_localsplus;
4812
Victor Stinner74319ae2016-08-25 00:04:09 +02004813 for (i = 0; i < nargs; i++) {
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004814 Py_INCREF(*args);
4815 fastlocals[i] = *args++;
4816 }
4817 result = PyEval_EvalFrameEx(f,0);
4818
4819 ++tstate->recursion_depth;
4820 Py_DECREF(f);
4821 --tstate->recursion_depth;
4822
4823 return result;
4824}
4825
Victor Stinnere90bdb12016-08-25 23:26:50 +02004826static PyObject *
Victor Stinnerd8735722016-09-09 12:36:44 -07004827fast_function(PyObject *func, PyObject **stack,
4828 Py_ssize_t nargs, PyObject *kwnames)
Jeremy Hylton52820442001-01-03 23:52:36 +00004829{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004830 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4831 PyObject *globals = PyFunction_GET_GLOBALS(func);
4832 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004833 PyObject *kwdefs, *closure, *name, *qualname;
4834 PyObject **d;
Victor Stinnerd8735722016-09-09 12:36:44 -07004835 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004836 Py_ssize_t nd;
Victor Stinnerd8735722016-09-09 12:36:44 -07004837
Victor Stinner57f91ac2016-09-12 13:37:07 +02004838 assert(PyFunction_Check(func));
4839 assert(nargs >= 0);
4840 assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
Victor Stinnerd8735722016-09-09 12:36:44 -07004841 assert((nargs == 0 && nkwargs == 0) || stack != NULL);
Victor Stinner57f91ac2016-09-12 13:37:07 +02004842 /* kwnames must only contains str strings, no subclass, and all keys must
4843 be unique */
Victor Stinner577e1f82016-08-25 00:29:32 +02004844
Victor Stinner74319ae2016-08-25 00:04:09 +02004845 if (co->co_kwonlyargcount == 0 && nkwargs == 0 &&
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004846 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
4847 {
Victor Stinner2eedc112016-08-22 12:29:42 +02004848 if (argdefs == NULL && co->co_argcount == nargs) {
Victor Stinnerd8735722016-09-09 12:36:44 -07004849 return _PyFunction_FastCall(co, stack, nargs, globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02004850 }
4851 else if (nargs == 0 && argdefs != NULL
4852 && co->co_argcount == Py_SIZE(argdefs)) {
4853 /* function called with no arguments, but all parameters have
4854 a default value: use default values as arguments .*/
4855 stack = &PyTuple_GET_ITEM(argdefs, 0);
Victor Stinnerd8735722016-09-09 12:36:44 -07004856 return _PyFunction_FastCall(co, stack, Py_SIZE(argdefs), globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02004857 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004858 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004859
4860 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
4861 closure = PyFunction_GET_CLOSURE(func);
4862 name = ((PyFunctionObject *)func) -> func_name;
4863 qualname = ((PyFunctionObject *)func) -> func_qualname;
4864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004865 if (argdefs != NULL) {
4866 d = &PyTuple_GET_ITEM(argdefs, 0);
4867 nd = Py_SIZE(argdefs);
4868 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004869 else {
4870 d = NULL;
4871 nd = 0;
4872 }
4873 return _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
Victor Stinner2eedc112016-08-22 12:29:42 +02004874 stack, nargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004875 nkwargs ? &PyTuple_GET_ITEM(kwnames, 0) : NULL,
4876 stack + nargs,
4877 nkwargs, 1,
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004878 d, (int)nd, kwdefs,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004879 closure, name, qualname);
4880}
4881
4882PyObject *
Victor Stinnerd8735722016-09-09 12:36:44 -07004883_PyFunction_FastCallKeywords(PyObject *func, PyObject **stack,
4884 Py_ssize_t nargs, PyObject *kwnames)
4885{
4886 return fast_function(func, stack, nargs, kwnames);
4887}
4888
4889PyObject *
Victor Stinner74319ae2016-08-25 00:04:09 +02004890_PyFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs,
Victor Stinnerb9009392016-08-22 23:15:44 +02004891 PyObject *kwargs)
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004892{
4893 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4894 PyObject *globals = PyFunction_GET_GLOBALS(func);
4895 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4896 PyObject *kwdefs, *closure, *name, *qualname;
Victor Stinnerb9009392016-08-22 23:15:44 +02004897 PyObject *kwtuple, **k;
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004898 PyObject **d;
Victor Stinner74319ae2016-08-25 00:04:09 +02004899 Py_ssize_t nd, nk;
Victor Stinnerb9009392016-08-22 23:15:44 +02004900 PyObject *result;
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004901
Victor Stinner74319ae2016-08-25 00:04:09 +02004902 assert(func != NULL);
4903 assert(nargs >= 0);
4904 assert(nargs == 0 || args != NULL);
Victor Stinnerb9009392016-08-22 23:15:44 +02004905 assert(kwargs == NULL || PyDict_Check(kwargs));
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004906
Victor Stinnerb9009392016-08-22 23:15:44 +02004907 if (co->co_kwonlyargcount == 0 &&
4908 (kwargs == NULL || PyDict_Size(kwargs) == 0) &&
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004909 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
4910 {
Victor Stinnerb9009392016-08-22 23:15:44 +02004911 /* Fast paths */
Victor Stinner2eedc112016-08-22 12:29:42 +02004912 if (argdefs == NULL && co->co_argcount == nargs) {
Victor Stinnerd8735722016-09-09 12:36:44 -07004913 return _PyFunction_FastCall(co, args, nargs, globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02004914 }
4915 else if (nargs == 0 && argdefs != NULL
4916 && co->co_argcount == Py_SIZE(argdefs)) {
4917 /* function called with no arguments, but all parameters have
4918 a default value: use default values as arguments .*/
4919 args = &PyTuple_GET_ITEM(argdefs, 0);
Victor Stinnerd8735722016-09-09 12:36:44 -07004920 return _PyFunction_FastCall(co, args, Py_SIZE(argdefs), globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02004921 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004922 }
4923
Victor Stinnerb9009392016-08-22 23:15:44 +02004924 if (kwargs != NULL) {
4925 Py_ssize_t pos, i;
4926 nk = PyDict_Size(kwargs);
4927
4928 kwtuple = PyTuple_New(2 * nk);
4929 if (kwtuple == NULL) {
4930 return NULL;
4931 }
4932
4933 k = &PyTuple_GET_ITEM(kwtuple, 0);
4934 pos = i = 0;
4935 while (PyDict_Next(kwargs, &pos, &k[i], &k[i+1])) {
4936 Py_INCREF(k[i]);
4937 Py_INCREF(k[i+1]);
4938 i += 2;
4939 }
4940 nk = i / 2;
4941 }
4942 else {
4943 kwtuple = NULL;
4944 k = NULL;
4945 nk = 0;
4946 }
4947
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004948 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
4949 closure = PyFunction_GET_CLOSURE(func);
4950 name = ((PyFunctionObject *)func) -> func_name;
4951 qualname = ((PyFunctionObject *)func) -> func_qualname;
4952
4953 if (argdefs != NULL) {
4954 d = &PyTuple_GET_ITEM(argdefs, 0);
4955 nd = Py_SIZE(argdefs);
4956 }
4957 else {
4958 d = NULL;
4959 nd = 0;
4960 }
Victor Stinnerb9009392016-08-22 23:15:44 +02004961
4962 result = _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
4963 args, nargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004964 k, k + 1, nk, 2,
Victor Stinnerb9009392016-08-22 23:15:44 +02004965 d, nd, kwdefs,
4966 closure, name, qualname);
4967 Py_XDECREF(kwtuple);
4968 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004969}
4970
4971static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004972do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004973{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004974 if (PyCFunction_Check(func)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004975 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004976 PyThreadState *tstate = PyThreadState_GET();
4977 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004978 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004979 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004980 else {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004981 return PyObject_Call(func, callargs, kwdict);
Victor Stinner74319ae2016-08-25 00:04:09 +02004982 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004983}
4984
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004985/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004986 nb_index slot defined, and store in *pi.
4987 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4988 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 +00004989 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004990*/
Tim Petersb5196382001-12-16 19:44:20 +00004991/* Note: If v is NULL, return success without storing into *pi. This
4992 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4993 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004994*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004995int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004996_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004997{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004998 if (v != NULL) {
4999 Py_ssize_t x;
5000 if (PyIndex_Check(v)) {
5001 x = PyNumber_AsSsize_t(v, NULL);
5002 if (x == -1 && PyErr_Occurred())
5003 return 0;
5004 }
5005 else {
5006 PyErr_SetString(PyExc_TypeError,
5007 "slice indices must be integers or "
5008 "None or have an __index__ method");
5009 return 0;
5010 }
5011 *pi = x;
5012 }
5013 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005014}
5015
Guido van Rossum486364b2007-06-30 05:01:58 +00005016#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005017 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00005018
Guido van Rossumb209a111997-04-29 18:18:01 +00005019static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02005020cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005021{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005022 int res = 0;
5023 switch (op) {
5024 case PyCmp_IS:
5025 res = (v == w);
5026 break;
5027 case PyCmp_IS_NOT:
5028 res = (v != w);
5029 break;
5030 case PyCmp_IN:
5031 res = PySequence_Contains(w, v);
5032 if (res < 0)
5033 return NULL;
5034 break;
5035 case PyCmp_NOT_IN:
5036 res = PySequence_Contains(w, v);
5037 if (res < 0)
5038 return NULL;
5039 res = !res;
5040 break;
5041 case PyCmp_EXC_MATCH:
5042 if (PyTuple_Check(w)) {
5043 Py_ssize_t i, length;
5044 length = PyTuple_Size(w);
5045 for (i = 0; i < length; i += 1) {
5046 PyObject *exc = PyTuple_GET_ITEM(w, i);
5047 if (!PyExceptionClass_Check(exc)) {
5048 PyErr_SetString(PyExc_TypeError,
5049 CANNOT_CATCH_MSG);
5050 return NULL;
5051 }
5052 }
5053 }
5054 else {
5055 if (!PyExceptionClass_Check(w)) {
5056 PyErr_SetString(PyExc_TypeError,
5057 CANNOT_CATCH_MSG);
5058 return NULL;
5059 }
5060 }
5061 res = PyErr_GivenExceptionMatches(v, w);
5062 break;
5063 default:
5064 return PyObject_RichCompare(v, w, op);
5065 }
5066 v = res ? Py_True : Py_False;
5067 Py_INCREF(v);
5068 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005069}
5070
Thomas Wouters52152252000-08-17 22:55:00 +00005071static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005072import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
5073{
5074 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005075 PyObject *import_func, *res;
5076 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005077
5078 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
5079 if (import_func == NULL) {
5080 PyErr_SetString(PyExc_ImportError, "__import__ not found");
5081 return NULL;
5082 }
5083
5084 /* Fast path for not overloaded __import__. */
5085 if (import_func == PyThreadState_GET()->interp->import_func) {
5086 int ilevel = _PyLong_AsInt(level);
5087 if (ilevel == -1 && PyErr_Occurred()) {
5088 return NULL;
5089 }
5090 res = PyImport_ImportModuleLevelObject(
5091 name,
5092 f->f_globals,
5093 f->f_locals == NULL ? Py_None : f->f_locals,
5094 fromlist,
5095 ilevel);
5096 return res;
5097 }
5098
5099 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005100
5101 stack[0] = name;
5102 stack[1] = f->f_globals;
5103 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5104 stack[3] = fromlist;
5105 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005106 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005107 Py_DECREF(import_func);
5108 return res;
5109}
5110
5111static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00005112import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005114 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005115 _Py_IDENTIFIER(__name__);
5116 PyObject *fullmodname, *pkgname;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005118 x = PyObject_GetAttr(v, name);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005119 if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
5120 return x;
5121 /* Issue #17636: in case this failed because of a circular relative
5122 import, try to fallback on reading the module directly from
5123 sys.modules. */
5124 PyErr_Clear();
5125 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005126 if (pkgname == NULL) {
5127 goto error;
5128 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005129 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
5130 Py_DECREF(pkgname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005131 if (fullmodname == NULL) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005132 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005133 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005134 x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005135 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005136 if (x == NULL) {
5137 goto error;
5138 }
5139 Py_INCREF(x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005140 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005141 error:
5142 PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
5143 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005144}
Guido van Rossumac7be682001-01-17 15:42:30 +00005145
Thomas Wouters52152252000-08-17 22:55:00 +00005146static int
5147import_all_from(PyObject *locals, PyObject *v)
5148{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005149 _Py_IDENTIFIER(__all__);
5150 _Py_IDENTIFIER(__dict__);
5151 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005152 PyObject *dict, *name, *value;
5153 int skip_leading_underscores = 0;
5154 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005156 if (all == NULL) {
5157 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5158 return -1; /* Unexpected error */
5159 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005160 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005161 if (dict == NULL) {
5162 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5163 return -1;
5164 PyErr_SetString(PyExc_ImportError,
5165 "from-import-* object has no __dict__ and no __all__");
5166 return -1;
5167 }
5168 all = PyMapping_Keys(dict);
5169 Py_DECREF(dict);
5170 if (all == NULL)
5171 return -1;
5172 skip_leading_underscores = 1;
5173 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005175 for (pos = 0, err = 0; ; pos++) {
5176 name = PySequence_GetItem(all, pos);
5177 if (name == NULL) {
5178 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5179 err = -1;
5180 else
5181 PyErr_Clear();
5182 break;
5183 }
5184 if (skip_leading_underscores &&
5185 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005186 PyUnicode_READY(name) != -1 &&
5187 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005188 {
5189 Py_DECREF(name);
5190 continue;
5191 }
5192 value = PyObject_GetAttr(v, name);
5193 if (value == NULL)
5194 err = -1;
5195 else if (PyDict_CheckExact(locals))
5196 err = PyDict_SetItem(locals, name, value);
5197 else
5198 err = PyObject_SetItem(locals, name, value);
5199 Py_DECREF(name);
5200 Py_XDECREF(value);
5201 if (err != 0)
5202 break;
5203 }
5204 Py_DECREF(all);
5205 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005206}
5207
Guido van Rossumac7be682001-01-17 15:42:30 +00005208static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005209format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005210{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005211 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005213 if (!obj)
5214 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005215
Serhiy Storchaka06515832016-11-20 09:13:07 +02005216 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005217 if (!obj_str)
5218 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005220 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005221}
Guido van Rossum950361c1997-01-24 13:49:28 +00005222
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005223static void
5224format_exc_unbound(PyCodeObject *co, int oparg)
5225{
5226 PyObject *name;
5227 /* Don't stomp existing exception */
5228 if (PyErr_Occurred())
5229 return;
5230 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5231 name = PyTuple_GET_ITEM(co->co_cellvars,
5232 oparg);
5233 format_exc_check_arg(
5234 PyExc_UnboundLocalError,
5235 UNBOUNDLOCAL_ERROR_MSG,
5236 name);
5237 } else {
5238 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5239 PyTuple_GET_SIZE(co->co_cellvars));
5240 format_exc_check_arg(PyExc_NameError,
5241 UNBOUNDFREE_ERROR_MSG, name);
5242 }
5243}
5244
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005245static PyObject *
5246unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005247 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005248{
5249 PyObject *res;
5250 if (Py_REFCNT(v) == 2) {
5251 /* In the common case, there are 2 references to the value
5252 * stored in 'variable' when the += is performed: one on the
5253 * value stack (in 'v') and one still stored in the
5254 * 'variable'. We try to delete the variable now to reduce
5255 * the refcnt to 1.
5256 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005257 int opcode, oparg;
5258 NEXTOPARG();
5259 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005260 case STORE_FAST:
5261 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005262 PyObject **fastlocals = f->f_localsplus;
5263 if (GETLOCAL(oparg) == v)
5264 SETLOCAL(oparg, NULL);
5265 break;
5266 }
5267 case STORE_DEREF:
5268 {
5269 PyObject **freevars = (f->f_localsplus +
5270 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005271 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005272 if (PyCell_GET(c) == v) {
5273 PyCell_SET(c, NULL);
5274 Py_DECREF(v);
5275 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005276 break;
5277 }
5278 case STORE_NAME:
5279 {
5280 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005281 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005282 PyObject *locals = f->f_locals;
5283 if (PyDict_CheckExact(locals) &&
5284 PyDict_GetItem(locals, name) == v) {
5285 if (PyDict_DelItem(locals, name) != 0) {
5286 PyErr_Clear();
5287 }
5288 }
5289 break;
5290 }
5291 }
5292 }
5293 res = v;
5294 PyUnicode_Append(&res, w);
5295 return res;
5296}
5297
Guido van Rossum950361c1997-01-24 13:49:28 +00005298#ifdef DYNAMIC_EXECUTION_PROFILE
5299
Skip Montanarof118cb12001-10-15 20:51:38 +00005300static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005301getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005302{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005303 int i;
5304 PyObject *l = PyList_New(256);
5305 if (l == NULL) return NULL;
5306 for (i = 0; i < 256; i++) {
5307 PyObject *x = PyLong_FromLong(a[i]);
5308 if (x == NULL) {
5309 Py_DECREF(l);
5310 return NULL;
5311 }
5312 PyList_SetItem(l, i, x);
5313 }
5314 for (i = 0; i < 256; i++)
5315 a[i] = 0;
5316 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005317}
5318
5319PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005320_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005321{
5322#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005323 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005324#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005325 int i;
5326 PyObject *l = PyList_New(257);
5327 if (l == NULL) return NULL;
5328 for (i = 0; i < 257; i++) {
5329 PyObject *x = getarray(dxpairs[i]);
5330 if (x == NULL) {
5331 Py_DECREF(l);
5332 return NULL;
5333 }
5334 PyList_SetItem(l, i, x);
5335 }
5336 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005337#endif
5338}
5339
5340#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005341
5342Py_ssize_t
5343_PyEval_RequestCodeExtraIndex(freefunc free)
5344{
5345 PyThreadState *tstate = PyThreadState_Get();
5346 Py_ssize_t new_index;
5347
5348 if (tstate->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
5349 return -1;
5350 }
5351 new_index = tstate->co_extra_user_count++;
5352 tstate->co_extra_freefuncs[new_index] = free;
5353 return new_index;
5354}
Łukasz Langaa785c872016-09-09 17:37:37 -07005355
5356static void
5357dtrace_function_entry(PyFrameObject *f)
5358{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005359 const char *filename;
5360 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005361 int lineno;
5362
5363 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5364 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5365 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5366
5367 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5368}
5369
5370static void
5371dtrace_function_return(PyFrameObject *f)
5372{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005373 const char *filename;
5374 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005375 int lineno;
5376
5377 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5378 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5379 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5380
5381 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5382}
5383
5384/* DTrace equivalent of maybe_call_line_trace. */
5385static void
5386maybe_dtrace_line(PyFrameObject *frame,
5387 int *instr_lb, int *instr_ub, int *instr_prev)
5388{
5389 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005390 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005391
5392 /* If the last instruction executed isn't in the current
5393 instruction window, reset the window.
5394 */
5395 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5396 PyAddrPair bounds;
5397 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5398 &bounds);
5399 *instr_lb = bounds.ap_lower;
5400 *instr_ub = bounds.ap_upper;
5401 }
5402 /* If the last instruction falls at the start of a line or if
5403 it represents a jump backwards, update the frame's line
5404 number and call the trace function. */
5405 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5406 frame->f_lineno = line;
5407 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5408 if (!co_filename)
5409 co_filename = "?";
5410 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5411 if (!co_name)
5412 co_name = "?";
5413 PyDTrace_LINE(co_filename, co_name, line);
5414 }
5415 *instr_prev = frame->f_lasti;
5416}