blob: b37205e50a619701469470f6faccdcb91d51899a [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"
Eric Snow2ebc5ce2017-09-07 23:51:28 -060013#include "internal/pystate.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000014
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000015#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040016#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000017#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000018#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070019#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040020#include "setobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000021#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000022
Guido van Rossumc6004111993-11-05 10:22:19 +000023#include <ctype.h>
24
Guido van Rossum408027e1996-12-30 16:17:54 +000025#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000026/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000027#define LLTRACE 1 /* Low-level trace feature */
28#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000029#endif
30
Yury Selivanovf2392132016-12-13 19:03:51 -050031/* Private API for the LOAD_METHOD opcode. */
32extern int _PyObject_GetMethod(PyObject *, PyObject *, PyObject **);
33
Jeremy Hylton52820442001-01-03 23:52:36 +000034typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000035
Guido van Rossum374a9221991-04-04 10:40:29 +000036/* Forward declarations */
Eric Snow2ebc5ce2017-09-07 23:51:28 -060037Py_LOCAL_INLINE(PyObject *) call_function(PyObject ***, Py_ssize_t,
38 PyObject *);
Victor Stinnerf9b760f2016-09-09 10:17:08 -070039static PyObject * do_call_core(PyObject *, PyObject *, PyObject *);
Jeremy Hylton52820442001-01-03 23:52:36 +000040
Guido van Rossum0a066c01992-03-27 17:29:15 +000041#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000042static int lltrace;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020043static int prtrace(PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000044#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010045static int call_trace(Py_tracefunc, PyObject *,
46 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000048static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010049 PyThreadState *, PyFrameObject *,
50 int, PyObject *);
51static void call_exc_trace(Py_tracefunc, PyObject *,
52 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000053static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060054 PyThreadState *, PyFrameObject *,
55 int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070056static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
57static void dtrace_function_entry(PyFrameObject *);
58static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000059
Thomas Wouters477c8d52006-05-27 19:21:47 +000060static PyObject * cmp_outcome(int, PyObject *, PyObject *);
Eric Snow2ebc5ce2017-09-07 23:51:28 -060061static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *,
62 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +000063static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +000064static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +000065static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +000066static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +020067static PyObject * unicode_concatenate(PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030068 PyFrameObject *, const _Py_CODEUNIT *);
Benjamin Petersonce798522012-01-22 11:24:29 -050069static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Serhiy Storchaka25e4f772017-08-03 11:37:15 +030070static int check_args_iterable(PyObject *func, PyObject *vararg);
71static void format_kwargs_mapping_error(PyObject *func, PyObject *kwargs);
Miss Islington (bot)fcd4e032018-04-04 07:09:00 -070072static void format_awaitable_error(PyTypeObject *, int);
Guido van Rossum374a9221991-04-04 10:40:29 +000073
Paul Prescode68140d2000-08-30 20:25:01 +000074#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000076#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000078#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 "free variable '%.200s' referenced before assignment" \
80 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000081
Guido van Rossum950361c1997-01-24 13:49:28 +000082/* Dynamic execution profile */
83#ifdef DYNAMIC_EXECUTION_PROFILE
84#ifdef DXPAIRS
85static long dxpairs[257][256];
86#define dxp dxpairs[256]
87#else
88static long dxp[256];
89#endif
90#endif
91
Eric Snow2ebc5ce2017-09-07 23:51:28 -060092#define GIL_REQUEST _Py_atomic_load_relaxed(&_PyRuntime.ceval.gil_drop_request)
Benjamin Petersond2be5b42010-09-10 22:47:02 +000093
Jeffrey Yasskin39370832010-05-03 19:29:34 +000094/* This can set eval_breaker to 0 even though gil_drop_request became
95 1. We believe this is all right because the eval loop will release
96 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +000097#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 _Py_atomic_store_relaxed( \
Eric Snow2ebc5ce2017-09-07 23:51:28 -060099 &_PyRuntime.ceval.eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000100 GIL_REQUEST | \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600101 _Py_atomic_load_relaxed(&_PyRuntime.ceval.pending.calls_to_do) | \
102 _PyRuntime.ceval.pending.async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000103
104#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600106 _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 1); \
107 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 } 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 { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600112 _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 COMPUTE_EVAL_BREAKER(); \
114 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000115
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000116/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000117#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600119 _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 1); \
120 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000122
123#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600125 _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 COMPUTE_EVAL_BREAKER(); \
127 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000128
129#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600131 _PyRuntime.ceval.pending.async_exc = 1; \
132 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000134
135#define UNSIGNAL_ASYNC_EXC() \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600136 do { \
137 _PyRuntime.ceval.pending.async_exc = 0; \
138 COMPUTE_EVAL_BREAKER(); \
139 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000140
141
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000142#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000143#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000144#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000145#include "pythread.h"
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000146#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000147
Tim Peters7f468f22004-10-11 02:40:51 +0000148int
149PyEval_ThreadsInitialized(void)
150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000152}
153
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000154void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000155PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000156{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 if (gil_created())
158 return;
159 create_gil();
160 take_gil(PyThreadState_GET());
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600161 _PyRuntime.ceval.pending.main_thread = PyThread_get_thread_ident();
162 if (!_PyRuntime.ceval.pending.lock)
163 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000164}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000165
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000166void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000167_PyEval_FiniThreads(void)
168{
169 if (!gil_created())
170 return;
171 destroy_gil();
172 assert(!gil_created());
173}
174
175void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000176PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000177{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 PyThreadState *tstate = PyThreadState_GET();
179 if (tstate == NULL)
180 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
181 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000182}
183
184void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000185PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 /* This function must succeed when the current thread state is NULL.
188 We therefore avoid PyThreadState_GET() which dumps a fatal error
189 in debug mode.
190 */
191 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
192 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000193}
194
195void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000196PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 if (tstate == NULL)
199 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
200 /* Check someone has called PyEval_InitThreads() to create the lock */
201 assert(gil_created());
202 take_gil(tstate);
203 if (PyThreadState_Swap(tstate) != NULL)
204 Py_FatalError(
205 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000206}
207
208void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000209PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000210{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 if (tstate == NULL)
212 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
213 if (PyThreadState_Swap(NULL) != tstate)
214 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
215 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000216}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000217
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200218/* This function is called from PyOS_AfterFork_Child to destroy all threads
219 * which are not running in the child process, and clear internal locks
220 * which might be held by those threads.
221 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000222
223void
224PyEval_ReInitThreads(void)
225{
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200226 PyThreadState *current_tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 if (!gil_created())
229 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 recreate_gil();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600231 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200232 take_gil(current_tstate);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600233 _PyRuntime.ceval.pending.main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000234
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200235 /* Destroy all threads except the current one */
236 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000237}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000238
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000239/* This function is used to signal that async exceptions are waiting to be
240 raised, therefore it is also useful in non-threaded builds. */
241
242void
243_PyEval_SignalAsyncExc(void)
244{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000246}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000247
Guido van Rossumff4949e1992-08-05 19:58:53 +0000248/* Functions save_thread and restore_thread are always defined so
249 dynamically loaded modules needn't be compiled separately for use
250 with and without threads: */
251
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000252PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000253PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 PyThreadState *tstate = PyThreadState_Swap(NULL);
256 if (tstate == NULL)
257 Py_FatalError("PyEval_SaveThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100258 assert(gil_created());
259 drop_gil(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000261}
262
263void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000264PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000265{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 if (tstate == NULL)
267 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100268 assert(gil_created());
269
270 int err = errno;
271 take_gil(tstate);
272 /* _Py_Finalizing is protected by the GIL */
273 if (_Py_IsFinalizing() && !_Py_CURRENTLY_FINALIZING(tstate)) {
274 drop_gil(tstate);
275 PyThread_exit_thread();
276 Py_UNREACHABLE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 }
Victor Stinner2914bb32018-01-29 11:57:45 +0100278 errno = err;
279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000281}
282
283
Guido van Rossuma9672091994-09-14 13:31:22 +0000284/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
285 signal handlers or Mac I/O completion routines) can schedule calls
286 to a function to be called synchronously.
287 The synchronous function is called with one void* argument.
288 It should return 0 for success or -1 for failure -- failure should
289 be accompanied by an exception.
290
291 If registry succeeds, the registry function returns 0; if it fails
292 (e.g. due to too many pending calls) it returns -1 (without setting
293 an exception condition).
294
295 Note that because registry may occur from within signal handlers,
296 or other asynchronous events, calling malloc() is unsafe!
297
Guido van Rossuma9672091994-09-14 13:31:22 +0000298 Any thread can schedule pending calls, but only the main thread
299 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000300 There is no facility to schedule calls to a particular thread, but
301 that should be easy to change, should that ever be required. In
302 that case, the static variables here should go into the python
303 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000304*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000305
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200306void
307_PyEval_SignalReceived(void)
308{
309 /* bpo-30703: Function called when the C signal handler of Python gets a
310 signal. We cannot queue a callback using Py_AddPendingCall() since
311 that function is not async-signal-safe. */
312 SIGNAL_PENDING_CALLS();
313}
314
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200315/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000316 scheduling to be made from any thread, and even from an executing
317 callback.
318 */
319
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000320int
321Py_AddPendingCall(int (*func)(void *), void *arg)
322{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 int i, j, result=0;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600324 PyThread_type_lock lock = _PyRuntime.ceval.pending.lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 /* try a few times for the lock. Since this mechanism is used
327 * for signal handling (on the main thread), there is a (slim)
328 * chance that a signal is delivered on the same thread while we
329 * hold the lock during the Py_MakePendingCalls() function.
330 * This avoids a deadlock in that case.
331 * Note that signals can be delivered on any thread. In particular,
332 * on Windows, a SIGINT is delivered on a system-created worker
333 * thread.
334 * We also check for lock being NULL, in the unlikely case that
335 * this function is called before any bytecode evaluation takes place.
336 */
337 if (lock != NULL) {
338 for (i = 0; i<100; i++) {
339 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
340 break;
341 }
342 if (i == 100)
343 return -1;
344 }
345
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600346 i = _PyRuntime.ceval.pending.last;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 j = (i + 1) % NPENDINGCALLS;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600348 if (j == _PyRuntime.ceval.pending.first) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 result = -1; /* Queue full */
350 } else {
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600351 _PyRuntime.ceval.pending.calls[i].func = func;
352 _PyRuntime.ceval.pending.calls[i].arg = arg;
353 _PyRuntime.ceval.pending.last = j;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 }
355 /* signal main loop */
356 SIGNAL_PENDING_CALLS();
357 if (lock != NULL)
358 PyThread_release_lock(lock);
359 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000360}
361
362int
363Py_MakePendingCalls(void)
364{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200365 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 int i;
367 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000368
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200369 assert(PyGILState_Check());
370
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600371 if (!_PyRuntime.ceval.pending.lock) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 /* initial allocation of the lock */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600373 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
374 if (_PyRuntime.ceval.pending.lock == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 return -1;
376 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 /* only service pending calls on main thread */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600379 if (_PyRuntime.ceval.pending.main_thread &&
380 PyThread_get_thread_ident() != _PyRuntime.ceval.pending.main_thread)
381 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 return 0;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600383 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200385 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200387 busy = 1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200388 /* unsignal before starting to call callbacks, so that any callback
389 added in-between re-signals */
390 UNSIGNAL_PENDING_CALLS();
391
392 /* Python signal handler doesn't really queue a callback: it only signals
393 that a signal was received, see _PyEval_SignalReceived(). */
394 if (PyErr_CheckSignals() < 0) {
395 goto error;
396 }
397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 /* perform a bounded number of calls, in case of recursion */
399 for (i=0; i<NPENDINGCALLS; i++) {
400 int j;
401 int (*func)(void *);
402 void *arg = NULL;
403
404 /* pop one item off the queue while holding the lock */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600405 PyThread_acquire_lock(_PyRuntime.ceval.pending.lock, WAIT_LOCK);
406 j = _PyRuntime.ceval.pending.first;
407 if (j == _PyRuntime.ceval.pending.last) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 func = NULL; /* Queue empty */
409 } else {
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600410 func = _PyRuntime.ceval.pending.calls[j].func;
411 arg = _PyRuntime.ceval.pending.calls[j].arg;
412 _PyRuntime.ceval.pending.first = (j + 1) % NPENDINGCALLS;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600414 PyThread_release_lock(_PyRuntime.ceval.pending.lock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 /* having released the lock, perform the callback */
416 if (func == NULL)
417 break;
418 r = func(arg);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200419 if (r) {
420 goto error;
421 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200423
Charles-François Natalif23339a2011-07-23 18:15:43 +0200424 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 return r;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200426
427error:
428 busy = 0;
429 SIGNAL_PENDING_CALLS(); /* We're not done yet */
430 return -1;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000431}
432
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000433/* The interpreter's recursion limit */
434
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000435#ifndef Py_DEFAULT_RECURSION_LIMIT
436#define Py_DEFAULT_RECURSION_LIMIT 1000
437#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600438
Eric Snow05351c12017-09-05 21:43:08 -0700439int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000440
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600441void
442_PyEval_Initialize(struct _ceval_runtime_state *state)
443{
444 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
445 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
446 _gil_initialize(&state->gil);
447}
448
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000449int
450Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000451{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600452 return _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000453}
454
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000455void
456Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000457{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600458 _PyRuntime.ceval.recursion_limit = new_limit;
459 _Py_CheckRecursionLimit = _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000460}
461
Armin Rigo2b3eb402003-10-28 12:05:48 +0000462/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
463 if the recursion_depth reaches _Py_CheckRecursionLimit.
464 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
465 to guarantee that _Py_CheckRecursiveCall() is regularly called.
466 Without USE_STACKCHECK, there is no need for this. */
467int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300468_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000469{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 PyThreadState *tstate = PyThreadState_GET();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600471 int recursion_limit = _PyRuntime.ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000472
473#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700474 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 if (PyOS_CheckStack()) {
476 --tstate->recursion_depth;
477 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
478 return -1;
479 }
pdox18967932017-10-25 23:03:01 -0700480 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700481 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700482#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 if (tstate->recursion_critical)
484 /* Somebody asked that we don't check for recursion. */
485 return 0;
486 if (tstate->overflowed) {
487 if (tstate->recursion_depth > recursion_limit + 50) {
488 /* Overflowing while handling an overflow. Give up. */
489 Py_FatalError("Cannot recover from stack overflow.");
490 }
491 return 0;
492 }
493 if (tstate->recursion_depth > recursion_limit) {
494 --tstate->recursion_depth;
495 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400496 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 "maximum recursion depth exceeded%s",
498 where);
499 return -1;
500 }
501 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000502}
503
Guido van Rossum374a9221991-04-04 10:40:29 +0000504/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000505enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000506 WHY_NOT = 0x0001, /* No error */
507 WHY_EXCEPTION = 0x0002, /* Exception occurred */
Stefan Krahb7e10102010-06-23 18:42:39 +0000508 WHY_RETURN = 0x0008, /* 'return' statement */
509 WHY_BREAK = 0x0010, /* 'break' statement */
510 WHY_CONTINUE = 0x0020, /* 'continue' statement */
511 WHY_YIELD = 0x0040, /* 'yield' operator */
512 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000513};
Guido van Rossum374a9221991-04-04 10:40:29 +0000514
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400515static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000516static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000517
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600518#define _Py_TracingPossible _PyRuntime.ceval.tracing_possible
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000519
Guido van Rossum374a9221991-04-04 10:40:29 +0000520
Guido van Rossumb209a111997-04-29 18:18:01 +0000521PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000522PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000523{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 return PyEval_EvalCodeEx(co,
525 globals, locals,
526 (PyObject **)NULL, 0,
527 (PyObject **)NULL, 0,
528 (PyObject **)NULL, 0,
529 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000530}
531
532
533/* Interpreter main loop */
534
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000535PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000536PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 /* This is for backward compatibility with extension modules that
538 used this API; core interpreter code should call
539 PyEval_EvalFrameEx() */
540 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000541}
542
543PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000544PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000545{
Brett Cannon3cebf932016-09-05 15:33:46 -0700546 PyThreadState *tstate = PyThreadState_GET();
547 return tstate->interp->eval_frame(f, throwflag);
548}
549
Victor Stinnerc6944e72016-11-11 02:13:35 +0100550PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700551_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
552{
Guido van Rossum950361c1997-01-24 13:49:28 +0000553#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000555#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200556 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300557 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200558 int opcode; /* Current opcode */
559 int oparg; /* Current opcode argument, if any */
560 enum why_code why; /* Reason for block stack unwind */
561 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 PyObject *retval = NULL; /* Return value */
563 PyThreadState *tstate = PyThreadState_GET();
564 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 is true when the line being executed has changed. The
571 initial values are such as to make this false the first
572 time it is tested. */
573 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000574
Serhiy Storchakaab874002016-09-11 13:48:15 +0300575 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 PyObject *names;
577 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000578
Brett Cannon368b4b72012-04-02 12:17:59 -0400579#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200580 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400581#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200582
Antoine Pitroub52ec782009-01-25 16:34:23 +0000583/* Computed GOTOs, or
584 the-optimization-commonly-but-improperly-known-as-"threaded code"
585 using gcc's labels-as-values extension
586 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
587
588 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000590 combined with a lookup table of jump addresses. However, since the
591 indirect jump instruction is shared by all opcodes, the CPU will have a
592 hard time making the right prediction for where to jump next (actually,
593 it will be always wrong except in the uncommon case of a sequence of
594 several identical opcodes).
595
596 "Threaded code" in contrast, uses an explicit jump table and an explicit
597 indirect jump instruction at the end of each opcode. Since the jump
598 instruction is at a different address for each opcode, the CPU will make a
599 separate prediction for each of these instructions, which is equivalent to
600 predicting the second opcode of each opcode pair. These predictions have
601 a much better chance to turn out valid, especially in small bytecode loops.
602
603 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000605 and potentially many more instructions (depending on the pipeline width).
606 A correctly predicted branch, however, is nearly free.
607
608 At the time of this writing, the "threaded code" version is up to 15-20%
609 faster than the normal "switch" version, depending on the compiler and the
610 CPU architecture.
611
612 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
613 because it would render the measurements invalid.
614
615
616 NOTE: care must be taken that the compiler doesn't try to "optimize" the
617 indirect jumps by sharing them between all opcodes. Such optimizations
618 can be disabled on gcc by using the -fno-gcse flag (or possibly
619 -fno-crossjumping).
620*/
621
Antoine Pitrou042b1282010-08-13 21:15:58 +0000622#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000623#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000624#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000625#endif
626
Antoine Pitrou042b1282010-08-13 21:15:58 +0000627#ifdef HAVE_COMPUTED_GOTOS
628 #ifndef USE_COMPUTED_GOTOS
629 #define USE_COMPUTED_GOTOS 1
630 #endif
631#else
632 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
633 #error "Computed gotos are not supported on this compiler."
634 #endif
635 #undef USE_COMPUTED_GOTOS
636 #define USE_COMPUTED_GOTOS 0
637#endif
638
639#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000640/* Import the static jump table */
641#include "opcode_targets.h"
642
Antoine Pitroub52ec782009-01-25 16:34:23 +0000643#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 TARGET_##op: \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000646
Antoine Pitroub52ec782009-01-25 16:34:23 +0000647#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600649 if (!_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 FAST_DISPATCH(); \
651 } \
652 continue; \
653 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000654
655#ifdef LLTRACE
656#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700658 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300660 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300661 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 } \
663 goto fast_next_opcode; \
664 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000665#else
666#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700668 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300670 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300671 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 } \
673 goto fast_next_opcode; \
674 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000675#endif
676
677#else
678#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 case op:
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300680
Antoine Pitroub52ec782009-01-25 16:34:23 +0000681#define DISPATCH() continue
682#define FAST_DISPATCH() goto fast_next_opcode
683#endif
684
685
Neal Norwitza81d2202002-07-14 00:27:26 +0000686/* Tuple access macros */
687
688#ifndef Py_DEBUG
689#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
690#else
691#define GETITEM(v, i) PyTuple_GetItem((v), (i))
692#endif
693
Guido van Rossum374a9221991-04-04 10:40:29 +0000694/* Code access macros */
695
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300696/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600697#define INSTR_OFFSET() \
698 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300699#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300700 _Py_CODEUNIT word = *next_instr; \
701 opcode = _Py_OPCODE(word); \
702 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300703 next_instr++; \
704 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300705#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
706#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000707
Raymond Hettingerf606f872003-03-16 03:11:04 +0000708/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 Some opcodes tend to come in pairs thus making it possible to
710 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300711 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 Verifying the prediction costs a single high-speed test of a register
714 variable against a constant. If the pairing was good, then the
715 processor's own internal branch predication has a high likelihood of
716 success, resulting in a nearly zero-overhead transition to the
717 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300718 including its unpredictable switch-case branch. Combined with the
719 processor's internal branch prediction, a successful PREDICT has the
720 effect of making the two opcodes run as if they were a single new opcode
721 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000722
Georg Brandl86b2fb92008-07-16 03:43:04 +0000723 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 predictions turned-on and interpret the results as if some opcodes
725 had been combined or turn-off predictions so that the opcode frequency
726 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000727
728 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 the CPU to record separate branch prediction information for each
730 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000731
Raymond Hettingerf606f872003-03-16 03:11:04 +0000732*/
733
Antoine Pitrou042b1282010-08-13 21:15:58 +0000734#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000736#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300737#define PREDICT(op) \
738 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300739 _Py_CODEUNIT word = *next_instr; \
740 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300741 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300742 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300743 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300744 goto PRED_##op; \
745 } \
746 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000747#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300748#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000749
Raymond Hettingerf606f872003-03-16 03:11:04 +0000750
Guido van Rossum374a9221991-04-04 10:40:29 +0000751/* Stack manipulation macros */
752
Martin v. Löwis18e16552006-02-15 17:27:45 +0000753/* The stack can grow at most MAXINT deep, as co_nlocals and
754 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000755#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
756#define EMPTY() (STACK_LEVEL() == 0)
757#define TOP() (stack_pointer[-1])
758#define SECOND() (stack_pointer[-2])
759#define THIRD() (stack_pointer[-3])
760#define FOURTH() (stack_pointer[-4])
761#define PEEK(n) (stack_pointer[-(n)])
762#define SET_TOP(v) (stack_pointer[-1] = (v))
763#define SET_SECOND(v) (stack_pointer[-2] = (v))
764#define SET_THIRD(v) (stack_pointer[-3] = (v))
765#define SET_FOURTH(v) (stack_pointer[-4] = (v))
766#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
767#define BASIC_STACKADJ(n) (stack_pointer += n)
768#define BASIC_PUSH(v) (*stack_pointer++ = (v))
769#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000770
Guido van Rossum96a42c81992-01-12 02:29:51 +0000771#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000773 lltrace && prtrace(TOP(), "push")); \
774 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000776 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000778 lltrace && prtrace(TOP(), "stackadj")); \
779 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +0000780#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000781 prtrace((STACK_POINTER)[-1], "ext_pop")), \
782 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000783#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000784#define PUSH(v) BASIC_PUSH(v)
785#define POP() BASIC_POP()
786#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000787#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000788#endif
789
Guido van Rossum681d79a1995-07-18 14:51:37 +0000790/* Local variable macros */
791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000793
794/* The SETLOCAL() macro must not DECREF the local variable in-place and
795 then store the new value; it must copy the old value to a temporary
796 value, then store the new value, and then DECREF the temporary value.
797 This is because it is possible that during the DECREF the frame is
798 accessed by other code (e.g. a __del__ method or gc.collect()) and the
799 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000801 GETLOCAL(i) = value; \
802 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000803
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000804
805#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 while (STACK_LEVEL() > (b)->b_level) { \
807 PyObject *v = POP(); \
808 Py_XDECREF(v); \
809 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000810
811#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300812 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +0100814 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 assert(STACK_LEVEL() >= (b)->b_level + 3); \
816 while (STACK_LEVEL() > (b)->b_level + 3) { \
817 value = POP(); \
818 Py_XDECREF(value); \
819 } \
Mark Shannonae3087c2017-10-22 22:41:51 +0100820 exc_info = tstate->exc_info; \
821 type = exc_info->exc_type; \
822 value = exc_info->exc_value; \
823 traceback = exc_info->exc_traceback; \
824 exc_info->exc_type = POP(); \
825 exc_info->exc_value = POP(); \
826 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 Py_XDECREF(type); \
828 Py_XDECREF(value); \
829 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300830 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000831
Guido van Rossuma027efa1997-05-05 20:56:21 +0000832/* Start of code */
833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 /* push frame */
835 if (Py_EnterRecursiveCall(""))
836 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 if (tstate->use_tracing) {
841 if (tstate->c_tracefunc != NULL) {
842 /* tstate->c_tracefunc, if defined, is a
843 function that will be called on *every* entry
844 to a code block. Its return value, if not
845 None, is a function that will be called at
846 the start of each executed line of code.
847 (Actually, the function must return itself
848 in order to continue tracing.) The trace
849 functions are called with three arguments:
850 a pointer to the current frame, a string
851 indicating why the function is called, and
852 an argument which depends on the situation.
853 The global trace function is also called
854 whenever an exception is detected. */
855 if (call_trace_protected(tstate->c_tracefunc,
856 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100857 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 /* Trace function raised an error */
859 goto exit_eval_frame;
860 }
861 }
862 if (tstate->c_profilefunc != NULL) {
863 /* Similar for c_profilefunc, except it needn't
864 return itself and isn't called for "line" events */
865 if (call_trace_protected(tstate->c_profilefunc,
866 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100867 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 /* Profile function raised an error */
869 goto exit_eval_frame;
870 }
871 }
872 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000873
Łukasz Langaa785c872016-09-09 17:37:37 -0700874 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
875 dtrace_function_entry(f);
876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 co = f->f_code;
878 names = co->co_names;
879 consts = co->co_consts;
880 fastlocals = f->f_localsplus;
881 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300882 assert(PyBytes_Check(co->co_code));
883 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +0300884 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
885 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
886 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300887 /*
888 f->f_lasti refers to the index of the last instruction,
889 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000890
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300891 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500892 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 When the PREDICT() macros are enabled, some opcode pairs follow in
895 direct succession without updating f->f_lasti. A successful
896 prediction effectively links the two codes together as if they
897 were a single new opcode; accordingly,f->f_lasti will point to
898 the first code in the pair (for instance, GET_ITER followed by
899 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300900 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300902 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300903 next_instr = first_instr;
904 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +0300905 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
906 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300907 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 stack_pointer = f->f_stacktop;
909 assert(stack_pointer != NULL);
910 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +0200911 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000912
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000913
Tim Peters5ca576e2001-06-18 22:08:13 +0000914#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200915 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000916#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 why = WHY_NOT;
Guido van Rossumac7be682001-01-17 15:42:30 +0000919
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400920 if (throwflag) /* support for generator.throw() */
921 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000922
Victor Stinnerace47d72013-07-18 01:41:08 +0200923#ifdef Py_DEBUG
924 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100925 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +0000926 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +0200927 assert(!PyErr_Occurred());
928#endif
929
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 assert(stack_pointer >= f->f_valuestack); /* else underflow */
932 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +0200933 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 /* Do periodic things. Doing this every time through
936 the loop would add too much overhead, so we do it
937 only every Nth instruction. We also do it if
938 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
939 event needs attention (e.g. a signal handler or
940 async I/O handler); see Py_AddPendingCall() and
941 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000942
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600943 if (_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) {
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -0700944 if (_Py_OPCODE(*next_instr) == SETUP_FINALLY ||
945 _Py_OPCODE(*next_instr) == YIELD_FROM) {
946 /* Two cases where we skip running signal handlers and other
947 pending calls:
948 - If we're about to enter the try: of a try/finally (not
949 *very* useful, but might help in some cases and it's
950 traditional)
951 - If we're resuming a chain of nested 'yield from' or
952 'await' calls, then each frame is parked with YIELD_FROM
953 as its next opcode. If the user hit control-C we want to
954 wait until we've reached the innermost frame before
955 running the signal handler and raising KeyboardInterrupt
956 (see bpo-30039).
957 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 goto fast_next_opcode;
959 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600960 if (_Py_atomic_load_relaxed(
961 &_PyRuntime.ceval.pending.calls_to_do))
962 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400963 if (Py_MakePendingCalls() < 0)
964 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600966 if (_Py_atomic_load_relaxed(
967 &_PyRuntime.ceval.gil_drop_request))
968 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 /* Give another thread a chance */
970 if (PyThreadState_Swap(NULL) != tstate)
971 Py_FatalError("ceval: tstate mix-up");
972 drop_gil(tstate);
973
974 /* Other threads may run now */
975
976 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -0700977
978 /* Check if we should make a quick exit. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600979 if (_Py_IsFinalizing() &&
980 !_Py_CURRENTLY_FINALIZING(tstate))
981 {
Benjamin Peterson17548dd2014-06-16 22:59:07 -0700982 drop_gil(tstate);
983 PyThread_exit_thread();
984 }
985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 if (PyThreadState_Swap(tstate) != NULL)
987 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 }
989 /* Check for asynchronous exceptions. */
990 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400991 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 tstate->async_exc = NULL;
993 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400994 PyErr_SetNone(exc);
995 Py_DECREF(exc);
996 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 }
998 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 fast_next_opcode:
1001 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001002
Łukasz Langaa785c872016-09-09 17:37:37 -07001003 if (PyDTrace_LINE_ENABLED())
1004 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001009 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001010 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 /* see maybe_call_line_trace
1012 for expository comments */
1013 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001014
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 err = maybe_call_line_trace(tstate->c_tracefunc,
1016 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001017 tstate, f,
1018 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 /* Reload possibly changed frame fields */
1020 JUMPTO(f->f_lasti);
1021 if (f->f_stacktop != NULL) {
1022 stack_pointer = f->f_stacktop;
1023 f->f_stacktop = NULL;
1024 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001025 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001027 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001031
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001032 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001033 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001034#ifdef DYNAMIC_EXECUTION_PROFILE
1035#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 dxpairs[lastopcode][opcode]++;
1037 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001038#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001040#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001041
Guido van Rossum96a42c81992-01-12 02:29:51 +00001042#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 if (lltrace) {
1046 if (HAS_ARG(opcode)) {
1047 printf("%d: %d, %d\n",
1048 f->f_lasti, opcode, oparg);
1049 }
1050 else {
1051 printf("%d: %d\n",
1052 f->f_lasti, opcode);
1053 }
1054 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001055#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 /* BEWARE!
1060 It is essential that any operation that fails sets either
1061 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1062 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 TARGET(NOP)
1065 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001066
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001067 TARGET(LOAD_FAST) {
1068 PyObject *value = GETLOCAL(oparg);
1069 if (value == NULL) {
1070 format_exc_check_arg(PyExc_UnboundLocalError,
1071 UNBOUNDLOCAL_ERROR_MSG,
1072 PyTuple_GetItem(co->co_varnames, oparg));
1073 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001075 Py_INCREF(value);
1076 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001078 }
1079
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001080 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001081 TARGET(LOAD_CONST) {
1082 PyObject *value = GETITEM(consts, oparg);
1083 Py_INCREF(value);
1084 PUSH(value);
1085 FAST_DISPATCH();
1086 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001087
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001088 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001089 TARGET(STORE_FAST) {
1090 PyObject *value = POP();
1091 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001093 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001094
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001095 TARGET(POP_TOP) {
1096 PyObject *value = POP();
1097 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001099 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001100
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001101 TARGET(ROT_TWO) {
1102 PyObject *top = TOP();
1103 PyObject *second = SECOND();
1104 SET_TOP(second);
1105 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001107 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001108
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001109 TARGET(ROT_THREE) {
1110 PyObject *top = TOP();
1111 PyObject *second = SECOND();
1112 PyObject *third = THIRD();
1113 SET_TOP(second);
1114 SET_SECOND(third);
1115 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001117 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001118
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001119 TARGET(DUP_TOP) {
1120 PyObject *top = TOP();
1121 Py_INCREF(top);
1122 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001124 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001125
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001126 TARGET(DUP_TOP_TWO) {
1127 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001128 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001129 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001130 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001131 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001132 SET_TOP(top);
1133 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001134 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001135 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001136
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001137 TARGET(UNARY_POSITIVE) {
1138 PyObject *value = TOP();
1139 PyObject *res = PyNumber_Positive(value);
1140 Py_DECREF(value);
1141 SET_TOP(res);
1142 if (res == NULL)
1143 goto error;
1144 DISPATCH();
1145 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001146
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001147 TARGET(UNARY_NEGATIVE) {
1148 PyObject *value = TOP();
1149 PyObject *res = PyNumber_Negative(value);
1150 Py_DECREF(value);
1151 SET_TOP(res);
1152 if (res == NULL)
1153 goto error;
1154 DISPATCH();
1155 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001156
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001157 TARGET(UNARY_NOT) {
1158 PyObject *value = TOP();
1159 int err = PyObject_IsTrue(value);
1160 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 if (err == 0) {
1162 Py_INCREF(Py_True);
1163 SET_TOP(Py_True);
1164 DISPATCH();
1165 }
1166 else if (err > 0) {
1167 Py_INCREF(Py_False);
1168 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 DISPATCH();
1170 }
1171 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001172 goto error;
1173 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001174
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001175 TARGET(UNARY_INVERT) {
1176 PyObject *value = TOP();
1177 PyObject *res = PyNumber_Invert(value);
1178 Py_DECREF(value);
1179 SET_TOP(res);
1180 if (res == NULL)
1181 goto error;
1182 DISPATCH();
1183 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001184
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001185 TARGET(BINARY_POWER) {
1186 PyObject *exp = POP();
1187 PyObject *base = TOP();
1188 PyObject *res = PyNumber_Power(base, exp, Py_None);
1189 Py_DECREF(base);
1190 Py_DECREF(exp);
1191 SET_TOP(res);
1192 if (res == NULL)
1193 goto error;
1194 DISPATCH();
1195 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001196
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001197 TARGET(BINARY_MULTIPLY) {
1198 PyObject *right = POP();
1199 PyObject *left = TOP();
1200 PyObject *res = PyNumber_Multiply(left, right);
1201 Py_DECREF(left);
1202 Py_DECREF(right);
1203 SET_TOP(res);
1204 if (res == NULL)
1205 goto error;
1206 DISPATCH();
1207 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001208
Benjamin Petersond51374e2014-04-09 23:55:56 -04001209 TARGET(BINARY_MATRIX_MULTIPLY) {
1210 PyObject *right = POP();
1211 PyObject *left = TOP();
1212 PyObject *res = PyNumber_MatrixMultiply(left, right);
1213 Py_DECREF(left);
1214 Py_DECREF(right);
1215 SET_TOP(res);
1216 if (res == NULL)
1217 goto error;
1218 DISPATCH();
1219 }
1220
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001221 TARGET(BINARY_TRUE_DIVIDE) {
1222 PyObject *divisor = POP();
1223 PyObject *dividend = TOP();
1224 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1225 Py_DECREF(dividend);
1226 Py_DECREF(divisor);
1227 SET_TOP(quotient);
1228 if (quotient == NULL)
1229 goto error;
1230 DISPATCH();
1231 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001232
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001233 TARGET(BINARY_FLOOR_DIVIDE) {
1234 PyObject *divisor = POP();
1235 PyObject *dividend = TOP();
1236 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1237 Py_DECREF(dividend);
1238 Py_DECREF(divisor);
1239 SET_TOP(quotient);
1240 if (quotient == NULL)
1241 goto error;
1242 DISPATCH();
1243 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001244
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001245 TARGET(BINARY_MODULO) {
1246 PyObject *divisor = POP();
1247 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001248 PyObject *res;
1249 if (PyUnicode_CheckExact(dividend) && (
1250 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1251 // fast path; string formatting, but not if the RHS is a str subclass
1252 // (see issue28598)
1253 res = PyUnicode_Format(dividend, divisor);
1254 } else {
1255 res = PyNumber_Remainder(dividend, divisor);
1256 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001257 Py_DECREF(divisor);
1258 Py_DECREF(dividend);
1259 SET_TOP(res);
1260 if (res == NULL)
1261 goto error;
1262 DISPATCH();
1263 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001264
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001265 TARGET(BINARY_ADD) {
1266 PyObject *right = POP();
1267 PyObject *left = TOP();
1268 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001269 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1270 CPython using bytecode, it is simply worthless.
1271 See http://bugs.python.org/issue21955 and
1272 http://bugs.python.org/issue10044 for the discussion. In short,
1273 no patch shown any impact on a realistic benchmark, only a minor
1274 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001275 if (PyUnicode_CheckExact(left) &&
1276 PyUnicode_CheckExact(right)) {
1277 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001278 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001279 }
1280 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001281 sum = PyNumber_Add(left, right);
1282 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001283 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001284 Py_DECREF(right);
1285 SET_TOP(sum);
1286 if (sum == NULL)
1287 goto error;
1288 DISPATCH();
1289 }
1290
1291 TARGET(BINARY_SUBTRACT) {
1292 PyObject *right = POP();
1293 PyObject *left = TOP();
1294 PyObject *diff = PyNumber_Subtract(left, right);
1295 Py_DECREF(right);
1296 Py_DECREF(left);
1297 SET_TOP(diff);
1298 if (diff == NULL)
1299 goto error;
1300 DISPATCH();
1301 }
1302
1303 TARGET(BINARY_SUBSCR) {
1304 PyObject *sub = POP();
1305 PyObject *container = TOP();
1306 PyObject *res = PyObject_GetItem(container, sub);
1307 Py_DECREF(container);
1308 Py_DECREF(sub);
1309 SET_TOP(res);
1310 if (res == NULL)
1311 goto error;
1312 DISPATCH();
1313 }
1314
1315 TARGET(BINARY_LSHIFT) {
1316 PyObject *right = POP();
1317 PyObject *left = TOP();
1318 PyObject *res = PyNumber_Lshift(left, right);
1319 Py_DECREF(left);
1320 Py_DECREF(right);
1321 SET_TOP(res);
1322 if (res == NULL)
1323 goto error;
1324 DISPATCH();
1325 }
1326
1327 TARGET(BINARY_RSHIFT) {
1328 PyObject *right = POP();
1329 PyObject *left = TOP();
1330 PyObject *res = PyNumber_Rshift(left, right);
1331 Py_DECREF(left);
1332 Py_DECREF(right);
1333 SET_TOP(res);
1334 if (res == NULL)
1335 goto error;
1336 DISPATCH();
1337 }
1338
1339 TARGET(BINARY_AND) {
1340 PyObject *right = POP();
1341 PyObject *left = TOP();
1342 PyObject *res = PyNumber_And(left, right);
1343 Py_DECREF(left);
1344 Py_DECREF(right);
1345 SET_TOP(res);
1346 if (res == NULL)
1347 goto error;
1348 DISPATCH();
1349 }
1350
1351 TARGET(BINARY_XOR) {
1352 PyObject *right = POP();
1353 PyObject *left = TOP();
1354 PyObject *res = PyNumber_Xor(left, right);
1355 Py_DECREF(left);
1356 Py_DECREF(right);
1357 SET_TOP(res);
1358 if (res == NULL)
1359 goto error;
1360 DISPATCH();
1361 }
1362
1363 TARGET(BINARY_OR) {
1364 PyObject *right = POP();
1365 PyObject *left = TOP();
1366 PyObject *res = PyNumber_Or(left, right);
1367 Py_DECREF(left);
1368 Py_DECREF(right);
1369 SET_TOP(res);
1370 if (res == NULL)
1371 goto error;
1372 DISPATCH();
1373 }
1374
1375 TARGET(LIST_APPEND) {
1376 PyObject *v = POP();
1377 PyObject *list = PEEK(oparg);
1378 int err;
1379 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001381 if (err != 0)
1382 goto error;
1383 PREDICT(JUMP_ABSOLUTE);
1384 DISPATCH();
1385 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001386
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001387 TARGET(SET_ADD) {
1388 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001389 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001390 int err;
1391 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001393 if (err != 0)
1394 goto error;
1395 PREDICT(JUMP_ABSOLUTE);
1396 DISPATCH();
1397 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001398
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001399 TARGET(INPLACE_POWER) {
1400 PyObject *exp = POP();
1401 PyObject *base = TOP();
1402 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1403 Py_DECREF(base);
1404 Py_DECREF(exp);
1405 SET_TOP(res);
1406 if (res == NULL)
1407 goto error;
1408 DISPATCH();
1409 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001410
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001411 TARGET(INPLACE_MULTIPLY) {
1412 PyObject *right = POP();
1413 PyObject *left = TOP();
1414 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1415 Py_DECREF(left);
1416 Py_DECREF(right);
1417 SET_TOP(res);
1418 if (res == NULL)
1419 goto error;
1420 DISPATCH();
1421 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001422
Benjamin Petersond51374e2014-04-09 23:55:56 -04001423 TARGET(INPLACE_MATRIX_MULTIPLY) {
1424 PyObject *right = POP();
1425 PyObject *left = TOP();
1426 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1427 Py_DECREF(left);
1428 Py_DECREF(right);
1429 SET_TOP(res);
1430 if (res == NULL)
1431 goto error;
1432 DISPATCH();
1433 }
1434
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001435 TARGET(INPLACE_TRUE_DIVIDE) {
1436 PyObject *divisor = POP();
1437 PyObject *dividend = TOP();
1438 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1439 Py_DECREF(dividend);
1440 Py_DECREF(divisor);
1441 SET_TOP(quotient);
1442 if (quotient == NULL)
1443 goto error;
1444 DISPATCH();
1445 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001446
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001447 TARGET(INPLACE_FLOOR_DIVIDE) {
1448 PyObject *divisor = POP();
1449 PyObject *dividend = TOP();
1450 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1451 Py_DECREF(dividend);
1452 Py_DECREF(divisor);
1453 SET_TOP(quotient);
1454 if (quotient == NULL)
1455 goto error;
1456 DISPATCH();
1457 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001458
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001459 TARGET(INPLACE_MODULO) {
1460 PyObject *right = POP();
1461 PyObject *left = TOP();
1462 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1463 Py_DECREF(left);
1464 Py_DECREF(right);
1465 SET_TOP(mod);
1466 if (mod == NULL)
1467 goto error;
1468 DISPATCH();
1469 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001470
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001471 TARGET(INPLACE_ADD) {
1472 PyObject *right = POP();
1473 PyObject *left = TOP();
1474 PyObject *sum;
1475 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1476 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001477 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001478 }
1479 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001480 sum = PyNumber_InPlaceAdd(left, right);
1481 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001482 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001483 Py_DECREF(right);
1484 SET_TOP(sum);
1485 if (sum == NULL)
1486 goto error;
1487 DISPATCH();
1488 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001489
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001490 TARGET(INPLACE_SUBTRACT) {
1491 PyObject *right = POP();
1492 PyObject *left = TOP();
1493 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1494 Py_DECREF(left);
1495 Py_DECREF(right);
1496 SET_TOP(diff);
1497 if (diff == NULL)
1498 goto error;
1499 DISPATCH();
1500 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001501
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001502 TARGET(INPLACE_LSHIFT) {
1503 PyObject *right = POP();
1504 PyObject *left = TOP();
1505 PyObject *res = PyNumber_InPlaceLshift(left, right);
1506 Py_DECREF(left);
1507 Py_DECREF(right);
1508 SET_TOP(res);
1509 if (res == NULL)
1510 goto error;
1511 DISPATCH();
1512 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001513
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001514 TARGET(INPLACE_RSHIFT) {
1515 PyObject *right = POP();
1516 PyObject *left = TOP();
1517 PyObject *res = PyNumber_InPlaceRshift(left, right);
1518 Py_DECREF(left);
1519 Py_DECREF(right);
1520 SET_TOP(res);
1521 if (res == NULL)
1522 goto error;
1523 DISPATCH();
1524 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001525
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001526 TARGET(INPLACE_AND) {
1527 PyObject *right = POP();
1528 PyObject *left = TOP();
1529 PyObject *res = PyNumber_InPlaceAnd(left, right);
1530 Py_DECREF(left);
1531 Py_DECREF(right);
1532 SET_TOP(res);
1533 if (res == NULL)
1534 goto error;
1535 DISPATCH();
1536 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001537
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001538 TARGET(INPLACE_XOR) {
1539 PyObject *right = POP();
1540 PyObject *left = TOP();
1541 PyObject *res = PyNumber_InPlaceXor(left, right);
1542 Py_DECREF(left);
1543 Py_DECREF(right);
1544 SET_TOP(res);
1545 if (res == NULL)
1546 goto error;
1547 DISPATCH();
1548 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001549
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001550 TARGET(INPLACE_OR) {
1551 PyObject *right = POP();
1552 PyObject *left = TOP();
1553 PyObject *res = PyNumber_InPlaceOr(left, right);
1554 Py_DECREF(left);
1555 Py_DECREF(right);
1556 SET_TOP(res);
1557 if (res == NULL)
1558 goto error;
1559 DISPATCH();
1560 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001561
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001562 TARGET(STORE_SUBSCR) {
1563 PyObject *sub = TOP();
1564 PyObject *container = SECOND();
1565 PyObject *v = THIRD();
1566 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 STACKADJ(-3);
Martin Panter95f53c12016-07-18 08:23:26 +00001568 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001569 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001571 Py_DECREF(container);
1572 Py_DECREF(sub);
1573 if (err != 0)
1574 goto error;
1575 DISPATCH();
1576 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001577
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001578 TARGET(DELETE_SUBSCR) {
1579 PyObject *sub = TOP();
1580 PyObject *container = SECOND();
1581 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 STACKADJ(-2);
Martin Panter95f53c12016-07-18 08:23:26 +00001583 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001584 err = PyObject_DelItem(container, sub);
1585 Py_DECREF(container);
1586 Py_DECREF(sub);
1587 if (err != 0)
1588 goto error;
1589 DISPATCH();
1590 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001591
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001592 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001593 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001594 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001595 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001596 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001597 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 PyErr_SetString(PyExc_RuntimeError,
1599 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001600 Py_DECREF(value);
1601 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001602 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001603 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001604 Py_DECREF(value);
1605 if (res == NULL)
1606 goto error;
1607 Py_DECREF(res);
1608 DISPATCH();
1609 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001610
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001611 TARGET(RAISE_VARARGS) {
1612 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 switch (oparg) {
1614 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001615 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001616 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001618 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001619 /* fall through */
1620 case 0:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001621 if (do_raise(exc, cause)) {
1622 why = WHY_EXCEPTION;
1623 goto fast_block_end;
1624 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 break;
1626 default:
1627 PyErr_SetString(PyExc_SystemError,
1628 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 break;
1630 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001631 goto error;
1632 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001633
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001634 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635 retval = POP();
1636 why = WHY_RETURN;
1637 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001638 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001639
Yury Selivanov75445082015-05-11 22:57:16 -04001640 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001641 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001642 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001643 PyObject *obj = TOP();
1644 PyTypeObject *type = Py_TYPE(obj);
1645
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001646 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001647 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001648 }
Yury Selivanov75445082015-05-11 22:57:16 -04001649
1650 if (getter != NULL) {
1651 iter = (*getter)(obj);
1652 Py_DECREF(obj);
1653 if (iter == NULL) {
1654 SET_TOP(NULL);
1655 goto error;
1656 }
1657 }
1658 else {
1659 SET_TOP(NULL);
1660 PyErr_Format(
1661 PyExc_TypeError,
1662 "'async for' requires an object with "
1663 "__aiter__ method, got %.100s",
1664 type->tp_name);
1665 Py_DECREF(obj);
1666 goto error;
1667 }
1668
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001669 if (Py_TYPE(iter)->tp_as_async == NULL ||
1670 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001671
Yury Selivanov398ff912017-03-02 22:20:00 -05001672 SET_TOP(NULL);
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001673 PyErr_Format(
1674 PyExc_TypeError,
1675 "'async for' received an object from __aiter__ "
1676 "that does not implement __anext__: %.100s",
1677 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001678 Py_DECREF(iter);
1679 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001680 }
1681
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001682 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001683 DISPATCH();
1684 }
1685
1686 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001687 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001688 PyObject *next_iter = NULL;
1689 PyObject *awaitable = NULL;
1690 PyObject *aiter = TOP();
1691 PyTypeObject *type = Py_TYPE(aiter);
1692
Yury Selivanoveb636452016-09-08 22:01:51 -07001693 if (PyAsyncGen_CheckExact(aiter)) {
1694 awaitable = type->tp_as_async->am_anext(aiter);
1695 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001696 goto error;
1697 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001698 } else {
1699 if (type->tp_as_async != NULL){
1700 getter = type->tp_as_async->am_anext;
1701 }
Yury Selivanov75445082015-05-11 22:57:16 -04001702
Yury Selivanoveb636452016-09-08 22:01:51 -07001703 if (getter != NULL) {
1704 next_iter = (*getter)(aiter);
1705 if (next_iter == NULL) {
1706 goto error;
1707 }
1708 }
1709 else {
1710 PyErr_Format(
1711 PyExc_TypeError,
1712 "'async for' requires an iterator with "
1713 "__anext__ method, got %.100s",
1714 type->tp_name);
1715 goto error;
1716 }
Yury Selivanov75445082015-05-11 22:57:16 -04001717
Yury Selivanoveb636452016-09-08 22:01:51 -07001718 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1719 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001720 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001721 PyExc_TypeError,
1722 "'async for' received an invalid object "
1723 "from __anext__: %.100s",
1724 Py_TYPE(next_iter)->tp_name);
1725
1726 Py_DECREF(next_iter);
1727 goto error;
1728 } else {
1729 Py_DECREF(next_iter);
1730 }
1731 }
Yury Selivanov75445082015-05-11 22:57:16 -04001732
1733 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001734 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001735 DISPATCH();
1736 }
1737
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001738 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001739 TARGET(GET_AWAITABLE) {
1740 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001741 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001742
Miss Islington (bot)fcd4e032018-04-04 07:09:00 -07001743 if (iter == NULL) {
1744 format_awaitable_error(Py_TYPE(iterable),
1745 _Py_OPCODE(next_instr[-2]));
1746 }
1747
Yury Selivanov75445082015-05-11 22:57:16 -04001748 Py_DECREF(iterable);
1749
Yury Selivanovc724bae2016-03-02 11:30:46 -05001750 if (iter != NULL && PyCoro_CheckExact(iter)) {
1751 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1752 if (yf != NULL) {
1753 /* `iter` is a coroutine object that is being
1754 awaited, `yf` is a pointer to the current awaitable
1755 being awaited on. */
1756 Py_DECREF(yf);
1757 Py_CLEAR(iter);
1758 PyErr_SetString(
1759 PyExc_RuntimeError,
1760 "coroutine is being awaited already");
1761 /* The code below jumps to `error` if `iter` is NULL. */
1762 }
1763 }
1764
Yury Selivanov75445082015-05-11 22:57:16 -04001765 SET_TOP(iter); /* Even if it's NULL */
1766
1767 if (iter == NULL) {
1768 goto error;
1769 }
1770
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001771 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001772 DISPATCH();
1773 }
1774
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001775 TARGET(YIELD_FROM) {
1776 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001777 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001778 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001779 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1780 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001781 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001782 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001783 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001784 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001785 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001786 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001787 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001788 Py_DECREF(v);
1789 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001790 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001791 if (tstate->c_tracefunc != NULL
1792 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001793 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001794 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001795 if (err < 0)
1796 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001797 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001798 SET_TOP(val);
1799 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001800 }
Martin Panter95f53c12016-07-18 08:23:26 +00001801 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001802 f->f_stacktop = stack_pointer;
1803 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001804 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001805 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001806 f->f_lasti -= sizeof(_Py_CODEUNIT);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001807 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001808 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001809
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001810 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07001812
1813 if (co->co_flags & CO_ASYNC_GENERATOR) {
1814 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
1815 Py_DECREF(retval);
1816 if (w == NULL) {
1817 retval = NULL;
1818 goto error;
1819 }
1820 retval = w;
1821 }
1822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001823 f->f_stacktop = stack_pointer;
1824 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001826 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001827
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001828 TARGET(POP_EXCEPT) {
1829 PyTryBlock *b = PyFrame_BlockPop(f);
1830 if (b->b_type != EXCEPT_HANDLER) {
1831 PyErr_SetString(PyExc_SystemError,
1832 "popped block is not an except handler");
1833 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001835 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001837 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001838
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001839 PREDICTED(POP_BLOCK);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001840 TARGET(POP_BLOCK) {
1841 PyTryBlock *b = PyFrame_BlockPop(f);
1842 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001843 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001844 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001846 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001847 TARGET(END_FINALLY) {
1848 PyObject *status = POP();
1849 if (PyLong_Check(status)) {
1850 why = (enum why_code) PyLong_AS_LONG(status);
1851 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001852 if (why == WHY_RETURN ||
1853 why == WHY_CONTINUE)
1854 retval = POP();
1855 if (why == WHY_SILENCED) {
1856 /* An exception was silenced by 'with', we must
1857 manually unwind the EXCEPT_HANDLER block which was
1858 created when the exception was caught, otherwise
1859 the stack will be in an inconsistent state. */
1860 PyTryBlock *b = PyFrame_BlockPop(f);
1861 assert(b->b_type == EXCEPT_HANDLER);
1862 UNWIND_EXCEPT_HANDLER(b);
1863 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001864 Py_DECREF(status);
1865 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001867 Py_DECREF(status);
1868 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001870 else if (PyExceptionClass_Check(status)) {
1871 PyObject *exc = POP();
1872 PyObject *tb = POP();
1873 PyErr_Restore(status, exc, tb);
1874 why = WHY_EXCEPTION;
1875 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001877 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 PyErr_SetString(PyExc_SystemError,
1879 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001880 Py_DECREF(status);
1881 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001883 Py_DECREF(status);
1884 DISPATCH();
1885 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001886
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001887 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02001888 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001889
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001890 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001891 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001892 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
1893 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02001894 PyErr_SetString(PyExc_NameError,
1895 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001896 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001897 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001898 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001899 }
1900 else {
1901 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
1902 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02001903 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001904 bc = PyObject_GetItem(f->f_builtins, build_class_str);
1905 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02001906 if (PyErr_ExceptionMatches(PyExc_KeyError))
1907 PyErr_SetString(PyExc_NameError,
1908 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001909 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001910 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001912 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04001913 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02001914 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001915
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001916 TARGET(STORE_NAME) {
1917 PyObject *name = GETITEM(names, oparg);
1918 PyObject *v = POP();
1919 PyObject *ns = f->f_locals;
1920 int err;
1921 if (ns == NULL) {
1922 PyErr_Format(PyExc_SystemError,
1923 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001925 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001927 if (PyDict_CheckExact(ns))
1928 err = PyDict_SetItem(ns, name, v);
1929 else
1930 err = PyObject_SetItem(ns, name, v);
1931 Py_DECREF(v);
1932 if (err != 0)
1933 goto error;
1934 DISPATCH();
1935 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001936
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001937 TARGET(DELETE_NAME) {
1938 PyObject *name = GETITEM(names, oparg);
1939 PyObject *ns = f->f_locals;
1940 int err;
1941 if (ns == NULL) {
1942 PyErr_Format(PyExc_SystemError,
1943 "no locals when deleting %R", name);
1944 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001946 err = PyObject_DelItem(ns, name);
1947 if (err != 0) {
1948 format_exc_check_arg(PyExc_NameError,
1949 NAME_ERROR_MSG,
1950 name);
1951 goto error;
1952 }
1953 DISPATCH();
1954 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00001955
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001956 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001957 TARGET(UNPACK_SEQUENCE) {
1958 PyObject *seq = POP(), *item, **items;
1959 if (PyTuple_CheckExact(seq) &&
1960 PyTuple_GET_SIZE(seq) == oparg) {
1961 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001963 item = items[oparg];
1964 Py_INCREF(item);
1965 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001967 } else if (PyList_CheckExact(seq) &&
1968 PyList_GET_SIZE(seq) == oparg) {
1969 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001971 item = items[oparg];
1972 Py_INCREF(item);
1973 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001975 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 stack_pointer + oparg)) {
1977 STACKADJ(oparg);
1978 } else {
1979 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001980 Py_DECREF(seq);
1981 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001983 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04001984 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985 }
Guido van Rossum0368b722007-05-11 16:50:42 +00001986
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001987 TARGET(UNPACK_EX) {
1988 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
1989 PyObject *seq = POP();
1990
1991 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
1992 stack_pointer + totalargs)) {
1993 stack_pointer += totalargs;
1994 } else {
1995 Py_DECREF(seq);
1996 goto error;
1997 }
1998 Py_DECREF(seq);
1999 DISPATCH();
2000 }
2001
2002 TARGET(STORE_ATTR) {
2003 PyObject *name = GETITEM(names, oparg);
2004 PyObject *owner = TOP();
2005 PyObject *v = SECOND();
2006 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002008 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002010 Py_DECREF(owner);
2011 if (err != 0)
2012 goto error;
2013 DISPATCH();
2014 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002015
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002016 TARGET(DELETE_ATTR) {
2017 PyObject *name = GETITEM(names, oparg);
2018 PyObject *owner = POP();
2019 int err;
2020 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2021 Py_DECREF(owner);
2022 if (err != 0)
2023 goto error;
2024 DISPATCH();
2025 }
2026
2027 TARGET(STORE_GLOBAL) {
2028 PyObject *name = GETITEM(names, oparg);
2029 PyObject *v = POP();
2030 int err;
2031 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002032 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002033 if (err != 0)
2034 goto error;
2035 DISPATCH();
2036 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002037
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002038 TARGET(DELETE_GLOBAL) {
2039 PyObject *name = GETITEM(names, oparg);
2040 int err;
2041 err = PyDict_DelItem(f->f_globals, name);
2042 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002043 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002044 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002045 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002046 }
2047 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002048 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002049
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002050 TARGET(LOAD_NAME) {
2051 PyObject *name = GETITEM(names, oparg);
2052 PyObject *locals = f->f_locals;
2053 PyObject *v;
2054 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002055 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002056 "no locals when loading %R", name);
2057 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002058 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002059 if (PyDict_CheckExact(locals)) {
2060 v = PyDict_GetItem(locals, name);
2061 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 }
2063 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002064 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002065 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002066 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2067 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002068 PyErr_Clear();
2069 }
2070 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002071 if (v == NULL) {
2072 v = PyDict_GetItem(f->f_globals, name);
2073 Py_XINCREF(v);
2074 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002075 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002076 v = PyDict_GetItem(f->f_builtins, name);
2077 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002078 format_exc_check_arg(
2079 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002080 NAME_ERROR_MSG, name);
2081 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002082 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002083 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002084 }
2085 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002086 v = PyObject_GetItem(f->f_builtins, name);
2087 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002088 if (PyErr_ExceptionMatches(PyExc_KeyError))
2089 format_exc_check_arg(
2090 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002091 NAME_ERROR_MSG, name);
2092 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002093 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002094 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002097 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002098 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002099 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002100
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002101 TARGET(LOAD_GLOBAL) {
2102 PyObject *name = GETITEM(names, oparg);
2103 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002104 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002105 && PyDict_CheckExact(f->f_builtins))
2106 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002107 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002108 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002109 name);
2110 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002111 if (!_PyErr_OCCURRED()) {
2112 /* _PyDict_LoadGlobal() returns NULL without raising
2113 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002114 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002115 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002116 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002117 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002118 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002119 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002120 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002121 else {
2122 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002123
2124 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002125 v = PyObject_GetItem(f->f_globals, name);
2126 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002127 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2128 goto error;
2129 PyErr_Clear();
2130
Victor Stinnerb4efc962015-11-20 09:24:02 +01002131 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002132 v = PyObject_GetItem(f->f_builtins, name);
2133 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002134 if (PyErr_ExceptionMatches(PyExc_KeyError))
2135 format_exc_check_arg(
2136 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002137 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002138 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002139 }
2140 }
2141 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002142 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002143 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002144 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002145
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002146 TARGET(DELETE_FAST) {
2147 PyObject *v = GETLOCAL(oparg);
2148 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002149 SETLOCAL(oparg, NULL);
2150 DISPATCH();
2151 }
2152 format_exc_check_arg(
2153 PyExc_UnboundLocalError,
2154 UNBOUNDLOCAL_ERROR_MSG,
2155 PyTuple_GetItem(co->co_varnames, oparg)
2156 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002157 goto error;
2158 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002159
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002160 TARGET(DELETE_DEREF) {
2161 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002162 PyObject *oldobj = PyCell_GET(cell);
2163 if (oldobj != NULL) {
2164 PyCell_SET(cell, NULL);
2165 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002166 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002167 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002168 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002169 goto error;
2170 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002171
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002172 TARGET(LOAD_CLOSURE) {
2173 PyObject *cell = freevars[oparg];
2174 Py_INCREF(cell);
2175 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002176 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002177 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002178
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002179 TARGET(LOAD_CLASSDEREF) {
2180 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002181 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002182 assert(locals);
2183 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2184 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2185 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2186 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2187 if (PyDict_CheckExact(locals)) {
2188 value = PyDict_GetItem(locals, name);
2189 Py_XINCREF(value);
2190 }
2191 else {
2192 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002193 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002194 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2195 goto error;
2196 PyErr_Clear();
2197 }
2198 }
2199 if (!value) {
2200 PyObject *cell = freevars[oparg];
2201 value = PyCell_GET(cell);
2202 if (value == NULL) {
2203 format_exc_unbound(co, oparg);
2204 goto error;
2205 }
2206 Py_INCREF(value);
2207 }
2208 PUSH(value);
2209 DISPATCH();
2210 }
2211
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002212 TARGET(LOAD_DEREF) {
2213 PyObject *cell = freevars[oparg];
2214 PyObject *value = PyCell_GET(cell);
2215 if (value == NULL) {
2216 format_exc_unbound(co, oparg);
2217 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002219 Py_INCREF(value);
2220 PUSH(value);
2221 DISPATCH();
2222 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002223
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002224 TARGET(STORE_DEREF) {
2225 PyObject *v = POP();
2226 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002227 PyObject *oldobj = PyCell_GET(cell);
2228 PyCell_SET(cell, v);
2229 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002230 DISPATCH();
2231 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002232
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002233 TARGET(BUILD_STRING) {
2234 PyObject *str;
2235 PyObject *empty = PyUnicode_New(0, 0);
2236 if (empty == NULL) {
2237 goto error;
2238 }
2239 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2240 Py_DECREF(empty);
2241 if (str == NULL)
2242 goto error;
2243 while (--oparg >= 0) {
2244 PyObject *item = POP();
2245 Py_DECREF(item);
2246 }
2247 PUSH(str);
2248 DISPATCH();
2249 }
2250
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002251 TARGET(BUILD_TUPLE) {
2252 PyObject *tup = PyTuple_New(oparg);
2253 if (tup == NULL)
2254 goto error;
2255 while (--oparg >= 0) {
2256 PyObject *item = POP();
2257 PyTuple_SET_ITEM(tup, oparg, item);
2258 }
2259 PUSH(tup);
2260 DISPATCH();
2261 }
2262
2263 TARGET(BUILD_LIST) {
2264 PyObject *list = PyList_New(oparg);
2265 if (list == NULL)
2266 goto error;
2267 while (--oparg >= 0) {
2268 PyObject *item = POP();
2269 PyList_SET_ITEM(list, oparg, item);
2270 }
2271 PUSH(list);
2272 DISPATCH();
2273 }
2274
Serhiy Storchaka73442852016-10-02 10:33:46 +03002275 TARGET(BUILD_TUPLE_UNPACK_WITH_CALL)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002276 TARGET(BUILD_TUPLE_UNPACK)
2277 TARGET(BUILD_LIST_UNPACK) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002278 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002279 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002280 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002281 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002282
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002283 if (sum == NULL)
2284 goto error;
2285
2286 for (i = oparg; i > 0; i--) {
2287 PyObject *none_val;
2288
2289 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2290 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002291 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002292 PyErr_ExceptionMatches(PyExc_TypeError))
2293 {
2294 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002295 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002296 Py_DECREF(sum);
2297 goto error;
2298 }
2299 Py_DECREF(none_val);
2300 }
2301
2302 if (convert_to_tuple) {
2303 return_value = PyList_AsTuple(sum);
2304 Py_DECREF(sum);
2305 if (return_value == NULL)
2306 goto error;
2307 }
2308 else {
2309 return_value = sum;
2310 }
2311
2312 while (oparg--)
2313 Py_DECREF(POP());
2314 PUSH(return_value);
2315 DISPATCH();
2316 }
2317
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002318 TARGET(BUILD_SET) {
2319 PyObject *set = PySet_New(NULL);
2320 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002321 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002322 if (set == NULL)
2323 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002324 for (i = oparg; i > 0; i--) {
2325 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002326 if (err == 0)
2327 err = PySet_Add(set, item);
2328 Py_DECREF(item);
2329 }
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002330 STACKADJ(-oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002331 if (err != 0) {
2332 Py_DECREF(set);
2333 goto error;
2334 }
2335 PUSH(set);
2336 DISPATCH();
2337 }
2338
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002339 TARGET(BUILD_SET_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002340 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002341 PyObject *sum = PySet_New(NULL);
2342 if (sum == NULL)
2343 goto error;
2344
2345 for (i = oparg; i > 0; i--) {
2346 if (_PySet_Update(sum, PEEK(i)) < 0) {
2347 Py_DECREF(sum);
2348 goto error;
2349 }
2350 }
2351
2352 while (oparg--)
2353 Py_DECREF(POP());
2354 PUSH(sum);
2355 DISPATCH();
2356 }
2357
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002358 TARGET(BUILD_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002359 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002360 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2361 if (map == NULL)
2362 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002363 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002364 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002365 PyObject *key = PEEK(2*i);
2366 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002367 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002368 if (err != 0) {
2369 Py_DECREF(map);
2370 goto error;
2371 }
2372 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002373
2374 while (oparg--) {
2375 Py_DECREF(POP());
2376 Py_DECREF(POP());
2377 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002378 PUSH(map);
2379 DISPATCH();
2380 }
2381
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002382 TARGET(SETUP_ANNOTATIONS) {
2383 _Py_IDENTIFIER(__annotations__);
2384 int err;
2385 PyObject *ann_dict;
2386 if (f->f_locals == NULL) {
2387 PyErr_Format(PyExc_SystemError,
2388 "no locals found when setting up annotations");
2389 goto error;
2390 }
2391 /* check if __annotations__ in locals()... */
2392 if (PyDict_CheckExact(f->f_locals)) {
2393 ann_dict = _PyDict_GetItemId(f->f_locals,
2394 &PyId___annotations__);
2395 if (ann_dict == NULL) {
2396 /* ...if not, create a new one */
2397 ann_dict = PyDict_New();
2398 if (ann_dict == NULL) {
2399 goto error;
2400 }
2401 err = _PyDict_SetItemId(f->f_locals,
2402 &PyId___annotations__, ann_dict);
2403 Py_DECREF(ann_dict);
2404 if (err != 0) {
2405 goto error;
2406 }
2407 }
2408 }
2409 else {
2410 /* do the same if locals() is not a dict */
2411 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2412 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002413 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002414 }
2415 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2416 if (ann_dict == NULL) {
2417 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2418 goto error;
2419 }
2420 PyErr_Clear();
2421 ann_dict = PyDict_New();
2422 if (ann_dict == NULL) {
2423 goto error;
2424 }
2425 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2426 Py_DECREF(ann_dict);
2427 if (err != 0) {
2428 goto error;
2429 }
2430 }
2431 else {
2432 Py_DECREF(ann_dict);
2433 }
2434 }
2435 DISPATCH();
2436 }
2437
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002438 TARGET(BUILD_CONST_KEY_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002439 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002440 PyObject *map;
2441 PyObject *keys = TOP();
2442 if (!PyTuple_CheckExact(keys) ||
2443 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2444 PyErr_SetString(PyExc_SystemError,
2445 "bad BUILD_CONST_KEY_MAP keys argument");
2446 goto error;
2447 }
2448 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2449 if (map == NULL) {
2450 goto error;
2451 }
2452 for (i = oparg; i > 0; i--) {
2453 int err;
2454 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2455 PyObject *value = PEEK(i + 1);
2456 err = PyDict_SetItem(map, key, value);
2457 if (err != 0) {
2458 Py_DECREF(map);
2459 goto error;
2460 }
2461 }
2462
2463 Py_DECREF(POP());
2464 while (oparg--) {
2465 Py_DECREF(POP());
2466 }
2467 PUSH(map);
2468 DISPATCH();
2469 }
2470
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002471 TARGET(BUILD_MAP_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002472 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002473 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002474 if (sum == NULL)
2475 goto error;
2476
2477 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002478 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002479 if (PyDict_Update(sum, arg) < 0) {
2480 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2481 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002482 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002483 arg->ob_type->tp_name);
2484 }
2485 Py_DECREF(sum);
2486 goto error;
2487 }
2488 }
2489
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002490 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002491 Py_DECREF(POP());
2492 PUSH(sum);
2493 DISPATCH();
2494 }
2495
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002496 TARGET(BUILD_MAP_UNPACK_WITH_CALL) {
2497 Py_ssize_t i;
2498 PyObject *sum = PyDict_New();
2499 if (sum == NULL)
2500 goto error;
2501
2502 for (i = oparg; i > 0; i--) {
2503 PyObject *arg = PEEK(i);
2504 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
2505 PyObject *func = PEEK(2 + oparg);
2506 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002507 format_kwargs_mapping_error(func, arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002508 }
2509 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2510 PyObject *exc, *val, *tb;
2511 PyErr_Fetch(&exc, &val, &tb);
2512 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
2513 PyObject *key = PyTuple_GET_ITEM(val, 0);
2514 if (!PyUnicode_Check(key)) {
2515 PyErr_Format(PyExc_TypeError,
2516 "%.200s%.200s keywords must be strings",
2517 PyEval_GetFuncName(func),
2518 PyEval_GetFuncDesc(func));
2519 } else {
2520 PyErr_Format(PyExc_TypeError,
2521 "%.200s%.200s got multiple "
2522 "values for keyword argument '%U'",
2523 PyEval_GetFuncName(func),
2524 PyEval_GetFuncDesc(func),
2525 key);
2526 }
2527 Py_XDECREF(exc);
2528 Py_XDECREF(val);
2529 Py_XDECREF(tb);
2530 }
2531 else {
2532 PyErr_Restore(exc, val, tb);
2533 }
2534 }
2535 Py_DECREF(sum);
2536 goto error;
2537 }
2538 }
2539
2540 while (oparg--)
2541 Py_DECREF(POP());
2542 PUSH(sum);
2543 DISPATCH();
2544 }
2545
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002546 TARGET(MAP_ADD) {
2547 PyObject *key = TOP();
2548 PyObject *value = SECOND();
2549 PyObject *map;
2550 int err;
2551 STACKADJ(-2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002552 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002553 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002554 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002555 Py_DECREF(value);
2556 Py_DECREF(key);
2557 if (err != 0)
2558 goto error;
2559 PREDICT(JUMP_ABSOLUTE);
2560 DISPATCH();
2561 }
2562
2563 TARGET(LOAD_ATTR) {
2564 PyObject *name = GETITEM(names, oparg);
2565 PyObject *owner = TOP();
2566 PyObject *res = PyObject_GetAttr(owner, name);
2567 Py_DECREF(owner);
2568 SET_TOP(res);
2569 if (res == NULL)
2570 goto error;
2571 DISPATCH();
2572 }
2573
2574 TARGET(COMPARE_OP) {
2575 PyObject *right = POP();
2576 PyObject *left = TOP();
2577 PyObject *res = cmp_outcome(oparg, left, right);
2578 Py_DECREF(left);
2579 Py_DECREF(right);
2580 SET_TOP(res);
2581 if (res == NULL)
2582 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002583 PREDICT(POP_JUMP_IF_FALSE);
2584 PREDICT(POP_JUMP_IF_TRUE);
2585 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002586 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002587
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002588 TARGET(IMPORT_NAME) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002589 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002590 PyObject *fromlist = POP();
2591 PyObject *level = TOP();
2592 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002593 res = import_name(f, name, fromlist, level);
2594 Py_DECREF(level);
2595 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002596 SET_TOP(res);
2597 if (res == NULL)
2598 goto error;
2599 DISPATCH();
2600 }
2601
2602 TARGET(IMPORT_STAR) {
2603 PyObject *from = POP(), *locals;
2604 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002605 if (PyFrame_FastToLocalsWithError(f) < 0) {
2606 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002607 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002608 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002609
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002610 locals = f->f_locals;
2611 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002612 PyErr_SetString(PyExc_SystemError,
2613 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002614 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002615 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002616 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002617 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002618 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002619 Py_DECREF(from);
2620 if (err != 0)
2621 goto error;
2622 DISPATCH();
2623 }
Guido van Rossum25831651993-05-19 14:50:45 +00002624
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002625 TARGET(IMPORT_FROM) {
2626 PyObject *name = GETITEM(names, oparg);
2627 PyObject *from = TOP();
2628 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002629 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002630 PUSH(res);
2631 if (res == NULL)
2632 goto error;
2633 DISPATCH();
2634 }
Thomas Wouters52152252000-08-17 22:55:00 +00002635
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002636 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002637 JUMPBY(oparg);
2638 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002639 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002640
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002641 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002642 TARGET(POP_JUMP_IF_FALSE) {
2643 PyObject *cond = POP();
2644 int err;
2645 if (cond == Py_True) {
2646 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002647 FAST_DISPATCH();
2648 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002649 if (cond == Py_False) {
2650 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002651 JUMPTO(oparg);
2652 FAST_DISPATCH();
2653 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002654 err = PyObject_IsTrue(cond);
2655 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002656 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002657 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002658 else if (err == 0)
2659 JUMPTO(oparg);
2660 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002661 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002662 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002663 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002664
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002665 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002666 TARGET(POP_JUMP_IF_TRUE) {
2667 PyObject *cond = POP();
2668 int err;
2669 if (cond == Py_False) {
2670 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002671 FAST_DISPATCH();
2672 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002673 if (cond == Py_True) {
2674 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002675 JUMPTO(oparg);
2676 FAST_DISPATCH();
2677 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002678 err = PyObject_IsTrue(cond);
2679 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002680 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002681 JUMPTO(oparg);
2682 }
2683 else if (err == 0)
2684 ;
2685 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002686 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002687 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002688 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002689
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002690 TARGET(JUMP_IF_FALSE_OR_POP) {
2691 PyObject *cond = TOP();
2692 int err;
2693 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002694 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002695 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002696 FAST_DISPATCH();
2697 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002698 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002699 JUMPTO(oparg);
2700 FAST_DISPATCH();
2701 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002702 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002703 if (err > 0) {
2704 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002705 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002706 }
2707 else if (err == 0)
2708 JUMPTO(oparg);
2709 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002710 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002711 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002712 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002713
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002714 TARGET(JUMP_IF_TRUE_OR_POP) {
2715 PyObject *cond = TOP();
2716 int err;
2717 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002718 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002719 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002720 FAST_DISPATCH();
2721 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002722 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002723 JUMPTO(oparg);
2724 FAST_DISPATCH();
2725 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002726 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002727 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002728 JUMPTO(oparg);
2729 }
2730 else if (err == 0) {
2731 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002732 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002733 }
2734 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002735 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002736 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002737 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002738
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002739 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002740 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002741 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002742#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002743 /* Enabling this path speeds-up all while and for-loops by bypassing
2744 the per-loop checks for signals. By default, this should be turned-off
2745 because it prevents detection of a control-break in tight loops like
2746 "while 1: pass". Compile with this option turned-on when you need
2747 the speed-up and do not need break checking inside tight loops (ones
2748 that contain only instructions ending with FAST_DISPATCH).
2749 */
2750 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002751#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002752 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002753#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002754 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002755
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002756 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002758 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002759 PyObject *iter = PyObject_GetIter(iterable);
2760 Py_DECREF(iterable);
2761 SET_TOP(iter);
2762 if (iter == NULL)
2763 goto error;
2764 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002765 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002766 DISPATCH();
2767 }
2768
2769 TARGET(GET_YIELD_FROM_ITER) {
2770 /* before: [obj]; after [getiter(obj)] */
2771 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002772 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002773 if (PyCoro_CheckExact(iterable)) {
2774 /* `iterable` is a coroutine */
2775 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2776 /* and it is used in a 'yield from' expression of a
2777 regular generator. */
2778 Py_DECREF(iterable);
2779 SET_TOP(NULL);
2780 PyErr_SetString(PyExc_TypeError,
2781 "cannot 'yield from' a coroutine object "
2782 "in a non-coroutine generator");
2783 goto error;
2784 }
2785 }
2786 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002787 /* `iterable` is not a generator. */
2788 iter = PyObject_GetIter(iterable);
2789 Py_DECREF(iterable);
2790 SET_TOP(iter);
2791 if (iter == NULL)
2792 goto error;
2793 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002794 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002795 DISPATCH();
2796 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002797
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002798 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002799 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002800 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002801 PyObject *iter = TOP();
2802 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2803 if (next != NULL) {
2804 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002805 PREDICT(STORE_FAST);
2806 PREDICT(UNPACK_SEQUENCE);
2807 DISPATCH();
2808 }
2809 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002810 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2811 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08002812 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002813 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 PyErr_Clear();
2815 }
2816 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002817 STACKADJ(-1);
2818 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002819 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002820 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002821 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002822 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002823
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002824 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002825 why = WHY_BREAK;
2826 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002827 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002828
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002829 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002831 if (retval == NULL)
2832 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002833 why = WHY_CONTINUE;
2834 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002835 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002836
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002837 TARGET(SETUP_LOOP)
2838 TARGET(SETUP_EXCEPT)
2839 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002840 /* NOTE: If you add any new block-setup opcodes that
2841 are not try/except/finally handlers, you may need
2842 to update the PyGen_NeedsFinalizing() function.
2843 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002844
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002845 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2846 STACK_LEVEL());
2847 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002848 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002849
Yury Selivanov75445082015-05-11 22:57:16 -04002850 TARGET(BEFORE_ASYNC_WITH) {
2851 _Py_IDENTIFIER(__aexit__);
2852 _Py_IDENTIFIER(__aenter__);
2853
2854 PyObject *mgr = TOP();
2855 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
2856 *enter;
2857 PyObject *res;
2858 if (exit == NULL)
2859 goto error;
2860 SET_TOP(exit);
2861 enter = special_lookup(mgr, &PyId___aenter__);
2862 Py_DECREF(mgr);
2863 if (enter == NULL)
2864 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002865 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04002866 Py_DECREF(enter);
2867 if (res == NULL)
2868 goto error;
2869 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002870 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002871 DISPATCH();
2872 }
2873
2874 TARGET(SETUP_ASYNC_WITH) {
2875 PyObject *res = POP();
2876 /* Setup the finally block before pushing the result
2877 of __aenter__ on the stack. */
2878 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2879 STACK_LEVEL());
2880 PUSH(res);
2881 DISPATCH();
2882 }
2883
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002884 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05002885 _Py_IDENTIFIER(__exit__);
2886 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002887 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08002888 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002889 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08002890 if (enter == NULL)
2891 goto error;
2892 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08002893 if (exit == NULL) {
2894 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002895 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08002896 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002897 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002898 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002899 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002900 Py_DECREF(enter);
2901 if (res == NULL)
2902 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 /* Setup the finally block before pushing the result
2904 of __enter__ on the stack. */
2905 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2906 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002907
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002908 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002909 DISPATCH();
2910 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002911
Yury Selivanov75445082015-05-11 22:57:16 -04002912 TARGET(WITH_CLEANUP_START) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04002913 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002914 how/why we entered the finally clause:
2915 - TOP = None
2916 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2917 - TOP = WHY_*; no retval below it
2918 - (TOP, SECOND, THIRD) = exc_info()
2919 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2920 Below them is EXIT, the context.__exit__ bound method.
2921 In the last case, we must call
2922 EXIT(TOP, SECOND, THIRD)
2923 otherwise we must call
2924 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002925
Benjamin Peterson8f169482013-10-29 22:25:06 -04002926 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002927 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04002928 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002929 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002931 In addition, if the stack represents an exception,
2932 *and* the function call returns a 'true' value, we
2933 push WHY_SILENCED onto the stack. END_FINALLY will
2934 then not re-raise the exception. (But non-local
2935 gotos should still be resumed.)
2936 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002937
Victor Stinner842cfff2016-12-01 14:45:31 +01002938 PyObject* stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002939 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01002940 PyObject *exc, *val, *tb, *res;
2941
2942 val = tb = Py_None;
2943 exc = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002944 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002945 (void)POP();
2946 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002947 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002948 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002949 else if (PyLong_Check(exc)) {
2950 STACKADJ(-1);
2951 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002952 case WHY_RETURN:
2953 case WHY_CONTINUE:
2954 /* Retval in TOP. */
2955 exit_func = SECOND();
2956 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002957 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002958 break;
2959 default:
2960 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002961 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002962 break;
2963 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002964 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002965 }
2966 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002967 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002968 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002969 val = SECOND();
2970 tb = THIRD();
2971 tp2 = FOURTH();
2972 exc2 = PEEK(5);
2973 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002974 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002975 SET_VALUE(7, tb2);
2976 SET_VALUE(6, exc2);
2977 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002978 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2979 SET_FOURTH(NULL);
2980 /* We just shifted the stack down, so we have
2981 to tell the except handler block that the
2982 values are lower than it expects. */
2983 block = &f->f_blockstack[f->f_iblock - 1];
2984 assert(block->b_type == EXCEPT_HANDLER);
2985 block->b_level--;
2986 }
Victor Stinner842cfff2016-12-01 14:45:31 +01002987
2988 stack[0] = exc;
2989 stack[1] = val;
2990 stack[2] = tb;
2991 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002992 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002993 if (res == NULL)
2994 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002995
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10002996 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04002997 PUSH(exc);
2998 PUSH(res);
2999 PREDICT(WITH_CLEANUP_FINISH);
3000 DISPATCH();
3001 }
3002
3003 PREDICTED(WITH_CLEANUP_FINISH);
3004 TARGET(WITH_CLEANUP_FINISH) {
3005 PyObject *res = POP();
3006 PyObject *exc = POP();
3007 int err;
3008
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003009 if (exc != Py_None)
3010 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003011 else
3012 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003013
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003014 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003015 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003016
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003017 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003018 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003019 else if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003020 /* There was an exception and a True return */
3021 PUSH(PyLong_FromLong((long) WHY_SILENCED));
3022 }
3023 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003024 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003025 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003026
Yury Selivanovf2392132016-12-13 19:03:51 -05003027 TARGET(LOAD_METHOD) {
3028 /* Designed to work in tamdem with CALL_METHOD. */
3029 PyObject *name = GETITEM(names, oparg);
3030 PyObject *obj = TOP();
3031 PyObject *meth = NULL;
3032
3033 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3034
Yury Selivanovf2392132016-12-13 19:03:51 -05003035 if (meth == NULL) {
3036 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003037 goto error;
3038 }
3039
3040 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003041 /* We can bypass temporary bound method object.
3042 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003043
INADA Naoki015bce62017-01-16 17:23:30 +09003044 meth | self | arg1 | ... | argN
3045 */
3046 SET_TOP(meth);
3047 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003048 }
3049 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003050 /* meth is not an unbound method (but a regular attr, or
3051 something was returned by a descriptor protocol). Set
3052 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003053 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003054
3055 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003056 */
INADA Naoki015bce62017-01-16 17:23:30 +09003057 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003058 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003059 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003060 }
3061 DISPATCH();
3062 }
3063
3064 TARGET(CALL_METHOD) {
3065 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003066 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003067
3068 sp = stack_pointer;
3069
INADA Naoki015bce62017-01-16 17:23:30 +09003070 meth = PEEK(oparg + 2);
3071 if (meth == NULL) {
3072 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3073 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003074
3075 Stack layout:
3076
INADA Naoki015bce62017-01-16 17:23:30 +09003077 ... | NULL | callable | arg1 | ... | argN
3078 ^- TOP()
3079 ^- (-oparg)
3080 ^- (-oparg-1)
3081 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003082
Ville Skyttä49b27342017-08-03 09:00:59 +03003083 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003084 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003085 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003086 res = call_function(&sp, oparg, NULL);
3087 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003088 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003089 }
3090 else {
3091 /* This is a method call. Stack layout:
3092
INADA Naoki015bce62017-01-16 17:23:30 +09003093 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003094 ^- TOP()
3095 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003096 ^- (-oparg-1)
3097 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003098
INADA Naoki015bce62017-01-16 17:23:30 +09003099 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003100 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003101 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003102 */
3103 res = call_function(&sp, oparg + 1, NULL);
3104 stack_pointer = sp;
3105 }
3106
3107 PUSH(res);
3108 if (res == NULL)
3109 goto error;
3110 DISPATCH();
3111 }
3112
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003113 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003114 TARGET(CALL_FUNCTION) {
3115 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003116 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003117 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003118 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003119 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003120 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003121 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003122 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003123 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003124 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003125
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003126 TARGET(CALL_FUNCTION_KW) {
3127 PyObject **sp, *res, *names;
3128
3129 names = POP();
3130 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003131 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003132 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003133 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003134 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003135 Py_DECREF(names);
3136
3137 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003138 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003139 }
3140 DISPATCH();
3141 }
3142
3143 TARGET(CALL_FUNCTION_EX) {
3144 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003145 if (oparg & 0x01) {
3146 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003147 if (!PyDict_CheckExact(kwargs)) {
3148 PyObject *d = PyDict_New();
3149 if (d == NULL)
3150 goto error;
3151 if (PyDict_Update(d, kwargs) != 0) {
3152 Py_DECREF(d);
3153 /* PyDict_Update raises attribute
3154 * error (percolated from an attempt
3155 * to get 'keys' attribute) instead of
3156 * a type error if its second argument
3157 * is not a mapping.
3158 */
3159 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003160 format_kwargs_mapping_error(SECOND(), kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003161 }
Victor Stinnereece2222016-09-12 11:16:37 +02003162 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003163 goto error;
3164 }
3165 Py_DECREF(kwargs);
3166 kwargs = d;
3167 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003168 assert(PyDict_CheckExact(kwargs));
3169 }
3170 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003171 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003172 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003173 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003174 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003175 goto error;
3176 }
3177 Py_SETREF(callargs, PySequence_Tuple(callargs));
3178 if (callargs == NULL) {
3179 goto error;
3180 }
3181 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003182 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003183
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003184 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003185 Py_DECREF(func);
3186 Py_DECREF(callargs);
3187 Py_XDECREF(kwargs);
3188
3189 SET_TOP(result);
3190 if (result == NULL) {
3191 goto error;
3192 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003193 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003194 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003195
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003196 TARGET(MAKE_FUNCTION) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003197 PyObject *qualname = POP();
3198 PyObject *codeobj = POP();
3199 PyFunctionObject *func = (PyFunctionObject *)
3200 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003201
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003202 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003203 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003204 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003205 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003206 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003207
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003208 if (oparg & 0x08) {
3209 assert(PyTuple_CheckExact(TOP()));
3210 func ->func_closure = POP();
3211 }
3212 if (oparg & 0x04) {
3213 assert(PyDict_CheckExact(TOP()));
3214 func->func_annotations = POP();
3215 }
3216 if (oparg & 0x02) {
3217 assert(PyDict_CheckExact(TOP()));
3218 func->func_kwdefaults = POP();
3219 }
3220 if (oparg & 0x01) {
3221 assert(PyTuple_CheckExact(TOP()));
3222 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003223 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003224
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003225 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003226 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003227 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003228
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003229 TARGET(BUILD_SLICE) {
3230 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003231 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003232 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003233 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003234 step = NULL;
3235 stop = POP();
3236 start = TOP();
3237 slice = PySlice_New(start, stop, step);
3238 Py_DECREF(start);
3239 Py_DECREF(stop);
3240 Py_XDECREF(step);
3241 SET_TOP(slice);
3242 if (slice == NULL)
3243 goto error;
3244 DISPATCH();
3245 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003246
Eric V. Smitha78c7952015-11-03 12:45:05 -05003247 TARGET(FORMAT_VALUE) {
3248 /* Handles f-string value formatting. */
3249 PyObject *result;
3250 PyObject *fmt_spec;
3251 PyObject *value;
3252 PyObject *(*conv_fn)(PyObject *);
3253 int which_conversion = oparg & FVC_MASK;
3254 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3255
3256 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003257 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003258
3259 /* See if any conversion is specified. */
3260 switch (which_conversion) {
3261 case FVC_STR: conv_fn = PyObject_Str; break;
3262 case FVC_REPR: conv_fn = PyObject_Repr; break;
3263 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3264
3265 /* Must be 0 (meaning no conversion), since only four
3266 values are allowed by (oparg & FVC_MASK). */
3267 default: conv_fn = NULL; break;
3268 }
3269
3270 /* If there's a conversion function, call it and replace
3271 value with that result. Otherwise, just use value,
3272 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003273 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003274 result = conv_fn(value);
3275 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003276 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003277 Py_XDECREF(fmt_spec);
3278 goto error;
3279 }
3280 value = result;
3281 }
3282
3283 /* If value is a unicode object, and there's no fmt_spec,
3284 then we know the result of format(value) is value
3285 itself. In that case, skip calling format(). I plan to
3286 move this optimization in to PyObject_Format()
3287 itself. */
3288 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3289 /* Do nothing, just transfer ownership to result. */
3290 result = value;
3291 } else {
3292 /* Actually call format(). */
3293 result = PyObject_Format(value, fmt_spec);
3294 Py_DECREF(value);
3295 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003296 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003297 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003298 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003299 }
3300
Eric V. Smith135d5f42016-02-05 18:23:08 -05003301 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003302 DISPATCH();
3303 }
3304
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003305 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003306 int oldoparg = oparg;
3307 NEXTOPARG();
3308 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003309 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003310 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003311
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003312
Antoine Pitrou042b1282010-08-13 21:15:58 +00003313#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003314 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003315#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003316 default:
3317 fprintf(stderr,
3318 "XXX lineno: %d, opcode: %d\n",
3319 PyFrame_GetLineNumber(f),
3320 opcode);
3321 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003322 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003324 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003325
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003326 /* This should never be reached. Every opcode should end with DISPATCH()
3327 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003328 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003329
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003330error:
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003331
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003332 assert(why == WHY_NOT);
3333 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003334
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003335 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003336#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003337 if (!PyErr_Occurred())
3338 PyErr_SetString(PyExc_SystemError,
3339 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003340#else
3341 assert(PyErr_Occurred());
3342#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003343
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003344 /* Log traceback info. */
3345 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003346
Benjamin Peterson51f46162013-01-23 08:38:47 -05003347 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003348 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3349 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003350
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003351fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003352 assert(why != WHY_NOT);
3353
3354 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003355 while (why != WHY_NOT && f->f_iblock > 0) {
3356 /* Peek at the current block. */
3357 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003359 assert(why != WHY_YIELD);
3360 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3361 why = WHY_NOT;
3362 JUMPTO(PyLong_AS_LONG(retval));
3363 Py_DECREF(retval);
3364 break;
3365 }
3366 /* Now we have to pop the block. */
3367 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003369 if (b->b_type == EXCEPT_HANDLER) {
3370 UNWIND_EXCEPT_HANDLER(b);
3371 continue;
3372 }
3373 UNWIND_BLOCK(b);
3374 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3375 why = WHY_NOT;
3376 JUMPTO(b->b_handler);
3377 break;
3378 }
3379 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3380 || b->b_type == SETUP_FINALLY)) {
3381 PyObject *exc, *val, *tb;
3382 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003383 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003384 /* Beware, this invalidates all b->b_* fields */
3385 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003386 PUSH(exc_info->exc_traceback);
3387 PUSH(exc_info->exc_value);
3388 if (exc_info->exc_type != NULL) {
3389 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003390 }
3391 else {
3392 Py_INCREF(Py_None);
3393 PUSH(Py_None);
3394 }
3395 PyErr_Fetch(&exc, &val, &tb);
3396 /* Make the raw exception data
3397 available to the handler,
3398 so a program can emulate the
3399 Python main loop. */
3400 PyErr_NormalizeException(
3401 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003402 if (tb != NULL)
3403 PyException_SetTraceback(val, tb);
3404 else
3405 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003406 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003407 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003408 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003409 exc_info->exc_value = val;
3410 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003411 if (tb == NULL)
3412 tb = Py_None;
3413 Py_INCREF(tb);
3414 PUSH(tb);
3415 PUSH(val);
3416 PUSH(exc);
3417 why = WHY_NOT;
3418 JUMPTO(handler);
3419 break;
3420 }
3421 if (b->b_type == SETUP_FINALLY) {
3422 if (why & (WHY_RETURN | WHY_CONTINUE))
3423 PUSH(retval);
3424 PUSH(PyLong_FromLong((long)why));
3425 why = WHY_NOT;
3426 JUMPTO(b->b_handler);
3427 break;
3428 }
3429 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003431 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003433 if (why != WHY_NOT)
3434 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00003435
Victor Stinnerace47d72013-07-18 01:41:08 +02003436 assert(!PyErr_Occurred());
3437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003440 assert(why != WHY_YIELD);
3441 /* Pop remaining stack entries. */
3442 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003443 PyObject *o = POP();
3444 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003445 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003447 if (why != WHY_RETURN)
3448 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003449
Victor Stinner4a7cc882015-03-06 23:35:27 +01003450 assert((retval != NULL) ^ (PyErr_Occurred() != NULL));
Victor Stinnerace47d72013-07-18 01:41:08 +02003451
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003452fast_yield:
Benjamin Peterson83195c32011-07-03 13:44:00 -05003453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003454 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003455 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003456 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003457 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3458 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003459 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003460 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003461 why = WHY_EXCEPTION;
3462 }
3463 }
3464 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003465 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3466 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003467 PyTrace_RETURN, NULL);
3468 }
3469 }
3470 if (tstate->c_profilefunc) {
3471 if (why == WHY_EXCEPTION)
3472 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003473 tstate->c_profileobj,
3474 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003475 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003476 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3477 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003478 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003479 Py_CLEAR(retval);
Xiang Zhang997478e2018-01-29 11:32:12 +08003480 /* why = WHY_EXCEPTION; useless yet but cause compiler warnings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003481 }
3482 }
3483 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003485 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003486exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003487 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3488 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003489 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003490 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003491 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003492
Victor Stinnerefde1462015-03-21 15:04:43 +01003493 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003494}
3495
Benjamin Petersonb204a422011-06-05 22:04:07 -05003496static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003497format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3498{
3499 int err;
3500 Py_ssize_t len = PyList_GET_SIZE(names);
3501 PyObject *name_str, *comma, *tail, *tmp;
3502
3503 assert(PyList_CheckExact(names));
3504 assert(len >= 1);
3505 /* Deal with the joys of natural language. */
3506 switch (len) {
3507 case 1:
3508 name_str = PyList_GET_ITEM(names, 0);
3509 Py_INCREF(name_str);
3510 break;
3511 case 2:
3512 name_str = PyUnicode_FromFormat("%U and %U",
3513 PyList_GET_ITEM(names, len - 2),
3514 PyList_GET_ITEM(names, len - 1));
3515 break;
3516 default:
3517 tail = PyUnicode_FromFormat(", %U, and %U",
3518 PyList_GET_ITEM(names, len - 2),
3519 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003520 if (tail == NULL)
3521 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003522 /* Chop off the last two objects in the list. This shouldn't actually
3523 fail, but we can't be too careful. */
3524 err = PyList_SetSlice(names, len - 2, len, NULL);
3525 if (err == -1) {
3526 Py_DECREF(tail);
3527 return;
3528 }
3529 /* Stitch everything up into a nice comma-separated list. */
3530 comma = PyUnicode_FromString(", ");
3531 if (comma == NULL) {
3532 Py_DECREF(tail);
3533 return;
3534 }
3535 tmp = PyUnicode_Join(comma, names);
3536 Py_DECREF(comma);
3537 if (tmp == NULL) {
3538 Py_DECREF(tail);
3539 return;
3540 }
3541 name_str = PyUnicode_Concat(tmp, tail);
3542 Py_DECREF(tmp);
3543 Py_DECREF(tail);
3544 break;
3545 }
3546 if (name_str == NULL)
3547 return;
3548 PyErr_Format(PyExc_TypeError,
3549 "%U() missing %i required %s argument%s: %U",
3550 co->co_name,
3551 len,
3552 kind,
3553 len == 1 ? "" : "s",
3554 name_str);
3555 Py_DECREF(name_str);
3556}
3557
3558static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003559missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003560 PyObject **fastlocals)
3561{
Victor Stinner74319ae2016-08-25 00:04:09 +02003562 Py_ssize_t i, j = 0;
3563 Py_ssize_t start, end;
3564 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003565 const char *kind = positional ? "positional" : "keyword-only";
3566 PyObject *missing_names;
3567
3568 /* Compute the names of the arguments that are missing. */
3569 missing_names = PyList_New(missing);
3570 if (missing_names == NULL)
3571 return;
3572 if (positional) {
3573 start = 0;
3574 end = co->co_argcount - defcount;
3575 }
3576 else {
3577 start = co->co_argcount;
3578 end = start + co->co_kwonlyargcount;
3579 }
3580 for (i = start; i < end; i++) {
3581 if (GETLOCAL(i) == NULL) {
3582 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3583 PyObject *name = PyObject_Repr(raw);
3584 if (name == NULL) {
3585 Py_DECREF(missing_names);
3586 return;
3587 }
3588 PyList_SET_ITEM(missing_names, j++, name);
3589 }
3590 }
3591 assert(j == missing);
3592 format_missing(kind, co, missing_names);
3593 Py_DECREF(missing_names);
3594}
3595
3596static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003597too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3598 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003599{
3600 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003601 Py_ssize_t kwonly_given = 0;
3602 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003603 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003604 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003605
Benjamin Petersone109c702011-06-24 09:37:26 -05003606 assert((co->co_flags & CO_VARARGS) == 0);
3607 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003608 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3609 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003610 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003611 }
3612 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003613 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003614 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003615 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003616 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003617 }
3618 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003619 plural = (co_argcount != 1);
3620 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003621 }
3622 if (sig == NULL)
3623 return;
3624 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003625 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3626 kwonly_sig = PyUnicode_FromFormat(format,
3627 given != 1 ? "s" : "",
3628 kwonly_given,
3629 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003630 if (kwonly_sig == NULL) {
3631 Py_DECREF(sig);
3632 return;
3633 }
3634 }
3635 else {
3636 /* This will not fail. */
3637 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003638 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003639 }
3640 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003641 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003642 co->co_name,
3643 sig,
3644 plural ? "s" : "",
3645 given,
3646 kwonly_sig,
3647 given == 1 && !kwonly_given ? "was" : "were");
3648 Py_DECREF(sig);
3649 Py_DECREF(kwonly_sig);
3650}
3651
Guido van Rossumc2e20742006-02-27 22:32:47 +00003652/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003653 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003654 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003655
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003656PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003657_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003658 PyObject *const *args, Py_ssize_t argcount,
3659 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003660 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003661 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003662 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003663 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003664{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003665 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003666 PyFrameObject *f;
3667 PyObject *retval = NULL;
3668 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003669 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003670 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003671 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3672 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003673 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003675 if (globals == NULL) {
3676 PyErr_SetString(PyExc_SystemError,
3677 "PyEval_EvalCodeEx: NULL globals");
3678 return NULL;
3679 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003680
Victor Stinnerc7020012016-08-16 23:40:29 +02003681 /* Create the frame */
3682 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003683 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003684 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003685 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003686 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003687 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003688 fastlocals = f->f_localsplus;
3689 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003690
Victor Stinnerc7020012016-08-16 23:40:29 +02003691 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003692 if (co->co_flags & CO_VARKEYWORDS) {
3693 kwdict = PyDict_New();
3694 if (kwdict == NULL)
3695 goto fail;
3696 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003697 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003698 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003699 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003700 SETLOCAL(i, kwdict);
3701 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003702 else {
3703 kwdict = NULL;
3704 }
3705
3706 /* Copy positional arguments into local variables */
3707 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003708 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003709 }
3710 else {
3711 n = argcount;
3712 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003713 for (i = 0; i < n; i++) {
3714 x = args[i];
3715 Py_INCREF(x);
3716 SETLOCAL(i, x);
3717 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003718
3719 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003720 if (co->co_flags & CO_VARARGS) {
3721 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003722 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003723 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003724 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003725 SETLOCAL(total_args, u);
3726 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003727 x = args[i];
3728 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003729 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003730 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003731 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003732
Serhiy Storchakab7281052016-09-12 00:52:40 +03003733 /* Handle keyword arguments passed as two strided arrays */
3734 kwcount *= kwstep;
3735 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003736 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003737 PyObject *keyword = kwnames[i];
3738 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003739 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003740
Benjamin Petersonb204a422011-06-05 22:04:07 -05003741 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3742 PyErr_Format(PyExc_TypeError,
3743 "%U() keywords must be strings",
3744 co->co_name);
3745 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003746 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003747
Benjamin Petersonb204a422011-06-05 22:04:07 -05003748 /* Speed hack: do raw pointer compares. As names are
3749 normally interned this should almost always hit. */
3750 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3751 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003752 PyObject *name = co_varnames[j];
3753 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003754 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003755 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003756 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003757
Benjamin Petersonb204a422011-06-05 22:04:07 -05003758 /* Slow fallback, just in case */
3759 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003760 PyObject *name = co_varnames[j];
3761 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3762 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003763 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003764 }
3765 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003766 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003767 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003768 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003769
Victor Stinner231d1f32017-01-11 02:12:06 +01003770 assert(j >= total_args);
3771 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003772 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003773 "%U() got an unexpected keyword argument '%S'",
3774 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003775 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003776 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003777
Christian Heimes0bd447f2013-07-20 14:48:10 +02003778 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3779 goto fail;
3780 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003781 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003782
Benjamin Petersonb204a422011-06-05 22:04:07 -05003783 kw_found:
3784 if (GETLOCAL(j) != NULL) {
3785 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003786 "%U() got multiple values for argument '%S'",
3787 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003788 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003789 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003790 Py_INCREF(value);
3791 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003792 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003793
3794 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003795 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003796 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003797 goto fail;
3798 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003799
3800 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003801 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02003802 Py_ssize_t m = co->co_argcount - defcount;
3803 Py_ssize_t missing = 0;
3804 for (i = argcount; i < m; i++) {
3805 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003806 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02003807 }
3808 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003809 if (missing) {
3810 missing_arguments(co, missing, defcount, fastlocals);
3811 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003812 }
3813 if (n > m)
3814 i = n - m;
3815 else
3816 i = 0;
3817 for (; i < defcount; i++) {
3818 if (GETLOCAL(m+i) == NULL) {
3819 PyObject *def = defs[i];
3820 Py_INCREF(def);
3821 SETLOCAL(m+i, def);
3822 }
3823 }
3824 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003825
3826 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003827 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02003828 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003829 for (i = co->co_argcount; i < total_args; i++) {
3830 PyObject *name;
3831 if (GETLOCAL(i) != NULL)
3832 continue;
3833 name = PyTuple_GET_ITEM(co->co_varnames, i);
3834 if (kwdefs != NULL) {
3835 PyObject *def = PyDict_GetItem(kwdefs, name);
3836 if (def) {
3837 Py_INCREF(def);
3838 SETLOCAL(i, def);
3839 continue;
3840 }
3841 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003842 missing++;
3843 }
3844 if (missing) {
3845 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003846 goto fail;
3847 }
3848 }
3849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003850 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003851 vars into frame. */
3852 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003853 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02003854 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05003855 /* Possibly account for the cell variable being an argument. */
3856 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003857 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003858 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003859 /* Clear the local copy. */
3860 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003861 }
3862 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003863 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003864 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003865 if (c == NULL)
3866 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003867 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003868 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003869
3870 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05003871 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3872 PyObject *o = PyTuple_GET_ITEM(closure, i);
3873 Py_INCREF(o);
3874 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003875 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003876
Yury Selivanoveb636452016-09-08 22:01:51 -07003877 /* Handle generator/coroutine/asynchronous generator */
3878 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003879 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04003880 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003881 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04003882
3883 if (is_coro && tstate->in_coroutine_wrapper) {
3884 assert(coro_wrapper != NULL);
3885 PyErr_Format(PyExc_RuntimeError,
3886 "coroutine wrapper %.200R attempted "
3887 "to recursively wrap %.200R",
3888 coro_wrapper,
3889 co);
3890 goto fail;
3891 }
Yury Selivanov75445082015-05-11 22:57:16 -04003892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003893 /* Don't need to keep the reference to f_back, it will be set
3894 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003895 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003897 /* Create a new generator that owns the ready to run frame
3898 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04003899 if (is_coro) {
3900 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07003901 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
3902 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003903 } else {
3904 gen = PyGen_NewWithQualName(f, name, qualname);
3905 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003906 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003907 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003908 }
INADA Naoki9c157762016-12-26 18:52:46 +09003909
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003910 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04003911
Yury Selivanov94c22632015-06-04 10:16:51 -04003912 if (is_coro && coro_wrapper != NULL) {
3913 PyObject *wrapped;
3914 tstate->in_coroutine_wrapper = 1;
3915 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
3916 tstate->in_coroutine_wrapper = 0;
3917 return wrapped;
3918 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04003919
Yury Selivanov75445082015-05-11 22:57:16 -04003920 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003921 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003922
Victor Stinner59a73272016-12-09 18:51:13 +01003923 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003924
Thomas Woutersce272b62007-09-19 21:19:28 +00003925fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003927 /* decref'ing the frame can cause __del__ methods to get invoked,
3928 which can call back into Python. While we're done with the
3929 current Python frame (f), the associated C stack is still in use,
3930 so recursion_depth must be boosted for the duration.
3931 */
3932 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003933 if (Py_REFCNT(f) > 1) {
3934 Py_DECREF(f);
3935 _PyObject_GC_TRACK(f);
3936 }
3937 else {
3938 ++tstate->recursion_depth;
3939 Py_DECREF(f);
3940 --tstate->recursion_depth;
3941 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003942 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003943}
3944
Victor Stinner40ee3012014-06-16 15:59:28 +02003945PyObject *
3946PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003947 PyObject *const *args, int argcount,
3948 PyObject *const *kws, int kwcount,
3949 PyObject *const *defs, int defcount,
3950 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02003951{
3952 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02003953 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06003954 kws, kws != NULL ? kws + 1 : NULL,
3955 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02003956 defs, defcount,
3957 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003958 NULL, NULL);
3959}
Tim Peters5ca576e2001-06-18 22:08:13 +00003960
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003961static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05003962special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003963{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003964 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05003965 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003966 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003967 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003968 return NULL;
3969 }
3970 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003971}
3972
3973
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003974/* Logic for the raise statement (too complicated for inlining).
3975 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003976static int
Collin Winter828f04a2007-08-31 00:04:24 +00003977do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003978{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003979 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003981 if (exc == NULL) {
3982 /* Reraise */
3983 PyThreadState *tstate = PyThreadState_GET();
Mark Shannonae3087c2017-10-22 22:41:51 +01003984 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003985 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01003986 type = exc_info->exc_type;
3987 value = exc_info->exc_value;
3988 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02003989 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003990 PyErr_SetString(PyExc_RuntimeError,
3991 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003992 return 0;
3993 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003994 Py_XINCREF(type);
3995 Py_XINCREF(value);
3996 Py_XINCREF(tb);
3997 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003998 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003999 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004001 /* We support the following forms of raise:
4002 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004003 raise <instance>
4004 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004006 if (PyExceptionClass_Check(exc)) {
4007 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004008 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004009 if (value == NULL)
4010 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004011 if (!PyExceptionInstance_Check(value)) {
4012 PyErr_Format(PyExc_TypeError,
4013 "calling %R should have returned an instance of "
4014 "BaseException, not %R",
4015 type, Py_TYPE(value));
4016 goto raise_error;
4017 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004018 }
4019 else if (PyExceptionInstance_Check(exc)) {
4020 value = exc;
4021 type = PyExceptionInstance_Class(exc);
4022 Py_INCREF(type);
4023 }
4024 else {
4025 /* Not something you can raise. You get an exception
4026 anyway, just not what you specified :-) */
4027 Py_DECREF(exc);
4028 PyErr_SetString(PyExc_TypeError,
4029 "exceptions must derive from BaseException");
4030 goto raise_error;
4031 }
Collin Winter828f04a2007-08-31 00:04:24 +00004032
Serhiy Storchakac0191582016-09-27 11:37:10 +03004033 assert(type != NULL);
4034 assert(value != NULL);
4035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004036 if (cause) {
4037 PyObject *fixed_cause;
4038 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004039 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004040 if (fixed_cause == NULL)
4041 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004042 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004043 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004044 else if (PyExceptionInstance_Check(cause)) {
4045 fixed_cause = cause;
4046 }
4047 else if (cause == Py_None) {
4048 Py_DECREF(cause);
4049 fixed_cause = NULL;
4050 }
4051 else {
4052 PyErr_SetString(PyExc_TypeError,
4053 "exception causes must derive from "
4054 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004055 goto raise_error;
4056 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004057 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004058 }
Collin Winter828f04a2007-08-31 00:04:24 +00004059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004060 PyErr_SetObject(type, value);
4061 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004062 Py_DECREF(value);
4063 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004064 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004065
4066raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004067 Py_XDECREF(value);
4068 Py_XDECREF(type);
4069 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004070 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004071}
4072
Tim Petersd6d010b2001-06-21 02:49:55 +00004073/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004074 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004075
Guido van Rossum0368b722007-05-11 16:50:42 +00004076 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4077 with a variable target.
4078*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004079
Barry Warsawe42b18f1997-08-25 22:13:04 +00004080static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004081unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004082{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004083 int i = 0, j = 0;
4084 Py_ssize_t ll = 0;
4085 PyObject *it; /* iter(v) */
4086 PyObject *w;
4087 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004089 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004091 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004092 if (it == NULL) {
4093 if (PyErr_ExceptionMatches(PyExc_TypeError) &&
4094 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4095 {
4096 PyErr_Format(PyExc_TypeError,
4097 "cannot unpack non-iterable %.200s object",
4098 v->ob_type->tp_name);
4099 }
4100 return 0;
4101 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004102
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004103 for (; i < argcnt; i++) {
4104 w = PyIter_Next(it);
4105 if (w == NULL) {
4106 /* Iterator done, via error or exhaustion. */
4107 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004108 if (argcntafter == -1) {
4109 PyErr_Format(PyExc_ValueError,
4110 "not enough values to unpack (expected %d, got %d)",
4111 argcnt, i);
4112 }
4113 else {
4114 PyErr_Format(PyExc_ValueError,
4115 "not enough values to unpack "
4116 "(expected at least %d, got %d)",
4117 argcnt + argcntafter, i);
4118 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004119 }
4120 goto Error;
4121 }
4122 *--sp = w;
4123 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004125 if (argcntafter == -1) {
4126 /* We better have exhausted the iterator now. */
4127 w = PyIter_Next(it);
4128 if (w == NULL) {
4129 if (PyErr_Occurred())
4130 goto Error;
4131 Py_DECREF(it);
4132 return 1;
4133 }
4134 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004135 PyErr_Format(PyExc_ValueError,
4136 "too many values to unpack (expected %d)",
4137 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004138 goto Error;
4139 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004141 l = PySequence_List(it);
4142 if (l == NULL)
4143 goto Error;
4144 *--sp = l;
4145 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004147 ll = PyList_GET_SIZE(l);
4148 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004149 PyErr_Format(PyExc_ValueError,
4150 "not enough values to unpack (expected at least %d, got %zd)",
4151 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004152 goto Error;
4153 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004155 /* Pop the "after-variable" args off the list. */
4156 for (j = argcntafter; j > 0; j--, i++) {
4157 *--sp = PyList_GET_ITEM(l, ll - j);
4158 }
4159 /* Resize the list. */
4160 Py_SIZE(l) = ll - argcntafter;
4161 Py_DECREF(it);
4162 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004163
Tim Petersd6d010b2001-06-21 02:49:55 +00004164Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004165 for (; i > 0; i--, sp++)
4166 Py_DECREF(*sp);
4167 Py_XDECREF(it);
4168 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004169}
4170
4171
Guido van Rossum96a42c81992-01-12 02:29:51 +00004172#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004173static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004174prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004175{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004176 printf("%s ", str);
4177 if (PyObject_Print(v, stdout, 0) != 0)
4178 PyErr_Clear(); /* Don't know what else to do */
4179 printf("\n");
4180 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004181}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004182#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004183
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004184static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004185call_exc_trace(Py_tracefunc func, PyObject *self,
4186 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004187{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004188 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004189 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004190 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004191 if (value == NULL) {
4192 value = Py_None;
4193 Py_INCREF(value);
4194 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004195 PyErr_NormalizeException(&type, &value, &orig_traceback);
4196 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004197 arg = PyTuple_Pack(3, type, value, traceback);
4198 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004199 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004200 return;
4201 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004202 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004203 Py_DECREF(arg);
4204 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004205 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004206 else {
4207 Py_XDECREF(type);
4208 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004209 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004210 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004211}
4212
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004213static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004214call_trace_protected(Py_tracefunc func, PyObject *obj,
4215 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004216 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004217{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004218 PyObject *type, *value, *traceback;
4219 int err;
4220 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004221 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004222 if (err == 0)
4223 {
4224 PyErr_Restore(type, value, traceback);
4225 return 0;
4226 }
4227 else {
4228 Py_XDECREF(type);
4229 Py_XDECREF(value);
4230 Py_XDECREF(traceback);
4231 return -1;
4232 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004233}
4234
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004235static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004236call_trace(Py_tracefunc func, PyObject *obj,
4237 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004238 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004239{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004240 int result;
4241 if (tstate->tracing)
4242 return 0;
4243 tstate->tracing++;
4244 tstate->use_tracing = 0;
4245 result = func(obj, frame, what, arg);
4246 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4247 || (tstate->c_profilefunc != NULL));
4248 tstate->tracing--;
4249 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004250}
4251
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004252PyObject *
4253_PyEval_CallTracing(PyObject *func, PyObject *args)
4254{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004255 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004256 int save_tracing = tstate->tracing;
4257 int save_use_tracing = tstate->use_tracing;
4258 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004260 tstate->tracing = 0;
4261 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4262 || (tstate->c_profilefunc != NULL));
4263 result = PyObject_Call(func, args, NULL);
4264 tstate->tracing = save_tracing;
4265 tstate->use_tracing = save_use_tracing;
4266 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004267}
4268
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004269/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004270static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004271maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004272 PyThreadState *tstate, PyFrameObject *frame,
4273 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004275 int result = 0;
4276 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004278 /* If the last instruction executed isn't in the current
4279 instruction window, reset the window.
4280 */
4281 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4282 PyAddrPair bounds;
4283 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4284 &bounds);
4285 *instr_lb = bounds.ap_lower;
4286 *instr_ub = bounds.ap_upper;
4287 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004288 /* If the last instruction falls at the start of a line or if it
4289 represents a jump backwards, update the frame's line number and
4290 then call the trace function if we're tracing source lines.
4291 */
4292 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004293 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004294 if (frame->f_trace_lines) {
4295 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4296 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004297 }
George King20faa682017-10-18 17:44:22 -07004298 /* Always emit an opcode event if we're tracing all opcodes. */
4299 if (frame->f_trace_opcodes) {
4300 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4301 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004302 *instr_prev = frame->f_lasti;
4303 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004304}
4305
Fred Drake5755ce62001-06-27 19:19:46 +00004306void
4307PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004308{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004309 PyThreadState *tstate = PyThreadState_GET();
4310 PyObject *temp = tstate->c_profileobj;
4311 Py_XINCREF(arg);
4312 tstate->c_profilefunc = NULL;
4313 tstate->c_profileobj = NULL;
4314 /* Must make sure that tracing is not ignored if 'temp' is freed */
4315 tstate->use_tracing = tstate->c_tracefunc != NULL;
4316 Py_XDECREF(temp);
4317 tstate->c_profilefunc = func;
4318 tstate->c_profileobj = arg;
4319 /* Flag that tracing or profiling is turned on */
4320 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004321}
4322
4323void
4324PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004326 PyThreadState *tstate = PyThreadState_GET();
4327 PyObject *temp = tstate->c_traceobj;
4328 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4329 Py_XINCREF(arg);
4330 tstate->c_tracefunc = NULL;
4331 tstate->c_traceobj = NULL;
4332 /* Must make sure that profiling is not ignored if 'temp' is freed */
4333 tstate->use_tracing = tstate->c_profilefunc != NULL;
4334 Py_XDECREF(temp);
4335 tstate->c_tracefunc = func;
4336 tstate->c_traceobj = arg;
4337 /* Flag that tracing or profiling is turned on */
4338 tstate->use_tracing = ((func != NULL)
4339 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004340}
4341
Yury Selivanov75445082015-05-11 22:57:16 -04004342void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004343_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4344{
4345 assert(new_depth >= 0);
4346 PyThreadState *tstate = PyThreadState_GET();
4347 tstate->coroutine_origin_tracking_depth = new_depth;
4348}
4349
4350int
4351_PyEval_GetCoroutineOriginTrackingDepth(void)
4352{
4353 PyThreadState *tstate = PyThreadState_GET();
4354 return tstate->coroutine_origin_tracking_depth;
4355}
4356
4357void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004358_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004359{
4360 PyThreadState *tstate = PyThreadState_GET();
4361
Yury Selivanov75445082015-05-11 22:57:16 -04004362 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004363 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004364}
4365
4366PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004367_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004368{
4369 PyThreadState *tstate = PyThreadState_GET();
4370 return tstate->coroutine_wrapper;
4371}
4372
Yury Selivanoveb636452016-09-08 22:01:51 -07004373void
4374_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4375{
4376 PyThreadState *tstate = PyThreadState_GET();
4377
4378 Py_XINCREF(firstiter);
4379 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4380}
4381
4382PyObject *
4383_PyEval_GetAsyncGenFirstiter(void)
4384{
4385 PyThreadState *tstate = PyThreadState_GET();
4386 return tstate->async_gen_firstiter;
4387}
4388
4389void
4390_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4391{
4392 PyThreadState *tstate = PyThreadState_GET();
4393
4394 Py_XINCREF(finalizer);
4395 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4396}
4397
4398PyObject *
4399_PyEval_GetAsyncGenFinalizer(void)
4400{
4401 PyThreadState *tstate = PyThreadState_GET();
4402 return tstate->async_gen_finalizer;
4403}
4404
Guido van Rossumb209a111997-04-29 18:18:01 +00004405PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004406PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004407{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004408 PyFrameObject *current_frame = PyEval_GetFrame();
4409 if (current_frame == NULL)
4410 return PyThreadState_GET()->interp->builtins;
4411 else
4412 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004413}
4414
Guido van Rossumb209a111997-04-29 18:18:01 +00004415PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004416PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004417{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004418 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004419 if (current_frame == NULL) {
4420 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004421 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004422 }
4423
4424 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4425 return NULL;
4426
4427 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004428 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004429}
4430
Guido van Rossumb209a111997-04-29 18:18:01 +00004431PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004432PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004433{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004434 PyFrameObject *current_frame = PyEval_GetFrame();
4435 if (current_frame == NULL)
4436 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004437
4438 assert(current_frame->f_globals != NULL);
4439 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004440}
4441
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004442PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004443PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004445 PyThreadState *tstate = PyThreadState_GET();
4446 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004447}
4448
Guido van Rossum6135a871995-01-09 17:53:26 +00004449int
Tim Peters5ba58662001-07-16 02:29:45 +00004450PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004451{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004452 PyFrameObject *current_frame = PyEval_GetFrame();
4453 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004455 if (current_frame != NULL) {
4456 const int codeflags = current_frame->f_code->co_flags;
4457 const int compilerflags = codeflags & PyCF_MASK;
4458 if (compilerflags) {
4459 result = 1;
4460 cf->cf_flags |= compilerflags;
4461 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004462#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004463 if (codeflags & CO_GENERATOR_ALLOWED) {
4464 result = 1;
4465 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4466 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004467#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004468 }
4469 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004470}
4471
Guido van Rossum3f5da241990-12-20 15:06:42 +00004472
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004473const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004474PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004475{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004476 if (PyMethod_Check(func))
4477 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4478 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004479 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004480 else if (PyCFunction_Check(func))
4481 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4482 else
4483 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004484}
4485
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004486const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004487PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004488{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 if (PyMethod_Check(func))
4490 return "()";
4491 else if (PyFunction_Check(func))
4492 return "()";
4493 else if (PyCFunction_Check(func))
4494 return "()";
4495 else
4496 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004497}
4498
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004499#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004500if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004501 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4502 tstate, tstate->frame, \
4503 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004504 x = NULL; \
4505 } \
4506 else { \
4507 x = call; \
4508 if (tstate->c_profilefunc != NULL) { \
4509 if (x == NULL) { \
4510 call_trace_protected(tstate->c_profilefunc, \
4511 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004512 tstate, tstate->frame, \
4513 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004514 /* XXX should pass (type, value, tb) */ \
4515 } else { \
4516 if (call_trace(tstate->c_profilefunc, \
4517 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004518 tstate, tstate->frame, \
4519 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004520 Py_DECREF(x); \
4521 x = NULL; \
4522 } \
4523 } \
4524 } \
4525 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004526} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004527 x = call; \
4528 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004529
Victor Stinner415c5102017-01-11 00:54:57 +01004530/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4531 to reduce the stack consumption. */
4532Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004533call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004534{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004535 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004536 PyObject *func = *pfunc;
4537 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004538 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4539 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004540 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004542 /* Always dispatch PyCFunction first, because these are
4543 presumed to be the most frequent callable object.
4544 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004545 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004546 PyThreadState *tstate = PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004547 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004548 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004549 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
4550 PyThreadState *tstate = PyThreadState_GET();
INADA Naoki93fac8d2017-03-07 14:24:37 +09004551 if (tstate->use_tracing && tstate->c_profilefunc) {
4552 // We need to create PyCFunctionObject for tracing.
4553 PyMethodDescrObject *descr = (PyMethodDescrObject*)func;
4554 func = PyCFunction_NewEx(descr->d_method, stack[0], NULL);
4555 if (func == NULL) {
4556 return NULL;
4557 }
4558 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack+1, nargs-1,
4559 kwnames));
4560 Py_DECREF(func);
4561 }
4562 else {
4563 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4564 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004565 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004566 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004567 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004568 /* Optimize access to bound methods. Reuse the Python stack
4569 to pass 'self' as the first argument, replace 'func'
4570 with 'self'. It avoids the creation of a new temporary tuple
4571 for arguments (to replace func with self) when the method uses
4572 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004573 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004574 Py_INCREF(self);
4575 func = PyMethod_GET_FUNCTION(func);
4576 Py_INCREF(func);
4577 Py_SETREF(*pfunc, self);
4578 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004579 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004580 }
4581 else {
4582 Py_INCREF(func);
4583 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004584
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004585 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004586 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004587 }
4588 else {
4589 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4590 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004591 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004592 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004593
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004594 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4595
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004596 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004597 while ((*pp_stack) > pfunc) {
4598 w = EXT_POP(*pp_stack);
4599 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004600 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004601
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004602 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004603}
4604
Jeremy Hylton52820442001-01-03 23:52:36 +00004605static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004606do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004607{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004608 if (PyCFunction_Check(func)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004609 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004610 PyThreadState *tstate = PyThreadState_GET();
4611 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004612 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004613 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004614 else {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004615 return PyObject_Call(func, callargs, kwdict);
Victor Stinner74319ae2016-08-25 00:04:09 +02004616 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004617}
4618
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004619/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004620 nb_index slot defined, and store in *pi.
4621 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004622 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004623 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004624*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004625int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004626_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004627{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004628 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004629 Py_ssize_t x;
4630 if (PyIndex_Check(v)) {
4631 x = PyNumber_AsSsize_t(v, NULL);
4632 if (x == -1 && PyErr_Occurred())
4633 return 0;
4634 }
4635 else {
4636 PyErr_SetString(PyExc_TypeError,
4637 "slice indices must be integers or "
4638 "None or have an __index__ method");
4639 return 0;
4640 }
4641 *pi = x;
4642 }
4643 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004644}
4645
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004646int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004647_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004648{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004649 Py_ssize_t x;
4650 if (PyIndex_Check(v)) {
4651 x = PyNumber_AsSsize_t(v, NULL);
4652 if (x == -1 && PyErr_Occurred())
4653 return 0;
4654 }
4655 else {
4656 PyErr_SetString(PyExc_TypeError,
4657 "slice indices must be integers or "
4658 "have an __index__ method");
4659 return 0;
4660 }
4661 *pi = x;
4662 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004663}
4664
4665
Guido van Rossum486364b2007-06-30 05:01:58 +00004666#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004667 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004668
Guido van Rossumb209a111997-04-29 18:18:01 +00004669static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004670cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004671{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004672 int res = 0;
4673 switch (op) {
4674 case PyCmp_IS:
4675 res = (v == w);
4676 break;
4677 case PyCmp_IS_NOT:
4678 res = (v != w);
4679 break;
4680 case PyCmp_IN:
4681 res = PySequence_Contains(w, v);
4682 if (res < 0)
4683 return NULL;
4684 break;
4685 case PyCmp_NOT_IN:
4686 res = PySequence_Contains(w, v);
4687 if (res < 0)
4688 return NULL;
4689 res = !res;
4690 break;
4691 case PyCmp_EXC_MATCH:
4692 if (PyTuple_Check(w)) {
4693 Py_ssize_t i, length;
4694 length = PyTuple_Size(w);
4695 for (i = 0; i < length; i += 1) {
4696 PyObject *exc = PyTuple_GET_ITEM(w, i);
4697 if (!PyExceptionClass_Check(exc)) {
4698 PyErr_SetString(PyExc_TypeError,
4699 CANNOT_CATCH_MSG);
4700 return NULL;
4701 }
4702 }
4703 }
4704 else {
4705 if (!PyExceptionClass_Check(w)) {
4706 PyErr_SetString(PyExc_TypeError,
4707 CANNOT_CATCH_MSG);
4708 return NULL;
4709 }
4710 }
4711 res = PyErr_GivenExceptionMatches(v, w);
4712 break;
4713 default:
4714 return PyObject_RichCompare(v, w, op);
4715 }
4716 v = res ? Py_True : Py_False;
4717 Py_INCREF(v);
4718 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004719}
4720
Thomas Wouters52152252000-08-17 22:55:00 +00004721static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004722import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4723{
4724 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004725 PyObject *import_func, *res;
4726 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004727
4728 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
4729 if (import_func == NULL) {
4730 PyErr_SetString(PyExc_ImportError, "__import__ not found");
4731 return NULL;
4732 }
4733
4734 /* Fast path for not overloaded __import__. */
4735 if (import_func == PyThreadState_GET()->interp->import_func) {
4736 int ilevel = _PyLong_AsInt(level);
4737 if (ilevel == -1 && PyErr_Occurred()) {
4738 return NULL;
4739 }
4740 res = PyImport_ImportModuleLevelObject(
4741 name,
4742 f->f_globals,
4743 f->f_locals == NULL ? Py_None : f->f_locals,
4744 fromlist,
4745 ilevel);
4746 return res;
4747 }
4748
4749 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004750
4751 stack[0] = name;
4752 stack[1] = f->f_globals;
4753 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
4754 stack[3] = fromlist;
4755 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02004756 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004757 Py_DECREF(import_func);
4758 return res;
4759}
4760
4761static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004762import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004763{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004764 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02004765 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08004766 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004767
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004768 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02004769 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004770 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004771 /* Issue #17636: in case this failed because of a circular relative
4772 import, try to fallback on reading the module directly from
4773 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02004774 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07004775 if (pkgname == NULL) {
4776 goto error;
4777 }
Oren Milman6db70332017-09-19 14:23:01 +03004778 if (!PyUnicode_Check(pkgname)) {
4779 Py_CLEAR(pkgname);
4780 goto error;
4781 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004782 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07004783 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08004784 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004785 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07004786 }
Eric Snow3f9eee62017-09-15 16:35:20 -06004787 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004788 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07004789 if (x == NULL) {
4790 goto error;
4791 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004792 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004793 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07004794 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004795 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004796 if (pkgname == NULL) {
4797 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
4798 if (pkgname_or_unknown == NULL) {
4799 Py_XDECREF(pkgpath);
4800 return NULL;
4801 }
4802 } else {
4803 pkgname_or_unknown = pkgname;
4804 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004805
4806 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
4807 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08004808 errmsg = PyUnicode_FromFormat(
4809 "cannot import name %R from %R (unknown location)",
4810 name, pkgname_or_unknown
4811 );
4812 /* NULL check for errmsg done by PyErr_SetImportError. */
4813 PyErr_SetImportError(errmsg, pkgname, NULL);
4814 }
4815 else {
4816 errmsg = PyUnicode_FromFormat(
4817 "cannot import name %R from %R (%S)",
4818 name, pkgname_or_unknown, pkgpath
4819 );
4820 /* NULL check for errmsg done by PyErr_SetImportError. */
4821 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004822 }
4823
Xiang Zhang4830f582017-03-21 11:13:42 +08004824 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004825 Py_XDECREF(pkgname_or_unknown);
4826 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07004827 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00004828}
Guido van Rossumac7be682001-01-17 15:42:30 +00004829
Thomas Wouters52152252000-08-17 22:55:00 +00004830static int
4831import_all_from(PyObject *locals, PyObject *v)
4832{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004833 _Py_IDENTIFIER(__all__);
4834 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004835 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004836 int skip_leading_underscores = 0;
4837 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004838
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004839 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
4840 return -1; /* Unexpected error */
4841 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004842 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004843 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
4844 return -1;
4845 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004846 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004847 PyErr_SetString(PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004848 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004849 return -1;
4850 }
4851 all = PyMapping_Keys(dict);
4852 Py_DECREF(dict);
4853 if (all == NULL)
4854 return -1;
4855 skip_leading_underscores = 1;
4856 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004858 for (pos = 0, err = 0; ; pos++) {
4859 name = PySequence_GetItem(all, pos);
4860 if (name == NULL) {
4861 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4862 err = -1;
4863 else
4864 PyErr_Clear();
4865 break;
4866 }
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03004867 if (skip_leading_underscores && PyUnicode_Check(name)) {
4868 if (PyUnicode_READY(name) == -1) {
4869 Py_DECREF(name);
4870 err = -1;
4871 break;
4872 }
4873 if (PyUnicode_READ_CHAR(name, 0) == '_') {
4874 Py_DECREF(name);
4875 continue;
4876 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004877 }
4878 value = PyObject_GetAttr(v, name);
4879 if (value == NULL)
4880 err = -1;
4881 else if (PyDict_CheckExact(locals))
4882 err = PyDict_SetItem(locals, name, value);
4883 else
4884 err = PyObject_SetItem(locals, name, value);
4885 Py_DECREF(name);
4886 Py_XDECREF(value);
4887 if (err != 0)
4888 break;
4889 }
4890 Py_DECREF(all);
4891 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004892}
4893
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03004894static int
4895check_args_iterable(PyObject *func, PyObject *args)
4896{
4897 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
4898 PyErr_Format(PyExc_TypeError,
4899 "%.200s%.200s argument after * "
4900 "must be an iterable, not %.200s",
4901 PyEval_GetFuncName(func),
4902 PyEval_GetFuncDesc(func),
4903 args->ob_type->tp_name);
4904 return -1;
4905 }
4906 return 0;
4907}
4908
4909static void
4910format_kwargs_mapping_error(PyObject *func, PyObject *kwargs)
4911{
4912 PyErr_Format(PyExc_TypeError,
4913 "%.200s%.200s argument after ** "
4914 "must be a mapping, not %.200s",
4915 PyEval_GetFuncName(func),
4916 PyEval_GetFuncDesc(func),
4917 kwargs->ob_type->tp_name);
4918}
4919
Guido van Rossumac7be682001-01-17 15:42:30 +00004920static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004921format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004922{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004923 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004925 if (!obj)
4926 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004927
Serhiy Storchaka06515832016-11-20 09:13:07 +02004928 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004929 if (!obj_str)
4930 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004932 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004933}
Guido van Rossum950361c1997-01-24 13:49:28 +00004934
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004935static void
4936format_exc_unbound(PyCodeObject *co, int oparg)
4937{
4938 PyObject *name;
4939 /* Don't stomp existing exception */
4940 if (PyErr_Occurred())
4941 return;
4942 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4943 name = PyTuple_GET_ITEM(co->co_cellvars,
4944 oparg);
4945 format_exc_check_arg(
4946 PyExc_UnboundLocalError,
4947 UNBOUNDLOCAL_ERROR_MSG,
4948 name);
4949 } else {
4950 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4951 PyTuple_GET_SIZE(co->co_cellvars));
4952 format_exc_check_arg(PyExc_NameError,
4953 UNBOUNDFREE_ERROR_MSG, name);
4954 }
4955}
4956
Miss Islington (bot)fcd4e032018-04-04 07:09:00 -07004957static void
4958format_awaitable_error(PyTypeObject *type, int prevopcode)
4959{
4960 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
4961 if (prevopcode == BEFORE_ASYNC_WITH) {
4962 PyErr_Format(PyExc_TypeError,
4963 "'async with' received an object from __aenter__ "
4964 "that does not implement __await__: %.100s",
4965 type->tp_name);
4966 }
4967 else if (prevopcode == WITH_CLEANUP_START) {
4968 PyErr_Format(PyExc_TypeError,
4969 "'async with' received an object from __aexit__ "
4970 "that does not implement __await__: %.100s",
4971 type->tp_name);
4972 }
4973 }
4974}
4975
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004976static PyObject *
4977unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03004978 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004979{
4980 PyObject *res;
4981 if (Py_REFCNT(v) == 2) {
4982 /* In the common case, there are 2 references to the value
4983 * stored in 'variable' when the += is performed: one on the
4984 * value stack (in 'v') and one still stored in the
4985 * 'variable'. We try to delete the variable now to reduce
4986 * the refcnt to 1.
4987 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03004988 int opcode, oparg;
4989 NEXTOPARG();
4990 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004991 case STORE_FAST:
4992 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004993 PyObject **fastlocals = f->f_localsplus;
4994 if (GETLOCAL(oparg) == v)
4995 SETLOCAL(oparg, NULL);
4996 break;
4997 }
4998 case STORE_DEREF:
4999 {
5000 PyObject **freevars = (f->f_localsplus +
5001 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005002 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005003 if (PyCell_GET(c) == v) {
5004 PyCell_SET(c, NULL);
5005 Py_DECREF(v);
5006 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005007 break;
5008 }
5009 case STORE_NAME:
5010 {
5011 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005012 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005013 PyObject *locals = f->f_locals;
5014 if (PyDict_CheckExact(locals) &&
5015 PyDict_GetItem(locals, name) == v) {
5016 if (PyDict_DelItem(locals, name) != 0) {
5017 PyErr_Clear();
5018 }
5019 }
5020 break;
5021 }
5022 }
5023 }
5024 res = v;
5025 PyUnicode_Append(&res, w);
5026 return res;
5027}
5028
Guido van Rossum950361c1997-01-24 13:49:28 +00005029#ifdef DYNAMIC_EXECUTION_PROFILE
5030
Skip Montanarof118cb12001-10-15 20:51:38 +00005031static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005032getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005033{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005034 int i;
5035 PyObject *l = PyList_New(256);
5036 if (l == NULL) return NULL;
5037 for (i = 0; i < 256; i++) {
5038 PyObject *x = PyLong_FromLong(a[i]);
5039 if (x == NULL) {
5040 Py_DECREF(l);
5041 return NULL;
5042 }
5043 PyList_SetItem(l, i, x);
5044 }
5045 for (i = 0; i < 256; i++)
5046 a[i] = 0;
5047 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005048}
5049
5050PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005051_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005052{
5053#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005054 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005055#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005056 int i;
5057 PyObject *l = PyList_New(257);
5058 if (l == NULL) return NULL;
5059 for (i = 0; i < 257; i++) {
5060 PyObject *x = getarray(dxpairs[i]);
5061 if (x == NULL) {
5062 Py_DECREF(l);
5063 return NULL;
5064 }
5065 PyList_SetItem(l, i, x);
5066 }
5067 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005068#endif
5069}
5070
5071#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005072
5073Py_ssize_t
5074_PyEval_RequestCodeExtraIndex(freefunc free)
5075{
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005076 PyInterpreterState *interp = PyThreadState_Get()->interp;
Brett Cannon5c4de282016-09-07 11:16:41 -07005077 Py_ssize_t new_index;
5078
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005079 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005080 return -1;
5081 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005082 new_index = interp->co_extra_user_count++;
5083 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005084 return new_index;
5085}
Łukasz Langaa785c872016-09-09 17:37:37 -07005086
5087static void
5088dtrace_function_entry(PyFrameObject *f)
5089{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005090 const char *filename;
5091 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005092 int lineno;
5093
5094 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5095 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5096 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5097
5098 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5099}
5100
5101static void
5102dtrace_function_return(PyFrameObject *f)
5103{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005104 const char *filename;
5105 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005106 int lineno;
5107
5108 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5109 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5110 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5111
5112 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5113}
5114
5115/* DTrace equivalent of maybe_call_line_trace. */
5116static void
5117maybe_dtrace_line(PyFrameObject *frame,
5118 int *instr_lb, int *instr_ub, int *instr_prev)
5119{
5120 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005121 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005122
5123 /* If the last instruction executed isn't in the current
5124 instruction window, reset the window.
5125 */
5126 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5127 PyAddrPair bounds;
5128 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5129 &bounds);
5130 *instr_lb = bounds.ap_lower;
5131 *instr_ub = bounds.ap_upper;
5132 }
5133 /* If the last instruction falls at the start of a line or if
5134 it represents a jump backwards, update the frame's line
5135 number and call the trace function. */
5136 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5137 frame->f_lineno = line;
5138 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5139 if (!co_filename)
5140 co_filename = "?";
5141 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5142 if (!co_name)
5143 co_name = "?";
5144 PyDTrace_LINE(co_filename, co_name, line);
5145 }
5146 *instr_prev = frame->f_lasti;
5147}