blob: 342dc10af6a629b80ceb4017a7b584fe3f589a9b [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);
Guido van Rossum374a9221991-04-04 10:40:29 +000079
Paul Prescode68140d2000-08-30 20:25:01 +000080#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000082#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000084#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 "free variable '%.200s' referenced before assignment" \
86 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000087
Guido van Rossum950361c1997-01-24 13:49:28 +000088/* Dynamic execution profile */
89#ifdef DYNAMIC_EXECUTION_PROFILE
90#ifdef DXPAIRS
91static long dxpairs[257][256];
92#define dxp dxpairs[256]
93#else
94static long dxp[256];
95#endif
96#endif
97
Eric Snow2ebc5ce2017-09-07 23:51:28 -060098#define GIL_REQUEST _Py_atomic_load_relaxed(&_PyRuntime.ceval.gil_drop_request)
Benjamin Petersond2be5b42010-09-10 22:47:02 +000099
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000100/* This can set eval_breaker to 0 even though gil_drop_request became
101 1. We believe this is all right because the eval loop will release
102 the GIL eventually anyway. */
Eric Snowb75b1a352019-04-12 10:20:10 -0600103#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 _Py_atomic_store_relaxed( \
Eric Snowb75b1a352019-04-12 10:20:10 -0600105 &_PyRuntime.ceval.eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000106 GIL_REQUEST | \
Eric Snowfdf282d2019-01-11 14:26:55 -0700107 _Py_atomic_load_relaxed(&_PyRuntime.ceval.signals_pending) | \
Eric Snowb75b1a352019-04-12 10:20:10 -0600108 _Py_atomic_load_relaxed(&_PyRuntime.ceval.pending.calls_to_do) | \
109 _PyRuntime.ceval.pending.async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000110
Eric Snowb75b1a352019-04-12 10:20:10 -0600111#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600113 _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 1); \
Eric Snowb75b1a352019-04-12 10:20:10 -0600114 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000116
Eric Snowb75b1a352019-04-12 10:20:10 -0600117#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600119 _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 0); \
Eric Snowb75b1a352019-04-12 10:20:10 -0600120 COMPUTE_EVAL_BREAKER(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000122
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000123/* Pending calls are only modified under pending_lock */
Eric Snowb75b1a352019-04-12 10:20:10 -0600124#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 do { \
Eric Snowb75b1a352019-04-12 10:20:10 -0600126 _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 1); \
127 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000129
Eric Snowb75b1a352019-04-12 10:20:10 -0600130#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 do { \
Eric Snowb75b1a352019-04-12 10:20:10 -0600132 _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 0); \
133 COMPUTE_EVAL_BREAKER(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000135
Eric Snowfdf282d2019-01-11 14:26:55 -0700136#define SIGNAL_PENDING_SIGNALS() \
137 do { \
138 _Py_atomic_store_relaxed(&_PyRuntime.ceval.signals_pending, 1); \
Eric Snowb75b1a352019-04-12 10:20:10 -0600139 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700140 } while (0)
141
142#define UNSIGNAL_PENDING_SIGNALS() \
143 do { \
144 _Py_atomic_store_relaxed(&_PyRuntime.ceval.signals_pending, 0); \
Eric Snowb75b1a352019-04-12 10:20:10 -0600145 COMPUTE_EVAL_BREAKER(); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700146 } while (0)
147
Eric Snowb75b1a352019-04-12 10:20:10 -0600148#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 do { \
Eric Snowb75b1a352019-04-12 10:20:10 -0600150 _PyRuntime.ceval.pending.async_exc = 1; \
151 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000153
Eric Snowb75b1a352019-04-12 10:20:10 -0600154#define UNSIGNAL_ASYNC_EXC() \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600155 do { \
Eric Snowb75b1a352019-04-12 10:20:10 -0600156 _PyRuntime.ceval.pending.async_exc = 0; \
157 COMPUTE_EVAL_BREAKER(); \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600158 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000159
160
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000161#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000162#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000163#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000164#include "pythread.h"
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000165#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000166
Tim Peters7f468f22004-10-11 02:40:51 +0000167int
168PyEval_ThreadsInitialized(void)
169{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000171}
172
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000173void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000174PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000175{
Victor Stinnera7126792019-03-19 14:19:38 +0100176 if (gil_created()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100178 }
179
Inada Naoki001fee12019-02-20 10:00:09 +0900180 PyThread_init_thread();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 create_gil();
Victor Stinner50b48572018-11-01 01:51:40 +0100182 take_gil(_PyThreadState_GET());
Eric Snow8479a342019-03-08 23:44:33 -0700183
Eric Snowb75b1a352019-04-12 10:20:10 -0600184 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
185 if (_PyRuntime.ceval.pending.lock == NULL) {
186 Py_FatalError("Can't initialize threads for pending calls");
187 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000188}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000189
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000190void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000191_PyEval_FiniThreads(void)
192{
Victor Stinnera7126792019-03-19 14:19:38 +0100193 if (!gil_created()) {
Antoine Pitrou1df15362010-09-13 14:16:46 +0000194 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100195 }
196
Antoine Pitrou1df15362010-09-13 14:16:46 +0000197 destroy_gil();
198 assert(!gil_created());
Eric Snowb75b1a352019-04-12 10:20:10 -0600199
200 if (_PyRuntime.ceval.pending.lock != NULL) {
201 PyThread_free_lock(_PyRuntime.ceval.pending.lock);
202 _PyRuntime.ceval.pending.lock = NULL;
203 }
Antoine Pitrou1df15362010-09-13 14:16:46 +0000204}
205
206void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000207PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000208{
Victor Stinner50b48572018-11-01 01:51:40 +0100209 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 if (tstate == NULL)
211 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
212 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000213}
214
215void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000216PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000217{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100219 We therefore avoid PyThreadState_Get() which dumps a fatal error
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 in debug mode.
221 */
Victor Stinner50b48572018-11-01 01:51:40 +0100222 drop_gil(_PyThreadState_GET());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000223}
224
225void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000226PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000227{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 if (tstate == NULL)
229 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
230 /* Check someone has called PyEval_InitThreads() to create the lock */
231 assert(gil_created());
232 take_gil(tstate);
233 if (PyThreadState_Swap(tstate) != NULL)
234 Py_FatalError(
235 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000236}
237
238void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000239PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000240{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 if (tstate == NULL)
242 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
243 if (PyThreadState_Swap(NULL) != tstate)
244 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
245 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000246}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000247
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200248/* This function is called from PyOS_AfterFork_Child to destroy all threads
249 * which are not running in the child process, and clear internal locks
250 * which might be held by those threads.
251 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000252
253void
254PyEval_ReInitThreads(void)
255{
Victor Stinner50b48572018-11-01 01:51:40 +0100256 PyThreadState *current_tstate = _PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 if (!gil_created())
259 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 recreate_gil();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200261 take_gil(current_tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700262
Eric Snowb75b1a352019-04-12 10:20:10 -0600263 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
264 if (_PyRuntime.ceval.pending.lock == NULL) {
Eric Snow8479a342019-03-08 23:44:33 -0700265 Py_FatalError("Can't initialize threads for pending calls");
266 }
Jesse Nollera8513972008-07-17 16:49:17 +0000267
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200268 /* Destroy all threads except the current one */
269 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000270}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000271
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000272/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600273 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000274
275void
Eric Snowb75b1a352019-04-12 10:20:10 -0600276_PyEval_SignalAsyncExc(void)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000277{
Eric Snowb75b1a352019-04-12 10:20:10 -0600278 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000279}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000280
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000281PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000282PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000283{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 PyThreadState *tstate = PyThreadState_Swap(NULL);
285 if (tstate == NULL)
286 Py_FatalError("PyEval_SaveThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100287 assert(gil_created());
288 drop_gil(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000290}
291
292void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000293PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000294{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 if (tstate == NULL)
296 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100297 assert(gil_created());
298
299 int err = errno;
300 take_gil(tstate);
301 /* _Py_Finalizing is protected by the GIL */
302 if (_Py_IsFinalizing() && !_Py_CURRENTLY_FINALIZING(tstate)) {
303 drop_gil(tstate);
304 PyThread_exit_thread();
305 Py_UNREACHABLE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 }
Victor Stinner2914bb32018-01-29 11:57:45 +0100307 errno = err;
308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000310}
311
312
Guido van Rossuma9672091994-09-14 13:31:22 +0000313/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
314 signal handlers or Mac I/O completion routines) can schedule calls
315 to a function to be called synchronously.
316 The synchronous function is called with one void* argument.
317 It should return 0 for success or -1 for failure -- failure should
318 be accompanied by an exception.
319
320 If registry succeeds, the registry function returns 0; if it fails
321 (e.g. due to too many pending calls) it returns -1 (without setting
322 an exception condition).
323
324 Note that because registry may occur from within signal handlers,
325 or other asynchronous events, calling malloc() is unsafe!
326
Guido van Rossuma9672091994-09-14 13:31:22 +0000327 Any thread can schedule pending calls, but only the main thread
328 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000329 There is no facility to schedule calls to a particular thread, but
330 that should be easy to change, should that ever be required. In
331 that case, the static variables here should go into the python
332 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000333*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000334
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200335void
336_PyEval_SignalReceived(void)
337{
338 /* bpo-30703: Function called when the C signal handler of Python gets a
339 signal. We cannot queue a callback using Py_AddPendingCall() since
340 that function is not async-signal-safe. */
Eric Snowfdf282d2019-01-11 14:26:55 -0700341 SIGNAL_PENDING_SIGNALS();
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200342}
343
Eric Snow5be45a62019-03-08 22:47:07 -0700344/* Push one item onto the queue while holding the lock. */
345static int
Eric Snowb75b1a352019-04-12 10:20:10 -0600346_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600347 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700348{
Eric Snow842a2f02019-03-15 15:47:51 -0600349 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700350 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600351 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700352 return -1; /* Queue full */
353 }
Eric Snow842a2f02019-03-15 15:47:51 -0600354 pending->calls[i].func = func;
355 pending->calls[i].arg = arg;
356 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700357 return 0;
358}
359
360/* Pop one item off the queue while holding the lock. */
361static void
Eric Snowb75b1a352019-04-12 10:20:10 -0600362_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600363 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700364{
Eric Snow842a2f02019-03-15 15:47:51 -0600365 int i = pending->first;
366 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700367 return; /* Queue empty */
368 }
369
Eric Snow842a2f02019-03-15 15:47:51 -0600370 *func = pending->calls[i].func;
371 *arg = pending->calls[i].arg;
372 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700373}
374
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200375/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000376 scheduling to be made from any thread, and even from an executing
377 callback.
378 */
379
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000380int
Eric Snowb75b1a352019-04-12 10:20:10 -0600381Py_AddPendingCall(int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000382{
Eric Snowb75b1a352019-04-12 10:20:10 -0600383 struct _pending_calls *pending = &_PyRuntime.ceval.pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600384
385 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
386 if (pending->finishing) {
387 PyThread_release_lock(pending->lock);
388
389 PyObject *exc, *val, *tb;
390 PyErr_Fetch(&exc, &val, &tb);
391 PyErr_SetString(PyExc_SystemError,
392 "Py_AddPendingCall: cannot add pending calls "
393 "(Python shutting down)");
394 PyErr_Print();
395 PyErr_Restore(exc, val, tb);
396 return -1;
397 }
Eric Snowb75b1a352019-04-12 10:20:10 -0600398 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600399 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700400
Eric Snowb75b1a352019-04-12 10:20:10 -0600401 /* signal main loop */
402 SIGNAL_PENDING_CALLS();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000404}
405
Eric Snowfdf282d2019-01-11 14:26:55 -0700406static int
407handle_signals(void)
408{
Eric Snow5be45a62019-03-08 22:47:07 -0700409 /* Only handle signals on main thread. PyEval_InitThreads must
410 * have been called already.
411 */
412 if (PyThread_get_thread_ident() != _PyRuntime.main_thread) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700413 return 0;
414 }
Eric Snow64d6cc82019-02-23 15:40:43 -0700415 /*
416 * Ensure that the thread isn't currently running some other
417 * interpreter.
418 */
419 if (_PyInterpreterState_GET_UNSAFE() != _PyRuntime.interpreters.main) {
420 return 0;
421 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700422
423 UNSIGNAL_PENDING_SIGNALS();
Eric Snow64d6cc82019-02-23 15:40:43 -0700424 if (_PyErr_CheckSignals() < 0) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700425 SIGNAL_PENDING_SIGNALS(); /* We're not done yet */
426 return -1;
427 }
428 return 0;
429}
430
431static int
Eric Snowb75b1a352019-04-12 10:20:10 -0600432make_pending_calls(struct _pending_calls* pending)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000433{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200434 static int busy = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000435
Eric Snowb75b1a352019-04-12 10:20:10 -0600436 /* only service pending calls on main thread */
437 if (PyThread_get_thread_ident() != _PyRuntime.main_thread) {
438 return 0;
439 }
440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 /* don't perform recursive pending calls */
Eric Snowfdf282d2019-01-11 14:26:55 -0700442 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700444 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200445 busy = 1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200446 /* unsignal before starting to call callbacks, so that any callback
447 added in-between re-signals */
Eric Snowb75b1a352019-04-12 10:20:10 -0600448 UNSIGNAL_PENDING_CALLS();
Eric Snowfdf282d2019-01-11 14:26:55 -0700449 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 /* perform a bounded number of calls, in case of recursion */
Eric Snowfdf282d2019-01-11 14:26:55 -0700452 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700453 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 void *arg = NULL;
455
456 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600457 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Eric Snowb75b1a352019-04-12 10:20:10 -0600458 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600459 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700460
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100461 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700462 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100463 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700464 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700465 res = func(arg);
466 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200467 goto error;
468 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200470
Charles-François Natalif23339a2011-07-23 18:15:43 +0200471 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700472 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200473
474error:
475 busy = 0;
Eric Snowb75b1a352019-04-12 10:20:10 -0600476 SIGNAL_PENDING_CALLS();
Eric Snowfdf282d2019-01-11 14:26:55 -0700477 return res;
478}
479
Eric Snow842a2f02019-03-15 15:47:51 -0600480void
Eric Snowb75b1a352019-04-12 10:20:10 -0600481_Py_FinishPendingCalls(void)
Eric Snow842a2f02019-03-15 15:47:51 -0600482{
Eric Snowb75b1a352019-04-12 10:20:10 -0600483 struct _pending_calls *pending = &_PyRuntime.ceval.pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600484
485 assert(PyGILState_Check());
486
487 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
488 pending->finishing = 1;
489 PyThread_release_lock(pending->lock);
490
491 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
492 return;
493 }
494
Eric Snowb75b1a352019-04-12 10:20:10 -0600495 if (make_pending_calls(pending) < 0) {
Eric Snow842a2f02019-03-15 15:47:51 -0600496 PyObject *exc, *val, *tb;
497 PyErr_Fetch(&exc, &val, &tb);
498 PyErr_BadInternalCall();
499 _PyErr_ChainExceptions(exc, val, tb);
500 PyErr_Print();
501 }
502}
503
Eric Snowfdf282d2019-01-11 14:26:55 -0700504/* Py_MakePendingCalls() is a simple wrapper for the sake
505 of backward-compatibility. */
506int
507Py_MakePendingCalls(void)
508{
509 assert(PyGILState_Check());
510
511 /* Python signal handler doesn't really queue a callback: it only signals
512 that a signal was received, see _PyEval_SignalReceived(). */
513 int res = handle_signals();
514 if (res != 0) {
515 return res;
516 }
517
Eric Snowb75b1a352019-04-12 10:20:10 -0600518 res = make_pending_calls(&_PyRuntime.ceval.pending);
519 if (res != 0) {
520 return res;
521 }
522
523 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000524}
525
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000526/* The interpreter's recursion limit */
527
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000528#ifndef Py_DEFAULT_RECURSION_LIMIT
529#define Py_DEFAULT_RECURSION_LIMIT 1000
530#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600531
Eric Snow05351c12017-09-05 21:43:08 -0700532int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000533
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600534void
535_PyEval_Initialize(struct _ceval_runtime_state *state)
536{
537 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
538 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
539 _gil_initialize(&state->gil);
540}
541
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000542int
543Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000544{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600545 return _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000546}
547
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000548void
549Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000550{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600551 _PyRuntime.ceval.recursion_limit = new_limit;
552 _Py_CheckRecursionLimit = _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000553}
554
Armin Rigo2b3eb402003-10-28 12:05:48 +0000555/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
556 if the recursion_depth reaches _Py_CheckRecursionLimit.
557 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
558 to guarantee that _Py_CheckRecursiveCall() is regularly called.
559 Without USE_STACKCHECK, there is no need for this. */
560int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300561_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000562{
Victor Stinner50b48572018-11-01 01:51:40 +0100563 PyThreadState *tstate = _PyThreadState_GET();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600564 int recursion_limit = _PyRuntime.ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000565
566#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700567 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 if (PyOS_CheckStack()) {
569 --tstate->recursion_depth;
570 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
571 return -1;
572 }
pdox18967932017-10-25 23:03:01 -0700573 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700574 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700575#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 if (tstate->recursion_critical)
577 /* Somebody asked that we don't check for recursion. */
578 return 0;
579 if (tstate->overflowed) {
580 if (tstate->recursion_depth > recursion_limit + 50) {
581 /* Overflowing while handling an overflow. Give up. */
582 Py_FatalError("Cannot recover from stack overflow.");
583 }
584 return 0;
585 }
586 if (tstate->recursion_depth > recursion_limit) {
587 --tstate->recursion_depth;
588 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400589 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 "maximum recursion depth exceeded%s",
591 where);
592 return -1;
593 }
594 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000595}
596
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400597static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000598static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000599
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600600#define _Py_TracingPossible _PyRuntime.ceval.tracing_possible
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000601
Guido van Rossum374a9221991-04-04 10:40:29 +0000602
Guido van Rossumb209a111997-04-29 18:18:01 +0000603PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000604PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000605{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 return PyEval_EvalCodeEx(co,
607 globals, locals,
608 (PyObject **)NULL, 0,
609 (PyObject **)NULL, 0,
610 (PyObject **)NULL, 0,
611 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000612}
613
614
615/* Interpreter main loop */
616
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000617PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000618PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 /* This is for backward compatibility with extension modules that
620 used this API; core interpreter code should call
621 PyEval_EvalFrameEx() */
622 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000623}
624
625PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000626PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000627{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200628 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
629 return interp->eval_frame(f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700630}
631
Victor Stinnerc6944e72016-11-11 02:13:35 +0100632PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700633_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
634{
Guido van Rossum950361c1997-01-24 13:49:28 +0000635#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000637#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200638 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300639 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200640 int opcode; /* Current opcode */
641 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200642 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 PyObject *retval = NULL; /* Return value */
Victor Stinner50b48572018-11-01 01:51:40 +0100644 PyThreadState *tstate = _PyThreadState_GET();
Eric Snowb75b1a352019-04-12 10:20:10 -0600645 _Py_atomic_int *eval_breaker = &_PyRuntime.ceval.eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 is true when the line being executed has changed. The
653 initial values are such as to make this false the first
654 time it is tested. */
655 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000656
Serhiy Storchakaab874002016-09-11 13:48:15 +0300657 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 PyObject *names;
659 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000660
Brett Cannon368b4b72012-04-02 12:17:59 -0400661#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200662 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400663#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200664
Antoine Pitroub52ec782009-01-25 16:34:23 +0000665/* Computed GOTOs, or
666 the-optimization-commonly-but-improperly-known-as-"threaded code"
667 using gcc's labels-as-values extension
668 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
669
670 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000672 combined with a lookup table of jump addresses. However, since the
673 indirect jump instruction is shared by all opcodes, the CPU will have a
674 hard time making the right prediction for where to jump next (actually,
675 it will be always wrong except in the uncommon case of a sequence of
676 several identical opcodes).
677
678 "Threaded code" in contrast, uses an explicit jump table and an explicit
679 indirect jump instruction at the end of each opcode. Since the jump
680 instruction is at a different address for each opcode, the CPU will make a
681 separate prediction for each of these instructions, which is equivalent to
682 predicting the second opcode of each opcode pair. These predictions have
683 a much better chance to turn out valid, especially in small bytecode loops.
684
685 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000687 and potentially many more instructions (depending on the pipeline width).
688 A correctly predicted branch, however, is nearly free.
689
690 At the time of this writing, the "threaded code" version is up to 15-20%
691 faster than the normal "switch" version, depending on the compiler and the
692 CPU architecture.
693
694 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
695 because it would render the measurements invalid.
696
697
698 NOTE: care must be taken that the compiler doesn't try to "optimize" the
699 indirect jumps by sharing them between all opcodes. Such optimizations
700 can be disabled on gcc by using the -fno-gcse flag (or possibly
701 -fno-crossjumping).
702*/
703
Antoine Pitrou042b1282010-08-13 21:15:58 +0000704#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000705#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000706#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000707#endif
708
Antoine Pitrou042b1282010-08-13 21:15:58 +0000709#ifdef HAVE_COMPUTED_GOTOS
710 #ifndef USE_COMPUTED_GOTOS
711 #define USE_COMPUTED_GOTOS 1
712 #endif
713#else
714 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
715 #error "Computed gotos are not supported on this compiler."
716 #endif
717 #undef USE_COMPUTED_GOTOS
718 #define USE_COMPUTED_GOTOS 0
719#endif
720
721#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000722/* Import the static jump table */
723#include "opcode_targets.h"
724
Antoine Pitroub52ec782009-01-25 16:34:23 +0000725#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700726 op: \
727 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000728
Antoine Pitroub52ec782009-01-25 16:34:23 +0000729#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 { \
Eric Snow7bda9de2019-03-08 17:25:54 -0700731 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 FAST_DISPATCH(); \
733 } \
734 continue; \
735 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000736
737#ifdef LLTRACE
738#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700740 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300742 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300743 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 } \
745 goto fast_next_opcode; \
746 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000747#else
748#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700750 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300752 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300753 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 } \
755 goto fast_next_opcode; \
756 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000757#endif
758
759#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700760#define TARGET(op) op
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300761
Antoine Pitroub52ec782009-01-25 16:34:23 +0000762#define DISPATCH() continue
763#define FAST_DISPATCH() goto fast_next_opcode
764#endif
765
766
Neal Norwitza81d2202002-07-14 00:27:26 +0000767/* Tuple access macros */
768
769#ifndef Py_DEBUG
770#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
771#else
772#define GETITEM(v, i) PyTuple_GetItem((v), (i))
773#endif
774
Guido van Rossum374a9221991-04-04 10:40:29 +0000775/* Code access macros */
776
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300777/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600778#define INSTR_OFFSET() \
779 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300780#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300781 _Py_CODEUNIT word = *next_instr; \
782 opcode = _Py_OPCODE(word); \
783 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300784 next_instr++; \
785 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300786#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
787#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000788
Raymond Hettingerf606f872003-03-16 03:11:04 +0000789/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 Some opcodes tend to come in pairs thus making it possible to
791 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300792 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 Verifying the prediction costs a single high-speed test of a register
795 variable against a constant. If the pairing was good, then the
796 processor's own internal branch predication has a high likelihood of
797 success, resulting in a nearly zero-overhead transition to the
798 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300799 including its unpredictable switch-case branch. Combined with the
800 processor's internal branch prediction, a successful PREDICT has the
801 effect of making the two opcodes run as if they were a single new opcode
802 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000803
Georg Brandl86b2fb92008-07-16 03:43:04 +0000804 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 predictions turned-on and interpret the results as if some opcodes
806 had been combined or turn-off predictions so that the opcode frequency
807 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000808
809 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 the CPU to record separate branch prediction information for each
811 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000812
Raymond Hettingerf606f872003-03-16 03:11:04 +0000813*/
814
Antoine Pitrou042b1282010-08-13 21:15:58 +0000815#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000817#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300818#define PREDICT(op) \
819 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300820 _Py_CODEUNIT word = *next_instr; \
821 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300822 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300823 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300824 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300825 goto PRED_##op; \
826 } \
827 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000828#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300829#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000830
Raymond Hettingerf606f872003-03-16 03:11:04 +0000831
Guido van Rossum374a9221991-04-04 10:40:29 +0000832/* Stack manipulation macros */
833
Martin v. Löwis18e16552006-02-15 17:27:45 +0000834/* The stack can grow at most MAXINT deep, as co_nlocals and
835 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000836#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
837#define EMPTY() (STACK_LEVEL() == 0)
838#define TOP() (stack_pointer[-1])
839#define SECOND() (stack_pointer[-2])
840#define THIRD() (stack_pointer[-3])
841#define FOURTH() (stack_pointer[-4])
842#define PEEK(n) (stack_pointer[-(n)])
843#define SET_TOP(v) (stack_pointer[-1] = (v))
844#define SET_SECOND(v) (stack_pointer[-2] = (v))
845#define SET_THIRD(v) (stack_pointer[-3] = (v))
846#define SET_FOURTH(v) (stack_pointer[-4] = (v))
847#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
848#define BASIC_STACKADJ(n) (stack_pointer += n)
849#define BASIC_PUSH(v) (*stack_pointer++ = (v))
850#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000851
Guido van Rossum96a42c81992-01-12 02:29:51 +0000852#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000854 lltrace && prtrace(TOP(), "push")); \
855 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000857 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +0000858#define STACK_GROW(n) do { \
859 assert(n >= 0); \
860 (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000861 lltrace && prtrace(TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000862 assert(STACK_LEVEL() <= co->co_stacksize); \
863 } while (0)
864#define STACK_SHRINK(n) do { \
865 assert(n >= 0); \
866 (void)(lltrace && prtrace(TOP(), "stackadj")); \
867 (void)(BASIC_STACKADJ(-n)); \
868 assert(STACK_LEVEL() <= co->co_stacksize); \
869 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +0000870#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000871 prtrace((STACK_POINTER)[-1], "ext_pop")), \
872 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000873#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000874#define PUSH(v) BASIC_PUSH(v)
875#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +0000876#define STACK_GROW(n) BASIC_STACKADJ(n)
877#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000878#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000879#endif
880
Guido van Rossum681d79a1995-07-18 14:51:37 +0000881/* Local variable macros */
882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000884
885/* The SETLOCAL() macro must not DECREF the local variable in-place and
886 then store the new value; it must copy the old value to a temporary
887 value, then store the new value, and then DECREF the temporary value.
888 This is because it is possible that during the DECREF the frame is
889 accessed by other code (e.g. a __del__ method or gc.collect()) and the
890 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000892 GETLOCAL(i) = value; \
893 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000894
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000895
896#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 while (STACK_LEVEL() > (b)->b_level) { \
898 PyObject *v = POP(); \
899 Py_XDECREF(v); \
900 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000901
902#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300903 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +0100905 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 assert(STACK_LEVEL() >= (b)->b_level + 3); \
907 while (STACK_LEVEL() > (b)->b_level + 3) { \
908 value = POP(); \
909 Py_XDECREF(value); \
910 } \
Mark Shannonae3087c2017-10-22 22:41:51 +0100911 exc_info = tstate->exc_info; \
912 type = exc_info->exc_type; \
913 value = exc_info->exc_value; \
914 traceback = exc_info->exc_traceback; \
915 exc_info->exc_type = POP(); \
916 exc_info->exc_value = POP(); \
917 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 Py_XDECREF(type); \
919 Py_XDECREF(value); \
920 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300921 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000922
Guido van Rossuma027efa1997-05-05 20:56:21 +0000923/* Start of code */
924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 /* push frame */
926 if (Py_EnterRecursiveCall(""))
927 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 if (tstate->use_tracing) {
932 if (tstate->c_tracefunc != NULL) {
933 /* tstate->c_tracefunc, if defined, is a
934 function that will be called on *every* entry
935 to a code block. Its return value, if not
936 None, is a function that will be called at
937 the start of each executed line of code.
938 (Actually, the function must return itself
939 in order to continue tracing.) The trace
940 functions are called with three arguments:
941 a pointer to the current frame, a string
942 indicating why the function is called, and
943 an argument which depends on the situation.
944 The global trace function is also called
945 whenever an exception is detected. */
946 if (call_trace_protected(tstate->c_tracefunc,
947 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100948 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 /* Trace function raised an error */
950 goto exit_eval_frame;
951 }
952 }
953 if (tstate->c_profilefunc != NULL) {
954 /* Similar for c_profilefunc, except it needn't
955 return itself and isn't called for "line" events */
956 if (call_trace_protected(tstate->c_profilefunc,
957 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100958 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 /* Profile function raised an error */
960 goto exit_eval_frame;
961 }
962 }
963 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000964
Łukasz Langaa785c872016-09-09 17:37:37 -0700965 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
966 dtrace_function_entry(f);
967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 co = f->f_code;
969 names = co->co_names;
970 consts = co->co_consts;
971 fastlocals = f->f_localsplus;
972 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300973 assert(PyBytes_Check(co->co_code));
974 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +0300975 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
976 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
977 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300978 /*
979 f->f_lasti refers to the index of the last instruction,
980 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000981
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300982 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500983 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 When the PREDICT() macros are enabled, some opcode pairs follow in
986 direct succession without updating f->f_lasti. A successful
987 prediction effectively links the two codes together as if they
988 were a single new opcode; accordingly,f->f_lasti will point to
989 the first code in the pair (for instance, GET_ITER followed by
990 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300991 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300993 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300994 next_instr = first_instr;
995 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +0300996 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
997 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300998 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 stack_pointer = f->f_stacktop;
1000 assert(stack_pointer != NULL);
1001 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001002 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001003
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001004
Tim Peters5ca576e2001-06-18 22:08:13 +00001005#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001006 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001007#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001008
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001009 if (throwflag) /* support for generator.throw() */
1010 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001011
Victor Stinnerace47d72013-07-18 01:41:08 +02001012#ifdef Py_DEBUG
1013 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001014 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001015 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +02001016 assert(!PyErr_Occurred());
1017#endif
1018
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001019main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1022 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +02001023 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 /* Do periodic things. Doing this every time through
1026 the loop would add too much overhead, so we do it
1027 only every Nth instruction. We also do it if
1028 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1029 event needs attention (e.g. a signal handler or
1030 async I/O handler); see Py_AddPendingCall() and
1031 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001032
Eric Snow7bda9de2019-03-08 17:25:54 -07001033 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001034 opcode = _Py_OPCODE(*next_instr);
1035 if (opcode == SETUP_FINALLY ||
1036 opcode == SETUP_WITH ||
1037 opcode == BEFORE_ASYNC_WITH ||
1038 opcode == YIELD_FROM) {
1039 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001040 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001041 - If we're about to enter the 'with:'. It will prevent
1042 emitting a resource warning in the common idiom
1043 'with open(path) as file:'.
1044 - If we're about to enter the 'async with:'.
1045 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001046 *very* useful, but might help in some cases and it's
1047 traditional)
1048 - If we're resuming a chain of nested 'yield from' or
1049 'await' calls, then each frame is parked with YIELD_FROM
1050 as its next opcode. If the user hit control-C we want to
1051 wait until we've reached the innermost frame before
1052 running the signal handler and raising KeyboardInterrupt
1053 (see bpo-30039).
1054 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 goto fast_next_opcode;
1056 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001057
1058 if (_Py_atomic_load_relaxed(
1059 &_PyRuntime.ceval.signals_pending))
1060 {
1061 if (handle_signals() != 0) {
1062 goto error;
1063 }
1064 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001065 if (_Py_atomic_load_relaxed(
Eric Snowb75b1a352019-04-12 10:20:10 -06001066 &_PyRuntime.ceval.pending.calls_to_do))
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001067 {
Eric Snowb75b1a352019-04-12 10:20:10 -06001068 if (make_pending_calls(&_PyRuntime.ceval.pending) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001069 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001070 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001072
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001073 if (_Py_atomic_load_relaxed(
1074 &_PyRuntime.ceval.gil_drop_request))
1075 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 /* Give another thread a chance */
1077 if (PyThreadState_Swap(NULL) != tstate)
1078 Py_FatalError("ceval: tstate mix-up");
1079 drop_gil(tstate);
1080
1081 /* Other threads may run now */
1082
1083 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001084
1085 /* Check if we should make a quick exit. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001086 if (_Py_IsFinalizing() &&
1087 !_Py_CURRENTLY_FINALIZING(tstate))
1088 {
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001089 drop_gil(tstate);
1090 PyThread_exit_thread();
1091 }
1092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 if (PyThreadState_Swap(tstate) != NULL)
1094 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 }
1096 /* Check for asynchronous exceptions. */
1097 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001098 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 tstate->async_exc = NULL;
Eric Snowb75b1a352019-04-12 10:20:10 -06001100 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001101 PyErr_SetNone(exc);
1102 Py_DECREF(exc);
1103 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 }
1105 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 fast_next_opcode:
1108 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001109
Łukasz Langaa785c872016-09-09 17:37:37 -07001110 if (PyDTrace_LINE_ENABLED())
1111 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001116 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001117 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 /* see maybe_call_line_trace
1119 for expository comments */
1120 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 err = maybe_call_line_trace(tstate->c_tracefunc,
1123 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001124 tstate, f,
1125 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 /* Reload possibly changed frame fields */
1127 JUMPTO(f->f_lasti);
1128 if (f->f_stacktop != NULL) {
1129 stack_pointer = f->f_stacktop;
1130 f->f_stacktop = NULL;
1131 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001132 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001134 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001138
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001139 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001140 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001141#ifdef DYNAMIC_EXECUTION_PROFILE
1142#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 dxpairs[lastopcode][opcode]++;
1144 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001145#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001147#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001148
Guido van Rossum96a42c81992-01-12 02:29:51 +00001149#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 if (lltrace) {
1153 if (HAS_ARG(opcode)) {
1154 printf("%d: %d, %d\n",
1155 f->f_lasti, opcode, oparg);
1156 }
1157 else {
1158 printf("%d: %d\n",
1159 f->f_lasti, opcode);
1160 }
1161 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001162#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001167 It is essential that any operation that fails must goto error
1168 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001169
Benjamin Petersonddd19492018-09-16 22:38:02 -07001170 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001172 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001173
Benjamin Petersonddd19492018-09-16 22:38:02 -07001174 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001175 PyObject *value = GETLOCAL(oparg);
1176 if (value == NULL) {
1177 format_exc_check_arg(PyExc_UnboundLocalError,
1178 UNBOUNDLOCAL_ERROR_MSG,
1179 PyTuple_GetItem(co->co_varnames, oparg));
1180 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001182 Py_INCREF(value);
1183 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001185 }
1186
Benjamin Petersonddd19492018-09-16 22:38:02 -07001187 case TARGET(LOAD_CONST): {
1188 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001189 PyObject *value = GETITEM(consts, oparg);
1190 Py_INCREF(value);
1191 PUSH(value);
1192 FAST_DISPATCH();
1193 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001194
Benjamin Petersonddd19492018-09-16 22:38:02 -07001195 case TARGET(STORE_FAST): {
1196 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001197 PyObject *value = POP();
1198 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001200 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001201
Benjamin Petersonddd19492018-09-16 22:38:02 -07001202 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001203 PyObject *value = POP();
1204 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001206 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001207
Benjamin Petersonddd19492018-09-16 22:38:02 -07001208 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001209 PyObject *top = TOP();
1210 PyObject *second = SECOND();
1211 SET_TOP(second);
1212 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001214 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001215
Benjamin Petersonddd19492018-09-16 22:38:02 -07001216 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001217 PyObject *top = TOP();
1218 PyObject *second = SECOND();
1219 PyObject *third = THIRD();
1220 SET_TOP(second);
1221 SET_SECOND(third);
1222 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001224 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001225
Benjamin Petersonddd19492018-09-16 22:38:02 -07001226 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001227 PyObject *top = TOP();
1228 PyObject *second = SECOND();
1229 PyObject *third = THIRD();
1230 PyObject *fourth = FOURTH();
1231 SET_TOP(second);
1232 SET_SECOND(third);
1233 SET_THIRD(fourth);
1234 SET_FOURTH(top);
1235 FAST_DISPATCH();
1236 }
1237
Benjamin Petersonddd19492018-09-16 22:38:02 -07001238 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001239 PyObject *top = TOP();
1240 Py_INCREF(top);
1241 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001243 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001244
Benjamin Petersonddd19492018-09-16 22:38:02 -07001245 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001246 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001247 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001248 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001249 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001250 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001251 SET_TOP(top);
1252 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001253 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001254 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001255
Benjamin Petersonddd19492018-09-16 22:38:02 -07001256 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001257 PyObject *value = TOP();
1258 PyObject *res = PyNumber_Positive(value);
1259 Py_DECREF(value);
1260 SET_TOP(res);
1261 if (res == NULL)
1262 goto error;
1263 DISPATCH();
1264 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001265
Benjamin Petersonddd19492018-09-16 22:38:02 -07001266 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001267 PyObject *value = TOP();
1268 PyObject *res = PyNumber_Negative(value);
1269 Py_DECREF(value);
1270 SET_TOP(res);
1271 if (res == NULL)
1272 goto error;
1273 DISPATCH();
1274 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001275
Benjamin Petersonddd19492018-09-16 22:38:02 -07001276 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001277 PyObject *value = TOP();
1278 int err = PyObject_IsTrue(value);
1279 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 if (err == 0) {
1281 Py_INCREF(Py_True);
1282 SET_TOP(Py_True);
1283 DISPATCH();
1284 }
1285 else if (err > 0) {
1286 Py_INCREF(Py_False);
1287 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 DISPATCH();
1289 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001290 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001291 goto error;
1292 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001293
Benjamin Petersonddd19492018-09-16 22:38:02 -07001294 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001295 PyObject *value = TOP();
1296 PyObject *res = PyNumber_Invert(value);
1297 Py_DECREF(value);
1298 SET_TOP(res);
1299 if (res == NULL)
1300 goto error;
1301 DISPATCH();
1302 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001303
Benjamin Petersonddd19492018-09-16 22:38:02 -07001304 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001305 PyObject *exp = POP();
1306 PyObject *base = TOP();
1307 PyObject *res = PyNumber_Power(base, exp, Py_None);
1308 Py_DECREF(base);
1309 Py_DECREF(exp);
1310 SET_TOP(res);
1311 if (res == NULL)
1312 goto error;
1313 DISPATCH();
1314 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001315
Benjamin Petersonddd19492018-09-16 22:38:02 -07001316 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001317 PyObject *right = POP();
1318 PyObject *left = TOP();
1319 PyObject *res = PyNumber_Multiply(left, right);
1320 Py_DECREF(left);
1321 Py_DECREF(right);
1322 SET_TOP(res);
1323 if (res == NULL)
1324 goto error;
1325 DISPATCH();
1326 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001327
Benjamin Petersonddd19492018-09-16 22:38:02 -07001328 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001329 PyObject *right = POP();
1330 PyObject *left = TOP();
1331 PyObject *res = PyNumber_MatrixMultiply(left, right);
1332 Py_DECREF(left);
1333 Py_DECREF(right);
1334 SET_TOP(res);
1335 if (res == NULL)
1336 goto error;
1337 DISPATCH();
1338 }
1339
Benjamin Petersonddd19492018-09-16 22:38:02 -07001340 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001341 PyObject *divisor = POP();
1342 PyObject *dividend = TOP();
1343 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1344 Py_DECREF(dividend);
1345 Py_DECREF(divisor);
1346 SET_TOP(quotient);
1347 if (quotient == NULL)
1348 goto error;
1349 DISPATCH();
1350 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001351
Benjamin Petersonddd19492018-09-16 22:38:02 -07001352 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001353 PyObject *divisor = POP();
1354 PyObject *dividend = TOP();
1355 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1356 Py_DECREF(dividend);
1357 Py_DECREF(divisor);
1358 SET_TOP(quotient);
1359 if (quotient == NULL)
1360 goto error;
1361 DISPATCH();
1362 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001363
Benjamin Petersonddd19492018-09-16 22:38:02 -07001364 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001365 PyObject *divisor = POP();
1366 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001367 PyObject *res;
1368 if (PyUnicode_CheckExact(dividend) && (
1369 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1370 // fast path; string formatting, but not if the RHS is a str subclass
1371 // (see issue28598)
1372 res = PyUnicode_Format(dividend, divisor);
1373 } else {
1374 res = PyNumber_Remainder(dividend, divisor);
1375 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001376 Py_DECREF(divisor);
1377 Py_DECREF(dividend);
1378 SET_TOP(res);
1379 if (res == NULL)
1380 goto error;
1381 DISPATCH();
1382 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001383
Benjamin Petersonddd19492018-09-16 22:38:02 -07001384 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001385 PyObject *right = POP();
1386 PyObject *left = TOP();
1387 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001388 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1389 CPython using bytecode, it is simply worthless.
1390 See http://bugs.python.org/issue21955 and
1391 http://bugs.python.org/issue10044 for the discussion. In short,
1392 no patch shown any impact on a realistic benchmark, only a minor
1393 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001394 if (PyUnicode_CheckExact(left) &&
1395 PyUnicode_CheckExact(right)) {
1396 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001397 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001398 }
1399 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001400 sum = PyNumber_Add(left, right);
1401 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001402 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001403 Py_DECREF(right);
1404 SET_TOP(sum);
1405 if (sum == NULL)
1406 goto error;
1407 DISPATCH();
1408 }
1409
Benjamin Petersonddd19492018-09-16 22:38:02 -07001410 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001411 PyObject *right = POP();
1412 PyObject *left = TOP();
1413 PyObject *diff = PyNumber_Subtract(left, right);
1414 Py_DECREF(right);
1415 Py_DECREF(left);
1416 SET_TOP(diff);
1417 if (diff == NULL)
1418 goto error;
1419 DISPATCH();
1420 }
1421
Benjamin Petersonddd19492018-09-16 22:38:02 -07001422 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001423 PyObject *sub = POP();
1424 PyObject *container = TOP();
1425 PyObject *res = PyObject_GetItem(container, sub);
1426 Py_DECREF(container);
1427 Py_DECREF(sub);
1428 SET_TOP(res);
1429 if (res == NULL)
1430 goto error;
1431 DISPATCH();
1432 }
1433
Benjamin Petersonddd19492018-09-16 22:38:02 -07001434 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001435 PyObject *right = POP();
1436 PyObject *left = TOP();
1437 PyObject *res = PyNumber_Lshift(left, right);
1438 Py_DECREF(left);
1439 Py_DECREF(right);
1440 SET_TOP(res);
1441 if (res == NULL)
1442 goto error;
1443 DISPATCH();
1444 }
1445
Benjamin Petersonddd19492018-09-16 22:38:02 -07001446 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001447 PyObject *right = POP();
1448 PyObject *left = TOP();
1449 PyObject *res = PyNumber_Rshift(left, right);
1450 Py_DECREF(left);
1451 Py_DECREF(right);
1452 SET_TOP(res);
1453 if (res == NULL)
1454 goto error;
1455 DISPATCH();
1456 }
1457
Benjamin Petersonddd19492018-09-16 22:38:02 -07001458 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001459 PyObject *right = POP();
1460 PyObject *left = TOP();
1461 PyObject *res = PyNumber_And(left, right);
1462 Py_DECREF(left);
1463 Py_DECREF(right);
1464 SET_TOP(res);
1465 if (res == NULL)
1466 goto error;
1467 DISPATCH();
1468 }
1469
Benjamin Petersonddd19492018-09-16 22:38:02 -07001470 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001471 PyObject *right = POP();
1472 PyObject *left = TOP();
1473 PyObject *res = PyNumber_Xor(left, right);
1474 Py_DECREF(left);
1475 Py_DECREF(right);
1476 SET_TOP(res);
1477 if (res == NULL)
1478 goto error;
1479 DISPATCH();
1480 }
1481
Benjamin Petersonddd19492018-09-16 22:38:02 -07001482 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001483 PyObject *right = POP();
1484 PyObject *left = TOP();
1485 PyObject *res = PyNumber_Or(left, right);
1486 Py_DECREF(left);
1487 Py_DECREF(right);
1488 SET_TOP(res);
1489 if (res == NULL)
1490 goto error;
1491 DISPATCH();
1492 }
1493
Benjamin Petersonddd19492018-09-16 22:38:02 -07001494 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001495 PyObject *v = POP();
1496 PyObject *list = PEEK(oparg);
1497 int err;
1498 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001500 if (err != 0)
1501 goto error;
1502 PREDICT(JUMP_ABSOLUTE);
1503 DISPATCH();
1504 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001505
Benjamin Petersonddd19492018-09-16 22:38:02 -07001506 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001507 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001508 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001509 int err;
1510 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001512 if (err != 0)
1513 goto error;
1514 PREDICT(JUMP_ABSOLUTE);
1515 DISPATCH();
1516 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001517
Benjamin Petersonddd19492018-09-16 22:38:02 -07001518 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001519 PyObject *exp = POP();
1520 PyObject *base = TOP();
1521 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1522 Py_DECREF(base);
1523 Py_DECREF(exp);
1524 SET_TOP(res);
1525 if (res == NULL)
1526 goto error;
1527 DISPATCH();
1528 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001529
Benjamin Petersonddd19492018-09-16 22:38:02 -07001530 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001531 PyObject *right = POP();
1532 PyObject *left = TOP();
1533 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1534 Py_DECREF(left);
1535 Py_DECREF(right);
1536 SET_TOP(res);
1537 if (res == NULL)
1538 goto error;
1539 DISPATCH();
1540 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001541
Benjamin Petersonddd19492018-09-16 22:38:02 -07001542 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001543 PyObject *right = POP();
1544 PyObject *left = TOP();
1545 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1546 Py_DECREF(left);
1547 Py_DECREF(right);
1548 SET_TOP(res);
1549 if (res == NULL)
1550 goto error;
1551 DISPATCH();
1552 }
1553
Benjamin Petersonddd19492018-09-16 22:38:02 -07001554 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001555 PyObject *divisor = POP();
1556 PyObject *dividend = TOP();
1557 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1558 Py_DECREF(dividend);
1559 Py_DECREF(divisor);
1560 SET_TOP(quotient);
1561 if (quotient == NULL)
1562 goto error;
1563 DISPATCH();
1564 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001565
Benjamin Petersonddd19492018-09-16 22:38:02 -07001566 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001567 PyObject *divisor = POP();
1568 PyObject *dividend = TOP();
1569 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1570 Py_DECREF(dividend);
1571 Py_DECREF(divisor);
1572 SET_TOP(quotient);
1573 if (quotient == NULL)
1574 goto error;
1575 DISPATCH();
1576 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001577
Benjamin Petersonddd19492018-09-16 22:38:02 -07001578 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001579 PyObject *right = POP();
1580 PyObject *left = TOP();
1581 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1582 Py_DECREF(left);
1583 Py_DECREF(right);
1584 SET_TOP(mod);
1585 if (mod == NULL)
1586 goto error;
1587 DISPATCH();
1588 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001589
Benjamin Petersonddd19492018-09-16 22:38:02 -07001590 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001591 PyObject *right = POP();
1592 PyObject *left = TOP();
1593 PyObject *sum;
1594 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1595 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001596 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001597 }
1598 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001599 sum = PyNumber_InPlaceAdd(left, right);
1600 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001601 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001602 Py_DECREF(right);
1603 SET_TOP(sum);
1604 if (sum == NULL)
1605 goto error;
1606 DISPATCH();
1607 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001608
Benjamin Petersonddd19492018-09-16 22:38:02 -07001609 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001610 PyObject *right = POP();
1611 PyObject *left = TOP();
1612 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1613 Py_DECREF(left);
1614 Py_DECREF(right);
1615 SET_TOP(diff);
1616 if (diff == NULL)
1617 goto error;
1618 DISPATCH();
1619 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001620
Benjamin Petersonddd19492018-09-16 22:38:02 -07001621 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001622 PyObject *right = POP();
1623 PyObject *left = TOP();
1624 PyObject *res = PyNumber_InPlaceLshift(left, right);
1625 Py_DECREF(left);
1626 Py_DECREF(right);
1627 SET_TOP(res);
1628 if (res == NULL)
1629 goto error;
1630 DISPATCH();
1631 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001632
Benjamin Petersonddd19492018-09-16 22:38:02 -07001633 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001634 PyObject *right = POP();
1635 PyObject *left = TOP();
1636 PyObject *res = PyNumber_InPlaceRshift(left, right);
1637 Py_DECREF(left);
1638 Py_DECREF(right);
1639 SET_TOP(res);
1640 if (res == NULL)
1641 goto error;
1642 DISPATCH();
1643 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001644
Benjamin Petersonddd19492018-09-16 22:38:02 -07001645 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001646 PyObject *right = POP();
1647 PyObject *left = TOP();
1648 PyObject *res = PyNumber_InPlaceAnd(left, right);
1649 Py_DECREF(left);
1650 Py_DECREF(right);
1651 SET_TOP(res);
1652 if (res == NULL)
1653 goto error;
1654 DISPATCH();
1655 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001656
Benjamin Petersonddd19492018-09-16 22:38:02 -07001657 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001658 PyObject *right = POP();
1659 PyObject *left = TOP();
1660 PyObject *res = PyNumber_InPlaceXor(left, right);
1661 Py_DECREF(left);
1662 Py_DECREF(right);
1663 SET_TOP(res);
1664 if (res == NULL)
1665 goto error;
1666 DISPATCH();
1667 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001668
Benjamin Petersonddd19492018-09-16 22:38:02 -07001669 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001670 PyObject *right = POP();
1671 PyObject *left = TOP();
1672 PyObject *res = PyNumber_InPlaceOr(left, right);
1673 Py_DECREF(left);
1674 Py_DECREF(right);
1675 SET_TOP(res);
1676 if (res == NULL)
1677 goto error;
1678 DISPATCH();
1679 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001680
Benjamin Petersonddd19492018-09-16 22:38:02 -07001681 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001682 PyObject *sub = TOP();
1683 PyObject *container = SECOND();
1684 PyObject *v = THIRD();
1685 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001686 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001687 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001688 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001689 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001690 Py_DECREF(container);
1691 Py_DECREF(sub);
1692 if (err != 0)
1693 goto error;
1694 DISPATCH();
1695 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001696
Benjamin Petersonddd19492018-09-16 22:38:02 -07001697 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001698 PyObject *sub = TOP();
1699 PyObject *container = SECOND();
1700 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001701 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001702 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001703 err = PyObject_DelItem(container, sub);
1704 Py_DECREF(container);
1705 Py_DECREF(sub);
1706 if (err != 0)
1707 goto error;
1708 DISPATCH();
1709 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001710
Benjamin Petersonddd19492018-09-16 22:38:02 -07001711 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001712 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001713 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001714 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001715 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001716 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 PyErr_SetString(PyExc_RuntimeError,
1718 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001719 Py_DECREF(value);
1720 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001722 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001723 Py_DECREF(value);
1724 if (res == NULL)
1725 goto error;
1726 Py_DECREF(res);
1727 DISPATCH();
1728 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001729
Benjamin Petersonddd19492018-09-16 22:38:02 -07001730 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001731 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 switch (oparg) {
1733 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001734 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001735 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001736 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001737 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001738 /* fall through */
1739 case 0:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001740 if (do_raise(exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001741 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001742 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 break;
1744 default:
1745 PyErr_SetString(PyExc_SystemError,
1746 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001747 break;
1748 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001749 goto error;
1750 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001751
Benjamin Petersonddd19492018-09-16 22:38:02 -07001752 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001754 assert(f->f_iblock == 0);
1755 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001756 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001757
Benjamin Petersonddd19492018-09-16 22:38:02 -07001758 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001759 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001760 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001761 PyObject *obj = TOP();
1762 PyTypeObject *type = Py_TYPE(obj);
1763
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001764 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001765 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001766 }
Yury Selivanov75445082015-05-11 22:57:16 -04001767
1768 if (getter != NULL) {
1769 iter = (*getter)(obj);
1770 Py_DECREF(obj);
1771 if (iter == NULL) {
1772 SET_TOP(NULL);
1773 goto error;
1774 }
1775 }
1776 else {
1777 SET_TOP(NULL);
1778 PyErr_Format(
1779 PyExc_TypeError,
1780 "'async for' requires an object with "
1781 "__aiter__ method, got %.100s",
1782 type->tp_name);
1783 Py_DECREF(obj);
1784 goto error;
1785 }
1786
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001787 if (Py_TYPE(iter)->tp_as_async == NULL ||
1788 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001789
Yury Selivanov398ff912017-03-02 22:20:00 -05001790 SET_TOP(NULL);
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001791 PyErr_Format(
1792 PyExc_TypeError,
1793 "'async for' received an object from __aiter__ "
1794 "that does not implement __anext__: %.100s",
1795 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001796 Py_DECREF(iter);
1797 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001798 }
1799
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001800 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001801 DISPATCH();
1802 }
1803
Benjamin Petersonddd19492018-09-16 22:38:02 -07001804 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001805 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001806 PyObject *next_iter = NULL;
1807 PyObject *awaitable = NULL;
1808 PyObject *aiter = TOP();
1809 PyTypeObject *type = Py_TYPE(aiter);
1810
Yury Selivanoveb636452016-09-08 22:01:51 -07001811 if (PyAsyncGen_CheckExact(aiter)) {
1812 awaitable = type->tp_as_async->am_anext(aiter);
1813 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001814 goto error;
1815 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001816 } else {
1817 if (type->tp_as_async != NULL){
1818 getter = type->tp_as_async->am_anext;
1819 }
Yury Selivanov75445082015-05-11 22:57:16 -04001820
Yury Selivanoveb636452016-09-08 22:01:51 -07001821 if (getter != NULL) {
1822 next_iter = (*getter)(aiter);
1823 if (next_iter == NULL) {
1824 goto error;
1825 }
1826 }
1827 else {
1828 PyErr_Format(
1829 PyExc_TypeError,
1830 "'async for' requires an iterator with "
1831 "__anext__ method, got %.100s",
1832 type->tp_name);
1833 goto error;
1834 }
Yury Selivanov75445082015-05-11 22:57:16 -04001835
Yury Selivanoveb636452016-09-08 22:01:51 -07001836 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1837 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001838 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001839 PyExc_TypeError,
1840 "'async for' received an invalid object "
1841 "from __anext__: %.100s",
1842 Py_TYPE(next_iter)->tp_name);
1843
1844 Py_DECREF(next_iter);
1845 goto error;
1846 } else {
1847 Py_DECREF(next_iter);
1848 }
1849 }
Yury Selivanov75445082015-05-11 22:57:16 -04001850
1851 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001852 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001853 DISPATCH();
1854 }
1855
Benjamin Petersonddd19492018-09-16 22:38:02 -07001856 case TARGET(GET_AWAITABLE): {
1857 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001858 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001859 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001860
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03001861 if (iter == NULL) {
1862 format_awaitable_error(Py_TYPE(iterable),
1863 _Py_OPCODE(next_instr[-2]));
1864 }
1865
Yury Selivanov75445082015-05-11 22:57:16 -04001866 Py_DECREF(iterable);
1867
Yury Selivanovc724bae2016-03-02 11:30:46 -05001868 if (iter != NULL && PyCoro_CheckExact(iter)) {
1869 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1870 if (yf != NULL) {
1871 /* `iter` is a coroutine object that is being
1872 awaited, `yf` is a pointer to the current awaitable
1873 being awaited on. */
1874 Py_DECREF(yf);
1875 Py_CLEAR(iter);
1876 PyErr_SetString(
1877 PyExc_RuntimeError,
1878 "coroutine is being awaited already");
1879 /* The code below jumps to `error` if `iter` is NULL. */
1880 }
1881 }
1882
Yury Selivanov75445082015-05-11 22:57:16 -04001883 SET_TOP(iter); /* Even if it's NULL */
1884
1885 if (iter == NULL) {
1886 goto error;
1887 }
1888
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001889 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001890 DISPATCH();
1891 }
1892
Benjamin Petersonddd19492018-09-16 22:38:02 -07001893 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001894 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001895 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001896 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001897 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1898 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001899 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001900 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001901 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001902 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001903 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001904 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001905 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001906 Py_DECREF(v);
1907 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001908 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001909 if (tstate->c_tracefunc != NULL
1910 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001911 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001912 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001913 if (err < 0)
1914 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001915 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001916 SET_TOP(val);
1917 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001918 }
Martin Panter95f53c12016-07-18 08:23:26 +00001919 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001920 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001921 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001922 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001923 f->f_lasti -= sizeof(_Py_CODEUNIT);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001924 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001925 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001926
Benjamin Petersonddd19492018-09-16 22:38:02 -07001927 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07001929
1930 if (co->co_flags & CO_ASYNC_GENERATOR) {
1931 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
1932 Py_DECREF(retval);
1933 if (w == NULL) {
1934 retval = NULL;
1935 goto error;
1936 }
1937 retval = w;
1938 }
1939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 f->f_stacktop = stack_pointer;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001941 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001942 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001943
Benjamin Petersonddd19492018-09-16 22:38:02 -07001944 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001945 PyObject *type, *value, *traceback;
1946 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001947 PyTryBlock *b = PyFrame_BlockPop(f);
1948 if (b->b_type != EXCEPT_HANDLER) {
1949 PyErr_SetString(PyExc_SystemError,
1950 "popped block is not an except handler");
1951 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001953 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
1954 STACK_LEVEL() <= (b)->b_level + 4);
1955 exc_info = tstate->exc_info;
1956 type = exc_info->exc_type;
1957 value = exc_info->exc_value;
1958 traceback = exc_info->exc_traceback;
1959 exc_info->exc_type = POP();
1960 exc_info->exc_value = POP();
1961 exc_info->exc_traceback = POP();
1962 Py_XDECREF(type);
1963 Py_XDECREF(value);
1964 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001965 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001966 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001967
Benjamin Petersonddd19492018-09-16 22:38:02 -07001968 case TARGET(POP_BLOCK): {
1969 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001970 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001972 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001973
Benjamin Petersonddd19492018-09-16 22:38:02 -07001974 case TARGET(POP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001975 /* If oparg is 0 at the top of the stack are 1 or 6 values:
1976 Either:
1977 - TOP = NULL or an integer
1978 or:
1979 - (TOP, SECOND, THIRD) = exc_info()
1980 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
1981
1982 If oparg is 1 the value for 'return' was additionally pushed
1983 at the top of the stack.
1984 */
1985 PyObject *res = NULL;
1986 if (oparg) {
1987 res = POP();
1988 }
1989 PyObject *exc = POP();
1990 if (exc == NULL || PyLong_CheckExact(exc)) {
1991 Py_XDECREF(exc);
1992 }
1993 else {
1994 Py_DECREF(exc);
1995 Py_DECREF(POP());
1996 Py_DECREF(POP());
1997
1998 PyObject *type, *value, *traceback;
1999 _PyErr_StackItem *exc_info;
2000 PyTryBlock *b = PyFrame_BlockPop(f);
2001 if (b->b_type != EXCEPT_HANDLER) {
2002 PyErr_SetString(PyExc_SystemError,
2003 "popped block is not an except handler");
2004 Py_XDECREF(res);
2005 goto error;
2006 }
2007 assert(STACK_LEVEL() == (b)->b_level + 3);
2008 exc_info = tstate->exc_info;
2009 type = exc_info->exc_type;
2010 value = exc_info->exc_value;
2011 traceback = exc_info->exc_traceback;
2012 exc_info->exc_type = POP();
2013 exc_info->exc_value = POP();
2014 exc_info->exc_traceback = POP();
2015 Py_XDECREF(type);
2016 Py_XDECREF(value);
2017 Py_XDECREF(traceback);
2018 }
2019 if (oparg) {
2020 PUSH(res);
2021 }
2022 DISPATCH();
2023 }
2024
Benjamin Petersonddd19492018-09-16 22:38:02 -07002025 case TARGET(CALL_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002026 PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
2027 if (ret == NULL) {
2028 goto error;
2029 }
2030 PUSH(ret);
2031 JUMPBY(oparg);
2032 FAST_DISPATCH();
2033 }
2034
Benjamin Petersonddd19492018-09-16 22:38:02 -07002035 case TARGET(BEGIN_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002036 /* Push NULL onto the stack for using it in END_FINALLY,
2037 POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH.
2038 */
2039 PUSH(NULL);
2040 FAST_DISPATCH();
2041 }
2042
Benjamin Petersonddd19492018-09-16 22:38:02 -07002043 case TARGET(END_FINALLY): {
2044 PREDICTED(END_FINALLY);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002045 /* At the top of the stack are 1 or 6 values:
2046 Either:
2047 - TOP = NULL or an integer
2048 or:
2049 - (TOP, SECOND, THIRD) = exc_info()
2050 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2051 */
2052 PyObject *exc = POP();
2053 if (exc == NULL) {
2054 FAST_DISPATCH();
2055 }
2056 else if (PyLong_CheckExact(exc)) {
2057 int ret = _PyLong_AsInt(exc);
2058 Py_DECREF(exc);
2059 if (ret == -1 && PyErr_Occurred()) {
2060 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002062 JUMPTO(ret);
2063 FAST_DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002064 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002065 else {
2066 assert(PyExceptionClass_Check(exc));
2067 PyObject *val = POP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002068 PyObject *tb = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002069 PyErr_Restore(exc, val, tb);
2070 goto exception_unwind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002072 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002073
Benjamin Petersonddd19492018-09-16 22:38:02 -07002074 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002075 PyObject *exc = POP();
2076 assert(PyExceptionClass_Check(exc));
2077 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2078 PyTryBlock *b = PyFrame_BlockPop(f);
2079 assert(b->b_type == EXCEPT_HANDLER);
2080 Py_DECREF(exc);
2081 UNWIND_EXCEPT_HANDLER(b);
2082 Py_DECREF(POP());
2083 JUMPBY(oparg);
2084 FAST_DISPATCH();
2085 }
2086 else {
2087 PyObject *val = POP();
2088 PyObject *tb = POP();
2089 PyErr_Restore(exc, val, tb);
2090 goto exception_unwind;
2091 }
2092 }
2093
Benjamin Petersonddd19492018-09-16 22:38:02 -07002094 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002095 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002096
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002097 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002098 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002099 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002100 if (bc == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002101 if (!PyErr_Occurred()) {
2102 PyErr_SetString(PyExc_NameError,
2103 "__build_class__ not found");
2104 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002105 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002106 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002107 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002108 }
2109 else {
2110 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2111 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002112 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002113 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2114 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002115 if (PyErr_ExceptionMatches(PyExc_KeyError))
2116 PyErr_SetString(PyExc_NameError,
2117 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002118 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002119 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002120 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002121 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002122 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002123 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002124
Benjamin Petersonddd19492018-09-16 22:38:02 -07002125 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002126 PyObject *name = GETITEM(names, oparg);
2127 PyObject *v = POP();
2128 PyObject *ns = f->f_locals;
2129 int err;
2130 if (ns == NULL) {
2131 PyErr_Format(PyExc_SystemError,
2132 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002133 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002134 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002135 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002136 if (PyDict_CheckExact(ns))
2137 err = PyDict_SetItem(ns, name, v);
2138 else
2139 err = PyObject_SetItem(ns, name, v);
2140 Py_DECREF(v);
2141 if (err != 0)
2142 goto error;
2143 DISPATCH();
2144 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002145
Benjamin Petersonddd19492018-09-16 22:38:02 -07002146 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002147 PyObject *name = GETITEM(names, oparg);
2148 PyObject *ns = f->f_locals;
2149 int err;
2150 if (ns == NULL) {
2151 PyErr_Format(PyExc_SystemError,
2152 "no locals when deleting %R", name);
2153 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002155 err = PyObject_DelItem(ns, name);
2156 if (err != 0) {
2157 format_exc_check_arg(PyExc_NameError,
2158 NAME_ERROR_MSG,
2159 name);
2160 goto error;
2161 }
2162 DISPATCH();
2163 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002164
Benjamin Petersonddd19492018-09-16 22:38:02 -07002165 case TARGET(UNPACK_SEQUENCE): {
2166 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002167 PyObject *seq = POP(), *item, **items;
2168 if (PyTuple_CheckExact(seq) &&
2169 PyTuple_GET_SIZE(seq) == oparg) {
2170 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002171 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002172 item = items[oparg];
2173 Py_INCREF(item);
2174 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002176 } else if (PyList_CheckExact(seq) &&
2177 PyList_GET_SIZE(seq) == oparg) {
2178 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002180 item = items[oparg];
2181 Py_INCREF(item);
2182 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002183 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002184 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002186 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002187 } else {
2188 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002189 Py_DECREF(seq);
2190 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002192 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002193 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002195
Benjamin Petersonddd19492018-09-16 22:38:02 -07002196 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002197 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2198 PyObject *seq = POP();
2199
2200 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2201 stack_pointer + totalargs)) {
2202 stack_pointer += totalargs;
2203 } else {
2204 Py_DECREF(seq);
2205 goto error;
2206 }
2207 Py_DECREF(seq);
2208 DISPATCH();
2209 }
2210
Benjamin Petersonddd19492018-09-16 22:38:02 -07002211 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002212 PyObject *name = GETITEM(names, oparg);
2213 PyObject *owner = TOP();
2214 PyObject *v = SECOND();
2215 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002216 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002217 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002219 Py_DECREF(owner);
2220 if (err != 0)
2221 goto error;
2222 DISPATCH();
2223 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002224
Benjamin Petersonddd19492018-09-16 22:38:02 -07002225 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002226 PyObject *name = GETITEM(names, oparg);
2227 PyObject *owner = POP();
2228 int err;
2229 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2230 Py_DECREF(owner);
2231 if (err != 0)
2232 goto error;
2233 DISPATCH();
2234 }
2235
Benjamin Petersonddd19492018-09-16 22:38:02 -07002236 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002237 PyObject *name = GETITEM(names, oparg);
2238 PyObject *v = POP();
2239 int err;
2240 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002241 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002242 if (err != 0)
2243 goto error;
2244 DISPATCH();
2245 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002246
Benjamin Petersonddd19492018-09-16 22:38:02 -07002247 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002248 PyObject *name = GETITEM(names, oparg);
2249 int err;
2250 err = PyDict_DelItem(f->f_globals, name);
2251 if (err != 0) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002252 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2253 format_exc_check_arg(
2254 PyExc_NameError, NAME_ERROR_MSG, name);
2255 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002256 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002257 }
2258 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002259 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002260
Benjamin Petersonddd19492018-09-16 22:38:02 -07002261 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002262 PyObject *name = GETITEM(names, oparg);
2263 PyObject *locals = f->f_locals;
2264 PyObject *v;
2265 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002266 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002267 "no locals when loading %R", name);
2268 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002269 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002270 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002271 v = PyDict_GetItemWithError(locals, name);
2272 if (v != NULL) {
2273 Py_INCREF(v);
2274 }
2275 else if (PyErr_Occurred()) {
2276 goto error;
2277 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 }
2279 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002280 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002281 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002282 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2283 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002284 PyErr_Clear();
2285 }
2286 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002287 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002288 v = PyDict_GetItemWithError(f->f_globals, name);
2289 if (v != NULL) {
2290 Py_INCREF(v);
2291 }
2292 else if (PyErr_Occurred()) {
2293 goto error;
2294 }
2295 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002296 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002297 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002298 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002299 if (!PyErr_Occurred()) {
2300 format_exc_check_arg(
Victor Stinnerb0b22422012-04-19 00:57:45 +02002301 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002302 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002303 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002304 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002305 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002306 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002307 }
2308 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002309 v = PyObject_GetItem(f->f_builtins, name);
2310 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002311 if (PyErr_ExceptionMatches(PyExc_KeyError))
2312 format_exc_check_arg(
2313 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002314 NAME_ERROR_MSG, name);
2315 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002316 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002317 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002320 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002321 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002322 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002323
Benjamin Petersonddd19492018-09-16 22:38:02 -07002324 case TARGET(LOAD_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002325 PyObject *name = GETITEM(names, oparg);
2326 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002327 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002328 && PyDict_CheckExact(f->f_builtins))
2329 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002330 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002331 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002332 name);
2333 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002334 if (!_PyErr_OCCURRED()) {
2335 /* _PyDict_LoadGlobal() returns NULL without raising
2336 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002337 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002338 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002339 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002340 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002341 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002342 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002343 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002344 else {
2345 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002346
2347 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002348 v = PyObject_GetItem(f->f_globals, name);
2349 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002350 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2351 goto error;
2352 PyErr_Clear();
2353
Victor Stinnerb4efc962015-11-20 09:24:02 +01002354 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002355 v = PyObject_GetItem(f->f_builtins, name);
2356 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002357 if (PyErr_ExceptionMatches(PyExc_KeyError))
2358 format_exc_check_arg(
2359 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002360 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002361 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002362 }
2363 }
2364 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002365 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002367 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002368
Benjamin Petersonddd19492018-09-16 22:38:02 -07002369 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002370 PyObject *v = GETLOCAL(oparg);
2371 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 SETLOCAL(oparg, NULL);
2373 DISPATCH();
2374 }
2375 format_exc_check_arg(
2376 PyExc_UnboundLocalError,
2377 UNBOUNDLOCAL_ERROR_MSG,
2378 PyTuple_GetItem(co->co_varnames, oparg)
2379 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002380 goto error;
2381 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002382
Benjamin Petersonddd19492018-09-16 22:38:02 -07002383 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002384 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002385 PyObject *oldobj = PyCell_GET(cell);
2386 if (oldobj != NULL) {
2387 PyCell_SET(cell, NULL);
2388 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002389 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002390 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002391 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002392 goto error;
2393 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002394
Benjamin Petersonddd19492018-09-16 22:38:02 -07002395 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002396 PyObject *cell = freevars[oparg];
2397 Py_INCREF(cell);
2398 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002399 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002400 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002401
Benjamin Petersonddd19492018-09-16 22:38:02 -07002402 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002403 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002404 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002405 assert(locals);
2406 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2407 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2408 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2409 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2410 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002411 value = PyDict_GetItemWithError(locals, name);
2412 if (value != NULL) {
2413 Py_INCREF(value);
2414 }
2415 else if (PyErr_Occurred()) {
2416 goto error;
2417 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002418 }
2419 else {
2420 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002421 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002422 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2423 goto error;
2424 PyErr_Clear();
2425 }
2426 }
2427 if (!value) {
2428 PyObject *cell = freevars[oparg];
2429 value = PyCell_GET(cell);
2430 if (value == NULL) {
2431 format_exc_unbound(co, oparg);
2432 goto error;
2433 }
2434 Py_INCREF(value);
2435 }
2436 PUSH(value);
2437 DISPATCH();
2438 }
2439
Benjamin Petersonddd19492018-09-16 22:38:02 -07002440 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002441 PyObject *cell = freevars[oparg];
2442 PyObject *value = PyCell_GET(cell);
2443 if (value == NULL) {
2444 format_exc_unbound(co, oparg);
2445 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002446 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002447 Py_INCREF(value);
2448 PUSH(value);
2449 DISPATCH();
2450 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002451
Benjamin Petersonddd19492018-09-16 22:38:02 -07002452 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002453 PyObject *v = POP();
2454 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002455 PyObject *oldobj = PyCell_GET(cell);
2456 PyCell_SET(cell, v);
2457 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002458 DISPATCH();
2459 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002460
Benjamin Petersonddd19492018-09-16 22:38:02 -07002461 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002462 PyObject *str;
2463 PyObject *empty = PyUnicode_New(0, 0);
2464 if (empty == NULL) {
2465 goto error;
2466 }
2467 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2468 Py_DECREF(empty);
2469 if (str == NULL)
2470 goto error;
2471 while (--oparg >= 0) {
2472 PyObject *item = POP();
2473 Py_DECREF(item);
2474 }
2475 PUSH(str);
2476 DISPATCH();
2477 }
2478
Benjamin Petersonddd19492018-09-16 22:38:02 -07002479 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002480 PyObject *tup = PyTuple_New(oparg);
2481 if (tup == NULL)
2482 goto error;
2483 while (--oparg >= 0) {
2484 PyObject *item = POP();
2485 PyTuple_SET_ITEM(tup, oparg, item);
2486 }
2487 PUSH(tup);
2488 DISPATCH();
2489 }
2490
Benjamin Petersonddd19492018-09-16 22:38:02 -07002491 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002492 PyObject *list = PyList_New(oparg);
2493 if (list == NULL)
2494 goto error;
2495 while (--oparg >= 0) {
2496 PyObject *item = POP();
2497 PyList_SET_ITEM(list, oparg, item);
2498 }
2499 PUSH(list);
2500 DISPATCH();
2501 }
2502
Benjamin Petersonddd19492018-09-16 22:38:02 -07002503 case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL):
2504 case TARGET(BUILD_TUPLE_UNPACK):
2505 case TARGET(BUILD_LIST_UNPACK): {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002506 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002507 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002508 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002509 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002510
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002511 if (sum == NULL)
2512 goto error;
2513
2514 for (i = oparg; i > 0; i--) {
2515 PyObject *none_val;
2516
2517 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2518 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002519 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002520 PyErr_ExceptionMatches(PyExc_TypeError))
2521 {
2522 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002523 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002524 Py_DECREF(sum);
2525 goto error;
2526 }
2527 Py_DECREF(none_val);
2528 }
2529
2530 if (convert_to_tuple) {
2531 return_value = PyList_AsTuple(sum);
2532 Py_DECREF(sum);
2533 if (return_value == NULL)
2534 goto error;
2535 }
2536 else {
2537 return_value = sum;
2538 }
2539
2540 while (oparg--)
2541 Py_DECREF(POP());
2542 PUSH(return_value);
2543 DISPATCH();
2544 }
2545
Benjamin Petersonddd19492018-09-16 22:38:02 -07002546 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002547 PyObject *set = PySet_New(NULL);
2548 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002549 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002550 if (set == NULL)
2551 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002552 for (i = oparg; i > 0; i--) {
2553 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002554 if (err == 0)
2555 err = PySet_Add(set, item);
2556 Py_DECREF(item);
2557 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002558 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002559 if (err != 0) {
2560 Py_DECREF(set);
2561 goto error;
2562 }
2563 PUSH(set);
2564 DISPATCH();
2565 }
2566
Benjamin Petersonddd19492018-09-16 22:38:02 -07002567 case TARGET(BUILD_SET_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002568 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002569 PyObject *sum = PySet_New(NULL);
2570 if (sum == NULL)
2571 goto error;
2572
2573 for (i = oparg; i > 0; i--) {
2574 if (_PySet_Update(sum, PEEK(i)) < 0) {
2575 Py_DECREF(sum);
2576 goto error;
2577 }
2578 }
2579
2580 while (oparg--)
2581 Py_DECREF(POP());
2582 PUSH(sum);
2583 DISPATCH();
2584 }
2585
Benjamin Petersonddd19492018-09-16 22:38:02 -07002586 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002587 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002588 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2589 if (map == NULL)
2590 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002591 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002592 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002593 PyObject *key = PEEK(2*i);
2594 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002595 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002596 if (err != 0) {
2597 Py_DECREF(map);
2598 goto error;
2599 }
2600 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002601
2602 while (oparg--) {
2603 Py_DECREF(POP());
2604 Py_DECREF(POP());
2605 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002606 PUSH(map);
2607 DISPATCH();
2608 }
2609
Benjamin Petersonddd19492018-09-16 22:38:02 -07002610 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002611 _Py_IDENTIFIER(__annotations__);
2612 int err;
2613 PyObject *ann_dict;
2614 if (f->f_locals == NULL) {
2615 PyErr_Format(PyExc_SystemError,
2616 "no locals found when setting up annotations");
2617 goto error;
2618 }
2619 /* check if __annotations__ in locals()... */
2620 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002621 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002622 &PyId___annotations__);
2623 if (ann_dict == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002624 if (PyErr_Occurred()) {
2625 goto error;
2626 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002627 /* ...if not, create a new one */
2628 ann_dict = PyDict_New();
2629 if (ann_dict == NULL) {
2630 goto error;
2631 }
2632 err = _PyDict_SetItemId(f->f_locals,
2633 &PyId___annotations__, ann_dict);
2634 Py_DECREF(ann_dict);
2635 if (err != 0) {
2636 goto error;
2637 }
2638 }
2639 }
2640 else {
2641 /* do the same if locals() is not a dict */
2642 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2643 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002644 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002645 }
2646 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2647 if (ann_dict == NULL) {
2648 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2649 goto error;
2650 }
2651 PyErr_Clear();
2652 ann_dict = PyDict_New();
2653 if (ann_dict == NULL) {
2654 goto error;
2655 }
2656 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2657 Py_DECREF(ann_dict);
2658 if (err != 0) {
2659 goto error;
2660 }
2661 }
2662 else {
2663 Py_DECREF(ann_dict);
2664 }
2665 }
2666 DISPATCH();
2667 }
2668
Benjamin Petersonddd19492018-09-16 22:38:02 -07002669 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002670 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002671 PyObject *map;
2672 PyObject *keys = TOP();
2673 if (!PyTuple_CheckExact(keys) ||
2674 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2675 PyErr_SetString(PyExc_SystemError,
2676 "bad BUILD_CONST_KEY_MAP keys argument");
2677 goto error;
2678 }
2679 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2680 if (map == NULL) {
2681 goto error;
2682 }
2683 for (i = oparg; i > 0; i--) {
2684 int err;
2685 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2686 PyObject *value = PEEK(i + 1);
2687 err = PyDict_SetItem(map, key, value);
2688 if (err != 0) {
2689 Py_DECREF(map);
2690 goto error;
2691 }
2692 }
2693
2694 Py_DECREF(POP());
2695 while (oparg--) {
2696 Py_DECREF(POP());
2697 }
2698 PUSH(map);
2699 DISPATCH();
2700 }
2701
Benjamin Petersonddd19492018-09-16 22:38:02 -07002702 case TARGET(BUILD_MAP_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002703 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002704 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002705 if (sum == NULL)
2706 goto error;
2707
2708 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002709 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002710 if (PyDict_Update(sum, arg) < 0) {
2711 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2712 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002713 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002714 arg->ob_type->tp_name);
2715 }
2716 Py_DECREF(sum);
2717 goto error;
2718 }
2719 }
2720
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002721 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002722 Py_DECREF(POP());
2723 PUSH(sum);
2724 DISPATCH();
2725 }
2726
Benjamin Petersonddd19492018-09-16 22:38:02 -07002727 case TARGET(BUILD_MAP_UNPACK_WITH_CALL): {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002728 Py_ssize_t i;
2729 PyObject *sum = PyDict_New();
2730 if (sum == NULL)
2731 goto error;
2732
2733 for (i = oparg; i > 0; i--) {
2734 PyObject *arg = PEEK(i);
2735 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002736 Py_DECREF(sum);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02002737 format_kwargs_error(PEEK(2 + oparg), arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002738 goto error;
2739 }
2740 }
2741
2742 while (oparg--)
2743 Py_DECREF(POP());
2744 PUSH(sum);
2745 DISPATCH();
2746 }
2747
Benjamin Petersonddd19492018-09-16 22:38:02 -07002748 case TARGET(MAP_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002749 PyObject *key = TOP();
2750 PyObject *value = SECOND();
2751 PyObject *map;
2752 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002753 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002754 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002755 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002756 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002757 Py_DECREF(value);
2758 Py_DECREF(key);
2759 if (err != 0)
2760 goto error;
2761 PREDICT(JUMP_ABSOLUTE);
2762 DISPATCH();
2763 }
2764
Benjamin Petersonddd19492018-09-16 22:38:02 -07002765 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002766 PyObject *name = GETITEM(names, oparg);
2767 PyObject *owner = TOP();
2768 PyObject *res = PyObject_GetAttr(owner, name);
2769 Py_DECREF(owner);
2770 SET_TOP(res);
2771 if (res == NULL)
2772 goto error;
2773 DISPATCH();
2774 }
2775
Benjamin Petersonddd19492018-09-16 22:38:02 -07002776 case TARGET(COMPARE_OP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002777 PyObject *right = POP();
2778 PyObject *left = TOP();
2779 PyObject *res = cmp_outcome(oparg, left, right);
2780 Py_DECREF(left);
2781 Py_DECREF(right);
2782 SET_TOP(res);
2783 if (res == NULL)
2784 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002785 PREDICT(POP_JUMP_IF_FALSE);
2786 PREDICT(POP_JUMP_IF_TRUE);
2787 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002788 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002789
Benjamin Petersonddd19492018-09-16 22:38:02 -07002790 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002791 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002792 PyObject *fromlist = POP();
2793 PyObject *level = TOP();
2794 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002795 res = import_name(f, name, fromlist, level);
2796 Py_DECREF(level);
2797 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002798 SET_TOP(res);
2799 if (res == NULL)
2800 goto error;
2801 DISPATCH();
2802 }
2803
Benjamin Petersonddd19492018-09-16 22:38:02 -07002804 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002805 PyObject *from = POP(), *locals;
2806 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002807 if (PyFrame_FastToLocalsWithError(f) < 0) {
2808 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002809 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002810 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002811
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002812 locals = f->f_locals;
2813 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 PyErr_SetString(PyExc_SystemError,
2815 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002816 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002817 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002818 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002819 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002820 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002821 Py_DECREF(from);
2822 if (err != 0)
2823 goto error;
2824 DISPATCH();
2825 }
Guido van Rossum25831651993-05-19 14:50:45 +00002826
Benjamin Petersonddd19492018-09-16 22:38:02 -07002827 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002828 PyObject *name = GETITEM(names, oparg);
2829 PyObject *from = TOP();
2830 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002831 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002832 PUSH(res);
2833 if (res == NULL)
2834 goto error;
2835 DISPATCH();
2836 }
Thomas Wouters52152252000-08-17 22:55:00 +00002837
Benjamin Petersonddd19492018-09-16 22:38:02 -07002838 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002839 JUMPBY(oparg);
2840 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002841 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002842
Benjamin Petersonddd19492018-09-16 22:38:02 -07002843 case TARGET(POP_JUMP_IF_FALSE): {
2844 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002845 PyObject *cond = POP();
2846 int err;
2847 if (cond == Py_True) {
2848 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002849 FAST_DISPATCH();
2850 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002851 if (cond == Py_False) {
2852 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 JUMPTO(oparg);
2854 FAST_DISPATCH();
2855 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002856 err = PyObject_IsTrue(cond);
2857 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002858 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002859 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002860 else if (err == 0)
2861 JUMPTO(oparg);
2862 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002863 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002864 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002865 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002866
Benjamin Petersonddd19492018-09-16 22:38:02 -07002867 case TARGET(POP_JUMP_IF_TRUE): {
2868 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002869 PyObject *cond = POP();
2870 int err;
2871 if (cond == Py_False) {
2872 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002873 FAST_DISPATCH();
2874 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002875 if (cond == Py_True) {
2876 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002877 JUMPTO(oparg);
2878 FAST_DISPATCH();
2879 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002880 err = PyObject_IsTrue(cond);
2881 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002882 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002883 JUMPTO(oparg);
2884 }
2885 else if (err == 0)
2886 ;
2887 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002888 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002889 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002890 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002891
Benjamin Petersonddd19492018-09-16 22:38:02 -07002892 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002893 PyObject *cond = TOP();
2894 int err;
2895 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002896 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002897 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002898 FAST_DISPATCH();
2899 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002900 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 JUMPTO(oparg);
2902 FAST_DISPATCH();
2903 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002904 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002906 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002907 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 }
2909 else if (err == 0)
2910 JUMPTO(oparg);
2911 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002912 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002914 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002915
Benjamin Petersonddd19492018-09-16 22:38:02 -07002916 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002917 PyObject *cond = TOP();
2918 int err;
2919 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002920 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002921 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002922 FAST_DISPATCH();
2923 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002924 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 JUMPTO(oparg);
2926 FAST_DISPATCH();
2927 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002928 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002929 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002930 JUMPTO(oparg);
2931 }
2932 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002933 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002934 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002935 }
2936 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002937 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002938 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002939 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002940
Benjamin Petersonddd19492018-09-16 22:38:02 -07002941 case TARGET(JUMP_ABSOLUTE): {
2942 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002943 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002944#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002945 /* Enabling this path speeds-up all while and for-loops by bypassing
2946 the per-loop checks for signals. By default, this should be turned-off
2947 because it prevents detection of a control-break in tight loops like
2948 "while 1: pass". Compile with this option turned-on when you need
2949 the speed-up and do not need break checking inside tight loops (ones
2950 that contain only instructions ending with FAST_DISPATCH).
2951 */
2952 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002953#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002954 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002955#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002956 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002957
Benjamin Petersonddd19492018-09-16 22:38:02 -07002958 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002959 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002960 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002961 PyObject *iter = PyObject_GetIter(iterable);
2962 Py_DECREF(iterable);
2963 SET_TOP(iter);
2964 if (iter == NULL)
2965 goto error;
2966 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002967 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002968 DISPATCH();
2969 }
2970
Benjamin Petersonddd19492018-09-16 22:38:02 -07002971 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04002972 /* before: [obj]; after [getiter(obj)] */
2973 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002974 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002975 if (PyCoro_CheckExact(iterable)) {
2976 /* `iterable` is a coroutine */
2977 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2978 /* and it is used in a 'yield from' expression of a
2979 regular generator. */
2980 Py_DECREF(iterable);
2981 SET_TOP(NULL);
2982 PyErr_SetString(PyExc_TypeError,
2983 "cannot 'yield from' a coroutine object "
2984 "in a non-coroutine generator");
2985 goto error;
2986 }
2987 }
2988 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002989 /* `iterable` is not a generator. */
2990 iter = PyObject_GetIter(iterable);
2991 Py_DECREF(iterable);
2992 SET_TOP(iter);
2993 if (iter == NULL)
2994 goto error;
2995 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002996 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002997 DISPATCH();
2998 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002999
Benjamin Petersonddd19492018-09-16 22:38:02 -07003000 case TARGET(FOR_ITER): {
3001 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003002 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003003 PyObject *iter = TOP();
3004 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3005 if (next != NULL) {
3006 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003007 PREDICT(STORE_FAST);
3008 PREDICT(UNPACK_SEQUENCE);
3009 DISPATCH();
3010 }
3011 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003012 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
3013 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08003014 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003015 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003016 PyErr_Clear();
3017 }
3018 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003019 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003020 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003021 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003022 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003023 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003024 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003025
Benjamin Petersonddd19492018-09-16 22:38:02 -07003026 case TARGET(SETUP_FINALLY): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003027 /* NOTE: If you add any new block-setup opcodes that
3028 are not try/except/finally handlers, you may need
3029 to update the PyGen_NeedsFinalizing() function.
3030 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003031
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003032 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003033 STACK_LEVEL());
3034 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003035 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003036
Benjamin Petersonddd19492018-09-16 22:38:02 -07003037 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003038 _Py_IDENTIFIER(__aexit__);
3039 _Py_IDENTIFIER(__aenter__);
3040
3041 PyObject *mgr = TOP();
3042 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3043 *enter;
3044 PyObject *res;
3045 if (exit == NULL)
3046 goto error;
3047 SET_TOP(exit);
3048 enter = special_lookup(mgr, &PyId___aenter__);
3049 Py_DECREF(mgr);
3050 if (enter == NULL)
3051 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003052 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003053 Py_DECREF(enter);
3054 if (res == NULL)
3055 goto error;
3056 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003057 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003058 DISPATCH();
3059 }
3060
Benjamin Petersonddd19492018-09-16 22:38:02 -07003061 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003062 PyObject *res = POP();
3063 /* Setup the finally block before pushing the result
3064 of __aenter__ on the stack. */
3065 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3066 STACK_LEVEL());
3067 PUSH(res);
3068 DISPATCH();
3069 }
3070
Benjamin Petersonddd19492018-09-16 22:38:02 -07003071 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003072 _Py_IDENTIFIER(__exit__);
3073 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003074 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08003075 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003076 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08003077 if (enter == NULL)
3078 goto error;
3079 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003080 if (exit == NULL) {
3081 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003082 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003083 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003084 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003085 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003086 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003087 Py_DECREF(enter);
3088 if (res == NULL)
3089 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090 /* Setup the finally block before pushing the result
3091 of __enter__ on the stack. */
3092 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3093 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003094
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003095 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003096 DISPATCH();
3097 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003098
Benjamin Petersonddd19492018-09-16 22:38:02 -07003099 case TARGET(WITH_CLEANUP_START): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003100 /* At the top of the stack are 1 or 6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003101 how/why we entered the finally clause:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003102 - TOP = NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003103 - (TOP, SECOND, THIRD) = exc_info()
3104 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003105 Below them is EXIT, the context.__exit__ or context.__aexit__
3106 bound method.
3107 In the first case, we must call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003108 EXIT(None, None, None)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003109 otherwise we must call
3110 EXIT(TOP, SECOND, THIRD)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003111
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003112 In the first case, we remove EXIT from the
3113 stack, leaving TOP, and push TOP on the stack.
3114 Otherwise we shift the bottom 3 values of the
3115 stack down, replace the empty spot with NULL, and push
3116 None on the stack.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003117
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003118 Finally we push the result of the call.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003119 */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003120 PyObject *stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003121 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003122 PyObject *exc, *val, *tb, *res;
3123
3124 val = tb = Py_None;
3125 exc = TOP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003126 if (exc == NULL) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003127 STACK_SHRINK(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003128 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003129 SET_TOP(exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003130 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003131 }
3132 else {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003133 assert(PyExceptionClass_Check(exc));
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003134 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003135 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003136 val = SECOND();
3137 tb = THIRD();
3138 tp2 = FOURTH();
3139 exc2 = PEEK(5);
3140 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003141 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003142 SET_VALUE(7, tb2);
3143 SET_VALUE(6, exc2);
3144 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003145 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3146 SET_FOURTH(NULL);
3147 /* We just shifted the stack down, so we have
3148 to tell the except handler block that the
3149 values are lower than it expects. */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003150 assert(f->f_iblock > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003151 block = &f->f_blockstack[f->f_iblock - 1];
3152 assert(block->b_type == EXCEPT_HANDLER);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003153 assert(block->b_level > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154 block->b_level--;
3155 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003156
3157 stack[0] = exc;
3158 stack[1] = val;
3159 stack[2] = tb;
3160 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003161 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003162 if (res == NULL)
3163 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003164
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003165 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003166 PUSH(exc);
3167 PUSH(res);
3168 PREDICT(WITH_CLEANUP_FINISH);
3169 DISPATCH();
3170 }
3171
Benjamin Petersonddd19492018-09-16 22:38:02 -07003172 case TARGET(WITH_CLEANUP_FINISH): {
3173 PREDICTED(WITH_CLEANUP_FINISH);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003174 /* TOP = the result of calling the context.__exit__ bound method
3175 SECOND = either None or exception type
3176
3177 If SECOND is None below is NULL or the return address,
3178 otherwise below are 7 values representing an exception.
3179 */
Yury Selivanov75445082015-05-11 22:57:16 -04003180 PyObject *res = POP();
3181 PyObject *exc = POP();
3182 int err;
3183
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003184 if (exc != Py_None)
3185 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003186 else
3187 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003188
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003189 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003190 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003192 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003193 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003194 else if (err > 0) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003195 /* There was an exception and a True return.
3196 * We must manually unwind the EXCEPT_HANDLER block
3197 * which was created when the exception was caught,
Quan Tian3bd0d622018-10-20 05:30:03 +08003198 * otherwise the stack will be in an inconsistent state.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003199 */
3200 PyTryBlock *b = PyFrame_BlockPop(f);
3201 assert(b->b_type == EXCEPT_HANDLER);
3202 UNWIND_EXCEPT_HANDLER(b);
3203 PUSH(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003204 }
3205 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003206 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003207 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003208
Benjamin Petersonddd19492018-09-16 22:38:02 -07003209 case TARGET(LOAD_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003210 /* Designed to work in tamdem with CALL_METHOD. */
3211 PyObject *name = GETITEM(names, oparg);
3212 PyObject *obj = TOP();
3213 PyObject *meth = NULL;
3214
3215 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3216
Yury Selivanovf2392132016-12-13 19:03:51 -05003217 if (meth == NULL) {
3218 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003219 goto error;
3220 }
3221
3222 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003223 /* We can bypass temporary bound method object.
3224 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003225
INADA Naoki015bce62017-01-16 17:23:30 +09003226 meth | self | arg1 | ... | argN
3227 */
3228 SET_TOP(meth);
3229 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003230 }
3231 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003232 /* meth is not an unbound method (but a regular attr, or
3233 something was returned by a descriptor protocol). Set
3234 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003235 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003236
3237 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003238 */
INADA Naoki015bce62017-01-16 17:23:30 +09003239 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003240 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003241 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003242 }
3243 DISPATCH();
3244 }
3245
Benjamin Petersonddd19492018-09-16 22:38:02 -07003246 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003247 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003248 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003249
3250 sp = stack_pointer;
3251
INADA Naoki015bce62017-01-16 17:23:30 +09003252 meth = PEEK(oparg + 2);
3253 if (meth == NULL) {
3254 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3255 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003256
3257 Stack layout:
3258
INADA Naoki015bce62017-01-16 17:23:30 +09003259 ... | NULL | callable | arg1 | ... | argN
3260 ^- TOP()
3261 ^- (-oparg)
3262 ^- (-oparg-1)
3263 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003264
Ville Skyttä49b27342017-08-03 09:00:59 +03003265 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003266 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003267 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003268 res = call_function(&sp, oparg, NULL);
3269 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003270 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003271 }
3272 else {
3273 /* This is a method call. Stack layout:
3274
INADA Naoki015bce62017-01-16 17:23:30 +09003275 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003276 ^- TOP()
3277 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003278 ^- (-oparg-1)
3279 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003280
INADA Naoki015bce62017-01-16 17:23:30 +09003281 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003282 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003283 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003284 */
3285 res = call_function(&sp, oparg + 1, NULL);
3286 stack_pointer = sp;
3287 }
3288
3289 PUSH(res);
3290 if (res == NULL)
3291 goto error;
3292 DISPATCH();
3293 }
3294
Benjamin Petersonddd19492018-09-16 22:38:02 -07003295 case TARGET(CALL_FUNCTION): {
3296 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003297 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003298 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003299 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003300 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003301 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003302 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003303 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003304 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003305 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003306 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003307
Benjamin Petersonddd19492018-09-16 22:38:02 -07003308 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003309 PyObject **sp, *res, *names;
3310
3311 names = POP();
3312 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003313 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003314 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003316 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003317 Py_DECREF(names);
3318
3319 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003320 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003321 }
3322 DISPATCH();
3323 }
3324
Benjamin Petersonddd19492018-09-16 22:38:02 -07003325 case TARGET(CALL_FUNCTION_EX): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003326 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003327 if (oparg & 0x01) {
3328 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003329 if (!PyDict_CheckExact(kwargs)) {
3330 PyObject *d = PyDict_New();
3331 if (d == NULL)
3332 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003333 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003334 Py_DECREF(d);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003335 format_kwargs_error(SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003336 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003337 goto error;
3338 }
3339 Py_DECREF(kwargs);
3340 kwargs = d;
3341 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003342 assert(PyDict_CheckExact(kwargs));
3343 }
3344 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003345 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003346 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003347 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003348 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003349 goto error;
3350 }
3351 Py_SETREF(callargs, PySequence_Tuple(callargs));
3352 if (callargs == NULL) {
3353 goto error;
3354 }
3355 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003356 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003357
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003358 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003359 Py_DECREF(func);
3360 Py_DECREF(callargs);
3361 Py_XDECREF(kwargs);
3362
3363 SET_TOP(result);
3364 if (result == NULL) {
3365 goto error;
3366 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003367 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003368 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003369
Benjamin Petersonddd19492018-09-16 22:38:02 -07003370 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003371 PyObject *qualname = POP();
3372 PyObject *codeobj = POP();
3373 PyFunctionObject *func = (PyFunctionObject *)
3374 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003375
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003376 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003377 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003378 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003379 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003380 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003381
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003382 if (oparg & 0x08) {
3383 assert(PyTuple_CheckExact(TOP()));
3384 func ->func_closure = POP();
3385 }
3386 if (oparg & 0x04) {
3387 assert(PyDict_CheckExact(TOP()));
3388 func->func_annotations = POP();
3389 }
3390 if (oparg & 0x02) {
3391 assert(PyDict_CheckExact(TOP()));
3392 func->func_kwdefaults = POP();
3393 }
3394 if (oparg & 0x01) {
3395 assert(PyTuple_CheckExact(TOP()));
3396 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003397 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003398
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003399 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003400 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003401 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003402
Benjamin Petersonddd19492018-09-16 22:38:02 -07003403 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003404 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003405 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003406 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003407 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003408 step = NULL;
3409 stop = POP();
3410 start = TOP();
3411 slice = PySlice_New(start, stop, step);
3412 Py_DECREF(start);
3413 Py_DECREF(stop);
3414 Py_XDECREF(step);
3415 SET_TOP(slice);
3416 if (slice == NULL)
3417 goto error;
3418 DISPATCH();
3419 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003420
Benjamin Petersonddd19492018-09-16 22:38:02 -07003421 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003422 /* Handles f-string value formatting. */
3423 PyObject *result;
3424 PyObject *fmt_spec;
3425 PyObject *value;
3426 PyObject *(*conv_fn)(PyObject *);
3427 int which_conversion = oparg & FVC_MASK;
3428 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3429
3430 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003431 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003432
3433 /* See if any conversion is specified. */
3434 switch (which_conversion) {
3435 case FVC_STR: conv_fn = PyObject_Str; break;
3436 case FVC_REPR: conv_fn = PyObject_Repr; break;
3437 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3438
3439 /* Must be 0 (meaning no conversion), since only four
3440 values are allowed by (oparg & FVC_MASK). */
3441 default: conv_fn = NULL; break;
3442 }
3443
3444 /* If there's a conversion function, call it and replace
3445 value with that result. Otherwise, just use value,
3446 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003447 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003448 result = conv_fn(value);
3449 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003450 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003451 Py_XDECREF(fmt_spec);
3452 goto error;
3453 }
3454 value = result;
3455 }
3456
3457 /* If value is a unicode object, and there's no fmt_spec,
3458 then we know the result of format(value) is value
3459 itself. In that case, skip calling format(). I plan to
3460 move this optimization in to PyObject_Format()
3461 itself. */
3462 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3463 /* Do nothing, just transfer ownership to result. */
3464 result = value;
3465 } else {
3466 /* Actually call format(). */
3467 result = PyObject_Format(value, fmt_spec);
3468 Py_DECREF(value);
3469 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003470 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003471 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003472 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003473 }
3474
Eric V. Smith135d5f42016-02-05 18:23:08 -05003475 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003476 DISPATCH();
3477 }
3478
Benjamin Petersonddd19492018-09-16 22:38:02 -07003479 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003480 int oldoparg = oparg;
3481 NEXTOPARG();
3482 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003483 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003484 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003485
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003486
Antoine Pitrou042b1282010-08-13 21:15:58 +00003487#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003488 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003489#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003490 default:
3491 fprintf(stderr,
3492 "XXX lineno: %d, opcode: %d\n",
3493 PyFrame_GetLineNumber(f),
3494 opcode);
3495 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003496 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003498 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003499
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003500 /* This should never be reached. Every opcode should end with DISPATCH()
3501 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003502 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003503
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003504error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003505 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003506#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003507 if (!PyErr_Occurred())
3508 PyErr_SetString(PyExc_SystemError,
3509 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003510#else
3511 assert(PyErr_Occurred());
3512#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003513
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003514 /* Log traceback info. */
3515 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003516
Benjamin Peterson51f46162013-01-23 08:38:47 -05003517 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003518 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3519 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003520
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003521exception_unwind:
3522 /* Unwind stacks if an exception occurred */
3523 while (f->f_iblock > 0) {
3524 /* Pop the current block. */
3525 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003527 if (b->b_type == EXCEPT_HANDLER) {
3528 UNWIND_EXCEPT_HANDLER(b);
3529 continue;
3530 }
3531 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003532 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003533 PyObject *exc, *val, *tb;
3534 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003535 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003536 /* Beware, this invalidates all b->b_* fields */
3537 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003538 PUSH(exc_info->exc_traceback);
3539 PUSH(exc_info->exc_value);
3540 if (exc_info->exc_type != NULL) {
3541 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003542 }
3543 else {
3544 Py_INCREF(Py_None);
3545 PUSH(Py_None);
3546 }
3547 PyErr_Fetch(&exc, &val, &tb);
3548 /* Make the raw exception data
3549 available to the handler,
3550 so a program can emulate the
3551 Python main loop. */
3552 PyErr_NormalizeException(
3553 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003554 if (tb != NULL)
3555 PyException_SetTraceback(val, tb);
3556 else
3557 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003558 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003559 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003560 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003561 exc_info->exc_value = val;
3562 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003563 if (tb == NULL)
3564 tb = Py_None;
3565 Py_INCREF(tb);
3566 PUSH(tb);
3567 PUSH(val);
3568 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003569 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003570 /* Resume normal execution */
3571 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003572 }
3573 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003574
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003575 /* End the loop as we still have an error */
3576 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003577 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003579 /* Pop remaining stack entries. */
3580 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003581 PyObject *o = POP();
3582 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003583 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003584
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003585 assert(retval == NULL);
3586 assert(PyErr_Occurred());
Guido van Rossumac7be682001-01-17 15:42:30 +00003587
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003588return_or_yield:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003589 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003590 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003591 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3592 tstate, f, PyTrace_RETURN, retval)) {
3593 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003594 }
3595 }
3596 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003597 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3598 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003599 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003600 }
3601 }
3602 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003604 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003605exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003606 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3607 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003608 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003609 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003610 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003611
Victor Stinnerefde1462015-03-21 15:04:43 +01003612 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003613}
3614
Benjamin Petersonb204a422011-06-05 22:04:07 -05003615static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003616format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3617{
3618 int err;
3619 Py_ssize_t len = PyList_GET_SIZE(names);
3620 PyObject *name_str, *comma, *tail, *tmp;
3621
3622 assert(PyList_CheckExact(names));
3623 assert(len >= 1);
3624 /* Deal with the joys of natural language. */
3625 switch (len) {
3626 case 1:
3627 name_str = PyList_GET_ITEM(names, 0);
3628 Py_INCREF(name_str);
3629 break;
3630 case 2:
3631 name_str = PyUnicode_FromFormat("%U and %U",
3632 PyList_GET_ITEM(names, len - 2),
3633 PyList_GET_ITEM(names, len - 1));
3634 break;
3635 default:
3636 tail = PyUnicode_FromFormat(", %U, and %U",
3637 PyList_GET_ITEM(names, len - 2),
3638 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003639 if (tail == NULL)
3640 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003641 /* Chop off the last two objects in the list. This shouldn't actually
3642 fail, but we can't be too careful. */
3643 err = PyList_SetSlice(names, len - 2, len, NULL);
3644 if (err == -1) {
3645 Py_DECREF(tail);
3646 return;
3647 }
3648 /* Stitch everything up into a nice comma-separated list. */
3649 comma = PyUnicode_FromString(", ");
3650 if (comma == NULL) {
3651 Py_DECREF(tail);
3652 return;
3653 }
3654 tmp = PyUnicode_Join(comma, names);
3655 Py_DECREF(comma);
3656 if (tmp == NULL) {
3657 Py_DECREF(tail);
3658 return;
3659 }
3660 name_str = PyUnicode_Concat(tmp, tail);
3661 Py_DECREF(tmp);
3662 Py_DECREF(tail);
3663 break;
3664 }
3665 if (name_str == NULL)
3666 return;
3667 PyErr_Format(PyExc_TypeError,
3668 "%U() missing %i required %s argument%s: %U",
3669 co->co_name,
3670 len,
3671 kind,
3672 len == 1 ? "" : "s",
3673 name_str);
3674 Py_DECREF(name_str);
3675}
3676
3677static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003678missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003679 PyObject **fastlocals)
3680{
Victor Stinner74319ae2016-08-25 00:04:09 +02003681 Py_ssize_t i, j = 0;
3682 Py_ssize_t start, end;
3683 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003684 const char *kind = positional ? "positional" : "keyword-only";
3685 PyObject *missing_names;
3686
3687 /* Compute the names of the arguments that are missing. */
3688 missing_names = PyList_New(missing);
3689 if (missing_names == NULL)
3690 return;
3691 if (positional) {
3692 start = 0;
3693 end = co->co_argcount - defcount;
3694 }
3695 else {
3696 start = co->co_argcount;
3697 end = start + co->co_kwonlyargcount;
3698 }
3699 for (i = start; i < end; i++) {
3700 if (GETLOCAL(i) == NULL) {
3701 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3702 PyObject *name = PyObject_Repr(raw);
3703 if (name == NULL) {
3704 Py_DECREF(missing_names);
3705 return;
3706 }
3707 PyList_SET_ITEM(missing_names, j++, name);
3708 }
3709 }
3710 assert(j == missing);
3711 format_missing(kind, co, missing_names);
3712 Py_DECREF(missing_names);
3713}
3714
3715static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003716too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3717 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003718{
3719 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003720 Py_ssize_t kwonly_given = 0;
3721 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003722 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003723 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003724
Benjamin Petersone109c702011-06-24 09:37:26 -05003725 assert((co->co_flags & CO_VARARGS) == 0);
3726 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003727 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3728 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003729 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003730 }
3731 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003732 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003733 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003734 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003735 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003736 }
3737 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003738 plural = (co_argcount != 1);
3739 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003740 }
3741 if (sig == NULL)
3742 return;
3743 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003744 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3745 kwonly_sig = PyUnicode_FromFormat(format,
3746 given != 1 ? "s" : "",
3747 kwonly_given,
3748 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003749 if (kwonly_sig == NULL) {
3750 Py_DECREF(sig);
3751 return;
3752 }
3753 }
3754 else {
3755 /* This will not fail. */
3756 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003757 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003758 }
3759 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003760 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003761 co->co_name,
3762 sig,
3763 plural ? "s" : "",
3764 given,
3765 kwonly_sig,
3766 given == 1 && !kwonly_given ? "was" : "were");
3767 Py_DECREF(sig);
3768 Py_DECREF(kwonly_sig);
3769}
3770
Guido van Rossumc2e20742006-02-27 22:32:47 +00003771/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003772 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003773 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003774
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003775PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003776_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003777 PyObject *const *args, Py_ssize_t argcount,
3778 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003779 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003780 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003781 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003782 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003783{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003784 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003785 PyFrameObject *f;
3786 PyObject *retval = NULL;
3787 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003788 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003789 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003790 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3791 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003792 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003794 if (globals == NULL) {
3795 PyErr_SetString(PyExc_SystemError,
3796 "PyEval_EvalCodeEx: NULL globals");
3797 return NULL;
3798 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003799
Victor Stinnerc7020012016-08-16 23:40:29 +02003800 /* Create the frame */
Victor Stinner50b48572018-11-01 01:51:40 +01003801 tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003802 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003803 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003804 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003805 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003806 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003807 fastlocals = f->f_localsplus;
3808 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003809
Victor Stinnerc7020012016-08-16 23:40:29 +02003810 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003811 if (co->co_flags & CO_VARKEYWORDS) {
3812 kwdict = PyDict_New();
3813 if (kwdict == NULL)
3814 goto fail;
3815 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003816 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003817 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003818 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003819 SETLOCAL(i, kwdict);
3820 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003821 else {
3822 kwdict = NULL;
3823 }
3824
3825 /* Copy positional arguments into local variables */
3826 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003827 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003828 }
3829 else {
3830 n = argcount;
3831 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003832 for (i = 0; i < n; i++) {
3833 x = args[i];
3834 Py_INCREF(x);
3835 SETLOCAL(i, x);
3836 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003837
3838 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003839 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05003840 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003841 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003842 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003843 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003844 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003845 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003846
Serhiy Storchakab7281052016-09-12 00:52:40 +03003847 /* Handle keyword arguments passed as two strided arrays */
3848 kwcount *= kwstep;
3849 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003850 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003851 PyObject *keyword = kwnames[i];
3852 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003853 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003854
Benjamin Petersonb204a422011-06-05 22:04:07 -05003855 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3856 PyErr_Format(PyExc_TypeError,
3857 "%U() keywords must be strings",
3858 co->co_name);
3859 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003860 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003861
Benjamin Petersonb204a422011-06-05 22:04:07 -05003862 /* Speed hack: do raw pointer compares. As names are
3863 normally interned this should almost always hit. */
3864 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3865 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003866 PyObject *name = co_varnames[j];
3867 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003868 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003869 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003870 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003871
Benjamin Petersonb204a422011-06-05 22:04:07 -05003872 /* Slow fallback, just in case */
3873 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003874 PyObject *name = co_varnames[j];
3875 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3876 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003877 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003878 }
3879 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003880 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003881 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003882 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003883
Victor Stinner231d1f32017-01-11 02:12:06 +01003884 assert(j >= total_args);
3885 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003886 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003887 "%U() got an unexpected keyword argument '%S'",
3888 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003889 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003890 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003891
Christian Heimes0bd447f2013-07-20 14:48:10 +02003892 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3893 goto fail;
3894 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003895 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003896
Benjamin Petersonb204a422011-06-05 22:04:07 -05003897 kw_found:
3898 if (GETLOCAL(j) != NULL) {
3899 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003900 "%U() got multiple values for argument '%S'",
3901 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003902 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003903 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003904 Py_INCREF(value);
3905 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003906 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003907
3908 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003909 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003910 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003911 goto fail;
3912 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003913
3914 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003915 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02003916 Py_ssize_t m = co->co_argcount - defcount;
3917 Py_ssize_t missing = 0;
3918 for (i = argcount; i < m; i++) {
3919 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003920 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02003921 }
3922 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003923 if (missing) {
3924 missing_arguments(co, missing, defcount, fastlocals);
3925 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003926 }
3927 if (n > m)
3928 i = n - m;
3929 else
3930 i = 0;
3931 for (; i < defcount; i++) {
3932 if (GETLOCAL(m+i) == NULL) {
3933 PyObject *def = defs[i];
3934 Py_INCREF(def);
3935 SETLOCAL(m+i, def);
3936 }
3937 }
3938 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003939
3940 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003941 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02003942 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003943 for (i = co->co_argcount; i < total_args; i++) {
3944 PyObject *name;
3945 if (GETLOCAL(i) != NULL)
3946 continue;
3947 name = PyTuple_GET_ITEM(co->co_varnames, i);
3948 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003949 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003950 if (def) {
3951 Py_INCREF(def);
3952 SETLOCAL(i, def);
3953 continue;
3954 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003955 else if (PyErr_Occurred()) {
3956 goto fail;
3957 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003958 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003959 missing++;
3960 }
3961 if (missing) {
3962 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003963 goto fail;
3964 }
3965 }
3966
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003967 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003968 vars into frame. */
3969 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003970 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02003971 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05003972 /* Possibly account for the cell variable being an argument. */
3973 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003974 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003975 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003976 /* Clear the local copy. */
3977 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003978 }
3979 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003980 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003981 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003982 if (c == NULL)
3983 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003984 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003985 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003986
3987 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05003988 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3989 PyObject *o = PyTuple_GET_ITEM(closure, i);
3990 Py_INCREF(o);
3991 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003992 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003993
Yury Selivanoveb636452016-09-08 22:01:51 -07003994 /* Handle generator/coroutine/asynchronous generator */
3995 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003996 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04003997 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003998 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04003999
4000 if (is_coro && tstate->in_coroutine_wrapper) {
4001 assert(coro_wrapper != NULL);
4002 PyErr_Format(PyExc_RuntimeError,
4003 "coroutine wrapper %.200R attempted "
4004 "to recursively wrap %.200R",
4005 coro_wrapper,
4006 co);
4007 goto fail;
4008 }
Yury Selivanov75445082015-05-11 22:57:16 -04004009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004010 /* Don't need to keep the reference to f_back, it will be set
4011 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004012 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004014 /* Create a new generator that owns the ready to run frame
4015 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004016 if (is_coro) {
4017 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004018 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4019 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004020 } else {
4021 gen = PyGen_NewWithQualName(f, name, qualname);
4022 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004023 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004024 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004025 }
INADA Naoki9c157762016-12-26 18:52:46 +09004026
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004027 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004028
Yury Selivanov94c22632015-06-04 10:16:51 -04004029 if (is_coro && coro_wrapper != NULL) {
4030 PyObject *wrapped;
4031 tstate->in_coroutine_wrapper = 1;
4032 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4033 tstate->in_coroutine_wrapper = 0;
4034 return wrapped;
4035 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004036
Yury Selivanov75445082015-05-11 22:57:16 -04004037 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004038 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004039
Victor Stinner59a73272016-12-09 18:51:13 +01004040 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004041
Thomas Woutersce272b62007-09-19 21:19:28 +00004042fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004044 /* decref'ing the frame can cause __del__ methods to get invoked,
4045 which can call back into Python. While we're done with the
4046 current Python frame (f), the associated C stack is still in use,
4047 so recursion_depth must be boosted for the duration.
4048 */
4049 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09004050 if (Py_REFCNT(f) > 1) {
4051 Py_DECREF(f);
4052 _PyObject_GC_TRACK(f);
4053 }
4054 else {
4055 ++tstate->recursion_depth;
4056 Py_DECREF(f);
4057 --tstate->recursion_depth;
4058 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004059 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004060}
4061
Victor Stinner40ee3012014-06-16 15:59:28 +02004062PyObject *
4063PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004064 PyObject *const *args, int argcount,
4065 PyObject *const *kws, int kwcount,
4066 PyObject *const *defs, int defcount,
4067 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004068{
4069 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004070 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004071 kws, kws != NULL ? kws + 1 : NULL,
4072 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004073 defs, defcount,
4074 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004075 NULL, NULL);
4076}
Tim Peters5ca576e2001-06-18 22:08:13 +00004077
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004078static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004079special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004080{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004081 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004082 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004083 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004084 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004085 return NULL;
4086 }
4087 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004088}
4089
4090
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004091/* Logic for the raise statement (too complicated for inlining).
4092 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004093static int
Collin Winter828f04a2007-08-31 00:04:24 +00004094do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004095{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004096 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004098 if (exc == NULL) {
4099 /* Reraise */
Victor Stinner50b48572018-11-01 01:51:40 +01004100 PyThreadState *tstate = _PyThreadState_GET();
Mark Shannonae3087c2017-10-22 22:41:51 +01004101 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004102 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004103 type = exc_info->exc_type;
4104 value = exc_info->exc_value;
4105 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004106 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004107 PyErr_SetString(PyExc_RuntimeError,
4108 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004109 return 0;
4110 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004111 Py_XINCREF(type);
4112 Py_XINCREF(value);
4113 Py_XINCREF(tb);
4114 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004115 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004116 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004118 /* We support the following forms of raise:
4119 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004120 raise <instance>
4121 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004123 if (PyExceptionClass_Check(exc)) {
4124 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004125 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004126 if (value == NULL)
4127 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004128 if (!PyExceptionInstance_Check(value)) {
4129 PyErr_Format(PyExc_TypeError,
4130 "calling %R should have returned an instance of "
4131 "BaseException, not %R",
4132 type, Py_TYPE(value));
4133 goto raise_error;
4134 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004135 }
4136 else if (PyExceptionInstance_Check(exc)) {
4137 value = exc;
4138 type = PyExceptionInstance_Class(exc);
4139 Py_INCREF(type);
4140 }
4141 else {
4142 /* Not something you can raise. You get an exception
4143 anyway, just not what you specified :-) */
4144 Py_DECREF(exc);
4145 PyErr_SetString(PyExc_TypeError,
4146 "exceptions must derive from BaseException");
4147 goto raise_error;
4148 }
Collin Winter828f04a2007-08-31 00:04:24 +00004149
Serhiy Storchakac0191582016-09-27 11:37:10 +03004150 assert(type != NULL);
4151 assert(value != NULL);
4152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004153 if (cause) {
4154 PyObject *fixed_cause;
4155 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004156 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004157 if (fixed_cause == NULL)
4158 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004159 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004160 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004161 else if (PyExceptionInstance_Check(cause)) {
4162 fixed_cause = cause;
4163 }
4164 else if (cause == Py_None) {
4165 Py_DECREF(cause);
4166 fixed_cause = NULL;
4167 }
4168 else {
4169 PyErr_SetString(PyExc_TypeError,
4170 "exception causes must derive from "
4171 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004172 goto raise_error;
4173 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004174 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004175 }
Collin Winter828f04a2007-08-31 00:04:24 +00004176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004177 PyErr_SetObject(type, value);
4178 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004179 Py_DECREF(value);
4180 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004181 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004182
4183raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004184 Py_XDECREF(value);
4185 Py_XDECREF(type);
4186 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004187 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004188}
4189
Tim Petersd6d010b2001-06-21 02:49:55 +00004190/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004191 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004192
Guido van Rossum0368b722007-05-11 16:50:42 +00004193 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4194 with a variable target.
4195*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004196
Barry Warsawe42b18f1997-08-25 22:13:04 +00004197static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004198unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004199{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004200 int i = 0, j = 0;
4201 Py_ssize_t ll = 0;
4202 PyObject *it; /* iter(v) */
4203 PyObject *w;
4204 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004206 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004208 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004209 if (it == NULL) {
4210 if (PyErr_ExceptionMatches(PyExc_TypeError) &&
4211 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4212 {
4213 PyErr_Format(PyExc_TypeError,
4214 "cannot unpack non-iterable %.200s object",
4215 v->ob_type->tp_name);
4216 }
4217 return 0;
4218 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004220 for (; i < argcnt; i++) {
4221 w = PyIter_Next(it);
4222 if (w == NULL) {
4223 /* Iterator done, via error or exhaustion. */
4224 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004225 if (argcntafter == -1) {
4226 PyErr_Format(PyExc_ValueError,
4227 "not enough values to unpack (expected %d, got %d)",
4228 argcnt, i);
4229 }
4230 else {
4231 PyErr_Format(PyExc_ValueError,
4232 "not enough values to unpack "
4233 "(expected at least %d, got %d)",
4234 argcnt + argcntafter, i);
4235 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004236 }
4237 goto Error;
4238 }
4239 *--sp = w;
4240 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004242 if (argcntafter == -1) {
4243 /* We better have exhausted the iterator now. */
4244 w = PyIter_Next(it);
4245 if (w == NULL) {
4246 if (PyErr_Occurred())
4247 goto Error;
4248 Py_DECREF(it);
4249 return 1;
4250 }
4251 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004252 PyErr_Format(PyExc_ValueError,
4253 "too many values to unpack (expected %d)",
4254 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004255 goto Error;
4256 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004258 l = PySequence_List(it);
4259 if (l == NULL)
4260 goto Error;
4261 *--sp = l;
4262 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004264 ll = PyList_GET_SIZE(l);
4265 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004266 PyErr_Format(PyExc_ValueError,
4267 "not enough values to unpack (expected at least %d, got %zd)",
4268 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004269 goto Error;
4270 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004272 /* Pop the "after-variable" args off the list. */
4273 for (j = argcntafter; j > 0; j--, i++) {
4274 *--sp = PyList_GET_ITEM(l, ll - j);
4275 }
4276 /* Resize the list. */
4277 Py_SIZE(l) = ll - argcntafter;
4278 Py_DECREF(it);
4279 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004280
Tim Petersd6d010b2001-06-21 02:49:55 +00004281Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004282 for (; i > 0; i--, sp++)
4283 Py_DECREF(*sp);
4284 Py_XDECREF(it);
4285 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004286}
4287
4288
Guido van Rossum96a42c81992-01-12 02:29:51 +00004289#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004290static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004291prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004292{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004293 printf("%s ", str);
4294 if (PyObject_Print(v, stdout, 0) != 0)
4295 PyErr_Clear(); /* Don't know what else to do */
4296 printf("\n");
4297 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004298}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004299#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004300
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004301static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004302call_exc_trace(Py_tracefunc func, PyObject *self,
4303 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004304{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004305 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004306 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004307 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004308 if (value == NULL) {
4309 value = Py_None;
4310 Py_INCREF(value);
4311 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004312 PyErr_NormalizeException(&type, &value, &orig_traceback);
4313 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004314 arg = PyTuple_Pack(3, type, value, traceback);
4315 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004316 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004317 return;
4318 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004319 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004320 Py_DECREF(arg);
4321 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004322 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004323 else {
4324 Py_XDECREF(type);
4325 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004326 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004327 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004328}
4329
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004330static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004331call_trace_protected(Py_tracefunc func, PyObject *obj,
4332 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004333 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004334{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004335 PyObject *type, *value, *traceback;
4336 int err;
4337 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004338 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004339 if (err == 0)
4340 {
4341 PyErr_Restore(type, value, traceback);
4342 return 0;
4343 }
4344 else {
4345 Py_XDECREF(type);
4346 Py_XDECREF(value);
4347 Py_XDECREF(traceback);
4348 return -1;
4349 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004350}
4351
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004352static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004353call_trace(Py_tracefunc func, PyObject *obj,
4354 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004355 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004356{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004357 int result;
4358 if (tstate->tracing)
4359 return 0;
4360 tstate->tracing++;
4361 tstate->use_tracing = 0;
4362 result = func(obj, frame, what, arg);
4363 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4364 || (tstate->c_profilefunc != NULL));
4365 tstate->tracing--;
4366 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004367}
4368
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004369PyObject *
4370_PyEval_CallTracing(PyObject *func, PyObject *args)
4371{
Victor Stinner50b48572018-11-01 01:51:40 +01004372 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004373 int save_tracing = tstate->tracing;
4374 int save_use_tracing = tstate->use_tracing;
4375 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004377 tstate->tracing = 0;
4378 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4379 || (tstate->c_profilefunc != NULL));
4380 result = PyObject_Call(func, args, NULL);
4381 tstate->tracing = save_tracing;
4382 tstate->use_tracing = save_use_tracing;
4383 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004384}
4385
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004386/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004387static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004388maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004389 PyThreadState *tstate, PyFrameObject *frame,
4390 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004391{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004392 int result = 0;
4393 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004395 /* If the last instruction executed isn't in the current
4396 instruction window, reset the window.
4397 */
4398 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4399 PyAddrPair bounds;
4400 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4401 &bounds);
4402 *instr_lb = bounds.ap_lower;
4403 *instr_ub = bounds.ap_upper;
4404 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004405 /* If the last instruction falls at the start of a line or if it
4406 represents a jump backwards, update the frame's line number and
4407 then call the trace function if we're tracing source lines.
4408 */
4409 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004410 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004411 if (frame->f_trace_lines) {
4412 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4413 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414 }
George King20faa682017-10-18 17:44:22 -07004415 /* Always emit an opcode event if we're tracing all opcodes. */
4416 if (frame->f_trace_opcodes) {
4417 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4418 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004419 *instr_prev = frame->f_lasti;
4420 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004421}
4422
Fred Drake5755ce62001-06-27 19:19:46 +00004423void
4424PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004425{
Victor Stinner50b48572018-11-01 01:51:40 +01004426 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004427 PyObject *temp = tstate->c_profileobj;
4428 Py_XINCREF(arg);
4429 tstate->c_profilefunc = NULL;
4430 tstate->c_profileobj = NULL;
4431 /* Must make sure that tracing is not ignored if 'temp' is freed */
4432 tstate->use_tracing = tstate->c_tracefunc != NULL;
4433 Py_XDECREF(temp);
4434 tstate->c_profilefunc = func;
4435 tstate->c_profileobj = arg;
4436 /* Flag that tracing or profiling is turned on */
4437 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004438}
4439
4440void
4441PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4442{
Victor Stinner50b48572018-11-01 01:51:40 +01004443 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004444 PyObject *temp = tstate->c_traceobj;
4445 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4446 Py_XINCREF(arg);
4447 tstate->c_tracefunc = NULL;
4448 tstate->c_traceobj = NULL;
4449 /* Must make sure that profiling is not ignored if 'temp' is freed */
4450 tstate->use_tracing = tstate->c_profilefunc != NULL;
4451 Py_XDECREF(temp);
4452 tstate->c_tracefunc = func;
4453 tstate->c_traceobj = arg;
4454 /* Flag that tracing or profiling is turned on */
4455 tstate->use_tracing = ((func != NULL)
4456 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004457}
4458
Yury Selivanov75445082015-05-11 22:57:16 -04004459void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004460_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4461{
4462 assert(new_depth >= 0);
Victor Stinner50b48572018-11-01 01:51:40 +01004463 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004464 tstate->coroutine_origin_tracking_depth = new_depth;
4465}
4466
4467int
4468_PyEval_GetCoroutineOriginTrackingDepth(void)
4469{
Victor Stinner50b48572018-11-01 01:51:40 +01004470 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004471 return tstate->coroutine_origin_tracking_depth;
4472}
4473
4474void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004475_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004476{
Victor Stinner50b48572018-11-01 01:51:40 +01004477 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004478
Yury Selivanov75445082015-05-11 22:57:16 -04004479 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004480 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004481}
4482
4483PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004484_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004485{
Victor Stinner50b48572018-11-01 01:51:40 +01004486 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004487 return tstate->coroutine_wrapper;
4488}
4489
Yury Selivanoveb636452016-09-08 22:01:51 -07004490void
4491_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4492{
Victor Stinner50b48572018-11-01 01:51:40 +01004493 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004494
4495 Py_XINCREF(firstiter);
4496 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4497}
4498
4499PyObject *
4500_PyEval_GetAsyncGenFirstiter(void)
4501{
Victor Stinner50b48572018-11-01 01:51:40 +01004502 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004503 return tstate->async_gen_firstiter;
4504}
4505
4506void
4507_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4508{
Victor Stinner50b48572018-11-01 01:51:40 +01004509 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004510
4511 Py_XINCREF(finalizer);
4512 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4513}
4514
4515PyObject *
4516_PyEval_GetAsyncGenFinalizer(void)
4517{
Victor Stinner50b48572018-11-01 01:51:40 +01004518 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004519 return tstate->async_gen_finalizer;
4520}
4521
Guido van Rossumb209a111997-04-29 18:18:01 +00004522PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004523PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004524{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004525 PyFrameObject *current_frame = PyEval_GetFrame();
4526 if (current_frame == NULL)
Victor Stinnercaba55b2018-08-03 15:33:52 +02004527 return _PyInterpreterState_GET_UNSAFE()->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004528 else
4529 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004530}
4531
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004532/* Convenience function to get a builtin from its name */
4533PyObject *
4534_PyEval_GetBuiltinId(_Py_Identifier *name)
4535{
4536 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4537 if (attr) {
4538 Py_INCREF(attr);
4539 }
4540 else if (!PyErr_Occurred()) {
4541 PyErr_SetObject(PyExc_AttributeError, _PyUnicode_FromId(name));
4542 }
4543 return attr;
4544}
4545
Guido van Rossumb209a111997-04-29 18:18:01 +00004546PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004547PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004548{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004549 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004550 if (current_frame == NULL) {
4551 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004552 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004553 }
4554
4555 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4556 return NULL;
4557
4558 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004559 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004560}
4561
Guido van Rossumb209a111997-04-29 18:18:01 +00004562PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004563PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004564{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004565 PyFrameObject *current_frame = PyEval_GetFrame();
4566 if (current_frame == NULL)
4567 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004568
4569 assert(current_frame->f_globals != NULL);
4570 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004571}
4572
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004573PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004574PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004575{
Victor Stinner50b48572018-11-01 01:51:40 +01004576 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004577 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004578}
4579
Guido van Rossum6135a871995-01-09 17:53:26 +00004580int
Tim Peters5ba58662001-07-16 02:29:45 +00004581PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004583 PyFrameObject *current_frame = PyEval_GetFrame();
4584 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004586 if (current_frame != NULL) {
4587 const int codeflags = current_frame->f_code->co_flags;
4588 const int compilerflags = codeflags & PyCF_MASK;
4589 if (compilerflags) {
4590 result = 1;
4591 cf->cf_flags |= compilerflags;
4592 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004593#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004594 if (codeflags & CO_GENERATOR_ALLOWED) {
4595 result = 1;
4596 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4597 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004598#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004599 }
4600 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004601}
4602
Guido van Rossum3f5da241990-12-20 15:06:42 +00004603
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004604const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004605PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004606{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004607 if (PyMethod_Check(func))
4608 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4609 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004610 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004611 else if (PyCFunction_Check(func))
4612 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4613 else
4614 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004615}
4616
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004617const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004618PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004619{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004620 if (PyMethod_Check(func))
4621 return "()";
4622 else if (PyFunction_Check(func))
4623 return "()";
4624 else if (PyCFunction_Check(func))
4625 return "()";
4626 else
4627 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004628}
4629
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004630#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004631if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004632 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4633 tstate, tstate->frame, \
4634 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004635 x = NULL; \
4636 } \
4637 else { \
4638 x = call; \
4639 if (tstate->c_profilefunc != NULL) { \
4640 if (x == NULL) { \
4641 call_trace_protected(tstate->c_profilefunc, \
4642 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004643 tstate, tstate->frame, \
4644 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004645 /* XXX should pass (type, value, tb) */ \
4646 } else { \
4647 if (call_trace(tstate->c_profilefunc, \
4648 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004649 tstate, tstate->frame, \
4650 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004651 Py_DECREF(x); \
4652 x = NULL; \
4653 } \
4654 } \
4655 } \
4656 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004657} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004658 x = call; \
4659 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004660
Victor Stinner415c5102017-01-11 00:54:57 +01004661/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4662 to reduce the stack consumption. */
4663Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004664call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004665{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004666 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004667 PyObject *func = *pfunc;
4668 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004669 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4670 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004671 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004673 /* Always dispatch PyCFunction first, because these are
4674 presumed to be the most frequent callable object.
4675 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004676 if (PyCFunction_Check(func)) {
Victor Stinner50b48572018-11-01 01:51:40 +01004677 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004678 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004679 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004680 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
Victor Stinner50b48572018-11-01 01:51:40 +01004681 PyThreadState *tstate = _PyThreadState_GET();
jdemeyer56868f92018-07-21 10:30:59 +02004682 if (nargs > 0 && tstate->use_tracing) {
4683 /* We need to create a temporary bound method as argument
4684 for profiling.
4685
4686 If nargs == 0, then this cannot work because we have no
4687 "self". In any case, the call itself would raise
4688 TypeError (foo needs an argument), so we just skip
4689 profiling. */
4690 PyObject *self = stack[0];
4691 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
jdemeyer147d9552018-07-23 18:41:20 +02004692 if (func != NULL) {
4693 C_TRACE(x, _PyCFunction_FastCallKeywords(func,
4694 stack+1, nargs-1,
4695 kwnames));
4696 Py_DECREF(func);
INADA Naoki93fac8d2017-03-07 14:24:37 +09004697 }
jdemeyer147d9552018-07-23 18:41:20 +02004698 else {
4699 x = NULL;
4700 }
INADA Naoki93fac8d2017-03-07 14:24:37 +09004701 }
4702 else {
4703 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4704 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004705 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004706 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004707 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004708 /* Optimize access to bound methods. Reuse the Python stack
4709 to pass 'self' as the first argument, replace 'func'
4710 with 'self'. It avoids the creation of a new temporary tuple
4711 for arguments (to replace func with self) when the method uses
4712 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004713 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004714 Py_INCREF(self);
4715 func = PyMethod_GET_FUNCTION(func);
4716 Py_INCREF(func);
4717 Py_SETREF(*pfunc, self);
4718 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004719 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004720 }
4721 else {
4722 Py_INCREF(func);
4723 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004724
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004725 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004726 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004727 }
4728 else {
4729 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4730 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004731 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004732 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004733
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004734 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4735
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004736 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004737 while ((*pp_stack) > pfunc) {
4738 w = EXT_POP(*pp_stack);
4739 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004740 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004742 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004743}
4744
Jeremy Hylton52820442001-01-03 23:52:36 +00004745static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004746do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004747{
jdemeyere89de732018-09-19 12:06:20 +02004748 PyObject *result;
4749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004750 if (PyCFunction_Check(func)) {
Victor Stinner50b48572018-11-01 01:51:40 +01004751 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004752 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004753 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004754 }
jdemeyere89de732018-09-19 12:06:20 +02004755 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
Victor Stinner50b48572018-11-01 01:51:40 +01004756 PyThreadState *tstate = _PyThreadState_GET();
jdemeyere89de732018-09-19 12:06:20 +02004757 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4758 if (nargs > 0 && tstate->use_tracing) {
4759 /* We need to create a temporary bound method as argument
4760 for profiling.
4761
4762 If nargs == 0, then this cannot work because we have no
4763 "self". In any case, the call itself would raise
4764 TypeError (foo needs an argument), so we just skip
4765 profiling. */
4766 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4767 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4768 if (func == NULL) {
4769 return NULL;
4770 }
4771
4772 C_TRACE(result, _PyCFunction_FastCallDict(func,
Victor Stinnerd17a6932018-11-09 16:56:48 +01004773 &_PyTuple_ITEMS(callargs)[1],
jdemeyere89de732018-09-19 12:06:20 +02004774 nargs - 1,
4775 kwdict));
4776 Py_DECREF(func);
4777 return result;
4778 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004779 }
jdemeyere89de732018-09-19 12:06:20 +02004780 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004781}
4782
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004783/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004784 nb_index slot defined, and store in *pi.
4785 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004786 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004787 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004788*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004789int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004790_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004791{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004792 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004793 Py_ssize_t x;
4794 if (PyIndex_Check(v)) {
4795 x = PyNumber_AsSsize_t(v, NULL);
4796 if (x == -1 && PyErr_Occurred())
4797 return 0;
4798 }
4799 else {
4800 PyErr_SetString(PyExc_TypeError,
4801 "slice indices must be integers or "
4802 "None or have an __index__ method");
4803 return 0;
4804 }
4805 *pi = x;
4806 }
4807 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004808}
4809
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004810int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004811_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004812{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004813 Py_ssize_t x;
4814 if (PyIndex_Check(v)) {
4815 x = PyNumber_AsSsize_t(v, NULL);
4816 if (x == -1 && PyErr_Occurred())
4817 return 0;
4818 }
4819 else {
4820 PyErr_SetString(PyExc_TypeError,
4821 "slice indices must be integers or "
4822 "have an __index__ method");
4823 return 0;
4824 }
4825 *pi = x;
4826 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004827}
4828
4829
Guido van Rossum486364b2007-06-30 05:01:58 +00004830#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004831 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004832
Guido van Rossumb209a111997-04-29 18:18:01 +00004833static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004834cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004835{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004836 int res = 0;
4837 switch (op) {
4838 case PyCmp_IS:
4839 res = (v == w);
4840 break;
4841 case PyCmp_IS_NOT:
4842 res = (v != w);
4843 break;
4844 case PyCmp_IN:
4845 res = PySequence_Contains(w, v);
4846 if (res < 0)
4847 return NULL;
4848 break;
4849 case PyCmp_NOT_IN:
4850 res = PySequence_Contains(w, v);
4851 if (res < 0)
4852 return NULL;
4853 res = !res;
4854 break;
4855 case PyCmp_EXC_MATCH:
4856 if (PyTuple_Check(w)) {
4857 Py_ssize_t i, length;
4858 length = PyTuple_Size(w);
4859 for (i = 0; i < length; i += 1) {
4860 PyObject *exc = PyTuple_GET_ITEM(w, i);
4861 if (!PyExceptionClass_Check(exc)) {
4862 PyErr_SetString(PyExc_TypeError,
4863 CANNOT_CATCH_MSG);
4864 return NULL;
4865 }
4866 }
4867 }
4868 else {
4869 if (!PyExceptionClass_Check(w)) {
4870 PyErr_SetString(PyExc_TypeError,
4871 CANNOT_CATCH_MSG);
4872 return NULL;
4873 }
4874 }
4875 res = PyErr_GivenExceptionMatches(v, w);
4876 break;
4877 default:
4878 return PyObject_RichCompare(v, w, op);
4879 }
4880 v = res ? Py_True : Py_False;
4881 Py_INCREF(v);
4882 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004883}
4884
Thomas Wouters52152252000-08-17 22:55:00 +00004885static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004886import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4887{
4888 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004889 PyObject *import_func, *res;
4890 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004891
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004892 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004893 if (import_func == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004894 if (!PyErr_Occurred()) {
4895 PyErr_SetString(PyExc_ImportError, "__import__ not found");
4896 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004897 return NULL;
4898 }
4899
4900 /* Fast path for not overloaded __import__. */
Victor Stinnercaba55b2018-08-03 15:33:52 +02004901 if (import_func == _PyInterpreterState_GET_UNSAFE()->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004902 int ilevel = _PyLong_AsInt(level);
4903 if (ilevel == -1 && PyErr_Occurred()) {
4904 return NULL;
4905 }
4906 res = PyImport_ImportModuleLevelObject(
4907 name,
4908 f->f_globals,
4909 f->f_locals == NULL ? Py_None : f->f_locals,
4910 fromlist,
4911 ilevel);
4912 return res;
4913 }
4914
4915 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004916
4917 stack[0] = name;
4918 stack[1] = f->f_globals;
4919 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
4920 stack[3] = fromlist;
4921 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02004922 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004923 Py_DECREF(import_func);
4924 return res;
4925}
4926
4927static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004928import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004929{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004930 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02004931 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08004932 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004933
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004934 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02004935 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004936 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004937 /* Issue #17636: in case this failed because of a circular relative
4938 import, try to fallback on reading the module directly from
4939 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02004940 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07004941 if (pkgname == NULL) {
4942 goto error;
4943 }
Oren Milman6db70332017-09-19 14:23:01 +03004944 if (!PyUnicode_Check(pkgname)) {
4945 Py_CLEAR(pkgname);
4946 goto error;
4947 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004948 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07004949 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08004950 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004951 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07004952 }
Eric Snow3f9eee62017-09-15 16:35:20 -06004953 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004954 Py_DECREF(fullmodname);
Stefan Krah027b09c2019-03-25 21:50:58 +01004955 if (x == NULL && !PyErr_Occurred()) {
Brett Cannon3008bc02015-08-11 18:01:31 -07004956 goto error;
4957 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004958 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004959 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07004960 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004961 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004962 if (pkgname == NULL) {
4963 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
4964 if (pkgname_or_unknown == NULL) {
4965 Py_XDECREF(pkgpath);
4966 return NULL;
4967 }
4968 } else {
4969 pkgname_or_unknown = pkgname;
4970 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004971
4972 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
4973 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08004974 errmsg = PyUnicode_FromFormat(
4975 "cannot import name %R from %R (unknown location)",
4976 name, pkgname_or_unknown
4977 );
Stefan Krah027b09c2019-03-25 21:50:58 +01004978 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08004979 PyErr_SetImportError(errmsg, pkgname, NULL);
4980 }
4981 else {
4982 errmsg = PyUnicode_FromFormat(
4983 "cannot import name %R from %R (%S)",
4984 name, pkgname_or_unknown, pkgpath
4985 );
Stefan Krah027b09c2019-03-25 21:50:58 +01004986 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08004987 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004988 }
4989
Xiang Zhang4830f582017-03-21 11:13:42 +08004990 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004991 Py_XDECREF(pkgname_or_unknown);
4992 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07004993 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00004994}
Guido van Rossumac7be682001-01-17 15:42:30 +00004995
Thomas Wouters52152252000-08-17 22:55:00 +00004996static int
4997import_all_from(PyObject *locals, PyObject *v)
4998{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004999 _Py_IDENTIFIER(__all__);
5000 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005001 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005002 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005003 int skip_leading_underscores = 0;
5004 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005005
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005006 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5007 return -1; /* Unexpected error */
5008 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005009 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005010 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5011 return -1;
5012 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005013 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005014 PyErr_SetString(PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005015 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005016 return -1;
5017 }
5018 all = PyMapping_Keys(dict);
5019 Py_DECREF(dict);
5020 if (all == NULL)
5021 return -1;
5022 skip_leading_underscores = 1;
5023 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005025 for (pos = 0, err = 0; ; pos++) {
5026 name = PySequence_GetItem(all, pos);
5027 if (name == NULL) {
5028 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5029 err = -1;
5030 else
5031 PyErr_Clear();
5032 break;
5033 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005034 if (!PyUnicode_Check(name)) {
5035 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5036 if (modname == NULL) {
5037 Py_DECREF(name);
5038 err = -1;
5039 break;
5040 }
5041 if (!PyUnicode_Check(modname)) {
5042 PyErr_Format(PyExc_TypeError,
5043 "module __name__ must be a string, not %.100s",
5044 Py_TYPE(modname)->tp_name);
5045 }
5046 else {
5047 PyErr_Format(PyExc_TypeError,
5048 "%s in %U.%s must be str, not %.100s",
5049 skip_leading_underscores ? "Key" : "Item",
5050 modname,
5051 skip_leading_underscores ? "__dict__" : "__all__",
5052 Py_TYPE(name)->tp_name);
5053 }
5054 Py_DECREF(modname);
5055 Py_DECREF(name);
5056 err = -1;
5057 break;
5058 }
5059 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005060 if (PyUnicode_READY(name) == -1) {
5061 Py_DECREF(name);
5062 err = -1;
5063 break;
5064 }
5065 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5066 Py_DECREF(name);
5067 continue;
5068 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005069 }
5070 value = PyObject_GetAttr(v, name);
5071 if (value == NULL)
5072 err = -1;
5073 else if (PyDict_CheckExact(locals))
5074 err = PyDict_SetItem(locals, name, value);
5075 else
5076 err = PyObject_SetItem(locals, name, value);
5077 Py_DECREF(name);
5078 Py_XDECREF(value);
5079 if (err != 0)
5080 break;
5081 }
5082 Py_DECREF(all);
5083 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005084}
5085
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005086static int
5087check_args_iterable(PyObject *func, PyObject *args)
5088{
5089 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
5090 PyErr_Format(PyExc_TypeError,
5091 "%.200s%.200s argument after * "
5092 "must be an iterable, not %.200s",
5093 PyEval_GetFuncName(func),
5094 PyEval_GetFuncDesc(func),
5095 args->ob_type->tp_name);
5096 return -1;
5097 }
5098 return 0;
5099}
5100
5101static void
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005102format_kwargs_error(PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005103{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005104 /* _PyDict_MergeEx raises attribute
5105 * error (percolated from an attempt
5106 * to get 'keys' attribute) instead of
5107 * a type error if its second argument
5108 * is not a mapping.
5109 */
5110 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
5111 PyErr_Format(PyExc_TypeError,
5112 "%.200s%.200s argument after ** "
5113 "must be a mapping, not %.200s",
5114 PyEval_GetFuncName(func),
5115 PyEval_GetFuncDesc(func),
5116 kwargs->ob_type->tp_name);
5117 }
5118 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
5119 PyObject *exc, *val, *tb;
5120 PyErr_Fetch(&exc, &val, &tb);
5121 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
5122 PyObject *key = PyTuple_GET_ITEM(val, 0);
5123 if (!PyUnicode_Check(key)) {
5124 PyErr_Format(PyExc_TypeError,
5125 "%.200s%.200s keywords must be strings",
5126 PyEval_GetFuncName(func),
5127 PyEval_GetFuncDesc(func));
5128 } else {
5129 PyErr_Format(PyExc_TypeError,
5130 "%.200s%.200s got multiple "
5131 "values for keyword argument '%U'",
5132 PyEval_GetFuncName(func),
5133 PyEval_GetFuncDesc(func),
5134 key);
5135 }
5136 Py_XDECREF(exc);
5137 Py_XDECREF(val);
5138 Py_XDECREF(tb);
5139 }
5140 else {
5141 PyErr_Restore(exc, val, tb);
5142 }
5143 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005144}
5145
Guido van Rossumac7be682001-01-17 15:42:30 +00005146static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005147format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005148{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005149 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005151 if (!obj)
5152 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005153
Serhiy Storchaka06515832016-11-20 09:13:07 +02005154 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005155 if (!obj_str)
5156 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005158 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005159}
Guido van Rossum950361c1997-01-24 13:49:28 +00005160
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005161static void
5162format_exc_unbound(PyCodeObject *co, int oparg)
5163{
5164 PyObject *name;
5165 /* Don't stomp existing exception */
5166 if (PyErr_Occurred())
5167 return;
5168 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5169 name = PyTuple_GET_ITEM(co->co_cellvars,
5170 oparg);
5171 format_exc_check_arg(
5172 PyExc_UnboundLocalError,
5173 UNBOUNDLOCAL_ERROR_MSG,
5174 name);
5175 } else {
5176 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5177 PyTuple_GET_SIZE(co->co_cellvars));
5178 format_exc_check_arg(PyExc_NameError,
5179 UNBOUNDFREE_ERROR_MSG, name);
5180 }
5181}
5182
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005183static void
5184format_awaitable_error(PyTypeObject *type, int prevopcode)
5185{
5186 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5187 if (prevopcode == BEFORE_ASYNC_WITH) {
5188 PyErr_Format(PyExc_TypeError,
5189 "'async with' received an object from __aenter__ "
5190 "that does not implement __await__: %.100s",
5191 type->tp_name);
5192 }
5193 else if (prevopcode == WITH_CLEANUP_START) {
5194 PyErr_Format(PyExc_TypeError,
5195 "'async with' received an object from __aexit__ "
5196 "that does not implement __await__: %.100s",
5197 type->tp_name);
5198 }
5199 }
5200}
5201
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005202static PyObject *
5203unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005204 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005205{
5206 PyObject *res;
5207 if (Py_REFCNT(v) == 2) {
5208 /* In the common case, there are 2 references to the value
5209 * stored in 'variable' when the += is performed: one on the
5210 * value stack (in 'v') and one still stored in the
5211 * 'variable'. We try to delete the variable now to reduce
5212 * the refcnt to 1.
5213 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005214 int opcode, oparg;
5215 NEXTOPARG();
5216 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005217 case STORE_FAST:
5218 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005219 PyObject **fastlocals = f->f_localsplus;
5220 if (GETLOCAL(oparg) == v)
5221 SETLOCAL(oparg, NULL);
5222 break;
5223 }
5224 case STORE_DEREF:
5225 {
5226 PyObject **freevars = (f->f_localsplus +
5227 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005228 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005229 if (PyCell_GET(c) == v) {
5230 PyCell_SET(c, NULL);
5231 Py_DECREF(v);
5232 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005233 break;
5234 }
5235 case STORE_NAME:
5236 {
5237 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005238 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005239 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005240 if (locals && PyDict_CheckExact(locals)) {
5241 PyObject *w = PyDict_GetItemWithError(locals, name);
5242 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
5243 (w == NULL && PyErr_Occurred()))
5244 {
5245 Py_DECREF(v);
5246 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005247 }
5248 }
5249 break;
5250 }
5251 }
5252 }
5253 res = v;
5254 PyUnicode_Append(&res, w);
5255 return res;
5256}
5257
Guido van Rossum950361c1997-01-24 13:49:28 +00005258#ifdef DYNAMIC_EXECUTION_PROFILE
5259
Skip Montanarof118cb12001-10-15 20:51:38 +00005260static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005261getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005262{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005263 int i;
5264 PyObject *l = PyList_New(256);
5265 if (l == NULL) return NULL;
5266 for (i = 0; i < 256; i++) {
5267 PyObject *x = PyLong_FromLong(a[i]);
5268 if (x == NULL) {
5269 Py_DECREF(l);
5270 return NULL;
5271 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005272 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005273 }
5274 for (i = 0; i < 256; i++)
5275 a[i] = 0;
5276 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005277}
5278
5279PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005280_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005281{
5282#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005283 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005284#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005285 int i;
5286 PyObject *l = PyList_New(257);
5287 if (l == NULL) return NULL;
5288 for (i = 0; i < 257; i++) {
5289 PyObject *x = getarray(dxpairs[i]);
5290 if (x == NULL) {
5291 Py_DECREF(l);
5292 return NULL;
5293 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005294 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005295 }
5296 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005297#endif
5298}
5299
5300#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005301
5302Py_ssize_t
5303_PyEval_RequestCodeExtraIndex(freefunc free)
5304{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005305 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005306 Py_ssize_t new_index;
5307
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005308 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005309 return -1;
5310 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005311 new_index = interp->co_extra_user_count++;
5312 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005313 return new_index;
5314}
Łukasz Langaa785c872016-09-09 17:37:37 -07005315
5316static void
5317dtrace_function_entry(PyFrameObject *f)
5318{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005319 const char *filename;
5320 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005321 int lineno;
5322
5323 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5324 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5325 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5326
5327 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5328}
5329
5330static void
5331dtrace_function_return(PyFrameObject *f)
5332{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005333 const char *filename;
5334 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005335 int lineno;
5336
5337 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5338 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5339 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5340
5341 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5342}
5343
5344/* DTrace equivalent of maybe_call_line_trace. */
5345static void
5346maybe_dtrace_line(PyFrameObject *frame,
5347 int *instr_lb, int *instr_ub, int *instr_prev)
5348{
5349 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005350 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005351
5352 /* If the last instruction executed isn't in the current
5353 instruction window, reset the window.
5354 */
5355 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5356 PyAddrPair bounds;
5357 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5358 &bounds);
5359 *instr_lb = bounds.ap_lower;
5360 *instr_ub = bounds.ap_upper;
5361 }
5362 /* If the last instruction falls at the start of a line or if
5363 it represents a jump backwards, update the frame's line
5364 number and call the trace function. */
5365 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5366 frame->f_lineno = line;
5367 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5368 if (!co_filename)
5369 co_filename = "?";
5370 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5371 if (!co_name)
5372 co_name = "?";
5373 PyDTrace_LINE(co_filename, co_name, line);
5374 }
5375 *instr_prev = frame->f_lasti;
5376}