blob: 4e43df2713d801d0e2f89be9799b41d64ff6f725 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +010013#include "pycore_object.h"
Victor Stinner621cebe2018-11-12 16:53:38 +010014#include "pycore_pystate.h"
Victor Stinnerec13b932018-11-25 23:56:17 +010015#include "pycore_tupleobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000016
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000017#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040018#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000019#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000020#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070021#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040022#include "setobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000023#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000024
Guido van Rossumc6004111993-11-05 10:22:19 +000025#include <ctype.h>
26
Guido van Rossum408027e1996-12-30 16:17:54 +000027#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000028/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029#define LLTRACE 1 /* Low-level trace feature */
30#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000031#endif
32
Victor Stinner5c75f372019-04-17 23:02:26 +020033#if !defined(Py_BUILD_CORE)
34# error "ceval.c must be build with Py_BUILD_CORE define for best performance"
35#endif
36
Yury Selivanovf2392132016-12-13 19:03:51 -050037/* Private API for the LOAD_METHOD opcode. */
38extern int _PyObject_GetMethod(PyObject *, PyObject *, PyObject **);
39
Jeremy Hylton52820442001-01-03 23:52:36 +000040typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000041
Guido van Rossum374a9221991-04-04 10:40:29 +000042/* Forward declarations */
Eric Snow2ebc5ce2017-09-07 23:51:28 -060043Py_LOCAL_INLINE(PyObject *) call_function(PyObject ***, Py_ssize_t,
44 PyObject *);
Victor Stinnerf9b760f2016-09-09 10:17:08 -070045static PyObject * do_call_core(PyObject *, PyObject *, PyObject *);
Jeremy Hylton52820442001-01-03 23:52:36 +000046
Guido van Rossum0a066c01992-03-27 17:29:15 +000047#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000048static int lltrace;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020049static int prtrace(PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000050#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010051static int call_trace(Py_tracefunc, PyObject *,
52 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000054static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010055 PyThreadState *, PyFrameObject *,
56 int, PyObject *);
57static void call_exc_trace(Py_tracefunc, PyObject *,
58 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000059static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060060 PyThreadState *, PyFrameObject *,
61 int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070062static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
63static void dtrace_function_entry(PyFrameObject *);
64static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000065
Thomas Wouters477c8d52006-05-27 19:21:47 +000066static PyObject * cmp_outcome(int, PyObject *, PyObject *);
Eric Snow2ebc5ce2017-09-07 23:51:28 -060067static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *,
68 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +000069static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +000070static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +000071static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +000072static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +020073static PyObject * unicode_concatenate(PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030074 PyFrameObject *, const _Py_CODEUNIT *);
Benjamin Petersonce798522012-01-22 11:24:29 -050075static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Serhiy Storchaka25e4f772017-08-03 11:37:15 +030076static int check_args_iterable(PyObject *func, PyObject *vararg);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +020077static void format_kwargs_error(PyObject *func, PyObject *kwargs);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +030078static void format_awaitable_error(PyTypeObject *, int);
Joannah Nanjekyef781d202019-04-29 04:38:45 -040079static inline void exit_thread_if_finalizing(PyThreadState *);
Guido van Rossum374a9221991-04-04 10:40:29 +000080
Paul Prescode68140d2000-08-30 20:25:01 +000081#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000083#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000085#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 "free variable '%.200s' referenced before assignment" \
87 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000088
Guido van Rossum950361c1997-01-24 13:49:28 +000089/* Dynamic execution profile */
90#ifdef DYNAMIC_EXECUTION_PROFILE
91#ifdef DXPAIRS
92static long dxpairs[257][256];
93#define dxp dxpairs[256]
94#else
95static long dxp[256];
96#endif
97#endif
98
Eric Snow2ebc5ce2017-09-07 23:51:28 -060099#define GIL_REQUEST _Py_atomic_load_relaxed(&_PyRuntime.ceval.gil_drop_request)
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000100
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000101/* This can set eval_breaker to 0 even though gil_drop_request became
102 1. We believe this is all right because the eval loop will release
103 the GIL eventually anyway. */
Eric Snowb75b1a352019-04-12 10:20:10 -0600104#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 _Py_atomic_store_relaxed( \
Eric Snowb75b1a352019-04-12 10:20:10 -0600106 &_PyRuntime.ceval.eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000107 GIL_REQUEST | \
Eric Snowfdf282d2019-01-11 14:26:55 -0700108 _Py_atomic_load_relaxed(&_PyRuntime.ceval.signals_pending) | \
Eric Snowb75b1a352019-04-12 10:20:10 -0600109 _Py_atomic_load_relaxed(&_PyRuntime.ceval.pending.calls_to_do) | \
110 _PyRuntime.ceval.pending.async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000111
Eric Snowb75b1a352019-04-12 10:20:10 -0600112#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600114 _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 1); \
Eric Snowb75b1a352019-04-12 10:20:10 -0600115 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000117
Eric Snowb75b1a352019-04-12 10:20:10 -0600118#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000119 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600120 _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 0); \
Eric Snowb75b1a352019-04-12 10:20:10 -0600121 COMPUTE_EVAL_BREAKER(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000123
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000124/* Pending calls are only modified under pending_lock */
Eric Snowb75b1a352019-04-12 10:20:10 -0600125#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 do { \
Eric Snowb75b1a352019-04-12 10:20:10 -0600127 _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 1); \
128 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000130
Eric Snowb75b1a352019-04-12 10:20:10 -0600131#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 do { \
Eric Snowb75b1a352019-04-12 10:20:10 -0600133 _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 0); \
134 COMPUTE_EVAL_BREAKER(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000136
Eric Snowfdf282d2019-01-11 14:26:55 -0700137#define SIGNAL_PENDING_SIGNALS() \
138 do { \
139 _Py_atomic_store_relaxed(&_PyRuntime.ceval.signals_pending, 1); \
Eric Snowb75b1a352019-04-12 10:20:10 -0600140 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700141 } while (0)
142
143#define UNSIGNAL_PENDING_SIGNALS() \
144 do { \
145 _Py_atomic_store_relaxed(&_PyRuntime.ceval.signals_pending, 0); \
Eric Snowb75b1a352019-04-12 10:20:10 -0600146 COMPUTE_EVAL_BREAKER(); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700147 } while (0)
148
Eric Snowb75b1a352019-04-12 10:20:10 -0600149#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 do { \
Eric Snowb75b1a352019-04-12 10:20:10 -0600151 _PyRuntime.ceval.pending.async_exc = 1; \
152 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000154
Eric Snowb75b1a352019-04-12 10:20:10 -0600155#define UNSIGNAL_ASYNC_EXC() \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600156 do { \
Eric Snowb75b1a352019-04-12 10:20:10 -0600157 _PyRuntime.ceval.pending.async_exc = 0; \
158 COMPUTE_EVAL_BREAKER(); \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600159 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000160
161
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000162#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000163#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000164#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000165#include "pythread.h"
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000166#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000167
Tim Peters7f468f22004-10-11 02:40:51 +0000168int
169PyEval_ThreadsInitialized(void)
170{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000172}
173
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000174void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000175PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000176{
Victor Stinnera7126792019-03-19 14:19:38 +0100177 if (gil_created()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100179 }
180
Inada Naoki001fee12019-02-20 10:00:09 +0900181 PyThread_init_thread();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000182 create_gil();
Victor Stinner50b48572018-11-01 01:51:40 +0100183 take_gil(_PyThreadState_GET());
Eric Snow8479a342019-03-08 23:44:33 -0700184
Eric Snowb75b1a352019-04-12 10:20:10 -0600185 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
186 if (_PyRuntime.ceval.pending.lock == NULL) {
187 Py_FatalError("Can't initialize threads for pending calls");
188 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000189}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000190
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000191void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000192_PyEval_FiniThreads(void)
193{
Victor Stinnera7126792019-03-19 14:19:38 +0100194 if (!gil_created()) {
Antoine Pitrou1df15362010-09-13 14:16:46 +0000195 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100196 }
197
Antoine Pitrou1df15362010-09-13 14:16:46 +0000198 destroy_gil();
199 assert(!gil_created());
Victor Stinner99fcc612019-04-29 13:04:07 +0200200
201 if (_PyRuntime.ceval.pending.lock != NULL) {
202 PyThread_free_lock(_PyRuntime.ceval.pending.lock);
203 _PyRuntime.ceval.pending.lock = NULL;
204 }
Antoine Pitrou1df15362010-09-13 14:16:46 +0000205}
206
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400207static inline void
208exit_thread_if_finalizing(PyThreadState *tstate)
209{
210 /* _Py_Finalizing is protected by the GIL */
211 if (_Py_IsFinalizing() && !_Py_CURRENTLY_FINALIZING(tstate)) {
212 drop_gil(tstate);
213 PyThread_exit_thread();
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400214 }
215}
216
Antoine Pitrou1df15362010-09-13 14:16:46 +0000217void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000218PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000219{
Victor Stinner50b48572018-11-01 01:51:40 +0100220 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 if (tstate == NULL)
222 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
223 take_gil(tstate);
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400224 exit_thread_if_finalizing(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000225}
226
227void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000228PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000229{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100231 We therefore avoid PyThreadState_Get() which dumps a fatal error
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 in debug mode.
233 */
Victor Stinner50b48572018-11-01 01:51:40 +0100234 drop_gil(_PyThreadState_GET());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000235}
236
237void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000238PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000239{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 if (tstate == NULL)
241 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
242 /* Check someone has called PyEval_InitThreads() to create the lock */
243 assert(gil_created());
244 take_gil(tstate);
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400245 exit_thread_if_finalizing(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 if (PyThreadState_Swap(tstate) != NULL)
247 Py_FatalError(
248 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000249}
250
251void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000252PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000253{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 if (tstate == NULL)
255 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
256 if (PyThreadState_Swap(NULL) != tstate)
257 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
258 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000259}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000260
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200261/* This function is called from PyOS_AfterFork_Child to destroy all threads
262 * which are not running in the child process, and clear internal locks
263 * which might be held by those threads.
264 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000265
266void
267PyEval_ReInitThreads(void)
268{
Victor Stinner50b48572018-11-01 01:51:40 +0100269 PyThreadState *current_tstate = _PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 if (!gil_created())
272 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 recreate_gil();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200274 take_gil(current_tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700275
Eric Snowb75b1a352019-04-12 10:20:10 -0600276 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
277 if (_PyRuntime.ceval.pending.lock == NULL) {
Eric Snow8479a342019-03-08 23:44:33 -0700278 Py_FatalError("Can't initialize threads for pending calls");
279 }
Jesse Nollera8513972008-07-17 16:49:17 +0000280
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200281 /* Destroy all threads except the current one */
282 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000283}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000284
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000285/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600286 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000287
288void
Eric Snowb75b1a352019-04-12 10:20:10 -0600289_PyEval_SignalAsyncExc(void)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000290{
Eric Snowb75b1a352019-04-12 10:20:10 -0600291 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000292}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000293
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000294PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000295PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000296{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 PyThreadState *tstate = PyThreadState_Swap(NULL);
298 if (tstate == NULL)
299 Py_FatalError("PyEval_SaveThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100300 assert(gil_created());
301 drop_gil(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000303}
304
305void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000306PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000307{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 if (tstate == NULL)
309 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100310 assert(gil_created());
311
312 int err = errno;
313 take_gil(tstate);
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400314 exit_thread_if_finalizing(tstate);
Victor Stinner2914bb32018-01-29 11:57:45 +0100315 errno = err;
316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000318}
319
320
Guido van Rossuma9672091994-09-14 13:31:22 +0000321/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
322 signal handlers or Mac I/O completion routines) can schedule calls
323 to a function to be called synchronously.
324 The synchronous function is called with one void* argument.
325 It should return 0 for success or -1 for failure -- failure should
326 be accompanied by an exception.
327
328 If registry succeeds, the registry function returns 0; if it fails
329 (e.g. due to too many pending calls) it returns -1 (without setting
330 an exception condition).
331
332 Note that because registry may occur from within signal handlers,
333 or other asynchronous events, calling malloc() is unsafe!
334
Guido van Rossuma9672091994-09-14 13:31:22 +0000335 Any thread can schedule pending calls, but only the main thread
336 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000337 There is no facility to schedule calls to a particular thread, but
338 that should be easy to change, should that ever be required. In
339 that case, the static variables here should go into the python
340 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000341*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000342
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200343void
344_PyEval_SignalReceived(void)
345{
346 /* bpo-30703: Function called when the C signal handler of Python gets a
347 signal. We cannot queue a callback using Py_AddPendingCall() since
348 that function is not async-signal-safe. */
Eric Snowfdf282d2019-01-11 14:26:55 -0700349 SIGNAL_PENDING_SIGNALS();
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200350}
351
Eric Snow5be45a62019-03-08 22:47:07 -0700352/* Push one item onto the queue while holding the lock. */
353static int
Eric Snowb75b1a352019-04-12 10:20:10 -0600354_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600355 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700356{
Eric Snow842a2f02019-03-15 15:47:51 -0600357 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700358 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600359 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700360 return -1; /* Queue full */
361 }
Eric Snow842a2f02019-03-15 15:47:51 -0600362 pending->calls[i].func = func;
363 pending->calls[i].arg = arg;
364 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700365 return 0;
366}
367
368/* Pop one item off the queue while holding the lock. */
369static void
Eric Snowb75b1a352019-04-12 10:20:10 -0600370_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600371 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700372{
Eric Snow842a2f02019-03-15 15:47:51 -0600373 int i = pending->first;
374 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700375 return; /* Queue empty */
376 }
377
Eric Snow842a2f02019-03-15 15:47:51 -0600378 *func = pending->calls[i].func;
379 *arg = pending->calls[i].arg;
380 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700381}
382
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200383/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000384 scheduling to be made from any thread, and even from an executing
385 callback.
386 */
387
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000388int
Eric Snowb75b1a352019-04-12 10:20:10 -0600389Py_AddPendingCall(int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000390{
Eric Snowb75b1a352019-04-12 10:20:10 -0600391 struct _pending_calls *pending = &_PyRuntime.ceval.pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600392
393 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
394 if (pending->finishing) {
395 PyThread_release_lock(pending->lock);
396
397 PyObject *exc, *val, *tb;
398 PyErr_Fetch(&exc, &val, &tb);
399 PyErr_SetString(PyExc_SystemError,
400 "Py_AddPendingCall: cannot add pending calls "
401 "(Python shutting down)");
402 PyErr_Print();
403 PyErr_Restore(exc, val, tb);
404 return -1;
405 }
Eric Snowb75b1a352019-04-12 10:20:10 -0600406 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600407 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700408
Eric Snowb75b1a352019-04-12 10:20:10 -0600409 /* signal main loop */
410 SIGNAL_PENDING_CALLS();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000412}
413
Eric Snowfdf282d2019-01-11 14:26:55 -0700414static int
415handle_signals(void)
416{
Eric Snow5be45a62019-03-08 22:47:07 -0700417 /* Only handle signals on main thread. PyEval_InitThreads must
418 * have been called already.
419 */
420 if (PyThread_get_thread_ident() != _PyRuntime.main_thread) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700421 return 0;
422 }
Eric Snow64d6cc82019-02-23 15:40:43 -0700423 /*
424 * Ensure that the thread isn't currently running some other
425 * interpreter.
426 */
427 if (_PyInterpreterState_GET_UNSAFE() != _PyRuntime.interpreters.main) {
428 return 0;
429 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700430
431 UNSIGNAL_PENDING_SIGNALS();
Eric Snow64d6cc82019-02-23 15:40:43 -0700432 if (_PyErr_CheckSignals() < 0) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700433 SIGNAL_PENDING_SIGNALS(); /* We're not done yet */
434 return -1;
435 }
436 return 0;
437}
438
439static int
Eric Snowb75b1a352019-04-12 10:20:10 -0600440make_pending_calls(struct _pending_calls* pending)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000441{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200442 static int busy = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000443
Eric Snowb75b1a352019-04-12 10:20:10 -0600444 /* only service pending calls on main thread */
445 if (PyThread_get_thread_ident() != _PyRuntime.main_thread) {
446 return 0;
447 }
448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 /* don't perform recursive pending calls */
Eric Snowfdf282d2019-01-11 14:26:55 -0700450 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700452 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200453 busy = 1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200454 /* unsignal before starting to call callbacks, so that any callback
455 added in-between re-signals */
Eric Snowb75b1a352019-04-12 10:20:10 -0600456 UNSIGNAL_PENDING_CALLS();
Eric Snowfdf282d2019-01-11 14:26:55 -0700457 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 /* perform a bounded number of calls, in case of recursion */
Eric Snowfdf282d2019-01-11 14:26:55 -0700460 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700461 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 void *arg = NULL;
463
464 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600465 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Eric Snowb75b1a352019-04-12 10:20:10 -0600466 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600467 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700468
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100469 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700470 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100471 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700472 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700473 res = func(arg);
474 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200475 goto error;
476 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200478
Charles-François Natalif23339a2011-07-23 18:15:43 +0200479 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700480 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200481
482error:
483 busy = 0;
Eric Snowb75b1a352019-04-12 10:20:10 -0600484 SIGNAL_PENDING_CALLS();
Eric Snowfdf282d2019-01-11 14:26:55 -0700485 return res;
486}
487
Eric Snow842a2f02019-03-15 15:47:51 -0600488void
Eric Snowb75b1a352019-04-12 10:20:10 -0600489_Py_FinishPendingCalls(void)
Eric Snow842a2f02019-03-15 15:47:51 -0600490{
Eric Snowb75b1a352019-04-12 10:20:10 -0600491 struct _pending_calls *pending = &_PyRuntime.ceval.pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600492
493 assert(PyGILState_Check());
494
495 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
496 pending->finishing = 1;
497 PyThread_release_lock(pending->lock);
498
499 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
500 return;
501 }
502
Eric Snowb75b1a352019-04-12 10:20:10 -0600503 if (make_pending_calls(pending) < 0) {
Eric Snow842a2f02019-03-15 15:47:51 -0600504 PyObject *exc, *val, *tb;
505 PyErr_Fetch(&exc, &val, &tb);
506 PyErr_BadInternalCall();
507 _PyErr_ChainExceptions(exc, val, tb);
508 PyErr_Print();
509 }
510}
511
Eric Snowfdf282d2019-01-11 14:26:55 -0700512/* Py_MakePendingCalls() is a simple wrapper for the sake
513 of backward-compatibility. */
514int
515Py_MakePendingCalls(void)
516{
517 assert(PyGILState_Check());
518
519 /* Python signal handler doesn't really queue a callback: it only signals
520 that a signal was received, see _PyEval_SignalReceived(). */
521 int res = handle_signals();
522 if (res != 0) {
523 return res;
524 }
525
Eric Snowb75b1a352019-04-12 10:20:10 -0600526 res = make_pending_calls(&_PyRuntime.ceval.pending);
527 if (res != 0) {
528 return res;
529 }
530
531 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000532}
533
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000534/* The interpreter's recursion limit */
535
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000536#ifndef Py_DEFAULT_RECURSION_LIMIT
537#define Py_DEFAULT_RECURSION_LIMIT 1000
538#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600539
Eric Snow05351c12017-09-05 21:43:08 -0700540int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000541
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600542void
543_PyEval_Initialize(struct _ceval_runtime_state *state)
544{
545 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
546 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
547 _gil_initialize(&state->gil);
548}
549
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000550int
551Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000552{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600553 return _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000554}
555
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000556void
557Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000558{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600559 _PyRuntime.ceval.recursion_limit = new_limit;
560 _Py_CheckRecursionLimit = _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000561}
562
Armin Rigo2b3eb402003-10-28 12:05:48 +0000563/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
564 if the recursion_depth reaches _Py_CheckRecursionLimit.
565 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
566 to guarantee that _Py_CheckRecursiveCall() is regularly called.
567 Without USE_STACKCHECK, there is no need for this. */
568int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300569_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000570{
Victor Stinner50b48572018-11-01 01:51:40 +0100571 PyThreadState *tstate = _PyThreadState_GET();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600572 int recursion_limit = _PyRuntime.ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000573
574#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700575 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 if (PyOS_CheckStack()) {
577 --tstate->recursion_depth;
578 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
579 return -1;
580 }
pdox18967932017-10-25 23:03:01 -0700581 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700582 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700583#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 if (tstate->recursion_critical)
585 /* Somebody asked that we don't check for recursion. */
586 return 0;
587 if (tstate->overflowed) {
588 if (tstate->recursion_depth > recursion_limit + 50) {
589 /* Overflowing while handling an overflow. Give up. */
590 Py_FatalError("Cannot recover from stack overflow.");
591 }
592 return 0;
593 }
594 if (tstate->recursion_depth > recursion_limit) {
595 --tstate->recursion_depth;
596 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400597 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 "maximum recursion depth exceeded%s",
599 where);
600 return -1;
601 }
602 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000603}
604
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400605static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000606static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000607
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600608#define _Py_TracingPossible _PyRuntime.ceval.tracing_possible
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000609
Guido van Rossum374a9221991-04-04 10:40:29 +0000610
Guido van Rossumb209a111997-04-29 18:18:01 +0000611PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000612PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000613{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 return PyEval_EvalCodeEx(co,
615 globals, locals,
616 (PyObject **)NULL, 0,
617 (PyObject **)NULL, 0,
618 (PyObject **)NULL, 0,
619 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000620}
621
622
623/* Interpreter main loop */
624
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000625PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000626PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 /* This is for backward compatibility with extension modules that
628 used this API; core interpreter code should call
629 PyEval_EvalFrameEx() */
630 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000631}
632
633PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000634PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000635{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200636 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
637 return interp->eval_frame(f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700638}
639
Victor Stinnerc6944e72016-11-11 02:13:35 +0100640PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700641_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
642{
Guido van Rossum950361c1997-01-24 13:49:28 +0000643#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000645#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200646 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300647 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200648 int opcode; /* Current opcode */
649 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200650 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 PyObject *retval = NULL; /* Return value */
Victor Stinner50b48572018-11-01 01:51:40 +0100652 PyThreadState *tstate = _PyThreadState_GET();
Eric Snowb75b1a352019-04-12 10:20:10 -0600653 _Py_atomic_int *eval_breaker = &_PyRuntime.ceval.eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 is true when the line being executed has changed. The
661 initial values are such as to make this false the first
662 time it is tested. */
663 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000664
Serhiy Storchakaab874002016-09-11 13:48:15 +0300665 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 PyObject *names;
667 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000668
Brett Cannon368b4b72012-04-02 12:17:59 -0400669#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200670 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400671#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200672
Antoine Pitroub52ec782009-01-25 16:34:23 +0000673/* Computed GOTOs, or
674 the-optimization-commonly-but-improperly-known-as-"threaded code"
675 using gcc's labels-as-values extension
676 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
677
678 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000680 combined with a lookup table of jump addresses. However, since the
681 indirect jump instruction is shared by all opcodes, the CPU will have a
682 hard time making the right prediction for where to jump next (actually,
683 it will be always wrong except in the uncommon case of a sequence of
684 several identical opcodes).
685
686 "Threaded code" in contrast, uses an explicit jump table and an explicit
687 indirect jump instruction at the end of each opcode. Since the jump
688 instruction is at a different address for each opcode, the CPU will make a
689 separate prediction for each of these instructions, which is equivalent to
690 predicting the second opcode of each opcode pair. These predictions have
691 a much better chance to turn out valid, especially in small bytecode loops.
692
693 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000695 and potentially many more instructions (depending on the pipeline width).
696 A correctly predicted branch, however, is nearly free.
697
698 At the time of this writing, the "threaded code" version is up to 15-20%
699 faster than the normal "switch" version, depending on the compiler and the
700 CPU architecture.
701
702 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
703 because it would render the measurements invalid.
704
705
706 NOTE: care must be taken that the compiler doesn't try to "optimize" the
707 indirect jumps by sharing them between all opcodes. Such optimizations
708 can be disabled on gcc by using the -fno-gcse flag (or possibly
709 -fno-crossjumping).
710*/
711
Antoine Pitrou042b1282010-08-13 21:15:58 +0000712#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000713#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000714#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000715#endif
716
Antoine Pitrou042b1282010-08-13 21:15:58 +0000717#ifdef HAVE_COMPUTED_GOTOS
718 #ifndef USE_COMPUTED_GOTOS
719 #define USE_COMPUTED_GOTOS 1
720 #endif
721#else
722 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
723 #error "Computed gotos are not supported on this compiler."
724 #endif
725 #undef USE_COMPUTED_GOTOS
726 #define USE_COMPUTED_GOTOS 0
727#endif
728
729#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000730/* Import the static jump table */
731#include "opcode_targets.h"
732
Antoine Pitroub52ec782009-01-25 16:34:23 +0000733#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700734 op: \
735 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000736
Antoine Pitroub52ec782009-01-25 16:34:23 +0000737#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 { \
Eric Snow7bda9de2019-03-08 17:25:54 -0700739 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 FAST_DISPATCH(); \
741 } \
742 continue; \
743 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000744
745#ifdef LLTRACE
746#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700748 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300750 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300751 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 } \
753 goto fast_next_opcode; \
754 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000755#else
756#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700758 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300760 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300761 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 } \
763 goto fast_next_opcode; \
764 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000765#endif
766
767#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700768#define TARGET(op) op
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300769
Antoine Pitroub52ec782009-01-25 16:34:23 +0000770#define DISPATCH() continue
771#define FAST_DISPATCH() goto fast_next_opcode
772#endif
773
774
Neal Norwitza81d2202002-07-14 00:27:26 +0000775/* Tuple access macros */
776
777#ifndef Py_DEBUG
778#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
779#else
780#define GETITEM(v, i) PyTuple_GetItem((v), (i))
781#endif
782
Guido van Rossum374a9221991-04-04 10:40:29 +0000783/* Code access macros */
784
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300785/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600786#define INSTR_OFFSET() \
787 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300788#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300789 _Py_CODEUNIT word = *next_instr; \
790 opcode = _Py_OPCODE(word); \
791 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300792 next_instr++; \
793 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300794#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
795#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000796
Raymond Hettingerf606f872003-03-16 03:11:04 +0000797/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 Some opcodes tend to come in pairs thus making it possible to
799 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300800 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 Verifying the prediction costs a single high-speed test of a register
803 variable against a constant. If the pairing was good, then the
804 processor's own internal branch predication has a high likelihood of
805 success, resulting in a nearly zero-overhead transition to the
806 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300807 including its unpredictable switch-case branch. Combined with the
808 processor's internal branch prediction, a successful PREDICT has the
809 effect of making the two opcodes run as if they were a single new opcode
810 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000811
Georg Brandl86b2fb92008-07-16 03:43:04 +0000812 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 predictions turned-on and interpret the results as if some opcodes
814 had been combined or turn-off predictions so that the opcode frequency
815 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000816
817 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 the CPU to record separate branch prediction information for each
819 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000820
Raymond Hettingerf606f872003-03-16 03:11:04 +0000821*/
822
Antoine Pitrou042b1282010-08-13 21:15:58 +0000823#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000825#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300826#define PREDICT(op) \
827 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300828 _Py_CODEUNIT word = *next_instr; \
829 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300830 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300831 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300832 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300833 goto PRED_##op; \
834 } \
835 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000836#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300837#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000838
Raymond Hettingerf606f872003-03-16 03:11:04 +0000839
Guido van Rossum374a9221991-04-04 10:40:29 +0000840/* Stack manipulation macros */
841
Martin v. Löwis18e16552006-02-15 17:27:45 +0000842/* The stack can grow at most MAXINT deep, as co_nlocals and
843 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000844#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
845#define EMPTY() (STACK_LEVEL() == 0)
846#define TOP() (stack_pointer[-1])
847#define SECOND() (stack_pointer[-2])
848#define THIRD() (stack_pointer[-3])
849#define FOURTH() (stack_pointer[-4])
850#define PEEK(n) (stack_pointer[-(n)])
851#define SET_TOP(v) (stack_pointer[-1] = (v))
852#define SET_SECOND(v) (stack_pointer[-2] = (v))
853#define SET_THIRD(v) (stack_pointer[-3] = (v))
854#define SET_FOURTH(v) (stack_pointer[-4] = (v))
855#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
856#define BASIC_STACKADJ(n) (stack_pointer += n)
857#define BASIC_PUSH(v) (*stack_pointer++ = (v))
858#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000859
Guido van Rossum96a42c81992-01-12 02:29:51 +0000860#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000862 lltrace && prtrace(TOP(), "push")); \
863 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000865 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +0000866#define STACK_GROW(n) do { \
867 assert(n >= 0); \
868 (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000869 lltrace && prtrace(TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000870 assert(STACK_LEVEL() <= co->co_stacksize); \
871 } while (0)
872#define STACK_SHRINK(n) do { \
873 assert(n >= 0); \
874 (void)(lltrace && prtrace(TOP(), "stackadj")); \
875 (void)(BASIC_STACKADJ(-n)); \
876 assert(STACK_LEVEL() <= co->co_stacksize); \
877 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +0000878#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000879 prtrace((STACK_POINTER)[-1], "ext_pop")), \
880 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000881#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000882#define PUSH(v) BASIC_PUSH(v)
883#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +0000884#define STACK_GROW(n) BASIC_STACKADJ(n)
885#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000886#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000887#endif
888
Guido van Rossum681d79a1995-07-18 14:51:37 +0000889/* Local variable macros */
890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000892
893/* The SETLOCAL() macro must not DECREF the local variable in-place and
894 then store the new value; it must copy the old value to a temporary
895 value, then store the new value, and then DECREF the temporary value.
896 This is because it is possible that during the DECREF the frame is
897 accessed by other code (e.g. a __del__ method or gc.collect()) and the
898 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000900 GETLOCAL(i) = value; \
901 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000902
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000903
904#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 while (STACK_LEVEL() > (b)->b_level) { \
906 PyObject *v = POP(); \
907 Py_XDECREF(v); \
908 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000909
910#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300911 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +0100913 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 assert(STACK_LEVEL() >= (b)->b_level + 3); \
915 while (STACK_LEVEL() > (b)->b_level + 3) { \
916 value = POP(); \
917 Py_XDECREF(value); \
918 } \
Mark Shannonae3087c2017-10-22 22:41:51 +0100919 exc_info = tstate->exc_info; \
920 type = exc_info->exc_type; \
921 value = exc_info->exc_value; \
922 traceback = exc_info->exc_traceback; \
923 exc_info->exc_type = POP(); \
924 exc_info->exc_value = POP(); \
925 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 Py_XDECREF(type); \
927 Py_XDECREF(value); \
928 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300929 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000930
Guido van Rossuma027efa1997-05-05 20:56:21 +0000931/* Start of code */
932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 /* push frame */
934 if (Py_EnterRecursiveCall(""))
935 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 if (tstate->use_tracing) {
940 if (tstate->c_tracefunc != NULL) {
941 /* tstate->c_tracefunc, if defined, is a
942 function that will be called on *every* entry
943 to a code block. Its return value, if not
944 None, is a function that will be called at
945 the start of each executed line of code.
946 (Actually, the function must return itself
947 in order to continue tracing.) The trace
948 functions are called with three arguments:
949 a pointer to the current frame, a string
950 indicating why the function is called, and
951 an argument which depends on the situation.
952 The global trace function is also called
953 whenever an exception is detected. */
954 if (call_trace_protected(tstate->c_tracefunc,
955 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100956 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 /* Trace function raised an error */
958 goto exit_eval_frame;
959 }
960 }
961 if (tstate->c_profilefunc != NULL) {
962 /* Similar for c_profilefunc, except it needn't
963 return itself and isn't called for "line" events */
964 if (call_trace_protected(tstate->c_profilefunc,
965 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100966 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 /* Profile function raised an error */
968 goto exit_eval_frame;
969 }
970 }
971 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000972
Łukasz Langaa785c872016-09-09 17:37:37 -0700973 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
974 dtrace_function_entry(f);
975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 co = f->f_code;
977 names = co->co_names;
978 consts = co->co_consts;
979 fastlocals = f->f_localsplus;
980 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300981 assert(PyBytes_Check(co->co_code));
982 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +0300983 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
984 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
985 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300986 /*
987 f->f_lasti refers to the index of the last instruction,
988 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000989
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300990 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500991 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 When the PREDICT() macros are enabled, some opcode pairs follow in
994 direct succession without updating f->f_lasti. A successful
995 prediction effectively links the two codes together as if they
996 were a single new opcode; accordingly,f->f_lasti will point to
997 the first code in the pair (for instance, GET_ITER followed by
998 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300999 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001001 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001002 next_instr = first_instr;
1003 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001004 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1005 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001006 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 stack_pointer = f->f_stacktop;
1008 assert(stack_pointer != NULL);
1009 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001010 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001011
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001012
Tim Peters5ca576e2001-06-18 22:08:13 +00001013#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001014 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001015#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001016
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001017 if (throwflag) /* support for generator.throw() */
1018 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001019
Victor Stinnerace47d72013-07-18 01:41:08 +02001020#ifdef Py_DEBUG
1021 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001022 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001023 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +02001024 assert(!PyErr_Occurred());
1025#endif
1026
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001027main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1030 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +02001031 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001032
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 /* Do periodic things. Doing this every time through
1034 the loop would add too much overhead, so we do it
1035 only every Nth instruction. We also do it if
1036 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1037 event needs attention (e.g. a signal handler or
1038 async I/O handler); see Py_AddPendingCall() and
1039 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001040
Eric Snow7bda9de2019-03-08 17:25:54 -07001041 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001042 opcode = _Py_OPCODE(*next_instr);
1043 if (opcode == SETUP_FINALLY ||
1044 opcode == SETUP_WITH ||
1045 opcode == BEFORE_ASYNC_WITH ||
1046 opcode == YIELD_FROM) {
1047 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001048 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001049 - If we're about to enter the 'with:'. It will prevent
1050 emitting a resource warning in the common idiom
1051 'with open(path) as file:'.
1052 - If we're about to enter the 'async with:'.
1053 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001054 *very* useful, but might help in some cases and it's
1055 traditional)
1056 - If we're resuming a chain of nested 'yield from' or
1057 'await' calls, then each frame is parked with YIELD_FROM
1058 as its next opcode. If the user hit control-C we want to
1059 wait until we've reached the innermost frame before
1060 running the signal handler and raising KeyboardInterrupt
1061 (see bpo-30039).
1062 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 goto fast_next_opcode;
1064 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001065
1066 if (_Py_atomic_load_relaxed(
1067 &_PyRuntime.ceval.signals_pending))
1068 {
1069 if (handle_signals() != 0) {
1070 goto error;
1071 }
1072 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001073 if (_Py_atomic_load_relaxed(
Eric Snowb75b1a352019-04-12 10:20:10 -06001074 &_PyRuntime.ceval.pending.calls_to_do))
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001075 {
Eric Snowb75b1a352019-04-12 10:20:10 -06001076 if (make_pending_calls(&_PyRuntime.ceval.pending) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001077 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001078 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001080
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001081 if (_Py_atomic_load_relaxed(
1082 &_PyRuntime.ceval.gil_drop_request))
1083 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 /* Give another thread a chance */
1085 if (PyThreadState_Swap(NULL) != tstate)
1086 Py_FatalError("ceval: tstate mix-up");
1087 drop_gil(tstate);
1088
1089 /* Other threads may run now */
1090
1091 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001092
1093 /* Check if we should make a quick exit. */
Joannah Nanjekyef781d202019-04-29 04:38:45 -04001094 exit_thread_if_finalizing(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 if (PyThreadState_Swap(tstate) != NULL)
1097 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 }
1099 /* Check for asynchronous exceptions. */
1100 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001101 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 tstate->async_exc = NULL;
Eric Snowb75b1a352019-04-12 10:20:10 -06001103 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001104 PyErr_SetNone(exc);
1105 Py_DECREF(exc);
1106 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 }
1108 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001110 fast_next_opcode:
1111 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001112
Łukasz Langaa785c872016-09-09 17:37:37 -07001113 if (PyDTrace_LINE_ENABLED())
1114 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001119 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001120 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 /* see maybe_call_line_trace
1122 for expository comments */
1123 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 err = maybe_call_line_trace(tstate->c_tracefunc,
1126 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001127 tstate, f,
1128 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 /* Reload possibly changed frame fields */
1130 JUMPTO(f->f_lasti);
1131 if (f->f_stacktop != NULL) {
1132 stack_pointer = f->f_stacktop;
1133 f->f_stacktop = NULL;
1134 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001135 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001137 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001141
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001142 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001143 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001144#ifdef DYNAMIC_EXECUTION_PROFILE
1145#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 dxpairs[lastopcode][opcode]++;
1147 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001148#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001150#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001151
Guido van Rossum96a42c81992-01-12 02:29:51 +00001152#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 if (lltrace) {
1156 if (HAS_ARG(opcode)) {
1157 printf("%d: %d, %d\n",
1158 f->f_lasti, opcode, oparg);
1159 }
1160 else {
1161 printf("%d: %d\n",
1162 f->f_lasti, opcode);
1163 }
1164 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001165#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001170 It is essential that any operation that fails must goto error
1171 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001172
Benjamin Petersonddd19492018-09-16 22:38:02 -07001173 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001175 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001176
Benjamin Petersonddd19492018-09-16 22:38:02 -07001177 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001178 PyObject *value = GETLOCAL(oparg);
1179 if (value == NULL) {
1180 format_exc_check_arg(PyExc_UnboundLocalError,
1181 UNBOUNDLOCAL_ERROR_MSG,
1182 PyTuple_GetItem(co->co_varnames, oparg));
1183 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001185 Py_INCREF(value);
1186 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001188 }
1189
Benjamin Petersonddd19492018-09-16 22:38:02 -07001190 case TARGET(LOAD_CONST): {
1191 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001192 PyObject *value = GETITEM(consts, oparg);
1193 Py_INCREF(value);
1194 PUSH(value);
1195 FAST_DISPATCH();
1196 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001197
Benjamin Petersonddd19492018-09-16 22:38:02 -07001198 case TARGET(STORE_FAST): {
1199 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001200 PyObject *value = POP();
1201 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001203 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001204
Benjamin Petersonddd19492018-09-16 22:38:02 -07001205 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001206 PyObject *value = POP();
1207 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001209 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001210
Benjamin Petersonddd19492018-09-16 22:38:02 -07001211 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001212 PyObject *top = TOP();
1213 PyObject *second = SECOND();
1214 SET_TOP(second);
1215 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001217 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001218
Benjamin Petersonddd19492018-09-16 22:38:02 -07001219 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001220 PyObject *top = TOP();
1221 PyObject *second = SECOND();
1222 PyObject *third = THIRD();
1223 SET_TOP(second);
1224 SET_SECOND(third);
1225 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001227 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001228
Benjamin Petersonddd19492018-09-16 22:38:02 -07001229 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001230 PyObject *top = TOP();
1231 PyObject *second = SECOND();
1232 PyObject *third = THIRD();
1233 PyObject *fourth = FOURTH();
1234 SET_TOP(second);
1235 SET_SECOND(third);
1236 SET_THIRD(fourth);
1237 SET_FOURTH(top);
1238 FAST_DISPATCH();
1239 }
1240
Benjamin Petersonddd19492018-09-16 22:38:02 -07001241 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001242 PyObject *top = TOP();
1243 Py_INCREF(top);
1244 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001246 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001247
Benjamin Petersonddd19492018-09-16 22:38:02 -07001248 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001249 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001250 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001251 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001252 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001253 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001254 SET_TOP(top);
1255 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001256 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001257 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001258
Benjamin Petersonddd19492018-09-16 22:38:02 -07001259 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001260 PyObject *value = TOP();
1261 PyObject *res = PyNumber_Positive(value);
1262 Py_DECREF(value);
1263 SET_TOP(res);
1264 if (res == NULL)
1265 goto error;
1266 DISPATCH();
1267 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001268
Benjamin Petersonddd19492018-09-16 22:38:02 -07001269 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001270 PyObject *value = TOP();
1271 PyObject *res = PyNumber_Negative(value);
1272 Py_DECREF(value);
1273 SET_TOP(res);
1274 if (res == NULL)
1275 goto error;
1276 DISPATCH();
1277 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001278
Benjamin Petersonddd19492018-09-16 22:38:02 -07001279 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001280 PyObject *value = TOP();
1281 int err = PyObject_IsTrue(value);
1282 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 if (err == 0) {
1284 Py_INCREF(Py_True);
1285 SET_TOP(Py_True);
1286 DISPATCH();
1287 }
1288 else if (err > 0) {
1289 Py_INCREF(Py_False);
1290 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 DISPATCH();
1292 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001293 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001294 goto error;
1295 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001296
Benjamin Petersonddd19492018-09-16 22:38:02 -07001297 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001298 PyObject *value = TOP();
1299 PyObject *res = PyNumber_Invert(value);
1300 Py_DECREF(value);
1301 SET_TOP(res);
1302 if (res == NULL)
1303 goto error;
1304 DISPATCH();
1305 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001306
Benjamin Petersonddd19492018-09-16 22:38:02 -07001307 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001308 PyObject *exp = POP();
1309 PyObject *base = TOP();
1310 PyObject *res = PyNumber_Power(base, exp, Py_None);
1311 Py_DECREF(base);
1312 Py_DECREF(exp);
1313 SET_TOP(res);
1314 if (res == NULL)
1315 goto error;
1316 DISPATCH();
1317 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001318
Benjamin Petersonddd19492018-09-16 22:38:02 -07001319 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001320 PyObject *right = POP();
1321 PyObject *left = TOP();
1322 PyObject *res = PyNumber_Multiply(left, right);
1323 Py_DECREF(left);
1324 Py_DECREF(right);
1325 SET_TOP(res);
1326 if (res == NULL)
1327 goto error;
1328 DISPATCH();
1329 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001330
Benjamin Petersonddd19492018-09-16 22:38:02 -07001331 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001332 PyObject *right = POP();
1333 PyObject *left = TOP();
1334 PyObject *res = PyNumber_MatrixMultiply(left, right);
1335 Py_DECREF(left);
1336 Py_DECREF(right);
1337 SET_TOP(res);
1338 if (res == NULL)
1339 goto error;
1340 DISPATCH();
1341 }
1342
Benjamin Petersonddd19492018-09-16 22:38:02 -07001343 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001344 PyObject *divisor = POP();
1345 PyObject *dividend = TOP();
1346 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1347 Py_DECREF(dividend);
1348 Py_DECREF(divisor);
1349 SET_TOP(quotient);
1350 if (quotient == NULL)
1351 goto error;
1352 DISPATCH();
1353 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001354
Benjamin Petersonddd19492018-09-16 22:38:02 -07001355 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001356 PyObject *divisor = POP();
1357 PyObject *dividend = TOP();
1358 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1359 Py_DECREF(dividend);
1360 Py_DECREF(divisor);
1361 SET_TOP(quotient);
1362 if (quotient == NULL)
1363 goto error;
1364 DISPATCH();
1365 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001366
Benjamin Petersonddd19492018-09-16 22:38:02 -07001367 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001368 PyObject *divisor = POP();
1369 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001370 PyObject *res;
1371 if (PyUnicode_CheckExact(dividend) && (
1372 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1373 // fast path; string formatting, but not if the RHS is a str subclass
1374 // (see issue28598)
1375 res = PyUnicode_Format(dividend, divisor);
1376 } else {
1377 res = PyNumber_Remainder(dividend, divisor);
1378 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001379 Py_DECREF(divisor);
1380 Py_DECREF(dividend);
1381 SET_TOP(res);
1382 if (res == NULL)
1383 goto error;
1384 DISPATCH();
1385 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001386
Benjamin Petersonddd19492018-09-16 22:38:02 -07001387 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001388 PyObject *right = POP();
1389 PyObject *left = TOP();
1390 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001391 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1392 CPython using bytecode, it is simply worthless.
1393 See http://bugs.python.org/issue21955 and
1394 http://bugs.python.org/issue10044 for the discussion. In short,
1395 no patch shown any impact on a realistic benchmark, only a minor
1396 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001397 if (PyUnicode_CheckExact(left) &&
1398 PyUnicode_CheckExact(right)) {
1399 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001400 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001401 }
1402 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001403 sum = PyNumber_Add(left, right);
1404 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001405 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001406 Py_DECREF(right);
1407 SET_TOP(sum);
1408 if (sum == NULL)
1409 goto error;
1410 DISPATCH();
1411 }
1412
Benjamin Petersonddd19492018-09-16 22:38:02 -07001413 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001414 PyObject *right = POP();
1415 PyObject *left = TOP();
1416 PyObject *diff = PyNumber_Subtract(left, right);
1417 Py_DECREF(right);
1418 Py_DECREF(left);
1419 SET_TOP(diff);
1420 if (diff == NULL)
1421 goto error;
1422 DISPATCH();
1423 }
1424
Benjamin Petersonddd19492018-09-16 22:38:02 -07001425 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001426 PyObject *sub = POP();
1427 PyObject *container = TOP();
1428 PyObject *res = PyObject_GetItem(container, sub);
1429 Py_DECREF(container);
1430 Py_DECREF(sub);
1431 SET_TOP(res);
1432 if (res == NULL)
1433 goto error;
1434 DISPATCH();
1435 }
1436
Benjamin Petersonddd19492018-09-16 22:38:02 -07001437 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001438 PyObject *right = POP();
1439 PyObject *left = TOP();
1440 PyObject *res = PyNumber_Lshift(left, right);
1441 Py_DECREF(left);
1442 Py_DECREF(right);
1443 SET_TOP(res);
1444 if (res == NULL)
1445 goto error;
1446 DISPATCH();
1447 }
1448
Benjamin Petersonddd19492018-09-16 22:38:02 -07001449 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001450 PyObject *right = POP();
1451 PyObject *left = TOP();
1452 PyObject *res = PyNumber_Rshift(left, right);
1453 Py_DECREF(left);
1454 Py_DECREF(right);
1455 SET_TOP(res);
1456 if (res == NULL)
1457 goto error;
1458 DISPATCH();
1459 }
1460
Benjamin Petersonddd19492018-09-16 22:38:02 -07001461 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001462 PyObject *right = POP();
1463 PyObject *left = TOP();
1464 PyObject *res = PyNumber_And(left, right);
1465 Py_DECREF(left);
1466 Py_DECREF(right);
1467 SET_TOP(res);
1468 if (res == NULL)
1469 goto error;
1470 DISPATCH();
1471 }
1472
Benjamin Petersonddd19492018-09-16 22:38:02 -07001473 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001474 PyObject *right = POP();
1475 PyObject *left = TOP();
1476 PyObject *res = PyNumber_Xor(left, right);
1477 Py_DECREF(left);
1478 Py_DECREF(right);
1479 SET_TOP(res);
1480 if (res == NULL)
1481 goto error;
1482 DISPATCH();
1483 }
1484
Benjamin Petersonddd19492018-09-16 22:38:02 -07001485 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001486 PyObject *right = POP();
1487 PyObject *left = TOP();
1488 PyObject *res = PyNumber_Or(left, right);
1489 Py_DECREF(left);
1490 Py_DECREF(right);
1491 SET_TOP(res);
1492 if (res == NULL)
1493 goto error;
1494 DISPATCH();
1495 }
1496
Benjamin Petersonddd19492018-09-16 22:38:02 -07001497 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001498 PyObject *v = POP();
1499 PyObject *list = PEEK(oparg);
1500 int err;
1501 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001503 if (err != 0)
1504 goto error;
1505 PREDICT(JUMP_ABSOLUTE);
1506 DISPATCH();
1507 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001508
Benjamin Petersonddd19492018-09-16 22:38:02 -07001509 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001510 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001511 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001512 int err;
1513 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001515 if (err != 0)
1516 goto error;
1517 PREDICT(JUMP_ABSOLUTE);
1518 DISPATCH();
1519 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001520
Benjamin Petersonddd19492018-09-16 22:38:02 -07001521 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001522 PyObject *exp = POP();
1523 PyObject *base = TOP();
1524 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1525 Py_DECREF(base);
1526 Py_DECREF(exp);
1527 SET_TOP(res);
1528 if (res == NULL)
1529 goto error;
1530 DISPATCH();
1531 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001532
Benjamin Petersonddd19492018-09-16 22:38:02 -07001533 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001534 PyObject *right = POP();
1535 PyObject *left = TOP();
1536 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1537 Py_DECREF(left);
1538 Py_DECREF(right);
1539 SET_TOP(res);
1540 if (res == NULL)
1541 goto error;
1542 DISPATCH();
1543 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001544
Benjamin Petersonddd19492018-09-16 22:38:02 -07001545 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001546 PyObject *right = POP();
1547 PyObject *left = TOP();
1548 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1549 Py_DECREF(left);
1550 Py_DECREF(right);
1551 SET_TOP(res);
1552 if (res == NULL)
1553 goto error;
1554 DISPATCH();
1555 }
1556
Benjamin Petersonddd19492018-09-16 22:38:02 -07001557 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001558 PyObject *divisor = POP();
1559 PyObject *dividend = TOP();
1560 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1561 Py_DECREF(dividend);
1562 Py_DECREF(divisor);
1563 SET_TOP(quotient);
1564 if (quotient == NULL)
1565 goto error;
1566 DISPATCH();
1567 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001568
Benjamin Petersonddd19492018-09-16 22:38:02 -07001569 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001570 PyObject *divisor = POP();
1571 PyObject *dividend = TOP();
1572 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1573 Py_DECREF(dividend);
1574 Py_DECREF(divisor);
1575 SET_TOP(quotient);
1576 if (quotient == NULL)
1577 goto error;
1578 DISPATCH();
1579 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001580
Benjamin Petersonddd19492018-09-16 22:38:02 -07001581 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001582 PyObject *right = POP();
1583 PyObject *left = TOP();
1584 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1585 Py_DECREF(left);
1586 Py_DECREF(right);
1587 SET_TOP(mod);
1588 if (mod == NULL)
1589 goto error;
1590 DISPATCH();
1591 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001592
Benjamin Petersonddd19492018-09-16 22:38:02 -07001593 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001594 PyObject *right = POP();
1595 PyObject *left = TOP();
1596 PyObject *sum;
1597 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1598 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001599 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001600 }
1601 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001602 sum = PyNumber_InPlaceAdd(left, right);
1603 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001604 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001605 Py_DECREF(right);
1606 SET_TOP(sum);
1607 if (sum == NULL)
1608 goto error;
1609 DISPATCH();
1610 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001611
Benjamin Petersonddd19492018-09-16 22:38:02 -07001612 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001613 PyObject *right = POP();
1614 PyObject *left = TOP();
1615 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1616 Py_DECREF(left);
1617 Py_DECREF(right);
1618 SET_TOP(diff);
1619 if (diff == NULL)
1620 goto error;
1621 DISPATCH();
1622 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001623
Benjamin Petersonddd19492018-09-16 22:38:02 -07001624 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001625 PyObject *right = POP();
1626 PyObject *left = TOP();
1627 PyObject *res = PyNumber_InPlaceLshift(left, right);
1628 Py_DECREF(left);
1629 Py_DECREF(right);
1630 SET_TOP(res);
1631 if (res == NULL)
1632 goto error;
1633 DISPATCH();
1634 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001635
Benjamin Petersonddd19492018-09-16 22:38:02 -07001636 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001637 PyObject *right = POP();
1638 PyObject *left = TOP();
1639 PyObject *res = PyNumber_InPlaceRshift(left, right);
1640 Py_DECREF(left);
1641 Py_DECREF(right);
1642 SET_TOP(res);
1643 if (res == NULL)
1644 goto error;
1645 DISPATCH();
1646 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001647
Benjamin Petersonddd19492018-09-16 22:38:02 -07001648 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001649 PyObject *right = POP();
1650 PyObject *left = TOP();
1651 PyObject *res = PyNumber_InPlaceAnd(left, right);
1652 Py_DECREF(left);
1653 Py_DECREF(right);
1654 SET_TOP(res);
1655 if (res == NULL)
1656 goto error;
1657 DISPATCH();
1658 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001659
Benjamin Petersonddd19492018-09-16 22:38:02 -07001660 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001661 PyObject *right = POP();
1662 PyObject *left = TOP();
1663 PyObject *res = PyNumber_InPlaceXor(left, right);
1664 Py_DECREF(left);
1665 Py_DECREF(right);
1666 SET_TOP(res);
1667 if (res == NULL)
1668 goto error;
1669 DISPATCH();
1670 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001671
Benjamin Petersonddd19492018-09-16 22:38:02 -07001672 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001673 PyObject *right = POP();
1674 PyObject *left = TOP();
1675 PyObject *res = PyNumber_InPlaceOr(left, right);
1676 Py_DECREF(left);
1677 Py_DECREF(right);
1678 SET_TOP(res);
1679 if (res == NULL)
1680 goto error;
1681 DISPATCH();
1682 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001683
Benjamin Petersonddd19492018-09-16 22:38:02 -07001684 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001685 PyObject *sub = TOP();
1686 PyObject *container = SECOND();
1687 PyObject *v = THIRD();
1688 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001689 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001690 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001691 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001692 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001693 Py_DECREF(container);
1694 Py_DECREF(sub);
1695 if (err != 0)
1696 goto error;
1697 DISPATCH();
1698 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001699
Benjamin Petersonddd19492018-09-16 22:38:02 -07001700 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001701 PyObject *sub = TOP();
1702 PyObject *container = SECOND();
1703 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001704 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001705 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001706 err = PyObject_DelItem(container, sub);
1707 Py_DECREF(container);
1708 Py_DECREF(sub);
1709 if (err != 0)
1710 goto error;
1711 DISPATCH();
1712 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001713
Benjamin Petersonddd19492018-09-16 22:38:02 -07001714 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001715 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001716 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001717 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001718 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001719 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 PyErr_SetString(PyExc_RuntimeError,
1721 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001722 Py_DECREF(value);
1723 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001725 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001726 Py_DECREF(value);
1727 if (res == NULL)
1728 goto error;
1729 Py_DECREF(res);
1730 DISPATCH();
1731 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001732
Benjamin Petersonddd19492018-09-16 22:38:02 -07001733 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001734 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 switch (oparg) {
1736 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001737 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001738 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001740 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001741 /* fall through */
1742 case 0:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001743 if (do_raise(exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001744 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001745 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 break;
1747 default:
1748 PyErr_SetString(PyExc_SystemError,
1749 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 break;
1751 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001752 goto error;
1753 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001754
Benjamin Petersonddd19492018-09-16 22:38:02 -07001755 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001757 assert(f->f_iblock == 0);
1758 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001759 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001760
Benjamin Petersonddd19492018-09-16 22:38:02 -07001761 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001762 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001763 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001764 PyObject *obj = TOP();
1765 PyTypeObject *type = Py_TYPE(obj);
1766
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001767 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001768 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001769 }
Yury Selivanov75445082015-05-11 22:57:16 -04001770
1771 if (getter != NULL) {
1772 iter = (*getter)(obj);
1773 Py_DECREF(obj);
1774 if (iter == NULL) {
1775 SET_TOP(NULL);
1776 goto error;
1777 }
1778 }
1779 else {
1780 SET_TOP(NULL);
1781 PyErr_Format(
1782 PyExc_TypeError,
1783 "'async for' requires an object with "
1784 "__aiter__ method, got %.100s",
1785 type->tp_name);
1786 Py_DECREF(obj);
1787 goto error;
1788 }
1789
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001790 if (Py_TYPE(iter)->tp_as_async == NULL ||
1791 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001792
Yury Selivanov398ff912017-03-02 22:20:00 -05001793 SET_TOP(NULL);
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001794 PyErr_Format(
1795 PyExc_TypeError,
1796 "'async for' received an object from __aiter__ "
1797 "that does not implement __anext__: %.100s",
1798 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001799 Py_DECREF(iter);
1800 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001801 }
1802
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001803 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001804 DISPATCH();
1805 }
1806
Benjamin Petersonddd19492018-09-16 22:38:02 -07001807 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001808 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001809 PyObject *next_iter = NULL;
1810 PyObject *awaitable = NULL;
1811 PyObject *aiter = TOP();
1812 PyTypeObject *type = Py_TYPE(aiter);
1813
Yury Selivanoveb636452016-09-08 22:01:51 -07001814 if (PyAsyncGen_CheckExact(aiter)) {
1815 awaitable = type->tp_as_async->am_anext(aiter);
1816 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001817 goto error;
1818 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001819 } else {
1820 if (type->tp_as_async != NULL){
1821 getter = type->tp_as_async->am_anext;
1822 }
Yury Selivanov75445082015-05-11 22:57:16 -04001823
Yury Selivanoveb636452016-09-08 22:01:51 -07001824 if (getter != NULL) {
1825 next_iter = (*getter)(aiter);
1826 if (next_iter == NULL) {
1827 goto error;
1828 }
1829 }
1830 else {
1831 PyErr_Format(
1832 PyExc_TypeError,
1833 "'async for' requires an iterator with "
1834 "__anext__ method, got %.100s",
1835 type->tp_name);
1836 goto error;
1837 }
Yury Selivanov75445082015-05-11 22:57:16 -04001838
Yury Selivanoveb636452016-09-08 22:01:51 -07001839 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1840 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001841 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001842 PyExc_TypeError,
1843 "'async for' received an invalid object "
1844 "from __anext__: %.100s",
1845 Py_TYPE(next_iter)->tp_name);
1846
1847 Py_DECREF(next_iter);
1848 goto error;
1849 } else {
1850 Py_DECREF(next_iter);
1851 }
1852 }
Yury Selivanov75445082015-05-11 22:57:16 -04001853
1854 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001855 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001856 DISPATCH();
1857 }
1858
Benjamin Petersonddd19492018-09-16 22:38:02 -07001859 case TARGET(GET_AWAITABLE): {
1860 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001861 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001862 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001863
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03001864 if (iter == NULL) {
1865 format_awaitable_error(Py_TYPE(iterable),
1866 _Py_OPCODE(next_instr[-2]));
1867 }
1868
Yury Selivanov75445082015-05-11 22:57:16 -04001869 Py_DECREF(iterable);
1870
Yury Selivanovc724bae2016-03-02 11:30:46 -05001871 if (iter != NULL && PyCoro_CheckExact(iter)) {
1872 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1873 if (yf != NULL) {
1874 /* `iter` is a coroutine object that is being
1875 awaited, `yf` is a pointer to the current awaitable
1876 being awaited on. */
1877 Py_DECREF(yf);
1878 Py_CLEAR(iter);
1879 PyErr_SetString(
1880 PyExc_RuntimeError,
1881 "coroutine is being awaited already");
1882 /* The code below jumps to `error` if `iter` is NULL. */
1883 }
1884 }
1885
Yury Selivanov75445082015-05-11 22:57:16 -04001886 SET_TOP(iter); /* Even if it's NULL */
1887
1888 if (iter == NULL) {
1889 goto error;
1890 }
1891
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001892 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001893 DISPATCH();
1894 }
1895
Benjamin Petersonddd19492018-09-16 22:38:02 -07001896 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001897 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001898 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001899 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001900 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1901 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001902 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001903 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001904 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001905 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001906 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001907 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001908 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001909 Py_DECREF(v);
1910 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001911 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001912 if (tstate->c_tracefunc != NULL
1913 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001914 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001915 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001916 if (err < 0)
1917 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001918 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001919 SET_TOP(val);
1920 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001921 }
Martin Panter95f53c12016-07-18 08:23:26 +00001922 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001923 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001924 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001925 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001926 f->f_lasti -= sizeof(_Py_CODEUNIT);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001927 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001928 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001929
Benjamin Petersonddd19492018-09-16 22:38:02 -07001930 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07001932
1933 if (co->co_flags & CO_ASYNC_GENERATOR) {
1934 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
1935 Py_DECREF(retval);
1936 if (w == NULL) {
1937 retval = NULL;
1938 goto error;
1939 }
1940 retval = w;
1941 }
1942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 f->f_stacktop = stack_pointer;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001944 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001945 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001946
Benjamin Petersonddd19492018-09-16 22:38:02 -07001947 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001948 PyObject *type, *value, *traceback;
1949 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001950 PyTryBlock *b = PyFrame_BlockPop(f);
1951 if (b->b_type != EXCEPT_HANDLER) {
1952 PyErr_SetString(PyExc_SystemError,
1953 "popped block is not an except handler");
1954 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001956 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
1957 STACK_LEVEL() <= (b)->b_level + 4);
1958 exc_info = tstate->exc_info;
1959 type = exc_info->exc_type;
1960 value = exc_info->exc_value;
1961 traceback = exc_info->exc_traceback;
1962 exc_info->exc_type = POP();
1963 exc_info->exc_value = POP();
1964 exc_info->exc_traceback = POP();
1965 Py_XDECREF(type);
1966 Py_XDECREF(value);
1967 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001969 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001970
Benjamin Petersonddd19492018-09-16 22:38:02 -07001971 case TARGET(POP_BLOCK): {
1972 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001973 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001975 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001976
Benjamin Petersonddd19492018-09-16 22:38:02 -07001977 case TARGET(POP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001978 /* If oparg is 0 at the top of the stack are 1 or 6 values:
1979 Either:
1980 - TOP = NULL or an integer
1981 or:
1982 - (TOP, SECOND, THIRD) = exc_info()
1983 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
1984
1985 If oparg is 1 the value for 'return' was additionally pushed
1986 at the top of the stack.
1987 */
1988 PyObject *res = NULL;
1989 if (oparg) {
1990 res = POP();
1991 }
1992 PyObject *exc = POP();
1993 if (exc == NULL || PyLong_CheckExact(exc)) {
1994 Py_XDECREF(exc);
1995 }
1996 else {
1997 Py_DECREF(exc);
1998 Py_DECREF(POP());
1999 Py_DECREF(POP());
2000
2001 PyObject *type, *value, *traceback;
2002 _PyErr_StackItem *exc_info;
2003 PyTryBlock *b = PyFrame_BlockPop(f);
2004 if (b->b_type != EXCEPT_HANDLER) {
2005 PyErr_SetString(PyExc_SystemError,
2006 "popped block is not an except handler");
2007 Py_XDECREF(res);
2008 goto error;
2009 }
2010 assert(STACK_LEVEL() == (b)->b_level + 3);
2011 exc_info = tstate->exc_info;
2012 type = exc_info->exc_type;
2013 value = exc_info->exc_value;
2014 traceback = exc_info->exc_traceback;
2015 exc_info->exc_type = POP();
2016 exc_info->exc_value = POP();
2017 exc_info->exc_traceback = POP();
2018 Py_XDECREF(type);
2019 Py_XDECREF(value);
2020 Py_XDECREF(traceback);
2021 }
2022 if (oparg) {
2023 PUSH(res);
2024 }
2025 DISPATCH();
2026 }
2027
Benjamin Petersonddd19492018-09-16 22:38:02 -07002028 case TARGET(CALL_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002029 PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
2030 if (ret == NULL) {
2031 goto error;
2032 }
2033 PUSH(ret);
2034 JUMPBY(oparg);
2035 FAST_DISPATCH();
2036 }
2037
Benjamin Petersonddd19492018-09-16 22:38:02 -07002038 case TARGET(BEGIN_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002039 /* Push NULL onto the stack for using it in END_FINALLY,
2040 POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH.
2041 */
2042 PUSH(NULL);
2043 FAST_DISPATCH();
2044 }
2045
Benjamin Petersonddd19492018-09-16 22:38:02 -07002046 case TARGET(END_FINALLY): {
2047 PREDICTED(END_FINALLY);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002048 /* At the top of the stack are 1 or 6 values:
2049 Either:
2050 - TOP = NULL or an integer
2051 or:
2052 - (TOP, SECOND, THIRD) = exc_info()
2053 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2054 */
2055 PyObject *exc = POP();
2056 if (exc == NULL) {
2057 FAST_DISPATCH();
2058 }
2059 else if (PyLong_CheckExact(exc)) {
2060 int ret = _PyLong_AsInt(exc);
2061 Py_DECREF(exc);
2062 if (ret == -1 && PyErr_Occurred()) {
2063 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002064 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002065 JUMPTO(ret);
2066 FAST_DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002068 else {
2069 assert(PyExceptionClass_Check(exc));
2070 PyObject *val = POP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002071 PyObject *tb = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002072 PyErr_Restore(exc, val, tb);
2073 goto exception_unwind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002075 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002076
Benjamin Petersonddd19492018-09-16 22:38:02 -07002077 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002078 PyObject *exc = POP();
2079 assert(PyExceptionClass_Check(exc));
2080 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2081 PyTryBlock *b = PyFrame_BlockPop(f);
2082 assert(b->b_type == EXCEPT_HANDLER);
2083 Py_DECREF(exc);
2084 UNWIND_EXCEPT_HANDLER(b);
2085 Py_DECREF(POP());
2086 JUMPBY(oparg);
2087 FAST_DISPATCH();
2088 }
2089 else {
2090 PyObject *val = POP();
2091 PyObject *tb = POP();
2092 PyErr_Restore(exc, val, tb);
2093 goto exception_unwind;
2094 }
2095 }
2096
Benjamin Petersonddd19492018-09-16 22:38:02 -07002097 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002098 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002099
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002100 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002101 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002102 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002103 if (bc == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002104 if (!PyErr_Occurred()) {
2105 PyErr_SetString(PyExc_NameError,
2106 "__build_class__ not found");
2107 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002108 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002109 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002110 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002111 }
2112 else {
2113 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2114 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002115 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002116 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2117 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002118 if (PyErr_ExceptionMatches(PyExc_KeyError))
2119 PyErr_SetString(PyExc_NameError,
2120 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002121 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002122 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002123 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002124 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002125 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002126 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002127
Benjamin Petersonddd19492018-09-16 22:38:02 -07002128 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002129 PyObject *name = GETITEM(names, oparg);
2130 PyObject *v = POP();
2131 PyObject *ns = f->f_locals;
2132 int err;
2133 if (ns == NULL) {
2134 PyErr_Format(PyExc_SystemError,
2135 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002136 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002137 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002139 if (PyDict_CheckExact(ns))
2140 err = PyDict_SetItem(ns, name, v);
2141 else
2142 err = PyObject_SetItem(ns, name, v);
2143 Py_DECREF(v);
2144 if (err != 0)
2145 goto error;
2146 DISPATCH();
2147 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002148
Benjamin Petersonddd19492018-09-16 22:38:02 -07002149 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002150 PyObject *name = GETITEM(names, oparg);
2151 PyObject *ns = f->f_locals;
2152 int err;
2153 if (ns == NULL) {
2154 PyErr_Format(PyExc_SystemError,
2155 "no locals when deleting %R", name);
2156 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002157 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002158 err = PyObject_DelItem(ns, name);
2159 if (err != 0) {
2160 format_exc_check_arg(PyExc_NameError,
2161 NAME_ERROR_MSG,
2162 name);
2163 goto error;
2164 }
2165 DISPATCH();
2166 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002167
Benjamin Petersonddd19492018-09-16 22:38:02 -07002168 case TARGET(UNPACK_SEQUENCE): {
2169 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002170 PyObject *seq = POP(), *item, **items;
2171 if (PyTuple_CheckExact(seq) &&
2172 PyTuple_GET_SIZE(seq) == oparg) {
2173 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002174 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002175 item = items[oparg];
2176 Py_INCREF(item);
2177 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002178 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002179 } else if (PyList_CheckExact(seq) &&
2180 PyList_GET_SIZE(seq) == oparg) {
2181 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002183 item = items[oparg];
2184 Py_INCREF(item);
2185 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002186 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002187 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002188 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002189 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002190 } else {
2191 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002192 Py_DECREF(seq);
2193 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002195 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002196 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002198
Benjamin Petersonddd19492018-09-16 22:38:02 -07002199 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002200 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2201 PyObject *seq = POP();
2202
2203 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2204 stack_pointer + totalargs)) {
2205 stack_pointer += totalargs;
2206 } else {
2207 Py_DECREF(seq);
2208 goto error;
2209 }
2210 Py_DECREF(seq);
2211 DISPATCH();
2212 }
2213
Benjamin Petersonddd19492018-09-16 22:38:02 -07002214 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002215 PyObject *name = GETITEM(names, oparg);
2216 PyObject *owner = TOP();
2217 PyObject *v = SECOND();
2218 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002219 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002220 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002221 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002222 Py_DECREF(owner);
2223 if (err != 0)
2224 goto error;
2225 DISPATCH();
2226 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002227
Benjamin Petersonddd19492018-09-16 22:38:02 -07002228 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002229 PyObject *name = GETITEM(names, oparg);
2230 PyObject *owner = POP();
2231 int err;
2232 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2233 Py_DECREF(owner);
2234 if (err != 0)
2235 goto error;
2236 DISPATCH();
2237 }
2238
Benjamin Petersonddd19492018-09-16 22:38:02 -07002239 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002240 PyObject *name = GETITEM(names, oparg);
2241 PyObject *v = POP();
2242 int err;
2243 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002244 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002245 if (err != 0)
2246 goto error;
2247 DISPATCH();
2248 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002249
Benjamin Petersonddd19492018-09-16 22:38:02 -07002250 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002251 PyObject *name = GETITEM(names, oparg);
2252 int err;
2253 err = PyDict_DelItem(f->f_globals, name);
2254 if (err != 0) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002255 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2256 format_exc_check_arg(
2257 PyExc_NameError, NAME_ERROR_MSG, name);
2258 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002259 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002260 }
2261 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002262 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002263
Benjamin Petersonddd19492018-09-16 22:38:02 -07002264 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002265 PyObject *name = GETITEM(names, oparg);
2266 PyObject *locals = f->f_locals;
2267 PyObject *v;
2268 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002269 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002270 "no locals when loading %R", name);
2271 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002273 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002274 v = PyDict_GetItemWithError(locals, name);
2275 if (v != NULL) {
2276 Py_INCREF(v);
2277 }
2278 else if (PyErr_Occurred()) {
2279 goto error;
2280 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 }
2282 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002283 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002284 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002285 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2286 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 PyErr_Clear();
2288 }
2289 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002290 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002291 v = PyDict_GetItemWithError(f->f_globals, name);
2292 if (v != NULL) {
2293 Py_INCREF(v);
2294 }
2295 else if (PyErr_Occurred()) {
2296 goto error;
2297 }
2298 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002299 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002300 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002301 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002302 if (!PyErr_Occurred()) {
2303 format_exc_check_arg(
Victor Stinnerb0b22422012-04-19 00:57:45 +02002304 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002305 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002306 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002307 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002308 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002309 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002310 }
2311 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002312 v = PyObject_GetItem(f->f_builtins, name);
2313 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002314 if (PyErr_ExceptionMatches(PyExc_KeyError))
2315 format_exc_check_arg(
2316 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002317 NAME_ERROR_MSG, name);
2318 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002319 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002320 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002321 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002322 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002323 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002324 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002325 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002326
Benjamin Petersonddd19492018-09-16 22:38:02 -07002327 case TARGET(LOAD_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002328 PyObject *name = GETITEM(names, oparg);
2329 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002330 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002331 && PyDict_CheckExact(f->f_builtins))
2332 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002333 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002334 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002335 name);
2336 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002337 if (!_PyErr_OCCURRED()) {
2338 /* _PyDict_LoadGlobal() returns NULL without raising
2339 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002340 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002341 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002342 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002343 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002345 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002347 else {
2348 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002349
2350 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002351 v = PyObject_GetItem(f->f_globals, name);
2352 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002353 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2354 goto error;
2355 PyErr_Clear();
2356
Victor Stinnerb4efc962015-11-20 09:24:02 +01002357 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002358 v = PyObject_GetItem(f->f_builtins, name);
2359 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002360 if (PyErr_ExceptionMatches(PyExc_KeyError))
2361 format_exc_check_arg(
2362 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002363 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002364 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002365 }
2366 }
2367 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002368 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002370 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002371
Benjamin Petersonddd19492018-09-16 22:38:02 -07002372 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002373 PyObject *v = GETLOCAL(oparg);
2374 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375 SETLOCAL(oparg, NULL);
2376 DISPATCH();
2377 }
2378 format_exc_check_arg(
2379 PyExc_UnboundLocalError,
2380 UNBOUNDLOCAL_ERROR_MSG,
2381 PyTuple_GetItem(co->co_varnames, oparg)
2382 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002383 goto error;
2384 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002385
Benjamin Petersonddd19492018-09-16 22:38:02 -07002386 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002387 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002388 PyObject *oldobj = PyCell_GET(cell);
2389 if (oldobj != NULL) {
2390 PyCell_SET(cell, NULL);
2391 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002392 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002393 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002394 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002395 goto error;
2396 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002397
Benjamin Petersonddd19492018-09-16 22:38:02 -07002398 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002399 PyObject *cell = freevars[oparg];
2400 Py_INCREF(cell);
2401 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002403 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002404
Benjamin Petersonddd19492018-09-16 22:38:02 -07002405 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002406 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002407 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002408 assert(locals);
2409 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2410 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2411 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2412 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2413 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002414 value = PyDict_GetItemWithError(locals, name);
2415 if (value != NULL) {
2416 Py_INCREF(value);
2417 }
2418 else if (PyErr_Occurred()) {
2419 goto error;
2420 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002421 }
2422 else {
2423 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002424 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002425 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2426 goto error;
2427 PyErr_Clear();
2428 }
2429 }
2430 if (!value) {
2431 PyObject *cell = freevars[oparg];
2432 value = PyCell_GET(cell);
2433 if (value == NULL) {
2434 format_exc_unbound(co, oparg);
2435 goto error;
2436 }
2437 Py_INCREF(value);
2438 }
2439 PUSH(value);
2440 DISPATCH();
2441 }
2442
Benjamin Petersonddd19492018-09-16 22:38:02 -07002443 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002444 PyObject *cell = freevars[oparg];
2445 PyObject *value = PyCell_GET(cell);
2446 if (value == NULL) {
2447 format_exc_unbound(co, oparg);
2448 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002450 Py_INCREF(value);
2451 PUSH(value);
2452 DISPATCH();
2453 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002454
Benjamin Petersonddd19492018-09-16 22:38:02 -07002455 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002456 PyObject *v = POP();
2457 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002458 PyObject *oldobj = PyCell_GET(cell);
2459 PyCell_SET(cell, v);
2460 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002461 DISPATCH();
2462 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002463
Benjamin Petersonddd19492018-09-16 22:38:02 -07002464 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002465 PyObject *str;
2466 PyObject *empty = PyUnicode_New(0, 0);
2467 if (empty == NULL) {
2468 goto error;
2469 }
2470 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2471 Py_DECREF(empty);
2472 if (str == NULL)
2473 goto error;
2474 while (--oparg >= 0) {
2475 PyObject *item = POP();
2476 Py_DECREF(item);
2477 }
2478 PUSH(str);
2479 DISPATCH();
2480 }
2481
Benjamin Petersonddd19492018-09-16 22:38:02 -07002482 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002483 PyObject *tup = PyTuple_New(oparg);
2484 if (tup == NULL)
2485 goto error;
2486 while (--oparg >= 0) {
2487 PyObject *item = POP();
2488 PyTuple_SET_ITEM(tup, oparg, item);
2489 }
2490 PUSH(tup);
2491 DISPATCH();
2492 }
2493
Benjamin Petersonddd19492018-09-16 22:38:02 -07002494 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002495 PyObject *list = PyList_New(oparg);
2496 if (list == NULL)
2497 goto error;
2498 while (--oparg >= 0) {
2499 PyObject *item = POP();
2500 PyList_SET_ITEM(list, oparg, item);
2501 }
2502 PUSH(list);
2503 DISPATCH();
2504 }
2505
Benjamin Petersonddd19492018-09-16 22:38:02 -07002506 case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL):
2507 case TARGET(BUILD_TUPLE_UNPACK):
2508 case TARGET(BUILD_LIST_UNPACK): {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002509 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002510 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002511 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002512 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002513
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002514 if (sum == NULL)
2515 goto error;
2516
2517 for (i = oparg; i > 0; i--) {
2518 PyObject *none_val;
2519
2520 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2521 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002522 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002523 PyErr_ExceptionMatches(PyExc_TypeError))
2524 {
2525 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002526 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002527 Py_DECREF(sum);
2528 goto error;
2529 }
2530 Py_DECREF(none_val);
2531 }
2532
2533 if (convert_to_tuple) {
2534 return_value = PyList_AsTuple(sum);
2535 Py_DECREF(sum);
2536 if (return_value == NULL)
2537 goto error;
2538 }
2539 else {
2540 return_value = sum;
2541 }
2542
2543 while (oparg--)
2544 Py_DECREF(POP());
2545 PUSH(return_value);
2546 DISPATCH();
2547 }
2548
Benjamin Petersonddd19492018-09-16 22:38:02 -07002549 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002550 PyObject *set = PySet_New(NULL);
2551 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002552 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002553 if (set == NULL)
2554 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002555 for (i = oparg; i > 0; i--) {
2556 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002557 if (err == 0)
2558 err = PySet_Add(set, item);
2559 Py_DECREF(item);
2560 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002561 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002562 if (err != 0) {
2563 Py_DECREF(set);
2564 goto error;
2565 }
2566 PUSH(set);
2567 DISPATCH();
2568 }
2569
Benjamin Petersonddd19492018-09-16 22:38:02 -07002570 case TARGET(BUILD_SET_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002571 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002572 PyObject *sum = PySet_New(NULL);
2573 if (sum == NULL)
2574 goto error;
2575
2576 for (i = oparg; i > 0; i--) {
2577 if (_PySet_Update(sum, PEEK(i)) < 0) {
2578 Py_DECREF(sum);
2579 goto error;
2580 }
2581 }
2582
2583 while (oparg--)
2584 Py_DECREF(POP());
2585 PUSH(sum);
2586 DISPATCH();
2587 }
2588
Benjamin Petersonddd19492018-09-16 22:38:02 -07002589 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002590 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002591 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2592 if (map == NULL)
2593 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002594 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002595 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002596 PyObject *key = PEEK(2*i);
2597 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002598 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002599 if (err != 0) {
2600 Py_DECREF(map);
2601 goto error;
2602 }
2603 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002604
2605 while (oparg--) {
2606 Py_DECREF(POP());
2607 Py_DECREF(POP());
2608 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002609 PUSH(map);
2610 DISPATCH();
2611 }
2612
Benjamin Petersonddd19492018-09-16 22:38:02 -07002613 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002614 _Py_IDENTIFIER(__annotations__);
2615 int err;
2616 PyObject *ann_dict;
2617 if (f->f_locals == NULL) {
2618 PyErr_Format(PyExc_SystemError,
2619 "no locals found when setting up annotations");
2620 goto error;
2621 }
2622 /* check if __annotations__ in locals()... */
2623 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002624 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002625 &PyId___annotations__);
2626 if (ann_dict == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002627 if (PyErr_Occurred()) {
2628 goto error;
2629 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002630 /* ...if not, create a new one */
2631 ann_dict = PyDict_New();
2632 if (ann_dict == NULL) {
2633 goto error;
2634 }
2635 err = _PyDict_SetItemId(f->f_locals,
2636 &PyId___annotations__, ann_dict);
2637 Py_DECREF(ann_dict);
2638 if (err != 0) {
2639 goto error;
2640 }
2641 }
2642 }
2643 else {
2644 /* do the same if locals() is not a dict */
2645 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2646 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002647 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002648 }
2649 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2650 if (ann_dict == NULL) {
2651 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2652 goto error;
2653 }
2654 PyErr_Clear();
2655 ann_dict = PyDict_New();
2656 if (ann_dict == NULL) {
2657 goto error;
2658 }
2659 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2660 Py_DECREF(ann_dict);
2661 if (err != 0) {
2662 goto error;
2663 }
2664 }
2665 else {
2666 Py_DECREF(ann_dict);
2667 }
2668 }
2669 DISPATCH();
2670 }
2671
Benjamin Petersonddd19492018-09-16 22:38:02 -07002672 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002673 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002674 PyObject *map;
2675 PyObject *keys = TOP();
2676 if (!PyTuple_CheckExact(keys) ||
2677 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2678 PyErr_SetString(PyExc_SystemError,
2679 "bad BUILD_CONST_KEY_MAP keys argument");
2680 goto error;
2681 }
2682 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2683 if (map == NULL) {
2684 goto error;
2685 }
2686 for (i = oparg; i > 0; i--) {
2687 int err;
2688 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2689 PyObject *value = PEEK(i + 1);
2690 err = PyDict_SetItem(map, key, value);
2691 if (err != 0) {
2692 Py_DECREF(map);
2693 goto error;
2694 }
2695 }
2696
2697 Py_DECREF(POP());
2698 while (oparg--) {
2699 Py_DECREF(POP());
2700 }
2701 PUSH(map);
2702 DISPATCH();
2703 }
2704
Benjamin Petersonddd19492018-09-16 22:38:02 -07002705 case TARGET(BUILD_MAP_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002706 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002707 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002708 if (sum == NULL)
2709 goto error;
2710
2711 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002712 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002713 if (PyDict_Update(sum, arg) < 0) {
2714 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2715 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002716 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002717 arg->ob_type->tp_name);
2718 }
2719 Py_DECREF(sum);
2720 goto error;
2721 }
2722 }
2723
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002724 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002725 Py_DECREF(POP());
2726 PUSH(sum);
2727 DISPATCH();
2728 }
2729
Benjamin Petersonddd19492018-09-16 22:38:02 -07002730 case TARGET(BUILD_MAP_UNPACK_WITH_CALL): {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002731 Py_ssize_t i;
2732 PyObject *sum = PyDict_New();
2733 if (sum == NULL)
2734 goto error;
2735
2736 for (i = oparg; i > 0; i--) {
2737 PyObject *arg = PEEK(i);
2738 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002739 Py_DECREF(sum);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02002740 format_kwargs_error(PEEK(2 + oparg), arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002741 goto error;
2742 }
2743 }
2744
2745 while (oparg--)
2746 Py_DECREF(POP());
2747 PUSH(sum);
2748 DISPATCH();
2749 }
2750
Benjamin Petersonddd19492018-09-16 22:38:02 -07002751 case TARGET(MAP_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002752 PyObject *key = TOP();
2753 PyObject *value = SECOND();
2754 PyObject *map;
2755 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002756 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002757 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002758 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002759 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002760 Py_DECREF(value);
2761 Py_DECREF(key);
2762 if (err != 0)
2763 goto error;
2764 PREDICT(JUMP_ABSOLUTE);
2765 DISPATCH();
2766 }
2767
Benjamin Petersonddd19492018-09-16 22:38:02 -07002768 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002769 PyObject *name = GETITEM(names, oparg);
2770 PyObject *owner = TOP();
2771 PyObject *res = PyObject_GetAttr(owner, name);
2772 Py_DECREF(owner);
2773 SET_TOP(res);
2774 if (res == NULL)
2775 goto error;
2776 DISPATCH();
2777 }
2778
Benjamin Petersonddd19492018-09-16 22:38:02 -07002779 case TARGET(COMPARE_OP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002780 PyObject *right = POP();
2781 PyObject *left = TOP();
2782 PyObject *res = cmp_outcome(oparg, left, right);
2783 Py_DECREF(left);
2784 Py_DECREF(right);
2785 SET_TOP(res);
2786 if (res == NULL)
2787 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002788 PREDICT(POP_JUMP_IF_FALSE);
2789 PREDICT(POP_JUMP_IF_TRUE);
2790 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002791 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002792
Benjamin Petersonddd19492018-09-16 22:38:02 -07002793 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002794 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002795 PyObject *fromlist = POP();
2796 PyObject *level = TOP();
2797 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002798 res = import_name(f, name, fromlist, level);
2799 Py_DECREF(level);
2800 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002801 SET_TOP(res);
2802 if (res == NULL)
2803 goto error;
2804 DISPATCH();
2805 }
2806
Benjamin Petersonddd19492018-09-16 22:38:02 -07002807 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002808 PyObject *from = POP(), *locals;
2809 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002810 if (PyFrame_FastToLocalsWithError(f) < 0) {
2811 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002812 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002813 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002814
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002815 locals = f->f_locals;
2816 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002817 PyErr_SetString(PyExc_SystemError,
2818 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002819 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002820 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002821 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002822 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002823 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002824 Py_DECREF(from);
2825 if (err != 0)
2826 goto error;
2827 DISPATCH();
2828 }
Guido van Rossum25831651993-05-19 14:50:45 +00002829
Benjamin Petersonddd19492018-09-16 22:38:02 -07002830 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002831 PyObject *name = GETITEM(names, oparg);
2832 PyObject *from = TOP();
2833 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002834 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002835 PUSH(res);
2836 if (res == NULL)
2837 goto error;
2838 DISPATCH();
2839 }
Thomas Wouters52152252000-08-17 22:55:00 +00002840
Benjamin Petersonddd19492018-09-16 22:38:02 -07002841 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 JUMPBY(oparg);
2843 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002844 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002845
Benjamin Petersonddd19492018-09-16 22:38:02 -07002846 case TARGET(POP_JUMP_IF_FALSE): {
2847 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002848 PyObject *cond = POP();
2849 int err;
2850 if (cond == Py_True) {
2851 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002852 FAST_DISPATCH();
2853 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002854 if (cond == Py_False) {
2855 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002856 JUMPTO(oparg);
2857 FAST_DISPATCH();
2858 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002859 err = PyObject_IsTrue(cond);
2860 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002862 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 else if (err == 0)
2864 JUMPTO(oparg);
2865 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002866 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002867 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002868 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002869
Benjamin Petersonddd19492018-09-16 22:38:02 -07002870 case TARGET(POP_JUMP_IF_TRUE): {
2871 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002872 PyObject *cond = POP();
2873 int err;
2874 if (cond == Py_False) {
2875 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 FAST_DISPATCH();
2877 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002878 if (cond == Py_True) {
2879 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 JUMPTO(oparg);
2881 FAST_DISPATCH();
2882 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002883 err = PyObject_IsTrue(cond);
2884 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002885 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002886 JUMPTO(oparg);
2887 }
2888 else if (err == 0)
2889 ;
2890 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002891 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002892 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002893 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002894
Benjamin Petersonddd19492018-09-16 22:38:02 -07002895 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002896 PyObject *cond = TOP();
2897 int err;
2898 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002899 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002900 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 FAST_DISPATCH();
2902 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002903 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002904 JUMPTO(oparg);
2905 FAST_DISPATCH();
2906 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002907 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002909 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002910 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002911 }
2912 else if (err == 0)
2913 JUMPTO(oparg);
2914 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002915 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002916 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002917 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002918
Benjamin Petersonddd19492018-09-16 22:38:02 -07002919 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002920 PyObject *cond = TOP();
2921 int err;
2922 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002923 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002924 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 FAST_DISPATCH();
2926 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002927 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002928 JUMPTO(oparg);
2929 FAST_DISPATCH();
2930 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002931 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002932 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002933 JUMPTO(oparg);
2934 }
2935 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002936 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002937 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002938 }
2939 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002940 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002941 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002942 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002943
Benjamin Petersonddd19492018-09-16 22:38:02 -07002944 case TARGET(JUMP_ABSOLUTE): {
2945 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002947#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002948 /* Enabling this path speeds-up all while and for-loops by bypassing
2949 the per-loop checks for signals. By default, this should be turned-off
2950 because it prevents detection of a control-break in tight loops like
2951 "while 1: pass". Compile with this option turned-on when you need
2952 the speed-up and do not need break checking inside tight loops (ones
2953 that contain only instructions ending with FAST_DISPATCH).
2954 */
2955 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002956#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002957 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002958#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002959 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002960
Benjamin Petersonddd19492018-09-16 22:38:02 -07002961 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002962 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002963 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002964 PyObject *iter = PyObject_GetIter(iterable);
2965 Py_DECREF(iterable);
2966 SET_TOP(iter);
2967 if (iter == NULL)
2968 goto error;
2969 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002970 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002971 DISPATCH();
2972 }
2973
Benjamin Petersonddd19492018-09-16 22:38:02 -07002974 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04002975 /* before: [obj]; after [getiter(obj)] */
2976 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002977 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002978 if (PyCoro_CheckExact(iterable)) {
2979 /* `iterable` is a coroutine */
2980 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2981 /* and it is used in a 'yield from' expression of a
2982 regular generator. */
2983 Py_DECREF(iterable);
2984 SET_TOP(NULL);
2985 PyErr_SetString(PyExc_TypeError,
2986 "cannot 'yield from' a coroutine object "
2987 "in a non-coroutine generator");
2988 goto error;
2989 }
2990 }
2991 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002992 /* `iterable` is not a generator. */
2993 iter = PyObject_GetIter(iterable);
2994 Py_DECREF(iterable);
2995 SET_TOP(iter);
2996 if (iter == NULL)
2997 goto error;
2998 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002999 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003000 DISPATCH();
3001 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003002
Benjamin Petersonddd19492018-09-16 22:38:02 -07003003 case TARGET(FOR_ITER): {
3004 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003005 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003006 PyObject *iter = TOP();
3007 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3008 if (next != NULL) {
3009 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003010 PREDICT(STORE_FAST);
3011 PREDICT(UNPACK_SEQUENCE);
3012 DISPATCH();
3013 }
3014 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003015 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
3016 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08003017 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003018 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003019 PyErr_Clear();
3020 }
3021 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003022 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003023 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003024 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003025 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003026 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003027 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003028
Benjamin Petersonddd19492018-09-16 22:38:02 -07003029 case TARGET(SETUP_FINALLY): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003030 /* NOTE: If you add any new block-setup opcodes that
3031 are not try/except/finally handlers, you may need
3032 to update the PyGen_NeedsFinalizing() function.
3033 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003034
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003035 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003036 STACK_LEVEL());
3037 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003038 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003039
Benjamin Petersonddd19492018-09-16 22:38:02 -07003040 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003041 _Py_IDENTIFIER(__aexit__);
3042 _Py_IDENTIFIER(__aenter__);
3043
3044 PyObject *mgr = TOP();
3045 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3046 *enter;
3047 PyObject *res;
3048 if (exit == NULL)
3049 goto error;
3050 SET_TOP(exit);
3051 enter = special_lookup(mgr, &PyId___aenter__);
3052 Py_DECREF(mgr);
3053 if (enter == NULL)
3054 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003055 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003056 Py_DECREF(enter);
3057 if (res == NULL)
3058 goto error;
3059 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003060 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003061 DISPATCH();
3062 }
3063
Benjamin Petersonddd19492018-09-16 22:38:02 -07003064 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003065 PyObject *res = POP();
3066 /* Setup the finally block before pushing the result
3067 of __aenter__ on the stack. */
3068 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3069 STACK_LEVEL());
3070 PUSH(res);
3071 DISPATCH();
3072 }
3073
Benjamin Petersonddd19492018-09-16 22:38:02 -07003074 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003075 _Py_IDENTIFIER(__exit__);
3076 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003077 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08003078 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003079 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08003080 if (enter == NULL)
3081 goto error;
3082 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003083 if (exit == NULL) {
3084 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003085 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003086 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003087 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003088 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003089 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003090 Py_DECREF(enter);
3091 if (res == NULL)
3092 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003093 /* Setup the finally block before pushing the result
3094 of __enter__ on the stack. */
3095 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3096 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003097
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003098 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003099 DISPATCH();
3100 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003101
Benjamin Petersonddd19492018-09-16 22:38:02 -07003102 case TARGET(WITH_CLEANUP_START): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003103 /* At the top of the stack are 1 or 6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003104 how/why we entered the finally clause:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003105 - TOP = NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003106 - (TOP, SECOND, THIRD) = exc_info()
3107 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003108 Below them is EXIT, the context.__exit__ or context.__aexit__
3109 bound method.
3110 In the first case, we must call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003111 EXIT(None, None, None)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003112 otherwise we must call
3113 EXIT(TOP, SECOND, THIRD)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003114
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003115 In the first case, we remove EXIT from the
3116 stack, leaving TOP, and push TOP on the stack.
3117 Otherwise we shift the bottom 3 values of the
3118 stack down, replace the empty spot with NULL, and push
3119 None on the stack.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003120
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003121 Finally we push the result of the call.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003122 */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003123 PyObject *stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003124 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003125 PyObject *exc, *val, *tb, *res;
3126
3127 val = tb = Py_None;
3128 exc = TOP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003129 if (exc == NULL) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003130 STACK_SHRINK(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003131 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003132 SET_TOP(exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003133 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003134 }
3135 else {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003136 assert(PyExceptionClass_Check(exc));
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003137 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003138 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003139 val = SECOND();
3140 tb = THIRD();
3141 tp2 = FOURTH();
3142 exc2 = PEEK(5);
3143 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003144 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003145 SET_VALUE(7, tb2);
3146 SET_VALUE(6, exc2);
3147 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003148 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3149 SET_FOURTH(NULL);
3150 /* We just shifted the stack down, so we have
3151 to tell the except handler block that the
3152 values are lower than it expects. */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003153 assert(f->f_iblock > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154 block = &f->f_blockstack[f->f_iblock - 1];
3155 assert(block->b_type == EXCEPT_HANDLER);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003156 assert(block->b_level > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003157 block->b_level--;
3158 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003159
3160 stack[0] = exc;
3161 stack[1] = val;
3162 stack[2] = tb;
3163 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003165 if (res == NULL)
3166 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003167
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003168 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003169 PUSH(exc);
3170 PUSH(res);
3171 PREDICT(WITH_CLEANUP_FINISH);
3172 DISPATCH();
3173 }
3174
Benjamin Petersonddd19492018-09-16 22:38:02 -07003175 case TARGET(WITH_CLEANUP_FINISH): {
3176 PREDICTED(WITH_CLEANUP_FINISH);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003177 /* TOP = the result of calling the context.__exit__ bound method
3178 SECOND = either None or exception type
3179
3180 If SECOND is None below is NULL or the return address,
3181 otherwise below are 7 values representing an exception.
3182 */
Yury Selivanov75445082015-05-11 22:57:16 -04003183 PyObject *res = POP();
3184 PyObject *exc = POP();
3185 int err;
3186
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003187 if (exc != Py_None)
3188 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003189 else
3190 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003191
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003192 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003193 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003195 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003196 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003197 else if (err > 0) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003198 /* There was an exception and a True return.
3199 * We must manually unwind the EXCEPT_HANDLER block
3200 * which was created when the exception was caught,
Quan Tian3bd0d622018-10-20 05:30:03 +08003201 * otherwise the stack will be in an inconsistent state.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003202 */
3203 PyTryBlock *b = PyFrame_BlockPop(f);
3204 assert(b->b_type == EXCEPT_HANDLER);
3205 UNWIND_EXCEPT_HANDLER(b);
3206 PUSH(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003207 }
3208 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003209 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003210 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003211
Benjamin Petersonddd19492018-09-16 22:38:02 -07003212 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003213 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003214 PyObject *name = GETITEM(names, oparg);
3215 PyObject *obj = TOP();
3216 PyObject *meth = NULL;
3217
3218 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3219
Yury Selivanovf2392132016-12-13 19:03:51 -05003220 if (meth == NULL) {
3221 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003222 goto error;
3223 }
3224
3225 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003226 /* We can bypass temporary bound method object.
3227 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003228
INADA Naoki015bce62017-01-16 17:23:30 +09003229 meth | self | arg1 | ... | argN
3230 */
3231 SET_TOP(meth);
3232 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003233 }
3234 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003235 /* meth is not an unbound method (but a regular attr, or
3236 something was returned by a descriptor protocol). Set
3237 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003238 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003239
3240 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003241 */
INADA Naoki015bce62017-01-16 17:23:30 +09003242 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003243 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003244 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003245 }
3246 DISPATCH();
3247 }
3248
Benjamin Petersonddd19492018-09-16 22:38:02 -07003249 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003250 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003251 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003252
3253 sp = stack_pointer;
3254
INADA Naoki015bce62017-01-16 17:23:30 +09003255 meth = PEEK(oparg + 2);
3256 if (meth == NULL) {
3257 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3258 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003259
3260 Stack layout:
3261
INADA Naoki015bce62017-01-16 17:23:30 +09003262 ... | NULL | callable | arg1 | ... | argN
3263 ^- TOP()
3264 ^- (-oparg)
3265 ^- (-oparg-1)
3266 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003267
Ville Skyttä49b27342017-08-03 09:00:59 +03003268 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003269 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003270 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003271 res = call_function(&sp, oparg, NULL);
3272 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003273 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003274 }
3275 else {
3276 /* This is a method call. Stack layout:
3277
INADA Naoki015bce62017-01-16 17:23:30 +09003278 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003279 ^- TOP()
3280 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003281 ^- (-oparg-1)
3282 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003283
INADA Naoki015bce62017-01-16 17:23:30 +09003284 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003285 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003286 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003287 */
3288 res = call_function(&sp, oparg + 1, NULL);
3289 stack_pointer = sp;
3290 }
3291
3292 PUSH(res);
3293 if (res == NULL)
3294 goto error;
3295 DISPATCH();
3296 }
3297
Benjamin Petersonddd19492018-09-16 22:38:02 -07003298 case TARGET(CALL_FUNCTION): {
3299 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003300 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003301 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003302 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003303 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003304 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003305 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003306 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003307 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003308 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003309 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003310
Benjamin Petersonddd19492018-09-16 22:38:02 -07003311 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003312 PyObject **sp, *res, *names;
3313
3314 names = POP();
3315 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003316 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003317 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003318 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003319 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003320 Py_DECREF(names);
3321
3322 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003323 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003324 }
3325 DISPATCH();
3326 }
3327
Benjamin Petersonddd19492018-09-16 22:38:02 -07003328 case TARGET(CALL_FUNCTION_EX): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003329 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003330 if (oparg & 0x01) {
3331 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003332 if (!PyDict_CheckExact(kwargs)) {
3333 PyObject *d = PyDict_New();
3334 if (d == NULL)
3335 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003336 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003337 Py_DECREF(d);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003338 format_kwargs_error(SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003339 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003340 goto error;
3341 }
3342 Py_DECREF(kwargs);
3343 kwargs = d;
3344 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003345 assert(PyDict_CheckExact(kwargs));
3346 }
3347 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003348 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003349 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003350 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003351 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003352 goto error;
3353 }
3354 Py_SETREF(callargs, PySequence_Tuple(callargs));
3355 if (callargs == NULL) {
3356 goto error;
3357 }
3358 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003359 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003360
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003361 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003362 Py_DECREF(func);
3363 Py_DECREF(callargs);
3364 Py_XDECREF(kwargs);
3365
3366 SET_TOP(result);
3367 if (result == NULL) {
3368 goto error;
3369 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003370 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003371 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003372
Benjamin Petersonddd19492018-09-16 22:38:02 -07003373 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003374 PyObject *qualname = POP();
3375 PyObject *codeobj = POP();
3376 PyFunctionObject *func = (PyFunctionObject *)
3377 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003378
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003379 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003380 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003381 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003382 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003383 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003384
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003385 if (oparg & 0x08) {
3386 assert(PyTuple_CheckExact(TOP()));
3387 func ->func_closure = POP();
3388 }
3389 if (oparg & 0x04) {
3390 assert(PyDict_CheckExact(TOP()));
3391 func->func_annotations = POP();
3392 }
3393 if (oparg & 0x02) {
3394 assert(PyDict_CheckExact(TOP()));
3395 func->func_kwdefaults = POP();
3396 }
3397 if (oparg & 0x01) {
3398 assert(PyTuple_CheckExact(TOP()));
3399 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003400 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003401
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003402 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003403 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003404 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003405
Benjamin Petersonddd19492018-09-16 22:38:02 -07003406 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003407 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003408 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003409 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003410 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003411 step = NULL;
3412 stop = POP();
3413 start = TOP();
3414 slice = PySlice_New(start, stop, step);
3415 Py_DECREF(start);
3416 Py_DECREF(stop);
3417 Py_XDECREF(step);
3418 SET_TOP(slice);
3419 if (slice == NULL)
3420 goto error;
3421 DISPATCH();
3422 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003423
Benjamin Petersonddd19492018-09-16 22:38:02 -07003424 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003425 /* Handles f-string value formatting. */
3426 PyObject *result;
3427 PyObject *fmt_spec;
3428 PyObject *value;
3429 PyObject *(*conv_fn)(PyObject *);
3430 int which_conversion = oparg & FVC_MASK;
3431 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3432
3433 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003434 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003435
3436 /* See if any conversion is specified. */
3437 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003438 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003439 case FVC_STR: conv_fn = PyObject_Str; break;
3440 case FVC_REPR: conv_fn = PyObject_Repr; break;
3441 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003442 default:
3443 PyErr_Format(PyExc_SystemError,
3444 "unexpected conversion flag %d",
3445 which_conversion);
3446 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003447 }
3448
3449 /* If there's a conversion function, call it and replace
3450 value with that result. Otherwise, just use value,
3451 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003452 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003453 result = conv_fn(value);
3454 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003455 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003456 Py_XDECREF(fmt_spec);
3457 goto error;
3458 }
3459 value = result;
3460 }
3461
3462 /* If value is a unicode object, and there's no fmt_spec,
3463 then we know the result of format(value) is value
3464 itself. In that case, skip calling format(). I plan to
3465 move this optimization in to PyObject_Format()
3466 itself. */
3467 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3468 /* Do nothing, just transfer ownership to result. */
3469 result = value;
3470 } else {
3471 /* Actually call format(). */
3472 result = PyObject_Format(value, fmt_spec);
3473 Py_DECREF(value);
3474 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003475 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003476 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003477 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003478 }
3479
Eric V. Smith135d5f42016-02-05 18:23:08 -05003480 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003481 DISPATCH();
3482 }
3483
Benjamin Petersonddd19492018-09-16 22:38:02 -07003484 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003485 int oldoparg = oparg;
3486 NEXTOPARG();
3487 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003488 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003489 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003490
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003491
Antoine Pitrou042b1282010-08-13 21:15:58 +00003492#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003493 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003494#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003495 default:
3496 fprintf(stderr,
3497 "XXX lineno: %d, opcode: %d\n",
3498 PyFrame_GetLineNumber(f),
3499 opcode);
3500 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003501 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003503 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003504
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003505 /* This should never be reached. Every opcode should end with DISPATCH()
3506 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003507 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003508
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003509error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003510 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003511#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003512 if (!PyErr_Occurred())
3513 PyErr_SetString(PyExc_SystemError,
3514 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003515#else
3516 assert(PyErr_Occurred());
3517#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003518
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003519 /* Log traceback info. */
3520 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003521
Benjamin Peterson51f46162013-01-23 08:38:47 -05003522 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003523 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3524 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003525
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003526exception_unwind:
3527 /* Unwind stacks if an exception occurred */
3528 while (f->f_iblock > 0) {
3529 /* Pop the current block. */
3530 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003532 if (b->b_type == EXCEPT_HANDLER) {
3533 UNWIND_EXCEPT_HANDLER(b);
3534 continue;
3535 }
3536 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003537 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003538 PyObject *exc, *val, *tb;
3539 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003540 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003541 /* Beware, this invalidates all b->b_* fields */
3542 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003543 PUSH(exc_info->exc_traceback);
3544 PUSH(exc_info->exc_value);
3545 if (exc_info->exc_type != NULL) {
3546 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003547 }
3548 else {
3549 Py_INCREF(Py_None);
3550 PUSH(Py_None);
3551 }
3552 PyErr_Fetch(&exc, &val, &tb);
3553 /* Make the raw exception data
3554 available to the handler,
3555 so a program can emulate the
3556 Python main loop. */
3557 PyErr_NormalizeException(
3558 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003559 if (tb != NULL)
3560 PyException_SetTraceback(val, tb);
3561 else
3562 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003563 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003564 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003565 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003566 exc_info->exc_value = val;
3567 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003568 if (tb == NULL)
3569 tb = Py_None;
3570 Py_INCREF(tb);
3571 PUSH(tb);
3572 PUSH(val);
3573 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003574 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003575 /* Resume normal execution */
3576 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003577 }
3578 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003579
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003580 /* End the loop as we still have an error */
3581 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003582 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003584 /* Pop remaining stack entries. */
3585 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003586 PyObject *o = POP();
3587 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003588 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003589
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003590 assert(retval == NULL);
3591 assert(PyErr_Occurred());
Guido van Rossumac7be682001-01-17 15:42:30 +00003592
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003593return_or_yield:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003594 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003595 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003596 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3597 tstate, f, PyTrace_RETURN, retval)) {
3598 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003599 }
3600 }
3601 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003602 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3603 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003604 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003605 }
3606 }
3607 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003609 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003610exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003611 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3612 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003613 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003614 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003615 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003616
Victor Stinnerefde1462015-03-21 15:04:43 +01003617 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003618}
3619
Benjamin Petersonb204a422011-06-05 22:04:07 -05003620static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003621format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3622{
3623 int err;
3624 Py_ssize_t len = PyList_GET_SIZE(names);
3625 PyObject *name_str, *comma, *tail, *tmp;
3626
3627 assert(PyList_CheckExact(names));
3628 assert(len >= 1);
3629 /* Deal with the joys of natural language. */
3630 switch (len) {
3631 case 1:
3632 name_str = PyList_GET_ITEM(names, 0);
3633 Py_INCREF(name_str);
3634 break;
3635 case 2:
3636 name_str = PyUnicode_FromFormat("%U and %U",
3637 PyList_GET_ITEM(names, len - 2),
3638 PyList_GET_ITEM(names, len - 1));
3639 break;
3640 default:
3641 tail = PyUnicode_FromFormat(", %U, and %U",
3642 PyList_GET_ITEM(names, len - 2),
3643 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003644 if (tail == NULL)
3645 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003646 /* Chop off the last two objects in the list. This shouldn't actually
3647 fail, but we can't be too careful. */
3648 err = PyList_SetSlice(names, len - 2, len, NULL);
3649 if (err == -1) {
3650 Py_DECREF(tail);
3651 return;
3652 }
3653 /* Stitch everything up into a nice comma-separated list. */
3654 comma = PyUnicode_FromString(", ");
3655 if (comma == NULL) {
3656 Py_DECREF(tail);
3657 return;
3658 }
3659 tmp = PyUnicode_Join(comma, names);
3660 Py_DECREF(comma);
3661 if (tmp == NULL) {
3662 Py_DECREF(tail);
3663 return;
3664 }
3665 name_str = PyUnicode_Concat(tmp, tail);
3666 Py_DECREF(tmp);
3667 Py_DECREF(tail);
3668 break;
3669 }
3670 if (name_str == NULL)
3671 return;
3672 PyErr_Format(PyExc_TypeError,
3673 "%U() missing %i required %s argument%s: %U",
3674 co->co_name,
3675 len,
3676 kind,
3677 len == 1 ? "" : "s",
3678 name_str);
3679 Py_DECREF(name_str);
3680}
3681
3682static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003683missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003684 PyObject **fastlocals)
3685{
Victor Stinner74319ae2016-08-25 00:04:09 +02003686 Py_ssize_t i, j = 0;
3687 Py_ssize_t start, end;
3688 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003689 const char *kind = positional ? "positional" : "keyword-only";
3690 PyObject *missing_names;
3691
3692 /* Compute the names of the arguments that are missing. */
3693 missing_names = PyList_New(missing);
3694 if (missing_names == NULL)
3695 return;
3696 if (positional) {
3697 start = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003698 end = co->co_posonlyargcount + co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003699 }
3700 else {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003701 start = co->co_posonlyargcount + co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003702 end = start + co->co_kwonlyargcount;
3703 }
3704 for (i = start; i < end; i++) {
3705 if (GETLOCAL(i) == NULL) {
3706 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3707 PyObject *name = PyObject_Repr(raw);
3708 if (name == NULL) {
3709 Py_DECREF(missing_names);
3710 return;
3711 }
3712 PyList_SET_ITEM(missing_names, j++, name);
3713 }
3714 }
3715 assert(j == missing);
3716 format_missing(kind, co, missing_names);
3717 Py_DECREF(missing_names);
3718}
3719
3720static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003721too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3722 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003723{
3724 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003725 Py_ssize_t kwonly_given = 0;
3726 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003727 PyObject *sig, *kwonly_sig;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003728 Py_ssize_t co_posonlyargcount = co->co_posonlyargcount;
Victor Stinner74319ae2016-08-25 00:04:09 +02003729 Py_ssize_t co_argcount = co->co_argcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003730 Py_ssize_t total_positional = co_argcount + co_posonlyargcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003731
Benjamin Petersone109c702011-06-24 09:37:26 -05003732 assert((co->co_flags & CO_VARARGS) == 0);
3733 /* Count missing keyword-only args. */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003734 for (i = total_positional; i < total_positional + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003735 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003736 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003737 }
3738 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003739 if (defcount) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003740 Py_ssize_t atleast = total_positional - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003741 plural = 1;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003742 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, total_positional);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003743 }
3744 else {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003745 plural = (total_positional != 1);
3746 sig = PyUnicode_FromFormat("%zd", total_positional);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003747 }
3748 if (sig == NULL)
3749 return;
3750 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003751 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3752 kwonly_sig = PyUnicode_FromFormat(format,
3753 given != 1 ? "s" : "",
3754 kwonly_given,
3755 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003756 if (kwonly_sig == NULL) {
3757 Py_DECREF(sig);
3758 return;
3759 }
3760 }
3761 else {
3762 /* This will not fail. */
3763 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003764 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003765 }
3766 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003767 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003768 co->co_name,
3769 sig,
3770 plural ? "s" : "",
3771 given,
3772 kwonly_sig,
3773 given == 1 && !kwonly_given ? "was" : "were");
3774 Py_DECREF(sig);
3775 Py_DECREF(kwonly_sig);
3776}
3777
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003778static int
3779positional_only_passed_as_keyword(PyCodeObject *co, Py_ssize_t kwcount,
3780 PyObject* const* kwnames)
3781{
3782 int posonly_conflicts = 0;
3783 PyObject* posonly_names = PyList_New(0);
3784
3785 for(int k=0; k < co->co_posonlyargcount; k++){
3786 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
3787
3788 for (int k2=0; k2<kwcount; k2++){
3789 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
3790 PyObject* kwname = kwnames[k2];
3791 if (kwname == posonly_name){
3792 if(PyList_Append(posonly_names, kwname) != 0) {
3793 goto fail;
3794 }
3795 posonly_conflicts++;
3796 continue;
3797 }
3798
3799 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
3800
3801 if ( cmp > 0) {
3802 if(PyList_Append(posonly_names, kwname) != 0) {
3803 goto fail;
3804 }
3805 posonly_conflicts++;
3806 } else if (cmp < 0) {
3807 goto fail;
3808 }
3809
3810 }
3811 }
3812 if (posonly_conflicts) {
3813 PyObject* comma = PyUnicode_FromString(", ");
3814 if (comma == NULL) {
3815 goto fail;
3816 }
3817 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
3818 Py_DECREF(comma);
3819 if (error_names == NULL) {
3820 goto fail;
3821 }
3822 PyErr_Format(PyExc_TypeError,
3823 "%U() got some positional-only arguments passed"
3824 " as keyword arguments: '%U'",
3825 co->co_name, error_names);
3826 Py_DECREF(error_names);
3827 goto fail;
3828 }
3829
3830 Py_DECREF(posonly_names);
3831 return 0;
3832
3833fail:
3834 Py_XDECREF(posonly_names);
3835 return 1;
3836
3837}
3838
Guido van Rossumc2e20742006-02-27 22:32:47 +00003839/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003840 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003841 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003842
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003843PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003844_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003845 PyObject *const *args, Py_ssize_t argcount,
3846 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003847 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003848 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003849 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003850 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003851{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003852 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003853 PyFrameObject *f;
3854 PyObject *retval = NULL;
3855 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003856 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003857 PyObject *x, *u;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003858 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount + co->co_posonlyargcount;
3859 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003860 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003862 if (globals == NULL) {
3863 PyErr_SetString(PyExc_SystemError,
3864 "PyEval_EvalCodeEx: NULL globals");
3865 return NULL;
3866 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003867
Victor Stinnerc7020012016-08-16 23:40:29 +02003868 /* Create the frame */
Victor Stinner50b48572018-11-01 01:51:40 +01003869 tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003870 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003871 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003872 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003873 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003874 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003875 fastlocals = f->f_localsplus;
3876 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003877
Victor Stinnerc7020012016-08-16 23:40:29 +02003878 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003879 if (co->co_flags & CO_VARKEYWORDS) {
3880 kwdict = PyDict_New();
3881 if (kwdict == NULL)
3882 goto fail;
3883 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003884 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003885 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003886 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003887 SETLOCAL(i, kwdict);
3888 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003889 else {
3890 kwdict = NULL;
3891 }
3892
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003893 /* Copy positional only arguments into local variables */
3894 if (argcount > co->co_argcount + co->co_posonlyargcount) {
3895 n = co->co_posonlyargcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003896 }
3897 else {
3898 n = argcount;
3899 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003900 for (j = 0; j < n; j++) {
3901 x = args[j];
3902 Py_INCREF(x);
3903 SETLOCAL(j, x);
3904 }
3905
3906
3907 /* Copy positional arguments into local variables */
3908 if (argcount > co->co_argcount + co->co_posonlyargcount) {
3909 n += co->co_argcount;
3910 }
3911 else {
3912 n = argcount;
3913 }
3914 for (i = j; i < n; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003915 x = args[i];
3916 Py_INCREF(x);
3917 SETLOCAL(i, x);
3918 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003919
3920 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003921 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05003922 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003923 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003924 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003925 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003926 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003927 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003928
Serhiy Storchakab7281052016-09-12 00:52:40 +03003929 /* Handle keyword arguments passed as two strided arrays */
3930 kwcount *= kwstep;
3931 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003932 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003933 PyObject *keyword = kwnames[i];
3934 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003935 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003936
Benjamin Petersonb204a422011-06-05 22:04:07 -05003937 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3938 PyErr_Format(PyExc_TypeError,
3939 "%U() keywords must be strings",
3940 co->co_name);
3941 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003942 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003943
Benjamin Petersonb204a422011-06-05 22:04:07 -05003944 /* Speed hack: do raw pointer compares. As names are
3945 normally interned this should almost always hit. */
3946 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003947 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003948 PyObject *name = co_varnames[j];
3949 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003950 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003951 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003952 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003953
Benjamin Petersonb204a422011-06-05 22:04:07 -05003954 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003955 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003956 PyObject *name = co_varnames[j];
3957 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3958 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003959 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003960 }
3961 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003962 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003963 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003964 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003965
Victor Stinner231d1f32017-01-11 02:12:06 +01003966 assert(j >= total_args);
3967 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003968
3969 if (co->co_posonlyargcount && positional_only_passed_as_keyword(co, kwcount, kwnames)) {
3970 goto fail;
3971 }
3972
Benjamin Petersonb204a422011-06-05 22:04:07 -05003973 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003974 "%U() got an unexpected keyword argument '%S'",
3975 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003976 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003977 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003978
Christian Heimes0bd447f2013-07-20 14:48:10 +02003979 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3980 goto fail;
3981 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003982 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003983
Benjamin Petersonb204a422011-06-05 22:04:07 -05003984 kw_found:
3985 if (GETLOCAL(j) != NULL) {
3986 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003987 "%U() got multiple values for argument '%S'",
3988 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003989 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003990 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003991 Py_INCREF(value);
3992 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003993 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003994
3995 /* Check the number of positional arguments */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003996 if ((argcount > co->co_argcount + co->co_posonlyargcount) && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003997 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003998 goto fail;
3999 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004000
4001 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004002 if (argcount < co->co_posonlyargcount + co->co_argcount) {
4003 Py_ssize_t m = co->co_posonlyargcount + co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004004 Py_ssize_t missing = 0;
4005 for (i = argcount; i < m; i++) {
4006 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004007 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004008 }
4009 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004010 if (missing) {
4011 missing_arguments(co, missing, defcount, fastlocals);
4012 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004013 }
4014 if (n > m)
4015 i = n - m;
4016 else
4017 i = 0;
4018 for (; i < defcount; i++) {
4019 if (GETLOCAL(m+i) == NULL) {
4020 PyObject *def = defs[i];
4021 Py_INCREF(def);
4022 SETLOCAL(m+i, def);
4023 }
4024 }
4025 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004026
4027 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004028 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004029 Py_ssize_t missing = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004030 for (i = co->co_posonlyargcount + co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004031 PyObject *name;
4032 if (GETLOCAL(i) != NULL)
4033 continue;
4034 name = PyTuple_GET_ITEM(co->co_varnames, i);
4035 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004036 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004037 if (def) {
4038 Py_INCREF(def);
4039 SETLOCAL(i, def);
4040 continue;
4041 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004042 else if (PyErr_Occurred()) {
4043 goto fail;
4044 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004045 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004046 missing++;
4047 }
4048 if (missing) {
4049 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004050 goto fail;
4051 }
4052 }
4053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004054 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004055 vars into frame. */
4056 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004057 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004058 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004059 /* Possibly account for the cell variable being an argument. */
4060 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004061 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004062 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004063 /* Clear the local copy. */
4064 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004065 }
4066 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004067 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004068 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004069 if (c == NULL)
4070 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004071 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004072 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004073
4074 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004075 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4076 PyObject *o = PyTuple_GET_ITEM(closure, i);
4077 Py_INCREF(o);
4078 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004079 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004080
Yury Selivanoveb636452016-09-08 22:01:51 -07004081 /* Handle generator/coroutine/asynchronous generator */
4082 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004083 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04004084 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004085 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004086
4087 if (is_coro && tstate->in_coroutine_wrapper) {
4088 assert(coro_wrapper != NULL);
4089 PyErr_Format(PyExc_RuntimeError,
4090 "coroutine wrapper %.200R attempted "
4091 "to recursively wrap %.200R",
4092 coro_wrapper,
4093 co);
4094 goto fail;
4095 }
Yury Selivanov75445082015-05-11 22:57:16 -04004096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004097 /* Don't need to keep the reference to f_back, it will be set
4098 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004099 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004101 /* Create a new generator that owns the ready to run frame
4102 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004103 if (is_coro) {
4104 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004105 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4106 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004107 } else {
4108 gen = PyGen_NewWithQualName(f, name, qualname);
4109 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004110 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004111 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004112 }
INADA Naoki9c157762016-12-26 18:52:46 +09004113
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004114 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004115
Yury Selivanov94c22632015-06-04 10:16:51 -04004116 if (is_coro && coro_wrapper != NULL) {
4117 PyObject *wrapped;
4118 tstate->in_coroutine_wrapper = 1;
4119 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4120 tstate->in_coroutine_wrapper = 0;
4121 return wrapped;
4122 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004123
Yury Selivanov75445082015-05-11 22:57:16 -04004124 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004125 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004126
Victor Stinner59a73272016-12-09 18:51:13 +01004127 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004128
Thomas Woutersce272b62007-09-19 21:19:28 +00004129fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004131 /* decref'ing the frame can cause __del__ methods to get invoked,
4132 which can call back into Python. While we're done with the
4133 current Python frame (f), the associated C stack is still in use,
4134 so recursion_depth must be boosted for the duration.
4135 */
4136 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09004137 if (Py_REFCNT(f) > 1) {
4138 Py_DECREF(f);
4139 _PyObject_GC_TRACK(f);
4140 }
4141 else {
4142 ++tstate->recursion_depth;
4143 Py_DECREF(f);
4144 --tstate->recursion_depth;
4145 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004146 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004147}
4148
Victor Stinner40ee3012014-06-16 15:59:28 +02004149PyObject *
4150PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004151 PyObject *const *args, int argcount,
4152 PyObject *const *kws, int kwcount,
4153 PyObject *const *defs, int defcount,
4154 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004155{
4156 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004157 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004158 kws, kws != NULL ? kws + 1 : NULL,
4159 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004160 defs, defcount,
4161 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004162 NULL, NULL);
4163}
Tim Peters5ca576e2001-06-18 22:08:13 +00004164
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004165static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004166special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004167{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004168 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004169 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004170 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004171 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004172 return NULL;
4173 }
4174 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004175}
4176
4177
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004178/* Logic for the raise statement (too complicated for inlining).
4179 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004180static int
Collin Winter828f04a2007-08-31 00:04:24 +00004181do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004183 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004185 if (exc == NULL) {
4186 /* Reraise */
Victor Stinner50b48572018-11-01 01:51:40 +01004187 PyThreadState *tstate = _PyThreadState_GET();
Mark Shannonae3087c2017-10-22 22:41:51 +01004188 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004189 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004190 type = exc_info->exc_type;
4191 value = exc_info->exc_value;
4192 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004193 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004194 PyErr_SetString(PyExc_RuntimeError,
4195 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004196 return 0;
4197 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004198 Py_XINCREF(type);
4199 Py_XINCREF(value);
4200 Py_XINCREF(tb);
4201 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004202 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004203 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004204
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004205 /* We support the following forms of raise:
4206 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004207 raise <instance>
4208 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004210 if (PyExceptionClass_Check(exc)) {
4211 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004212 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004213 if (value == NULL)
4214 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004215 if (!PyExceptionInstance_Check(value)) {
4216 PyErr_Format(PyExc_TypeError,
4217 "calling %R should have returned an instance of "
4218 "BaseException, not %R",
4219 type, Py_TYPE(value));
4220 goto raise_error;
4221 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004222 }
4223 else if (PyExceptionInstance_Check(exc)) {
4224 value = exc;
4225 type = PyExceptionInstance_Class(exc);
4226 Py_INCREF(type);
4227 }
4228 else {
4229 /* Not something you can raise. You get an exception
4230 anyway, just not what you specified :-) */
4231 Py_DECREF(exc);
4232 PyErr_SetString(PyExc_TypeError,
4233 "exceptions must derive from BaseException");
4234 goto raise_error;
4235 }
Collin Winter828f04a2007-08-31 00:04:24 +00004236
Serhiy Storchakac0191582016-09-27 11:37:10 +03004237 assert(type != NULL);
4238 assert(value != NULL);
4239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004240 if (cause) {
4241 PyObject *fixed_cause;
4242 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004243 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004244 if (fixed_cause == NULL)
4245 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004246 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004247 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004248 else if (PyExceptionInstance_Check(cause)) {
4249 fixed_cause = cause;
4250 }
4251 else if (cause == Py_None) {
4252 Py_DECREF(cause);
4253 fixed_cause = NULL;
4254 }
4255 else {
4256 PyErr_SetString(PyExc_TypeError,
4257 "exception causes must derive from "
4258 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004259 goto raise_error;
4260 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004261 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004262 }
Collin Winter828f04a2007-08-31 00:04:24 +00004263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004264 PyErr_SetObject(type, value);
4265 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004266 Py_DECREF(value);
4267 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004268 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004269
4270raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004271 Py_XDECREF(value);
4272 Py_XDECREF(type);
4273 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004274 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004275}
4276
Tim Petersd6d010b2001-06-21 02:49:55 +00004277/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004278 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004279
Guido van Rossum0368b722007-05-11 16:50:42 +00004280 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4281 with a variable target.
4282*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004283
Barry Warsawe42b18f1997-08-25 22:13:04 +00004284static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004285unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004287 int i = 0, j = 0;
4288 Py_ssize_t ll = 0;
4289 PyObject *it; /* iter(v) */
4290 PyObject *w;
4291 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004293 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004295 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004296 if (it == NULL) {
4297 if (PyErr_ExceptionMatches(PyExc_TypeError) &&
4298 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4299 {
4300 PyErr_Format(PyExc_TypeError,
4301 "cannot unpack non-iterable %.200s object",
4302 v->ob_type->tp_name);
4303 }
4304 return 0;
4305 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004307 for (; i < argcnt; i++) {
4308 w = PyIter_Next(it);
4309 if (w == NULL) {
4310 /* Iterator done, via error or exhaustion. */
4311 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004312 if (argcntafter == -1) {
4313 PyErr_Format(PyExc_ValueError,
4314 "not enough values to unpack (expected %d, got %d)",
4315 argcnt, i);
4316 }
4317 else {
4318 PyErr_Format(PyExc_ValueError,
4319 "not enough values to unpack "
4320 "(expected at least %d, got %d)",
4321 argcnt + argcntafter, i);
4322 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004323 }
4324 goto Error;
4325 }
4326 *--sp = w;
4327 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004329 if (argcntafter == -1) {
4330 /* We better have exhausted the iterator now. */
4331 w = PyIter_Next(it);
4332 if (w == NULL) {
4333 if (PyErr_Occurred())
4334 goto Error;
4335 Py_DECREF(it);
4336 return 1;
4337 }
4338 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004339 PyErr_Format(PyExc_ValueError,
4340 "too many values to unpack (expected %d)",
4341 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004342 goto Error;
4343 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004345 l = PySequence_List(it);
4346 if (l == NULL)
4347 goto Error;
4348 *--sp = l;
4349 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004351 ll = PyList_GET_SIZE(l);
4352 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004353 PyErr_Format(PyExc_ValueError,
4354 "not enough values to unpack (expected at least %d, got %zd)",
4355 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004356 goto Error;
4357 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004359 /* Pop the "after-variable" args off the list. */
4360 for (j = argcntafter; j > 0; j--, i++) {
4361 *--sp = PyList_GET_ITEM(l, ll - j);
4362 }
4363 /* Resize the list. */
4364 Py_SIZE(l) = ll - argcntafter;
4365 Py_DECREF(it);
4366 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004367
Tim Petersd6d010b2001-06-21 02:49:55 +00004368Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004369 for (; i > 0; i--, sp++)
4370 Py_DECREF(*sp);
4371 Py_XDECREF(it);
4372 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004373}
4374
4375
Guido van Rossum96a42c81992-01-12 02:29:51 +00004376#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004377static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004378prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004379{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004380 printf("%s ", str);
4381 if (PyObject_Print(v, stdout, 0) != 0)
4382 PyErr_Clear(); /* Don't know what else to do */
4383 printf("\n");
4384 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004385}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004386#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004387
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004388static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004389call_exc_trace(Py_tracefunc func, PyObject *self,
4390 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004391{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004392 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004393 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004394 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004395 if (value == NULL) {
4396 value = Py_None;
4397 Py_INCREF(value);
4398 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004399 PyErr_NormalizeException(&type, &value, &orig_traceback);
4400 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004401 arg = PyTuple_Pack(3, type, value, traceback);
4402 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004403 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004404 return;
4405 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004406 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004407 Py_DECREF(arg);
4408 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004409 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004410 else {
4411 Py_XDECREF(type);
4412 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004413 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004415}
4416
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004417static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004418call_trace_protected(Py_tracefunc func, PyObject *obj,
4419 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004420 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004421{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004422 PyObject *type, *value, *traceback;
4423 int err;
4424 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004425 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004426 if (err == 0)
4427 {
4428 PyErr_Restore(type, value, traceback);
4429 return 0;
4430 }
4431 else {
4432 Py_XDECREF(type);
4433 Py_XDECREF(value);
4434 Py_XDECREF(traceback);
4435 return -1;
4436 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004437}
4438
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004439static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004440call_trace(Py_tracefunc func, PyObject *obj,
4441 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004442 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004443{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004444 int result;
4445 if (tstate->tracing)
4446 return 0;
4447 tstate->tracing++;
4448 tstate->use_tracing = 0;
4449 result = func(obj, frame, what, arg);
4450 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4451 || (tstate->c_profilefunc != NULL));
4452 tstate->tracing--;
4453 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004454}
4455
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004456PyObject *
4457_PyEval_CallTracing(PyObject *func, PyObject *args)
4458{
Victor Stinner50b48572018-11-01 01:51:40 +01004459 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004460 int save_tracing = tstate->tracing;
4461 int save_use_tracing = tstate->use_tracing;
4462 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004464 tstate->tracing = 0;
4465 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4466 || (tstate->c_profilefunc != NULL));
4467 result = PyObject_Call(func, args, NULL);
4468 tstate->tracing = save_tracing;
4469 tstate->use_tracing = save_use_tracing;
4470 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004471}
4472
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004473/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004474static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004475maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004476 PyThreadState *tstate, PyFrameObject *frame,
4477 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004478{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004479 int result = 0;
4480 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004482 /* If the last instruction executed isn't in the current
4483 instruction window, reset the window.
4484 */
4485 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4486 PyAddrPair bounds;
4487 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4488 &bounds);
4489 *instr_lb = bounds.ap_lower;
4490 *instr_ub = bounds.ap_upper;
4491 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004492 /* If the last instruction falls at the start of a line or if it
4493 represents a jump backwards, update the frame's line number and
4494 then call the trace function if we're tracing source lines.
4495 */
4496 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004497 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004498 if (frame->f_trace_lines) {
4499 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4500 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004501 }
George King20faa682017-10-18 17:44:22 -07004502 /* Always emit an opcode event if we're tracing all opcodes. */
4503 if (frame->f_trace_opcodes) {
4504 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4505 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004506 *instr_prev = frame->f_lasti;
4507 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004508}
4509
Fred Drake5755ce62001-06-27 19:19:46 +00004510void
4511PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004512{
Victor Stinner50b48572018-11-01 01:51:40 +01004513 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004514 PyObject *temp = tstate->c_profileobj;
4515 Py_XINCREF(arg);
4516 tstate->c_profilefunc = NULL;
4517 tstate->c_profileobj = NULL;
4518 /* Must make sure that tracing is not ignored if 'temp' is freed */
4519 tstate->use_tracing = tstate->c_tracefunc != NULL;
4520 Py_XDECREF(temp);
4521 tstate->c_profilefunc = func;
4522 tstate->c_profileobj = arg;
4523 /* Flag that tracing or profiling is turned on */
4524 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004525}
4526
4527void
4528PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4529{
Victor Stinner50b48572018-11-01 01:51:40 +01004530 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004531 PyObject *temp = tstate->c_traceobj;
4532 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4533 Py_XINCREF(arg);
4534 tstate->c_tracefunc = NULL;
4535 tstate->c_traceobj = NULL;
4536 /* Must make sure that profiling is not ignored if 'temp' is freed */
4537 tstate->use_tracing = tstate->c_profilefunc != NULL;
4538 Py_XDECREF(temp);
4539 tstate->c_tracefunc = func;
4540 tstate->c_traceobj = arg;
4541 /* Flag that tracing or profiling is turned on */
4542 tstate->use_tracing = ((func != NULL)
4543 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004544}
4545
Yury Selivanov75445082015-05-11 22:57:16 -04004546void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004547_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4548{
4549 assert(new_depth >= 0);
Victor Stinner50b48572018-11-01 01:51:40 +01004550 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004551 tstate->coroutine_origin_tracking_depth = new_depth;
4552}
4553
4554int
4555_PyEval_GetCoroutineOriginTrackingDepth(void)
4556{
Victor Stinner50b48572018-11-01 01:51:40 +01004557 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004558 return tstate->coroutine_origin_tracking_depth;
4559}
4560
4561void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004562_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004563{
Victor Stinner50b48572018-11-01 01:51:40 +01004564 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004565
Yury Selivanov75445082015-05-11 22:57:16 -04004566 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004567 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004568}
4569
4570PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004571_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004572{
Victor Stinner50b48572018-11-01 01:51:40 +01004573 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004574 return tstate->coroutine_wrapper;
4575}
4576
Yury Selivanoveb636452016-09-08 22:01:51 -07004577void
4578_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4579{
Victor Stinner50b48572018-11-01 01:51:40 +01004580 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004581
4582 Py_XINCREF(firstiter);
4583 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4584}
4585
4586PyObject *
4587_PyEval_GetAsyncGenFirstiter(void)
4588{
Victor Stinner50b48572018-11-01 01:51:40 +01004589 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004590 return tstate->async_gen_firstiter;
4591}
4592
4593void
4594_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4595{
Victor Stinner50b48572018-11-01 01:51:40 +01004596 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004597
4598 Py_XINCREF(finalizer);
4599 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4600}
4601
4602PyObject *
4603_PyEval_GetAsyncGenFinalizer(void)
4604{
Victor Stinner50b48572018-11-01 01:51:40 +01004605 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004606 return tstate->async_gen_finalizer;
4607}
4608
Guido van Rossumb209a111997-04-29 18:18:01 +00004609PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004610PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004611{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004612 PyFrameObject *current_frame = PyEval_GetFrame();
4613 if (current_frame == NULL)
Victor Stinnercaba55b2018-08-03 15:33:52 +02004614 return _PyInterpreterState_GET_UNSAFE()->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004615 else
4616 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004617}
4618
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004619/* Convenience function to get a builtin from its name */
4620PyObject *
4621_PyEval_GetBuiltinId(_Py_Identifier *name)
4622{
4623 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4624 if (attr) {
4625 Py_INCREF(attr);
4626 }
4627 else if (!PyErr_Occurred()) {
4628 PyErr_SetObject(PyExc_AttributeError, _PyUnicode_FromId(name));
4629 }
4630 return attr;
4631}
4632
Guido van Rossumb209a111997-04-29 18:18:01 +00004633PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004634PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004635{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004636 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004637 if (current_frame == NULL) {
4638 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004639 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004640 }
4641
4642 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4643 return NULL;
4644
4645 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004646 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004647}
4648
Guido van Rossumb209a111997-04-29 18:18:01 +00004649PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004650PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004651{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004652 PyFrameObject *current_frame = PyEval_GetFrame();
4653 if (current_frame == NULL)
4654 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004655
4656 assert(current_frame->f_globals != NULL);
4657 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004658}
4659
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004660PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004661PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004662{
Victor Stinner50b48572018-11-01 01:51:40 +01004663 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004664 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004665}
4666
Guido van Rossum6135a871995-01-09 17:53:26 +00004667int
Tim Peters5ba58662001-07-16 02:29:45 +00004668PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004669{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004670 PyFrameObject *current_frame = PyEval_GetFrame();
4671 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004673 if (current_frame != NULL) {
4674 const int codeflags = current_frame->f_code->co_flags;
4675 const int compilerflags = codeflags & PyCF_MASK;
4676 if (compilerflags) {
4677 result = 1;
4678 cf->cf_flags |= compilerflags;
4679 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004680#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004681 if (codeflags & CO_GENERATOR_ALLOWED) {
4682 result = 1;
4683 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4684 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004685#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004686 }
4687 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004688}
4689
Guido van Rossum3f5da241990-12-20 15:06:42 +00004690
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004691const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004692PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004693{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004694 if (PyMethod_Check(func))
4695 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4696 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004697 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004698 else if (PyCFunction_Check(func))
4699 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4700 else
4701 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004702}
4703
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004704const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004705PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004706{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004707 if (PyMethod_Check(func))
4708 return "()";
4709 else if (PyFunction_Check(func))
4710 return "()";
4711 else if (PyCFunction_Check(func))
4712 return "()";
4713 else
4714 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004715}
4716
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004717#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004718if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004719 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4720 tstate, tstate->frame, \
4721 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004722 x = NULL; \
4723 } \
4724 else { \
4725 x = call; \
4726 if (tstate->c_profilefunc != NULL) { \
4727 if (x == NULL) { \
4728 call_trace_protected(tstate->c_profilefunc, \
4729 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004730 tstate, tstate->frame, \
4731 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004732 /* XXX should pass (type, value, tb) */ \
4733 } else { \
4734 if (call_trace(tstate->c_profilefunc, \
4735 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004736 tstate, tstate->frame, \
4737 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004738 Py_DECREF(x); \
4739 x = NULL; \
4740 } \
4741 } \
4742 } \
4743 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004744} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004745 x = call; \
4746 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004747
Victor Stinner415c5102017-01-11 00:54:57 +01004748/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4749 to reduce the stack consumption. */
4750Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004751call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004752{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004753 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004754 PyObject *func = *pfunc;
4755 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004756 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4757 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004758 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004760 /* Always dispatch PyCFunction first, because these are
4761 presumed to be the most frequent callable object.
4762 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004763 if (PyCFunction_Check(func)) {
Victor Stinner50b48572018-11-01 01:51:40 +01004764 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004765 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004766 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004767 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
Victor Stinner50b48572018-11-01 01:51:40 +01004768 PyThreadState *tstate = _PyThreadState_GET();
jdemeyer56868f92018-07-21 10:30:59 +02004769 if (nargs > 0 && tstate->use_tracing) {
4770 /* We need to create a temporary bound method as argument
4771 for profiling.
4772
4773 If nargs == 0, then this cannot work because we have no
4774 "self". In any case, the call itself would raise
4775 TypeError (foo needs an argument), so we just skip
4776 profiling. */
4777 PyObject *self = stack[0];
4778 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
jdemeyer147d9552018-07-23 18:41:20 +02004779 if (func != NULL) {
4780 C_TRACE(x, _PyCFunction_FastCallKeywords(func,
4781 stack+1, nargs-1,
4782 kwnames));
4783 Py_DECREF(func);
INADA Naoki93fac8d2017-03-07 14:24:37 +09004784 }
jdemeyer147d9552018-07-23 18:41:20 +02004785 else {
4786 x = NULL;
4787 }
INADA Naoki93fac8d2017-03-07 14:24:37 +09004788 }
4789 else {
4790 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4791 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004792 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004793 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004794 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004795 /* Optimize access to bound methods. Reuse the Python stack
4796 to pass 'self' as the first argument, replace 'func'
4797 with 'self'. It avoids the creation of a new temporary tuple
4798 for arguments (to replace func with self) when the method uses
4799 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004800 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004801 Py_INCREF(self);
4802 func = PyMethod_GET_FUNCTION(func);
4803 Py_INCREF(func);
4804 Py_SETREF(*pfunc, self);
4805 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004806 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004807 }
4808 else {
4809 Py_INCREF(func);
4810 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004811
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004812 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004813 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004814 }
4815 else {
4816 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4817 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004818 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004819 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004820
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004821 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4822
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004823 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004824 while ((*pp_stack) > pfunc) {
4825 w = EXT_POP(*pp_stack);
4826 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004827 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004829 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004830}
4831
Jeremy Hylton52820442001-01-03 23:52:36 +00004832static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004833do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004834{
jdemeyere89de732018-09-19 12:06:20 +02004835 PyObject *result;
4836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004837 if (PyCFunction_Check(func)) {
Victor Stinner50b48572018-11-01 01:51:40 +01004838 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004839 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004840 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004841 }
jdemeyere89de732018-09-19 12:06:20 +02004842 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
Victor Stinner50b48572018-11-01 01:51:40 +01004843 PyThreadState *tstate = _PyThreadState_GET();
jdemeyere89de732018-09-19 12:06:20 +02004844 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4845 if (nargs > 0 && tstate->use_tracing) {
4846 /* We need to create a temporary bound method as argument
4847 for profiling.
4848
4849 If nargs == 0, then this cannot work because we have no
4850 "self". In any case, the call itself would raise
4851 TypeError (foo needs an argument), so we just skip
4852 profiling. */
4853 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4854 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4855 if (func == NULL) {
4856 return NULL;
4857 }
4858
4859 C_TRACE(result, _PyCFunction_FastCallDict(func,
Victor Stinnerd17a6932018-11-09 16:56:48 +01004860 &_PyTuple_ITEMS(callargs)[1],
jdemeyere89de732018-09-19 12:06:20 +02004861 nargs - 1,
4862 kwdict));
4863 Py_DECREF(func);
4864 return result;
4865 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004866 }
jdemeyere89de732018-09-19 12:06:20 +02004867 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004868}
4869
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004870/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004871 nb_index slot defined, and store in *pi.
4872 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004873 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004874 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004875*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004876int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004877_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004878{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004879 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004880 Py_ssize_t x;
4881 if (PyIndex_Check(v)) {
4882 x = PyNumber_AsSsize_t(v, NULL);
4883 if (x == -1 && PyErr_Occurred())
4884 return 0;
4885 }
4886 else {
4887 PyErr_SetString(PyExc_TypeError,
4888 "slice indices must be integers or "
4889 "None or have an __index__ method");
4890 return 0;
4891 }
4892 *pi = x;
4893 }
4894 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004895}
4896
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004897int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004898_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004899{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004900 Py_ssize_t x;
4901 if (PyIndex_Check(v)) {
4902 x = PyNumber_AsSsize_t(v, NULL);
4903 if (x == -1 && PyErr_Occurred())
4904 return 0;
4905 }
4906 else {
4907 PyErr_SetString(PyExc_TypeError,
4908 "slice indices must be integers or "
4909 "have an __index__ method");
4910 return 0;
4911 }
4912 *pi = x;
4913 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004914}
4915
4916
Guido van Rossum486364b2007-06-30 05:01:58 +00004917#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004918 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004919
Guido van Rossumb209a111997-04-29 18:18:01 +00004920static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004921cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004922{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004923 int res = 0;
4924 switch (op) {
4925 case PyCmp_IS:
4926 res = (v == w);
4927 break;
4928 case PyCmp_IS_NOT:
4929 res = (v != w);
4930 break;
4931 case PyCmp_IN:
4932 res = PySequence_Contains(w, v);
4933 if (res < 0)
4934 return NULL;
4935 break;
4936 case PyCmp_NOT_IN:
4937 res = PySequence_Contains(w, v);
4938 if (res < 0)
4939 return NULL;
4940 res = !res;
4941 break;
4942 case PyCmp_EXC_MATCH:
4943 if (PyTuple_Check(w)) {
4944 Py_ssize_t i, length;
4945 length = PyTuple_Size(w);
4946 for (i = 0; i < length; i += 1) {
4947 PyObject *exc = PyTuple_GET_ITEM(w, i);
4948 if (!PyExceptionClass_Check(exc)) {
4949 PyErr_SetString(PyExc_TypeError,
4950 CANNOT_CATCH_MSG);
4951 return NULL;
4952 }
4953 }
4954 }
4955 else {
4956 if (!PyExceptionClass_Check(w)) {
4957 PyErr_SetString(PyExc_TypeError,
4958 CANNOT_CATCH_MSG);
4959 return NULL;
4960 }
4961 }
4962 res = PyErr_GivenExceptionMatches(v, w);
4963 break;
4964 default:
4965 return PyObject_RichCompare(v, w, op);
4966 }
4967 v = res ? Py_True : Py_False;
4968 Py_INCREF(v);
4969 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004970}
4971
Thomas Wouters52152252000-08-17 22:55:00 +00004972static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004973import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4974{
4975 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004976 PyObject *import_func, *res;
4977 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004978
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004979 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004980 if (import_func == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004981 if (!PyErr_Occurred()) {
4982 PyErr_SetString(PyExc_ImportError, "__import__ not found");
4983 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004984 return NULL;
4985 }
4986
4987 /* Fast path for not overloaded __import__. */
Victor Stinnercaba55b2018-08-03 15:33:52 +02004988 if (import_func == _PyInterpreterState_GET_UNSAFE()->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004989 int ilevel = _PyLong_AsInt(level);
4990 if (ilevel == -1 && PyErr_Occurred()) {
4991 return NULL;
4992 }
4993 res = PyImport_ImportModuleLevelObject(
4994 name,
4995 f->f_globals,
4996 f->f_locals == NULL ? Py_None : f->f_locals,
4997 fromlist,
4998 ilevel);
4999 return res;
5000 }
5001
5002 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005003
5004 stack[0] = name;
5005 stack[1] = f->f_globals;
5006 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5007 stack[3] = fromlist;
5008 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005009 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005010 Py_DECREF(import_func);
5011 return res;
5012}
5013
5014static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00005015import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005016{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005017 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005018 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08005019 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005020
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005021 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005022 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005023 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005024 /* Issue #17636: in case this failed because of a circular relative
5025 import, try to fallback on reading the module directly from
5026 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005027 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005028 if (pkgname == NULL) {
5029 goto error;
5030 }
Oren Milman6db70332017-09-19 14:23:01 +03005031 if (!PyUnicode_Check(pkgname)) {
5032 Py_CLEAR(pkgname);
5033 goto error;
5034 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005035 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005036 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005037 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005038 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005039 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005040 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005041 Py_DECREF(fullmodname);
Stefan Krah027b09c2019-03-25 21:50:58 +01005042 if (x == NULL && !PyErr_Occurred()) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005043 goto error;
5044 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005045 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005046 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005047 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005048 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005049 if (pkgname == NULL) {
5050 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5051 if (pkgname_or_unknown == NULL) {
5052 Py_XDECREF(pkgpath);
5053 return NULL;
5054 }
5055 } else {
5056 pkgname_or_unknown = pkgname;
5057 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005058
5059 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
5060 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08005061 errmsg = PyUnicode_FromFormat(
5062 "cannot import name %R from %R (unknown location)",
5063 name, pkgname_or_unknown
5064 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005065 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005066 PyErr_SetImportError(errmsg, pkgname, NULL);
5067 }
5068 else {
5069 errmsg = PyUnicode_FromFormat(
5070 "cannot import name %R from %R (%S)",
5071 name, pkgname_or_unknown, pkgpath
5072 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005073 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005074 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005075 }
5076
Xiang Zhang4830f582017-03-21 11:13:42 +08005077 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005078 Py_XDECREF(pkgname_or_unknown);
5079 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005080 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005081}
Guido van Rossumac7be682001-01-17 15:42:30 +00005082
Thomas Wouters52152252000-08-17 22:55:00 +00005083static int
5084import_all_from(PyObject *locals, PyObject *v)
5085{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005086 _Py_IDENTIFIER(__all__);
5087 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005088 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005089 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005090 int skip_leading_underscores = 0;
5091 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005092
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005093 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5094 return -1; /* Unexpected error */
5095 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005096 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005097 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5098 return -1;
5099 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005100 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005101 PyErr_SetString(PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005102 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005103 return -1;
5104 }
5105 all = PyMapping_Keys(dict);
5106 Py_DECREF(dict);
5107 if (all == NULL)
5108 return -1;
5109 skip_leading_underscores = 1;
5110 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005112 for (pos = 0, err = 0; ; pos++) {
5113 name = PySequence_GetItem(all, pos);
5114 if (name == NULL) {
5115 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5116 err = -1;
5117 else
5118 PyErr_Clear();
5119 break;
5120 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005121 if (!PyUnicode_Check(name)) {
5122 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5123 if (modname == NULL) {
5124 Py_DECREF(name);
5125 err = -1;
5126 break;
5127 }
5128 if (!PyUnicode_Check(modname)) {
5129 PyErr_Format(PyExc_TypeError,
5130 "module __name__ must be a string, not %.100s",
5131 Py_TYPE(modname)->tp_name);
5132 }
5133 else {
5134 PyErr_Format(PyExc_TypeError,
5135 "%s in %U.%s must be str, not %.100s",
5136 skip_leading_underscores ? "Key" : "Item",
5137 modname,
5138 skip_leading_underscores ? "__dict__" : "__all__",
5139 Py_TYPE(name)->tp_name);
5140 }
5141 Py_DECREF(modname);
5142 Py_DECREF(name);
5143 err = -1;
5144 break;
5145 }
5146 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005147 if (PyUnicode_READY(name) == -1) {
5148 Py_DECREF(name);
5149 err = -1;
5150 break;
5151 }
5152 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5153 Py_DECREF(name);
5154 continue;
5155 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005156 }
5157 value = PyObject_GetAttr(v, name);
5158 if (value == NULL)
5159 err = -1;
5160 else if (PyDict_CheckExact(locals))
5161 err = PyDict_SetItem(locals, name, value);
5162 else
5163 err = PyObject_SetItem(locals, name, value);
5164 Py_DECREF(name);
5165 Py_XDECREF(value);
5166 if (err != 0)
5167 break;
5168 }
5169 Py_DECREF(all);
5170 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005171}
5172
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005173static int
5174check_args_iterable(PyObject *func, PyObject *args)
5175{
5176 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
5177 PyErr_Format(PyExc_TypeError,
5178 "%.200s%.200s argument after * "
5179 "must be an iterable, not %.200s",
5180 PyEval_GetFuncName(func),
5181 PyEval_GetFuncDesc(func),
5182 args->ob_type->tp_name);
5183 return -1;
5184 }
5185 return 0;
5186}
5187
5188static void
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005189format_kwargs_error(PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005190{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005191 /* _PyDict_MergeEx raises attribute
5192 * error (percolated from an attempt
5193 * to get 'keys' attribute) instead of
5194 * a type error if its second argument
5195 * is not a mapping.
5196 */
5197 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
5198 PyErr_Format(PyExc_TypeError,
5199 "%.200s%.200s argument after ** "
5200 "must be a mapping, not %.200s",
5201 PyEval_GetFuncName(func),
5202 PyEval_GetFuncDesc(func),
5203 kwargs->ob_type->tp_name);
5204 }
5205 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
5206 PyObject *exc, *val, *tb;
5207 PyErr_Fetch(&exc, &val, &tb);
5208 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
5209 PyObject *key = PyTuple_GET_ITEM(val, 0);
5210 if (!PyUnicode_Check(key)) {
5211 PyErr_Format(PyExc_TypeError,
5212 "%.200s%.200s keywords must be strings",
5213 PyEval_GetFuncName(func),
5214 PyEval_GetFuncDesc(func));
5215 } else {
5216 PyErr_Format(PyExc_TypeError,
5217 "%.200s%.200s got multiple "
5218 "values for keyword argument '%U'",
5219 PyEval_GetFuncName(func),
5220 PyEval_GetFuncDesc(func),
5221 key);
5222 }
5223 Py_XDECREF(exc);
5224 Py_XDECREF(val);
5225 Py_XDECREF(tb);
5226 }
5227 else {
5228 PyErr_Restore(exc, val, tb);
5229 }
5230 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005231}
5232
Guido van Rossumac7be682001-01-17 15:42:30 +00005233static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005234format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005235{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005236 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005238 if (!obj)
5239 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005240
Serhiy Storchaka06515832016-11-20 09:13:07 +02005241 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005242 if (!obj_str)
5243 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005245 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005246}
Guido van Rossum950361c1997-01-24 13:49:28 +00005247
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005248static void
5249format_exc_unbound(PyCodeObject *co, int oparg)
5250{
5251 PyObject *name;
5252 /* Don't stomp existing exception */
5253 if (PyErr_Occurred())
5254 return;
5255 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5256 name = PyTuple_GET_ITEM(co->co_cellvars,
5257 oparg);
5258 format_exc_check_arg(
5259 PyExc_UnboundLocalError,
5260 UNBOUNDLOCAL_ERROR_MSG,
5261 name);
5262 } else {
5263 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5264 PyTuple_GET_SIZE(co->co_cellvars));
5265 format_exc_check_arg(PyExc_NameError,
5266 UNBOUNDFREE_ERROR_MSG, name);
5267 }
5268}
5269
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005270static void
5271format_awaitable_error(PyTypeObject *type, int prevopcode)
5272{
5273 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5274 if (prevopcode == BEFORE_ASYNC_WITH) {
5275 PyErr_Format(PyExc_TypeError,
5276 "'async with' received an object from __aenter__ "
5277 "that does not implement __await__: %.100s",
5278 type->tp_name);
5279 }
5280 else if (prevopcode == WITH_CLEANUP_START) {
5281 PyErr_Format(PyExc_TypeError,
5282 "'async with' received an object from __aexit__ "
5283 "that does not implement __await__: %.100s",
5284 type->tp_name);
5285 }
5286 }
5287}
5288
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005289static PyObject *
5290unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005291 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005292{
5293 PyObject *res;
5294 if (Py_REFCNT(v) == 2) {
5295 /* In the common case, there are 2 references to the value
5296 * stored in 'variable' when the += is performed: one on the
5297 * value stack (in 'v') and one still stored in the
5298 * 'variable'. We try to delete the variable now to reduce
5299 * the refcnt to 1.
5300 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005301 int opcode, oparg;
5302 NEXTOPARG();
5303 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005304 case STORE_FAST:
5305 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005306 PyObject **fastlocals = f->f_localsplus;
5307 if (GETLOCAL(oparg) == v)
5308 SETLOCAL(oparg, NULL);
5309 break;
5310 }
5311 case STORE_DEREF:
5312 {
5313 PyObject **freevars = (f->f_localsplus +
5314 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005315 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005316 if (PyCell_GET(c) == v) {
5317 PyCell_SET(c, NULL);
5318 Py_DECREF(v);
5319 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005320 break;
5321 }
5322 case STORE_NAME:
5323 {
5324 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005325 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005326 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005327 if (locals && PyDict_CheckExact(locals)) {
5328 PyObject *w = PyDict_GetItemWithError(locals, name);
5329 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
5330 (w == NULL && PyErr_Occurred()))
5331 {
5332 Py_DECREF(v);
5333 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005334 }
5335 }
5336 break;
5337 }
5338 }
5339 }
5340 res = v;
5341 PyUnicode_Append(&res, w);
5342 return res;
5343}
5344
Guido van Rossum950361c1997-01-24 13:49:28 +00005345#ifdef DYNAMIC_EXECUTION_PROFILE
5346
Skip Montanarof118cb12001-10-15 20:51:38 +00005347static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005348getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005349{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005350 int i;
5351 PyObject *l = PyList_New(256);
5352 if (l == NULL) return NULL;
5353 for (i = 0; i < 256; i++) {
5354 PyObject *x = PyLong_FromLong(a[i]);
5355 if (x == NULL) {
5356 Py_DECREF(l);
5357 return NULL;
5358 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005359 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005360 }
5361 for (i = 0; i < 256; i++)
5362 a[i] = 0;
5363 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005364}
5365
5366PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005367_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005368{
5369#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005370 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005371#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005372 int i;
5373 PyObject *l = PyList_New(257);
5374 if (l == NULL) return NULL;
5375 for (i = 0; i < 257; i++) {
5376 PyObject *x = getarray(dxpairs[i]);
5377 if (x == NULL) {
5378 Py_DECREF(l);
5379 return NULL;
5380 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005381 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005382 }
5383 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005384#endif
5385}
5386
5387#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005388
5389Py_ssize_t
5390_PyEval_RequestCodeExtraIndex(freefunc free)
5391{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005392 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005393 Py_ssize_t new_index;
5394
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005395 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005396 return -1;
5397 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005398 new_index = interp->co_extra_user_count++;
5399 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005400 return new_index;
5401}
Łukasz Langaa785c872016-09-09 17:37:37 -07005402
5403static void
5404dtrace_function_entry(PyFrameObject *f)
5405{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005406 const char *filename;
5407 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005408 int lineno;
5409
5410 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5411 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5412 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5413
5414 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5415}
5416
5417static void
5418dtrace_function_return(PyFrameObject *f)
5419{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005420 const char *filename;
5421 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005422 int lineno;
5423
5424 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5425 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5426 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5427
5428 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5429}
5430
5431/* DTrace equivalent of maybe_call_line_trace. */
5432static void
5433maybe_dtrace_line(PyFrameObject *frame,
5434 int *instr_lb, int *instr_ub, int *instr_prev)
5435{
5436 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005437 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005438
5439 /* If the last instruction executed isn't in the current
5440 instruction window, reset the window.
5441 */
5442 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5443 PyAddrPair bounds;
5444 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5445 &bounds);
5446 *instr_lb = bounds.ap_lower;
5447 *instr_ub = bounds.ap_upper;
5448 }
5449 /* If the last instruction falls at the start of a line or if
5450 it represents a jump backwards, update the frame's line
5451 number and call the trace function. */
5452 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5453 frame->f_lineno = line;
5454 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5455 if (!co_filename)
5456 co_filename = "?";
5457 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5458 if (!co_name)
5459 co_name = "?";
5460 PyDTrace_LINE(co_filename, co_name, line);
5461 }
5462 *instr_prev = frame->f_lasti;
5463}