blob: 86cb4cd82474f1c75af4483e0bb05583e572e801 [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)) {
Miss Islington (bot)f5197dd2018-07-09 06:31:03 -0700944 opcode = _Py_OPCODE(*next_instr);
945 if (opcode == SETUP_FINALLY ||
946 opcode == SETUP_WITH ||
947 opcode == BEFORE_ASYNC_WITH ||
948 opcode == YIELD_FROM) {
949 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -0700950 pending calls:
Miss Islington (bot)f5197dd2018-07-09 06:31:03 -0700951 - If we're about to enter the 'with:'. It will prevent
952 emitting a resource warning in the common idiom
953 'with open(path) as file:'.
954 - If we're about to enter the 'async with:'.
955 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -0700956 *very* useful, but might help in some cases and it's
957 traditional)
958 - If we're resuming a chain of nested 'yield from' or
959 'await' calls, then each frame is parked with YIELD_FROM
960 as its next opcode. If the user hit control-C we want to
961 wait until we've reached the innermost frame before
962 running the signal handler and raising KeyboardInterrupt
963 (see bpo-30039).
964 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 goto fast_next_opcode;
966 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600967 if (_Py_atomic_load_relaxed(
968 &_PyRuntime.ceval.pending.calls_to_do))
969 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400970 if (Py_MakePendingCalls() < 0)
971 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600973 if (_Py_atomic_load_relaxed(
974 &_PyRuntime.ceval.gil_drop_request))
975 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 /* Give another thread a chance */
977 if (PyThreadState_Swap(NULL) != tstate)
978 Py_FatalError("ceval: tstate mix-up");
979 drop_gil(tstate);
980
981 /* Other threads may run now */
982
983 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -0700984
985 /* Check if we should make a quick exit. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600986 if (_Py_IsFinalizing() &&
987 !_Py_CURRENTLY_FINALIZING(tstate))
988 {
Benjamin Peterson17548dd2014-06-16 22:59:07 -0700989 drop_gil(tstate);
990 PyThread_exit_thread();
991 }
992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 if (PyThreadState_Swap(tstate) != NULL)
994 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 }
996 /* Check for asynchronous exceptions. */
997 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400998 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 tstate->async_exc = NULL;
1000 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001001 PyErr_SetNone(exc);
1002 Py_DECREF(exc);
1003 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 }
1005 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 fast_next_opcode:
1008 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001009
Łukasz Langaa785c872016-09-09 17:37:37 -07001010 if (PyDTrace_LINE_ENABLED())
1011 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001014
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001016 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001017 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 /* see maybe_call_line_trace
1019 for expository comments */
1020 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 err = maybe_call_line_trace(tstate->c_tracefunc,
1023 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001024 tstate, f,
1025 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 /* Reload possibly changed frame fields */
1027 JUMPTO(f->f_lasti);
1028 if (f->f_stacktop != NULL) {
1029 stack_pointer = f->f_stacktop;
1030 f->f_stacktop = NULL;
1031 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001032 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001034 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001038
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001039 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001040 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001041#ifdef DYNAMIC_EXECUTION_PROFILE
1042#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 dxpairs[lastopcode][opcode]++;
1044 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001045#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001047#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001048
Guido van Rossum96a42c81992-01-12 02:29:51 +00001049#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 if (lltrace) {
1053 if (HAS_ARG(opcode)) {
1054 printf("%d: %d, %d\n",
1055 f->f_lasti, opcode, oparg);
1056 }
1057 else {
1058 printf("%d: %d\n",
1059 f->f_lasti, opcode);
1060 }
1061 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001062#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 /* BEWARE!
1067 It is essential that any operation that fails sets either
1068 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1069 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 TARGET(NOP)
1072 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001073
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001074 TARGET(LOAD_FAST) {
1075 PyObject *value = GETLOCAL(oparg);
1076 if (value == NULL) {
1077 format_exc_check_arg(PyExc_UnboundLocalError,
1078 UNBOUNDLOCAL_ERROR_MSG,
1079 PyTuple_GetItem(co->co_varnames, oparg));
1080 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001082 Py_INCREF(value);
1083 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001085 }
1086
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001087 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001088 TARGET(LOAD_CONST) {
1089 PyObject *value = GETITEM(consts, oparg);
1090 Py_INCREF(value);
1091 PUSH(value);
1092 FAST_DISPATCH();
1093 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001094
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001095 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001096 TARGET(STORE_FAST) {
1097 PyObject *value = POP();
1098 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001100 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001101
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001102 TARGET(POP_TOP) {
1103 PyObject *value = POP();
1104 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001106 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001107
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001108 TARGET(ROT_TWO) {
1109 PyObject *top = TOP();
1110 PyObject *second = SECOND();
1111 SET_TOP(second);
1112 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001114 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001115
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001116 TARGET(ROT_THREE) {
1117 PyObject *top = TOP();
1118 PyObject *second = SECOND();
1119 PyObject *third = THIRD();
1120 SET_TOP(second);
1121 SET_SECOND(third);
1122 SET_THIRD(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) {
1127 PyObject *top = TOP();
1128 Py_INCREF(top);
1129 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001131 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001132
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001133 TARGET(DUP_TOP_TWO) {
1134 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001135 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001136 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001137 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001138 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001139 SET_TOP(top);
1140 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001141 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001142 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001143
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001144 TARGET(UNARY_POSITIVE) {
1145 PyObject *value = TOP();
1146 PyObject *res = PyNumber_Positive(value);
1147 Py_DECREF(value);
1148 SET_TOP(res);
1149 if (res == NULL)
1150 goto error;
1151 DISPATCH();
1152 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001153
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001154 TARGET(UNARY_NEGATIVE) {
1155 PyObject *value = TOP();
1156 PyObject *res = PyNumber_Negative(value);
1157 Py_DECREF(value);
1158 SET_TOP(res);
1159 if (res == NULL)
1160 goto error;
1161 DISPATCH();
1162 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001163
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001164 TARGET(UNARY_NOT) {
1165 PyObject *value = TOP();
1166 int err = PyObject_IsTrue(value);
1167 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 if (err == 0) {
1169 Py_INCREF(Py_True);
1170 SET_TOP(Py_True);
1171 DISPATCH();
1172 }
1173 else if (err > 0) {
1174 Py_INCREF(Py_False);
1175 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 DISPATCH();
1177 }
1178 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001179 goto error;
1180 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001181
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001182 TARGET(UNARY_INVERT) {
1183 PyObject *value = TOP();
1184 PyObject *res = PyNumber_Invert(value);
1185 Py_DECREF(value);
1186 SET_TOP(res);
1187 if (res == NULL)
1188 goto error;
1189 DISPATCH();
1190 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001191
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001192 TARGET(BINARY_POWER) {
1193 PyObject *exp = POP();
1194 PyObject *base = TOP();
1195 PyObject *res = PyNumber_Power(base, exp, Py_None);
1196 Py_DECREF(base);
1197 Py_DECREF(exp);
1198 SET_TOP(res);
1199 if (res == NULL)
1200 goto error;
1201 DISPATCH();
1202 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001203
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001204 TARGET(BINARY_MULTIPLY) {
1205 PyObject *right = POP();
1206 PyObject *left = TOP();
1207 PyObject *res = PyNumber_Multiply(left, right);
1208 Py_DECREF(left);
1209 Py_DECREF(right);
1210 SET_TOP(res);
1211 if (res == NULL)
1212 goto error;
1213 DISPATCH();
1214 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001215
Benjamin Petersond51374e2014-04-09 23:55:56 -04001216 TARGET(BINARY_MATRIX_MULTIPLY) {
1217 PyObject *right = POP();
1218 PyObject *left = TOP();
1219 PyObject *res = PyNumber_MatrixMultiply(left, right);
1220 Py_DECREF(left);
1221 Py_DECREF(right);
1222 SET_TOP(res);
1223 if (res == NULL)
1224 goto error;
1225 DISPATCH();
1226 }
1227
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001228 TARGET(BINARY_TRUE_DIVIDE) {
1229 PyObject *divisor = POP();
1230 PyObject *dividend = TOP();
1231 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1232 Py_DECREF(dividend);
1233 Py_DECREF(divisor);
1234 SET_TOP(quotient);
1235 if (quotient == NULL)
1236 goto error;
1237 DISPATCH();
1238 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001239
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001240 TARGET(BINARY_FLOOR_DIVIDE) {
1241 PyObject *divisor = POP();
1242 PyObject *dividend = TOP();
1243 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1244 Py_DECREF(dividend);
1245 Py_DECREF(divisor);
1246 SET_TOP(quotient);
1247 if (quotient == NULL)
1248 goto error;
1249 DISPATCH();
1250 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001251
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001252 TARGET(BINARY_MODULO) {
1253 PyObject *divisor = POP();
1254 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001255 PyObject *res;
1256 if (PyUnicode_CheckExact(dividend) && (
1257 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1258 // fast path; string formatting, but not if the RHS is a str subclass
1259 // (see issue28598)
1260 res = PyUnicode_Format(dividend, divisor);
1261 } else {
1262 res = PyNumber_Remainder(dividend, divisor);
1263 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001264 Py_DECREF(divisor);
1265 Py_DECREF(dividend);
1266 SET_TOP(res);
1267 if (res == NULL)
1268 goto error;
1269 DISPATCH();
1270 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001271
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001272 TARGET(BINARY_ADD) {
1273 PyObject *right = POP();
1274 PyObject *left = TOP();
1275 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001276 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1277 CPython using bytecode, it is simply worthless.
1278 See http://bugs.python.org/issue21955 and
1279 http://bugs.python.org/issue10044 for the discussion. In short,
1280 no patch shown any impact on a realistic benchmark, only a minor
1281 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001282 if (PyUnicode_CheckExact(left) &&
1283 PyUnicode_CheckExact(right)) {
1284 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001285 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001286 }
1287 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001288 sum = PyNumber_Add(left, right);
1289 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001290 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001291 Py_DECREF(right);
1292 SET_TOP(sum);
1293 if (sum == NULL)
1294 goto error;
1295 DISPATCH();
1296 }
1297
1298 TARGET(BINARY_SUBTRACT) {
1299 PyObject *right = POP();
1300 PyObject *left = TOP();
1301 PyObject *diff = PyNumber_Subtract(left, right);
1302 Py_DECREF(right);
1303 Py_DECREF(left);
1304 SET_TOP(diff);
1305 if (diff == NULL)
1306 goto error;
1307 DISPATCH();
1308 }
1309
1310 TARGET(BINARY_SUBSCR) {
1311 PyObject *sub = POP();
1312 PyObject *container = TOP();
1313 PyObject *res = PyObject_GetItem(container, sub);
1314 Py_DECREF(container);
1315 Py_DECREF(sub);
1316 SET_TOP(res);
1317 if (res == NULL)
1318 goto error;
1319 DISPATCH();
1320 }
1321
1322 TARGET(BINARY_LSHIFT) {
1323 PyObject *right = POP();
1324 PyObject *left = TOP();
1325 PyObject *res = PyNumber_Lshift(left, right);
1326 Py_DECREF(left);
1327 Py_DECREF(right);
1328 SET_TOP(res);
1329 if (res == NULL)
1330 goto error;
1331 DISPATCH();
1332 }
1333
1334 TARGET(BINARY_RSHIFT) {
1335 PyObject *right = POP();
1336 PyObject *left = TOP();
1337 PyObject *res = PyNumber_Rshift(left, right);
1338 Py_DECREF(left);
1339 Py_DECREF(right);
1340 SET_TOP(res);
1341 if (res == NULL)
1342 goto error;
1343 DISPATCH();
1344 }
1345
1346 TARGET(BINARY_AND) {
1347 PyObject *right = POP();
1348 PyObject *left = TOP();
1349 PyObject *res = PyNumber_And(left, right);
1350 Py_DECREF(left);
1351 Py_DECREF(right);
1352 SET_TOP(res);
1353 if (res == NULL)
1354 goto error;
1355 DISPATCH();
1356 }
1357
1358 TARGET(BINARY_XOR) {
1359 PyObject *right = POP();
1360 PyObject *left = TOP();
1361 PyObject *res = PyNumber_Xor(left, right);
1362 Py_DECREF(left);
1363 Py_DECREF(right);
1364 SET_TOP(res);
1365 if (res == NULL)
1366 goto error;
1367 DISPATCH();
1368 }
1369
1370 TARGET(BINARY_OR) {
1371 PyObject *right = POP();
1372 PyObject *left = TOP();
1373 PyObject *res = PyNumber_Or(left, right);
1374 Py_DECREF(left);
1375 Py_DECREF(right);
1376 SET_TOP(res);
1377 if (res == NULL)
1378 goto error;
1379 DISPATCH();
1380 }
1381
1382 TARGET(LIST_APPEND) {
1383 PyObject *v = POP();
1384 PyObject *list = PEEK(oparg);
1385 int err;
1386 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001388 if (err != 0)
1389 goto error;
1390 PREDICT(JUMP_ABSOLUTE);
1391 DISPATCH();
1392 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001393
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001394 TARGET(SET_ADD) {
1395 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001396 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001397 int err;
1398 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001400 if (err != 0)
1401 goto error;
1402 PREDICT(JUMP_ABSOLUTE);
1403 DISPATCH();
1404 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001405
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001406 TARGET(INPLACE_POWER) {
1407 PyObject *exp = POP();
1408 PyObject *base = TOP();
1409 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1410 Py_DECREF(base);
1411 Py_DECREF(exp);
1412 SET_TOP(res);
1413 if (res == NULL)
1414 goto error;
1415 DISPATCH();
1416 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001417
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001418 TARGET(INPLACE_MULTIPLY) {
1419 PyObject *right = POP();
1420 PyObject *left = TOP();
1421 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1422 Py_DECREF(left);
1423 Py_DECREF(right);
1424 SET_TOP(res);
1425 if (res == NULL)
1426 goto error;
1427 DISPATCH();
1428 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001429
Benjamin Petersond51374e2014-04-09 23:55:56 -04001430 TARGET(INPLACE_MATRIX_MULTIPLY) {
1431 PyObject *right = POP();
1432 PyObject *left = TOP();
1433 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1434 Py_DECREF(left);
1435 Py_DECREF(right);
1436 SET_TOP(res);
1437 if (res == NULL)
1438 goto error;
1439 DISPATCH();
1440 }
1441
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001442 TARGET(INPLACE_TRUE_DIVIDE) {
1443 PyObject *divisor = POP();
1444 PyObject *dividend = TOP();
1445 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1446 Py_DECREF(dividend);
1447 Py_DECREF(divisor);
1448 SET_TOP(quotient);
1449 if (quotient == NULL)
1450 goto error;
1451 DISPATCH();
1452 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001453
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001454 TARGET(INPLACE_FLOOR_DIVIDE) {
1455 PyObject *divisor = POP();
1456 PyObject *dividend = TOP();
1457 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1458 Py_DECREF(dividend);
1459 Py_DECREF(divisor);
1460 SET_TOP(quotient);
1461 if (quotient == NULL)
1462 goto error;
1463 DISPATCH();
1464 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001465
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001466 TARGET(INPLACE_MODULO) {
1467 PyObject *right = POP();
1468 PyObject *left = TOP();
1469 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1470 Py_DECREF(left);
1471 Py_DECREF(right);
1472 SET_TOP(mod);
1473 if (mod == NULL)
1474 goto error;
1475 DISPATCH();
1476 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001477
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001478 TARGET(INPLACE_ADD) {
1479 PyObject *right = POP();
1480 PyObject *left = TOP();
1481 PyObject *sum;
1482 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1483 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001484 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001485 }
1486 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001487 sum = PyNumber_InPlaceAdd(left, right);
1488 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001489 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001490 Py_DECREF(right);
1491 SET_TOP(sum);
1492 if (sum == NULL)
1493 goto error;
1494 DISPATCH();
1495 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001496
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001497 TARGET(INPLACE_SUBTRACT) {
1498 PyObject *right = POP();
1499 PyObject *left = TOP();
1500 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1501 Py_DECREF(left);
1502 Py_DECREF(right);
1503 SET_TOP(diff);
1504 if (diff == NULL)
1505 goto error;
1506 DISPATCH();
1507 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001508
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001509 TARGET(INPLACE_LSHIFT) {
1510 PyObject *right = POP();
1511 PyObject *left = TOP();
1512 PyObject *res = PyNumber_InPlaceLshift(left, right);
1513 Py_DECREF(left);
1514 Py_DECREF(right);
1515 SET_TOP(res);
1516 if (res == NULL)
1517 goto error;
1518 DISPATCH();
1519 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001520
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001521 TARGET(INPLACE_RSHIFT) {
1522 PyObject *right = POP();
1523 PyObject *left = TOP();
1524 PyObject *res = PyNumber_InPlaceRshift(left, right);
1525 Py_DECREF(left);
1526 Py_DECREF(right);
1527 SET_TOP(res);
1528 if (res == NULL)
1529 goto error;
1530 DISPATCH();
1531 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001532
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001533 TARGET(INPLACE_AND) {
1534 PyObject *right = POP();
1535 PyObject *left = TOP();
1536 PyObject *res = PyNumber_InPlaceAnd(left, right);
1537 Py_DECREF(left);
1538 Py_DECREF(right);
1539 SET_TOP(res);
1540 if (res == NULL)
1541 goto error;
1542 DISPATCH();
1543 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001544
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001545 TARGET(INPLACE_XOR) {
1546 PyObject *right = POP();
1547 PyObject *left = TOP();
1548 PyObject *res = PyNumber_InPlaceXor(left, right);
1549 Py_DECREF(left);
1550 Py_DECREF(right);
1551 SET_TOP(res);
1552 if (res == NULL)
1553 goto error;
1554 DISPATCH();
1555 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001556
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001557 TARGET(INPLACE_OR) {
1558 PyObject *right = POP();
1559 PyObject *left = TOP();
1560 PyObject *res = PyNumber_InPlaceOr(left, right);
1561 Py_DECREF(left);
1562 Py_DECREF(right);
1563 SET_TOP(res);
1564 if (res == NULL)
1565 goto error;
1566 DISPATCH();
1567 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001568
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001569 TARGET(STORE_SUBSCR) {
1570 PyObject *sub = TOP();
1571 PyObject *container = SECOND();
1572 PyObject *v = THIRD();
1573 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 STACKADJ(-3);
Martin Panter95f53c12016-07-18 08:23:26 +00001575 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001576 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001578 Py_DECREF(container);
1579 Py_DECREF(sub);
1580 if (err != 0)
1581 goto error;
1582 DISPATCH();
1583 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001584
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001585 TARGET(DELETE_SUBSCR) {
1586 PyObject *sub = TOP();
1587 PyObject *container = SECOND();
1588 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 STACKADJ(-2);
Martin Panter95f53c12016-07-18 08:23:26 +00001590 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001591 err = PyObject_DelItem(container, sub);
1592 Py_DECREF(container);
1593 Py_DECREF(sub);
1594 if (err != 0)
1595 goto error;
1596 DISPATCH();
1597 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001598
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001599 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001600 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001601 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001602 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001603 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001604 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 PyErr_SetString(PyExc_RuntimeError,
1606 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001607 Py_DECREF(value);
1608 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001610 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001611 Py_DECREF(value);
1612 if (res == NULL)
1613 goto error;
1614 Py_DECREF(res);
1615 DISPATCH();
1616 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001617
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001618 TARGET(RAISE_VARARGS) {
1619 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 switch (oparg) {
1621 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001622 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001623 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001625 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001626 /* fall through */
1627 case 0:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001628 if (do_raise(exc, cause)) {
1629 why = WHY_EXCEPTION;
1630 goto fast_block_end;
1631 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632 break;
1633 default:
1634 PyErr_SetString(PyExc_SystemError,
1635 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 break;
1637 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001638 goto error;
1639 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001640
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001641 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 retval = POP();
1643 why = WHY_RETURN;
1644 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001645 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001646
Yury Selivanov75445082015-05-11 22:57:16 -04001647 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001648 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001649 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001650 PyObject *obj = TOP();
1651 PyTypeObject *type = Py_TYPE(obj);
1652
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001653 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001654 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001655 }
Yury Selivanov75445082015-05-11 22:57:16 -04001656
1657 if (getter != NULL) {
1658 iter = (*getter)(obj);
1659 Py_DECREF(obj);
1660 if (iter == NULL) {
1661 SET_TOP(NULL);
1662 goto error;
1663 }
1664 }
1665 else {
1666 SET_TOP(NULL);
1667 PyErr_Format(
1668 PyExc_TypeError,
1669 "'async for' requires an object with "
1670 "__aiter__ method, got %.100s",
1671 type->tp_name);
1672 Py_DECREF(obj);
1673 goto error;
1674 }
1675
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001676 if (Py_TYPE(iter)->tp_as_async == NULL ||
1677 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001678
Yury Selivanov398ff912017-03-02 22:20:00 -05001679 SET_TOP(NULL);
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001680 PyErr_Format(
1681 PyExc_TypeError,
1682 "'async for' received an object from __aiter__ "
1683 "that does not implement __anext__: %.100s",
1684 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001685 Py_DECREF(iter);
1686 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001687 }
1688
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001689 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001690 DISPATCH();
1691 }
1692
1693 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001694 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001695 PyObject *next_iter = NULL;
1696 PyObject *awaitable = NULL;
1697 PyObject *aiter = TOP();
1698 PyTypeObject *type = Py_TYPE(aiter);
1699
Yury Selivanoveb636452016-09-08 22:01:51 -07001700 if (PyAsyncGen_CheckExact(aiter)) {
1701 awaitable = type->tp_as_async->am_anext(aiter);
1702 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001703 goto error;
1704 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001705 } else {
1706 if (type->tp_as_async != NULL){
1707 getter = type->tp_as_async->am_anext;
1708 }
Yury Selivanov75445082015-05-11 22:57:16 -04001709
Yury Selivanoveb636452016-09-08 22:01:51 -07001710 if (getter != NULL) {
1711 next_iter = (*getter)(aiter);
1712 if (next_iter == NULL) {
1713 goto error;
1714 }
1715 }
1716 else {
1717 PyErr_Format(
1718 PyExc_TypeError,
1719 "'async for' requires an iterator with "
1720 "__anext__ method, got %.100s",
1721 type->tp_name);
1722 goto error;
1723 }
Yury Selivanov75445082015-05-11 22:57:16 -04001724
Yury Selivanoveb636452016-09-08 22:01:51 -07001725 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1726 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001727 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001728 PyExc_TypeError,
1729 "'async for' received an invalid object "
1730 "from __anext__: %.100s",
1731 Py_TYPE(next_iter)->tp_name);
1732
1733 Py_DECREF(next_iter);
1734 goto error;
1735 } else {
1736 Py_DECREF(next_iter);
1737 }
1738 }
Yury Selivanov75445082015-05-11 22:57:16 -04001739
1740 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001741 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001742 DISPATCH();
1743 }
1744
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001745 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001746 TARGET(GET_AWAITABLE) {
1747 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001748 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001749
Miss Islington (bot)fcd4e032018-04-04 07:09:00 -07001750 if (iter == NULL) {
1751 format_awaitable_error(Py_TYPE(iterable),
1752 _Py_OPCODE(next_instr[-2]));
1753 }
1754
Yury Selivanov75445082015-05-11 22:57:16 -04001755 Py_DECREF(iterable);
1756
Yury Selivanovc724bae2016-03-02 11:30:46 -05001757 if (iter != NULL && PyCoro_CheckExact(iter)) {
1758 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1759 if (yf != NULL) {
1760 /* `iter` is a coroutine object that is being
1761 awaited, `yf` is a pointer to the current awaitable
1762 being awaited on. */
1763 Py_DECREF(yf);
1764 Py_CLEAR(iter);
1765 PyErr_SetString(
1766 PyExc_RuntimeError,
1767 "coroutine is being awaited already");
1768 /* The code below jumps to `error` if `iter` is NULL. */
1769 }
1770 }
1771
Yury Selivanov75445082015-05-11 22:57:16 -04001772 SET_TOP(iter); /* Even if it's NULL */
1773
1774 if (iter == NULL) {
1775 goto error;
1776 }
1777
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001778 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001779 DISPATCH();
1780 }
1781
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001782 TARGET(YIELD_FROM) {
1783 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001784 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001785 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001786 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1787 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001788 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001789 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001790 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001791 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001792 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001793 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001794 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001795 Py_DECREF(v);
1796 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001797 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001798 if (tstate->c_tracefunc != NULL
1799 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001800 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001801 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001802 if (err < 0)
1803 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001804 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001805 SET_TOP(val);
1806 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001807 }
Martin Panter95f53c12016-07-18 08:23:26 +00001808 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001809 f->f_stacktop = stack_pointer;
1810 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001811 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001812 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001813 f->f_lasti -= sizeof(_Py_CODEUNIT);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001814 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001815 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001816
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001817 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07001819
1820 if (co->co_flags & CO_ASYNC_GENERATOR) {
1821 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
1822 Py_DECREF(retval);
1823 if (w == NULL) {
1824 retval = NULL;
1825 goto error;
1826 }
1827 retval = w;
1828 }
1829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 f->f_stacktop = stack_pointer;
1831 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001833 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001834
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001835 TARGET(POP_EXCEPT) {
1836 PyTryBlock *b = PyFrame_BlockPop(f);
1837 if (b->b_type != EXCEPT_HANDLER) {
1838 PyErr_SetString(PyExc_SystemError,
1839 "popped block is not an except handler");
1840 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001841 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001842 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001843 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001844 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001845
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001846 PREDICTED(POP_BLOCK);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001847 TARGET(POP_BLOCK) {
1848 PyTryBlock *b = PyFrame_BlockPop(f);
1849 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001851 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001854 TARGET(END_FINALLY) {
1855 PyObject *status = POP();
1856 if (PyLong_Check(status)) {
1857 why = (enum why_code) PyLong_AS_LONG(status);
1858 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 if (why == WHY_RETURN ||
1860 why == WHY_CONTINUE)
1861 retval = POP();
1862 if (why == WHY_SILENCED) {
1863 /* An exception was silenced by 'with', we must
1864 manually unwind the EXCEPT_HANDLER block which was
1865 created when the exception was caught, otherwise
1866 the stack will be in an inconsistent state. */
1867 PyTryBlock *b = PyFrame_BlockPop(f);
1868 assert(b->b_type == EXCEPT_HANDLER);
1869 UNWIND_EXCEPT_HANDLER(b);
1870 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001871 Py_DECREF(status);
1872 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001874 Py_DECREF(status);
1875 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001877 else if (PyExceptionClass_Check(status)) {
1878 PyObject *exc = POP();
1879 PyObject *tb = POP();
1880 PyErr_Restore(status, exc, tb);
1881 why = WHY_EXCEPTION;
1882 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001884 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885 PyErr_SetString(PyExc_SystemError,
1886 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001887 Py_DECREF(status);
1888 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001890 Py_DECREF(status);
1891 DISPATCH();
1892 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001893
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001894 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02001895 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001896
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001897 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001898 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001899 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
1900 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02001901 PyErr_SetString(PyExc_NameError,
1902 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001903 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001904 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001905 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001906 }
1907 else {
1908 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
1909 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02001910 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001911 bc = PyObject_GetItem(f->f_builtins, build_class_str);
1912 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02001913 if (PyErr_ExceptionMatches(PyExc_KeyError))
1914 PyErr_SetString(PyExc_NameError,
1915 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001916 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001917 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001919 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04001920 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02001921 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001922
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001923 TARGET(STORE_NAME) {
1924 PyObject *name = GETITEM(names, oparg);
1925 PyObject *v = POP();
1926 PyObject *ns = f->f_locals;
1927 int err;
1928 if (ns == NULL) {
1929 PyErr_Format(PyExc_SystemError,
1930 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001932 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001934 if (PyDict_CheckExact(ns))
1935 err = PyDict_SetItem(ns, name, v);
1936 else
1937 err = PyObject_SetItem(ns, name, v);
1938 Py_DECREF(v);
1939 if (err != 0)
1940 goto error;
1941 DISPATCH();
1942 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001943
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001944 TARGET(DELETE_NAME) {
1945 PyObject *name = GETITEM(names, oparg);
1946 PyObject *ns = f->f_locals;
1947 int err;
1948 if (ns == NULL) {
1949 PyErr_Format(PyExc_SystemError,
1950 "no locals when deleting %R", name);
1951 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001953 err = PyObject_DelItem(ns, name);
1954 if (err != 0) {
1955 format_exc_check_arg(PyExc_NameError,
1956 NAME_ERROR_MSG,
1957 name);
1958 goto error;
1959 }
1960 DISPATCH();
1961 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00001962
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001963 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001964 TARGET(UNPACK_SEQUENCE) {
1965 PyObject *seq = POP(), *item, **items;
1966 if (PyTuple_CheckExact(seq) &&
1967 PyTuple_GET_SIZE(seq) == oparg) {
1968 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001970 item = items[oparg];
1971 Py_INCREF(item);
1972 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001973 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001974 } else if (PyList_CheckExact(seq) &&
1975 PyList_GET_SIZE(seq) == oparg) {
1976 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001977 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001978 item = items[oparg];
1979 Py_INCREF(item);
1980 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001982 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001983 stack_pointer + oparg)) {
1984 STACKADJ(oparg);
1985 } else {
1986 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001987 Py_DECREF(seq);
1988 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001990 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04001991 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001992 }
Guido van Rossum0368b722007-05-11 16:50:42 +00001993
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001994 TARGET(UNPACK_EX) {
1995 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
1996 PyObject *seq = POP();
1997
1998 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
1999 stack_pointer + totalargs)) {
2000 stack_pointer += totalargs;
2001 } else {
2002 Py_DECREF(seq);
2003 goto error;
2004 }
2005 Py_DECREF(seq);
2006 DISPATCH();
2007 }
2008
2009 TARGET(STORE_ATTR) {
2010 PyObject *name = GETITEM(names, oparg);
2011 PyObject *owner = TOP();
2012 PyObject *v = SECOND();
2013 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002014 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002015 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002017 Py_DECREF(owner);
2018 if (err != 0)
2019 goto error;
2020 DISPATCH();
2021 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002022
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002023 TARGET(DELETE_ATTR) {
2024 PyObject *name = GETITEM(names, oparg);
2025 PyObject *owner = POP();
2026 int err;
2027 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2028 Py_DECREF(owner);
2029 if (err != 0)
2030 goto error;
2031 DISPATCH();
2032 }
2033
2034 TARGET(STORE_GLOBAL) {
2035 PyObject *name = GETITEM(names, oparg);
2036 PyObject *v = POP();
2037 int err;
2038 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002039 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002040 if (err != 0)
2041 goto error;
2042 DISPATCH();
2043 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002044
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002045 TARGET(DELETE_GLOBAL) {
2046 PyObject *name = GETITEM(names, oparg);
2047 int err;
2048 err = PyDict_DelItem(f->f_globals, name);
2049 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002050 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002051 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002052 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002053 }
2054 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002055 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002056
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002057 TARGET(LOAD_NAME) {
2058 PyObject *name = GETITEM(names, oparg);
2059 PyObject *locals = f->f_locals;
2060 PyObject *v;
2061 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002063 "no locals when loading %R", name);
2064 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002065 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002066 if (PyDict_CheckExact(locals)) {
2067 v = PyDict_GetItem(locals, name);
2068 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 }
2070 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002071 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002072 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002073 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2074 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075 PyErr_Clear();
2076 }
2077 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002078 if (v == NULL) {
2079 v = PyDict_GetItem(f->f_globals, name);
2080 Py_XINCREF(v);
2081 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002082 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002083 v = PyDict_GetItem(f->f_builtins, name);
2084 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002085 format_exc_check_arg(
2086 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002087 NAME_ERROR_MSG, name);
2088 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002089 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002090 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002091 }
2092 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002093 v = PyObject_GetItem(f->f_builtins, name);
2094 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002095 if (PyErr_ExceptionMatches(PyExc_KeyError))
2096 format_exc_check_arg(
2097 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002098 NAME_ERROR_MSG, name);
2099 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002100 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002101 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002102 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002104 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002106 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002107
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002108 TARGET(LOAD_GLOBAL) {
2109 PyObject *name = GETITEM(names, oparg);
2110 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002111 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002112 && PyDict_CheckExact(f->f_builtins))
2113 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002114 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002115 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002116 name);
2117 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002118 if (!_PyErr_OCCURRED()) {
2119 /* _PyDict_LoadGlobal() returns NULL without raising
2120 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002121 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002122 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002123 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002124 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002125 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002126 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002127 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002128 else {
2129 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002130
2131 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002132 v = PyObject_GetItem(f->f_globals, name);
2133 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002134 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2135 goto error;
2136 PyErr_Clear();
2137
Victor Stinnerb4efc962015-11-20 09:24:02 +01002138 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002139 v = PyObject_GetItem(f->f_builtins, name);
2140 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002141 if (PyErr_ExceptionMatches(PyExc_KeyError))
2142 format_exc_check_arg(
2143 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002144 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002145 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002146 }
2147 }
2148 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002149 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002150 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002151 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002152
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002153 TARGET(DELETE_FAST) {
2154 PyObject *v = GETLOCAL(oparg);
2155 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002156 SETLOCAL(oparg, NULL);
2157 DISPATCH();
2158 }
2159 format_exc_check_arg(
2160 PyExc_UnboundLocalError,
2161 UNBOUNDLOCAL_ERROR_MSG,
2162 PyTuple_GetItem(co->co_varnames, oparg)
2163 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002164 goto error;
2165 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002166
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002167 TARGET(DELETE_DEREF) {
2168 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002169 PyObject *oldobj = PyCell_GET(cell);
2170 if (oldobj != NULL) {
2171 PyCell_SET(cell, NULL);
2172 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002173 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002174 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002175 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002176 goto error;
2177 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002178
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002179 TARGET(LOAD_CLOSURE) {
2180 PyObject *cell = freevars[oparg];
2181 Py_INCREF(cell);
2182 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002183 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002184 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002185
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002186 TARGET(LOAD_CLASSDEREF) {
2187 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002188 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002189 assert(locals);
2190 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2191 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2192 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2193 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2194 if (PyDict_CheckExact(locals)) {
2195 value = PyDict_GetItem(locals, name);
2196 Py_XINCREF(value);
2197 }
2198 else {
2199 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002200 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002201 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2202 goto error;
2203 PyErr_Clear();
2204 }
2205 }
2206 if (!value) {
2207 PyObject *cell = freevars[oparg];
2208 value = PyCell_GET(cell);
2209 if (value == NULL) {
2210 format_exc_unbound(co, oparg);
2211 goto error;
2212 }
2213 Py_INCREF(value);
2214 }
2215 PUSH(value);
2216 DISPATCH();
2217 }
2218
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002219 TARGET(LOAD_DEREF) {
2220 PyObject *cell = freevars[oparg];
2221 PyObject *value = PyCell_GET(cell);
2222 if (value == NULL) {
2223 format_exc_unbound(co, oparg);
2224 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002225 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002226 Py_INCREF(value);
2227 PUSH(value);
2228 DISPATCH();
2229 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002230
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002231 TARGET(STORE_DEREF) {
2232 PyObject *v = POP();
2233 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002234 PyObject *oldobj = PyCell_GET(cell);
2235 PyCell_SET(cell, v);
2236 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002237 DISPATCH();
2238 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002239
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002240 TARGET(BUILD_STRING) {
2241 PyObject *str;
2242 PyObject *empty = PyUnicode_New(0, 0);
2243 if (empty == NULL) {
2244 goto error;
2245 }
2246 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2247 Py_DECREF(empty);
2248 if (str == NULL)
2249 goto error;
2250 while (--oparg >= 0) {
2251 PyObject *item = POP();
2252 Py_DECREF(item);
2253 }
2254 PUSH(str);
2255 DISPATCH();
2256 }
2257
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002258 TARGET(BUILD_TUPLE) {
2259 PyObject *tup = PyTuple_New(oparg);
2260 if (tup == NULL)
2261 goto error;
2262 while (--oparg >= 0) {
2263 PyObject *item = POP();
2264 PyTuple_SET_ITEM(tup, oparg, item);
2265 }
2266 PUSH(tup);
2267 DISPATCH();
2268 }
2269
2270 TARGET(BUILD_LIST) {
2271 PyObject *list = PyList_New(oparg);
2272 if (list == NULL)
2273 goto error;
2274 while (--oparg >= 0) {
2275 PyObject *item = POP();
2276 PyList_SET_ITEM(list, oparg, item);
2277 }
2278 PUSH(list);
2279 DISPATCH();
2280 }
2281
Serhiy Storchaka73442852016-10-02 10:33:46 +03002282 TARGET(BUILD_TUPLE_UNPACK_WITH_CALL)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002283 TARGET(BUILD_TUPLE_UNPACK)
2284 TARGET(BUILD_LIST_UNPACK) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002285 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002286 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002287 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002288 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002289
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002290 if (sum == NULL)
2291 goto error;
2292
2293 for (i = oparg; i > 0; i--) {
2294 PyObject *none_val;
2295
2296 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2297 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002298 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002299 PyErr_ExceptionMatches(PyExc_TypeError))
2300 {
2301 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002302 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002303 Py_DECREF(sum);
2304 goto error;
2305 }
2306 Py_DECREF(none_val);
2307 }
2308
2309 if (convert_to_tuple) {
2310 return_value = PyList_AsTuple(sum);
2311 Py_DECREF(sum);
2312 if (return_value == NULL)
2313 goto error;
2314 }
2315 else {
2316 return_value = sum;
2317 }
2318
2319 while (oparg--)
2320 Py_DECREF(POP());
2321 PUSH(return_value);
2322 DISPATCH();
2323 }
2324
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002325 TARGET(BUILD_SET) {
2326 PyObject *set = PySet_New(NULL);
2327 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002328 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002329 if (set == NULL)
2330 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002331 for (i = oparg; i > 0; i--) {
2332 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002333 if (err == 0)
2334 err = PySet_Add(set, item);
2335 Py_DECREF(item);
2336 }
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002337 STACKADJ(-oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002338 if (err != 0) {
2339 Py_DECREF(set);
2340 goto error;
2341 }
2342 PUSH(set);
2343 DISPATCH();
2344 }
2345
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002346 TARGET(BUILD_SET_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002347 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002348 PyObject *sum = PySet_New(NULL);
2349 if (sum == NULL)
2350 goto error;
2351
2352 for (i = oparg; i > 0; i--) {
2353 if (_PySet_Update(sum, PEEK(i)) < 0) {
2354 Py_DECREF(sum);
2355 goto error;
2356 }
2357 }
2358
2359 while (oparg--)
2360 Py_DECREF(POP());
2361 PUSH(sum);
2362 DISPATCH();
2363 }
2364
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002365 TARGET(BUILD_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002366 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002367 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2368 if (map == NULL)
2369 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002370 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002371 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002372 PyObject *key = PEEK(2*i);
2373 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002374 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002375 if (err != 0) {
2376 Py_DECREF(map);
2377 goto error;
2378 }
2379 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002380
2381 while (oparg--) {
2382 Py_DECREF(POP());
2383 Py_DECREF(POP());
2384 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002385 PUSH(map);
2386 DISPATCH();
2387 }
2388
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002389 TARGET(SETUP_ANNOTATIONS) {
2390 _Py_IDENTIFIER(__annotations__);
2391 int err;
2392 PyObject *ann_dict;
2393 if (f->f_locals == NULL) {
2394 PyErr_Format(PyExc_SystemError,
2395 "no locals found when setting up annotations");
2396 goto error;
2397 }
2398 /* check if __annotations__ in locals()... */
2399 if (PyDict_CheckExact(f->f_locals)) {
2400 ann_dict = _PyDict_GetItemId(f->f_locals,
2401 &PyId___annotations__);
2402 if (ann_dict == NULL) {
2403 /* ...if not, create a new one */
2404 ann_dict = PyDict_New();
2405 if (ann_dict == NULL) {
2406 goto error;
2407 }
2408 err = _PyDict_SetItemId(f->f_locals,
2409 &PyId___annotations__, ann_dict);
2410 Py_DECREF(ann_dict);
2411 if (err != 0) {
2412 goto error;
2413 }
2414 }
2415 }
2416 else {
2417 /* do the same if locals() is not a dict */
2418 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2419 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002420 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002421 }
2422 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2423 if (ann_dict == NULL) {
2424 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2425 goto error;
2426 }
2427 PyErr_Clear();
2428 ann_dict = PyDict_New();
2429 if (ann_dict == NULL) {
2430 goto error;
2431 }
2432 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2433 Py_DECREF(ann_dict);
2434 if (err != 0) {
2435 goto error;
2436 }
2437 }
2438 else {
2439 Py_DECREF(ann_dict);
2440 }
2441 }
2442 DISPATCH();
2443 }
2444
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002445 TARGET(BUILD_CONST_KEY_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002446 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002447 PyObject *map;
2448 PyObject *keys = TOP();
2449 if (!PyTuple_CheckExact(keys) ||
2450 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2451 PyErr_SetString(PyExc_SystemError,
2452 "bad BUILD_CONST_KEY_MAP keys argument");
2453 goto error;
2454 }
2455 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2456 if (map == NULL) {
2457 goto error;
2458 }
2459 for (i = oparg; i > 0; i--) {
2460 int err;
2461 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2462 PyObject *value = PEEK(i + 1);
2463 err = PyDict_SetItem(map, key, value);
2464 if (err != 0) {
2465 Py_DECREF(map);
2466 goto error;
2467 }
2468 }
2469
2470 Py_DECREF(POP());
2471 while (oparg--) {
2472 Py_DECREF(POP());
2473 }
2474 PUSH(map);
2475 DISPATCH();
2476 }
2477
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002478 TARGET(BUILD_MAP_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002479 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002480 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002481 if (sum == NULL)
2482 goto error;
2483
2484 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002485 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002486 if (PyDict_Update(sum, arg) < 0) {
2487 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2488 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002489 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002490 arg->ob_type->tp_name);
2491 }
2492 Py_DECREF(sum);
2493 goto error;
2494 }
2495 }
2496
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002497 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002498 Py_DECREF(POP());
2499 PUSH(sum);
2500 DISPATCH();
2501 }
2502
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002503 TARGET(BUILD_MAP_UNPACK_WITH_CALL) {
2504 Py_ssize_t i;
2505 PyObject *sum = PyDict_New();
2506 if (sum == NULL)
2507 goto error;
2508
2509 for (i = oparg; i > 0; i--) {
2510 PyObject *arg = PEEK(i);
2511 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
2512 PyObject *func = PEEK(2 + oparg);
2513 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002514 format_kwargs_mapping_error(func, arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002515 }
2516 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2517 PyObject *exc, *val, *tb;
2518 PyErr_Fetch(&exc, &val, &tb);
2519 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
2520 PyObject *key = PyTuple_GET_ITEM(val, 0);
2521 if (!PyUnicode_Check(key)) {
2522 PyErr_Format(PyExc_TypeError,
2523 "%.200s%.200s keywords must be strings",
2524 PyEval_GetFuncName(func),
2525 PyEval_GetFuncDesc(func));
2526 } else {
2527 PyErr_Format(PyExc_TypeError,
2528 "%.200s%.200s got multiple "
2529 "values for keyword argument '%U'",
2530 PyEval_GetFuncName(func),
2531 PyEval_GetFuncDesc(func),
2532 key);
2533 }
2534 Py_XDECREF(exc);
2535 Py_XDECREF(val);
2536 Py_XDECREF(tb);
2537 }
2538 else {
2539 PyErr_Restore(exc, val, tb);
2540 }
2541 }
2542 Py_DECREF(sum);
2543 goto error;
2544 }
2545 }
2546
2547 while (oparg--)
2548 Py_DECREF(POP());
2549 PUSH(sum);
2550 DISPATCH();
2551 }
2552
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002553 TARGET(MAP_ADD) {
2554 PyObject *key = TOP();
2555 PyObject *value = SECOND();
2556 PyObject *map;
2557 int err;
2558 STACKADJ(-2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002559 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002560 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002561 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002562 Py_DECREF(value);
2563 Py_DECREF(key);
2564 if (err != 0)
2565 goto error;
2566 PREDICT(JUMP_ABSOLUTE);
2567 DISPATCH();
2568 }
2569
2570 TARGET(LOAD_ATTR) {
2571 PyObject *name = GETITEM(names, oparg);
2572 PyObject *owner = TOP();
2573 PyObject *res = PyObject_GetAttr(owner, name);
2574 Py_DECREF(owner);
2575 SET_TOP(res);
2576 if (res == NULL)
2577 goto error;
2578 DISPATCH();
2579 }
2580
2581 TARGET(COMPARE_OP) {
2582 PyObject *right = POP();
2583 PyObject *left = TOP();
2584 PyObject *res = cmp_outcome(oparg, left, right);
2585 Py_DECREF(left);
2586 Py_DECREF(right);
2587 SET_TOP(res);
2588 if (res == NULL)
2589 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002590 PREDICT(POP_JUMP_IF_FALSE);
2591 PREDICT(POP_JUMP_IF_TRUE);
2592 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002593 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002594
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002595 TARGET(IMPORT_NAME) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002596 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002597 PyObject *fromlist = POP();
2598 PyObject *level = TOP();
2599 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002600 res = import_name(f, name, fromlist, level);
2601 Py_DECREF(level);
2602 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002603 SET_TOP(res);
2604 if (res == NULL)
2605 goto error;
2606 DISPATCH();
2607 }
2608
2609 TARGET(IMPORT_STAR) {
2610 PyObject *from = POP(), *locals;
2611 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002612 if (PyFrame_FastToLocalsWithError(f) < 0) {
2613 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002614 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002615 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002616
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002617 locals = f->f_locals;
2618 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002619 PyErr_SetString(PyExc_SystemError,
2620 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002621 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002622 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002623 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002624 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002625 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002626 Py_DECREF(from);
2627 if (err != 0)
2628 goto error;
2629 DISPATCH();
2630 }
Guido van Rossum25831651993-05-19 14:50:45 +00002631
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002632 TARGET(IMPORT_FROM) {
2633 PyObject *name = GETITEM(names, oparg);
2634 PyObject *from = TOP();
2635 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002636 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002637 PUSH(res);
2638 if (res == NULL)
2639 goto error;
2640 DISPATCH();
2641 }
Thomas Wouters52152252000-08-17 22:55:00 +00002642
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002643 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002644 JUMPBY(oparg);
2645 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002646 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002647
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002648 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002649 TARGET(POP_JUMP_IF_FALSE) {
2650 PyObject *cond = POP();
2651 int err;
2652 if (cond == Py_True) {
2653 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002654 FAST_DISPATCH();
2655 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002656 if (cond == Py_False) {
2657 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002658 JUMPTO(oparg);
2659 FAST_DISPATCH();
2660 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002661 err = PyObject_IsTrue(cond);
2662 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002663 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002664 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002665 else if (err == 0)
2666 JUMPTO(oparg);
2667 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002668 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002669 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002670 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002671
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002672 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002673 TARGET(POP_JUMP_IF_TRUE) {
2674 PyObject *cond = POP();
2675 int err;
2676 if (cond == Py_False) {
2677 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002678 FAST_DISPATCH();
2679 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002680 if (cond == Py_True) {
2681 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002682 JUMPTO(oparg);
2683 FAST_DISPATCH();
2684 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002685 err = PyObject_IsTrue(cond);
2686 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002687 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002688 JUMPTO(oparg);
2689 }
2690 else if (err == 0)
2691 ;
2692 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002693 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002694 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002695 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002696
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002697 TARGET(JUMP_IF_FALSE_OR_POP) {
2698 PyObject *cond = TOP();
2699 int err;
2700 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002701 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002702 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002703 FAST_DISPATCH();
2704 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002705 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002706 JUMPTO(oparg);
2707 FAST_DISPATCH();
2708 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002709 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002710 if (err > 0) {
2711 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002712 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002713 }
2714 else if (err == 0)
2715 JUMPTO(oparg);
2716 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002717 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002718 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002719 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002720
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002721 TARGET(JUMP_IF_TRUE_OR_POP) {
2722 PyObject *cond = TOP();
2723 int err;
2724 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002725 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002726 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002727 FAST_DISPATCH();
2728 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002729 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002730 JUMPTO(oparg);
2731 FAST_DISPATCH();
2732 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002733 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002734 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002735 JUMPTO(oparg);
2736 }
2737 else if (err == 0) {
2738 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002739 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002740 }
2741 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002742 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002743 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002744 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002745
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002746 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002747 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002748 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002749#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002750 /* Enabling this path speeds-up all while and for-loops by bypassing
2751 the per-loop checks for signals. By default, this should be turned-off
2752 because it prevents detection of a control-break in tight loops like
2753 "while 1: pass". Compile with this option turned-on when you need
2754 the speed-up and do not need break checking inside tight loops (ones
2755 that contain only instructions ending with FAST_DISPATCH).
2756 */
2757 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002758#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002759 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002760#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002761 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002762
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002763 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002764 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002765 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002766 PyObject *iter = PyObject_GetIter(iterable);
2767 Py_DECREF(iterable);
2768 SET_TOP(iter);
2769 if (iter == NULL)
2770 goto error;
2771 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002772 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002773 DISPATCH();
2774 }
2775
2776 TARGET(GET_YIELD_FROM_ITER) {
2777 /* before: [obj]; after [getiter(obj)] */
2778 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002779 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002780 if (PyCoro_CheckExact(iterable)) {
2781 /* `iterable` is a coroutine */
2782 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2783 /* and it is used in a 'yield from' expression of a
2784 regular generator. */
2785 Py_DECREF(iterable);
2786 SET_TOP(NULL);
2787 PyErr_SetString(PyExc_TypeError,
2788 "cannot 'yield from' a coroutine object "
2789 "in a non-coroutine generator");
2790 goto error;
2791 }
2792 }
2793 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002794 /* `iterable` is not a generator. */
2795 iter = PyObject_GetIter(iterable);
2796 Py_DECREF(iterable);
2797 SET_TOP(iter);
2798 if (iter == NULL)
2799 goto error;
2800 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002801 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002802 DISPATCH();
2803 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002804
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002805 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002806 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002807 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002808 PyObject *iter = TOP();
2809 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2810 if (next != NULL) {
2811 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002812 PREDICT(STORE_FAST);
2813 PREDICT(UNPACK_SEQUENCE);
2814 DISPATCH();
2815 }
2816 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002817 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2818 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08002819 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002820 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002821 PyErr_Clear();
2822 }
2823 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002824 STACKADJ(-1);
2825 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002826 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002827 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002828 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002829 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002830
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002831 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 why = WHY_BREAK;
2833 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002834 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002835
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002836 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002837 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002838 if (retval == NULL)
2839 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002840 why = WHY_CONTINUE;
2841 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002842 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002843
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002844 TARGET(SETUP_LOOP)
2845 TARGET(SETUP_EXCEPT)
2846 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002847 /* NOTE: If you add any new block-setup opcodes that
2848 are not try/except/finally handlers, you may need
2849 to update the PyGen_NeedsFinalizing() function.
2850 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002852 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2853 STACK_LEVEL());
2854 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002855 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002856
Yury Selivanov75445082015-05-11 22:57:16 -04002857 TARGET(BEFORE_ASYNC_WITH) {
2858 _Py_IDENTIFIER(__aexit__);
2859 _Py_IDENTIFIER(__aenter__);
2860
2861 PyObject *mgr = TOP();
2862 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
2863 *enter;
2864 PyObject *res;
2865 if (exit == NULL)
2866 goto error;
2867 SET_TOP(exit);
2868 enter = special_lookup(mgr, &PyId___aenter__);
2869 Py_DECREF(mgr);
2870 if (enter == NULL)
2871 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002872 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04002873 Py_DECREF(enter);
2874 if (res == NULL)
2875 goto error;
2876 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002877 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002878 DISPATCH();
2879 }
2880
2881 TARGET(SETUP_ASYNC_WITH) {
2882 PyObject *res = POP();
2883 /* Setup the finally block before pushing the result
2884 of __aenter__ on the stack. */
2885 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2886 STACK_LEVEL());
2887 PUSH(res);
2888 DISPATCH();
2889 }
2890
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002891 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05002892 _Py_IDENTIFIER(__exit__);
2893 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002894 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08002895 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002896 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08002897 if (enter == NULL)
2898 goto error;
2899 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08002900 if (exit == NULL) {
2901 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002902 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08002903 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002904 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002905 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002906 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002907 Py_DECREF(enter);
2908 if (res == NULL)
2909 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002910 /* Setup the finally block before pushing the result
2911 of __enter__ on the stack. */
2912 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2913 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002914
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002915 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002916 DISPATCH();
2917 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002918
Yury Selivanov75445082015-05-11 22:57:16 -04002919 TARGET(WITH_CLEANUP_START) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04002920 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002921 how/why we entered the finally clause:
2922 - TOP = None
2923 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2924 - TOP = WHY_*; no retval below it
2925 - (TOP, SECOND, THIRD) = exc_info()
2926 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2927 Below them is EXIT, the context.__exit__ bound method.
2928 In the last case, we must call
2929 EXIT(TOP, SECOND, THIRD)
2930 otherwise we must call
2931 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002932
Benjamin Peterson8f169482013-10-29 22:25:06 -04002933 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002934 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04002935 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002938 In addition, if the stack represents an exception,
2939 *and* the function call returns a 'true' value, we
2940 push WHY_SILENCED onto the stack. END_FINALLY will
2941 then not re-raise the exception. (But non-local
2942 gotos should still be resumed.)
2943 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002944
Victor Stinner842cfff2016-12-01 14:45:31 +01002945 PyObject* stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01002947 PyObject *exc, *val, *tb, *res;
2948
2949 val = tb = Py_None;
2950 exc = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002951 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002952 (void)POP();
2953 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002954 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002955 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002956 else if (PyLong_Check(exc)) {
2957 STACKADJ(-1);
2958 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002959 case WHY_RETURN:
2960 case WHY_CONTINUE:
2961 /* Retval in TOP. */
2962 exit_func = SECOND();
2963 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002964 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002965 break;
2966 default:
2967 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002968 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002969 break;
2970 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002971 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002972 }
2973 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002974 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002975 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002976 val = SECOND();
2977 tb = THIRD();
2978 tp2 = FOURTH();
2979 exc2 = PEEK(5);
2980 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002981 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002982 SET_VALUE(7, tb2);
2983 SET_VALUE(6, exc2);
2984 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002985 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2986 SET_FOURTH(NULL);
2987 /* We just shifted the stack down, so we have
2988 to tell the except handler block that the
2989 values are lower than it expects. */
2990 block = &f->f_blockstack[f->f_iblock - 1];
2991 assert(block->b_type == EXCEPT_HANDLER);
2992 block->b_level--;
2993 }
Victor Stinner842cfff2016-12-01 14:45:31 +01002994
2995 stack[0] = exc;
2996 stack[1] = val;
2997 stack[2] = tb;
2998 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002999 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003000 if (res == NULL)
3001 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003002
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003003 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003004 PUSH(exc);
3005 PUSH(res);
3006 PREDICT(WITH_CLEANUP_FINISH);
3007 DISPATCH();
3008 }
3009
3010 PREDICTED(WITH_CLEANUP_FINISH);
3011 TARGET(WITH_CLEANUP_FINISH) {
3012 PyObject *res = POP();
3013 PyObject *exc = POP();
3014 int err;
3015
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003016 if (exc != Py_None)
3017 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003018 else
3019 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003020
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003021 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003022 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003024 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003025 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003026 else if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003027 /* There was an exception and a True return */
3028 PUSH(PyLong_FromLong((long) WHY_SILENCED));
3029 }
3030 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003031 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003032 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003033
Yury Selivanovf2392132016-12-13 19:03:51 -05003034 TARGET(LOAD_METHOD) {
3035 /* Designed to work in tamdem with CALL_METHOD. */
3036 PyObject *name = GETITEM(names, oparg);
3037 PyObject *obj = TOP();
3038 PyObject *meth = NULL;
3039
3040 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3041
Yury Selivanovf2392132016-12-13 19:03:51 -05003042 if (meth == NULL) {
3043 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003044 goto error;
3045 }
3046
3047 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003048 /* We can bypass temporary bound method object.
3049 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003050
INADA Naoki015bce62017-01-16 17:23:30 +09003051 meth | self | arg1 | ... | argN
3052 */
3053 SET_TOP(meth);
3054 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003055 }
3056 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003057 /* meth is not an unbound method (but a regular attr, or
3058 something was returned by a descriptor protocol). Set
3059 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003060 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003061
3062 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003063 */
INADA Naoki015bce62017-01-16 17:23:30 +09003064 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003065 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003066 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003067 }
3068 DISPATCH();
3069 }
3070
3071 TARGET(CALL_METHOD) {
3072 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003073 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003074
3075 sp = stack_pointer;
3076
INADA Naoki015bce62017-01-16 17:23:30 +09003077 meth = PEEK(oparg + 2);
3078 if (meth == NULL) {
3079 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3080 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003081
3082 Stack layout:
3083
INADA Naoki015bce62017-01-16 17:23:30 +09003084 ... | NULL | callable | arg1 | ... | argN
3085 ^- TOP()
3086 ^- (-oparg)
3087 ^- (-oparg-1)
3088 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003089
Ville Skyttä49b27342017-08-03 09:00:59 +03003090 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003091 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003092 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003093 res = call_function(&sp, oparg, NULL);
3094 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003095 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003096 }
3097 else {
3098 /* This is a method call. Stack layout:
3099
INADA Naoki015bce62017-01-16 17:23:30 +09003100 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003101 ^- TOP()
3102 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003103 ^- (-oparg-1)
3104 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003105
INADA Naoki015bce62017-01-16 17:23:30 +09003106 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003107 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003108 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003109 */
3110 res = call_function(&sp, oparg + 1, NULL);
3111 stack_pointer = sp;
3112 }
3113
3114 PUSH(res);
3115 if (res == NULL)
3116 goto error;
3117 DISPATCH();
3118 }
3119
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003120 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003121 TARGET(CALL_FUNCTION) {
3122 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003123 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003124 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003125 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003126 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003127 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003128 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003129 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003130 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003131 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003132
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003133 TARGET(CALL_FUNCTION_KW) {
3134 PyObject **sp, *res, *names;
3135
3136 names = POP();
3137 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003138 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003139 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003140 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003141 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003142 Py_DECREF(names);
3143
3144 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003145 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003146 }
3147 DISPATCH();
3148 }
3149
3150 TARGET(CALL_FUNCTION_EX) {
3151 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003152 if (oparg & 0x01) {
3153 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003154 if (!PyDict_CheckExact(kwargs)) {
3155 PyObject *d = PyDict_New();
3156 if (d == NULL)
3157 goto error;
3158 if (PyDict_Update(d, kwargs) != 0) {
3159 Py_DECREF(d);
3160 /* PyDict_Update raises attribute
3161 * error (percolated from an attempt
3162 * to get 'keys' attribute) instead of
3163 * a type error if its second argument
3164 * is not a mapping.
3165 */
3166 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003167 format_kwargs_mapping_error(SECOND(), kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003168 }
Victor Stinnereece2222016-09-12 11:16:37 +02003169 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003170 goto error;
3171 }
3172 Py_DECREF(kwargs);
3173 kwargs = d;
3174 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003175 assert(PyDict_CheckExact(kwargs));
3176 }
3177 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003178 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003179 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003180 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003181 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003182 goto error;
3183 }
3184 Py_SETREF(callargs, PySequence_Tuple(callargs));
3185 if (callargs == NULL) {
3186 goto error;
3187 }
3188 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003189 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003190
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003191 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003192 Py_DECREF(func);
3193 Py_DECREF(callargs);
3194 Py_XDECREF(kwargs);
3195
3196 SET_TOP(result);
3197 if (result == NULL) {
3198 goto error;
3199 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003200 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003201 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003202
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003203 TARGET(MAKE_FUNCTION) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003204 PyObject *qualname = POP();
3205 PyObject *codeobj = POP();
3206 PyFunctionObject *func = (PyFunctionObject *)
3207 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003208
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003209 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003210 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003211 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003212 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003213 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003214
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003215 if (oparg & 0x08) {
3216 assert(PyTuple_CheckExact(TOP()));
3217 func ->func_closure = POP();
3218 }
3219 if (oparg & 0x04) {
3220 assert(PyDict_CheckExact(TOP()));
3221 func->func_annotations = POP();
3222 }
3223 if (oparg & 0x02) {
3224 assert(PyDict_CheckExact(TOP()));
3225 func->func_kwdefaults = POP();
3226 }
3227 if (oparg & 0x01) {
3228 assert(PyTuple_CheckExact(TOP()));
3229 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003230 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003231
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003232 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003233 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003234 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003235
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003236 TARGET(BUILD_SLICE) {
3237 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003238 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003239 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003240 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003241 step = NULL;
3242 stop = POP();
3243 start = TOP();
3244 slice = PySlice_New(start, stop, step);
3245 Py_DECREF(start);
3246 Py_DECREF(stop);
3247 Py_XDECREF(step);
3248 SET_TOP(slice);
3249 if (slice == NULL)
3250 goto error;
3251 DISPATCH();
3252 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003253
Eric V. Smitha78c7952015-11-03 12:45:05 -05003254 TARGET(FORMAT_VALUE) {
3255 /* Handles f-string value formatting. */
3256 PyObject *result;
3257 PyObject *fmt_spec;
3258 PyObject *value;
3259 PyObject *(*conv_fn)(PyObject *);
3260 int which_conversion = oparg & FVC_MASK;
3261 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3262
3263 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003264 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003265
3266 /* See if any conversion is specified. */
3267 switch (which_conversion) {
3268 case FVC_STR: conv_fn = PyObject_Str; break;
3269 case FVC_REPR: conv_fn = PyObject_Repr; break;
3270 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3271
3272 /* Must be 0 (meaning no conversion), since only four
3273 values are allowed by (oparg & FVC_MASK). */
3274 default: conv_fn = NULL; break;
3275 }
3276
3277 /* If there's a conversion function, call it and replace
3278 value with that result. Otherwise, just use value,
3279 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003280 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003281 result = conv_fn(value);
3282 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003283 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003284 Py_XDECREF(fmt_spec);
3285 goto error;
3286 }
3287 value = result;
3288 }
3289
3290 /* If value is a unicode object, and there's no fmt_spec,
3291 then we know the result of format(value) is value
3292 itself. In that case, skip calling format(). I plan to
3293 move this optimization in to PyObject_Format()
3294 itself. */
3295 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3296 /* Do nothing, just transfer ownership to result. */
3297 result = value;
3298 } else {
3299 /* Actually call format(). */
3300 result = PyObject_Format(value, fmt_spec);
3301 Py_DECREF(value);
3302 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003303 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003304 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003305 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003306 }
3307
Eric V. Smith135d5f42016-02-05 18:23:08 -05003308 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003309 DISPATCH();
3310 }
3311
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003312 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003313 int oldoparg = oparg;
3314 NEXTOPARG();
3315 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003316 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003317 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003318
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003319
Antoine Pitrou042b1282010-08-13 21:15:58 +00003320#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003321 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003322#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003323 default:
3324 fprintf(stderr,
3325 "XXX lineno: %d, opcode: %d\n",
3326 PyFrame_GetLineNumber(f),
3327 opcode);
3328 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003329 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003331 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003332
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003333 /* This should never be reached. Every opcode should end with DISPATCH()
3334 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003335 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003336
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003337error:
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003338
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003339 assert(why == WHY_NOT);
3340 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003341
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003342 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003343#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003344 if (!PyErr_Occurred())
3345 PyErr_SetString(PyExc_SystemError,
3346 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003347#else
3348 assert(PyErr_Occurred());
3349#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003350
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003351 /* Log traceback info. */
3352 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003353
Benjamin Peterson51f46162013-01-23 08:38:47 -05003354 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003355 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3356 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003357
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003358fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003359 assert(why != WHY_NOT);
3360
3361 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003362 while (why != WHY_NOT && f->f_iblock > 0) {
3363 /* Peek at the current block. */
3364 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003366 assert(why != WHY_YIELD);
3367 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3368 why = WHY_NOT;
3369 JUMPTO(PyLong_AS_LONG(retval));
3370 Py_DECREF(retval);
3371 break;
3372 }
3373 /* Now we have to pop the block. */
3374 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003376 if (b->b_type == EXCEPT_HANDLER) {
3377 UNWIND_EXCEPT_HANDLER(b);
3378 continue;
3379 }
3380 UNWIND_BLOCK(b);
3381 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3382 why = WHY_NOT;
3383 JUMPTO(b->b_handler);
3384 break;
3385 }
3386 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3387 || b->b_type == SETUP_FINALLY)) {
3388 PyObject *exc, *val, *tb;
3389 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003390 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003391 /* Beware, this invalidates all b->b_* fields */
3392 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003393 PUSH(exc_info->exc_traceback);
3394 PUSH(exc_info->exc_value);
3395 if (exc_info->exc_type != NULL) {
3396 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003397 }
3398 else {
3399 Py_INCREF(Py_None);
3400 PUSH(Py_None);
3401 }
3402 PyErr_Fetch(&exc, &val, &tb);
3403 /* Make the raw exception data
3404 available to the handler,
3405 so a program can emulate the
3406 Python main loop. */
3407 PyErr_NormalizeException(
3408 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003409 if (tb != NULL)
3410 PyException_SetTraceback(val, tb);
3411 else
3412 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003413 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003414 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003415 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003416 exc_info->exc_value = val;
3417 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003418 if (tb == NULL)
3419 tb = Py_None;
3420 Py_INCREF(tb);
3421 PUSH(tb);
3422 PUSH(val);
3423 PUSH(exc);
3424 why = WHY_NOT;
3425 JUMPTO(handler);
3426 break;
3427 }
3428 if (b->b_type == SETUP_FINALLY) {
3429 if (why & (WHY_RETURN | WHY_CONTINUE))
3430 PUSH(retval);
3431 PUSH(PyLong_FromLong((long)why));
3432 why = WHY_NOT;
3433 JUMPTO(b->b_handler);
3434 break;
3435 }
3436 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003440 if (why != WHY_NOT)
3441 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00003442
Victor Stinnerace47d72013-07-18 01:41:08 +02003443 assert(!PyErr_Occurred());
3444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003445 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003447 assert(why != WHY_YIELD);
3448 /* Pop remaining stack entries. */
3449 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003450 PyObject *o = POP();
3451 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003452 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003454 if (why != WHY_RETURN)
3455 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003456
Victor Stinner4a7cc882015-03-06 23:35:27 +01003457 assert((retval != NULL) ^ (PyErr_Occurred() != NULL));
Victor Stinnerace47d72013-07-18 01:41:08 +02003458
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003459fast_yield:
Benjamin Peterson83195c32011-07-03 13:44:00 -05003460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003461 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003462 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003463 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003464 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3465 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003466 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003467 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003468 why = WHY_EXCEPTION;
3469 }
3470 }
3471 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003472 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3473 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003474 PyTrace_RETURN, NULL);
3475 }
3476 }
3477 if (tstate->c_profilefunc) {
3478 if (why == WHY_EXCEPTION)
3479 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003480 tstate->c_profileobj,
3481 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003482 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003483 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3484 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003485 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003486 Py_CLEAR(retval);
Xiang Zhang997478e2018-01-29 11:32:12 +08003487 /* why = WHY_EXCEPTION; useless yet but cause compiler warnings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003488 }
3489 }
3490 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003492 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003493exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003494 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3495 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003496 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003497 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003498 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003499
Victor Stinnerefde1462015-03-21 15:04:43 +01003500 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003501}
3502
Benjamin Petersonb204a422011-06-05 22:04:07 -05003503static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003504format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3505{
3506 int err;
3507 Py_ssize_t len = PyList_GET_SIZE(names);
3508 PyObject *name_str, *comma, *tail, *tmp;
3509
3510 assert(PyList_CheckExact(names));
3511 assert(len >= 1);
3512 /* Deal with the joys of natural language. */
3513 switch (len) {
3514 case 1:
3515 name_str = PyList_GET_ITEM(names, 0);
3516 Py_INCREF(name_str);
3517 break;
3518 case 2:
3519 name_str = PyUnicode_FromFormat("%U and %U",
3520 PyList_GET_ITEM(names, len - 2),
3521 PyList_GET_ITEM(names, len - 1));
3522 break;
3523 default:
3524 tail = PyUnicode_FromFormat(", %U, and %U",
3525 PyList_GET_ITEM(names, len - 2),
3526 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003527 if (tail == NULL)
3528 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003529 /* Chop off the last two objects in the list. This shouldn't actually
3530 fail, but we can't be too careful. */
3531 err = PyList_SetSlice(names, len - 2, len, NULL);
3532 if (err == -1) {
3533 Py_DECREF(tail);
3534 return;
3535 }
3536 /* Stitch everything up into a nice comma-separated list. */
3537 comma = PyUnicode_FromString(", ");
3538 if (comma == NULL) {
3539 Py_DECREF(tail);
3540 return;
3541 }
3542 tmp = PyUnicode_Join(comma, names);
3543 Py_DECREF(comma);
3544 if (tmp == NULL) {
3545 Py_DECREF(tail);
3546 return;
3547 }
3548 name_str = PyUnicode_Concat(tmp, tail);
3549 Py_DECREF(tmp);
3550 Py_DECREF(tail);
3551 break;
3552 }
3553 if (name_str == NULL)
3554 return;
3555 PyErr_Format(PyExc_TypeError,
3556 "%U() missing %i required %s argument%s: %U",
3557 co->co_name,
3558 len,
3559 kind,
3560 len == 1 ? "" : "s",
3561 name_str);
3562 Py_DECREF(name_str);
3563}
3564
3565static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003566missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003567 PyObject **fastlocals)
3568{
Victor Stinner74319ae2016-08-25 00:04:09 +02003569 Py_ssize_t i, j = 0;
3570 Py_ssize_t start, end;
3571 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003572 const char *kind = positional ? "positional" : "keyword-only";
3573 PyObject *missing_names;
3574
3575 /* Compute the names of the arguments that are missing. */
3576 missing_names = PyList_New(missing);
3577 if (missing_names == NULL)
3578 return;
3579 if (positional) {
3580 start = 0;
3581 end = co->co_argcount - defcount;
3582 }
3583 else {
3584 start = co->co_argcount;
3585 end = start + co->co_kwonlyargcount;
3586 }
3587 for (i = start; i < end; i++) {
3588 if (GETLOCAL(i) == NULL) {
3589 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3590 PyObject *name = PyObject_Repr(raw);
3591 if (name == NULL) {
3592 Py_DECREF(missing_names);
3593 return;
3594 }
3595 PyList_SET_ITEM(missing_names, j++, name);
3596 }
3597 }
3598 assert(j == missing);
3599 format_missing(kind, co, missing_names);
3600 Py_DECREF(missing_names);
3601}
3602
3603static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003604too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3605 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003606{
3607 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003608 Py_ssize_t kwonly_given = 0;
3609 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003610 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003611 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003612
Benjamin Petersone109c702011-06-24 09:37:26 -05003613 assert((co->co_flags & CO_VARARGS) == 0);
3614 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003615 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3616 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003617 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003618 }
3619 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003620 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003621 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003622 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003623 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003624 }
3625 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003626 plural = (co_argcount != 1);
3627 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003628 }
3629 if (sig == NULL)
3630 return;
3631 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003632 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3633 kwonly_sig = PyUnicode_FromFormat(format,
3634 given != 1 ? "s" : "",
3635 kwonly_given,
3636 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003637 if (kwonly_sig == NULL) {
3638 Py_DECREF(sig);
3639 return;
3640 }
3641 }
3642 else {
3643 /* This will not fail. */
3644 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003645 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003646 }
3647 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003648 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003649 co->co_name,
3650 sig,
3651 plural ? "s" : "",
3652 given,
3653 kwonly_sig,
3654 given == 1 && !kwonly_given ? "was" : "were");
3655 Py_DECREF(sig);
3656 Py_DECREF(kwonly_sig);
3657}
3658
Guido van Rossumc2e20742006-02-27 22:32:47 +00003659/* This is gonna seem *real weird*, but if you put some other code between
Miss Islington (bot)3c193cf2018-04-06 15:14:29 -07003660 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003661 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003662
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003663PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003664_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003665 PyObject *const *args, Py_ssize_t argcount,
3666 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003667 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003668 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003669 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003670 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003671{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003672 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003673 PyFrameObject *f;
3674 PyObject *retval = NULL;
3675 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003676 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003677 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003678 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3679 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003680 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003682 if (globals == NULL) {
3683 PyErr_SetString(PyExc_SystemError,
3684 "PyEval_EvalCodeEx: NULL globals");
3685 return NULL;
3686 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003687
Victor Stinnerc7020012016-08-16 23:40:29 +02003688 /* Create the frame */
3689 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003690 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003691 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003692 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003693 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003694 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003695 fastlocals = f->f_localsplus;
3696 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003697
Victor Stinnerc7020012016-08-16 23:40:29 +02003698 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003699 if (co->co_flags & CO_VARKEYWORDS) {
3700 kwdict = PyDict_New();
3701 if (kwdict == NULL)
3702 goto fail;
3703 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003704 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003705 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003706 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003707 SETLOCAL(i, kwdict);
3708 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003709 else {
3710 kwdict = NULL;
3711 }
3712
3713 /* Copy positional arguments into local variables */
3714 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003715 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003716 }
3717 else {
3718 n = argcount;
3719 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003720 for (i = 0; i < n; i++) {
3721 x = args[i];
3722 Py_INCREF(x);
3723 SETLOCAL(i, x);
3724 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003725
3726 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003727 if (co->co_flags & CO_VARARGS) {
3728 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003729 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003730 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003731 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003732 SETLOCAL(total_args, u);
3733 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003734 x = args[i];
3735 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003736 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003737 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003738 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003739
Serhiy Storchakab7281052016-09-12 00:52:40 +03003740 /* Handle keyword arguments passed as two strided arrays */
3741 kwcount *= kwstep;
3742 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003743 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003744 PyObject *keyword = kwnames[i];
3745 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003746 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003747
Benjamin Petersonb204a422011-06-05 22:04:07 -05003748 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3749 PyErr_Format(PyExc_TypeError,
3750 "%U() keywords must be strings",
3751 co->co_name);
3752 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003753 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003754
Benjamin Petersonb204a422011-06-05 22:04:07 -05003755 /* Speed hack: do raw pointer compares. As names are
3756 normally interned this should almost always hit. */
3757 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3758 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003759 PyObject *name = co_varnames[j];
3760 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003761 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003762 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003763 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003764
Benjamin Petersonb204a422011-06-05 22:04:07 -05003765 /* Slow fallback, just in case */
3766 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003767 PyObject *name = co_varnames[j];
3768 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3769 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003770 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003771 }
3772 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003773 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003774 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003775 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003776
Victor Stinner231d1f32017-01-11 02:12:06 +01003777 assert(j >= total_args);
3778 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003779 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003780 "%U() got an unexpected keyword argument '%S'",
3781 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003782 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003783 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003784
Christian Heimes0bd447f2013-07-20 14:48:10 +02003785 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3786 goto fail;
3787 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003788 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003789
Benjamin Petersonb204a422011-06-05 22:04:07 -05003790 kw_found:
3791 if (GETLOCAL(j) != NULL) {
3792 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003793 "%U() got multiple values for argument '%S'",
3794 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003795 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003796 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003797 Py_INCREF(value);
3798 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003799 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003800
3801 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003802 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003803 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003804 goto fail;
3805 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003806
3807 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003808 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02003809 Py_ssize_t m = co->co_argcount - defcount;
3810 Py_ssize_t missing = 0;
3811 for (i = argcount; i < m; i++) {
3812 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003813 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02003814 }
3815 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003816 if (missing) {
3817 missing_arguments(co, missing, defcount, fastlocals);
3818 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003819 }
3820 if (n > m)
3821 i = n - m;
3822 else
3823 i = 0;
3824 for (; i < defcount; i++) {
3825 if (GETLOCAL(m+i) == NULL) {
3826 PyObject *def = defs[i];
3827 Py_INCREF(def);
3828 SETLOCAL(m+i, def);
3829 }
3830 }
3831 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003832
3833 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003834 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02003835 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003836 for (i = co->co_argcount; i < total_args; i++) {
3837 PyObject *name;
3838 if (GETLOCAL(i) != NULL)
3839 continue;
3840 name = PyTuple_GET_ITEM(co->co_varnames, i);
3841 if (kwdefs != NULL) {
3842 PyObject *def = PyDict_GetItem(kwdefs, name);
3843 if (def) {
3844 Py_INCREF(def);
3845 SETLOCAL(i, def);
3846 continue;
3847 }
3848 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003849 missing++;
3850 }
3851 if (missing) {
3852 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003853 goto fail;
3854 }
3855 }
3856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003857 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003858 vars into frame. */
3859 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003860 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02003861 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05003862 /* Possibly account for the cell variable being an argument. */
3863 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003864 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003865 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003866 /* Clear the local copy. */
3867 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003868 }
3869 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003870 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003871 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003872 if (c == NULL)
3873 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003874 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003875 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003876
3877 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05003878 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3879 PyObject *o = PyTuple_GET_ITEM(closure, i);
3880 Py_INCREF(o);
3881 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003882 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003883
Yury Selivanoveb636452016-09-08 22:01:51 -07003884 /* Handle generator/coroutine/asynchronous generator */
3885 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003886 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04003887 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003888 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04003889
3890 if (is_coro && tstate->in_coroutine_wrapper) {
3891 assert(coro_wrapper != NULL);
3892 PyErr_Format(PyExc_RuntimeError,
3893 "coroutine wrapper %.200R attempted "
3894 "to recursively wrap %.200R",
3895 coro_wrapper,
3896 co);
3897 goto fail;
3898 }
Yury Selivanov75445082015-05-11 22:57:16 -04003899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003900 /* Don't need to keep the reference to f_back, it will be set
3901 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003902 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003904 /* Create a new generator that owns the ready to run frame
3905 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04003906 if (is_coro) {
3907 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07003908 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
3909 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003910 } else {
3911 gen = PyGen_NewWithQualName(f, name, qualname);
3912 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003913 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003914 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003915 }
INADA Naoki9c157762016-12-26 18:52:46 +09003916
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003917 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04003918
Yury Selivanov94c22632015-06-04 10:16:51 -04003919 if (is_coro && coro_wrapper != NULL) {
3920 PyObject *wrapped;
3921 tstate->in_coroutine_wrapper = 1;
3922 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
3923 tstate->in_coroutine_wrapper = 0;
3924 return wrapped;
3925 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04003926
Yury Selivanov75445082015-05-11 22:57:16 -04003927 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003928 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003929
Victor Stinner59a73272016-12-09 18:51:13 +01003930 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003931
Thomas Woutersce272b62007-09-19 21:19:28 +00003932fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003934 /* decref'ing the frame can cause __del__ methods to get invoked,
3935 which can call back into Python. While we're done with the
3936 current Python frame (f), the associated C stack is still in use,
3937 so recursion_depth must be boosted for the duration.
3938 */
3939 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003940 if (Py_REFCNT(f) > 1) {
3941 Py_DECREF(f);
3942 _PyObject_GC_TRACK(f);
3943 }
3944 else {
3945 ++tstate->recursion_depth;
3946 Py_DECREF(f);
3947 --tstate->recursion_depth;
3948 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003949 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003950}
3951
Victor Stinner40ee3012014-06-16 15:59:28 +02003952PyObject *
3953PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003954 PyObject *const *args, int argcount,
3955 PyObject *const *kws, int kwcount,
3956 PyObject *const *defs, int defcount,
3957 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02003958{
3959 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02003960 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06003961 kws, kws != NULL ? kws + 1 : NULL,
3962 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02003963 defs, defcount,
3964 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003965 NULL, NULL);
3966}
Tim Peters5ca576e2001-06-18 22:08:13 +00003967
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003968static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05003969special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003970{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003971 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05003972 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003973 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003974 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003975 return NULL;
3976 }
3977 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003978}
3979
3980
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003981/* Logic for the raise statement (too complicated for inlining).
3982 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003983static int
Collin Winter828f04a2007-08-31 00:04:24 +00003984do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003985{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003986 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003988 if (exc == NULL) {
3989 /* Reraise */
3990 PyThreadState *tstate = PyThreadState_GET();
Mark Shannonae3087c2017-10-22 22:41:51 +01003991 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003992 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01003993 type = exc_info->exc_type;
3994 value = exc_info->exc_value;
3995 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02003996 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003997 PyErr_SetString(PyExc_RuntimeError,
3998 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003999 return 0;
4000 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004001 Py_XINCREF(type);
4002 Py_XINCREF(value);
4003 Py_XINCREF(tb);
4004 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004005 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004006 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004008 /* We support the following forms of raise:
4009 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004010 raise <instance>
4011 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004013 if (PyExceptionClass_Check(exc)) {
4014 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004015 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004016 if (value == NULL)
4017 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004018 if (!PyExceptionInstance_Check(value)) {
4019 PyErr_Format(PyExc_TypeError,
4020 "calling %R should have returned an instance of "
4021 "BaseException, not %R",
4022 type, Py_TYPE(value));
4023 goto raise_error;
4024 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004025 }
4026 else if (PyExceptionInstance_Check(exc)) {
4027 value = exc;
4028 type = PyExceptionInstance_Class(exc);
4029 Py_INCREF(type);
4030 }
4031 else {
4032 /* Not something you can raise. You get an exception
4033 anyway, just not what you specified :-) */
4034 Py_DECREF(exc);
4035 PyErr_SetString(PyExc_TypeError,
4036 "exceptions must derive from BaseException");
4037 goto raise_error;
4038 }
Collin Winter828f04a2007-08-31 00:04:24 +00004039
Serhiy Storchakac0191582016-09-27 11:37:10 +03004040 assert(type != NULL);
4041 assert(value != NULL);
4042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004043 if (cause) {
4044 PyObject *fixed_cause;
4045 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004046 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004047 if (fixed_cause == NULL)
4048 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004049 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004050 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004051 else if (PyExceptionInstance_Check(cause)) {
4052 fixed_cause = cause;
4053 }
4054 else if (cause == Py_None) {
4055 Py_DECREF(cause);
4056 fixed_cause = NULL;
4057 }
4058 else {
4059 PyErr_SetString(PyExc_TypeError,
4060 "exception causes must derive from "
4061 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004062 goto raise_error;
4063 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004064 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004065 }
Collin Winter828f04a2007-08-31 00:04:24 +00004066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004067 PyErr_SetObject(type, value);
4068 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004069 Py_DECREF(value);
4070 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004071 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004072
4073raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004074 Py_XDECREF(value);
4075 Py_XDECREF(type);
4076 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004077 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004078}
4079
Tim Petersd6d010b2001-06-21 02:49:55 +00004080/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004081 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004082
Guido van Rossum0368b722007-05-11 16:50:42 +00004083 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4084 with a variable target.
4085*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004086
Barry Warsawe42b18f1997-08-25 22:13:04 +00004087static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004088unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004089{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004090 int i = 0, j = 0;
4091 Py_ssize_t ll = 0;
4092 PyObject *it; /* iter(v) */
4093 PyObject *w;
4094 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004096 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004098 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004099 if (it == NULL) {
4100 if (PyErr_ExceptionMatches(PyExc_TypeError) &&
4101 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4102 {
4103 PyErr_Format(PyExc_TypeError,
4104 "cannot unpack non-iterable %.200s object",
4105 v->ob_type->tp_name);
4106 }
4107 return 0;
4108 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004110 for (; i < argcnt; i++) {
4111 w = PyIter_Next(it);
4112 if (w == NULL) {
4113 /* Iterator done, via error or exhaustion. */
4114 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004115 if (argcntafter == -1) {
4116 PyErr_Format(PyExc_ValueError,
4117 "not enough values to unpack (expected %d, got %d)",
4118 argcnt, i);
4119 }
4120 else {
4121 PyErr_Format(PyExc_ValueError,
4122 "not enough values to unpack "
4123 "(expected at least %d, got %d)",
4124 argcnt + argcntafter, i);
4125 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004126 }
4127 goto Error;
4128 }
4129 *--sp = w;
4130 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004132 if (argcntafter == -1) {
4133 /* We better have exhausted the iterator now. */
4134 w = PyIter_Next(it);
4135 if (w == NULL) {
4136 if (PyErr_Occurred())
4137 goto Error;
4138 Py_DECREF(it);
4139 return 1;
4140 }
4141 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004142 PyErr_Format(PyExc_ValueError,
4143 "too many values to unpack (expected %d)",
4144 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004145 goto Error;
4146 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004148 l = PySequence_List(it);
4149 if (l == NULL)
4150 goto Error;
4151 *--sp = l;
4152 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004154 ll = PyList_GET_SIZE(l);
4155 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004156 PyErr_Format(PyExc_ValueError,
4157 "not enough values to unpack (expected at least %d, got %zd)",
4158 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004159 goto Error;
4160 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004162 /* Pop the "after-variable" args off the list. */
4163 for (j = argcntafter; j > 0; j--, i++) {
4164 *--sp = PyList_GET_ITEM(l, ll - j);
4165 }
4166 /* Resize the list. */
4167 Py_SIZE(l) = ll - argcntafter;
4168 Py_DECREF(it);
4169 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004170
Tim Petersd6d010b2001-06-21 02:49:55 +00004171Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004172 for (; i > 0; i--, sp++)
4173 Py_DECREF(*sp);
4174 Py_XDECREF(it);
4175 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004176}
4177
4178
Guido van Rossum96a42c81992-01-12 02:29:51 +00004179#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004180static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004181prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004183 printf("%s ", str);
4184 if (PyObject_Print(v, stdout, 0) != 0)
4185 PyErr_Clear(); /* Don't know what else to do */
4186 printf("\n");
4187 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004188}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004189#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004190
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004191static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004192call_exc_trace(Py_tracefunc func, PyObject *self,
4193 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004194{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004195 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004196 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004197 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004198 if (value == NULL) {
4199 value = Py_None;
4200 Py_INCREF(value);
4201 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004202 PyErr_NormalizeException(&type, &value, &orig_traceback);
4203 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004204 arg = PyTuple_Pack(3, type, value, traceback);
4205 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004206 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004207 return;
4208 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004209 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004210 Py_DECREF(arg);
4211 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004212 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004213 else {
4214 Py_XDECREF(type);
4215 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004216 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004217 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004218}
4219
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004220static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004221call_trace_protected(Py_tracefunc func, PyObject *obj,
4222 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004223 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004224{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004225 PyObject *type, *value, *traceback;
4226 int err;
4227 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004228 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004229 if (err == 0)
4230 {
4231 PyErr_Restore(type, value, traceback);
4232 return 0;
4233 }
4234 else {
4235 Py_XDECREF(type);
4236 Py_XDECREF(value);
4237 Py_XDECREF(traceback);
4238 return -1;
4239 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004240}
4241
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004242static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004243call_trace(Py_tracefunc func, PyObject *obj,
4244 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004245 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004246{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004247 int result;
4248 if (tstate->tracing)
4249 return 0;
4250 tstate->tracing++;
4251 tstate->use_tracing = 0;
4252 result = func(obj, frame, what, arg);
4253 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4254 || (tstate->c_profilefunc != NULL));
4255 tstate->tracing--;
4256 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004257}
4258
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004259PyObject *
4260_PyEval_CallTracing(PyObject *func, PyObject *args)
4261{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004262 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004263 int save_tracing = tstate->tracing;
4264 int save_use_tracing = tstate->use_tracing;
4265 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004267 tstate->tracing = 0;
4268 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4269 || (tstate->c_profilefunc != NULL));
4270 result = PyObject_Call(func, args, NULL);
4271 tstate->tracing = save_tracing;
4272 tstate->use_tracing = save_use_tracing;
4273 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004274}
4275
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004276/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004277static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004278maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004279 PyThreadState *tstate, PyFrameObject *frame,
4280 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004281{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004282 int result = 0;
4283 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004285 /* If the last instruction executed isn't in the current
4286 instruction window, reset the window.
4287 */
4288 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4289 PyAddrPair bounds;
4290 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4291 &bounds);
4292 *instr_lb = bounds.ap_lower;
4293 *instr_ub = bounds.ap_upper;
4294 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004295 /* If the last instruction falls at the start of a line or if it
4296 represents a jump backwards, update the frame's line number and
4297 then call the trace function if we're tracing source lines.
4298 */
4299 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004300 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004301 if (frame->f_trace_lines) {
4302 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4303 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004304 }
George King20faa682017-10-18 17:44:22 -07004305 /* Always emit an opcode event if we're tracing all opcodes. */
4306 if (frame->f_trace_opcodes) {
4307 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4308 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004309 *instr_prev = frame->f_lasti;
4310 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004311}
4312
Fred Drake5755ce62001-06-27 19:19:46 +00004313void
4314PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004316 PyThreadState *tstate = PyThreadState_GET();
4317 PyObject *temp = tstate->c_profileobj;
4318 Py_XINCREF(arg);
4319 tstate->c_profilefunc = NULL;
4320 tstate->c_profileobj = NULL;
4321 /* Must make sure that tracing is not ignored if 'temp' is freed */
4322 tstate->use_tracing = tstate->c_tracefunc != NULL;
4323 Py_XDECREF(temp);
4324 tstate->c_profilefunc = func;
4325 tstate->c_profileobj = arg;
4326 /* Flag that tracing or profiling is turned on */
4327 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004328}
4329
4330void
4331PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4332{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004333 PyThreadState *tstate = PyThreadState_GET();
4334 PyObject *temp = tstate->c_traceobj;
4335 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4336 Py_XINCREF(arg);
4337 tstate->c_tracefunc = NULL;
4338 tstate->c_traceobj = NULL;
4339 /* Must make sure that profiling is not ignored if 'temp' is freed */
4340 tstate->use_tracing = tstate->c_profilefunc != NULL;
4341 Py_XDECREF(temp);
4342 tstate->c_tracefunc = func;
4343 tstate->c_traceobj = arg;
4344 /* Flag that tracing or profiling is turned on */
4345 tstate->use_tracing = ((func != NULL)
4346 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004347}
4348
Yury Selivanov75445082015-05-11 22:57:16 -04004349void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004350_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4351{
4352 assert(new_depth >= 0);
4353 PyThreadState *tstate = PyThreadState_GET();
4354 tstate->coroutine_origin_tracking_depth = new_depth;
4355}
4356
4357int
4358_PyEval_GetCoroutineOriginTrackingDepth(void)
4359{
4360 PyThreadState *tstate = PyThreadState_GET();
4361 return tstate->coroutine_origin_tracking_depth;
4362}
4363
4364void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004365_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004366{
4367 PyThreadState *tstate = PyThreadState_GET();
4368
Yury Selivanov75445082015-05-11 22:57:16 -04004369 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004370 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004371}
4372
4373PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004374_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004375{
4376 PyThreadState *tstate = PyThreadState_GET();
4377 return tstate->coroutine_wrapper;
4378}
4379
Yury Selivanoveb636452016-09-08 22:01:51 -07004380void
4381_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4382{
4383 PyThreadState *tstate = PyThreadState_GET();
4384
4385 Py_XINCREF(firstiter);
4386 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4387}
4388
4389PyObject *
4390_PyEval_GetAsyncGenFirstiter(void)
4391{
4392 PyThreadState *tstate = PyThreadState_GET();
4393 return tstate->async_gen_firstiter;
4394}
4395
4396void
4397_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4398{
4399 PyThreadState *tstate = PyThreadState_GET();
4400
4401 Py_XINCREF(finalizer);
4402 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4403}
4404
4405PyObject *
4406_PyEval_GetAsyncGenFinalizer(void)
4407{
4408 PyThreadState *tstate = PyThreadState_GET();
4409 return tstate->async_gen_finalizer;
4410}
4411
Guido van Rossumb209a111997-04-29 18:18:01 +00004412PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004413PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004414{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004415 PyFrameObject *current_frame = PyEval_GetFrame();
4416 if (current_frame == NULL)
4417 return PyThreadState_GET()->interp->builtins;
4418 else
4419 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004420}
4421
Guido van Rossumb209a111997-04-29 18:18:01 +00004422PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004423PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004425 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004426 if (current_frame == NULL) {
4427 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004428 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004429 }
4430
4431 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4432 return NULL;
4433
4434 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004435 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004436}
4437
Guido van Rossumb209a111997-04-29 18:18:01 +00004438PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004439PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004441 PyFrameObject *current_frame = PyEval_GetFrame();
4442 if (current_frame == NULL)
4443 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004444
4445 assert(current_frame->f_globals != NULL);
4446 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004447}
4448
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004449PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004450PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004451{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004452 PyThreadState *tstate = PyThreadState_GET();
4453 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004454}
4455
Guido van Rossum6135a871995-01-09 17:53:26 +00004456int
Tim Peters5ba58662001-07-16 02:29:45 +00004457PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004458{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004459 PyFrameObject *current_frame = PyEval_GetFrame();
4460 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004462 if (current_frame != NULL) {
4463 const int codeflags = current_frame->f_code->co_flags;
4464 const int compilerflags = codeflags & PyCF_MASK;
4465 if (compilerflags) {
4466 result = 1;
4467 cf->cf_flags |= compilerflags;
4468 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004469#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004470 if (codeflags & CO_GENERATOR_ALLOWED) {
4471 result = 1;
4472 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4473 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004474#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004475 }
4476 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004477}
4478
Guido van Rossum3f5da241990-12-20 15:06:42 +00004479
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004480const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004481PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004482{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004483 if (PyMethod_Check(func))
4484 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4485 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004486 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004487 else if (PyCFunction_Check(func))
4488 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4489 else
4490 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004491}
4492
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004493const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004494PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004495{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004496 if (PyMethod_Check(func))
4497 return "()";
4498 else if (PyFunction_Check(func))
4499 return "()";
4500 else if (PyCFunction_Check(func))
4501 return "()";
4502 else
4503 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004504}
4505
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004506#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004507if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004508 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4509 tstate, tstate->frame, \
4510 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004511 x = NULL; \
4512 } \
4513 else { \
4514 x = call; \
4515 if (tstate->c_profilefunc != NULL) { \
4516 if (x == NULL) { \
4517 call_trace_protected(tstate->c_profilefunc, \
4518 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004519 tstate, tstate->frame, \
4520 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004521 /* XXX should pass (type, value, tb) */ \
4522 } else { \
4523 if (call_trace(tstate->c_profilefunc, \
4524 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004525 tstate, tstate->frame, \
4526 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004527 Py_DECREF(x); \
4528 x = NULL; \
4529 } \
4530 } \
4531 } \
4532 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004533} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004534 x = call; \
4535 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004536
Victor Stinner415c5102017-01-11 00:54:57 +01004537/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4538 to reduce the stack consumption. */
4539Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004540call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004541{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004542 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004543 PyObject *func = *pfunc;
4544 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004545 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4546 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004547 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004549 /* Always dispatch PyCFunction first, because these are
4550 presumed to be the most frequent callable object.
4551 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004552 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004553 PyThreadState *tstate = PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004554 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004555 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004556 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
4557 PyThreadState *tstate = PyThreadState_GET();
INADA Naoki93fac8d2017-03-07 14:24:37 +09004558 if (tstate->use_tracing && tstate->c_profilefunc) {
4559 // We need to create PyCFunctionObject for tracing.
4560 PyMethodDescrObject *descr = (PyMethodDescrObject*)func;
4561 func = PyCFunction_NewEx(descr->d_method, stack[0], NULL);
4562 if (func == NULL) {
4563 return NULL;
4564 }
4565 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack+1, nargs-1,
4566 kwnames));
4567 Py_DECREF(func);
4568 }
4569 else {
4570 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4571 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004572 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004573 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004574 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004575 /* Optimize access to bound methods. Reuse the Python stack
4576 to pass 'self' as the first argument, replace 'func'
4577 with 'self'. It avoids the creation of a new temporary tuple
4578 for arguments (to replace func with self) when the method uses
4579 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004580 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004581 Py_INCREF(self);
4582 func = PyMethod_GET_FUNCTION(func);
4583 Py_INCREF(func);
4584 Py_SETREF(*pfunc, self);
4585 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004586 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004587 }
4588 else {
4589 Py_INCREF(func);
4590 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004591
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004592 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004593 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004594 }
4595 else {
4596 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4597 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004598 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004599 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004600
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004601 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4602
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004603 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004604 while ((*pp_stack) > pfunc) {
4605 w = EXT_POP(*pp_stack);
4606 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004607 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004609 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004610}
4611
Jeremy Hylton52820442001-01-03 23:52:36 +00004612static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004613do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004614{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004615 if (PyCFunction_Check(func)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004616 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004617 PyThreadState *tstate = PyThreadState_GET();
4618 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004619 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004620 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004621 else {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004622 return PyObject_Call(func, callargs, kwdict);
Victor Stinner74319ae2016-08-25 00:04:09 +02004623 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004624}
4625
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004626/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004627 nb_index slot defined, and store in *pi.
4628 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004629 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004630 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004631*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004632int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004633_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004634{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004635 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004636 Py_ssize_t x;
4637 if (PyIndex_Check(v)) {
4638 x = PyNumber_AsSsize_t(v, NULL);
4639 if (x == -1 && PyErr_Occurred())
4640 return 0;
4641 }
4642 else {
4643 PyErr_SetString(PyExc_TypeError,
4644 "slice indices must be integers or "
4645 "None or have an __index__ method");
4646 return 0;
4647 }
4648 *pi = x;
4649 }
4650 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004651}
4652
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004653int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004654_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004655{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004656 Py_ssize_t x;
4657 if (PyIndex_Check(v)) {
4658 x = PyNumber_AsSsize_t(v, NULL);
4659 if (x == -1 && PyErr_Occurred())
4660 return 0;
4661 }
4662 else {
4663 PyErr_SetString(PyExc_TypeError,
4664 "slice indices must be integers or "
4665 "have an __index__ method");
4666 return 0;
4667 }
4668 *pi = x;
4669 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004670}
4671
4672
Guido van Rossum486364b2007-06-30 05:01:58 +00004673#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004674 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004675
Guido van Rossumb209a111997-04-29 18:18:01 +00004676static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004677cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004678{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004679 int res = 0;
4680 switch (op) {
4681 case PyCmp_IS:
4682 res = (v == w);
4683 break;
4684 case PyCmp_IS_NOT:
4685 res = (v != w);
4686 break;
4687 case PyCmp_IN:
4688 res = PySequence_Contains(w, v);
4689 if (res < 0)
4690 return NULL;
4691 break;
4692 case PyCmp_NOT_IN:
4693 res = PySequence_Contains(w, v);
4694 if (res < 0)
4695 return NULL;
4696 res = !res;
4697 break;
4698 case PyCmp_EXC_MATCH:
4699 if (PyTuple_Check(w)) {
4700 Py_ssize_t i, length;
4701 length = PyTuple_Size(w);
4702 for (i = 0; i < length; i += 1) {
4703 PyObject *exc = PyTuple_GET_ITEM(w, i);
4704 if (!PyExceptionClass_Check(exc)) {
4705 PyErr_SetString(PyExc_TypeError,
4706 CANNOT_CATCH_MSG);
4707 return NULL;
4708 }
4709 }
4710 }
4711 else {
4712 if (!PyExceptionClass_Check(w)) {
4713 PyErr_SetString(PyExc_TypeError,
4714 CANNOT_CATCH_MSG);
4715 return NULL;
4716 }
4717 }
4718 res = PyErr_GivenExceptionMatches(v, w);
4719 break;
4720 default:
4721 return PyObject_RichCompare(v, w, op);
4722 }
4723 v = res ? Py_True : Py_False;
4724 Py_INCREF(v);
4725 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004726}
4727
Thomas Wouters52152252000-08-17 22:55:00 +00004728static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004729import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4730{
4731 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004732 PyObject *import_func, *res;
4733 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004734
4735 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
4736 if (import_func == NULL) {
4737 PyErr_SetString(PyExc_ImportError, "__import__ not found");
4738 return NULL;
4739 }
4740
4741 /* Fast path for not overloaded __import__. */
4742 if (import_func == PyThreadState_GET()->interp->import_func) {
4743 int ilevel = _PyLong_AsInt(level);
4744 if (ilevel == -1 && PyErr_Occurred()) {
4745 return NULL;
4746 }
4747 res = PyImport_ImportModuleLevelObject(
4748 name,
4749 f->f_globals,
4750 f->f_locals == NULL ? Py_None : f->f_locals,
4751 fromlist,
4752 ilevel);
4753 return res;
4754 }
4755
4756 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004757
4758 stack[0] = name;
4759 stack[1] = f->f_globals;
4760 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
4761 stack[3] = fromlist;
4762 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02004763 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004764 Py_DECREF(import_func);
4765 return res;
4766}
4767
4768static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004769import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004770{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004771 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02004772 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08004773 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004774
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004775 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02004776 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004777 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004778 /* Issue #17636: in case this failed because of a circular relative
4779 import, try to fallback on reading the module directly from
4780 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02004781 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07004782 if (pkgname == NULL) {
4783 goto error;
4784 }
Oren Milman6db70332017-09-19 14:23:01 +03004785 if (!PyUnicode_Check(pkgname)) {
4786 Py_CLEAR(pkgname);
4787 goto error;
4788 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004789 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07004790 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08004791 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004792 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07004793 }
Eric Snow3f9eee62017-09-15 16:35:20 -06004794 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004795 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07004796 if (x == NULL) {
4797 goto error;
4798 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004799 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004800 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07004801 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004802 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004803 if (pkgname == NULL) {
4804 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
4805 if (pkgname_or_unknown == NULL) {
4806 Py_XDECREF(pkgpath);
4807 return NULL;
4808 }
4809 } else {
4810 pkgname_or_unknown = pkgname;
4811 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004812
4813 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
4814 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08004815 errmsg = PyUnicode_FromFormat(
4816 "cannot import name %R from %R (unknown location)",
4817 name, pkgname_or_unknown
4818 );
4819 /* NULL check for errmsg done by PyErr_SetImportError. */
4820 PyErr_SetImportError(errmsg, pkgname, NULL);
4821 }
4822 else {
4823 errmsg = PyUnicode_FromFormat(
4824 "cannot import name %R from %R (%S)",
4825 name, pkgname_or_unknown, pkgpath
4826 );
4827 /* NULL check for errmsg done by PyErr_SetImportError. */
4828 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004829 }
4830
Xiang Zhang4830f582017-03-21 11:13:42 +08004831 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004832 Py_XDECREF(pkgname_or_unknown);
4833 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07004834 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00004835}
Guido van Rossumac7be682001-01-17 15:42:30 +00004836
Thomas Wouters52152252000-08-17 22:55:00 +00004837static int
4838import_all_from(PyObject *locals, PyObject *v)
4839{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004840 _Py_IDENTIFIER(__all__);
4841 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004842 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004843 int skip_leading_underscores = 0;
4844 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004845
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004846 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
4847 return -1; /* Unexpected error */
4848 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004849 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004850 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
4851 return -1;
4852 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004853 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004854 PyErr_SetString(PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004855 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004856 return -1;
4857 }
4858 all = PyMapping_Keys(dict);
4859 Py_DECREF(dict);
4860 if (all == NULL)
4861 return -1;
4862 skip_leading_underscores = 1;
4863 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004865 for (pos = 0, err = 0; ; pos++) {
4866 name = PySequence_GetItem(all, pos);
4867 if (name == NULL) {
4868 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4869 err = -1;
4870 else
4871 PyErr_Clear();
4872 break;
4873 }
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03004874 if (skip_leading_underscores && PyUnicode_Check(name)) {
4875 if (PyUnicode_READY(name) == -1) {
4876 Py_DECREF(name);
4877 err = -1;
4878 break;
4879 }
4880 if (PyUnicode_READ_CHAR(name, 0) == '_') {
4881 Py_DECREF(name);
4882 continue;
4883 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004884 }
4885 value = PyObject_GetAttr(v, name);
4886 if (value == NULL)
4887 err = -1;
4888 else if (PyDict_CheckExact(locals))
4889 err = PyDict_SetItem(locals, name, value);
4890 else
4891 err = PyObject_SetItem(locals, name, value);
4892 Py_DECREF(name);
4893 Py_XDECREF(value);
4894 if (err != 0)
4895 break;
4896 }
4897 Py_DECREF(all);
4898 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004899}
4900
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03004901static int
4902check_args_iterable(PyObject *func, PyObject *args)
4903{
4904 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
4905 PyErr_Format(PyExc_TypeError,
4906 "%.200s%.200s argument after * "
4907 "must be an iterable, not %.200s",
4908 PyEval_GetFuncName(func),
4909 PyEval_GetFuncDesc(func),
4910 args->ob_type->tp_name);
4911 return -1;
4912 }
4913 return 0;
4914}
4915
4916static void
4917format_kwargs_mapping_error(PyObject *func, PyObject *kwargs)
4918{
4919 PyErr_Format(PyExc_TypeError,
4920 "%.200s%.200s argument after ** "
4921 "must be a mapping, not %.200s",
4922 PyEval_GetFuncName(func),
4923 PyEval_GetFuncDesc(func),
4924 kwargs->ob_type->tp_name);
4925}
4926
Guido van Rossumac7be682001-01-17 15:42:30 +00004927static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004928format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004929{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004930 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004932 if (!obj)
4933 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004934
Serhiy Storchaka06515832016-11-20 09:13:07 +02004935 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004936 if (!obj_str)
4937 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004939 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004940}
Guido van Rossum950361c1997-01-24 13:49:28 +00004941
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004942static void
4943format_exc_unbound(PyCodeObject *co, int oparg)
4944{
4945 PyObject *name;
4946 /* Don't stomp existing exception */
4947 if (PyErr_Occurred())
4948 return;
4949 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4950 name = PyTuple_GET_ITEM(co->co_cellvars,
4951 oparg);
4952 format_exc_check_arg(
4953 PyExc_UnboundLocalError,
4954 UNBOUNDLOCAL_ERROR_MSG,
4955 name);
4956 } else {
4957 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4958 PyTuple_GET_SIZE(co->co_cellvars));
4959 format_exc_check_arg(PyExc_NameError,
4960 UNBOUNDFREE_ERROR_MSG, name);
4961 }
4962}
4963
Miss Islington (bot)fcd4e032018-04-04 07:09:00 -07004964static void
4965format_awaitable_error(PyTypeObject *type, int prevopcode)
4966{
4967 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
4968 if (prevopcode == BEFORE_ASYNC_WITH) {
4969 PyErr_Format(PyExc_TypeError,
4970 "'async with' received an object from __aenter__ "
4971 "that does not implement __await__: %.100s",
4972 type->tp_name);
4973 }
4974 else if (prevopcode == WITH_CLEANUP_START) {
4975 PyErr_Format(PyExc_TypeError,
4976 "'async with' received an object from __aexit__ "
4977 "that does not implement __await__: %.100s",
4978 type->tp_name);
4979 }
4980 }
4981}
4982
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004983static PyObject *
4984unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03004985 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004986{
4987 PyObject *res;
4988 if (Py_REFCNT(v) == 2) {
4989 /* In the common case, there are 2 references to the value
4990 * stored in 'variable' when the += is performed: one on the
4991 * value stack (in 'v') and one still stored in the
4992 * 'variable'. We try to delete the variable now to reduce
4993 * the refcnt to 1.
4994 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03004995 int opcode, oparg;
4996 NEXTOPARG();
4997 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004998 case STORE_FAST:
4999 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005000 PyObject **fastlocals = f->f_localsplus;
5001 if (GETLOCAL(oparg) == v)
5002 SETLOCAL(oparg, NULL);
5003 break;
5004 }
5005 case STORE_DEREF:
5006 {
5007 PyObject **freevars = (f->f_localsplus +
5008 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005009 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005010 if (PyCell_GET(c) == v) {
5011 PyCell_SET(c, NULL);
5012 Py_DECREF(v);
5013 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005014 break;
5015 }
5016 case STORE_NAME:
5017 {
5018 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005019 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005020 PyObject *locals = f->f_locals;
5021 if (PyDict_CheckExact(locals) &&
5022 PyDict_GetItem(locals, name) == v) {
5023 if (PyDict_DelItem(locals, name) != 0) {
5024 PyErr_Clear();
5025 }
5026 }
5027 break;
5028 }
5029 }
5030 }
5031 res = v;
5032 PyUnicode_Append(&res, w);
5033 return res;
5034}
5035
Guido van Rossum950361c1997-01-24 13:49:28 +00005036#ifdef DYNAMIC_EXECUTION_PROFILE
5037
Skip Montanarof118cb12001-10-15 20:51:38 +00005038static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005039getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005040{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005041 int i;
5042 PyObject *l = PyList_New(256);
5043 if (l == NULL) return NULL;
5044 for (i = 0; i < 256; i++) {
5045 PyObject *x = PyLong_FromLong(a[i]);
5046 if (x == NULL) {
5047 Py_DECREF(l);
5048 return NULL;
5049 }
5050 PyList_SetItem(l, i, x);
5051 }
5052 for (i = 0; i < 256; i++)
5053 a[i] = 0;
5054 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005055}
5056
5057PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005058_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005059{
5060#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005061 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005062#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005063 int i;
5064 PyObject *l = PyList_New(257);
5065 if (l == NULL) return NULL;
5066 for (i = 0; i < 257; i++) {
5067 PyObject *x = getarray(dxpairs[i]);
5068 if (x == NULL) {
5069 Py_DECREF(l);
5070 return NULL;
5071 }
5072 PyList_SetItem(l, i, x);
5073 }
5074 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005075#endif
5076}
5077
5078#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005079
5080Py_ssize_t
5081_PyEval_RequestCodeExtraIndex(freefunc free)
5082{
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005083 PyInterpreterState *interp = PyThreadState_Get()->interp;
Brett Cannon5c4de282016-09-07 11:16:41 -07005084 Py_ssize_t new_index;
5085
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005086 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005087 return -1;
5088 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005089 new_index = interp->co_extra_user_count++;
5090 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005091 return new_index;
5092}
Łukasz Langaa785c872016-09-09 17:37:37 -07005093
5094static void
5095dtrace_function_entry(PyFrameObject *f)
5096{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005097 const char *filename;
5098 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005099 int lineno;
5100
5101 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5102 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5103 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5104
5105 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5106}
5107
5108static void
5109dtrace_function_return(PyFrameObject *f)
5110{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005111 const char *filename;
5112 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005113 int lineno;
5114
5115 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5116 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5117 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5118
5119 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5120}
5121
5122/* DTrace equivalent of maybe_call_line_trace. */
5123static void
5124maybe_dtrace_line(PyFrameObject *frame,
5125 int *instr_lb, int *instr_ub, int *instr_prev)
5126{
5127 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005128 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005129
5130 /* If the last instruction executed isn't in the current
5131 instruction window, reset the window.
5132 */
5133 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5134 PyAddrPair bounds;
5135 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5136 &bounds);
5137 *instr_lb = bounds.ap_lower;
5138 *instr_ub = bounds.ap_upper;
5139 }
5140 /* If the last instruction falls at the start of a line or if
5141 it represents a jump backwards, update the frame's line
5142 number and call the trace function. */
5143 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5144 frame->f_lineno = line;
5145 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5146 if (!co_filename)
5147 co_filename = "?";
5148 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5149 if (!co_name)
5150 co_name = "?";
5151 PyDTrace_LINE(co_filename, co_name, line);
5152 }
5153 *instr_prev = frame->f_lasti;
5154}