blob: 90ad591dcd8f2c4c522b350bbdac703edaff1499 [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);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +030072static 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
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400504static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000505static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000506
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600507#define _Py_TracingPossible _PyRuntime.ceval.tracing_possible
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000508
Guido van Rossum374a9221991-04-04 10:40:29 +0000509
Guido van Rossumb209a111997-04-29 18:18:01 +0000510PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000511PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000512{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 return PyEval_EvalCodeEx(co,
514 globals, locals,
515 (PyObject **)NULL, 0,
516 (PyObject **)NULL, 0,
517 (PyObject **)NULL, 0,
518 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000519}
520
521
522/* Interpreter main loop */
523
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000524PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000525PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 /* This is for backward compatibility with extension modules that
527 used this API; core interpreter code should call
528 PyEval_EvalFrameEx() */
529 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000530}
531
532PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000533PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000534{
Brett Cannon3cebf932016-09-05 15:33:46 -0700535 PyThreadState *tstate = PyThreadState_GET();
536 return tstate->interp->eval_frame(f, throwflag);
537}
538
Victor Stinnerc6944e72016-11-11 02:13:35 +0100539PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700540_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
541{
Guido van Rossum950361c1997-01-24 13:49:28 +0000542#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000544#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200545 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300546 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200547 int opcode; /* Current opcode */
548 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200549 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 PyObject *retval = NULL; /* Return value */
551 PyThreadState *tstate = PyThreadState_GET();
552 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 is true when the line being executed has changed. The
559 initial values are such as to make this false the first
560 time it is tested. */
561 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000562
Serhiy Storchakaab874002016-09-11 13:48:15 +0300563 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 PyObject *names;
565 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000566
Brett Cannon368b4b72012-04-02 12:17:59 -0400567#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200568 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400569#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200570
Antoine Pitroub52ec782009-01-25 16:34:23 +0000571/* Computed GOTOs, or
572 the-optimization-commonly-but-improperly-known-as-"threaded code"
573 using gcc's labels-as-values extension
574 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
575
576 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000578 combined with a lookup table of jump addresses. However, since the
579 indirect jump instruction is shared by all opcodes, the CPU will have a
580 hard time making the right prediction for where to jump next (actually,
581 it will be always wrong except in the uncommon case of a sequence of
582 several identical opcodes).
583
584 "Threaded code" in contrast, uses an explicit jump table and an explicit
585 indirect jump instruction at the end of each opcode. Since the jump
586 instruction is at a different address for each opcode, the CPU will make a
587 separate prediction for each of these instructions, which is equivalent to
588 predicting the second opcode of each opcode pair. These predictions have
589 a much better chance to turn out valid, especially in small bytecode loops.
590
591 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000593 and potentially many more instructions (depending on the pipeline width).
594 A correctly predicted branch, however, is nearly free.
595
596 At the time of this writing, the "threaded code" version is up to 15-20%
597 faster than the normal "switch" version, depending on the compiler and the
598 CPU architecture.
599
600 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
601 because it would render the measurements invalid.
602
603
604 NOTE: care must be taken that the compiler doesn't try to "optimize" the
605 indirect jumps by sharing them between all opcodes. Such optimizations
606 can be disabled on gcc by using the -fno-gcse flag (or possibly
607 -fno-crossjumping).
608*/
609
Antoine Pitrou042b1282010-08-13 21:15:58 +0000610#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000611#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000612#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000613#endif
614
Antoine Pitrou042b1282010-08-13 21:15:58 +0000615#ifdef HAVE_COMPUTED_GOTOS
616 #ifndef USE_COMPUTED_GOTOS
617 #define USE_COMPUTED_GOTOS 1
618 #endif
619#else
620 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
621 #error "Computed gotos are not supported on this compiler."
622 #endif
623 #undef USE_COMPUTED_GOTOS
624 #define USE_COMPUTED_GOTOS 0
625#endif
626
627#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000628/* Import the static jump table */
629#include "opcode_targets.h"
630
Antoine Pitroub52ec782009-01-25 16:34:23 +0000631#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 TARGET_##op: \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000634
Antoine Pitroub52ec782009-01-25 16:34:23 +0000635#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600637 if (!_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 FAST_DISPATCH(); \
639 } \
640 continue; \
641 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000642
643#ifdef LLTRACE
644#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700646 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300648 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300649 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 } \
651 goto fast_next_opcode; \
652 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000653#else
654#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700656 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300658 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300659 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 } \
661 goto fast_next_opcode; \
662 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000663#endif
664
665#else
666#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 case op:
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300668
Antoine Pitroub52ec782009-01-25 16:34:23 +0000669#define DISPATCH() continue
670#define FAST_DISPATCH() goto fast_next_opcode
671#endif
672
673
Neal Norwitza81d2202002-07-14 00:27:26 +0000674/* Tuple access macros */
675
676#ifndef Py_DEBUG
677#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
678#else
679#define GETITEM(v, i) PyTuple_GetItem((v), (i))
680#endif
681
Guido van Rossum374a9221991-04-04 10:40:29 +0000682/* Code access macros */
683
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300684/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600685#define INSTR_OFFSET() \
686 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300687#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300688 _Py_CODEUNIT word = *next_instr; \
689 opcode = _Py_OPCODE(word); \
690 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300691 next_instr++; \
692 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300693#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
694#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000695
Raymond Hettingerf606f872003-03-16 03:11:04 +0000696/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 Some opcodes tend to come in pairs thus making it possible to
698 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300699 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 Verifying the prediction costs a single high-speed test of a register
702 variable against a constant. If the pairing was good, then the
703 processor's own internal branch predication has a high likelihood of
704 success, resulting in a nearly zero-overhead transition to the
705 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300706 including its unpredictable switch-case branch. Combined with the
707 processor's internal branch prediction, a successful PREDICT has the
708 effect of making the two opcodes run as if they were a single new opcode
709 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000710
Georg Brandl86b2fb92008-07-16 03:43:04 +0000711 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 predictions turned-on and interpret the results as if some opcodes
713 had been combined or turn-off predictions so that the opcode frequency
714 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000715
716 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 the CPU to record separate branch prediction information for each
718 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000719
Raymond Hettingerf606f872003-03-16 03:11:04 +0000720*/
721
Antoine Pitrou042b1282010-08-13 21:15:58 +0000722#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000724#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300725#define PREDICT(op) \
726 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300727 _Py_CODEUNIT word = *next_instr; \
728 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300729 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300730 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300731 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300732 goto PRED_##op; \
733 } \
734 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000735#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300736#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000737
Raymond Hettingerf606f872003-03-16 03:11:04 +0000738
Guido van Rossum374a9221991-04-04 10:40:29 +0000739/* Stack manipulation macros */
740
Martin v. Löwis18e16552006-02-15 17:27:45 +0000741/* The stack can grow at most MAXINT deep, as co_nlocals and
742 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000743#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
744#define EMPTY() (STACK_LEVEL() == 0)
745#define TOP() (stack_pointer[-1])
746#define SECOND() (stack_pointer[-2])
747#define THIRD() (stack_pointer[-3])
748#define FOURTH() (stack_pointer[-4])
749#define PEEK(n) (stack_pointer[-(n)])
750#define SET_TOP(v) (stack_pointer[-1] = (v))
751#define SET_SECOND(v) (stack_pointer[-2] = (v))
752#define SET_THIRD(v) (stack_pointer[-3] = (v))
753#define SET_FOURTH(v) (stack_pointer[-4] = (v))
754#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
755#define BASIC_STACKADJ(n) (stack_pointer += n)
756#define BASIC_PUSH(v) (*stack_pointer++ = (v))
757#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000758
Guido van Rossum96a42c81992-01-12 02:29:51 +0000759#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000761 lltrace && prtrace(TOP(), "push")); \
762 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000764 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000766 lltrace && prtrace(TOP(), "stackadj")); \
767 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +0000768#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000769 prtrace((STACK_POINTER)[-1], "ext_pop")), \
770 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000771#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000772#define PUSH(v) BASIC_PUSH(v)
773#define POP() BASIC_POP()
774#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000775#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000776#endif
777
Guido van Rossum681d79a1995-07-18 14:51:37 +0000778/* Local variable macros */
779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000781
782/* The SETLOCAL() macro must not DECREF the local variable in-place and
783 then store the new value; it must copy the old value to a temporary
784 value, then store the new value, and then DECREF the temporary value.
785 This is because it is possible that during the DECREF the frame is
786 accessed by other code (e.g. a __del__ method or gc.collect()) and the
787 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000789 GETLOCAL(i) = value; \
790 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000791
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000792
793#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 while (STACK_LEVEL() > (b)->b_level) { \
795 PyObject *v = POP(); \
796 Py_XDECREF(v); \
797 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000798
799#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300800 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +0100802 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 assert(STACK_LEVEL() >= (b)->b_level + 3); \
804 while (STACK_LEVEL() > (b)->b_level + 3) { \
805 value = POP(); \
806 Py_XDECREF(value); \
807 } \
Mark Shannonae3087c2017-10-22 22:41:51 +0100808 exc_info = tstate->exc_info; \
809 type = exc_info->exc_type; \
810 value = exc_info->exc_value; \
811 traceback = exc_info->exc_traceback; \
812 exc_info->exc_type = POP(); \
813 exc_info->exc_value = POP(); \
814 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 Py_XDECREF(type); \
816 Py_XDECREF(value); \
817 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300818 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000819
Guido van Rossuma027efa1997-05-05 20:56:21 +0000820/* Start of code */
821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 /* push frame */
823 if (Py_EnterRecursiveCall(""))
824 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 if (tstate->use_tracing) {
829 if (tstate->c_tracefunc != NULL) {
830 /* tstate->c_tracefunc, if defined, is a
831 function that will be called on *every* entry
832 to a code block. Its return value, if not
833 None, is a function that will be called at
834 the start of each executed line of code.
835 (Actually, the function must return itself
836 in order to continue tracing.) The trace
837 functions are called with three arguments:
838 a pointer to the current frame, a string
839 indicating why the function is called, and
840 an argument which depends on the situation.
841 The global trace function is also called
842 whenever an exception is detected. */
843 if (call_trace_protected(tstate->c_tracefunc,
844 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100845 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 /* Trace function raised an error */
847 goto exit_eval_frame;
848 }
849 }
850 if (tstate->c_profilefunc != NULL) {
851 /* Similar for c_profilefunc, except it needn't
852 return itself and isn't called for "line" events */
853 if (call_trace_protected(tstate->c_profilefunc,
854 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100855 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 /* Profile function raised an error */
857 goto exit_eval_frame;
858 }
859 }
860 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000861
Łukasz Langaa785c872016-09-09 17:37:37 -0700862 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
863 dtrace_function_entry(f);
864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 co = f->f_code;
866 names = co->co_names;
867 consts = co->co_consts;
868 fastlocals = f->f_localsplus;
869 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300870 assert(PyBytes_Check(co->co_code));
871 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +0300872 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
873 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
874 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300875 /*
876 f->f_lasti refers to the index of the last instruction,
877 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000878
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300879 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500880 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 When the PREDICT() macros are enabled, some opcode pairs follow in
883 direct succession without updating f->f_lasti. A successful
884 prediction effectively links the two codes together as if they
885 were a single new opcode; accordingly,f->f_lasti will point to
886 the first code in the pair (for instance, GET_ITER followed by
887 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300888 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300890 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300891 next_instr = first_instr;
892 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +0300893 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
894 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300895 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 stack_pointer = f->f_stacktop;
897 assert(stack_pointer != NULL);
898 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +0200899 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000900
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000901
Tim Peters5ca576e2001-06-18 22:08:13 +0000902#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200903 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000904#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000905
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400906 if (throwflag) /* support for generator.throw() */
907 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000908
Victor Stinnerace47d72013-07-18 01:41:08 +0200909#ifdef Py_DEBUG
910 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100911 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +0000912 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +0200913 assert(!PyErr_Occurred());
914#endif
915
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200916main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 assert(stack_pointer >= f->f_valuestack); /* else underflow */
919 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +0200920 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 /* Do periodic things. Doing this every time through
923 the loop would add too much overhead, so we do it
924 only every Nth instruction. We also do it if
925 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
926 event needs attention (e.g. a signal handler or
927 async I/O handler); see Py_AddPendingCall() and
928 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000929
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600930 if (_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +0300931 opcode = _Py_OPCODE(*next_instr);
932 if (opcode == SETUP_FINALLY ||
933 opcode == SETUP_WITH ||
934 opcode == BEFORE_ASYNC_WITH ||
935 opcode == YIELD_FROM) {
936 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -0700937 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +0300938 - If we're about to enter the 'with:'. It will prevent
939 emitting a resource warning in the common idiom
940 'with open(path) as file:'.
941 - If we're about to enter the 'async with:'.
942 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -0700943 *very* useful, but might help in some cases and it's
944 traditional)
945 - If we're resuming a chain of nested 'yield from' or
946 'await' calls, then each frame is parked with YIELD_FROM
947 as its next opcode. If the user hit control-C we want to
948 wait until we've reached the innermost frame before
949 running the signal handler and raising KeyboardInterrupt
950 (see bpo-30039).
951 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 goto fast_next_opcode;
953 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600954 if (_Py_atomic_load_relaxed(
955 &_PyRuntime.ceval.pending.calls_to_do))
956 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400957 if (Py_MakePendingCalls() < 0)
958 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600960 if (_Py_atomic_load_relaxed(
961 &_PyRuntime.ceval.gil_drop_request))
962 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 /* Give another thread a chance */
964 if (PyThreadState_Swap(NULL) != tstate)
965 Py_FatalError("ceval: tstate mix-up");
966 drop_gil(tstate);
967
968 /* Other threads may run now */
969
970 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -0700971
972 /* Check if we should make a quick exit. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600973 if (_Py_IsFinalizing() &&
974 !_Py_CURRENTLY_FINALIZING(tstate))
975 {
Benjamin Peterson17548dd2014-06-16 22:59:07 -0700976 drop_gil(tstate);
977 PyThread_exit_thread();
978 }
979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 if (PyThreadState_Swap(tstate) != NULL)
981 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 }
983 /* Check for asynchronous exceptions. */
984 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400985 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 tstate->async_exc = NULL;
987 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400988 PyErr_SetNone(exc);
989 Py_DECREF(exc);
990 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 }
992 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 fast_next_opcode:
995 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000996
Łukasz Langaa785c872016-09-09 17:37:37 -0700997 if (PyDTrace_LINE_ENABLED())
998 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001003 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001004 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 /* see maybe_call_line_trace
1006 for expository comments */
1007 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 err = maybe_call_line_trace(tstate->c_tracefunc,
1010 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001011 tstate, f,
1012 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 /* Reload possibly changed frame fields */
1014 JUMPTO(f->f_lasti);
1015 if (f->f_stacktop != NULL) {
1016 stack_pointer = f->f_stacktop;
1017 f->f_stacktop = NULL;
1018 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001019 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001021 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001025
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001026 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001027 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001028#ifdef DYNAMIC_EXECUTION_PROFILE
1029#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 dxpairs[lastopcode][opcode]++;
1031 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001032#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001034#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001035
Guido van Rossum96a42c81992-01-12 02:29:51 +00001036#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001038
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 if (lltrace) {
1040 if (HAS_ARG(opcode)) {
1041 printf("%d: %d, %d\n",
1042 f->f_lasti, opcode, oparg);
1043 }
1044 else {
1045 printf("%d: %d\n",
1046 f->f_lasti, opcode);
1047 }
1048 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001049#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001054 It is essential that any operation that fails must goto error
1055 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 TARGET(NOP)
1058 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001059
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001060 TARGET(LOAD_FAST) {
1061 PyObject *value = GETLOCAL(oparg);
1062 if (value == NULL) {
1063 format_exc_check_arg(PyExc_UnboundLocalError,
1064 UNBOUNDLOCAL_ERROR_MSG,
1065 PyTuple_GetItem(co->co_varnames, oparg));
1066 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001068 Py_INCREF(value);
1069 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001071 }
1072
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001073 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001074 TARGET(LOAD_CONST) {
1075 PyObject *value = GETITEM(consts, oparg);
1076 Py_INCREF(value);
1077 PUSH(value);
1078 FAST_DISPATCH();
1079 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001080
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001081 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001082 TARGET(STORE_FAST) {
1083 PyObject *value = POP();
1084 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001086 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001087
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001088 TARGET(POP_TOP) {
1089 PyObject *value = POP();
1090 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001092 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001093
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001094 TARGET(ROT_TWO) {
1095 PyObject *top = TOP();
1096 PyObject *second = SECOND();
1097 SET_TOP(second);
1098 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001100 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001101
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001102 TARGET(ROT_THREE) {
1103 PyObject *top = TOP();
1104 PyObject *second = SECOND();
1105 PyObject *third = THIRD();
1106 SET_TOP(second);
1107 SET_SECOND(third);
1108 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001110 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001111
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001112 TARGET(ROT_FOUR) {
1113 PyObject *top = TOP();
1114 PyObject *second = SECOND();
1115 PyObject *third = THIRD();
1116 PyObject *fourth = FOURTH();
1117 SET_TOP(second);
1118 SET_SECOND(third);
1119 SET_THIRD(fourth);
1120 SET_FOURTH(top);
1121 FAST_DISPATCH();
1122 }
1123
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001124 TARGET(DUP_TOP) {
1125 PyObject *top = TOP();
1126 Py_INCREF(top);
1127 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001129 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001130
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001131 TARGET(DUP_TOP_TWO) {
1132 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001133 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001134 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001135 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001136 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001137 SET_TOP(top);
1138 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001139 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001140 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001141
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001142 TARGET(UNARY_POSITIVE) {
1143 PyObject *value = TOP();
1144 PyObject *res = PyNumber_Positive(value);
1145 Py_DECREF(value);
1146 SET_TOP(res);
1147 if (res == NULL)
1148 goto error;
1149 DISPATCH();
1150 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001151
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001152 TARGET(UNARY_NEGATIVE) {
1153 PyObject *value = TOP();
1154 PyObject *res = PyNumber_Negative(value);
1155 Py_DECREF(value);
1156 SET_TOP(res);
1157 if (res == NULL)
1158 goto error;
1159 DISPATCH();
1160 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001161
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001162 TARGET(UNARY_NOT) {
1163 PyObject *value = TOP();
1164 int err = PyObject_IsTrue(value);
1165 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 if (err == 0) {
1167 Py_INCREF(Py_True);
1168 SET_TOP(Py_True);
1169 DISPATCH();
1170 }
1171 else if (err > 0) {
1172 Py_INCREF(Py_False);
1173 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 DISPATCH();
1175 }
1176 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001177 goto error;
1178 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001179
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001180 TARGET(UNARY_INVERT) {
1181 PyObject *value = TOP();
1182 PyObject *res = PyNumber_Invert(value);
1183 Py_DECREF(value);
1184 SET_TOP(res);
1185 if (res == NULL)
1186 goto error;
1187 DISPATCH();
1188 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001189
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001190 TARGET(BINARY_POWER) {
1191 PyObject *exp = POP();
1192 PyObject *base = TOP();
1193 PyObject *res = PyNumber_Power(base, exp, Py_None);
1194 Py_DECREF(base);
1195 Py_DECREF(exp);
1196 SET_TOP(res);
1197 if (res == NULL)
1198 goto error;
1199 DISPATCH();
1200 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001201
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001202 TARGET(BINARY_MULTIPLY) {
1203 PyObject *right = POP();
1204 PyObject *left = TOP();
1205 PyObject *res = PyNumber_Multiply(left, right);
1206 Py_DECREF(left);
1207 Py_DECREF(right);
1208 SET_TOP(res);
1209 if (res == NULL)
1210 goto error;
1211 DISPATCH();
1212 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001213
Benjamin Petersond51374e2014-04-09 23:55:56 -04001214 TARGET(BINARY_MATRIX_MULTIPLY) {
1215 PyObject *right = POP();
1216 PyObject *left = TOP();
1217 PyObject *res = PyNumber_MatrixMultiply(left, right);
1218 Py_DECREF(left);
1219 Py_DECREF(right);
1220 SET_TOP(res);
1221 if (res == NULL)
1222 goto error;
1223 DISPATCH();
1224 }
1225
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001226 TARGET(BINARY_TRUE_DIVIDE) {
1227 PyObject *divisor = POP();
1228 PyObject *dividend = TOP();
1229 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1230 Py_DECREF(dividend);
1231 Py_DECREF(divisor);
1232 SET_TOP(quotient);
1233 if (quotient == NULL)
1234 goto error;
1235 DISPATCH();
1236 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001237
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001238 TARGET(BINARY_FLOOR_DIVIDE) {
1239 PyObject *divisor = POP();
1240 PyObject *dividend = TOP();
1241 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1242 Py_DECREF(dividend);
1243 Py_DECREF(divisor);
1244 SET_TOP(quotient);
1245 if (quotient == NULL)
1246 goto error;
1247 DISPATCH();
1248 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001249
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001250 TARGET(BINARY_MODULO) {
1251 PyObject *divisor = POP();
1252 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001253 PyObject *res;
1254 if (PyUnicode_CheckExact(dividend) && (
1255 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1256 // fast path; string formatting, but not if the RHS is a str subclass
1257 // (see issue28598)
1258 res = PyUnicode_Format(dividend, divisor);
1259 } else {
1260 res = PyNumber_Remainder(dividend, divisor);
1261 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001262 Py_DECREF(divisor);
1263 Py_DECREF(dividend);
1264 SET_TOP(res);
1265 if (res == NULL)
1266 goto error;
1267 DISPATCH();
1268 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001269
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001270 TARGET(BINARY_ADD) {
1271 PyObject *right = POP();
1272 PyObject *left = TOP();
1273 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001274 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1275 CPython using bytecode, it is simply worthless.
1276 See http://bugs.python.org/issue21955 and
1277 http://bugs.python.org/issue10044 for the discussion. In short,
1278 no patch shown any impact on a realistic benchmark, only a minor
1279 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001280 if (PyUnicode_CheckExact(left) &&
1281 PyUnicode_CheckExact(right)) {
1282 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001283 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001284 }
1285 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001286 sum = PyNumber_Add(left, right);
1287 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001288 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001289 Py_DECREF(right);
1290 SET_TOP(sum);
1291 if (sum == NULL)
1292 goto error;
1293 DISPATCH();
1294 }
1295
1296 TARGET(BINARY_SUBTRACT) {
1297 PyObject *right = POP();
1298 PyObject *left = TOP();
1299 PyObject *diff = PyNumber_Subtract(left, right);
1300 Py_DECREF(right);
1301 Py_DECREF(left);
1302 SET_TOP(diff);
1303 if (diff == NULL)
1304 goto error;
1305 DISPATCH();
1306 }
1307
1308 TARGET(BINARY_SUBSCR) {
1309 PyObject *sub = POP();
1310 PyObject *container = TOP();
1311 PyObject *res = PyObject_GetItem(container, sub);
1312 Py_DECREF(container);
1313 Py_DECREF(sub);
1314 SET_TOP(res);
1315 if (res == NULL)
1316 goto error;
1317 DISPATCH();
1318 }
1319
1320 TARGET(BINARY_LSHIFT) {
1321 PyObject *right = POP();
1322 PyObject *left = TOP();
1323 PyObject *res = PyNumber_Lshift(left, right);
1324 Py_DECREF(left);
1325 Py_DECREF(right);
1326 SET_TOP(res);
1327 if (res == NULL)
1328 goto error;
1329 DISPATCH();
1330 }
1331
1332 TARGET(BINARY_RSHIFT) {
1333 PyObject *right = POP();
1334 PyObject *left = TOP();
1335 PyObject *res = PyNumber_Rshift(left, right);
1336 Py_DECREF(left);
1337 Py_DECREF(right);
1338 SET_TOP(res);
1339 if (res == NULL)
1340 goto error;
1341 DISPATCH();
1342 }
1343
1344 TARGET(BINARY_AND) {
1345 PyObject *right = POP();
1346 PyObject *left = TOP();
1347 PyObject *res = PyNumber_And(left, right);
1348 Py_DECREF(left);
1349 Py_DECREF(right);
1350 SET_TOP(res);
1351 if (res == NULL)
1352 goto error;
1353 DISPATCH();
1354 }
1355
1356 TARGET(BINARY_XOR) {
1357 PyObject *right = POP();
1358 PyObject *left = TOP();
1359 PyObject *res = PyNumber_Xor(left, right);
1360 Py_DECREF(left);
1361 Py_DECREF(right);
1362 SET_TOP(res);
1363 if (res == NULL)
1364 goto error;
1365 DISPATCH();
1366 }
1367
1368 TARGET(BINARY_OR) {
1369 PyObject *right = POP();
1370 PyObject *left = TOP();
1371 PyObject *res = PyNumber_Or(left, right);
1372 Py_DECREF(left);
1373 Py_DECREF(right);
1374 SET_TOP(res);
1375 if (res == NULL)
1376 goto error;
1377 DISPATCH();
1378 }
1379
1380 TARGET(LIST_APPEND) {
1381 PyObject *v = POP();
1382 PyObject *list = PEEK(oparg);
1383 int err;
1384 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001386 if (err != 0)
1387 goto error;
1388 PREDICT(JUMP_ABSOLUTE);
1389 DISPATCH();
1390 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001391
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001392 TARGET(SET_ADD) {
1393 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001394 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001395 int err;
1396 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001398 if (err != 0)
1399 goto error;
1400 PREDICT(JUMP_ABSOLUTE);
1401 DISPATCH();
1402 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001403
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001404 TARGET(INPLACE_POWER) {
1405 PyObject *exp = POP();
1406 PyObject *base = TOP();
1407 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1408 Py_DECREF(base);
1409 Py_DECREF(exp);
1410 SET_TOP(res);
1411 if (res == NULL)
1412 goto error;
1413 DISPATCH();
1414 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001415
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001416 TARGET(INPLACE_MULTIPLY) {
1417 PyObject *right = POP();
1418 PyObject *left = TOP();
1419 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1420 Py_DECREF(left);
1421 Py_DECREF(right);
1422 SET_TOP(res);
1423 if (res == NULL)
1424 goto error;
1425 DISPATCH();
1426 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001427
Benjamin Petersond51374e2014-04-09 23:55:56 -04001428 TARGET(INPLACE_MATRIX_MULTIPLY) {
1429 PyObject *right = POP();
1430 PyObject *left = TOP();
1431 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1432 Py_DECREF(left);
1433 Py_DECREF(right);
1434 SET_TOP(res);
1435 if (res == NULL)
1436 goto error;
1437 DISPATCH();
1438 }
1439
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001440 TARGET(INPLACE_TRUE_DIVIDE) {
1441 PyObject *divisor = POP();
1442 PyObject *dividend = TOP();
1443 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1444 Py_DECREF(dividend);
1445 Py_DECREF(divisor);
1446 SET_TOP(quotient);
1447 if (quotient == NULL)
1448 goto error;
1449 DISPATCH();
1450 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001451
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001452 TARGET(INPLACE_FLOOR_DIVIDE) {
1453 PyObject *divisor = POP();
1454 PyObject *dividend = TOP();
1455 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1456 Py_DECREF(dividend);
1457 Py_DECREF(divisor);
1458 SET_TOP(quotient);
1459 if (quotient == NULL)
1460 goto error;
1461 DISPATCH();
1462 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001463
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001464 TARGET(INPLACE_MODULO) {
1465 PyObject *right = POP();
1466 PyObject *left = TOP();
1467 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1468 Py_DECREF(left);
1469 Py_DECREF(right);
1470 SET_TOP(mod);
1471 if (mod == NULL)
1472 goto error;
1473 DISPATCH();
1474 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001475
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001476 TARGET(INPLACE_ADD) {
1477 PyObject *right = POP();
1478 PyObject *left = TOP();
1479 PyObject *sum;
1480 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1481 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001482 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001483 }
1484 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001485 sum = PyNumber_InPlaceAdd(left, right);
1486 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001487 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001488 Py_DECREF(right);
1489 SET_TOP(sum);
1490 if (sum == NULL)
1491 goto error;
1492 DISPATCH();
1493 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001494
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001495 TARGET(INPLACE_SUBTRACT) {
1496 PyObject *right = POP();
1497 PyObject *left = TOP();
1498 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1499 Py_DECREF(left);
1500 Py_DECREF(right);
1501 SET_TOP(diff);
1502 if (diff == NULL)
1503 goto error;
1504 DISPATCH();
1505 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001506
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001507 TARGET(INPLACE_LSHIFT) {
1508 PyObject *right = POP();
1509 PyObject *left = TOP();
1510 PyObject *res = PyNumber_InPlaceLshift(left, right);
1511 Py_DECREF(left);
1512 Py_DECREF(right);
1513 SET_TOP(res);
1514 if (res == NULL)
1515 goto error;
1516 DISPATCH();
1517 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001518
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001519 TARGET(INPLACE_RSHIFT) {
1520 PyObject *right = POP();
1521 PyObject *left = TOP();
1522 PyObject *res = PyNumber_InPlaceRshift(left, right);
1523 Py_DECREF(left);
1524 Py_DECREF(right);
1525 SET_TOP(res);
1526 if (res == NULL)
1527 goto error;
1528 DISPATCH();
1529 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001530
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001531 TARGET(INPLACE_AND) {
1532 PyObject *right = POP();
1533 PyObject *left = TOP();
1534 PyObject *res = PyNumber_InPlaceAnd(left, right);
1535 Py_DECREF(left);
1536 Py_DECREF(right);
1537 SET_TOP(res);
1538 if (res == NULL)
1539 goto error;
1540 DISPATCH();
1541 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001542
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001543 TARGET(INPLACE_XOR) {
1544 PyObject *right = POP();
1545 PyObject *left = TOP();
1546 PyObject *res = PyNumber_InPlaceXor(left, right);
1547 Py_DECREF(left);
1548 Py_DECREF(right);
1549 SET_TOP(res);
1550 if (res == NULL)
1551 goto error;
1552 DISPATCH();
1553 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001554
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001555 TARGET(INPLACE_OR) {
1556 PyObject *right = POP();
1557 PyObject *left = TOP();
1558 PyObject *res = PyNumber_InPlaceOr(left, right);
1559 Py_DECREF(left);
1560 Py_DECREF(right);
1561 SET_TOP(res);
1562 if (res == NULL)
1563 goto error;
1564 DISPATCH();
1565 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001566
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001567 TARGET(STORE_SUBSCR) {
1568 PyObject *sub = TOP();
1569 PyObject *container = SECOND();
1570 PyObject *v = THIRD();
1571 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 STACKADJ(-3);
Martin Panter95f53c12016-07-18 08:23:26 +00001573 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001574 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001576 Py_DECREF(container);
1577 Py_DECREF(sub);
1578 if (err != 0)
1579 goto error;
1580 DISPATCH();
1581 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001582
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001583 TARGET(DELETE_SUBSCR) {
1584 PyObject *sub = TOP();
1585 PyObject *container = SECOND();
1586 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 STACKADJ(-2);
Martin Panter95f53c12016-07-18 08:23:26 +00001588 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001589 err = PyObject_DelItem(container, sub);
1590 Py_DECREF(container);
1591 Py_DECREF(sub);
1592 if (err != 0)
1593 goto error;
1594 DISPATCH();
1595 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001596
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001597 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001598 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001599 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001600 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001601 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001602 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 PyErr_SetString(PyExc_RuntimeError,
1604 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001605 Py_DECREF(value);
1606 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001608 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001609 Py_DECREF(value);
1610 if (res == NULL)
1611 goto error;
1612 Py_DECREF(res);
1613 DISPATCH();
1614 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001615
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001616 TARGET(RAISE_VARARGS) {
1617 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 switch (oparg) {
1619 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001620 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001621 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001623 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001624 /* fall through */
1625 case 0:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001626 if (do_raise(exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001627 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001628 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 break;
1630 default:
1631 PyErr_SetString(PyExc_SystemError,
1632 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 break;
1634 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001635 goto error;
1636 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001637
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001638 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001640 assert(f->f_iblock == 0);
1641 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001642 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001643
Yury Selivanov75445082015-05-11 22:57:16 -04001644 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001645 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001646 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001647 PyObject *obj = TOP();
1648 PyTypeObject *type = Py_TYPE(obj);
1649
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001650 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001651 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001652 }
Yury Selivanov75445082015-05-11 22:57:16 -04001653
1654 if (getter != NULL) {
1655 iter = (*getter)(obj);
1656 Py_DECREF(obj);
1657 if (iter == NULL) {
1658 SET_TOP(NULL);
1659 goto error;
1660 }
1661 }
1662 else {
1663 SET_TOP(NULL);
1664 PyErr_Format(
1665 PyExc_TypeError,
1666 "'async for' requires an object with "
1667 "__aiter__ method, got %.100s",
1668 type->tp_name);
1669 Py_DECREF(obj);
1670 goto error;
1671 }
1672
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001673 if (Py_TYPE(iter)->tp_as_async == NULL ||
1674 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001675
Yury Selivanov398ff912017-03-02 22:20:00 -05001676 SET_TOP(NULL);
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001677 PyErr_Format(
1678 PyExc_TypeError,
1679 "'async for' received an object from __aiter__ "
1680 "that does not implement __anext__: %.100s",
1681 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001682 Py_DECREF(iter);
1683 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001684 }
1685
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001686 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001687 DISPATCH();
1688 }
1689
1690 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001691 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001692 PyObject *next_iter = NULL;
1693 PyObject *awaitable = NULL;
1694 PyObject *aiter = TOP();
1695 PyTypeObject *type = Py_TYPE(aiter);
1696
Yury Selivanoveb636452016-09-08 22:01:51 -07001697 if (PyAsyncGen_CheckExact(aiter)) {
1698 awaitable = type->tp_as_async->am_anext(aiter);
1699 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001700 goto error;
1701 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001702 } else {
1703 if (type->tp_as_async != NULL){
1704 getter = type->tp_as_async->am_anext;
1705 }
Yury Selivanov75445082015-05-11 22:57:16 -04001706
Yury Selivanoveb636452016-09-08 22:01:51 -07001707 if (getter != NULL) {
1708 next_iter = (*getter)(aiter);
1709 if (next_iter == NULL) {
1710 goto error;
1711 }
1712 }
1713 else {
1714 PyErr_Format(
1715 PyExc_TypeError,
1716 "'async for' requires an iterator with "
1717 "__anext__ method, got %.100s",
1718 type->tp_name);
1719 goto error;
1720 }
Yury Selivanov75445082015-05-11 22:57:16 -04001721
Yury Selivanoveb636452016-09-08 22:01:51 -07001722 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1723 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001724 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001725 PyExc_TypeError,
1726 "'async for' received an invalid object "
1727 "from __anext__: %.100s",
1728 Py_TYPE(next_iter)->tp_name);
1729
1730 Py_DECREF(next_iter);
1731 goto error;
1732 } else {
1733 Py_DECREF(next_iter);
1734 }
1735 }
Yury Selivanov75445082015-05-11 22:57:16 -04001736
1737 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001738 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001739 DISPATCH();
1740 }
1741
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001742 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001743 TARGET(GET_AWAITABLE) {
1744 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001745 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001746
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03001747 if (iter == NULL) {
1748 format_awaitable_error(Py_TYPE(iterable),
1749 _Py_OPCODE(next_instr[-2]));
1750 }
1751
Yury Selivanov75445082015-05-11 22:57:16 -04001752 Py_DECREF(iterable);
1753
Yury Selivanovc724bae2016-03-02 11:30:46 -05001754 if (iter != NULL && PyCoro_CheckExact(iter)) {
1755 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1756 if (yf != NULL) {
1757 /* `iter` is a coroutine object that is being
1758 awaited, `yf` is a pointer to the current awaitable
1759 being awaited on. */
1760 Py_DECREF(yf);
1761 Py_CLEAR(iter);
1762 PyErr_SetString(
1763 PyExc_RuntimeError,
1764 "coroutine is being awaited already");
1765 /* The code below jumps to `error` if `iter` is NULL. */
1766 }
1767 }
1768
Yury Selivanov75445082015-05-11 22:57:16 -04001769 SET_TOP(iter); /* Even if it's NULL */
1770
1771 if (iter == NULL) {
1772 goto error;
1773 }
1774
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001775 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001776 DISPATCH();
1777 }
1778
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001779 TARGET(YIELD_FROM) {
1780 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001781 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001782 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001783 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1784 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001785 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001786 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001787 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001788 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001789 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001790 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001791 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001792 Py_DECREF(v);
1793 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001794 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001795 if (tstate->c_tracefunc != NULL
1796 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001797 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001798 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001799 if (err < 0)
1800 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001801 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001802 SET_TOP(val);
1803 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001804 }
Martin Panter95f53c12016-07-18 08:23:26 +00001805 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001806 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001807 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001808 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001809 f->f_lasti -= sizeof(_Py_CODEUNIT);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001810 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001811 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001812
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001813 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07001815
1816 if (co->co_flags & CO_ASYNC_GENERATOR) {
1817 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
1818 Py_DECREF(retval);
1819 if (w == NULL) {
1820 retval = NULL;
1821 goto error;
1822 }
1823 retval = w;
1824 }
1825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 f->f_stacktop = stack_pointer;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001827 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001828 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001829
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001830 TARGET(POP_EXCEPT) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001831 PyObject *type, *value, *traceback;
1832 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001833 PyTryBlock *b = PyFrame_BlockPop(f);
1834 if (b->b_type != EXCEPT_HANDLER) {
1835 PyErr_SetString(PyExc_SystemError,
1836 "popped block is not an except handler");
1837 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001839 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
1840 STACK_LEVEL() <= (b)->b_level + 4);
1841 exc_info = tstate->exc_info;
1842 type = exc_info->exc_type;
1843 value = exc_info->exc_value;
1844 traceback = exc_info->exc_traceback;
1845 exc_info->exc_type = POP();
1846 exc_info->exc_value = POP();
1847 exc_info->exc_traceback = POP();
1848 Py_XDECREF(type);
1849 Py_XDECREF(value);
1850 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001851 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001852 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001853
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001854 PREDICTED(POP_BLOCK);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001855 TARGET(POP_BLOCK) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001856 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001858 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001859
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001860 TARGET(POP_FINALLY) {
1861 /* If oparg is 0 at the top of the stack are 1 or 6 values:
1862 Either:
1863 - TOP = NULL or an integer
1864 or:
1865 - (TOP, SECOND, THIRD) = exc_info()
1866 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
1867
1868 If oparg is 1 the value for 'return' was additionally pushed
1869 at the top of the stack.
1870 */
1871 PyObject *res = NULL;
1872 if (oparg) {
1873 res = POP();
1874 }
1875 PyObject *exc = POP();
1876 if (exc == NULL || PyLong_CheckExact(exc)) {
1877 Py_XDECREF(exc);
1878 }
1879 else {
1880 Py_DECREF(exc);
1881 Py_DECREF(POP());
1882 Py_DECREF(POP());
1883
1884 PyObject *type, *value, *traceback;
1885 _PyErr_StackItem *exc_info;
1886 PyTryBlock *b = PyFrame_BlockPop(f);
1887 if (b->b_type != EXCEPT_HANDLER) {
1888 PyErr_SetString(PyExc_SystemError,
1889 "popped block is not an except handler");
1890 Py_XDECREF(res);
1891 goto error;
1892 }
1893 assert(STACK_LEVEL() == (b)->b_level + 3);
1894 exc_info = tstate->exc_info;
1895 type = exc_info->exc_type;
1896 value = exc_info->exc_value;
1897 traceback = exc_info->exc_traceback;
1898 exc_info->exc_type = POP();
1899 exc_info->exc_value = POP();
1900 exc_info->exc_traceback = POP();
1901 Py_XDECREF(type);
1902 Py_XDECREF(value);
1903 Py_XDECREF(traceback);
1904 }
1905 if (oparg) {
1906 PUSH(res);
1907 }
1908 DISPATCH();
1909 }
1910
1911 TARGET(CALL_FINALLY) {
1912 PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
1913 if (ret == NULL) {
1914 goto error;
1915 }
1916 PUSH(ret);
1917 JUMPBY(oparg);
1918 FAST_DISPATCH();
1919 }
1920
1921 TARGET(BEGIN_FINALLY) {
1922 /* Push NULL onto the stack for using it in END_FINALLY,
1923 POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH.
1924 */
1925 PUSH(NULL);
1926 FAST_DISPATCH();
1927 }
1928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001929 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001930 TARGET(END_FINALLY) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001931 /* At the top of the stack are 1 or 6 values:
1932 Either:
1933 - TOP = NULL or an integer
1934 or:
1935 - (TOP, SECOND, THIRD) = exc_info()
1936 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
1937 */
1938 PyObject *exc = POP();
1939 if (exc == NULL) {
1940 FAST_DISPATCH();
1941 }
1942 else if (PyLong_CheckExact(exc)) {
1943 int ret = _PyLong_AsInt(exc);
1944 Py_DECREF(exc);
1945 if (ret == -1 && PyErr_Occurred()) {
1946 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001948 JUMPTO(ret);
1949 FAST_DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001951 else {
1952 assert(PyExceptionClass_Check(exc));
1953 PyObject *val = POP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001954 PyObject *tb = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001955 PyErr_Restore(exc, val, tb);
1956 goto exception_unwind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001958 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001959
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02001960 TARGET(END_ASYNC_FOR) {
1961 PyObject *exc = POP();
1962 assert(PyExceptionClass_Check(exc));
1963 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
1964 PyTryBlock *b = PyFrame_BlockPop(f);
1965 assert(b->b_type == EXCEPT_HANDLER);
1966 Py_DECREF(exc);
1967 UNWIND_EXCEPT_HANDLER(b);
1968 Py_DECREF(POP());
1969 JUMPBY(oparg);
1970 FAST_DISPATCH();
1971 }
1972 else {
1973 PyObject *val = POP();
1974 PyObject *tb = POP();
1975 PyErr_Restore(exc, val, tb);
1976 goto exception_unwind;
1977 }
1978 }
1979
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001980 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02001981 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001982
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001983 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001984 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001985 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
1986 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02001987 PyErr_SetString(PyExc_NameError,
1988 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001989 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001990 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001991 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001992 }
1993 else {
1994 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
1995 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02001996 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001997 bc = PyObject_GetItem(f->f_builtins, build_class_str);
1998 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02001999 if (PyErr_ExceptionMatches(PyExc_KeyError))
2000 PyErr_SetString(PyExc_NameError,
2001 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002002 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002003 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002005 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002006 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002007 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002008
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002009 TARGET(STORE_NAME) {
2010 PyObject *name = GETITEM(names, oparg);
2011 PyObject *v = POP();
2012 PyObject *ns = f->f_locals;
2013 int err;
2014 if (ns == NULL) {
2015 PyErr_Format(PyExc_SystemError,
2016 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002017 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002018 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002019 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002020 if (PyDict_CheckExact(ns))
2021 err = PyDict_SetItem(ns, name, v);
2022 else
2023 err = PyObject_SetItem(ns, name, v);
2024 Py_DECREF(v);
2025 if (err != 0)
2026 goto error;
2027 DISPATCH();
2028 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002029
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002030 TARGET(DELETE_NAME) {
2031 PyObject *name = GETITEM(names, oparg);
2032 PyObject *ns = f->f_locals;
2033 int err;
2034 if (ns == NULL) {
2035 PyErr_Format(PyExc_SystemError,
2036 "no locals when deleting %R", name);
2037 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002039 err = PyObject_DelItem(ns, name);
2040 if (err != 0) {
2041 format_exc_check_arg(PyExc_NameError,
2042 NAME_ERROR_MSG,
2043 name);
2044 goto error;
2045 }
2046 DISPATCH();
2047 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002048
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002049 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002050 TARGET(UNPACK_SEQUENCE) {
2051 PyObject *seq = POP(), *item, **items;
2052 if (PyTuple_CheckExact(seq) &&
2053 PyTuple_GET_SIZE(seq) == oparg) {
2054 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002055 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002056 item = items[oparg];
2057 Py_INCREF(item);
2058 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002060 } else if (PyList_CheckExact(seq) &&
2061 PyList_GET_SIZE(seq) == oparg) {
2062 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002063 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002064 item = items[oparg];
2065 Py_INCREF(item);
2066 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002068 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 stack_pointer + oparg)) {
2070 STACKADJ(oparg);
2071 } else {
2072 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002073 Py_DECREF(seq);
2074 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002076 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002077 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002078 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002079
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002080 TARGET(UNPACK_EX) {
2081 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2082 PyObject *seq = POP();
2083
2084 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2085 stack_pointer + totalargs)) {
2086 stack_pointer += totalargs;
2087 } else {
2088 Py_DECREF(seq);
2089 goto error;
2090 }
2091 Py_DECREF(seq);
2092 DISPATCH();
2093 }
2094
2095 TARGET(STORE_ATTR) {
2096 PyObject *name = GETITEM(names, oparg);
2097 PyObject *owner = TOP();
2098 PyObject *v = SECOND();
2099 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002100 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002101 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002102 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002103 Py_DECREF(owner);
2104 if (err != 0)
2105 goto error;
2106 DISPATCH();
2107 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002108
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002109 TARGET(DELETE_ATTR) {
2110 PyObject *name = GETITEM(names, oparg);
2111 PyObject *owner = POP();
2112 int err;
2113 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2114 Py_DECREF(owner);
2115 if (err != 0)
2116 goto error;
2117 DISPATCH();
2118 }
2119
2120 TARGET(STORE_GLOBAL) {
2121 PyObject *name = GETITEM(names, oparg);
2122 PyObject *v = POP();
2123 int err;
2124 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002125 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002126 if (err != 0)
2127 goto error;
2128 DISPATCH();
2129 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002130
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002131 TARGET(DELETE_GLOBAL) {
2132 PyObject *name = GETITEM(names, oparg);
2133 int err;
2134 err = PyDict_DelItem(f->f_globals, name);
2135 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002136 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002137 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002138 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002139 }
2140 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002141 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002142
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002143 TARGET(LOAD_NAME) {
2144 PyObject *name = GETITEM(names, oparg);
2145 PyObject *locals = f->f_locals;
2146 PyObject *v;
2147 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002149 "no locals when loading %R", name);
2150 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002151 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002152 if (PyDict_CheckExact(locals)) {
2153 v = PyDict_GetItem(locals, name);
2154 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155 }
2156 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002157 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002158 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002159 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2160 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002161 PyErr_Clear();
2162 }
2163 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002164 if (v == NULL) {
2165 v = PyDict_GetItem(f->f_globals, name);
2166 Py_XINCREF(v);
2167 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002168 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002169 v = PyDict_GetItem(f->f_builtins, name);
2170 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002171 format_exc_check_arg(
2172 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002173 NAME_ERROR_MSG, name);
2174 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002175 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002176 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002177 }
2178 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002179 v = PyObject_GetItem(f->f_builtins, name);
2180 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002181 if (PyErr_ExceptionMatches(PyExc_KeyError))
2182 format_exc_check_arg(
2183 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002184 NAME_ERROR_MSG, name);
2185 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002186 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002187 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002188 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002189 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002190 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002192 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002193
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002194 TARGET(LOAD_GLOBAL) {
2195 PyObject *name = GETITEM(names, oparg);
2196 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002197 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002198 && PyDict_CheckExact(f->f_builtins))
2199 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002200 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002201 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002202 name);
2203 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002204 if (!_PyErr_OCCURRED()) {
2205 /* _PyDict_LoadGlobal() returns NULL without raising
2206 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002207 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002208 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002209 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002210 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002212 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002214 else {
2215 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002216
2217 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002218 v = PyObject_GetItem(f->f_globals, name);
2219 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002220 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2221 goto error;
2222 PyErr_Clear();
2223
Victor Stinnerb4efc962015-11-20 09:24:02 +01002224 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002225 v = PyObject_GetItem(f->f_builtins, name);
2226 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002227 if (PyErr_ExceptionMatches(PyExc_KeyError))
2228 format_exc_check_arg(
2229 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002230 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002231 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002232 }
2233 }
2234 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002235 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002237 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002238
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002239 TARGET(DELETE_FAST) {
2240 PyObject *v = GETLOCAL(oparg);
2241 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002242 SETLOCAL(oparg, NULL);
2243 DISPATCH();
2244 }
2245 format_exc_check_arg(
2246 PyExc_UnboundLocalError,
2247 UNBOUNDLOCAL_ERROR_MSG,
2248 PyTuple_GetItem(co->co_varnames, oparg)
2249 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002250 goto error;
2251 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002252
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002253 TARGET(DELETE_DEREF) {
2254 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002255 PyObject *oldobj = PyCell_GET(cell);
2256 if (oldobj != NULL) {
2257 PyCell_SET(cell, NULL);
2258 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002259 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002260 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002261 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002262 goto error;
2263 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002264
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002265 TARGET(LOAD_CLOSURE) {
2266 PyObject *cell = freevars[oparg];
2267 Py_INCREF(cell);
2268 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002269 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002270 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002271
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002272 TARGET(LOAD_CLASSDEREF) {
2273 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002274 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002275 assert(locals);
2276 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2277 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2278 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2279 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2280 if (PyDict_CheckExact(locals)) {
2281 value = PyDict_GetItem(locals, name);
2282 Py_XINCREF(value);
2283 }
2284 else {
2285 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002286 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002287 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2288 goto error;
2289 PyErr_Clear();
2290 }
2291 }
2292 if (!value) {
2293 PyObject *cell = freevars[oparg];
2294 value = PyCell_GET(cell);
2295 if (value == NULL) {
2296 format_exc_unbound(co, oparg);
2297 goto error;
2298 }
2299 Py_INCREF(value);
2300 }
2301 PUSH(value);
2302 DISPATCH();
2303 }
2304
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002305 TARGET(LOAD_DEREF) {
2306 PyObject *cell = freevars[oparg];
2307 PyObject *value = PyCell_GET(cell);
2308 if (value == NULL) {
2309 format_exc_unbound(co, oparg);
2310 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002312 Py_INCREF(value);
2313 PUSH(value);
2314 DISPATCH();
2315 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002316
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002317 TARGET(STORE_DEREF) {
2318 PyObject *v = POP();
2319 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002320 PyObject *oldobj = PyCell_GET(cell);
2321 PyCell_SET(cell, v);
2322 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002323 DISPATCH();
2324 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002325
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002326 TARGET(BUILD_STRING) {
2327 PyObject *str;
2328 PyObject *empty = PyUnicode_New(0, 0);
2329 if (empty == NULL) {
2330 goto error;
2331 }
2332 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2333 Py_DECREF(empty);
2334 if (str == NULL)
2335 goto error;
2336 while (--oparg >= 0) {
2337 PyObject *item = POP();
2338 Py_DECREF(item);
2339 }
2340 PUSH(str);
2341 DISPATCH();
2342 }
2343
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002344 TARGET(BUILD_TUPLE) {
2345 PyObject *tup = PyTuple_New(oparg);
2346 if (tup == NULL)
2347 goto error;
2348 while (--oparg >= 0) {
2349 PyObject *item = POP();
2350 PyTuple_SET_ITEM(tup, oparg, item);
2351 }
2352 PUSH(tup);
2353 DISPATCH();
2354 }
2355
2356 TARGET(BUILD_LIST) {
2357 PyObject *list = PyList_New(oparg);
2358 if (list == NULL)
2359 goto error;
2360 while (--oparg >= 0) {
2361 PyObject *item = POP();
2362 PyList_SET_ITEM(list, oparg, item);
2363 }
2364 PUSH(list);
2365 DISPATCH();
2366 }
2367
Serhiy Storchaka73442852016-10-02 10:33:46 +03002368 TARGET(BUILD_TUPLE_UNPACK_WITH_CALL)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002369 TARGET(BUILD_TUPLE_UNPACK)
2370 TARGET(BUILD_LIST_UNPACK) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002371 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002372 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002373 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002374 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002375
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002376 if (sum == NULL)
2377 goto error;
2378
2379 for (i = oparg; i > 0; i--) {
2380 PyObject *none_val;
2381
2382 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2383 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002384 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002385 PyErr_ExceptionMatches(PyExc_TypeError))
2386 {
2387 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002388 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002389 Py_DECREF(sum);
2390 goto error;
2391 }
2392 Py_DECREF(none_val);
2393 }
2394
2395 if (convert_to_tuple) {
2396 return_value = PyList_AsTuple(sum);
2397 Py_DECREF(sum);
2398 if (return_value == NULL)
2399 goto error;
2400 }
2401 else {
2402 return_value = sum;
2403 }
2404
2405 while (oparg--)
2406 Py_DECREF(POP());
2407 PUSH(return_value);
2408 DISPATCH();
2409 }
2410
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002411 TARGET(BUILD_SET) {
2412 PyObject *set = PySet_New(NULL);
2413 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002414 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002415 if (set == NULL)
2416 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002417 for (i = oparg; i > 0; i--) {
2418 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002419 if (err == 0)
2420 err = PySet_Add(set, item);
2421 Py_DECREF(item);
2422 }
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002423 STACKADJ(-oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002424 if (err != 0) {
2425 Py_DECREF(set);
2426 goto error;
2427 }
2428 PUSH(set);
2429 DISPATCH();
2430 }
2431
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002432 TARGET(BUILD_SET_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002433 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002434 PyObject *sum = PySet_New(NULL);
2435 if (sum == NULL)
2436 goto error;
2437
2438 for (i = oparg; i > 0; i--) {
2439 if (_PySet_Update(sum, PEEK(i)) < 0) {
2440 Py_DECREF(sum);
2441 goto error;
2442 }
2443 }
2444
2445 while (oparg--)
2446 Py_DECREF(POP());
2447 PUSH(sum);
2448 DISPATCH();
2449 }
2450
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002451 TARGET(BUILD_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002452 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002453 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2454 if (map == NULL)
2455 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002456 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002457 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002458 PyObject *key = PEEK(2*i);
2459 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002460 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002461 if (err != 0) {
2462 Py_DECREF(map);
2463 goto error;
2464 }
2465 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002466
2467 while (oparg--) {
2468 Py_DECREF(POP());
2469 Py_DECREF(POP());
2470 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002471 PUSH(map);
2472 DISPATCH();
2473 }
2474
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002475 TARGET(SETUP_ANNOTATIONS) {
2476 _Py_IDENTIFIER(__annotations__);
2477 int err;
2478 PyObject *ann_dict;
2479 if (f->f_locals == NULL) {
2480 PyErr_Format(PyExc_SystemError,
2481 "no locals found when setting up annotations");
2482 goto error;
2483 }
2484 /* check if __annotations__ in locals()... */
2485 if (PyDict_CheckExact(f->f_locals)) {
2486 ann_dict = _PyDict_GetItemId(f->f_locals,
2487 &PyId___annotations__);
2488 if (ann_dict == NULL) {
2489 /* ...if not, create a new one */
2490 ann_dict = PyDict_New();
2491 if (ann_dict == NULL) {
2492 goto error;
2493 }
2494 err = _PyDict_SetItemId(f->f_locals,
2495 &PyId___annotations__, ann_dict);
2496 Py_DECREF(ann_dict);
2497 if (err != 0) {
2498 goto error;
2499 }
2500 }
2501 }
2502 else {
2503 /* do the same if locals() is not a dict */
2504 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2505 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002506 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002507 }
2508 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2509 if (ann_dict == NULL) {
2510 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2511 goto error;
2512 }
2513 PyErr_Clear();
2514 ann_dict = PyDict_New();
2515 if (ann_dict == NULL) {
2516 goto error;
2517 }
2518 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2519 Py_DECREF(ann_dict);
2520 if (err != 0) {
2521 goto error;
2522 }
2523 }
2524 else {
2525 Py_DECREF(ann_dict);
2526 }
2527 }
2528 DISPATCH();
2529 }
2530
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002531 TARGET(BUILD_CONST_KEY_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002532 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002533 PyObject *map;
2534 PyObject *keys = TOP();
2535 if (!PyTuple_CheckExact(keys) ||
2536 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2537 PyErr_SetString(PyExc_SystemError,
2538 "bad BUILD_CONST_KEY_MAP keys argument");
2539 goto error;
2540 }
2541 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2542 if (map == NULL) {
2543 goto error;
2544 }
2545 for (i = oparg; i > 0; i--) {
2546 int err;
2547 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2548 PyObject *value = PEEK(i + 1);
2549 err = PyDict_SetItem(map, key, value);
2550 if (err != 0) {
2551 Py_DECREF(map);
2552 goto error;
2553 }
2554 }
2555
2556 Py_DECREF(POP());
2557 while (oparg--) {
2558 Py_DECREF(POP());
2559 }
2560 PUSH(map);
2561 DISPATCH();
2562 }
2563
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002564 TARGET(BUILD_MAP_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002565 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002566 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002567 if (sum == NULL)
2568 goto error;
2569
2570 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002571 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002572 if (PyDict_Update(sum, arg) < 0) {
2573 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2574 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002575 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002576 arg->ob_type->tp_name);
2577 }
2578 Py_DECREF(sum);
2579 goto error;
2580 }
2581 }
2582
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002583 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002584 Py_DECREF(POP());
2585 PUSH(sum);
2586 DISPATCH();
2587 }
2588
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002589 TARGET(BUILD_MAP_UNPACK_WITH_CALL) {
2590 Py_ssize_t i;
2591 PyObject *sum = PyDict_New();
2592 if (sum == NULL)
2593 goto error;
2594
2595 for (i = oparg; i > 0; i--) {
2596 PyObject *arg = PEEK(i);
2597 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
2598 PyObject *func = PEEK(2 + oparg);
2599 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002600 format_kwargs_mapping_error(func, arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002601 }
2602 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2603 PyObject *exc, *val, *tb;
2604 PyErr_Fetch(&exc, &val, &tb);
2605 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
2606 PyObject *key = PyTuple_GET_ITEM(val, 0);
2607 if (!PyUnicode_Check(key)) {
2608 PyErr_Format(PyExc_TypeError,
2609 "%.200s%.200s keywords must be strings",
2610 PyEval_GetFuncName(func),
2611 PyEval_GetFuncDesc(func));
2612 } else {
2613 PyErr_Format(PyExc_TypeError,
2614 "%.200s%.200s got multiple "
2615 "values for keyword argument '%U'",
2616 PyEval_GetFuncName(func),
2617 PyEval_GetFuncDesc(func),
2618 key);
2619 }
2620 Py_XDECREF(exc);
2621 Py_XDECREF(val);
2622 Py_XDECREF(tb);
2623 }
2624 else {
2625 PyErr_Restore(exc, val, tb);
2626 }
2627 }
2628 Py_DECREF(sum);
2629 goto error;
2630 }
2631 }
2632
2633 while (oparg--)
2634 Py_DECREF(POP());
2635 PUSH(sum);
2636 DISPATCH();
2637 }
2638
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002639 TARGET(MAP_ADD) {
2640 PyObject *key = TOP();
2641 PyObject *value = SECOND();
2642 PyObject *map;
2643 int err;
2644 STACKADJ(-2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002645 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002646 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002647 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002648 Py_DECREF(value);
2649 Py_DECREF(key);
2650 if (err != 0)
2651 goto error;
2652 PREDICT(JUMP_ABSOLUTE);
2653 DISPATCH();
2654 }
2655
2656 TARGET(LOAD_ATTR) {
2657 PyObject *name = GETITEM(names, oparg);
2658 PyObject *owner = TOP();
2659 PyObject *res = PyObject_GetAttr(owner, name);
2660 Py_DECREF(owner);
2661 SET_TOP(res);
2662 if (res == NULL)
2663 goto error;
2664 DISPATCH();
2665 }
2666
2667 TARGET(COMPARE_OP) {
2668 PyObject *right = POP();
2669 PyObject *left = TOP();
2670 PyObject *res = cmp_outcome(oparg, left, right);
2671 Py_DECREF(left);
2672 Py_DECREF(right);
2673 SET_TOP(res);
2674 if (res == NULL)
2675 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002676 PREDICT(POP_JUMP_IF_FALSE);
2677 PREDICT(POP_JUMP_IF_TRUE);
2678 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002679 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002680
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002681 TARGET(IMPORT_NAME) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002682 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002683 PyObject *fromlist = POP();
2684 PyObject *level = TOP();
2685 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002686 res = import_name(f, name, fromlist, level);
2687 Py_DECREF(level);
2688 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002689 SET_TOP(res);
2690 if (res == NULL)
2691 goto error;
2692 DISPATCH();
2693 }
2694
2695 TARGET(IMPORT_STAR) {
2696 PyObject *from = POP(), *locals;
2697 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002698 if (PyFrame_FastToLocalsWithError(f) < 0) {
2699 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002700 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002701 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002702
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002703 locals = f->f_locals;
2704 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002705 PyErr_SetString(PyExc_SystemError,
2706 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002707 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002708 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002709 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002710 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002711 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002712 Py_DECREF(from);
2713 if (err != 0)
2714 goto error;
2715 DISPATCH();
2716 }
Guido van Rossum25831651993-05-19 14:50:45 +00002717
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002718 TARGET(IMPORT_FROM) {
2719 PyObject *name = GETITEM(names, oparg);
2720 PyObject *from = TOP();
2721 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002722 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002723 PUSH(res);
2724 if (res == NULL)
2725 goto error;
2726 DISPATCH();
2727 }
Thomas Wouters52152252000-08-17 22:55:00 +00002728
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002729 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002730 JUMPBY(oparg);
2731 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002732 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002733
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002734 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002735 TARGET(POP_JUMP_IF_FALSE) {
2736 PyObject *cond = POP();
2737 int err;
2738 if (cond == Py_True) {
2739 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002740 FAST_DISPATCH();
2741 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002742 if (cond == Py_False) {
2743 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002744 JUMPTO(oparg);
2745 FAST_DISPATCH();
2746 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002747 err = PyObject_IsTrue(cond);
2748 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002749 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002750 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 else if (err == 0)
2752 JUMPTO(oparg);
2753 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002754 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002755 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002756 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002757
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002758 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002759 TARGET(POP_JUMP_IF_TRUE) {
2760 PyObject *cond = POP();
2761 int err;
2762 if (cond == Py_False) {
2763 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002764 FAST_DISPATCH();
2765 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002766 if (cond == Py_True) {
2767 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002768 JUMPTO(oparg);
2769 FAST_DISPATCH();
2770 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002771 err = PyObject_IsTrue(cond);
2772 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002773 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002774 JUMPTO(oparg);
2775 }
2776 else if (err == 0)
2777 ;
2778 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002779 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002780 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002781 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002782
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002783 TARGET(JUMP_IF_FALSE_OR_POP) {
2784 PyObject *cond = TOP();
2785 int err;
2786 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002787 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002788 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002789 FAST_DISPATCH();
2790 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002791 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002792 JUMPTO(oparg);
2793 FAST_DISPATCH();
2794 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002795 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002796 if (err > 0) {
2797 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002798 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002799 }
2800 else if (err == 0)
2801 JUMPTO(oparg);
2802 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002803 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002804 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002805 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002806
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002807 TARGET(JUMP_IF_TRUE_OR_POP) {
2808 PyObject *cond = TOP();
2809 int err;
2810 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002811 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002812 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002813 FAST_DISPATCH();
2814 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002815 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002816 JUMPTO(oparg);
2817 FAST_DISPATCH();
2818 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002819 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002820 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002821 JUMPTO(oparg);
2822 }
2823 else if (err == 0) {
2824 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002825 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002826 }
2827 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002828 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002829 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002830 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002831
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002832 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002833 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002834 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002835#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002836 /* Enabling this path speeds-up all while and for-loops by bypassing
2837 the per-loop checks for signals. By default, this should be turned-off
2838 because it prevents detection of a control-break in tight loops like
2839 "while 1: pass". Compile with this option turned-on when you need
2840 the speed-up and do not need break checking inside tight loops (ones
2841 that contain only instructions ending with FAST_DISPATCH).
2842 */
2843 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002844#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002845 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002846#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002847 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002848
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002849 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002850 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002851 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002852 PyObject *iter = PyObject_GetIter(iterable);
2853 Py_DECREF(iterable);
2854 SET_TOP(iter);
2855 if (iter == NULL)
2856 goto error;
2857 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002858 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002859 DISPATCH();
2860 }
2861
2862 TARGET(GET_YIELD_FROM_ITER) {
2863 /* before: [obj]; after [getiter(obj)] */
2864 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002865 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002866 if (PyCoro_CheckExact(iterable)) {
2867 /* `iterable` is a coroutine */
2868 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2869 /* and it is used in a 'yield from' expression of a
2870 regular generator. */
2871 Py_DECREF(iterable);
2872 SET_TOP(NULL);
2873 PyErr_SetString(PyExc_TypeError,
2874 "cannot 'yield from' a coroutine object "
2875 "in a non-coroutine generator");
2876 goto error;
2877 }
2878 }
2879 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002880 /* `iterable` is not a generator. */
2881 iter = PyObject_GetIter(iterable);
2882 Py_DECREF(iterable);
2883 SET_TOP(iter);
2884 if (iter == NULL)
2885 goto error;
2886 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002887 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002888 DISPATCH();
2889 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002890
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002891 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002892 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002893 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002894 PyObject *iter = TOP();
2895 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2896 if (next != NULL) {
2897 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002898 PREDICT(STORE_FAST);
2899 PREDICT(UNPACK_SEQUENCE);
2900 DISPATCH();
2901 }
2902 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002903 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2904 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08002905 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002906 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002907 PyErr_Clear();
2908 }
2909 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002910 STACKADJ(-1);
2911 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002912 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002913 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002914 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002915 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002916
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002917 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002918 /* NOTE: If you add any new block-setup opcodes that
2919 are not try/except/finally handlers, you may need
2920 to update the PyGen_NeedsFinalizing() function.
2921 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002922
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002923 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 STACK_LEVEL());
2925 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002926 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002927
Yury Selivanov75445082015-05-11 22:57:16 -04002928 TARGET(BEFORE_ASYNC_WITH) {
2929 _Py_IDENTIFIER(__aexit__);
2930 _Py_IDENTIFIER(__aenter__);
2931
2932 PyObject *mgr = TOP();
2933 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
2934 *enter;
2935 PyObject *res;
2936 if (exit == NULL)
2937 goto error;
2938 SET_TOP(exit);
2939 enter = special_lookup(mgr, &PyId___aenter__);
2940 Py_DECREF(mgr);
2941 if (enter == NULL)
2942 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002943 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04002944 Py_DECREF(enter);
2945 if (res == NULL)
2946 goto error;
2947 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002948 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002949 DISPATCH();
2950 }
2951
2952 TARGET(SETUP_ASYNC_WITH) {
2953 PyObject *res = POP();
2954 /* Setup the finally block before pushing the result
2955 of __aenter__ on the stack. */
2956 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2957 STACK_LEVEL());
2958 PUSH(res);
2959 DISPATCH();
2960 }
2961
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002962 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05002963 _Py_IDENTIFIER(__exit__);
2964 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002965 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08002966 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002967 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08002968 if (enter == NULL)
2969 goto error;
2970 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08002971 if (exit == NULL) {
2972 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002973 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08002974 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002975 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002976 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002977 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002978 Py_DECREF(enter);
2979 if (res == NULL)
2980 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002981 /* Setup the finally block before pushing the result
2982 of __enter__ on the stack. */
2983 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2984 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002985
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002986 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002987 DISPATCH();
2988 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002989
Yury Selivanov75445082015-05-11 22:57:16 -04002990 TARGET(WITH_CLEANUP_START) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002991 /* At the top of the stack are 1 or 6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002992 how/why we entered the finally clause:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002993 - TOP = NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002994 - (TOP, SECOND, THIRD) = exc_info()
2995 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002996 Below them is EXIT, the context.__exit__ or context.__aexit__
2997 bound method.
2998 In the first case, we must call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002999 EXIT(None, None, None)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003000 otherwise we must call
3001 EXIT(TOP, SECOND, THIRD)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003002
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003003 In the first case, we remove EXIT from the
3004 stack, leaving TOP, and push TOP on the stack.
3005 Otherwise we shift the bottom 3 values of the
3006 stack down, replace the empty spot with NULL, and push
3007 None on the stack.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003008
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003009 Finally we push the result of the call.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003010 */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003011 PyObject *stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003012 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003013 PyObject *exc, *val, *tb, *res;
3014
3015 val = tb = Py_None;
3016 exc = TOP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003017 if (exc == NULL) {
3018 STACKADJ(-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003019 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003020 SET_TOP(exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003021 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003022 }
3023 else {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003024 assert(PyExceptionClass_Check(exc));
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003025 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003026 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003027 val = SECOND();
3028 tb = THIRD();
3029 tp2 = FOURTH();
3030 exc2 = PEEK(5);
3031 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003032 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003033 SET_VALUE(7, tb2);
3034 SET_VALUE(6, exc2);
3035 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003036 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3037 SET_FOURTH(NULL);
3038 /* We just shifted the stack down, so we have
3039 to tell the except handler block that the
3040 values are lower than it expects. */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003041 assert(f->f_iblock > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003042 block = &f->f_blockstack[f->f_iblock - 1];
3043 assert(block->b_type == EXCEPT_HANDLER);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003044 assert(block->b_level > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003045 block->b_level--;
3046 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003047
3048 stack[0] = exc;
3049 stack[1] = val;
3050 stack[2] = tb;
3051 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003052 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003053 if (res == NULL)
3054 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003055
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003056 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003057 PUSH(exc);
3058 PUSH(res);
3059 PREDICT(WITH_CLEANUP_FINISH);
3060 DISPATCH();
3061 }
3062
3063 PREDICTED(WITH_CLEANUP_FINISH);
3064 TARGET(WITH_CLEANUP_FINISH) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003065 /* TOP = the result of calling the context.__exit__ bound method
3066 SECOND = either None or exception type
3067
3068 If SECOND is None below is NULL or the return address,
3069 otherwise below are 7 values representing an exception.
3070 */
Yury Selivanov75445082015-05-11 22:57:16 -04003071 PyObject *res = POP();
3072 PyObject *exc = POP();
3073 int err;
3074
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003075 if (exc != Py_None)
3076 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003077 else
3078 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003079
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003080 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003081 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003083 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003084 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003085 else if (err > 0) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003086 /* There was an exception and a True return.
3087 * We must manually unwind the EXCEPT_HANDLER block
3088 * which was created when the exception was caught,
3089 * otherwise the stack will be in an inconsisten state.
3090 */
3091 PyTryBlock *b = PyFrame_BlockPop(f);
3092 assert(b->b_type == EXCEPT_HANDLER);
3093 UNWIND_EXCEPT_HANDLER(b);
3094 PUSH(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003095 }
3096 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003097 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003098 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003099
Yury Selivanovf2392132016-12-13 19:03:51 -05003100 TARGET(LOAD_METHOD) {
3101 /* Designed to work in tamdem with CALL_METHOD. */
3102 PyObject *name = GETITEM(names, oparg);
3103 PyObject *obj = TOP();
3104 PyObject *meth = NULL;
3105
3106 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3107
Yury Selivanovf2392132016-12-13 19:03:51 -05003108 if (meth == NULL) {
3109 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003110 goto error;
3111 }
3112
3113 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003114 /* We can bypass temporary bound method object.
3115 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003116
INADA Naoki015bce62017-01-16 17:23:30 +09003117 meth | self | arg1 | ... | argN
3118 */
3119 SET_TOP(meth);
3120 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003121 }
3122 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003123 /* meth is not an unbound method (but a regular attr, or
3124 something was returned by a descriptor protocol). Set
3125 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003126 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003127
3128 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003129 */
INADA Naoki015bce62017-01-16 17:23:30 +09003130 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003131 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003132 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003133 }
3134 DISPATCH();
3135 }
3136
3137 TARGET(CALL_METHOD) {
3138 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003139 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003140
3141 sp = stack_pointer;
3142
INADA Naoki015bce62017-01-16 17:23:30 +09003143 meth = PEEK(oparg + 2);
3144 if (meth == NULL) {
3145 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3146 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003147
3148 Stack layout:
3149
INADA Naoki015bce62017-01-16 17:23:30 +09003150 ... | NULL | callable | arg1 | ... | argN
3151 ^- TOP()
3152 ^- (-oparg)
3153 ^- (-oparg-1)
3154 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003155
Ville Skyttä49b27342017-08-03 09:00:59 +03003156 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003157 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003158 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003159 res = call_function(&sp, oparg, NULL);
3160 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003161 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003162 }
3163 else {
3164 /* This is a method call. Stack layout:
3165
INADA Naoki015bce62017-01-16 17:23:30 +09003166 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003167 ^- TOP()
3168 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003169 ^- (-oparg-1)
3170 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003171
INADA Naoki015bce62017-01-16 17:23:30 +09003172 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003173 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003174 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003175 */
3176 res = call_function(&sp, oparg + 1, NULL);
3177 stack_pointer = sp;
3178 }
3179
3180 PUSH(res);
3181 if (res == NULL)
3182 goto error;
3183 DISPATCH();
3184 }
3185
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003186 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003187 TARGET(CALL_FUNCTION) {
3188 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003189 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003190 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003191 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003192 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003193 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003194 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003195 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003196 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003197 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003198
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003199 TARGET(CALL_FUNCTION_KW) {
3200 PyObject **sp, *res, *names;
3201
3202 names = POP();
3203 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003204 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003205 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003206 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003207 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003208 Py_DECREF(names);
3209
3210 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003211 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003212 }
3213 DISPATCH();
3214 }
3215
3216 TARGET(CALL_FUNCTION_EX) {
3217 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003218 if (oparg & 0x01) {
3219 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003220 if (!PyDict_CheckExact(kwargs)) {
3221 PyObject *d = PyDict_New();
3222 if (d == NULL)
3223 goto error;
3224 if (PyDict_Update(d, kwargs) != 0) {
3225 Py_DECREF(d);
3226 /* PyDict_Update raises attribute
3227 * error (percolated from an attempt
3228 * to get 'keys' attribute) instead of
3229 * a type error if its second argument
3230 * is not a mapping.
3231 */
3232 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003233 format_kwargs_mapping_error(SECOND(), kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003234 }
Victor Stinnereece2222016-09-12 11:16:37 +02003235 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003236 goto error;
3237 }
3238 Py_DECREF(kwargs);
3239 kwargs = d;
3240 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003241 assert(PyDict_CheckExact(kwargs));
3242 }
3243 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003244 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003245 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003246 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003247 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003248 goto error;
3249 }
3250 Py_SETREF(callargs, PySequence_Tuple(callargs));
3251 if (callargs == NULL) {
3252 goto error;
3253 }
3254 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003255 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003256
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003257 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003258 Py_DECREF(func);
3259 Py_DECREF(callargs);
3260 Py_XDECREF(kwargs);
3261
3262 SET_TOP(result);
3263 if (result == NULL) {
3264 goto error;
3265 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003266 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003267 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003268
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003269 TARGET(MAKE_FUNCTION) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003270 PyObject *qualname = POP();
3271 PyObject *codeobj = POP();
3272 PyFunctionObject *func = (PyFunctionObject *)
3273 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003274
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003275 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003276 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003277 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003278 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003279 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003280
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003281 if (oparg & 0x08) {
3282 assert(PyTuple_CheckExact(TOP()));
3283 func ->func_closure = POP();
3284 }
3285 if (oparg & 0x04) {
3286 assert(PyDict_CheckExact(TOP()));
3287 func->func_annotations = POP();
3288 }
3289 if (oparg & 0x02) {
3290 assert(PyDict_CheckExact(TOP()));
3291 func->func_kwdefaults = POP();
3292 }
3293 if (oparg & 0x01) {
3294 assert(PyTuple_CheckExact(TOP()));
3295 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003296 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003297
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003298 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003299 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003300 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003301
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003302 TARGET(BUILD_SLICE) {
3303 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003304 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003305 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003306 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003307 step = NULL;
3308 stop = POP();
3309 start = TOP();
3310 slice = PySlice_New(start, stop, step);
3311 Py_DECREF(start);
3312 Py_DECREF(stop);
3313 Py_XDECREF(step);
3314 SET_TOP(slice);
3315 if (slice == NULL)
3316 goto error;
3317 DISPATCH();
3318 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003319
Eric V. Smitha78c7952015-11-03 12:45:05 -05003320 TARGET(FORMAT_VALUE) {
3321 /* Handles f-string value formatting. */
3322 PyObject *result;
3323 PyObject *fmt_spec;
3324 PyObject *value;
3325 PyObject *(*conv_fn)(PyObject *);
3326 int which_conversion = oparg & FVC_MASK;
3327 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3328
3329 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003330 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003331
3332 /* See if any conversion is specified. */
3333 switch (which_conversion) {
3334 case FVC_STR: conv_fn = PyObject_Str; break;
3335 case FVC_REPR: conv_fn = PyObject_Repr; break;
3336 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3337
3338 /* Must be 0 (meaning no conversion), since only four
3339 values are allowed by (oparg & FVC_MASK). */
3340 default: conv_fn = NULL; break;
3341 }
3342
3343 /* If there's a conversion function, call it and replace
3344 value with that result. Otherwise, just use value,
3345 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003346 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003347 result = conv_fn(value);
3348 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003349 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003350 Py_XDECREF(fmt_spec);
3351 goto error;
3352 }
3353 value = result;
3354 }
3355
3356 /* If value is a unicode object, and there's no fmt_spec,
3357 then we know the result of format(value) is value
3358 itself. In that case, skip calling format(). I plan to
3359 move this optimization in to PyObject_Format()
3360 itself. */
3361 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3362 /* Do nothing, just transfer ownership to result. */
3363 result = value;
3364 } else {
3365 /* Actually call format(). */
3366 result = PyObject_Format(value, fmt_spec);
3367 Py_DECREF(value);
3368 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003369 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003370 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003371 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003372 }
3373
Eric V. Smith135d5f42016-02-05 18:23:08 -05003374 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003375 DISPATCH();
3376 }
3377
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003378 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003379 int oldoparg = oparg;
3380 NEXTOPARG();
3381 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003382 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003383 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003384
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003385
Antoine Pitrou042b1282010-08-13 21:15:58 +00003386#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003387 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003388#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003389 default:
3390 fprintf(stderr,
3391 "XXX lineno: %d, opcode: %d\n",
3392 PyFrame_GetLineNumber(f),
3393 opcode);
3394 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003395 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003397 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003398
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003399 /* This should never be reached. Every opcode should end with DISPATCH()
3400 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003401 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003402
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003403error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003404 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003405#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003406 if (!PyErr_Occurred())
3407 PyErr_SetString(PyExc_SystemError,
3408 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003409#else
3410 assert(PyErr_Occurred());
3411#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003412
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003413 /* Log traceback info. */
3414 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003415
Benjamin Peterson51f46162013-01-23 08:38:47 -05003416 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003417 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3418 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003419
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003420exception_unwind:
3421 /* Unwind stacks if an exception occurred */
3422 while (f->f_iblock > 0) {
3423 /* Pop the current block. */
3424 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003426 if (b->b_type == EXCEPT_HANDLER) {
3427 UNWIND_EXCEPT_HANDLER(b);
3428 continue;
3429 }
3430 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003431 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003432 PyObject *exc, *val, *tb;
3433 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003434 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003435 /* Beware, this invalidates all b->b_* fields */
3436 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003437 PUSH(exc_info->exc_traceback);
3438 PUSH(exc_info->exc_value);
3439 if (exc_info->exc_type != NULL) {
3440 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003441 }
3442 else {
3443 Py_INCREF(Py_None);
3444 PUSH(Py_None);
3445 }
3446 PyErr_Fetch(&exc, &val, &tb);
3447 /* Make the raw exception data
3448 available to the handler,
3449 so a program can emulate the
3450 Python main loop. */
3451 PyErr_NormalizeException(
3452 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003453 if (tb != NULL)
3454 PyException_SetTraceback(val, tb);
3455 else
3456 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003457 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003458 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003459 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003460 exc_info->exc_value = val;
3461 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003462 if (tb == NULL)
3463 tb = Py_None;
3464 Py_INCREF(tb);
3465 PUSH(tb);
3466 PUSH(val);
3467 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003468 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003469 /* Resume normal execution */
3470 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003471 }
3472 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003473
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003474 /* End the loop as we still have an error */
3475 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003476 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003478 /* Pop remaining stack entries. */
3479 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003480 PyObject *o = POP();
3481 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003482 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003483
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003484 assert(retval == NULL);
3485 assert(PyErr_Occurred());
Guido van Rossumac7be682001-01-17 15:42:30 +00003486
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003487return_or_yield:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003488 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003489 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003490 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3491 tstate, f, PyTrace_RETURN, retval)) {
3492 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003493 }
3494 }
3495 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003496 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3497 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003498 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003499 }
3500 }
3501 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003503 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003504exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003505 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3506 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003507 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003508 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003509 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003510
Victor Stinnerefde1462015-03-21 15:04:43 +01003511 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003512}
3513
Benjamin Petersonb204a422011-06-05 22:04:07 -05003514static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003515format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3516{
3517 int err;
3518 Py_ssize_t len = PyList_GET_SIZE(names);
3519 PyObject *name_str, *comma, *tail, *tmp;
3520
3521 assert(PyList_CheckExact(names));
3522 assert(len >= 1);
3523 /* Deal with the joys of natural language. */
3524 switch (len) {
3525 case 1:
3526 name_str = PyList_GET_ITEM(names, 0);
3527 Py_INCREF(name_str);
3528 break;
3529 case 2:
3530 name_str = PyUnicode_FromFormat("%U and %U",
3531 PyList_GET_ITEM(names, len - 2),
3532 PyList_GET_ITEM(names, len - 1));
3533 break;
3534 default:
3535 tail = PyUnicode_FromFormat(", %U, and %U",
3536 PyList_GET_ITEM(names, len - 2),
3537 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003538 if (tail == NULL)
3539 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003540 /* Chop off the last two objects in the list. This shouldn't actually
3541 fail, but we can't be too careful. */
3542 err = PyList_SetSlice(names, len - 2, len, NULL);
3543 if (err == -1) {
3544 Py_DECREF(tail);
3545 return;
3546 }
3547 /* Stitch everything up into a nice comma-separated list. */
3548 comma = PyUnicode_FromString(", ");
3549 if (comma == NULL) {
3550 Py_DECREF(tail);
3551 return;
3552 }
3553 tmp = PyUnicode_Join(comma, names);
3554 Py_DECREF(comma);
3555 if (tmp == NULL) {
3556 Py_DECREF(tail);
3557 return;
3558 }
3559 name_str = PyUnicode_Concat(tmp, tail);
3560 Py_DECREF(tmp);
3561 Py_DECREF(tail);
3562 break;
3563 }
3564 if (name_str == NULL)
3565 return;
3566 PyErr_Format(PyExc_TypeError,
3567 "%U() missing %i required %s argument%s: %U",
3568 co->co_name,
3569 len,
3570 kind,
3571 len == 1 ? "" : "s",
3572 name_str);
3573 Py_DECREF(name_str);
3574}
3575
3576static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003577missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003578 PyObject **fastlocals)
3579{
Victor Stinner74319ae2016-08-25 00:04:09 +02003580 Py_ssize_t i, j = 0;
3581 Py_ssize_t start, end;
3582 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003583 const char *kind = positional ? "positional" : "keyword-only";
3584 PyObject *missing_names;
3585
3586 /* Compute the names of the arguments that are missing. */
3587 missing_names = PyList_New(missing);
3588 if (missing_names == NULL)
3589 return;
3590 if (positional) {
3591 start = 0;
3592 end = co->co_argcount - defcount;
3593 }
3594 else {
3595 start = co->co_argcount;
3596 end = start + co->co_kwonlyargcount;
3597 }
3598 for (i = start; i < end; i++) {
3599 if (GETLOCAL(i) == NULL) {
3600 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3601 PyObject *name = PyObject_Repr(raw);
3602 if (name == NULL) {
3603 Py_DECREF(missing_names);
3604 return;
3605 }
3606 PyList_SET_ITEM(missing_names, j++, name);
3607 }
3608 }
3609 assert(j == missing);
3610 format_missing(kind, co, missing_names);
3611 Py_DECREF(missing_names);
3612}
3613
3614static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003615too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3616 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003617{
3618 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003619 Py_ssize_t kwonly_given = 0;
3620 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003621 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003622 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003623
Benjamin Petersone109c702011-06-24 09:37:26 -05003624 assert((co->co_flags & CO_VARARGS) == 0);
3625 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003626 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3627 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003628 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003629 }
3630 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003631 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003632 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003633 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003634 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003635 }
3636 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003637 plural = (co_argcount != 1);
3638 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003639 }
3640 if (sig == NULL)
3641 return;
3642 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003643 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3644 kwonly_sig = PyUnicode_FromFormat(format,
3645 given != 1 ? "s" : "",
3646 kwonly_given,
3647 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003648 if (kwonly_sig == NULL) {
3649 Py_DECREF(sig);
3650 return;
3651 }
3652 }
3653 else {
3654 /* This will not fail. */
3655 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003656 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003657 }
3658 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003659 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003660 co->co_name,
3661 sig,
3662 plural ? "s" : "",
3663 given,
3664 kwonly_sig,
3665 given == 1 && !kwonly_given ? "was" : "were");
3666 Py_DECREF(sig);
3667 Py_DECREF(kwonly_sig);
3668}
3669
Guido van Rossumc2e20742006-02-27 22:32:47 +00003670/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003671 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003672 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003673
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003674PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003675_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003676 PyObject *const *args, Py_ssize_t argcount,
3677 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003678 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003679 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003680 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003681 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003682{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003683 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003684 PyFrameObject *f;
3685 PyObject *retval = NULL;
3686 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003687 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003688 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003689 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3690 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003691 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003693 if (globals == NULL) {
3694 PyErr_SetString(PyExc_SystemError,
3695 "PyEval_EvalCodeEx: NULL globals");
3696 return NULL;
3697 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003698
Victor Stinnerc7020012016-08-16 23:40:29 +02003699 /* Create the frame */
3700 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003701 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003702 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003703 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003704 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003705 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003706 fastlocals = f->f_localsplus;
3707 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003708
Victor Stinnerc7020012016-08-16 23:40:29 +02003709 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003710 if (co->co_flags & CO_VARKEYWORDS) {
3711 kwdict = PyDict_New();
3712 if (kwdict == NULL)
3713 goto fail;
3714 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003715 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003716 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003717 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003718 SETLOCAL(i, kwdict);
3719 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003720 else {
3721 kwdict = NULL;
3722 }
3723
3724 /* Copy positional arguments into local variables */
3725 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003726 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003727 }
3728 else {
3729 n = argcount;
3730 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003731 for (i = 0; i < n; i++) {
3732 x = args[i];
3733 Py_INCREF(x);
3734 SETLOCAL(i, x);
3735 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003736
3737 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003738 if (co->co_flags & CO_VARARGS) {
3739 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003740 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003741 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003742 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003743 SETLOCAL(total_args, u);
3744 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003745 x = args[i];
3746 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003747 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003748 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003749 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003750
Serhiy Storchakab7281052016-09-12 00:52:40 +03003751 /* Handle keyword arguments passed as two strided arrays */
3752 kwcount *= kwstep;
3753 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003754 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003755 PyObject *keyword = kwnames[i];
3756 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003757 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003758
Benjamin Petersonb204a422011-06-05 22:04:07 -05003759 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3760 PyErr_Format(PyExc_TypeError,
3761 "%U() keywords must be strings",
3762 co->co_name);
3763 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003764 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003765
Benjamin Petersonb204a422011-06-05 22:04:07 -05003766 /* Speed hack: do raw pointer compares. As names are
3767 normally interned this should almost always hit. */
3768 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3769 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003770 PyObject *name = co_varnames[j];
3771 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003772 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003773 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003774 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003775
Benjamin Petersonb204a422011-06-05 22:04:07 -05003776 /* Slow fallback, just in case */
3777 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003778 PyObject *name = co_varnames[j];
3779 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3780 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003781 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003782 }
3783 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003784 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003785 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003786 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003787
Victor Stinner231d1f32017-01-11 02:12:06 +01003788 assert(j >= total_args);
3789 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003790 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003791 "%U() got an unexpected keyword argument '%S'",
3792 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003793 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003794 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003795
Christian Heimes0bd447f2013-07-20 14:48:10 +02003796 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3797 goto fail;
3798 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003799 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003800
Benjamin Petersonb204a422011-06-05 22:04:07 -05003801 kw_found:
3802 if (GETLOCAL(j) != NULL) {
3803 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003804 "%U() got multiple values for argument '%S'",
3805 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003806 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003807 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003808 Py_INCREF(value);
3809 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003810 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003811
3812 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003813 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003814 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003815 goto fail;
3816 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003817
3818 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003819 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02003820 Py_ssize_t m = co->co_argcount - defcount;
3821 Py_ssize_t missing = 0;
3822 for (i = argcount; i < m; i++) {
3823 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003824 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02003825 }
3826 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003827 if (missing) {
3828 missing_arguments(co, missing, defcount, fastlocals);
3829 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003830 }
3831 if (n > m)
3832 i = n - m;
3833 else
3834 i = 0;
3835 for (; i < defcount; i++) {
3836 if (GETLOCAL(m+i) == NULL) {
3837 PyObject *def = defs[i];
3838 Py_INCREF(def);
3839 SETLOCAL(m+i, def);
3840 }
3841 }
3842 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003843
3844 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003845 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02003846 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003847 for (i = co->co_argcount; i < total_args; i++) {
3848 PyObject *name;
3849 if (GETLOCAL(i) != NULL)
3850 continue;
3851 name = PyTuple_GET_ITEM(co->co_varnames, i);
3852 if (kwdefs != NULL) {
3853 PyObject *def = PyDict_GetItem(kwdefs, name);
3854 if (def) {
3855 Py_INCREF(def);
3856 SETLOCAL(i, def);
3857 continue;
3858 }
3859 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003860 missing++;
3861 }
3862 if (missing) {
3863 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003864 goto fail;
3865 }
3866 }
3867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003868 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003869 vars into frame. */
3870 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003871 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02003872 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05003873 /* Possibly account for the cell variable being an argument. */
3874 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003875 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003876 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003877 /* Clear the local copy. */
3878 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003879 }
3880 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003881 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003882 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003883 if (c == NULL)
3884 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003885 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003886 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003887
3888 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05003889 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3890 PyObject *o = PyTuple_GET_ITEM(closure, i);
3891 Py_INCREF(o);
3892 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003893 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003894
Yury Selivanoveb636452016-09-08 22:01:51 -07003895 /* Handle generator/coroutine/asynchronous generator */
3896 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003897 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04003898 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003899 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04003900
3901 if (is_coro && tstate->in_coroutine_wrapper) {
3902 assert(coro_wrapper != NULL);
3903 PyErr_Format(PyExc_RuntimeError,
3904 "coroutine wrapper %.200R attempted "
3905 "to recursively wrap %.200R",
3906 coro_wrapper,
3907 co);
3908 goto fail;
3909 }
Yury Selivanov75445082015-05-11 22:57:16 -04003910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003911 /* Don't need to keep the reference to f_back, it will be set
3912 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003913 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003915 /* Create a new generator that owns the ready to run frame
3916 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04003917 if (is_coro) {
3918 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07003919 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
3920 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003921 } else {
3922 gen = PyGen_NewWithQualName(f, name, qualname);
3923 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003924 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003925 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003926 }
INADA Naoki9c157762016-12-26 18:52:46 +09003927
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003928 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04003929
Yury Selivanov94c22632015-06-04 10:16:51 -04003930 if (is_coro && coro_wrapper != NULL) {
3931 PyObject *wrapped;
3932 tstate->in_coroutine_wrapper = 1;
3933 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
3934 tstate->in_coroutine_wrapper = 0;
3935 return wrapped;
3936 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04003937
Yury Selivanov75445082015-05-11 22:57:16 -04003938 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003939 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003940
Victor Stinner59a73272016-12-09 18:51:13 +01003941 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003942
Thomas Woutersce272b62007-09-19 21:19:28 +00003943fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003945 /* decref'ing the frame can cause __del__ methods to get invoked,
3946 which can call back into Python. While we're done with the
3947 current Python frame (f), the associated C stack is still in use,
3948 so recursion_depth must be boosted for the duration.
3949 */
3950 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003951 if (Py_REFCNT(f) > 1) {
3952 Py_DECREF(f);
3953 _PyObject_GC_TRACK(f);
3954 }
3955 else {
3956 ++tstate->recursion_depth;
3957 Py_DECREF(f);
3958 --tstate->recursion_depth;
3959 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003960 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003961}
3962
Victor Stinner40ee3012014-06-16 15:59:28 +02003963PyObject *
3964PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003965 PyObject *const *args, int argcount,
3966 PyObject *const *kws, int kwcount,
3967 PyObject *const *defs, int defcount,
3968 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02003969{
3970 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02003971 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06003972 kws, kws != NULL ? kws + 1 : NULL,
3973 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02003974 defs, defcount,
3975 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003976 NULL, NULL);
3977}
Tim Peters5ca576e2001-06-18 22:08:13 +00003978
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003979static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05003980special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003981{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003982 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05003983 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003984 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003985 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003986 return NULL;
3987 }
3988 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003989}
3990
3991
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003992/* Logic for the raise statement (too complicated for inlining).
3993 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003994static int
Collin Winter828f04a2007-08-31 00:04:24 +00003995do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003996{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003997 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003999 if (exc == NULL) {
4000 /* Reraise */
4001 PyThreadState *tstate = PyThreadState_GET();
Mark Shannonae3087c2017-10-22 22:41:51 +01004002 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004003 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004004 type = exc_info->exc_type;
4005 value = exc_info->exc_value;
4006 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004007 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004008 PyErr_SetString(PyExc_RuntimeError,
4009 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004010 return 0;
4011 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004012 Py_XINCREF(type);
4013 Py_XINCREF(value);
4014 Py_XINCREF(tb);
4015 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004016 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004017 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004019 /* We support the following forms of raise:
4020 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004021 raise <instance>
4022 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004024 if (PyExceptionClass_Check(exc)) {
4025 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004026 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004027 if (value == NULL)
4028 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004029 if (!PyExceptionInstance_Check(value)) {
4030 PyErr_Format(PyExc_TypeError,
4031 "calling %R should have returned an instance of "
4032 "BaseException, not %R",
4033 type, Py_TYPE(value));
4034 goto raise_error;
4035 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004036 }
4037 else if (PyExceptionInstance_Check(exc)) {
4038 value = exc;
4039 type = PyExceptionInstance_Class(exc);
4040 Py_INCREF(type);
4041 }
4042 else {
4043 /* Not something you can raise. You get an exception
4044 anyway, just not what you specified :-) */
4045 Py_DECREF(exc);
4046 PyErr_SetString(PyExc_TypeError,
4047 "exceptions must derive from BaseException");
4048 goto raise_error;
4049 }
Collin Winter828f04a2007-08-31 00:04:24 +00004050
Serhiy Storchakac0191582016-09-27 11:37:10 +03004051 assert(type != NULL);
4052 assert(value != NULL);
4053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004054 if (cause) {
4055 PyObject *fixed_cause;
4056 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004057 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004058 if (fixed_cause == NULL)
4059 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004060 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004061 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004062 else if (PyExceptionInstance_Check(cause)) {
4063 fixed_cause = cause;
4064 }
4065 else if (cause == Py_None) {
4066 Py_DECREF(cause);
4067 fixed_cause = NULL;
4068 }
4069 else {
4070 PyErr_SetString(PyExc_TypeError,
4071 "exception causes must derive from "
4072 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004073 goto raise_error;
4074 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004075 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004076 }
Collin Winter828f04a2007-08-31 00:04:24 +00004077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004078 PyErr_SetObject(type, value);
4079 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004080 Py_DECREF(value);
4081 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004082 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004083
4084raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004085 Py_XDECREF(value);
4086 Py_XDECREF(type);
4087 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004088 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004089}
4090
Tim Petersd6d010b2001-06-21 02:49:55 +00004091/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004092 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004093
Guido van Rossum0368b722007-05-11 16:50:42 +00004094 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4095 with a variable target.
4096*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004097
Barry Warsawe42b18f1997-08-25 22:13:04 +00004098static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004099unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004100{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004101 int i = 0, j = 0;
4102 Py_ssize_t ll = 0;
4103 PyObject *it; /* iter(v) */
4104 PyObject *w;
4105 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004107 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004109 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004110 if (it == NULL) {
4111 if (PyErr_ExceptionMatches(PyExc_TypeError) &&
4112 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4113 {
4114 PyErr_Format(PyExc_TypeError,
4115 "cannot unpack non-iterable %.200s object",
4116 v->ob_type->tp_name);
4117 }
4118 return 0;
4119 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004121 for (; i < argcnt; i++) {
4122 w = PyIter_Next(it);
4123 if (w == NULL) {
4124 /* Iterator done, via error or exhaustion. */
4125 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004126 if (argcntafter == -1) {
4127 PyErr_Format(PyExc_ValueError,
4128 "not enough values to unpack (expected %d, got %d)",
4129 argcnt, i);
4130 }
4131 else {
4132 PyErr_Format(PyExc_ValueError,
4133 "not enough values to unpack "
4134 "(expected at least %d, got %d)",
4135 argcnt + argcntafter, i);
4136 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004137 }
4138 goto Error;
4139 }
4140 *--sp = w;
4141 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004143 if (argcntafter == -1) {
4144 /* We better have exhausted the iterator now. */
4145 w = PyIter_Next(it);
4146 if (w == NULL) {
4147 if (PyErr_Occurred())
4148 goto Error;
4149 Py_DECREF(it);
4150 return 1;
4151 }
4152 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004153 PyErr_Format(PyExc_ValueError,
4154 "too many values to unpack (expected %d)",
4155 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004156 goto Error;
4157 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004159 l = PySequence_List(it);
4160 if (l == NULL)
4161 goto Error;
4162 *--sp = l;
4163 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004165 ll = PyList_GET_SIZE(l);
4166 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004167 PyErr_Format(PyExc_ValueError,
4168 "not enough values to unpack (expected at least %d, got %zd)",
4169 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004170 goto Error;
4171 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004173 /* Pop the "after-variable" args off the list. */
4174 for (j = argcntafter; j > 0; j--, i++) {
4175 *--sp = PyList_GET_ITEM(l, ll - j);
4176 }
4177 /* Resize the list. */
4178 Py_SIZE(l) = ll - argcntafter;
4179 Py_DECREF(it);
4180 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004181
Tim Petersd6d010b2001-06-21 02:49:55 +00004182Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004183 for (; i > 0; i--, sp++)
4184 Py_DECREF(*sp);
4185 Py_XDECREF(it);
4186 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004187}
4188
4189
Guido van Rossum96a42c81992-01-12 02:29:51 +00004190#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004191static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004192prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004193{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004194 printf("%s ", str);
4195 if (PyObject_Print(v, stdout, 0) != 0)
4196 PyErr_Clear(); /* Don't know what else to do */
4197 printf("\n");
4198 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004199}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004200#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004201
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004202static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004203call_exc_trace(Py_tracefunc func, PyObject *self,
4204 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004205{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004206 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004207 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004208 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004209 if (value == NULL) {
4210 value = Py_None;
4211 Py_INCREF(value);
4212 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004213 PyErr_NormalizeException(&type, &value, &orig_traceback);
4214 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004215 arg = PyTuple_Pack(3, type, value, traceback);
4216 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004217 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004218 return;
4219 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004220 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004221 Py_DECREF(arg);
4222 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004223 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004224 else {
4225 Py_XDECREF(type);
4226 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004227 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004228 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004229}
4230
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004231static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004232call_trace_protected(Py_tracefunc func, PyObject *obj,
4233 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004234 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004235{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004236 PyObject *type, *value, *traceback;
4237 int err;
4238 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004239 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004240 if (err == 0)
4241 {
4242 PyErr_Restore(type, value, traceback);
4243 return 0;
4244 }
4245 else {
4246 Py_XDECREF(type);
4247 Py_XDECREF(value);
4248 Py_XDECREF(traceback);
4249 return -1;
4250 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004251}
4252
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004253static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004254call_trace(Py_tracefunc func, PyObject *obj,
4255 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004256 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004258 int result;
4259 if (tstate->tracing)
4260 return 0;
4261 tstate->tracing++;
4262 tstate->use_tracing = 0;
4263 result = func(obj, frame, what, arg);
4264 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4265 || (tstate->c_profilefunc != NULL));
4266 tstate->tracing--;
4267 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004268}
4269
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004270PyObject *
4271_PyEval_CallTracing(PyObject *func, PyObject *args)
4272{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004273 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004274 int save_tracing = tstate->tracing;
4275 int save_use_tracing = tstate->use_tracing;
4276 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004278 tstate->tracing = 0;
4279 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4280 || (tstate->c_profilefunc != NULL));
4281 result = PyObject_Call(func, args, NULL);
4282 tstate->tracing = save_tracing;
4283 tstate->use_tracing = save_use_tracing;
4284 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004285}
4286
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004287/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004288static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004289maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004290 PyThreadState *tstate, PyFrameObject *frame,
4291 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004292{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004293 int result = 0;
4294 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004296 /* If the last instruction executed isn't in the current
4297 instruction window, reset the window.
4298 */
4299 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4300 PyAddrPair bounds;
4301 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4302 &bounds);
4303 *instr_lb = bounds.ap_lower;
4304 *instr_ub = bounds.ap_upper;
4305 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004306 /* If the last instruction falls at the start of a line or if it
4307 represents a jump backwards, update the frame's line number and
4308 then call the trace function if we're tracing source lines.
4309 */
4310 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004311 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004312 if (frame->f_trace_lines) {
4313 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4314 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004315 }
George King20faa682017-10-18 17:44:22 -07004316 /* Always emit an opcode event if we're tracing all opcodes. */
4317 if (frame->f_trace_opcodes) {
4318 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4319 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004320 *instr_prev = frame->f_lasti;
4321 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004322}
4323
Fred Drake5755ce62001-06-27 19:19:46 +00004324void
4325PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004327 PyThreadState *tstate = PyThreadState_GET();
4328 PyObject *temp = tstate->c_profileobj;
4329 Py_XINCREF(arg);
4330 tstate->c_profilefunc = NULL;
4331 tstate->c_profileobj = NULL;
4332 /* Must make sure that tracing is not ignored if 'temp' is freed */
4333 tstate->use_tracing = tstate->c_tracefunc != NULL;
4334 Py_XDECREF(temp);
4335 tstate->c_profilefunc = func;
4336 tstate->c_profileobj = arg;
4337 /* Flag that tracing or profiling is turned on */
4338 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004339}
4340
4341void
4342PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004344 PyThreadState *tstate = PyThreadState_GET();
4345 PyObject *temp = tstate->c_traceobj;
4346 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4347 Py_XINCREF(arg);
4348 tstate->c_tracefunc = NULL;
4349 tstate->c_traceobj = NULL;
4350 /* Must make sure that profiling is not ignored if 'temp' is freed */
4351 tstate->use_tracing = tstate->c_profilefunc != NULL;
4352 Py_XDECREF(temp);
4353 tstate->c_tracefunc = func;
4354 tstate->c_traceobj = arg;
4355 /* Flag that tracing or profiling is turned on */
4356 tstate->use_tracing = ((func != NULL)
4357 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004358}
4359
Yury Selivanov75445082015-05-11 22:57:16 -04004360void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004361_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4362{
4363 assert(new_depth >= 0);
4364 PyThreadState *tstate = PyThreadState_GET();
4365 tstate->coroutine_origin_tracking_depth = new_depth;
4366}
4367
4368int
4369_PyEval_GetCoroutineOriginTrackingDepth(void)
4370{
4371 PyThreadState *tstate = PyThreadState_GET();
4372 return tstate->coroutine_origin_tracking_depth;
4373}
4374
4375void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004376_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004377{
4378 PyThreadState *tstate = PyThreadState_GET();
4379
Yury Selivanov75445082015-05-11 22:57:16 -04004380 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004381 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004382}
4383
4384PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004385_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004386{
4387 PyThreadState *tstate = PyThreadState_GET();
4388 return tstate->coroutine_wrapper;
4389}
4390
Yury Selivanoveb636452016-09-08 22:01:51 -07004391void
4392_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4393{
4394 PyThreadState *tstate = PyThreadState_GET();
4395
4396 Py_XINCREF(firstiter);
4397 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4398}
4399
4400PyObject *
4401_PyEval_GetAsyncGenFirstiter(void)
4402{
4403 PyThreadState *tstate = PyThreadState_GET();
4404 return tstate->async_gen_firstiter;
4405}
4406
4407void
4408_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4409{
4410 PyThreadState *tstate = PyThreadState_GET();
4411
4412 Py_XINCREF(finalizer);
4413 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4414}
4415
4416PyObject *
4417_PyEval_GetAsyncGenFinalizer(void)
4418{
4419 PyThreadState *tstate = PyThreadState_GET();
4420 return tstate->async_gen_finalizer;
4421}
4422
Guido van Rossumb209a111997-04-29 18:18:01 +00004423PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004424PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004425{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004426 PyFrameObject *current_frame = PyEval_GetFrame();
4427 if (current_frame == NULL)
4428 return PyThreadState_GET()->interp->builtins;
4429 else
4430 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004431}
4432
Guido van Rossumb209a111997-04-29 18:18:01 +00004433PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004434PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004436 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004437 if (current_frame == NULL) {
4438 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004439 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004440 }
4441
4442 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4443 return NULL;
4444
4445 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004446 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004447}
4448
Guido van Rossumb209a111997-04-29 18:18:01 +00004449PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004450PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004451{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004452 PyFrameObject *current_frame = PyEval_GetFrame();
4453 if (current_frame == NULL)
4454 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004455
4456 assert(current_frame->f_globals != NULL);
4457 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004458}
4459
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004460PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004461PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004462{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004463 PyThreadState *tstate = PyThreadState_GET();
4464 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004465}
4466
Guido van Rossum6135a871995-01-09 17:53:26 +00004467int
Tim Peters5ba58662001-07-16 02:29:45 +00004468PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004469{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004470 PyFrameObject *current_frame = PyEval_GetFrame();
4471 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004473 if (current_frame != NULL) {
4474 const int codeflags = current_frame->f_code->co_flags;
4475 const int compilerflags = codeflags & PyCF_MASK;
4476 if (compilerflags) {
4477 result = 1;
4478 cf->cf_flags |= compilerflags;
4479 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004480#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004481 if (codeflags & CO_GENERATOR_ALLOWED) {
4482 result = 1;
4483 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4484 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004485#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004486 }
4487 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004488}
4489
Guido van Rossum3f5da241990-12-20 15:06:42 +00004490
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004491const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004492PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004493{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004494 if (PyMethod_Check(func))
4495 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4496 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004497 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004498 else if (PyCFunction_Check(func))
4499 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4500 else
4501 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004502}
4503
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004504const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004505PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004506{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004507 if (PyMethod_Check(func))
4508 return "()";
4509 else if (PyFunction_Check(func))
4510 return "()";
4511 else if (PyCFunction_Check(func))
4512 return "()";
4513 else
4514 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004515}
4516
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004517#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004518if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004519 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4520 tstate, tstate->frame, \
4521 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004522 x = NULL; \
4523 } \
4524 else { \
4525 x = call; \
4526 if (tstate->c_profilefunc != NULL) { \
4527 if (x == NULL) { \
4528 call_trace_protected(tstate->c_profilefunc, \
4529 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004530 tstate, tstate->frame, \
4531 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004532 /* XXX should pass (type, value, tb) */ \
4533 } else { \
4534 if (call_trace(tstate->c_profilefunc, \
4535 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004536 tstate, tstate->frame, \
4537 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004538 Py_DECREF(x); \
4539 x = NULL; \
4540 } \
4541 } \
4542 } \
4543 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004544} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004545 x = call; \
4546 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004547
Victor Stinner415c5102017-01-11 00:54:57 +01004548/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4549 to reduce the stack consumption. */
4550Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004551call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004552{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004553 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004554 PyObject *func = *pfunc;
4555 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004556 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4557 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004558 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004560 /* Always dispatch PyCFunction first, because these are
4561 presumed to be the most frequent callable object.
4562 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004563 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004564 PyThreadState *tstate = PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004565 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004566 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004567 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
4568 PyThreadState *tstate = PyThreadState_GET();
INADA Naoki93fac8d2017-03-07 14:24:37 +09004569 if (tstate->use_tracing && tstate->c_profilefunc) {
4570 // We need to create PyCFunctionObject for tracing.
4571 PyMethodDescrObject *descr = (PyMethodDescrObject*)func;
4572 func = PyCFunction_NewEx(descr->d_method, stack[0], NULL);
4573 if (func == NULL) {
4574 return NULL;
4575 }
4576 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack+1, nargs-1,
4577 kwnames));
4578 Py_DECREF(func);
4579 }
4580 else {
4581 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4582 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004583 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004584 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004585 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004586 /* Optimize access to bound methods. Reuse the Python stack
4587 to pass 'self' as the first argument, replace 'func'
4588 with 'self'. It avoids the creation of a new temporary tuple
4589 for arguments (to replace func with self) when the method uses
4590 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004591 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004592 Py_INCREF(self);
4593 func = PyMethod_GET_FUNCTION(func);
4594 Py_INCREF(func);
4595 Py_SETREF(*pfunc, self);
4596 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004597 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004598 }
4599 else {
4600 Py_INCREF(func);
4601 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004602
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004603 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004604 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004605 }
4606 else {
4607 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4608 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004609 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004610 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004611
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004612 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4613
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004614 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004615 while ((*pp_stack) > pfunc) {
4616 w = EXT_POP(*pp_stack);
4617 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004618 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004620 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004621}
4622
Jeremy Hylton52820442001-01-03 23:52:36 +00004623static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004624do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004625{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004626 if (PyCFunction_Check(func)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004627 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004628 PyThreadState *tstate = PyThreadState_GET();
4629 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004630 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004631 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004632 else {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004633 return PyObject_Call(func, callargs, kwdict);
Victor Stinner74319ae2016-08-25 00:04:09 +02004634 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004635}
4636
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004637/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004638 nb_index slot defined, and store in *pi.
4639 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004640 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004641 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004642*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004643int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004644_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004645{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004646 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004647 Py_ssize_t x;
4648 if (PyIndex_Check(v)) {
4649 x = PyNumber_AsSsize_t(v, NULL);
4650 if (x == -1 && PyErr_Occurred())
4651 return 0;
4652 }
4653 else {
4654 PyErr_SetString(PyExc_TypeError,
4655 "slice indices must be integers or "
4656 "None or have an __index__ method");
4657 return 0;
4658 }
4659 *pi = x;
4660 }
4661 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004662}
4663
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004664int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004665_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004666{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004667 Py_ssize_t x;
4668 if (PyIndex_Check(v)) {
4669 x = PyNumber_AsSsize_t(v, NULL);
4670 if (x == -1 && PyErr_Occurred())
4671 return 0;
4672 }
4673 else {
4674 PyErr_SetString(PyExc_TypeError,
4675 "slice indices must be integers or "
4676 "have an __index__ method");
4677 return 0;
4678 }
4679 *pi = x;
4680 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004681}
4682
4683
Guido van Rossum486364b2007-06-30 05:01:58 +00004684#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004685 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004686
Guido van Rossumb209a111997-04-29 18:18:01 +00004687static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004688cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004689{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004690 int res = 0;
4691 switch (op) {
4692 case PyCmp_IS:
4693 res = (v == w);
4694 break;
4695 case PyCmp_IS_NOT:
4696 res = (v != w);
4697 break;
4698 case PyCmp_IN:
4699 res = PySequence_Contains(w, v);
4700 if (res < 0)
4701 return NULL;
4702 break;
4703 case PyCmp_NOT_IN:
4704 res = PySequence_Contains(w, v);
4705 if (res < 0)
4706 return NULL;
4707 res = !res;
4708 break;
4709 case PyCmp_EXC_MATCH:
4710 if (PyTuple_Check(w)) {
4711 Py_ssize_t i, length;
4712 length = PyTuple_Size(w);
4713 for (i = 0; i < length; i += 1) {
4714 PyObject *exc = PyTuple_GET_ITEM(w, i);
4715 if (!PyExceptionClass_Check(exc)) {
4716 PyErr_SetString(PyExc_TypeError,
4717 CANNOT_CATCH_MSG);
4718 return NULL;
4719 }
4720 }
4721 }
4722 else {
4723 if (!PyExceptionClass_Check(w)) {
4724 PyErr_SetString(PyExc_TypeError,
4725 CANNOT_CATCH_MSG);
4726 return NULL;
4727 }
4728 }
4729 res = PyErr_GivenExceptionMatches(v, w);
4730 break;
4731 default:
4732 return PyObject_RichCompare(v, w, op);
4733 }
4734 v = res ? Py_True : Py_False;
4735 Py_INCREF(v);
4736 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004737}
4738
Thomas Wouters52152252000-08-17 22:55:00 +00004739static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004740import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4741{
4742 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004743 PyObject *import_func, *res;
4744 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004745
4746 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
4747 if (import_func == NULL) {
4748 PyErr_SetString(PyExc_ImportError, "__import__ not found");
4749 return NULL;
4750 }
4751
4752 /* Fast path for not overloaded __import__. */
4753 if (import_func == PyThreadState_GET()->interp->import_func) {
4754 int ilevel = _PyLong_AsInt(level);
4755 if (ilevel == -1 && PyErr_Occurred()) {
4756 return NULL;
4757 }
4758 res = PyImport_ImportModuleLevelObject(
4759 name,
4760 f->f_globals,
4761 f->f_locals == NULL ? Py_None : f->f_locals,
4762 fromlist,
4763 ilevel);
4764 return res;
4765 }
4766
4767 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004768
4769 stack[0] = name;
4770 stack[1] = f->f_globals;
4771 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
4772 stack[3] = fromlist;
4773 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02004774 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004775 Py_DECREF(import_func);
4776 return res;
4777}
4778
4779static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004780import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004781{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004782 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02004783 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08004784 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004785
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004786 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02004787 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004788 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004789 /* Issue #17636: in case this failed because of a circular relative
4790 import, try to fallback on reading the module directly from
4791 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02004792 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07004793 if (pkgname == NULL) {
4794 goto error;
4795 }
Oren Milman6db70332017-09-19 14:23:01 +03004796 if (!PyUnicode_Check(pkgname)) {
4797 Py_CLEAR(pkgname);
4798 goto error;
4799 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004800 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07004801 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08004802 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004803 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07004804 }
Eric Snow3f9eee62017-09-15 16:35:20 -06004805 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004806 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07004807 if (x == NULL) {
4808 goto error;
4809 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004810 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004811 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07004812 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004813 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004814 if (pkgname == NULL) {
4815 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
4816 if (pkgname_or_unknown == NULL) {
4817 Py_XDECREF(pkgpath);
4818 return NULL;
4819 }
4820 } else {
4821 pkgname_or_unknown = pkgname;
4822 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004823
4824 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
4825 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08004826 errmsg = PyUnicode_FromFormat(
4827 "cannot import name %R from %R (unknown location)",
4828 name, pkgname_or_unknown
4829 );
4830 /* NULL check for errmsg done by PyErr_SetImportError. */
4831 PyErr_SetImportError(errmsg, pkgname, NULL);
4832 }
4833 else {
4834 errmsg = PyUnicode_FromFormat(
4835 "cannot import name %R from %R (%S)",
4836 name, pkgname_or_unknown, pkgpath
4837 );
4838 /* NULL check for errmsg done by PyErr_SetImportError. */
4839 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004840 }
4841
Xiang Zhang4830f582017-03-21 11:13:42 +08004842 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004843 Py_XDECREF(pkgname_or_unknown);
4844 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07004845 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00004846}
Guido van Rossumac7be682001-01-17 15:42:30 +00004847
Thomas Wouters52152252000-08-17 22:55:00 +00004848static int
4849import_all_from(PyObject *locals, PyObject *v)
4850{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004851 _Py_IDENTIFIER(__all__);
4852 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08004853 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004854 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004855 int skip_leading_underscores = 0;
4856 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004857
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004858 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
4859 return -1; /* Unexpected error */
4860 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004861 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004862 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
4863 return -1;
4864 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004865 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004866 PyErr_SetString(PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004867 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004868 return -1;
4869 }
4870 all = PyMapping_Keys(dict);
4871 Py_DECREF(dict);
4872 if (all == NULL)
4873 return -1;
4874 skip_leading_underscores = 1;
4875 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004877 for (pos = 0, err = 0; ; pos++) {
4878 name = PySequence_GetItem(all, pos);
4879 if (name == NULL) {
4880 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4881 err = -1;
4882 else
4883 PyErr_Clear();
4884 break;
4885 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08004886 if (!PyUnicode_Check(name)) {
4887 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
4888 if (modname == NULL) {
4889 Py_DECREF(name);
4890 err = -1;
4891 break;
4892 }
4893 if (!PyUnicode_Check(modname)) {
4894 PyErr_Format(PyExc_TypeError,
4895 "module __name__ must be a string, not %.100s",
4896 Py_TYPE(modname)->tp_name);
4897 }
4898 else {
4899 PyErr_Format(PyExc_TypeError,
4900 "%s in %U.%s must be str, not %.100s",
4901 skip_leading_underscores ? "Key" : "Item",
4902 modname,
4903 skip_leading_underscores ? "__dict__" : "__all__",
4904 Py_TYPE(name)->tp_name);
4905 }
4906 Py_DECREF(modname);
4907 Py_DECREF(name);
4908 err = -1;
4909 break;
4910 }
4911 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03004912 if (PyUnicode_READY(name) == -1) {
4913 Py_DECREF(name);
4914 err = -1;
4915 break;
4916 }
4917 if (PyUnicode_READ_CHAR(name, 0) == '_') {
4918 Py_DECREF(name);
4919 continue;
4920 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004921 }
4922 value = PyObject_GetAttr(v, name);
4923 if (value == NULL)
4924 err = -1;
4925 else if (PyDict_CheckExact(locals))
4926 err = PyDict_SetItem(locals, name, value);
4927 else
4928 err = PyObject_SetItem(locals, name, value);
4929 Py_DECREF(name);
4930 Py_XDECREF(value);
4931 if (err != 0)
4932 break;
4933 }
4934 Py_DECREF(all);
4935 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004936}
4937
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03004938static int
4939check_args_iterable(PyObject *func, PyObject *args)
4940{
4941 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
4942 PyErr_Format(PyExc_TypeError,
4943 "%.200s%.200s argument after * "
4944 "must be an iterable, not %.200s",
4945 PyEval_GetFuncName(func),
4946 PyEval_GetFuncDesc(func),
4947 args->ob_type->tp_name);
4948 return -1;
4949 }
4950 return 0;
4951}
4952
4953static void
4954format_kwargs_mapping_error(PyObject *func, PyObject *kwargs)
4955{
4956 PyErr_Format(PyExc_TypeError,
4957 "%.200s%.200s argument after ** "
4958 "must be a mapping, not %.200s",
4959 PyEval_GetFuncName(func),
4960 PyEval_GetFuncDesc(func),
4961 kwargs->ob_type->tp_name);
4962}
4963
Guido van Rossumac7be682001-01-17 15:42:30 +00004964static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004965format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004966{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004967 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004968
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004969 if (!obj)
4970 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004971
Serhiy Storchaka06515832016-11-20 09:13:07 +02004972 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004973 if (!obj_str)
4974 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004976 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004977}
Guido van Rossum950361c1997-01-24 13:49:28 +00004978
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004979static void
4980format_exc_unbound(PyCodeObject *co, int oparg)
4981{
4982 PyObject *name;
4983 /* Don't stomp existing exception */
4984 if (PyErr_Occurred())
4985 return;
4986 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4987 name = PyTuple_GET_ITEM(co->co_cellvars,
4988 oparg);
4989 format_exc_check_arg(
4990 PyExc_UnboundLocalError,
4991 UNBOUNDLOCAL_ERROR_MSG,
4992 name);
4993 } else {
4994 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4995 PyTuple_GET_SIZE(co->co_cellvars));
4996 format_exc_check_arg(PyExc_NameError,
4997 UNBOUNDFREE_ERROR_MSG, name);
4998 }
4999}
5000
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005001static void
5002format_awaitable_error(PyTypeObject *type, int prevopcode)
5003{
5004 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5005 if (prevopcode == BEFORE_ASYNC_WITH) {
5006 PyErr_Format(PyExc_TypeError,
5007 "'async with' received an object from __aenter__ "
5008 "that does not implement __await__: %.100s",
5009 type->tp_name);
5010 }
5011 else if (prevopcode == WITH_CLEANUP_START) {
5012 PyErr_Format(PyExc_TypeError,
5013 "'async with' received an object from __aexit__ "
5014 "that does not implement __await__: %.100s",
5015 type->tp_name);
5016 }
5017 }
5018}
5019
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005020static PyObject *
5021unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005022 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005023{
5024 PyObject *res;
5025 if (Py_REFCNT(v) == 2) {
5026 /* In the common case, there are 2 references to the value
5027 * stored in 'variable' when the += is performed: one on the
5028 * value stack (in 'v') and one still stored in the
5029 * 'variable'. We try to delete the variable now to reduce
5030 * the refcnt to 1.
5031 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005032 int opcode, oparg;
5033 NEXTOPARG();
5034 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005035 case STORE_FAST:
5036 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005037 PyObject **fastlocals = f->f_localsplus;
5038 if (GETLOCAL(oparg) == v)
5039 SETLOCAL(oparg, NULL);
5040 break;
5041 }
5042 case STORE_DEREF:
5043 {
5044 PyObject **freevars = (f->f_localsplus +
5045 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005046 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005047 if (PyCell_GET(c) == v) {
5048 PyCell_SET(c, NULL);
5049 Py_DECREF(v);
5050 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005051 break;
5052 }
5053 case STORE_NAME:
5054 {
5055 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005056 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005057 PyObject *locals = f->f_locals;
5058 if (PyDict_CheckExact(locals) &&
5059 PyDict_GetItem(locals, name) == v) {
5060 if (PyDict_DelItem(locals, name) != 0) {
5061 PyErr_Clear();
5062 }
5063 }
5064 break;
5065 }
5066 }
5067 }
5068 res = v;
5069 PyUnicode_Append(&res, w);
5070 return res;
5071}
5072
Guido van Rossum950361c1997-01-24 13:49:28 +00005073#ifdef DYNAMIC_EXECUTION_PROFILE
5074
Skip Montanarof118cb12001-10-15 20:51:38 +00005075static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005076getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005077{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005078 int i;
5079 PyObject *l = PyList_New(256);
5080 if (l == NULL) return NULL;
5081 for (i = 0; i < 256; i++) {
5082 PyObject *x = PyLong_FromLong(a[i]);
5083 if (x == NULL) {
5084 Py_DECREF(l);
5085 return NULL;
5086 }
5087 PyList_SetItem(l, i, x);
5088 }
5089 for (i = 0; i < 256; i++)
5090 a[i] = 0;
5091 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005092}
5093
5094PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005095_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005096{
5097#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005098 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005099#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005100 int i;
5101 PyObject *l = PyList_New(257);
5102 if (l == NULL) return NULL;
5103 for (i = 0; i < 257; i++) {
5104 PyObject *x = getarray(dxpairs[i]);
5105 if (x == NULL) {
5106 Py_DECREF(l);
5107 return NULL;
5108 }
5109 PyList_SetItem(l, i, x);
5110 }
5111 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005112#endif
5113}
5114
5115#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005116
5117Py_ssize_t
5118_PyEval_RequestCodeExtraIndex(freefunc free)
5119{
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005120 PyInterpreterState *interp = PyThreadState_Get()->interp;
Brett Cannon5c4de282016-09-07 11:16:41 -07005121 Py_ssize_t new_index;
5122
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005123 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005124 return -1;
5125 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005126 new_index = interp->co_extra_user_count++;
5127 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005128 return new_index;
5129}
Łukasz Langaa785c872016-09-09 17:37:37 -07005130
5131static void
5132dtrace_function_entry(PyFrameObject *f)
5133{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005134 const char *filename;
5135 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005136 int lineno;
5137
5138 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5139 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5140 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5141
5142 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5143}
5144
5145static void
5146dtrace_function_return(PyFrameObject *f)
5147{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005148 const char *filename;
5149 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005150 int lineno;
5151
5152 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5153 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5154 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5155
5156 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5157}
5158
5159/* DTrace equivalent of maybe_call_line_trace. */
5160static void
5161maybe_dtrace_line(PyFrameObject *frame,
5162 int *instr_lb, int *instr_ub, int *instr_prev)
5163{
5164 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005165 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005166
5167 /* If the last instruction executed isn't in the current
5168 instruction window, reset the window.
5169 */
5170 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5171 PyAddrPair bounds;
5172 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5173 &bounds);
5174 *instr_lb = bounds.ap_lower;
5175 *instr_ub = bounds.ap_upper;
5176 }
5177 /* If the last instruction falls at the start of a line or if
5178 it represents a jump backwards, update the frame's line
5179 number and call the trace function. */
5180 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5181 frame->f_lineno = line;
5182 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5183 if (!co_filename)
5184 co_filename = "?";
5185 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5186 if (!co_name)
5187 co_name = "?";
5188 PyDTrace_LINE(co_filename, co_name, line);
5189 }
5190 *instr_prev = frame->f_lasti;
5191}