blob: ccd0427a142936a27089b692eb66ef81023cdc05 [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());
Eric Snowb75b1a352019-04-12 10:20:10 -0600200
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();
214 Py_UNREACHABLE();
215 }
216}
217
Antoine Pitrou1df15362010-09-13 14:16:46 +0000218void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000219PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000220{
Victor Stinner50b48572018-11-01 01:51:40 +0100221 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 if (tstate == NULL)
223 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
224 take_gil(tstate);
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400225 exit_thread_if_finalizing(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000226}
227
228void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000229PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000230{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100232 We therefore avoid PyThreadState_Get() which dumps a fatal error
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 in debug mode.
234 */
Victor Stinner50b48572018-11-01 01:51:40 +0100235 drop_gil(_PyThreadState_GET());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000236}
237
238void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000239PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000240{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 if (tstate == NULL)
242 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
243 /* Check someone has called PyEval_InitThreads() to create the lock */
244 assert(gil_created());
245 take_gil(tstate);
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400246 exit_thread_if_finalizing(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 if (PyThreadState_Swap(tstate) != NULL)
248 Py_FatalError(
249 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000250}
251
252void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000253PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 if (tstate == NULL)
256 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
257 if (PyThreadState_Swap(NULL) != tstate)
258 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
259 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000260}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000261
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200262/* This function is called from PyOS_AfterFork_Child to destroy all threads
263 * which are not running in the child process, and clear internal locks
264 * which might be held by those threads.
265 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000266
267void
268PyEval_ReInitThreads(void)
269{
Victor Stinner50b48572018-11-01 01:51:40 +0100270 PyThreadState *current_tstate = _PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 if (!gil_created())
273 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 recreate_gil();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200275 take_gil(current_tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700276
Eric Snowb75b1a352019-04-12 10:20:10 -0600277 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
278 if (_PyRuntime.ceval.pending.lock == NULL) {
Eric Snow8479a342019-03-08 23:44:33 -0700279 Py_FatalError("Can't initialize threads for pending calls");
280 }
Jesse Nollera8513972008-07-17 16:49:17 +0000281
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200282 /* Destroy all threads except the current one */
283 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000284}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000285
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000286/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600287 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000288
289void
Eric Snowb75b1a352019-04-12 10:20:10 -0600290_PyEval_SignalAsyncExc(void)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000291{
Eric Snowb75b1a352019-04-12 10:20:10 -0600292 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000293}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000294
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000295PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000296PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 PyThreadState *tstate = PyThreadState_Swap(NULL);
299 if (tstate == NULL)
300 Py_FatalError("PyEval_SaveThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100301 assert(gil_created());
302 drop_gil(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000304}
305
306void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000307PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000308{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 if (tstate == NULL)
310 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100311 assert(gil_created());
312
313 int err = errno;
314 take_gil(tstate);
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400315 exit_thread_if_finalizing(tstate);
Victor Stinner2914bb32018-01-29 11:57:45 +0100316 errno = err;
317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000319}
320
321
Guido van Rossuma9672091994-09-14 13:31:22 +0000322/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
323 signal handlers or Mac I/O completion routines) can schedule calls
324 to a function to be called synchronously.
325 The synchronous function is called with one void* argument.
326 It should return 0 for success or -1 for failure -- failure should
327 be accompanied by an exception.
328
329 If registry succeeds, the registry function returns 0; if it fails
330 (e.g. due to too many pending calls) it returns -1 (without setting
331 an exception condition).
332
333 Note that because registry may occur from within signal handlers,
334 or other asynchronous events, calling malloc() is unsafe!
335
Guido van Rossuma9672091994-09-14 13:31:22 +0000336 Any thread can schedule pending calls, but only the main thread
337 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000338 There is no facility to schedule calls to a particular thread, but
339 that should be easy to change, should that ever be required. In
340 that case, the static variables here should go into the python
341 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000342*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000343
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200344void
345_PyEval_SignalReceived(void)
346{
347 /* bpo-30703: Function called when the C signal handler of Python gets a
348 signal. We cannot queue a callback using Py_AddPendingCall() since
349 that function is not async-signal-safe. */
Eric Snowfdf282d2019-01-11 14:26:55 -0700350 SIGNAL_PENDING_SIGNALS();
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200351}
352
Eric Snow5be45a62019-03-08 22:47:07 -0700353/* Push one item onto the queue while holding the lock. */
354static int
Eric Snowb75b1a352019-04-12 10:20:10 -0600355_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600356 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700357{
Eric Snow842a2f02019-03-15 15:47:51 -0600358 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700359 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600360 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700361 return -1; /* Queue full */
362 }
Eric Snow842a2f02019-03-15 15:47:51 -0600363 pending->calls[i].func = func;
364 pending->calls[i].arg = arg;
365 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700366 return 0;
367}
368
369/* Pop one item off the queue while holding the lock. */
370static void
Eric Snowb75b1a352019-04-12 10:20:10 -0600371_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600372 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700373{
Eric Snow842a2f02019-03-15 15:47:51 -0600374 int i = pending->first;
375 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700376 return; /* Queue empty */
377 }
378
Eric Snow842a2f02019-03-15 15:47:51 -0600379 *func = pending->calls[i].func;
380 *arg = pending->calls[i].arg;
381 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700382}
383
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200384/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000385 scheduling to be made from any thread, and even from an executing
386 callback.
387 */
388
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000389int
Eric Snowb75b1a352019-04-12 10:20:10 -0600390Py_AddPendingCall(int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000391{
Eric Snowb75b1a352019-04-12 10:20:10 -0600392 struct _pending_calls *pending = &_PyRuntime.ceval.pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600393
394 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
395 if (pending->finishing) {
396 PyThread_release_lock(pending->lock);
397
398 PyObject *exc, *val, *tb;
399 PyErr_Fetch(&exc, &val, &tb);
400 PyErr_SetString(PyExc_SystemError,
401 "Py_AddPendingCall: cannot add pending calls "
402 "(Python shutting down)");
403 PyErr_Print();
404 PyErr_Restore(exc, val, tb);
405 return -1;
406 }
Eric Snowb75b1a352019-04-12 10:20:10 -0600407 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600408 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700409
Eric Snowb75b1a352019-04-12 10:20:10 -0600410 /* signal main loop */
411 SIGNAL_PENDING_CALLS();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000413}
414
Eric Snowfdf282d2019-01-11 14:26:55 -0700415static int
416handle_signals(void)
417{
Eric Snow5be45a62019-03-08 22:47:07 -0700418 /* Only handle signals on main thread. PyEval_InitThreads must
419 * have been called already.
420 */
421 if (PyThread_get_thread_ident() != _PyRuntime.main_thread) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700422 return 0;
423 }
Eric Snow64d6cc82019-02-23 15:40:43 -0700424 /*
425 * Ensure that the thread isn't currently running some other
426 * interpreter.
427 */
428 if (_PyInterpreterState_GET_UNSAFE() != _PyRuntime.interpreters.main) {
429 return 0;
430 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700431
432 UNSIGNAL_PENDING_SIGNALS();
Eric Snow64d6cc82019-02-23 15:40:43 -0700433 if (_PyErr_CheckSignals() < 0) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700434 SIGNAL_PENDING_SIGNALS(); /* We're not done yet */
435 return -1;
436 }
437 return 0;
438}
439
440static int
Eric Snowb75b1a352019-04-12 10:20:10 -0600441make_pending_calls(struct _pending_calls* pending)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000442{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200443 static int busy = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000444
Eric Snowb75b1a352019-04-12 10:20:10 -0600445 /* only service pending calls on main thread */
446 if (PyThread_get_thread_ident() != _PyRuntime.main_thread) {
447 return 0;
448 }
449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 /* don't perform recursive pending calls */
Eric Snowfdf282d2019-01-11 14:26:55 -0700451 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700453 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200454 busy = 1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200455 /* unsignal before starting to call callbacks, so that any callback
456 added in-between re-signals */
Eric Snowb75b1a352019-04-12 10:20:10 -0600457 UNSIGNAL_PENDING_CALLS();
Eric Snowfdf282d2019-01-11 14:26:55 -0700458 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 /* perform a bounded number of calls, in case of recursion */
Eric Snowfdf282d2019-01-11 14:26:55 -0700461 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700462 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 void *arg = NULL;
464
465 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600466 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Eric Snowb75b1a352019-04-12 10:20:10 -0600467 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600468 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700469
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100470 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700471 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100472 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700473 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700474 res = func(arg);
475 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200476 goto error;
477 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200479
Charles-François Natalif23339a2011-07-23 18:15:43 +0200480 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700481 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200482
483error:
484 busy = 0;
Eric Snowb75b1a352019-04-12 10:20:10 -0600485 SIGNAL_PENDING_CALLS();
Eric Snowfdf282d2019-01-11 14:26:55 -0700486 return res;
487}
488
Eric Snow842a2f02019-03-15 15:47:51 -0600489void
Eric Snowb75b1a352019-04-12 10:20:10 -0600490_Py_FinishPendingCalls(void)
Eric Snow842a2f02019-03-15 15:47:51 -0600491{
Eric Snowb75b1a352019-04-12 10:20:10 -0600492 struct _pending_calls *pending = &_PyRuntime.ceval.pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600493
494 assert(PyGILState_Check());
495
496 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
497 pending->finishing = 1;
498 PyThread_release_lock(pending->lock);
499
500 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
501 return;
502 }
503
Eric Snowb75b1a352019-04-12 10:20:10 -0600504 if (make_pending_calls(pending) < 0) {
Eric Snow842a2f02019-03-15 15:47:51 -0600505 PyObject *exc, *val, *tb;
506 PyErr_Fetch(&exc, &val, &tb);
507 PyErr_BadInternalCall();
508 _PyErr_ChainExceptions(exc, val, tb);
509 PyErr_Print();
510 }
511}
512
Eric Snowfdf282d2019-01-11 14:26:55 -0700513/* Py_MakePendingCalls() is a simple wrapper for the sake
514 of backward-compatibility. */
515int
516Py_MakePendingCalls(void)
517{
518 assert(PyGILState_Check());
519
520 /* Python signal handler doesn't really queue a callback: it only signals
521 that a signal was received, see _PyEval_SignalReceived(). */
522 int res = handle_signals();
523 if (res != 0) {
524 return res;
525 }
526
Eric Snowb75b1a352019-04-12 10:20:10 -0600527 res = make_pending_calls(&_PyRuntime.ceval.pending);
528 if (res != 0) {
529 return res;
530 }
531
532 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000533}
534
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000535/* The interpreter's recursion limit */
536
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000537#ifndef Py_DEFAULT_RECURSION_LIMIT
538#define Py_DEFAULT_RECURSION_LIMIT 1000
539#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600540
Eric Snow05351c12017-09-05 21:43:08 -0700541int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000542
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600543void
544_PyEval_Initialize(struct _ceval_runtime_state *state)
545{
546 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
547 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
548 _gil_initialize(&state->gil);
549}
550
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000551int
552Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000553{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600554 return _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000555}
556
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000557void
558Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000559{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600560 _PyRuntime.ceval.recursion_limit = new_limit;
561 _Py_CheckRecursionLimit = _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000562}
563
Armin Rigo2b3eb402003-10-28 12:05:48 +0000564/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
565 if the recursion_depth reaches _Py_CheckRecursionLimit.
566 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
567 to guarantee that _Py_CheckRecursiveCall() is regularly called.
568 Without USE_STACKCHECK, there is no need for this. */
569int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300570_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000571{
Victor Stinner50b48572018-11-01 01:51:40 +0100572 PyThreadState *tstate = _PyThreadState_GET();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600573 int recursion_limit = _PyRuntime.ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000574
575#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700576 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 if (PyOS_CheckStack()) {
578 --tstate->recursion_depth;
579 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
580 return -1;
581 }
pdox18967932017-10-25 23:03:01 -0700582 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700583 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700584#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 if (tstate->recursion_critical)
586 /* Somebody asked that we don't check for recursion. */
587 return 0;
588 if (tstate->overflowed) {
589 if (tstate->recursion_depth > recursion_limit + 50) {
590 /* Overflowing while handling an overflow. Give up. */
591 Py_FatalError("Cannot recover from stack overflow.");
592 }
593 return 0;
594 }
595 if (tstate->recursion_depth > recursion_limit) {
596 --tstate->recursion_depth;
597 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400598 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 "maximum recursion depth exceeded%s",
600 where);
601 return -1;
602 }
603 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000604}
605
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400606static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000607static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000608
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600609#define _Py_TracingPossible _PyRuntime.ceval.tracing_possible
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000610
Guido van Rossum374a9221991-04-04 10:40:29 +0000611
Guido van Rossumb209a111997-04-29 18:18:01 +0000612PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000613PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000614{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 return PyEval_EvalCodeEx(co,
616 globals, locals,
617 (PyObject **)NULL, 0,
618 (PyObject **)NULL, 0,
619 (PyObject **)NULL, 0,
620 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000621}
622
623
624/* Interpreter main loop */
625
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000626PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000627PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 /* This is for backward compatibility with extension modules that
629 used this API; core interpreter code should call
630 PyEval_EvalFrameEx() */
631 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000632}
633
634PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000635PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000636{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200637 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
638 return interp->eval_frame(f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700639}
640
Victor Stinnerc6944e72016-11-11 02:13:35 +0100641PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700642_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
643{
Guido van Rossum950361c1997-01-24 13:49:28 +0000644#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000646#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200647 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300648 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200649 int opcode; /* Current opcode */
650 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200651 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 PyObject *retval = NULL; /* Return value */
Victor Stinner50b48572018-11-01 01:51:40 +0100653 PyThreadState *tstate = _PyThreadState_GET();
Eric Snowb75b1a352019-04-12 10:20:10 -0600654 _Py_atomic_int *eval_breaker = &_PyRuntime.ceval.eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 is true when the line being executed has changed. The
662 initial values are such as to make this false the first
663 time it is tested. */
664 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000665
Serhiy Storchakaab874002016-09-11 13:48:15 +0300666 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 PyObject *names;
668 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000669
Brett Cannon368b4b72012-04-02 12:17:59 -0400670#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200671 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400672#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200673
Antoine Pitroub52ec782009-01-25 16:34:23 +0000674/* Computed GOTOs, or
675 the-optimization-commonly-but-improperly-known-as-"threaded code"
676 using gcc's labels-as-values extension
677 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
678
679 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000681 combined with a lookup table of jump addresses. However, since the
682 indirect jump instruction is shared by all opcodes, the CPU will have a
683 hard time making the right prediction for where to jump next (actually,
684 it will be always wrong except in the uncommon case of a sequence of
685 several identical opcodes).
686
687 "Threaded code" in contrast, uses an explicit jump table and an explicit
688 indirect jump instruction at the end of each opcode. Since the jump
689 instruction is at a different address for each opcode, the CPU will make a
690 separate prediction for each of these instructions, which is equivalent to
691 predicting the second opcode of each opcode pair. These predictions have
692 a much better chance to turn out valid, especially in small bytecode loops.
693
694 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000696 and potentially many more instructions (depending on the pipeline width).
697 A correctly predicted branch, however, is nearly free.
698
699 At the time of this writing, the "threaded code" version is up to 15-20%
700 faster than the normal "switch" version, depending on the compiler and the
701 CPU architecture.
702
703 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
704 because it would render the measurements invalid.
705
706
707 NOTE: care must be taken that the compiler doesn't try to "optimize" the
708 indirect jumps by sharing them between all opcodes. Such optimizations
709 can be disabled on gcc by using the -fno-gcse flag (or possibly
710 -fno-crossjumping).
711*/
712
Antoine Pitrou042b1282010-08-13 21:15:58 +0000713#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000714#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000715#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000716#endif
717
Antoine Pitrou042b1282010-08-13 21:15:58 +0000718#ifdef HAVE_COMPUTED_GOTOS
719 #ifndef USE_COMPUTED_GOTOS
720 #define USE_COMPUTED_GOTOS 1
721 #endif
722#else
723 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
724 #error "Computed gotos are not supported on this compiler."
725 #endif
726 #undef USE_COMPUTED_GOTOS
727 #define USE_COMPUTED_GOTOS 0
728#endif
729
730#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000731/* Import the static jump table */
732#include "opcode_targets.h"
733
Antoine Pitroub52ec782009-01-25 16:34:23 +0000734#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700735 op: \
736 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000737
Antoine Pitroub52ec782009-01-25 16:34:23 +0000738#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 { \
Eric Snow7bda9de2019-03-08 17:25:54 -0700740 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 FAST_DISPATCH(); \
742 } \
743 continue; \
744 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000745
746#ifdef LLTRACE
747#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700749 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300751 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300752 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 } \
754 goto fast_next_opcode; \
755 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000756#else
757#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700759 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300761 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300762 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 } \
764 goto fast_next_opcode; \
765 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000766#endif
767
768#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700769#define TARGET(op) op
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300770
Antoine Pitroub52ec782009-01-25 16:34:23 +0000771#define DISPATCH() continue
772#define FAST_DISPATCH() goto fast_next_opcode
773#endif
774
775
Neal Norwitza81d2202002-07-14 00:27:26 +0000776/* Tuple access macros */
777
778#ifndef Py_DEBUG
779#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
780#else
781#define GETITEM(v, i) PyTuple_GetItem((v), (i))
782#endif
783
Guido van Rossum374a9221991-04-04 10:40:29 +0000784/* Code access macros */
785
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300786/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600787#define INSTR_OFFSET() \
788 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300789#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300790 _Py_CODEUNIT word = *next_instr; \
791 opcode = _Py_OPCODE(word); \
792 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300793 next_instr++; \
794 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300795#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
796#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000797
Raymond Hettingerf606f872003-03-16 03:11:04 +0000798/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 Some opcodes tend to come in pairs thus making it possible to
800 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300801 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 Verifying the prediction costs a single high-speed test of a register
804 variable against a constant. If the pairing was good, then the
805 processor's own internal branch predication has a high likelihood of
806 success, resulting in a nearly zero-overhead transition to the
807 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300808 including its unpredictable switch-case branch. Combined with the
809 processor's internal branch prediction, a successful PREDICT has the
810 effect of making the two opcodes run as if they were a single new opcode
811 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000812
Georg Brandl86b2fb92008-07-16 03:43:04 +0000813 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 predictions turned-on and interpret the results as if some opcodes
815 had been combined or turn-off predictions so that the opcode frequency
816 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000817
818 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 the CPU to record separate branch prediction information for each
820 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000821
Raymond Hettingerf606f872003-03-16 03:11:04 +0000822*/
823
Antoine Pitrou042b1282010-08-13 21:15:58 +0000824#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000826#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300827#define PREDICT(op) \
828 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300829 _Py_CODEUNIT word = *next_instr; \
830 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300831 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300832 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300833 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300834 goto PRED_##op; \
835 } \
836 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000837#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300838#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000839
Raymond Hettingerf606f872003-03-16 03:11:04 +0000840
Guido van Rossum374a9221991-04-04 10:40:29 +0000841/* Stack manipulation macros */
842
Martin v. Löwis18e16552006-02-15 17:27:45 +0000843/* The stack can grow at most MAXINT deep, as co_nlocals and
844 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000845#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
846#define EMPTY() (STACK_LEVEL() == 0)
847#define TOP() (stack_pointer[-1])
848#define SECOND() (stack_pointer[-2])
849#define THIRD() (stack_pointer[-3])
850#define FOURTH() (stack_pointer[-4])
851#define PEEK(n) (stack_pointer[-(n)])
852#define SET_TOP(v) (stack_pointer[-1] = (v))
853#define SET_SECOND(v) (stack_pointer[-2] = (v))
854#define SET_THIRD(v) (stack_pointer[-3] = (v))
855#define SET_FOURTH(v) (stack_pointer[-4] = (v))
856#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
857#define BASIC_STACKADJ(n) (stack_pointer += n)
858#define BASIC_PUSH(v) (*stack_pointer++ = (v))
859#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000860
Guido van Rossum96a42c81992-01-12 02:29:51 +0000861#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000863 lltrace && prtrace(TOP(), "push")); \
864 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000866 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +0000867#define STACK_GROW(n) do { \
868 assert(n >= 0); \
869 (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000870 lltrace && prtrace(TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000871 assert(STACK_LEVEL() <= co->co_stacksize); \
872 } while (0)
873#define STACK_SHRINK(n) do { \
874 assert(n >= 0); \
875 (void)(lltrace && prtrace(TOP(), "stackadj")); \
876 (void)(BASIC_STACKADJ(-n)); \
877 assert(STACK_LEVEL() <= co->co_stacksize); \
878 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +0000879#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000880 prtrace((STACK_POINTER)[-1], "ext_pop")), \
881 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000882#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000883#define PUSH(v) BASIC_PUSH(v)
884#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +0000885#define STACK_GROW(n) BASIC_STACKADJ(n)
886#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000887#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000888#endif
889
Guido van Rossum681d79a1995-07-18 14:51:37 +0000890/* Local variable macros */
891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000893
894/* The SETLOCAL() macro must not DECREF the local variable in-place and
895 then store the new value; it must copy the old value to a temporary
896 value, then store the new value, and then DECREF the temporary value.
897 This is because it is possible that during the DECREF the frame is
898 accessed by other code (e.g. a __del__ method or gc.collect()) and the
899 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000901 GETLOCAL(i) = value; \
902 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000903
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000904
905#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 while (STACK_LEVEL() > (b)->b_level) { \
907 PyObject *v = POP(); \
908 Py_XDECREF(v); \
909 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000910
911#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300912 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +0100914 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 assert(STACK_LEVEL() >= (b)->b_level + 3); \
916 while (STACK_LEVEL() > (b)->b_level + 3) { \
917 value = POP(); \
918 Py_XDECREF(value); \
919 } \
Mark Shannonae3087c2017-10-22 22:41:51 +0100920 exc_info = tstate->exc_info; \
921 type = exc_info->exc_type; \
922 value = exc_info->exc_value; \
923 traceback = exc_info->exc_traceback; \
924 exc_info->exc_type = POP(); \
925 exc_info->exc_value = POP(); \
926 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 Py_XDECREF(type); \
928 Py_XDECREF(value); \
929 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300930 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000931
Guido van Rossuma027efa1997-05-05 20:56:21 +0000932/* Start of code */
933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 /* push frame */
935 if (Py_EnterRecursiveCall(""))
936 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 if (tstate->use_tracing) {
941 if (tstate->c_tracefunc != NULL) {
942 /* tstate->c_tracefunc, if defined, is a
943 function that will be called on *every* entry
944 to a code block. Its return value, if not
945 None, is a function that will be called at
946 the start of each executed line of code.
947 (Actually, the function must return itself
948 in order to continue tracing.) The trace
949 functions are called with three arguments:
950 a pointer to the current frame, a string
951 indicating why the function is called, and
952 an argument which depends on the situation.
953 The global trace function is also called
954 whenever an exception is detected. */
955 if (call_trace_protected(tstate->c_tracefunc,
956 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100957 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 /* Trace function raised an error */
959 goto exit_eval_frame;
960 }
961 }
962 if (tstate->c_profilefunc != NULL) {
963 /* Similar for c_profilefunc, except it needn't
964 return itself and isn't called for "line" events */
965 if (call_trace_protected(tstate->c_profilefunc,
966 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100967 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 /* Profile function raised an error */
969 goto exit_eval_frame;
970 }
971 }
972 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000973
Łukasz Langaa785c872016-09-09 17:37:37 -0700974 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
975 dtrace_function_entry(f);
976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 co = f->f_code;
978 names = co->co_names;
979 consts = co->co_consts;
980 fastlocals = f->f_localsplus;
981 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300982 assert(PyBytes_Check(co->co_code));
983 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +0300984 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
985 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
986 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300987 /*
988 f->f_lasti refers to the index of the last instruction,
989 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000990
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300991 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500992 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 When the PREDICT() macros are enabled, some opcode pairs follow in
995 direct succession without updating f->f_lasti. A successful
996 prediction effectively links the two codes together as if they
997 were a single new opcode; accordingly,f->f_lasti will point to
998 the first code in the pair (for instance, GET_ITER followed by
999 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001000 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001002 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001003 next_instr = first_instr;
1004 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001005 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1006 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001007 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 stack_pointer = f->f_stacktop;
1009 assert(stack_pointer != NULL);
1010 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001011 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001012
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001013
Tim Peters5ca576e2001-06-18 22:08:13 +00001014#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001015 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001016#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001017
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001018 if (throwflag) /* support for generator.throw() */
1019 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001020
Victor Stinnerace47d72013-07-18 01:41:08 +02001021#ifdef Py_DEBUG
1022 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001023 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001024 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +02001025 assert(!PyErr_Occurred());
1026#endif
1027
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001028main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1031 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +02001032 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 /* Do periodic things. Doing this every time through
1035 the loop would add too much overhead, so we do it
1036 only every Nth instruction. We also do it if
1037 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1038 event needs attention (e.g. a signal handler or
1039 async I/O handler); see Py_AddPendingCall() and
1040 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001041
Eric Snow7bda9de2019-03-08 17:25:54 -07001042 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001043 opcode = _Py_OPCODE(*next_instr);
1044 if (opcode == SETUP_FINALLY ||
1045 opcode == SETUP_WITH ||
1046 opcode == BEFORE_ASYNC_WITH ||
1047 opcode == YIELD_FROM) {
1048 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001049 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001050 - If we're about to enter the 'with:'. It will prevent
1051 emitting a resource warning in the common idiom
1052 'with open(path) as file:'.
1053 - If we're about to enter the 'async with:'.
1054 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001055 *very* useful, but might help in some cases and it's
1056 traditional)
1057 - If we're resuming a chain of nested 'yield from' or
1058 'await' calls, then each frame is parked with YIELD_FROM
1059 as its next opcode. If the user hit control-C we want to
1060 wait until we've reached the innermost frame before
1061 running the signal handler and raising KeyboardInterrupt
1062 (see bpo-30039).
1063 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 goto fast_next_opcode;
1065 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001066
1067 if (_Py_atomic_load_relaxed(
1068 &_PyRuntime.ceval.signals_pending))
1069 {
1070 if (handle_signals() != 0) {
1071 goto error;
1072 }
1073 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001074 if (_Py_atomic_load_relaxed(
Eric Snowb75b1a352019-04-12 10:20:10 -06001075 &_PyRuntime.ceval.pending.calls_to_do))
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001076 {
Eric Snowb75b1a352019-04-12 10:20:10 -06001077 if (make_pending_calls(&_PyRuntime.ceval.pending) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001078 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001079 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001081
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001082 if (_Py_atomic_load_relaxed(
1083 &_PyRuntime.ceval.gil_drop_request))
1084 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 /* Give another thread a chance */
1086 if (PyThreadState_Swap(NULL) != tstate)
1087 Py_FatalError("ceval: tstate mix-up");
1088 drop_gil(tstate);
1089
1090 /* Other threads may run now */
1091
1092 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001093
1094 /* Check if we should make a quick exit. */
Joannah Nanjekyef781d202019-04-29 04:38:45 -04001095 exit_thread_if_finalizing(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 if (PyThreadState_Swap(tstate) != NULL)
1098 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 }
1100 /* Check for asynchronous exceptions. */
1101 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001102 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 tstate->async_exc = NULL;
Eric Snowb75b1a352019-04-12 10:20:10 -06001104 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001105 PyErr_SetNone(exc);
1106 Py_DECREF(exc);
1107 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 }
1109 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 fast_next_opcode:
1112 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001113
Łukasz Langaa785c872016-09-09 17:37:37 -07001114 if (PyDTrace_LINE_ENABLED())
1115 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001118
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001120 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001121 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 /* see maybe_call_line_trace
1123 for expository comments */
1124 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 err = maybe_call_line_trace(tstate->c_tracefunc,
1127 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001128 tstate, f,
1129 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 /* Reload possibly changed frame fields */
1131 JUMPTO(f->f_lasti);
1132 if (f->f_stacktop != NULL) {
1133 stack_pointer = f->f_stacktop;
1134 f->f_stacktop = NULL;
1135 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001136 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001138 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001142
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001143 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001144 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001145#ifdef DYNAMIC_EXECUTION_PROFILE
1146#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 dxpairs[lastopcode][opcode]++;
1148 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001149#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001151#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001152
Guido van Rossum96a42c81992-01-12 02:29:51 +00001153#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 if (lltrace) {
1157 if (HAS_ARG(opcode)) {
1158 printf("%d: %d, %d\n",
1159 f->f_lasti, opcode, oparg);
1160 }
1161 else {
1162 printf("%d: %d\n",
1163 f->f_lasti, opcode);
1164 }
1165 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001166#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001171 It is essential that any operation that fails must goto error
1172 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001173
Benjamin Petersonddd19492018-09-16 22:38:02 -07001174 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001176 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001177
Benjamin Petersonddd19492018-09-16 22:38:02 -07001178 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001179 PyObject *value = GETLOCAL(oparg);
1180 if (value == NULL) {
1181 format_exc_check_arg(PyExc_UnboundLocalError,
1182 UNBOUNDLOCAL_ERROR_MSG,
1183 PyTuple_GetItem(co->co_varnames, oparg));
1184 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001186 Py_INCREF(value);
1187 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001189 }
1190
Benjamin Petersonddd19492018-09-16 22:38:02 -07001191 case TARGET(LOAD_CONST): {
1192 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001193 PyObject *value = GETITEM(consts, oparg);
1194 Py_INCREF(value);
1195 PUSH(value);
1196 FAST_DISPATCH();
1197 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001198
Benjamin Petersonddd19492018-09-16 22:38:02 -07001199 case TARGET(STORE_FAST): {
1200 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001201 PyObject *value = POP();
1202 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001204 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001205
Benjamin Petersonddd19492018-09-16 22:38:02 -07001206 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001207 PyObject *value = POP();
1208 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001210 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001211
Benjamin Petersonddd19492018-09-16 22:38:02 -07001212 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001213 PyObject *top = TOP();
1214 PyObject *second = SECOND();
1215 SET_TOP(second);
1216 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001218 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001219
Benjamin Petersonddd19492018-09-16 22:38:02 -07001220 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001221 PyObject *top = TOP();
1222 PyObject *second = SECOND();
1223 PyObject *third = THIRD();
1224 SET_TOP(second);
1225 SET_SECOND(third);
1226 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001228 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001229
Benjamin Petersonddd19492018-09-16 22:38:02 -07001230 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001231 PyObject *top = TOP();
1232 PyObject *second = SECOND();
1233 PyObject *third = THIRD();
1234 PyObject *fourth = FOURTH();
1235 SET_TOP(second);
1236 SET_SECOND(third);
1237 SET_THIRD(fourth);
1238 SET_FOURTH(top);
1239 FAST_DISPATCH();
1240 }
1241
Benjamin Petersonddd19492018-09-16 22:38:02 -07001242 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001243 PyObject *top = TOP();
1244 Py_INCREF(top);
1245 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001247 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001248
Benjamin Petersonddd19492018-09-16 22:38:02 -07001249 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001250 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001251 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001252 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001253 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001254 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001255 SET_TOP(top);
1256 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001257 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001258 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001259
Benjamin Petersonddd19492018-09-16 22:38:02 -07001260 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001261 PyObject *value = TOP();
1262 PyObject *res = PyNumber_Positive(value);
1263 Py_DECREF(value);
1264 SET_TOP(res);
1265 if (res == NULL)
1266 goto error;
1267 DISPATCH();
1268 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001269
Benjamin Petersonddd19492018-09-16 22:38:02 -07001270 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001271 PyObject *value = TOP();
1272 PyObject *res = PyNumber_Negative(value);
1273 Py_DECREF(value);
1274 SET_TOP(res);
1275 if (res == NULL)
1276 goto error;
1277 DISPATCH();
1278 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001279
Benjamin Petersonddd19492018-09-16 22:38:02 -07001280 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001281 PyObject *value = TOP();
1282 int err = PyObject_IsTrue(value);
1283 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 if (err == 0) {
1285 Py_INCREF(Py_True);
1286 SET_TOP(Py_True);
1287 DISPATCH();
1288 }
1289 else if (err > 0) {
1290 Py_INCREF(Py_False);
1291 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 DISPATCH();
1293 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001294 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001295 goto error;
1296 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001297
Benjamin Petersonddd19492018-09-16 22:38:02 -07001298 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001299 PyObject *value = TOP();
1300 PyObject *res = PyNumber_Invert(value);
1301 Py_DECREF(value);
1302 SET_TOP(res);
1303 if (res == NULL)
1304 goto error;
1305 DISPATCH();
1306 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001307
Benjamin Petersonddd19492018-09-16 22:38:02 -07001308 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001309 PyObject *exp = POP();
1310 PyObject *base = TOP();
1311 PyObject *res = PyNumber_Power(base, exp, Py_None);
1312 Py_DECREF(base);
1313 Py_DECREF(exp);
1314 SET_TOP(res);
1315 if (res == NULL)
1316 goto error;
1317 DISPATCH();
1318 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001319
Benjamin Petersonddd19492018-09-16 22:38:02 -07001320 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001321 PyObject *right = POP();
1322 PyObject *left = TOP();
1323 PyObject *res = PyNumber_Multiply(left, right);
1324 Py_DECREF(left);
1325 Py_DECREF(right);
1326 SET_TOP(res);
1327 if (res == NULL)
1328 goto error;
1329 DISPATCH();
1330 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001331
Benjamin Petersonddd19492018-09-16 22:38:02 -07001332 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001333 PyObject *right = POP();
1334 PyObject *left = TOP();
1335 PyObject *res = PyNumber_MatrixMultiply(left, right);
1336 Py_DECREF(left);
1337 Py_DECREF(right);
1338 SET_TOP(res);
1339 if (res == NULL)
1340 goto error;
1341 DISPATCH();
1342 }
1343
Benjamin Petersonddd19492018-09-16 22:38:02 -07001344 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001345 PyObject *divisor = POP();
1346 PyObject *dividend = TOP();
1347 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1348 Py_DECREF(dividend);
1349 Py_DECREF(divisor);
1350 SET_TOP(quotient);
1351 if (quotient == NULL)
1352 goto error;
1353 DISPATCH();
1354 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001355
Benjamin Petersonddd19492018-09-16 22:38:02 -07001356 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001357 PyObject *divisor = POP();
1358 PyObject *dividend = TOP();
1359 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1360 Py_DECREF(dividend);
1361 Py_DECREF(divisor);
1362 SET_TOP(quotient);
1363 if (quotient == NULL)
1364 goto error;
1365 DISPATCH();
1366 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001367
Benjamin Petersonddd19492018-09-16 22:38:02 -07001368 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001369 PyObject *divisor = POP();
1370 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001371 PyObject *res;
1372 if (PyUnicode_CheckExact(dividend) && (
1373 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1374 // fast path; string formatting, but not if the RHS is a str subclass
1375 // (see issue28598)
1376 res = PyUnicode_Format(dividend, divisor);
1377 } else {
1378 res = PyNumber_Remainder(dividend, divisor);
1379 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001380 Py_DECREF(divisor);
1381 Py_DECREF(dividend);
1382 SET_TOP(res);
1383 if (res == NULL)
1384 goto error;
1385 DISPATCH();
1386 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001387
Benjamin Petersonddd19492018-09-16 22:38:02 -07001388 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001389 PyObject *right = POP();
1390 PyObject *left = TOP();
1391 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001392 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1393 CPython using bytecode, it is simply worthless.
1394 See http://bugs.python.org/issue21955 and
1395 http://bugs.python.org/issue10044 for the discussion. In short,
1396 no patch shown any impact on a realistic benchmark, only a minor
1397 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001398 if (PyUnicode_CheckExact(left) &&
1399 PyUnicode_CheckExact(right)) {
1400 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001401 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001402 }
1403 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001404 sum = PyNumber_Add(left, right);
1405 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001406 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001407 Py_DECREF(right);
1408 SET_TOP(sum);
1409 if (sum == NULL)
1410 goto error;
1411 DISPATCH();
1412 }
1413
Benjamin Petersonddd19492018-09-16 22:38:02 -07001414 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001415 PyObject *right = POP();
1416 PyObject *left = TOP();
1417 PyObject *diff = PyNumber_Subtract(left, right);
1418 Py_DECREF(right);
1419 Py_DECREF(left);
1420 SET_TOP(diff);
1421 if (diff == NULL)
1422 goto error;
1423 DISPATCH();
1424 }
1425
Benjamin Petersonddd19492018-09-16 22:38:02 -07001426 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001427 PyObject *sub = POP();
1428 PyObject *container = TOP();
1429 PyObject *res = PyObject_GetItem(container, sub);
1430 Py_DECREF(container);
1431 Py_DECREF(sub);
1432 SET_TOP(res);
1433 if (res == NULL)
1434 goto error;
1435 DISPATCH();
1436 }
1437
Benjamin Petersonddd19492018-09-16 22:38:02 -07001438 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001439 PyObject *right = POP();
1440 PyObject *left = TOP();
1441 PyObject *res = PyNumber_Lshift(left, right);
1442 Py_DECREF(left);
1443 Py_DECREF(right);
1444 SET_TOP(res);
1445 if (res == NULL)
1446 goto error;
1447 DISPATCH();
1448 }
1449
Benjamin Petersonddd19492018-09-16 22:38:02 -07001450 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001451 PyObject *right = POP();
1452 PyObject *left = TOP();
1453 PyObject *res = PyNumber_Rshift(left, right);
1454 Py_DECREF(left);
1455 Py_DECREF(right);
1456 SET_TOP(res);
1457 if (res == NULL)
1458 goto error;
1459 DISPATCH();
1460 }
1461
Benjamin Petersonddd19492018-09-16 22:38:02 -07001462 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001463 PyObject *right = POP();
1464 PyObject *left = TOP();
1465 PyObject *res = PyNumber_And(left, right);
1466 Py_DECREF(left);
1467 Py_DECREF(right);
1468 SET_TOP(res);
1469 if (res == NULL)
1470 goto error;
1471 DISPATCH();
1472 }
1473
Benjamin Petersonddd19492018-09-16 22:38:02 -07001474 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001475 PyObject *right = POP();
1476 PyObject *left = TOP();
1477 PyObject *res = PyNumber_Xor(left, right);
1478 Py_DECREF(left);
1479 Py_DECREF(right);
1480 SET_TOP(res);
1481 if (res == NULL)
1482 goto error;
1483 DISPATCH();
1484 }
1485
Benjamin Petersonddd19492018-09-16 22:38:02 -07001486 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001487 PyObject *right = POP();
1488 PyObject *left = TOP();
1489 PyObject *res = PyNumber_Or(left, right);
1490 Py_DECREF(left);
1491 Py_DECREF(right);
1492 SET_TOP(res);
1493 if (res == NULL)
1494 goto error;
1495 DISPATCH();
1496 }
1497
Benjamin Petersonddd19492018-09-16 22:38:02 -07001498 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001499 PyObject *v = POP();
1500 PyObject *list = PEEK(oparg);
1501 int err;
1502 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001503 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001504 if (err != 0)
1505 goto error;
1506 PREDICT(JUMP_ABSOLUTE);
1507 DISPATCH();
1508 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001509
Benjamin Petersonddd19492018-09-16 22:38:02 -07001510 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001511 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001512 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001513 int err;
1514 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001516 if (err != 0)
1517 goto error;
1518 PREDICT(JUMP_ABSOLUTE);
1519 DISPATCH();
1520 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001521
Benjamin Petersonddd19492018-09-16 22:38:02 -07001522 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001523 PyObject *exp = POP();
1524 PyObject *base = TOP();
1525 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1526 Py_DECREF(base);
1527 Py_DECREF(exp);
1528 SET_TOP(res);
1529 if (res == NULL)
1530 goto error;
1531 DISPATCH();
1532 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001533
Benjamin Petersonddd19492018-09-16 22:38:02 -07001534 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001535 PyObject *right = POP();
1536 PyObject *left = TOP();
1537 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1538 Py_DECREF(left);
1539 Py_DECREF(right);
1540 SET_TOP(res);
1541 if (res == NULL)
1542 goto error;
1543 DISPATCH();
1544 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001545
Benjamin Petersonddd19492018-09-16 22:38:02 -07001546 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001547 PyObject *right = POP();
1548 PyObject *left = TOP();
1549 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1550 Py_DECREF(left);
1551 Py_DECREF(right);
1552 SET_TOP(res);
1553 if (res == NULL)
1554 goto error;
1555 DISPATCH();
1556 }
1557
Benjamin Petersonddd19492018-09-16 22:38:02 -07001558 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001559 PyObject *divisor = POP();
1560 PyObject *dividend = TOP();
1561 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1562 Py_DECREF(dividend);
1563 Py_DECREF(divisor);
1564 SET_TOP(quotient);
1565 if (quotient == NULL)
1566 goto error;
1567 DISPATCH();
1568 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001569
Benjamin Petersonddd19492018-09-16 22:38:02 -07001570 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001571 PyObject *divisor = POP();
1572 PyObject *dividend = TOP();
1573 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1574 Py_DECREF(dividend);
1575 Py_DECREF(divisor);
1576 SET_TOP(quotient);
1577 if (quotient == NULL)
1578 goto error;
1579 DISPATCH();
1580 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001581
Benjamin Petersonddd19492018-09-16 22:38:02 -07001582 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001583 PyObject *right = POP();
1584 PyObject *left = TOP();
1585 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1586 Py_DECREF(left);
1587 Py_DECREF(right);
1588 SET_TOP(mod);
1589 if (mod == NULL)
1590 goto error;
1591 DISPATCH();
1592 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001593
Benjamin Petersonddd19492018-09-16 22:38:02 -07001594 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001595 PyObject *right = POP();
1596 PyObject *left = TOP();
1597 PyObject *sum;
1598 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1599 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001600 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001601 }
1602 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001603 sum = PyNumber_InPlaceAdd(left, right);
1604 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001605 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001606 Py_DECREF(right);
1607 SET_TOP(sum);
1608 if (sum == NULL)
1609 goto error;
1610 DISPATCH();
1611 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001612
Benjamin Petersonddd19492018-09-16 22:38:02 -07001613 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001614 PyObject *right = POP();
1615 PyObject *left = TOP();
1616 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1617 Py_DECREF(left);
1618 Py_DECREF(right);
1619 SET_TOP(diff);
1620 if (diff == NULL)
1621 goto error;
1622 DISPATCH();
1623 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001624
Benjamin Petersonddd19492018-09-16 22:38:02 -07001625 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001626 PyObject *right = POP();
1627 PyObject *left = TOP();
1628 PyObject *res = PyNumber_InPlaceLshift(left, right);
1629 Py_DECREF(left);
1630 Py_DECREF(right);
1631 SET_TOP(res);
1632 if (res == NULL)
1633 goto error;
1634 DISPATCH();
1635 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001636
Benjamin Petersonddd19492018-09-16 22:38:02 -07001637 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001638 PyObject *right = POP();
1639 PyObject *left = TOP();
1640 PyObject *res = PyNumber_InPlaceRshift(left, right);
1641 Py_DECREF(left);
1642 Py_DECREF(right);
1643 SET_TOP(res);
1644 if (res == NULL)
1645 goto error;
1646 DISPATCH();
1647 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001648
Benjamin Petersonddd19492018-09-16 22:38:02 -07001649 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001650 PyObject *right = POP();
1651 PyObject *left = TOP();
1652 PyObject *res = PyNumber_InPlaceAnd(left, right);
1653 Py_DECREF(left);
1654 Py_DECREF(right);
1655 SET_TOP(res);
1656 if (res == NULL)
1657 goto error;
1658 DISPATCH();
1659 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001660
Benjamin Petersonddd19492018-09-16 22:38:02 -07001661 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001662 PyObject *right = POP();
1663 PyObject *left = TOP();
1664 PyObject *res = PyNumber_InPlaceXor(left, right);
1665 Py_DECREF(left);
1666 Py_DECREF(right);
1667 SET_TOP(res);
1668 if (res == NULL)
1669 goto error;
1670 DISPATCH();
1671 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001672
Benjamin Petersonddd19492018-09-16 22:38:02 -07001673 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001674 PyObject *right = POP();
1675 PyObject *left = TOP();
1676 PyObject *res = PyNumber_InPlaceOr(left, right);
1677 Py_DECREF(left);
1678 Py_DECREF(right);
1679 SET_TOP(res);
1680 if (res == NULL)
1681 goto error;
1682 DISPATCH();
1683 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001684
Benjamin Petersonddd19492018-09-16 22:38:02 -07001685 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001686 PyObject *sub = TOP();
1687 PyObject *container = SECOND();
1688 PyObject *v = THIRD();
1689 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001690 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001691 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001692 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001694 Py_DECREF(container);
1695 Py_DECREF(sub);
1696 if (err != 0)
1697 goto error;
1698 DISPATCH();
1699 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001700
Benjamin Petersonddd19492018-09-16 22:38:02 -07001701 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001702 PyObject *sub = TOP();
1703 PyObject *container = SECOND();
1704 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001705 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001706 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001707 err = PyObject_DelItem(container, sub);
1708 Py_DECREF(container);
1709 Py_DECREF(sub);
1710 if (err != 0)
1711 goto error;
1712 DISPATCH();
1713 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001714
Benjamin Petersonddd19492018-09-16 22:38:02 -07001715 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001716 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001717 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001718 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001719 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001720 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 PyErr_SetString(PyExc_RuntimeError,
1722 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001723 Py_DECREF(value);
1724 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001726 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001727 Py_DECREF(value);
1728 if (res == NULL)
1729 goto error;
1730 Py_DECREF(res);
1731 DISPATCH();
1732 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001733
Benjamin Petersonddd19492018-09-16 22:38:02 -07001734 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001735 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001736 switch (oparg) {
1737 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001738 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001739 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001741 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001742 /* fall through */
1743 case 0:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001744 if (do_raise(exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001745 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001746 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001747 break;
1748 default:
1749 PyErr_SetString(PyExc_SystemError,
1750 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751 break;
1752 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001753 goto error;
1754 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001755
Benjamin Petersonddd19492018-09-16 22:38:02 -07001756 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001758 assert(f->f_iblock == 0);
1759 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001760 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001761
Benjamin Petersonddd19492018-09-16 22:38:02 -07001762 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001763 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001764 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001765 PyObject *obj = TOP();
1766 PyTypeObject *type = Py_TYPE(obj);
1767
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001768 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001769 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001770 }
Yury Selivanov75445082015-05-11 22:57:16 -04001771
1772 if (getter != NULL) {
1773 iter = (*getter)(obj);
1774 Py_DECREF(obj);
1775 if (iter == NULL) {
1776 SET_TOP(NULL);
1777 goto error;
1778 }
1779 }
1780 else {
1781 SET_TOP(NULL);
1782 PyErr_Format(
1783 PyExc_TypeError,
1784 "'async for' requires an object with "
1785 "__aiter__ method, got %.100s",
1786 type->tp_name);
1787 Py_DECREF(obj);
1788 goto error;
1789 }
1790
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001791 if (Py_TYPE(iter)->tp_as_async == NULL ||
1792 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001793
Yury Selivanov398ff912017-03-02 22:20:00 -05001794 SET_TOP(NULL);
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001795 PyErr_Format(
1796 PyExc_TypeError,
1797 "'async for' received an object from __aiter__ "
1798 "that does not implement __anext__: %.100s",
1799 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001800 Py_DECREF(iter);
1801 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001802 }
1803
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001804 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001805 DISPATCH();
1806 }
1807
Benjamin Petersonddd19492018-09-16 22:38:02 -07001808 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001809 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001810 PyObject *next_iter = NULL;
1811 PyObject *awaitable = NULL;
1812 PyObject *aiter = TOP();
1813 PyTypeObject *type = Py_TYPE(aiter);
1814
Yury Selivanoveb636452016-09-08 22:01:51 -07001815 if (PyAsyncGen_CheckExact(aiter)) {
1816 awaitable = type->tp_as_async->am_anext(aiter);
1817 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001818 goto error;
1819 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001820 } else {
1821 if (type->tp_as_async != NULL){
1822 getter = type->tp_as_async->am_anext;
1823 }
Yury Selivanov75445082015-05-11 22:57:16 -04001824
Yury Selivanoveb636452016-09-08 22:01:51 -07001825 if (getter != NULL) {
1826 next_iter = (*getter)(aiter);
1827 if (next_iter == NULL) {
1828 goto error;
1829 }
1830 }
1831 else {
1832 PyErr_Format(
1833 PyExc_TypeError,
1834 "'async for' requires an iterator with "
1835 "__anext__ method, got %.100s",
1836 type->tp_name);
1837 goto error;
1838 }
Yury Selivanov75445082015-05-11 22:57:16 -04001839
Yury Selivanoveb636452016-09-08 22:01:51 -07001840 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1841 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001842 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001843 PyExc_TypeError,
1844 "'async for' received an invalid object "
1845 "from __anext__: %.100s",
1846 Py_TYPE(next_iter)->tp_name);
1847
1848 Py_DECREF(next_iter);
1849 goto error;
1850 } else {
1851 Py_DECREF(next_iter);
1852 }
1853 }
Yury Selivanov75445082015-05-11 22:57:16 -04001854
1855 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001856 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001857 DISPATCH();
1858 }
1859
Benjamin Petersonddd19492018-09-16 22:38:02 -07001860 case TARGET(GET_AWAITABLE): {
1861 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001862 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001863 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001864
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03001865 if (iter == NULL) {
1866 format_awaitable_error(Py_TYPE(iterable),
1867 _Py_OPCODE(next_instr[-2]));
1868 }
1869
Yury Selivanov75445082015-05-11 22:57:16 -04001870 Py_DECREF(iterable);
1871
Yury Selivanovc724bae2016-03-02 11:30:46 -05001872 if (iter != NULL && PyCoro_CheckExact(iter)) {
1873 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1874 if (yf != NULL) {
1875 /* `iter` is a coroutine object that is being
1876 awaited, `yf` is a pointer to the current awaitable
1877 being awaited on. */
1878 Py_DECREF(yf);
1879 Py_CLEAR(iter);
1880 PyErr_SetString(
1881 PyExc_RuntimeError,
1882 "coroutine is being awaited already");
1883 /* The code below jumps to `error` if `iter` is NULL. */
1884 }
1885 }
1886
Yury Selivanov75445082015-05-11 22:57:16 -04001887 SET_TOP(iter); /* Even if it's NULL */
1888
1889 if (iter == NULL) {
1890 goto error;
1891 }
1892
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001893 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001894 DISPATCH();
1895 }
1896
Benjamin Petersonddd19492018-09-16 22:38:02 -07001897 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001898 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001899 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001900 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001901 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1902 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001903 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001904 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001905 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001906 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001907 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001908 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001909 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001910 Py_DECREF(v);
1911 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001912 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001913 if (tstate->c_tracefunc != NULL
1914 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001915 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001916 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001917 if (err < 0)
1918 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001919 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001920 SET_TOP(val);
1921 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001922 }
Martin Panter95f53c12016-07-18 08:23:26 +00001923 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001924 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001925 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001926 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001927 f->f_lasti -= sizeof(_Py_CODEUNIT);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001928 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001929 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001930
Benjamin Petersonddd19492018-09-16 22:38:02 -07001931 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07001933
1934 if (co->co_flags & CO_ASYNC_GENERATOR) {
1935 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
1936 Py_DECREF(retval);
1937 if (w == NULL) {
1938 retval = NULL;
1939 goto error;
1940 }
1941 retval = w;
1942 }
1943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 f->f_stacktop = stack_pointer;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001945 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001946 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001947
Benjamin Petersonddd19492018-09-16 22:38:02 -07001948 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001949 PyObject *type, *value, *traceback;
1950 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001951 PyTryBlock *b = PyFrame_BlockPop(f);
1952 if (b->b_type != EXCEPT_HANDLER) {
1953 PyErr_SetString(PyExc_SystemError,
1954 "popped block is not an except handler");
1955 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001957 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
1958 STACK_LEVEL() <= (b)->b_level + 4);
1959 exc_info = tstate->exc_info;
1960 type = exc_info->exc_type;
1961 value = exc_info->exc_value;
1962 traceback = exc_info->exc_traceback;
1963 exc_info->exc_type = POP();
1964 exc_info->exc_value = POP();
1965 exc_info->exc_traceback = POP();
1966 Py_XDECREF(type);
1967 Py_XDECREF(value);
1968 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001970 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001971
Benjamin Petersonddd19492018-09-16 22:38:02 -07001972 case TARGET(POP_BLOCK): {
1973 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001974 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001976 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001977
Benjamin Petersonddd19492018-09-16 22:38:02 -07001978 case TARGET(POP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001979 /* If oparg is 0 at the top of the stack are 1 or 6 values:
1980 Either:
1981 - TOP = NULL or an integer
1982 or:
1983 - (TOP, SECOND, THIRD) = exc_info()
1984 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
1985
1986 If oparg is 1 the value for 'return' was additionally pushed
1987 at the top of the stack.
1988 */
1989 PyObject *res = NULL;
1990 if (oparg) {
1991 res = POP();
1992 }
1993 PyObject *exc = POP();
1994 if (exc == NULL || PyLong_CheckExact(exc)) {
1995 Py_XDECREF(exc);
1996 }
1997 else {
1998 Py_DECREF(exc);
1999 Py_DECREF(POP());
2000 Py_DECREF(POP());
2001
2002 PyObject *type, *value, *traceback;
2003 _PyErr_StackItem *exc_info;
2004 PyTryBlock *b = PyFrame_BlockPop(f);
2005 if (b->b_type != EXCEPT_HANDLER) {
2006 PyErr_SetString(PyExc_SystemError,
2007 "popped block is not an except handler");
2008 Py_XDECREF(res);
2009 goto error;
2010 }
2011 assert(STACK_LEVEL() == (b)->b_level + 3);
2012 exc_info = tstate->exc_info;
2013 type = exc_info->exc_type;
2014 value = exc_info->exc_value;
2015 traceback = exc_info->exc_traceback;
2016 exc_info->exc_type = POP();
2017 exc_info->exc_value = POP();
2018 exc_info->exc_traceback = POP();
2019 Py_XDECREF(type);
2020 Py_XDECREF(value);
2021 Py_XDECREF(traceback);
2022 }
2023 if (oparg) {
2024 PUSH(res);
2025 }
2026 DISPATCH();
2027 }
2028
Benjamin Petersonddd19492018-09-16 22:38:02 -07002029 case TARGET(CALL_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002030 PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
2031 if (ret == NULL) {
2032 goto error;
2033 }
2034 PUSH(ret);
2035 JUMPBY(oparg);
2036 FAST_DISPATCH();
2037 }
2038
Benjamin Petersonddd19492018-09-16 22:38:02 -07002039 case TARGET(BEGIN_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002040 /* Push NULL onto the stack for using it in END_FINALLY,
2041 POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH.
2042 */
2043 PUSH(NULL);
2044 FAST_DISPATCH();
2045 }
2046
Benjamin Petersonddd19492018-09-16 22:38:02 -07002047 case TARGET(END_FINALLY): {
2048 PREDICTED(END_FINALLY);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002049 /* At the top of the stack are 1 or 6 values:
2050 Either:
2051 - TOP = NULL or an integer
2052 or:
2053 - (TOP, SECOND, THIRD) = exc_info()
2054 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2055 */
2056 PyObject *exc = POP();
2057 if (exc == NULL) {
2058 FAST_DISPATCH();
2059 }
2060 else if (PyLong_CheckExact(exc)) {
2061 int ret = _PyLong_AsInt(exc);
2062 Py_DECREF(exc);
2063 if (ret == -1 && PyErr_Occurred()) {
2064 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002065 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002066 JUMPTO(ret);
2067 FAST_DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002068 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002069 else {
2070 assert(PyExceptionClass_Check(exc));
2071 PyObject *val = POP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002072 PyObject *tb = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002073 PyErr_Restore(exc, val, tb);
2074 goto exception_unwind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002076 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002077
Benjamin Petersonddd19492018-09-16 22:38:02 -07002078 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002079 PyObject *exc = POP();
2080 assert(PyExceptionClass_Check(exc));
2081 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2082 PyTryBlock *b = PyFrame_BlockPop(f);
2083 assert(b->b_type == EXCEPT_HANDLER);
2084 Py_DECREF(exc);
2085 UNWIND_EXCEPT_HANDLER(b);
2086 Py_DECREF(POP());
2087 JUMPBY(oparg);
2088 FAST_DISPATCH();
2089 }
2090 else {
2091 PyObject *val = POP();
2092 PyObject *tb = POP();
2093 PyErr_Restore(exc, val, tb);
2094 goto exception_unwind;
2095 }
2096 }
2097
Benjamin Petersonddd19492018-09-16 22:38:02 -07002098 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002099 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002100
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002101 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002102 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002103 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002104 if (bc == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002105 if (!PyErr_Occurred()) {
2106 PyErr_SetString(PyExc_NameError,
2107 "__build_class__ not found");
2108 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002109 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002110 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002111 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002112 }
2113 else {
2114 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2115 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002116 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002117 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2118 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002119 if (PyErr_ExceptionMatches(PyExc_KeyError))
2120 PyErr_SetString(PyExc_NameError,
2121 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002122 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002123 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002125 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002126 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002127 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002128
Benjamin Petersonddd19492018-09-16 22:38:02 -07002129 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002130 PyObject *name = GETITEM(names, oparg);
2131 PyObject *v = POP();
2132 PyObject *ns = f->f_locals;
2133 int err;
2134 if (ns == NULL) {
2135 PyErr_Format(PyExc_SystemError,
2136 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002137 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002138 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002139 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002140 if (PyDict_CheckExact(ns))
2141 err = PyDict_SetItem(ns, name, v);
2142 else
2143 err = PyObject_SetItem(ns, name, v);
2144 Py_DECREF(v);
2145 if (err != 0)
2146 goto error;
2147 DISPATCH();
2148 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002149
Benjamin Petersonddd19492018-09-16 22:38:02 -07002150 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002151 PyObject *name = GETITEM(names, oparg);
2152 PyObject *ns = f->f_locals;
2153 int err;
2154 if (ns == NULL) {
2155 PyErr_Format(PyExc_SystemError,
2156 "no locals when deleting %R", name);
2157 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002158 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002159 err = PyObject_DelItem(ns, name);
2160 if (err != 0) {
2161 format_exc_check_arg(PyExc_NameError,
2162 NAME_ERROR_MSG,
2163 name);
2164 goto error;
2165 }
2166 DISPATCH();
2167 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002168
Benjamin Petersonddd19492018-09-16 22:38:02 -07002169 case TARGET(UNPACK_SEQUENCE): {
2170 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002171 PyObject *seq = POP(), *item, **items;
2172 if (PyTuple_CheckExact(seq) &&
2173 PyTuple_GET_SIZE(seq) == oparg) {
2174 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002176 item = items[oparg];
2177 Py_INCREF(item);
2178 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002180 } else if (PyList_CheckExact(seq) &&
2181 PyList_GET_SIZE(seq) == oparg) {
2182 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002183 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002184 item = items[oparg];
2185 Py_INCREF(item);
2186 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002187 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002188 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002189 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002190 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 } else {
2192 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002193 Py_DECREF(seq);
2194 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002196 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002197 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002199
Benjamin Petersonddd19492018-09-16 22:38:02 -07002200 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002201 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2202 PyObject *seq = POP();
2203
2204 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2205 stack_pointer + totalargs)) {
2206 stack_pointer += totalargs;
2207 } else {
2208 Py_DECREF(seq);
2209 goto error;
2210 }
2211 Py_DECREF(seq);
2212 DISPATCH();
2213 }
2214
Benjamin Petersonddd19492018-09-16 22:38:02 -07002215 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002216 PyObject *name = GETITEM(names, oparg);
2217 PyObject *owner = TOP();
2218 PyObject *v = SECOND();
2219 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002220 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002221 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002223 Py_DECREF(owner);
2224 if (err != 0)
2225 goto error;
2226 DISPATCH();
2227 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002228
Benjamin Petersonddd19492018-09-16 22:38:02 -07002229 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002230 PyObject *name = GETITEM(names, oparg);
2231 PyObject *owner = POP();
2232 int err;
2233 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2234 Py_DECREF(owner);
2235 if (err != 0)
2236 goto error;
2237 DISPATCH();
2238 }
2239
Benjamin Petersonddd19492018-09-16 22:38:02 -07002240 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002241 PyObject *name = GETITEM(names, oparg);
2242 PyObject *v = POP();
2243 int err;
2244 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002246 if (err != 0)
2247 goto error;
2248 DISPATCH();
2249 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002250
Benjamin Petersonddd19492018-09-16 22:38:02 -07002251 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002252 PyObject *name = GETITEM(names, oparg);
2253 int err;
2254 err = PyDict_DelItem(f->f_globals, name);
2255 if (err != 0) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002256 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2257 format_exc_check_arg(
2258 PyExc_NameError, NAME_ERROR_MSG, name);
2259 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002260 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002261 }
2262 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002263 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002264
Benjamin Petersonddd19492018-09-16 22:38:02 -07002265 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002266 PyObject *name = GETITEM(names, oparg);
2267 PyObject *locals = f->f_locals;
2268 PyObject *v;
2269 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002271 "no locals when loading %R", name);
2272 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002273 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002274 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002275 v = PyDict_GetItemWithError(locals, name);
2276 if (v != NULL) {
2277 Py_INCREF(v);
2278 }
2279 else if (PyErr_Occurred()) {
2280 goto error;
2281 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002282 }
2283 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002284 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002285 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002286 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2287 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 PyErr_Clear();
2289 }
2290 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002291 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002292 v = PyDict_GetItemWithError(f->f_globals, name);
2293 if (v != NULL) {
2294 Py_INCREF(v);
2295 }
2296 else if (PyErr_Occurred()) {
2297 goto error;
2298 }
2299 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002300 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002301 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002302 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002303 if (!PyErr_Occurred()) {
2304 format_exc_check_arg(
Victor Stinnerb0b22422012-04-19 00:57:45 +02002305 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002306 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002307 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002308 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002309 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002310 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002311 }
2312 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002313 v = PyObject_GetItem(f->f_builtins, name);
2314 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002315 if (PyErr_ExceptionMatches(PyExc_KeyError))
2316 format_exc_check_arg(
2317 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002318 NAME_ERROR_MSG, name);
2319 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002320 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002321 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002322 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002323 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002324 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002325 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002326 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002327
Benjamin Petersonddd19492018-09-16 22:38:02 -07002328 case TARGET(LOAD_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002329 PyObject *name = GETITEM(names, oparg);
2330 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002331 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002332 && PyDict_CheckExact(f->f_builtins))
2333 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002334 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002335 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002336 name);
2337 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002338 if (!_PyErr_OCCURRED()) {
2339 /* _PyDict_LoadGlobal() returns NULL without raising
2340 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002341 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002342 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002343 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002344 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002345 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002346 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002347 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002348 else {
2349 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002350
2351 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002352 v = PyObject_GetItem(f->f_globals, name);
2353 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002354 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2355 goto error;
2356 PyErr_Clear();
2357
Victor Stinnerb4efc962015-11-20 09:24:02 +01002358 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002359 v = PyObject_GetItem(f->f_builtins, name);
2360 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002361 if (PyErr_ExceptionMatches(PyExc_KeyError))
2362 format_exc_check_arg(
2363 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002364 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002365 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002366 }
2367 }
2368 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002369 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002370 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002371 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002372
Benjamin Petersonddd19492018-09-16 22:38:02 -07002373 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002374 PyObject *v = GETLOCAL(oparg);
2375 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 SETLOCAL(oparg, NULL);
2377 DISPATCH();
2378 }
2379 format_exc_check_arg(
2380 PyExc_UnboundLocalError,
2381 UNBOUNDLOCAL_ERROR_MSG,
2382 PyTuple_GetItem(co->co_varnames, oparg)
2383 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002384 goto error;
2385 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002386
Benjamin Petersonddd19492018-09-16 22:38:02 -07002387 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002388 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002389 PyObject *oldobj = PyCell_GET(cell);
2390 if (oldobj != NULL) {
2391 PyCell_SET(cell, NULL);
2392 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002393 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002394 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002395 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002396 goto error;
2397 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002398
Benjamin Petersonddd19492018-09-16 22:38:02 -07002399 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002400 PyObject *cell = freevars[oparg];
2401 Py_INCREF(cell);
2402 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002404 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002405
Benjamin Petersonddd19492018-09-16 22:38:02 -07002406 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002407 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002408 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002409 assert(locals);
2410 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2411 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2412 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2413 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2414 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002415 value = PyDict_GetItemWithError(locals, name);
2416 if (value != NULL) {
2417 Py_INCREF(value);
2418 }
2419 else if (PyErr_Occurred()) {
2420 goto error;
2421 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002422 }
2423 else {
2424 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002425 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002426 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2427 goto error;
2428 PyErr_Clear();
2429 }
2430 }
2431 if (!value) {
2432 PyObject *cell = freevars[oparg];
2433 value = PyCell_GET(cell);
2434 if (value == NULL) {
2435 format_exc_unbound(co, oparg);
2436 goto error;
2437 }
2438 Py_INCREF(value);
2439 }
2440 PUSH(value);
2441 DISPATCH();
2442 }
2443
Benjamin Petersonddd19492018-09-16 22:38:02 -07002444 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002445 PyObject *cell = freevars[oparg];
2446 PyObject *value = PyCell_GET(cell);
2447 if (value == NULL) {
2448 format_exc_unbound(co, oparg);
2449 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002450 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002451 Py_INCREF(value);
2452 PUSH(value);
2453 DISPATCH();
2454 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002455
Benjamin Petersonddd19492018-09-16 22:38:02 -07002456 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002457 PyObject *v = POP();
2458 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002459 PyObject *oldobj = PyCell_GET(cell);
2460 PyCell_SET(cell, v);
2461 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002462 DISPATCH();
2463 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002464
Benjamin Petersonddd19492018-09-16 22:38:02 -07002465 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002466 PyObject *str;
2467 PyObject *empty = PyUnicode_New(0, 0);
2468 if (empty == NULL) {
2469 goto error;
2470 }
2471 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2472 Py_DECREF(empty);
2473 if (str == NULL)
2474 goto error;
2475 while (--oparg >= 0) {
2476 PyObject *item = POP();
2477 Py_DECREF(item);
2478 }
2479 PUSH(str);
2480 DISPATCH();
2481 }
2482
Benjamin Petersonddd19492018-09-16 22:38:02 -07002483 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002484 PyObject *tup = PyTuple_New(oparg);
2485 if (tup == NULL)
2486 goto error;
2487 while (--oparg >= 0) {
2488 PyObject *item = POP();
2489 PyTuple_SET_ITEM(tup, oparg, item);
2490 }
2491 PUSH(tup);
2492 DISPATCH();
2493 }
2494
Benjamin Petersonddd19492018-09-16 22:38:02 -07002495 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002496 PyObject *list = PyList_New(oparg);
2497 if (list == NULL)
2498 goto error;
2499 while (--oparg >= 0) {
2500 PyObject *item = POP();
2501 PyList_SET_ITEM(list, oparg, item);
2502 }
2503 PUSH(list);
2504 DISPATCH();
2505 }
2506
Benjamin Petersonddd19492018-09-16 22:38:02 -07002507 case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL):
2508 case TARGET(BUILD_TUPLE_UNPACK):
2509 case TARGET(BUILD_LIST_UNPACK): {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002510 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002511 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002512 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002513 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002514
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002515 if (sum == NULL)
2516 goto error;
2517
2518 for (i = oparg; i > 0; i--) {
2519 PyObject *none_val;
2520
2521 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2522 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002523 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002524 PyErr_ExceptionMatches(PyExc_TypeError))
2525 {
2526 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002527 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002528 Py_DECREF(sum);
2529 goto error;
2530 }
2531 Py_DECREF(none_val);
2532 }
2533
2534 if (convert_to_tuple) {
2535 return_value = PyList_AsTuple(sum);
2536 Py_DECREF(sum);
2537 if (return_value == NULL)
2538 goto error;
2539 }
2540 else {
2541 return_value = sum;
2542 }
2543
2544 while (oparg--)
2545 Py_DECREF(POP());
2546 PUSH(return_value);
2547 DISPATCH();
2548 }
2549
Benjamin Petersonddd19492018-09-16 22:38:02 -07002550 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002551 PyObject *set = PySet_New(NULL);
2552 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002553 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002554 if (set == NULL)
2555 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002556 for (i = oparg; i > 0; i--) {
2557 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002558 if (err == 0)
2559 err = PySet_Add(set, item);
2560 Py_DECREF(item);
2561 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002562 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002563 if (err != 0) {
2564 Py_DECREF(set);
2565 goto error;
2566 }
2567 PUSH(set);
2568 DISPATCH();
2569 }
2570
Benjamin Petersonddd19492018-09-16 22:38:02 -07002571 case TARGET(BUILD_SET_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002572 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002573 PyObject *sum = PySet_New(NULL);
2574 if (sum == NULL)
2575 goto error;
2576
2577 for (i = oparg; i > 0; i--) {
2578 if (_PySet_Update(sum, PEEK(i)) < 0) {
2579 Py_DECREF(sum);
2580 goto error;
2581 }
2582 }
2583
2584 while (oparg--)
2585 Py_DECREF(POP());
2586 PUSH(sum);
2587 DISPATCH();
2588 }
2589
Benjamin Petersonddd19492018-09-16 22:38:02 -07002590 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002591 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002592 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2593 if (map == NULL)
2594 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002595 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002596 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002597 PyObject *key = PEEK(2*i);
2598 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002599 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002600 if (err != 0) {
2601 Py_DECREF(map);
2602 goto error;
2603 }
2604 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002605
2606 while (oparg--) {
2607 Py_DECREF(POP());
2608 Py_DECREF(POP());
2609 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002610 PUSH(map);
2611 DISPATCH();
2612 }
2613
Benjamin Petersonddd19492018-09-16 22:38:02 -07002614 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002615 _Py_IDENTIFIER(__annotations__);
2616 int err;
2617 PyObject *ann_dict;
2618 if (f->f_locals == NULL) {
2619 PyErr_Format(PyExc_SystemError,
2620 "no locals found when setting up annotations");
2621 goto error;
2622 }
2623 /* check if __annotations__ in locals()... */
2624 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002625 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002626 &PyId___annotations__);
2627 if (ann_dict == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002628 if (PyErr_Occurred()) {
2629 goto error;
2630 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002631 /* ...if not, create a new one */
2632 ann_dict = PyDict_New();
2633 if (ann_dict == NULL) {
2634 goto error;
2635 }
2636 err = _PyDict_SetItemId(f->f_locals,
2637 &PyId___annotations__, ann_dict);
2638 Py_DECREF(ann_dict);
2639 if (err != 0) {
2640 goto error;
2641 }
2642 }
2643 }
2644 else {
2645 /* do the same if locals() is not a dict */
2646 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2647 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002648 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002649 }
2650 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2651 if (ann_dict == NULL) {
2652 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2653 goto error;
2654 }
2655 PyErr_Clear();
2656 ann_dict = PyDict_New();
2657 if (ann_dict == NULL) {
2658 goto error;
2659 }
2660 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2661 Py_DECREF(ann_dict);
2662 if (err != 0) {
2663 goto error;
2664 }
2665 }
2666 else {
2667 Py_DECREF(ann_dict);
2668 }
2669 }
2670 DISPATCH();
2671 }
2672
Benjamin Petersonddd19492018-09-16 22:38:02 -07002673 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002674 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002675 PyObject *map;
2676 PyObject *keys = TOP();
2677 if (!PyTuple_CheckExact(keys) ||
2678 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2679 PyErr_SetString(PyExc_SystemError,
2680 "bad BUILD_CONST_KEY_MAP keys argument");
2681 goto error;
2682 }
2683 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2684 if (map == NULL) {
2685 goto error;
2686 }
2687 for (i = oparg; i > 0; i--) {
2688 int err;
2689 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2690 PyObject *value = PEEK(i + 1);
2691 err = PyDict_SetItem(map, key, value);
2692 if (err != 0) {
2693 Py_DECREF(map);
2694 goto error;
2695 }
2696 }
2697
2698 Py_DECREF(POP());
2699 while (oparg--) {
2700 Py_DECREF(POP());
2701 }
2702 PUSH(map);
2703 DISPATCH();
2704 }
2705
Benjamin Petersonddd19492018-09-16 22:38:02 -07002706 case TARGET(BUILD_MAP_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002707 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002708 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002709 if (sum == NULL)
2710 goto error;
2711
2712 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002713 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002714 if (PyDict_Update(sum, arg) < 0) {
2715 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2716 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002717 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002718 arg->ob_type->tp_name);
2719 }
2720 Py_DECREF(sum);
2721 goto error;
2722 }
2723 }
2724
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002725 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002726 Py_DECREF(POP());
2727 PUSH(sum);
2728 DISPATCH();
2729 }
2730
Benjamin Petersonddd19492018-09-16 22:38:02 -07002731 case TARGET(BUILD_MAP_UNPACK_WITH_CALL): {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002732 Py_ssize_t i;
2733 PyObject *sum = PyDict_New();
2734 if (sum == NULL)
2735 goto error;
2736
2737 for (i = oparg; i > 0; i--) {
2738 PyObject *arg = PEEK(i);
2739 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002740 Py_DECREF(sum);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02002741 format_kwargs_error(PEEK(2 + oparg), arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002742 goto error;
2743 }
2744 }
2745
2746 while (oparg--)
2747 Py_DECREF(POP());
2748 PUSH(sum);
2749 DISPATCH();
2750 }
2751
Benjamin Petersonddd19492018-09-16 22:38:02 -07002752 case TARGET(MAP_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002753 PyObject *key = TOP();
2754 PyObject *value = SECOND();
2755 PyObject *map;
2756 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002757 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002758 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002759 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002760 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002761 Py_DECREF(value);
2762 Py_DECREF(key);
2763 if (err != 0)
2764 goto error;
2765 PREDICT(JUMP_ABSOLUTE);
2766 DISPATCH();
2767 }
2768
Benjamin Petersonddd19492018-09-16 22:38:02 -07002769 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002770 PyObject *name = GETITEM(names, oparg);
2771 PyObject *owner = TOP();
2772 PyObject *res = PyObject_GetAttr(owner, name);
2773 Py_DECREF(owner);
2774 SET_TOP(res);
2775 if (res == NULL)
2776 goto error;
2777 DISPATCH();
2778 }
2779
Benjamin Petersonddd19492018-09-16 22:38:02 -07002780 case TARGET(COMPARE_OP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002781 PyObject *right = POP();
2782 PyObject *left = TOP();
2783 PyObject *res = cmp_outcome(oparg, left, right);
2784 Py_DECREF(left);
2785 Py_DECREF(right);
2786 SET_TOP(res);
2787 if (res == NULL)
2788 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002789 PREDICT(POP_JUMP_IF_FALSE);
2790 PREDICT(POP_JUMP_IF_TRUE);
2791 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002792 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002793
Benjamin Petersonddd19492018-09-16 22:38:02 -07002794 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002795 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002796 PyObject *fromlist = POP();
2797 PyObject *level = TOP();
2798 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002799 res = import_name(f, name, fromlist, level);
2800 Py_DECREF(level);
2801 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002802 SET_TOP(res);
2803 if (res == NULL)
2804 goto error;
2805 DISPATCH();
2806 }
2807
Benjamin Petersonddd19492018-09-16 22:38:02 -07002808 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002809 PyObject *from = POP(), *locals;
2810 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002811 if (PyFrame_FastToLocalsWithError(f) < 0) {
2812 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002813 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002814 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002815
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002816 locals = f->f_locals;
2817 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002818 PyErr_SetString(PyExc_SystemError,
2819 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002820 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002821 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002822 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002823 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002825 Py_DECREF(from);
2826 if (err != 0)
2827 goto error;
2828 DISPATCH();
2829 }
Guido van Rossum25831651993-05-19 14:50:45 +00002830
Benjamin Petersonddd19492018-09-16 22:38:02 -07002831 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002832 PyObject *name = GETITEM(names, oparg);
2833 PyObject *from = TOP();
2834 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002835 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002836 PUSH(res);
2837 if (res == NULL)
2838 goto error;
2839 DISPATCH();
2840 }
Thomas Wouters52152252000-08-17 22:55:00 +00002841
Benjamin Petersonddd19492018-09-16 22:38:02 -07002842 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002843 JUMPBY(oparg);
2844 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002845 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002846
Benjamin Petersonddd19492018-09-16 22:38:02 -07002847 case TARGET(POP_JUMP_IF_FALSE): {
2848 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002849 PyObject *cond = POP();
2850 int err;
2851 if (cond == Py_True) {
2852 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 FAST_DISPATCH();
2854 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002855 if (cond == Py_False) {
2856 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002857 JUMPTO(oparg);
2858 FAST_DISPATCH();
2859 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002860 err = PyObject_IsTrue(cond);
2861 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002862 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002863 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002864 else if (err == 0)
2865 JUMPTO(oparg);
2866 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002867 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002869 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002870
Benjamin Petersonddd19492018-09-16 22:38:02 -07002871 case TARGET(POP_JUMP_IF_TRUE): {
2872 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002873 PyObject *cond = POP();
2874 int err;
2875 if (cond == Py_False) {
2876 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002877 FAST_DISPATCH();
2878 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002879 if (cond == Py_True) {
2880 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 JUMPTO(oparg);
2882 FAST_DISPATCH();
2883 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002884 err = PyObject_IsTrue(cond);
2885 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002886 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002887 JUMPTO(oparg);
2888 }
2889 else if (err == 0)
2890 ;
2891 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002892 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002893 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002894 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002895
Benjamin Petersonddd19492018-09-16 22:38:02 -07002896 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002897 PyObject *cond = TOP();
2898 int err;
2899 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002900 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002901 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002902 FAST_DISPATCH();
2903 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002904 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 JUMPTO(oparg);
2906 FAST_DISPATCH();
2907 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002908 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002909 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002910 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002911 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002912 }
2913 else if (err == 0)
2914 JUMPTO(oparg);
2915 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002916 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002918 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002919
Benjamin Petersonddd19492018-09-16 22:38:02 -07002920 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002921 PyObject *cond = TOP();
2922 int err;
2923 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002924 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002925 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002926 FAST_DISPATCH();
2927 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002928 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002929 JUMPTO(oparg);
2930 FAST_DISPATCH();
2931 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002932 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002933 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002934 JUMPTO(oparg);
2935 }
2936 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002937 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002938 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002939 }
2940 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002941 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002942 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002943 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002944
Benjamin Petersonddd19492018-09-16 22:38:02 -07002945 case TARGET(JUMP_ABSOLUTE): {
2946 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002947 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002948#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002949 /* Enabling this path speeds-up all while and for-loops by bypassing
2950 the per-loop checks for signals. By default, this should be turned-off
2951 because it prevents detection of a control-break in tight loops like
2952 "while 1: pass". Compile with this option turned-on when you need
2953 the speed-up and do not need break checking inside tight loops (ones
2954 that contain only instructions ending with FAST_DISPATCH).
2955 */
2956 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002957#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002958 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002959#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002960 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002961
Benjamin Petersonddd19492018-09-16 22:38:02 -07002962 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002963 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002964 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002965 PyObject *iter = PyObject_GetIter(iterable);
2966 Py_DECREF(iterable);
2967 SET_TOP(iter);
2968 if (iter == NULL)
2969 goto error;
2970 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002971 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002972 DISPATCH();
2973 }
2974
Benjamin Petersonddd19492018-09-16 22:38:02 -07002975 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04002976 /* before: [obj]; after [getiter(obj)] */
2977 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002978 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002979 if (PyCoro_CheckExact(iterable)) {
2980 /* `iterable` is a coroutine */
2981 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2982 /* and it is used in a 'yield from' expression of a
2983 regular generator. */
2984 Py_DECREF(iterable);
2985 SET_TOP(NULL);
2986 PyErr_SetString(PyExc_TypeError,
2987 "cannot 'yield from' a coroutine object "
2988 "in a non-coroutine generator");
2989 goto error;
2990 }
2991 }
2992 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002993 /* `iterable` is not a generator. */
2994 iter = PyObject_GetIter(iterable);
2995 Py_DECREF(iterable);
2996 SET_TOP(iter);
2997 if (iter == NULL)
2998 goto error;
2999 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003000 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003001 DISPATCH();
3002 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003003
Benjamin Petersonddd19492018-09-16 22:38:02 -07003004 case TARGET(FOR_ITER): {
3005 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003006 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003007 PyObject *iter = TOP();
3008 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3009 if (next != NULL) {
3010 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003011 PREDICT(STORE_FAST);
3012 PREDICT(UNPACK_SEQUENCE);
3013 DISPATCH();
3014 }
3015 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003016 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
3017 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08003018 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003019 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003020 PyErr_Clear();
3021 }
3022 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003023 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003024 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003025 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003026 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003027 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003028 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003029
Benjamin Petersonddd19492018-09-16 22:38:02 -07003030 case TARGET(SETUP_FINALLY): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003031 /* NOTE: If you add any new block-setup opcodes that
3032 are not try/except/finally handlers, you may need
3033 to update the PyGen_NeedsFinalizing() function.
3034 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003035
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003036 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 STACK_LEVEL());
3038 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003039 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003040
Benjamin Petersonddd19492018-09-16 22:38:02 -07003041 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003042 _Py_IDENTIFIER(__aexit__);
3043 _Py_IDENTIFIER(__aenter__);
3044
3045 PyObject *mgr = TOP();
3046 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3047 *enter;
3048 PyObject *res;
3049 if (exit == NULL)
3050 goto error;
3051 SET_TOP(exit);
3052 enter = special_lookup(mgr, &PyId___aenter__);
3053 Py_DECREF(mgr);
3054 if (enter == NULL)
3055 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003056 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003057 Py_DECREF(enter);
3058 if (res == NULL)
3059 goto error;
3060 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003061 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003062 DISPATCH();
3063 }
3064
Benjamin Petersonddd19492018-09-16 22:38:02 -07003065 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003066 PyObject *res = POP();
3067 /* Setup the finally block before pushing the result
3068 of __aenter__ on the stack. */
3069 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3070 STACK_LEVEL());
3071 PUSH(res);
3072 DISPATCH();
3073 }
3074
Benjamin Petersonddd19492018-09-16 22:38:02 -07003075 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003076 _Py_IDENTIFIER(__exit__);
3077 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003078 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08003079 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003080 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08003081 if (enter == NULL)
3082 goto error;
3083 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003084 if (exit == NULL) {
3085 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003086 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003087 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003088 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003089 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003090 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003091 Py_DECREF(enter);
3092 if (res == NULL)
3093 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003094 /* Setup the finally block before pushing the result
3095 of __enter__ on the stack. */
3096 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3097 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003098
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003099 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003100 DISPATCH();
3101 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003102
Benjamin Petersonddd19492018-09-16 22:38:02 -07003103 case TARGET(WITH_CLEANUP_START): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003104 /* At the top of the stack are 1 or 6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003105 how/why we entered the finally clause:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003106 - TOP = NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003107 - (TOP, SECOND, THIRD) = exc_info()
3108 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003109 Below them is EXIT, the context.__exit__ or context.__aexit__
3110 bound method.
3111 In the first case, we must call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003112 EXIT(None, None, None)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003113 otherwise we must call
3114 EXIT(TOP, SECOND, THIRD)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003115
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003116 In the first case, we remove EXIT from the
3117 stack, leaving TOP, and push TOP on the stack.
3118 Otherwise we shift the bottom 3 values of the
3119 stack down, replace the empty spot with NULL, and push
3120 None on the stack.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003121
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003122 Finally we push the result of the call.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003123 */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003124 PyObject *stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003125 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003126 PyObject *exc, *val, *tb, *res;
3127
3128 val = tb = Py_None;
3129 exc = TOP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003130 if (exc == NULL) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003131 STACK_SHRINK(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003132 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003133 SET_TOP(exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003134 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003135 }
3136 else {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003137 assert(PyExceptionClass_Check(exc));
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003138 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003139 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003140 val = SECOND();
3141 tb = THIRD();
3142 tp2 = FOURTH();
3143 exc2 = PEEK(5);
3144 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003145 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003146 SET_VALUE(7, tb2);
3147 SET_VALUE(6, exc2);
3148 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003149 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3150 SET_FOURTH(NULL);
3151 /* We just shifted the stack down, so we have
3152 to tell the except handler block that the
3153 values are lower than it expects. */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003154 assert(f->f_iblock > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003155 block = &f->f_blockstack[f->f_iblock - 1];
3156 assert(block->b_type == EXCEPT_HANDLER);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003157 assert(block->b_level > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003158 block->b_level--;
3159 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003160
3161 stack[0] = exc;
3162 stack[1] = val;
3163 stack[2] = tb;
3164 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003165 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003166 if (res == NULL)
3167 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003168
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003169 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003170 PUSH(exc);
3171 PUSH(res);
3172 PREDICT(WITH_CLEANUP_FINISH);
3173 DISPATCH();
3174 }
3175
Benjamin Petersonddd19492018-09-16 22:38:02 -07003176 case TARGET(WITH_CLEANUP_FINISH): {
3177 PREDICTED(WITH_CLEANUP_FINISH);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003178 /* TOP = the result of calling the context.__exit__ bound method
3179 SECOND = either None or exception type
3180
3181 If SECOND is None below is NULL or the return address,
3182 otherwise below are 7 values representing an exception.
3183 */
Yury Selivanov75445082015-05-11 22:57:16 -04003184 PyObject *res = POP();
3185 PyObject *exc = POP();
3186 int err;
3187
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003188 if (exc != Py_None)
3189 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003190 else
3191 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003192
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003193 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003194 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003196 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003197 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003198 else if (err > 0) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003199 /* There was an exception and a True return.
3200 * We must manually unwind the EXCEPT_HANDLER block
3201 * which was created when the exception was caught,
Quan Tian3bd0d622018-10-20 05:30:03 +08003202 * otherwise the stack will be in an inconsistent state.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003203 */
3204 PyTryBlock *b = PyFrame_BlockPop(f);
3205 assert(b->b_type == EXCEPT_HANDLER);
3206 UNWIND_EXCEPT_HANDLER(b);
3207 PUSH(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003208 }
3209 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003210 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003211 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003212
Benjamin Petersonddd19492018-09-16 22:38:02 -07003213 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003214 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003215 PyObject *name = GETITEM(names, oparg);
3216 PyObject *obj = TOP();
3217 PyObject *meth = NULL;
3218
3219 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3220
Yury Selivanovf2392132016-12-13 19:03:51 -05003221 if (meth == NULL) {
3222 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003223 goto error;
3224 }
3225
3226 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003227 /* We can bypass temporary bound method object.
3228 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003229
INADA Naoki015bce62017-01-16 17:23:30 +09003230 meth | self | arg1 | ... | argN
3231 */
3232 SET_TOP(meth);
3233 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003234 }
3235 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003236 /* meth is not an unbound method (but a regular attr, or
3237 something was returned by a descriptor protocol). Set
3238 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003239 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003240
3241 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003242 */
INADA Naoki015bce62017-01-16 17:23:30 +09003243 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003244 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003245 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003246 }
3247 DISPATCH();
3248 }
3249
Benjamin Petersonddd19492018-09-16 22:38:02 -07003250 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003251 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003252 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003253
3254 sp = stack_pointer;
3255
INADA Naoki015bce62017-01-16 17:23:30 +09003256 meth = PEEK(oparg + 2);
3257 if (meth == NULL) {
3258 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3259 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003260
3261 Stack layout:
3262
INADA Naoki015bce62017-01-16 17:23:30 +09003263 ... | NULL | callable | arg1 | ... | argN
3264 ^- TOP()
3265 ^- (-oparg)
3266 ^- (-oparg-1)
3267 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003268
Ville Skyttä49b27342017-08-03 09:00:59 +03003269 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003270 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003271 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003272 res = call_function(&sp, oparg, NULL);
3273 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003274 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003275 }
3276 else {
3277 /* This is a method call. Stack layout:
3278
INADA Naoki015bce62017-01-16 17:23:30 +09003279 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003280 ^- TOP()
3281 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003282 ^- (-oparg-1)
3283 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003284
INADA Naoki015bce62017-01-16 17:23:30 +09003285 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003286 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003287 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003288 */
3289 res = call_function(&sp, oparg + 1, NULL);
3290 stack_pointer = sp;
3291 }
3292
3293 PUSH(res);
3294 if (res == NULL)
3295 goto error;
3296 DISPATCH();
3297 }
3298
Benjamin Petersonddd19492018-09-16 22:38:02 -07003299 case TARGET(CALL_FUNCTION): {
3300 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003301 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003302 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003303 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003304 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003305 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003306 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003307 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003308 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003309 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003310 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003311
Benjamin Petersonddd19492018-09-16 22:38:02 -07003312 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003313 PyObject **sp, *res, *names;
3314
3315 names = POP();
3316 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003317 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003318 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003319 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003320 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003321 Py_DECREF(names);
3322
3323 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003324 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003325 }
3326 DISPATCH();
3327 }
3328
Benjamin Petersonddd19492018-09-16 22:38:02 -07003329 case TARGET(CALL_FUNCTION_EX): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003330 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003331 if (oparg & 0x01) {
3332 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003333 if (!PyDict_CheckExact(kwargs)) {
3334 PyObject *d = PyDict_New();
3335 if (d == NULL)
3336 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003337 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003338 Py_DECREF(d);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003339 format_kwargs_error(SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003340 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003341 goto error;
3342 }
3343 Py_DECREF(kwargs);
3344 kwargs = d;
3345 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003346 assert(PyDict_CheckExact(kwargs));
3347 }
3348 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003349 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003350 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003351 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003352 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003353 goto error;
3354 }
3355 Py_SETREF(callargs, PySequence_Tuple(callargs));
3356 if (callargs == NULL) {
3357 goto error;
3358 }
3359 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003360 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003361
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003362 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003363 Py_DECREF(func);
3364 Py_DECREF(callargs);
3365 Py_XDECREF(kwargs);
3366
3367 SET_TOP(result);
3368 if (result == NULL) {
3369 goto error;
3370 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003371 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003372 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003373
Benjamin Petersonddd19492018-09-16 22:38:02 -07003374 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003375 PyObject *qualname = POP();
3376 PyObject *codeobj = POP();
3377 PyFunctionObject *func = (PyFunctionObject *)
3378 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003379
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003380 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003381 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003382 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003383 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003384 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003385
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003386 if (oparg & 0x08) {
3387 assert(PyTuple_CheckExact(TOP()));
3388 func ->func_closure = POP();
3389 }
3390 if (oparg & 0x04) {
3391 assert(PyDict_CheckExact(TOP()));
3392 func->func_annotations = POP();
3393 }
3394 if (oparg & 0x02) {
3395 assert(PyDict_CheckExact(TOP()));
3396 func->func_kwdefaults = POP();
3397 }
3398 if (oparg & 0x01) {
3399 assert(PyTuple_CheckExact(TOP()));
3400 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003401 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003402
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003403 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003404 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003405 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003406
Benjamin Petersonddd19492018-09-16 22:38:02 -07003407 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003408 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003409 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003410 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003411 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003412 step = NULL;
3413 stop = POP();
3414 start = TOP();
3415 slice = PySlice_New(start, stop, step);
3416 Py_DECREF(start);
3417 Py_DECREF(stop);
3418 Py_XDECREF(step);
3419 SET_TOP(slice);
3420 if (slice == NULL)
3421 goto error;
3422 DISPATCH();
3423 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003424
Benjamin Petersonddd19492018-09-16 22:38:02 -07003425 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003426 /* Handles f-string value formatting. */
3427 PyObject *result;
3428 PyObject *fmt_spec;
3429 PyObject *value;
3430 PyObject *(*conv_fn)(PyObject *);
3431 int which_conversion = oparg & FVC_MASK;
3432 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3433
3434 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003435 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003436
3437 /* See if any conversion is specified. */
3438 switch (which_conversion) {
3439 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;
3442
3443 /* Must be 0 (meaning no conversion), since only four
3444 values are allowed by (oparg & FVC_MASK). */
3445 default: conv_fn = NULL; break;
3446 }
3447
3448 /* If there's a conversion function, call it and replace
3449 value with that result. Otherwise, just use value,
3450 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003451 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003452 result = conv_fn(value);
3453 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003454 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003455 Py_XDECREF(fmt_spec);
3456 goto error;
3457 }
3458 value = result;
3459 }
3460
3461 /* If value is a unicode object, and there's no fmt_spec,
3462 then we know the result of format(value) is value
3463 itself. In that case, skip calling format(). I plan to
3464 move this optimization in to PyObject_Format()
3465 itself. */
3466 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3467 /* Do nothing, just transfer ownership to result. */
3468 result = value;
3469 } else {
3470 /* Actually call format(). */
3471 result = PyObject_Format(value, fmt_spec);
3472 Py_DECREF(value);
3473 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003474 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003475 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003476 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003477 }
3478
Eric V. Smith135d5f42016-02-05 18:23:08 -05003479 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003480 DISPATCH();
3481 }
3482
Benjamin Petersonddd19492018-09-16 22:38:02 -07003483 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003484 int oldoparg = oparg;
3485 NEXTOPARG();
3486 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003487 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003488 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003489
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003490
Antoine Pitrou042b1282010-08-13 21:15:58 +00003491#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003492 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003493#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003494 default:
3495 fprintf(stderr,
3496 "XXX lineno: %d, opcode: %d\n",
3497 PyFrame_GetLineNumber(f),
3498 opcode);
3499 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003500 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003502 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003503
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003504 /* This should never be reached. Every opcode should end with DISPATCH()
3505 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003506 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003507
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003508error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003509 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003510#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003511 if (!PyErr_Occurred())
3512 PyErr_SetString(PyExc_SystemError,
3513 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003514#else
3515 assert(PyErr_Occurred());
3516#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003517
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003518 /* Log traceback info. */
3519 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003520
Benjamin Peterson51f46162013-01-23 08:38:47 -05003521 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003522 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3523 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003524
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003525exception_unwind:
3526 /* Unwind stacks if an exception occurred */
3527 while (f->f_iblock > 0) {
3528 /* Pop the current block. */
3529 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003531 if (b->b_type == EXCEPT_HANDLER) {
3532 UNWIND_EXCEPT_HANDLER(b);
3533 continue;
3534 }
3535 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003536 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003537 PyObject *exc, *val, *tb;
3538 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003539 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003540 /* Beware, this invalidates all b->b_* fields */
3541 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003542 PUSH(exc_info->exc_traceback);
3543 PUSH(exc_info->exc_value);
3544 if (exc_info->exc_type != NULL) {
3545 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003546 }
3547 else {
3548 Py_INCREF(Py_None);
3549 PUSH(Py_None);
3550 }
3551 PyErr_Fetch(&exc, &val, &tb);
3552 /* Make the raw exception data
3553 available to the handler,
3554 so a program can emulate the
3555 Python main loop. */
3556 PyErr_NormalizeException(
3557 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003558 if (tb != NULL)
3559 PyException_SetTraceback(val, tb);
3560 else
3561 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003562 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003563 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003564 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003565 exc_info->exc_value = val;
3566 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003567 if (tb == NULL)
3568 tb = Py_None;
3569 Py_INCREF(tb);
3570 PUSH(tb);
3571 PUSH(val);
3572 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003573 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003574 /* Resume normal execution */
3575 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003576 }
3577 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003578
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003579 /* End the loop as we still have an error */
3580 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003581 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003583 /* Pop remaining stack entries. */
3584 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003585 PyObject *o = POP();
3586 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003587 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003588
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003589 assert(retval == NULL);
3590 assert(PyErr_Occurred());
Guido van Rossumac7be682001-01-17 15:42:30 +00003591
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003592return_or_yield:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003593 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003594 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003595 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3596 tstate, f, PyTrace_RETURN, retval)) {
3597 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003598 }
3599 }
3600 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003601 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3602 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003603 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003604 }
3605 }
3606 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003608 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003609exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003610 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3611 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003612 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003613 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003614 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003615
Victor Stinnerefde1462015-03-21 15:04:43 +01003616 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003617}
3618
Benjamin Petersonb204a422011-06-05 22:04:07 -05003619static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003620format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3621{
3622 int err;
3623 Py_ssize_t len = PyList_GET_SIZE(names);
3624 PyObject *name_str, *comma, *tail, *tmp;
3625
3626 assert(PyList_CheckExact(names));
3627 assert(len >= 1);
3628 /* Deal with the joys of natural language. */
3629 switch (len) {
3630 case 1:
3631 name_str = PyList_GET_ITEM(names, 0);
3632 Py_INCREF(name_str);
3633 break;
3634 case 2:
3635 name_str = PyUnicode_FromFormat("%U and %U",
3636 PyList_GET_ITEM(names, len - 2),
3637 PyList_GET_ITEM(names, len - 1));
3638 break;
3639 default:
3640 tail = PyUnicode_FromFormat(", %U, and %U",
3641 PyList_GET_ITEM(names, len - 2),
3642 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003643 if (tail == NULL)
3644 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003645 /* Chop off the last two objects in the list. This shouldn't actually
3646 fail, but we can't be too careful. */
3647 err = PyList_SetSlice(names, len - 2, len, NULL);
3648 if (err == -1) {
3649 Py_DECREF(tail);
3650 return;
3651 }
3652 /* Stitch everything up into a nice comma-separated list. */
3653 comma = PyUnicode_FromString(", ");
3654 if (comma == NULL) {
3655 Py_DECREF(tail);
3656 return;
3657 }
3658 tmp = PyUnicode_Join(comma, names);
3659 Py_DECREF(comma);
3660 if (tmp == NULL) {
3661 Py_DECREF(tail);
3662 return;
3663 }
3664 name_str = PyUnicode_Concat(tmp, tail);
3665 Py_DECREF(tmp);
3666 Py_DECREF(tail);
3667 break;
3668 }
3669 if (name_str == NULL)
3670 return;
3671 PyErr_Format(PyExc_TypeError,
3672 "%U() missing %i required %s argument%s: %U",
3673 co->co_name,
3674 len,
3675 kind,
3676 len == 1 ? "" : "s",
3677 name_str);
3678 Py_DECREF(name_str);
3679}
3680
3681static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003682missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003683 PyObject **fastlocals)
3684{
Victor Stinner74319ae2016-08-25 00:04:09 +02003685 Py_ssize_t i, j = 0;
3686 Py_ssize_t start, end;
3687 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003688 const char *kind = positional ? "positional" : "keyword-only";
3689 PyObject *missing_names;
3690
3691 /* Compute the names of the arguments that are missing. */
3692 missing_names = PyList_New(missing);
3693 if (missing_names == NULL)
3694 return;
3695 if (positional) {
3696 start = 0;
3697 end = co->co_argcount - defcount;
3698 }
3699 else {
3700 start = co->co_argcount;
3701 end = start + co->co_kwonlyargcount;
3702 }
3703 for (i = start; i < end; i++) {
3704 if (GETLOCAL(i) == NULL) {
3705 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3706 PyObject *name = PyObject_Repr(raw);
3707 if (name == NULL) {
3708 Py_DECREF(missing_names);
3709 return;
3710 }
3711 PyList_SET_ITEM(missing_names, j++, name);
3712 }
3713 }
3714 assert(j == missing);
3715 format_missing(kind, co, missing_names);
3716 Py_DECREF(missing_names);
3717}
3718
3719static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003720too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3721 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003722{
3723 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003724 Py_ssize_t kwonly_given = 0;
3725 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003726 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003727 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003728
Benjamin Petersone109c702011-06-24 09:37:26 -05003729 assert((co->co_flags & CO_VARARGS) == 0);
3730 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003731 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3732 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003733 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003734 }
3735 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003736 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003737 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003738 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003739 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003740 }
3741 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003742 plural = (co_argcount != 1);
3743 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003744 }
3745 if (sig == NULL)
3746 return;
3747 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003748 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3749 kwonly_sig = PyUnicode_FromFormat(format,
3750 given != 1 ? "s" : "",
3751 kwonly_given,
3752 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003753 if (kwonly_sig == NULL) {
3754 Py_DECREF(sig);
3755 return;
3756 }
3757 }
3758 else {
3759 /* This will not fail. */
3760 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003761 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003762 }
3763 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003764 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003765 co->co_name,
3766 sig,
3767 plural ? "s" : "",
3768 given,
3769 kwonly_sig,
3770 given == 1 && !kwonly_given ? "was" : "were");
3771 Py_DECREF(sig);
3772 Py_DECREF(kwonly_sig);
3773}
3774
Guido van Rossumc2e20742006-02-27 22:32:47 +00003775/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003776 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003777 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003778
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003779PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003780_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003781 PyObject *const *args, Py_ssize_t argcount,
3782 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003783 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003784 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003785 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003786 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003787{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003788 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003789 PyFrameObject *f;
3790 PyObject *retval = NULL;
3791 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003792 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003793 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003794 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3795 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003796 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003798 if (globals == NULL) {
3799 PyErr_SetString(PyExc_SystemError,
3800 "PyEval_EvalCodeEx: NULL globals");
3801 return NULL;
3802 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003803
Victor Stinnerc7020012016-08-16 23:40:29 +02003804 /* Create the frame */
Victor Stinner50b48572018-11-01 01:51:40 +01003805 tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003806 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003807 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003808 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003809 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003810 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003811 fastlocals = f->f_localsplus;
3812 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003813
Victor Stinnerc7020012016-08-16 23:40:29 +02003814 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003815 if (co->co_flags & CO_VARKEYWORDS) {
3816 kwdict = PyDict_New();
3817 if (kwdict == NULL)
3818 goto fail;
3819 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003820 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003821 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003822 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003823 SETLOCAL(i, kwdict);
3824 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003825 else {
3826 kwdict = NULL;
3827 }
3828
3829 /* Copy positional arguments into local variables */
3830 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003831 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003832 }
3833 else {
3834 n = argcount;
3835 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003836 for (i = 0; i < n; i++) {
3837 x = args[i];
3838 Py_INCREF(x);
3839 SETLOCAL(i, x);
3840 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003841
3842 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003843 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05003844 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003845 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003846 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003847 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003848 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003849 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003850
Serhiy Storchakab7281052016-09-12 00:52:40 +03003851 /* Handle keyword arguments passed as two strided arrays */
3852 kwcount *= kwstep;
3853 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003854 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003855 PyObject *keyword = kwnames[i];
3856 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003857 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003858
Benjamin Petersonb204a422011-06-05 22:04:07 -05003859 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3860 PyErr_Format(PyExc_TypeError,
3861 "%U() keywords must be strings",
3862 co->co_name);
3863 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003864 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003865
Benjamin Petersonb204a422011-06-05 22:04:07 -05003866 /* Speed hack: do raw pointer compares. As names are
3867 normally interned this should almost always hit. */
3868 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3869 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003870 PyObject *name = co_varnames[j];
3871 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003872 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003873 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003874 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003875
Benjamin Petersonb204a422011-06-05 22:04:07 -05003876 /* Slow fallback, just in case */
3877 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003878 PyObject *name = co_varnames[j];
3879 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3880 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003881 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003882 }
3883 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003884 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003885 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003886 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003887
Victor Stinner231d1f32017-01-11 02:12:06 +01003888 assert(j >= total_args);
3889 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003890 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003891 "%U() got an unexpected keyword argument '%S'",
3892 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003893 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003894 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003895
Christian Heimes0bd447f2013-07-20 14:48:10 +02003896 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3897 goto fail;
3898 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003899 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003900
Benjamin Petersonb204a422011-06-05 22:04:07 -05003901 kw_found:
3902 if (GETLOCAL(j) != NULL) {
3903 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003904 "%U() got multiple values for argument '%S'",
3905 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003906 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003907 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003908 Py_INCREF(value);
3909 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003910 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003911
3912 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003913 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003914 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003915 goto fail;
3916 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003917
3918 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003919 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02003920 Py_ssize_t m = co->co_argcount - defcount;
3921 Py_ssize_t missing = 0;
3922 for (i = argcount; i < m; i++) {
3923 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003924 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02003925 }
3926 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003927 if (missing) {
3928 missing_arguments(co, missing, defcount, fastlocals);
3929 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003930 }
3931 if (n > m)
3932 i = n - m;
3933 else
3934 i = 0;
3935 for (; i < defcount; i++) {
3936 if (GETLOCAL(m+i) == NULL) {
3937 PyObject *def = defs[i];
3938 Py_INCREF(def);
3939 SETLOCAL(m+i, def);
3940 }
3941 }
3942 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003943
3944 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003945 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02003946 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003947 for (i = co->co_argcount; i < total_args; i++) {
3948 PyObject *name;
3949 if (GETLOCAL(i) != NULL)
3950 continue;
3951 name = PyTuple_GET_ITEM(co->co_varnames, i);
3952 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003953 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003954 if (def) {
3955 Py_INCREF(def);
3956 SETLOCAL(i, def);
3957 continue;
3958 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003959 else if (PyErr_Occurred()) {
3960 goto fail;
3961 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003962 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003963 missing++;
3964 }
3965 if (missing) {
3966 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003967 goto fail;
3968 }
3969 }
3970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003971 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003972 vars into frame. */
3973 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003974 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02003975 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05003976 /* Possibly account for the cell variable being an argument. */
3977 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003978 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003979 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003980 /* Clear the local copy. */
3981 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003982 }
3983 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003984 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003985 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003986 if (c == NULL)
3987 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003988 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003989 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003990
3991 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05003992 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3993 PyObject *o = PyTuple_GET_ITEM(closure, i);
3994 Py_INCREF(o);
3995 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003996 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003997
Yury Selivanoveb636452016-09-08 22:01:51 -07003998 /* Handle generator/coroutine/asynchronous generator */
3999 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004000 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04004001 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004002 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004003
4004 if (is_coro && tstate->in_coroutine_wrapper) {
4005 assert(coro_wrapper != NULL);
4006 PyErr_Format(PyExc_RuntimeError,
4007 "coroutine wrapper %.200R attempted "
4008 "to recursively wrap %.200R",
4009 coro_wrapper,
4010 co);
4011 goto fail;
4012 }
Yury Selivanov75445082015-05-11 22:57:16 -04004013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004014 /* Don't need to keep the reference to f_back, it will be set
4015 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004016 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004018 /* Create a new generator that owns the ready to run frame
4019 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004020 if (is_coro) {
4021 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004022 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4023 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004024 } else {
4025 gen = PyGen_NewWithQualName(f, name, qualname);
4026 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004027 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004028 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004029 }
INADA Naoki9c157762016-12-26 18:52:46 +09004030
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004031 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004032
Yury Selivanov94c22632015-06-04 10:16:51 -04004033 if (is_coro && coro_wrapper != NULL) {
4034 PyObject *wrapped;
4035 tstate->in_coroutine_wrapper = 1;
4036 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4037 tstate->in_coroutine_wrapper = 0;
4038 return wrapped;
4039 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004040
Yury Selivanov75445082015-05-11 22:57:16 -04004041 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004042 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004043
Victor Stinner59a73272016-12-09 18:51:13 +01004044 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004045
Thomas Woutersce272b62007-09-19 21:19:28 +00004046fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004048 /* decref'ing the frame can cause __del__ methods to get invoked,
4049 which can call back into Python. While we're done with the
4050 current Python frame (f), the associated C stack is still in use,
4051 so recursion_depth must be boosted for the duration.
4052 */
4053 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09004054 if (Py_REFCNT(f) > 1) {
4055 Py_DECREF(f);
4056 _PyObject_GC_TRACK(f);
4057 }
4058 else {
4059 ++tstate->recursion_depth;
4060 Py_DECREF(f);
4061 --tstate->recursion_depth;
4062 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004063 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004064}
4065
Victor Stinner40ee3012014-06-16 15:59:28 +02004066PyObject *
4067PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004068 PyObject *const *args, int argcount,
4069 PyObject *const *kws, int kwcount,
4070 PyObject *const *defs, int defcount,
4071 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004072{
4073 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004074 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004075 kws, kws != NULL ? kws + 1 : NULL,
4076 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004077 defs, defcount,
4078 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004079 NULL, NULL);
4080}
Tim Peters5ca576e2001-06-18 22:08:13 +00004081
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004082static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004083special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004084{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004085 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004086 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004087 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004088 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004089 return NULL;
4090 }
4091 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004092}
4093
4094
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004095/* Logic for the raise statement (too complicated for inlining).
4096 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004097static int
Collin Winter828f04a2007-08-31 00:04:24 +00004098do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004099{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004100 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004102 if (exc == NULL) {
4103 /* Reraise */
Victor Stinner50b48572018-11-01 01:51:40 +01004104 PyThreadState *tstate = _PyThreadState_GET();
Mark Shannonae3087c2017-10-22 22:41:51 +01004105 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004106 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004107 type = exc_info->exc_type;
4108 value = exc_info->exc_value;
4109 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004110 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004111 PyErr_SetString(PyExc_RuntimeError,
4112 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004113 return 0;
4114 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004115 Py_XINCREF(type);
4116 Py_XINCREF(value);
4117 Py_XINCREF(tb);
4118 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004119 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004120 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004122 /* We support the following forms of raise:
4123 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004124 raise <instance>
4125 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004127 if (PyExceptionClass_Check(exc)) {
4128 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004129 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004130 if (value == NULL)
4131 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004132 if (!PyExceptionInstance_Check(value)) {
4133 PyErr_Format(PyExc_TypeError,
4134 "calling %R should have returned an instance of "
4135 "BaseException, not %R",
4136 type, Py_TYPE(value));
4137 goto raise_error;
4138 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004139 }
4140 else if (PyExceptionInstance_Check(exc)) {
4141 value = exc;
4142 type = PyExceptionInstance_Class(exc);
4143 Py_INCREF(type);
4144 }
4145 else {
4146 /* Not something you can raise. You get an exception
4147 anyway, just not what you specified :-) */
4148 Py_DECREF(exc);
4149 PyErr_SetString(PyExc_TypeError,
4150 "exceptions must derive from BaseException");
4151 goto raise_error;
4152 }
Collin Winter828f04a2007-08-31 00:04:24 +00004153
Serhiy Storchakac0191582016-09-27 11:37:10 +03004154 assert(type != NULL);
4155 assert(value != NULL);
4156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004157 if (cause) {
4158 PyObject *fixed_cause;
4159 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004160 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004161 if (fixed_cause == NULL)
4162 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004163 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004164 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004165 else if (PyExceptionInstance_Check(cause)) {
4166 fixed_cause = cause;
4167 }
4168 else if (cause == Py_None) {
4169 Py_DECREF(cause);
4170 fixed_cause = NULL;
4171 }
4172 else {
4173 PyErr_SetString(PyExc_TypeError,
4174 "exception causes must derive from "
4175 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004176 goto raise_error;
4177 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004178 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004179 }
Collin Winter828f04a2007-08-31 00:04:24 +00004180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004181 PyErr_SetObject(type, value);
4182 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004183 Py_DECREF(value);
4184 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004185 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004186
4187raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004188 Py_XDECREF(value);
4189 Py_XDECREF(type);
4190 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004191 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004192}
4193
Tim Petersd6d010b2001-06-21 02:49:55 +00004194/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004195 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004196
Guido van Rossum0368b722007-05-11 16:50:42 +00004197 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4198 with a variable target.
4199*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004200
Barry Warsawe42b18f1997-08-25 22:13:04 +00004201static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004202unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004204 int i = 0, j = 0;
4205 Py_ssize_t ll = 0;
4206 PyObject *it; /* iter(v) */
4207 PyObject *w;
4208 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004210 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004212 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004213 if (it == NULL) {
4214 if (PyErr_ExceptionMatches(PyExc_TypeError) &&
4215 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4216 {
4217 PyErr_Format(PyExc_TypeError,
4218 "cannot unpack non-iterable %.200s object",
4219 v->ob_type->tp_name);
4220 }
4221 return 0;
4222 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004224 for (; i < argcnt; i++) {
4225 w = PyIter_Next(it);
4226 if (w == NULL) {
4227 /* Iterator done, via error or exhaustion. */
4228 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004229 if (argcntafter == -1) {
4230 PyErr_Format(PyExc_ValueError,
4231 "not enough values to unpack (expected %d, got %d)",
4232 argcnt, i);
4233 }
4234 else {
4235 PyErr_Format(PyExc_ValueError,
4236 "not enough values to unpack "
4237 "(expected at least %d, got %d)",
4238 argcnt + argcntafter, i);
4239 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004240 }
4241 goto Error;
4242 }
4243 *--sp = w;
4244 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004246 if (argcntafter == -1) {
4247 /* We better have exhausted the iterator now. */
4248 w = PyIter_Next(it);
4249 if (w == NULL) {
4250 if (PyErr_Occurred())
4251 goto Error;
4252 Py_DECREF(it);
4253 return 1;
4254 }
4255 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004256 PyErr_Format(PyExc_ValueError,
4257 "too many values to unpack (expected %d)",
4258 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004259 goto Error;
4260 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004262 l = PySequence_List(it);
4263 if (l == NULL)
4264 goto Error;
4265 *--sp = l;
4266 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004268 ll = PyList_GET_SIZE(l);
4269 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004270 PyErr_Format(PyExc_ValueError,
4271 "not enough values to unpack (expected at least %d, got %zd)",
4272 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004273 goto Error;
4274 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004276 /* Pop the "after-variable" args off the list. */
4277 for (j = argcntafter; j > 0; j--, i++) {
4278 *--sp = PyList_GET_ITEM(l, ll - j);
4279 }
4280 /* Resize the list. */
4281 Py_SIZE(l) = ll - argcntafter;
4282 Py_DECREF(it);
4283 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004284
Tim Petersd6d010b2001-06-21 02:49:55 +00004285Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004286 for (; i > 0; i--, sp++)
4287 Py_DECREF(*sp);
4288 Py_XDECREF(it);
4289 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004290}
4291
4292
Guido van Rossum96a42c81992-01-12 02:29:51 +00004293#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004294static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004295prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004296{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004297 printf("%s ", str);
4298 if (PyObject_Print(v, stdout, 0) != 0)
4299 PyErr_Clear(); /* Don't know what else to do */
4300 printf("\n");
4301 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004302}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004303#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004304
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004305static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004306call_exc_trace(Py_tracefunc func, PyObject *self,
4307 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004308{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004309 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004310 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004311 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004312 if (value == NULL) {
4313 value = Py_None;
4314 Py_INCREF(value);
4315 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004316 PyErr_NormalizeException(&type, &value, &orig_traceback);
4317 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004318 arg = PyTuple_Pack(3, type, value, traceback);
4319 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004320 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004321 return;
4322 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004323 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004324 Py_DECREF(arg);
4325 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004326 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004327 else {
4328 Py_XDECREF(type);
4329 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004330 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004331 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004332}
4333
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004334static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004335call_trace_protected(Py_tracefunc func, PyObject *obj,
4336 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004337 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004338{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004339 PyObject *type, *value, *traceback;
4340 int err;
4341 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004342 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004343 if (err == 0)
4344 {
4345 PyErr_Restore(type, value, traceback);
4346 return 0;
4347 }
4348 else {
4349 Py_XDECREF(type);
4350 Py_XDECREF(value);
4351 Py_XDECREF(traceback);
4352 return -1;
4353 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004354}
4355
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004356static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004357call_trace(Py_tracefunc func, PyObject *obj,
4358 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004359 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004361 int result;
4362 if (tstate->tracing)
4363 return 0;
4364 tstate->tracing++;
4365 tstate->use_tracing = 0;
4366 result = func(obj, frame, what, arg);
4367 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4368 || (tstate->c_profilefunc != NULL));
4369 tstate->tracing--;
4370 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004371}
4372
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004373PyObject *
4374_PyEval_CallTracing(PyObject *func, PyObject *args)
4375{
Victor Stinner50b48572018-11-01 01:51:40 +01004376 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004377 int save_tracing = tstate->tracing;
4378 int save_use_tracing = tstate->use_tracing;
4379 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004381 tstate->tracing = 0;
4382 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4383 || (tstate->c_profilefunc != NULL));
4384 result = PyObject_Call(func, args, NULL);
4385 tstate->tracing = save_tracing;
4386 tstate->use_tracing = save_use_tracing;
4387 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004388}
4389
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004390/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004391static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004392maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004393 PyThreadState *tstate, PyFrameObject *frame,
4394 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004396 int result = 0;
4397 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004399 /* If the last instruction executed isn't in the current
4400 instruction window, reset the window.
4401 */
4402 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4403 PyAddrPair bounds;
4404 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4405 &bounds);
4406 *instr_lb = bounds.ap_lower;
4407 *instr_ub = bounds.ap_upper;
4408 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004409 /* If the last instruction falls at the start of a line or if it
4410 represents a jump backwards, update the frame's line number and
4411 then call the trace function if we're tracing source lines.
4412 */
4413 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004415 if (frame->f_trace_lines) {
4416 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4417 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004418 }
George King20faa682017-10-18 17:44:22 -07004419 /* Always emit an opcode event if we're tracing all opcodes. */
4420 if (frame->f_trace_opcodes) {
4421 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4422 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004423 *instr_prev = frame->f_lasti;
4424 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004425}
4426
Fred Drake5755ce62001-06-27 19:19:46 +00004427void
4428PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004429{
Victor Stinner50b48572018-11-01 01:51:40 +01004430 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004431 PyObject *temp = tstate->c_profileobj;
4432 Py_XINCREF(arg);
4433 tstate->c_profilefunc = NULL;
4434 tstate->c_profileobj = NULL;
4435 /* Must make sure that tracing is not ignored if 'temp' is freed */
4436 tstate->use_tracing = tstate->c_tracefunc != NULL;
4437 Py_XDECREF(temp);
4438 tstate->c_profilefunc = func;
4439 tstate->c_profileobj = arg;
4440 /* Flag that tracing or profiling is turned on */
4441 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004442}
4443
4444void
4445PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4446{
Victor Stinner50b48572018-11-01 01:51:40 +01004447 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004448 PyObject *temp = tstate->c_traceobj;
4449 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4450 Py_XINCREF(arg);
4451 tstate->c_tracefunc = NULL;
4452 tstate->c_traceobj = NULL;
4453 /* Must make sure that profiling is not ignored if 'temp' is freed */
4454 tstate->use_tracing = tstate->c_profilefunc != NULL;
4455 Py_XDECREF(temp);
4456 tstate->c_tracefunc = func;
4457 tstate->c_traceobj = arg;
4458 /* Flag that tracing or profiling is turned on */
4459 tstate->use_tracing = ((func != NULL)
4460 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004461}
4462
Yury Selivanov75445082015-05-11 22:57:16 -04004463void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004464_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4465{
4466 assert(new_depth >= 0);
Victor Stinner50b48572018-11-01 01:51:40 +01004467 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004468 tstate->coroutine_origin_tracking_depth = new_depth;
4469}
4470
4471int
4472_PyEval_GetCoroutineOriginTrackingDepth(void)
4473{
Victor Stinner50b48572018-11-01 01:51:40 +01004474 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004475 return tstate->coroutine_origin_tracking_depth;
4476}
4477
4478void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004479_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004480{
Victor Stinner50b48572018-11-01 01:51:40 +01004481 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004482
Yury Selivanov75445082015-05-11 22:57:16 -04004483 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004484 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004485}
4486
4487PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004488_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004489{
Victor Stinner50b48572018-11-01 01:51:40 +01004490 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004491 return tstate->coroutine_wrapper;
4492}
4493
Yury Selivanoveb636452016-09-08 22:01:51 -07004494void
4495_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4496{
Victor Stinner50b48572018-11-01 01:51:40 +01004497 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004498
4499 Py_XINCREF(firstiter);
4500 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4501}
4502
4503PyObject *
4504_PyEval_GetAsyncGenFirstiter(void)
4505{
Victor Stinner50b48572018-11-01 01:51:40 +01004506 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004507 return tstate->async_gen_firstiter;
4508}
4509
4510void
4511_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4512{
Victor Stinner50b48572018-11-01 01:51:40 +01004513 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004514
4515 Py_XINCREF(finalizer);
4516 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4517}
4518
4519PyObject *
4520_PyEval_GetAsyncGenFinalizer(void)
4521{
Victor Stinner50b48572018-11-01 01:51:40 +01004522 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004523 return tstate->async_gen_finalizer;
4524}
4525
Guido van Rossumb209a111997-04-29 18:18:01 +00004526PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004527PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004528{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004529 PyFrameObject *current_frame = PyEval_GetFrame();
4530 if (current_frame == NULL)
Victor Stinnercaba55b2018-08-03 15:33:52 +02004531 return _PyInterpreterState_GET_UNSAFE()->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004532 else
4533 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004534}
4535
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004536/* Convenience function to get a builtin from its name */
4537PyObject *
4538_PyEval_GetBuiltinId(_Py_Identifier *name)
4539{
4540 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4541 if (attr) {
4542 Py_INCREF(attr);
4543 }
4544 else if (!PyErr_Occurred()) {
4545 PyErr_SetObject(PyExc_AttributeError, _PyUnicode_FromId(name));
4546 }
4547 return attr;
4548}
4549
Guido van Rossumb209a111997-04-29 18:18:01 +00004550PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004551PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004553 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004554 if (current_frame == NULL) {
4555 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004556 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004557 }
4558
4559 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4560 return NULL;
4561
4562 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004563 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004564}
4565
Guido van Rossumb209a111997-04-29 18:18:01 +00004566PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004567PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004568{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004569 PyFrameObject *current_frame = PyEval_GetFrame();
4570 if (current_frame == NULL)
4571 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004572
4573 assert(current_frame->f_globals != NULL);
4574 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004575}
4576
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004577PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004578PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004579{
Victor Stinner50b48572018-11-01 01:51:40 +01004580 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004581 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004582}
4583
Guido van Rossum6135a871995-01-09 17:53:26 +00004584int
Tim Peters5ba58662001-07-16 02:29:45 +00004585PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004586{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004587 PyFrameObject *current_frame = PyEval_GetFrame();
4588 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004590 if (current_frame != NULL) {
4591 const int codeflags = current_frame->f_code->co_flags;
4592 const int compilerflags = codeflags & PyCF_MASK;
4593 if (compilerflags) {
4594 result = 1;
4595 cf->cf_flags |= compilerflags;
4596 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004597#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004598 if (codeflags & CO_GENERATOR_ALLOWED) {
4599 result = 1;
4600 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4601 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004602#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004603 }
4604 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004605}
4606
Guido van Rossum3f5da241990-12-20 15:06:42 +00004607
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004608const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004609PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004610{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004611 if (PyMethod_Check(func))
4612 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4613 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004614 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004615 else if (PyCFunction_Check(func))
4616 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4617 else
4618 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004619}
4620
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004621const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004622PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004623{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004624 if (PyMethod_Check(func))
4625 return "()";
4626 else if (PyFunction_Check(func))
4627 return "()";
4628 else if (PyCFunction_Check(func))
4629 return "()";
4630 else
4631 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004632}
4633
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004634#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004635if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004636 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4637 tstate, tstate->frame, \
4638 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004639 x = NULL; \
4640 } \
4641 else { \
4642 x = call; \
4643 if (tstate->c_profilefunc != NULL) { \
4644 if (x == NULL) { \
4645 call_trace_protected(tstate->c_profilefunc, \
4646 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004647 tstate, tstate->frame, \
4648 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004649 /* XXX should pass (type, value, tb) */ \
4650 } else { \
4651 if (call_trace(tstate->c_profilefunc, \
4652 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004653 tstate, tstate->frame, \
4654 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004655 Py_DECREF(x); \
4656 x = NULL; \
4657 } \
4658 } \
4659 } \
4660 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004661} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004662 x = call; \
4663 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004664
Victor Stinner415c5102017-01-11 00:54:57 +01004665/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4666 to reduce the stack consumption. */
4667Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004668call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004669{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004670 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004671 PyObject *func = *pfunc;
4672 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004673 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4674 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004675 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004677 /* Always dispatch PyCFunction first, because these are
4678 presumed to be the most frequent callable object.
4679 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004680 if (PyCFunction_Check(func)) {
Victor Stinner50b48572018-11-01 01:51:40 +01004681 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004682 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004683 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004684 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
Victor Stinner50b48572018-11-01 01:51:40 +01004685 PyThreadState *tstate = _PyThreadState_GET();
jdemeyer56868f92018-07-21 10:30:59 +02004686 if (nargs > 0 && tstate->use_tracing) {
4687 /* We need to create a temporary bound method as argument
4688 for profiling.
4689
4690 If nargs == 0, then this cannot work because we have no
4691 "self". In any case, the call itself would raise
4692 TypeError (foo needs an argument), so we just skip
4693 profiling. */
4694 PyObject *self = stack[0];
4695 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
jdemeyer147d9552018-07-23 18:41:20 +02004696 if (func != NULL) {
4697 C_TRACE(x, _PyCFunction_FastCallKeywords(func,
4698 stack+1, nargs-1,
4699 kwnames));
4700 Py_DECREF(func);
INADA Naoki93fac8d2017-03-07 14:24:37 +09004701 }
jdemeyer147d9552018-07-23 18:41:20 +02004702 else {
4703 x = NULL;
4704 }
INADA Naoki93fac8d2017-03-07 14:24:37 +09004705 }
4706 else {
4707 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4708 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004709 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004710 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004711 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004712 /* Optimize access to bound methods. Reuse the Python stack
4713 to pass 'self' as the first argument, replace 'func'
4714 with 'self'. It avoids the creation of a new temporary tuple
4715 for arguments (to replace func with self) when the method uses
4716 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004717 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004718 Py_INCREF(self);
4719 func = PyMethod_GET_FUNCTION(func);
4720 Py_INCREF(func);
4721 Py_SETREF(*pfunc, self);
4722 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004723 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004724 }
4725 else {
4726 Py_INCREF(func);
4727 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004728
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004729 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004730 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004731 }
4732 else {
4733 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4734 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004735 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004736 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004737
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004738 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4739
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004740 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004741 while ((*pp_stack) > pfunc) {
4742 w = EXT_POP(*pp_stack);
4743 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004744 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004746 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004747}
4748
Jeremy Hylton52820442001-01-03 23:52:36 +00004749static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004750do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004751{
jdemeyere89de732018-09-19 12:06:20 +02004752 PyObject *result;
4753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004754 if (PyCFunction_Check(func)) {
Victor Stinner50b48572018-11-01 01:51:40 +01004755 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004756 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004757 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004758 }
jdemeyere89de732018-09-19 12:06:20 +02004759 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
Victor Stinner50b48572018-11-01 01:51:40 +01004760 PyThreadState *tstate = _PyThreadState_GET();
jdemeyere89de732018-09-19 12:06:20 +02004761 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4762 if (nargs > 0 && tstate->use_tracing) {
4763 /* We need to create a temporary bound method as argument
4764 for profiling.
4765
4766 If nargs == 0, then this cannot work because we have no
4767 "self". In any case, the call itself would raise
4768 TypeError (foo needs an argument), so we just skip
4769 profiling. */
4770 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4771 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4772 if (func == NULL) {
4773 return NULL;
4774 }
4775
4776 C_TRACE(result, _PyCFunction_FastCallDict(func,
Victor Stinnerd17a6932018-11-09 16:56:48 +01004777 &_PyTuple_ITEMS(callargs)[1],
jdemeyere89de732018-09-19 12:06:20 +02004778 nargs - 1,
4779 kwdict));
4780 Py_DECREF(func);
4781 return result;
4782 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004783 }
jdemeyere89de732018-09-19 12:06:20 +02004784 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004785}
4786
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004787/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004788 nb_index slot defined, and store in *pi.
4789 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004790 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004791 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004792*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004793int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004794_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004795{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004796 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004797 Py_ssize_t x;
4798 if (PyIndex_Check(v)) {
4799 x = PyNumber_AsSsize_t(v, NULL);
4800 if (x == -1 && PyErr_Occurred())
4801 return 0;
4802 }
4803 else {
4804 PyErr_SetString(PyExc_TypeError,
4805 "slice indices must be integers or "
4806 "None or have an __index__ method");
4807 return 0;
4808 }
4809 *pi = x;
4810 }
4811 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004812}
4813
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004814int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004815_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004816{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004817 Py_ssize_t x;
4818 if (PyIndex_Check(v)) {
4819 x = PyNumber_AsSsize_t(v, NULL);
4820 if (x == -1 && PyErr_Occurred())
4821 return 0;
4822 }
4823 else {
4824 PyErr_SetString(PyExc_TypeError,
4825 "slice indices must be integers or "
4826 "have an __index__ method");
4827 return 0;
4828 }
4829 *pi = x;
4830 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004831}
4832
4833
Guido van Rossum486364b2007-06-30 05:01:58 +00004834#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004835 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004836
Guido van Rossumb209a111997-04-29 18:18:01 +00004837static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004838cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004839{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004840 int res = 0;
4841 switch (op) {
4842 case PyCmp_IS:
4843 res = (v == w);
4844 break;
4845 case PyCmp_IS_NOT:
4846 res = (v != w);
4847 break;
4848 case PyCmp_IN:
4849 res = PySequence_Contains(w, v);
4850 if (res < 0)
4851 return NULL;
4852 break;
4853 case PyCmp_NOT_IN:
4854 res = PySequence_Contains(w, v);
4855 if (res < 0)
4856 return NULL;
4857 res = !res;
4858 break;
4859 case PyCmp_EXC_MATCH:
4860 if (PyTuple_Check(w)) {
4861 Py_ssize_t i, length;
4862 length = PyTuple_Size(w);
4863 for (i = 0; i < length; i += 1) {
4864 PyObject *exc = PyTuple_GET_ITEM(w, i);
4865 if (!PyExceptionClass_Check(exc)) {
4866 PyErr_SetString(PyExc_TypeError,
4867 CANNOT_CATCH_MSG);
4868 return NULL;
4869 }
4870 }
4871 }
4872 else {
4873 if (!PyExceptionClass_Check(w)) {
4874 PyErr_SetString(PyExc_TypeError,
4875 CANNOT_CATCH_MSG);
4876 return NULL;
4877 }
4878 }
4879 res = PyErr_GivenExceptionMatches(v, w);
4880 break;
4881 default:
4882 return PyObject_RichCompare(v, w, op);
4883 }
4884 v = res ? Py_True : Py_False;
4885 Py_INCREF(v);
4886 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004887}
4888
Thomas Wouters52152252000-08-17 22:55:00 +00004889static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004890import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4891{
4892 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004893 PyObject *import_func, *res;
4894 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004895
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004896 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004897 if (import_func == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004898 if (!PyErr_Occurred()) {
4899 PyErr_SetString(PyExc_ImportError, "__import__ not found");
4900 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004901 return NULL;
4902 }
4903
4904 /* Fast path for not overloaded __import__. */
Victor Stinnercaba55b2018-08-03 15:33:52 +02004905 if (import_func == _PyInterpreterState_GET_UNSAFE()->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004906 int ilevel = _PyLong_AsInt(level);
4907 if (ilevel == -1 && PyErr_Occurred()) {
4908 return NULL;
4909 }
4910 res = PyImport_ImportModuleLevelObject(
4911 name,
4912 f->f_globals,
4913 f->f_locals == NULL ? Py_None : f->f_locals,
4914 fromlist,
4915 ilevel);
4916 return res;
4917 }
4918
4919 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004920
4921 stack[0] = name;
4922 stack[1] = f->f_globals;
4923 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
4924 stack[3] = fromlist;
4925 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02004926 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004927 Py_DECREF(import_func);
4928 return res;
4929}
4930
4931static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004932import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004933{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004934 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02004935 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08004936 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004937
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004938 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02004939 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004940 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004941 /* Issue #17636: in case this failed because of a circular relative
4942 import, try to fallback on reading the module directly from
4943 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02004944 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07004945 if (pkgname == NULL) {
4946 goto error;
4947 }
Oren Milman6db70332017-09-19 14:23:01 +03004948 if (!PyUnicode_Check(pkgname)) {
4949 Py_CLEAR(pkgname);
4950 goto error;
4951 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004952 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07004953 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08004954 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004955 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07004956 }
Eric Snow3f9eee62017-09-15 16:35:20 -06004957 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004958 Py_DECREF(fullmodname);
Stefan Krah027b09c2019-03-25 21:50:58 +01004959 if (x == NULL && !PyErr_Occurred()) {
Brett Cannon3008bc02015-08-11 18:01:31 -07004960 goto error;
4961 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004962 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004963 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07004964 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004965 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004966 if (pkgname == NULL) {
4967 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
4968 if (pkgname_or_unknown == NULL) {
4969 Py_XDECREF(pkgpath);
4970 return NULL;
4971 }
4972 } else {
4973 pkgname_or_unknown = pkgname;
4974 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004975
4976 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
4977 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08004978 errmsg = PyUnicode_FromFormat(
4979 "cannot import name %R from %R (unknown location)",
4980 name, pkgname_or_unknown
4981 );
Stefan Krah027b09c2019-03-25 21:50:58 +01004982 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08004983 PyErr_SetImportError(errmsg, pkgname, NULL);
4984 }
4985 else {
4986 errmsg = PyUnicode_FromFormat(
4987 "cannot import name %R from %R (%S)",
4988 name, pkgname_or_unknown, pkgpath
4989 );
Stefan Krah027b09c2019-03-25 21:50:58 +01004990 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08004991 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004992 }
4993
Xiang Zhang4830f582017-03-21 11:13:42 +08004994 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004995 Py_XDECREF(pkgname_or_unknown);
4996 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07004997 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00004998}
Guido van Rossumac7be682001-01-17 15:42:30 +00004999
Thomas Wouters52152252000-08-17 22:55:00 +00005000static int
5001import_all_from(PyObject *locals, PyObject *v)
5002{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005003 _Py_IDENTIFIER(__all__);
5004 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005005 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005006 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005007 int skip_leading_underscores = 0;
5008 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005009
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005010 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5011 return -1; /* Unexpected error */
5012 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005013 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005014 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5015 return -1;
5016 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005017 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005018 PyErr_SetString(PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005019 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005020 return -1;
5021 }
5022 all = PyMapping_Keys(dict);
5023 Py_DECREF(dict);
5024 if (all == NULL)
5025 return -1;
5026 skip_leading_underscores = 1;
5027 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005029 for (pos = 0, err = 0; ; pos++) {
5030 name = PySequence_GetItem(all, pos);
5031 if (name == NULL) {
5032 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5033 err = -1;
5034 else
5035 PyErr_Clear();
5036 break;
5037 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005038 if (!PyUnicode_Check(name)) {
5039 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5040 if (modname == NULL) {
5041 Py_DECREF(name);
5042 err = -1;
5043 break;
5044 }
5045 if (!PyUnicode_Check(modname)) {
5046 PyErr_Format(PyExc_TypeError,
5047 "module __name__ must be a string, not %.100s",
5048 Py_TYPE(modname)->tp_name);
5049 }
5050 else {
5051 PyErr_Format(PyExc_TypeError,
5052 "%s in %U.%s must be str, not %.100s",
5053 skip_leading_underscores ? "Key" : "Item",
5054 modname,
5055 skip_leading_underscores ? "__dict__" : "__all__",
5056 Py_TYPE(name)->tp_name);
5057 }
5058 Py_DECREF(modname);
5059 Py_DECREF(name);
5060 err = -1;
5061 break;
5062 }
5063 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005064 if (PyUnicode_READY(name) == -1) {
5065 Py_DECREF(name);
5066 err = -1;
5067 break;
5068 }
5069 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5070 Py_DECREF(name);
5071 continue;
5072 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005073 }
5074 value = PyObject_GetAttr(v, name);
5075 if (value == NULL)
5076 err = -1;
5077 else if (PyDict_CheckExact(locals))
5078 err = PyDict_SetItem(locals, name, value);
5079 else
5080 err = PyObject_SetItem(locals, name, value);
5081 Py_DECREF(name);
5082 Py_XDECREF(value);
5083 if (err != 0)
5084 break;
5085 }
5086 Py_DECREF(all);
5087 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005088}
5089
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005090static int
5091check_args_iterable(PyObject *func, PyObject *args)
5092{
5093 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
5094 PyErr_Format(PyExc_TypeError,
5095 "%.200s%.200s argument after * "
5096 "must be an iterable, not %.200s",
5097 PyEval_GetFuncName(func),
5098 PyEval_GetFuncDesc(func),
5099 args->ob_type->tp_name);
5100 return -1;
5101 }
5102 return 0;
5103}
5104
5105static void
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005106format_kwargs_error(PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005107{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005108 /* _PyDict_MergeEx raises attribute
5109 * error (percolated from an attempt
5110 * to get 'keys' attribute) instead of
5111 * a type error if its second argument
5112 * is not a mapping.
5113 */
5114 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
5115 PyErr_Format(PyExc_TypeError,
5116 "%.200s%.200s argument after ** "
5117 "must be a mapping, not %.200s",
5118 PyEval_GetFuncName(func),
5119 PyEval_GetFuncDesc(func),
5120 kwargs->ob_type->tp_name);
5121 }
5122 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
5123 PyObject *exc, *val, *tb;
5124 PyErr_Fetch(&exc, &val, &tb);
5125 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
5126 PyObject *key = PyTuple_GET_ITEM(val, 0);
5127 if (!PyUnicode_Check(key)) {
5128 PyErr_Format(PyExc_TypeError,
5129 "%.200s%.200s keywords must be strings",
5130 PyEval_GetFuncName(func),
5131 PyEval_GetFuncDesc(func));
5132 } else {
5133 PyErr_Format(PyExc_TypeError,
5134 "%.200s%.200s got multiple "
5135 "values for keyword argument '%U'",
5136 PyEval_GetFuncName(func),
5137 PyEval_GetFuncDesc(func),
5138 key);
5139 }
5140 Py_XDECREF(exc);
5141 Py_XDECREF(val);
5142 Py_XDECREF(tb);
5143 }
5144 else {
5145 PyErr_Restore(exc, val, tb);
5146 }
5147 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005148}
5149
Guido van Rossumac7be682001-01-17 15:42:30 +00005150static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005151format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005152{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005153 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005155 if (!obj)
5156 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005157
Serhiy Storchaka06515832016-11-20 09:13:07 +02005158 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005159 if (!obj_str)
5160 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005162 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005163}
Guido van Rossum950361c1997-01-24 13:49:28 +00005164
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005165static void
5166format_exc_unbound(PyCodeObject *co, int oparg)
5167{
5168 PyObject *name;
5169 /* Don't stomp existing exception */
5170 if (PyErr_Occurred())
5171 return;
5172 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5173 name = PyTuple_GET_ITEM(co->co_cellvars,
5174 oparg);
5175 format_exc_check_arg(
5176 PyExc_UnboundLocalError,
5177 UNBOUNDLOCAL_ERROR_MSG,
5178 name);
5179 } else {
5180 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5181 PyTuple_GET_SIZE(co->co_cellvars));
5182 format_exc_check_arg(PyExc_NameError,
5183 UNBOUNDFREE_ERROR_MSG, name);
5184 }
5185}
5186
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005187static void
5188format_awaitable_error(PyTypeObject *type, int prevopcode)
5189{
5190 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5191 if (prevopcode == BEFORE_ASYNC_WITH) {
5192 PyErr_Format(PyExc_TypeError,
5193 "'async with' received an object from __aenter__ "
5194 "that does not implement __await__: %.100s",
5195 type->tp_name);
5196 }
5197 else if (prevopcode == WITH_CLEANUP_START) {
5198 PyErr_Format(PyExc_TypeError,
5199 "'async with' received an object from __aexit__ "
5200 "that does not implement __await__: %.100s",
5201 type->tp_name);
5202 }
5203 }
5204}
5205
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005206static PyObject *
5207unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005208 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005209{
5210 PyObject *res;
5211 if (Py_REFCNT(v) == 2) {
5212 /* In the common case, there are 2 references to the value
5213 * stored in 'variable' when the += is performed: one on the
5214 * value stack (in 'v') and one still stored in the
5215 * 'variable'. We try to delete the variable now to reduce
5216 * the refcnt to 1.
5217 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005218 int opcode, oparg;
5219 NEXTOPARG();
5220 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005221 case STORE_FAST:
5222 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005223 PyObject **fastlocals = f->f_localsplus;
5224 if (GETLOCAL(oparg) == v)
5225 SETLOCAL(oparg, NULL);
5226 break;
5227 }
5228 case STORE_DEREF:
5229 {
5230 PyObject **freevars = (f->f_localsplus +
5231 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005232 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005233 if (PyCell_GET(c) == v) {
5234 PyCell_SET(c, NULL);
5235 Py_DECREF(v);
5236 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005237 break;
5238 }
5239 case STORE_NAME:
5240 {
5241 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005242 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005243 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005244 if (locals && PyDict_CheckExact(locals)) {
5245 PyObject *w = PyDict_GetItemWithError(locals, name);
5246 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
5247 (w == NULL && PyErr_Occurred()))
5248 {
5249 Py_DECREF(v);
5250 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005251 }
5252 }
5253 break;
5254 }
5255 }
5256 }
5257 res = v;
5258 PyUnicode_Append(&res, w);
5259 return res;
5260}
5261
Guido van Rossum950361c1997-01-24 13:49:28 +00005262#ifdef DYNAMIC_EXECUTION_PROFILE
5263
Skip Montanarof118cb12001-10-15 20:51:38 +00005264static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005265getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005266{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005267 int i;
5268 PyObject *l = PyList_New(256);
5269 if (l == NULL) return NULL;
5270 for (i = 0; i < 256; i++) {
5271 PyObject *x = PyLong_FromLong(a[i]);
5272 if (x == NULL) {
5273 Py_DECREF(l);
5274 return NULL;
5275 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005276 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005277 }
5278 for (i = 0; i < 256; i++)
5279 a[i] = 0;
5280 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005281}
5282
5283PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005284_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005285{
5286#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005287 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005288#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005289 int i;
5290 PyObject *l = PyList_New(257);
5291 if (l == NULL) return NULL;
5292 for (i = 0; i < 257; i++) {
5293 PyObject *x = getarray(dxpairs[i]);
5294 if (x == NULL) {
5295 Py_DECREF(l);
5296 return NULL;
5297 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005298 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005299 }
5300 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005301#endif
5302}
5303
5304#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005305
5306Py_ssize_t
5307_PyEval_RequestCodeExtraIndex(freefunc free)
5308{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005309 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005310 Py_ssize_t new_index;
5311
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005312 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005313 return -1;
5314 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005315 new_index = interp->co_extra_user_count++;
5316 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005317 return new_index;
5318}
Łukasz Langaa785c872016-09-09 17:37:37 -07005319
5320static void
5321dtrace_function_entry(PyFrameObject *f)
5322{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005323 const char *filename;
5324 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005325 int lineno;
5326
5327 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5328 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5329 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5330
5331 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5332}
5333
5334static void
5335dtrace_function_return(PyFrameObject *f)
5336{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005337 const char *filename;
5338 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005339 int lineno;
5340
5341 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5342 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5343 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5344
5345 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5346}
5347
5348/* DTrace equivalent of maybe_call_line_trace. */
5349static void
5350maybe_dtrace_line(PyFrameObject *frame,
5351 int *instr_lb, int *instr_ub, int *instr_prev)
5352{
5353 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005354 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005355
5356 /* If the last instruction executed isn't in the current
5357 instruction window, reset the window.
5358 */
5359 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5360 PyAddrPair bounds;
5361 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5362 &bounds);
5363 *instr_lb = bounds.ap_lower;
5364 *instr_ub = bounds.ap_upper;
5365 }
5366 /* If the last instruction falls at the start of a line or if
5367 it represents a jump backwards, update the frame's line
5368 number and call the trace function. */
5369 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5370 frame->f_lineno = line;
5371 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5372 if (!co_filename)
5373 co_filename = "?";
5374 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5375 if (!co_name)
5376 co_name = "?";
5377 PyDTrace_LINE(co_filename, co_name, line);
5378 }
5379 *instr_prev = frame->f_lasti;
5380}