blob: f3a74b00a2b64a75edc14129fd4447dc3ca96700 [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{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200535 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
536 return interp->eval_frame(f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700537}
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())
costypetrisor8ed317f2018-07-31 20:55:14 +0000765#define STACK_GROW(n) do { \
766 assert(n >= 0); \
767 (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000768 lltrace && prtrace(TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000769 assert(STACK_LEVEL() <= co->co_stacksize); \
770 } while (0)
771#define STACK_SHRINK(n) do { \
772 assert(n >= 0); \
773 (void)(lltrace && prtrace(TOP(), "stackadj")); \
774 (void)(BASIC_STACKADJ(-n)); \
775 assert(STACK_LEVEL() <= co->co_stacksize); \
776 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +0000777#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000778 prtrace((STACK_POINTER)[-1], "ext_pop")), \
779 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000780#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000781#define PUSH(v) BASIC_PUSH(v)
782#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +0000783#define STACK_GROW(n) BASIC_STACKADJ(n)
784#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000785#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000786#endif
787
Guido van Rossum681d79a1995-07-18 14:51:37 +0000788/* Local variable macros */
789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000791
792/* The SETLOCAL() macro must not DECREF the local variable in-place and
793 then store the new value; it must copy the old value to a temporary
794 value, then store the new value, and then DECREF the temporary value.
795 This is because it is possible that during the DECREF the frame is
796 accessed by other code (e.g. a __del__ method or gc.collect()) and the
797 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000799 GETLOCAL(i) = value; \
800 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000801
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000802
803#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 while (STACK_LEVEL() > (b)->b_level) { \
805 PyObject *v = POP(); \
806 Py_XDECREF(v); \
807 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000808
809#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300810 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +0100812 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 assert(STACK_LEVEL() >= (b)->b_level + 3); \
814 while (STACK_LEVEL() > (b)->b_level + 3) { \
815 value = POP(); \
816 Py_XDECREF(value); \
817 } \
Mark Shannonae3087c2017-10-22 22:41:51 +0100818 exc_info = tstate->exc_info; \
819 type = exc_info->exc_type; \
820 value = exc_info->exc_value; \
821 traceback = exc_info->exc_traceback; \
822 exc_info->exc_type = POP(); \
823 exc_info->exc_value = POP(); \
824 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 Py_XDECREF(type); \
826 Py_XDECREF(value); \
827 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300828 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000829
Guido van Rossuma027efa1997-05-05 20:56:21 +0000830/* Start of code */
831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 /* push frame */
833 if (Py_EnterRecursiveCall(""))
834 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 if (tstate->use_tracing) {
839 if (tstate->c_tracefunc != NULL) {
840 /* tstate->c_tracefunc, if defined, is a
841 function that will be called on *every* entry
842 to a code block. Its return value, if not
843 None, is a function that will be called at
844 the start of each executed line of code.
845 (Actually, the function must return itself
846 in order to continue tracing.) The trace
847 functions are called with three arguments:
848 a pointer to the current frame, a string
849 indicating why the function is called, and
850 an argument which depends on the situation.
851 The global trace function is also called
852 whenever an exception is detected. */
853 if (call_trace_protected(tstate->c_tracefunc,
854 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100855 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 /* Trace function raised an error */
857 goto exit_eval_frame;
858 }
859 }
860 if (tstate->c_profilefunc != NULL) {
861 /* Similar for c_profilefunc, except it needn't
862 return itself and isn't called for "line" events */
863 if (call_trace_protected(tstate->c_profilefunc,
864 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100865 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 /* Profile function raised an error */
867 goto exit_eval_frame;
868 }
869 }
870 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000871
Łukasz Langaa785c872016-09-09 17:37:37 -0700872 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
873 dtrace_function_entry(f);
874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 co = f->f_code;
876 names = co->co_names;
877 consts = co->co_consts;
878 fastlocals = f->f_localsplus;
879 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300880 assert(PyBytes_Check(co->co_code));
881 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +0300882 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
883 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
884 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300885 /*
886 f->f_lasti refers to the index of the last instruction,
887 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000888
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300889 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500890 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 When the PREDICT() macros are enabled, some opcode pairs follow in
893 direct succession without updating f->f_lasti. A successful
894 prediction effectively links the two codes together as if they
895 were a single new opcode; accordingly,f->f_lasti will point to
896 the first code in the pair (for instance, GET_ITER followed by
897 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300898 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300900 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300901 next_instr = first_instr;
902 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +0300903 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
904 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300905 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 stack_pointer = f->f_stacktop;
907 assert(stack_pointer != NULL);
908 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +0200909 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000910
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000911
Tim Peters5ca576e2001-06-18 22:08:13 +0000912#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200913 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000914#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000915
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400916 if (throwflag) /* support for generator.throw() */
917 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000918
Victor Stinnerace47d72013-07-18 01:41:08 +0200919#ifdef Py_DEBUG
920 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100921 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +0000922 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +0200923 assert(!PyErr_Occurred());
924#endif
925
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200926main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 assert(stack_pointer >= f->f_valuestack); /* else underflow */
929 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +0200930 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 /* Do periodic things. Doing this every time through
933 the loop would add too much overhead, so we do it
934 only every Nth instruction. We also do it if
935 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
936 event needs attention (e.g. a signal handler or
937 async I/O handler); see Py_AddPendingCall() and
938 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000939
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600940 if (_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +0300941 opcode = _Py_OPCODE(*next_instr);
942 if (opcode == SETUP_FINALLY ||
943 opcode == SETUP_WITH ||
944 opcode == BEFORE_ASYNC_WITH ||
945 opcode == YIELD_FROM) {
946 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -0700947 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +0300948 - If we're about to enter the 'with:'. It will prevent
949 emitting a resource warning in the common idiom
950 'with open(path) as file:'.
951 - If we're about to enter the 'async with:'.
952 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -0700953 *very* useful, but might help in some cases and it's
954 traditional)
955 - If we're resuming a chain of nested 'yield from' or
956 'await' calls, then each frame is parked with YIELD_FROM
957 as its next opcode. If the user hit control-C we want to
958 wait until we've reached the innermost frame before
959 running the signal handler and raising KeyboardInterrupt
960 (see bpo-30039).
961 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 goto fast_next_opcode;
963 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600964 if (_Py_atomic_load_relaxed(
965 &_PyRuntime.ceval.pending.calls_to_do))
966 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400967 if (Py_MakePendingCalls() < 0)
968 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600970 if (_Py_atomic_load_relaxed(
971 &_PyRuntime.ceval.gil_drop_request))
972 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 /* Give another thread a chance */
974 if (PyThreadState_Swap(NULL) != tstate)
975 Py_FatalError("ceval: tstate mix-up");
976 drop_gil(tstate);
977
978 /* Other threads may run now */
979
980 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -0700981
982 /* Check if we should make a quick exit. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600983 if (_Py_IsFinalizing() &&
984 !_Py_CURRENTLY_FINALIZING(tstate))
985 {
Benjamin Peterson17548dd2014-06-16 22:59:07 -0700986 drop_gil(tstate);
987 PyThread_exit_thread();
988 }
989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 if (PyThreadState_Swap(tstate) != NULL)
991 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 }
993 /* Check for asynchronous exceptions. */
994 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400995 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 tstate->async_exc = NULL;
997 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400998 PyErr_SetNone(exc);
999 Py_DECREF(exc);
1000 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 }
1002 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 fast_next_opcode:
1005 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001006
Łukasz Langaa785c872016-09-09 17:37:37 -07001007 if (PyDTrace_LINE_ENABLED())
1008 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001013 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001014 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 /* see maybe_call_line_trace
1016 for expository comments */
1017 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 err = maybe_call_line_trace(tstate->c_tracefunc,
1020 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001021 tstate, f,
1022 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 /* Reload possibly changed frame fields */
1024 JUMPTO(f->f_lasti);
1025 if (f->f_stacktop != NULL) {
1026 stack_pointer = f->f_stacktop;
1027 f->f_stacktop = NULL;
1028 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001029 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001031 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001035
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001036 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001037 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001038#ifdef DYNAMIC_EXECUTION_PROFILE
1039#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 dxpairs[lastopcode][opcode]++;
1041 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001042#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001044#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001045
Guido van Rossum96a42c81992-01-12 02:29:51 +00001046#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 if (lltrace) {
1050 if (HAS_ARG(opcode)) {
1051 printf("%d: %d, %d\n",
1052 f->f_lasti, opcode, oparg);
1053 }
1054 else {
1055 printf("%d: %d\n",
1056 f->f_lasti, opcode);
1057 }
1058 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001059#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001062
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001064 It is essential that any operation that fails must goto error
1065 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 TARGET(NOP)
1068 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001069
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001070 TARGET(LOAD_FAST) {
1071 PyObject *value = GETLOCAL(oparg);
1072 if (value == NULL) {
1073 format_exc_check_arg(PyExc_UnboundLocalError,
1074 UNBOUNDLOCAL_ERROR_MSG,
1075 PyTuple_GetItem(co->co_varnames, oparg));
1076 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001078 Py_INCREF(value);
1079 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001081 }
1082
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001083 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001084 TARGET(LOAD_CONST) {
1085 PyObject *value = GETITEM(consts, oparg);
1086 Py_INCREF(value);
1087 PUSH(value);
1088 FAST_DISPATCH();
1089 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001090
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001091 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001092 TARGET(STORE_FAST) {
1093 PyObject *value = POP();
1094 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001096 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001097
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001098 TARGET(POP_TOP) {
1099 PyObject *value = POP();
1100 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001102 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001103
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001104 TARGET(ROT_TWO) {
1105 PyObject *top = TOP();
1106 PyObject *second = SECOND();
1107 SET_TOP(second);
1108 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001110 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001111
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001112 TARGET(ROT_THREE) {
1113 PyObject *top = TOP();
1114 PyObject *second = SECOND();
1115 PyObject *third = THIRD();
1116 SET_TOP(second);
1117 SET_SECOND(third);
1118 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001120 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001121
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001122 TARGET(ROT_FOUR) {
1123 PyObject *top = TOP();
1124 PyObject *second = SECOND();
1125 PyObject *third = THIRD();
1126 PyObject *fourth = FOURTH();
1127 SET_TOP(second);
1128 SET_SECOND(third);
1129 SET_THIRD(fourth);
1130 SET_FOURTH(top);
1131 FAST_DISPATCH();
1132 }
1133
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001134 TARGET(DUP_TOP) {
1135 PyObject *top = TOP();
1136 Py_INCREF(top);
1137 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001139 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001140
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001141 TARGET(DUP_TOP_TWO) {
1142 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001143 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001144 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001145 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001146 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001147 SET_TOP(top);
1148 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001149 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001150 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001151
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001152 TARGET(UNARY_POSITIVE) {
1153 PyObject *value = TOP();
1154 PyObject *res = PyNumber_Positive(value);
1155 Py_DECREF(value);
1156 SET_TOP(res);
1157 if (res == NULL)
1158 goto error;
1159 DISPATCH();
1160 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001161
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001162 TARGET(UNARY_NEGATIVE) {
1163 PyObject *value = TOP();
1164 PyObject *res = PyNumber_Negative(value);
1165 Py_DECREF(value);
1166 SET_TOP(res);
1167 if (res == NULL)
1168 goto error;
1169 DISPATCH();
1170 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001171
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001172 TARGET(UNARY_NOT) {
1173 PyObject *value = TOP();
1174 int err = PyObject_IsTrue(value);
1175 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 if (err == 0) {
1177 Py_INCREF(Py_True);
1178 SET_TOP(Py_True);
1179 DISPATCH();
1180 }
1181 else if (err > 0) {
1182 Py_INCREF(Py_False);
1183 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 DISPATCH();
1185 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001186 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001187 goto error;
1188 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001189
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001190 TARGET(UNARY_INVERT) {
1191 PyObject *value = TOP();
1192 PyObject *res = PyNumber_Invert(value);
1193 Py_DECREF(value);
1194 SET_TOP(res);
1195 if (res == NULL)
1196 goto error;
1197 DISPATCH();
1198 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001199
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001200 TARGET(BINARY_POWER) {
1201 PyObject *exp = POP();
1202 PyObject *base = TOP();
1203 PyObject *res = PyNumber_Power(base, exp, Py_None);
1204 Py_DECREF(base);
1205 Py_DECREF(exp);
1206 SET_TOP(res);
1207 if (res == NULL)
1208 goto error;
1209 DISPATCH();
1210 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001211
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001212 TARGET(BINARY_MULTIPLY) {
1213 PyObject *right = POP();
1214 PyObject *left = TOP();
1215 PyObject *res = PyNumber_Multiply(left, right);
1216 Py_DECREF(left);
1217 Py_DECREF(right);
1218 SET_TOP(res);
1219 if (res == NULL)
1220 goto error;
1221 DISPATCH();
1222 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001223
Benjamin Petersond51374e2014-04-09 23:55:56 -04001224 TARGET(BINARY_MATRIX_MULTIPLY) {
1225 PyObject *right = POP();
1226 PyObject *left = TOP();
1227 PyObject *res = PyNumber_MatrixMultiply(left, right);
1228 Py_DECREF(left);
1229 Py_DECREF(right);
1230 SET_TOP(res);
1231 if (res == NULL)
1232 goto error;
1233 DISPATCH();
1234 }
1235
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001236 TARGET(BINARY_TRUE_DIVIDE) {
1237 PyObject *divisor = POP();
1238 PyObject *dividend = TOP();
1239 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1240 Py_DECREF(dividend);
1241 Py_DECREF(divisor);
1242 SET_TOP(quotient);
1243 if (quotient == NULL)
1244 goto error;
1245 DISPATCH();
1246 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001247
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001248 TARGET(BINARY_FLOOR_DIVIDE) {
1249 PyObject *divisor = POP();
1250 PyObject *dividend = TOP();
1251 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1252 Py_DECREF(dividend);
1253 Py_DECREF(divisor);
1254 SET_TOP(quotient);
1255 if (quotient == NULL)
1256 goto error;
1257 DISPATCH();
1258 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001259
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001260 TARGET(BINARY_MODULO) {
1261 PyObject *divisor = POP();
1262 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001263 PyObject *res;
1264 if (PyUnicode_CheckExact(dividend) && (
1265 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1266 // fast path; string formatting, but not if the RHS is a str subclass
1267 // (see issue28598)
1268 res = PyUnicode_Format(dividend, divisor);
1269 } else {
1270 res = PyNumber_Remainder(dividend, divisor);
1271 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001272 Py_DECREF(divisor);
1273 Py_DECREF(dividend);
1274 SET_TOP(res);
1275 if (res == NULL)
1276 goto error;
1277 DISPATCH();
1278 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001279
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001280 TARGET(BINARY_ADD) {
1281 PyObject *right = POP();
1282 PyObject *left = TOP();
1283 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001284 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1285 CPython using bytecode, it is simply worthless.
1286 See http://bugs.python.org/issue21955 and
1287 http://bugs.python.org/issue10044 for the discussion. In short,
1288 no patch shown any impact on a realistic benchmark, only a minor
1289 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001290 if (PyUnicode_CheckExact(left) &&
1291 PyUnicode_CheckExact(right)) {
1292 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001293 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001294 }
1295 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001296 sum = PyNumber_Add(left, right);
1297 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001298 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001299 Py_DECREF(right);
1300 SET_TOP(sum);
1301 if (sum == NULL)
1302 goto error;
1303 DISPATCH();
1304 }
1305
1306 TARGET(BINARY_SUBTRACT) {
1307 PyObject *right = POP();
1308 PyObject *left = TOP();
1309 PyObject *diff = PyNumber_Subtract(left, right);
1310 Py_DECREF(right);
1311 Py_DECREF(left);
1312 SET_TOP(diff);
1313 if (diff == NULL)
1314 goto error;
1315 DISPATCH();
1316 }
1317
1318 TARGET(BINARY_SUBSCR) {
1319 PyObject *sub = POP();
1320 PyObject *container = TOP();
1321 PyObject *res = PyObject_GetItem(container, sub);
1322 Py_DECREF(container);
1323 Py_DECREF(sub);
1324 SET_TOP(res);
1325 if (res == NULL)
1326 goto error;
1327 DISPATCH();
1328 }
1329
1330 TARGET(BINARY_LSHIFT) {
1331 PyObject *right = POP();
1332 PyObject *left = TOP();
1333 PyObject *res = PyNumber_Lshift(left, right);
1334 Py_DECREF(left);
1335 Py_DECREF(right);
1336 SET_TOP(res);
1337 if (res == NULL)
1338 goto error;
1339 DISPATCH();
1340 }
1341
1342 TARGET(BINARY_RSHIFT) {
1343 PyObject *right = POP();
1344 PyObject *left = TOP();
1345 PyObject *res = PyNumber_Rshift(left, right);
1346 Py_DECREF(left);
1347 Py_DECREF(right);
1348 SET_TOP(res);
1349 if (res == NULL)
1350 goto error;
1351 DISPATCH();
1352 }
1353
1354 TARGET(BINARY_AND) {
1355 PyObject *right = POP();
1356 PyObject *left = TOP();
1357 PyObject *res = PyNumber_And(left, right);
1358 Py_DECREF(left);
1359 Py_DECREF(right);
1360 SET_TOP(res);
1361 if (res == NULL)
1362 goto error;
1363 DISPATCH();
1364 }
1365
1366 TARGET(BINARY_XOR) {
1367 PyObject *right = POP();
1368 PyObject *left = TOP();
1369 PyObject *res = PyNumber_Xor(left, right);
1370 Py_DECREF(left);
1371 Py_DECREF(right);
1372 SET_TOP(res);
1373 if (res == NULL)
1374 goto error;
1375 DISPATCH();
1376 }
1377
1378 TARGET(BINARY_OR) {
1379 PyObject *right = POP();
1380 PyObject *left = TOP();
1381 PyObject *res = PyNumber_Or(left, right);
1382 Py_DECREF(left);
1383 Py_DECREF(right);
1384 SET_TOP(res);
1385 if (res == NULL)
1386 goto error;
1387 DISPATCH();
1388 }
1389
1390 TARGET(LIST_APPEND) {
1391 PyObject *v = POP();
1392 PyObject *list = PEEK(oparg);
1393 int err;
1394 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001396 if (err != 0)
1397 goto error;
1398 PREDICT(JUMP_ABSOLUTE);
1399 DISPATCH();
1400 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001401
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001402 TARGET(SET_ADD) {
1403 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001404 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001405 int err;
1406 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001408 if (err != 0)
1409 goto error;
1410 PREDICT(JUMP_ABSOLUTE);
1411 DISPATCH();
1412 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001413
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001414 TARGET(INPLACE_POWER) {
1415 PyObject *exp = POP();
1416 PyObject *base = TOP();
1417 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1418 Py_DECREF(base);
1419 Py_DECREF(exp);
1420 SET_TOP(res);
1421 if (res == NULL)
1422 goto error;
1423 DISPATCH();
1424 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001425
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001426 TARGET(INPLACE_MULTIPLY) {
1427 PyObject *right = POP();
1428 PyObject *left = TOP();
1429 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1430 Py_DECREF(left);
1431 Py_DECREF(right);
1432 SET_TOP(res);
1433 if (res == NULL)
1434 goto error;
1435 DISPATCH();
1436 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001437
Benjamin Petersond51374e2014-04-09 23:55:56 -04001438 TARGET(INPLACE_MATRIX_MULTIPLY) {
1439 PyObject *right = POP();
1440 PyObject *left = TOP();
1441 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1442 Py_DECREF(left);
1443 Py_DECREF(right);
1444 SET_TOP(res);
1445 if (res == NULL)
1446 goto error;
1447 DISPATCH();
1448 }
1449
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001450 TARGET(INPLACE_TRUE_DIVIDE) {
1451 PyObject *divisor = POP();
1452 PyObject *dividend = TOP();
1453 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1454 Py_DECREF(dividend);
1455 Py_DECREF(divisor);
1456 SET_TOP(quotient);
1457 if (quotient == NULL)
1458 goto error;
1459 DISPATCH();
1460 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001461
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001462 TARGET(INPLACE_FLOOR_DIVIDE) {
1463 PyObject *divisor = POP();
1464 PyObject *dividend = TOP();
1465 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1466 Py_DECREF(dividend);
1467 Py_DECREF(divisor);
1468 SET_TOP(quotient);
1469 if (quotient == NULL)
1470 goto error;
1471 DISPATCH();
1472 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001473
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001474 TARGET(INPLACE_MODULO) {
1475 PyObject *right = POP();
1476 PyObject *left = TOP();
1477 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1478 Py_DECREF(left);
1479 Py_DECREF(right);
1480 SET_TOP(mod);
1481 if (mod == NULL)
1482 goto error;
1483 DISPATCH();
1484 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001485
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001486 TARGET(INPLACE_ADD) {
1487 PyObject *right = POP();
1488 PyObject *left = TOP();
1489 PyObject *sum;
1490 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1491 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001492 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001493 }
1494 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001495 sum = PyNumber_InPlaceAdd(left, right);
1496 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001497 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001498 Py_DECREF(right);
1499 SET_TOP(sum);
1500 if (sum == NULL)
1501 goto error;
1502 DISPATCH();
1503 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001504
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001505 TARGET(INPLACE_SUBTRACT) {
1506 PyObject *right = POP();
1507 PyObject *left = TOP();
1508 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1509 Py_DECREF(left);
1510 Py_DECREF(right);
1511 SET_TOP(diff);
1512 if (diff == NULL)
1513 goto error;
1514 DISPATCH();
1515 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001516
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001517 TARGET(INPLACE_LSHIFT) {
1518 PyObject *right = POP();
1519 PyObject *left = TOP();
1520 PyObject *res = PyNumber_InPlaceLshift(left, right);
1521 Py_DECREF(left);
1522 Py_DECREF(right);
1523 SET_TOP(res);
1524 if (res == NULL)
1525 goto error;
1526 DISPATCH();
1527 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001528
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001529 TARGET(INPLACE_RSHIFT) {
1530 PyObject *right = POP();
1531 PyObject *left = TOP();
1532 PyObject *res = PyNumber_InPlaceRshift(left, right);
1533 Py_DECREF(left);
1534 Py_DECREF(right);
1535 SET_TOP(res);
1536 if (res == NULL)
1537 goto error;
1538 DISPATCH();
1539 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001540
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001541 TARGET(INPLACE_AND) {
1542 PyObject *right = POP();
1543 PyObject *left = TOP();
1544 PyObject *res = PyNumber_InPlaceAnd(left, right);
1545 Py_DECREF(left);
1546 Py_DECREF(right);
1547 SET_TOP(res);
1548 if (res == NULL)
1549 goto error;
1550 DISPATCH();
1551 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001552
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001553 TARGET(INPLACE_XOR) {
1554 PyObject *right = POP();
1555 PyObject *left = TOP();
1556 PyObject *res = PyNumber_InPlaceXor(left, right);
1557 Py_DECREF(left);
1558 Py_DECREF(right);
1559 SET_TOP(res);
1560 if (res == NULL)
1561 goto error;
1562 DISPATCH();
1563 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001564
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001565 TARGET(INPLACE_OR) {
1566 PyObject *right = POP();
1567 PyObject *left = TOP();
1568 PyObject *res = PyNumber_InPlaceOr(left, right);
1569 Py_DECREF(left);
1570 Py_DECREF(right);
1571 SET_TOP(res);
1572 if (res == NULL)
1573 goto error;
1574 DISPATCH();
1575 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001576
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001577 TARGET(STORE_SUBSCR) {
1578 PyObject *sub = TOP();
1579 PyObject *container = SECOND();
1580 PyObject *v = THIRD();
1581 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001582 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001583 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001584 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001586 Py_DECREF(container);
1587 Py_DECREF(sub);
1588 if (err != 0)
1589 goto error;
1590 DISPATCH();
1591 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001592
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001593 TARGET(DELETE_SUBSCR) {
1594 PyObject *sub = TOP();
1595 PyObject *container = SECOND();
1596 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001597 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001598 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001599 err = PyObject_DelItem(container, sub);
1600 Py_DECREF(container);
1601 Py_DECREF(sub);
1602 if (err != 0)
1603 goto error;
1604 DISPATCH();
1605 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001606
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001607 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001608 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001609 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001610 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001611 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001612 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 PyErr_SetString(PyExc_RuntimeError,
1614 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001615 Py_DECREF(value);
1616 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001618 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001619 Py_DECREF(value);
1620 if (res == NULL)
1621 goto error;
1622 Py_DECREF(res);
1623 DISPATCH();
1624 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001625
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001626 TARGET(RAISE_VARARGS) {
1627 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 switch (oparg) {
1629 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001630 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001631 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001633 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001634 /* fall through */
1635 case 0:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001636 if (do_raise(exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001637 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001638 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 break;
1640 default:
1641 PyErr_SetString(PyExc_SystemError,
1642 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001643 break;
1644 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001645 goto error;
1646 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001647
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001648 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001650 assert(f->f_iblock == 0);
1651 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001652 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001653
Yury Selivanov75445082015-05-11 22:57:16 -04001654 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001655 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001656 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001657 PyObject *obj = TOP();
1658 PyTypeObject *type = Py_TYPE(obj);
1659
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001660 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001661 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001662 }
Yury Selivanov75445082015-05-11 22:57:16 -04001663
1664 if (getter != NULL) {
1665 iter = (*getter)(obj);
1666 Py_DECREF(obj);
1667 if (iter == NULL) {
1668 SET_TOP(NULL);
1669 goto error;
1670 }
1671 }
1672 else {
1673 SET_TOP(NULL);
1674 PyErr_Format(
1675 PyExc_TypeError,
1676 "'async for' requires an object with "
1677 "__aiter__ method, got %.100s",
1678 type->tp_name);
1679 Py_DECREF(obj);
1680 goto error;
1681 }
1682
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001683 if (Py_TYPE(iter)->tp_as_async == NULL ||
1684 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001685
Yury Selivanov398ff912017-03-02 22:20:00 -05001686 SET_TOP(NULL);
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001687 PyErr_Format(
1688 PyExc_TypeError,
1689 "'async for' received an object from __aiter__ "
1690 "that does not implement __anext__: %.100s",
1691 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001692 Py_DECREF(iter);
1693 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001694 }
1695
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001696 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001697 DISPATCH();
1698 }
1699
1700 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001701 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001702 PyObject *next_iter = NULL;
1703 PyObject *awaitable = NULL;
1704 PyObject *aiter = TOP();
1705 PyTypeObject *type = Py_TYPE(aiter);
1706
Yury Selivanoveb636452016-09-08 22:01:51 -07001707 if (PyAsyncGen_CheckExact(aiter)) {
1708 awaitable = type->tp_as_async->am_anext(aiter);
1709 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001710 goto error;
1711 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001712 } else {
1713 if (type->tp_as_async != NULL){
1714 getter = type->tp_as_async->am_anext;
1715 }
Yury Selivanov75445082015-05-11 22:57:16 -04001716
Yury Selivanoveb636452016-09-08 22:01:51 -07001717 if (getter != NULL) {
1718 next_iter = (*getter)(aiter);
1719 if (next_iter == NULL) {
1720 goto error;
1721 }
1722 }
1723 else {
1724 PyErr_Format(
1725 PyExc_TypeError,
1726 "'async for' requires an iterator with "
1727 "__anext__ method, got %.100s",
1728 type->tp_name);
1729 goto error;
1730 }
Yury Selivanov75445082015-05-11 22:57:16 -04001731
Yury Selivanoveb636452016-09-08 22:01:51 -07001732 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1733 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001734 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001735 PyExc_TypeError,
1736 "'async for' received an invalid object "
1737 "from __anext__: %.100s",
1738 Py_TYPE(next_iter)->tp_name);
1739
1740 Py_DECREF(next_iter);
1741 goto error;
1742 } else {
1743 Py_DECREF(next_iter);
1744 }
1745 }
Yury Selivanov75445082015-05-11 22:57:16 -04001746
1747 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001748 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001749 DISPATCH();
1750 }
1751
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001752 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001753 TARGET(GET_AWAITABLE) {
1754 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001755 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001756
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03001757 if (iter == NULL) {
1758 format_awaitable_error(Py_TYPE(iterable),
1759 _Py_OPCODE(next_instr[-2]));
1760 }
1761
Yury Selivanov75445082015-05-11 22:57:16 -04001762 Py_DECREF(iterable);
1763
Yury Selivanovc724bae2016-03-02 11:30:46 -05001764 if (iter != NULL && PyCoro_CheckExact(iter)) {
1765 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1766 if (yf != NULL) {
1767 /* `iter` is a coroutine object that is being
1768 awaited, `yf` is a pointer to the current awaitable
1769 being awaited on. */
1770 Py_DECREF(yf);
1771 Py_CLEAR(iter);
1772 PyErr_SetString(
1773 PyExc_RuntimeError,
1774 "coroutine is being awaited already");
1775 /* The code below jumps to `error` if `iter` is NULL. */
1776 }
1777 }
1778
Yury Selivanov75445082015-05-11 22:57:16 -04001779 SET_TOP(iter); /* Even if it's NULL */
1780
1781 if (iter == NULL) {
1782 goto error;
1783 }
1784
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001785 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001786 DISPATCH();
1787 }
1788
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001789 TARGET(YIELD_FROM) {
1790 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001791 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001792 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001793 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1794 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001795 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001796 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001797 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001798 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001799 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001800 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001801 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001802 Py_DECREF(v);
1803 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001804 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001805 if (tstate->c_tracefunc != NULL
1806 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001807 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001808 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001809 if (err < 0)
1810 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001811 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001812 SET_TOP(val);
1813 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001814 }
Martin Panter95f53c12016-07-18 08:23:26 +00001815 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001816 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001817 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001818 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001819 f->f_lasti -= sizeof(_Py_CODEUNIT);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001820 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001821 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001822
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001823 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07001825
1826 if (co->co_flags & CO_ASYNC_GENERATOR) {
1827 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
1828 Py_DECREF(retval);
1829 if (w == NULL) {
1830 retval = NULL;
1831 goto error;
1832 }
1833 retval = w;
1834 }
1835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 f->f_stacktop = stack_pointer;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001837 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001838 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001839
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001840 TARGET(POP_EXCEPT) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001841 PyObject *type, *value, *traceback;
1842 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001843 PyTryBlock *b = PyFrame_BlockPop(f);
1844 if (b->b_type != EXCEPT_HANDLER) {
1845 PyErr_SetString(PyExc_SystemError,
1846 "popped block is not an except handler");
1847 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001848 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001849 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
1850 STACK_LEVEL() <= (b)->b_level + 4);
1851 exc_info = tstate->exc_info;
1852 type = exc_info->exc_type;
1853 value = exc_info->exc_value;
1854 traceback = exc_info->exc_traceback;
1855 exc_info->exc_type = POP();
1856 exc_info->exc_value = POP();
1857 exc_info->exc_traceback = POP();
1858 Py_XDECREF(type);
1859 Py_XDECREF(value);
1860 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001861 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001862 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001863
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001864 PREDICTED(POP_BLOCK);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001865 TARGET(POP_BLOCK) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001866 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001868 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001869
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001870 TARGET(POP_FINALLY) {
1871 /* If oparg is 0 at the top of the stack are 1 or 6 values:
1872 Either:
1873 - TOP = NULL or an integer
1874 or:
1875 - (TOP, SECOND, THIRD) = exc_info()
1876 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
1877
1878 If oparg is 1 the value for 'return' was additionally pushed
1879 at the top of the stack.
1880 */
1881 PyObject *res = NULL;
1882 if (oparg) {
1883 res = POP();
1884 }
1885 PyObject *exc = POP();
1886 if (exc == NULL || PyLong_CheckExact(exc)) {
1887 Py_XDECREF(exc);
1888 }
1889 else {
1890 Py_DECREF(exc);
1891 Py_DECREF(POP());
1892 Py_DECREF(POP());
1893
1894 PyObject *type, *value, *traceback;
1895 _PyErr_StackItem *exc_info;
1896 PyTryBlock *b = PyFrame_BlockPop(f);
1897 if (b->b_type != EXCEPT_HANDLER) {
1898 PyErr_SetString(PyExc_SystemError,
1899 "popped block is not an except handler");
1900 Py_XDECREF(res);
1901 goto error;
1902 }
1903 assert(STACK_LEVEL() == (b)->b_level + 3);
1904 exc_info = tstate->exc_info;
1905 type = exc_info->exc_type;
1906 value = exc_info->exc_value;
1907 traceback = exc_info->exc_traceback;
1908 exc_info->exc_type = POP();
1909 exc_info->exc_value = POP();
1910 exc_info->exc_traceback = POP();
1911 Py_XDECREF(type);
1912 Py_XDECREF(value);
1913 Py_XDECREF(traceback);
1914 }
1915 if (oparg) {
1916 PUSH(res);
1917 }
1918 DISPATCH();
1919 }
1920
1921 TARGET(CALL_FINALLY) {
1922 PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
1923 if (ret == NULL) {
1924 goto error;
1925 }
1926 PUSH(ret);
1927 JUMPBY(oparg);
1928 FAST_DISPATCH();
1929 }
1930
1931 TARGET(BEGIN_FINALLY) {
1932 /* Push NULL onto the stack for using it in END_FINALLY,
1933 POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH.
1934 */
1935 PUSH(NULL);
1936 FAST_DISPATCH();
1937 }
1938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001939 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001940 TARGET(END_FINALLY) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001941 /* At the top of the stack are 1 or 6 values:
1942 Either:
1943 - TOP = NULL or an integer
1944 or:
1945 - (TOP, SECOND, THIRD) = exc_info()
1946 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
1947 */
1948 PyObject *exc = POP();
1949 if (exc == NULL) {
1950 FAST_DISPATCH();
1951 }
1952 else if (PyLong_CheckExact(exc)) {
1953 int ret = _PyLong_AsInt(exc);
1954 Py_DECREF(exc);
1955 if (ret == -1 && PyErr_Occurred()) {
1956 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001958 JUMPTO(ret);
1959 FAST_DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001961 else {
1962 assert(PyExceptionClass_Check(exc));
1963 PyObject *val = POP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001964 PyObject *tb = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001965 PyErr_Restore(exc, val, tb);
1966 goto exception_unwind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001968 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001969
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02001970 TARGET(END_ASYNC_FOR) {
1971 PyObject *exc = POP();
1972 assert(PyExceptionClass_Check(exc));
1973 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
1974 PyTryBlock *b = PyFrame_BlockPop(f);
1975 assert(b->b_type == EXCEPT_HANDLER);
1976 Py_DECREF(exc);
1977 UNWIND_EXCEPT_HANDLER(b);
1978 Py_DECREF(POP());
1979 JUMPBY(oparg);
1980 FAST_DISPATCH();
1981 }
1982 else {
1983 PyObject *val = POP();
1984 PyObject *tb = POP();
1985 PyErr_Restore(exc, val, tb);
1986 goto exception_unwind;
1987 }
1988 }
1989
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001990 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02001991 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001992
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001993 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001994 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001995 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
1996 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02001997 PyErr_SetString(PyExc_NameError,
1998 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001999 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002000 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002001 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002002 }
2003 else {
2004 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2005 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002006 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002007 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2008 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002009 if (PyErr_ExceptionMatches(PyExc_KeyError))
2010 PyErr_SetString(PyExc_NameError,
2011 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002012 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002013 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002014 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002015 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002016 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002017 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002018
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002019 TARGET(STORE_NAME) {
2020 PyObject *name = GETITEM(names, oparg);
2021 PyObject *v = POP();
2022 PyObject *ns = f->f_locals;
2023 int err;
2024 if (ns == NULL) {
2025 PyErr_Format(PyExc_SystemError,
2026 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002028 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002029 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002030 if (PyDict_CheckExact(ns))
2031 err = PyDict_SetItem(ns, name, v);
2032 else
2033 err = PyObject_SetItem(ns, name, v);
2034 Py_DECREF(v);
2035 if (err != 0)
2036 goto error;
2037 DISPATCH();
2038 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002039
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002040 TARGET(DELETE_NAME) {
2041 PyObject *name = GETITEM(names, oparg);
2042 PyObject *ns = f->f_locals;
2043 int err;
2044 if (ns == NULL) {
2045 PyErr_Format(PyExc_SystemError,
2046 "no locals when deleting %R", name);
2047 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002049 err = PyObject_DelItem(ns, name);
2050 if (err != 0) {
2051 format_exc_check_arg(PyExc_NameError,
2052 NAME_ERROR_MSG,
2053 name);
2054 goto error;
2055 }
2056 DISPATCH();
2057 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002058
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002059 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002060 TARGET(UNPACK_SEQUENCE) {
2061 PyObject *seq = POP(), *item, **items;
2062 if (PyTuple_CheckExact(seq) &&
2063 PyTuple_GET_SIZE(seq) == oparg) {
2064 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002065 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002066 item = items[oparg];
2067 Py_INCREF(item);
2068 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002070 } else if (PyList_CheckExact(seq) &&
2071 PyList_GET_SIZE(seq) == oparg) {
2072 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002074 item = items[oparg];
2075 Py_INCREF(item);
2076 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002078 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002079 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002080 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002081 } else {
2082 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002083 Py_DECREF(seq);
2084 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002086 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002087 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002089
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002090 TARGET(UNPACK_EX) {
2091 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2092 PyObject *seq = POP();
2093
2094 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2095 stack_pointer + totalargs)) {
2096 stack_pointer += totalargs;
2097 } else {
2098 Py_DECREF(seq);
2099 goto error;
2100 }
2101 Py_DECREF(seq);
2102 DISPATCH();
2103 }
2104
2105 TARGET(STORE_ATTR) {
2106 PyObject *name = GETITEM(names, oparg);
2107 PyObject *owner = TOP();
2108 PyObject *v = SECOND();
2109 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002110 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002111 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002113 Py_DECREF(owner);
2114 if (err != 0)
2115 goto error;
2116 DISPATCH();
2117 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002118
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002119 TARGET(DELETE_ATTR) {
2120 PyObject *name = GETITEM(names, oparg);
2121 PyObject *owner = POP();
2122 int err;
2123 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2124 Py_DECREF(owner);
2125 if (err != 0)
2126 goto error;
2127 DISPATCH();
2128 }
2129
2130 TARGET(STORE_GLOBAL) {
2131 PyObject *name = GETITEM(names, oparg);
2132 PyObject *v = POP();
2133 int err;
2134 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002135 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002136 if (err != 0)
2137 goto error;
2138 DISPATCH();
2139 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002140
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002141 TARGET(DELETE_GLOBAL) {
2142 PyObject *name = GETITEM(names, oparg);
2143 int err;
2144 err = PyDict_DelItem(f->f_globals, name);
2145 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002146 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002147 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002148 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002149 }
2150 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002151 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002152
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002153 TARGET(LOAD_NAME) {
2154 PyObject *name = GETITEM(names, oparg);
2155 PyObject *locals = f->f_locals;
2156 PyObject *v;
2157 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002158 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002159 "no locals when loading %R", name);
2160 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002161 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002162 if (PyDict_CheckExact(locals)) {
2163 v = PyDict_GetItem(locals, name);
2164 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165 }
2166 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002167 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002168 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002169 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2170 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002171 PyErr_Clear();
2172 }
2173 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002174 if (v == NULL) {
2175 v = PyDict_GetItem(f->f_globals, name);
2176 Py_XINCREF(v);
2177 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002178 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002179 v = PyDict_GetItem(f->f_builtins, name);
2180 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002181 format_exc_check_arg(
2182 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002183 NAME_ERROR_MSG, name);
2184 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002185 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002186 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002187 }
2188 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002189 v = PyObject_GetItem(f->f_builtins, name);
2190 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002191 if (PyErr_ExceptionMatches(PyExc_KeyError))
2192 format_exc_check_arg(
2193 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002194 NAME_ERROR_MSG, name);
2195 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002196 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002197 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002200 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002201 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002202 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002203
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002204 TARGET(LOAD_GLOBAL) {
2205 PyObject *name = GETITEM(names, oparg);
2206 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002207 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002208 && PyDict_CheckExact(f->f_builtins))
2209 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002210 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002211 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002212 name);
2213 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002214 if (!_PyErr_OCCURRED()) {
2215 /* _PyDict_LoadGlobal() returns NULL without raising
2216 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002217 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002218 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002219 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002220 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002221 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002222 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002223 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002224 else {
2225 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002226
2227 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002228 v = PyObject_GetItem(f->f_globals, name);
2229 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002230 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2231 goto error;
2232 PyErr_Clear();
2233
Victor Stinnerb4efc962015-11-20 09:24:02 +01002234 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002235 v = PyObject_GetItem(f->f_builtins, name);
2236 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002237 if (PyErr_ExceptionMatches(PyExc_KeyError))
2238 format_exc_check_arg(
2239 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002240 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002241 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002242 }
2243 }
2244 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002245 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002246 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002247 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002248
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002249 TARGET(DELETE_FAST) {
2250 PyObject *v = GETLOCAL(oparg);
2251 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002252 SETLOCAL(oparg, NULL);
2253 DISPATCH();
2254 }
2255 format_exc_check_arg(
2256 PyExc_UnboundLocalError,
2257 UNBOUNDLOCAL_ERROR_MSG,
2258 PyTuple_GetItem(co->co_varnames, oparg)
2259 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002260 goto error;
2261 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002262
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002263 TARGET(DELETE_DEREF) {
2264 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002265 PyObject *oldobj = PyCell_GET(cell);
2266 if (oldobj != NULL) {
2267 PyCell_SET(cell, NULL);
2268 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002269 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002270 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002271 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002272 goto error;
2273 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002274
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002275 TARGET(LOAD_CLOSURE) {
2276 PyObject *cell = freevars[oparg];
2277 Py_INCREF(cell);
2278 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002279 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002280 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002281
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002282 TARGET(LOAD_CLASSDEREF) {
2283 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002284 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002285 assert(locals);
2286 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2287 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2288 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2289 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2290 if (PyDict_CheckExact(locals)) {
2291 value = PyDict_GetItem(locals, name);
2292 Py_XINCREF(value);
2293 }
2294 else {
2295 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002296 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002297 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2298 goto error;
2299 PyErr_Clear();
2300 }
2301 }
2302 if (!value) {
2303 PyObject *cell = freevars[oparg];
2304 value = PyCell_GET(cell);
2305 if (value == NULL) {
2306 format_exc_unbound(co, oparg);
2307 goto error;
2308 }
2309 Py_INCREF(value);
2310 }
2311 PUSH(value);
2312 DISPATCH();
2313 }
2314
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002315 TARGET(LOAD_DEREF) {
2316 PyObject *cell = freevars[oparg];
2317 PyObject *value = PyCell_GET(cell);
2318 if (value == NULL) {
2319 format_exc_unbound(co, oparg);
2320 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002321 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002322 Py_INCREF(value);
2323 PUSH(value);
2324 DISPATCH();
2325 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002326
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002327 TARGET(STORE_DEREF) {
2328 PyObject *v = POP();
2329 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002330 PyObject *oldobj = PyCell_GET(cell);
2331 PyCell_SET(cell, v);
2332 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002333 DISPATCH();
2334 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002335
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002336 TARGET(BUILD_STRING) {
2337 PyObject *str;
2338 PyObject *empty = PyUnicode_New(0, 0);
2339 if (empty == NULL) {
2340 goto error;
2341 }
2342 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2343 Py_DECREF(empty);
2344 if (str == NULL)
2345 goto error;
2346 while (--oparg >= 0) {
2347 PyObject *item = POP();
2348 Py_DECREF(item);
2349 }
2350 PUSH(str);
2351 DISPATCH();
2352 }
2353
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002354 TARGET(BUILD_TUPLE) {
2355 PyObject *tup = PyTuple_New(oparg);
2356 if (tup == NULL)
2357 goto error;
2358 while (--oparg >= 0) {
2359 PyObject *item = POP();
2360 PyTuple_SET_ITEM(tup, oparg, item);
2361 }
2362 PUSH(tup);
2363 DISPATCH();
2364 }
2365
2366 TARGET(BUILD_LIST) {
2367 PyObject *list = PyList_New(oparg);
2368 if (list == NULL)
2369 goto error;
2370 while (--oparg >= 0) {
2371 PyObject *item = POP();
2372 PyList_SET_ITEM(list, oparg, item);
2373 }
2374 PUSH(list);
2375 DISPATCH();
2376 }
2377
Serhiy Storchaka73442852016-10-02 10:33:46 +03002378 TARGET(BUILD_TUPLE_UNPACK_WITH_CALL)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002379 TARGET(BUILD_TUPLE_UNPACK)
2380 TARGET(BUILD_LIST_UNPACK) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002381 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002382 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002383 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002384 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002385
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002386 if (sum == NULL)
2387 goto error;
2388
2389 for (i = oparg; i > 0; i--) {
2390 PyObject *none_val;
2391
2392 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2393 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002394 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002395 PyErr_ExceptionMatches(PyExc_TypeError))
2396 {
2397 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002398 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002399 Py_DECREF(sum);
2400 goto error;
2401 }
2402 Py_DECREF(none_val);
2403 }
2404
2405 if (convert_to_tuple) {
2406 return_value = PyList_AsTuple(sum);
2407 Py_DECREF(sum);
2408 if (return_value == NULL)
2409 goto error;
2410 }
2411 else {
2412 return_value = sum;
2413 }
2414
2415 while (oparg--)
2416 Py_DECREF(POP());
2417 PUSH(return_value);
2418 DISPATCH();
2419 }
2420
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002421 TARGET(BUILD_SET) {
2422 PyObject *set = PySet_New(NULL);
2423 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002424 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002425 if (set == NULL)
2426 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002427 for (i = oparg; i > 0; i--) {
2428 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002429 if (err == 0)
2430 err = PySet_Add(set, item);
2431 Py_DECREF(item);
2432 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002433 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002434 if (err != 0) {
2435 Py_DECREF(set);
2436 goto error;
2437 }
2438 PUSH(set);
2439 DISPATCH();
2440 }
2441
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002442 TARGET(BUILD_SET_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002443 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002444 PyObject *sum = PySet_New(NULL);
2445 if (sum == NULL)
2446 goto error;
2447
2448 for (i = oparg; i > 0; i--) {
2449 if (_PySet_Update(sum, PEEK(i)) < 0) {
2450 Py_DECREF(sum);
2451 goto error;
2452 }
2453 }
2454
2455 while (oparg--)
2456 Py_DECREF(POP());
2457 PUSH(sum);
2458 DISPATCH();
2459 }
2460
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002461 TARGET(BUILD_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002462 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002463 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2464 if (map == NULL)
2465 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002466 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002467 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002468 PyObject *key = PEEK(2*i);
2469 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002470 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002471 if (err != 0) {
2472 Py_DECREF(map);
2473 goto error;
2474 }
2475 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002476
2477 while (oparg--) {
2478 Py_DECREF(POP());
2479 Py_DECREF(POP());
2480 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002481 PUSH(map);
2482 DISPATCH();
2483 }
2484
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002485 TARGET(SETUP_ANNOTATIONS) {
2486 _Py_IDENTIFIER(__annotations__);
2487 int err;
2488 PyObject *ann_dict;
2489 if (f->f_locals == NULL) {
2490 PyErr_Format(PyExc_SystemError,
2491 "no locals found when setting up annotations");
2492 goto error;
2493 }
2494 /* check if __annotations__ in locals()... */
2495 if (PyDict_CheckExact(f->f_locals)) {
2496 ann_dict = _PyDict_GetItemId(f->f_locals,
2497 &PyId___annotations__);
2498 if (ann_dict == NULL) {
2499 /* ...if not, create a new one */
2500 ann_dict = PyDict_New();
2501 if (ann_dict == NULL) {
2502 goto error;
2503 }
2504 err = _PyDict_SetItemId(f->f_locals,
2505 &PyId___annotations__, ann_dict);
2506 Py_DECREF(ann_dict);
2507 if (err != 0) {
2508 goto error;
2509 }
2510 }
2511 }
2512 else {
2513 /* do the same if locals() is not a dict */
2514 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2515 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002516 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002517 }
2518 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2519 if (ann_dict == NULL) {
2520 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2521 goto error;
2522 }
2523 PyErr_Clear();
2524 ann_dict = PyDict_New();
2525 if (ann_dict == NULL) {
2526 goto error;
2527 }
2528 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2529 Py_DECREF(ann_dict);
2530 if (err != 0) {
2531 goto error;
2532 }
2533 }
2534 else {
2535 Py_DECREF(ann_dict);
2536 }
2537 }
2538 DISPATCH();
2539 }
2540
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002541 TARGET(BUILD_CONST_KEY_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002542 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002543 PyObject *map;
2544 PyObject *keys = TOP();
2545 if (!PyTuple_CheckExact(keys) ||
2546 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2547 PyErr_SetString(PyExc_SystemError,
2548 "bad BUILD_CONST_KEY_MAP keys argument");
2549 goto error;
2550 }
2551 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2552 if (map == NULL) {
2553 goto error;
2554 }
2555 for (i = oparg; i > 0; i--) {
2556 int err;
2557 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2558 PyObject *value = PEEK(i + 1);
2559 err = PyDict_SetItem(map, key, value);
2560 if (err != 0) {
2561 Py_DECREF(map);
2562 goto error;
2563 }
2564 }
2565
2566 Py_DECREF(POP());
2567 while (oparg--) {
2568 Py_DECREF(POP());
2569 }
2570 PUSH(map);
2571 DISPATCH();
2572 }
2573
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002574 TARGET(BUILD_MAP_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002575 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002576 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002577 if (sum == NULL)
2578 goto error;
2579
2580 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002581 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002582 if (PyDict_Update(sum, arg) < 0) {
2583 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2584 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002585 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002586 arg->ob_type->tp_name);
2587 }
2588 Py_DECREF(sum);
2589 goto error;
2590 }
2591 }
2592
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002593 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002594 Py_DECREF(POP());
2595 PUSH(sum);
2596 DISPATCH();
2597 }
2598
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002599 TARGET(BUILD_MAP_UNPACK_WITH_CALL) {
2600 Py_ssize_t i;
2601 PyObject *sum = PyDict_New();
2602 if (sum == NULL)
2603 goto error;
2604
2605 for (i = oparg; i > 0; i--) {
2606 PyObject *arg = PEEK(i);
2607 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
2608 PyObject *func = PEEK(2 + oparg);
2609 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002610 format_kwargs_mapping_error(func, arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002611 }
2612 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2613 PyObject *exc, *val, *tb;
2614 PyErr_Fetch(&exc, &val, &tb);
2615 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
2616 PyObject *key = PyTuple_GET_ITEM(val, 0);
2617 if (!PyUnicode_Check(key)) {
2618 PyErr_Format(PyExc_TypeError,
2619 "%.200s%.200s keywords must be strings",
2620 PyEval_GetFuncName(func),
2621 PyEval_GetFuncDesc(func));
2622 } else {
2623 PyErr_Format(PyExc_TypeError,
2624 "%.200s%.200s got multiple "
2625 "values for keyword argument '%U'",
2626 PyEval_GetFuncName(func),
2627 PyEval_GetFuncDesc(func),
2628 key);
2629 }
2630 Py_XDECREF(exc);
2631 Py_XDECREF(val);
2632 Py_XDECREF(tb);
2633 }
2634 else {
2635 PyErr_Restore(exc, val, tb);
2636 }
2637 }
2638 Py_DECREF(sum);
2639 goto error;
2640 }
2641 }
2642
2643 while (oparg--)
2644 Py_DECREF(POP());
2645 PUSH(sum);
2646 DISPATCH();
2647 }
2648
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002649 TARGET(MAP_ADD) {
2650 PyObject *key = TOP();
2651 PyObject *value = SECOND();
2652 PyObject *map;
2653 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002654 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002655 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002656 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002657 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002658 Py_DECREF(value);
2659 Py_DECREF(key);
2660 if (err != 0)
2661 goto error;
2662 PREDICT(JUMP_ABSOLUTE);
2663 DISPATCH();
2664 }
2665
2666 TARGET(LOAD_ATTR) {
2667 PyObject *name = GETITEM(names, oparg);
2668 PyObject *owner = TOP();
2669 PyObject *res = PyObject_GetAttr(owner, name);
2670 Py_DECREF(owner);
2671 SET_TOP(res);
2672 if (res == NULL)
2673 goto error;
2674 DISPATCH();
2675 }
2676
2677 TARGET(COMPARE_OP) {
2678 PyObject *right = POP();
2679 PyObject *left = TOP();
2680 PyObject *res = cmp_outcome(oparg, left, right);
2681 Py_DECREF(left);
2682 Py_DECREF(right);
2683 SET_TOP(res);
2684 if (res == NULL)
2685 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002686 PREDICT(POP_JUMP_IF_FALSE);
2687 PREDICT(POP_JUMP_IF_TRUE);
2688 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002689 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002690
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002691 TARGET(IMPORT_NAME) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002692 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002693 PyObject *fromlist = POP();
2694 PyObject *level = TOP();
2695 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002696 res = import_name(f, name, fromlist, level);
2697 Py_DECREF(level);
2698 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002699 SET_TOP(res);
2700 if (res == NULL)
2701 goto error;
2702 DISPATCH();
2703 }
2704
2705 TARGET(IMPORT_STAR) {
2706 PyObject *from = POP(), *locals;
2707 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002708 if (PyFrame_FastToLocalsWithError(f) < 0) {
2709 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002710 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002711 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002712
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002713 locals = f->f_locals;
2714 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002715 PyErr_SetString(PyExc_SystemError,
2716 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002717 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002718 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002719 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002720 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002721 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002722 Py_DECREF(from);
2723 if (err != 0)
2724 goto error;
2725 DISPATCH();
2726 }
Guido van Rossum25831651993-05-19 14:50:45 +00002727
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002728 TARGET(IMPORT_FROM) {
2729 PyObject *name = GETITEM(names, oparg);
2730 PyObject *from = TOP();
2731 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002732 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002733 PUSH(res);
2734 if (res == NULL)
2735 goto error;
2736 DISPATCH();
2737 }
Thomas Wouters52152252000-08-17 22:55:00 +00002738
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002739 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002740 JUMPBY(oparg);
2741 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002742 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002743
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002744 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002745 TARGET(POP_JUMP_IF_FALSE) {
2746 PyObject *cond = POP();
2747 int err;
2748 if (cond == Py_True) {
2749 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002750 FAST_DISPATCH();
2751 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002752 if (cond == Py_False) {
2753 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002754 JUMPTO(oparg);
2755 FAST_DISPATCH();
2756 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002757 err = PyObject_IsTrue(cond);
2758 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002759 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002760 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002761 else if (err == 0)
2762 JUMPTO(oparg);
2763 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002764 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002765 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002766 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002767
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002768 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002769 TARGET(POP_JUMP_IF_TRUE) {
2770 PyObject *cond = POP();
2771 int err;
2772 if (cond == Py_False) {
2773 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002774 FAST_DISPATCH();
2775 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002776 if (cond == Py_True) {
2777 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002778 JUMPTO(oparg);
2779 FAST_DISPATCH();
2780 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002781 err = PyObject_IsTrue(cond);
2782 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002783 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002784 JUMPTO(oparg);
2785 }
2786 else if (err == 0)
2787 ;
2788 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002789 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002790 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002791 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002792
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002793 TARGET(JUMP_IF_FALSE_OR_POP) {
2794 PyObject *cond = TOP();
2795 int err;
2796 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002797 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002798 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002799 FAST_DISPATCH();
2800 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002801 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002802 JUMPTO(oparg);
2803 FAST_DISPATCH();
2804 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002805 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002806 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002807 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002808 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002809 }
2810 else if (err == 0)
2811 JUMPTO(oparg);
2812 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002813 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002815 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002816
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002817 TARGET(JUMP_IF_TRUE_OR_POP) {
2818 PyObject *cond = TOP();
2819 int err;
2820 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002821 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002822 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002823 FAST_DISPATCH();
2824 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002825 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002826 JUMPTO(oparg);
2827 FAST_DISPATCH();
2828 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002829 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002831 JUMPTO(oparg);
2832 }
2833 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002834 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002835 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002836 }
2837 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002838 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002839 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002840 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002841
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002842 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002843 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002844 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002845#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846 /* Enabling this path speeds-up all while and for-loops by bypassing
2847 the per-loop checks for signals. By default, this should be turned-off
2848 because it prevents detection of a control-break in tight loops like
2849 "while 1: pass". Compile with this option turned-on when you need
2850 the speed-up and do not need break checking inside tight loops (ones
2851 that contain only instructions ending with FAST_DISPATCH).
2852 */
2853 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002854#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002855 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002856#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002857 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002858
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002859 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002860 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002861 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002862 PyObject *iter = PyObject_GetIter(iterable);
2863 Py_DECREF(iterable);
2864 SET_TOP(iter);
2865 if (iter == NULL)
2866 goto error;
2867 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002868 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002869 DISPATCH();
2870 }
2871
2872 TARGET(GET_YIELD_FROM_ITER) {
2873 /* before: [obj]; after [getiter(obj)] */
2874 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002875 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002876 if (PyCoro_CheckExact(iterable)) {
2877 /* `iterable` is a coroutine */
2878 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2879 /* and it is used in a 'yield from' expression of a
2880 regular generator. */
2881 Py_DECREF(iterable);
2882 SET_TOP(NULL);
2883 PyErr_SetString(PyExc_TypeError,
2884 "cannot 'yield from' a coroutine object "
2885 "in a non-coroutine generator");
2886 goto error;
2887 }
2888 }
2889 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002890 /* `iterable` is not a generator. */
2891 iter = PyObject_GetIter(iterable);
2892 Py_DECREF(iterable);
2893 SET_TOP(iter);
2894 if (iter == NULL)
2895 goto error;
2896 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002897 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002898 DISPATCH();
2899 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002900
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002901 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002902 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002904 PyObject *iter = TOP();
2905 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2906 if (next != NULL) {
2907 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 PREDICT(STORE_FAST);
2909 PREDICT(UNPACK_SEQUENCE);
2910 DISPATCH();
2911 }
2912 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002913 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2914 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08002915 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002916 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 PyErr_Clear();
2918 }
2919 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00002920 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002921 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002922 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002923 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002925 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002926
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002927 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002928 /* NOTE: If you add any new block-setup opcodes that
2929 are not try/except/finally handlers, you may need
2930 to update the PyGen_NeedsFinalizing() function.
2931 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002932
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002933 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002934 STACK_LEVEL());
2935 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002936 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002937
Yury Selivanov75445082015-05-11 22:57:16 -04002938 TARGET(BEFORE_ASYNC_WITH) {
2939 _Py_IDENTIFIER(__aexit__);
2940 _Py_IDENTIFIER(__aenter__);
2941
2942 PyObject *mgr = TOP();
2943 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
2944 *enter;
2945 PyObject *res;
2946 if (exit == NULL)
2947 goto error;
2948 SET_TOP(exit);
2949 enter = special_lookup(mgr, &PyId___aenter__);
2950 Py_DECREF(mgr);
2951 if (enter == NULL)
2952 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002953 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04002954 Py_DECREF(enter);
2955 if (res == NULL)
2956 goto error;
2957 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002958 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002959 DISPATCH();
2960 }
2961
2962 TARGET(SETUP_ASYNC_WITH) {
2963 PyObject *res = POP();
2964 /* Setup the finally block before pushing the result
2965 of __aenter__ on the stack. */
2966 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2967 STACK_LEVEL());
2968 PUSH(res);
2969 DISPATCH();
2970 }
2971
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002972 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05002973 _Py_IDENTIFIER(__exit__);
2974 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002975 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08002976 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002977 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08002978 if (enter == NULL)
2979 goto error;
2980 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08002981 if (exit == NULL) {
2982 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002983 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08002984 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002985 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002986 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002987 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002988 Py_DECREF(enter);
2989 if (res == NULL)
2990 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002991 /* Setup the finally block before pushing the result
2992 of __enter__ on the stack. */
2993 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2994 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002995
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002996 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002997 DISPATCH();
2998 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002999
Yury Selivanov75445082015-05-11 22:57:16 -04003000 TARGET(WITH_CLEANUP_START) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003001 /* At the top of the stack are 1 or 6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003002 how/why we entered the finally clause:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003003 - TOP = NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003004 - (TOP, SECOND, THIRD) = exc_info()
3005 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003006 Below them is EXIT, the context.__exit__ or context.__aexit__
3007 bound method.
3008 In the first case, we must call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003009 EXIT(None, None, None)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003010 otherwise we must call
3011 EXIT(TOP, SECOND, THIRD)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003012
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003013 In the first case, we remove EXIT from the
3014 stack, leaving TOP, and push TOP on the stack.
3015 Otherwise we shift the bottom 3 values of the
3016 stack down, replace the empty spot with NULL, and push
3017 None on the stack.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003018
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003019 Finally we push the result of the call.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003020 */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003021 PyObject *stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003022 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003023 PyObject *exc, *val, *tb, *res;
3024
3025 val = tb = Py_None;
3026 exc = TOP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003027 if (exc == NULL) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003028 STACK_SHRINK(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003029 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003030 SET_TOP(exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003031 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003032 }
3033 else {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003034 assert(PyExceptionClass_Check(exc));
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003035 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003036 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003037 val = SECOND();
3038 tb = THIRD();
3039 tp2 = FOURTH();
3040 exc2 = PEEK(5);
3041 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003042 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003043 SET_VALUE(7, tb2);
3044 SET_VALUE(6, exc2);
3045 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003046 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3047 SET_FOURTH(NULL);
3048 /* We just shifted the stack down, so we have
3049 to tell the except handler block that the
3050 values are lower than it expects. */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003051 assert(f->f_iblock > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003052 block = &f->f_blockstack[f->f_iblock - 1];
3053 assert(block->b_type == EXCEPT_HANDLER);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003054 assert(block->b_level > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 block->b_level--;
3056 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003057
3058 stack[0] = exc;
3059 stack[1] = val;
3060 stack[2] = tb;
3061 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003062 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003063 if (res == NULL)
3064 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003065
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003066 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003067 PUSH(exc);
3068 PUSH(res);
3069 PREDICT(WITH_CLEANUP_FINISH);
3070 DISPATCH();
3071 }
3072
3073 PREDICTED(WITH_CLEANUP_FINISH);
3074 TARGET(WITH_CLEANUP_FINISH) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003075 /* TOP = the result of calling the context.__exit__ bound method
3076 SECOND = either None or exception type
3077
3078 If SECOND is None below is NULL or the return address,
3079 otherwise below are 7 values representing an exception.
3080 */
Yury Selivanov75445082015-05-11 22:57:16 -04003081 PyObject *res = POP();
3082 PyObject *exc = POP();
3083 int err;
3084
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003085 if (exc != Py_None)
3086 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003087 else
3088 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003089
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003090 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003091 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003093 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003094 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003095 else if (err > 0) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003096 /* There was an exception and a True return.
3097 * We must manually unwind the EXCEPT_HANDLER block
3098 * which was created when the exception was caught,
3099 * otherwise the stack will be in an inconsisten state.
3100 */
3101 PyTryBlock *b = PyFrame_BlockPop(f);
3102 assert(b->b_type == EXCEPT_HANDLER);
3103 UNWIND_EXCEPT_HANDLER(b);
3104 PUSH(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003105 }
3106 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003107 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003108 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003109
Yury Selivanovf2392132016-12-13 19:03:51 -05003110 TARGET(LOAD_METHOD) {
3111 /* Designed to work in tamdem with CALL_METHOD. */
3112 PyObject *name = GETITEM(names, oparg);
3113 PyObject *obj = TOP();
3114 PyObject *meth = NULL;
3115
3116 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3117
Yury Selivanovf2392132016-12-13 19:03:51 -05003118 if (meth == NULL) {
3119 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003120 goto error;
3121 }
3122
3123 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003124 /* We can bypass temporary bound method object.
3125 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003126
INADA Naoki015bce62017-01-16 17:23:30 +09003127 meth | self | arg1 | ... | argN
3128 */
3129 SET_TOP(meth);
3130 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003131 }
3132 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003133 /* meth is not an unbound method (but a regular attr, or
3134 something was returned by a descriptor protocol). Set
3135 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003136 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003137
3138 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003139 */
INADA Naoki015bce62017-01-16 17:23:30 +09003140 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003141 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003142 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003143 }
3144 DISPATCH();
3145 }
3146
3147 TARGET(CALL_METHOD) {
3148 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003149 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003150
3151 sp = stack_pointer;
3152
INADA Naoki015bce62017-01-16 17:23:30 +09003153 meth = PEEK(oparg + 2);
3154 if (meth == NULL) {
3155 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3156 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003157
3158 Stack layout:
3159
INADA Naoki015bce62017-01-16 17:23:30 +09003160 ... | NULL | callable | arg1 | ... | argN
3161 ^- TOP()
3162 ^- (-oparg)
3163 ^- (-oparg-1)
3164 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003165
Ville Skyttä49b27342017-08-03 09:00:59 +03003166 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003167 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003168 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003169 res = call_function(&sp, oparg, NULL);
3170 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003171 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003172 }
3173 else {
3174 /* This is a method call. Stack layout:
3175
INADA Naoki015bce62017-01-16 17:23:30 +09003176 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003177 ^- TOP()
3178 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003179 ^- (-oparg-1)
3180 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003181
INADA Naoki015bce62017-01-16 17:23:30 +09003182 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003183 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003184 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003185 */
3186 res = call_function(&sp, oparg + 1, NULL);
3187 stack_pointer = sp;
3188 }
3189
3190 PUSH(res);
3191 if (res == NULL)
3192 goto error;
3193 DISPATCH();
3194 }
3195
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003196 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003197 TARGET(CALL_FUNCTION) {
3198 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003199 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003200 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003201 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003202 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003203 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003204 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003205 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003206 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003207 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003208
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003209 TARGET(CALL_FUNCTION_KW) {
3210 PyObject **sp, *res, *names;
3211
3212 names = POP();
3213 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003214 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003215 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003216 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003217 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003218 Py_DECREF(names);
3219
3220 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003221 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003222 }
3223 DISPATCH();
3224 }
3225
3226 TARGET(CALL_FUNCTION_EX) {
3227 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003228 if (oparg & 0x01) {
3229 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003230 if (!PyDict_CheckExact(kwargs)) {
3231 PyObject *d = PyDict_New();
3232 if (d == NULL)
3233 goto error;
3234 if (PyDict_Update(d, kwargs) != 0) {
3235 Py_DECREF(d);
3236 /* PyDict_Update raises attribute
3237 * error (percolated from an attempt
3238 * to get 'keys' attribute) instead of
3239 * a type error if its second argument
3240 * is not a mapping.
3241 */
3242 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003243 format_kwargs_mapping_error(SECOND(), kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003244 }
Victor Stinnereece2222016-09-12 11:16:37 +02003245 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003246 goto error;
3247 }
3248 Py_DECREF(kwargs);
3249 kwargs = d;
3250 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003251 assert(PyDict_CheckExact(kwargs));
3252 }
3253 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003254 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003255 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003256 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003257 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003258 goto error;
3259 }
3260 Py_SETREF(callargs, PySequence_Tuple(callargs));
3261 if (callargs == NULL) {
3262 goto error;
3263 }
3264 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003265 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003266
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003267 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003268 Py_DECREF(func);
3269 Py_DECREF(callargs);
3270 Py_XDECREF(kwargs);
3271
3272 SET_TOP(result);
3273 if (result == NULL) {
3274 goto error;
3275 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003276 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003277 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003278
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003279 TARGET(MAKE_FUNCTION) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003280 PyObject *qualname = POP();
3281 PyObject *codeobj = POP();
3282 PyFunctionObject *func = (PyFunctionObject *)
3283 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003284
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003285 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003286 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003287 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003288 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003289 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003290
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003291 if (oparg & 0x08) {
3292 assert(PyTuple_CheckExact(TOP()));
3293 func ->func_closure = POP();
3294 }
3295 if (oparg & 0x04) {
3296 assert(PyDict_CheckExact(TOP()));
3297 func->func_annotations = POP();
3298 }
3299 if (oparg & 0x02) {
3300 assert(PyDict_CheckExact(TOP()));
3301 func->func_kwdefaults = POP();
3302 }
3303 if (oparg & 0x01) {
3304 assert(PyTuple_CheckExact(TOP()));
3305 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003306 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003307
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003308 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003309 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003310 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003311
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003312 TARGET(BUILD_SLICE) {
3313 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003314 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003315 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003316 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003317 step = NULL;
3318 stop = POP();
3319 start = TOP();
3320 slice = PySlice_New(start, stop, step);
3321 Py_DECREF(start);
3322 Py_DECREF(stop);
3323 Py_XDECREF(step);
3324 SET_TOP(slice);
3325 if (slice == NULL)
3326 goto error;
3327 DISPATCH();
3328 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003329
Eric V. Smitha78c7952015-11-03 12:45:05 -05003330 TARGET(FORMAT_VALUE) {
3331 /* Handles f-string value formatting. */
3332 PyObject *result;
3333 PyObject *fmt_spec;
3334 PyObject *value;
3335 PyObject *(*conv_fn)(PyObject *);
3336 int which_conversion = oparg & FVC_MASK;
3337 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3338
3339 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003340 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003341
3342 /* See if any conversion is specified. */
3343 switch (which_conversion) {
3344 case FVC_STR: conv_fn = PyObject_Str; break;
3345 case FVC_REPR: conv_fn = PyObject_Repr; break;
3346 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3347
3348 /* Must be 0 (meaning no conversion), since only four
3349 values are allowed by (oparg & FVC_MASK). */
3350 default: conv_fn = NULL; break;
3351 }
3352
3353 /* If there's a conversion function, call it and replace
3354 value with that result. Otherwise, just use value,
3355 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003356 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003357 result = conv_fn(value);
3358 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003359 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003360 Py_XDECREF(fmt_spec);
3361 goto error;
3362 }
3363 value = result;
3364 }
3365
3366 /* If value is a unicode object, and there's no fmt_spec,
3367 then we know the result of format(value) is value
3368 itself. In that case, skip calling format(). I plan to
3369 move this optimization in to PyObject_Format()
3370 itself. */
3371 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3372 /* Do nothing, just transfer ownership to result. */
3373 result = value;
3374 } else {
3375 /* Actually call format(). */
3376 result = PyObject_Format(value, fmt_spec);
3377 Py_DECREF(value);
3378 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003379 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003380 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003381 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003382 }
3383
Eric V. Smith135d5f42016-02-05 18:23:08 -05003384 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003385 DISPATCH();
3386 }
3387
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003388 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003389 int oldoparg = oparg;
3390 NEXTOPARG();
3391 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003392 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003393 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003394
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003395
Antoine Pitrou042b1282010-08-13 21:15:58 +00003396#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003397 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003398#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003399 default:
3400 fprintf(stderr,
3401 "XXX lineno: %d, opcode: %d\n",
3402 PyFrame_GetLineNumber(f),
3403 opcode);
3404 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003405 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003407 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003408
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003409 /* This should never be reached. Every opcode should end with DISPATCH()
3410 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003411 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003412
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003413error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003414 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003415#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003416 if (!PyErr_Occurred())
3417 PyErr_SetString(PyExc_SystemError,
3418 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003419#else
3420 assert(PyErr_Occurred());
3421#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003422
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003423 /* Log traceback info. */
3424 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003425
Benjamin Peterson51f46162013-01-23 08:38:47 -05003426 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003427 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3428 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003429
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003430exception_unwind:
3431 /* Unwind stacks if an exception occurred */
3432 while (f->f_iblock > 0) {
3433 /* Pop the current block. */
3434 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003436 if (b->b_type == EXCEPT_HANDLER) {
3437 UNWIND_EXCEPT_HANDLER(b);
3438 continue;
3439 }
3440 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003441 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003442 PyObject *exc, *val, *tb;
3443 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003444 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003445 /* Beware, this invalidates all b->b_* fields */
3446 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003447 PUSH(exc_info->exc_traceback);
3448 PUSH(exc_info->exc_value);
3449 if (exc_info->exc_type != NULL) {
3450 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003451 }
3452 else {
3453 Py_INCREF(Py_None);
3454 PUSH(Py_None);
3455 }
3456 PyErr_Fetch(&exc, &val, &tb);
3457 /* Make the raw exception data
3458 available to the handler,
3459 so a program can emulate the
3460 Python main loop. */
3461 PyErr_NormalizeException(
3462 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003463 if (tb != NULL)
3464 PyException_SetTraceback(val, tb);
3465 else
3466 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003467 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003468 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003469 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003470 exc_info->exc_value = val;
3471 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003472 if (tb == NULL)
3473 tb = Py_None;
3474 Py_INCREF(tb);
3475 PUSH(tb);
3476 PUSH(val);
3477 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003478 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003479 /* Resume normal execution */
3480 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003481 }
3482 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003483
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003484 /* End the loop as we still have an error */
3485 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003486 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003488 /* Pop remaining stack entries. */
3489 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003490 PyObject *o = POP();
3491 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003492 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003493
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003494 assert(retval == NULL);
3495 assert(PyErr_Occurred());
Guido van Rossumac7be682001-01-17 15:42:30 +00003496
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003497return_or_yield:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003498 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003499 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003500 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3501 tstate, f, PyTrace_RETURN, retval)) {
3502 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003503 }
3504 }
3505 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003506 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3507 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003508 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003509 }
3510 }
3511 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003513 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003514exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003515 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3516 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003517 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003518 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003519 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003520
Victor Stinnerefde1462015-03-21 15:04:43 +01003521 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003522}
3523
Benjamin Petersonb204a422011-06-05 22:04:07 -05003524static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003525format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3526{
3527 int err;
3528 Py_ssize_t len = PyList_GET_SIZE(names);
3529 PyObject *name_str, *comma, *tail, *tmp;
3530
3531 assert(PyList_CheckExact(names));
3532 assert(len >= 1);
3533 /* Deal with the joys of natural language. */
3534 switch (len) {
3535 case 1:
3536 name_str = PyList_GET_ITEM(names, 0);
3537 Py_INCREF(name_str);
3538 break;
3539 case 2:
3540 name_str = PyUnicode_FromFormat("%U and %U",
3541 PyList_GET_ITEM(names, len - 2),
3542 PyList_GET_ITEM(names, len - 1));
3543 break;
3544 default:
3545 tail = PyUnicode_FromFormat(", %U, and %U",
3546 PyList_GET_ITEM(names, len - 2),
3547 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003548 if (tail == NULL)
3549 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003550 /* Chop off the last two objects in the list. This shouldn't actually
3551 fail, but we can't be too careful. */
3552 err = PyList_SetSlice(names, len - 2, len, NULL);
3553 if (err == -1) {
3554 Py_DECREF(tail);
3555 return;
3556 }
3557 /* Stitch everything up into a nice comma-separated list. */
3558 comma = PyUnicode_FromString(", ");
3559 if (comma == NULL) {
3560 Py_DECREF(tail);
3561 return;
3562 }
3563 tmp = PyUnicode_Join(comma, names);
3564 Py_DECREF(comma);
3565 if (tmp == NULL) {
3566 Py_DECREF(tail);
3567 return;
3568 }
3569 name_str = PyUnicode_Concat(tmp, tail);
3570 Py_DECREF(tmp);
3571 Py_DECREF(tail);
3572 break;
3573 }
3574 if (name_str == NULL)
3575 return;
3576 PyErr_Format(PyExc_TypeError,
3577 "%U() missing %i required %s argument%s: %U",
3578 co->co_name,
3579 len,
3580 kind,
3581 len == 1 ? "" : "s",
3582 name_str);
3583 Py_DECREF(name_str);
3584}
3585
3586static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003587missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003588 PyObject **fastlocals)
3589{
Victor Stinner74319ae2016-08-25 00:04:09 +02003590 Py_ssize_t i, j = 0;
3591 Py_ssize_t start, end;
3592 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003593 const char *kind = positional ? "positional" : "keyword-only";
3594 PyObject *missing_names;
3595
3596 /* Compute the names of the arguments that are missing. */
3597 missing_names = PyList_New(missing);
3598 if (missing_names == NULL)
3599 return;
3600 if (positional) {
3601 start = 0;
3602 end = co->co_argcount - defcount;
3603 }
3604 else {
3605 start = co->co_argcount;
3606 end = start + co->co_kwonlyargcount;
3607 }
3608 for (i = start; i < end; i++) {
3609 if (GETLOCAL(i) == NULL) {
3610 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3611 PyObject *name = PyObject_Repr(raw);
3612 if (name == NULL) {
3613 Py_DECREF(missing_names);
3614 return;
3615 }
3616 PyList_SET_ITEM(missing_names, j++, name);
3617 }
3618 }
3619 assert(j == missing);
3620 format_missing(kind, co, missing_names);
3621 Py_DECREF(missing_names);
3622}
3623
3624static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003625too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3626 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003627{
3628 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003629 Py_ssize_t kwonly_given = 0;
3630 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003631 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003632 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003633
Benjamin Petersone109c702011-06-24 09:37:26 -05003634 assert((co->co_flags & CO_VARARGS) == 0);
3635 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003636 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3637 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003638 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003639 }
3640 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003641 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003642 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003643 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003644 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003645 }
3646 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003647 plural = (co_argcount != 1);
3648 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003649 }
3650 if (sig == NULL)
3651 return;
3652 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003653 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3654 kwonly_sig = PyUnicode_FromFormat(format,
3655 given != 1 ? "s" : "",
3656 kwonly_given,
3657 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003658 if (kwonly_sig == NULL) {
3659 Py_DECREF(sig);
3660 return;
3661 }
3662 }
3663 else {
3664 /* This will not fail. */
3665 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003666 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003667 }
3668 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003669 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003670 co->co_name,
3671 sig,
3672 plural ? "s" : "",
3673 given,
3674 kwonly_sig,
3675 given == 1 && !kwonly_given ? "was" : "were");
3676 Py_DECREF(sig);
3677 Py_DECREF(kwonly_sig);
3678}
3679
Guido van Rossumc2e20742006-02-27 22:32:47 +00003680/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003681 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003682 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003683
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003684PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003685_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003686 PyObject *const *args, Py_ssize_t argcount,
3687 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003688 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003689 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003690 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003691 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003692{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003693 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003694 PyFrameObject *f;
3695 PyObject *retval = NULL;
3696 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003697 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003698 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003699 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3700 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003701 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003703 if (globals == NULL) {
3704 PyErr_SetString(PyExc_SystemError,
3705 "PyEval_EvalCodeEx: NULL globals");
3706 return NULL;
3707 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003708
Victor Stinnerc7020012016-08-16 23:40:29 +02003709 /* Create the frame */
3710 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003711 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003712 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003713 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003714 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003715 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003716 fastlocals = f->f_localsplus;
3717 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003718
Victor Stinnerc7020012016-08-16 23:40:29 +02003719 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003720 if (co->co_flags & CO_VARKEYWORDS) {
3721 kwdict = PyDict_New();
3722 if (kwdict == NULL)
3723 goto fail;
3724 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003725 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003726 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003727 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003728 SETLOCAL(i, kwdict);
3729 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003730 else {
3731 kwdict = NULL;
3732 }
3733
3734 /* Copy positional arguments into local variables */
3735 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003736 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003737 }
3738 else {
3739 n = argcount;
3740 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003741 for (i = 0; i < n; i++) {
3742 x = args[i];
3743 Py_INCREF(x);
3744 SETLOCAL(i, x);
3745 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003746
3747 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003748 if (co->co_flags & CO_VARARGS) {
3749 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003750 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003751 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003752 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003753 SETLOCAL(total_args, u);
3754 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003755 x = args[i];
3756 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003757 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003758 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003759 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003760
Serhiy Storchakab7281052016-09-12 00:52:40 +03003761 /* Handle keyword arguments passed as two strided arrays */
3762 kwcount *= kwstep;
3763 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003764 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003765 PyObject *keyword = kwnames[i];
3766 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003767 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003768
Benjamin Petersonb204a422011-06-05 22:04:07 -05003769 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3770 PyErr_Format(PyExc_TypeError,
3771 "%U() keywords must be strings",
3772 co->co_name);
3773 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003774 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003775
Benjamin Petersonb204a422011-06-05 22:04:07 -05003776 /* Speed hack: do raw pointer compares. As names are
3777 normally interned this should almost always hit. */
3778 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3779 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003780 PyObject *name = co_varnames[j];
3781 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003782 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003783 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003784 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003785
Benjamin Petersonb204a422011-06-05 22:04:07 -05003786 /* Slow fallback, just in case */
3787 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003788 PyObject *name = co_varnames[j];
3789 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3790 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003791 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003792 }
3793 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003794 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003795 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003796 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003797
Victor Stinner231d1f32017-01-11 02:12:06 +01003798 assert(j >= total_args);
3799 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003800 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003801 "%U() got an unexpected keyword argument '%S'",
3802 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003803 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003804 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003805
Christian Heimes0bd447f2013-07-20 14:48:10 +02003806 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3807 goto fail;
3808 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003809 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003810
Benjamin Petersonb204a422011-06-05 22:04:07 -05003811 kw_found:
3812 if (GETLOCAL(j) != NULL) {
3813 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003814 "%U() got multiple values for argument '%S'",
3815 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003816 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003817 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003818 Py_INCREF(value);
3819 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003820 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003821
3822 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003823 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003824 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003825 goto fail;
3826 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003827
3828 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003829 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02003830 Py_ssize_t m = co->co_argcount - defcount;
3831 Py_ssize_t missing = 0;
3832 for (i = argcount; i < m; i++) {
3833 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003834 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02003835 }
3836 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003837 if (missing) {
3838 missing_arguments(co, missing, defcount, fastlocals);
3839 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003840 }
3841 if (n > m)
3842 i = n - m;
3843 else
3844 i = 0;
3845 for (; i < defcount; i++) {
3846 if (GETLOCAL(m+i) == NULL) {
3847 PyObject *def = defs[i];
3848 Py_INCREF(def);
3849 SETLOCAL(m+i, def);
3850 }
3851 }
3852 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003853
3854 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003855 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02003856 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003857 for (i = co->co_argcount; i < total_args; i++) {
3858 PyObject *name;
3859 if (GETLOCAL(i) != NULL)
3860 continue;
3861 name = PyTuple_GET_ITEM(co->co_varnames, i);
3862 if (kwdefs != NULL) {
3863 PyObject *def = PyDict_GetItem(kwdefs, name);
3864 if (def) {
3865 Py_INCREF(def);
3866 SETLOCAL(i, def);
3867 continue;
3868 }
3869 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003870 missing++;
3871 }
3872 if (missing) {
3873 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003874 goto fail;
3875 }
3876 }
3877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003878 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003879 vars into frame. */
3880 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003881 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02003882 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05003883 /* Possibly account for the cell variable being an argument. */
3884 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003885 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003886 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003887 /* Clear the local copy. */
3888 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003889 }
3890 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003891 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003892 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003893 if (c == NULL)
3894 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003895 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003896 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003897
3898 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05003899 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3900 PyObject *o = PyTuple_GET_ITEM(closure, i);
3901 Py_INCREF(o);
3902 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003903 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003904
Yury Selivanoveb636452016-09-08 22:01:51 -07003905 /* Handle generator/coroutine/asynchronous generator */
3906 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003907 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04003908 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003909 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04003910
3911 if (is_coro && tstate->in_coroutine_wrapper) {
3912 assert(coro_wrapper != NULL);
3913 PyErr_Format(PyExc_RuntimeError,
3914 "coroutine wrapper %.200R attempted "
3915 "to recursively wrap %.200R",
3916 coro_wrapper,
3917 co);
3918 goto fail;
3919 }
Yury Selivanov75445082015-05-11 22:57:16 -04003920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003921 /* Don't need to keep the reference to f_back, it will be set
3922 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003923 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003925 /* Create a new generator that owns the ready to run frame
3926 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04003927 if (is_coro) {
3928 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07003929 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
3930 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003931 } else {
3932 gen = PyGen_NewWithQualName(f, name, qualname);
3933 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003934 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003935 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003936 }
INADA Naoki9c157762016-12-26 18:52:46 +09003937
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003938 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04003939
Yury Selivanov94c22632015-06-04 10:16:51 -04003940 if (is_coro && coro_wrapper != NULL) {
3941 PyObject *wrapped;
3942 tstate->in_coroutine_wrapper = 1;
3943 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
3944 tstate->in_coroutine_wrapper = 0;
3945 return wrapped;
3946 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04003947
Yury Selivanov75445082015-05-11 22:57:16 -04003948 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003949 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003950
Victor Stinner59a73272016-12-09 18:51:13 +01003951 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003952
Thomas Woutersce272b62007-09-19 21:19:28 +00003953fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003955 /* decref'ing the frame can cause __del__ methods to get invoked,
3956 which can call back into Python. While we're done with the
3957 current Python frame (f), the associated C stack is still in use,
3958 so recursion_depth must be boosted for the duration.
3959 */
3960 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003961 if (Py_REFCNT(f) > 1) {
3962 Py_DECREF(f);
3963 _PyObject_GC_TRACK(f);
3964 }
3965 else {
3966 ++tstate->recursion_depth;
3967 Py_DECREF(f);
3968 --tstate->recursion_depth;
3969 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003970 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003971}
3972
Victor Stinner40ee3012014-06-16 15:59:28 +02003973PyObject *
3974PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003975 PyObject *const *args, int argcount,
3976 PyObject *const *kws, int kwcount,
3977 PyObject *const *defs, int defcount,
3978 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02003979{
3980 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02003981 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06003982 kws, kws != NULL ? kws + 1 : NULL,
3983 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02003984 defs, defcount,
3985 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003986 NULL, NULL);
3987}
Tim Peters5ca576e2001-06-18 22:08:13 +00003988
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003989static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05003990special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003991{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003992 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05003993 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003994 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003995 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003996 return NULL;
3997 }
3998 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003999}
4000
4001
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004002/* Logic for the raise statement (too complicated for inlining).
4003 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004004static int
Collin Winter828f04a2007-08-31 00:04:24 +00004005do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004006{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004007 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004009 if (exc == NULL) {
4010 /* Reraise */
4011 PyThreadState *tstate = PyThreadState_GET();
Mark Shannonae3087c2017-10-22 22:41:51 +01004012 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004013 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004014 type = exc_info->exc_type;
4015 value = exc_info->exc_value;
4016 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004017 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004018 PyErr_SetString(PyExc_RuntimeError,
4019 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004020 return 0;
4021 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004022 Py_XINCREF(type);
4023 Py_XINCREF(value);
4024 Py_XINCREF(tb);
4025 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004026 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004027 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004029 /* We support the following forms of raise:
4030 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004031 raise <instance>
4032 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004034 if (PyExceptionClass_Check(exc)) {
4035 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004036 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004037 if (value == NULL)
4038 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004039 if (!PyExceptionInstance_Check(value)) {
4040 PyErr_Format(PyExc_TypeError,
4041 "calling %R should have returned an instance of "
4042 "BaseException, not %R",
4043 type, Py_TYPE(value));
4044 goto raise_error;
4045 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004046 }
4047 else if (PyExceptionInstance_Check(exc)) {
4048 value = exc;
4049 type = PyExceptionInstance_Class(exc);
4050 Py_INCREF(type);
4051 }
4052 else {
4053 /* Not something you can raise. You get an exception
4054 anyway, just not what you specified :-) */
4055 Py_DECREF(exc);
4056 PyErr_SetString(PyExc_TypeError,
4057 "exceptions must derive from BaseException");
4058 goto raise_error;
4059 }
Collin Winter828f04a2007-08-31 00:04:24 +00004060
Serhiy Storchakac0191582016-09-27 11:37:10 +03004061 assert(type != NULL);
4062 assert(value != NULL);
4063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004064 if (cause) {
4065 PyObject *fixed_cause;
4066 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004067 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004068 if (fixed_cause == NULL)
4069 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004070 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004071 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004072 else if (PyExceptionInstance_Check(cause)) {
4073 fixed_cause = cause;
4074 }
4075 else if (cause == Py_None) {
4076 Py_DECREF(cause);
4077 fixed_cause = NULL;
4078 }
4079 else {
4080 PyErr_SetString(PyExc_TypeError,
4081 "exception causes must derive from "
4082 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004083 goto raise_error;
4084 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004085 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004086 }
Collin Winter828f04a2007-08-31 00:04:24 +00004087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004088 PyErr_SetObject(type, value);
4089 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004090 Py_DECREF(value);
4091 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004092 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004093
4094raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004095 Py_XDECREF(value);
4096 Py_XDECREF(type);
4097 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004098 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004099}
4100
Tim Petersd6d010b2001-06-21 02:49:55 +00004101/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004102 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004103
Guido van Rossum0368b722007-05-11 16:50:42 +00004104 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4105 with a variable target.
4106*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004107
Barry Warsawe42b18f1997-08-25 22:13:04 +00004108static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004109unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004110{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004111 int i = 0, j = 0;
4112 Py_ssize_t ll = 0;
4113 PyObject *it; /* iter(v) */
4114 PyObject *w;
4115 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004117 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004118
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004119 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004120 if (it == NULL) {
4121 if (PyErr_ExceptionMatches(PyExc_TypeError) &&
4122 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4123 {
4124 PyErr_Format(PyExc_TypeError,
4125 "cannot unpack non-iterable %.200s object",
4126 v->ob_type->tp_name);
4127 }
4128 return 0;
4129 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004131 for (; i < argcnt; i++) {
4132 w = PyIter_Next(it);
4133 if (w == NULL) {
4134 /* Iterator done, via error or exhaustion. */
4135 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004136 if (argcntafter == -1) {
4137 PyErr_Format(PyExc_ValueError,
4138 "not enough values to unpack (expected %d, got %d)",
4139 argcnt, i);
4140 }
4141 else {
4142 PyErr_Format(PyExc_ValueError,
4143 "not enough values to unpack "
4144 "(expected at least %d, got %d)",
4145 argcnt + argcntafter, i);
4146 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004147 }
4148 goto Error;
4149 }
4150 *--sp = w;
4151 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004153 if (argcntafter == -1) {
4154 /* We better have exhausted the iterator now. */
4155 w = PyIter_Next(it);
4156 if (w == NULL) {
4157 if (PyErr_Occurred())
4158 goto Error;
4159 Py_DECREF(it);
4160 return 1;
4161 }
4162 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004163 PyErr_Format(PyExc_ValueError,
4164 "too many values to unpack (expected %d)",
4165 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004166 goto Error;
4167 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004169 l = PySequence_List(it);
4170 if (l == NULL)
4171 goto Error;
4172 *--sp = l;
4173 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004175 ll = PyList_GET_SIZE(l);
4176 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004177 PyErr_Format(PyExc_ValueError,
4178 "not enough values to unpack (expected at least %d, got %zd)",
4179 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004180 goto Error;
4181 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004183 /* Pop the "after-variable" args off the list. */
4184 for (j = argcntafter; j > 0; j--, i++) {
4185 *--sp = PyList_GET_ITEM(l, ll - j);
4186 }
4187 /* Resize the list. */
4188 Py_SIZE(l) = ll - argcntafter;
4189 Py_DECREF(it);
4190 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004191
Tim Petersd6d010b2001-06-21 02:49:55 +00004192Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004193 for (; i > 0; i--, sp++)
4194 Py_DECREF(*sp);
4195 Py_XDECREF(it);
4196 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004197}
4198
4199
Guido van Rossum96a42c81992-01-12 02:29:51 +00004200#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004201static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004202prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004204 printf("%s ", str);
4205 if (PyObject_Print(v, stdout, 0) != 0)
4206 PyErr_Clear(); /* Don't know what else to do */
4207 printf("\n");
4208 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004209}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004210#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004211
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004212static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004213call_exc_trace(Py_tracefunc func, PyObject *self,
4214 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004215{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004216 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004217 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004218 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004219 if (value == NULL) {
4220 value = Py_None;
4221 Py_INCREF(value);
4222 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004223 PyErr_NormalizeException(&type, &value, &orig_traceback);
4224 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004225 arg = PyTuple_Pack(3, type, value, traceback);
4226 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004227 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004228 return;
4229 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004230 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004231 Py_DECREF(arg);
4232 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004233 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004234 else {
4235 Py_XDECREF(type);
4236 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004237 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004238 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004239}
4240
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004241static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004242call_trace_protected(Py_tracefunc func, PyObject *obj,
4243 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004244 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004245{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004246 PyObject *type, *value, *traceback;
4247 int err;
4248 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004249 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004250 if (err == 0)
4251 {
4252 PyErr_Restore(type, value, traceback);
4253 return 0;
4254 }
4255 else {
4256 Py_XDECREF(type);
4257 Py_XDECREF(value);
4258 Py_XDECREF(traceback);
4259 return -1;
4260 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004261}
4262
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004263static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004264call_trace(Py_tracefunc func, PyObject *obj,
4265 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004266 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004267{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004268 int result;
4269 if (tstate->tracing)
4270 return 0;
4271 tstate->tracing++;
4272 tstate->use_tracing = 0;
4273 result = func(obj, frame, what, arg);
4274 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4275 || (tstate->c_profilefunc != NULL));
4276 tstate->tracing--;
4277 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004278}
4279
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004280PyObject *
4281_PyEval_CallTracing(PyObject *func, PyObject *args)
4282{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004283 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004284 int save_tracing = tstate->tracing;
4285 int save_use_tracing = tstate->use_tracing;
4286 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004288 tstate->tracing = 0;
4289 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4290 || (tstate->c_profilefunc != NULL));
4291 result = PyObject_Call(func, args, NULL);
4292 tstate->tracing = save_tracing;
4293 tstate->use_tracing = save_use_tracing;
4294 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004295}
4296
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004297/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004298static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004299maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004300 PyThreadState *tstate, PyFrameObject *frame,
4301 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004302{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004303 int result = 0;
4304 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004306 /* If the last instruction executed isn't in the current
4307 instruction window, reset the window.
4308 */
4309 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4310 PyAddrPair bounds;
4311 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4312 &bounds);
4313 *instr_lb = bounds.ap_lower;
4314 *instr_ub = bounds.ap_upper;
4315 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004316 /* If the last instruction falls at the start of a line or if it
4317 represents a jump backwards, update the frame's line number and
4318 then call the trace function if we're tracing source lines.
4319 */
4320 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004321 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004322 if (frame->f_trace_lines) {
4323 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4324 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004325 }
George King20faa682017-10-18 17:44:22 -07004326 /* Always emit an opcode event if we're tracing all opcodes. */
4327 if (frame->f_trace_opcodes) {
4328 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4329 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004330 *instr_prev = frame->f_lasti;
4331 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004332}
4333
Fred Drake5755ce62001-06-27 19:19:46 +00004334void
4335PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004336{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004337 PyThreadState *tstate = PyThreadState_GET();
4338 PyObject *temp = tstate->c_profileobj;
4339 Py_XINCREF(arg);
4340 tstate->c_profilefunc = NULL;
4341 tstate->c_profileobj = NULL;
4342 /* Must make sure that tracing is not ignored if 'temp' is freed */
4343 tstate->use_tracing = tstate->c_tracefunc != NULL;
4344 Py_XDECREF(temp);
4345 tstate->c_profilefunc = func;
4346 tstate->c_profileobj = arg;
4347 /* Flag that tracing or profiling is turned on */
4348 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004349}
4350
4351void
4352PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4353{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004354 PyThreadState *tstate = PyThreadState_GET();
4355 PyObject *temp = tstate->c_traceobj;
4356 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4357 Py_XINCREF(arg);
4358 tstate->c_tracefunc = NULL;
4359 tstate->c_traceobj = NULL;
4360 /* Must make sure that profiling is not ignored if 'temp' is freed */
4361 tstate->use_tracing = tstate->c_profilefunc != NULL;
4362 Py_XDECREF(temp);
4363 tstate->c_tracefunc = func;
4364 tstate->c_traceobj = arg;
4365 /* Flag that tracing or profiling is turned on */
4366 tstate->use_tracing = ((func != NULL)
4367 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004368}
4369
Yury Selivanov75445082015-05-11 22:57:16 -04004370void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004371_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4372{
4373 assert(new_depth >= 0);
4374 PyThreadState *tstate = PyThreadState_GET();
4375 tstate->coroutine_origin_tracking_depth = new_depth;
4376}
4377
4378int
4379_PyEval_GetCoroutineOriginTrackingDepth(void)
4380{
4381 PyThreadState *tstate = PyThreadState_GET();
4382 return tstate->coroutine_origin_tracking_depth;
4383}
4384
4385void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004386_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004387{
4388 PyThreadState *tstate = PyThreadState_GET();
4389
Yury Selivanov75445082015-05-11 22:57:16 -04004390 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004391 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004392}
4393
4394PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004395_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004396{
4397 PyThreadState *tstate = PyThreadState_GET();
4398 return tstate->coroutine_wrapper;
4399}
4400
Yury Selivanoveb636452016-09-08 22:01:51 -07004401void
4402_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4403{
4404 PyThreadState *tstate = PyThreadState_GET();
4405
4406 Py_XINCREF(firstiter);
4407 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4408}
4409
4410PyObject *
4411_PyEval_GetAsyncGenFirstiter(void)
4412{
4413 PyThreadState *tstate = PyThreadState_GET();
4414 return tstate->async_gen_firstiter;
4415}
4416
4417void
4418_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4419{
4420 PyThreadState *tstate = PyThreadState_GET();
4421
4422 Py_XINCREF(finalizer);
4423 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4424}
4425
4426PyObject *
4427_PyEval_GetAsyncGenFinalizer(void)
4428{
4429 PyThreadState *tstate = PyThreadState_GET();
4430 return tstate->async_gen_finalizer;
4431}
4432
Guido van Rossumb209a111997-04-29 18:18:01 +00004433PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004434PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004436 PyFrameObject *current_frame = PyEval_GetFrame();
4437 if (current_frame == NULL)
Victor Stinnercaba55b2018-08-03 15:33:52 +02004438 return _PyInterpreterState_GET_UNSAFE()->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004439 else
4440 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004441}
4442
Guido van Rossumb209a111997-04-29 18:18:01 +00004443PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004444PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004445{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004446 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004447 if (current_frame == NULL) {
4448 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004449 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004450 }
4451
4452 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4453 return NULL;
4454
4455 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004456 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004457}
4458
Guido van Rossumb209a111997-04-29 18:18:01 +00004459PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004460PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004461{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004462 PyFrameObject *current_frame = PyEval_GetFrame();
4463 if (current_frame == NULL)
4464 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004465
4466 assert(current_frame->f_globals != NULL);
4467 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004468}
4469
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004470PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004471PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004472{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004473 PyThreadState *tstate = PyThreadState_GET();
4474 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004475}
4476
Guido van Rossum6135a871995-01-09 17:53:26 +00004477int
Tim Peters5ba58662001-07-16 02:29:45 +00004478PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004479{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004480 PyFrameObject *current_frame = PyEval_GetFrame();
4481 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004483 if (current_frame != NULL) {
4484 const int codeflags = current_frame->f_code->co_flags;
4485 const int compilerflags = codeflags & PyCF_MASK;
4486 if (compilerflags) {
4487 result = 1;
4488 cf->cf_flags |= compilerflags;
4489 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004490#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004491 if (codeflags & CO_GENERATOR_ALLOWED) {
4492 result = 1;
4493 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4494 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004495#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004496 }
4497 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004498}
4499
Guido van Rossum3f5da241990-12-20 15:06:42 +00004500
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004501const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004502PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004503{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004504 if (PyMethod_Check(func))
4505 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4506 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004507 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004508 else if (PyCFunction_Check(func))
4509 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4510 else
4511 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004512}
4513
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004514const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004515PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004516{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004517 if (PyMethod_Check(func))
4518 return "()";
4519 else if (PyFunction_Check(func))
4520 return "()";
4521 else if (PyCFunction_Check(func))
4522 return "()";
4523 else
4524 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004525}
4526
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004527#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004528if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004529 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4530 tstate, tstate->frame, \
4531 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004532 x = NULL; \
4533 } \
4534 else { \
4535 x = call; \
4536 if (tstate->c_profilefunc != NULL) { \
4537 if (x == NULL) { \
4538 call_trace_protected(tstate->c_profilefunc, \
4539 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004540 tstate, tstate->frame, \
4541 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004542 /* XXX should pass (type, value, tb) */ \
4543 } else { \
4544 if (call_trace(tstate->c_profilefunc, \
4545 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004546 tstate, tstate->frame, \
4547 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004548 Py_DECREF(x); \
4549 x = NULL; \
4550 } \
4551 } \
4552 } \
4553 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004554} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004555 x = call; \
4556 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004557
Victor Stinner415c5102017-01-11 00:54:57 +01004558/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4559 to reduce the stack consumption. */
4560Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004561call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004562{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004563 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004564 PyObject *func = *pfunc;
4565 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004566 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4567 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004568 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004570 /* Always dispatch PyCFunction first, because these are
4571 presumed to be the most frequent callable object.
4572 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004573 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004574 PyThreadState *tstate = PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004575 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004576 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004577 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
4578 PyThreadState *tstate = PyThreadState_GET();
jdemeyer56868f92018-07-21 10:30:59 +02004579 if (nargs > 0 && tstate->use_tracing) {
4580 /* We need to create a temporary bound method as argument
4581 for profiling.
4582
4583 If nargs == 0, then this cannot work because we have no
4584 "self". In any case, the call itself would raise
4585 TypeError (foo needs an argument), so we just skip
4586 profiling. */
4587 PyObject *self = stack[0];
4588 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
jdemeyer147d9552018-07-23 18:41:20 +02004589 if (func != NULL) {
4590 C_TRACE(x, _PyCFunction_FastCallKeywords(func,
4591 stack+1, nargs-1,
4592 kwnames));
4593 Py_DECREF(func);
INADA Naoki93fac8d2017-03-07 14:24:37 +09004594 }
jdemeyer147d9552018-07-23 18:41:20 +02004595 else {
4596 x = NULL;
4597 }
INADA Naoki93fac8d2017-03-07 14:24:37 +09004598 }
4599 else {
4600 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4601 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004602 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004603 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004604 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004605 /* Optimize access to bound methods. Reuse the Python stack
4606 to pass 'self' as the first argument, replace 'func'
4607 with 'self'. It avoids the creation of a new temporary tuple
4608 for arguments (to replace func with self) when the method uses
4609 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004610 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004611 Py_INCREF(self);
4612 func = PyMethod_GET_FUNCTION(func);
4613 Py_INCREF(func);
4614 Py_SETREF(*pfunc, self);
4615 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004616 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004617 }
4618 else {
4619 Py_INCREF(func);
4620 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004621
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004622 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004623 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004624 }
4625 else {
4626 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4627 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004628 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004629 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004630
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004631 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4632
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004633 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004634 while ((*pp_stack) > pfunc) {
4635 w = EXT_POP(*pp_stack);
4636 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004637 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004639 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004640}
4641
Jeremy Hylton52820442001-01-03 23:52:36 +00004642static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004643do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004644{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004645 if (PyCFunction_Check(func)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004646 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004647 PyThreadState *tstate = PyThreadState_GET();
4648 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004649 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004650 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004651 else {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004652 return PyObject_Call(func, callargs, kwdict);
Victor Stinner74319ae2016-08-25 00:04:09 +02004653 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004654}
4655
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004656/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004657 nb_index slot defined, and store in *pi.
4658 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004659 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004660 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004661*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004662int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004663_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004664{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004665 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004666 Py_ssize_t x;
4667 if (PyIndex_Check(v)) {
4668 x = PyNumber_AsSsize_t(v, NULL);
4669 if (x == -1 && PyErr_Occurred())
4670 return 0;
4671 }
4672 else {
4673 PyErr_SetString(PyExc_TypeError,
4674 "slice indices must be integers or "
4675 "None or have an __index__ method");
4676 return 0;
4677 }
4678 *pi = x;
4679 }
4680 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004681}
4682
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004683int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004684_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004685{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004686 Py_ssize_t x;
4687 if (PyIndex_Check(v)) {
4688 x = PyNumber_AsSsize_t(v, NULL);
4689 if (x == -1 && PyErr_Occurred())
4690 return 0;
4691 }
4692 else {
4693 PyErr_SetString(PyExc_TypeError,
4694 "slice indices must be integers or "
4695 "have an __index__ method");
4696 return 0;
4697 }
4698 *pi = x;
4699 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004700}
4701
4702
Guido van Rossum486364b2007-06-30 05:01:58 +00004703#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004704 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004705
Guido van Rossumb209a111997-04-29 18:18:01 +00004706static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004707cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004708{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004709 int res = 0;
4710 switch (op) {
4711 case PyCmp_IS:
4712 res = (v == w);
4713 break;
4714 case PyCmp_IS_NOT:
4715 res = (v != w);
4716 break;
4717 case PyCmp_IN:
4718 res = PySequence_Contains(w, v);
4719 if (res < 0)
4720 return NULL;
4721 break;
4722 case PyCmp_NOT_IN:
4723 res = PySequence_Contains(w, v);
4724 if (res < 0)
4725 return NULL;
4726 res = !res;
4727 break;
4728 case PyCmp_EXC_MATCH:
4729 if (PyTuple_Check(w)) {
4730 Py_ssize_t i, length;
4731 length = PyTuple_Size(w);
4732 for (i = 0; i < length; i += 1) {
4733 PyObject *exc = PyTuple_GET_ITEM(w, i);
4734 if (!PyExceptionClass_Check(exc)) {
4735 PyErr_SetString(PyExc_TypeError,
4736 CANNOT_CATCH_MSG);
4737 return NULL;
4738 }
4739 }
4740 }
4741 else {
4742 if (!PyExceptionClass_Check(w)) {
4743 PyErr_SetString(PyExc_TypeError,
4744 CANNOT_CATCH_MSG);
4745 return NULL;
4746 }
4747 }
4748 res = PyErr_GivenExceptionMatches(v, w);
4749 break;
4750 default:
4751 return PyObject_RichCompare(v, w, op);
4752 }
4753 v = res ? Py_True : Py_False;
4754 Py_INCREF(v);
4755 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004756}
4757
Thomas Wouters52152252000-08-17 22:55:00 +00004758static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004759import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4760{
4761 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004762 PyObject *import_func, *res;
4763 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004764
4765 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
4766 if (import_func == NULL) {
4767 PyErr_SetString(PyExc_ImportError, "__import__ not found");
4768 return NULL;
4769 }
4770
4771 /* Fast path for not overloaded __import__. */
Victor Stinnercaba55b2018-08-03 15:33:52 +02004772 if (import_func == _PyInterpreterState_GET_UNSAFE()->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004773 int ilevel = _PyLong_AsInt(level);
4774 if (ilevel == -1 && PyErr_Occurred()) {
4775 return NULL;
4776 }
4777 res = PyImport_ImportModuleLevelObject(
4778 name,
4779 f->f_globals,
4780 f->f_locals == NULL ? Py_None : f->f_locals,
4781 fromlist,
4782 ilevel);
4783 return res;
4784 }
4785
4786 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004787
4788 stack[0] = name;
4789 stack[1] = f->f_globals;
4790 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
4791 stack[3] = fromlist;
4792 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02004793 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004794 Py_DECREF(import_func);
4795 return res;
4796}
4797
4798static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004799import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004800{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004801 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02004802 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08004803 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004804
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004805 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02004806 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004807 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004808 /* Issue #17636: in case this failed because of a circular relative
4809 import, try to fallback on reading the module directly from
4810 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02004811 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07004812 if (pkgname == NULL) {
4813 goto error;
4814 }
Oren Milman6db70332017-09-19 14:23:01 +03004815 if (!PyUnicode_Check(pkgname)) {
4816 Py_CLEAR(pkgname);
4817 goto error;
4818 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004819 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07004820 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08004821 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004822 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07004823 }
Eric Snow3f9eee62017-09-15 16:35:20 -06004824 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004825 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07004826 if (x == NULL) {
4827 goto error;
4828 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004829 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004830 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07004831 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004832 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004833 if (pkgname == NULL) {
4834 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
4835 if (pkgname_or_unknown == NULL) {
4836 Py_XDECREF(pkgpath);
4837 return NULL;
4838 }
4839 } else {
4840 pkgname_or_unknown = pkgname;
4841 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004842
4843 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
4844 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08004845 errmsg = PyUnicode_FromFormat(
4846 "cannot import name %R from %R (unknown location)",
4847 name, pkgname_or_unknown
4848 );
4849 /* NULL check for errmsg done by PyErr_SetImportError. */
4850 PyErr_SetImportError(errmsg, pkgname, NULL);
4851 }
4852 else {
4853 errmsg = PyUnicode_FromFormat(
4854 "cannot import name %R from %R (%S)",
4855 name, pkgname_or_unknown, pkgpath
4856 );
4857 /* NULL check for errmsg done by PyErr_SetImportError. */
4858 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004859 }
4860
Xiang Zhang4830f582017-03-21 11:13:42 +08004861 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004862 Py_XDECREF(pkgname_or_unknown);
4863 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07004864 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00004865}
Guido van Rossumac7be682001-01-17 15:42:30 +00004866
Thomas Wouters52152252000-08-17 22:55:00 +00004867static int
4868import_all_from(PyObject *locals, PyObject *v)
4869{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004870 _Py_IDENTIFIER(__all__);
4871 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08004872 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004873 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004874 int skip_leading_underscores = 0;
4875 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004876
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004877 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
4878 return -1; /* Unexpected error */
4879 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004880 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004881 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
4882 return -1;
4883 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004884 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004885 PyErr_SetString(PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004886 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004887 return -1;
4888 }
4889 all = PyMapping_Keys(dict);
4890 Py_DECREF(dict);
4891 if (all == NULL)
4892 return -1;
4893 skip_leading_underscores = 1;
4894 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004896 for (pos = 0, err = 0; ; pos++) {
4897 name = PySequence_GetItem(all, pos);
4898 if (name == NULL) {
4899 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4900 err = -1;
4901 else
4902 PyErr_Clear();
4903 break;
4904 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08004905 if (!PyUnicode_Check(name)) {
4906 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
4907 if (modname == NULL) {
4908 Py_DECREF(name);
4909 err = -1;
4910 break;
4911 }
4912 if (!PyUnicode_Check(modname)) {
4913 PyErr_Format(PyExc_TypeError,
4914 "module __name__ must be a string, not %.100s",
4915 Py_TYPE(modname)->tp_name);
4916 }
4917 else {
4918 PyErr_Format(PyExc_TypeError,
4919 "%s in %U.%s must be str, not %.100s",
4920 skip_leading_underscores ? "Key" : "Item",
4921 modname,
4922 skip_leading_underscores ? "__dict__" : "__all__",
4923 Py_TYPE(name)->tp_name);
4924 }
4925 Py_DECREF(modname);
4926 Py_DECREF(name);
4927 err = -1;
4928 break;
4929 }
4930 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03004931 if (PyUnicode_READY(name) == -1) {
4932 Py_DECREF(name);
4933 err = -1;
4934 break;
4935 }
4936 if (PyUnicode_READ_CHAR(name, 0) == '_') {
4937 Py_DECREF(name);
4938 continue;
4939 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004940 }
4941 value = PyObject_GetAttr(v, name);
4942 if (value == NULL)
4943 err = -1;
4944 else if (PyDict_CheckExact(locals))
4945 err = PyDict_SetItem(locals, name, value);
4946 else
4947 err = PyObject_SetItem(locals, name, value);
4948 Py_DECREF(name);
4949 Py_XDECREF(value);
4950 if (err != 0)
4951 break;
4952 }
4953 Py_DECREF(all);
4954 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004955}
4956
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03004957static int
4958check_args_iterable(PyObject *func, PyObject *args)
4959{
4960 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
4961 PyErr_Format(PyExc_TypeError,
4962 "%.200s%.200s argument after * "
4963 "must be an iterable, not %.200s",
4964 PyEval_GetFuncName(func),
4965 PyEval_GetFuncDesc(func),
4966 args->ob_type->tp_name);
4967 return -1;
4968 }
4969 return 0;
4970}
4971
4972static void
4973format_kwargs_mapping_error(PyObject *func, PyObject *kwargs)
4974{
4975 PyErr_Format(PyExc_TypeError,
4976 "%.200s%.200s argument after ** "
4977 "must be a mapping, not %.200s",
4978 PyEval_GetFuncName(func),
4979 PyEval_GetFuncDesc(func),
4980 kwargs->ob_type->tp_name);
4981}
4982
Guido van Rossumac7be682001-01-17 15:42:30 +00004983static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004984format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004985{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004986 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004988 if (!obj)
4989 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004990
Serhiy Storchaka06515832016-11-20 09:13:07 +02004991 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004992 if (!obj_str)
4993 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004995 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004996}
Guido van Rossum950361c1997-01-24 13:49:28 +00004997
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004998static void
4999format_exc_unbound(PyCodeObject *co, int oparg)
5000{
5001 PyObject *name;
5002 /* Don't stomp existing exception */
5003 if (PyErr_Occurred())
5004 return;
5005 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5006 name = PyTuple_GET_ITEM(co->co_cellvars,
5007 oparg);
5008 format_exc_check_arg(
5009 PyExc_UnboundLocalError,
5010 UNBOUNDLOCAL_ERROR_MSG,
5011 name);
5012 } else {
5013 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5014 PyTuple_GET_SIZE(co->co_cellvars));
5015 format_exc_check_arg(PyExc_NameError,
5016 UNBOUNDFREE_ERROR_MSG, name);
5017 }
5018}
5019
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005020static void
5021format_awaitable_error(PyTypeObject *type, int prevopcode)
5022{
5023 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5024 if (prevopcode == BEFORE_ASYNC_WITH) {
5025 PyErr_Format(PyExc_TypeError,
5026 "'async with' received an object from __aenter__ "
5027 "that does not implement __await__: %.100s",
5028 type->tp_name);
5029 }
5030 else if (prevopcode == WITH_CLEANUP_START) {
5031 PyErr_Format(PyExc_TypeError,
5032 "'async with' received an object from __aexit__ "
5033 "that does not implement __await__: %.100s",
5034 type->tp_name);
5035 }
5036 }
5037}
5038
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005039static PyObject *
5040unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005041 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005042{
5043 PyObject *res;
5044 if (Py_REFCNT(v) == 2) {
5045 /* In the common case, there are 2 references to the value
5046 * stored in 'variable' when the += is performed: one on the
5047 * value stack (in 'v') and one still stored in the
5048 * 'variable'. We try to delete the variable now to reduce
5049 * the refcnt to 1.
5050 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005051 int opcode, oparg;
5052 NEXTOPARG();
5053 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005054 case STORE_FAST:
5055 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005056 PyObject **fastlocals = f->f_localsplus;
5057 if (GETLOCAL(oparg) == v)
5058 SETLOCAL(oparg, NULL);
5059 break;
5060 }
5061 case STORE_DEREF:
5062 {
5063 PyObject **freevars = (f->f_localsplus +
5064 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005065 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005066 if (PyCell_GET(c) == v) {
5067 PyCell_SET(c, NULL);
5068 Py_DECREF(v);
5069 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005070 break;
5071 }
5072 case STORE_NAME:
5073 {
5074 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005075 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005076 PyObject *locals = f->f_locals;
5077 if (PyDict_CheckExact(locals) &&
5078 PyDict_GetItem(locals, name) == v) {
5079 if (PyDict_DelItem(locals, name) != 0) {
5080 PyErr_Clear();
5081 }
5082 }
5083 break;
5084 }
5085 }
5086 }
5087 res = v;
5088 PyUnicode_Append(&res, w);
5089 return res;
5090}
5091
Guido van Rossum950361c1997-01-24 13:49:28 +00005092#ifdef DYNAMIC_EXECUTION_PROFILE
5093
Skip Montanarof118cb12001-10-15 20:51:38 +00005094static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005095getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005096{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005097 int i;
5098 PyObject *l = PyList_New(256);
5099 if (l == NULL) return NULL;
5100 for (i = 0; i < 256; i++) {
5101 PyObject *x = PyLong_FromLong(a[i]);
5102 if (x == NULL) {
5103 Py_DECREF(l);
5104 return NULL;
5105 }
5106 PyList_SetItem(l, i, x);
5107 }
5108 for (i = 0; i < 256; i++)
5109 a[i] = 0;
5110 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005111}
5112
5113PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005114_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005115{
5116#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005117 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005118#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005119 int i;
5120 PyObject *l = PyList_New(257);
5121 if (l == NULL) return NULL;
5122 for (i = 0; i < 257; i++) {
5123 PyObject *x = getarray(dxpairs[i]);
5124 if (x == NULL) {
5125 Py_DECREF(l);
5126 return NULL;
5127 }
5128 PyList_SetItem(l, i, x);
5129 }
5130 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005131#endif
5132}
5133
5134#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005135
5136Py_ssize_t
5137_PyEval_RequestCodeExtraIndex(freefunc free)
5138{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005139 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005140 Py_ssize_t new_index;
5141
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005142 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005143 return -1;
5144 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005145 new_index = interp->co_extra_user_count++;
5146 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005147 return new_index;
5148}
Łukasz Langaa785c872016-09-09 17:37:37 -07005149
5150static void
5151dtrace_function_entry(PyFrameObject *f)
5152{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005153 const char *filename;
5154 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005155 int lineno;
5156
5157 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5158 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5159 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5160
5161 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5162}
5163
5164static void
5165dtrace_function_return(PyFrameObject *f)
5166{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005167 const char *filename;
5168 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005169 int lineno;
5170
5171 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5172 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5173 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5174
5175 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5176}
5177
5178/* DTrace equivalent of maybe_call_line_trace. */
5179static void
5180maybe_dtrace_line(PyFrameObject *frame,
5181 int *instr_lb, int *instr_ub, int *instr_prev)
5182{
5183 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005184 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005185
5186 /* If the last instruction executed isn't in the current
5187 instruction window, reset the window.
5188 */
5189 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5190 PyAddrPair bounds;
5191 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5192 &bounds);
5193 *instr_lb = bounds.ap_lower;
5194 *instr_ub = bounds.ap_upper;
5195 }
5196 /* If the last instruction falls at the start of a line or if
5197 it represents a jump backwards, update the frame's line
5198 number and call the trace function. */
5199 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5200 frame->f_lineno = line;
5201 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5202 if (!co_filename)
5203 co_filename = "?";
5204 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5205 if (!co_name)
5206 co_name = "?";
5207 PyDTrace_LINE(co_filename, co_name, line);
5208 }
5209 *instr_prev = frame->f_lasti;
5210}