blob: 28e923219d389c79760136822e9b9379e7d67184 [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
Yury Selivanovf2392132016-12-13 19:03:51 -050033/* Private API for the LOAD_METHOD opcode. */
34extern int _PyObject_GetMethod(PyObject *, PyObject *, PyObject **);
35
Jeremy Hylton52820442001-01-03 23:52:36 +000036typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000037
Guido van Rossum374a9221991-04-04 10:40:29 +000038/* Forward declarations */
Eric Snow2ebc5ce2017-09-07 23:51:28 -060039Py_LOCAL_INLINE(PyObject *) call_function(PyObject ***, Py_ssize_t,
40 PyObject *);
Victor Stinnerf9b760f2016-09-09 10:17:08 -070041static PyObject * do_call_core(PyObject *, PyObject *, PyObject *);
Jeremy Hylton52820442001-01-03 23:52:36 +000042
Guido van Rossum0a066c01992-03-27 17:29:15 +000043#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000044static int lltrace;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020045static int prtrace(PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000046#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010047static int call_trace(Py_tracefunc, PyObject *,
48 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000050static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010051 PyThreadState *, PyFrameObject *,
52 int, PyObject *);
53static void call_exc_trace(Py_tracefunc, PyObject *,
54 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000055static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060056 PyThreadState *, PyFrameObject *,
57 int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070058static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
59static void dtrace_function_entry(PyFrameObject *);
60static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000061
Thomas Wouters477c8d52006-05-27 19:21:47 +000062static PyObject * cmp_outcome(int, PyObject *, PyObject *);
Eric Snow2ebc5ce2017-09-07 23:51:28 -060063static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *,
64 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +000065static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +000066static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +000067static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +000068static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +020069static PyObject * unicode_concatenate(PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030070 PyFrameObject *, const _Py_CODEUNIT *);
Benjamin Petersonce798522012-01-22 11:24:29 -050071static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Serhiy Storchaka25e4f772017-08-03 11:37:15 +030072static int check_args_iterable(PyObject *func, PyObject *vararg);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +020073static void format_kwargs_error(PyObject *func, PyObject *kwargs);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +030074static void format_awaitable_error(PyTypeObject *, int);
Guido van Rossum374a9221991-04-04 10:40:29 +000075
Paul Prescode68140d2000-08-30 20:25:01 +000076#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000078#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000080#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 "free variable '%.200s' referenced before assignment" \
82 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000083
Guido van Rossum950361c1997-01-24 13:49:28 +000084/* Dynamic execution profile */
85#ifdef DYNAMIC_EXECUTION_PROFILE
86#ifdef DXPAIRS
87static long dxpairs[257][256];
88#define dxp dxpairs[256]
89#else
90static long dxp[256];
91#endif
92#endif
93
Eric Snow2ebc5ce2017-09-07 23:51:28 -060094#define GIL_REQUEST _Py_atomic_load_relaxed(&_PyRuntime.ceval.gil_drop_request)
Benjamin Petersond2be5b42010-09-10 22:47:02 +000095
Jeffrey Yasskin39370832010-05-03 19:29:34 +000096/* This can set eval_breaker to 0 even though gil_drop_request became
97 1. We believe this is all right because the eval loop will release
98 the GIL eventually anyway. */
Victor Stinner4d61e6e2019-03-04 14:21:28 +010099#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 _Py_atomic_store_relaxed( \
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100101 &_PyRuntime.ceval.eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000102 GIL_REQUEST | \
Eric Snowfdf282d2019-01-11 14:26:55 -0700103 _Py_atomic_load_relaxed(&_PyRuntime.ceval.signals_pending) | \
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100104 _Py_atomic_load_relaxed(&_PyRuntime.ceval.pending.calls_to_do) | \
105 _PyRuntime.ceval.pending.async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000106
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100107#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600109 _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 1); \
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100110 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000112
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100113#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600115 _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 0); \
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100116 COMPUTE_EVAL_BREAKER(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000118
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000119/* Pending calls are only modified under pending_lock */
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100120#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 do { \
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100122 _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 1); \
123 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000125
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100126#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 do { \
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100128 _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 0); \
129 COMPUTE_EVAL_BREAKER(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000131
Eric Snowfdf282d2019-01-11 14:26:55 -0700132#define SIGNAL_PENDING_SIGNALS() \
133 do { \
134 _Py_atomic_store_relaxed(&_PyRuntime.ceval.signals_pending, 1); \
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100135 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700136 } while (0)
137
138#define UNSIGNAL_PENDING_SIGNALS() \
139 do { \
140 _Py_atomic_store_relaxed(&_PyRuntime.ceval.signals_pending, 0); \
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100141 COMPUTE_EVAL_BREAKER(); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700142 } while (0)
143
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100144#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 do { \
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100146 _PyRuntime.ceval.pending.async_exc = 1; \
147 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000149
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100150#define UNSIGNAL_ASYNC_EXC() \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600151 do { \
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100152 _PyRuntime.ceval.pending.async_exc = 0; \
153 COMPUTE_EVAL_BREAKER(); \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600154 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000155
156
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000157#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000158#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000159#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000160#include "pythread.h"
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000161#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000162
Tim Peters7f468f22004-10-11 02:40:51 +0000163int
164PyEval_ThreadsInitialized(void)
165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000167}
168
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000169void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000170PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000171{
Victor Stinnera7126792019-03-19 14:19:38 +0100172 if (gil_created()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100174 }
175
Inada Naoki001fee12019-02-20 10:00:09 +0900176 PyThread_init_thread();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 create_gil();
Victor Stinner50b48572018-11-01 01:51:40 +0100178 take_gil(_PyThreadState_GET());
Eric Snow8479a342019-03-08 23:44:33 -0700179
180 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
181 if (_PyRuntime.ceval.pending.lock == NULL) {
Victor Stinnere3f40702019-03-15 16:04:20 +0100182 Py_FatalError("Can't initialize threads for pending calls");
Eric Snow5be45a62019-03-08 22:47:07 -0700183 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000184}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000185
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000186void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000187_PyEval_FiniThreads(void)
188{
Victor Stinnera7126792019-03-19 14:19:38 +0100189 if (!gil_created()) {
Antoine Pitrou1df15362010-09-13 14:16:46 +0000190 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100191 }
192
Antoine Pitrou1df15362010-09-13 14:16:46 +0000193 destroy_gil();
194 assert(!gil_created());
Victor Stinnera7126792019-03-19 14:19:38 +0100195
196 if (_PyRuntime.ceval.pending.lock != NULL) {
197 PyThread_free_lock(_PyRuntime.ceval.pending.lock);
198 _PyRuntime.ceval.pending.lock = NULL;
199 }
Antoine Pitrou1df15362010-09-13 14:16:46 +0000200}
201
202void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000203PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000204{
Victor Stinner50b48572018-11-01 01:51:40 +0100205 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 if (tstate == NULL)
207 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
208 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000209}
210
211void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000212PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100215 We therefore avoid PyThreadState_Get() which dumps a fatal error
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 in debug mode.
217 */
Victor Stinner50b48572018-11-01 01:51:40 +0100218 drop_gil(_PyThreadState_GET());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000219}
220
221void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000222PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000223{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 if (tstate == NULL)
225 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
226 /* Check someone has called PyEval_InitThreads() to create the lock */
227 assert(gil_created());
228 take_gil(tstate);
229 if (PyThreadState_Swap(tstate) != NULL)
230 Py_FatalError(
231 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000232}
233
234void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000235PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000236{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 if (tstate == NULL)
238 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
239 if (PyThreadState_Swap(NULL) != tstate)
240 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
241 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000242}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000243
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200244/* This function is called from PyOS_AfterFork_Child to destroy all threads
245 * which are not running in the child process, and clear internal locks
246 * which might be held by those threads.
247 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000248
249void
250PyEval_ReInitThreads(void)
251{
Victor Stinner50b48572018-11-01 01:51:40 +0100252 PyThreadState *current_tstate = _PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 if (!gil_created())
255 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 recreate_gil();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200257 take_gil(current_tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700258
Eric Snow5be45a62019-03-08 22:47:07 -0700259 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
Eric Snow8479a342019-03-08 23:44:33 -0700260 if (_PyRuntime.ceval.pending.lock == NULL) {
261 Py_FatalError("Can't initialize threads for pending calls");
262 }
Jesse Nollera8513972008-07-17 16:49:17 +0000263
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200264 /* Destroy all threads except the current one */
265 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000266}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000267
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000268/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600269 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000270
271void
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100272_PyEval_SignalAsyncExc(void)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000273{
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100274 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000275}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000276
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000277PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000278PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000279{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 PyThreadState *tstate = PyThreadState_Swap(NULL);
281 if (tstate == NULL)
282 Py_FatalError("PyEval_SaveThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100283 assert(gil_created());
284 drop_gil(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000286}
287
288void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000289PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 if (tstate == NULL)
292 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100293 assert(gil_created());
294
295 int err = errno;
296 take_gil(tstate);
297 /* _Py_Finalizing is protected by the GIL */
298 if (_Py_IsFinalizing() && !_Py_CURRENTLY_FINALIZING(tstate)) {
299 drop_gil(tstate);
300 PyThread_exit_thread();
301 Py_UNREACHABLE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 }
Victor Stinner2914bb32018-01-29 11:57:45 +0100303 errno = err;
304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000306}
307
308
Guido van Rossuma9672091994-09-14 13:31:22 +0000309/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
310 signal handlers or Mac I/O completion routines) can schedule calls
311 to a function to be called synchronously.
312 The synchronous function is called with one void* argument.
313 It should return 0 for success or -1 for failure -- failure should
314 be accompanied by an exception.
315
316 If registry succeeds, the registry function returns 0; if it fails
317 (e.g. due to too many pending calls) it returns -1 (without setting
318 an exception condition).
319
320 Note that because registry may occur from within signal handlers,
321 or other asynchronous events, calling malloc() is unsafe!
322
Guido van Rossuma9672091994-09-14 13:31:22 +0000323 Any thread can schedule pending calls, but only the main thread
324 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000325 There is no facility to schedule calls to a particular thread, but
326 that should be easy to change, should that ever be required. In
327 that case, the static variables here should go into the python
328 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000329*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000330
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200331void
332_PyEval_SignalReceived(void)
333{
334 /* bpo-30703: Function called when the C signal handler of Python gets a
335 signal. We cannot queue a callback using Py_AddPendingCall() since
336 that function is not async-signal-safe. */
Eric Snowfdf282d2019-01-11 14:26:55 -0700337 SIGNAL_PENDING_SIGNALS();
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200338}
339
Eric Snow5be45a62019-03-08 22:47:07 -0700340/* Push one item onto the queue while holding the lock. */
341static int
Eric Snow842a2f02019-03-15 15:47:51 -0600342_push_pending_call(struct _pending_calls *pending,
343 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700344{
Eric Snow842a2f02019-03-15 15:47:51 -0600345 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700346 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600347 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700348 return -1; /* Queue full */
349 }
Eric Snow842a2f02019-03-15 15:47:51 -0600350 pending->calls[i].func = func;
351 pending->calls[i].arg = arg;
352 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700353 return 0;
354}
355
356/* Pop one item off the queue while holding the lock. */
357static void
Eric Snow842a2f02019-03-15 15:47:51 -0600358_pop_pending_call(struct _pending_calls *pending,
359 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700360{
Eric Snow842a2f02019-03-15 15:47:51 -0600361 int i = pending->first;
362 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700363 return; /* Queue empty */
364 }
365
Eric Snow842a2f02019-03-15 15:47:51 -0600366 *func = pending->calls[i].func;
367 *arg = pending->calls[i].arg;
368 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700369}
370
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200371/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000372 scheduling to be made from any thread, and even from an executing
373 callback.
374 */
375
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000376int
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100377Py_AddPendingCall(int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000378{
Eric Snow842a2f02019-03-15 15:47:51 -0600379 struct _pending_calls *pending = &_PyRuntime.ceval.pending;
380
381 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
382 if (pending->finishing) {
383 PyThread_release_lock(pending->lock);
384
385 PyObject *exc, *val, *tb;
386 PyErr_Fetch(&exc, &val, &tb);
387 PyErr_SetString(PyExc_SystemError,
388 "Py_AddPendingCall: cannot add pending calls "
389 "(Python shutting down)");
390 PyErr_Print();
391 PyErr_Restore(exc, val, tb);
392 return -1;
393 }
394 int result = _push_pending_call(pending, func, arg);
395 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 /* signal main loop */
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100398 SIGNAL_PENDING_CALLS();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000400}
401
Eric Snowfdf282d2019-01-11 14:26:55 -0700402static int
403handle_signals(void)
404{
Eric Snow5be45a62019-03-08 22:47:07 -0700405 /* Only handle signals on main thread. PyEval_InitThreads must
406 * have been called already.
407 */
408 if (PyThread_get_thread_ident() != _PyRuntime.main_thread) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700409 return 0;
410 }
Eric Snow64d6cc82019-02-23 15:40:43 -0700411 /*
412 * Ensure that the thread isn't currently running some other
413 * interpreter.
414 */
415 if (_PyInterpreterState_GET_UNSAFE() != _PyRuntime.interpreters.main) {
416 return 0;
417 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700418
419 UNSIGNAL_PENDING_SIGNALS();
Eric Snow64d6cc82019-02-23 15:40:43 -0700420 if (_PyErr_CheckSignals() < 0) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700421 SIGNAL_PENDING_SIGNALS(); /* We're not done yet */
422 return -1;
423 }
424 return 0;
425}
426
427static int
Eric Snow842a2f02019-03-15 15:47:51 -0600428make_pending_calls(struct _pending_calls* pending)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000429{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200430 static int busy = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000431
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100432 /* only service pending calls on main thread */
Eric Snow5be45a62019-03-08 22:47:07 -0700433 if (PyThread_get_thread_ident() != _PyRuntime.main_thread) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100434 return 0;
435 }
436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 /* don't perform recursive pending calls */
Eric Snowfdf282d2019-01-11 14:26:55 -0700438 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700440 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200441 busy = 1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200442 /* unsignal before starting to call callbacks, so that any callback
443 added in-between re-signals */
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100444 UNSIGNAL_PENDING_CALLS();
Eric Snowfdf282d2019-01-11 14:26:55 -0700445 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 /* perform a bounded number of calls, in case of recursion */
Eric Snowfdf282d2019-01-11 14:26:55 -0700448 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700449 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 void *arg = NULL;
451
452 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600453 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
454 _pop_pending_call(pending, &func, &arg);
455 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700456
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100457 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700458 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100459 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700460 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700461 res = func(arg);
462 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200463 goto error;
464 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200466
Charles-François Natalif23339a2011-07-23 18:15:43 +0200467 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700468 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200469
470error:
471 busy = 0;
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100472 SIGNAL_PENDING_CALLS();
Eric Snowfdf282d2019-01-11 14:26:55 -0700473 return res;
474}
475
Eric Snow842a2f02019-03-15 15:47:51 -0600476void
477_Py_FinishPendingCalls(void)
478{
479 struct _pending_calls *pending = &_PyRuntime.ceval.pending;
480
481 assert(PyGILState_Check());
482
483 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
484 pending->finishing = 1;
485 PyThread_release_lock(pending->lock);
486
487 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
488 return;
489 }
490
491 if (make_pending_calls(pending) < 0) {
492 PyObject *exc, *val, *tb;
493 PyErr_Fetch(&exc, &val, &tb);
494 PyErr_BadInternalCall();
495 _PyErr_ChainExceptions(exc, val, tb);
496 PyErr_Print();
497 }
498}
499
Eric Snowfdf282d2019-01-11 14:26:55 -0700500/* Py_MakePendingCalls() is a simple wrapper for the sake
501 of backward-compatibility. */
502int
503Py_MakePendingCalls(void)
504{
505 assert(PyGILState_Check());
506
507 /* Python signal handler doesn't really queue a callback: it only signals
508 that a signal was received, see _PyEval_SignalReceived(). */
509 int res = handle_signals();
510 if (res != 0) {
511 return res;
512 }
513
Eric Snow842a2f02019-03-15 15:47:51 -0600514 res = make_pending_calls(&_PyRuntime.ceval.pending);
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100515 if (res != 0) {
516 return res;
517 }
518
519 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000520}
521
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000522/* The interpreter's recursion limit */
523
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000524#ifndef Py_DEFAULT_RECURSION_LIMIT
525#define Py_DEFAULT_RECURSION_LIMIT 1000
526#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600527
Eric Snow05351c12017-09-05 21:43:08 -0700528int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000529
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600530void
531_PyEval_Initialize(struct _ceval_runtime_state *state)
532{
533 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
534 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
535 _gil_initialize(&state->gil);
536}
537
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000538int
539Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000540{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600541 return _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000542}
543
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000544void
545Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000546{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600547 _PyRuntime.ceval.recursion_limit = new_limit;
548 _Py_CheckRecursionLimit = _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000549}
550
Armin Rigo2b3eb402003-10-28 12:05:48 +0000551/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
552 if the recursion_depth reaches _Py_CheckRecursionLimit.
553 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
554 to guarantee that _Py_CheckRecursiveCall() is regularly called.
555 Without USE_STACKCHECK, there is no need for this. */
556int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300557_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000558{
Victor Stinner50b48572018-11-01 01:51:40 +0100559 PyThreadState *tstate = _PyThreadState_GET();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600560 int recursion_limit = _PyRuntime.ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000561
562#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700563 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 if (PyOS_CheckStack()) {
565 --tstate->recursion_depth;
566 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
567 return -1;
568 }
pdox18967932017-10-25 23:03:01 -0700569 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700570 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700571#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 if (tstate->recursion_critical)
573 /* Somebody asked that we don't check for recursion. */
574 return 0;
575 if (tstate->overflowed) {
576 if (tstate->recursion_depth > recursion_limit + 50) {
577 /* Overflowing while handling an overflow. Give up. */
578 Py_FatalError("Cannot recover from stack overflow.");
579 }
580 return 0;
581 }
582 if (tstate->recursion_depth > recursion_limit) {
583 --tstate->recursion_depth;
584 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400585 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 "maximum recursion depth exceeded%s",
587 where);
588 return -1;
589 }
590 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000591}
592
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400593static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000594static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000595
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600596#define _Py_TracingPossible _PyRuntime.ceval.tracing_possible
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000597
Guido van Rossum374a9221991-04-04 10:40:29 +0000598
Guido van Rossumb209a111997-04-29 18:18:01 +0000599PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000600PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 return PyEval_EvalCodeEx(co,
603 globals, locals,
604 (PyObject **)NULL, 0,
605 (PyObject **)NULL, 0,
606 (PyObject **)NULL, 0,
607 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000608}
609
610
611/* Interpreter main loop */
612
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000613PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000614PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 /* This is for backward compatibility with extension modules that
616 used this API; core interpreter code should call
617 PyEval_EvalFrameEx() */
618 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000619}
620
621PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000622PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000623{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200624 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
625 return interp->eval_frame(f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700626}
627
Victor Stinnerc6944e72016-11-11 02:13:35 +0100628PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700629_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
630{
Guido van Rossum950361c1997-01-24 13:49:28 +0000631#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000633#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200634 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300635 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200636 int opcode; /* Current opcode */
637 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200638 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 PyObject *retval = NULL; /* Return value */
Victor Stinner50b48572018-11-01 01:51:40 +0100640 PyThreadState *tstate = _PyThreadState_GET();
Eric Snow7bda9de2019-03-08 17:25:54 -0700641 _Py_atomic_int *eval_breaker = &_PyRuntime.ceval.eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 is true when the line being executed has changed. The
649 initial values are such as to make this false the first
650 time it is tested. */
651 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000652
Serhiy Storchakaab874002016-09-11 13:48:15 +0300653 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 PyObject *names;
655 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000656
Brett Cannon368b4b72012-04-02 12:17:59 -0400657#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200658 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400659#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200660
Antoine Pitroub52ec782009-01-25 16:34:23 +0000661/* Computed GOTOs, or
662 the-optimization-commonly-but-improperly-known-as-"threaded code"
663 using gcc's labels-as-values extension
664 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
665
666 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000668 combined with a lookup table of jump addresses. However, since the
669 indirect jump instruction is shared by all opcodes, the CPU will have a
670 hard time making the right prediction for where to jump next (actually,
671 it will be always wrong except in the uncommon case of a sequence of
672 several identical opcodes).
673
674 "Threaded code" in contrast, uses an explicit jump table and an explicit
675 indirect jump instruction at the end of each opcode. Since the jump
676 instruction is at a different address for each opcode, the CPU will make a
677 separate prediction for each of these instructions, which is equivalent to
678 predicting the second opcode of each opcode pair. These predictions have
679 a much better chance to turn out valid, especially in small bytecode loops.
680
681 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000683 and potentially many more instructions (depending on the pipeline width).
684 A correctly predicted branch, however, is nearly free.
685
686 At the time of this writing, the "threaded code" version is up to 15-20%
687 faster than the normal "switch" version, depending on the compiler and the
688 CPU architecture.
689
690 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
691 because it would render the measurements invalid.
692
693
694 NOTE: care must be taken that the compiler doesn't try to "optimize" the
695 indirect jumps by sharing them between all opcodes. Such optimizations
696 can be disabled on gcc by using the -fno-gcse flag (or possibly
697 -fno-crossjumping).
698*/
699
Antoine Pitrou042b1282010-08-13 21:15:58 +0000700#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000701#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000702#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000703#endif
704
Antoine Pitrou042b1282010-08-13 21:15:58 +0000705#ifdef HAVE_COMPUTED_GOTOS
706 #ifndef USE_COMPUTED_GOTOS
707 #define USE_COMPUTED_GOTOS 1
708 #endif
709#else
710 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
711 #error "Computed gotos are not supported on this compiler."
712 #endif
713 #undef USE_COMPUTED_GOTOS
714 #define USE_COMPUTED_GOTOS 0
715#endif
716
717#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000718/* Import the static jump table */
719#include "opcode_targets.h"
720
Antoine Pitroub52ec782009-01-25 16:34:23 +0000721#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700722 op: \
723 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000724
Antoine Pitroub52ec782009-01-25 16:34:23 +0000725#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 { \
Eric Snow7bda9de2019-03-08 17:25:54 -0700727 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 FAST_DISPATCH(); \
729 } \
730 continue; \
731 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000732
733#ifdef LLTRACE
734#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700736 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300738 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300739 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 } \
741 goto fast_next_opcode; \
742 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000743#else
744#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700746 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300748 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300749 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 } \
751 goto fast_next_opcode; \
752 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000753#endif
754
755#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700756#define TARGET(op) op
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300757
Antoine Pitroub52ec782009-01-25 16:34:23 +0000758#define DISPATCH() continue
759#define FAST_DISPATCH() goto fast_next_opcode
760#endif
761
762
Neal Norwitza81d2202002-07-14 00:27:26 +0000763/* Tuple access macros */
764
765#ifndef Py_DEBUG
766#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
767#else
768#define GETITEM(v, i) PyTuple_GetItem((v), (i))
769#endif
770
Guido van Rossum374a9221991-04-04 10:40:29 +0000771/* Code access macros */
772
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300773/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600774#define INSTR_OFFSET() \
775 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300776#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300777 _Py_CODEUNIT word = *next_instr; \
778 opcode = _Py_OPCODE(word); \
779 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300780 next_instr++; \
781 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300782#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
783#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000784
Raymond Hettingerf606f872003-03-16 03:11:04 +0000785/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 Some opcodes tend to come in pairs thus making it possible to
787 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300788 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 Verifying the prediction costs a single high-speed test of a register
791 variable against a constant. If the pairing was good, then the
792 processor's own internal branch predication has a high likelihood of
793 success, resulting in a nearly zero-overhead transition to the
794 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300795 including its unpredictable switch-case branch. Combined with the
796 processor's internal branch prediction, a successful PREDICT has the
797 effect of making the two opcodes run as if they were a single new opcode
798 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000799
Georg Brandl86b2fb92008-07-16 03:43:04 +0000800 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 predictions turned-on and interpret the results as if some opcodes
802 had been combined or turn-off predictions so that the opcode frequency
803 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000804
805 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 the CPU to record separate branch prediction information for each
807 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000808
Raymond Hettingerf606f872003-03-16 03:11:04 +0000809*/
810
Antoine Pitrou042b1282010-08-13 21:15:58 +0000811#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000813#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300814#define PREDICT(op) \
815 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300816 _Py_CODEUNIT word = *next_instr; \
817 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300818 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300819 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300820 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300821 goto PRED_##op; \
822 } \
823 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000824#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300825#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000826
Raymond Hettingerf606f872003-03-16 03:11:04 +0000827
Guido van Rossum374a9221991-04-04 10:40:29 +0000828/* Stack manipulation macros */
829
Martin v. Löwis18e16552006-02-15 17:27:45 +0000830/* The stack can grow at most MAXINT deep, as co_nlocals and
831 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000832#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
833#define EMPTY() (STACK_LEVEL() == 0)
834#define TOP() (stack_pointer[-1])
835#define SECOND() (stack_pointer[-2])
836#define THIRD() (stack_pointer[-3])
837#define FOURTH() (stack_pointer[-4])
838#define PEEK(n) (stack_pointer[-(n)])
839#define SET_TOP(v) (stack_pointer[-1] = (v))
840#define SET_SECOND(v) (stack_pointer[-2] = (v))
841#define SET_THIRD(v) (stack_pointer[-3] = (v))
842#define SET_FOURTH(v) (stack_pointer[-4] = (v))
843#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
844#define BASIC_STACKADJ(n) (stack_pointer += n)
845#define BASIC_PUSH(v) (*stack_pointer++ = (v))
846#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000847
Guido van Rossum96a42c81992-01-12 02:29:51 +0000848#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000850 lltrace && prtrace(TOP(), "push")); \
851 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000853 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +0000854#define STACK_GROW(n) do { \
855 assert(n >= 0); \
856 (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000857 lltrace && prtrace(TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000858 assert(STACK_LEVEL() <= co->co_stacksize); \
859 } while (0)
860#define STACK_SHRINK(n) do { \
861 assert(n >= 0); \
862 (void)(lltrace && prtrace(TOP(), "stackadj")); \
863 (void)(BASIC_STACKADJ(-n)); \
864 assert(STACK_LEVEL() <= co->co_stacksize); \
865 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +0000866#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000867 prtrace((STACK_POINTER)[-1], "ext_pop")), \
868 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000869#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000870#define PUSH(v) BASIC_PUSH(v)
871#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +0000872#define STACK_GROW(n) BASIC_STACKADJ(n)
873#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000874#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000875#endif
876
Guido van Rossum681d79a1995-07-18 14:51:37 +0000877/* Local variable macros */
878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000880
881/* The SETLOCAL() macro must not DECREF the local variable in-place and
882 then store the new value; it must copy the old value to a temporary
883 value, then store the new value, and then DECREF the temporary value.
884 This is because it is possible that during the DECREF the frame is
885 accessed by other code (e.g. a __del__ method or gc.collect()) and the
886 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000888 GETLOCAL(i) = value; \
889 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000890
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000891
892#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 while (STACK_LEVEL() > (b)->b_level) { \
894 PyObject *v = POP(); \
895 Py_XDECREF(v); \
896 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000897
898#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300899 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +0100901 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 assert(STACK_LEVEL() >= (b)->b_level + 3); \
903 while (STACK_LEVEL() > (b)->b_level + 3) { \
904 value = POP(); \
905 Py_XDECREF(value); \
906 } \
Mark Shannonae3087c2017-10-22 22:41:51 +0100907 exc_info = tstate->exc_info; \
908 type = exc_info->exc_type; \
909 value = exc_info->exc_value; \
910 traceback = exc_info->exc_traceback; \
911 exc_info->exc_type = POP(); \
912 exc_info->exc_value = POP(); \
913 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 Py_XDECREF(type); \
915 Py_XDECREF(value); \
916 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300917 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000918
Guido van Rossuma027efa1997-05-05 20:56:21 +0000919/* Start of code */
920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 /* push frame */
922 if (Py_EnterRecursiveCall(""))
923 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 if (tstate->use_tracing) {
928 if (tstate->c_tracefunc != NULL) {
929 /* tstate->c_tracefunc, if defined, is a
930 function that will be called on *every* entry
931 to a code block. Its return value, if not
932 None, is a function that will be called at
933 the start of each executed line of code.
934 (Actually, the function must return itself
935 in order to continue tracing.) The trace
936 functions are called with three arguments:
937 a pointer to the current frame, a string
938 indicating why the function is called, and
939 an argument which depends on the situation.
940 The global trace function is also called
941 whenever an exception is detected. */
942 if (call_trace_protected(tstate->c_tracefunc,
943 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100944 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 /* Trace function raised an error */
946 goto exit_eval_frame;
947 }
948 }
949 if (tstate->c_profilefunc != NULL) {
950 /* Similar for c_profilefunc, except it needn't
951 return itself and isn't called for "line" events */
952 if (call_trace_protected(tstate->c_profilefunc,
953 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100954 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 /* Profile function raised an error */
956 goto exit_eval_frame;
957 }
958 }
959 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000960
Łukasz Langaa785c872016-09-09 17:37:37 -0700961 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
962 dtrace_function_entry(f);
963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 co = f->f_code;
965 names = co->co_names;
966 consts = co->co_consts;
967 fastlocals = f->f_localsplus;
968 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300969 assert(PyBytes_Check(co->co_code));
970 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +0300971 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
972 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
973 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300974 /*
975 f->f_lasti refers to the index of the last instruction,
976 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000977
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300978 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500979 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 When the PREDICT() macros are enabled, some opcode pairs follow in
982 direct succession without updating f->f_lasti. A successful
983 prediction effectively links the two codes together as if they
984 were a single new opcode; accordingly,f->f_lasti will point to
985 the first code in the pair (for instance, GET_ITER followed by
986 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300987 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300989 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300990 next_instr = first_instr;
991 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +0300992 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
993 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300994 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 stack_pointer = f->f_stacktop;
996 assert(stack_pointer != NULL);
997 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +0200998 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000999
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001000
Tim Peters5ca576e2001-06-18 22:08:13 +00001001#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001002 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001003#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001004
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001005 if (throwflag) /* support for generator.throw() */
1006 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001007
Victor Stinnerace47d72013-07-18 01:41:08 +02001008#ifdef Py_DEBUG
1009 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001010 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001011 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +02001012 assert(!PyErr_Occurred());
1013#endif
1014
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001015main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1018 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +02001019 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 /* Do periodic things. Doing this every time through
1022 the loop would add too much overhead, so we do it
1023 only every Nth instruction. We also do it if
1024 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1025 event needs attention (e.g. a signal handler or
1026 async I/O handler); see Py_AddPendingCall() and
1027 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001028
Eric Snow7bda9de2019-03-08 17:25:54 -07001029 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001030 opcode = _Py_OPCODE(*next_instr);
1031 if (opcode == SETUP_FINALLY ||
1032 opcode == SETUP_WITH ||
1033 opcode == BEFORE_ASYNC_WITH ||
1034 opcode == YIELD_FROM) {
1035 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001036 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001037 - If we're about to enter the 'with:'. It will prevent
1038 emitting a resource warning in the common idiom
1039 'with open(path) as file:'.
1040 - If we're about to enter the 'async with:'.
1041 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001042 *very* useful, but might help in some cases and it's
1043 traditional)
1044 - If we're resuming a chain of nested 'yield from' or
1045 'await' calls, then each frame is parked with YIELD_FROM
1046 as its next opcode. If the user hit control-C we want to
1047 wait until we've reached the innermost frame before
1048 running the signal handler and raising KeyboardInterrupt
1049 (see bpo-30039).
1050 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 goto fast_next_opcode;
1052 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001053
1054 if (_Py_atomic_load_relaxed(
1055 &_PyRuntime.ceval.signals_pending))
1056 {
1057 if (handle_signals() != 0) {
1058 goto error;
1059 }
1060 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001061 if (_Py_atomic_load_relaxed(
Victor Stinner4d61e6e2019-03-04 14:21:28 +01001062 &_PyRuntime.ceval.pending.calls_to_do))
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001063 {
Eric Snow842a2f02019-03-15 15:47:51 -06001064 if (make_pending_calls(&_PyRuntime.ceval.pending) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001065 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001066 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001068
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001069 if (_Py_atomic_load_relaxed(
1070 &_PyRuntime.ceval.gil_drop_request))
1071 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 /* Give another thread a chance */
1073 if (PyThreadState_Swap(NULL) != tstate)
1074 Py_FatalError("ceval: tstate mix-up");
1075 drop_gil(tstate);
1076
1077 /* Other threads may run now */
1078
1079 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001080
1081 /* Check if we should make a quick exit. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001082 if (_Py_IsFinalizing() &&
1083 !_Py_CURRENTLY_FINALIZING(tstate))
1084 {
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001085 drop_gil(tstate);
1086 PyThread_exit_thread();
1087 }
1088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 if (PyThreadState_Swap(tstate) != NULL)
1090 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 }
1092 /* Check for asynchronous exceptions. */
1093 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001094 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 tstate->async_exc = NULL;
Victor Stinner4d61e6e2019-03-04 14:21:28 +01001096 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001097 PyErr_SetNone(exc);
1098 Py_DECREF(exc);
1099 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 }
1101 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001102
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 fast_next_opcode:
1104 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001105
Łukasz Langaa785c872016-09-09 17:37:37 -07001106 if (PyDTrace_LINE_ENABLED())
1107 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001112 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001113 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 /* see maybe_call_line_trace
1115 for expository comments */
1116 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 err = maybe_call_line_trace(tstate->c_tracefunc,
1119 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001120 tstate, f,
1121 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 /* Reload possibly changed frame fields */
1123 JUMPTO(f->f_lasti);
1124 if (f->f_stacktop != NULL) {
1125 stack_pointer = f->f_stacktop;
1126 f->f_stacktop = NULL;
1127 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001128 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001130 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001134
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001135 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001136 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001137#ifdef DYNAMIC_EXECUTION_PROFILE
1138#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 dxpairs[lastopcode][opcode]++;
1140 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001141#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001143#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001144
Guido van Rossum96a42c81992-01-12 02:29:51 +00001145#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 if (lltrace) {
1149 if (HAS_ARG(opcode)) {
1150 printf("%d: %d, %d\n",
1151 f->f_lasti, opcode, oparg);
1152 }
1153 else {
1154 printf("%d: %d\n",
1155 f->f_lasti, opcode);
1156 }
1157 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001158#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001159
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001163 It is essential that any operation that fails must goto error
1164 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001165
Benjamin Petersonddd19492018-09-16 22:38:02 -07001166 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001168 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001169
Benjamin Petersonddd19492018-09-16 22:38:02 -07001170 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001171 PyObject *value = GETLOCAL(oparg);
1172 if (value == NULL) {
1173 format_exc_check_arg(PyExc_UnboundLocalError,
1174 UNBOUNDLOCAL_ERROR_MSG,
1175 PyTuple_GetItem(co->co_varnames, oparg));
1176 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001178 Py_INCREF(value);
1179 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001181 }
1182
Benjamin Petersonddd19492018-09-16 22:38:02 -07001183 case TARGET(LOAD_CONST): {
1184 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001185 PyObject *value = GETITEM(consts, oparg);
1186 Py_INCREF(value);
1187 PUSH(value);
1188 FAST_DISPATCH();
1189 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001190
Benjamin Petersonddd19492018-09-16 22:38:02 -07001191 case TARGET(STORE_FAST): {
1192 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001193 PyObject *value = POP();
1194 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001196 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001197
Benjamin Petersonddd19492018-09-16 22:38:02 -07001198 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001199 PyObject *value = POP();
1200 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001202 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001203
Benjamin Petersonddd19492018-09-16 22:38:02 -07001204 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001205 PyObject *top = TOP();
1206 PyObject *second = SECOND();
1207 SET_TOP(second);
1208 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001210 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001211
Benjamin Petersonddd19492018-09-16 22:38:02 -07001212 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001213 PyObject *top = TOP();
1214 PyObject *second = SECOND();
1215 PyObject *third = THIRD();
1216 SET_TOP(second);
1217 SET_SECOND(third);
1218 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001220 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001221
Benjamin Petersonddd19492018-09-16 22:38:02 -07001222 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001223 PyObject *top = TOP();
1224 PyObject *second = SECOND();
1225 PyObject *third = THIRD();
1226 PyObject *fourth = FOURTH();
1227 SET_TOP(second);
1228 SET_SECOND(third);
1229 SET_THIRD(fourth);
1230 SET_FOURTH(top);
1231 FAST_DISPATCH();
1232 }
1233
Benjamin Petersonddd19492018-09-16 22:38:02 -07001234 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001235 PyObject *top = TOP();
1236 Py_INCREF(top);
1237 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001239 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001240
Benjamin Petersonddd19492018-09-16 22:38:02 -07001241 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001242 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001243 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001244 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001245 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001246 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001247 SET_TOP(top);
1248 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001249 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001250 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001251
Benjamin Petersonddd19492018-09-16 22:38:02 -07001252 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001253 PyObject *value = TOP();
1254 PyObject *res = PyNumber_Positive(value);
1255 Py_DECREF(value);
1256 SET_TOP(res);
1257 if (res == NULL)
1258 goto error;
1259 DISPATCH();
1260 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001261
Benjamin Petersonddd19492018-09-16 22:38:02 -07001262 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001263 PyObject *value = TOP();
1264 PyObject *res = PyNumber_Negative(value);
1265 Py_DECREF(value);
1266 SET_TOP(res);
1267 if (res == NULL)
1268 goto error;
1269 DISPATCH();
1270 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001271
Benjamin Petersonddd19492018-09-16 22:38:02 -07001272 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001273 PyObject *value = TOP();
1274 int err = PyObject_IsTrue(value);
1275 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 if (err == 0) {
1277 Py_INCREF(Py_True);
1278 SET_TOP(Py_True);
1279 DISPATCH();
1280 }
1281 else if (err > 0) {
1282 Py_INCREF(Py_False);
1283 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 DISPATCH();
1285 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001286 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001287 goto error;
1288 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001289
Benjamin Petersonddd19492018-09-16 22:38:02 -07001290 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001291 PyObject *value = TOP();
1292 PyObject *res = PyNumber_Invert(value);
1293 Py_DECREF(value);
1294 SET_TOP(res);
1295 if (res == NULL)
1296 goto error;
1297 DISPATCH();
1298 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001299
Benjamin Petersonddd19492018-09-16 22:38:02 -07001300 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001301 PyObject *exp = POP();
1302 PyObject *base = TOP();
1303 PyObject *res = PyNumber_Power(base, exp, Py_None);
1304 Py_DECREF(base);
1305 Py_DECREF(exp);
1306 SET_TOP(res);
1307 if (res == NULL)
1308 goto error;
1309 DISPATCH();
1310 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001311
Benjamin Petersonddd19492018-09-16 22:38:02 -07001312 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001313 PyObject *right = POP();
1314 PyObject *left = TOP();
1315 PyObject *res = PyNumber_Multiply(left, right);
1316 Py_DECREF(left);
1317 Py_DECREF(right);
1318 SET_TOP(res);
1319 if (res == NULL)
1320 goto error;
1321 DISPATCH();
1322 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001323
Benjamin Petersonddd19492018-09-16 22:38:02 -07001324 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001325 PyObject *right = POP();
1326 PyObject *left = TOP();
1327 PyObject *res = PyNumber_MatrixMultiply(left, right);
1328 Py_DECREF(left);
1329 Py_DECREF(right);
1330 SET_TOP(res);
1331 if (res == NULL)
1332 goto error;
1333 DISPATCH();
1334 }
1335
Benjamin Petersonddd19492018-09-16 22:38:02 -07001336 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001337 PyObject *divisor = POP();
1338 PyObject *dividend = TOP();
1339 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1340 Py_DECREF(dividend);
1341 Py_DECREF(divisor);
1342 SET_TOP(quotient);
1343 if (quotient == NULL)
1344 goto error;
1345 DISPATCH();
1346 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001347
Benjamin Petersonddd19492018-09-16 22:38:02 -07001348 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001349 PyObject *divisor = POP();
1350 PyObject *dividend = TOP();
1351 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1352 Py_DECREF(dividend);
1353 Py_DECREF(divisor);
1354 SET_TOP(quotient);
1355 if (quotient == NULL)
1356 goto error;
1357 DISPATCH();
1358 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001359
Benjamin Petersonddd19492018-09-16 22:38:02 -07001360 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001361 PyObject *divisor = POP();
1362 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001363 PyObject *res;
1364 if (PyUnicode_CheckExact(dividend) && (
1365 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1366 // fast path; string formatting, but not if the RHS is a str subclass
1367 // (see issue28598)
1368 res = PyUnicode_Format(dividend, divisor);
1369 } else {
1370 res = PyNumber_Remainder(dividend, divisor);
1371 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001372 Py_DECREF(divisor);
1373 Py_DECREF(dividend);
1374 SET_TOP(res);
1375 if (res == NULL)
1376 goto error;
1377 DISPATCH();
1378 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001379
Benjamin Petersonddd19492018-09-16 22:38:02 -07001380 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001381 PyObject *right = POP();
1382 PyObject *left = TOP();
1383 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001384 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1385 CPython using bytecode, it is simply worthless.
1386 See http://bugs.python.org/issue21955 and
1387 http://bugs.python.org/issue10044 for the discussion. In short,
1388 no patch shown any impact on a realistic benchmark, only a minor
1389 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001390 if (PyUnicode_CheckExact(left) &&
1391 PyUnicode_CheckExact(right)) {
1392 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001393 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001394 }
1395 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001396 sum = PyNumber_Add(left, right);
1397 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001398 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001399 Py_DECREF(right);
1400 SET_TOP(sum);
1401 if (sum == NULL)
1402 goto error;
1403 DISPATCH();
1404 }
1405
Benjamin Petersonddd19492018-09-16 22:38:02 -07001406 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001407 PyObject *right = POP();
1408 PyObject *left = TOP();
1409 PyObject *diff = PyNumber_Subtract(left, right);
1410 Py_DECREF(right);
1411 Py_DECREF(left);
1412 SET_TOP(diff);
1413 if (diff == NULL)
1414 goto error;
1415 DISPATCH();
1416 }
1417
Benjamin Petersonddd19492018-09-16 22:38:02 -07001418 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001419 PyObject *sub = POP();
1420 PyObject *container = TOP();
1421 PyObject *res = PyObject_GetItem(container, sub);
1422 Py_DECREF(container);
1423 Py_DECREF(sub);
1424 SET_TOP(res);
1425 if (res == NULL)
1426 goto error;
1427 DISPATCH();
1428 }
1429
Benjamin Petersonddd19492018-09-16 22:38:02 -07001430 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001431 PyObject *right = POP();
1432 PyObject *left = TOP();
1433 PyObject *res = PyNumber_Lshift(left, right);
1434 Py_DECREF(left);
1435 Py_DECREF(right);
1436 SET_TOP(res);
1437 if (res == NULL)
1438 goto error;
1439 DISPATCH();
1440 }
1441
Benjamin Petersonddd19492018-09-16 22:38:02 -07001442 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001443 PyObject *right = POP();
1444 PyObject *left = TOP();
1445 PyObject *res = PyNumber_Rshift(left, right);
1446 Py_DECREF(left);
1447 Py_DECREF(right);
1448 SET_TOP(res);
1449 if (res == NULL)
1450 goto error;
1451 DISPATCH();
1452 }
1453
Benjamin Petersonddd19492018-09-16 22:38:02 -07001454 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001455 PyObject *right = POP();
1456 PyObject *left = TOP();
1457 PyObject *res = PyNumber_And(left, right);
1458 Py_DECREF(left);
1459 Py_DECREF(right);
1460 SET_TOP(res);
1461 if (res == NULL)
1462 goto error;
1463 DISPATCH();
1464 }
1465
Benjamin Petersonddd19492018-09-16 22:38:02 -07001466 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001467 PyObject *right = POP();
1468 PyObject *left = TOP();
1469 PyObject *res = PyNumber_Xor(left, right);
1470 Py_DECREF(left);
1471 Py_DECREF(right);
1472 SET_TOP(res);
1473 if (res == NULL)
1474 goto error;
1475 DISPATCH();
1476 }
1477
Benjamin Petersonddd19492018-09-16 22:38:02 -07001478 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001479 PyObject *right = POP();
1480 PyObject *left = TOP();
1481 PyObject *res = PyNumber_Or(left, right);
1482 Py_DECREF(left);
1483 Py_DECREF(right);
1484 SET_TOP(res);
1485 if (res == NULL)
1486 goto error;
1487 DISPATCH();
1488 }
1489
Benjamin Petersonddd19492018-09-16 22:38:02 -07001490 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001491 PyObject *v = POP();
1492 PyObject *list = PEEK(oparg);
1493 int err;
1494 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001496 if (err != 0)
1497 goto error;
1498 PREDICT(JUMP_ABSOLUTE);
1499 DISPATCH();
1500 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001501
Benjamin Petersonddd19492018-09-16 22:38:02 -07001502 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001503 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001504 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001505 int err;
1506 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001508 if (err != 0)
1509 goto error;
1510 PREDICT(JUMP_ABSOLUTE);
1511 DISPATCH();
1512 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001513
Benjamin Petersonddd19492018-09-16 22:38:02 -07001514 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001515 PyObject *exp = POP();
1516 PyObject *base = TOP();
1517 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1518 Py_DECREF(base);
1519 Py_DECREF(exp);
1520 SET_TOP(res);
1521 if (res == NULL)
1522 goto error;
1523 DISPATCH();
1524 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001525
Benjamin Petersonddd19492018-09-16 22:38:02 -07001526 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001527 PyObject *right = POP();
1528 PyObject *left = TOP();
1529 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1530 Py_DECREF(left);
1531 Py_DECREF(right);
1532 SET_TOP(res);
1533 if (res == NULL)
1534 goto error;
1535 DISPATCH();
1536 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001537
Benjamin Petersonddd19492018-09-16 22:38:02 -07001538 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001539 PyObject *right = POP();
1540 PyObject *left = TOP();
1541 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1542 Py_DECREF(left);
1543 Py_DECREF(right);
1544 SET_TOP(res);
1545 if (res == NULL)
1546 goto error;
1547 DISPATCH();
1548 }
1549
Benjamin Petersonddd19492018-09-16 22:38:02 -07001550 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001551 PyObject *divisor = POP();
1552 PyObject *dividend = TOP();
1553 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1554 Py_DECREF(dividend);
1555 Py_DECREF(divisor);
1556 SET_TOP(quotient);
1557 if (quotient == NULL)
1558 goto error;
1559 DISPATCH();
1560 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001561
Benjamin Petersonddd19492018-09-16 22:38:02 -07001562 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001563 PyObject *divisor = POP();
1564 PyObject *dividend = TOP();
1565 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1566 Py_DECREF(dividend);
1567 Py_DECREF(divisor);
1568 SET_TOP(quotient);
1569 if (quotient == NULL)
1570 goto error;
1571 DISPATCH();
1572 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001573
Benjamin Petersonddd19492018-09-16 22:38:02 -07001574 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001575 PyObject *right = POP();
1576 PyObject *left = TOP();
1577 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1578 Py_DECREF(left);
1579 Py_DECREF(right);
1580 SET_TOP(mod);
1581 if (mod == NULL)
1582 goto error;
1583 DISPATCH();
1584 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001585
Benjamin Petersonddd19492018-09-16 22:38:02 -07001586 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001587 PyObject *right = POP();
1588 PyObject *left = TOP();
1589 PyObject *sum;
1590 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1591 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001592 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001593 }
1594 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001595 sum = PyNumber_InPlaceAdd(left, right);
1596 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001597 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001598 Py_DECREF(right);
1599 SET_TOP(sum);
1600 if (sum == NULL)
1601 goto error;
1602 DISPATCH();
1603 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001604
Benjamin Petersonddd19492018-09-16 22:38:02 -07001605 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001606 PyObject *right = POP();
1607 PyObject *left = TOP();
1608 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1609 Py_DECREF(left);
1610 Py_DECREF(right);
1611 SET_TOP(diff);
1612 if (diff == NULL)
1613 goto error;
1614 DISPATCH();
1615 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001616
Benjamin Petersonddd19492018-09-16 22:38:02 -07001617 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001618 PyObject *right = POP();
1619 PyObject *left = TOP();
1620 PyObject *res = PyNumber_InPlaceLshift(left, right);
1621 Py_DECREF(left);
1622 Py_DECREF(right);
1623 SET_TOP(res);
1624 if (res == NULL)
1625 goto error;
1626 DISPATCH();
1627 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001628
Benjamin Petersonddd19492018-09-16 22:38:02 -07001629 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001630 PyObject *right = POP();
1631 PyObject *left = TOP();
1632 PyObject *res = PyNumber_InPlaceRshift(left, right);
1633 Py_DECREF(left);
1634 Py_DECREF(right);
1635 SET_TOP(res);
1636 if (res == NULL)
1637 goto error;
1638 DISPATCH();
1639 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001640
Benjamin Petersonddd19492018-09-16 22:38:02 -07001641 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001642 PyObject *right = POP();
1643 PyObject *left = TOP();
1644 PyObject *res = PyNumber_InPlaceAnd(left, right);
1645 Py_DECREF(left);
1646 Py_DECREF(right);
1647 SET_TOP(res);
1648 if (res == NULL)
1649 goto error;
1650 DISPATCH();
1651 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001652
Benjamin Petersonddd19492018-09-16 22:38:02 -07001653 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001654 PyObject *right = POP();
1655 PyObject *left = TOP();
1656 PyObject *res = PyNumber_InPlaceXor(left, right);
1657 Py_DECREF(left);
1658 Py_DECREF(right);
1659 SET_TOP(res);
1660 if (res == NULL)
1661 goto error;
1662 DISPATCH();
1663 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001664
Benjamin Petersonddd19492018-09-16 22:38:02 -07001665 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001666 PyObject *right = POP();
1667 PyObject *left = TOP();
1668 PyObject *res = PyNumber_InPlaceOr(left, right);
1669 Py_DECREF(left);
1670 Py_DECREF(right);
1671 SET_TOP(res);
1672 if (res == NULL)
1673 goto error;
1674 DISPATCH();
1675 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001676
Benjamin Petersonddd19492018-09-16 22:38:02 -07001677 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001678 PyObject *sub = TOP();
1679 PyObject *container = SECOND();
1680 PyObject *v = THIRD();
1681 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001682 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001683 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001684 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001686 Py_DECREF(container);
1687 Py_DECREF(sub);
1688 if (err != 0)
1689 goto error;
1690 DISPATCH();
1691 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001692
Benjamin Petersonddd19492018-09-16 22:38:02 -07001693 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001694 PyObject *sub = TOP();
1695 PyObject *container = SECOND();
1696 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001697 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001698 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001699 err = PyObject_DelItem(container, sub);
1700 Py_DECREF(container);
1701 Py_DECREF(sub);
1702 if (err != 0)
1703 goto error;
1704 DISPATCH();
1705 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001706
Benjamin Petersonddd19492018-09-16 22:38:02 -07001707 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001708 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001709 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001710 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001711 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001712 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 PyErr_SetString(PyExc_RuntimeError,
1714 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001715 Py_DECREF(value);
1716 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001718 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001719 Py_DECREF(value);
1720 if (res == NULL)
1721 goto error;
1722 Py_DECREF(res);
1723 DISPATCH();
1724 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001725
Benjamin Petersonddd19492018-09-16 22:38:02 -07001726 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001727 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 switch (oparg) {
1729 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001730 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001731 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001733 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001734 /* fall through */
1735 case 0:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001736 if (do_raise(exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001737 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001738 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 break;
1740 default:
1741 PyErr_SetString(PyExc_SystemError,
1742 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 break;
1744 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001745 goto error;
1746 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001747
Benjamin Petersonddd19492018-09-16 22:38:02 -07001748 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001750 assert(f->f_iblock == 0);
1751 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001752 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001753
Benjamin Petersonddd19492018-09-16 22:38:02 -07001754 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001755 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001756 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001757 PyObject *obj = TOP();
1758 PyTypeObject *type = Py_TYPE(obj);
1759
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001760 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001761 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001762 }
Yury Selivanov75445082015-05-11 22:57:16 -04001763
1764 if (getter != NULL) {
1765 iter = (*getter)(obj);
1766 Py_DECREF(obj);
1767 if (iter == NULL) {
1768 SET_TOP(NULL);
1769 goto error;
1770 }
1771 }
1772 else {
1773 SET_TOP(NULL);
1774 PyErr_Format(
1775 PyExc_TypeError,
1776 "'async for' requires an object with "
1777 "__aiter__ method, got %.100s",
1778 type->tp_name);
1779 Py_DECREF(obj);
1780 goto error;
1781 }
1782
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001783 if (Py_TYPE(iter)->tp_as_async == NULL ||
1784 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001785
Yury Selivanov398ff912017-03-02 22:20:00 -05001786 SET_TOP(NULL);
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001787 PyErr_Format(
1788 PyExc_TypeError,
1789 "'async for' received an object from __aiter__ "
1790 "that does not implement __anext__: %.100s",
1791 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001792 Py_DECREF(iter);
1793 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001794 }
1795
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001796 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001797 DISPATCH();
1798 }
1799
Benjamin Petersonddd19492018-09-16 22:38:02 -07001800 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001801 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001802 PyObject *next_iter = NULL;
1803 PyObject *awaitable = NULL;
1804 PyObject *aiter = TOP();
1805 PyTypeObject *type = Py_TYPE(aiter);
1806
Yury Selivanoveb636452016-09-08 22:01:51 -07001807 if (PyAsyncGen_CheckExact(aiter)) {
1808 awaitable = type->tp_as_async->am_anext(aiter);
1809 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001810 goto error;
1811 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001812 } else {
1813 if (type->tp_as_async != NULL){
1814 getter = type->tp_as_async->am_anext;
1815 }
Yury Selivanov75445082015-05-11 22:57:16 -04001816
Yury Selivanoveb636452016-09-08 22:01:51 -07001817 if (getter != NULL) {
1818 next_iter = (*getter)(aiter);
1819 if (next_iter == NULL) {
1820 goto error;
1821 }
1822 }
1823 else {
1824 PyErr_Format(
1825 PyExc_TypeError,
1826 "'async for' requires an iterator with "
1827 "__anext__ method, got %.100s",
1828 type->tp_name);
1829 goto error;
1830 }
Yury Selivanov75445082015-05-11 22:57:16 -04001831
Yury Selivanoveb636452016-09-08 22:01:51 -07001832 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1833 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001834 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001835 PyExc_TypeError,
1836 "'async for' received an invalid object "
1837 "from __anext__: %.100s",
1838 Py_TYPE(next_iter)->tp_name);
1839
1840 Py_DECREF(next_iter);
1841 goto error;
1842 } else {
1843 Py_DECREF(next_iter);
1844 }
1845 }
Yury Selivanov75445082015-05-11 22:57:16 -04001846
1847 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001848 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001849 DISPATCH();
1850 }
1851
Benjamin Petersonddd19492018-09-16 22:38:02 -07001852 case TARGET(GET_AWAITABLE): {
1853 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001854 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001855 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001856
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03001857 if (iter == NULL) {
1858 format_awaitable_error(Py_TYPE(iterable),
1859 _Py_OPCODE(next_instr[-2]));
1860 }
1861
Yury Selivanov75445082015-05-11 22:57:16 -04001862 Py_DECREF(iterable);
1863
Yury Selivanovc724bae2016-03-02 11:30:46 -05001864 if (iter != NULL && PyCoro_CheckExact(iter)) {
1865 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1866 if (yf != NULL) {
1867 /* `iter` is a coroutine object that is being
1868 awaited, `yf` is a pointer to the current awaitable
1869 being awaited on. */
1870 Py_DECREF(yf);
1871 Py_CLEAR(iter);
1872 PyErr_SetString(
1873 PyExc_RuntimeError,
1874 "coroutine is being awaited already");
1875 /* The code below jumps to `error` if `iter` is NULL. */
1876 }
1877 }
1878
Yury Selivanov75445082015-05-11 22:57:16 -04001879 SET_TOP(iter); /* Even if it's NULL */
1880
1881 if (iter == NULL) {
1882 goto error;
1883 }
1884
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001885 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001886 DISPATCH();
1887 }
1888
Benjamin Petersonddd19492018-09-16 22:38:02 -07001889 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001890 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001891 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001892 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001893 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1894 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001895 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001896 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001897 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001898 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001899 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001900 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001901 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001902 Py_DECREF(v);
1903 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001904 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001905 if (tstate->c_tracefunc != NULL
1906 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001907 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001908 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001909 if (err < 0)
1910 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001911 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001912 SET_TOP(val);
1913 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001914 }
Martin Panter95f53c12016-07-18 08:23:26 +00001915 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001916 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001917 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001918 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001919 f->f_lasti -= sizeof(_Py_CODEUNIT);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001920 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001921 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001922
Benjamin Petersonddd19492018-09-16 22:38:02 -07001923 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07001925
1926 if (co->co_flags & CO_ASYNC_GENERATOR) {
1927 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
1928 Py_DECREF(retval);
1929 if (w == NULL) {
1930 retval = NULL;
1931 goto error;
1932 }
1933 retval = w;
1934 }
1935
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 f->f_stacktop = stack_pointer;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001937 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001938 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001939
Benjamin Petersonddd19492018-09-16 22:38:02 -07001940 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001941 PyObject *type, *value, *traceback;
1942 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001943 PyTryBlock *b = PyFrame_BlockPop(f);
1944 if (b->b_type != EXCEPT_HANDLER) {
1945 PyErr_SetString(PyExc_SystemError,
1946 "popped block is not an except handler");
1947 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001949 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
1950 STACK_LEVEL() <= (b)->b_level + 4);
1951 exc_info = tstate->exc_info;
1952 type = exc_info->exc_type;
1953 value = exc_info->exc_value;
1954 traceback = exc_info->exc_traceback;
1955 exc_info->exc_type = POP();
1956 exc_info->exc_value = POP();
1957 exc_info->exc_traceback = POP();
1958 Py_XDECREF(type);
1959 Py_XDECREF(value);
1960 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001962 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001963
Benjamin Petersonddd19492018-09-16 22:38:02 -07001964 case TARGET(POP_BLOCK): {
1965 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001966 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001968 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001969
Benjamin Petersonddd19492018-09-16 22:38:02 -07001970 case TARGET(POP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001971 /* If oparg is 0 at the top of the stack are 1 or 6 values:
1972 Either:
1973 - TOP = NULL or an integer
1974 or:
1975 - (TOP, SECOND, THIRD) = exc_info()
1976 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
1977
1978 If oparg is 1 the value for 'return' was additionally pushed
1979 at the top of the stack.
1980 */
1981 PyObject *res = NULL;
1982 if (oparg) {
1983 res = POP();
1984 }
1985 PyObject *exc = POP();
1986 if (exc == NULL || PyLong_CheckExact(exc)) {
1987 Py_XDECREF(exc);
1988 }
1989 else {
1990 Py_DECREF(exc);
1991 Py_DECREF(POP());
1992 Py_DECREF(POP());
1993
1994 PyObject *type, *value, *traceback;
1995 _PyErr_StackItem *exc_info;
1996 PyTryBlock *b = PyFrame_BlockPop(f);
1997 if (b->b_type != EXCEPT_HANDLER) {
1998 PyErr_SetString(PyExc_SystemError,
1999 "popped block is not an except handler");
2000 Py_XDECREF(res);
2001 goto error;
2002 }
2003 assert(STACK_LEVEL() == (b)->b_level + 3);
2004 exc_info = tstate->exc_info;
2005 type = exc_info->exc_type;
2006 value = exc_info->exc_value;
2007 traceback = exc_info->exc_traceback;
2008 exc_info->exc_type = POP();
2009 exc_info->exc_value = POP();
2010 exc_info->exc_traceback = POP();
2011 Py_XDECREF(type);
2012 Py_XDECREF(value);
2013 Py_XDECREF(traceback);
2014 }
2015 if (oparg) {
2016 PUSH(res);
2017 }
2018 DISPATCH();
2019 }
2020
Benjamin Petersonddd19492018-09-16 22:38:02 -07002021 case TARGET(CALL_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002022 PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
2023 if (ret == NULL) {
2024 goto error;
2025 }
2026 PUSH(ret);
2027 JUMPBY(oparg);
2028 FAST_DISPATCH();
2029 }
2030
Benjamin Petersonddd19492018-09-16 22:38:02 -07002031 case TARGET(BEGIN_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002032 /* Push NULL onto the stack for using it in END_FINALLY,
2033 POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH.
2034 */
2035 PUSH(NULL);
2036 FAST_DISPATCH();
2037 }
2038
Benjamin Petersonddd19492018-09-16 22:38:02 -07002039 case TARGET(END_FINALLY): {
2040 PREDICTED(END_FINALLY);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002041 /* At the top of the stack are 1 or 6 values:
2042 Either:
2043 - TOP = NULL or an integer
2044 or:
2045 - (TOP, SECOND, THIRD) = exc_info()
2046 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2047 */
2048 PyObject *exc = POP();
2049 if (exc == NULL) {
2050 FAST_DISPATCH();
2051 }
2052 else if (PyLong_CheckExact(exc)) {
2053 int ret = _PyLong_AsInt(exc);
2054 Py_DECREF(exc);
2055 if (ret == -1 && PyErr_Occurred()) {
2056 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002058 JUMPTO(ret);
2059 FAST_DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002060 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002061 else {
2062 assert(PyExceptionClass_Check(exc));
2063 PyObject *val = POP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002064 PyObject *tb = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002065 PyErr_Restore(exc, val, tb);
2066 goto exception_unwind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002068 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002069
Benjamin Petersonddd19492018-09-16 22:38:02 -07002070 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002071 PyObject *exc = POP();
2072 assert(PyExceptionClass_Check(exc));
2073 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2074 PyTryBlock *b = PyFrame_BlockPop(f);
2075 assert(b->b_type == EXCEPT_HANDLER);
2076 Py_DECREF(exc);
2077 UNWIND_EXCEPT_HANDLER(b);
2078 Py_DECREF(POP());
2079 JUMPBY(oparg);
2080 FAST_DISPATCH();
2081 }
2082 else {
2083 PyObject *val = POP();
2084 PyObject *tb = POP();
2085 PyErr_Restore(exc, val, tb);
2086 goto exception_unwind;
2087 }
2088 }
2089
Benjamin Petersonddd19492018-09-16 22:38:02 -07002090 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002091 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002092
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002093 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002094 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002095 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002096 if (bc == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002097 if (!PyErr_Occurred()) {
2098 PyErr_SetString(PyExc_NameError,
2099 "__build_class__ not found");
2100 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002101 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002102 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002103 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002104 }
2105 else {
2106 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2107 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002108 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002109 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2110 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002111 if (PyErr_ExceptionMatches(PyExc_KeyError))
2112 PyErr_SetString(PyExc_NameError,
2113 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002114 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002115 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002116 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002117 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002118 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002119 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002120
Benjamin Petersonddd19492018-09-16 22:38:02 -07002121 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002122 PyObject *name = GETITEM(names, oparg);
2123 PyObject *v = POP();
2124 PyObject *ns = f->f_locals;
2125 int err;
2126 if (ns == NULL) {
2127 PyErr_Format(PyExc_SystemError,
2128 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002129 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002130 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002131 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002132 if (PyDict_CheckExact(ns))
2133 err = PyDict_SetItem(ns, name, v);
2134 else
2135 err = PyObject_SetItem(ns, name, v);
2136 Py_DECREF(v);
2137 if (err != 0)
2138 goto error;
2139 DISPATCH();
2140 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002141
Benjamin Petersonddd19492018-09-16 22:38:02 -07002142 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002143 PyObject *name = GETITEM(names, oparg);
2144 PyObject *ns = f->f_locals;
2145 int err;
2146 if (ns == NULL) {
2147 PyErr_Format(PyExc_SystemError,
2148 "no locals when deleting %R", name);
2149 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002150 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002151 err = PyObject_DelItem(ns, name);
2152 if (err != 0) {
2153 format_exc_check_arg(PyExc_NameError,
2154 NAME_ERROR_MSG,
2155 name);
2156 goto error;
2157 }
2158 DISPATCH();
2159 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002160
Benjamin Petersonddd19492018-09-16 22:38:02 -07002161 case TARGET(UNPACK_SEQUENCE): {
2162 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002163 PyObject *seq = POP(), *item, **items;
2164 if (PyTuple_CheckExact(seq) &&
2165 PyTuple_GET_SIZE(seq) == oparg) {
2166 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002167 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002168 item = items[oparg];
2169 Py_INCREF(item);
2170 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002171 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002172 } else if (PyList_CheckExact(seq) &&
2173 PyList_GET_SIZE(seq) == oparg) {
2174 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002176 item = items[oparg];
2177 Py_INCREF(item);
2178 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002180 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002182 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002183 } else {
2184 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002185 Py_DECREF(seq);
2186 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002187 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002188 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002189 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002190 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002191
Benjamin Petersonddd19492018-09-16 22:38:02 -07002192 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002193 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2194 PyObject *seq = POP();
2195
2196 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2197 stack_pointer + totalargs)) {
2198 stack_pointer += totalargs;
2199 } else {
2200 Py_DECREF(seq);
2201 goto error;
2202 }
2203 Py_DECREF(seq);
2204 DISPATCH();
2205 }
2206
Benjamin Petersonddd19492018-09-16 22:38:02 -07002207 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002208 PyObject *name = GETITEM(names, oparg);
2209 PyObject *owner = TOP();
2210 PyObject *v = SECOND();
2211 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002212 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002213 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002214 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002215 Py_DECREF(owner);
2216 if (err != 0)
2217 goto error;
2218 DISPATCH();
2219 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002220
Benjamin Petersonddd19492018-09-16 22:38:02 -07002221 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002222 PyObject *name = GETITEM(names, oparg);
2223 PyObject *owner = POP();
2224 int err;
2225 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2226 Py_DECREF(owner);
2227 if (err != 0)
2228 goto error;
2229 DISPATCH();
2230 }
2231
Benjamin Petersonddd19492018-09-16 22:38:02 -07002232 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002233 PyObject *name = GETITEM(names, oparg);
2234 PyObject *v = POP();
2235 int err;
2236 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002237 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002238 if (err != 0)
2239 goto error;
2240 DISPATCH();
2241 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002242
Benjamin Petersonddd19492018-09-16 22:38:02 -07002243 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002244 PyObject *name = GETITEM(names, oparg);
2245 int err;
2246 err = PyDict_DelItem(f->f_globals, name);
2247 if (err != 0) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002248 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2249 format_exc_check_arg(
2250 PyExc_NameError, NAME_ERROR_MSG, name);
2251 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002252 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002253 }
2254 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002255 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002256
Benjamin Petersonddd19492018-09-16 22:38:02 -07002257 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002258 PyObject *name = GETITEM(names, oparg);
2259 PyObject *locals = f->f_locals;
2260 PyObject *v;
2261 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002263 "no locals when loading %R", name);
2264 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002266 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002267 v = PyDict_GetItemWithError(locals, name);
2268 if (v != NULL) {
2269 Py_INCREF(v);
2270 }
2271 else if (PyErr_Occurred()) {
2272 goto error;
2273 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002274 }
2275 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002276 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002277 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002278 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2279 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002280 PyErr_Clear();
2281 }
2282 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002283 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002284 v = PyDict_GetItemWithError(f->f_globals, name);
2285 if (v != NULL) {
2286 Py_INCREF(v);
2287 }
2288 else if (PyErr_Occurred()) {
2289 goto error;
2290 }
2291 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002292 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002293 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002294 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002295 if (!PyErr_Occurred()) {
2296 format_exc_check_arg(
Victor Stinnerb0b22422012-04-19 00:57:45 +02002297 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002298 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002299 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002300 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002301 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002302 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002303 }
2304 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002305 v = PyObject_GetItem(f->f_builtins, name);
2306 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002307 if (PyErr_ExceptionMatches(PyExc_KeyError))
2308 format_exc_check_arg(
2309 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002310 NAME_ERROR_MSG, name);
2311 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002312 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002313 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002314 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002316 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002318 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002319
Benjamin Petersonddd19492018-09-16 22:38:02 -07002320 case TARGET(LOAD_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002321 PyObject *name = GETITEM(names, oparg);
2322 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002323 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002324 && PyDict_CheckExact(f->f_builtins))
2325 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002326 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002327 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002328 name);
2329 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002330 if (!_PyErr_OCCURRED()) {
2331 /* _PyDict_LoadGlobal() returns NULL without raising
2332 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002333 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002334 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002335 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002336 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002337 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002338 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002339 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002340 else {
2341 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002342
2343 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002344 v = PyObject_GetItem(f->f_globals, name);
2345 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002346 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2347 goto error;
2348 PyErr_Clear();
2349
Victor Stinnerb4efc962015-11-20 09:24:02 +01002350 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002351 v = PyObject_GetItem(f->f_builtins, name);
2352 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002353 if (PyErr_ExceptionMatches(PyExc_KeyError))
2354 format_exc_check_arg(
2355 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002356 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002357 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002358 }
2359 }
2360 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002361 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002363 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002364
Benjamin Petersonddd19492018-09-16 22:38:02 -07002365 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002366 PyObject *v = GETLOCAL(oparg);
2367 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 SETLOCAL(oparg, NULL);
2369 DISPATCH();
2370 }
2371 format_exc_check_arg(
2372 PyExc_UnboundLocalError,
2373 UNBOUNDLOCAL_ERROR_MSG,
2374 PyTuple_GetItem(co->co_varnames, oparg)
2375 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002376 goto error;
2377 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002378
Benjamin Petersonddd19492018-09-16 22:38:02 -07002379 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002380 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002381 PyObject *oldobj = PyCell_GET(cell);
2382 if (oldobj != NULL) {
2383 PyCell_SET(cell, NULL);
2384 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002385 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002386 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002387 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002388 goto error;
2389 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002390
Benjamin Petersonddd19492018-09-16 22:38:02 -07002391 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002392 PyObject *cell = freevars[oparg];
2393 Py_INCREF(cell);
2394 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002396 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002397
Benjamin Petersonddd19492018-09-16 22:38:02 -07002398 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002399 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002400 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002401 assert(locals);
2402 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2403 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2404 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2405 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2406 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002407 value = PyDict_GetItemWithError(locals, name);
2408 if (value != NULL) {
2409 Py_INCREF(value);
2410 }
2411 else if (PyErr_Occurred()) {
2412 goto error;
2413 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002414 }
2415 else {
2416 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002417 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002418 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2419 goto error;
2420 PyErr_Clear();
2421 }
2422 }
2423 if (!value) {
2424 PyObject *cell = freevars[oparg];
2425 value = PyCell_GET(cell);
2426 if (value == NULL) {
2427 format_exc_unbound(co, oparg);
2428 goto error;
2429 }
2430 Py_INCREF(value);
2431 }
2432 PUSH(value);
2433 DISPATCH();
2434 }
2435
Benjamin Petersonddd19492018-09-16 22:38:02 -07002436 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002437 PyObject *cell = freevars[oparg];
2438 PyObject *value = PyCell_GET(cell);
2439 if (value == NULL) {
2440 format_exc_unbound(co, oparg);
2441 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002442 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002443 Py_INCREF(value);
2444 PUSH(value);
2445 DISPATCH();
2446 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002447
Benjamin Petersonddd19492018-09-16 22:38:02 -07002448 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002449 PyObject *v = POP();
2450 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002451 PyObject *oldobj = PyCell_GET(cell);
2452 PyCell_SET(cell, v);
2453 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002454 DISPATCH();
2455 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002456
Benjamin Petersonddd19492018-09-16 22:38:02 -07002457 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002458 PyObject *str;
2459 PyObject *empty = PyUnicode_New(0, 0);
2460 if (empty == NULL) {
2461 goto error;
2462 }
2463 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2464 Py_DECREF(empty);
2465 if (str == NULL)
2466 goto error;
2467 while (--oparg >= 0) {
2468 PyObject *item = POP();
2469 Py_DECREF(item);
2470 }
2471 PUSH(str);
2472 DISPATCH();
2473 }
2474
Benjamin Petersonddd19492018-09-16 22:38:02 -07002475 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002476 PyObject *tup = PyTuple_New(oparg);
2477 if (tup == NULL)
2478 goto error;
2479 while (--oparg >= 0) {
2480 PyObject *item = POP();
2481 PyTuple_SET_ITEM(tup, oparg, item);
2482 }
2483 PUSH(tup);
2484 DISPATCH();
2485 }
2486
Benjamin Petersonddd19492018-09-16 22:38:02 -07002487 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002488 PyObject *list = PyList_New(oparg);
2489 if (list == NULL)
2490 goto error;
2491 while (--oparg >= 0) {
2492 PyObject *item = POP();
2493 PyList_SET_ITEM(list, oparg, item);
2494 }
2495 PUSH(list);
2496 DISPATCH();
2497 }
2498
Benjamin Petersonddd19492018-09-16 22:38:02 -07002499 case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL):
2500 case TARGET(BUILD_TUPLE_UNPACK):
2501 case TARGET(BUILD_LIST_UNPACK): {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002502 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002503 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002504 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002505 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002506
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002507 if (sum == NULL)
2508 goto error;
2509
2510 for (i = oparg; i > 0; i--) {
2511 PyObject *none_val;
2512
2513 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2514 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002515 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002516 PyErr_ExceptionMatches(PyExc_TypeError))
2517 {
2518 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002519 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002520 Py_DECREF(sum);
2521 goto error;
2522 }
2523 Py_DECREF(none_val);
2524 }
2525
2526 if (convert_to_tuple) {
2527 return_value = PyList_AsTuple(sum);
2528 Py_DECREF(sum);
2529 if (return_value == NULL)
2530 goto error;
2531 }
2532 else {
2533 return_value = sum;
2534 }
2535
2536 while (oparg--)
2537 Py_DECREF(POP());
2538 PUSH(return_value);
2539 DISPATCH();
2540 }
2541
Benjamin Petersonddd19492018-09-16 22:38:02 -07002542 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002543 PyObject *set = PySet_New(NULL);
2544 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002545 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002546 if (set == NULL)
2547 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002548 for (i = oparg; i > 0; i--) {
2549 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002550 if (err == 0)
2551 err = PySet_Add(set, item);
2552 Py_DECREF(item);
2553 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002554 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002555 if (err != 0) {
2556 Py_DECREF(set);
2557 goto error;
2558 }
2559 PUSH(set);
2560 DISPATCH();
2561 }
2562
Benjamin Petersonddd19492018-09-16 22:38:02 -07002563 case TARGET(BUILD_SET_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002564 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002565 PyObject *sum = PySet_New(NULL);
2566 if (sum == NULL)
2567 goto error;
2568
2569 for (i = oparg; i > 0; i--) {
2570 if (_PySet_Update(sum, PEEK(i)) < 0) {
2571 Py_DECREF(sum);
2572 goto error;
2573 }
2574 }
2575
2576 while (oparg--)
2577 Py_DECREF(POP());
2578 PUSH(sum);
2579 DISPATCH();
2580 }
2581
Benjamin Petersonddd19492018-09-16 22:38:02 -07002582 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002583 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002584 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2585 if (map == NULL)
2586 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002587 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002588 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002589 PyObject *key = PEEK(2*i);
2590 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002591 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002592 if (err != 0) {
2593 Py_DECREF(map);
2594 goto error;
2595 }
2596 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002597
2598 while (oparg--) {
2599 Py_DECREF(POP());
2600 Py_DECREF(POP());
2601 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002602 PUSH(map);
2603 DISPATCH();
2604 }
2605
Benjamin Petersonddd19492018-09-16 22:38:02 -07002606 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002607 _Py_IDENTIFIER(__annotations__);
2608 int err;
2609 PyObject *ann_dict;
2610 if (f->f_locals == NULL) {
2611 PyErr_Format(PyExc_SystemError,
2612 "no locals found when setting up annotations");
2613 goto error;
2614 }
2615 /* check if __annotations__ in locals()... */
2616 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002617 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002618 &PyId___annotations__);
2619 if (ann_dict == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002620 if (PyErr_Occurred()) {
2621 goto error;
2622 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002623 /* ...if not, create a new one */
2624 ann_dict = PyDict_New();
2625 if (ann_dict == NULL) {
2626 goto error;
2627 }
2628 err = _PyDict_SetItemId(f->f_locals,
2629 &PyId___annotations__, ann_dict);
2630 Py_DECREF(ann_dict);
2631 if (err != 0) {
2632 goto error;
2633 }
2634 }
2635 }
2636 else {
2637 /* do the same if locals() is not a dict */
2638 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2639 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002640 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002641 }
2642 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2643 if (ann_dict == NULL) {
2644 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2645 goto error;
2646 }
2647 PyErr_Clear();
2648 ann_dict = PyDict_New();
2649 if (ann_dict == NULL) {
2650 goto error;
2651 }
2652 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2653 Py_DECREF(ann_dict);
2654 if (err != 0) {
2655 goto error;
2656 }
2657 }
2658 else {
2659 Py_DECREF(ann_dict);
2660 }
2661 }
2662 DISPATCH();
2663 }
2664
Benjamin Petersonddd19492018-09-16 22:38:02 -07002665 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002666 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002667 PyObject *map;
2668 PyObject *keys = TOP();
2669 if (!PyTuple_CheckExact(keys) ||
2670 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2671 PyErr_SetString(PyExc_SystemError,
2672 "bad BUILD_CONST_KEY_MAP keys argument");
2673 goto error;
2674 }
2675 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2676 if (map == NULL) {
2677 goto error;
2678 }
2679 for (i = oparg; i > 0; i--) {
2680 int err;
2681 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2682 PyObject *value = PEEK(i + 1);
2683 err = PyDict_SetItem(map, key, value);
2684 if (err != 0) {
2685 Py_DECREF(map);
2686 goto error;
2687 }
2688 }
2689
2690 Py_DECREF(POP());
2691 while (oparg--) {
2692 Py_DECREF(POP());
2693 }
2694 PUSH(map);
2695 DISPATCH();
2696 }
2697
Benjamin Petersonddd19492018-09-16 22:38:02 -07002698 case TARGET(BUILD_MAP_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002699 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002700 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002701 if (sum == NULL)
2702 goto error;
2703
2704 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002705 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002706 if (PyDict_Update(sum, arg) < 0) {
2707 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2708 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002709 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002710 arg->ob_type->tp_name);
2711 }
2712 Py_DECREF(sum);
2713 goto error;
2714 }
2715 }
2716
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002717 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002718 Py_DECREF(POP());
2719 PUSH(sum);
2720 DISPATCH();
2721 }
2722
Benjamin Petersonddd19492018-09-16 22:38:02 -07002723 case TARGET(BUILD_MAP_UNPACK_WITH_CALL): {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002724 Py_ssize_t i;
2725 PyObject *sum = PyDict_New();
2726 if (sum == NULL)
2727 goto error;
2728
2729 for (i = oparg; i > 0; i--) {
2730 PyObject *arg = PEEK(i);
2731 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002732 Py_DECREF(sum);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02002733 format_kwargs_error(PEEK(2 + oparg), arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002734 goto error;
2735 }
2736 }
2737
2738 while (oparg--)
2739 Py_DECREF(POP());
2740 PUSH(sum);
2741 DISPATCH();
2742 }
2743
Benjamin Petersonddd19492018-09-16 22:38:02 -07002744 case TARGET(MAP_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002745 PyObject *key = TOP();
2746 PyObject *value = SECOND();
2747 PyObject *map;
2748 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002749 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002750 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002751 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002752 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002753 Py_DECREF(value);
2754 Py_DECREF(key);
2755 if (err != 0)
2756 goto error;
2757 PREDICT(JUMP_ABSOLUTE);
2758 DISPATCH();
2759 }
2760
Benjamin Petersonddd19492018-09-16 22:38:02 -07002761 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002762 PyObject *name = GETITEM(names, oparg);
2763 PyObject *owner = TOP();
2764 PyObject *res = PyObject_GetAttr(owner, name);
2765 Py_DECREF(owner);
2766 SET_TOP(res);
2767 if (res == NULL)
2768 goto error;
2769 DISPATCH();
2770 }
2771
Benjamin Petersonddd19492018-09-16 22:38:02 -07002772 case TARGET(COMPARE_OP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002773 PyObject *right = POP();
2774 PyObject *left = TOP();
2775 PyObject *res = cmp_outcome(oparg, left, right);
2776 Py_DECREF(left);
2777 Py_DECREF(right);
2778 SET_TOP(res);
2779 if (res == NULL)
2780 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002781 PREDICT(POP_JUMP_IF_FALSE);
2782 PREDICT(POP_JUMP_IF_TRUE);
2783 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002784 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002785
Benjamin Petersonddd19492018-09-16 22:38:02 -07002786 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002787 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002788 PyObject *fromlist = POP();
2789 PyObject *level = TOP();
2790 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002791 res = import_name(f, name, fromlist, level);
2792 Py_DECREF(level);
2793 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002794 SET_TOP(res);
2795 if (res == NULL)
2796 goto error;
2797 DISPATCH();
2798 }
2799
Benjamin Petersonddd19492018-09-16 22:38:02 -07002800 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002801 PyObject *from = POP(), *locals;
2802 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002803 if (PyFrame_FastToLocalsWithError(f) < 0) {
2804 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002805 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002806 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002807
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002808 locals = f->f_locals;
2809 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002810 PyErr_SetString(PyExc_SystemError,
2811 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002812 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002813 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002815 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002816 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002817 Py_DECREF(from);
2818 if (err != 0)
2819 goto error;
2820 DISPATCH();
2821 }
Guido van Rossum25831651993-05-19 14:50:45 +00002822
Benjamin Petersonddd19492018-09-16 22:38:02 -07002823 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002824 PyObject *name = GETITEM(names, oparg);
2825 PyObject *from = TOP();
2826 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002827 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002828 PUSH(res);
2829 if (res == NULL)
2830 goto error;
2831 DISPATCH();
2832 }
Thomas Wouters52152252000-08-17 22:55:00 +00002833
Benjamin Petersonddd19492018-09-16 22:38:02 -07002834 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002835 JUMPBY(oparg);
2836 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002837 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002838
Benjamin Petersonddd19492018-09-16 22:38:02 -07002839 case TARGET(POP_JUMP_IF_FALSE): {
2840 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002841 PyObject *cond = POP();
2842 int err;
2843 if (cond == Py_True) {
2844 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002845 FAST_DISPATCH();
2846 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002847 if (cond == Py_False) {
2848 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002849 JUMPTO(oparg);
2850 FAST_DISPATCH();
2851 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002852 err = PyObject_IsTrue(cond);
2853 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002854 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002855 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002856 else if (err == 0)
2857 JUMPTO(oparg);
2858 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002859 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002860 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002861 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002862
Benjamin Petersonddd19492018-09-16 22:38:02 -07002863 case TARGET(POP_JUMP_IF_TRUE): {
2864 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002865 PyObject *cond = POP();
2866 int err;
2867 if (cond == Py_False) {
2868 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 FAST_DISPATCH();
2870 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002871 if (cond == Py_True) {
2872 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002873 JUMPTO(oparg);
2874 FAST_DISPATCH();
2875 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002876 err = PyObject_IsTrue(cond);
2877 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 JUMPTO(oparg);
2880 }
2881 else if (err == 0)
2882 ;
2883 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002884 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002885 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002886 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002887
Benjamin Petersonddd19492018-09-16 22:38:02 -07002888 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002889 PyObject *cond = TOP();
2890 int err;
2891 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002892 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002893 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002894 FAST_DISPATCH();
2895 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002896 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002897 JUMPTO(oparg);
2898 FAST_DISPATCH();
2899 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002900 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002902 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002903 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002904 }
2905 else if (err == 0)
2906 JUMPTO(oparg);
2907 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002908 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002909 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002910 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002911
Benjamin Petersonddd19492018-09-16 22:38:02 -07002912 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002913 PyObject *cond = TOP();
2914 int err;
2915 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002916 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002917 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002918 FAST_DISPATCH();
2919 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002920 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002921 JUMPTO(oparg);
2922 FAST_DISPATCH();
2923 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002924 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002926 JUMPTO(oparg);
2927 }
2928 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002929 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002930 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002931 }
2932 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002933 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002934 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002935 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002936
Benjamin Petersonddd19492018-09-16 22:38:02 -07002937 case TARGET(JUMP_ABSOLUTE): {
2938 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002939 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002940#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002941 /* Enabling this path speeds-up all while and for-loops by bypassing
2942 the per-loop checks for signals. By default, this should be turned-off
2943 because it prevents detection of a control-break in tight loops like
2944 "while 1: pass". Compile with this option turned-on when you need
2945 the speed-up and do not need break checking inside tight loops (ones
2946 that contain only instructions ending with FAST_DISPATCH).
2947 */
2948 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002949#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002950 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002951#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002952 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002953
Benjamin Petersonddd19492018-09-16 22:38:02 -07002954 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002955 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002956 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002957 PyObject *iter = PyObject_GetIter(iterable);
2958 Py_DECREF(iterable);
2959 SET_TOP(iter);
2960 if (iter == NULL)
2961 goto error;
2962 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002963 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002964 DISPATCH();
2965 }
2966
Benjamin Petersonddd19492018-09-16 22:38:02 -07002967 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04002968 /* before: [obj]; after [getiter(obj)] */
2969 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002970 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002971 if (PyCoro_CheckExact(iterable)) {
2972 /* `iterable` is a coroutine */
2973 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2974 /* and it is used in a 'yield from' expression of a
2975 regular generator. */
2976 Py_DECREF(iterable);
2977 SET_TOP(NULL);
2978 PyErr_SetString(PyExc_TypeError,
2979 "cannot 'yield from' a coroutine object "
2980 "in a non-coroutine generator");
2981 goto error;
2982 }
2983 }
2984 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002985 /* `iterable` is not a generator. */
2986 iter = PyObject_GetIter(iterable);
2987 Py_DECREF(iterable);
2988 SET_TOP(iter);
2989 if (iter == NULL)
2990 goto error;
2991 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002992 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002993 DISPATCH();
2994 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002995
Benjamin Petersonddd19492018-09-16 22:38:02 -07002996 case TARGET(FOR_ITER): {
2997 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002998 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002999 PyObject *iter = TOP();
3000 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3001 if (next != NULL) {
3002 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003003 PREDICT(STORE_FAST);
3004 PREDICT(UNPACK_SEQUENCE);
3005 DISPATCH();
3006 }
3007 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003008 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
3009 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08003010 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003011 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003012 PyErr_Clear();
3013 }
3014 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003015 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003016 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003017 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003018 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003019 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003020 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003021
Benjamin Petersonddd19492018-09-16 22:38:02 -07003022 case TARGET(SETUP_FINALLY): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003023 /* NOTE: If you add any new block-setup opcodes that
3024 are not try/except/finally handlers, you may need
3025 to update the PyGen_NeedsFinalizing() function.
3026 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003027
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003028 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003029 STACK_LEVEL());
3030 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003031 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003032
Benjamin Petersonddd19492018-09-16 22:38:02 -07003033 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003034 _Py_IDENTIFIER(__aexit__);
3035 _Py_IDENTIFIER(__aenter__);
3036
3037 PyObject *mgr = TOP();
3038 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3039 *enter;
3040 PyObject *res;
3041 if (exit == NULL)
3042 goto error;
3043 SET_TOP(exit);
3044 enter = special_lookup(mgr, &PyId___aenter__);
3045 Py_DECREF(mgr);
3046 if (enter == NULL)
3047 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003048 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003049 Py_DECREF(enter);
3050 if (res == NULL)
3051 goto error;
3052 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003053 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003054 DISPATCH();
3055 }
3056
Benjamin Petersonddd19492018-09-16 22:38:02 -07003057 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003058 PyObject *res = POP();
3059 /* Setup the finally block before pushing the result
3060 of __aenter__ on the stack. */
3061 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3062 STACK_LEVEL());
3063 PUSH(res);
3064 DISPATCH();
3065 }
3066
Benjamin Petersonddd19492018-09-16 22:38:02 -07003067 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003068 _Py_IDENTIFIER(__exit__);
3069 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003070 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08003071 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003072 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08003073 if (enter == NULL)
3074 goto error;
3075 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003076 if (exit == NULL) {
3077 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003078 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003079 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003080 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003081 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003082 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003083 Py_DECREF(enter);
3084 if (res == NULL)
3085 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003086 /* Setup the finally block before pushing the result
3087 of __enter__ on the stack. */
3088 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3089 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003090
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003091 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003092 DISPATCH();
3093 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003094
Benjamin Petersonddd19492018-09-16 22:38:02 -07003095 case TARGET(WITH_CLEANUP_START): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003096 /* At the top of the stack are 1 or 6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003097 how/why we entered the finally clause:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003098 - TOP = NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003099 - (TOP, SECOND, THIRD) = exc_info()
3100 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003101 Below them is EXIT, the context.__exit__ or context.__aexit__
3102 bound method.
3103 In the first case, we must call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003104 EXIT(None, None, None)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003105 otherwise we must call
3106 EXIT(TOP, SECOND, THIRD)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003107
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003108 In the first case, we remove EXIT from the
3109 stack, leaving TOP, and push TOP on the stack.
3110 Otherwise we shift the bottom 3 values of the
3111 stack down, replace the empty spot with NULL, and push
3112 None on the stack.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003113
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003114 Finally we push the result of the call.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003115 */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003116 PyObject *stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003117 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003118 PyObject *exc, *val, *tb, *res;
3119
3120 val = tb = Py_None;
3121 exc = TOP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003122 if (exc == NULL) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003123 STACK_SHRINK(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003124 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003125 SET_TOP(exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003126 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003127 }
3128 else {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003129 assert(PyExceptionClass_Check(exc));
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003130 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003131 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003132 val = SECOND();
3133 tb = THIRD();
3134 tp2 = FOURTH();
3135 exc2 = PEEK(5);
3136 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003137 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003138 SET_VALUE(7, tb2);
3139 SET_VALUE(6, exc2);
3140 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003141 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3142 SET_FOURTH(NULL);
3143 /* We just shifted the stack down, so we have
3144 to tell the except handler block that the
3145 values are lower than it expects. */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003146 assert(f->f_iblock > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003147 block = &f->f_blockstack[f->f_iblock - 1];
3148 assert(block->b_type == EXCEPT_HANDLER);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003149 assert(block->b_level > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003150 block->b_level--;
3151 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003152
3153 stack[0] = exc;
3154 stack[1] = val;
3155 stack[2] = tb;
3156 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003157 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003158 if (res == NULL)
3159 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003160
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003161 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003162 PUSH(exc);
3163 PUSH(res);
3164 PREDICT(WITH_CLEANUP_FINISH);
3165 DISPATCH();
3166 }
3167
Benjamin Petersonddd19492018-09-16 22:38:02 -07003168 case TARGET(WITH_CLEANUP_FINISH): {
3169 PREDICTED(WITH_CLEANUP_FINISH);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003170 /* TOP = the result of calling the context.__exit__ bound method
3171 SECOND = either None or exception type
3172
3173 If SECOND is None below is NULL or the return address,
3174 otherwise below are 7 values representing an exception.
3175 */
Yury Selivanov75445082015-05-11 22:57:16 -04003176 PyObject *res = POP();
3177 PyObject *exc = POP();
3178 int err;
3179
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003180 if (exc != Py_None)
3181 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003182 else
3183 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003184
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003185 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003186 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003188 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003189 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003190 else if (err > 0) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003191 /* There was an exception and a True return.
3192 * We must manually unwind the EXCEPT_HANDLER block
3193 * which was created when the exception was caught,
Quan Tian3bd0d622018-10-20 05:30:03 +08003194 * otherwise the stack will be in an inconsistent state.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003195 */
3196 PyTryBlock *b = PyFrame_BlockPop(f);
3197 assert(b->b_type == EXCEPT_HANDLER);
3198 UNWIND_EXCEPT_HANDLER(b);
3199 PUSH(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003200 }
3201 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003202 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003203 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003204
Benjamin Petersonddd19492018-09-16 22:38:02 -07003205 case TARGET(LOAD_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003206 /* Designed to work in tamdem with CALL_METHOD. */
3207 PyObject *name = GETITEM(names, oparg);
3208 PyObject *obj = TOP();
3209 PyObject *meth = NULL;
3210
3211 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3212
Yury Selivanovf2392132016-12-13 19:03:51 -05003213 if (meth == NULL) {
3214 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003215 goto error;
3216 }
3217
3218 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003219 /* We can bypass temporary bound method object.
3220 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003221
INADA Naoki015bce62017-01-16 17:23:30 +09003222 meth | self | arg1 | ... | argN
3223 */
3224 SET_TOP(meth);
3225 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003226 }
3227 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003228 /* meth is not an unbound method (but a regular attr, or
3229 something was returned by a descriptor protocol). Set
3230 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003231 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003232
3233 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003234 */
INADA Naoki015bce62017-01-16 17:23:30 +09003235 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003236 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003237 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003238 }
3239 DISPATCH();
3240 }
3241
Benjamin Petersonddd19492018-09-16 22:38:02 -07003242 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003243 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003244 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003245
3246 sp = stack_pointer;
3247
INADA Naoki015bce62017-01-16 17:23:30 +09003248 meth = PEEK(oparg + 2);
3249 if (meth == NULL) {
3250 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3251 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003252
3253 Stack layout:
3254
INADA Naoki015bce62017-01-16 17:23:30 +09003255 ... | NULL | callable | arg1 | ... | argN
3256 ^- TOP()
3257 ^- (-oparg)
3258 ^- (-oparg-1)
3259 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003260
Ville Skyttä49b27342017-08-03 09:00:59 +03003261 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003262 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003263 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003264 res = call_function(&sp, oparg, NULL);
3265 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003266 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003267 }
3268 else {
3269 /* This is a method call. Stack layout:
3270
INADA Naoki015bce62017-01-16 17:23:30 +09003271 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003272 ^- TOP()
3273 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003274 ^- (-oparg-1)
3275 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003276
INADA Naoki015bce62017-01-16 17:23:30 +09003277 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003278 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003279 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003280 */
3281 res = call_function(&sp, oparg + 1, NULL);
3282 stack_pointer = sp;
3283 }
3284
3285 PUSH(res);
3286 if (res == NULL)
3287 goto error;
3288 DISPATCH();
3289 }
3290
Benjamin Petersonddd19492018-09-16 22:38:02 -07003291 case TARGET(CALL_FUNCTION): {
3292 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003293 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003294 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003295 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003296 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003297 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003298 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003299 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003300 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003301 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003302 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003303
Benjamin Petersonddd19492018-09-16 22:38:02 -07003304 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003305 PyObject **sp, *res, *names;
3306
3307 names = POP();
3308 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003309 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003310 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003311 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003312 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003313 Py_DECREF(names);
3314
3315 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003316 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003317 }
3318 DISPATCH();
3319 }
3320
Benjamin Petersonddd19492018-09-16 22:38:02 -07003321 case TARGET(CALL_FUNCTION_EX): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003322 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003323 if (oparg & 0x01) {
3324 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003325 if (!PyDict_CheckExact(kwargs)) {
3326 PyObject *d = PyDict_New();
3327 if (d == NULL)
3328 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003329 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003330 Py_DECREF(d);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003331 format_kwargs_error(SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003332 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003333 goto error;
3334 }
3335 Py_DECREF(kwargs);
3336 kwargs = d;
3337 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003338 assert(PyDict_CheckExact(kwargs));
3339 }
3340 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003341 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003342 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003343 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003344 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003345 goto error;
3346 }
3347 Py_SETREF(callargs, PySequence_Tuple(callargs));
3348 if (callargs == NULL) {
3349 goto error;
3350 }
3351 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003352 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003353
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003354 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003355 Py_DECREF(func);
3356 Py_DECREF(callargs);
3357 Py_XDECREF(kwargs);
3358
3359 SET_TOP(result);
3360 if (result == NULL) {
3361 goto error;
3362 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003363 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003364 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003365
Benjamin Petersonddd19492018-09-16 22:38:02 -07003366 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003367 PyObject *qualname = POP();
3368 PyObject *codeobj = POP();
3369 PyFunctionObject *func = (PyFunctionObject *)
3370 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003371
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003372 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003373 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003374 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003375 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003376 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003377
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003378 if (oparg & 0x08) {
3379 assert(PyTuple_CheckExact(TOP()));
3380 func ->func_closure = POP();
3381 }
3382 if (oparg & 0x04) {
3383 assert(PyDict_CheckExact(TOP()));
3384 func->func_annotations = POP();
3385 }
3386 if (oparg & 0x02) {
3387 assert(PyDict_CheckExact(TOP()));
3388 func->func_kwdefaults = POP();
3389 }
3390 if (oparg & 0x01) {
3391 assert(PyTuple_CheckExact(TOP()));
3392 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003393 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003394
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003395 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003396 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003397 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003398
Benjamin Petersonddd19492018-09-16 22:38:02 -07003399 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003400 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003401 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003402 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003403 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003404 step = NULL;
3405 stop = POP();
3406 start = TOP();
3407 slice = PySlice_New(start, stop, step);
3408 Py_DECREF(start);
3409 Py_DECREF(stop);
3410 Py_XDECREF(step);
3411 SET_TOP(slice);
3412 if (slice == NULL)
3413 goto error;
3414 DISPATCH();
3415 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003416
Benjamin Petersonddd19492018-09-16 22:38:02 -07003417 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003418 /* Handles f-string value formatting. */
3419 PyObject *result;
3420 PyObject *fmt_spec;
3421 PyObject *value;
3422 PyObject *(*conv_fn)(PyObject *);
3423 int which_conversion = oparg & FVC_MASK;
3424 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3425
3426 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003427 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003428
3429 /* See if any conversion is specified. */
3430 switch (which_conversion) {
3431 case FVC_STR: conv_fn = PyObject_Str; break;
3432 case FVC_REPR: conv_fn = PyObject_Repr; break;
3433 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3434
3435 /* Must be 0 (meaning no conversion), since only four
3436 values are allowed by (oparg & FVC_MASK). */
3437 default: conv_fn = NULL; break;
3438 }
3439
3440 /* If there's a conversion function, call it and replace
3441 value with that result. Otherwise, just use value,
3442 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003443 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003444 result = conv_fn(value);
3445 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003446 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003447 Py_XDECREF(fmt_spec);
3448 goto error;
3449 }
3450 value = result;
3451 }
3452
3453 /* If value is a unicode object, and there's no fmt_spec,
3454 then we know the result of format(value) is value
3455 itself. In that case, skip calling format(). I plan to
3456 move this optimization in to PyObject_Format()
3457 itself. */
3458 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3459 /* Do nothing, just transfer ownership to result. */
3460 result = value;
3461 } else {
3462 /* Actually call format(). */
3463 result = PyObject_Format(value, fmt_spec);
3464 Py_DECREF(value);
3465 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003466 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003467 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003468 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003469 }
3470
Eric V. Smith135d5f42016-02-05 18:23:08 -05003471 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003472 DISPATCH();
3473 }
3474
Benjamin Petersonddd19492018-09-16 22:38:02 -07003475 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003476 int oldoparg = oparg;
3477 NEXTOPARG();
3478 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003479 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003480 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003481
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003482
Antoine Pitrou042b1282010-08-13 21:15:58 +00003483#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003484 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003485#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003486 default:
3487 fprintf(stderr,
3488 "XXX lineno: %d, opcode: %d\n",
3489 PyFrame_GetLineNumber(f),
3490 opcode);
3491 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003492 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003494 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003495
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003496 /* This should never be reached. Every opcode should end with DISPATCH()
3497 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003498 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003499
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003500error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003501 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003502#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003503 if (!PyErr_Occurred())
3504 PyErr_SetString(PyExc_SystemError,
3505 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003506#else
3507 assert(PyErr_Occurred());
3508#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003509
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003510 /* Log traceback info. */
3511 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003512
Benjamin Peterson51f46162013-01-23 08:38:47 -05003513 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003514 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3515 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003516
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003517exception_unwind:
3518 /* Unwind stacks if an exception occurred */
3519 while (f->f_iblock > 0) {
3520 /* Pop the current block. */
3521 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003523 if (b->b_type == EXCEPT_HANDLER) {
3524 UNWIND_EXCEPT_HANDLER(b);
3525 continue;
3526 }
3527 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003528 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003529 PyObject *exc, *val, *tb;
3530 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003531 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003532 /* Beware, this invalidates all b->b_* fields */
3533 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003534 PUSH(exc_info->exc_traceback);
3535 PUSH(exc_info->exc_value);
3536 if (exc_info->exc_type != NULL) {
3537 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003538 }
3539 else {
3540 Py_INCREF(Py_None);
3541 PUSH(Py_None);
3542 }
3543 PyErr_Fetch(&exc, &val, &tb);
3544 /* Make the raw exception data
3545 available to the handler,
3546 so a program can emulate the
3547 Python main loop. */
3548 PyErr_NormalizeException(
3549 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003550 if (tb != NULL)
3551 PyException_SetTraceback(val, tb);
3552 else
3553 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003554 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003555 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003556 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003557 exc_info->exc_value = val;
3558 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003559 if (tb == NULL)
3560 tb = Py_None;
3561 Py_INCREF(tb);
3562 PUSH(tb);
3563 PUSH(val);
3564 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003565 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003566 /* Resume normal execution */
3567 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003568 }
3569 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003570
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003571 /* End the loop as we still have an error */
3572 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003573 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003575 /* Pop remaining stack entries. */
3576 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003577 PyObject *o = POP();
3578 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003579 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003580
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003581 assert(retval == NULL);
3582 assert(PyErr_Occurred());
Guido van Rossumac7be682001-01-17 15:42:30 +00003583
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003584return_or_yield:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003585 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003586 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003587 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3588 tstate, f, PyTrace_RETURN, retval)) {
3589 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003590 }
3591 }
3592 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003593 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3594 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003595 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003596 }
3597 }
3598 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003600 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003601exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003602 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3603 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003604 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003605 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003606 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003607
Victor Stinnerefde1462015-03-21 15:04:43 +01003608 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003609}
3610
Benjamin Petersonb204a422011-06-05 22:04:07 -05003611static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003612format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3613{
3614 int err;
3615 Py_ssize_t len = PyList_GET_SIZE(names);
3616 PyObject *name_str, *comma, *tail, *tmp;
3617
3618 assert(PyList_CheckExact(names));
3619 assert(len >= 1);
3620 /* Deal with the joys of natural language. */
3621 switch (len) {
3622 case 1:
3623 name_str = PyList_GET_ITEM(names, 0);
3624 Py_INCREF(name_str);
3625 break;
3626 case 2:
3627 name_str = PyUnicode_FromFormat("%U and %U",
3628 PyList_GET_ITEM(names, len - 2),
3629 PyList_GET_ITEM(names, len - 1));
3630 break;
3631 default:
3632 tail = PyUnicode_FromFormat(", %U, and %U",
3633 PyList_GET_ITEM(names, len - 2),
3634 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003635 if (tail == NULL)
3636 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003637 /* Chop off the last two objects in the list. This shouldn't actually
3638 fail, but we can't be too careful. */
3639 err = PyList_SetSlice(names, len - 2, len, NULL);
3640 if (err == -1) {
3641 Py_DECREF(tail);
3642 return;
3643 }
3644 /* Stitch everything up into a nice comma-separated list. */
3645 comma = PyUnicode_FromString(", ");
3646 if (comma == NULL) {
3647 Py_DECREF(tail);
3648 return;
3649 }
3650 tmp = PyUnicode_Join(comma, names);
3651 Py_DECREF(comma);
3652 if (tmp == NULL) {
3653 Py_DECREF(tail);
3654 return;
3655 }
3656 name_str = PyUnicode_Concat(tmp, tail);
3657 Py_DECREF(tmp);
3658 Py_DECREF(tail);
3659 break;
3660 }
3661 if (name_str == NULL)
3662 return;
3663 PyErr_Format(PyExc_TypeError,
3664 "%U() missing %i required %s argument%s: %U",
3665 co->co_name,
3666 len,
3667 kind,
3668 len == 1 ? "" : "s",
3669 name_str);
3670 Py_DECREF(name_str);
3671}
3672
3673static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003674missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003675 PyObject **fastlocals)
3676{
Victor Stinner74319ae2016-08-25 00:04:09 +02003677 Py_ssize_t i, j = 0;
3678 Py_ssize_t start, end;
3679 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003680 const char *kind = positional ? "positional" : "keyword-only";
3681 PyObject *missing_names;
3682
3683 /* Compute the names of the arguments that are missing. */
3684 missing_names = PyList_New(missing);
3685 if (missing_names == NULL)
3686 return;
3687 if (positional) {
3688 start = 0;
3689 end = co->co_argcount - defcount;
3690 }
3691 else {
3692 start = co->co_argcount;
3693 end = start + co->co_kwonlyargcount;
3694 }
3695 for (i = start; i < end; i++) {
3696 if (GETLOCAL(i) == NULL) {
3697 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3698 PyObject *name = PyObject_Repr(raw);
3699 if (name == NULL) {
3700 Py_DECREF(missing_names);
3701 return;
3702 }
3703 PyList_SET_ITEM(missing_names, j++, name);
3704 }
3705 }
3706 assert(j == missing);
3707 format_missing(kind, co, missing_names);
3708 Py_DECREF(missing_names);
3709}
3710
3711static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003712too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3713 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003714{
3715 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003716 Py_ssize_t kwonly_given = 0;
3717 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003718 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003719 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003720
Benjamin Petersone109c702011-06-24 09:37:26 -05003721 assert((co->co_flags & CO_VARARGS) == 0);
3722 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003723 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3724 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003725 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003726 }
3727 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003728 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003729 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003730 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003731 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003732 }
3733 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003734 plural = (co_argcount != 1);
3735 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003736 }
3737 if (sig == NULL)
3738 return;
3739 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003740 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3741 kwonly_sig = PyUnicode_FromFormat(format,
3742 given != 1 ? "s" : "",
3743 kwonly_given,
3744 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003745 if (kwonly_sig == NULL) {
3746 Py_DECREF(sig);
3747 return;
3748 }
3749 }
3750 else {
3751 /* This will not fail. */
3752 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003753 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003754 }
3755 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003756 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003757 co->co_name,
3758 sig,
3759 plural ? "s" : "",
3760 given,
3761 kwonly_sig,
3762 given == 1 && !kwonly_given ? "was" : "were");
3763 Py_DECREF(sig);
3764 Py_DECREF(kwonly_sig);
3765}
3766
Guido van Rossumc2e20742006-02-27 22:32:47 +00003767/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003768 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003769 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003770
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003771PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003772_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003773 PyObject *const *args, Py_ssize_t argcount,
3774 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003775 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003776 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003777 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003778 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003779{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003780 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003781 PyFrameObject *f;
3782 PyObject *retval = NULL;
3783 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003784 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003785 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003786 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3787 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003788 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003790 if (globals == NULL) {
3791 PyErr_SetString(PyExc_SystemError,
3792 "PyEval_EvalCodeEx: NULL globals");
3793 return NULL;
3794 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003795
Victor Stinnerc7020012016-08-16 23:40:29 +02003796 /* Create the frame */
Victor Stinner50b48572018-11-01 01:51:40 +01003797 tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003798 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003799 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003800 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003801 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003802 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003803 fastlocals = f->f_localsplus;
3804 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003805
Victor Stinnerc7020012016-08-16 23:40:29 +02003806 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003807 if (co->co_flags & CO_VARKEYWORDS) {
3808 kwdict = PyDict_New();
3809 if (kwdict == NULL)
3810 goto fail;
3811 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003812 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003813 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003814 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003815 SETLOCAL(i, kwdict);
3816 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003817 else {
3818 kwdict = NULL;
3819 }
3820
3821 /* Copy positional arguments into local variables */
3822 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003823 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003824 }
3825 else {
3826 n = argcount;
3827 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003828 for (i = 0; i < n; i++) {
3829 x = args[i];
3830 Py_INCREF(x);
3831 SETLOCAL(i, x);
3832 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003833
3834 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003835 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05003836 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003837 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003838 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003839 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003840 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003841 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003842
Serhiy Storchakab7281052016-09-12 00:52:40 +03003843 /* Handle keyword arguments passed as two strided arrays */
3844 kwcount *= kwstep;
3845 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003846 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003847 PyObject *keyword = kwnames[i];
3848 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003849 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003850
Benjamin Petersonb204a422011-06-05 22:04:07 -05003851 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3852 PyErr_Format(PyExc_TypeError,
3853 "%U() keywords must be strings",
3854 co->co_name);
3855 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003856 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003857
Benjamin Petersonb204a422011-06-05 22:04:07 -05003858 /* Speed hack: do raw pointer compares. As names are
3859 normally interned this should almost always hit. */
3860 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3861 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003862 PyObject *name = co_varnames[j];
3863 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003864 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003865 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003866 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003867
Benjamin Petersonb204a422011-06-05 22:04:07 -05003868 /* Slow fallback, just in case */
3869 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003870 PyObject *name = co_varnames[j];
3871 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3872 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003873 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003874 }
3875 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003876 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003877 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003878 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003879
Victor Stinner231d1f32017-01-11 02:12:06 +01003880 assert(j >= total_args);
3881 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003882 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003883 "%U() got an unexpected keyword argument '%S'",
3884 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003885 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003886 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003887
Christian Heimes0bd447f2013-07-20 14:48:10 +02003888 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3889 goto fail;
3890 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003891 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003892
Benjamin Petersonb204a422011-06-05 22:04:07 -05003893 kw_found:
3894 if (GETLOCAL(j) != NULL) {
3895 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003896 "%U() got multiple values for argument '%S'",
3897 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003898 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003899 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003900 Py_INCREF(value);
3901 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003902 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003903
3904 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003905 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003906 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003907 goto fail;
3908 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003909
3910 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003911 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02003912 Py_ssize_t m = co->co_argcount - defcount;
3913 Py_ssize_t missing = 0;
3914 for (i = argcount; i < m; i++) {
3915 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003916 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02003917 }
3918 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003919 if (missing) {
3920 missing_arguments(co, missing, defcount, fastlocals);
3921 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003922 }
3923 if (n > m)
3924 i = n - m;
3925 else
3926 i = 0;
3927 for (; i < defcount; i++) {
3928 if (GETLOCAL(m+i) == NULL) {
3929 PyObject *def = defs[i];
3930 Py_INCREF(def);
3931 SETLOCAL(m+i, def);
3932 }
3933 }
3934 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003935
3936 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003937 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02003938 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003939 for (i = co->co_argcount; i < total_args; i++) {
3940 PyObject *name;
3941 if (GETLOCAL(i) != NULL)
3942 continue;
3943 name = PyTuple_GET_ITEM(co->co_varnames, i);
3944 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003945 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003946 if (def) {
3947 Py_INCREF(def);
3948 SETLOCAL(i, def);
3949 continue;
3950 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003951 else if (PyErr_Occurred()) {
3952 goto fail;
3953 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003954 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003955 missing++;
3956 }
3957 if (missing) {
3958 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003959 goto fail;
3960 }
3961 }
3962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003963 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003964 vars into frame. */
3965 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003966 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02003967 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05003968 /* Possibly account for the cell variable being an argument. */
3969 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003970 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003971 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003972 /* Clear the local copy. */
3973 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003974 }
3975 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003976 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003977 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003978 if (c == NULL)
3979 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003980 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003981 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003982
3983 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05003984 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3985 PyObject *o = PyTuple_GET_ITEM(closure, i);
3986 Py_INCREF(o);
3987 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003988 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003989
Yury Selivanoveb636452016-09-08 22:01:51 -07003990 /* Handle generator/coroutine/asynchronous generator */
3991 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003992 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04003993 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003994 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04003995
3996 if (is_coro && tstate->in_coroutine_wrapper) {
3997 assert(coro_wrapper != NULL);
3998 PyErr_Format(PyExc_RuntimeError,
3999 "coroutine wrapper %.200R attempted "
4000 "to recursively wrap %.200R",
4001 coro_wrapper,
4002 co);
4003 goto fail;
4004 }
Yury Selivanov75445082015-05-11 22:57:16 -04004005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004006 /* Don't need to keep the reference to f_back, it will be set
4007 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004008 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004010 /* Create a new generator that owns the ready to run frame
4011 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004012 if (is_coro) {
4013 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004014 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4015 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004016 } else {
4017 gen = PyGen_NewWithQualName(f, name, qualname);
4018 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004019 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004020 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004021 }
INADA Naoki9c157762016-12-26 18:52:46 +09004022
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004023 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004024
Yury Selivanov94c22632015-06-04 10:16:51 -04004025 if (is_coro && coro_wrapper != NULL) {
4026 PyObject *wrapped;
4027 tstate->in_coroutine_wrapper = 1;
4028 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4029 tstate->in_coroutine_wrapper = 0;
4030 return wrapped;
4031 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004032
Yury Selivanov75445082015-05-11 22:57:16 -04004033 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004034 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004035
Victor Stinner59a73272016-12-09 18:51:13 +01004036 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004037
Thomas Woutersce272b62007-09-19 21:19:28 +00004038fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004040 /* decref'ing the frame can cause __del__ methods to get invoked,
4041 which can call back into Python. While we're done with the
4042 current Python frame (f), the associated C stack is still in use,
4043 so recursion_depth must be boosted for the duration.
4044 */
4045 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09004046 if (Py_REFCNT(f) > 1) {
4047 Py_DECREF(f);
4048 _PyObject_GC_TRACK(f);
4049 }
4050 else {
4051 ++tstate->recursion_depth;
4052 Py_DECREF(f);
4053 --tstate->recursion_depth;
4054 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004055 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004056}
4057
Victor Stinner40ee3012014-06-16 15:59:28 +02004058PyObject *
4059PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004060 PyObject *const *args, int argcount,
4061 PyObject *const *kws, int kwcount,
4062 PyObject *const *defs, int defcount,
4063 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004064{
4065 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004066 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004067 kws, kws != NULL ? kws + 1 : NULL,
4068 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004069 defs, defcount,
4070 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004071 NULL, NULL);
4072}
Tim Peters5ca576e2001-06-18 22:08:13 +00004073
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004074static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004075special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004076{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004077 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004078 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004079 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004080 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004081 return NULL;
4082 }
4083 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004084}
4085
4086
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004087/* Logic for the raise statement (too complicated for inlining).
4088 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004089static int
Collin Winter828f04a2007-08-31 00:04:24 +00004090do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004091{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004092 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004094 if (exc == NULL) {
4095 /* Reraise */
Victor Stinner50b48572018-11-01 01:51:40 +01004096 PyThreadState *tstate = _PyThreadState_GET();
Mark Shannonae3087c2017-10-22 22:41:51 +01004097 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004098 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004099 type = exc_info->exc_type;
4100 value = exc_info->exc_value;
4101 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004102 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004103 PyErr_SetString(PyExc_RuntimeError,
4104 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004105 return 0;
4106 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004107 Py_XINCREF(type);
4108 Py_XINCREF(value);
4109 Py_XINCREF(tb);
4110 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004111 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004112 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004114 /* We support the following forms of raise:
4115 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004116 raise <instance>
4117 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004118
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004119 if (PyExceptionClass_Check(exc)) {
4120 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004121 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004122 if (value == NULL)
4123 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004124 if (!PyExceptionInstance_Check(value)) {
4125 PyErr_Format(PyExc_TypeError,
4126 "calling %R should have returned an instance of "
4127 "BaseException, not %R",
4128 type, Py_TYPE(value));
4129 goto raise_error;
4130 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004131 }
4132 else if (PyExceptionInstance_Check(exc)) {
4133 value = exc;
4134 type = PyExceptionInstance_Class(exc);
4135 Py_INCREF(type);
4136 }
4137 else {
4138 /* Not something you can raise. You get an exception
4139 anyway, just not what you specified :-) */
4140 Py_DECREF(exc);
4141 PyErr_SetString(PyExc_TypeError,
4142 "exceptions must derive from BaseException");
4143 goto raise_error;
4144 }
Collin Winter828f04a2007-08-31 00:04:24 +00004145
Serhiy Storchakac0191582016-09-27 11:37:10 +03004146 assert(type != NULL);
4147 assert(value != NULL);
4148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004149 if (cause) {
4150 PyObject *fixed_cause;
4151 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004152 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004153 if (fixed_cause == NULL)
4154 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004155 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004156 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004157 else if (PyExceptionInstance_Check(cause)) {
4158 fixed_cause = cause;
4159 }
4160 else if (cause == Py_None) {
4161 Py_DECREF(cause);
4162 fixed_cause = NULL;
4163 }
4164 else {
4165 PyErr_SetString(PyExc_TypeError,
4166 "exception causes must derive from "
4167 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004168 goto raise_error;
4169 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004170 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004171 }
Collin Winter828f04a2007-08-31 00:04:24 +00004172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004173 PyErr_SetObject(type, value);
4174 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004175 Py_DECREF(value);
4176 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004177 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004178
4179raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004180 Py_XDECREF(value);
4181 Py_XDECREF(type);
4182 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004183 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004184}
4185
Tim Petersd6d010b2001-06-21 02:49:55 +00004186/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004187 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004188
Guido van Rossum0368b722007-05-11 16:50:42 +00004189 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4190 with a variable target.
4191*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004192
Barry Warsawe42b18f1997-08-25 22:13:04 +00004193static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004194unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004196 int i = 0, j = 0;
4197 Py_ssize_t ll = 0;
4198 PyObject *it; /* iter(v) */
4199 PyObject *w;
4200 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004202 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004204 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004205 if (it == NULL) {
4206 if (PyErr_ExceptionMatches(PyExc_TypeError) &&
4207 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4208 {
4209 PyErr_Format(PyExc_TypeError,
4210 "cannot unpack non-iterable %.200s object",
4211 v->ob_type->tp_name);
4212 }
4213 return 0;
4214 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004216 for (; i < argcnt; i++) {
4217 w = PyIter_Next(it);
4218 if (w == NULL) {
4219 /* Iterator done, via error or exhaustion. */
4220 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004221 if (argcntafter == -1) {
4222 PyErr_Format(PyExc_ValueError,
4223 "not enough values to unpack (expected %d, got %d)",
4224 argcnt, i);
4225 }
4226 else {
4227 PyErr_Format(PyExc_ValueError,
4228 "not enough values to unpack "
4229 "(expected at least %d, got %d)",
4230 argcnt + argcntafter, i);
4231 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004232 }
4233 goto Error;
4234 }
4235 *--sp = w;
4236 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004238 if (argcntafter == -1) {
4239 /* We better have exhausted the iterator now. */
4240 w = PyIter_Next(it);
4241 if (w == NULL) {
4242 if (PyErr_Occurred())
4243 goto Error;
4244 Py_DECREF(it);
4245 return 1;
4246 }
4247 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004248 PyErr_Format(PyExc_ValueError,
4249 "too many values to unpack (expected %d)",
4250 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004251 goto Error;
4252 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004254 l = PySequence_List(it);
4255 if (l == NULL)
4256 goto Error;
4257 *--sp = l;
4258 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004260 ll = PyList_GET_SIZE(l);
4261 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004262 PyErr_Format(PyExc_ValueError,
4263 "not enough values to unpack (expected at least %d, got %zd)",
4264 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004265 goto Error;
4266 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004268 /* Pop the "after-variable" args off the list. */
4269 for (j = argcntafter; j > 0; j--, i++) {
4270 *--sp = PyList_GET_ITEM(l, ll - j);
4271 }
4272 /* Resize the list. */
4273 Py_SIZE(l) = ll - argcntafter;
4274 Py_DECREF(it);
4275 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004276
Tim Petersd6d010b2001-06-21 02:49:55 +00004277Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004278 for (; i > 0; i--, sp++)
4279 Py_DECREF(*sp);
4280 Py_XDECREF(it);
4281 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004282}
4283
4284
Guido van Rossum96a42c81992-01-12 02:29:51 +00004285#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004286static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004287prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004288{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004289 printf("%s ", str);
4290 if (PyObject_Print(v, stdout, 0) != 0)
4291 PyErr_Clear(); /* Don't know what else to do */
4292 printf("\n");
4293 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004294}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004295#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004296
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004297static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004298call_exc_trace(Py_tracefunc func, PyObject *self,
4299 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004300{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004301 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004302 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004303 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004304 if (value == NULL) {
4305 value = Py_None;
4306 Py_INCREF(value);
4307 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004308 PyErr_NormalizeException(&type, &value, &orig_traceback);
4309 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004310 arg = PyTuple_Pack(3, type, value, traceback);
4311 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004312 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004313 return;
4314 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004315 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004316 Py_DECREF(arg);
4317 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004318 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004319 else {
4320 Py_XDECREF(type);
4321 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004322 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004323 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004324}
4325
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004326static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004327call_trace_protected(Py_tracefunc func, PyObject *obj,
4328 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004329 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004330{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004331 PyObject *type, *value, *traceback;
4332 int err;
4333 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004334 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004335 if (err == 0)
4336 {
4337 PyErr_Restore(type, value, traceback);
4338 return 0;
4339 }
4340 else {
4341 Py_XDECREF(type);
4342 Py_XDECREF(value);
4343 Py_XDECREF(traceback);
4344 return -1;
4345 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004346}
4347
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004348static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004349call_trace(Py_tracefunc func, PyObject *obj,
4350 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004351 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004352{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004353 int result;
4354 if (tstate->tracing)
4355 return 0;
4356 tstate->tracing++;
4357 tstate->use_tracing = 0;
4358 result = func(obj, frame, what, arg);
4359 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4360 || (tstate->c_profilefunc != NULL));
4361 tstate->tracing--;
4362 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004363}
4364
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004365PyObject *
4366_PyEval_CallTracing(PyObject *func, PyObject *args)
4367{
Victor Stinner50b48572018-11-01 01:51:40 +01004368 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004369 int save_tracing = tstate->tracing;
4370 int save_use_tracing = tstate->use_tracing;
4371 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004373 tstate->tracing = 0;
4374 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4375 || (tstate->c_profilefunc != NULL));
4376 result = PyObject_Call(func, args, NULL);
4377 tstate->tracing = save_tracing;
4378 tstate->use_tracing = save_use_tracing;
4379 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004380}
4381
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004382/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004383static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004384maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004385 PyThreadState *tstate, PyFrameObject *frame,
4386 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004387{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004388 int result = 0;
4389 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004391 /* If the last instruction executed isn't in the current
4392 instruction window, reset the window.
4393 */
4394 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4395 PyAddrPair bounds;
4396 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4397 &bounds);
4398 *instr_lb = bounds.ap_lower;
4399 *instr_ub = bounds.ap_upper;
4400 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004401 /* If the last instruction falls at the start of a line or if it
4402 represents a jump backwards, update the frame's line number and
4403 then call the trace function if we're tracing source lines.
4404 */
4405 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004406 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004407 if (frame->f_trace_lines) {
4408 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4409 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004410 }
George King20faa682017-10-18 17:44:22 -07004411 /* Always emit an opcode event if we're tracing all opcodes. */
4412 if (frame->f_trace_opcodes) {
4413 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4414 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004415 *instr_prev = frame->f_lasti;
4416 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004417}
4418
Fred Drake5755ce62001-06-27 19:19:46 +00004419void
4420PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004421{
Victor Stinner50b48572018-11-01 01:51:40 +01004422 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004423 PyObject *temp = tstate->c_profileobj;
4424 Py_XINCREF(arg);
4425 tstate->c_profilefunc = NULL;
4426 tstate->c_profileobj = NULL;
4427 /* Must make sure that tracing is not ignored if 'temp' is freed */
4428 tstate->use_tracing = tstate->c_tracefunc != NULL;
4429 Py_XDECREF(temp);
4430 tstate->c_profilefunc = func;
4431 tstate->c_profileobj = arg;
4432 /* Flag that tracing or profiling is turned on */
4433 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004434}
4435
4436void
4437PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4438{
Victor Stinner50b48572018-11-01 01:51:40 +01004439 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004440 PyObject *temp = tstate->c_traceobj;
4441 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4442 Py_XINCREF(arg);
4443 tstate->c_tracefunc = NULL;
4444 tstate->c_traceobj = NULL;
4445 /* Must make sure that profiling is not ignored if 'temp' is freed */
4446 tstate->use_tracing = tstate->c_profilefunc != NULL;
4447 Py_XDECREF(temp);
4448 tstate->c_tracefunc = func;
4449 tstate->c_traceobj = arg;
4450 /* Flag that tracing or profiling is turned on */
4451 tstate->use_tracing = ((func != NULL)
4452 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004453}
4454
Yury Selivanov75445082015-05-11 22:57:16 -04004455void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004456_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4457{
4458 assert(new_depth >= 0);
Victor Stinner50b48572018-11-01 01:51:40 +01004459 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004460 tstate->coroutine_origin_tracking_depth = new_depth;
4461}
4462
4463int
4464_PyEval_GetCoroutineOriginTrackingDepth(void)
4465{
Victor Stinner50b48572018-11-01 01:51:40 +01004466 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004467 return tstate->coroutine_origin_tracking_depth;
4468}
4469
4470void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004471_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004472{
Victor Stinner50b48572018-11-01 01:51:40 +01004473 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004474
Yury Selivanov75445082015-05-11 22:57:16 -04004475 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004476 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004477}
4478
4479PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004480_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004481{
Victor Stinner50b48572018-11-01 01:51:40 +01004482 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004483 return tstate->coroutine_wrapper;
4484}
4485
Yury Selivanoveb636452016-09-08 22:01:51 -07004486void
4487_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4488{
Victor Stinner50b48572018-11-01 01:51:40 +01004489 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004490
4491 Py_XINCREF(firstiter);
4492 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4493}
4494
4495PyObject *
4496_PyEval_GetAsyncGenFirstiter(void)
4497{
Victor Stinner50b48572018-11-01 01:51:40 +01004498 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004499 return tstate->async_gen_firstiter;
4500}
4501
4502void
4503_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4504{
Victor Stinner50b48572018-11-01 01:51:40 +01004505 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004506
4507 Py_XINCREF(finalizer);
4508 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4509}
4510
4511PyObject *
4512_PyEval_GetAsyncGenFinalizer(void)
4513{
Victor Stinner50b48572018-11-01 01:51:40 +01004514 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004515 return tstate->async_gen_finalizer;
4516}
4517
Guido van Rossumb209a111997-04-29 18:18:01 +00004518PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004519PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004520{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004521 PyFrameObject *current_frame = PyEval_GetFrame();
4522 if (current_frame == NULL)
Victor Stinnercaba55b2018-08-03 15:33:52 +02004523 return _PyInterpreterState_GET_UNSAFE()->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004524 else
4525 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004526}
4527
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004528/* Convenience function to get a builtin from its name */
4529PyObject *
4530_PyEval_GetBuiltinId(_Py_Identifier *name)
4531{
4532 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4533 if (attr) {
4534 Py_INCREF(attr);
4535 }
4536 else if (!PyErr_Occurred()) {
4537 PyErr_SetObject(PyExc_AttributeError, _PyUnicode_FromId(name));
4538 }
4539 return attr;
4540}
4541
Guido van Rossumb209a111997-04-29 18:18:01 +00004542PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004543PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004544{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004545 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004546 if (current_frame == NULL) {
4547 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004548 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004549 }
4550
4551 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4552 return NULL;
4553
4554 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004555 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004556}
4557
Guido van Rossumb209a111997-04-29 18:18:01 +00004558PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004559PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004560{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004561 PyFrameObject *current_frame = PyEval_GetFrame();
4562 if (current_frame == NULL)
4563 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004564
4565 assert(current_frame->f_globals != NULL);
4566 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004567}
4568
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004569PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004570PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004571{
Victor Stinner50b48572018-11-01 01:51:40 +01004572 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004573 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004574}
4575
Guido van Rossum6135a871995-01-09 17:53:26 +00004576int
Tim Peters5ba58662001-07-16 02:29:45 +00004577PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004578{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004579 PyFrameObject *current_frame = PyEval_GetFrame();
4580 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004582 if (current_frame != NULL) {
4583 const int codeflags = current_frame->f_code->co_flags;
4584 const int compilerflags = codeflags & PyCF_MASK;
4585 if (compilerflags) {
4586 result = 1;
4587 cf->cf_flags |= compilerflags;
4588 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004589#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004590 if (codeflags & CO_GENERATOR_ALLOWED) {
4591 result = 1;
4592 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4593 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004594#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004595 }
4596 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004597}
4598
Guido van Rossum3f5da241990-12-20 15:06:42 +00004599
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004600const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004601PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004602{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004603 if (PyMethod_Check(func))
4604 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4605 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004606 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004607 else if (PyCFunction_Check(func))
4608 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4609 else
4610 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004611}
4612
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004613const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004614PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004615{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004616 if (PyMethod_Check(func))
4617 return "()";
4618 else if (PyFunction_Check(func))
4619 return "()";
4620 else if (PyCFunction_Check(func))
4621 return "()";
4622 else
4623 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004624}
4625
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004626#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004627if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004628 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4629 tstate, tstate->frame, \
4630 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004631 x = NULL; \
4632 } \
4633 else { \
4634 x = call; \
4635 if (tstate->c_profilefunc != NULL) { \
4636 if (x == NULL) { \
4637 call_trace_protected(tstate->c_profilefunc, \
4638 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004639 tstate, tstate->frame, \
4640 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004641 /* XXX should pass (type, value, tb) */ \
4642 } else { \
4643 if (call_trace(tstate->c_profilefunc, \
4644 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004645 tstate, tstate->frame, \
4646 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004647 Py_DECREF(x); \
4648 x = NULL; \
4649 } \
4650 } \
4651 } \
4652 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004653} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004654 x = call; \
4655 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004656
Victor Stinner415c5102017-01-11 00:54:57 +01004657/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4658 to reduce the stack consumption. */
4659Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004660call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004661{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004662 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004663 PyObject *func = *pfunc;
4664 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004665 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4666 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004667 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004669 /* Always dispatch PyCFunction first, because these are
4670 presumed to be the most frequent callable object.
4671 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004672 if (PyCFunction_Check(func)) {
Victor Stinner50b48572018-11-01 01:51:40 +01004673 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004674 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004675 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004676 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
Victor Stinner50b48572018-11-01 01:51:40 +01004677 PyThreadState *tstate = _PyThreadState_GET();
jdemeyer56868f92018-07-21 10:30:59 +02004678 if (nargs > 0 && tstate->use_tracing) {
4679 /* We need to create a temporary bound method as argument
4680 for profiling.
4681
4682 If nargs == 0, then this cannot work because we have no
4683 "self". In any case, the call itself would raise
4684 TypeError (foo needs an argument), so we just skip
4685 profiling. */
4686 PyObject *self = stack[0];
4687 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
jdemeyer147d9552018-07-23 18:41:20 +02004688 if (func != NULL) {
4689 C_TRACE(x, _PyCFunction_FastCallKeywords(func,
4690 stack+1, nargs-1,
4691 kwnames));
4692 Py_DECREF(func);
INADA Naoki93fac8d2017-03-07 14:24:37 +09004693 }
jdemeyer147d9552018-07-23 18:41:20 +02004694 else {
4695 x = NULL;
4696 }
INADA Naoki93fac8d2017-03-07 14:24:37 +09004697 }
4698 else {
4699 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4700 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004701 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004702 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004703 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004704 /* Optimize access to bound methods. Reuse the Python stack
4705 to pass 'self' as the first argument, replace 'func'
4706 with 'self'. It avoids the creation of a new temporary tuple
4707 for arguments (to replace func with self) when the method uses
4708 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004709 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004710 Py_INCREF(self);
4711 func = PyMethod_GET_FUNCTION(func);
4712 Py_INCREF(func);
4713 Py_SETREF(*pfunc, self);
4714 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004715 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004716 }
4717 else {
4718 Py_INCREF(func);
4719 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004720
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004721 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004722 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004723 }
4724 else {
4725 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4726 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004727 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004728 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004729
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004730 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4731
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004732 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004733 while ((*pp_stack) > pfunc) {
4734 w = EXT_POP(*pp_stack);
4735 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004736 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004737
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004738 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004739}
4740
Jeremy Hylton52820442001-01-03 23:52:36 +00004741static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004742do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004743{
jdemeyere89de732018-09-19 12:06:20 +02004744 PyObject *result;
4745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004746 if (PyCFunction_Check(func)) {
Victor Stinner50b48572018-11-01 01:51:40 +01004747 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004748 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004749 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004750 }
jdemeyere89de732018-09-19 12:06:20 +02004751 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
Victor Stinner50b48572018-11-01 01:51:40 +01004752 PyThreadState *tstate = _PyThreadState_GET();
jdemeyere89de732018-09-19 12:06:20 +02004753 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4754 if (nargs > 0 && tstate->use_tracing) {
4755 /* We need to create a temporary bound method as argument
4756 for profiling.
4757
4758 If nargs == 0, then this cannot work because we have no
4759 "self". In any case, the call itself would raise
4760 TypeError (foo needs an argument), so we just skip
4761 profiling. */
4762 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4763 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4764 if (func == NULL) {
4765 return NULL;
4766 }
4767
4768 C_TRACE(result, _PyCFunction_FastCallDict(func,
Victor Stinnerd17a6932018-11-09 16:56:48 +01004769 &_PyTuple_ITEMS(callargs)[1],
jdemeyere89de732018-09-19 12:06:20 +02004770 nargs - 1,
4771 kwdict));
4772 Py_DECREF(func);
4773 return result;
4774 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004775 }
jdemeyere89de732018-09-19 12:06:20 +02004776 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004777}
4778
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004779/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004780 nb_index slot defined, and store in *pi.
4781 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004782 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004783 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004784*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004785int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004786_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004787{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004788 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004789 Py_ssize_t x;
4790 if (PyIndex_Check(v)) {
4791 x = PyNumber_AsSsize_t(v, NULL);
4792 if (x == -1 && PyErr_Occurred())
4793 return 0;
4794 }
4795 else {
4796 PyErr_SetString(PyExc_TypeError,
4797 "slice indices must be integers or "
4798 "None or have an __index__ method");
4799 return 0;
4800 }
4801 *pi = x;
4802 }
4803 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004804}
4805
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004806int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004807_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004808{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004809 Py_ssize_t x;
4810 if (PyIndex_Check(v)) {
4811 x = PyNumber_AsSsize_t(v, NULL);
4812 if (x == -1 && PyErr_Occurred())
4813 return 0;
4814 }
4815 else {
4816 PyErr_SetString(PyExc_TypeError,
4817 "slice indices must be integers or "
4818 "have an __index__ method");
4819 return 0;
4820 }
4821 *pi = x;
4822 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004823}
4824
4825
Guido van Rossum486364b2007-06-30 05:01:58 +00004826#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004827 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004828
Guido van Rossumb209a111997-04-29 18:18:01 +00004829static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004830cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004831{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004832 int res = 0;
4833 switch (op) {
4834 case PyCmp_IS:
4835 res = (v == w);
4836 break;
4837 case PyCmp_IS_NOT:
4838 res = (v != w);
4839 break;
4840 case PyCmp_IN:
4841 res = PySequence_Contains(w, v);
4842 if (res < 0)
4843 return NULL;
4844 break;
4845 case PyCmp_NOT_IN:
4846 res = PySequence_Contains(w, v);
4847 if (res < 0)
4848 return NULL;
4849 res = !res;
4850 break;
4851 case PyCmp_EXC_MATCH:
4852 if (PyTuple_Check(w)) {
4853 Py_ssize_t i, length;
4854 length = PyTuple_Size(w);
4855 for (i = 0; i < length; i += 1) {
4856 PyObject *exc = PyTuple_GET_ITEM(w, i);
4857 if (!PyExceptionClass_Check(exc)) {
4858 PyErr_SetString(PyExc_TypeError,
4859 CANNOT_CATCH_MSG);
4860 return NULL;
4861 }
4862 }
4863 }
4864 else {
4865 if (!PyExceptionClass_Check(w)) {
4866 PyErr_SetString(PyExc_TypeError,
4867 CANNOT_CATCH_MSG);
4868 return NULL;
4869 }
4870 }
4871 res = PyErr_GivenExceptionMatches(v, w);
4872 break;
4873 default:
4874 return PyObject_RichCompare(v, w, op);
4875 }
4876 v = res ? Py_True : Py_False;
4877 Py_INCREF(v);
4878 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004879}
4880
Thomas Wouters52152252000-08-17 22:55:00 +00004881static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004882import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4883{
4884 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004885 PyObject *import_func, *res;
4886 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004887
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004888 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004889 if (import_func == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004890 if (!PyErr_Occurred()) {
4891 PyErr_SetString(PyExc_ImportError, "__import__ not found");
4892 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004893 return NULL;
4894 }
4895
4896 /* Fast path for not overloaded __import__. */
Victor Stinnercaba55b2018-08-03 15:33:52 +02004897 if (import_func == _PyInterpreterState_GET_UNSAFE()->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004898 int ilevel = _PyLong_AsInt(level);
4899 if (ilevel == -1 && PyErr_Occurred()) {
4900 return NULL;
4901 }
4902 res = PyImport_ImportModuleLevelObject(
4903 name,
4904 f->f_globals,
4905 f->f_locals == NULL ? Py_None : f->f_locals,
4906 fromlist,
4907 ilevel);
4908 return res;
4909 }
4910
4911 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004912
4913 stack[0] = name;
4914 stack[1] = f->f_globals;
4915 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
4916 stack[3] = fromlist;
4917 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02004918 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004919 Py_DECREF(import_func);
4920 return res;
4921}
4922
4923static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004924import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004925{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004926 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02004927 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08004928 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004929
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004930 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02004931 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004932 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004933 /* Issue #17636: in case this failed because of a circular relative
4934 import, try to fallback on reading the module directly from
4935 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02004936 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07004937 if (pkgname == NULL) {
4938 goto error;
4939 }
Oren Milman6db70332017-09-19 14:23:01 +03004940 if (!PyUnicode_Check(pkgname)) {
4941 Py_CLEAR(pkgname);
4942 goto error;
4943 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004944 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07004945 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08004946 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004947 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07004948 }
Eric Snow3f9eee62017-09-15 16:35:20 -06004949 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004950 Py_DECREF(fullmodname);
Stefan Krah027b09c2019-03-25 21:50:58 +01004951 if (x == NULL && !PyErr_Occurred()) {
Brett Cannon3008bc02015-08-11 18:01:31 -07004952 goto error;
4953 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004954 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004955 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07004956 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004957 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004958 if (pkgname == NULL) {
4959 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
4960 if (pkgname_or_unknown == NULL) {
4961 Py_XDECREF(pkgpath);
4962 return NULL;
4963 }
4964 } else {
4965 pkgname_or_unknown = pkgname;
4966 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004967
4968 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
4969 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08004970 errmsg = PyUnicode_FromFormat(
4971 "cannot import name %R from %R (unknown location)",
4972 name, pkgname_or_unknown
4973 );
Stefan Krah027b09c2019-03-25 21:50:58 +01004974 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08004975 PyErr_SetImportError(errmsg, pkgname, NULL);
4976 }
4977 else {
4978 errmsg = PyUnicode_FromFormat(
4979 "cannot import name %R from %R (%S)",
4980 name, pkgname_or_unknown, pkgpath
4981 );
Stefan Krah027b09c2019-03-25 21:50:58 +01004982 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08004983 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004984 }
4985
Xiang Zhang4830f582017-03-21 11:13:42 +08004986 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004987 Py_XDECREF(pkgname_or_unknown);
4988 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07004989 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00004990}
Guido van Rossumac7be682001-01-17 15:42:30 +00004991
Thomas Wouters52152252000-08-17 22:55:00 +00004992static int
4993import_all_from(PyObject *locals, PyObject *v)
4994{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004995 _Py_IDENTIFIER(__all__);
4996 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08004997 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004998 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004999 int skip_leading_underscores = 0;
5000 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005001
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005002 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5003 return -1; /* Unexpected error */
5004 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005005 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005006 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5007 return -1;
5008 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005009 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005010 PyErr_SetString(PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005011 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005012 return -1;
5013 }
5014 all = PyMapping_Keys(dict);
5015 Py_DECREF(dict);
5016 if (all == NULL)
5017 return -1;
5018 skip_leading_underscores = 1;
5019 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005021 for (pos = 0, err = 0; ; pos++) {
5022 name = PySequence_GetItem(all, pos);
5023 if (name == NULL) {
5024 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5025 err = -1;
5026 else
5027 PyErr_Clear();
5028 break;
5029 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005030 if (!PyUnicode_Check(name)) {
5031 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5032 if (modname == NULL) {
5033 Py_DECREF(name);
5034 err = -1;
5035 break;
5036 }
5037 if (!PyUnicode_Check(modname)) {
5038 PyErr_Format(PyExc_TypeError,
5039 "module __name__ must be a string, not %.100s",
5040 Py_TYPE(modname)->tp_name);
5041 }
5042 else {
5043 PyErr_Format(PyExc_TypeError,
5044 "%s in %U.%s must be str, not %.100s",
5045 skip_leading_underscores ? "Key" : "Item",
5046 modname,
5047 skip_leading_underscores ? "__dict__" : "__all__",
5048 Py_TYPE(name)->tp_name);
5049 }
5050 Py_DECREF(modname);
5051 Py_DECREF(name);
5052 err = -1;
5053 break;
5054 }
5055 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005056 if (PyUnicode_READY(name) == -1) {
5057 Py_DECREF(name);
5058 err = -1;
5059 break;
5060 }
5061 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5062 Py_DECREF(name);
5063 continue;
5064 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005065 }
5066 value = PyObject_GetAttr(v, name);
5067 if (value == NULL)
5068 err = -1;
5069 else if (PyDict_CheckExact(locals))
5070 err = PyDict_SetItem(locals, name, value);
5071 else
5072 err = PyObject_SetItem(locals, name, value);
5073 Py_DECREF(name);
5074 Py_XDECREF(value);
5075 if (err != 0)
5076 break;
5077 }
5078 Py_DECREF(all);
5079 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005080}
5081
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005082static int
5083check_args_iterable(PyObject *func, PyObject *args)
5084{
5085 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
5086 PyErr_Format(PyExc_TypeError,
5087 "%.200s%.200s argument after * "
5088 "must be an iterable, not %.200s",
5089 PyEval_GetFuncName(func),
5090 PyEval_GetFuncDesc(func),
5091 args->ob_type->tp_name);
5092 return -1;
5093 }
5094 return 0;
5095}
5096
5097static void
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005098format_kwargs_error(PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005099{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005100 /* _PyDict_MergeEx raises attribute
5101 * error (percolated from an attempt
5102 * to get 'keys' attribute) instead of
5103 * a type error if its second argument
5104 * is not a mapping.
5105 */
5106 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
5107 PyErr_Format(PyExc_TypeError,
5108 "%.200s%.200s argument after ** "
5109 "must be a mapping, not %.200s",
5110 PyEval_GetFuncName(func),
5111 PyEval_GetFuncDesc(func),
5112 kwargs->ob_type->tp_name);
5113 }
5114 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
5115 PyObject *exc, *val, *tb;
5116 PyErr_Fetch(&exc, &val, &tb);
5117 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
5118 PyObject *key = PyTuple_GET_ITEM(val, 0);
5119 if (!PyUnicode_Check(key)) {
5120 PyErr_Format(PyExc_TypeError,
5121 "%.200s%.200s keywords must be strings",
5122 PyEval_GetFuncName(func),
5123 PyEval_GetFuncDesc(func));
5124 } else {
5125 PyErr_Format(PyExc_TypeError,
5126 "%.200s%.200s got multiple "
5127 "values for keyword argument '%U'",
5128 PyEval_GetFuncName(func),
5129 PyEval_GetFuncDesc(func),
5130 key);
5131 }
5132 Py_XDECREF(exc);
5133 Py_XDECREF(val);
5134 Py_XDECREF(tb);
5135 }
5136 else {
5137 PyErr_Restore(exc, val, tb);
5138 }
5139 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005140}
5141
Guido van Rossumac7be682001-01-17 15:42:30 +00005142static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005143format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005145 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005147 if (!obj)
5148 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005149
Serhiy Storchaka06515832016-11-20 09:13:07 +02005150 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005151 if (!obj_str)
5152 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005154 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005155}
Guido van Rossum950361c1997-01-24 13:49:28 +00005156
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005157static void
5158format_exc_unbound(PyCodeObject *co, int oparg)
5159{
5160 PyObject *name;
5161 /* Don't stomp existing exception */
5162 if (PyErr_Occurred())
5163 return;
5164 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5165 name = PyTuple_GET_ITEM(co->co_cellvars,
5166 oparg);
5167 format_exc_check_arg(
5168 PyExc_UnboundLocalError,
5169 UNBOUNDLOCAL_ERROR_MSG,
5170 name);
5171 } else {
5172 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5173 PyTuple_GET_SIZE(co->co_cellvars));
5174 format_exc_check_arg(PyExc_NameError,
5175 UNBOUNDFREE_ERROR_MSG, name);
5176 }
5177}
5178
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005179static void
5180format_awaitable_error(PyTypeObject *type, int prevopcode)
5181{
5182 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5183 if (prevopcode == BEFORE_ASYNC_WITH) {
5184 PyErr_Format(PyExc_TypeError,
5185 "'async with' received an object from __aenter__ "
5186 "that does not implement __await__: %.100s",
5187 type->tp_name);
5188 }
5189 else if (prevopcode == WITH_CLEANUP_START) {
5190 PyErr_Format(PyExc_TypeError,
5191 "'async with' received an object from __aexit__ "
5192 "that does not implement __await__: %.100s",
5193 type->tp_name);
5194 }
5195 }
5196}
5197
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005198static PyObject *
5199unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005200 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005201{
5202 PyObject *res;
5203 if (Py_REFCNT(v) == 2) {
5204 /* In the common case, there are 2 references to the value
5205 * stored in 'variable' when the += is performed: one on the
5206 * value stack (in 'v') and one still stored in the
5207 * 'variable'. We try to delete the variable now to reduce
5208 * the refcnt to 1.
5209 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005210 int opcode, oparg;
5211 NEXTOPARG();
5212 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005213 case STORE_FAST:
5214 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005215 PyObject **fastlocals = f->f_localsplus;
5216 if (GETLOCAL(oparg) == v)
5217 SETLOCAL(oparg, NULL);
5218 break;
5219 }
5220 case STORE_DEREF:
5221 {
5222 PyObject **freevars = (f->f_localsplus +
5223 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005224 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005225 if (PyCell_GET(c) == v) {
5226 PyCell_SET(c, NULL);
5227 Py_DECREF(v);
5228 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005229 break;
5230 }
5231 case STORE_NAME:
5232 {
5233 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005234 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005235 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005236 if (locals && PyDict_CheckExact(locals)) {
5237 PyObject *w = PyDict_GetItemWithError(locals, name);
5238 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
5239 (w == NULL && PyErr_Occurred()))
5240 {
5241 Py_DECREF(v);
5242 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005243 }
5244 }
5245 break;
5246 }
5247 }
5248 }
5249 res = v;
5250 PyUnicode_Append(&res, w);
5251 return res;
5252}
5253
Guido van Rossum950361c1997-01-24 13:49:28 +00005254#ifdef DYNAMIC_EXECUTION_PROFILE
5255
Skip Montanarof118cb12001-10-15 20:51:38 +00005256static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005257getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005258{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005259 int i;
5260 PyObject *l = PyList_New(256);
5261 if (l == NULL) return NULL;
5262 for (i = 0; i < 256; i++) {
5263 PyObject *x = PyLong_FromLong(a[i]);
5264 if (x == NULL) {
5265 Py_DECREF(l);
5266 return NULL;
5267 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005268 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005269 }
5270 for (i = 0; i < 256; i++)
5271 a[i] = 0;
5272 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005273}
5274
5275PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005276_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005277{
5278#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005279 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005280#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005281 int i;
5282 PyObject *l = PyList_New(257);
5283 if (l == NULL) return NULL;
5284 for (i = 0; i < 257; i++) {
5285 PyObject *x = getarray(dxpairs[i]);
5286 if (x == NULL) {
5287 Py_DECREF(l);
5288 return NULL;
5289 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005290 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005291 }
5292 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005293#endif
5294}
5295
5296#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005297
5298Py_ssize_t
5299_PyEval_RequestCodeExtraIndex(freefunc free)
5300{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005301 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005302 Py_ssize_t new_index;
5303
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005304 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005305 return -1;
5306 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005307 new_index = interp->co_extra_user_count++;
5308 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005309 return new_index;
5310}
Łukasz Langaa785c872016-09-09 17:37:37 -07005311
5312static void
5313dtrace_function_entry(PyFrameObject *f)
5314{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005315 const char *filename;
5316 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005317 int lineno;
5318
5319 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5320 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5321 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5322
5323 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5324}
5325
5326static void
5327dtrace_function_return(PyFrameObject *f)
5328{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005329 const char *filename;
5330 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005331 int lineno;
5332
5333 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5334 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5335 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5336
5337 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5338}
5339
5340/* DTrace equivalent of maybe_call_line_trace. */
5341static void
5342maybe_dtrace_line(PyFrameObject *frame,
5343 int *instr_lb, int *instr_ub, int *instr_prev)
5344{
5345 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005346 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005347
5348 /* If the last instruction executed isn't in the current
5349 instruction window, reset the window.
5350 */
5351 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5352 PyAddrPair bounds;
5353 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5354 &bounds);
5355 *instr_lb = bounds.ap_lower;
5356 *instr_ub = bounds.ap_upper;
5357 }
5358 /* If the last instruction falls at the start of a line or if
5359 it represents a jump backwards, update the frame's line
5360 number and call the trace function. */
5361 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5362 frame->f_lineno = line;
5363 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5364 if (!co_filename)
5365 co_filename = "?";
5366 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5367 if (!co_name)
5368 co_name = "?";
5369 PyDTrace_LINE(co_filename, co_name, line);
5370 }
5371 *instr_prev = frame->f_lasti;
5372}