blob: 07db1d378b6cb95c2205b8c000f389d49fb487f2 [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"
Victor Stinnerbcda8f12018-11-21 22:27:47 +010013#include "pycore_object.h"
Victor Stinner621cebe2018-11-12 16:53:38 +010014#include "pycore_pystate.h"
Victor Stinnerec13b932018-11-25 23:56:17 +010015#include "pycore_tupleobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000016
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000017#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040018#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000019#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000020#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070021#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040022#include "setobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000023#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000024
Guido van Rossumc6004111993-11-05 10:22:19 +000025#include <ctype.h>
26
Guido van Rossum408027e1996-12-30 16:17:54 +000027#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000028/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029#define LLTRACE 1 /* Low-level trace feature */
30#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000031#endif
32
Victor Stinner5c75f372019-04-17 23:02:26 +020033#if !defined(Py_BUILD_CORE)
34# error "ceval.c must be build with Py_BUILD_CORE define for best performance"
35#endif
36
Yury Selivanovf2392132016-12-13 19:03:51 -050037/* Private API for the LOAD_METHOD opcode. */
38extern int _PyObject_GetMethod(PyObject *, PyObject *, PyObject **);
39
Jeremy Hylton52820442001-01-03 23:52:36 +000040typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000041
Guido van Rossum374a9221991-04-04 10:40:29 +000042/* Forward declarations */
Eric Snow2ebc5ce2017-09-07 23:51:28 -060043Py_LOCAL_INLINE(PyObject *) call_function(PyObject ***, Py_ssize_t,
44 PyObject *);
Victor Stinnerf9b760f2016-09-09 10:17:08 -070045static PyObject * do_call_core(PyObject *, PyObject *, PyObject *);
Jeremy Hylton52820442001-01-03 23:52:36 +000046
Guido van Rossum0a066c01992-03-27 17:29:15 +000047#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000048static int lltrace;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020049static int prtrace(PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000050#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010051static int call_trace(Py_tracefunc, PyObject *,
52 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000054static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010055 PyThreadState *, PyFrameObject *,
56 int, PyObject *);
57static void call_exc_trace(Py_tracefunc, PyObject *,
58 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000059static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060060 PyThreadState *, PyFrameObject *,
61 int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070062static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
63static void dtrace_function_entry(PyFrameObject *);
64static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000065
Thomas Wouters477c8d52006-05-27 19:21:47 +000066static PyObject * cmp_outcome(int, PyObject *, PyObject *);
Eric Snow2ebc5ce2017-09-07 23:51:28 -060067static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *,
68 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +000069static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +000070static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +000071static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +000072static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +020073static PyObject * unicode_concatenate(PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030074 PyFrameObject *, const _Py_CODEUNIT *);
Benjamin Petersonce798522012-01-22 11:24:29 -050075static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Serhiy Storchaka25e4f772017-08-03 11:37:15 +030076static int check_args_iterable(PyObject *func, PyObject *vararg);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +020077static void format_kwargs_error(PyObject *func, PyObject *kwargs);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +030078static void format_awaitable_error(PyTypeObject *, int);
Joannah Nanjekyef781d202019-04-29 04:38:45 -040079static inline void exit_thread_if_finalizing(PyThreadState *);
Guido van Rossum374a9221991-04-04 10:40:29 +000080
Paul Prescode68140d2000-08-30 20:25:01 +000081#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000083#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000085#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 "free variable '%.200s' referenced before assignment" \
87 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000088
Guido van Rossum950361c1997-01-24 13:49:28 +000089/* Dynamic execution profile */
90#ifdef DYNAMIC_EXECUTION_PROFILE
91#ifdef DXPAIRS
92static long dxpairs[257][256];
93#define dxp dxpairs[256]
94#else
95static long dxp[256];
96#endif
97#endif
98
Eric Snow2ebc5ce2017-09-07 23:51:28 -060099#define GIL_REQUEST _Py_atomic_load_relaxed(&_PyRuntime.ceval.gil_drop_request)
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000100
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000101/* This can set eval_breaker to 0 even though gil_drop_request became
102 1. We believe this is all right because the eval loop will release
103 the GIL eventually anyway. */
Eric Snowb75b1a352019-04-12 10:20:10 -0600104#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 _Py_atomic_store_relaxed( \
Eric Snowb75b1a352019-04-12 10:20:10 -0600106 &_PyRuntime.ceval.eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000107 GIL_REQUEST | \
Eric Snowfdf282d2019-01-11 14:26:55 -0700108 _Py_atomic_load_relaxed(&_PyRuntime.ceval.signals_pending) | \
Eric Snowb75b1a352019-04-12 10:20:10 -0600109 _Py_atomic_load_relaxed(&_PyRuntime.ceval.pending.calls_to_do) | \
110 _PyRuntime.ceval.pending.async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000111
Eric Snowb75b1a352019-04-12 10:20:10 -0600112#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600114 _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 1); \
Eric Snowb75b1a352019-04-12 10:20:10 -0600115 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000117
Eric Snowb75b1a352019-04-12 10:20:10 -0600118#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000119 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600120 _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 0); \
Eric Snowb75b1a352019-04-12 10:20:10 -0600121 COMPUTE_EVAL_BREAKER(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000123
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000124/* Pending calls are only modified under pending_lock */
Eric Snowb75b1a352019-04-12 10:20:10 -0600125#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 do { \
Eric Snowb75b1a352019-04-12 10:20:10 -0600127 _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 1); \
128 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000130
Eric Snowb75b1a352019-04-12 10:20:10 -0600131#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 do { \
Eric Snowb75b1a352019-04-12 10:20:10 -0600133 _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 0); \
134 COMPUTE_EVAL_BREAKER(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000136
Eric Snowfdf282d2019-01-11 14:26:55 -0700137#define SIGNAL_PENDING_SIGNALS() \
138 do { \
139 _Py_atomic_store_relaxed(&_PyRuntime.ceval.signals_pending, 1); \
Eric Snowb75b1a352019-04-12 10:20:10 -0600140 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700141 } while (0)
142
143#define UNSIGNAL_PENDING_SIGNALS() \
144 do { \
145 _Py_atomic_store_relaxed(&_PyRuntime.ceval.signals_pending, 0); \
Eric Snowb75b1a352019-04-12 10:20:10 -0600146 COMPUTE_EVAL_BREAKER(); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700147 } while (0)
148
Eric Snowb75b1a352019-04-12 10:20:10 -0600149#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 do { \
Eric Snowb75b1a352019-04-12 10:20:10 -0600151 _PyRuntime.ceval.pending.async_exc = 1; \
152 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000154
Eric Snowb75b1a352019-04-12 10:20:10 -0600155#define UNSIGNAL_ASYNC_EXC() \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600156 do { \
Eric Snowb75b1a352019-04-12 10:20:10 -0600157 _PyRuntime.ceval.pending.async_exc = 0; \
158 COMPUTE_EVAL_BREAKER(); \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600159 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000160
161
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000162#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000163#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000164#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000165#include "pythread.h"
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000166#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000167
Tim Peters7f468f22004-10-11 02:40:51 +0000168int
169PyEval_ThreadsInitialized(void)
170{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000172}
173
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000174void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000175PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000176{
Victor Stinnera7126792019-03-19 14:19:38 +0100177 if (gil_created()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100179 }
180
Inada Naoki001fee12019-02-20 10:00:09 +0900181 PyThread_init_thread();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000182 create_gil();
Victor Stinner50b48572018-11-01 01:51:40 +0100183 take_gil(_PyThreadState_GET());
Eric Snow8479a342019-03-08 23:44:33 -0700184
Eric Snowb75b1a352019-04-12 10:20:10 -0600185 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
186 if (_PyRuntime.ceval.pending.lock == NULL) {
187 Py_FatalError("Can't initialize threads for pending calls");
188 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000189}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000190
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000191void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000192_PyEval_FiniThreads(void)
193{
Victor Stinnera7126792019-03-19 14:19:38 +0100194 if (!gil_created()) {
Antoine Pitrou1df15362010-09-13 14:16:46 +0000195 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100196 }
197
Antoine Pitrou1df15362010-09-13 14:16:46 +0000198 destroy_gil();
199 assert(!gil_created());
Victor Stinner99fcc612019-04-29 13:04:07 +0200200
201 if (_PyRuntime.ceval.pending.lock != NULL) {
202 PyThread_free_lock(_PyRuntime.ceval.pending.lock);
203 _PyRuntime.ceval.pending.lock = NULL;
204 }
Antoine Pitrou1df15362010-09-13 14:16:46 +0000205}
206
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400207static inline void
208exit_thread_if_finalizing(PyThreadState *tstate)
209{
210 /* _Py_Finalizing is protected by the GIL */
211 if (_Py_IsFinalizing() && !_Py_CURRENTLY_FINALIZING(tstate)) {
212 drop_gil(tstate);
213 PyThread_exit_thread();
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400214 }
215}
216
Antoine Pitrou1df15362010-09-13 14:16:46 +0000217void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000218PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000219{
Victor Stinner50b48572018-11-01 01:51:40 +0100220 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 if (tstate == NULL)
222 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
223 take_gil(tstate);
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400224 exit_thread_if_finalizing(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000225}
226
227void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000228PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000229{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100231 We therefore avoid PyThreadState_Get() which dumps a fatal error
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 in debug mode.
233 */
Victor Stinner50b48572018-11-01 01:51:40 +0100234 drop_gil(_PyThreadState_GET());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000235}
236
237void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000238PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000239{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 if (tstate == NULL)
241 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
242 /* Check someone has called PyEval_InitThreads() to create the lock */
243 assert(gil_created());
244 take_gil(tstate);
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400245 exit_thread_if_finalizing(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 if (PyThreadState_Swap(tstate) != NULL)
247 Py_FatalError(
248 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000249}
250
251void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000252PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000253{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 if (tstate == NULL)
255 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
256 if (PyThreadState_Swap(NULL) != tstate)
257 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
258 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000259}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000260
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200261/* This function is called from PyOS_AfterFork_Child to destroy all threads
262 * which are not running in the child process, and clear internal locks
263 * which might be held by those threads.
264 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000265
266void
267PyEval_ReInitThreads(void)
268{
Victor Stinner50b48572018-11-01 01:51:40 +0100269 PyThreadState *current_tstate = _PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 if (!gil_created())
272 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 recreate_gil();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200274 take_gil(current_tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700275
Eric Snowb75b1a352019-04-12 10:20:10 -0600276 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
277 if (_PyRuntime.ceval.pending.lock == NULL) {
Eric Snow8479a342019-03-08 23:44:33 -0700278 Py_FatalError("Can't initialize threads for pending calls");
279 }
Jesse Nollera8513972008-07-17 16:49:17 +0000280
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200281 /* Destroy all threads except the current one */
282 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000283}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000284
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000285/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600286 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000287
288void
Eric Snowb75b1a352019-04-12 10:20:10 -0600289_PyEval_SignalAsyncExc(void)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000290{
Eric Snowb75b1a352019-04-12 10:20:10 -0600291 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000292}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000293
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000294PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000295PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000296{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 PyThreadState *tstate = PyThreadState_Swap(NULL);
298 if (tstate == NULL)
299 Py_FatalError("PyEval_SaveThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100300 assert(gil_created());
301 drop_gil(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000303}
304
305void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000306PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000307{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 if (tstate == NULL)
309 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100310 assert(gil_created());
311
312 int err = errno;
313 take_gil(tstate);
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400314 exit_thread_if_finalizing(tstate);
Victor Stinner2914bb32018-01-29 11:57:45 +0100315 errno = err;
316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000318}
319
320
Guido van Rossuma9672091994-09-14 13:31:22 +0000321/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
322 signal handlers or Mac I/O completion routines) can schedule calls
323 to a function to be called synchronously.
324 The synchronous function is called with one void* argument.
325 It should return 0 for success or -1 for failure -- failure should
326 be accompanied by an exception.
327
328 If registry succeeds, the registry function returns 0; if it fails
329 (e.g. due to too many pending calls) it returns -1 (without setting
330 an exception condition).
331
332 Note that because registry may occur from within signal handlers,
333 or other asynchronous events, calling malloc() is unsafe!
334
Guido van Rossuma9672091994-09-14 13:31:22 +0000335 Any thread can schedule pending calls, but only the main thread
336 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000337 There is no facility to schedule calls to a particular thread, but
338 that should be easy to change, should that ever be required. In
339 that case, the static variables here should go into the python
340 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000341*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000342
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200343void
344_PyEval_SignalReceived(void)
345{
346 /* bpo-30703: Function called when the C signal handler of Python gets a
347 signal. We cannot queue a callback using Py_AddPendingCall() since
348 that function is not async-signal-safe. */
Eric Snowfdf282d2019-01-11 14:26:55 -0700349 SIGNAL_PENDING_SIGNALS();
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200350}
351
Eric Snow5be45a62019-03-08 22:47:07 -0700352/* Push one item onto the queue while holding the lock. */
353static int
Eric Snowb75b1a352019-04-12 10:20:10 -0600354_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600355 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700356{
Eric Snow842a2f02019-03-15 15:47:51 -0600357 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700358 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600359 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700360 return -1; /* Queue full */
361 }
Eric Snow842a2f02019-03-15 15:47:51 -0600362 pending->calls[i].func = func;
363 pending->calls[i].arg = arg;
364 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700365 return 0;
366}
367
368/* Pop one item off the queue while holding the lock. */
369static void
Eric Snowb75b1a352019-04-12 10:20:10 -0600370_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600371 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700372{
Eric Snow842a2f02019-03-15 15:47:51 -0600373 int i = pending->first;
374 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700375 return; /* Queue empty */
376 }
377
Eric Snow842a2f02019-03-15 15:47:51 -0600378 *func = pending->calls[i].func;
379 *arg = pending->calls[i].arg;
380 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700381}
382
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200383/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000384 scheduling to be made from any thread, and even from an executing
385 callback.
386 */
387
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000388int
Eric Snowb75b1a352019-04-12 10:20:10 -0600389Py_AddPendingCall(int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000390{
Eric Snowb75b1a352019-04-12 10:20:10 -0600391 struct _pending_calls *pending = &_PyRuntime.ceval.pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600392
393 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
394 if (pending->finishing) {
395 PyThread_release_lock(pending->lock);
396
397 PyObject *exc, *val, *tb;
398 PyErr_Fetch(&exc, &val, &tb);
399 PyErr_SetString(PyExc_SystemError,
400 "Py_AddPendingCall: cannot add pending calls "
401 "(Python shutting down)");
402 PyErr_Print();
403 PyErr_Restore(exc, val, tb);
404 return -1;
405 }
Eric Snowb75b1a352019-04-12 10:20:10 -0600406 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600407 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700408
Eric Snowb75b1a352019-04-12 10:20:10 -0600409 /* signal main loop */
410 SIGNAL_PENDING_CALLS();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000412}
413
Eric Snowfdf282d2019-01-11 14:26:55 -0700414static int
415handle_signals(void)
416{
Eric Snow5be45a62019-03-08 22:47:07 -0700417 /* Only handle signals on main thread. PyEval_InitThreads must
418 * have been called already.
419 */
420 if (PyThread_get_thread_ident() != _PyRuntime.main_thread) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700421 return 0;
422 }
Eric Snow64d6cc82019-02-23 15:40:43 -0700423 /*
424 * Ensure that the thread isn't currently running some other
425 * interpreter.
426 */
427 if (_PyInterpreterState_GET_UNSAFE() != _PyRuntime.interpreters.main) {
428 return 0;
429 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700430
431 UNSIGNAL_PENDING_SIGNALS();
Eric Snow64d6cc82019-02-23 15:40:43 -0700432 if (_PyErr_CheckSignals() < 0) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700433 SIGNAL_PENDING_SIGNALS(); /* We're not done yet */
434 return -1;
435 }
436 return 0;
437}
438
439static int
Eric Snowb75b1a352019-04-12 10:20:10 -0600440make_pending_calls(struct _pending_calls* pending)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000441{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200442 static int busy = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000443
Eric Snowb75b1a352019-04-12 10:20:10 -0600444 /* only service pending calls on main thread */
445 if (PyThread_get_thread_ident() != _PyRuntime.main_thread) {
446 return 0;
447 }
448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 /* don't perform recursive pending calls */
Eric Snowfdf282d2019-01-11 14:26:55 -0700450 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700452 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200453 busy = 1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200454 /* unsignal before starting to call callbacks, so that any callback
455 added in-between re-signals */
Eric Snowb75b1a352019-04-12 10:20:10 -0600456 UNSIGNAL_PENDING_CALLS();
Eric Snowfdf282d2019-01-11 14:26:55 -0700457 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 /* perform a bounded number of calls, in case of recursion */
Eric Snowfdf282d2019-01-11 14:26:55 -0700460 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700461 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 void *arg = NULL;
463
464 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600465 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Eric Snowb75b1a352019-04-12 10:20:10 -0600466 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600467 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700468
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100469 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700470 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100471 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700472 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700473 res = func(arg);
474 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200475 goto error;
476 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200478
Charles-François Natalif23339a2011-07-23 18:15:43 +0200479 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700480 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200481
482error:
483 busy = 0;
Eric Snowb75b1a352019-04-12 10:20:10 -0600484 SIGNAL_PENDING_CALLS();
Eric Snowfdf282d2019-01-11 14:26:55 -0700485 return res;
486}
487
Eric Snow842a2f02019-03-15 15:47:51 -0600488void
Eric Snowb75b1a352019-04-12 10:20:10 -0600489_Py_FinishPendingCalls(void)
Eric Snow842a2f02019-03-15 15:47:51 -0600490{
Eric Snowb75b1a352019-04-12 10:20:10 -0600491 struct _pending_calls *pending = &_PyRuntime.ceval.pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600492
493 assert(PyGILState_Check());
494
495 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
496 pending->finishing = 1;
497 PyThread_release_lock(pending->lock);
498
499 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
500 return;
501 }
502
Eric Snowb75b1a352019-04-12 10:20:10 -0600503 if (make_pending_calls(pending) < 0) {
Eric Snow842a2f02019-03-15 15:47:51 -0600504 PyObject *exc, *val, *tb;
505 PyErr_Fetch(&exc, &val, &tb);
506 PyErr_BadInternalCall();
507 _PyErr_ChainExceptions(exc, val, tb);
508 PyErr_Print();
509 }
510}
511
Eric Snowfdf282d2019-01-11 14:26:55 -0700512/* Py_MakePendingCalls() is a simple wrapper for the sake
513 of backward-compatibility. */
514int
515Py_MakePendingCalls(void)
516{
517 assert(PyGILState_Check());
518
519 /* Python signal handler doesn't really queue a callback: it only signals
520 that a signal was received, see _PyEval_SignalReceived(). */
521 int res = handle_signals();
522 if (res != 0) {
523 return res;
524 }
525
Eric Snowb75b1a352019-04-12 10:20:10 -0600526 res = make_pending_calls(&_PyRuntime.ceval.pending);
527 if (res != 0) {
528 return res;
529 }
530
531 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000532}
533
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000534/* The interpreter's recursion limit */
535
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000536#ifndef Py_DEFAULT_RECURSION_LIMIT
537#define Py_DEFAULT_RECURSION_LIMIT 1000
538#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600539
Eric Snow05351c12017-09-05 21:43:08 -0700540int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000541
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600542void
543_PyEval_Initialize(struct _ceval_runtime_state *state)
544{
545 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
546 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
547 _gil_initialize(&state->gil);
548}
549
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000550int
551Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000552{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600553 return _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000554}
555
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000556void
557Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000558{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600559 _PyRuntime.ceval.recursion_limit = new_limit;
560 _Py_CheckRecursionLimit = _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000561}
562
Armin Rigo2b3eb402003-10-28 12:05:48 +0000563/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
564 if the recursion_depth reaches _Py_CheckRecursionLimit.
565 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
566 to guarantee that _Py_CheckRecursiveCall() is regularly called.
567 Without USE_STACKCHECK, there is no need for this. */
568int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300569_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000570{
Victor Stinner50b48572018-11-01 01:51:40 +0100571 PyThreadState *tstate = _PyThreadState_GET();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600572 int recursion_limit = _PyRuntime.ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000573
574#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700575 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 if (PyOS_CheckStack()) {
577 --tstate->recursion_depth;
578 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
579 return -1;
580 }
pdox18967932017-10-25 23:03:01 -0700581 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700582 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700583#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 if (tstate->recursion_critical)
585 /* Somebody asked that we don't check for recursion. */
586 return 0;
587 if (tstate->overflowed) {
588 if (tstate->recursion_depth > recursion_limit + 50) {
589 /* Overflowing while handling an overflow. Give up. */
590 Py_FatalError("Cannot recover from stack overflow.");
591 }
592 return 0;
593 }
594 if (tstate->recursion_depth > recursion_limit) {
595 --tstate->recursion_depth;
596 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400597 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 "maximum recursion depth exceeded%s",
599 where);
600 return -1;
601 }
602 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000603}
604
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400605static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000606static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000607
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600608#define _Py_TracingPossible _PyRuntime.ceval.tracing_possible
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000609
Guido van Rossum374a9221991-04-04 10:40:29 +0000610
Guido van Rossumb209a111997-04-29 18:18:01 +0000611PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000612PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000613{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 return PyEval_EvalCodeEx(co,
615 globals, locals,
616 (PyObject **)NULL, 0,
617 (PyObject **)NULL, 0,
618 (PyObject **)NULL, 0,
619 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000620}
621
622
623/* Interpreter main loop */
624
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000625PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000626PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 /* This is for backward compatibility with extension modules that
628 used this API; core interpreter code should call
629 PyEval_EvalFrameEx() */
630 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000631}
632
633PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000634PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000635{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200636 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
637 return interp->eval_frame(f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700638}
639
Victor Stinnerc6944e72016-11-11 02:13:35 +0100640PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700641_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
642{
Guido van Rossum950361c1997-01-24 13:49:28 +0000643#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000645#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200646 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300647 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200648 int opcode; /* Current opcode */
649 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200650 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 PyObject *retval = NULL; /* Return value */
Victor Stinner50b48572018-11-01 01:51:40 +0100652 PyThreadState *tstate = _PyThreadState_GET();
Eric Snowb75b1a352019-04-12 10:20:10 -0600653 _Py_atomic_int *eval_breaker = &_PyRuntime.ceval.eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 is true when the line being executed has changed. The
661 initial values are such as to make this false the first
662 time it is tested. */
663 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000664
Serhiy Storchakaab874002016-09-11 13:48:15 +0300665 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 PyObject *names;
667 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000668
Brett Cannon368b4b72012-04-02 12:17:59 -0400669#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200670 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400671#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200672
Antoine Pitroub52ec782009-01-25 16:34:23 +0000673/* Computed GOTOs, or
674 the-optimization-commonly-but-improperly-known-as-"threaded code"
675 using gcc's labels-as-values extension
676 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
677
678 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000680 combined with a lookup table of jump addresses. However, since the
681 indirect jump instruction is shared by all opcodes, the CPU will have a
682 hard time making the right prediction for where to jump next (actually,
683 it will be always wrong except in the uncommon case of a sequence of
684 several identical opcodes).
685
686 "Threaded code" in contrast, uses an explicit jump table and an explicit
687 indirect jump instruction at the end of each opcode. Since the jump
688 instruction is at a different address for each opcode, the CPU will make a
689 separate prediction for each of these instructions, which is equivalent to
690 predicting the second opcode of each opcode pair. These predictions have
691 a much better chance to turn out valid, especially in small bytecode loops.
692
693 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000695 and potentially many more instructions (depending on the pipeline width).
696 A correctly predicted branch, however, is nearly free.
697
698 At the time of this writing, the "threaded code" version is up to 15-20%
699 faster than the normal "switch" version, depending on the compiler and the
700 CPU architecture.
701
702 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
703 because it would render the measurements invalid.
704
705
706 NOTE: care must be taken that the compiler doesn't try to "optimize" the
707 indirect jumps by sharing them between all opcodes. Such optimizations
708 can be disabled on gcc by using the -fno-gcse flag (or possibly
709 -fno-crossjumping).
710*/
711
Antoine Pitrou042b1282010-08-13 21:15:58 +0000712#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000713#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000714#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000715#endif
716
Antoine Pitrou042b1282010-08-13 21:15:58 +0000717#ifdef HAVE_COMPUTED_GOTOS
718 #ifndef USE_COMPUTED_GOTOS
719 #define USE_COMPUTED_GOTOS 1
720 #endif
721#else
722 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
723 #error "Computed gotos are not supported on this compiler."
724 #endif
725 #undef USE_COMPUTED_GOTOS
726 #define USE_COMPUTED_GOTOS 0
727#endif
728
729#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000730/* Import the static jump table */
731#include "opcode_targets.h"
732
Antoine Pitroub52ec782009-01-25 16:34:23 +0000733#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700734 op: \
735 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000736
Antoine Pitroub52ec782009-01-25 16:34:23 +0000737#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 { \
Eric Snow7bda9de2019-03-08 17:25:54 -0700739 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 FAST_DISPATCH(); \
741 } \
742 continue; \
743 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000744
745#ifdef LLTRACE
746#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700748 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300750 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300751 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 } \
753 goto fast_next_opcode; \
754 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000755#else
756#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700758 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300760 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300761 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 } \
763 goto fast_next_opcode; \
764 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000765#endif
766
767#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700768#define TARGET(op) op
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300769
Antoine Pitroub52ec782009-01-25 16:34:23 +0000770#define DISPATCH() continue
771#define FAST_DISPATCH() goto fast_next_opcode
772#endif
773
774
Neal Norwitza81d2202002-07-14 00:27:26 +0000775/* Tuple access macros */
776
777#ifndef Py_DEBUG
778#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
779#else
780#define GETITEM(v, i) PyTuple_GetItem((v), (i))
781#endif
782
Guido van Rossum374a9221991-04-04 10:40:29 +0000783/* Code access macros */
784
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300785/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600786#define INSTR_OFFSET() \
787 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300788#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300789 _Py_CODEUNIT word = *next_instr; \
790 opcode = _Py_OPCODE(word); \
791 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300792 next_instr++; \
793 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300794#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
795#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000796
Raymond Hettingerf606f872003-03-16 03:11:04 +0000797/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 Some opcodes tend to come in pairs thus making it possible to
799 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300800 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 Verifying the prediction costs a single high-speed test of a register
803 variable against a constant. If the pairing was good, then the
804 processor's own internal branch predication has a high likelihood of
805 success, resulting in a nearly zero-overhead transition to the
806 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300807 including its unpredictable switch-case branch. Combined with the
808 processor's internal branch prediction, a successful PREDICT has the
809 effect of making the two opcodes run as if they were a single new opcode
810 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000811
Georg Brandl86b2fb92008-07-16 03:43:04 +0000812 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 predictions turned-on and interpret the results as if some opcodes
814 had been combined or turn-off predictions so that the opcode frequency
815 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000816
817 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 the CPU to record separate branch prediction information for each
819 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000820
Raymond Hettingerf606f872003-03-16 03:11:04 +0000821*/
822
Antoine Pitrou042b1282010-08-13 21:15:58 +0000823#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000825#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300826#define PREDICT(op) \
827 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300828 _Py_CODEUNIT word = *next_instr; \
829 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300830 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300831 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300832 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300833 goto PRED_##op; \
834 } \
835 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000836#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300837#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000838
Raymond Hettingerf606f872003-03-16 03:11:04 +0000839
Guido van Rossum374a9221991-04-04 10:40:29 +0000840/* Stack manipulation macros */
841
Martin v. Löwis18e16552006-02-15 17:27:45 +0000842/* The stack can grow at most MAXINT deep, as co_nlocals and
843 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000844#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
845#define EMPTY() (STACK_LEVEL() == 0)
846#define TOP() (stack_pointer[-1])
847#define SECOND() (stack_pointer[-2])
848#define THIRD() (stack_pointer[-3])
849#define FOURTH() (stack_pointer[-4])
850#define PEEK(n) (stack_pointer[-(n)])
851#define SET_TOP(v) (stack_pointer[-1] = (v))
852#define SET_SECOND(v) (stack_pointer[-2] = (v))
853#define SET_THIRD(v) (stack_pointer[-3] = (v))
854#define SET_FOURTH(v) (stack_pointer[-4] = (v))
855#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
856#define BASIC_STACKADJ(n) (stack_pointer += n)
857#define BASIC_PUSH(v) (*stack_pointer++ = (v))
858#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000859
Guido van Rossum96a42c81992-01-12 02:29:51 +0000860#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000862 lltrace && prtrace(TOP(), "push")); \
863 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000865 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +0000866#define STACK_GROW(n) do { \
867 assert(n >= 0); \
868 (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000869 lltrace && prtrace(TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000870 assert(STACK_LEVEL() <= co->co_stacksize); \
871 } while (0)
872#define STACK_SHRINK(n) do { \
873 assert(n >= 0); \
874 (void)(lltrace && prtrace(TOP(), "stackadj")); \
875 (void)(BASIC_STACKADJ(-n)); \
876 assert(STACK_LEVEL() <= co->co_stacksize); \
877 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +0000878#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000879 prtrace((STACK_POINTER)[-1], "ext_pop")), \
880 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000881#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000882#define PUSH(v) BASIC_PUSH(v)
883#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +0000884#define STACK_GROW(n) BASIC_STACKADJ(n)
885#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000886#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000887#endif
888
Guido van Rossum681d79a1995-07-18 14:51:37 +0000889/* Local variable macros */
890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000892
893/* The SETLOCAL() macro must not DECREF the local variable in-place and
894 then store the new value; it must copy the old value to a temporary
895 value, then store the new value, and then DECREF the temporary value.
896 This is because it is possible that during the DECREF the frame is
897 accessed by other code (e.g. a __del__ method or gc.collect()) and the
898 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000900 GETLOCAL(i) = value; \
901 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000902
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000903
904#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 while (STACK_LEVEL() > (b)->b_level) { \
906 PyObject *v = POP(); \
907 Py_XDECREF(v); \
908 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000909
910#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300911 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +0100913 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 assert(STACK_LEVEL() >= (b)->b_level + 3); \
915 while (STACK_LEVEL() > (b)->b_level + 3) { \
916 value = POP(); \
917 Py_XDECREF(value); \
918 } \
Mark Shannonae3087c2017-10-22 22:41:51 +0100919 exc_info = tstate->exc_info; \
920 type = exc_info->exc_type; \
921 value = exc_info->exc_value; \
922 traceback = exc_info->exc_traceback; \
923 exc_info->exc_type = POP(); \
924 exc_info->exc_value = POP(); \
925 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 Py_XDECREF(type); \
927 Py_XDECREF(value); \
928 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300929 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000930
Guido van Rossuma027efa1997-05-05 20:56:21 +0000931/* Start of code */
932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 /* push frame */
934 if (Py_EnterRecursiveCall(""))
935 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 if (tstate->use_tracing) {
940 if (tstate->c_tracefunc != NULL) {
941 /* tstate->c_tracefunc, if defined, is a
942 function that will be called on *every* entry
943 to a code block. Its return value, if not
944 None, is a function that will be called at
945 the start of each executed line of code.
946 (Actually, the function must return itself
947 in order to continue tracing.) The trace
948 functions are called with three arguments:
949 a pointer to the current frame, a string
950 indicating why the function is called, and
951 an argument which depends on the situation.
952 The global trace function is also called
953 whenever an exception is detected. */
954 if (call_trace_protected(tstate->c_tracefunc,
955 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100956 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 /* Trace function raised an error */
958 goto exit_eval_frame;
959 }
960 }
961 if (tstate->c_profilefunc != NULL) {
962 /* Similar for c_profilefunc, except it needn't
963 return itself and isn't called for "line" events */
964 if (call_trace_protected(tstate->c_profilefunc,
965 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100966 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 /* Profile function raised an error */
968 goto exit_eval_frame;
969 }
970 }
971 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000972
Łukasz Langaa785c872016-09-09 17:37:37 -0700973 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
974 dtrace_function_entry(f);
975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 co = f->f_code;
977 names = co->co_names;
978 consts = co->co_consts;
979 fastlocals = f->f_localsplus;
980 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300981 assert(PyBytes_Check(co->co_code));
982 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +0300983 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
984 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
985 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300986 /*
987 f->f_lasti refers to the index of the last instruction,
988 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000989
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300990 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500991 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 When the PREDICT() macros are enabled, some opcode pairs follow in
994 direct succession without updating f->f_lasti. A successful
995 prediction effectively links the two codes together as if they
996 were a single new opcode; accordingly,f->f_lasti will point to
997 the first code in the pair (for instance, GET_ITER followed by
998 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300999 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001001 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001002 next_instr = first_instr;
1003 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001004 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1005 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001006 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 stack_pointer = f->f_stacktop;
1008 assert(stack_pointer != NULL);
1009 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001010 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001011
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001012
Tim Peters5ca576e2001-06-18 22:08:13 +00001013#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001014 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001015#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001016
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001017 if (throwflag) /* support for generator.throw() */
1018 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001019
Victor Stinnerace47d72013-07-18 01:41:08 +02001020#ifdef Py_DEBUG
1021 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001022 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001023 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +02001024 assert(!PyErr_Occurred());
1025#endif
1026
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001027main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1030 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +02001031 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001032
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 /* Do periodic things. Doing this every time through
1034 the loop would add too much overhead, so we do it
1035 only every Nth instruction. We also do it if
1036 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1037 event needs attention (e.g. a signal handler or
1038 async I/O handler); see Py_AddPendingCall() and
1039 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001040
Eric Snow7bda9de2019-03-08 17:25:54 -07001041 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001042 opcode = _Py_OPCODE(*next_instr);
1043 if (opcode == SETUP_FINALLY ||
1044 opcode == SETUP_WITH ||
1045 opcode == BEFORE_ASYNC_WITH ||
1046 opcode == YIELD_FROM) {
1047 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001048 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001049 - If we're about to enter the 'with:'. It will prevent
1050 emitting a resource warning in the common idiom
1051 'with open(path) as file:'.
1052 - If we're about to enter the 'async with:'.
1053 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001054 *very* useful, but might help in some cases and it's
1055 traditional)
1056 - If we're resuming a chain of nested 'yield from' or
1057 'await' calls, then each frame is parked with YIELD_FROM
1058 as its next opcode. If the user hit control-C we want to
1059 wait until we've reached the innermost frame before
1060 running the signal handler and raising KeyboardInterrupt
1061 (see bpo-30039).
1062 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 goto fast_next_opcode;
1064 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001065
1066 if (_Py_atomic_load_relaxed(
1067 &_PyRuntime.ceval.signals_pending))
1068 {
1069 if (handle_signals() != 0) {
1070 goto error;
1071 }
1072 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001073 if (_Py_atomic_load_relaxed(
Eric Snowb75b1a352019-04-12 10:20:10 -06001074 &_PyRuntime.ceval.pending.calls_to_do))
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001075 {
Eric Snowb75b1a352019-04-12 10:20:10 -06001076 if (make_pending_calls(&_PyRuntime.ceval.pending) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001077 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001078 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001080
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001081 if (_Py_atomic_load_relaxed(
1082 &_PyRuntime.ceval.gil_drop_request))
1083 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 /* Give another thread a chance */
1085 if (PyThreadState_Swap(NULL) != tstate)
1086 Py_FatalError("ceval: tstate mix-up");
1087 drop_gil(tstate);
1088
1089 /* Other threads may run now */
1090
1091 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001092
1093 /* Check if we should make a quick exit. */
Joannah Nanjekyef781d202019-04-29 04:38:45 -04001094 exit_thread_if_finalizing(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 if (PyThreadState_Swap(tstate) != NULL)
1097 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 }
1099 /* Check for asynchronous exceptions. */
1100 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001101 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 tstate->async_exc = NULL;
Eric Snowb75b1a352019-04-12 10:20:10 -06001103 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001104 PyErr_SetNone(exc);
1105 Py_DECREF(exc);
1106 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 }
1108 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001110 fast_next_opcode:
1111 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001112
Łukasz Langaa785c872016-09-09 17:37:37 -07001113 if (PyDTrace_LINE_ENABLED())
1114 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001119 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001120 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 /* see maybe_call_line_trace
1122 for expository comments */
1123 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 err = maybe_call_line_trace(tstate->c_tracefunc,
1126 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001127 tstate, f,
1128 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 /* Reload possibly changed frame fields */
1130 JUMPTO(f->f_lasti);
1131 if (f->f_stacktop != NULL) {
1132 stack_pointer = f->f_stacktop;
1133 f->f_stacktop = NULL;
1134 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001135 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001137 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001141
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001142 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001143 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001144#ifdef DYNAMIC_EXECUTION_PROFILE
1145#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 dxpairs[lastopcode][opcode]++;
1147 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001148#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001150#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001151
Guido van Rossum96a42c81992-01-12 02:29:51 +00001152#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 if (lltrace) {
1156 if (HAS_ARG(opcode)) {
1157 printf("%d: %d, %d\n",
1158 f->f_lasti, opcode, oparg);
1159 }
1160 else {
1161 printf("%d: %d\n",
1162 f->f_lasti, opcode);
1163 }
1164 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001165#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001170 It is essential that any operation that fails must goto error
1171 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001172
Benjamin Petersonddd19492018-09-16 22:38:02 -07001173 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001175 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001176
Benjamin Petersonddd19492018-09-16 22:38:02 -07001177 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001178 PyObject *value = GETLOCAL(oparg);
1179 if (value == NULL) {
1180 format_exc_check_arg(PyExc_UnboundLocalError,
1181 UNBOUNDLOCAL_ERROR_MSG,
1182 PyTuple_GetItem(co->co_varnames, oparg));
1183 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001185 Py_INCREF(value);
1186 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001188 }
1189
Benjamin Petersonddd19492018-09-16 22:38:02 -07001190 case TARGET(LOAD_CONST): {
1191 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001192 PyObject *value = GETITEM(consts, oparg);
1193 Py_INCREF(value);
1194 PUSH(value);
1195 FAST_DISPATCH();
1196 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001197
Benjamin Petersonddd19492018-09-16 22:38:02 -07001198 case TARGET(STORE_FAST): {
1199 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001200 PyObject *value = POP();
1201 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001203 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001204
Benjamin Petersonddd19492018-09-16 22:38:02 -07001205 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001206 PyObject *value = POP();
1207 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001209 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001210
Benjamin Petersonddd19492018-09-16 22:38:02 -07001211 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001212 PyObject *top = TOP();
1213 PyObject *second = SECOND();
1214 SET_TOP(second);
1215 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001217 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001218
Benjamin Petersonddd19492018-09-16 22:38:02 -07001219 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001220 PyObject *top = TOP();
1221 PyObject *second = SECOND();
1222 PyObject *third = THIRD();
1223 SET_TOP(second);
1224 SET_SECOND(third);
1225 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001227 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001228
Benjamin Petersonddd19492018-09-16 22:38:02 -07001229 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001230 PyObject *top = TOP();
1231 PyObject *second = SECOND();
1232 PyObject *third = THIRD();
1233 PyObject *fourth = FOURTH();
1234 SET_TOP(second);
1235 SET_SECOND(third);
1236 SET_THIRD(fourth);
1237 SET_FOURTH(top);
1238 FAST_DISPATCH();
1239 }
1240
Benjamin Petersonddd19492018-09-16 22:38:02 -07001241 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001242 PyObject *top = TOP();
1243 Py_INCREF(top);
1244 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001246 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001247
Benjamin Petersonddd19492018-09-16 22:38:02 -07001248 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001249 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001250 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001251 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001252 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001253 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001254 SET_TOP(top);
1255 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001256 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001257 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001258
Benjamin Petersonddd19492018-09-16 22:38:02 -07001259 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001260 PyObject *value = TOP();
1261 PyObject *res = PyNumber_Positive(value);
1262 Py_DECREF(value);
1263 SET_TOP(res);
1264 if (res == NULL)
1265 goto error;
1266 DISPATCH();
1267 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001268
Benjamin Petersonddd19492018-09-16 22:38:02 -07001269 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001270 PyObject *value = TOP();
1271 PyObject *res = PyNumber_Negative(value);
1272 Py_DECREF(value);
1273 SET_TOP(res);
1274 if (res == NULL)
1275 goto error;
1276 DISPATCH();
1277 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001278
Benjamin Petersonddd19492018-09-16 22:38:02 -07001279 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001280 PyObject *value = TOP();
1281 int err = PyObject_IsTrue(value);
1282 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 if (err == 0) {
1284 Py_INCREF(Py_True);
1285 SET_TOP(Py_True);
1286 DISPATCH();
1287 }
1288 else if (err > 0) {
1289 Py_INCREF(Py_False);
1290 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 DISPATCH();
1292 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001293 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001294 goto error;
1295 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001296
Benjamin Petersonddd19492018-09-16 22:38:02 -07001297 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001298 PyObject *value = TOP();
1299 PyObject *res = PyNumber_Invert(value);
1300 Py_DECREF(value);
1301 SET_TOP(res);
1302 if (res == NULL)
1303 goto error;
1304 DISPATCH();
1305 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001306
Benjamin Petersonddd19492018-09-16 22:38:02 -07001307 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001308 PyObject *exp = POP();
1309 PyObject *base = TOP();
1310 PyObject *res = PyNumber_Power(base, exp, Py_None);
1311 Py_DECREF(base);
1312 Py_DECREF(exp);
1313 SET_TOP(res);
1314 if (res == NULL)
1315 goto error;
1316 DISPATCH();
1317 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001318
Benjamin Petersonddd19492018-09-16 22:38:02 -07001319 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001320 PyObject *right = POP();
1321 PyObject *left = TOP();
1322 PyObject *res = PyNumber_Multiply(left, right);
1323 Py_DECREF(left);
1324 Py_DECREF(right);
1325 SET_TOP(res);
1326 if (res == NULL)
1327 goto error;
1328 DISPATCH();
1329 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001330
Benjamin Petersonddd19492018-09-16 22:38:02 -07001331 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001332 PyObject *right = POP();
1333 PyObject *left = TOP();
1334 PyObject *res = PyNumber_MatrixMultiply(left, right);
1335 Py_DECREF(left);
1336 Py_DECREF(right);
1337 SET_TOP(res);
1338 if (res == NULL)
1339 goto error;
1340 DISPATCH();
1341 }
1342
Benjamin Petersonddd19492018-09-16 22:38:02 -07001343 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001344 PyObject *divisor = POP();
1345 PyObject *dividend = TOP();
1346 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1347 Py_DECREF(dividend);
1348 Py_DECREF(divisor);
1349 SET_TOP(quotient);
1350 if (quotient == NULL)
1351 goto error;
1352 DISPATCH();
1353 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001354
Benjamin Petersonddd19492018-09-16 22:38:02 -07001355 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001356 PyObject *divisor = POP();
1357 PyObject *dividend = TOP();
1358 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1359 Py_DECREF(dividend);
1360 Py_DECREF(divisor);
1361 SET_TOP(quotient);
1362 if (quotient == NULL)
1363 goto error;
1364 DISPATCH();
1365 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001366
Benjamin Petersonddd19492018-09-16 22:38:02 -07001367 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001368 PyObject *divisor = POP();
1369 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001370 PyObject *res;
1371 if (PyUnicode_CheckExact(dividend) && (
1372 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1373 // fast path; string formatting, but not if the RHS is a str subclass
1374 // (see issue28598)
1375 res = PyUnicode_Format(dividend, divisor);
1376 } else {
1377 res = PyNumber_Remainder(dividend, divisor);
1378 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001379 Py_DECREF(divisor);
1380 Py_DECREF(dividend);
1381 SET_TOP(res);
1382 if (res == NULL)
1383 goto error;
1384 DISPATCH();
1385 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001386
Benjamin Petersonddd19492018-09-16 22:38:02 -07001387 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001388 PyObject *right = POP();
1389 PyObject *left = TOP();
1390 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001391 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1392 CPython using bytecode, it is simply worthless.
1393 See http://bugs.python.org/issue21955 and
1394 http://bugs.python.org/issue10044 for the discussion. In short,
1395 no patch shown any impact on a realistic benchmark, only a minor
1396 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001397 if (PyUnicode_CheckExact(left) &&
1398 PyUnicode_CheckExact(right)) {
1399 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001400 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001401 }
1402 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001403 sum = PyNumber_Add(left, right);
1404 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001405 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001406 Py_DECREF(right);
1407 SET_TOP(sum);
1408 if (sum == NULL)
1409 goto error;
1410 DISPATCH();
1411 }
1412
Benjamin Petersonddd19492018-09-16 22:38:02 -07001413 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001414 PyObject *right = POP();
1415 PyObject *left = TOP();
1416 PyObject *diff = PyNumber_Subtract(left, right);
1417 Py_DECREF(right);
1418 Py_DECREF(left);
1419 SET_TOP(diff);
1420 if (diff == NULL)
1421 goto error;
1422 DISPATCH();
1423 }
1424
Benjamin Petersonddd19492018-09-16 22:38:02 -07001425 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001426 PyObject *sub = POP();
1427 PyObject *container = TOP();
1428 PyObject *res = PyObject_GetItem(container, sub);
1429 Py_DECREF(container);
1430 Py_DECREF(sub);
1431 SET_TOP(res);
1432 if (res == NULL)
1433 goto error;
1434 DISPATCH();
1435 }
1436
Benjamin Petersonddd19492018-09-16 22:38:02 -07001437 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001438 PyObject *right = POP();
1439 PyObject *left = TOP();
1440 PyObject *res = PyNumber_Lshift(left, right);
1441 Py_DECREF(left);
1442 Py_DECREF(right);
1443 SET_TOP(res);
1444 if (res == NULL)
1445 goto error;
1446 DISPATCH();
1447 }
1448
Benjamin Petersonddd19492018-09-16 22:38:02 -07001449 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001450 PyObject *right = POP();
1451 PyObject *left = TOP();
1452 PyObject *res = PyNumber_Rshift(left, right);
1453 Py_DECREF(left);
1454 Py_DECREF(right);
1455 SET_TOP(res);
1456 if (res == NULL)
1457 goto error;
1458 DISPATCH();
1459 }
1460
Benjamin Petersonddd19492018-09-16 22:38:02 -07001461 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001462 PyObject *right = POP();
1463 PyObject *left = TOP();
1464 PyObject *res = PyNumber_And(left, right);
1465 Py_DECREF(left);
1466 Py_DECREF(right);
1467 SET_TOP(res);
1468 if (res == NULL)
1469 goto error;
1470 DISPATCH();
1471 }
1472
Benjamin Petersonddd19492018-09-16 22:38:02 -07001473 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001474 PyObject *right = POP();
1475 PyObject *left = TOP();
1476 PyObject *res = PyNumber_Xor(left, right);
1477 Py_DECREF(left);
1478 Py_DECREF(right);
1479 SET_TOP(res);
1480 if (res == NULL)
1481 goto error;
1482 DISPATCH();
1483 }
1484
Benjamin Petersonddd19492018-09-16 22:38:02 -07001485 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001486 PyObject *right = POP();
1487 PyObject *left = TOP();
1488 PyObject *res = PyNumber_Or(left, right);
1489 Py_DECREF(left);
1490 Py_DECREF(right);
1491 SET_TOP(res);
1492 if (res == NULL)
1493 goto error;
1494 DISPATCH();
1495 }
1496
Benjamin Petersonddd19492018-09-16 22:38:02 -07001497 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001498 PyObject *v = POP();
1499 PyObject *list = PEEK(oparg);
1500 int err;
1501 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001503 if (err != 0)
1504 goto error;
1505 PREDICT(JUMP_ABSOLUTE);
1506 DISPATCH();
1507 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001508
Benjamin Petersonddd19492018-09-16 22:38:02 -07001509 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001510 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001511 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001512 int err;
1513 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001515 if (err != 0)
1516 goto error;
1517 PREDICT(JUMP_ABSOLUTE);
1518 DISPATCH();
1519 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001520
Benjamin Petersonddd19492018-09-16 22:38:02 -07001521 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001522 PyObject *exp = POP();
1523 PyObject *base = TOP();
1524 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1525 Py_DECREF(base);
1526 Py_DECREF(exp);
1527 SET_TOP(res);
1528 if (res == NULL)
1529 goto error;
1530 DISPATCH();
1531 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001532
Benjamin Petersonddd19492018-09-16 22:38:02 -07001533 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001534 PyObject *right = POP();
1535 PyObject *left = TOP();
1536 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1537 Py_DECREF(left);
1538 Py_DECREF(right);
1539 SET_TOP(res);
1540 if (res == NULL)
1541 goto error;
1542 DISPATCH();
1543 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001544
Benjamin Petersonddd19492018-09-16 22:38:02 -07001545 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001546 PyObject *right = POP();
1547 PyObject *left = TOP();
1548 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1549 Py_DECREF(left);
1550 Py_DECREF(right);
1551 SET_TOP(res);
1552 if (res == NULL)
1553 goto error;
1554 DISPATCH();
1555 }
1556
Benjamin Petersonddd19492018-09-16 22:38:02 -07001557 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001558 PyObject *divisor = POP();
1559 PyObject *dividend = TOP();
1560 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1561 Py_DECREF(dividend);
1562 Py_DECREF(divisor);
1563 SET_TOP(quotient);
1564 if (quotient == NULL)
1565 goto error;
1566 DISPATCH();
1567 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001568
Benjamin Petersonddd19492018-09-16 22:38:02 -07001569 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001570 PyObject *divisor = POP();
1571 PyObject *dividend = TOP();
1572 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1573 Py_DECREF(dividend);
1574 Py_DECREF(divisor);
1575 SET_TOP(quotient);
1576 if (quotient == NULL)
1577 goto error;
1578 DISPATCH();
1579 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001580
Benjamin Petersonddd19492018-09-16 22:38:02 -07001581 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001582 PyObject *right = POP();
1583 PyObject *left = TOP();
1584 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1585 Py_DECREF(left);
1586 Py_DECREF(right);
1587 SET_TOP(mod);
1588 if (mod == NULL)
1589 goto error;
1590 DISPATCH();
1591 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001592
Benjamin Petersonddd19492018-09-16 22:38:02 -07001593 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001594 PyObject *right = POP();
1595 PyObject *left = TOP();
1596 PyObject *sum;
1597 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1598 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001599 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001600 }
1601 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001602 sum = PyNumber_InPlaceAdd(left, right);
1603 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001604 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001605 Py_DECREF(right);
1606 SET_TOP(sum);
1607 if (sum == NULL)
1608 goto error;
1609 DISPATCH();
1610 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001611
Benjamin Petersonddd19492018-09-16 22:38:02 -07001612 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001613 PyObject *right = POP();
1614 PyObject *left = TOP();
1615 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1616 Py_DECREF(left);
1617 Py_DECREF(right);
1618 SET_TOP(diff);
1619 if (diff == NULL)
1620 goto error;
1621 DISPATCH();
1622 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001623
Benjamin Petersonddd19492018-09-16 22:38:02 -07001624 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001625 PyObject *right = POP();
1626 PyObject *left = TOP();
1627 PyObject *res = PyNumber_InPlaceLshift(left, right);
1628 Py_DECREF(left);
1629 Py_DECREF(right);
1630 SET_TOP(res);
1631 if (res == NULL)
1632 goto error;
1633 DISPATCH();
1634 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001635
Benjamin Petersonddd19492018-09-16 22:38:02 -07001636 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001637 PyObject *right = POP();
1638 PyObject *left = TOP();
1639 PyObject *res = PyNumber_InPlaceRshift(left, right);
1640 Py_DECREF(left);
1641 Py_DECREF(right);
1642 SET_TOP(res);
1643 if (res == NULL)
1644 goto error;
1645 DISPATCH();
1646 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001647
Benjamin Petersonddd19492018-09-16 22:38:02 -07001648 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001649 PyObject *right = POP();
1650 PyObject *left = TOP();
1651 PyObject *res = PyNumber_InPlaceAnd(left, right);
1652 Py_DECREF(left);
1653 Py_DECREF(right);
1654 SET_TOP(res);
1655 if (res == NULL)
1656 goto error;
1657 DISPATCH();
1658 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001659
Benjamin Petersonddd19492018-09-16 22:38:02 -07001660 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001661 PyObject *right = POP();
1662 PyObject *left = TOP();
1663 PyObject *res = PyNumber_InPlaceXor(left, right);
1664 Py_DECREF(left);
1665 Py_DECREF(right);
1666 SET_TOP(res);
1667 if (res == NULL)
1668 goto error;
1669 DISPATCH();
1670 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001671
Benjamin Petersonddd19492018-09-16 22:38:02 -07001672 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001673 PyObject *right = POP();
1674 PyObject *left = TOP();
1675 PyObject *res = PyNumber_InPlaceOr(left, right);
1676 Py_DECREF(left);
1677 Py_DECREF(right);
1678 SET_TOP(res);
1679 if (res == NULL)
1680 goto error;
1681 DISPATCH();
1682 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001683
Benjamin Petersonddd19492018-09-16 22:38:02 -07001684 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001685 PyObject *sub = TOP();
1686 PyObject *container = SECOND();
1687 PyObject *v = THIRD();
1688 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001689 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001690 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001691 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001692 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001693 Py_DECREF(container);
1694 Py_DECREF(sub);
1695 if (err != 0)
1696 goto error;
1697 DISPATCH();
1698 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001699
Benjamin Petersonddd19492018-09-16 22:38:02 -07001700 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001701 PyObject *sub = TOP();
1702 PyObject *container = SECOND();
1703 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001704 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001705 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001706 err = PyObject_DelItem(container, sub);
1707 Py_DECREF(container);
1708 Py_DECREF(sub);
1709 if (err != 0)
1710 goto error;
1711 DISPATCH();
1712 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001713
Benjamin Petersonddd19492018-09-16 22:38:02 -07001714 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001715 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001716 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001717 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001718 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001719 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 PyErr_SetString(PyExc_RuntimeError,
1721 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001722 Py_DECREF(value);
1723 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001725 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001726 Py_DECREF(value);
1727 if (res == NULL)
1728 goto error;
1729 Py_DECREF(res);
1730 DISPATCH();
1731 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001732
Benjamin Petersonddd19492018-09-16 22:38:02 -07001733 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001734 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 switch (oparg) {
1736 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001737 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001738 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001740 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001741 /* fall through */
1742 case 0:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001743 if (do_raise(exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001744 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001745 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 break;
1747 default:
1748 PyErr_SetString(PyExc_SystemError,
1749 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 break;
1751 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001752 goto error;
1753 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001754
Benjamin Petersonddd19492018-09-16 22:38:02 -07001755 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001757 assert(f->f_iblock == 0);
Pablo Galindof00828a2019-05-09 16:52:02 +01001758 goto exit_returning;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001759 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001760
Benjamin Petersonddd19492018-09-16 22:38:02 -07001761 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001762 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001763 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001764 PyObject *obj = TOP();
1765 PyTypeObject *type = Py_TYPE(obj);
1766
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001767 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001768 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001769 }
Yury Selivanov75445082015-05-11 22:57:16 -04001770
1771 if (getter != NULL) {
1772 iter = (*getter)(obj);
1773 Py_DECREF(obj);
1774 if (iter == NULL) {
1775 SET_TOP(NULL);
1776 goto error;
1777 }
1778 }
1779 else {
1780 SET_TOP(NULL);
1781 PyErr_Format(
1782 PyExc_TypeError,
1783 "'async for' requires an object with "
1784 "__aiter__ method, got %.100s",
1785 type->tp_name);
1786 Py_DECREF(obj);
1787 goto error;
1788 }
1789
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001790 if (Py_TYPE(iter)->tp_as_async == NULL ||
1791 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001792
Yury Selivanov398ff912017-03-02 22:20:00 -05001793 SET_TOP(NULL);
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001794 PyErr_Format(
1795 PyExc_TypeError,
1796 "'async for' received an object from __aiter__ "
1797 "that does not implement __anext__: %.100s",
1798 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001799 Py_DECREF(iter);
1800 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001801 }
1802
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001803 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001804 DISPATCH();
1805 }
1806
Benjamin Petersonddd19492018-09-16 22:38:02 -07001807 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001808 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001809 PyObject *next_iter = NULL;
1810 PyObject *awaitable = NULL;
1811 PyObject *aiter = TOP();
1812 PyTypeObject *type = Py_TYPE(aiter);
1813
Yury Selivanoveb636452016-09-08 22:01:51 -07001814 if (PyAsyncGen_CheckExact(aiter)) {
1815 awaitable = type->tp_as_async->am_anext(aiter);
1816 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001817 goto error;
1818 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001819 } else {
1820 if (type->tp_as_async != NULL){
1821 getter = type->tp_as_async->am_anext;
1822 }
Yury Selivanov75445082015-05-11 22:57:16 -04001823
Yury Selivanoveb636452016-09-08 22:01:51 -07001824 if (getter != NULL) {
1825 next_iter = (*getter)(aiter);
1826 if (next_iter == NULL) {
1827 goto error;
1828 }
1829 }
1830 else {
1831 PyErr_Format(
1832 PyExc_TypeError,
1833 "'async for' requires an iterator with "
1834 "__anext__ method, got %.100s",
1835 type->tp_name);
1836 goto error;
1837 }
Yury Selivanov75445082015-05-11 22:57:16 -04001838
Yury Selivanoveb636452016-09-08 22:01:51 -07001839 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1840 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001841 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001842 PyExc_TypeError,
1843 "'async for' received an invalid object "
1844 "from __anext__: %.100s",
1845 Py_TYPE(next_iter)->tp_name);
1846
1847 Py_DECREF(next_iter);
1848 goto error;
1849 } else {
1850 Py_DECREF(next_iter);
1851 }
1852 }
Yury Selivanov75445082015-05-11 22:57:16 -04001853
1854 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001855 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001856 DISPATCH();
1857 }
1858
Benjamin Petersonddd19492018-09-16 22:38:02 -07001859 case TARGET(GET_AWAITABLE): {
1860 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001861 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001862 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001863
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03001864 if (iter == NULL) {
1865 format_awaitable_error(Py_TYPE(iterable),
1866 _Py_OPCODE(next_instr[-2]));
1867 }
1868
Yury Selivanov75445082015-05-11 22:57:16 -04001869 Py_DECREF(iterable);
1870
Yury Selivanovc724bae2016-03-02 11:30:46 -05001871 if (iter != NULL && PyCoro_CheckExact(iter)) {
1872 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1873 if (yf != NULL) {
1874 /* `iter` is a coroutine object that is being
1875 awaited, `yf` is a pointer to the current awaitable
1876 being awaited on. */
1877 Py_DECREF(yf);
1878 Py_CLEAR(iter);
1879 PyErr_SetString(
1880 PyExc_RuntimeError,
1881 "coroutine is being awaited already");
1882 /* The code below jumps to `error` if `iter` is NULL. */
1883 }
1884 }
1885
Yury Selivanov75445082015-05-11 22:57:16 -04001886 SET_TOP(iter); /* Even if it's NULL */
1887
1888 if (iter == NULL) {
1889 goto error;
1890 }
1891
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001892 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001893 DISPATCH();
1894 }
1895
Benjamin Petersonddd19492018-09-16 22:38:02 -07001896 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001897 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001898 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001899 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001900 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1901 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001902 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001903 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001904 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001905 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001906 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001907 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001908 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001909 Py_DECREF(v);
1910 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001911 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001912 if (tstate->c_tracefunc != NULL
1913 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001914 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001915 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001916 if (err < 0)
1917 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001918 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001919 SET_TOP(val);
1920 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001921 }
Martin Panter95f53c12016-07-18 08:23:26 +00001922 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001923 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001924 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001925 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001926 f->f_lasti -= sizeof(_Py_CODEUNIT);
Pablo Galindof00828a2019-05-09 16:52:02 +01001927 goto exit_yielding;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001928 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001929
Benjamin Petersonddd19492018-09-16 22:38:02 -07001930 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07001932
1933 if (co->co_flags & CO_ASYNC_GENERATOR) {
1934 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
1935 Py_DECREF(retval);
1936 if (w == NULL) {
1937 retval = NULL;
1938 goto error;
1939 }
1940 retval = w;
1941 }
1942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 f->f_stacktop = stack_pointer;
Pablo Galindof00828a2019-05-09 16:52:02 +01001944 goto exit_yielding;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001945 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001946
Benjamin Petersonddd19492018-09-16 22:38:02 -07001947 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001948 PyObject *type, *value, *traceback;
1949 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001950 PyTryBlock *b = PyFrame_BlockPop(f);
1951 if (b->b_type != EXCEPT_HANDLER) {
1952 PyErr_SetString(PyExc_SystemError,
1953 "popped block is not an except handler");
1954 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001956 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
1957 STACK_LEVEL() <= (b)->b_level + 4);
1958 exc_info = tstate->exc_info;
1959 type = exc_info->exc_type;
1960 value = exc_info->exc_value;
1961 traceback = exc_info->exc_traceback;
1962 exc_info->exc_type = POP();
1963 exc_info->exc_value = POP();
1964 exc_info->exc_traceback = POP();
1965 Py_XDECREF(type);
1966 Py_XDECREF(value);
1967 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001969 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001970
Benjamin Petersonddd19492018-09-16 22:38:02 -07001971 case TARGET(POP_BLOCK): {
1972 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001973 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001975 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001976
Benjamin Petersonddd19492018-09-16 22:38:02 -07001977 case TARGET(POP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001978 /* If oparg is 0 at the top of the stack are 1 or 6 values:
1979 Either:
1980 - TOP = NULL or an integer
1981 or:
1982 - (TOP, SECOND, THIRD) = exc_info()
1983 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
1984
1985 If oparg is 1 the value for 'return' was additionally pushed
1986 at the top of the stack.
1987 */
1988 PyObject *res = NULL;
1989 if (oparg) {
1990 res = POP();
1991 }
1992 PyObject *exc = POP();
1993 if (exc == NULL || PyLong_CheckExact(exc)) {
1994 Py_XDECREF(exc);
1995 }
1996 else {
1997 Py_DECREF(exc);
1998 Py_DECREF(POP());
1999 Py_DECREF(POP());
2000
2001 PyObject *type, *value, *traceback;
2002 _PyErr_StackItem *exc_info;
2003 PyTryBlock *b = PyFrame_BlockPop(f);
2004 if (b->b_type != EXCEPT_HANDLER) {
2005 PyErr_SetString(PyExc_SystemError,
2006 "popped block is not an except handler");
2007 Py_XDECREF(res);
2008 goto error;
2009 }
2010 assert(STACK_LEVEL() == (b)->b_level + 3);
2011 exc_info = tstate->exc_info;
2012 type = exc_info->exc_type;
2013 value = exc_info->exc_value;
2014 traceback = exc_info->exc_traceback;
2015 exc_info->exc_type = POP();
2016 exc_info->exc_value = POP();
2017 exc_info->exc_traceback = POP();
2018 Py_XDECREF(type);
2019 Py_XDECREF(value);
2020 Py_XDECREF(traceback);
2021 }
2022 if (oparg) {
2023 PUSH(res);
2024 }
2025 DISPATCH();
2026 }
2027
Benjamin Petersonddd19492018-09-16 22:38:02 -07002028 case TARGET(CALL_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002029 PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
2030 if (ret == NULL) {
2031 goto error;
2032 }
2033 PUSH(ret);
2034 JUMPBY(oparg);
2035 FAST_DISPATCH();
2036 }
2037
Benjamin Petersonddd19492018-09-16 22:38:02 -07002038 case TARGET(BEGIN_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002039 /* Push NULL onto the stack for using it in END_FINALLY,
2040 POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH.
2041 */
2042 PUSH(NULL);
2043 FAST_DISPATCH();
2044 }
2045
Benjamin Petersonddd19492018-09-16 22:38:02 -07002046 case TARGET(END_FINALLY): {
2047 PREDICTED(END_FINALLY);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002048 /* At the top of the stack are 1 or 6 values:
2049 Either:
2050 - TOP = NULL or an integer
2051 or:
2052 - (TOP, SECOND, THIRD) = exc_info()
2053 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2054 */
2055 PyObject *exc = POP();
2056 if (exc == NULL) {
2057 FAST_DISPATCH();
2058 }
2059 else if (PyLong_CheckExact(exc)) {
2060 int ret = _PyLong_AsInt(exc);
2061 Py_DECREF(exc);
2062 if (ret == -1 && PyErr_Occurred()) {
2063 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002064 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002065 JUMPTO(ret);
2066 FAST_DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002068 else {
2069 assert(PyExceptionClass_Check(exc));
2070 PyObject *val = POP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002071 PyObject *tb = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002072 PyErr_Restore(exc, val, tb);
2073 goto exception_unwind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002075 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002076
Benjamin Petersonddd19492018-09-16 22:38:02 -07002077 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002078 PyObject *exc = POP();
2079 assert(PyExceptionClass_Check(exc));
2080 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2081 PyTryBlock *b = PyFrame_BlockPop(f);
2082 assert(b->b_type == EXCEPT_HANDLER);
2083 Py_DECREF(exc);
2084 UNWIND_EXCEPT_HANDLER(b);
2085 Py_DECREF(POP());
2086 JUMPBY(oparg);
2087 FAST_DISPATCH();
2088 }
2089 else {
2090 PyObject *val = POP();
2091 PyObject *tb = POP();
2092 PyErr_Restore(exc, val, tb);
2093 goto exception_unwind;
2094 }
2095 }
2096
Benjamin Petersonddd19492018-09-16 22:38:02 -07002097 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002098 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002099
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002100 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002101 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002102 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002103 if (bc == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002104 if (!PyErr_Occurred()) {
2105 PyErr_SetString(PyExc_NameError,
2106 "__build_class__ not found");
2107 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002108 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002109 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002110 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002111 }
2112 else {
2113 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2114 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002115 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002116 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2117 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002118 if (PyErr_ExceptionMatches(PyExc_KeyError))
2119 PyErr_SetString(PyExc_NameError,
2120 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002121 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002122 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002123 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002124 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002125 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002126 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002127
Benjamin Petersonddd19492018-09-16 22:38:02 -07002128 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002129 PyObject *name = GETITEM(names, oparg);
2130 PyObject *v = POP();
2131 PyObject *ns = f->f_locals;
2132 int err;
2133 if (ns == NULL) {
2134 PyErr_Format(PyExc_SystemError,
2135 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002136 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002137 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002139 if (PyDict_CheckExact(ns))
2140 err = PyDict_SetItem(ns, name, v);
2141 else
2142 err = PyObject_SetItem(ns, name, v);
2143 Py_DECREF(v);
2144 if (err != 0)
2145 goto error;
2146 DISPATCH();
2147 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002148
Benjamin Petersonddd19492018-09-16 22:38:02 -07002149 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002150 PyObject *name = GETITEM(names, oparg);
2151 PyObject *ns = f->f_locals;
2152 int err;
2153 if (ns == NULL) {
2154 PyErr_Format(PyExc_SystemError,
2155 "no locals when deleting %R", name);
2156 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002157 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002158 err = PyObject_DelItem(ns, name);
2159 if (err != 0) {
2160 format_exc_check_arg(PyExc_NameError,
2161 NAME_ERROR_MSG,
2162 name);
2163 goto error;
2164 }
2165 DISPATCH();
2166 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002167
Benjamin Petersonddd19492018-09-16 22:38:02 -07002168 case TARGET(UNPACK_SEQUENCE): {
2169 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002170 PyObject *seq = POP(), *item, **items;
2171 if (PyTuple_CheckExact(seq) &&
2172 PyTuple_GET_SIZE(seq) == oparg) {
2173 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002174 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002175 item = items[oparg];
2176 Py_INCREF(item);
2177 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002178 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002179 } else if (PyList_CheckExact(seq) &&
2180 PyList_GET_SIZE(seq) == oparg) {
2181 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002183 item = items[oparg];
2184 Py_INCREF(item);
2185 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002186 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002187 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002188 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002189 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002190 } else {
2191 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002192 Py_DECREF(seq);
2193 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002195 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002196 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002198
Benjamin Petersonddd19492018-09-16 22:38:02 -07002199 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002200 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2201 PyObject *seq = POP();
2202
2203 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2204 stack_pointer + totalargs)) {
2205 stack_pointer += totalargs;
2206 } else {
2207 Py_DECREF(seq);
2208 goto error;
2209 }
2210 Py_DECREF(seq);
2211 DISPATCH();
2212 }
2213
Benjamin Petersonddd19492018-09-16 22:38:02 -07002214 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002215 PyObject *name = GETITEM(names, oparg);
2216 PyObject *owner = TOP();
2217 PyObject *v = SECOND();
2218 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002219 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002220 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002221 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002222 Py_DECREF(owner);
2223 if (err != 0)
2224 goto error;
2225 DISPATCH();
2226 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002227
Benjamin Petersonddd19492018-09-16 22:38:02 -07002228 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002229 PyObject *name = GETITEM(names, oparg);
2230 PyObject *owner = POP();
2231 int err;
2232 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2233 Py_DECREF(owner);
2234 if (err != 0)
2235 goto error;
2236 DISPATCH();
2237 }
2238
Benjamin Petersonddd19492018-09-16 22:38:02 -07002239 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002240 PyObject *name = GETITEM(names, oparg);
2241 PyObject *v = POP();
2242 int err;
2243 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002244 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002245 if (err != 0)
2246 goto error;
2247 DISPATCH();
2248 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002249
Benjamin Petersonddd19492018-09-16 22:38:02 -07002250 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002251 PyObject *name = GETITEM(names, oparg);
2252 int err;
2253 err = PyDict_DelItem(f->f_globals, name);
2254 if (err != 0) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002255 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2256 format_exc_check_arg(
2257 PyExc_NameError, NAME_ERROR_MSG, name);
2258 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002259 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002260 }
2261 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002262 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002263
Benjamin Petersonddd19492018-09-16 22:38:02 -07002264 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002265 PyObject *name = GETITEM(names, oparg);
2266 PyObject *locals = f->f_locals;
2267 PyObject *v;
2268 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002269 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002270 "no locals when loading %R", name);
2271 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002273 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002274 v = PyDict_GetItemWithError(locals, name);
2275 if (v != NULL) {
2276 Py_INCREF(v);
2277 }
2278 else if (PyErr_Occurred()) {
2279 goto error;
2280 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 }
2282 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002283 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002284 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002285 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2286 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 PyErr_Clear();
2288 }
2289 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002290 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002291 v = PyDict_GetItemWithError(f->f_globals, name);
2292 if (v != NULL) {
2293 Py_INCREF(v);
2294 }
2295 else if (PyErr_Occurred()) {
2296 goto error;
2297 }
2298 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002299 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002300 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002301 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002302 if (!PyErr_Occurred()) {
2303 format_exc_check_arg(
Victor Stinnerb0b22422012-04-19 00:57:45 +02002304 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002305 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002306 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002307 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002308 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002309 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002310 }
2311 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002312 v = PyObject_GetItem(f->f_builtins, name);
2313 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002314 if (PyErr_ExceptionMatches(PyExc_KeyError))
2315 format_exc_check_arg(
2316 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002317 NAME_ERROR_MSG, name);
2318 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002319 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002320 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002321 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002322 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002323 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002324 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002325 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002326
Benjamin Petersonddd19492018-09-16 22:38:02 -07002327 case TARGET(LOAD_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002328 PyObject *name = GETITEM(names, oparg);
2329 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002330 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002331 && PyDict_CheckExact(f->f_builtins))
2332 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002333 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002334 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002335 name);
2336 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002337 if (!_PyErr_OCCURRED()) {
2338 /* _PyDict_LoadGlobal() returns NULL without raising
2339 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002340 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002341 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002342 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002343 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002345 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002347 else {
2348 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002349
2350 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002351 v = PyObject_GetItem(f->f_globals, name);
2352 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002353 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2354 goto error;
2355 PyErr_Clear();
2356
Victor Stinnerb4efc962015-11-20 09:24:02 +01002357 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002358 v = PyObject_GetItem(f->f_builtins, name);
2359 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002360 if (PyErr_ExceptionMatches(PyExc_KeyError))
2361 format_exc_check_arg(
2362 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002363 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002364 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002365 }
2366 }
2367 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002368 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002370 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002371
Benjamin Petersonddd19492018-09-16 22:38:02 -07002372 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002373 PyObject *v = GETLOCAL(oparg);
2374 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375 SETLOCAL(oparg, NULL);
2376 DISPATCH();
2377 }
2378 format_exc_check_arg(
2379 PyExc_UnboundLocalError,
2380 UNBOUNDLOCAL_ERROR_MSG,
2381 PyTuple_GetItem(co->co_varnames, oparg)
2382 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002383 goto error;
2384 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002385
Benjamin Petersonddd19492018-09-16 22:38:02 -07002386 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002387 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002388 PyObject *oldobj = PyCell_GET(cell);
2389 if (oldobj != NULL) {
2390 PyCell_SET(cell, NULL);
2391 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002392 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002393 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002394 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002395 goto error;
2396 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002397
Benjamin Petersonddd19492018-09-16 22:38:02 -07002398 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002399 PyObject *cell = freevars[oparg];
2400 Py_INCREF(cell);
2401 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002403 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002404
Benjamin Petersonddd19492018-09-16 22:38:02 -07002405 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002406 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002407 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002408 assert(locals);
2409 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2410 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2411 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2412 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2413 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002414 value = PyDict_GetItemWithError(locals, name);
2415 if (value != NULL) {
2416 Py_INCREF(value);
2417 }
2418 else if (PyErr_Occurred()) {
2419 goto error;
2420 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002421 }
2422 else {
2423 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002424 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002425 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2426 goto error;
2427 PyErr_Clear();
2428 }
2429 }
2430 if (!value) {
2431 PyObject *cell = freevars[oparg];
2432 value = PyCell_GET(cell);
2433 if (value == NULL) {
2434 format_exc_unbound(co, oparg);
2435 goto error;
2436 }
2437 Py_INCREF(value);
2438 }
2439 PUSH(value);
2440 DISPATCH();
2441 }
2442
Benjamin Petersonddd19492018-09-16 22:38:02 -07002443 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002444 PyObject *cell = freevars[oparg];
2445 PyObject *value = PyCell_GET(cell);
2446 if (value == NULL) {
2447 format_exc_unbound(co, oparg);
2448 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002450 Py_INCREF(value);
2451 PUSH(value);
2452 DISPATCH();
2453 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002454
Benjamin Petersonddd19492018-09-16 22:38:02 -07002455 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002456 PyObject *v = POP();
2457 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002458 PyObject *oldobj = PyCell_GET(cell);
2459 PyCell_SET(cell, v);
2460 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002461 DISPATCH();
2462 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002463
Benjamin Petersonddd19492018-09-16 22:38:02 -07002464 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002465 PyObject *str;
2466 PyObject *empty = PyUnicode_New(0, 0);
2467 if (empty == NULL) {
2468 goto error;
2469 }
2470 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2471 Py_DECREF(empty);
2472 if (str == NULL)
2473 goto error;
2474 while (--oparg >= 0) {
2475 PyObject *item = POP();
2476 Py_DECREF(item);
2477 }
2478 PUSH(str);
2479 DISPATCH();
2480 }
2481
Benjamin Petersonddd19492018-09-16 22:38:02 -07002482 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002483 PyObject *tup = PyTuple_New(oparg);
2484 if (tup == NULL)
2485 goto error;
2486 while (--oparg >= 0) {
2487 PyObject *item = POP();
2488 PyTuple_SET_ITEM(tup, oparg, item);
2489 }
2490 PUSH(tup);
2491 DISPATCH();
2492 }
2493
Benjamin Petersonddd19492018-09-16 22:38:02 -07002494 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002495 PyObject *list = PyList_New(oparg);
2496 if (list == NULL)
2497 goto error;
2498 while (--oparg >= 0) {
2499 PyObject *item = POP();
2500 PyList_SET_ITEM(list, oparg, item);
2501 }
2502 PUSH(list);
2503 DISPATCH();
2504 }
2505
Benjamin Petersonddd19492018-09-16 22:38:02 -07002506 case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL):
2507 case TARGET(BUILD_TUPLE_UNPACK):
2508 case TARGET(BUILD_LIST_UNPACK): {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002509 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002510 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002511 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002512 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002513
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002514 if (sum == NULL)
2515 goto error;
2516
2517 for (i = oparg; i > 0; i--) {
2518 PyObject *none_val;
2519
2520 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2521 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002522 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002523 PyErr_ExceptionMatches(PyExc_TypeError))
2524 {
2525 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002526 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002527 Py_DECREF(sum);
2528 goto error;
2529 }
2530 Py_DECREF(none_val);
2531 }
2532
2533 if (convert_to_tuple) {
2534 return_value = PyList_AsTuple(sum);
2535 Py_DECREF(sum);
2536 if (return_value == NULL)
2537 goto error;
2538 }
2539 else {
2540 return_value = sum;
2541 }
2542
2543 while (oparg--)
2544 Py_DECREF(POP());
2545 PUSH(return_value);
2546 DISPATCH();
2547 }
2548
Benjamin Petersonddd19492018-09-16 22:38:02 -07002549 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002550 PyObject *set = PySet_New(NULL);
2551 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002552 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002553 if (set == NULL)
2554 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002555 for (i = oparg; i > 0; i--) {
2556 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002557 if (err == 0)
2558 err = PySet_Add(set, item);
2559 Py_DECREF(item);
2560 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002561 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002562 if (err != 0) {
2563 Py_DECREF(set);
2564 goto error;
2565 }
2566 PUSH(set);
2567 DISPATCH();
2568 }
2569
Benjamin Petersonddd19492018-09-16 22:38:02 -07002570 case TARGET(BUILD_SET_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002571 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002572 PyObject *sum = PySet_New(NULL);
2573 if (sum == NULL)
2574 goto error;
2575
2576 for (i = oparg; i > 0; i--) {
2577 if (_PySet_Update(sum, PEEK(i)) < 0) {
2578 Py_DECREF(sum);
2579 goto error;
2580 }
2581 }
2582
2583 while (oparg--)
2584 Py_DECREF(POP());
2585 PUSH(sum);
2586 DISPATCH();
2587 }
2588
Benjamin Petersonddd19492018-09-16 22:38:02 -07002589 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002590 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002591 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2592 if (map == NULL)
2593 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002594 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002595 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002596 PyObject *key = PEEK(2*i);
2597 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002598 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002599 if (err != 0) {
2600 Py_DECREF(map);
2601 goto error;
2602 }
2603 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002604
2605 while (oparg--) {
2606 Py_DECREF(POP());
2607 Py_DECREF(POP());
2608 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002609 PUSH(map);
2610 DISPATCH();
2611 }
2612
Benjamin Petersonddd19492018-09-16 22:38:02 -07002613 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002614 _Py_IDENTIFIER(__annotations__);
2615 int err;
2616 PyObject *ann_dict;
2617 if (f->f_locals == NULL) {
2618 PyErr_Format(PyExc_SystemError,
2619 "no locals found when setting up annotations");
2620 goto error;
2621 }
2622 /* check if __annotations__ in locals()... */
2623 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002624 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002625 &PyId___annotations__);
2626 if (ann_dict == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002627 if (PyErr_Occurred()) {
2628 goto error;
2629 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002630 /* ...if not, create a new one */
2631 ann_dict = PyDict_New();
2632 if (ann_dict == NULL) {
2633 goto error;
2634 }
2635 err = _PyDict_SetItemId(f->f_locals,
2636 &PyId___annotations__, ann_dict);
2637 Py_DECREF(ann_dict);
2638 if (err != 0) {
2639 goto error;
2640 }
2641 }
2642 }
2643 else {
2644 /* do the same if locals() is not a dict */
2645 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2646 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002647 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002648 }
2649 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2650 if (ann_dict == NULL) {
2651 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2652 goto error;
2653 }
2654 PyErr_Clear();
2655 ann_dict = PyDict_New();
2656 if (ann_dict == NULL) {
2657 goto error;
2658 }
2659 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2660 Py_DECREF(ann_dict);
2661 if (err != 0) {
2662 goto error;
2663 }
2664 }
2665 else {
2666 Py_DECREF(ann_dict);
2667 }
2668 }
2669 DISPATCH();
2670 }
2671
Benjamin Petersonddd19492018-09-16 22:38:02 -07002672 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002673 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002674 PyObject *map;
2675 PyObject *keys = TOP();
2676 if (!PyTuple_CheckExact(keys) ||
2677 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2678 PyErr_SetString(PyExc_SystemError,
2679 "bad BUILD_CONST_KEY_MAP keys argument");
2680 goto error;
2681 }
2682 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2683 if (map == NULL) {
2684 goto error;
2685 }
2686 for (i = oparg; i > 0; i--) {
2687 int err;
2688 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2689 PyObject *value = PEEK(i + 1);
2690 err = PyDict_SetItem(map, key, value);
2691 if (err != 0) {
2692 Py_DECREF(map);
2693 goto error;
2694 }
2695 }
2696
2697 Py_DECREF(POP());
2698 while (oparg--) {
2699 Py_DECREF(POP());
2700 }
2701 PUSH(map);
2702 DISPATCH();
2703 }
2704
Benjamin Petersonddd19492018-09-16 22:38:02 -07002705 case TARGET(BUILD_MAP_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002706 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002707 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002708 if (sum == NULL)
2709 goto error;
2710
2711 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002712 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002713 if (PyDict_Update(sum, arg) < 0) {
2714 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2715 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002716 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002717 arg->ob_type->tp_name);
2718 }
2719 Py_DECREF(sum);
2720 goto error;
2721 }
2722 }
2723
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002724 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002725 Py_DECREF(POP());
2726 PUSH(sum);
2727 DISPATCH();
2728 }
2729
Benjamin Petersonddd19492018-09-16 22:38:02 -07002730 case TARGET(BUILD_MAP_UNPACK_WITH_CALL): {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002731 Py_ssize_t i;
2732 PyObject *sum = PyDict_New();
2733 if (sum == NULL)
2734 goto error;
2735
2736 for (i = oparg; i > 0; i--) {
2737 PyObject *arg = PEEK(i);
2738 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002739 Py_DECREF(sum);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02002740 format_kwargs_error(PEEK(2 + oparg), arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002741 goto error;
2742 }
2743 }
2744
2745 while (oparg--)
2746 Py_DECREF(POP());
2747 PUSH(sum);
2748 DISPATCH();
2749 }
2750
Benjamin Petersonddd19492018-09-16 22:38:02 -07002751 case TARGET(MAP_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002752 PyObject *key = TOP();
2753 PyObject *value = SECOND();
2754 PyObject *map;
2755 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002756 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002757 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002758 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002759 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002760 Py_DECREF(value);
2761 Py_DECREF(key);
2762 if (err != 0)
2763 goto error;
2764 PREDICT(JUMP_ABSOLUTE);
2765 DISPATCH();
2766 }
2767
Benjamin Petersonddd19492018-09-16 22:38:02 -07002768 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002769 PyObject *name = GETITEM(names, oparg);
2770 PyObject *owner = TOP();
2771 PyObject *res = PyObject_GetAttr(owner, name);
2772 Py_DECREF(owner);
2773 SET_TOP(res);
2774 if (res == NULL)
2775 goto error;
2776 DISPATCH();
2777 }
2778
Benjamin Petersonddd19492018-09-16 22:38:02 -07002779 case TARGET(COMPARE_OP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002780 PyObject *right = POP();
2781 PyObject *left = TOP();
2782 PyObject *res = cmp_outcome(oparg, left, right);
2783 Py_DECREF(left);
2784 Py_DECREF(right);
2785 SET_TOP(res);
2786 if (res == NULL)
2787 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002788 PREDICT(POP_JUMP_IF_FALSE);
2789 PREDICT(POP_JUMP_IF_TRUE);
2790 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002791 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002792
Benjamin Petersonddd19492018-09-16 22:38:02 -07002793 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002794 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002795 PyObject *fromlist = POP();
2796 PyObject *level = TOP();
2797 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002798 res = import_name(f, name, fromlist, level);
2799 Py_DECREF(level);
2800 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002801 SET_TOP(res);
2802 if (res == NULL)
2803 goto error;
2804 DISPATCH();
2805 }
2806
Benjamin Petersonddd19492018-09-16 22:38:02 -07002807 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002808 PyObject *from = POP(), *locals;
2809 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002810 if (PyFrame_FastToLocalsWithError(f) < 0) {
2811 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002812 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002813 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002814
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002815 locals = f->f_locals;
2816 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002817 PyErr_SetString(PyExc_SystemError,
2818 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002819 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002820 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002821 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002822 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002823 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002824 Py_DECREF(from);
2825 if (err != 0)
2826 goto error;
2827 DISPATCH();
2828 }
Guido van Rossum25831651993-05-19 14:50:45 +00002829
Benjamin Petersonddd19492018-09-16 22:38:02 -07002830 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002831 PyObject *name = GETITEM(names, oparg);
2832 PyObject *from = TOP();
2833 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002834 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002835 PUSH(res);
2836 if (res == NULL)
2837 goto error;
2838 DISPATCH();
2839 }
Thomas Wouters52152252000-08-17 22:55:00 +00002840
Benjamin Petersonddd19492018-09-16 22:38:02 -07002841 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 JUMPBY(oparg);
2843 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002844 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002845
Benjamin Petersonddd19492018-09-16 22:38:02 -07002846 case TARGET(POP_JUMP_IF_FALSE): {
2847 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002848 PyObject *cond = POP();
2849 int err;
2850 if (cond == Py_True) {
2851 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002852 FAST_DISPATCH();
2853 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002854 if (cond == Py_False) {
2855 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002856 JUMPTO(oparg);
2857 FAST_DISPATCH();
2858 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002859 err = PyObject_IsTrue(cond);
2860 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002862 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 else if (err == 0)
2864 JUMPTO(oparg);
2865 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002866 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002867 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002868 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002869
Benjamin Petersonddd19492018-09-16 22:38:02 -07002870 case TARGET(POP_JUMP_IF_TRUE): {
2871 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002872 PyObject *cond = POP();
2873 int err;
2874 if (cond == Py_False) {
2875 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 FAST_DISPATCH();
2877 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002878 if (cond == Py_True) {
2879 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 JUMPTO(oparg);
2881 FAST_DISPATCH();
2882 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002883 err = PyObject_IsTrue(cond);
2884 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002885 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002886 JUMPTO(oparg);
2887 }
2888 else if (err == 0)
2889 ;
2890 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002891 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002892 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002893 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002894
Benjamin Petersonddd19492018-09-16 22:38:02 -07002895 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002896 PyObject *cond = TOP();
2897 int err;
2898 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002899 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002900 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 FAST_DISPATCH();
2902 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002903 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002904 JUMPTO(oparg);
2905 FAST_DISPATCH();
2906 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002907 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002909 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002910 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002911 }
2912 else if (err == 0)
2913 JUMPTO(oparg);
2914 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002915 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002916 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002917 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002918
Benjamin Petersonddd19492018-09-16 22:38:02 -07002919 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002920 PyObject *cond = TOP();
2921 int err;
2922 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002923 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002924 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 FAST_DISPATCH();
2926 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002927 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002928 JUMPTO(oparg);
2929 FAST_DISPATCH();
2930 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002931 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002932 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002933 JUMPTO(oparg);
2934 }
2935 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002936 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002937 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002938 }
2939 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002940 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002941 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002942 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002943
Benjamin Petersonddd19492018-09-16 22:38:02 -07002944 case TARGET(JUMP_ABSOLUTE): {
2945 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002947#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002948 /* Enabling this path speeds-up all while and for-loops by bypassing
2949 the per-loop checks for signals. By default, this should be turned-off
2950 because it prevents detection of a control-break in tight loops like
2951 "while 1: pass". Compile with this option turned-on when you need
2952 the speed-up and do not need break checking inside tight loops (ones
2953 that contain only instructions ending with FAST_DISPATCH).
2954 */
2955 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002956#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002957 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002958#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002959 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002960
Benjamin Petersonddd19492018-09-16 22:38:02 -07002961 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002962 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002963 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002964 PyObject *iter = PyObject_GetIter(iterable);
2965 Py_DECREF(iterable);
2966 SET_TOP(iter);
2967 if (iter == NULL)
2968 goto error;
2969 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002970 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002971 DISPATCH();
2972 }
2973
Benjamin Petersonddd19492018-09-16 22:38:02 -07002974 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04002975 /* before: [obj]; after [getiter(obj)] */
2976 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002977 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002978 if (PyCoro_CheckExact(iterable)) {
2979 /* `iterable` is a coroutine */
2980 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2981 /* and it is used in a 'yield from' expression of a
2982 regular generator. */
2983 Py_DECREF(iterable);
2984 SET_TOP(NULL);
2985 PyErr_SetString(PyExc_TypeError,
2986 "cannot 'yield from' a coroutine object "
2987 "in a non-coroutine generator");
2988 goto error;
2989 }
2990 }
2991 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002992 /* `iterable` is not a generator. */
2993 iter = PyObject_GetIter(iterable);
2994 Py_DECREF(iterable);
2995 SET_TOP(iter);
2996 if (iter == NULL)
2997 goto error;
2998 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002999 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003000 DISPATCH();
3001 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003002
Benjamin Petersonddd19492018-09-16 22:38:02 -07003003 case TARGET(FOR_ITER): {
3004 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003005 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003006 PyObject *iter = TOP();
3007 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3008 if (next != NULL) {
3009 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003010 PREDICT(STORE_FAST);
3011 PREDICT(UNPACK_SEQUENCE);
3012 DISPATCH();
3013 }
3014 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003015 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
3016 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08003017 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003018 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003019 PyErr_Clear();
3020 }
3021 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003022 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003023 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003024 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003025 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003026 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003027 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003028
Benjamin Petersonddd19492018-09-16 22:38:02 -07003029 case TARGET(SETUP_FINALLY): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003030 /* NOTE: If you add any new block-setup opcodes that
3031 are not try/except/finally handlers, you may need
3032 to update the PyGen_NeedsFinalizing() function.
3033 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003034
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003035 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003036 STACK_LEVEL());
3037 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003038 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003039
Benjamin Petersonddd19492018-09-16 22:38:02 -07003040 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003041 _Py_IDENTIFIER(__aexit__);
3042 _Py_IDENTIFIER(__aenter__);
3043
3044 PyObject *mgr = TOP();
3045 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3046 *enter;
3047 PyObject *res;
3048 if (exit == NULL)
3049 goto error;
3050 SET_TOP(exit);
3051 enter = special_lookup(mgr, &PyId___aenter__);
3052 Py_DECREF(mgr);
3053 if (enter == NULL)
3054 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003055 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003056 Py_DECREF(enter);
3057 if (res == NULL)
3058 goto error;
3059 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003060 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003061 DISPATCH();
3062 }
3063
Benjamin Petersonddd19492018-09-16 22:38:02 -07003064 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003065 PyObject *res = POP();
3066 /* Setup the finally block before pushing the result
3067 of __aenter__ on the stack. */
3068 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3069 STACK_LEVEL());
3070 PUSH(res);
3071 DISPATCH();
3072 }
3073
Benjamin Petersonddd19492018-09-16 22:38:02 -07003074 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003075 _Py_IDENTIFIER(__exit__);
3076 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003077 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08003078 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003079 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08003080 if (enter == NULL)
3081 goto error;
3082 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003083 if (exit == NULL) {
3084 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003085 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003086 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003087 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003088 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003089 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003090 Py_DECREF(enter);
3091 if (res == NULL)
3092 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003093 /* Setup the finally block before pushing the result
3094 of __enter__ on the stack. */
3095 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3096 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003097
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003098 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003099 DISPATCH();
3100 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003101
Benjamin Petersonddd19492018-09-16 22:38:02 -07003102 case TARGET(WITH_CLEANUP_START): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003103 /* At the top of the stack are 1 or 6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003104 how/why we entered the finally clause:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003105 - TOP = NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003106 - (TOP, SECOND, THIRD) = exc_info()
3107 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003108 Below them is EXIT, the context.__exit__ or context.__aexit__
3109 bound method.
3110 In the first case, we must call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003111 EXIT(None, None, None)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003112 otherwise we must call
3113 EXIT(TOP, SECOND, THIRD)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003114
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003115 In the first case, we remove EXIT from the
3116 stack, leaving TOP, and push TOP on the stack.
3117 Otherwise we shift the bottom 3 values of the
3118 stack down, replace the empty spot with NULL, and push
3119 None on the stack.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003120
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003121 Finally we push the result of the call.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003122 */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003123 PyObject *stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003124 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003125 PyObject *exc, *val, *tb, *res;
3126
3127 val = tb = Py_None;
3128 exc = TOP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003129 if (exc == NULL) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003130 STACK_SHRINK(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003131 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003132 SET_TOP(exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003133 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003134 }
3135 else {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003136 assert(PyExceptionClass_Check(exc));
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003137 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003138 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003139 val = SECOND();
3140 tb = THIRD();
3141 tp2 = FOURTH();
3142 exc2 = PEEK(5);
3143 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003144 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003145 SET_VALUE(7, tb2);
3146 SET_VALUE(6, exc2);
3147 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003148 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3149 SET_FOURTH(NULL);
3150 /* We just shifted the stack down, so we have
3151 to tell the except handler block that the
3152 values are lower than it expects. */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003153 assert(f->f_iblock > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154 block = &f->f_blockstack[f->f_iblock - 1];
3155 assert(block->b_type == EXCEPT_HANDLER);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003156 assert(block->b_level > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003157 block->b_level--;
3158 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003159
3160 stack[0] = exc;
3161 stack[1] = val;
3162 stack[2] = tb;
3163 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003165 if (res == NULL)
3166 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003167
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003168 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003169 PUSH(exc);
3170 PUSH(res);
3171 PREDICT(WITH_CLEANUP_FINISH);
3172 DISPATCH();
3173 }
3174
Benjamin Petersonddd19492018-09-16 22:38:02 -07003175 case TARGET(WITH_CLEANUP_FINISH): {
3176 PREDICTED(WITH_CLEANUP_FINISH);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003177 /* TOP = the result of calling the context.__exit__ bound method
3178 SECOND = either None or exception type
3179
3180 If SECOND is None below is NULL or the return address,
3181 otherwise below are 7 values representing an exception.
3182 */
Yury Selivanov75445082015-05-11 22:57:16 -04003183 PyObject *res = POP();
3184 PyObject *exc = POP();
3185 int err;
3186
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003187 if (exc != Py_None)
3188 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003189 else
3190 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003191
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003192 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003193 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003195 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003196 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003197 else if (err > 0) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003198 /* There was an exception and a True return.
3199 * We must manually unwind the EXCEPT_HANDLER block
3200 * which was created when the exception was caught,
Quan Tian3bd0d622018-10-20 05:30:03 +08003201 * otherwise the stack will be in an inconsistent state.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003202 */
3203 PyTryBlock *b = PyFrame_BlockPop(f);
3204 assert(b->b_type == EXCEPT_HANDLER);
3205 UNWIND_EXCEPT_HANDLER(b);
3206 PUSH(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003207 }
3208 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003209 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003210 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003211
Benjamin Petersonddd19492018-09-16 22:38:02 -07003212 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003213 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003214 PyObject *name = GETITEM(names, oparg);
3215 PyObject *obj = TOP();
3216 PyObject *meth = NULL;
3217
3218 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3219
Yury Selivanovf2392132016-12-13 19:03:51 -05003220 if (meth == NULL) {
3221 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003222 goto error;
3223 }
3224
3225 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003226 /* We can bypass temporary bound method object.
3227 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003228
INADA Naoki015bce62017-01-16 17:23:30 +09003229 meth | self | arg1 | ... | argN
3230 */
3231 SET_TOP(meth);
3232 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003233 }
3234 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003235 /* meth is not an unbound method (but a regular attr, or
3236 something was returned by a descriptor protocol). Set
3237 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003238 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003239
3240 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003241 */
INADA Naoki015bce62017-01-16 17:23:30 +09003242 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003243 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003244 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003245 }
3246 DISPATCH();
3247 }
3248
Benjamin Petersonddd19492018-09-16 22:38:02 -07003249 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003250 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003251 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003252
3253 sp = stack_pointer;
3254
INADA Naoki015bce62017-01-16 17:23:30 +09003255 meth = PEEK(oparg + 2);
3256 if (meth == NULL) {
3257 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3258 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003259
3260 Stack layout:
3261
INADA Naoki015bce62017-01-16 17:23:30 +09003262 ... | NULL | callable | arg1 | ... | argN
3263 ^- TOP()
3264 ^- (-oparg)
3265 ^- (-oparg-1)
3266 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003267
Ville Skyttä49b27342017-08-03 09:00:59 +03003268 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003269 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003270 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003271 res = call_function(&sp, oparg, NULL);
3272 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003273 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003274 }
3275 else {
3276 /* This is a method call. Stack layout:
3277
INADA Naoki015bce62017-01-16 17:23:30 +09003278 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003279 ^- TOP()
3280 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003281 ^- (-oparg-1)
3282 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003283
INADA Naoki015bce62017-01-16 17:23:30 +09003284 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003285 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003286 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003287 */
3288 res = call_function(&sp, oparg + 1, NULL);
3289 stack_pointer = sp;
3290 }
3291
3292 PUSH(res);
3293 if (res == NULL)
3294 goto error;
3295 DISPATCH();
3296 }
3297
Benjamin Petersonddd19492018-09-16 22:38:02 -07003298 case TARGET(CALL_FUNCTION): {
3299 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003300 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003301 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003302 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003303 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003304 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003305 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003306 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003307 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003308 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003309 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003310
Benjamin Petersonddd19492018-09-16 22:38:02 -07003311 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003312 PyObject **sp, *res, *names;
3313
3314 names = POP();
3315 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003316 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003317 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003318 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003319 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003320 Py_DECREF(names);
3321
3322 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003323 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003324 }
3325 DISPATCH();
3326 }
3327
Benjamin Petersonddd19492018-09-16 22:38:02 -07003328 case TARGET(CALL_FUNCTION_EX): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003329 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003330 if (oparg & 0x01) {
3331 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003332 if (!PyDict_CheckExact(kwargs)) {
3333 PyObject *d = PyDict_New();
3334 if (d == NULL)
3335 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003336 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003337 Py_DECREF(d);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003338 format_kwargs_error(SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003339 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003340 goto error;
3341 }
3342 Py_DECREF(kwargs);
3343 kwargs = d;
3344 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003345 assert(PyDict_CheckExact(kwargs));
3346 }
3347 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003348 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003349 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003350 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003351 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003352 goto error;
3353 }
3354 Py_SETREF(callargs, PySequence_Tuple(callargs));
3355 if (callargs == NULL) {
3356 goto error;
3357 }
3358 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003359 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003360
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003361 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003362 Py_DECREF(func);
3363 Py_DECREF(callargs);
3364 Py_XDECREF(kwargs);
3365
3366 SET_TOP(result);
3367 if (result == NULL) {
3368 goto error;
3369 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003370 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003371 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003372
Benjamin Petersonddd19492018-09-16 22:38:02 -07003373 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003374 PyObject *qualname = POP();
3375 PyObject *codeobj = POP();
3376 PyFunctionObject *func = (PyFunctionObject *)
3377 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003378
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003379 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003380 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003381 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003382 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003383 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003384
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003385 if (oparg & 0x08) {
3386 assert(PyTuple_CheckExact(TOP()));
3387 func ->func_closure = POP();
3388 }
3389 if (oparg & 0x04) {
3390 assert(PyDict_CheckExact(TOP()));
3391 func->func_annotations = POP();
3392 }
3393 if (oparg & 0x02) {
3394 assert(PyDict_CheckExact(TOP()));
3395 func->func_kwdefaults = POP();
3396 }
3397 if (oparg & 0x01) {
3398 assert(PyTuple_CheckExact(TOP()));
3399 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003400 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003401
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003402 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003403 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003404 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003405
Benjamin Petersonddd19492018-09-16 22:38:02 -07003406 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003407 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003408 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003409 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003410 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003411 step = NULL;
3412 stop = POP();
3413 start = TOP();
3414 slice = PySlice_New(start, stop, step);
3415 Py_DECREF(start);
3416 Py_DECREF(stop);
3417 Py_XDECREF(step);
3418 SET_TOP(slice);
3419 if (slice == NULL)
3420 goto error;
3421 DISPATCH();
3422 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003423
Benjamin Petersonddd19492018-09-16 22:38:02 -07003424 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003425 /* Handles f-string value formatting. */
3426 PyObject *result;
3427 PyObject *fmt_spec;
3428 PyObject *value;
3429 PyObject *(*conv_fn)(PyObject *);
3430 int which_conversion = oparg & FVC_MASK;
3431 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3432
3433 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003434 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003435
3436 /* See if any conversion is specified. */
3437 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003438 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003439 case FVC_STR: conv_fn = PyObject_Str; break;
3440 case FVC_REPR: conv_fn = PyObject_Repr; break;
3441 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003442 default:
3443 PyErr_Format(PyExc_SystemError,
3444 "unexpected conversion flag %d",
3445 which_conversion);
3446 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003447 }
3448
3449 /* If there's a conversion function, call it and replace
3450 value with that result. Otherwise, just use value,
3451 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003452 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003453 result = conv_fn(value);
3454 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003455 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003456 Py_XDECREF(fmt_spec);
3457 goto error;
3458 }
3459 value = result;
3460 }
3461
3462 /* If value is a unicode object, and there's no fmt_spec,
3463 then we know the result of format(value) is value
3464 itself. In that case, skip calling format(). I plan to
3465 move this optimization in to PyObject_Format()
3466 itself. */
3467 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3468 /* Do nothing, just transfer ownership to result. */
3469 result = value;
3470 } else {
3471 /* Actually call format(). */
3472 result = PyObject_Format(value, fmt_spec);
3473 Py_DECREF(value);
3474 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003475 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003476 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003477 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003478 }
3479
Eric V. Smith135d5f42016-02-05 18:23:08 -05003480 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003481 DISPATCH();
3482 }
3483
Benjamin Petersonddd19492018-09-16 22:38:02 -07003484 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003485 int oldoparg = oparg;
3486 NEXTOPARG();
3487 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003488 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003489 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003490
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003491
Antoine Pitrou042b1282010-08-13 21:15:58 +00003492#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003493 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003494#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003495 default:
3496 fprintf(stderr,
3497 "XXX lineno: %d, opcode: %d\n",
3498 PyFrame_GetLineNumber(f),
3499 opcode);
3500 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003501 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003503 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003504
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003505 /* This should never be reached. Every opcode should end with DISPATCH()
3506 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003507 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003508
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003509error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003510 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003511#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003512 if (!PyErr_Occurred())
3513 PyErr_SetString(PyExc_SystemError,
3514 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003515#else
3516 assert(PyErr_Occurred());
3517#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003518
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003519 /* Log traceback info. */
3520 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003521
Benjamin Peterson51f46162013-01-23 08:38:47 -05003522 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003523 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3524 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003525
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003526exception_unwind:
3527 /* Unwind stacks if an exception occurred */
3528 while (f->f_iblock > 0) {
3529 /* Pop the current block. */
3530 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003532 if (b->b_type == EXCEPT_HANDLER) {
3533 UNWIND_EXCEPT_HANDLER(b);
3534 continue;
3535 }
3536 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003537 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003538 PyObject *exc, *val, *tb;
3539 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003540 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003541 /* Beware, this invalidates all b->b_* fields */
3542 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003543 PUSH(exc_info->exc_traceback);
3544 PUSH(exc_info->exc_value);
3545 if (exc_info->exc_type != NULL) {
3546 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003547 }
3548 else {
3549 Py_INCREF(Py_None);
3550 PUSH(Py_None);
3551 }
3552 PyErr_Fetch(&exc, &val, &tb);
3553 /* Make the raw exception data
3554 available to the handler,
3555 so a program can emulate the
3556 Python main loop. */
3557 PyErr_NormalizeException(
3558 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003559 if (tb != NULL)
3560 PyException_SetTraceback(val, tb);
3561 else
3562 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003563 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003564 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003565 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003566 exc_info->exc_value = val;
3567 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003568 if (tb == NULL)
3569 tb = Py_None;
3570 Py_INCREF(tb);
3571 PUSH(tb);
3572 PUSH(val);
3573 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003574 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003575 /* Resume normal execution */
3576 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003577 }
3578 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003579
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003580 /* End the loop as we still have an error */
3581 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003582 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003583
Pablo Galindof00828a2019-05-09 16:52:02 +01003584 assert(retval == NULL);
3585 assert(PyErr_Occurred());
3586
3587exit_returning:
3588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003589 /* Pop remaining stack entries. */
3590 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003591 PyObject *o = POP();
3592 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003593 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003594
Pablo Galindof00828a2019-05-09 16:52:02 +01003595exit_yielding:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003596 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003597 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003598 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3599 tstate, f, PyTrace_RETURN, retval)) {
3600 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003601 }
3602 }
3603 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003604 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3605 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003606 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003607 }
3608 }
3609 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003611 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003612exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003613 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3614 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003615 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003616 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003617 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003618
Victor Stinnerefde1462015-03-21 15:04:43 +01003619 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003620}
3621
Benjamin Petersonb204a422011-06-05 22:04:07 -05003622static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003623format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3624{
3625 int err;
3626 Py_ssize_t len = PyList_GET_SIZE(names);
3627 PyObject *name_str, *comma, *tail, *tmp;
3628
3629 assert(PyList_CheckExact(names));
3630 assert(len >= 1);
3631 /* Deal with the joys of natural language. */
3632 switch (len) {
3633 case 1:
3634 name_str = PyList_GET_ITEM(names, 0);
3635 Py_INCREF(name_str);
3636 break;
3637 case 2:
3638 name_str = PyUnicode_FromFormat("%U and %U",
3639 PyList_GET_ITEM(names, len - 2),
3640 PyList_GET_ITEM(names, len - 1));
3641 break;
3642 default:
3643 tail = PyUnicode_FromFormat(", %U, and %U",
3644 PyList_GET_ITEM(names, len - 2),
3645 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003646 if (tail == NULL)
3647 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003648 /* Chop off the last two objects in the list. This shouldn't actually
3649 fail, but we can't be too careful. */
3650 err = PyList_SetSlice(names, len - 2, len, NULL);
3651 if (err == -1) {
3652 Py_DECREF(tail);
3653 return;
3654 }
3655 /* Stitch everything up into a nice comma-separated list. */
3656 comma = PyUnicode_FromString(", ");
3657 if (comma == NULL) {
3658 Py_DECREF(tail);
3659 return;
3660 }
3661 tmp = PyUnicode_Join(comma, names);
3662 Py_DECREF(comma);
3663 if (tmp == NULL) {
3664 Py_DECREF(tail);
3665 return;
3666 }
3667 name_str = PyUnicode_Concat(tmp, tail);
3668 Py_DECREF(tmp);
3669 Py_DECREF(tail);
3670 break;
3671 }
3672 if (name_str == NULL)
3673 return;
3674 PyErr_Format(PyExc_TypeError,
3675 "%U() missing %i required %s argument%s: %U",
3676 co->co_name,
3677 len,
3678 kind,
3679 len == 1 ? "" : "s",
3680 name_str);
3681 Py_DECREF(name_str);
3682}
3683
3684static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003685missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003686 PyObject **fastlocals)
3687{
Victor Stinner74319ae2016-08-25 00:04:09 +02003688 Py_ssize_t i, j = 0;
3689 Py_ssize_t start, end;
3690 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003691 const char *kind = positional ? "positional" : "keyword-only";
3692 PyObject *missing_names;
3693
3694 /* Compute the names of the arguments that are missing. */
3695 missing_names = PyList_New(missing);
3696 if (missing_names == NULL)
3697 return;
3698 if (positional) {
3699 start = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003700 end = co->co_posonlyargcount + co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003701 }
3702 else {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003703 start = co->co_posonlyargcount + co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003704 end = start + co->co_kwonlyargcount;
3705 }
3706 for (i = start; i < end; i++) {
3707 if (GETLOCAL(i) == NULL) {
3708 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3709 PyObject *name = PyObject_Repr(raw);
3710 if (name == NULL) {
3711 Py_DECREF(missing_names);
3712 return;
3713 }
3714 PyList_SET_ITEM(missing_names, j++, name);
3715 }
3716 }
3717 assert(j == missing);
3718 format_missing(kind, co, missing_names);
3719 Py_DECREF(missing_names);
3720}
3721
3722static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003723too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3724 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003725{
3726 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003727 Py_ssize_t kwonly_given = 0;
3728 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003729 PyObject *sig, *kwonly_sig;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003730 Py_ssize_t co_posonlyargcount = co->co_posonlyargcount;
Victor Stinner74319ae2016-08-25 00:04:09 +02003731 Py_ssize_t co_argcount = co->co_argcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003732 Py_ssize_t total_positional = co_argcount + co_posonlyargcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003733
Benjamin Petersone109c702011-06-24 09:37:26 -05003734 assert((co->co_flags & CO_VARARGS) == 0);
3735 /* Count missing keyword-only args. */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003736 for (i = total_positional; i < total_positional + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003737 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003738 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003739 }
3740 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003741 if (defcount) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003742 Py_ssize_t atleast = total_positional - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003743 plural = 1;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003744 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, total_positional);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003745 }
3746 else {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003747 plural = (total_positional != 1);
3748 sig = PyUnicode_FromFormat("%zd", total_positional);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003749 }
3750 if (sig == NULL)
3751 return;
3752 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003753 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3754 kwonly_sig = PyUnicode_FromFormat(format,
3755 given != 1 ? "s" : "",
3756 kwonly_given,
3757 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003758 if (kwonly_sig == NULL) {
3759 Py_DECREF(sig);
3760 return;
3761 }
3762 }
3763 else {
3764 /* This will not fail. */
3765 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003766 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003767 }
3768 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003769 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003770 co->co_name,
3771 sig,
3772 plural ? "s" : "",
3773 given,
3774 kwonly_sig,
3775 given == 1 && !kwonly_given ? "was" : "were");
3776 Py_DECREF(sig);
3777 Py_DECREF(kwonly_sig);
3778}
3779
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003780static int
3781positional_only_passed_as_keyword(PyCodeObject *co, Py_ssize_t kwcount,
3782 PyObject* const* kwnames)
3783{
3784 int posonly_conflicts = 0;
3785 PyObject* posonly_names = PyList_New(0);
3786
3787 for(int k=0; k < co->co_posonlyargcount; k++){
3788 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
3789
3790 for (int k2=0; k2<kwcount; k2++){
3791 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
3792 PyObject* kwname = kwnames[k2];
3793 if (kwname == posonly_name){
3794 if(PyList_Append(posonly_names, kwname) != 0) {
3795 goto fail;
3796 }
3797 posonly_conflicts++;
3798 continue;
3799 }
3800
3801 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
3802
3803 if ( cmp > 0) {
3804 if(PyList_Append(posonly_names, kwname) != 0) {
3805 goto fail;
3806 }
3807 posonly_conflicts++;
3808 } else if (cmp < 0) {
3809 goto fail;
3810 }
3811
3812 }
3813 }
3814 if (posonly_conflicts) {
3815 PyObject* comma = PyUnicode_FromString(", ");
3816 if (comma == NULL) {
3817 goto fail;
3818 }
3819 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
3820 Py_DECREF(comma);
3821 if (error_names == NULL) {
3822 goto fail;
3823 }
3824 PyErr_Format(PyExc_TypeError,
3825 "%U() got some positional-only arguments passed"
3826 " as keyword arguments: '%U'",
3827 co->co_name, error_names);
3828 Py_DECREF(error_names);
3829 goto fail;
3830 }
3831
3832 Py_DECREF(posonly_names);
3833 return 0;
3834
3835fail:
3836 Py_XDECREF(posonly_names);
3837 return 1;
3838
3839}
3840
Guido van Rossumc2e20742006-02-27 22:32:47 +00003841/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003842 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003843 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003844
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003845PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003846_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003847 PyObject *const *args, Py_ssize_t argcount,
3848 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003849 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003850 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003851 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003852 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003853{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003854 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003855 PyFrameObject *f;
3856 PyObject *retval = NULL;
3857 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003858 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003859 PyObject *x, *u;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003860 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount + co->co_posonlyargcount;
3861 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003862 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003864 if (globals == NULL) {
3865 PyErr_SetString(PyExc_SystemError,
3866 "PyEval_EvalCodeEx: NULL globals");
3867 return NULL;
3868 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003869
Victor Stinnerc7020012016-08-16 23:40:29 +02003870 /* Create the frame */
Victor Stinner50b48572018-11-01 01:51:40 +01003871 tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003872 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003873 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003874 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003875 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003876 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003877 fastlocals = f->f_localsplus;
3878 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003879
Victor Stinnerc7020012016-08-16 23:40:29 +02003880 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003881 if (co->co_flags & CO_VARKEYWORDS) {
3882 kwdict = PyDict_New();
3883 if (kwdict == NULL)
3884 goto fail;
3885 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003886 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003887 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003888 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003889 SETLOCAL(i, kwdict);
3890 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003891 else {
3892 kwdict = NULL;
3893 }
3894
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003895 /* Copy positional only arguments into local variables */
3896 if (argcount > co->co_argcount + co->co_posonlyargcount) {
3897 n = co->co_posonlyargcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003898 }
3899 else {
3900 n = argcount;
3901 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003902 for (j = 0; j < n; j++) {
3903 x = args[j];
3904 Py_INCREF(x);
3905 SETLOCAL(j, x);
3906 }
3907
3908
3909 /* Copy positional arguments into local variables */
3910 if (argcount > co->co_argcount + co->co_posonlyargcount) {
3911 n += co->co_argcount;
3912 }
3913 else {
3914 n = argcount;
3915 }
3916 for (i = j; i < n; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003917 x = args[i];
3918 Py_INCREF(x);
3919 SETLOCAL(i, x);
3920 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003921
3922 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003923 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05003924 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003925 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003926 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003927 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003928 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003929 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003930
Serhiy Storchakab7281052016-09-12 00:52:40 +03003931 /* Handle keyword arguments passed as two strided arrays */
3932 kwcount *= kwstep;
3933 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003934 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003935 PyObject *keyword = kwnames[i];
3936 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003937 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003938
Benjamin Petersonb204a422011-06-05 22:04:07 -05003939 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3940 PyErr_Format(PyExc_TypeError,
3941 "%U() keywords must be strings",
3942 co->co_name);
3943 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003944 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003945
Benjamin Petersonb204a422011-06-05 22:04:07 -05003946 /* Speed hack: do raw pointer compares. As names are
3947 normally interned this should almost always hit. */
3948 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003949 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003950 PyObject *name = co_varnames[j];
3951 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003952 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003953 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003954 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003955
Benjamin Petersonb204a422011-06-05 22:04:07 -05003956 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003957 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003958 PyObject *name = co_varnames[j];
3959 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3960 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003961 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003962 }
3963 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003964 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003965 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003966 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003967
Victor Stinner231d1f32017-01-11 02:12:06 +01003968 assert(j >= total_args);
3969 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003970
3971 if (co->co_posonlyargcount && positional_only_passed_as_keyword(co, kwcount, kwnames)) {
3972 goto fail;
3973 }
3974
Benjamin Petersonb204a422011-06-05 22:04:07 -05003975 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003976 "%U() got an unexpected keyword argument '%S'",
3977 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003978 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003979 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003980
Christian Heimes0bd447f2013-07-20 14:48:10 +02003981 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3982 goto fail;
3983 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003984 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003985
Benjamin Petersonb204a422011-06-05 22:04:07 -05003986 kw_found:
3987 if (GETLOCAL(j) != NULL) {
3988 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003989 "%U() got multiple values for argument '%S'",
3990 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003991 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003992 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003993 Py_INCREF(value);
3994 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003995 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003996
3997 /* Check the number of positional arguments */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003998 if ((argcount > co->co_argcount + co->co_posonlyargcount) && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003999 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004000 goto fail;
4001 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004002
4003 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004004 if (argcount < co->co_posonlyargcount + co->co_argcount) {
4005 Py_ssize_t m = co->co_posonlyargcount + co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004006 Py_ssize_t missing = 0;
4007 for (i = argcount; i < m; i++) {
4008 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004009 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004010 }
4011 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004012 if (missing) {
4013 missing_arguments(co, missing, defcount, fastlocals);
4014 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004015 }
4016 if (n > m)
4017 i = n - m;
4018 else
4019 i = 0;
4020 for (; i < defcount; i++) {
4021 if (GETLOCAL(m+i) == NULL) {
4022 PyObject *def = defs[i];
4023 Py_INCREF(def);
4024 SETLOCAL(m+i, def);
4025 }
4026 }
4027 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004028
4029 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004030 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004031 Py_ssize_t missing = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004032 for (i = co->co_posonlyargcount + co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004033 PyObject *name;
4034 if (GETLOCAL(i) != NULL)
4035 continue;
4036 name = PyTuple_GET_ITEM(co->co_varnames, i);
4037 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004038 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004039 if (def) {
4040 Py_INCREF(def);
4041 SETLOCAL(i, def);
4042 continue;
4043 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004044 else if (PyErr_Occurred()) {
4045 goto fail;
4046 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004047 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004048 missing++;
4049 }
4050 if (missing) {
4051 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004052 goto fail;
4053 }
4054 }
4055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004056 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004057 vars into frame. */
4058 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004059 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004060 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004061 /* Possibly account for the cell variable being an argument. */
4062 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004063 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004064 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004065 /* Clear the local copy. */
4066 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004067 }
4068 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004069 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004070 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004071 if (c == NULL)
4072 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004073 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004074 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004075
4076 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004077 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4078 PyObject *o = PyTuple_GET_ITEM(closure, i);
4079 Py_INCREF(o);
4080 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004081 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004082
Yury Selivanoveb636452016-09-08 22:01:51 -07004083 /* Handle generator/coroutine/asynchronous generator */
4084 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004085 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04004086 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004087 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004088
4089 if (is_coro && tstate->in_coroutine_wrapper) {
4090 assert(coro_wrapper != NULL);
4091 PyErr_Format(PyExc_RuntimeError,
4092 "coroutine wrapper %.200R attempted "
4093 "to recursively wrap %.200R",
4094 coro_wrapper,
4095 co);
4096 goto fail;
4097 }
Yury Selivanov75445082015-05-11 22:57:16 -04004098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004099 /* Don't need to keep the reference to f_back, it will be set
4100 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004101 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004102
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004103 /* Create a new generator that owns the ready to run frame
4104 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004105 if (is_coro) {
4106 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004107 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4108 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004109 } else {
4110 gen = PyGen_NewWithQualName(f, name, qualname);
4111 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004112 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004113 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004114 }
INADA Naoki9c157762016-12-26 18:52:46 +09004115
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004116 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004117
Yury Selivanov94c22632015-06-04 10:16:51 -04004118 if (is_coro && coro_wrapper != NULL) {
4119 PyObject *wrapped;
4120 tstate->in_coroutine_wrapper = 1;
4121 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4122 tstate->in_coroutine_wrapper = 0;
4123 return wrapped;
4124 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004125
Yury Selivanov75445082015-05-11 22:57:16 -04004126 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004127 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004128
Victor Stinner59a73272016-12-09 18:51:13 +01004129 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004130
Thomas Woutersce272b62007-09-19 21:19:28 +00004131fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004133 /* decref'ing the frame can cause __del__ methods to get invoked,
4134 which can call back into Python. While we're done with the
4135 current Python frame (f), the associated C stack is still in use,
4136 so recursion_depth must be boosted for the duration.
4137 */
4138 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09004139 if (Py_REFCNT(f) > 1) {
4140 Py_DECREF(f);
4141 _PyObject_GC_TRACK(f);
4142 }
4143 else {
4144 ++tstate->recursion_depth;
4145 Py_DECREF(f);
4146 --tstate->recursion_depth;
4147 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004148 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004149}
4150
Victor Stinner40ee3012014-06-16 15:59:28 +02004151PyObject *
4152PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004153 PyObject *const *args, int argcount,
4154 PyObject *const *kws, int kwcount,
4155 PyObject *const *defs, int defcount,
4156 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004157{
4158 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004159 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004160 kws, kws != NULL ? kws + 1 : NULL,
4161 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004162 defs, defcount,
4163 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004164 NULL, NULL);
4165}
Tim Peters5ca576e2001-06-18 22:08:13 +00004166
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004167static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004168special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004169{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004170 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004171 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004172 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004173 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004174 return NULL;
4175 }
4176 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004177}
4178
4179
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004180/* Logic for the raise statement (too complicated for inlining).
4181 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004182static int
Collin Winter828f04a2007-08-31 00:04:24 +00004183do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004184{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004185 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004186
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004187 if (exc == NULL) {
4188 /* Reraise */
Victor Stinner50b48572018-11-01 01:51:40 +01004189 PyThreadState *tstate = _PyThreadState_GET();
Mark Shannonae3087c2017-10-22 22:41:51 +01004190 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004191 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004192 type = exc_info->exc_type;
4193 value = exc_info->exc_value;
4194 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004195 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004196 PyErr_SetString(PyExc_RuntimeError,
4197 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004198 return 0;
4199 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004200 Py_XINCREF(type);
4201 Py_XINCREF(value);
4202 Py_XINCREF(tb);
4203 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004204 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004205 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004207 /* We support the following forms of raise:
4208 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004209 raise <instance>
4210 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004212 if (PyExceptionClass_Check(exc)) {
4213 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004214 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004215 if (value == NULL)
4216 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004217 if (!PyExceptionInstance_Check(value)) {
4218 PyErr_Format(PyExc_TypeError,
4219 "calling %R should have returned an instance of "
4220 "BaseException, not %R",
4221 type, Py_TYPE(value));
4222 goto raise_error;
4223 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004224 }
4225 else if (PyExceptionInstance_Check(exc)) {
4226 value = exc;
4227 type = PyExceptionInstance_Class(exc);
4228 Py_INCREF(type);
4229 }
4230 else {
4231 /* Not something you can raise. You get an exception
4232 anyway, just not what you specified :-) */
4233 Py_DECREF(exc);
4234 PyErr_SetString(PyExc_TypeError,
4235 "exceptions must derive from BaseException");
4236 goto raise_error;
4237 }
Collin Winter828f04a2007-08-31 00:04:24 +00004238
Serhiy Storchakac0191582016-09-27 11:37:10 +03004239 assert(type != NULL);
4240 assert(value != NULL);
4241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004242 if (cause) {
4243 PyObject *fixed_cause;
4244 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004245 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004246 if (fixed_cause == NULL)
4247 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004248 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004249 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004250 else if (PyExceptionInstance_Check(cause)) {
4251 fixed_cause = cause;
4252 }
4253 else if (cause == Py_None) {
4254 Py_DECREF(cause);
4255 fixed_cause = NULL;
4256 }
4257 else {
4258 PyErr_SetString(PyExc_TypeError,
4259 "exception causes must derive from "
4260 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004261 goto raise_error;
4262 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004263 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004264 }
Collin Winter828f04a2007-08-31 00:04:24 +00004265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004266 PyErr_SetObject(type, value);
4267 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004268 Py_DECREF(value);
4269 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004270 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004271
4272raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004273 Py_XDECREF(value);
4274 Py_XDECREF(type);
4275 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004276 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004277}
4278
Tim Petersd6d010b2001-06-21 02:49:55 +00004279/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004280 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004281
Guido van Rossum0368b722007-05-11 16:50:42 +00004282 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4283 with a variable target.
4284*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004285
Barry Warsawe42b18f1997-08-25 22:13:04 +00004286static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004287unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004288{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004289 int i = 0, j = 0;
4290 Py_ssize_t ll = 0;
4291 PyObject *it; /* iter(v) */
4292 PyObject *w;
4293 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004295 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004297 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004298 if (it == NULL) {
4299 if (PyErr_ExceptionMatches(PyExc_TypeError) &&
4300 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4301 {
4302 PyErr_Format(PyExc_TypeError,
4303 "cannot unpack non-iterable %.200s object",
4304 v->ob_type->tp_name);
4305 }
4306 return 0;
4307 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004309 for (; i < argcnt; i++) {
4310 w = PyIter_Next(it);
4311 if (w == NULL) {
4312 /* Iterator done, via error or exhaustion. */
4313 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004314 if (argcntafter == -1) {
4315 PyErr_Format(PyExc_ValueError,
4316 "not enough values to unpack (expected %d, got %d)",
4317 argcnt, i);
4318 }
4319 else {
4320 PyErr_Format(PyExc_ValueError,
4321 "not enough values to unpack "
4322 "(expected at least %d, got %d)",
4323 argcnt + argcntafter, i);
4324 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004325 }
4326 goto Error;
4327 }
4328 *--sp = w;
4329 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004331 if (argcntafter == -1) {
4332 /* We better have exhausted the iterator now. */
4333 w = PyIter_Next(it);
4334 if (w == NULL) {
4335 if (PyErr_Occurred())
4336 goto Error;
4337 Py_DECREF(it);
4338 return 1;
4339 }
4340 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004341 PyErr_Format(PyExc_ValueError,
4342 "too many values to unpack (expected %d)",
4343 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004344 goto Error;
4345 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004347 l = PySequence_List(it);
4348 if (l == NULL)
4349 goto Error;
4350 *--sp = l;
4351 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004353 ll = PyList_GET_SIZE(l);
4354 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004355 PyErr_Format(PyExc_ValueError,
4356 "not enough values to unpack (expected at least %d, got %zd)",
4357 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004358 goto Error;
4359 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004361 /* Pop the "after-variable" args off the list. */
4362 for (j = argcntafter; j > 0; j--, i++) {
4363 *--sp = PyList_GET_ITEM(l, ll - j);
4364 }
4365 /* Resize the list. */
4366 Py_SIZE(l) = ll - argcntafter;
4367 Py_DECREF(it);
4368 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004369
Tim Petersd6d010b2001-06-21 02:49:55 +00004370Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004371 for (; i > 0; i--, sp++)
4372 Py_DECREF(*sp);
4373 Py_XDECREF(it);
4374 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004375}
4376
4377
Guido van Rossum96a42c81992-01-12 02:29:51 +00004378#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004379static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004380prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004381{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004382 printf("%s ", str);
4383 if (PyObject_Print(v, stdout, 0) != 0)
4384 PyErr_Clear(); /* Don't know what else to do */
4385 printf("\n");
4386 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004387}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004388#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004389
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004390static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004391call_exc_trace(Py_tracefunc func, PyObject *self,
4392 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004393{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004394 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004395 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004396 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004397 if (value == NULL) {
4398 value = Py_None;
4399 Py_INCREF(value);
4400 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004401 PyErr_NormalizeException(&type, &value, &orig_traceback);
4402 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004403 arg = PyTuple_Pack(3, type, value, traceback);
4404 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004405 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004406 return;
4407 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004408 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004409 Py_DECREF(arg);
4410 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004411 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004412 else {
4413 Py_XDECREF(type);
4414 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004415 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004416 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004417}
4418
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004419static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004420call_trace_protected(Py_tracefunc func, PyObject *obj,
4421 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004422 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004423{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004424 PyObject *type, *value, *traceback;
4425 int err;
4426 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004427 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004428 if (err == 0)
4429 {
4430 PyErr_Restore(type, value, traceback);
4431 return 0;
4432 }
4433 else {
4434 Py_XDECREF(type);
4435 Py_XDECREF(value);
4436 Py_XDECREF(traceback);
4437 return -1;
4438 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004439}
4440
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004441static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004442call_trace(Py_tracefunc func, PyObject *obj,
4443 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004444 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004445{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004446 int result;
4447 if (tstate->tracing)
4448 return 0;
4449 tstate->tracing++;
4450 tstate->use_tracing = 0;
4451 result = func(obj, frame, what, arg);
4452 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4453 || (tstate->c_profilefunc != NULL));
4454 tstate->tracing--;
4455 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004456}
4457
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004458PyObject *
4459_PyEval_CallTracing(PyObject *func, PyObject *args)
4460{
Victor Stinner50b48572018-11-01 01:51:40 +01004461 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004462 int save_tracing = tstate->tracing;
4463 int save_use_tracing = tstate->use_tracing;
4464 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004466 tstate->tracing = 0;
4467 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4468 || (tstate->c_profilefunc != NULL));
4469 result = PyObject_Call(func, args, NULL);
4470 tstate->tracing = save_tracing;
4471 tstate->use_tracing = save_use_tracing;
4472 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004473}
4474
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004475/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004476static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004477maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004478 PyThreadState *tstate, PyFrameObject *frame,
4479 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004480{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004481 int result = 0;
4482 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004484 /* If the last instruction executed isn't in the current
4485 instruction window, reset the window.
4486 */
4487 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4488 PyAddrPair bounds;
4489 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4490 &bounds);
4491 *instr_lb = bounds.ap_lower;
4492 *instr_ub = bounds.ap_upper;
4493 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004494 /* If the last instruction falls at the start of a line or if it
4495 represents a jump backwards, update the frame's line number and
4496 then call the trace function if we're tracing source lines.
4497 */
4498 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004499 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004500 if (frame->f_trace_lines) {
4501 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4502 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004503 }
George King20faa682017-10-18 17:44:22 -07004504 /* Always emit an opcode event if we're tracing all opcodes. */
4505 if (frame->f_trace_opcodes) {
4506 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4507 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004508 *instr_prev = frame->f_lasti;
4509 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004510}
4511
Fred Drake5755ce62001-06-27 19:19:46 +00004512void
4513PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004514{
Victor Stinner50b48572018-11-01 01:51:40 +01004515 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004516 PyObject *temp = tstate->c_profileobj;
4517 Py_XINCREF(arg);
4518 tstate->c_profilefunc = NULL;
4519 tstate->c_profileobj = NULL;
4520 /* Must make sure that tracing is not ignored if 'temp' is freed */
4521 tstate->use_tracing = tstate->c_tracefunc != NULL;
4522 Py_XDECREF(temp);
4523 tstate->c_profilefunc = func;
4524 tstate->c_profileobj = arg;
4525 /* Flag that tracing or profiling is turned on */
4526 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004527}
4528
4529void
4530PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4531{
Victor Stinner50b48572018-11-01 01:51:40 +01004532 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004533 PyObject *temp = tstate->c_traceobj;
4534 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4535 Py_XINCREF(arg);
4536 tstate->c_tracefunc = NULL;
4537 tstate->c_traceobj = NULL;
4538 /* Must make sure that profiling is not ignored if 'temp' is freed */
4539 tstate->use_tracing = tstate->c_profilefunc != NULL;
4540 Py_XDECREF(temp);
4541 tstate->c_tracefunc = func;
4542 tstate->c_traceobj = arg;
4543 /* Flag that tracing or profiling is turned on */
4544 tstate->use_tracing = ((func != NULL)
4545 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004546}
4547
Yury Selivanov75445082015-05-11 22:57:16 -04004548void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004549_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4550{
4551 assert(new_depth >= 0);
Victor Stinner50b48572018-11-01 01:51:40 +01004552 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004553 tstate->coroutine_origin_tracking_depth = new_depth;
4554}
4555
4556int
4557_PyEval_GetCoroutineOriginTrackingDepth(void)
4558{
Victor Stinner50b48572018-11-01 01:51:40 +01004559 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004560 return tstate->coroutine_origin_tracking_depth;
4561}
4562
4563void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004564_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004565{
Victor Stinner50b48572018-11-01 01:51:40 +01004566 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004567
Yury Selivanov75445082015-05-11 22:57:16 -04004568 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004569 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004570}
4571
4572PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004573_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004574{
Victor Stinner50b48572018-11-01 01:51:40 +01004575 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004576 return tstate->coroutine_wrapper;
4577}
4578
Yury Selivanoveb636452016-09-08 22:01:51 -07004579void
4580_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4581{
Victor Stinner50b48572018-11-01 01:51:40 +01004582 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004583
4584 Py_XINCREF(firstiter);
4585 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4586}
4587
4588PyObject *
4589_PyEval_GetAsyncGenFirstiter(void)
4590{
Victor Stinner50b48572018-11-01 01:51:40 +01004591 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004592 return tstate->async_gen_firstiter;
4593}
4594
4595void
4596_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4597{
Victor Stinner50b48572018-11-01 01:51:40 +01004598 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004599
4600 Py_XINCREF(finalizer);
4601 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4602}
4603
4604PyObject *
4605_PyEval_GetAsyncGenFinalizer(void)
4606{
Victor Stinner50b48572018-11-01 01:51:40 +01004607 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004608 return tstate->async_gen_finalizer;
4609}
4610
Guido van Rossumb209a111997-04-29 18:18:01 +00004611PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004612PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004613{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004614 PyFrameObject *current_frame = PyEval_GetFrame();
4615 if (current_frame == NULL)
Victor Stinnercaba55b2018-08-03 15:33:52 +02004616 return _PyInterpreterState_GET_UNSAFE()->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004617 else
4618 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004619}
4620
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004621/* Convenience function to get a builtin from its name */
4622PyObject *
4623_PyEval_GetBuiltinId(_Py_Identifier *name)
4624{
4625 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4626 if (attr) {
4627 Py_INCREF(attr);
4628 }
4629 else if (!PyErr_Occurred()) {
4630 PyErr_SetObject(PyExc_AttributeError, _PyUnicode_FromId(name));
4631 }
4632 return attr;
4633}
4634
Guido van Rossumb209a111997-04-29 18:18:01 +00004635PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004636PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004637{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004638 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004639 if (current_frame == NULL) {
4640 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004641 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004642 }
4643
4644 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4645 return NULL;
4646
4647 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004648 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004649}
4650
Guido van Rossumb209a111997-04-29 18:18:01 +00004651PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004652PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004653{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004654 PyFrameObject *current_frame = PyEval_GetFrame();
4655 if (current_frame == NULL)
4656 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004657
4658 assert(current_frame->f_globals != NULL);
4659 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004660}
4661
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004662PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004663PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004664{
Victor Stinner50b48572018-11-01 01:51:40 +01004665 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004666 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004667}
4668
Guido van Rossum6135a871995-01-09 17:53:26 +00004669int
Tim Peters5ba58662001-07-16 02:29:45 +00004670PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004671{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004672 PyFrameObject *current_frame = PyEval_GetFrame();
4673 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004675 if (current_frame != NULL) {
4676 const int codeflags = current_frame->f_code->co_flags;
4677 const int compilerflags = codeflags & PyCF_MASK;
4678 if (compilerflags) {
4679 result = 1;
4680 cf->cf_flags |= compilerflags;
4681 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004682#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004683 if (codeflags & CO_GENERATOR_ALLOWED) {
4684 result = 1;
4685 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4686 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004687#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004688 }
4689 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004690}
4691
Guido van Rossum3f5da241990-12-20 15:06:42 +00004692
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004693const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004694PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004695{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004696 if (PyMethod_Check(func))
4697 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4698 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004699 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004700 else if (PyCFunction_Check(func))
4701 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4702 else
4703 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004704}
4705
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004706const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004707PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004708{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004709 if (PyMethod_Check(func))
4710 return "()";
4711 else if (PyFunction_Check(func))
4712 return "()";
4713 else if (PyCFunction_Check(func))
4714 return "()";
4715 else
4716 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004717}
4718
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004719#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004720if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004721 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4722 tstate, tstate->frame, \
4723 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004724 x = NULL; \
4725 } \
4726 else { \
4727 x = call; \
4728 if (tstate->c_profilefunc != NULL) { \
4729 if (x == NULL) { \
4730 call_trace_protected(tstate->c_profilefunc, \
4731 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004732 tstate, tstate->frame, \
4733 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004734 /* XXX should pass (type, value, tb) */ \
4735 } else { \
4736 if (call_trace(tstate->c_profilefunc, \
4737 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004738 tstate, tstate->frame, \
4739 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004740 Py_DECREF(x); \
4741 x = NULL; \
4742 } \
4743 } \
4744 } \
4745 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004746} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004747 x = call; \
4748 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004749
Victor Stinner415c5102017-01-11 00:54:57 +01004750/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4751 to reduce the stack consumption. */
4752Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004753call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004754{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004755 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004756 PyObject *func = *pfunc;
4757 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004758 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4759 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004760 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004762 /* Always dispatch PyCFunction first, because these are
4763 presumed to be the most frequent callable object.
4764 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004765 if (PyCFunction_Check(func)) {
Victor Stinner50b48572018-11-01 01:51:40 +01004766 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004767 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004768 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004769 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
Victor Stinner50b48572018-11-01 01:51:40 +01004770 PyThreadState *tstate = _PyThreadState_GET();
jdemeyer56868f92018-07-21 10:30:59 +02004771 if (nargs > 0 && tstate->use_tracing) {
4772 /* We need to create a temporary bound method as argument
4773 for profiling.
4774
4775 If nargs == 0, then this cannot work because we have no
4776 "self". In any case, the call itself would raise
4777 TypeError (foo needs an argument), so we just skip
4778 profiling. */
4779 PyObject *self = stack[0];
4780 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
jdemeyer147d9552018-07-23 18:41:20 +02004781 if (func != NULL) {
4782 C_TRACE(x, _PyCFunction_FastCallKeywords(func,
4783 stack+1, nargs-1,
4784 kwnames));
4785 Py_DECREF(func);
INADA Naoki93fac8d2017-03-07 14:24:37 +09004786 }
jdemeyer147d9552018-07-23 18:41:20 +02004787 else {
4788 x = NULL;
4789 }
INADA Naoki93fac8d2017-03-07 14:24:37 +09004790 }
4791 else {
4792 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4793 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004794 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004795 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004796 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004797 /* Optimize access to bound methods. Reuse the Python stack
4798 to pass 'self' as the first argument, replace 'func'
4799 with 'self'. It avoids the creation of a new temporary tuple
4800 for arguments (to replace func with self) when the method uses
4801 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004802 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004803 Py_INCREF(self);
4804 func = PyMethod_GET_FUNCTION(func);
4805 Py_INCREF(func);
4806 Py_SETREF(*pfunc, self);
4807 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004808 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004809 }
4810 else {
4811 Py_INCREF(func);
4812 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004813
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004814 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004815 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004816 }
4817 else {
4818 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4819 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004820 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004821 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004822
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004823 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4824
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004825 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004826 while ((*pp_stack) > pfunc) {
4827 w = EXT_POP(*pp_stack);
4828 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004829 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004831 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004832}
4833
Jeremy Hylton52820442001-01-03 23:52:36 +00004834static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004835do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004836{
jdemeyere89de732018-09-19 12:06:20 +02004837 PyObject *result;
4838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004839 if (PyCFunction_Check(func)) {
Victor Stinner50b48572018-11-01 01:51:40 +01004840 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004841 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004842 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004843 }
jdemeyere89de732018-09-19 12:06:20 +02004844 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
Victor Stinner50b48572018-11-01 01:51:40 +01004845 PyThreadState *tstate = _PyThreadState_GET();
jdemeyere89de732018-09-19 12:06:20 +02004846 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4847 if (nargs > 0 && tstate->use_tracing) {
4848 /* We need to create a temporary bound method as argument
4849 for profiling.
4850
4851 If nargs == 0, then this cannot work because we have no
4852 "self". In any case, the call itself would raise
4853 TypeError (foo needs an argument), so we just skip
4854 profiling. */
4855 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4856 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4857 if (func == NULL) {
4858 return NULL;
4859 }
4860
4861 C_TRACE(result, _PyCFunction_FastCallDict(func,
Victor Stinnerd17a6932018-11-09 16:56:48 +01004862 &_PyTuple_ITEMS(callargs)[1],
jdemeyere89de732018-09-19 12:06:20 +02004863 nargs - 1,
4864 kwdict));
4865 Py_DECREF(func);
4866 return result;
4867 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004868 }
jdemeyere89de732018-09-19 12:06:20 +02004869 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004870}
4871
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004872/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004873 nb_index slot defined, and store in *pi.
4874 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004875 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004876 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004877*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004878int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004879_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004880{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004881 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004882 Py_ssize_t x;
4883 if (PyIndex_Check(v)) {
4884 x = PyNumber_AsSsize_t(v, NULL);
4885 if (x == -1 && PyErr_Occurred())
4886 return 0;
4887 }
4888 else {
4889 PyErr_SetString(PyExc_TypeError,
4890 "slice indices must be integers or "
4891 "None or have an __index__ method");
4892 return 0;
4893 }
4894 *pi = x;
4895 }
4896 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004897}
4898
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004899int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004900_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004901{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004902 Py_ssize_t x;
4903 if (PyIndex_Check(v)) {
4904 x = PyNumber_AsSsize_t(v, NULL);
4905 if (x == -1 && PyErr_Occurred())
4906 return 0;
4907 }
4908 else {
4909 PyErr_SetString(PyExc_TypeError,
4910 "slice indices must be integers or "
4911 "have an __index__ method");
4912 return 0;
4913 }
4914 *pi = x;
4915 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004916}
4917
4918
Guido van Rossum486364b2007-06-30 05:01:58 +00004919#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004920 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004921
Guido van Rossumb209a111997-04-29 18:18:01 +00004922static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004923cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004924{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004925 int res = 0;
4926 switch (op) {
4927 case PyCmp_IS:
4928 res = (v == w);
4929 break;
4930 case PyCmp_IS_NOT:
4931 res = (v != w);
4932 break;
4933 case PyCmp_IN:
4934 res = PySequence_Contains(w, v);
4935 if (res < 0)
4936 return NULL;
4937 break;
4938 case PyCmp_NOT_IN:
4939 res = PySequence_Contains(w, v);
4940 if (res < 0)
4941 return NULL;
4942 res = !res;
4943 break;
4944 case PyCmp_EXC_MATCH:
4945 if (PyTuple_Check(w)) {
4946 Py_ssize_t i, length;
4947 length = PyTuple_Size(w);
4948 for (i = 0; i < length; i += 1) {
4949 PyObject *exc = PyTuple_GET_ITEM(w, i);
4950 if (!PyExceptionClass_Check(exc)) {
4951 PyErr_SetString(PyExc_TypeError,
4952 CANNOT_CATCH_MSG);
4953 return NULL;
4954 }
4955 }
4956 }
4957 else {
4958 if (!PyExceptionClass_Check(w)) {
4959 PyErr_SetString(PyExc_TypeError,
4960 CANNOT_CATCH_MSG);
4961 return NULL;
4962 }
4963 }
4964 res = PyErr_GivenExceptionMatches(v, w);
4965 break;
4966 default:
4967 return PyObject_RichCompare(v, w, op);
4968 }
4969 v = res ? Py_True : Py_False;
4970 Py_INCREF(v);
4971 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004972}
4973
Thomas Wouters52152252000-08-17 22:55:00 +00004974static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004975import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4976{
4977 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004978 PyObject *import_func, *res;
4979 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004980
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004981 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004982 if (import_func == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004983 if (!PyErr_Occurred()) {
4984 PyErr_SetString(PyExc_ImportError, "__import__ not found");
4985 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004986 return NULL;
4987 }
4988
4989 /* Fast path for not overloaded __import__. */
Victor Stinnercaba55b2018-08-03 15:33:52 +02004990 if (import_func == _PyInterpreterState_GET_UNSAFE()->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004991 int ilevel = _PyLong_AsInt(level);
4992 if (ilevel == -1 && PyErr_Occurred()) {
4993 return NULL;
4994 }
4995 res = PyImport_ImportModuleLevelObject(
4996 name,
4997 f->f_globals,
4998 f->f_locals == NULL ? Py_None : f->f_locals,
4999 fromlist,
5000 ilevel);
5001 return res;
5002 }
5003
5004 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005005
5006 stack[0] = name;
5007 stack[1] = f->f_globals;
5008 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5009 stack[3] = fromlist;
5010 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005011 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005012 Py_DECREF(import_func);
5013 return res;
5014}
5015
5016static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00005017import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005018{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005019 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005020 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08005021 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005022
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005023 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005024 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005025 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005026 /* Issue #17636: in case this failed because of a circular relative
5027 import, try to fallback on reading the module directly from
5028 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005029 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005030 if (pkgname == NULL) {
5031 goto error;
5032 }
Oren Milman6db70332017-09-19 14:23:01 +03005033 if (!PyUnicode_Check(pkgname)) {
5034 Py_CLEAR(pkgname);
5035 goto error;
5036 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005037 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005038 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005039 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005040 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005041 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005042 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005043 Py_DECREF(fullmodname);
Stefan Krah027b09c2019-03-25 21:50:58 +01005044 if (x == NULL && !PyErr_Occurred()) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005045 goto error;
5046 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005047 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005048 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005049 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005050 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005051 if (pkgname == NULL) {
5052 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5053 if (pkgname_or_unknown == NULL) {
5054 Py_XDECREF(pkgpath);
5055 return NULL;
5056 }
5057 } else {
5058 pkgname_or_unknown = pkgname;
5059 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005060
5061 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
5062 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08005063 errmsg = PyUnicode_FromFormat(
5064 "cannot import name %R from %R (unknown location)",
5065 name, pkgname_or_unknown
5066 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005067 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005068 PyErr_SetImportError(errmsg, pkgname, NULL);
5069 }
5070 else {
5071 errmsg = PyUnicode_FromFormat(
5072 "cannot import name %R from %R (%S)",
5073 name, pkgname_or_unknown, pkgpath
5074 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005075 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005076 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005077 }
5078
Xiang Zhang4830f582017-03-21 11:13:42 +08005079 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005080 Py_XDECREF(pkgname_or_unknown);
5081 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005082 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005083}
Guido van Rossumac7be682001-01-17 15:42:30 +00005084
Thomas Wouters52152252000-08-17 22:55:00 +00005085static int
5086import_all_from(PyObject *locals, PyObject *v)
5087{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005088 _Py_IDENTIFIER(__all__);
5089 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005090 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005091 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005092 int skip_leading_underscores = 0;
5093 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005094
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005095 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5096 return -1; /* Unexpected error */
5097 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005098 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005099 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5100 return -1;
5101 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005102 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005103 PyErr_SetString(PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005104 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005105 return -1;
5106 }
5107 all = PyMapping_Keys(dict);
5108 Py_DECREF(dict);
5109 if (all == NULL)
5110 return -1;
5111 skip_leading_underscores = 1;
5112 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005114 for (pos = 0, err = 0; ; pos++) {
5115 name = PySequence_GetItem(all, pos);
5116 if (name == NULL) {
5117 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5118 err = -1;
5119 else
5120 PyErr_Clear();
5121 break;
5122 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005123 if (!PyUnicode_Check(name)) {
5124 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5125 if (modname == NULL) {
5126 Py_DECREF(name);
5127 err = -1;
5128 break;
5129 }
5130 if (!PyUnicode_Check(modname)) {
5131 PyErr_Format(PyExc_TypeError,
5132 "module __name__ must be a string, not %.100s",
5133 Py_TYPE(modname)->tp_name);
5134 }
5135 else {
5136 PyErr_Format(PyExc_TypeError,
5137 "%s in %U.%s must be str, not %.100s",
5138 skip_leading_underscores ? "Key" : "Item",
5139 modname,
5140 skip_leading_underscores ? "__dict__" : "__all__",
5141 Py_TYPE(name)->tp_name);
5142 }
5143 Py_DECREF(modname);
5144 Py_DECREF(name);
5145 err = -1;
5146 break;
5147 }
5148 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005149 if (PyUnicode_READY(name) == -1) {
5150 Py_DECREF(name);
5151 err = -1;
5152 break;
5153 }
5154 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5155 Py_DECREF(name);
5156 continue;
5157 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005158 }
5159 value = PyObject_GetAttr(v, name);
5160 if (value == NULL)
5161 err = -1;
5162 else if (PyDict_CheckExact(locals))
5163 err = PyDict_SetItem(locals, name, value);
5164 else
5165 err = PyObject_SetItem(locals, name, value);
5166 Py_DECREF(name);
5167 Py_XDECREF(value);
5168 if (err != 0)
5169 break;
5170 }
5171 Py_DECREF(all);
5172 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005173}
5174
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005175static int
5176check_args_iterable(PyObject *func, PyObject *args)
5177{
5178 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
5179 PyErr_Format(PyExc_TypeError,
5180 "%.200s%.200s argument after * "
5181 "must be an iterable, not %.200s",
5182 PyEval_GetFuncName(func),
5183 PyEval_GetFuncDesc(func),
5184 args->ob_type->tp_name);
5185 return -1;
5186 }
5187 return 0;
5188}
5189
5190static void
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005191format_kwargs_error(PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005192{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005193 /* _PyDict_MergeEx raises attribute
5194 * error (percolated from an attempt
5195 * to get 'keys' attribute) instead of
5196 * a type error if its second argument
5197 * is not a mapping.
5198 */
5199 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
5200 PyErr_Format(PyExc_TypeError,
5201 "%.200s%.200s argument after ** "
5202 "must be a mapping, not %.200s",
5203 PyEval_GetFuncName(func),
5204 PyEval_GetFuncDesc(func),
5205 kwargs->ob_type->tp_name);
5206 }
5207 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
5208 PyObject *exc, *val, *tb;
5209 PyErr_Fetch(&exc, &val, &tb);
5210 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
5211 PyObject *key = PyTuple_GET_ITEM(val, 0);
5212 if (!PyUnicode_Check(key)) {
5213 PyErr_Format(PyExc_TypeError,
5214 "%.200s%.200s keywords must be strings",
5215 PyEval_GetFuncName(func),
5216 PyEval_GetFuncDesc(func));
5217 } else {
5218 PyErr_Format(PyExc_TypeError,
5219 "%.200s%.200s got multiple "
5220 "values for keyword argument '%U'",
5221 PyEval_GetFuncName(func),
5222 PyEval_GetFuncDesc(func),
5223 key);
5224 }
5225 Py_XDECREF(exc);
5226 Py_XDECREF(val);
5227 Py_XDECREF(tb);
5228 }
5229 else {
5230 PyErr_Restore(exc, val, tb);
5231 }
5232 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005233}
5234
Guido van Rossumac7be682001-01-17 15:42:30 +00005235static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005236format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005237{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005238 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005240 if (!obj)
5241 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005242
Serhiy Storchaka06515832016-11-20 09:13:07 +02005243 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005244 if (!obj_str)
5245 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005247 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005248}
Guido van Rossum950361c1997-01-24 13:49:28 +00005249
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005250static void
5251format_exc_unbound(PyCodeObject *co, int oparg)
5252{
5253 PyObject *name;
5254 /* Don't stomp existing exception */
5255 if (PyErr_Occurred())
5256 return;
5257 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5258 name = PyTuple_GET_ITEM(co->co_cellvars,
5259 oparg);
5260 format_exc_check_arg(
5261 PyExc_UnboundLocalError,
5262 UNBOUNDLOCAL_ERROR_MSG,
5263 name);
5264 } else {
5265 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5266 PyTuple_GET_SIZE(co->co_cellvars));
5267 format_exc_check_arg(PyExc_NameError,
5268 UNBOUNDFREE_ERROR_MSG, name);
5269 }
5270}
5271
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005272static void
5273format_awaitable_error(PyTypeObject *type, int prevopcode)
5274{
5275 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5276 if (prevopcode == BEFORE_ASYNC_WITH) {
5277 PyErr_Format(PyExc_TypeError,
5278 "'async with' received an object from __aenter__ "
5279 "that does not implement __await__: %.100s",
5280 type->tp_name);
5281 }
5282 else if (prevopcode == WITH_CLEANUP_START) {
5283 PyErr_Format(PyExc_TypeError,
5284 "'async with' received an object from __aexit__ "
5285 "that does not implement __await__: %.100s",
5286 type->tp_name);
5287 }
5288 }
5289}
5290
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005291static PyObject *
5292unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005293 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005294{
5295 PyObject *res;
5296 if (Py_REFCNT(v) == 2) {
5297 /* In the common case, there are 2 references to the value
5298 * stored in 'variable' when the += is performed: one on the
5299 * value stack (in 'v') and one still stored in the
5300 * 'variable'. We try to delete the variable now to reduce
5301 * the refcnt to 1.
5302 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005303 int opcode, oparg;
5304 NEXTOPARG();
5305 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005306 case STORE_FAST:
5307 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005308 PyObject **fastlocals = f->f_localsplus;
5309 if (GETLOCAL(oparg) == v)
5310 SETLOCAL(oparg, NULL);
5311 break;
5312 }
5313 case STORE_DEREF:
5314 {
5315 PyObject **freevars = (f->f_localsplus +
5316 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005317 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005318 if (PyCell_GET(c) == v) {
5319 PyCell_SET(c, NULL);
5320 Py_DECREF(v);
5321 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005322 break;
5323 }
5324 case STORE_NAME:
5325 {
5326 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005327 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005328 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005329 if (locals && PyDict_CheckExact(locals)) {
5330 PyObject *w = PyDict_GetItemWithError(locals, name);
5331 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
5332 (w == NULL && PyErr_Occurred()))
5333 {
5334 Py_DECREF(v);
5335 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005336 }
5337 }
5338 break;
5339 }
5340 }
5341 }
5342 res = v;
5343 PyUnicode_Append(&res, w);
5344 return res;
5345}
5346
Guido van Rossum950361c1997-01-24 13:49:28 +00005347#ifdef DYNAMIC_EXECUTION_PROFILE
5348
Skip Montanarof118cb12001-10-15 20:51:38 +00005349static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005350getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005352 int i;
5353 PyObject *l = PyList_New(256);
5354 if (l == NULL) return NULL;
5355 for (i = 0; i < 256; i++) {
5356 PyObject *x = PyLong_FromLong(a[i]);
5357 if (x == NULL) {
5358 Py_DECREF(l);
5359 return NULL;
5360 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005361 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005362 }
5363 for (i = 0; i < 256; i++)
5364 a[i] = 0;
5365 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005366}
5367
5368PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005369_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005370{
5371#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005372 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005373#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005374 int i;
5375 PyObject *l = PyList_New(257);
5376 if (l == NULL) return NULL;
5377 for (i = 0; i < 257; i++) {
5378 PyObject *x = getarray(dxpairs[i]);
5379 if (x == NULL) {
5380 Py_DECREF(l);
5381 return NULL;
5382 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005383 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005384 }
5385 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005386#endif
5387}
5388
5389#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005390
5391Py_ssize_t
5392_PyEval_RequestCodeExtraIndex(freefunc free)
5393{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005394 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005395 Py_ssize_t new_index;
5396
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005397 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005398 return -1;
5399 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005400 new_index = interp->co_extra_user_count++;
5401 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005402 return new_index;
5403}
Łukasz Langaa785c872016-09-09 17:37:37 -07005404
5405static void
5406dtrace_function_entry(PyFrameObject *f)
5407{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005408 const char *filename;
5409 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005410 int lineno;
5411
5412 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5413 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5414 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5415
5416 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5417}
5418
5419static void
5420dtrace_function_return(PyFrameObject *f)
5421{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005422 const char *filename;
5423 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005424 int lineno;
5425
5426 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5427 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5428 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5429
5430 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5431}
5432
5433/* DTrace equivalent of maybe_call_line_trace. */
5434static void
5435maybe_dtrace_line(PyFrameObject *frame,
5436 int *instr_lb, int *instr_ub, int *instr_prev)
5437{
5438 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005439 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005440
5441 /* If the last instruction executed isn't in the current
5442 instruction window, reset the window.
5443 */
5444 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5445 PyAddrPair bounds;
5446 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5447 &bounds);
5448 *instr_lb = bounds.ap_lower;
5449 *instr_ub = bounds.ap_upper;
5450 }
5451 /* If the last instruction falls at the start of a line or if
5452 it represents a jump backwards, update the frame's line
5453 number and call the trace function. */
5454 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5455 frame->f_lineno = line;
5456 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5457 if (!co_filename)
5458 co_filename = "?";
5459 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5460 if (!co_name)
5461 co_name = "?";
5462 PyDTrace_LINE(co_filename, co_name, line);
5463 }
5464 *instr_prev = frame->f_lasti;
5465}