blob: 422a29ed77d65776ba4c22defa5e26aa3b96b54d [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)) {
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -0700931 if (_Py_OPCODE(*next_instr) == SETUP_FINALLY ||
932 _Py_OPCODE(*next_instr) == YIELD_FROM) {
933 /* Two cases where we skip running signal handlers and other
934 pending calls:
935 - If we're about to enter the try: of a try/finally (not
936 *very* useful, but might help in some cases and it's
937 traditional)
938 - If we're resuming a chain of nested 'yield from' or
939 'await' calls, then each frame is parked with YIELD_FROM
940 as its next opcode. If the user hit control-C we want to
941 wait until we've reached the innermost frame before
942 running the signal handler and raising KeyboardInterrupt
943 (see bpo-30039).
944 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 goto fast_next_opcode;
946 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600947 if (_Py_atomic_load_relaxed(
948 &_PyRuntime.ceval.pending.calls_to_do))
949 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400950 if (Py_MakePendingCalls() < 0)
951 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600953 if (_Py_atomic_load_relaxed(
954 &_PyRuntime.ceval.gil_drop_request))
955 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 /* Give another thread a chance */
957 if (PyThreadState_Swap(NULL) != tstate)
958 Py_FatalError("ceval: tstate mix-up");
959 drop_gil(tstate);
960
961 /* Other threads may run now */
962
963 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -0700964
965 /* Check if we should make a quick exit. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600966 if (_Py_IsFinalizing() &&
967 !_Py_CURRENTLY_FINALIZING(tstate))
968 {
Benjamin Peterson17548dd2014-06-16 22:59:07 -0700969 drop_gil(tstate);
970 PyThread_exit_thread();
971 }
972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 if (PyThreadState_Swap(tstate) != NULL)
974 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 }
976 /* Check for asynchronous exceptions. */
977 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400978 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 tstate->async_exc = NULL;
980 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400981 PyErr_SetNone(exc);
982 Py_DECREF(exc);
983 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 }
985 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 fast_next_opcode:
988 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000989
Łukasz Langaa785c872016-09-09 17:37:37 -0700990 if (PyDTrace_LINE_ENABLED())
991 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -0500996 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400997 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 /* see maybe_call_line_trace
999 for expository comments */
1000 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 err = maybe_call_line_trace(tstate->c_tracefunc,
1003 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001004 tstate, f,
1005 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 /* Reload possibly changed frame fields */
1007 JUMPTO(f->f_lasti);
1008 if (f->f_stacktop != NULL) {
1009 stack_pointer = f->f_stacktop;
1010 f->f_stacktop = NULL;
1011 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001012 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001014 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001016
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001018
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001019 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001020 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001021#ifdef DYNAMIC_EXECUTION_PROFILE
1022#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 dxpairs[lastopcode][opcode]++;
1024 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001025#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001027#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001028
Guido van Rossum96a42c81992-01-12 02:29:51 +00001029#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 if (lltrace) {
1033 if (HAS_ARG(opcode)) {
1034 printf("%d: %d, %d\n",
1035 f->f_lasti, opcode, oparg);
1036 }
1037 else {
1038 printf("%d: %d\n",
1039 f->f_lasti, opcode);
1040 }
1041 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001042#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001047 It is essential that any operation that fails must goto error
1048 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 TARGET(NOP)
1051 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001052
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001053 TARGET(LOAD_FAST) {
1054 PyObject *value = GETLOCAL(oparg);
1055 if (value == NULL) {
1056 format_exc_check_arg(PyExc_UnboundLocalError,
1057 UNBOUNDLOCAL_ERROR_MSG,
1058 PyTuple_GetItem(co->co_varnames, oparg));
1059 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001061 Py_INCREF(value);
1062 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001064 }
1065
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001066 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001067 TARGET(LOAD_CONST) {
1068 PyObject *value = GETITEM(consts, oparg);
1069 Py_INCREF(value);
1070 PUSH(value);
1071 FAST_DISPATCH();
1072 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001073
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001074 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001075 TARGET(STORE_FAST) {
1076 PyObject *value = POP();
1077 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001079 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001080
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001081 TARGET(POP_TOP) {
1082 PyObject *value = POP();
1083 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001085 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001086
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001087 TARGET(ROT_TWO) {
1088 PyObject *top = TOP();
1089 PyObject *second = SECOND();
1090 SET_TOP(second);
1091 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001093 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001094
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001095 TARGET(ROT_THREE) {
1096 PyObject *top = TOP();
1097 PyObject *second = SECOND();
1098 PyObject *third = THIRD();
1099 SET_TOP(second);
1100 SET_SECOND(third);
1101 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001103 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001104
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001105 TARGET(ROT_FOUR) {
1106 PyObject *top = TOP();
1107 PyObject *second = SECOND();
1108 PyObject *third = THIRD();
1109 PyObject *fourth = FOURTH();
1110 SET_TOP(second);
1111 SET_SECOND(third);
1112 SET_THIRD(fourth);
1113 SET_FOURTH(top);
1114 FAST_DISPATCH();
1115 }
1116
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001117 TARGET(DUP_TOP) {
1118 PyObject *top = TOP();
1119 Py_INCREF(top);
1120 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001122 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001123
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001124 TARGET(DUP_TOP_TWO) {
1125 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001126 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001127 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001128 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001129 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001130 SET_TOP(top);
1131 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001132 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001133 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001134
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001135 TARGET(UNARY_POSITIVE) {
1136 PyObject *value = TOP();
1137 PyObject *res = PyNumber_Positive(value);
1138 Py_DECREF(value);
1139 SET_TOP(res);
1140 if (res == NULL)
1141 goto error;
1142 DISPATCH();
1143 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001144
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001145 TARGET(UNARY_NEGATIVE) {
1146 PyObject *value = TOP();
1147 PyObject *res = PyNumber_Negative(value);
1148 Py_DECREF(value);
1149 SET_TOP(res);
1150 if (res == NULL)
1151 goto error;
1152 DISPATCH();
1153 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001154
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001155 TARGET(UNARY_NOT) {
1156 PyObject *value = TOP();
1157 int err = PyObject_IsTrue(value);
1158 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 if (err == 0) {
1160 Py_INCREF(Py_True);
1161 SET_TOP(Py_True);
1162 DISPATCH();
1163 }
1164 else if (err > 0) {
1165 Py_INCREF(Py_False);
1166 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 DISPATCH();
1168 }
1169 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001170 goto error;
1171 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001172
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001173 TARGET(UNARY_INVERT) {
1174 PyObject *value = TOP();
1175 PyObject *res = PyNumber_Invert(value);
1176 Py_DECREF(value);
1177 SET_TOP(res);
1178 if (res == NULL)
1179 goto error;
1180 DISPATCH();
1181 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001182
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001183 TARGET(BINARY_POWER) {
1184 PyObject *exp = POP();
1185 PyObject *base = TOP();
1186 PyObject *res = PyNumber_Power(base, exp, Py_None);
1187 Py_DECREF(base);
1188 Py_DECREF(exp);
1189 SET_TOP(res);
1190 if (res == NULL)
1191 goto error;
1192 DISPATCH();
1193 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001194
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001195 TARGET(BINARY_MULTIPLY) {
1196 PyObject *right = POP();
1197 PyObject *left = TOP();
1198 PyObject *res = PyNumber_Multiply(left, right);
1199 Py_DECREF(left);
1200 Py_DECREF(right);
1201 SET_TOP(res);
1202 if (res == NULL)
1203 goto error;
1204 DISPATCH();
1205 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001206
Benjamin Petersond51374e2014-04-09 23:55:56 -04001207 TARGET(BINARY_MATRIX_MULTIPLY) {
1208 PyObject *right = POP();
1209 PyObject *left = TOP();
1210 PyObject *res = PyNumber_MatrixMultiply(left, right);
1211 Py_DECREF(left);
1212 Py_DECREF(right);
1213 SET_TOP(res);
1214 if (res == NULL)
1215 goto error;
1216 DISPATCH();
1217 }
1218
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001219 TARGET(BINARY_TRUE_DIVIDE) {
1220 PyObject *divisor = POP();
1221 PyObject *dividend = TOP();
1222 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1223 Py_DECREF(dividend);
1224 Py_DECREF(divisor);
1225 SET_TOP(quotient);
1226 if (quotient == NULL)
1227 goto error;
1228 DISPATCH();
1229 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001230
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001231 TARGET(BINARY_FLOOR_DIVIDE) {
1232 PyObject *divisor = POP();
1233 PyObject *dividend = TOP();
1234 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1235 Py_DECREF(dividend);
1236 Py_DECREF(divisor);
1237 SET_TOP(quotient);
1238 if (quotient == NULL)
1239 goto error;
1240 DISPATCH();
1241 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001242
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001243 TARGET(BINARY_MODULO) {
1244 PyObject *divisor = POP();
1245 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001246 PyObject *res;
1247 if (PyUnicode_CheckExact(dividend) && (
1248 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1249 // fast path; string formatting, but not if the RHS is a str subclass
1250 // (see issue28598)
1251 res = PyUnicode_Format(dividend, divisor);
1252 } else {
1253 res = PyNumber_Remainder(dividend, divisor);
1254 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001255 Py_DECREF(divisor);
1256 Py_DECREF(dividend);
1257 SET_TOP(res);
1258 if (res == NULL)
1259 goto error;
1260 DISPATCH();
1261 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001262
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001263 TARGET(BINARY_ADD) {
1264 PyObject *right = POP();
1265 PyObject *left = TOP();
1266 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001267 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1268 CPython using bytecode, it is simply worthless.
1269 See http://bugs.python.org/issue21955 and
1270 http://bugs.python.org/issue10044 for the discussion. In short,
1271 no patch shown any impact on a realistic benchmark, only a minor
1272 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001273 if (PyUnicode_CheckExact(left) &&
1274 PyUnicode_CheckExact(right)) {
1275 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001276 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001277 }
1278 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001279 sum = PyNumber_Add(left, right);
1280 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001281 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001282 Py_DECREF(right);
1283 SET_TOP(sum);
1284 if (sum == NULL)
1285 goto error;
1286 DISPATCH();
1287 }
1288
1289 TARGET(BINARY_SUBTRACT) {
1290 PyObject *right = POP();
1291 PyObject *left = TOP();
1292 PyObject *diff = PyNumber_Subtract(left, right);
1293 Py_DECREF(right);
1294 Py_DECREF(left);
1295 SET_TOP(diff);
1296 if (diff == NULL)
1297 goto error;
1298 DISPATCH();
1299 }
1300
1301 TARGET(BINARY_SUBSCR) {
1302 PyObject *sub = POP();
1303 PyObject *container = TOP();
1304 PyObject *res = PyObject_GetItem(container, sub);
1305 Py_DECREF(container);
1306 Py_DECREF(sub);
1307 SET_TOP(res);
1308 if (res == NULL)
1309 goto error;
1310 DISPATCH();
1311 }
1312
1313 TARGET(BINARY_LSHIFT) {
1314 PyObject *right = POP();
1315 PyObject *left = TOP();
1316 PyObject *res = PyNumber_Lshift(left, right);
1317 Py_DECREF(left);
1318 Py_DECREF(right);
1319 SET_TOP(res);
1320 if (res == NULL)
1321 goto error;
1322 DISPATCH();
1323 }
1324
1325 TARGET(BINARY_RSHIFT) {
1326 PyObject *right = POP();
1327 PyObject *left = TOP();
1328 PyObject *res = PyNumber_Rshift(left, right);
1329 Py_DECREF(left);
1330 Py_DECREF(right);
1331 SET_TOP(res);
1332 if (res == NULL)
1333 goto error;
1334 DISPATCH();
1335 }
1336
1337 TARGET(BINARY_AND) {
1338 PyObject *right = POP();
1339 PyObject *left = TOP();
1340 PyObject *res = PyNumber_And(left, right);
1341 Py_DECREF(left);
1342 Py_DECREF(right);
1343 SET_TOP(res);
1344 if (res == NULL)
1345 goto error;
1346 DISPATCH();
1347 }
1348
1349 TARGET(BINARY_XOR) {
1350 PyObject *right = POP();
1351 PyObject *left = TOP();
1352 PyObject *res = PyNumber_Xor(left, right);
1353 Py_DECREF(left);
1354 Py_DECREF(right);
1355 SET_TOP(res);
1356 if (res == NULL)
1357 goto error;
1358 DISPATCH();
1359 }
1360
1361 TARGET(BINARY_OR) {
1362 PyObject *right = POP();
1363 PyObject *left = TOP();
1364 PyObject *res = PyNumber_Or(left, right);
1365 Py_DECREF(left);
1366 Py_DECREF(right);
1367 SET_TOP(res);
1368 if (res == NULL)
1369 goto error;
1370 DISPATCH();
1371 }
1372
1373 TARGET(LIST_APPEND) {
1374 PyObject *v = POP();
1375 PyObject *list = PEEK(oparg);
1376 int err;
1377 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001379 if (err != 0)
1380 goto error;
1381 PREDICT(JUMP_ABSOLUTE);
1382 DISPATCH();
1383 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001384
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001385 TARGET(SET_ADD) {
1386 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001387 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001388 int err;
1389 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001391 if (err != 0)
1392 goto error;
1393 PREDICT(JUMP_ABSOLUTE);
1394 DISPATCH();
1395 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001396
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001397 TARGET(INPLACE_POWER) {
1398 PyObject *exp = POP();
1399 PyObject *base = TOP();
1400 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1401 Py_DECREF(base);
1402 Py_DECREF(exp);
1403 SET_TOP(res);
1404 if (res == NULL)
1405 goto error;
1406 DISPATCH();
1407 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001408
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001409 TARGET(INPLACE_MULTIPLY) {
1410 PyObject *right = POP();
1411 PyObject *left = TOP();
1412 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1413 Py_DECREF(left);
1414 Py_DECREF(right);
1415 SET_TOP(res);
1416 if (res == NULL)
1417 goto error;
1418 DISPATCH();
1419 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001420
Benjamin Petersond51374e2014-04-09 23:55:56 -04001421 TARGET(INPLACE_MATRIX_MULTIPLY) {
1422 PyObject *right = POP();
1423 PyObject *left = TOP();
1424 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1425 Py_DECREF(left);
1426 Py_DECREF(right);
1427 SET_TOP(res);
1428 if (res == NULL)
1429 goto error;
1430 DISPATCH();
1431 }
1432
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001433 TARGET(INPLACE_TRUE_DIVIDE) {
1434 PyObject *divisor = POP();
1435 PyObject *dividend = TOP();
1436 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1437 Py_DECREF(dividend);
1438 Py_DECREF(divisor);
1439 SET_TOP(quotient);
1440 if (quotient == NULL)
1441 goto error;
1442 DISPATCH();
1443 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001444
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001445 TARGET(INPLACE_FLOOR_DIVIDE) {
1446 PyObject *divisor = POP();
1447 PyObject *dividend = TOP();
1448 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1449 Py_DECREF(dividend);
1450 Py_DECREF(divisor);
1451 SET_TOP(quotient);
1452 if (quotient == NULL)
1453 goto error;
1454 DISPATCH();
1455 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001456
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001457 TARGET(INPLACE_MODULO) {
1458 PyObject *right = POP();
1459 PyObject *left = TOP();
1460 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1461 Py_DECREF(left);
1462 Py_DECREF(right);
1463 SET_TOP(mod);
1464 if (mod == NULL)
1465 goto error;
1466 DISPATCH();
1467 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001468
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001469 TARGET(INPLACE_ADD) {
1470 PyObject *right = POP();
1471 PyObject *left = TOP();
1472 PyObject *sum;
1473 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1474 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001475 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001476 }
1477 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001478 sum = PyNumber_InPlaceAdd(left, right);
1479 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001480 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001481 Py_DECREF(right);
1482 SET_TOP(sum);
1483 if (sum == NULL)
1484 goto error;
1485 DISPATCH();
1486 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001487
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001488 TARGET(INPLACE_SUBTRACT) {
1489 PyObject *right = POP();
1490 PyObject *left = TOP();
1491 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1492 Py_DECREF(left);
1493 Py_DECREF(right);
1494 SET_TOP(diff);
1495 if (diff == NULL)
1496 goto error;
1497 DISPATCH();
1498 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001499
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001500 TARGET(INPLACE_LSHIFT) {
1501 PyObject *right = POP();
1502 PyObject *left = TOP();
1503 PyObject *res = PyNumber_InPlaceLshift(left, right);
1504 Py_DECREF(left);
1505 Py_DECREF(right);
1506 SET_TOP(res);
1507 if (res == NULL)
1508 goto error;
1509 DISPATCH();
1510 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001511
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001512 TARGET(INPLACE_RSHIFT) {
1513 PyObject *right = POP();
1514 PyObject *left = TOP();
1515 PyObject *res = PyNumber_InPlaceRshift(left, right);
1516 Py_DECREF(left);
1517 Py_DECREF(right);
1518 SET_TOP(res);
1519 if (res == NULL)
1520 goto error;
1521 DISPATCH();
1522 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001523
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001524 TARGET(INPLACE_AND) {
1525 PyObject *right = POP();
1526 PyObject *left = TOP();
1527 PyObject *res = PyNumber_InPlaceAnd(left, right);
1528 Py_DECREF(left);
1529 Py_DECREF(right);
1530 SET_TOP(res);
1531 if (res == NULL)
1532 goto error;
1533 DISPATCH();
1534 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001535
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001536 TARGET(INPLACE_XOR) {
1537 PyObject *right = POP();
1538 PyObject *left = TOP();
1539 PyObject *res = PyNumber_InPlaceXor(left, right);
1540 Py_DECREF(left);
1541 Py_DECREF(right);
1542 SET_TOP(res);
1543 if (res == NULL)
1544 goto error;
1545 DISPATCH();
1546 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001547
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001548 TARGET(INPLACE_OR) {
1549 PyObject *right = POP();
1550 PyObject *left = TOP();
1551 PyObject *res = PyNumber_InPlaceOr(left, right);
1552 Py_DECREF(left);
1553 Py_DECREF(right);
1554 SET_TOP(res);
1555 if (res == NULL)
1556 goto error;
1557 DISPATCH();
1558 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001559
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001560 TARGET(STORE_SUBSCR) {
1561 PyObject *sub = TOP();
1562 PyObject *container = SECOND();
1563 PyObject *v = THIRD();
1564 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001565 STACKADJ(-3);
Martin Panter95f53c12016-07-18 08:23:26 +00001566 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001567 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001569 Py_DECREF(container);
1570 Py_DECREF(sub);
1571 if (err != 0)
1572 goto error;
1573 DISPATCH();
1574 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001575
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001576 TARGET(DELETE_SUBSCR) {
1577 PyObject *sub = TOP();
1578 PyObject *container = SECOND();
1579 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580 STACKADJ(-2);
Martin Panter95f53c12016-07-18 08:23:26 +00001581 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001582 err = PyObject_DelItem(container, sub);
1583 Py_DECREF(container);
1584 Py_DECREF(sub);
1585 if (err != 0)
1586 goto error;
1587 DISPATCH();
1588 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001589
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001590 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001591 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001592 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001593 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001594 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001595 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 PyErr_SetString(PyExc_RuntimeError,
1597 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001598 Py_DECREF(value);
1599 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001601 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001602 Py_DECREF(value);
1603 if (res == NULL)
1604 goto error;
1605 Py_DECREF(res);
1606 DISPATCH();
1607 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001608
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001609 TARGET(RAISE_VARARGS) {
1610 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 switch (oparg) {
1612 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001613 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001614 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001616 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001617 /* fall through */
1618 case 0:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001619 if (do_raise(exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001620 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001621 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 break;
1623 default:
1624 PyErr_SetString(PyExc_SystemError,
1625 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 break;
1627 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001628 goto error;
1629 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001630
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001631 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001633 assert(f->f_iblock == 0);
1634 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001635 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001636
Yury Selivanov75445082015-05-11 22:57:16 -04001637 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001638 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001639 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001640 PyObject *obj = TOP();
1641 PyTypeObject *type = Py_TYPE(obj);
1642
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001643 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001644 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001645 }
Yury Selivanov75445082015-05-11 22:57:16 -04001646
1647 if (getter != NULL) {
1648 iter = (*getter)(obj);
1649 Py_DECREF(obj);
1650 if (iter == NULL) {
1651 SET_TOP(NULL);
1652 goto error;
1653 }
1654 }
1655 else {
1656 SET_TOP(NULL);
1657 PyErr_Format(
1658 PyExc_TypeError,
1659 "'async for' requires an object with "
1660 "__aiter__ method, got %.100s",
1661 type->tp_name);
1662 Py_DECREF(obj);
1663 goto error;
1664 }
1665
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001666 if (Py_TYPE(iter)->tp_as_async == NULL ||
1667 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001668
Yury Selivanov398ff912017-03-02 22:20:00 -05001669 SET_TOP(NULL);
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001670 PyErr_Format(
1671 PyExc_TypeError,
1672 "'async for' received an object from __aiter__ "
1673 "that does not implement __anext__: %.100s",
1674 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001675 Py_DECREF(iter);
1676 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001677 }
1678
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001679 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001680 DISPATCH();
1681 }
1682
1683 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001684 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001685 PyObject *next_iter = NULL;
1686 PyObject *awaitable = NULL;
1687 PyObject *aiter = TOP();
1688 PyTypeObject *type = Py_TYPE(aiter);
1689
Yury Selivanoveb636452016-09-08 22:01:51 -07001690 if (PyAsyncGen_CheckExact(aiter)) {
1691 awaitable = type->tp_as_async->am_anext(aiter);
1692 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001693 goto error;
1694 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001695 } else {
1696 if (type->tp_as_async != NULL){
1697 getter = type->tp_as_async->am_anext;
1698 }
Yury Selivanov75445082015-05-11 22:57:16 -04001699
Yury Selivanoveb636452016-09-08 22:01:51 -07001700 if (getter != NULL) {
1701 next_iter = (*getter)(aiter);
1702 if (next_iter == NULL) {
1703 goto error;
1704 }
1705 }
1706 else {
1707 PyErr_Format(
1708 PyExc_TypeError,
1709 "'async for' requires an iterator with "
1710 "__anext__ method, got %.100s",
1711 type->tp_name);
1712 goto error;
1713 }
Yury Selivanov75445082015-05-11 22:57:16 -04001714
Yury Selivanoveb636452016-09-08 22:01:51 -07001715 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1716 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001717 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001718 PyExc_TypeError,
1719 "'async for' received an invalid object "
1720 "from __anext__: %.100s",
1721 Py_TYPE(next_iter)->tp_name);
1722
1723 Py_DECREF(next_iter);
1724 goto error;
1725 } else {
1726 Py_DECREF(next_iter);
1727 }
1728 }
Yury Selivanov75445082015-05-11 22:57:16 -04001729
1730 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001731 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001732 DISPATCH();
1733 }
1734
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001735 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001736 TARGET(GET_AWAITABLE) {
1737 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001738 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001739
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03001740 if (iter == NULL) {
1741 format_awaitable_error(Py_TYPE(iterable),
1742 _Py_OPCODE(next_instr[-2]));
1743 }
1744
Yury Selivanov75445082015-05-11 22:57:16 -04001745 Py_DECREF(iterable);
1746
Yury Selivanovc724bae2016-03-02 11:30:46 -05001747 if (iter != NULL && PyCoro_CheckExact(iter)) {
1748 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1749 if (yf != NULL) {
1750 /* `iter` is a coroutine object that is being
1751 awaited, `yf` is a pointer to the current awaitable
1752 being awaited on. */
1753 Py_DECREF(yf);
1754 Py_CLEAR(iter);
1755 PyErr_SetString(
1756 PyExc_RuntimeError,
1757 "coroutine is being awaited already");
1758 /* The code below jumps to `error` if `iter` is NULL. */
1759 }
1760 }
1761
Yury Selivanov75445082015-05-11 22:57:16 -04001762 SET_TOP(iter); /* Even if it's NULL */
1763
1764 if (iter == NULL) {
1765 goto error;
1766 }
1767
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001768 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001769 DISPATCH();
1770 }
1771
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001772 TARGET(YIELD_FROM) {
1773 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001774 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001775 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001776 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1777 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001778 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001779 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001780 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001781 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001782 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001783 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001784 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001785 Py_DECREF(v);
1786 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001787 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001788 if (tstate->c_tracefunc != NULL
1789 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001790 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001791 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001792 if (err < 0)
1793 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001794 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001795 SET_TOP(val);
1796 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001797 }
Martin Panter95f53c12016-07-18 08:23:26 +00001798 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001799 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001800 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001801 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001802 f->f_lasti -= sizeof(_Py_CODEUNIT);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001803 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001804 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001805
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001806 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07001808
1809 if (co->co_flags & CO_ASYNC_GENERATOR) {
1810 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
1811 Py_DECREF(retval);
1812 if (w == NULL) {
1813 retval = NULL;
1814 goto error;
1815 }
1816 retval = w;
1817 }
1818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 f->f_stacktop = stack_pointer;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001820 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001821 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001822
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001823 TARGET(POP_EXCEPT) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001824 PyObject *type, *value, *traceback;
1825 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001826 PyTryBlock *b = PyFrame_BlockPop(f);
1827 if (b->b_type != EXCEPT_HANDLER) {
1828 PyErr_SetString(PyExc_SystemError,
1829 "popped block is not an except handler");
1830 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001831 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001832 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
1833 STACK_LEVEL() <= (b)->b_level + 4);
1834 exc_info = tstate->exc_info;
1835 type = exc_info->exc_type;
1836 value = exc_info->exc_value;
1837 traceback = exc_info->exc_traceback;
1838 exc_info->exc_type = POP();
1839 exc_info->exc_value = POP();
1840 exc_info->exc_traceback = POP();
1841 Py_XDECREF(type);
1842 Py_XDECREF(value);
1843 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001845 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001846
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001847 PREDICTED(POP_BLOCK);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001848 TARGET(POP_BLOCK) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001849 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001851 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001852
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001853 TARGET(POP_FINALLY) {
1854 /* If oparg is 0 at the top of the stack are 1 or 6 values:
1855 Either:
1856 - TOP = NULL or an integer
1857 or:
1858 - (TOP, SECOND, THIRD) = exc_info()
1859 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
1860
1861 If oparg is 1 the value for 'return' was additionally pushed
1862 at the top of the stack.
1863 */
1864 PyObject *res = NULL;
1865 if (oparg) {
1866 res = POP();
1867 }
1868 PyObject *exc = POP();
1869 if (exc == NULL || PyLong_CheckExact(exc)) {
1870 Py_XDECREF(exc);
1871 }
1872 else {
1873 Py_DECREF(exc);
1874 Py_DECREF(POP());
1875 Py_DECREF(POP());
1876
1877 PyObject *type, *value, *traceback;
1878 _PyErr_StackItem *exc_info;
1879 PyTryBlock *b = PyFrame_BlockPop(f);
1880 if (b->b_type != EXCEPT_HANDLER) {
1881 PyErr_SetString(PyExc_SystemError,
1882 "popped block is not an except handler");
1883 Py_XDECREF(res);
1884 goto error;
1885 }
1886 assert(STACK_LEVEL() == (b)->b_level + 3);
1887 exc_info = tstate->exc_info;
1888 type = exc_info->exc_type;
1889 value = exc_info->exc_value;
1890 traceback = exc_info->exc_traceback;
1891 exc_info->exc_type = POP();
1892 exc_info->exc_value = POP();
1893 exc_info->exc_traceback = POP();
1894 Py_XDECREF(type);
1895 Py_XDECREF(value);
1896 Py_XDECREF(traceback);
1897 }
1898 if (oparg) {
1899 PUSH(res);
1900 }
1901 DISPATCH();
1902 }
1903
1904 TARGET(CALL_FINALLY) {
1905 PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
1906 if (ret == NULL) {
1907 goto error;
1908 }
1909 PUSH(ret);
1910 JUMPBY(oparg);
1911 FAST_DISPATCH();
1912 }
1913
1914 TARGET(BEGIN_FINALLY) {
1915 /* Push NULL onto the stack for using it in END_FINALLY,
1916 POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH.
1917 */
1918 PUSH(NULL);
1919 FAST_DISPATCH();
1920 }
1921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001923 TARGET(END_FINALLY) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001924 /* At the top of the stack are 1 or 6 values:
1925 Either:
1926 - TOP = NULL or an integer
1927 or:
1928 - (TOP, SECOND, THIRD) = exc_info()
1929 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
1930 */
1931 PyObject *exc = POP();
1932 if (exc == NULL) {
1933 FAST_DISPATCH();
1934 }
1935 else if (PyLong_CheckExact(exc)) {
1936 int ret = _PyLong_AsInt(exc);
1937 Py_DECREF(exc);
1938 if (ret == -1 && PyErr_Occurred()) {
1939 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001941 JUMPTO(ret);
1942 FAST_DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001944 else {
1945 assert(PyExceptionClass_Check(exc));
1946 PyObject *val = POP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001947 PyObject *tb = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001948 PyErr_Restore(exc, val, tb);
1949 goto exception_unwind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001951 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001952
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02001953 TARGET(END_ASYNC_FOR) {
1954 PyObject *exc = POP();
1955 assert(PyExceptionClass_Check(exc));
1956 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
1957 PyTryBlock *b = PyFrame_BlockPop(f);
1958 assert(b->b_type == EXCEPT_HANDLER);
1959 Py_DECREF(exc);
1960 UNWIND_EXCEPT_HANDLER(b);
1961 Py_DECREF(POP());
1962 JUMPBY(oparg);
1963 FAST_DISPATCH();
1964 }
1965 else {
1966 PyObject *val = POP();
1967 PyObject *tb = POP();
1968 PyErr_Restore(exc, val, tb);
1969 goto exception_unwind;
1970 }
1971 }
1972
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001973 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02001974 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001975
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001976 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001977 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001978 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
1979 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02001980 PyErr_SetString(PyExc_NameError,
1981 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001982 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001983 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001984 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001985 }
1986 else {
1987 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
1988 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02001989 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001990 bc = PyObject_GetItem(f->f_builtins, build_class_str);
1991 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02001992 if (PyErr_ExceptionMatches(PyExc_KeyError))
1993 PyErr_SetString(PyExc_NameError,
1994 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001995 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001996 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001998 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04001999 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002000 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002001
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002002 TARGET(STORE_NAME) {
2003 PyObject *name = GETITEM(names, oparg);
2004 PyObject *v = POP();
2005 PyObject *ns = f->f_locals;
2006 int err;
2007 if (ns == NULL) {
2008 PyErr_Format(PyExc_SystemError,
2009 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002011 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002013 if (PyDict_CheckExact(ns))
2014 err = PyDict_SetItem(ns, name, v);
2015 else
2016 err = PyObject_SetItem(ns, name, v);
2017 Py_DECREF(v);
2018 if (err != 0)
2019 goto error;
2020 DISPATCH();
2021 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002022
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002023 TARGET(DELETE_NAME) {
2024 PyObject *name = GETITEM(names, oparg);
2025 PyObject *ns = f->f_locals;
2026 int err;
2027 if (ns == NULL) {
2028 PyErr_Format(PyExc_SystemError,
2029 "no locals when deleting %R", name);
2030 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002031 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002032 err = PyObject_DelItem(ns, name);
2033 if (err != 0) {
2034 format_exc_check_arg(PyExc_NameError,
2035 NAME_ERROR_MSG,
2036 name);
2037 goto error;
2038 }
2039 DISPATCH();
2040 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002041
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002042 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002043 TARGET(UNPACK_SEQUENCE) {
2044 PyObject *seq = POP(), *item, **items;
2045 if (PyTuple_CheckExact(seq) &&
2046 PyTuple_GET_SIZE(seq) == oparg) {
2047 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002049 item = items[oparg];
2050 Py_INCREF(item);
2051 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002052 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002053 } else if (PyList_CheckExact(seq) &&
2054 PyList_GET_SIZE(seq) == oparg) {
2055 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002056 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002057 item = items[oparg];
2058 Py_INCREF(item);
2059 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002060 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002061 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 stack_pointer + oparg)) {
2063 STACKADJ(oparg);
2064 } else {
2065 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002066 Py_DECREF(seq);
2067 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002068 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002069 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002070 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002072
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002073 TARGET(UNPACK_EX) {
2074 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2075 PyObject *seq = POP();
2076
2077 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2078 stack_pointer + totalargs)) {
2079 stack_pointer += totalargs;
2080 } else {
2081 Py_DECREF(seq);
2082 goto error;
2083 }
2084 Py_DECREF(seq);
2085 DISPATCH();
2086 }
2087
2088 TARGET(STORE_ATTR) {
2089 PyObject *name = GETITEM(names, oparg);
2090 PyObject *owner = TOP();
2091 PyObject *v = SECOND();
2092 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002093 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002094 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002096 Py_DECREF(owner);
2097 if (err != 0)
2098 goto error;
2099 DISPATCH();
2100 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002101
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002102 TARGET(DELETE_ATTR) {
2103 PyObject *name = GETITEM(names, oparg);
2104 PyObject *owner = POP();
2105 int err;
2106 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2107 Py_DECREF(owner);
2108 if (err != 0)
2109 goto error;
2110 DISPATCH();
2111 }
2112
2113 TARGET(STORE_GLOBAL) {
2114 PyObject *name = GETITEM(names, oparg);
2115 PyObject *v = POP();
2116 int err;
2117 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002118 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002119 if (err != 0)
2120 goto error;
2121 DISPATCH();
2122 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002123
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002124 TARGET(DELETE_GLOBAL) {
2125 PyObject *name = GETITEM(names, oparg);
2126 int err;
2127 err = PyDict_DelItem(f->f_globals, name);
2128 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002129 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002130 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002131 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002132 }
2133 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002134 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002135
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002136 TARGET(LOAD_NAME) {
2137 PyObject *name = GETITEM(names, oparg);
2138 PyObject *locals = f->f_locals;
2139 PyObject *v;
2140 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002141 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002142 "no locals when loading %R", name);
2143 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002144 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002145 if (PyDict_CheckExact(locals)) {
2146 v = PyDict_GetItem(locals, name);
2147 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148 }
2149 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002150 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002151 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002152 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2153 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 PyErr_Clear();
2155 }
2156 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002157 if (v == NULL) {
2158 v = PyDict_GetItem(f->f_globals, name);
2159 Py_XINCREF(v);
2160 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002161 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002162 v = PyDict_GetItem(f->f_builtins, name);
2163 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002164 format_exc_check_arg(
2165 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002166 NAME_ERROR_MSG, name);
2167 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002168 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002169 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002170 }
2171 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002172 v = PyObject_GetItem(f->f_builtins, name);
2173 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002174 if (PyErr_ExceptionMatches(PyExc_KeyError))
2175 format_exc_check_arg(
2176 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002177 NAME_ERROR_MSG, name);
2178 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002179 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002180 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002183 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002185 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002186
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002187 TARGET(LOAD_GLOBAL) {
2188 PyObject *name = GETITEM(names, oparg);
2189 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002190 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002191 && PyDict_CheckExact(f->f_builtins))
2192 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002193 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002194 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002195 name);
2196 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002197 if (!_PyErr_OCCURRED()) {
2198 /* _PyDict_LoadGlobal() returns NULL without raising
2199 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002200 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002201 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002202 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002203 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002204 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002205 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002206 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002207 else {
2208 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002209
2210 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002211 v = PyObject_GetItem(f->f_globals, name);
2212 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002213 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2214 goto error;
2215 PyErr_Clear();
2216
Victor Stinnerb4efc962015-11-20 09:24:02 +01002217 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002218 v = PyObject_GetItem(f->f_builtins, name);
2219 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002220 if (PyErr_ExceptionMatches(PyExc_KeyError))
2221 format_exc_check_arg(
2222 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002223 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002224 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002225 }
2226 }
2227 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002228 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002229 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002230 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002231
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002232 TARGET(DELETE_FAST) {
2233 PyObject *v = GETLOCAL(oparg);
2234 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 SETLOCAL(oparg, NULL);
2236 DISPATCH();
2237 }
2238 format_exc_check_arg(
2239 PyExc_UnboundLocalError,
2240 UNBOUNDLOCAL_ERROR_MSG,
2241 PyTuple_GetItem(co->co_varnames, oparg)
2242 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002243 goto error;
2244 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002245
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002246 TARGET(DELETE_DEREF) {
2247 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002248 PyObject *oldobj = PyCell_GET(cell);
2249 if (oldobj != NULL) {
2250 PyCell_SET(cell, NULL);
2251 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002252 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002253 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002254 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002255 goto error;
2256 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002257
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002258 TARGET(LOAD_CLOSURE) {
2259 PyObject *cell = freevars[oparg];
2260 Py_INCREF(cell);
2261 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002263 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002264
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002265 TARGET(LOAD_CLASSDEREF) {
2266 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002267 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002268 assert(locals);
2269 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2270 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2271 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2272 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2273 if (PyDict_CheckExact(locals)) {
2274 value = PyDict_GetItem(locals, name);
2275 Py_XINCREF(value);
2276 }
2277 else {
2278 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002279 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002280 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2281 goto error;
2282 PyErr_Clear();
2283 }
2284 }
2285 if (!value) {
2286 PyObject *cell = freevars[oparg];
2287 value = PyCell_GET(cell);
2288 if (value == NULL) {
2289 format_exc_unbound(co, oparg);
2290 goto error;
2291 }
2292 Py_INCREF(value);
2293 }
2294 PUSH(value);
2295 DISPATCH();
2296 }
2297
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002298 TARGET(LOAD_DEREF) {
2299 PyObject *cell = freevars[oparg];
2300 PyObject *value = PyCell_GET(cell);
2301 if (value == NULL) {
2302 format_exc_unbound(co, oparg);
2303 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002304 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002305 Py_INCREF(value);
2306 PUSH(value);
2307 DISPATCH();
2308 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002309
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002310 TARGET(STORE_DEREF) {
2311 PyObject *v = POP();
2312 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002313 PyObject *oldobj = PyCell_GET(cell);
2314 PyCell_SET(cell, v);
2315 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002316 DISPATCH();
2317 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002318
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002319 TARGET(BUILD_STRING) {
2320 PyObject *str;
2321 PyObject *empty = PyUnicode_New(0, 0);
2322 if (empty == NULL) {
2323 goto error;
2324 }
2325 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2326 Py_DECREF(empty);
2327 if (str == NULL)
2328 goto error;
2329 while (--oparg >= 0) {
2330 PyObject *item = POP();
2331 Py_DECREF(item);
2332 }
2333 PUSH(str);
2334 DISPATCH();
2335 }
2336
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002337 TARGET(BUILD_TUPLE) {
2338 PyObject *tup = PyTuple_New(oparg);
2339 if (tup == NULL)
2340 goto error;
2341 while (--oparg >= 0) {
2342 PyObject *item = POP();
2343 PyTuple_SET_ITEM(tup, oparg, item);
2344 }
2345 PUSH(tup);
2346 DISPATCH();
2347 }
2348
2349 TARGET(BUILD_LIST) {
2350 PyObject *list = PyList_New(oparg);
2351 if (list == NULL)
2352 goto error;
2353 while (--oparg >= 0) {
2354 PyObject *item = POP();
2355 PyList_SET_ITEM(list, oparg, item);
2356 }
2357 PUSH(list);
2358 DISPATCH();
2359 }
2360
Serhiy Storchaka73442852016-10-02 10:33:46 +03002361 TARGET(BUILD_TUPLE_UNPACK_WITH_CALL)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002362 TARGET(BUILD_TUPLE_UNPACK)
2363 TARGET(BUILD_LIST_UNPACK) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002364 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002365 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002366 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002367 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002368
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002369 if (sum == NULL)
2370 goto error;
2371
2372 for (i = oparg; i > 0; i--) {
2373 PyObject *none_val;
2374
2375 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2376 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002377 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002378 PyErr_ExceptionMatches(PyExc_TypeError))
2379 {
2380 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002381 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002382 Py_DECREF(sum);
2383 goto error;
2384 }
2385 Py_DECREF(none_val);
2386 }
2387
2388 if (convert_to_tuple) {
2389 return_value = PyList_AsTuple(sum);
2390 Py_DECREF(sum);
2391 if (return_value == NULL)
2392 goto error;
2393 }
2394 else {
2395 return_value = sum;
2396 }
2397
2398 while (oparg--)
2399 Py_DECREF(POP());
2400 PUSH(return_value);
2401 DISPATCH();
2402 }
2403
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002404 TARGET(BUILD_SET) {
2405 PyObject *set = PySet_New(NULL);
2406 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002407 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002408 if (set == NULL)
2409 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002410 for (i = oparg; i > 0; i--) {
2411 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002412 if (err == 0)
2413 err = PySet_Add(set, item);
2414 Py_DECREF(item);
2415 }
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002416 STACKADJ(-oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002417 if (err != 0) {
2418 Py_DECREF(set);
2419 goto error;
2420 }
2421 PUSH(set);
2422 DISPATCH();
2423 }
2424
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002425 TARGET(BUILD_SET_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002426 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002427 PyObject *sum = PySet_New(NULL);
2428 if (sum == NULL)
2429 goto error;
2430
2431 for (i = oparg; i > 0; i--) {
2432 if (_PySet_Update(sum, PEEK(i)) < 0) {
2433 Py_DECREF(sum);
2434 goto error;
2435 }
2436 }
2437
2438 while (oparg--)
2439 Py_DECREF(POP());
2440 PUSH(sum);
2441 DISPATCH();
2442 }
2443
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002444 TARGET(BUILD_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002445 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002446 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2447 if (map == NULL)
2448 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002449 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002450 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002451 PyObject *key = PEEK(2*i);
2452 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002453 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002454 if (err != 0) {
2455 Py_DECREF(map);
2456 goto error;
2457 }
2458 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002459
2460 while (oparg--) {
2461 Py_DECREF(POP());
2462 Py_DECREF(POP());
2463 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002464 PUSH(map);
2465 DISPATCH();
2466 }
2467
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002468 TARGET(SETUP_ANNOTATIONS) {
2469 _Py_IDENTIFIER(__annotations__);
2470 int err;
2471 PyObject *ann_dict;
2472 if (f->f_locals == NULL) {
2473 PyErr_Format(PyExc_SystemError,
2474 "no locals found when setting up annotations");
2475 goto error;
2476 }
2477 /* check if __annotations__ in locals()... */
2478 if (PyDict_CheckExact(f->f_locals)) {
2479 ann_dict = _PyDict_GetItemId(f->f_locals,
2480 &PyId___annotations__);
2481 if (ann_dict == NULL) {
2482 /* ...if not, create a new one */
2483 ann_dict = PyDict_New();
2484 if (ann_dict == NULL) {
2485 goto error;
2486 }
2487 err = _PyDict_SetItemId(f->f_locals,
2488 &PyId___annotations__, ann_dict);
2489 Py_DECREF(ann_dict);
2490 if (err != 0) {
2491 goto error;
2492 }
2493 }
2494 }
2495 else {
2496 /* do the same if locals() is not a dict */
2497 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2498 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002499 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002500 }
2501 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2502 if (ann_dict == NULL) {
2503 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2504 goto error;
2505 }
2506 PyErr_Clear();
2507 ann_dict = PyDict_New();
2508 if (ann_dict == NULL) {
2509 goto error;
2510 }
2511 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2512 Py_DECREF(ann_dict);
2513 if (err != 0) {
2514 goto error;
2515 }
2516 }
2517 else {
2518 Py_DECREF(ann_dict);
2519 }
2520 }
2521 DISPATCH();
2522 }
2523
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002524 TARGET(BUILD_CONST_KEY_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002525 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002526 PyObject *map;
2527 PyObject *keys = TOP();
2528 if (!PyTuple_CheckExact(keys) ||
2529 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2530 PyErr_SetString(PyExc_SystemError,
2531 "bad BUILD_CONST_KEY_MAP keys argument");
2532 goto error;
2533 }
2534 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2535 if (map == NULL) {
2536 goto error;
2537 }
2538 for (i = oparg; i > 0; i--) {
2539 int err;
2540 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2541 PyObject *value = PEEK(i + 1);
2542 err = PyDict_SetItem(map, key, value);
2543 if (err != 0) {
2544 Py_DECREF(map);
2545 goto error;
2546 }
2547 }
2548
2549 Py_DECREF(POP());
2550 while (oparg--) {
2551 Py_DECREF(POP());
2552 }
2553 PUSH(map);
2554 DISPATCH();
2555 }
2556
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002557 TARGET(BUILD_MAP_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002558 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002559 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002560 if (sum == NULL)
2561 goto error;
2562
2563 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002564 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002565 if (PyDict_Update(sum, arg) < 0) {
2566 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2567 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002568 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002569 arg->ob_type->tp_name);
2570 }
2571 Py_DECREF(sum);
2572 goto error;
2573 }
2574 }
2575
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002576 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002577 Py_DECREF(POP());
2578 PUSH(sum);
2579 DISPATCH();
2580 }
2581
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002582 TARGET(BUILD_MAP_UNPACK_WITH_CALL) {
2583 Py_ssize_t i;
2584 PyObject *sum = PyDict_New();
2585 if (sum == NULL)
2586 goto error;
2587
2588 for (i = oparg; i > 0; i--) {
2589 PyObject *arg = PEEK(i);
2590 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
2591 PyObject *func = PEEK(2 + oparg);
2592 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002593 format_kwargs_mapping_error(func, arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002594 }
2595 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2596 PyObject *exc, *val, *tb;
2597 PyErr_Fetch(&exc, &val, &tb);
2598 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
2599 PyObject *key = PyTuple_GET_ITEM(val, 0);
2600 if (!PyUnicode_Check(key)) {
2601 PyErr_Format(PyExc_TypeError,
2602 "%.200s%.200s keywords must be strings",
2603 PyEval_GetFuncName(func),
2604 PyEval_GetFuncDesc(func));
2605 } else {
2606 PyErr_Format(PyExc_TypeError,
2607 "%.200s%.200s got multiple "
2608 "values for keyword argument '%U'",
2609 PyEval_GetFuncName(func),
2610 PyEval_GetFuncDesc(func),
2611 key);
2612 }
2613 Py_XDECREF(exc);
2614 Py_XDECREF(val);
2615 Py_XDECREF(tb);
2616 }
2617 else {
2618 PyErr_Restore(exc, val, tb);
2619 }
2620 }
2621 Py_DECREF(sum);
2622 goto error;
2623 }
2624 }
2625
2626 while (oparg--)
2627 Py_DECREF(POP());
2628 PUSH(sum);
2629 DISPATCH();
2630 }
2631
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002632 TARGET(MAP_ADD) {
2633 PyObject *key = TOP();
2634 PyObject *value = SECOND();
2635 PyObject *map;
2636 int err;
2637 STACKADJ(-2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002638 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002639 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002640 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002641 Py_DECREF(value);
2642 Py_DECREF(key);
2643 if (err != 0)
2644 goto error;
2645 PREDICT(JUMP_ABSOLUTE);
2646 DISPATCH();
2647 }
2648
2649 TARGET(LOAD_ATTR) {
2650 PyObject *name = GETITEM(names, oparg);
2651 PyObject *owner = TOP();
2652 PyObject *res = PyObject_GetAttr(owner, name);
2653 Py_DECREF(owner);
2654 SET_TOP(res);
2655 if (res == NULL)
2656 goto error;
2657 DISPATCH();
2658 }
2659
2660 TARGET(COMPARE_OP) {
2661 PyObject *right = POP();
2662 PyObject *left = TOP();
2663 PyObject *res = cmp_outcome(oparg, left, right);
2664 Py_DECREF(left);
2665 Py_DECREF(right);
2666 SET_TOP(res);
2667 if (res == NULL)
2668 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002669 PREDICT(POP_JUMP_IF_FALSE);
2670 PREDICT(POP_JUMP_IF_TRUE);
2671 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002672 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002673
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002674 TARGET(IMPORT_NAME) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002675 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002676 PyObject *fromlist = POP();
2677 PyObject *level = TOP();
2678 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002679 res = import_name(f, name, fromlist, level);
2680 Py_DECREF(level);
2681 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002682 SET_TOP(res);
2683 if (res == NULL)
2684 goto error;
2685 DISPATCH();
2686 }
2687
2688 TARGET(IMPORT_STAR) {
2689 PyObject *from = POP(), *locals;
2690 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002691 if (PyFrame_FastToLocalsWithError(f) < 0) {
2692 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002693 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002694 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002695
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002696 locals = f->f_locals;
2697 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002698 PyErr_SetString(PyExc_SystemError,
2699 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002700 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002701 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002702 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002703 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002704 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002705 Py_DECREF(from);
2706 if (err != 0)
2707 goto error;
2708 DISPATCH();
2709 }
Guido van Rossum25831651993-05-19 14:50:45 +00002710
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002711 TARGET(IMPORT_FROM) {
2712 PyObject *name = GETITEM(names, oparg);
2713 PyObject *from = TOP();
2714 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002715 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002716 PUSH(res);
2717 if (res == NULL)
2718 goto error;
2719 DISPATCH();
2720 }
Thomas Wouters52152252000-08-17 22:55:00 +00002721
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002722 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002723 JUMPBY(oparg);
2724 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002725 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002726
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002727 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002728 TARGET(POP_JUMP_IF_FALSE) {
2729 PyObject *cond = POP();
2730 int err;
2731 if (cond == Py_True) {
2732 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002733 FAST_DISPATCH();
2734 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002735 if (cond == Py_False) {
2736 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002737 JUMPTO(oparg);
2738 FAST_DISPATCH();
2739 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002740 err = PyObject_IsTrue(cond);
2741 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002742 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002743 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002744 else if (err == 0)
2745 JUMPTO(oparg);
2746 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002747 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002748 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002749 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002750
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002751 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002752 TARGET(POP_JUMP_IF_TRUE) {
2753 PyObject *cond = POP();
2754 int err;
2755 if (cond == Py_False) {
2756 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 FAST_DISPATCH();
2758 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002759 if (cond == Py_True) {
2760 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002761 JUMPTO(oparg);
2762 FAST_DISPATCH();
2763 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002764 err = PyObject_IsTrue(cond);
2765 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002766 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002767 JUMPTO(oparg);
2768 }
2769 else if (err == 0)
2770 ;
2771 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002772 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002773 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002774 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002775
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002776 TARGET(JUMP_IF_FALSE_OR_POP) {
2777 PyObject *cond = TOP();
2778 int err;
2779 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002780 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002781 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002782 FAST_DISPATCH();
2783 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002784 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002785 JUMPTO(oparg);
2786 FAST_DISPATCH();
2787 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002788 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002789 if (err > 0) {
2790 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002791 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002792 }
2793 else if (err == 0)
2794 JUMPTO(oparg);
2795 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002796 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002797 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002798 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002799
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002800 TARGET(JUMP_IF_TRUE_OR_POP) {
2801 PyObject *cond = TOP();
2802 int err;
2803 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002804 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002805 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002806 FAST_DISPATCH();
2807 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002808 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002809 JUMPTO(oparg);
2810 FAST_DISPATCH();
2811 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002812 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002813 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 JUMPTO(oparg);
2815 }
2816 else if (err == 0) {
2817 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002818 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002819 }
2820 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002821 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002822 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002823 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002824
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002825 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002826 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002827 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002828#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002829 /* Enabling this path speeds-up all while and for-loops by bypassing
2830 the per-loop checks for signals. By default, this should be turned-off
2831 because it prevents detection of a control-break in tight loops like
2832 "while 1: pass". Compile with this option turned-on when you need
2833 the speed-up and do not need break checking inside tight loops (ones
2834 that contain only instructions ending with FAST_DISPATCH).
2835 */
2836 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002837#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002838 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002839#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002840 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002841
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002842 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002843 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002844 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002845 PyObject *iter = PyObject_GetIter(iterable);
2846 Py_DECREF(iterable);
2847 SET_TOP(iter);
2848 if (iter == NULL)
2849 goto error;
2850 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002851 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002852 DISPATCH();
2853 }
2854
2855 TARGET(GET_YIELD_FROM_ITER) {
2856 /* before: [obj]; after [getiter(obj)] */
2857 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002858 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002859 if (PyCoro_CheckExact(iterable)) {
2860 /* `iterable` is a coroutine */
2861 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2862 /* and it is used in a 'yield from' expression of a
2863 regular generator. */
2864 Py_DECREF(iterable);
2865 SET_TOP(NULL);
2866 PyErr_SetString(PyExc_TypeError,
2867 "cannot 'yield from' a coroutine object "
2868 "in a non-coroutine generator");
2869 goto error;
2870 }
2871 }
2872 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002873 /* `iterable` is not a generator. */
2874 iter = PyObject_GetIter(iterable);
2875 Py_DECREF(iterable);
2876 SET_TOP(iter);
2877 if (iter == NULL)
2878 goto error;
2879 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002880 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002881 DISPATCH();
2882 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002883
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002884 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002885 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002886 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002887 PyObject *iter = TOP();
2888 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2889 if (next != NULL) {
2890 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002891 PREDICT(STORE_FAST);
2892 PREDICT(UNPACK_SEQUENCE);
2893 DISPATCH();
2894 }
2895 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002896 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2897 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08002898 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002899 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002900 PyErr_Clear();
2901 }
2902 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002903 STACKADJ(-1);
2904 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002906 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002907 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002908 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002909
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002910 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002911 /* NOTE: If you add any new block-setup opcodes that
2912 are not try/except/finally handlers, you may need
2913 to update the PyGen_NeedsFinalizing() function.
2914 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002915
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002916 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 STACK_LEVEL());
2918 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002919 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002920
Yury Selivanov75445082015-05-11 22:57:16 -04002921 TARGET(BEFORE_ASYNC_WITH) {
2922 _Py_IDENTIFIER(__aexit__);
2923 _Py_IDENTIFIER(__aenter__);
2924
2925 PyObject *mgr = TOP();
2926 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
2927 *enter;
2928 PyObject *res;
2929 if (exit == NULL)
2930 goto error;
2931 SET_TOP(exit);
2932 enter = special_lookup(mgr, &PyId___aenter__);
2933 Py_DECREF(mgr);
2934 if (enter == NULL)
2935 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002936 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04002937 Py_DECREF(enter);
2938 if (res == NULL)
2939 goto error;
2940 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002941 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002942 DISPATCH();
2943 }
2944
2945 TARGET(SETUP_ASYNC_WITH) {
2946 PyObject *res = POP();
2947 /* Setup the finally block before pushing the result
2948 of __aenter__ on the stack. */
2949 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2950 STACK_LEVEL());
2951 PUSH(res);
2952 DISPATCH();
2953 }
2954
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002955 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05002956 _Py_IDENTIFIER(__exit__);
2957 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002958 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08002959 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002960 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08002961 if (enter == NULL)
2962 goto error;
2963 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08002964 if (exit == NULL) {
2965 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002966 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08002967 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002968 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002969 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002970 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002971 Py_DECREF(enter);
2972 if (res == NULL)
2973 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002974 /* Setup the finally block before pushing the result
2975 of __enter__ on the stack. */
2976 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2977 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002978
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002979 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002980 DISPATCH();
2981 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002982
Yury Selivanov75445082015-05-11 22:57:16 -04002983 TARGET(WITH_CLEANUP_START) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002984 /* At the top of the stack are 1 or 6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002985 how/why we entered the finally clause:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002986 - TOP = NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002987 - (TOP, SECOND, THIRD) = exc_info()
2988 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002989 Below them is EXIT, the context.__exit__ or context.__aexit__
2990 bound method.
2991 In the first case, we must call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002992 EXIT(None, None, None)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002993 otherwise we must call
2994 EXIT(TOP, SECOND, THIRD)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002995
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002996 In the first case, we remove EXIT from the
2997 stack, leaving TOP, and push TOP on the stack.
2998 Otherwise we shift the bottom 3 values of the
2999 stack down, replace the empty spot with NULL, and push
3000 None on the stack.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003001
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003002 Finally we push the result of the call.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003003 */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003004 PyObject *stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003005 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003006 PyObject *exc, *val, *tb, *res;
3007
3008 val = tb = Py_None;
3009 exc = TOP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003010 if (exc == NULL) {
3011 STACKADJ(-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003012 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003013 SET_TOP(exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003014 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003015 }
3016 else {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003017 assert(PyExceptionClass_Check(exc));
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003018 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003019 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003020 val = SECOND();
3021 tb = THIRD();
3022 tp2 = FOURTH();
3023 exc2 = PEEK(5);
3024 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003025 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003026 SET_VALUE(7, tb2);
3027 SET_VALUE(6, exc2);
3028 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003029 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3030 SET_FOURTH(NULL);
3031 /* We just shifted the stack down, so we have
3032 to tell the except handler block that the
3033 values are lower than it expects. */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003034 assert(f->f_iblock > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003035 block = &f->f_blockstack[f->f_iblock - 1];
3036 assert(block->b_type == EXCEPT_HANDLER);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003037 assert(block->b_level > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003038 block->b_level--;
3039 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003040
3041 stack[0] = exc;
3042 stack[1] = val;
3043 stack[2] = tb;
3044 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003045 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003046 if (res == NULL)
3047 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003048
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003049 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003050 PUSH(exc);
3051 PUSH(res);
3052 PREDICT(WITH_CLEANUP_FINISH);
3053 DISPATCH();
3054 }
3055
3056 PREDICTED(WITH_CLEANUP_FINISH);
3057 TARGET(WITH_CLEANUP_FINISH) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003058 /* TOP = the result of calling the context.__exit__ bound method
3059 SECOND = either None or exception type
3060
3061 If SECOND is None below is NULL or the return address,
3062 otherwise below are 7 values representing an exception.
3063 */
Yury Selivanov75445082015-05-11 22:57:16 -04003064 PyObject *res = POP();
3065 PyObject *exc = POP();
3066 int err;
3067
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003068 if (exc != Py_None)
3069 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003070 else
3071 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003072
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003073 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003074 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003076 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003077 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003078 else if (err > 0) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003079 /* There was an exception and a True return.
3080 * We must manually unwind the EXCEPT_HANDLER block
3081 * which was created when the exception was caught,
3082 * otherwise the stack will be in an inconsisten state.
3083 */
3084 PyTryBlock *b = PyFrame_BlockPop(f);
3085 assert(b->b_type == EXCEPT_HANDLER);
3086 UNWIND_EXCEPT_HANDLER(b);
3087 PUSH(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088 }
3089 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003090 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003091 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003092
Yury Selivanovf2392132016-12-13 19:03:51 -05003093 TARGET(LOAD_METHOD) {
3094 /* Designed to work in tamdem with CALL_METHOD. */
3095 PyObject *name = GETITEM(names, oparg);
3096 PyObject *obj = TOP();
3097 PyObject *meth = NULL;
3098
3099 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3100
Yury Selivanovf2392132016-12-13 19:03:51 -05003101 if (meth == NULL) {
3102 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003103 goto error;
3104 }
3105
3106 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003107 /* We can bypass temporary bound method object.
3108 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003109
INADA Naoki015bce62017-01-16 17:23:30 +09003110 meth | self | arg1 | ... | argN
3111 */
3112 SET_TOP(meth);
3113 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003114 }
3115 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003116 /* meth is not an unbound method (but a regular attr, or
3117 something was returned by a descriptor protocol). Set
3118 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003119 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003120
3121 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003122 */
INADA Naoki015bce62017-01-16 17:23:30 +09003123 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003124 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003125 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003126 }
3127 DISPATCH();
3128 }
3129
3130 TARGET(CALL_METHOD) {
3131 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003132 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003133
3134 sp = stack_pointer;
3135
INADA Naoki015bce62017-01-16 17:23:30 +09003136 meth = PEEK(oparg + 2);
3137 if (meth == NULL) {
3138 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3139 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003140
3141 Stack layout:
3142
INADA Naoki015bce62017-01-16 17:23:30 +09003143 ... | NULL | callable | arg1 | ... | argN
3144 ^- TOP()
3145 ^- (-oparg)
3146 ^- (-oparg-1)
3147 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003148
Ville Skyttä49b27342017-08-03 09:00:59 +03003149 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003150 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003151 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003152 res = call_function(&sp, oparg, NULL);
3153 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003154 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003155 }
3156 else {
3157 /* This is a method call. Stack layout:
3158
INADA Naoki015bce62017-01-16 17:23:30 +09003159 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003160 ^- TOP()
3161 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003162 ^- (-oparg-1)
3163 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003164
INADA Naoki015bce62017-01-16 17:23:30 +09003165 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003166 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003167 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003168 */
3169 res = call_function(&sp, oparg + 1, NULL);
3170 stack_pointer = sp;
3171 }
3172
3173 PUSH(res);
3174 if (res == NULL)
3175 goto error;
3176 DISPATCH();
3177 }
3178
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003179 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003180 TARGET(CALL_FUNCTION) {
3181 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003182 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003183 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003184 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003185 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003186 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003187 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003188 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003189 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003190 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003191
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003192 TARGET(CALL_FUNCTION_KW) {
3193 PyObject **sp, *res, *names;
3194
3195 names = POP();
3196 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003197 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003198 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003199 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003200 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003201 Py_DECREF(names);
3202
3203 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003204 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003205 }
3206 DISPATCH();
3207 }
3208
3209 TARGET(CALL_FUNCTION_EX) {
3210 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003211 if (oparg & 0x01) {
3212 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003213 if (!PyDict_CheckExact(kwargs)) {
3214 PyObject *d = PyDict_New();
3215 if (d == NULL)
3216 goto error;
3217 if (PyDict_Update(d, kwargs) != 0) {
3218 Py_DECREF(d);
3219 /* PyDict_Update raises attribute
3220 * error (percolated from an attempt
3221 * to get 'keys' attribute) instead of
3222 * a type error if its second argument
3223 * is not a mapping.
3224 */
3225 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003226 format_kwargs_mapping_error(SECOND(), kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003227 }
Victor Stinnereece2222016-09-12 11:16:37 +02003228 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003229 goto error;
3230 }
3231 Py_DECREF(kwargs);
3232 kwargs = d;
3233 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003234 assert(PyDict_CheckExact(kwargs));
3235 }
3236 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003237 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003238 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003239 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003240 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003241 goto error;
3242 }
3243 Py_SETREF(callargs, PySequence_Tuple(callargs));
3244 if (callargs == NULL) {
3245 goto error;
3246 }
3247 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003248 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003249
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003250 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003251 Py_DECREF(func);
3252 Py_DECREF(callargs);
3253 Py_XDECREF(kwargs);
3254
3255 SET_TOP(result);
3256 if (result == NULL) {
3257 goto error;
3258 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003259 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003260 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003261
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003262 TARGET(MAKE_FUNCTION) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003263 PyObject *qualname = POP();
3264 PyObject *codeobj = POP();
3265 PyFunctionObject *func = (PyFunctionObject *)
3266 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003267
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003268 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003269 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003270 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003271 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003272 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003273
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003274 if (oparg & 0x08) {
3275 assert(PyTuple_CheckExact(TOP()));
3276 func ->func_closure = POP();
3277 }
3278 if (oparg & 0x04) {
3279 assert(PyDict_CheckExact(TOP()));
3280 func->func_annotations = POP();
3281 }
3282 if (oparg & 0x02) {
3283 assert(PyDict_CheckExact(TOP()));
3284 func->func_kwdefaults = POP();
3285 }
3286 if (oparg & 0x01) {
3287 assert(PyTuple_CheckExact(TOP()));
3288 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003289 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003290
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003291 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003292 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003293 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003294
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003295 TARGET(BUILD_SLICE) {
3296 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003297 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003298 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003299 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003300 step = NULL;
3301 stop = POP();
3302 start = TOP();
3303 slice = PySlice_New(start, stop, step);
3304 Py_DECREF(start);
3305 Py_DECREF(stop);
3306 Py_XDECREF(step);
3307 SET_TOP(slice);
3308 if (slice == NULL)
3309 goto error;
3310 DISPATCH();
3311 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003312
Eric V. Smitha78c7952015-11-03 12:45:05 -05003313 TARGET(FORMAT_VALUE) {
3314 /* Handles f-string value formatting. */
3315 PyObject *result;
3316 PyObject *fmt_spec;
3317 PyObject *value;
3318 PyObject *(*conv_fn)(PyObject *);
3319 int which_conversion = oparg & FVC_MASK;
3320 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3321
3322 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003323 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003324
3325 /* See if any conversion is specified. */
3326 switch (which_conversion) {
3327 case FVC_STR: conv_fn = PyObject_Str; break;
3328 case FVC_REPR: conv_fn = PyObject_Repr; break;
3329 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3330
3331 /* Must be 0 (meaning no conversion), since only four
3332 values are allowed by (oparg & FVC_MASK). */
3333 default: conv_fn = NULL; break;
3334 }
3335
3336 /* If there's a conversion function, call it and replace
3337 value with that result. Otherwise, just use value,
3338 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003339 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003340 result = conv_fn(value);
3341 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003342 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003343 Py_XDECREF(fmt_spec);
3344 goto error;
3345 }
3346 value = result;
3347 }
3348
3349 /* If value is a unicode object, and there's no fmt_spec,
3350 then we know the result of format(value) is value
3351 itself. In that case, skip calling format(). I plan to
3352 move this optimization in to PyObject_Format()
3353 itself. */
3354 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3355 /* Do nothing, just transfer ownership to result. */
3356 result = value;
3357 } else {
3358 /* Actually call format(). */
3359 result = PyObject_Format(value, fmt_spec);
3360 Py_DECREF(value);
3361 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003362 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003363 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003364 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003365 }
3366
Eric V. Smith135d5f42016-02-05 18:23:08 -05003367 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003368 DISPATCH();
3369 }
3370
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003371 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003372 int oldoparg = oparg;
3373 NEXTOPARG();
3374 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003375 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003376 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003377
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003378
Antoine Pitrou042b1282010-08-13 21:15:58 +00003379#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003380 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003381#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003382 default:
3383 fprintf(stderr,
3384 "XXX lineno: %d, opcode: %d\n",
3385 PyFrame_GetLineNumber(f),
3386 opcode);
3387 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003388 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003390 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003391
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003392 /* This should never be reached. Every opcode should end with DISPATCH()
3393 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003394 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003395
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003396error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003397 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003398#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003399 if (!PyErr_Occurred())
3400 PyErr_SetString(PyExc_SystemError,
3401 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003402#else
3403 assert(PyErr_Occurred());
3404#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003405
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003406 /* Log traceback info. */
3407 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003408
Benjamin Peterson51f46162013-01-23 08:38:47 -05003409 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003410 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3411 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003412
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003413exception_unwind:
3414 /* Unwind stacks if an exception occurred */
3415 while (f->f_iblock > 0) {
3416 /* Pop the current block. */
3417 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003419 if (b->b_type == EXCEPT_HANDLER) {
3420 UNWIND_EXCEPT_HANDLER(b);
3421 continue;
3422 }
3423 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003424 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003425 PyObject *exc, *val, *tb;
3426 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003427 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003428 /* Beware, this invalidates all b->b_* fields */
3429 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003430 PUSH(exc_info->exc_traceback);
3431 PUSH(exc_info->exc_value);
3432 if (exc_info->exc_type != NULL) {
3433 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003434 }
3435 else {
3436 Py_INCREF(Py_None);
3437 PUSH(Py_None);
3438 }
3439 PyErr_Fetch(&exc, &val, &tb);
3440 /* Make the raw exception data
3441 available to the handler,
3442 so a program can emulate the
3443 Python main loop. */
3444 PyErr_NormalizeException(
3445 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003446 if (tb != NULL)
3447 PyException_SetTraceback(val, tb);
3448 else
3449 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003450 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003451 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003452 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003453 exc_info->exc_value = val;
3454 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003455 if (tb == NULL)
3456 tb = Py_None;
3457 Py_INCREF(tb);
3458 PUSH(tb);
3459 PUSH(val);
3460 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003461 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003462 /* Resume normal execution */
3463 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003464 }
3465 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003466
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003467 /* End the loop as we still have an error */
3468 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003469 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003471 /* Pop remaining stack entries. */
3472 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003473 PyObject *o = POP();
3474 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003475 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003476
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003477 assert(retval == NULL);
3478 assert(PyErr_Occurred());
Guido van Rossumac7be682001-01-17 15:42:30 +00003479
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003480return_or_yield:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003481 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003482 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003483 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3484 tstate, f, PyTrace_RETURN, retval)) {
3485 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003486 }
3487 }
3488 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003489 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3490 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003491 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003492 }
3493 }
3494 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003496 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003497exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003498 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3499 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003500 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003501 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003502 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003503
Victor Stinnerefde1462015-03-21 15:04:43 +01003504 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003505}
3506
Benjamin Petersonb204a422011-06-05 22:04:07 -05003507static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003508format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3509{
3510 int err;
3511 Py_ssize_t len = PyList_GET_SIZE(names);
3512 PyObject *name_str, *comma, *tail, *tmp;
3513
3514 assert(PyList_CheckExact(names));
3515 assert(len >= 1);
3516 /* Deal with the joys of natural language. */
3517 switch (len) {
3518 case 1:
3519 name_str = PyList_GET_ITEM(names, 0);
3520 Py_INCREF(name_str);
3521 break;
3522 case 2:
3523 name_str = PyUnicode_FromFormat("%U and %U",
3524 PyList_GET_ITEM(names, len - 2),
3525 PyList_GET_ITEM(names, len - 1));
3526 break;
3527 default:
3528 tail = PyUnicode_FromFormat(", %U, and %U",
3529 PyList_GET_ITEM(names, len - 2),
3530 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003531 if (tail == NULL)
3532 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003533 /* Chop off the last two objects in the list. This shouldn't actually
3534 fail, but we can't be too careful. */
3535 err = PyList_SetSlice(names, len - 2, len, NULL);
3536 if (err == -1) {
3537 Py_DECREF(tail);
3538 return;
3539 }
3540 /* Stitch everything up into a nice comma-separated list. */
3541 comma = PyUnicode_FromString(", ");
3542 if (comma == NULL) {
3543 Py_DECREF(tail);
3544 return;
3545 }
3546 tmp = PyUnicode_Join(comma, names);
3547 Py_DECREF(comma);
3548 if (tmp == NULL) {
3549 Py_DECREF(tail);
3550 return;
3551 }
3552 name_str = PyUnicode_Concat(tmp, tail);
3553 Py_DECREF(tmp);
3554 Py_DECREF(tail);
3555 break;
3556 }
3557 if (name_str == NULL)
3558 return;
3559 PyErr_Format(PyExc_TypeError,
3560 "%U() missing %i required %s argument%s: %U",
3561 co->co_name,
3562 len,
3563 kind,
3564 len == 1 ? "" : "s",
3565 name_str);
3566 Py_DECREF(name_str);
3567}
3568
3569static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003570missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003571 PyObject **fastlocals)
3572{
Victor Stinner74319ae2016-08-25 00:04:09 +02003573 Py_ssize_t i, j = 0;
3574 Py_ssize_t start, end;
3575 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003576 const char *kind = positional ? "positional" : "keyword-only";
3577 PyObject *missing_names;
3578
3579 /* Compute the names of the arguments that are missing. */
3580 missing_names = PyList_New(missing);
3581 if (missing_names == NULL)
3582 return;
3583 if (positional) {
3584 start = 0;
3585 end = co->co_argcount - defcount;
3586 }
3587 else {
3588 start = co->co_argcount;
3589 end = start + co->co_kwonlyargcount;
3590 }
3591 for (i = start; i < end; i++) {
3592 if (GETLOCAL(i) == NULL) {
3593 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3594 PyObject *name = PyObject_Repr(raw);
3595 if (name == NULL) {
3596 Py_DECREF(missing_names);
3597 return;
3598 }
3599 PyList_SET_ITEM(missing_names, j++, name);
3600 }
3601 }
3602 assert(j == missing);
3603 format_missing(kind, co, missing_names);
3604 Py_DECREF(missing_names);
3605}
3606
3607static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003608too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3609 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003610{
3611 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003612 Py_ssize_t kwonly_given = 0;
3613 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003614 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003615 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003616
Benjamin Petersone109c702011-06-24 09:37:26 -05003617 assert((co->co_flags & CO_VARARGS) == 0);
3618 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003619 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3620 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003621 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003622 }
3623 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003624 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003625 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003626 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003627 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003628 }
3629 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003630 plural = (co_argcount != 1);
3631 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003632 }
3633 if (sig == NULL)
3634 return;
3635 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003636 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3637 kwonly_sig = PyUnicode_FromFormat(format,
3638 given != 1 ? "s" : "",
3639 kwonly_given,
3640 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003641 if (kwonly_sig == NULL) {
3642 Py_DECREF(sig);
3643 return;
3644 }
3645 }
3646 else {
3647 /* This will not fail. */
3648 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003649 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003650 }
3651 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003652 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003653 co->co_name,
3654 sig,
3655 plural ? "s" : "",
3656 given,
3657 kwonly_sig,
3658 given == 1 && !kwonly_given ? "was" : "were");
3659 Py_DECREF(sig);
3660 Py_DECREF(kwonly_sig);
3661}
3662
Guido van Rossumc2e20742006-02-27 22:32:47 +00003663/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003664 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003665 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003666
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003667PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003668_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003669 PyObject *const *args, Py_ssize_t argcount,
3670 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003671 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003672 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003673 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003674 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003675{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003676 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003677 PyFrameObject *f;
3678 PyObject *retval = NULL;
3679 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003680 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003681 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003682 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3683 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003684 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003686 if (globals == NULL) {
3687 PyErr_SetString(PyExc_SystemError,
3688 "PyEval_EvalCodeEx: NULL globals");
3689 return NULL;
3690 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003691
Victor Stinnerc7020012016-08-16 23:40:29 +02003692 /* Create the frame */
3693 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003694 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003695 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003696 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003697 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003698 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003699 fastlocals = f->f_localsplus;
3700 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003701
Victor Stinnerc7020012016-08-16 23:40:29 +02003702 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003703 if (co->co_flags & CO_VARKEYWORDS) {
3704 kwdict = PyDict_New();
3705 if (kwdict == NULL)
3706 goto fail;
3707 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003708 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003709 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003710 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003711 SETLOCAL(i, kwdict);
3712 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003713 else {
3714 kwdict = NULL;
3715 }
3716
3717 /* Copy positional arguments into local variables */
3718 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003719 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003720 }
3721 else {
3722 n = argcount;
3723 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003724 for (i = 0; i < n; i++) {
3725 x = args[i];
3726 Py_INCREF(x);
3727 SETLOCAL(i, x);
3728 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003729
3730 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003731 if (co->co_flags & CO_VARARGS) {
3732 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003733 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003734 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003735 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003736 SETLOCAL(total_args, u);
3737 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003738 x = args[i];
3739 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003740 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003741 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003742 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003743
Serhiy Storchakab7281052016-09-12 00:52:40 +03003744 /* Handle keyword arguments passed as two strided arrays */
3745 kwcount *= kwstep;
3746 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003747 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003748 PyObject *keyword = kwnames[i];
3749 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003750 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003751
Benjamin Petersonb204a422011-06-05 22:04:07 -05003752 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3753 PyErr_Format(PyExc_TypeError,
3754 "%U() keywords must be strings",
3755 co->co_name);
3756 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003757 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003758
Benjamin Petersonb204a422011-06-05 22:04:07 -05003759 /* Speed hack: do raw pointer compares. As names are
3760 normally interned this should almost always hit. */
3761 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3762 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003763 PyObject *name = co_varnames[j];
3764 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003765 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003766 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003767 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003768
Benjamin Petersonb204a422011-06-05 22:04:07 -05003769 /* Slow fallback, just in case */
3770 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003771 PyObject *name = co_varnames[j];
3772 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3773 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003774 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003775 }
3776 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003777 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003778 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003779 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003780
Victor Stinner231d1f32017-01-11 02:12:06 +01003781 assert(j >= total_args);
3782 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003783 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003784 "%U() got an unexpected keyword argument '%S'",
3785 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003786 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003787 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003788
Christian Heimes0bd447f2013-07-20 14:48:10 +02003789 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3790 goto fail;
3791 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003792 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003793
Benjamin Petersonb204a422011-06-05 22:04:07 -05003794 kw_found:
3795 if (GETLOCAL(j) != NULL) {
3796 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003797 "%U() got multiple values for argument '%S'",
3798 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003799 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003800 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003801 Py_INCREF(value);
3802 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003803 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003804
3805 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003806 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003807 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003808 goto fail;
3809 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003810
3811 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003812 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02003813 Py_ssize_t m = co->co_argcount - defcount;
3814 Py_ssize_t missing = 0;
3815 for (i = argcount; i < m; i++) {
3816 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003817 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02003818 }
3819 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003820 if (missing) {
3821 missing_arguments(co, missing, defcount, fastlocals);
3822 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003823 }
3824 if (n > m)
3825 i = n - m;
3826 else
3827 i = 0;
3828 for (; i < defcount; i++) {
3829 if (GETLOCAL(m+i) == NULL) {
3830 PyObject *def = defs[i];
3831 Py_INCREF(def);
3832 SETLOCAL(m+i, def);
3833 }
3834 }
3835 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003836
3837 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003838 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02003839 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003840 for (i = co->co_argcount; i < total_args; i++) {
3841 PyObject *name;
3842 if (GETLOCAL(i) != NULL)
3843 continue;
3844 name = PyTuple_GET_ITEM(co->co_varnames, i);
3845 if (kwdefs != NULL) {
3846 PyObject *def = PyDict_GetItem(kwdefs, name);
3847 if (def) {
3848 Py_INCREF(def);
3849 SETLOCAL(i, def);
3850 continue;
3851 }
3852 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003853 missing++;
3854 }
3855 if (missing) {
3856 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003857 goto fail;
3858 }
3859 }
3860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003861 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003862 vars into frame. */
3863 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003864 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02003865 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05003866 /* Possibly account for the cell variable being an argument. */
3867 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003868 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003869 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003870 /* Clear the local copy. */
3871 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003872 }
3873 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003874 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003875 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003876 if (c == NULL)
3877 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003878 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003879 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003880
3881 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05003882 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3883 PyObject *o = PyTuple_GET_ITEM(closure, i);
3884 Py_INCREF(o);
3885 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003886 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003887
Yury Selivanoveb636452016-09-08 22:01:51 -07003888 /* Handle generator/coroutine/asynchronous generator */
3889 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003890 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04003891 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003892 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04003893
3894 if (is_coro && tstate->in_coroutine_wrapper) {
3895 assert(coro_wrapper != NULL);
3896 PyErr_Format(PyExc_RuntimeError,
3897 "coroutine wrapper %.200R attempted "
3898 "to recursively wrap %.200R",
3899 coro_wrapper,
3900 co);
3901 goto fail;
3902 }
Yury Selivanov75445082015-05-11 22:57:16 -04003903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003904 /* Don't need to keep the reference to f_back, it will be set
3905 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003906 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003908 /* Create a new generator that owns the ready to run frame
3909 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04003910 if (is_coro) {
3911 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07003912 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
3913 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003914 } else {
3915 gen = PyGen_NewWithQualName(f, name, qualname);
3916 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003917 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003918 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003919 }
INADA Naoki9c157762016-12-26 18:52:46 +09003920
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003921 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04003922
Yury Selivanov94c22632015-06-04 10:16:51 -04003923 if (is_coro && coro_wrapper != NULL) {
3924 PyObject *wrapped;
3925 tstate->in_coroutine_wrapper = 1;
3926 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
3927 tstate->in_coroutine_wrapper = 0;
3928 return wrapped;
3929 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04003930
Yury Selivanov75445082015-05-11 22:57:16 -04003931 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003932 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003933
Victor Stinner59a73272016-12-09 18:51:13 +01003934 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003935
Thomas Woutersce272b62007-09-19 21:19:28 +00003936fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003938 /* decref'ing the frame can cause __del__ methods to get invoked,
3939 which can call back into Python. While we're done with the
3940 current Python frame (f), the associated C stack is still in use,
3941 so recursion_depth must be boosted for the duration.
3942 */
3943 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003944 if (Py_REFCNT(f) > 1) {
3945 Py_DECREF(f);
3946 _PyObject_GC_TRACK(f);
3947 }
3948 else {
3949 ++tstate->recursion_depth;
3950 Py_DECREF(f);
3951 --tstate->recursion_depth;
3952 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003953 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003954}
3955
Victor Stinner40ee3012014-06-16 15:59:28 +02003956PyObject *
3957PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003958 PyObject *const *args, int argcount,
3959 PyObject *const *kws, int kwcount,
3960 PyObject *const *defs, int defcount,
3961 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02003962{
3963 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02003964 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06003965 kws, kws != NULL ? kws + 1 : NULL,
3966 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02003967 defs, defcount,
3968 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003969 NULL, NULL);
3970}
Tim Peters5ca576e2001-06-18 22:08:13 +00003971
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003972static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05003973special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003974{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003975 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05003976 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003977 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003978 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003979 return NULL;
3980 }
3981 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003982}
3983
3984
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003985/* Logic for the raise statement (too complicated for inlining).
3986 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003987static int
Collin Winter828f04a2007-08-31 00:04:24 +00003988do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003989{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003990 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003992 if (exc == NULL) {
3993 /* Reraise */
3994 PyThreadState *tstate = PyThreadState_GET();
Mark Shannonae3087c2017-10-22 22:41:51 +01003995 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003996 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01003997 type = exc_info->exc_type;
3998 value = exc_info->exc_value;
3999 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004000 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004001 PyErr_SetString(PyExc_RuntimeError,
4002 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004003 return 0;
4004 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004005 Py_XINCREF(type);
4006 Py_XINCREF(value);
4007 Py_XINCREF(tb);
4008 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004009 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004010 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004012 /* We support the following forms of raise:
4013 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004014 raise <instance>
4015 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004016
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004017 if (PyExceptionClass_Check(exc)) {
4018 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004019 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004020 if (value == NULL)
4021 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004022 if (!PyExceptionInstance_Check(value)) {
4023 PyErr_Format(PyExc_TypeError,
4024 "calling %R should have returned an instance of "
4025 "BaseException, not %R",
4026 type, Py_TYPE(value));
4027 goto raise_error;
4028 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004029 }
4030 else if (PyExceptionInstance_Check(exc)) {
4031 value = exc;
4032 type = PyExceptionInstance_Class(exc);
4033 Py_INCREF(type);
4034 }
4035 else {
4036 /* Not something you can raise. You get an exception
4037 anyway, just not what you specified :-) */
4038 Py_DECREF(exc);
4039 PyErr_SetString(PyExc_TypeError,
4040 "exceptions must derive from BaseException");
4041 goto raise_error;
4042 }
Collin Winter828f04a2007-08-31 00:04:24 +00004043
Serhiy Storchakac0191582016-09-27 11:37:10 +03004044 assert(type != NULL);
4045 assert(value != NULL);
4046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004047 if (cause) {
4048 PyObject *fixed_cause;
4049 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004050 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004051 if (fixed_cause == NULL)
4052 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004053 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004054 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004055 else if (PyExceptionInstance_Check(cause)) {
4056 fixed_cause = cause;
4057 }
4058 else if (cause == Py_None) {
4059 Py_DECREF(cause);
4060 fixed_cause = NULL;
4061 }
4062 else {
4063 PyErr_SetString(PyExc_TypeError,
4064 "exception causes must derive from "
4065 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004066 goto raise_error;
4067 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004068 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004069 }
Collin Winter828f04a2007-08-31 00:04:24 +00004070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004071 PyErr_SetObject(type, value);
4072 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004073 Py_DECREF(value);
4074 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004075 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004076
4077raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004078 Py_XDECREF(value);
4079 Py_XDECREF(type);
4080 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004081 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004082}
4083
Tim Petersd6d010b2001-06-21 02:49:55 +00004084/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004085 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004086
Guido van Rossum0368b722007-05-11 16:50:42 +00004087 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4088 with a variable target.
4089*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004090
Barry Warsawe42b18f1997-08-25 22:13:04 +00004091static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004092unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004093{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004094 int i = 0, j = 0;
4095 Py_ssize_t ll = 0;
4096 PyObject *it; /* iter(v) */
4097 PyObject *w;
4098 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004100 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004102 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004103 if (it == NULL) {
4104 if (PyErr_ExceptionMatches(PyExc_TypeError) &&
4105 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4106 {
4107 PyErr_Format(PyExc_TypeError,
4108 "cannot unpack non-iterable %.200s object",
4109 v->ob_type->tp_name);
4110 }
4111 return 0;
4112 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004114 for (; i < argcnt; i++) {
4115 w = PyIter_Next(it);
4116 if (w == NULL) {
4117 /* Iterator done, via error or exhaustion. */
4118 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004119 if (argcntafter == -1) {
4120 PyErr_Format(PyExc_ValueError,
4121 "not enough values to unpack (expected %d, got %d)",
4122 argcnt, i);
4123 }
4124 else {
4125 PyErr_Format(PyExc_ValueError,
4126 "not enough values to unpack "
4127 "(expected at least %d, got %d)",
4128 argcnt + argcntafter, i);
4129 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004130 }
4131 goto Error;
4132 }
4133 *--sp = w;
4134 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004136 if (argcntafter == -1) {
4137 /* We better have exhausted the iterator now. */
4138 w = PyIter_Next(it);
4139 if (w == NULL) {
4140 if (PyErr_Occurred())
4141 goto Error;
4142 Py_DECREF(it);
4143 return 1;
4144 }
4145 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004146 PyErr_Format(PyExc_ValueError,
4147 "too many values to unpack (expected %d)",
4148 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004149 goto Error;
4150 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004152 l = PySequence_List(it);
4153 if (l == NULL)
4154 goto Error;
4155 *--sp = l;
4156 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004158 ll = PyList_GET_SIZE(l);
4159 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004160 PyErr_Format(PyExc_ValueError,
4161 "not enough values to unpack (expected at least %d, got %zd)",
4162 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004163 goto Error;
4164 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004166 /* Pop the "after-variable" args off the list. */
4167 for (j = argcntafter; j > 0; j--, i++) {
4168 *--sp = PyList_GET_ITEM(l, ll - j);
4169 }
4170 /* Resize the list. */
4171 Py_SIZE(l) = ll - argcntafter;
4172 Py_DECREF(it);
4173 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004174
Tim Petersd6d010b2001-06-21 02:49:55 +00004175Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004176 for (; i > 0; i--, sp++)
4177 Py_DECREF(*sp);
4178 Py_XDECREF(it);
4179 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004180}
4181
4182
Guido van Rossum96a42c81992-01-12 02:29:51 +00004183#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004184static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004185prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004187 printf("%s ", str);
4188 if (PyObject_Print(v, stdout, 0) != 0)
4189 PyErr_Clear(); /* Don't know what else to do */
4190 printf("\n");
4191 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004192}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004193#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004194
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004195static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004196call_exc_trace(Py_tracefunc func, PyObject *self,
4197 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004198{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004199 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004200 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004201 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004202 if (value == NULL) {
4203 value = Py_None;
4204 Py_INCREF(value);
4205 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004206 PyErr_NormalizeException(&type, &value, &orig_traceback);
4207 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004208 arg = PyTuple_Pack(3, type, value, traceback);
4209 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004210 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004211 return;
4212 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004213 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004214 Py_DECREF(arg);
4215 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004216 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004217 else {
4218 Py_XDECREF(type);
4219 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004220 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004221 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004222}
4223
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004224static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004225call_trace_protected(Py_tracefunc func, PyObject *obj,
4226 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004227 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004228{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004229 PyObject *type, *value, *traceback;
4230 int err;
4231 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004232 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004233 if (err == 0)
4234 {
4235 PyErr_Restore(type, value, traceback);
4236 return 0;
4237 }
4238 else {
4239 Py_XDECREF(type);
4240 Py_XDECREF(value);
4241 Py_XDECREF(traceback);
4242 return -1;
4243 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004244}
4245
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004246static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004247call_trace(Py_tracefunc func, PyObject *obj,
4248 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004249 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004250{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004251 int result;
4252 if (tstate->tracing)
4253 return 0;
4254 tstate->tracing++;
4255 tstate->use_tracing = 0;
4256 result = func(obj, frame, what, arg);
4257 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4258 || (tstate->c_profilefunc != NULL));
4259 tstate->tracing--;
4260 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004261}
4262
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004263PyObject *
4264_PyEval_CallTracing(PyObject *func, PyObject *args)
4265{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004266 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004267 int save_tracing = tstate->tracing;
4268 int save_use_tracing = tstate->use_tracing;
4269 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004271 tstate->tracing = 0;
4272 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4273 || (tstate->c_profilefunc != NULL));
4274 result = PyObject_Call(func, args, NULL);
4275 tstate->tracing = save_tracing;
4276 tstate->use_tracing = save_use_tracing;
4277 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004278}
4279
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004280/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004281static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004282maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004283 PyThreadState *tstate, PyFrameObject *frame,
4284 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004285{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004286 int result = 0;
4287 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004289 /* If the last instruction executed isn't in the current
4290 instruction window, reset the window.
4291 */
4292 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4293 PyAddrPair bounds;
4294 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4295 &bounds);
4296 *instr_lb = bounds.ap_lower;
4297 *instr_ub = bounds.ap_upper;
4298 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004299 /* If the last instruction falls at the start of a line or if it
4300 represents a jump backwards, update the frame's line number and
4301 then call the trace function if we're tracing source lines.
4302 */
4303 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004304 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004305 if (frame->f_trace_lines) {
4306 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4307 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004308 }
George King20faa682017-10-18 17:44:22 -07004309 /* Always emit an opcode event if we're tracing all opcodes. */
4310 if (frame->f_trace_opcodes) {
4311 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4312 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004313 *instr_prev = frame->f_lasti;
4314 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004315}
4316
Fred Drake5755ce62001-06-27 19:19:46 +00004317void
4318PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004319{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004320 PyThreadState *tstate = PyThreadState_GET();
4321 PyObject *temp = tstate->c_profileobj;
4322 Py_XINCREF(arg);
4323 tstate->c_profilefunc = NULL;
4324 tstate->c_profileobj = NULL;
4325 /* Must make sure that tracing is not ignored if 'temp' is freed */
4326 tstate->use_tracing = tstate->c_tracefunc != NULL;
4327 Py_XDECREF(temp);
4328 tstate->c_profilefunc = func;
4329 tstate->c_profileobj = arg;
4330 /* Flag that tracing or profiling is turned on */
4331 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004332}
4333
4334void
4335PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4336{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004337 PyThreadState *tstate = PyThreadState_GET();
4338 PyObject *temp = tstate->c_traceobj;
4339 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4340 Py_XINCREF(arg);
4341 tstate->c_tracefunc = NULL;
4342 tstate->c_traceobj = NULL;
4343 /* Must make sure that profiling is not ignored if 'temp' is freed */
4344 tstate->use_tracing = tstate->c_profilefunc != NULL;
4345 Py_XDECREF(temp);
4346 tstate->c_tracefunc = func;
4347 tstate->c_traceobj = arg;
4348 /* Flag that tracing or profiling is turned on */
4349 tstate->use_tracing = ((func != NULL)
4350 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004351}
4352
Yury Selivanov75445082015-05-11 22:57:16 -04004353void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004354_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4355{
4356 assert(new_depth >= 0);
4357 PyThreadState *tstate = PyThreadState_GET();
4358 tstate->coroutine_origin_tracking_depth = new_depth;
4359}
4360
4361int
4362_PyEval_GetCoroutineOriginTrackingDepth(void)
4363{
4364 PyThreadState *tstate = PyThreadState_GET();
4365 return tstate->coroutine_origin_tracking_depth;
4366}
4367
4368void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004369_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004370{
4371 PyThreadState *tstate = PyThreadState_GET();
4372
Yury Selivanov75445082015-05-11 22:57:16 -04004373 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004374 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004375}
4376
4377PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004378_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004379{
4380 PyThreadState *tstate = PyThreadState_GET();
4381 return tstate->coroutine_wrapper;
4382}
4383
Yury Selivanoveb636452016-09-08 22:01:51 -07004384void
4385_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4386{
4387 PyThreadState *tstate = PyThreadState_GET();
4388
4389 Py_XINCREF(firstiter);
4390 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4391}
4392
4393PyObject *
4394_PyEval_GetAsyncGenFirstiter(void)
4395{
4396 PyThreadState *tstate = PyThreadState_GET();
4397 return tstate->async_gen_firstiter;
4398}
4399
4400void
4401_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4402{
4403 PyThreadState *tstate = PyThreadState_GET();
4404
4405 Py_XINCREF(finalizer);
4406 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4407}
4408
4409PyObject *
4410_PyEval_GetAsyncGenFinalizer(void)
4411{
4412 PyThreadState *tstate = PyThreadState_GET();
4413 return tstate->async_gen_finalizer;
4414}
4415
Guido van Rossumb209a111997-04-29 18:18:01 +00004416PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004417PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004418{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004419 PyFrameObject *current_frame = PyEval_GetFrame();
4420 if (current_frame == NULL)
4421 return PyThreadState_GET()->interp->builtins;
4422 else
4423 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004424}
4425
Guido van Rossumb209a111997-04-29 18:18:01 +00004426PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004427PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004428{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004429 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004430 if (current_frame == NULL) {
4431 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004432 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004433 }
4434
4435 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4436 return NULL;
4437
4438 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004439 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004440}
4441
Guido van Rossumb209a111997-04-29 18:18:01 +00004442PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004443PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004445 PyFrameObject *current_frame = PyEval_GetFrame();
4446 if (current_frame == NULL)
4447 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004448
4449 assert(current_frame->f_globals != NULL);
4450 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004451}
4452
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004453PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004454PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004455{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004456 PyThreadState *tstate = PyThreadState_GET();
4457 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004458}
4459
Guido van Rossum6135a871995-01-09 17:53:26 +00004460int
Tim Peters5ba58662001-07-16 02:29:45 +00004461PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004462{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004463 PyFrameObject *current_frame = PyEval_GetFrame();
4464 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004466 if (current_frame != NULL) {
4467 const int codeflags = current_frame->f_code->co_flags;
4468 const int compilerflags = codeflags & PyCF_MASK;
4469 if (compilerflags) {
4470 result = 1;
4471 cf->cf_flags |= compilerflags;
4472 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004473#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004474 if (codeflags & CO_GENERATOR_ALLOWED) {
4475 result = 1;
4476 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4477 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004478#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004479 }
4480 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004481}
4482
Guido van Rossum3f5da241990-12-20 15:06:42 +00004483
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004484const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004485PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004486{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004487 if (PyMethod_Check(func))
4488 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4489 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004490 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004491 else if (PyCFunction_Check(func))
4492 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4493 else
4494 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004495}
4496
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004497const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004498PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004499{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004500 if (PyMethod_Check(func))
4501 return "()";
4502 else if (PyFunction_Check(func))
4503 return "()";
4504 else if (PyCFunction_Check(func))
4505 return "()";
4506 else
4507 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004508}
4509
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004510#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004511if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004512 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4513 tstate, tstate->frame, \
4514 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004515 x = NULL; \
4516 } \
4517 else { \
4518 x = call; \
4519 if (tstate->c_profilefunc != NULL) { \
4520 if (x == NULL) { \
4521 call_trace_protected(tstate->c_profilefunc, \
4522 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004523 tstate, tstate->frame, \
4524 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004525 /* XXX should pass (type, value, tb) */ \
4526 } else { \
4527 if (call_trace(tstate->c_profilefunc, \
4528 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004529 tstate, tstate->frame, \
4530 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004531 Py_DECREF(x); \
4532 x = NULL; \
4533 } \
4534 } \
4535 } \
4536 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004537} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004538 x = call; \
4539 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004540
Victor Stinner415c5102017-01-11 00:54:57 +01004541/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4542 to reduce the stack consumption. */
4543Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004544call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004545{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004546 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004547 PyObject *func = *pfunc;
4548 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004549 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4550 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004551 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004553 /* Always dispatch PyCFunction first, because these are
4554 presumed to be the most frequent callable object.
4555 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004556 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004557 PyThreadState *tstate = PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004558 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004559 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004560 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
4561 PyThreadState *tstate = PyThreadState_GET();
INADA Naoki93fac8d2017-03-07 14:24:37 +09004562 if (tstate->use_tracing && tstate->c_profilefunc) {
4563 // We need to create PyCFunctionObject for tracing.
4564 PyMethodDescrObject *descr = (PyMethodDescrObject*)func;
4565 func = PyCFunction_NewEx(descr->d_method, stack[0], NULL);
4566 if (func == NULL) {
4567 return NULL;
4568 }
4569 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack+1, nargs-1,
4570 kwnames));
4571 Py_DECREF(func);
4572 }
4573 else {
4574 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4575 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004576 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004577 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004578 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004579 /* Optimize access to bound methods. Reuse the Python stack
4580 to pass 'self' as the first argument, replace 'func'
4581 with 'self'. It avoids the creation of a new temporary tuple
4582 for arguments (to replace func with self) when the method uses
4583 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004584 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004585 Py_INCREF(self);
4586 func = PyMethod_GET_FUNCTION(func);
4587 Py_INCREF(func);
4588 Py_SETREF(*pfunc, self);
4589 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004590 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004591 }
4592 else {
4593 Py_INCREF(func);
4594 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004595
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004596 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004597 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004598 }
4599 else {
4600 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4601 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004602 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004603 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004604
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004605 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4606
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004607 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004608 while ((*pp_stack) > pfunc) {
4609 w = EXT_POP(*pp_stack);
4610 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004611 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004613 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004614}
4615
Jeremy Hylton52820442001-01-03 23:52:36 +00004616static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004617do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004618{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004619 if (PyCFunction_Check(func)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004620 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004621 PyThreadState *tstate = PyThreadState_GET();
4622 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004623 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004624 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004625 else {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004626 return PyObject_Call(func, callargs, kwdict);
Victor Stinner74319ae2016-08-25 00:04:09 +02004627 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004628}
4629
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004630/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004631 nb_index slot defined, and store in *pi.
4632 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004633 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004634 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004635*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004636int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004637_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004638{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004639 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004640 Py_ssize_t x;
4641 if (PyIndex_Check(v)) {
4642 x = PyNumber_AsSsize_t(v, NULL);
4643 if (x == -1 && PyErr_Occurred())
4644 return 0;
4645 }
4646 else {
4647 PyErr_SetString(PyExc_TypeError,
4648 "slice indices must be integers or "
4649 "None or have an __index__ method");
4650 return 0;
4651 }
4652 *pi = x;
4653 }
4654 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004655}
4656
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004657int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004658_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004659{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004660 Py_ssize_t x;
4661 if (PyIndex_Check(v)) {
4662 x = PyNumber_AsSsize_t(v, NULL);
4663 if (x == -1 && PyErr_Occurred())
4664 return 0;
4665 }
4666 else {
4667 PyErr_SetString(PyExc_TypeError,
4668 "slice indices must be integers or "
4669 "have an __index__ method");
4670 return 0;
4671 }
4672 *pi = x;
4673 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004674}
4675
4676
Guido van Rossum486364b2007-06-30 05:01:58 +00004677#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004678 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004679
Guido van Rossumb209a111997-04-29 18:18:01 +00004680static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004681cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004682{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004683 int res = 0;
4684 switch (op) {
4685 case PyCmp_IS:
4686 res = (v == w);
4687 break;
4688 case PyCmp_IS_NOT:
4689 res = (v != w);
4690 break;
4691 case PyCmp_IN:
4692 res = PySequence_Contains(w, v);
4693 if (res < 0)
4694 return NULL;
4695 break;
4696 case PyCmp_NOT_IN:
4697 res = PySequence_Contains(w, v);
4698 if (res < 0)
4699 return NULL;
4700 res = !res;
4701 break;
4702 case PyCmp_EXC_MATCH:
4703 if (PyTuple_Check(w)) {
4704 Py_ssize_t i, length;
4705 length = PyTuple_Size(w);
4706 for (i = 0; i < length; i += 1) {
4707 PyObject *exc = PyTuple_GET_ITEM(w, i);
4708 if (!PyExceptionClass_Check(exc)) {
4709 PyErr_SetString(PyExc_TypeError,
4710 CANNOT_CATCH_MSG);
4711 return NULL;
4712 }
4713 }
4714 }
4715 else {
4716 if (!PyExceptionClass_Check(w)) {
4717 PyErr_SetString(PyExc_TypeError,
4718 CANNOT_CATCH_MSG);
4719 return NULL;
4720 }
4721 }
4722 res = PyErr_GivenExceptionMatches(v, w);
4723 break;
4724 default:
4725 return PyObject_RichCompare(v, w, op);
4726 }
4727 v = res ? Py_True : Py_False;
4728 Py_INCREF(v);
4729 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004730}
4731
Thomas Wouters52152252000-08-17 22:55:00 +00004732static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004733import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4734{
4735 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004736 PyObject *import_func, *res;
4737 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004738
4739 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
4740 if (import_func == NULL) {
4741 PyErr_SetString(PyExc_ImportError, "__import__ not found");
4742 return NULL;
4743 }
4744
4745 /* Fast path for not overloaded __import__. */
4746 if (import_func == PyThreadState_GET()->interp->import_func) {
4747 int ilevel = _PyLong_AsInt(level);
4748 if (ilevel == -1 && PyErr_Occurred()) {
4749 return NULL;
4750 }
4751 res = PyImport_ImportModuleLevelObject(
4752 name,
4753 f->f_globals,
4754 f->f_locals == NULL ? Py_None : f->f_locals,
4755 fromlist,
4756 ilevel);
4757 return res;
4758 }
4759
4760 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004761
4762 stack[0] = name;
4763 stack[1] = f->f_globals;
4764 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
4765 stack[3] = fromlist;
4766 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02004767 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004768 Py_DECREF(import_func);
4769 return res;
4770}
4771
4772static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004773import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004774{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004775 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02004776 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08004777 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004778
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004779 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02004780 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004781 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004782 /* Issue #17636: in case this failed because of a circular relative
4783 import, try to fallback on reading the module directly from
4784 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02004785 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07004786 if (pkgname == NULL) {
4787 goto error;
4788 }
Oren Milman6db70332017-09-19 14:23:01 +03004789 if (!PyUnicode_Check(pkgname)) {
4790 Py_CLEAR(pkgname);
4791 goto error;
4792 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004793 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07004794 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08004795 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004796 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07004797 }
Eric Snow3f9eee62017-09-15 16:35:20 -06004798 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004799 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07004800 if (x == NULL) {
4801 goto error;
4802 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004803 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004804 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07004805 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004806 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004807 if (pkgname == NULL) {
4808 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
4809 if (pkgname_or_unknown == NULL) {
4810 Py_XDECREF(pkgpath);
4811 return NULL;
4812 }
4813 } else {
4814 pkgname_or_unknown = pkgname;
4815 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004816
4817 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
4818 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08004819 errmsg = PyUnicode_FromFormat(
4820 "cannot import name %R from %R (unknown location)",
4821 name, pkgname_or_unknown
4822 );
4823 /* NULL check for errmsg done by PyErr_SetImportError. */
4824 PyErr_SetImportError(errmsg, pkgname, NULL);
4825 }
4826 else {
4827 errmsg = PyUnicode_FromFormat(
4828 "cannot import name %R from %R (%S)",
4829 name, pkgname_or_unknown, pkgpath
4830 );
4831 /* NULL check for errmsg done by PyErr_SetImportError. */
4832 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004833 }
4834
Xiang Zhang4830f582017-03-21 11:13:42 +08004835 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004836 Py_XDECREF(pkgname_or_unknown);
4837 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07004838 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00004839}
Guido van Rossumac7be682001-01-17 15:42:30 +00004840
Thomas Wouters52152252000-08-17 22:55:00 +00004841static int
4842import_all_from(PyObject *locals, PyObject *v)
4843{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004844 _Py_IDENTIFIER(__all__);
4845 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08004846 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004847 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004848 int skip_leading_underscores = 0;
4849 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004850
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004851 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
4852 return -1; /* Unexpected error */
4853 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004854 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004855 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
4856 return -1;
4857 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004858 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004859 PyErr_SetString(PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004860 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004861 return -1;
4862 }
4863 all = PyMapping_Keys(dict);
4864 Py_DECREF(dict);
4865 if (all == NULL)
4866 return -1;
4867 skip_leading_underscores = 1;
4868 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004870 for (pos = 0, err = 0; ; pos++) {
4871 name = PySequence_GetItem(all, pos);
4872 if (name == NULL) {
4873 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4874 err = -1;
4875 else
4876 PyErr_Clear();
4877 break;
4878 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08004879 if (!PyUnicode_Check(name)) {
4880 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
4881 if (modname == NULL) {
4882 Py_DECREF(name);
4883 err = -1;
4884 break;
4885 }
4886 if (!PyUnicode_Check(modname)) {
4887 PyErr_Format(PyExc_TypeError,
4888 "module __name__ must be a string, not %.100s",
4889 Py_TYPE(modname)->tp_name);
4890 }
4891 else {
4892 PyErr_Format(PyExc_TypeError,
4893 "%s in %U.%s must be str, not %.100s",
4894 skip_leading_underscores ? "Key" : "Item",
4895 modname,
4896 skip_leading_underscores ? "__dict__" : "__all__",
4897 Py_TYPE(name)->tp_name);
4898 }
4899 Py_DECREF(modname);
4900 Py_DECREF(name);
4901 err = -1;
4902 break;
4903 }
4904 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03004905 if (PyUnicode_READY(name) == -1) {
4906 Py_DECREF(name);
4907 err = -1;
4908 break;
4909 }
4910 if (PyUnicode_READ_CHAR(name, 0) == '_') {
4911 Py_DECREF(name);
4912 continue;
4913 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004914 }
4915 value = PyObject_GetAttr(v, name);
4916 if (value == NULL)
4917 err = -1;
4918 else if (PyDict_CheckExact(locals))
4919 err = PyDict_SetItem(locals, name, value);
4920 else
4921 err = PyObject_SetItem(locals, name, value);
4922 Py_DECREF(name);
4923 Py_XDECREF(value);
4924 if (err != 0)
4925 break;
4926 }
4927 Py_DECREF(all);
4928 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004929}
4930
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03004931static int
4932check_args_iterable(PyObject *func, PyObject *args)
4933{
4934 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
4935 PyErr_Format(PyExc_TypeError,
4936 "%.200s%.200s argument after * "
4937 "must be an iterable, not %.200s",
4938 PyEval_GetFuncName(func),
4939 PyEval_GetFuncDesc(func),
4940 args->ob_type->tp_name);
4941 return -1;
4942 }
4943 return 0;
4944}
4945
4946static void
4947format_kwargs_mapping_error(PyObject *func, PyObject *kwargs)
4948{
4949 PyErr_Format(PyExc_TypeError,
4950 "%.200s%.200s argument after ** "
4951 "must be a mapping, not %.200s",
4952 PyEval_GetFuncName(func),
4953 PyEval_GetFuncDesc(func),
4954 kwargs->ob_type->tp_name);
4955}
4956
Guido van Rossumac7be682001-01-17 15:42:30 +00004957static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004958format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004959{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004960 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004962 if (!obj)
4963 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004964
Serhiy Storchaka06515832016-11-20 09:13:07 +02004965 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004966 if (!obj_str)
4967 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004968
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004969 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004970}
Guido van Rossum950361c1997-01-24 13:49:28 +00004971
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004972static void
4973format_exc_unbound(PyCodeObject *co, int oparg)
4974{
4975 PyObject *name;
4976 /* Don't stomp existing exception */
4977 if (PyErr_Occurred())
4978 return;
4979 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4980 name = PyTuple_GET_ITEM(co->co_cellvars,
4981 oparg);
4982 format_exc_check_arg(
4983 PyExc_UnboundLocalError,
4984 UNBOUNDLOCAL_ERROR_MSG,
4985 name);
4986 } else {
4987 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4988 PyTuple_GET_SIZE(co->co_cellvars));
4989 format_exc_check_arg(PyExc_NameError,
4990 UNBOUNDFREE_ERROR_MSG, name);
4991 }
4992}
4993
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03004994static void
4995format_awaitable_error(PyTypeObject *type, int prevopcode)
4996{
4997 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
4998 if (prevopcode == BEFORE_ASYNC_WITH) {
4999 PyErr_Format(PyExc_TypeError,
5000 "'async with' received an object from __aenter__ "
5001 "that does not implement __await__: %.100s",
5002 type->tp_name);
5003 }
5004 else if (prevopcode == WITH_CLEANUP_START) {
5005 PyErr_Format(PyExc_TypeError,
5006 "'async with' received an object from __aexit__ "
5007 "that does not implement __await__: %.100s",
5008 type->tp_name);
5009 }
5010 }
5011}
5012
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005013static PyObject *
5014unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005015 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005016{
5017 PyObject *res;
5018 if (Py_REFCNT(v) == 2) {
5019 /* In the common case, there are 2 references to the value
5020 * stored in 'variable' when the += is performed: one on the
5021 * value stack (in 'v') and one still stored in the
5022 * 'variable'. We try to delete the variable now to reduce
5023 * the refcnt to 1.
5024 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005025 int opcode, oparg;
5026 NEXTOPARG();
5027 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005028 case STORE_FAST:
5029 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005030 PyObject **fastlocals = f->f_localsplus;
5031 if (GETLOCAL(oparg) == v)
5032 SETLOCAL(oparg, NULL);
5033 break;
5034 }
5035 case STORE_DEREF:
5036 {
5037 PyObject **freevars = (f->f_localsplus +
5038 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005039 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005040 if (PyCell_GET(c) == v) {
5041 PyCell_SET(c, NULL);
5042 Py_DECREF(v);
5043 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005044 break;
5045 }
5046 case STORE_NAME:
5047 {
5048 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005049 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005050 PyObject *locals = f->f_locals;
5051 if (PyDict_CheckExact(locals) &&
5052 PyDict_GetItem(locals, name) == v) {
5053 if (PyDict_DelItem(locals, name) != 0) {
5054 PyErr_Clear();
5055 }
5056 }
5057 break;
5058 }
5059 }
5060 }
5061 res = v;
5062 PyUnicode_Append(&res, w);
5063 return res;
5064}
5065
Guido van Rossum950361c1997-01-24 13:49:28 +00005066#ifdef DYNAMIC_EXECUTION_PROFILE
5067
Skip Montanarof118cb12001-10-15 20:51:38 +00005068static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005069getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005070{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005071 int i;
5072 PyObject *l = PyList_New(256);
5073 if (l == NULL) return NULL;
5074 for (i = 0; i < 256; i++) {
5075 PyObject *x = PyLong_FromLong(a[i]);
5076 if (x == NULL) {
5077 Py_DECREF(l);
5078 return NULL;
5079 }
5080 PyList_SetItem(l, i, x);
5081 }
5082 for (i = 0; i < 256; i++)
5083 a[i] = 0;
5084 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005085}
5086
5087PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005088_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005089{
5090#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005091 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005092#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005093 int i;
5094 PyObject *l = PyList_New(257);
5095 if (l == NULL) return NULL;
5096 for (i = 0; i < 257; i++) {
5097 PyObject *x = getarray(dxpairs[i]);
5098 if (x == NULL) {
5099 Py_DECREF(l);
5100 return NULL;
5101 }
5102 PyList_SetItem(l, i, x);
5103 }
5104 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005105#endif
5106}
5107
5108#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005109
5110Py_ssize_t
5111_PyEval_RequestCodeExtraIndex(freefunc free)
5112{
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005113 PyInterpreterState *interp = PyThreadState_Get()->interp;
Brett Cannon5c4de282016-09-07 11:16:41 -07005114 Py_ssize_t new_index;
5115
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005116 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005117 return -1;
5118 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005119 new_index = interp->co_extra_user_count++;
5120 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005121 return new_index;
5122}
Łukasz Langaa785c872016-09-09 17:37:37 -07005123
5124static void
5125dtrace_function_entry(PyFrameObject *f)
5126{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005127 const char *filename;
5128 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005129 int lineno;
5130
5131 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5132 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5133 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5134
5135 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5136}
5137
5138static void
5139dtrace_function_return(PyFrameObject *f)
5140{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005141 const char *filename;
5142 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005143 int lineno;
5144
5145 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5146 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5147 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5148
5149 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5150}
5151
5152/* DTrace equivalent of maybe_call_line_trace. */
5153static void
5154maybe_dtrace_line(PyFrameObject *frame,
5155 int *instr_lb, int *instr_ub, int *instr_prev)
5156{
5157 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005158 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005159
5160 /* If the last instruction executed isn't in the current
5161 instruction window, reset the window.
5162 */
5163 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5164 PyAddrPair bounds;
5165 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5166 &bounds);
5167 *instr_lb = bounds.ap_lower;
5168 *instr_ub = bounds.ap_upper;
5169 }
5170 /* If the last instruction falls at the start of a line or if
5171 it represents a jump backwards, update the frame's line
5172 number and call the trace function. */
5173 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5174 frame->f_lineno = line;
5175 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5176 if (!co_filename)
5177 co_filename = "?";
5178 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5179 if (!co_name)
5180 co_name = "?";
5181 PyDTrace_LINE(co_filename, co_name, line);
5182 }
5183 *instr_prev = frame->f_lasti;
5184}