blob: a092a23556413604875173900b0b92c25720b4b3 [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 Stinner09532fe2019-05-10 23:39:09 +020013#include "pycore_ceval.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +010014#include "pycore_object.h"
Victor Stinner438a12d2019-05-24 17:01:38 +020015#include "pycore_pyerrors.h"
16#include "pycore_pylifecycle.h"
Victor Stinner621cebe2018-11-12 16:53:38 +010017#include "pycore_pystate.h"
Victor Stinnerec13b932018-11-25 23:56:17 +010018#include "pycore_tupleobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000019
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000020#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040021#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000022#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000023#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070024#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040025#include "setobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000026#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000027
Guido van Rossumc6004111993-11-05 10:22:19 +000028#include <ctype.h>
29
Guido van Rossum408027e1996-12-30 16:17:54 +000030#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000031/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000032#define LLTRACE 1 /* Low-level trace feature */
33#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000034#endif
35
Victor Stinner5c75f372019-04-17 23:02:26 +020036#if !defined(Py_BUILD_CORE)
37# error "ceval.c must be build with Py_BUILD_CORE define for best performance"
38#endif
39
Yury Selivanovf2392132016-12-13 19:03:51 -050040/* Private API for the LOAD_METHOD opcode. */
41extern int _PyObject_GetMethod(PyObject *, PyObject *, PyObject **);
42
Jeremy Hylton52820442001-01-03 23:52:36 +000043typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000044
Guido van Rossum374a9221991-04-04 10:40:29 +000045/* Forward declarations */
Victor Stinner09532fe2019-05-10 23:39:09 +020046Py_LOCAL_INLINE(PyObject *) call_function(
47 PyThreadState *tstate, PyObject ***pp_stack,
48 Py_ssize_t oparg, PyObject *kwnames);
49static PyObject * do_call_core(
50 PyThreadState *tstate, PyObject *func,
51 PyObject *callargs, PyObject *kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +000052
Guido van Rossum0a066c01992-03-27 17:29:15 +000053#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000054static int lltrace;
Victor Stinner438a12d2019-05-24 17:01:38 +020055static int prtrace(PyThreadState *, PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000056#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010057static int call_trace(Py_tracefunc, PyObject *,
58 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000060static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010061 PyThreadState *, PyFrameObject *,
62 int, PyObject *);
63static void call_exc_trace(Py_tracefunc, PyObject *,
64 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000065static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060066 PyThreadState *, PyFrameObject *,
67 int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070068static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
69static void dtrace_function_entry(PyFrameObject *);
70static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000071
Victor Stinner438a12d2019-05-24 17:01:38 +020072static PyObject * cmp_outcome(PyThreadState *, int, PyObject *, PyObject *);
73static PyObject * import_name(PyThreadState *, PyFrameObject *,
74 PyObject *, PyObject *, PyObject *);
75static PyObject * import_from(PyThreadState *, PyObject *, PyObject *);
76static int import_all_from(PyThreadState *, PyObject *, PyObject *);
77static void format_exc_check_arg(PyThreadState *, PyObject *, const char *, PyObject *);
78static void format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
79static PyObject * unicode_concatenate(PyThreadState *, PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030080 PyFrameObject *, const _Py_CODEUNIT *);
Victor Stinner438a12d2019-05-24 17:01:38 +020081static PyObject * special_lookup(PyThreadState *, PyObject *, _Py_Identifier *);
82static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg);
83static void format_kwargs_error(PyThreadState *, PyObject *func, PyObject *kwargs);
84static void format_awaitable_error(PyThreadState *, PyTypeObject *, int);
Guido van Rossum374a9221991-04-04 10:40:29 +000085
Paul Prescode68140d2000-08-30 20:25:01 +000086#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000088#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000090#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 "free variable '%.200s' referenced before assignment" \
92 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000093
Guido van Rossum950361c1997-01-24 13:49:28 +000094/* Dynamic execution profile */
95#ifdef DYNAMIC_EXECUTION_PROFILE
96#ifdef DXPAIRS
97static long dxpairs[257][256];
98#define dxp dxpairs[256]
99#else
100static long dxp[256];
101#endif
102#endif
103
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000104/* This can set eval_breaker to 0 even though gil_drop_request became
105 1. We believe this is all right because the eval loop will release
106 the GIL eventually anyway. */
Eric Snow6a150bc2019-06-01 15:39:46 -0600107#define COMPUTE_EVAL_BREAKER(ceval_r, ceval_i) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 _Py_atomic_store_relaxed( \
Eric Snow6a150bc2019-06-01 15:39:46 -0600109 &(ceval_r)->eval_breaker, \
110 _Py_atomic_load_relaxed(&(ceval_r)->gil_drop_request) | \
111 _Py_atomic_load_relaxed(&(ceval_r)->signals_pending) | \
112 _Py_atomic_load_relaxed(&(ceval_i)->pending.calls_to_do) | \
113 (ceval_i)->pending.async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000114
Eric Snow6a150bc2019-06-01 15:39:46 -0600115#define SET_GIL_DROP_REQUEST(ceval_r) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 do { \
Eric Snow6a150bc2019-06-01 15:39:46 -0600117 _Py_atomic_store_relaxed(&(ceval_r)->gil_drop_request, 1); \
118 _Py_atomic_store_relaxed(&(ceval_r)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000119 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000120
Eric Snow6a150bc2019-06-01 15:39:46 -0600121#define RESET_GIL_DROP_REQUEST(ceval_r, ceval_i) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 do { \
Eric Snow6a150bc2019-06-01 15:39:46 -0600123 _Py_atomic_store_relaxed(&(ceval_r)->gil_drop_request, 0); \
124 COMPUTE_EVAL_BREAKER(ceval_r, ceval_i); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000126
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000127/* Pending calls are only modified under pending_lock */
Eric Snow6a150bc2019-06-01 15:39:46 -0600128#define SIGNAL_PENDING_CALLS(ceval_r, ceval_i) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 do { \
Eric Snow6a150bc2019-06-01 15:39:46 -0600130 _Py_atomic_store_relaxed(&(ceval_i)->pending.calls_to_do, 1); \
131 _Py_atomic_store_relaxed(&(ceval_r)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000133
Eric Snow6a150bc2019-06-01 15:39:46 -0600134#define UNSIGNAL_PENDING_CALLS(ceval_r, ceval_i) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 do { \
Eric Snow6a150bc2019-06-01 15:39:46 -0600136 _Py_atomic_store_relaxed(&(ceval_i)->pending.calls_to_do, 0); \
137 COMPUTE_EVAL_BREAKER(ceval_r, ceval_i); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000139
Eric Snow6a150bc2019-06-01 15:39:46 -0600140#define SIGNAL_PENDING_SIGNALS(ceval_r) \
Eric Snowfdf282d2019-01-11 14:26:55 -0700141 do { \
Eric Snow6a150bc2019-06-01 15:39:46 -0600142 _Py_atomic_store_relaxed(&(ceval_r)->signals_pending, 1); \
143 _Py_atomic_store_relaxed(&(ceval_r)->eval_breaker, 1); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700144 } while (0)
145
Eric Snow6a150bc2019-06-01 15:39:46 -0600146#define UNSIGNAL_PENDING_SIGNALS(ceval_r, ceval_i) \
Eric Snowfdf282d2019-01-11 14:26:55 -0700147 do { \
Eric Snow6a150bc2019-06-01 15:39:46 -0600148 _Py_atomic_store_relaxed(&(ceval_r)->signals_pending, 0); \
149 COMPUTE_EVAL_BREAKER(ceval_r, ceval_i); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700150 } while (0)
151
Eric Snow6a150bc2019-06-01 15:39:46 -0600152#define SIGNAL_ASYNC_EXC(ceval_r, ceval_i) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 do { \
Eric Snow6a150bc2019-06-01 15:39:46 -0600154 (ceval_i)->pending.async_exc = 1; \
155 _Py_atomic_store_relaxed(&(ceval_r)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000157
Eric Snow6a150bc2019-06-01 15:39:46 -0600158#define UNSIGNAL_ASYNC_EXC(ceval_r, ceval_i) \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600159 do { \
Eric Snow6a150bc2019-06-01 15:39:46 -0600160 (ceval_i)->pending.async_exc = 0; \
161 COMPUTE_EVAL_BREAKER(ceval_r, ceval_i); \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600162 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000163
164
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000165#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000166#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000167#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000168#include "pythread.h"
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000169#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000170
Tim Peters7f468f22004-10-11 02:40:51 +0000171int
172PyEval_ThreadsInitialized(void)
173{
Victor Stinner09532fe2019-05-10 23:39:09 +0200174 return gil_created(&_PyRuntime.ceval.gil);
Tim Peters7f468f22004-10-11 02:40:51 +0000175}
176
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000177void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000178PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000179{
Victor Stinner09532fe2019-05-10 23:39:09 +0200180 _PyRuntimeState *runtime = &_PyRuntime;
Eric Snow6a150bc2019-06-01 15:39:46 -0600181 struct _ceval_runtime_state *ceval_r = &runtime->ceval;
182 struct _gil_runtime_state *gil = &ceval_r->gil;
Victor Stinner09532fe2019-05-10 23:39:09 +0200183 if (gil_created(gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100185 }
186
Inada Naoki001fee12019-02-20 10:00:09 +0900187 PyThread_init_thread();
Victor Stinner09532fe2019-05-10 23:39:09 +0200188 create_gil(gil);
189 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Eric Snow6a150bc2019-06-01 15:39:46 -0600190 take_gil(ceval_r, tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700191
Eric Snow6a150bc2019-06-01 15:39:46 -0600192 // The pending calls mutex is initialized in PyInterpreterState_New().
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000193}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000194
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000195void
Eric Snow6a150bc2019-06-01 15:39:46 -0600196_PyEval_FiniThreads(struct _ceval_runtime_state *ceval_r)
Antoine Pitrou1df15362010-09-13 14:16:46 +0000197{
Eric Snow6a150bc2019-06-01 15:39:46 -0600198 struct _gil_runtime_state *gil = &ceval_r->gil;
Victor Stinner09532fe2019-05-10 23:39:09 +0200199 if (!gil_created(gil)) {
Antoine Pitrou1df15362010-09-13 14:16:46 +0000200 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100201 }
202
Victor Stinner09532fe2019-05-10 23:39:09 +0200203 destroy_gil(gil);
204 assert(!gil_created(gil));
Victor Stinner99fcc612019-04-29 13:04:07 +0200205
Eric Snow6a150bc2019-06-01 15:39:46 -0600206 // The pending calls mutex is freed in PyInterpreterState_Delete().
Antoine Pitrou1df15362010-09-13 14:16:46 +0000207}
208
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400209static inline void
Eric Snow396e0a82019-05-31 21:16:47 -0600210exit_thread_if_finalizing(PyThreadState *tstate)
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400211{
Eric Snow6a150bc2019-06-01 15:39:46 -0600212 PyInterpreterState *interp = tstate->interp;
213 // Stop if thread/interpreter inalization already stated.
214 if (interp == NULL) {
215 return;
216 }
217 _PyRuntimeState *runtime = interp->runtime;
218 if (runtime == NULL) {
219 return;
220 }
221 // Don't exit if the main thread (i.e. of the main interpreter).
Victor Stinner09532fe2019-05-10 23:39:09 +0200222 if (runtime->finalizing != NULL && !_Py_CURRENTLY_FINALIZING(runtime, tstate)) {
Eric Snow6a150bc2019-06-01 15:39:46 -0600223 drop_gil(&runtime->ceval, &interp->ceval, tstate);
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400224 PyThread_exit_thread();
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400225 }
226}
227
Antoine Pitrou1df15362010-09-13 14:16:46 +0000228void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000229PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000230{
Victor Stinner09532fe2019-05-10 23:39:09 +0200231 _PyRuntimeState *runtime = &_PyRuntime;
Eric Snow6a150bc2019-06-01 15:39:46 -0600232 struct _ceval_runtime_state *ceval_r = &runtime->ceval;
Victor Stinner09532fe2019-05-10 23:39:09 +0200233 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
234 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
Victor Stinner09532fe2019-05-10 23:39:09 +0200236 }
Eric Snow6a150bc2019-06-01 15:39:46 -0600237 take_gil(ceval_r, tstate);
Eric Snow396e0a82019-05-31 21:16:47 -0600238 exit_thread_if_finalizing(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000239}
240
241void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000242PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000243{
Victor Stinner09532fe2019-05-10 23:39:09 +0200244 _PyRuntimeState *runtime = &_PyRuntime;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100246 We therefore avoid PyThreadState_Get() which dumps a fatal error
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 in debug mode.
248 */
Eric Snow6a150bc2019-06-01 15:39:46 -0600249 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
250 // Fall back to the main interpreter if there is not active Python
251 // thread. This only affects the eval_breaker.
252 PyInterpreterState *interp = runtime->interpreters.main;
253 if (tstate != NULL) {
254 interp = tstate->interp;
255 if (interp == NULL) {
256 Py_FatalError("PyEval_ReleaseLock: NULL interpreter state");
257 }
258 }
259 drop_gil(&runtime->ceval, &interp->ceval, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000260}
261
262void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000263PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000264{
Victor Stinner09532fe2019-05-10 23:39:09 +0200265 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200267 }
Eric Snow6a150bc2019-06-01 15:39:46 -0600268 PyInterpreterState *interp = tstate->interp;
269 if (interp == NULL) {
270 Py_FatalError("PyEval_AcquireThread: NULL interpreter state");
271 }
272 _PyRuntimeState *runtime = interp->runtime;
273 if (runtime == NULL) {
274 Py_FatalError("PyEval_AcquireThread: NULL runtime state");
275 }
276 struct _ceval_runtime_state *ceval_r = &runtime->ceval;
Victor Stinner09532fe2019-05-10 23:39:09 +0200277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 /* Check someone has called PyEval_InitThreads() to create the lock */
Eric Snow6a150bc2019-06-01 15:39:46 -0600279 assert(gil_created(&ceval_r->gil));
280 take_gil(ceval_r, tstate);
Eric Snow396e0a82019-05-31 21:16:47 -0600281 exit_thread_if_finalizing(tstate);
Victor Stinner09532fe2019-05-10 23:39:09 +0200282 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
283 Py_FatalError("PyEval_AcquireThread: non-NULL old thread state");
284 }
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000285}
286
287void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000288PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000289{
Victor Stinner09532fe2019-05-10 23:39:09 +0200290 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200292 }
Eric Snow6a150bc2019-06-01 15:39:46 -0600293 PyInterpreterState *interp = tstate->interp;
294 if (interp == NULL) {
295 Py_FatalError("PyEval_ReleaseThread: NULL interpreter state");
296 }
297 _PyRuntimeState *runtime = interp->runtime;
298 if (runtime == NULL) {
299 Py_FatalError("PyEval_ReleaseThread: NULL runtime state");
300 }
Victor Stinner09532fe2019-05-10 23:39:09 +0200301
Victor Stinner09532fe2019-05-10 23:39:09 +0200302 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
303 if (new_tstate != tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200305 }
Eric Snow6a150bc2019-06-01 15:39:46 -0600306 drop_gil(&runtime->ceval, &interp->ceval, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000307}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000308
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200309/* This function is called from PyOS_AfterFork_Child to destroy all threads
310 * which are not running in the child process, and clear internal locks
311 * which might be held by those threads.
312 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000313
314void
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200315_PyEval_ReInitThreads(_PyRuntimeState *runtime)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000316{
Eric Snow6a150bc2019-06-01 15:39:46 -0600317 struct _ceval_runtime_state *ceval_r = &runtime->ceval;
318 if (!gil_created(&ceval_r->gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 return;
Victor Stinner09532fe2019-05-10 23:39:09 +0200320 }
Eric Snow6a150bc2019-06-01 15:39:46 -0600321 recreate_gil(&ceval_r->gil);
Victor Stinner09532fe2019-05-10 23:39:09 +0200322 PyThreadState *current_tstate = _PyRuntimeState_GetThreadState(runtime);
Eric Snow6a150bc2019-06-01 15:39:46 -0600323 take_gil(ceval_r, current_tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700324
Eric Snow6a150bc2019-06-01 15:39:46 -0600325 // Only the main interpreter remains, so ignore the rest.
326 PyInterpreterState *interp = _PyRuntime.interpreters.main;
327 struct _ceval_pending_calls *pending = &interp->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200328 pending->lock = PyThread_allocate_lock();
329 if (pending->lock == NULL) {
Eric Snow8479a342019-03-08 23:44:33 -0700330 Py_FatalError("Can't initialize threads for pending calls");
331 }
Jesse Nollera8513972008-07-17 16:49:17 +0000332
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200333 /* Destroy all threads except the current one */
Eric Snow396e0a82019-05-31 21:16:47 -0600334 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000335}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000336
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000337/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600338 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000339
340void
Eric Snow6a150bc2019-06-01 15:39:46 -0600341_PyEval_SignalAsyncExc(struct _ceval_runtime_state *ceval_r,
342 struct _ceval_interpreter_state *ceval_i)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000343{
Eric Snow6a150bc2019-06-01 15:39:46 -0600344 SIGNAL_ASYNC_EXC(ceval_r, ceval_i);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000345}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000346
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000347PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000348PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000349{
Victor Stinner09532fe2019-05-10 23:39:09 +0200350 _PyRuntimeState *runtime = &_PyRuntime;
Eric Snow6a150bc2019-06-01 15:39:46 -0600351 struct _ceval_runtime_state *ceval_r = &runtime->ceval;
Victor Stinner09532fe2019-05-10 23:39:09 +0200352 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
353 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 Py_FatalError("PyEval_SaveThread: NULL tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +0200355 }
Eric Snow6a150bc2019-06-01 15:39:46 -0600356 PyInterpreterState *interp = tstate->interp;
357 if (interp == NULL) {
358 Py_FatalError("PyEval_SaveThread: NULL interpreter state");
359 }
360
361 assert(gil_created(&ceval_r->gil));
362 drop_gil(ceval_r, &interp->ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000364}
365
366void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000367PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000368{
Victor Stinner09532fe2019-05-10 23:39:09 +0200369 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +0200371 }
Eric Snow6a150bc2019-06-01 15:39:46 -0600372 PyInterpreterState *interp = tstate->interp;
373 if (interp == NULL) {
374 Py_FatalError("PyEval_RestoreThread: NULL interpreter state");
375 }
376 _PyRuntimeState *runtime = interp->runtime;
377 if (runtime == NULL) {
378 Py_FatalError("PyEval_RestoreThread: NULL runtime state");
379 }
380 struct _ceval_runtime_state *ceval_r = &runtime->ceval;
Eric Snow396e0a82019-05-31 21:16:47 -0600381
Eric Snow6a150bc2019-06-01 15:39:46 -0600382 assert(gil_created(&ceval_r->gil));
Victor Stinner2914bb32018-01-29 11:57:45 +0100383
384 int err = errno;
Eric Snow6a150bc2019-06-01 15:39:46 -0600385 take_gil(ceval_r, tstate);
Eric Snow396e0a82019-05-31 21:16:47 -0600386 exit_thread_if_finalizing(tstate);
Victor Stinner2914bb32018-01-29 11:57:45 +0100387 errno = err;
388
Victor Stinner09532fe2019-05-10 23:39:09 +0200389 _PyThreadState_Swap(&runtime->gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000390}
391
392
Guido van Rossuma9672091994-09-14 13:31:22 +0000393/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
394 signal handlers or Mac I/O completion routines) can schedule calls
395 to a function to be called synchronously.
396 The synchronous function is called with one void* argument.
397 It should return 0 for success or -1 for failure -- failure should
398 be accompanied by an exception.
399
400 If registry succeeds, the registry function returns 0; if it fails
401 (e.g. due to too many pending calls) it returns -1 (without setting
402 an exception condition).
403
404 Note that because registry may occur from within signal handlers,
405 or other asynchronous events, calling malloc() is unsafe!
406
Guido van Rossuma9672091994-09-14 13:31:22 +0000407 Any thread can schedule pending calls, but only the main thread
408 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000409 There is no facility to schedule calls to a particular thread, but
410 that should be easy to change, should that ever be required. In
411 that case, the static variables here should go into the python
412 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000413*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000414
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200415void
Eric Snow6a150bc2019-06-01 15:39:46 -0600416_PyEval_SignalReceived(struct _ceval_runtime_state *ceval_r)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200417{
418 /* bpo-30703: Function called when the C signal handler of Python gets a
419 signal. We cannot queue a callback using Py_AddPendingCall() since
420 that function is not async-signal-safe. */
Eric Snow6a150bc2019-06-01 15:39:46 -0600421 SIGNAL_PENDING_SIGNALS(ceval_r);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200422}
423
Eric Snow5be45a62019-03-08 22:47:07 -0700424/* Push one item onto the queue while holding the lock. */
425static int
Eric Snow6a150bc2019-06-01 15:39:46 -0600426_push_pending_call(struct _ceval_pending_calls *pending, unsigned long thread_id,
Eric Snow842a2f02019-03-15 15:47:51 -0600427 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700428{
Eric Snow842a2f02019-03-15 15:47:51 -0600429 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700430 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600431 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700432 return -1; /* Queue full */
433 }
Eric Snow6a150bc2019-06-01 15:39:46 -0600434 pending->calls[i].thread_id = thread_id;
Eric Snow842a2f02019-03-15 15:47:51 -0600435 pending->calls[i].func = func;
436 pending->calls[i].arg = arg;
437 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700438 return 0;
439}
440
441/* Pop one item off the queue while holding the lock. */
442static void
Eric Snow6a150bc2019-06-01 15:39:46 -0600443_pop_pending_call(struct _ceval_pending_calls *pending, unsigned long *thread_id,
Eric Snow842a2f02019-03-15 15:47:51 -0600444 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700445{
Eric Snow842a2f02019-03-15 15:47:51 -0600446 int i = pending->first;
447 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700448 return; /* Queue empty */
449 }
450
Eric Snow842a2f02019-03-15 15:47:51 -0600451 *func = pending->calls[i].func;
452 *arg = pending->calls[i].arg;
Eric Snow6a150bc2019-06-01 15:39:46 -0600453 *thread_id = pending->calls[i].thread_id;
Eric Snow842a2f02019-03-15 15:47:51 -0600454 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700455}
456
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200457/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000458 scheduling to be made from any thread, and even from an executing
459 callback.
460 */
461
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000462int
Victor Stinner438a12d2019-05-24 17:01:38 +0200463_PyEval_AddPendingCall(PyThreadState *tstate,
Eric Snow6a150bc2019-06-01 15:39:46 -0600464 struct _ceval_runtime_state *ceval_r,
465 struct _ceval_interpreter_state *ceval_i,
466 unsigned long thread_id,
Victor Stinner09532fe2019-05-10 23:39:09 +0200467 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000468{
Eric Snow6a150bc2019-06-01 15:39:46 -0600469 struct _ceval_pending_calls *pending = &ceval_i->pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600470
471 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
472 if (pending->finishing) {
473 PyThread_release_lock(pending->lock);
474
475 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +0200476 _PyErr_Fetch(tstate, &exc, &val, &tb);
477 _PyErr_SetString(tstate, PyExc_SystemError,
Eric Snow842a2f02019-03-15 15:47:51 -0600478 "Py_AddPendingCall: cannot add pending calls "
479 "(Python shutting down)");
Victor Stinner438a12d2019-05-24 17:01:38 +0200480 _PyErr_Print(tstate);
481 _PyErr_Restore(tstate, exc, val, tb);
Eric Snow842a2f02019-03-15 15:47:51 -0600482 return -1;
483 }
Eric Snow6a150bc2019-06-01 15:39:46 -0600484 int result = _push_pending_call(pending, thread_id, func, arg);
485
486 /* signal loop */
487 SIGNAL_PENDING_CALLS(ceval_r, ceval_i);
Eric Snow842a2f02019-03-15 15:47:51 -0600488 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000491}
492
Eric Snow6a150bc2019-06-01 15:39:46 -0600493/* Py_AddPendingCall() is a simple wrapper for the sake
494 of backward-compatibility. */
Victor Stinner09532fe2019-05-10 23:39:09 +0200495int
496Py_AddPendingCall(int (*func)(void *), void *arg)
497{
Victor Stinner438a12d2019-05-24 17:01:38 +0200498 _PyRuntimeState *runtime = &_PyRuntime;
Eric Snow6a150bc2019-06-01 15:39:46 -0600499 PyInterpreterState *interp = runtime->interpreters.main;
Victor Stinner438a12d2019-05-24 17:01:38 +0200500 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Eric Snow6a150bc2019-06-01 15:39:46 -0600501 return _PyEval_AddPendingCall(tstate,
502 &runtime->ceval, &interp->ceval,
503 runtime->main_thread,
504 func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200505}
506
Eric Snowfdf282d2019-01-11 14:26:55 -0700507static int
Victor Stinner09532fe2019-05-10 23:39:09 +0200508handle_signals(_PyRuntimeState *runtime)
Eric Snowfdf282d2019-01-11 14:26:55 -0700509{
Eric Snow5be45a62019-03-08 22:47:07 -0700510 /* Only handle signals on main thread. PyEval_InitThreads must
511 * have been called already.
512 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200513 if (PyThread_get_thread_ident() != runtime->main_thread) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700514 return 0;
515 }
Eric Snow64d6cc82019-02-23 15:40:43 -0700516 /*
517 * Ensure that the thread isn't currently running some other
518 * interpreter.
519 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200520 PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
521 if (interp != runtime->interpreters.main) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700522 return 0;
523 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700524
Eric Snow6a150bc2019-06-01 15:39:46 -0600525 struct _ceval_runtime_state *ceval_r = &runtime->ceval;
526 struct _ceval_interpreter_state *ceval_i = &interp->ceval;
527 UNSIGNAL_PENDING_SIGNALS(ceval_r, ceval_i);
Eric Snow64d6cc82019-02-23 15:40:43 -0700528 if (_PyErr_CheckSignals() < 0) {
Eric Snow6a150bc2019-06-01 15:39:46 -0600529 SIGNAL_PENDING_SIGNALS(ceval_r); /* We're not done yet */
Eric Snowfdf282d2019-01-11 14:26:55 -0700530 return -1;
531 }
532 return 0;
533}
534
535static int
Eric Snow6a150bc2019-06-01 15:39:46 -0600536make_pending_calls(PyInterpreterState *interp)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000537{
Eric Snow6a150bc2019-06-01 15:39:46 -0600538 if (interp == NULL) {
539 Py_FatalError("make_pending_calls: NULL interpreter state");
Eric Snowb75b1a352019-04-12 10:20:10 -0600540 }
Eric Snow6a150bc2019-06-01 15:39:46 -0600541 _PyRuntimeState *runtime = interp->runtime;
542 if (runtime == NULL) {
543 Py_FatalError("make_pending_calls: NULL runtime state");
544 }
545 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
546 if (tstate == NULL) {
547 Py_FatalError("make_pending_calls: NULL thread state");
548 }
549 if (tstate->interp == NULL || tstate->interp != interp) {
550 Py_FatalError("make_pending_calls: thread state mismatch");
551 }
552 static int busy = 0;
Eric Snowb75b1a352019-04-12 10:20:10 -0600553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 /* don't perform recursive pending calls */
Eric Snowfdf282d2019-01-11 14:26:55 -0700555 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700557 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200558 busy = 1;
Eric Snow6a150bc2019-06-01 15:39:46 -0600559 struct _ceval_runtime_state *ceval_r = &runtime->ceval;
560 struct _ceval_interpreter_state *ceval_i = &interp->ceval;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200561 /* unsignal before starting to call callbacks, so that any callback
562 added in-between re-signals */
Eric Snow6a150bc2019-06-01 15:39:46 -0600563 UNSIGNAL_PENDING_CALLS(ceval_r, ceval_i);
Eric Snowfdf282d2019-01-11 14:26:55 -0700564 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 /* perform a bounded number of calls, in case of recursion */
Eric Snow6a150bc2019-06-01 15:39:46 -0600567 struct _ceval_pending_calls *pending = &ceval_i->pending;
568 unsigned long thread_id = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700569 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700570 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 void *arg = NULL;
572
573 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600574 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Eric Snow6a150bc2019-06-01 15:39:46 -0600575 _pop_pending_call(pending, &thread_id, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600576 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700577
Eric Snow6a150bc2019-06-01 15:39:46 -0600578 if (thread_id && PyThread_get_thread_ident() != thread_id) {
579 // Thread mismatch, so move it to the end of the list
580 // and start over.
581 _PyEval_AddPendingCall(tstate,
582 &runtime->ceval, &interp->ceval,
583 thread_id,
584 func, arg);
585 goto error;
586 }
587
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100588 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700589 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100590 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700591 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700592 res = func(arg);
593 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200594 goto error;
595 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200597
Charles-François Natalif23339a2011-07-23 18:15:43 +0200598 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700599 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200600
601error:
602 busy = 0;
Eric Snow6a150bc2019-06-01 15:39:46 -0600603 SIGNAL_PENDING_CALLS(ceval_r, ceval_i);
Eric Snowfdf282d2019-01-11 14:26:55 -0700604 return res;
605}
606
Eric Snow842a2f02019-03-15 15:47:51 -0600607void
Eric Snow6a150bc2019-06-01 15:39:46 -0600608_PyEval_FinishPendingCalls(PyInterpreterState *interp)
Eric Snow842a2f02019-03-15 15:47:51 -0600609{
Eric Snow842a2f02019-03-15 15:47:51 -0600610 assert(PyGILState_Check());
611
Eric Snow6a150bc2019-06-01 15:39:46 -0600612 struct _ceval_pending_calls *pending = &interp->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200613
Eric Snow842a2f02019-03-15 15:47:51 -0600614 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
615 pending->finishing = 1;
616 PyThread_release_lock(pending->lock);
617
618 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
619 return;
620 }
621
Eric Snow6a150bc2019-06-01 15:39:46 -0600622 if (make_pending_calls(interp) < 0) {
623 _PyRuntimeState *runtime = interp->runtime;
624 if (runtime == NULL) {
625 runtime = &_PyRuntime;
626 }
627 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
628 if (tstate != NULL) {
629 PyObject *exc, *val, *tb;
630 _PyErr_Fetch(tstate, &exc, &val, &tb);
631 PyErr_BadInternalCall();
632 _PyErr_ChainExceptions(exc, val, tb);
633 _PyErr_Print(tstate);
634 }
Eric Snow842a2f02019-03-15 15:47:51 -0600635 }
636}
637
Eric Snowfdf282d2019-01-11 14:26:55 -0700638/* Py_MakePendingCalls() is a simple wrapper for the sake
639 of backward-compatibility. */
640int
641Py_MakePendingCalls(void)
642{
643 assert(PyGILState_Check());
644
645 /* Python signal handler doesn't really queue a callback: it only signals
646 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinner09532fe2019-05-10 23:39:09 +0200647 _PyRuntimeState *runtime = &_PyRuntime;
648 int res = handle_signals(runtime);
Eric Snowfdf282d2019-01-11 14:26:55 -0700649 if (res != 0) {
650 return res;
651 }
652
Eric Snow6a150bc2019-06-01 15:39:46 -0600653 PyInterpreterState *interp = _PyRuntime.interpreters.main;
654 res = make_pending_calls(interp);
Eric Snowb75b1a352019-04-12 10:20:10 -0600655 if (res != 0) {
656 return res;
657 }
658
659 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000660}
661
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000662/* The interpreter's recursion limit */
663
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000664#ifndef Py_DEFAULT_RECURSION_LIMIT
665#define Py_DEFAULT_RECURSION_LIMIT 1000
666#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600667
Eric Snow05351c12017-09-05 21:43:08 -0700668int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000669
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600670void
Eric Snow6a150bc2019-06-01 15:39:46 -0600671_PyEval_Initialize(struct _ceval_runtime_state *ceval_r)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600672{
Eric Snow6a150bc2019-06-01 15:39:46 -0600673 ceval_r->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600674 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Eric Snow6a150bc2019-06-01 15:39:46 -0600675 _gil_initialize(&ceval_r->gil);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600676}
677
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000678int
679Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000680{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600681 return _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000682}
683
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000684void
685Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000686{
Eric Snow6a150bc2019-06-01 15:39:46 -0600687 struct _ceval_runtime_state *ceval_r = &_PyRuntime.ceval;
688 ceval_r->recursion_limit = new_limit;
689 _Py_CheckRecursionLimit = ceval_r->recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000690}
691
Armin Rigo2b3eb402003-10-28 12:05:48 +0000692/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
693 if the recursion_depth reaches _Py_CheckRecursionLimit.
694 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
695 to guarantee that _Py_CheckRecursiveCall() is regularly called.
696 Without USE_STACKCHECK, there is no need for this. */
697int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300698_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000699{
Victor Stinner09532fe2019-05-10 23:39:09 +0200700 _PyRuntimeState *runtime = &_PyRuntime;
701 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
702 int recursion_limit = runtime->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000703
704#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700705 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 if (PyOS_CheckStack()) {
707 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200708 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 return -1;
710 }
pdox18967932017-10-25 23:03:01 -0700711 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700712 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700713#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 if (tstate->recursion_critical)
715 /* Somebody asked that we don't check for recursion. */
716 return 0;
717 if (tstate->overflowed) {
718 if (tstate->recursion_depth > recursion_limit + 50) {
719 /* Overflowing while handling an overflow. Give up. */
720 Py_FatalError("Cannot recover from stack overflow.");
721 }
722 return 0;
723 }
724 if (tstate->recursion_depth > recursion_limit) {
725 --tstate->recursion_depth;
726 tstate->overflowed = 1;
Victor Stinner438a12d2019-05-24 17:01:38 +0200727 _PyErr_Format(tstate, PyExc_RecursionError,
728 "maximum recursion depth exceeded%s",
729 where);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 return -1;
731 }
732 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000733}
734
Victor Stinner09532fe2019-05-10 23:39:09 +0200735static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200736static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000737
Eric Snow6a150bc2019-06-01 15:39:46 -0600738#define _Py_TracingPossible(ceval_r) ((ceval_r)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000739
Guido van Rossum374a9221991-04-04 10:40:29 +0000740
Guido van Rossumb209a111997-04-29 18:18:01 +0000741PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000742PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000743{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 return PyEval_EvalCodeEx(co,
745 globals, locals,
746 (PyObject **)NULL, 0,
747 (PyObject **)NULL, 0,
748 (PyObject **)NULL, 0,
749 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000750}
751
752
753/* Interpreter main loop */
754
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000755PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000756PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 /* This is for backward compatibility with extension modules that
758 used this API; core interpreter code should call
759 PyEval_EvalFrameEx() */
760 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000761}
762
763PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000764PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000765{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200766 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
767 return interp->eval_frame(f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700768}
769
Victor Stinnerc6944e72016-11-11 02:13:35 +0100770PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700771_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
772{
Guido van Rossum950361c1997-01-24 13:49:28 +0000773#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000775#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200776 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300777 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200778 int opcode; /* Current opcode */
779 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200780 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 PyObject *retval = NULL; /* Return value */
Victor Stinner09532fe2019-05-10 23:39:09 +0200782 _PyRuntimeState * const runtime = &_PyRuntime;
783 PyThreadState * const tstate = _PyRuntimeState_GetThreadState(runtime);
Eric Snow6a150bc2019-06-01 15:39:46 -0600784 PyInterpreterState * const interp = tstate->interp;
785 struct _ceval_runtime_state * const ceval_r = &runtime->ceval;
786 struct _ceval_interpreter_state * const ceval_i = &interp->ceval;
787 _Py_atomic_int * const eval_breaker = &ceval_r->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 is true when the line being executed has changed. The
795 initial values are such as to make this false the first
796 time it is tested. */
797 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000798
Serhiy Storchakaab874002016-09-11 13:48:15 +0300799 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 PyObject *names;
801 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000802
Brett Cannon368b4b72012-04-02 12:17:59 -0400803#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200804 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400805#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200806
Antoine Pitroub52ec782009-01-25 16:34:23 +0000807/* Computed GOTOs, or
808 the-optimization-commonly-but-improperly-known-as-"threaded code"
809 using gcc's labels-as-values extension
810 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
811
812 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000814 combined with a lookup table of jump addresses. However, since the
815 indirect jump instruction is shared by all opcodes, the CPU will have a
816 hard time making the right prediction for where to jump next (actually,
817 it will be always wrong except in the uncommon case of a sequence of
818 several identical opcodes).
819
820 "Threaded code" in contrast, uses an explicit jump table and an explicit
821 indirect jump instruction at the end of each opcode. Since the jump
822 instruction is at a different address for each opcode, the CPU will make a
823 separate prediction for each of these instructions, which is equivalent to
824 predicting the second opcode of each opcode pair. These predictions have
825 a much better chance to turn out valid, especially in small bytecode loops.
826
827 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000829 and potentially many more instructions (depending on the pipeline width).
830 A correctly predicted branch, however, is nearly free.
831
832 At the time of this writing, the "threaded code" version is up to 15-20%
833 faster than the normal "switch" version, depending on the compiler and the
834 CPU architecture.
835
836 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
837 because it would render the measurements invalid.
838
839
840 NOTE: care must be taken that the compiler doesn't try to "optimize" the
841 indirect jumps by sharing them between all opcodes. Such optimizations
842 can be disabled on gcc by using the -fno-gcse flag (or possibly
843 -fno-crossjumping).
844*/
845
Antoine Pitrou042b1282010-08-13 21:15:58 +0000846#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000847#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000848#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000849#endif
850
Antoine Pitrou042b1282010-08-13 21:15:58 +0000851#ifdef HAVE_COMPUTED_GOTOS
852 #ifndef USE_COMPUTED_GOTOS
853 #define USE_COMPUTED_GOTOS 1
854 #endif
855#else
856 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
857 #error "Computed gotos are not supported on this compiler."
858 #endif
859 #undef USE_COMPUTED_GOTOS
860 #define USE_COMPUTED_GOTOS 0
861#endif
862
863#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000864/* Import the static jump table */
865#include "opcode_targets.h"
866
Antoine Pitroub52ec782009-01-25 16:34:23 +0000867#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700868 op: \
869 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000870
Antoine Pitroub52ec782009-01-25 16:34:23 +0000871#ifdef LLTRACE
872#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 { \
Eric Snow6a150bc2019-06-01 15:39:46 -0600874 if (!lltrace && !_Py_TracingPossible(ceval_r) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300876 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300877 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 } \
879 goto fast_next_opcode; \
880 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000881#else
882#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 { \
Eric Snow6a150bc2019-06-01 15:39:46 -0600884 if (!_Py_TracingPossible(ceval_r) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300886 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300887 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 } \
889 goto fast_next_opcode; \
890 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000891#endif
892
Victor Stinner09532fe2019-05-10 23:39:09 +0200893#define DISPATCH() \
894 { \
895 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
896 FAST_DISPATCH(); \
897 } \
898 continue; \
899 }
900
Antoine Pitroub52ec782009-01-25 16:34:23 +0000901#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700902#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000903#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +0200904#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +0000905#endif
906
907
Neal Norwitza81d2202002-07-14 00:27:26 +0000908/* Tuple access macros */
909
910#ifndef Py_DEBUG
911#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
912#else
913#define GETITEM(v, i) PyTuple_GetItem((v), (i))
914#endif
915
Guido van Rossum374a9221991-04-04 10:40:29 +0000916/* Code access macros */
917
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300918/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600919#define INSTR_OFFSET() \
920 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300921#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300922 _Py_CODEUNIT word = *next_instr; \
923 opcode = _Py_OPCODE(word); \
924 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300925 next_instr++; \
926 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300927#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
928#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000929
Raymond Hettingerf606f872003-03-16 03:11:04 +0000930/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 Some opcodes tend to come in pairs thus making it possible to
932 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300933 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 Verifying the prediction costs a single high-speed test of a register
936 variable against a constant. If the pairing was good, then the
937 processor's own internal branch predication has a high likelihood of
938 success, resulting in a nearly zero-overhead transition to the
939 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300940 including its unpredictable switch-case branch. Combined with the
941 processor's internal branch prediction, a successful PREDICT has the
942 effect of making the two opcodes run as if they were a single new opcode
943 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000944
Georg Brandl86b2fb92008-07-16 03:43:04 +0000945 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 predictions turned-on and interpret the results as if some opcodes
947 had been combined or turn-off predictions so that the opcode frequency
948 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000949
950 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 the CPU to record separate branch prediction information for each
952 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000953
Raymond Hettingerf606f872003-03-16 03:11:04 +0000954*/
955
Antoine Pitrou042b1282010-08-13 21:15:58 +0000956#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000958#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300959#define PREDICT(op) \
960 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300961 _Py_CODEUNIT word = *next_instr; \
962 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300963 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300964 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300965 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300966 goto PRED_##op; \
967 } \
968 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000969#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300970#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000971
Raymond Hettingerf606f872003-03-16 03:11:04 +0000972
Guido van Rossum374a9221991-04-04 10:40:29 +0000973/* Stack manipulation macros */
974
Martin v. Löwis18e16552006-02-15 17:27:45 +0000975/* The stack can grow at most MAXINT deep, as co_nlocals and
976 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000977#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
978#define EMPTY() (STACK_LEVEL() == 0)
979#define TOP() (stack_pointer[-1])
980#define SECOND() (stack_pointer[-2])
981#define THIRD() (stack_pointer[-3])
982#define FOURTH() (stack_pointer[-4])
983#define PEEK(n) (stack_pointer[-(n)])
984#define SET_TOP(v) (stack_pointer[-1] = (v))
985#define SET_SECOND(v) (stack_pointer[-2] = (v))
986#define SET_THIRD(v) (stack_pointer[-3] = (v))
987#define SET_FOURTH(v) (stack_pointer[-4] = (v))
988#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
989#define BASIC_STACKADJ(n) (stack_pointer += n)
990#define BASIC_PUSH(v) (*stack_pointer++ = (v))
991#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000992
Guido van Rossum96a42c81992-01-12 02:29:51 +0000993#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +0200995 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000996 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +0200997#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000998 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +0000999#define STACK_GROW(n) do { \
1000 assert(n >= 0); \
1001 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001002 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001003 assert(STACK_LEVEL() <= co->co_stacksize); \
1004 } while (0)
1005#define STACK_SHRINK(n) do { \
1006 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001007 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001008 (void)(BASIC_STACKADJ(-n)); \
1009 assert(STACK_LEVEL() <= co->co_stacksize); \
1010 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001011#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001012 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001013 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001014#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001015#define PUSH(v) BASIC_PUSH(v)
1016#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001017#define STACK_GROW(n) BASIC_STACKADJ(n)
1018#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001019#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001020#endif
1021
Guido van Rossum681d79a1995-07-18 14:51:37 +00001022/* Local variable macros */
1023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001025
1026/* The SETLOCAL() macro must not DECREF the local variable in-place and
1027 then store the new value; it must copy the old value to a temporary
1028 value, then store the new value, and then DECREF the temporary value.
1029 This is because it is possible that during the DECREF the frame is
1030 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1031 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001033 GETLOCAL(i) = value; \
1034 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001035
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001036
1037#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 while (STACK_LEVEL() > (b)->b_level) { \
1039 PyObject *v = POP(); \
1040 Py_XDECREF(v); \
1041 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001042
1043#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001044 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001046 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1048 while (STACK_LEVEL() > (b)->b_level + 3) { \
1049 value = POP(); \
1050 Py_XDECREF(value); \
1051 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001052 exc_info = tstate->exc_info; \
1053 type = exc_info->exc_type; \
1054 value = exc_info->exc_value; \
1055 traceback = exc_info->exc_traceback; \
1056 exc_info->exc_type = POP(); \
1057 exc_info->exc_value = POP(); \
1058 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 Py_XDECREF(type); \
1060 Py_XDECREF(value); \
1061 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001062 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001063
Guido van Rossuma027efa1997-05-05 20:56:21 +00001064/* Start of code */
1065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 /* push frame */
1067 if (Py_EnterRecursiveCall(""))
1068 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001071
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 if (tstate->use_tracing) {
1073 if (tstate->c_tracefunc != NULL) {
1074 /* tstate->c_tracefunc, if defined, is a
1075 function that will be called on *every* entry
1076 to a code block. Its return value, if not
1077 None, is a function that will be called at
1078 the start of each executed line of code.
1079 (Actually, the function must return itself
1080 in order to continue tracing.) The trace
1081 functions are called with three arguments:
1082 a pointer to the current frame, a string
1083 indicating why the function is called, and
1084 an argument which depends on the situation.
1085 The global trace function is also called
1086 whenever an exception is detected. */
1087 if (call_trace_protected(tstate->c_tracefunc,
1088 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001089 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 /* Trace function raised an error */
1091 goto exit_eval_frame;
1092 }
1093 }
1094 if (tstate->c_profilefunc != NULL) {
1095 /* Similar for c_profilefunc, except it needn't
1096 return itself and isn't called for "line" events */
1097 if (call_trace_protected(tstate->c_profilefunc,
1098 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001099 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 /* Profile function raised an error */
1101 goto exit_eval_frame;
1102 }
1103 }
1104 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001105
Łukasz Langaa785c872016-09-09 17:37:37 -07001106 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1107 dtrace_function_entry(f);
1108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 co = f->f_code;
1110 names = co->co_names;
1111 consts = co->co_consts;
1112 fastlocals = f->f_localsplus;
1113 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001114 assert(PyBytes_Check(co->co_code));
1115 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001116 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1117 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1118 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001119 /*
1120 f->f_lasti refers to the index of the last instruction,
1121 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001122
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001123 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001124 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 When the PREDICT() macros are enabled, some opcode pairs follow in
1127 direct succession without updating f->f_lasti. A successful
1128 prediction effectively links the two codes together as if they
1129 were a single new opcode; accordingly,f->f_lasti will point to
1130 the first code in the pair (for instance, GET_ITER followed by
1131 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001132 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001134 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001135 next_instr = first_instr;
1136 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001137 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1138 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001139 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 stack_pointer = f->f_stacktop;
1141 assert(stack_pointer != NULL);
1142 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001143 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001144
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001145
Tim Peters5ca576e2001-06-18 22:08:13 +00001146#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001147 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001148#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001149
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001150 if (throwflag) /* support for generator.throw() */
1151 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001152
Victor Stinnerace47d72013-07-18 01:41:08 +02001153#ifdef Py_DEBUG
1154 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001155 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001156 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001157 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001158#endif
1159
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001160main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1163 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001164 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 /* Do periodic things. Doing this every time through
1167 the loop would add too much overhead, so we do it
1168 only every Nth instruction. We also do it if
1169 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1170 event needs attention (e.g. a signal handler or
1171 async I/O handler); see Py_AddPendingCall() and
1172 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001173
Eric Snow7bda9de2019-03-08 17:25:54 -07001174 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001175 opcode = _Py_OPCODE(*next_instr);
1176 if (opcode == SETUP_FINALLY ||
1177 opcode == SETUP_WITH ||
1178 opcode == BEFORE_ASYNC_WITH ||
1179 opcode == YIELD_FROM) {
1180 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001181 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001182 - If we're about to enter the 'with:'. It will prevent
1183 emitting a resource warning in the common idiom
1184 'with open(path) as file:'.
1185 - If we're about to enter the 'async with:'.
1186 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001187 *very* useful, but might help in some cases and it's
1188 traditional)
1189 - If we're resuming a chain of nested 'yield from' or
1190 'await' calls, then each frame is parked with YIELD_FROM
1191 as its next opcode. If the user hit control-C we want to
1192 wait until we've reached the innermost frame before
1193 running the signal handler and raising KeyboardInterrupt
1194 (see bpo-30039).
1195 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 goto fast_next_opcode;
1197 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001198
Eric Snow6a150bc2019-06-01 15:39:46 -06001199 if (_Py_atomic_load_relaxed(&ceval_r->signals_pending)) {
Victor Stinner09532fe2019-05-10 23:39:09 +02001200 if (handle_signals(runtime) != 0) {
Eric Snowfdf282d2019-01-11 14:26:55 -07001201 goto error;
1202 }
1203 }
Eric Snow6a150bc2019-06-01 15:39:46 -06001204 if (_Py_atomic_load_relaxed(&ceval_i->pending.calls_to_do)) {
1205 if (make_pending_calls(interp) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001206 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001207 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001209
Eric Snow6a150bc2019-06-01 15:39:46 -06001210 if (_Py_atomic_load_relaxed(&ceval_r->gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 /* Give another thread a chance */
Victor Stinner09532fe2019-05-10 23:39:09 +02001212 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 Py_FatalError("ceval: tstate mix-up");
Victor Stinner09532fe2019-05-10 23:39:09 +02001214 }
Eric Snow6a150bc2019-06-01 15:39:46 -06001215 drop_gil(ceval_r, ceval_i, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216
1217 /* Other threads may run now */
1218
Eric Snow6a150bc2019-06-01 15:39:46 -06001219 take_gil(ceval_r, tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001220
1221 /* Check if we should make a quick exit. */
Eric Snow396e0a82019-05-31 21:16:47 -06001222 exit_thread_if_finalizing(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001223
Victor Stinner09532fe2019-05-10 23:39:09 +02001224 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 Py_FatalError("ceval: orphan tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +02001226 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 }
1228 /* Check for asynchronous exceptions. */
1229 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001230 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 tstate->async_exc = NULL;
Eric Snow6a150bc2019-06-01 15:39:46 -06001232 UNSIGNAL_ASYNC_EXC(ceval_r, ceval_i);
Victor Stinner438a12d2019-05-24 17:01:38 +02001233 _PyErr_SetNone(tstate, exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001234 Py_DECREF(exc);
1235 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 }
1237 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 fast_next_opcode:
1240 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001241
Łukasz Langaa785c872016-09-09 17:37:37 -07001242 if (PyDTrace_LINE_ENABLED())
1243 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001246
Eric Snow6a150bc2019-06-01 15:39:46 -06001247 if (_Py_TracingPossible(ceval_r) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001248 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001249 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 /* see maybe_call_line_trace
1251 for expository comments */
1252 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 err = maybe_call_line_trace(tstate->c_tracefunc,
1255 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001256 tstate, f,
1257 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 /* Reload possibly changed frame fields */
1259 JUMPTO(f->f_lasti);
1260 if (f->f_stacktop != NULL) {
1261 stack_pointer = f->f_stacktop;
1262 f->f_stacktop = NULL;
1263 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001264 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001266 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001270
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001271 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001272 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001273#ifdef DYNAMIC_EXECUTION_PROFILE
1274#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 dxpairs[lastopcode][opcode]++;
1276 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001277#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001279#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001280
Guido van Rossum96a42c81992-01-12 02:29:51 +00001281#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 if (lltrace) {
1285 if (HAS_ARG(opcode)) {
1286 printf("%d: %d, %d\n",
1287 f->f_lasti, opcode, oparg);
1288 }
1289 else {
1290 printf("%d: %d\n",
1291 f->f_lasti, opcode);
1292 }
1293 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001294#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001299 It is essential that any operation that fails must goto error
1300 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001301
Benjamin Petersonddd19492018-09-16 22:38:02 -07001302 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001304 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001305
Benjamin Petersonddd19492018-09-16 22:38:02 -07001306 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001307 PyObject *value = GETLOCAL(oparg);
1308 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001309 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001310 UNBOUNDLOCAL_ERROR_MSG,
1311 PyTuple_GetItem(co->co_varnames, oparg));
1312 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001314 Py_INCREF(value);
1315 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001317 }
1318
Benjamin Petersonddd19492018-09-16 22:38:02 -07001319 case TARGET(LOAD_CONST): {
1320 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001321 PyObject *value = GETITEM(consts, oparg);
1322 Py_INCREF(value);
1323 PUSH(value);
1324 FAST_DISPATCH();
1325 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001326
Benjamin Petersonddd19492018-09-16 22:38:02 -07001327 case TARGET(STORE_FAST): {
1328 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001329 PyObject *value = POP();
1330 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001332 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001333
Benjamin Petersonddd19492018-09-16 22:38:02 -07001334 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001335 PyObject *value = POP();
1336 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001338 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001339
Benjamin Petersonddd19492018-09-16 22:38:02 -07001340 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001341 PyObject *top = TOP();
1342 PyObject *second = SECOND();
1343 SET_TOP(second);
1344 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001346 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001347
Benjamin Petersonddd19492018-09-16 22:38:02 -07001348 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001349 PyObject *top = TOP();
1350 PyObject *second = SECOND();
1351 PyObject *third = THIRD();
1352 SET_TOP(second);
1353 SET_SECOND(third);
1354 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001356 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001357
Benjamin Petersonddd19492018-09-16 22:38:02 -07001358 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001359 PyObject *top = TOP();
1360 PyObject *second = SECOND();
1361 PyObject *third = THIRD();
1362 PyObject *fourth = FOURTH();
1363 SET_TOP(second);
1364 SET_SECOND(third);
1365 SET_THIRD(fourth);
1366 SET_FOURTH(top);
1367 FAST_DISPATCH();
1368 }
1369
Benjamin Petersonddd19492018-09-16 22:38:02 -07001370 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001371 PyObject *top = TOP();
1372 Py_INCREF(top);
1373 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001375 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001376
Benjamin Petersonddd19492018-09-16 22:38:02 -07001377 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001378 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001379 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001380 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001381 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001382 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001383 SET_TOP(top);
1384 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001385 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001386 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001387
Benjamin Petersonddd19492018-09-16 22:38:02 -07001388 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001389 PyObject *value = TOP();
1390 PyObject *res = PyNumber_Positive(value);
1391 Py_DECREF(value);
1392 SET_TOP(res);
1393 if (res == NULL)
1394 goto error;
1395 DISPATCH();
1396 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001397
Benjamin Petersonddd19492018-09-16 22:38:02 -07001398 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001399 PyObject *value = TOP();
1400 PyObject *res = PyNumber_Negative(value);
1401 Py_DECREF(value);
1402 SET_TOP(res);
1403 if (res == NULL)
1404 goto error;
1405 DISPATCH();
1406 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001407
Benjamin Petersonddd19492018-09-16 22:38:02 -07001408 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001409 PyObject *value = TOP();
1410 int err = PyObject_IsTrue(value);
1411 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 if (err == 0) {
1413 Py_INCREF(Py_True);
1414 SET_TOP(Py_True);
1415 DISPATCH();
1416 }
1417 else if (err > 0) {
1418 Py_INCREF(Py_False);
1419 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 DISPATCH();
1421 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001422 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001423 goto error;
1424 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001425
Benjamin Petersonddd19492018-09-16 22:38:02 -07001426 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001427 PyObject *value = TOP();
1428 PyObject *res = PyNumber_Invert(value);
1429 Py_DECREF(value);
1430 SET_TOP(res);
1431 if (res == NULL)
1432 goto error;
1433 DISPATCH();
1434 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001435
Benjamin Petersonddd19492018-09-16 22:38:02 -07001436 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001437 PyObject *exp = POP();
1438 PyObject *base = TOP();
1439 PyObject *res = PyNumber_Power(base, exp, Py_None);
1440 Py_DECREF(base);
1441 Py_DECREF(exp);
1442 SET_TOP(res);
1443 if (res == NULL)
1444 goto error;
1445 DISPATCH();
1446 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001447
Benjamin Petersonddd19492018-09-16 22:38:02 -07001448 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001449 PyObject *right = POP();
1450 PyObject *left = TOP();
1451 PyObject *res = PyNumber_Multiply(left, right);
1452 Py_DECREF(left);
1453 Py_DECREF(right);
1454 SET_TOP(res);
1455 if (res == NULL)
1456 goto error;
1457 DISPATCH();
1458 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001459
Benjamin Petersonddd19492018-09-16 22:38:02 -07001460 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001461 PyObject *right = POP();
1462 PyObject *left = TOP();
1463 PyObject *res = PyNumber_MatrixMultiply(left, right);
1464 Py_DECREF(left);
1465 Py_DECREF(right);
1466 SET_TOP(res);
1467 if (res == NULL)
1468 goto error;
1469 DISPATCH();
1470 }
1471
Benjamin Petersonddd19492018-09-16 22:38:02 -07001472 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001473 PyObject *divisor = POP();
1474 PyObject *dividend = TOP();
1475 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1476 Py_DECREF(dividend);
1477 Py_DECREF(divisor);
1478 SET_TOP(quotient);
1479 if (quotient == NULL)
1480 goto error;
1481 DISPATCH();
1482 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001483
Benjamin Petersonddd19492018-09-16 22:38:02 -07001484 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001485 PyObject *divisor = POP();
1486 PyObject *dividend = TOP();
1487 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1488 Py_DECREF(dividend);
1489 Py_DECREF(divisor);
1490 SET_TOP(quotient);
1491 if (quotient == NULL)
1492 goto error;
1493 DISPATCH();
1494 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001495
Benjamin Petersonddd19492018-09-16 22:38:02 -07001496 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001497 PyObject *divisor = POP();
1498 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001499 PyObject *res;
1500 if (PyUnicode_CheckExact(dividend) && (
1501 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1502 // fast path; string formatting, but not if the RHS is a str subclass
1503 // (see issue28598)
1504 res = PyUnicode_Format(dividend, divisor);
1505 } else {
1506 res = PyNumber_Remainder(dividend, divisor);
1507 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001508 Py_DECREF(divisor);
1509 Py_DECREF(dividend);
1510 SET_TOP(res);
1511 if (res == NULL)
1512 goto error;
1513 DISPATCH();
1514 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001515
Benjamin Petersonddd19492018-09-16 22:38:02 -07001516 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001517 PyObject *right = POP();
1518 PyObject *left = TOP();
1519 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001520 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1521 CPython using bytecode, it is simply worthless.
1522 See http://bugs.python.org/issue21955 and
1523 http://bugs.python.org/issue10044 for the discussion. In short,
1524 no patch shown any impact on a realistic benchmark, only a minor
1525 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001526 if (PyUnicode_CheckExact(left) &&
1527 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001528 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001529 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001530 }
1531 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001532 sum = PyNumber_Add(left, right);
1533 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001534 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001535 Py_DECREF(right);
1536 SET_TOP(sum);
1537 if (sum == NULL)
1538 goto error;
1539 DISPATCH();
1540 }
1541
Benjamin Petersonddd19492018-09-16 22:38:02 -07001542 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001543 PyObject *right = POP();
1544 PyObject *left = TOP();
1545 PyObject *diff = PyNumber_Subtract(left, right);
1546 Py_DECREF(right);
1547 Py_DECREF(left);
1548 SET_TOP(diff);
1549 if (diff == NULL)
1550 goto error;
1551 DISPATCH();
1552 }
1553
Benjamin Petersonddd19492018-09-16 22:38:02 -07001554 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001555 PyObject *sub = POP();
1556 PyObject *container = TOP();
1557 PyObject *res = PyObject_GetItem(container, sub);
1558 Py_DECREF(container);
1559 Py_DECREF(sub);
1560 SET_TOP(res);
1561 if (res == NULL)
1562 goto error;
1563 DISPATCH();
1564 }
1565
Benjamin Petersonddd19492018-09-16 22:38:02 -07001566 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001567 PyObject *right = POP();
1568 PyObject *left = TOP();
1569 PyObject *res = PyNumber_Lshift(left, right);
1570 Py_DECREF(left);
1571 Py_DECREF(right);
1572 SET_TOP(res);
1573 if (res == NULL)
1574 goto error;
1575 DISPATCH();
1576 }
1577
Benjamin Petersonddd19492018-09-16 22:38:02 -07001578 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001579 PyObject *right = POP();
1580 PyObject *left = TOP();
1581 PyObject *res = PyNumber_Rshift(left, right);
1582 Py_DECREF(left);
1583 Py_DECREF(right);
1584 SET_TOP(res);
1585 if (res == NULL)
1586 goto error;
1587 DISPATCH();
1588 }
1589
Benjamin Petersonddd19492018-09-16 22:38:02 -07001590 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001591 PyObject *right = POP();
1592 PyObject *left = TOP();
1593 PyObject *res = PyNumber_And(left, right);
1594 Py_DECREF(left);
1595 Py_DECREF(right);
1596 SET_TOP(res);
1597 if (res == NULL)
1598 goto error;
1599 DISPATCH();
1600 }
1601
Benjamin Petersonddd19492018-09-16 22:38:02 -07001602 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001603 PyObject *right = POP();
1604 PyObject *left = TOP();
1605 PyObject *res = PyNumber_Xor(left, right);
1606 Py_DECREF(left);
1607 Py_DECREF(right);
1608 SET_TOP(res);
1609 if (res == NULL)
1610 goto error;
1611 DISPATCH();
1612 }
1613
Benjamin Petersonddd19492018-09-16 22:38:02 -07001614 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001615 PyObject *right = POP();
1616 PyObject *left = TOP();
1617 PyObject *res = PyNumber_Or(left, right);
1618 Py_DECREF(left);
1619 Py_DECREF(right);
1620 SET_TOP(res);
1621 if (res == NULL)
1622 goto error;
1623 DISPATCH();
1624 }
1625
Benjamin Petersonddd19492018-09-16 22:38:02 -07001626 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001627 PyObject *v = POP();
1628 PyObject *list = PEEK(oparg);
1629 int err;
1630 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001632 if (err != 0)
1633 goto error;
1634 PREDICT(JUMP_ABSOLUTE);
1635 DISPATCH();
1636 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001637
Benjamin Petersonddd19492018-09-16 22:38:02 -07001638 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001639 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001640 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001641 int err;
1642 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001643 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001644 if (err != 0)
1645 goto error;
1646 PREDICT(JUMP_ABSOLUTE);
1647 DISPATCH();
1648 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001649
Benjamin Petersonddd19492018-09-16 22:38:02 -07001650 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001651 PyObject *exp = POP();
1652 PyObject *base = TOP();
1653 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1654 Py_DECREF(base);
1655 Py_DECREF(exp);
1656 SET_TOP(res);
1657 if (res == NULL)
1658 goto error;
1659 DISPATCH();
1660 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001661
Benjamin Petersonddd19492018-09-16 22:38:02 -07001662 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001663 PyObject *right = POP();
1664 PyObject *left = TOP();
1665 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1666 Py_DECREF(left);
1667 Py_DECREF(right);
1668 SET_TOP(res);
1669 if (res == NULL)
1670 goto error;
1671 DISPATCH();
1672 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001673
Benjamin Petersonddd19492018-09-16 22:38:02 -07001674 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001675 PyObject *right = POP();
1676 PyObject *left = TOP();
1677 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1678 Py_DECREF(left);
1679 Py_DECREF(right);
1680 SET_TOP(res);
1681 if (res == NULL)
1682 goto error;
1683 DISPATCH();
1684 }
1685
Benjamin Petersonddd19492018-09-16 22:38:02 -07001686 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001687 PyObject *divisor = POP();
1688 PyObject *dividend = TOP();
1689 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1690 Py_DECREF(dividend);
1691 Py_DECREF(divisor);
1692 SET_TOP(quotient);
1693 if (quotient == NULL)
1694 goto error;
1695 DISPATCH();
1696 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001697
Benjamin Petersonddd19492018-09-16 22:38:02 -07001698 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001699 PyObject *divisor = POP();
1700 PyObject *dividend = TOP();
1701 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1702 Py_DECREF(dividend);
1703 Py_DECREF(divisor);
1704 SET_TOP(quotient);
1705 if (quotient == NULL)
1706 goto error;
1707 DISPATCH();
1708 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001709
Benjamin Petersonddd19492018-09-16 22:38:02 -07001710 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001711 PyObject *right = POP();
1712 PyObject *left = TOP();
1713 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1714 Py_DECREF(left);
1715 Py_DECREF(right);
1716 SET_TOP(mod);
1717 if (mod == NULL)
1718 goto error;
1719 DISPATCH();
1720 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001721
Benjamin Petersonddd19492018-09-16 22:38:02 -07001722 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001723 PyObject *right = POP();
1724 PyObject *left = TOP();
1725 PyObject *sum;
1726 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001727 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001728 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001729 }
1730 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001731 sum = PyNumber_InPlaceAdd(left, right);
1732 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001733 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001734 Py_DECREF(right);
1735 SET_TOP(sum);
1736 if (sum == NULL)
1737 goto error;
1738 DISPATCH();
1739 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001740
Benjamin Petersonddd19492018-09-16 22:38:02 -07001741 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001742 PyObject *right = POP();
1743 PyObject *left = TOP();
1744 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1745 Py_DECREF(left);
1746 Py_DECREF(right);
1747 SET_TOP(diff);
1748 if (diff == NULL)
1749 goto error;
1750 DISPATCH();
1751 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001752
Benjamin Petersonddd19492018-09-16 22:38:02 -07001753 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001754 PyObject *right = POP();
1755 PyObject *left = TOP();
1756 PyObject *res = PyNumber_InPlaceLshift(left, right);
1757 Py_DECREF(left);
1758 Py_DECREF(right);
1759 SET_TOP(res);
1760 if (res == NULL)
1761 goto error;
1762 DISPATCH();
1763 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001764
Benjamin Petersonddd19492018-09-16 22:38:02 -07001765 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001766 PyObject *right = POP();
1767 PyObject *left = TOP();
1768 PyObject *res = PyNumber_InPlaceRshift(left, right);
1769 Py_DECREF(left);
1770 Py_DECREF(right);
1771 SET_TOP(res);
1772 if (res == NULL)
1773 goto error;
1774 DISPATCH();
1775 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001776
Benjamin Petersonddd19492018-09-16 22:38:02 -07001777 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001778 PyObject *right = POP();
1779 PyObject *left = TOP();
1780 PyObject *res = PyNumber_InPlaceAnd(left, right);
1781 Py_DECREF(left);
1782 Py_DECREF(right);
1783 SET_TOP(res);
1784 if (res == NULL)
1785 goto error;
1786 DISPATCH();
1787 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001788
Benjamin Petersonddd19492018-09-16 22:38:02 -07001789 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001790 PyObject *right = POP();
1791 PyObject *left = TOP();
1792 PyObject *res = PyNumber_InPlaceXor(left, right);
1793 Py_DECREF(left);
1794 Py_DECREF(right);
1795 SET_TOP(res);
1796 if (res == NULL)
1797 goto error;
1798 DISPATCH();
1799 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001800
Benjamin Petersonddd19492018-09-16 22:38:02 -07001801 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001802 PyObject *right = POP();
1803 PyObject *left = TOP();
1804 PyObject *res = PyNumber_InPlaceOr(left, right);
1805 Py_DECREF(left);
1806 Py_DECREF(right);
1807 SET_TOP(res);
1808 if (res == NULL)
1809 goto error;
1810 DISPATCH();
1811 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001812
Benjamin Petersonddd19492018-09-16 22:38:02 -07001813 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001814 PyObject *sub = TOP();
1815 PyObject *container = SECOND();
1816 PyObject *v = THIRD();
1817 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001818 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001819 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001820 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001821 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001822 Py_DECREF(container);
1823 Py_DECREF(sub);
1824 if (err != 0)
1825 goto error;
1826 DISPATCH();
1827 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001828
Benjamin Petersonddd19492018-09-16 22:38:02 -07001829 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001830 PyObject *sub = TOP();
1831 PyObject *container = SECOND();
1832 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001833 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001834 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001835 err = PyObject_DelItem(container, sub);
1836 Py_DECREF(container);
1837 Py_DECREF(sub);
1838 if (err != 0)
1839 goto error;
1840 DISPATCH();
1841 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001842
Benjamin Petersonddd19492018-09-16 22:38:02 -07001843 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001844 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001845 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001846 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001847 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001848 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001849 _PyErr_SetString(tstate, PyExc_RuntimeError,
1850 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001851 Py_DECREF(value);
1852 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001854 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001855 Py_DECREF(value);
1856 if (res == NULL)
1857 goto error;
1858 Py_DECREF(res);
1859 DISPATCH();
1860 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001861
Benjamin Petersonddd19492018-09-16 22:38:02 -07001862 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001863 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 switch (oparg) {
1865 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001866 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001867 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001868 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001869 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001870 /* fall through */
1871 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02001872 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001873 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001874 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 break;
1876 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02001877 _PyErr_SetString(tstate, PyExc_SystemError,
1878 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 break;
1880 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001881 goto error;
1882 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001883
Benjamin Petersonddd19492018-09-16 22:38:02 -07001884 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001886 assert(f->f_iblock == 0);
Pablo Galindof00828a2019-05-09 16:52:02 +01001887 goto exit_returning;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001888 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001889
Benjamin Petersonddd19492018-09-16 22:38:02 -07001890 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001891 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001892 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001893 PyObject *obj = TOP();
1894 PyTypeObject *type = Py_TYPE(obj);
1895
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001896 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001897 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001898 }
Yury Selivanov75445082015-05-11 22:57:16 -04001899
1900 if (getter != NULL) {
1901 iter = (*getter)(obj);
1902 Py_DECREF(obj);
1903 if (iter == NULL) {
1904 SET_TOP(NULL);
1905 goto error;
1906 }
1907 }
1908 else {
1909 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001910 _PyErr_Format(tstate, PyExc_TypeError,
1911 "'async for' requires an object with "
1912 "__aiter__ method, got %.100s",
1913 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001914 Py_DECREF(obj);
1915 goto error;
1916 }
1917
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001918 if (Py_TYPE(iter)->tp_as_async == NULL ||
1919 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001920
Yury Selivanov398ff912017-03-02 22:20:00 -05001921 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001922 _PyErr_Format(tstate, PyExc_TypeError,
1923 "'async for' received an object from __aiter__ "
1924 "that does not implement __anext__: %.100s",
1925 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001926 Py_DECREF(iter);
1927 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001928 }
1929
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001930 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001931 DISPATCH();
1932 }
1933
Benjamin Petersonddd19492018-09-16 22:38:02 -07001934 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001935 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001936 PyObject *next_iter = NULL;
1937 PyObject *awaitable = NULL;
1938 PyObject *aiter = TOP();
1939 PyTypeObject *type = Py_TYPE(aiter);
1940
Yury Selivanoveb636452016-09-08 22:01:51 -07001941 if (PyAsyncGen_CheckExact(aiter)) {
1942 awaitable = type->tp_as_async->am_anext(aiter);
1943 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001944 goto error;
1945 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001946 } else {
1947 if (type->tp_as_async != NULL){
1948 getter = type->tp_as_async->am_anext;
1949 }
Yury Selivanov75445082015-05-11 22:57:16 -04001950
Yury Selivanoveb636452016-09-08 22:01:51 -07001951 if (getter != NULL) {
1952 next_iter = (*getter)(aiter);
1953 if (next_iter == NULL) {
1954 goto error;
1955 }
1956 }
1957 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02001958 _PyErr_Format(tstate, PyExc_TypeError,
1959 "'async for' requires an iterator with "
1960 "__anext__ method, got %.100s",
1961 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07001962 goto error;
1963 }
Yury Selivanov75445082015-05-11 22:57:16 -04001964
Yury Selivanoveb636452016-09-08 22:01:51 -07001965 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1966 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001967 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001968 PyExc_TypeError,
1969 "'async for' received an invalid object "
1970 "from __anext__: %.100s",
1971 Py_TYPE(next_iter)->tp_name);
1972
1973 Py_DECREF(next_iter);
1974 goto error;
1975 } else {
1976 Py_DECREF(next_iter);
1977 }
1978 }
Yury Selivanov75445082015-05-11 22:57:16 -04001979
1980 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001981 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001982 DISPATCH();
1983 }
1984
Benjamin Petersonddd19492018-09-16 22:38:02 -07001985 case TARGET(GET_AWAITABLE): {
1986 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001987 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001988 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001989
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03001990 if (iter == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001991 format_awaitable_error(tstate, Py_TYPE(iterable),
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03001992 _Py_OPCODE(next_instr[-2]));
1993 }
1994
Yury Selivanov75445082015-05-11 22:57:16 -04001995 Py_DECREF(iterable);
1996
Yury Selivanovc724bae2016-03-02 11:30:46 -05001997 if (iter != NULL && PyCoro_CheckExact(iter)) {
1998 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1999 if (yf != NULL) {
2000 /* `iter` is a coroutine object that is being
2001 awaited, `yf` is a pointer to the current awaitable
2002 being awaited on. */
2003 Py_DECREF(yf);
2004 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002005 _PyErr_SetString(tstate, PyExc_RuntimeError,
2006 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002007 /* The code below jumps to `error` if `iter` is NULL. */
2008 }
2009 }
2010
Yury Selivanov75445082015-05-11 22:57:16 -04002011 SET_TOP(iter); /* Even if it's NULL */
2012
2013 if (iter == NULL) {
2014 goto error;
2015 }
2016
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002017 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002018 DISPATCH();
2019 }
2020
Benjamin Petersonddd19492018-09-16 22:38:02 -07002021 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002022 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002023 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002024 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002025 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2026 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002027 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002028 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002029 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002030 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002031 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002032 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002033 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002034 Py_DECREF(v);
2035 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002036 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002037 if (tstate->c_tracefunc != NULL
Victor Stinner438a12d2019-05-24 17:01:38 +02002038 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002039 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002040 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002041 if (err < 0)
2042 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002043 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002044 SET_TOP(val);
2045 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002046 }
Martin Panter95f53c12016-07-18 08:23:26 +00002047 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002048 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002049 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002050 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002051 f->f_lasti -= sizeof(_Py_CODEUNIT);
Pablo Galindof00828a2019-05-09 16:52:02 +01002052 goto exit_yielding;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002053 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002054
Benjamin Petersonddd19492018-09-16 22:38:02 -07002055 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002056 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002057
2058 if (co->co_flags & CO_ASYNC_GENERATOR) {
2059 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2060 Py_DECREF(retval);
2061 if (w == NULL) {
2062 retval = NULL;
2063 goto error;
2064 }
2065 retval = w;
2066 }
2067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002068 f->f_stacktop = stack_pointer;
Pablo Galindof00828a2019-05-09 16:52:02 +01002069 goto exit_yielding;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002070 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002071
Benjamin Petersonddd19492018-09-16 22:38:02 -07002072 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002073 PyObject *type, *value, *traceback;
2074 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002075 PyTryBlock *b = PyFrame_BlockPop(f);
2076 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002077 _PyErr_SetString(tstate, PyExc_SystemError,
2078 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002079 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002080 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002081 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2082 STACK_LEVEL() <= (b)->b_level + 4);
2083 exc_info = tstate->exc_info;
2084 type = exc_info->exc_type;
2085 value = exc_info->exc_value;
2086 traceback = exc_info->exc_traceback;
2087 exc_info->exc_type = POP();
2088 exc_info->exc_value = POP();
2089 exc_info->exc_traceback = POP();
2090 Py_XDECREF(type);
2091 Py_XDECREF(value);
2092 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002093 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002094 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002095
Benjamin Petersonddd19492018-09-16 22:38:02 -07002096 case TARGET(POP_BLOCK): {
2097 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002098 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002099 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002100 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002101
Benjamin Petersonddd19492018-09-16 22:38:02 -07002102 case TARGET(POP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002103 /* If oparg is 0 at the top of the stack are 1 or 6 values:
2104 Either:
2105 - TOP = NULL or an integer
2106 or:
2107 - (TOP, SECOND, THIRD) = exc_info()
2108 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2109
2110 If oparg is 1 the value for 'return' was additionally pushed
2111 at the top of the stack.
2112 */
2113 PyObject *res = NULL;
2114 if (oparg) {
2115 res = POP();
2116 }
2117 PyObject *exc = POP();
2118 if (exc == NULL || PyLong_CheckExact(exc)) {
2119 Py_XDECREF(exc);
2120 }
2121 else {
2122 Py_DECREF(exc);
2123 Py_DECREF(POP());
2124 Py_DECREF(POP());
2125
2126 PyObject *type, *value, *traceback;
2127 _PyErr_StackItem *exc_info;
2128 PyTryBlock *b = PyFrame_BlockPop(f);
2129 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002130 _PyErr_SetString(tstate, PyExc_SystemError,
2131 "popped block is not an except handler");
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002132 Py_XDECREF(res);
2133 goto error;
2134 }
2135 assert(STACK_LEVEL() == (b)->b_level + 3);
2136 exc_info = tstate->exc_info;
2137 type = exc_info->exc_type;
2138 value = exc_info->exc_value;
2139 traceback = exc_info->exc_traceback;
2140 exc_info->exc_type = POP();
2141 exc_info->exc_value = POP();
2142 exc_info->exc_traceback = POP();
2143 Py_XDECREF(type);
2144 Py_XDECREF(value);
2145 Py_XDECREF(traceback);
2146 }
2147 if (oparg) {
2148 PUSH(res);
2149 }
2150 DISPATCH();
2151 }
2152
Benjamin Petersonddd19492018-09-16 22:38:02 -07002153 case TARGET(CALL_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002154 PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
2155 if (ret == NULL) {
2156 goto error;
2157 }
2158 PUSH(ret);
2159 JUMPBY(oparg);
2160 FAST_DISPATCH();
2161 }
2162
Benjamin Petersonddd19492018-09-16 22:38:02 -07002163 case TARGET(BEGIN_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002164 /* Push NULL onto the stack for using it in END_FINALLY,
2165 POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH.
2166 */
2167 PUSH(NULL);
2168 FAST_DISPATCH();
2169 }
2170
Benjamin Petersonddd19492018-09-16 22:38:02 -07002171 case TARGET(END_FINALLY): {
2172 PREDICTED(END_FINALLY);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002173 /* At the top of the stack are 1 or 6 values:
2174 Either:
2175 - TOP = NULL or an integer
2176 or:
2177 - (TOP, SECOND, THIRD) = exc_info()
2178 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2179 */
2180 PyObject *exc = POP();
2181 if (exc == NULL) {
2182 FAST_DISPATCH();
2183 }
2184 else if (PyLong_CheckExact(exc)) {
2185 int ret = _PyLong_AsInt(exc);
2186 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02002187 if (ret == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002188 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002189 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002190 JUMPTO(ret);
2191 FAST_DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002193 else {
2194 assert(PyExceptionClass_Check(exc));
2195 PyObject *val = POP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002196 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002197 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002198 goto exception_unwind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002200 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002201
Benjamin Petersonddd19492018-09-16 22:38:02 -07002202 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002203 PyObject *exc = POP();
2204 assert(PyExceptionClass_Check(exc));
2205 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2206 PyTryBlock *b = PyFrame_BlockPop(f);
2207 assert(b->b_type == EXCEPT_HANDLER);
2208 Py_DECREF(exc);
2209 UNWIND_EXCEPT_HANDLER(b);
2210 Py_DECREF(POP());
2211 JUMPBY(oparg);
2212 FAST_DISPATCH();
2213 }
2214 else {
2215 PyObject *val = POP();
2216 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002217 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002218 goto exception_unwind;
2219 }
2220 }
2221
Benjamin Petersonddd19492018-09-16 22:38:02 -07002222 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002223 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002224
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002225 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002226 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002227 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002228 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002229 if (!_PyErr_Occurred(tstate)) {
2230 _PyErr_SetString(tstate, PyExc_NameError,
2231 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002232 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002233 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002234 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002235 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002236 }
2237 else {
2238 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2239 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002240 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002241 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2242 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002243 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2244 _PyErr_SetString(tstate, PyExc_NameError,
2245 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002246 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002247 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002248 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002249 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002250 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002251 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002252
Benjamin Petersonddd19492018-09-16 22:38:02 -07002253 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002254 PyObject *name = GETITEM(names, oparg);
2255 PyObject *v = POP();
2256 PyObject *ns = f->f_locals;
2257 int err;
2258 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002259 _PyErr_Format(tstate, PyExc_SystemError,
2260 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002262 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002264 if (PyDict_CheckExact(ns))
2265 err = PyDict_SetItem(ns, name, v);
2266 else
2267 err = PyObject_SetItem(ns, name, v);
2268 Py_DECREF(v);
2269 if (err != 0)
2270 goto error;
2271 DISPATCH();
2272 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002273
Benjamin Petersonddd19492018-09-16 22:38:02 -07002274 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002275 PyObject *name = GETITEM(names, oparg);
2276 PyObject *ns = f->f_locals;
2277 int err;
2278 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002279 _PyErr_Format(tstate, PyExc_SystemError,
2280 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002281 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002282 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002283 err = PyObject_DelItem(ns, name);
2284 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002285 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002286 NAME_ERROR_MSG,
2287 name);
2288 goto error;
2289 }
2290 DISPATCH();
2291 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002292
Benjamin Petersonddd19492018-09-16 22:38:02 -07002293 case TARGET(UNPACK_SEQUENCE): {
2294 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002295 PyObject *seq = POP(), *item, **items;
2296 if (PyTuple_CheckExact(seq) &&
2297 PyTuple_GET_SIZE(seq) == oparg) {
2298 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002299 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002300 item = items[oparg];
2301 Py_INCREF(item);
2302 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002303 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002304 } else if (PyList_CheckExact(seq) &&
2305 PyList_GET_SIZE(seq) == oparg) {
2306 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002308 item = items[oparg];
2309 Py_INCREF(item);
2310 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002312 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002314 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 } else {
2316 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002317 Py_DECREF(seq);
2318 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002320 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002321 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002322 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002323
Benjamin Petersonddd19492018-09-16 22:38:02 -07002324 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002325 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2326 PyObject *seq = POP();
2327
Victor Stinner438a12d2019-05-24 17:01:38 +02002328 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002329 stack_pointer + totalargs)) {
2330 stack_pointer += totalargs;
2331 } else {
2332 Py_DECREF(seq);
2333 goto error;
2334 }
2335 Py_DECREF(seq);
2336 DISPATCH();
2337 }
2338
Benjamin Petersonddd19492018-09-16 22:38:02 -07002339 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002340 PyObject *name = GETITEM(names, oparg);
2341 PyObject *owner = TOP();
2342 PyObject *v = SECOND();
2343 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002344 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002345 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002347 Py_DECREF(owner);
2348 if (err != 0)
2349 goto error;
2350 DISPATCH();
2351 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002352
Benjamin Petersonddd19492018-09-16 22:38:02 -07002353 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002354 PyObject *name = GETITEM(names, oparg);
2355 PyObject *owner = POP();
2356 int err;
2357 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2358 Py_DECREF(owner);
2359 if (err != 0)
2360 goto error;
2361 DISPATCH();
2362 }
2363
Benjamin Petersonddd19492018-09-16 22:38:02 -07002364 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002365 PyObject *name = GETITEM(names, oparg);
2366 PyObject *v = POP();
2367 int err;
2368 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002370 if (err != 0)
2371 goto error;
2372 DISPATCH();
2373 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002374
Benjamin Petersonddd19492018-09-16 22:38:02 -07002375 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002376 PyObject *name = GETITEM(names, oparg);
2377 int err;
2378 err = PyDict_DelItem(f->f_globals, name);
2379 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002380 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2381 format_exc_check_arg(tstate, PyExc_NameError,
2382 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002383 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002384 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002385 }
2386 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002387 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002388
Benjamin Petersonddd19492018-09-16 22:38:02 -07002389 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002390 PyObject *name = GETITEM(names, oparg);
2391 PyObject *locals = f->f_locals;
2392 PyObject *v;
2393 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002394 _PyErr_Format(tstate, PyExc_SystemError,
2395 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002396 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002397 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002398 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002399 v = PyDict_GetItemWithError(locals, name);
2400 if (v != NULL) {
2401 Py_INCREF(v);
2402 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002403 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002404 goto error;
2405 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002406 }
2407 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002408 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002409 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002410 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002411 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002412 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413 }
2414 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002415 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002416 v = PyDict_GetItemWithError(f->f_globals, name);
2417 if (v != NULL) {
2418 Py_INCREF(v);
2419 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002420 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002421 goto error;
2422 }
2423 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002424 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002425 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002426 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002427 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002428 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002429 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002430 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002431 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002432 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002433 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002434 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002435 }
2436 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002437 v = PyObject_GetItem(f->f_builtins, name);
2438 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002439 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002440 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002441 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002442 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002443 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002444 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002445 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002446 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002447 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002448 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002449 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002450 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002451 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002452
Benjamin Petersonddd19492018-09-16 22:38:02 -07002453 case TARGET(LOAD_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002454 PyObject *name = GETITEM(names, oparg);
2455 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002456 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002457 && PyDict_CheckExact(f->f_builtins))
2458 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002459 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002460 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002461 name);
2462 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002463 if (!_PyErr_OCCURRED()) {
2464 /* _PyDict_LoadGlobal() returns NULL without raising
2465 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002466 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002467 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002468 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002469 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002471 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002472 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002473 else {
2474 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002475
2476 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002477 v = PyObject_GetItem(f->f_globals, name);
2478 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002479 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002480 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002481 }
2482 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002483
Victor Stinnerb4efc962015-11-20 09:24:02 +01002484 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002485 v = PyObject_GetItem(f->f_builtins, name);
2486 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002487 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002488 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002489 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002490 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002491 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002492 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002493 }
2494 }
2495 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002496 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002497 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002498 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002499
Benjamin Petersonddd19492018-09-16 22:38:02 -07002500 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002501 PyObject *v = GETLOCAL(oparg);
2502 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002503 SETLOCAL(oparg, NULL);
2504 DISPATCH();
2505 }
2506 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002507 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002508 UNBOUNDLOCAL_ERROR_MSG,
2509 PyTuple_GetItem(co->co_varnames, oparg)
2510 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002511 goto error;
2512 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002513
Benjamin Petersonddd19492018-09-16 22:38:02 -07002514 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002515 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002516 PyObject *oldobj = PyCell_GET(cell);
2517 if (oldobj != NULL) {
2518 PyCell_SET(cell, NULL);
2519 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002520 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002521 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002522 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002523 goto error;
2524 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002525
Benjamin Petersonddd19492018-09-16 22:38:02 -07002526 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002527 PyObject *cell = freevars[oparg];
2528 Py_INCREF(cell);
2529 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002530 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002531 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002532
Benjamin Petersonddd19492018-09-16 22:38:02 -07002533 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002534 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002535 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002536 assert(locals);
2537 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2538 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2539 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2540 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2541 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002542 value = PyDict_GetItemWithError(locals, name);
2543 if (value != NULL) {
2544 Py_INCREF(value);
2545 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002546 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002547 goto error;
2548 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002549 }
2550 else {
2551 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002552 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002553 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002554 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002555 }
2556 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002557 }
2558 }
2559 if (!value) {
2560 PyObject *cell = freevars[oparg];
2561 value = PyCell_GET(cell);
2562 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002563 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002564 goto error;
2565 }
2566 Py_INCREF(value);
2567 }
2568 PUSH(value);
2569 DISPATCH();
2570 }
2571
Benjamin Petersonddd19492018-09-16 22:38:02 -07002572 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002573 PyObject *cell = freevars[oparg];
2574 PyObject *value = PyCell_GET(cell);
2575 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002576 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002577 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002578 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002579 Py_INCREF(value);
2580 PUSH(value);
2581 DISPATCH();
2582 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002583
Benjamin Petersonddd19492018-09-16 22:38:02 -07002584 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002585 PyObject *v = POP();
2586 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002587 PyObject *oldobj = PyCell_GET(cell);
2588 PyCell_SET(cell, v);
2589 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002590 DISPATCH();
2591 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002592
Benjamin Petersonddd19492018-09-16 22:38:02 -07002593 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002594 PyObject *str;
2595 PyObject *empty = PyUnicode_New(0, 0);
2596 if (empty == NULL) {
2597 goto error;
2598 }
2599 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2600 Py_DECREF(empty);
2601 if (str == NULL)
2602 goto error;
2603 while (--oparg >= 0) {
2604 PyObject *item = POP();
2605 Py_DECREF(item);
2606 }
2607 PUSH(str);
2608 DISPATCH();
2609 }
2610
Benjamin Petersonddd19492018-09-16 22:38:02 -07002611 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002612 PyObject *tup = PyTuple_New(oparg);
2613 if (tup == NULL)
2614 goto error;
2615 while (--oparg >= 0) {
2616 PyObject *item = POP();
2617 PyTuple_SET_ITEM(tup, oparg, item);
2618 }
2619 PUSH(tup);
2620 DISPATCH();
2621 }
2622
Benjamin Petersonddd19492018-09-16 22:38:02 -07002623 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002624 PyObject *list = PyList_New(oparg);
2625 if (list == NULL)
2626 goto error;
2627 while (--oparg >= 0) {
2628 PyObject *item = POP();
2629 PyList_SET_ITEM(list, oparg, item);
2630 }
2631 PUSH(list);
2632 DISPATCH();
2633 }
2634
Benjamin Petersonddd19492018-09-16 22:38:02 -07002635 case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL):
2636 case TARGET(BUILD_TUPLE_UNPACK):
2637 case TARGET(BUILD_LIST_UNPACK): {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002638 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002639 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002640 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002641 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002642
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002643 if (sum == NULL)
2644 goto error;
2645
2646 for (i = oparg; i > 0; i--) {
2647 PyObject *none_val;
2648
2649 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2650 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002651 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Victor Stinner438a12d2019-05-24 17:01:38 +02002652 _PyErr_ExceptionMatches(tstate, PyExc_TypeError))
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002653 {
Victor Stinner438a12d2019-05-24 17:01:38 +02002654 check_args_iterable(tstate, PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002655 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002656 Py_DECREF(sum);
2657 goto error;
2658 }
2659 Py_DECREF(none_val);
2660 }
2661
2662 if (convert_to_tuple) {
2663 return_value = PyList_AsTuple(sum);
2664 Py_DECREF(sum);
2665 if (return_value == NULL)
2666 goto error;
2667 }
2668 else {
2669 return_value = sum;
2670 }
2671
2672 while (oparg--)
2673 Py_DECREF(POP());
2674 PUSH(return_value);
2675 DISPATCH();
2676 }
2677
Benjamin Petersonddd19492018-09-16 22:38:02 -07002678 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002679 PyObject *set = PySet_New(NULL);
2680 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002681 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002682 if (set == NULL)
2683 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002684 for (i = oparg; i > 0; i--) {
2685 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002686 if (err == 0)
2687 err = PySet_Add(set, item);
2688 Py_DECREF(item);
2689 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002690 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002691 if (err != 0) {
2692 Py_DECREF(set);
2693 goto error;
2694 }
2695 PUSH(set);
2696 DISPATCH();
2697 }
2698
Benjamin Petersonddd19492018-09-16 22:38:02 -07002699 case TARGET(BUILD_SET_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002700 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002701 PyObject *sum = PySet_New(NULL);
2702 if (sum == NULL)
2703 goto error;
2704
2705 for (i = oparg; i > 0; i--) {
2706 if (_PySet_Update(sum, PEEK(i)) < 0) {
2707 Py_DECREF(sum);
2708 goto error;
2709 }
2710 }
2711
2712 while (oparg--)
2713 Py_DECREF(POP());
2714 PUSH(sum);
2715 DISPATCH();
2716 }
2717
Benjamin Petersonddd19492018-09-16 22:38:02 -07002718 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002719 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002720 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2721 if (map == NULL)
2722 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002723 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002724 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002725 PyObject *key = PEEK(2*i);
2726 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002727 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002728 if (err != 0) {
2729 Py_DECREF(map);
2730 goto error;
2731 }
2732 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002733
2734 while (oparg--) {
2735 Py_DECREF(POP());
2736 Py_DECREF(POP());
2737 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002738 PUSH(map);
2739 DISPATCH();
2740 }
2741
Benjamin Petersonddd19492018-09-16 22:38:02 -07002742 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002743 _Py_IDENTIFIER(__annotations__);
2744 int err;
2745 PyObject *ann_dict;
2746 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002747 _PyErr_Format(tstate, PyExc_SystemError,
2748 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002749 goto error;
2750 }
2751 /* check if __annotations__ in locals()... */
2752 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002753 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002754 &PyId___annotations__);
2755 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002756 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002757 goto error;
2758 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002759 /* ...if not, create a new one */
2760 ann_dict = PyDict_New();
2761 if (ann_dict == NULL) {
2762 goto error;
2763 }
2764 err = _PyDict_SetItemId(f->f_locals,
2765 &PyId___annotations__, ann_dict);
2766 Py_DECREF(ann_dict);
2767 if (err != 0) {
2768 goto error;
2769 }
2770 }
2771 }
2772 else {
2773 /* do the same if locals() is not a dict */
2774 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2775 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002776 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002777 }
2778 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2779 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002780 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002781 goto error;
2782 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002783 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002784 ann_dict = PyDict_New();
2785 if (ann_dict == NULL) {
2786 goto error;
2787 }
2788 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2789 Py_DECREF(ann_dict);
2790 if (err != 0) {
2791 goto error;
2792 }
2793 }
2794 else {
2795 Py_DECREF(ann_dict);
2796 }
2797 }
2798 DISPATCH();
2799 }
2800
Benjamin Petersonddd19492018-09-16 22:38:02 -07002801 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002802 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002803 PyObject *map;
2804 PyObject *keys = TOP();
2805 if (!PyTuple_CheckExact(keys) ||
2806 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002807 _PyErr_SetString(tstate, PyExc_SystemError,
2808 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002809 goto error;
2810 }
2811 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2812 if (map == NULL) {
2813 goto error;
2814 }
2815 for (i = oparg; i > 0; i--) {
2816 int err;
2817 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2818 PyObject *value = PEEK(i + 1);
2819 err = PyDict_SetItem(map, key, value);
2820 if (err != 0) {
2821 Py_DECREF(map);
2822 goto error;
2823 }
2824 }
2825
2826 Py_DECREF(POP());
2827 while (oparg--) {
2828 Py_DECREF(POP());
2829 }
2830 PUSH(map);
2831 DISPATCH();
2832 }
2833
Benjamin Petersonddd19492018-09-16 22:38:02 -07002834 case TARGET(BUILD_MAP_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002835 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002836 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002837 if (sum == NULL)
2838 goto error;
2839
2840 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002841 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002842 if (PyDict_Update(sum, arg) < 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002843 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2844 _PyErr_Format(tstate, PyExc_TypeError,
2845 "'%.200s' object is not a mapping",
2846 arg->ob_type->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002847 }
2848 Py_DECREF(sum);
2849 goto error;
2850 }
2851 }
2852
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002853 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002854 Py_DECREF(POP());
2855 PUSH(sum);
2856 DISPATCH();
2857 }
2858
Benjamin Petersonddd19492018-09-16 22:38:02 -07002859 case TARGET(BUILD_MAP_UNPACK_WITH_CALL): {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002860 Py_ssize_t i;
2861 PyObject *sum = PyDict_New();
2862 if (sum == NULL)
2863 goto error;
2864
2865 for (i = oparg; i > 0; i--) {
2866 PyObject *arg = PEEK(i);
2867 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002868 Py_DECREF(sum);
Victor Stinner438a12d2019-05-24 17:01:38 +02002869 format_kwargs_error(tstate, PEEK(2 + oparg), arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002870 goto error;
2871 }
2872 }
2873
2874 while (oparg--)
2875 Py_DECREF(POP());
2876 PUSH(sum);
2877 DISPATCH();
2878 }
2879
Benjamin Petersonddd19492018-09-16 22:38:02 -07002880 case TARGET(MAP_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002881 PyObject *key = TOP();
2882 PyObject *value = SECOND();
2883 PyObject *map;
2884 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002885 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002886 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002887 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002888 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002889 Py_DECREF(value);
2890 Py_DECREF(key);
2891 if (err != 0)
2892 goto error;
2893 PREDICT(JUMP_ABSOLUTE);
2894 DISPATCH();
2895 }
2896
Benjamin Petersonddd19492018-09-16 22:38:02 -07002897 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002898 PyObject *name = GETITEM(names, oparg);
2899 PyObject *owner = TOP();
2900 PyObject *res = PyObject_GetAttr(owner, name);
2901 Py_DECREF(owner);
2902 SET_TOP(res);
2903 if (res == NULL)
2904 goto error;
2905 DISPATCH();
2906 }
2907
Benjamin Petersonddd19492018-09-16 22:38:02 -07002908 case TARGET(COMPARE_OP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002909 PyObject *right = POP();
2910 PyObject *left = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002911 PyObject *res = cmp_outcome(tstate, oparg, left, right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002912 Py_DECREF(left);
2913 Py_DECREF(right);
2914 SET_TOP(res);
2915 if (res == NULL)
2916 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 PREDICT(POP_JUMP_IF_FALSE);
2918 PREDICT(POP_JUMP_IF_TRUE);
2919 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002920 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002921
Benjamin Petersonddd19492018-09-16 22:38:02 -07002922 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002923 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002924 PyObject *fromlist = POP();
2925 PyObject *level = TOP();
2926 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02002927 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002928 Py_DECREF(level);
2929 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002930 SET_TOP(res);
2931 if (res == NULL)
2932 goto error;
2933 DISPATCH();
2934 }
2935
Benjamin Petersonddd19492018-09-16 22:38:02 -07002936 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002937 PyObject *from = POP(), *locals;
2938 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002939 if (PyFrame_FastToLocalsWithError(f) < 0) {
2940 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002941 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002942 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002943
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002944 locals = f->f_locals;
2945 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002946 _PyErr_SetString(tstate, PyExc_SystemError,
2947 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002948 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002949 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002950 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002951 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002952 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002953 Py_DECREF(from);
2954 if (err != 0)
2955 goto error;
2956 DISPATCH();
2957 }
Guido van Rossum25831651993-05-19 14:50:45 +00002958
Benjamin Petersonddd19492018-09-16 22:38:02 -07002959 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002960 PyObject *name = GETITEM(names, oparg);
2961 PyObject *from = TOP();
2962 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02002963 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002964 PUSH(res);
2965 if (res == NULL)
2966 goto error;
2967 DISPATCH();
2968 }
Thomas Wouters52152252000-08-17 22:55:00 +00002969
Benjamin Petersonddd19492018-09-16 22:38:02 -07002970 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002971 JUMPBY(oparg);
2972 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002973 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002974
Benjamin Petersonddd19492018-09-16 22:38:02 -07002975 case TARGET(POP_JUMP_IF_FALSE): {
2976 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002977 PyObject *cond = POP();
2978 int err;
2979 if (cond == Py_True) {
2980 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002981 FAST_DISPATCH();
2982 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002983 if (cond == Py_False) {
2984 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002985 JUMPTO(oparg);
2986 FAST_DISPATCH();
2987 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002988 err = PyObject_IsTrue(cond);
2989 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002990 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002991 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002992 else if (err == 0)
2993 JUMPTO(oparg);
2994 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002995 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002997 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002998
Benjamin Petersonddd19492018-09-16 22:38:02 -07002999 case TARGET(POP_JUMP_IF_TRUE): {
3000 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003001 PyObject *cond = POP();
3002 int err;
3003 if (cond == Py_False) {
3004 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003005 FAST_DISPATCH();
3006 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003007 if (cond == Py_True) {
3008 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003009 JUMPTO(oparg);
3010 FAST_DISPATCH();
3011 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003012 err = PyObject_IsTrue(cond);
3013 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003014 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003015 JUMPTO(oparg);
3016 }
3017 else if (err == 0)
3018 ;
3019 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003020 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003021 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003022 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003023
Benjamin Petersonddd19492018-09-16 22:38:02 -07003024 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003025 PyObject *cond = TOP();
3026 int err;
3027 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003028 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003029 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003030 FAST_DISPATCH();
3031 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003032 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003033 JUMPTO(oparg);
3034 FAST_DISPATCH();
3035 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003036 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003038 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003039 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003040 }
3041 else if (err == 0)
3042 JUMPTO(oparg);
3043 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003044 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003045 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003046 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003047
Benjamin Petersonddd19492018-09-16 22:38:02 -07003048 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003049 PyObject *cond = TOP();
3050 int err;
3051 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003052 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003053 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003054 FAST_DISPATCH();
3055 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003056 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003057 JUMPTO(oparg);
3058 FAST_DISPATCH();
3059 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003060 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003061 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003062 JUMPTO(oparg);
3063 }
3064 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003065 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003066 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003067 }
3068 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003069 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003070 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003071 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003072
Benjamin Petersonddd19492018-09-16 22:38:02 -07003073 case TARGET(JUMP_ABSOLUTE): {
3074 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003075 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003076#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003077 /* Enabling this path speeds-up all while and for-loops by bypassing
3078 the per-loop checks for signals. By default, this should be turned-off
3079 because it prevents detection of a control-break in tight loops like
3080 "while 1: pass". Compile with this option turned-on when you need
3081 the speed-up and do not need break checking inside tight loops (ones
3082 that contain only instructions ending with FAST_DISPATCH).
3083 */
3084 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003085#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003086 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003087#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003088 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003089
Benjamin Petersonddd19492018-09-16 22:38:02 -07003090 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003091 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003092 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003093 PyObject *iter = PyObject_GetIter(iterable);
3094 Py_DECREF(iterable);
3095 SET_TOP(iter);
3096 if (iter == NULL)
3097 goto error;
3098 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003099 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003100 DISPATCH();
3101 }
3102
Benjamin Petersonddd19492018-09-16 22:38:02 -07003103 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003104 /* before: [obj]; after [getiter(obj)] */
3105 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003106 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003107 if (PyCoro_CheckExact(iterable)) {
3108 /* `iterable` is a coroutine */
3109 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3110 /* and it is used in a 'yield from' expression of a
3111 regular generator. */
3112 Py_DECREF(iterable);
3113 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003114 _PyErr_SetString(tstate, PyExc_TypeError,
3115 "cannot 'yield from' a coroutine object "
3116 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003117 goto error;
3118 }
3119 }
3120 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003121 /* `iterable` is not a generator. */
3122 iter = PyObject_GetIter(iterable);
3123 Py_DECREF(iterable);
3124 SET_TOP(iter);
3125 if (iter == NULL)
3126 goto error;
3127 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003128 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003129 DISPATCH();
3130 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003131
Benjamin Petersonddd19492018-09-16 22:38:02 -07003132 case TARGET(FOR_ITER): {
3133 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003134 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003135 PyObject *iter = TOP();
3136 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3137 if (next != NULL) {
3138 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003139 PREDICT(STORE_FAST);
3140 PREDICT(UNPACK_SEQUENCE);
3141 DISPATCH();
3142 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003143 if (_PyErr_Occurred(tstate)) {
3144 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003145 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003146 }
3147 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003148 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003149 }
3150 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003151 }
3152 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003153 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003154 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003155 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003156 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003157 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003158 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003159
Benjamin Petersonddd19492018-09-16 22:38:02 -07003160 case TARGET(SETUP_FINALLY): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003161 /* NOTE: If you add any new block-setup opcodes that
3162 are not try/except/finally handlers, you may need
3163 to update the PyGen_NeedsFinalizing() function.
3164 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003165
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003166 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003167 STACK_LEVEL());
3168 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003169 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003170
Benjamin Petersonddd19492018-09-16 22:38:02 -07003171 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003172 _Py_IDENTIFIER(__aexit__);
3173 _Py_IDENTIFIER(__aenter__);
3174
3175 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003176 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__),
Yury Selivanov75445082015-05-11 22:57:16 -04003177 *enter;
3178 PyObject *res;
3179 if (exit == NULL)
3180 goto error;
3181 SET_TOP(exit);
Victor Stinner438a12d2019-05-24 17:01:38 +02003182 enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003183 Py_DECREF(mgr);
3184 if (enter == NULL)
3185 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003186 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003187 Py_DECREF(enter);
3188 if (res == NULL)
3189 goto error;
3190 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003191 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003192 DISPATCH();
3193 }
3194
Benjamin Petersonddd19492018-09-16 22:38:02 -07003195 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003196 PyObject *res = POP();
3197 /* Setup the finally block before pushing the result
3198 of __aenter__ on the stack. */
3199 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3200 STACK_LEVEL());
3201 PUSH(res);
3202 DISPATCH();
3203 }
3204
Benjamin Petersonddd19492018-09-16 22:38:02 -07003205 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003206 _Py_IDENTIFIER(__exit__);
3207 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003208 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003209 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003210 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003211 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003212 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003213 }
3214 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003215 if (exit == NULL) {
3216 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003217 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003218 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003219 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003220 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003221 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003222 Py_DECREF(enter);
3223 if (res == NULL)
3224 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225 /* Setup the finally block before pushing the result
3226 of __enter__ on the stack. */
3227 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3228 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003229
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003230 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003231 DISPATCH();
3232 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003233
Benjamin Petersonddd19492018-09-16 22:38:02 -07003234 case TARGET(WITH_CLEANUP_START): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003235 /* At the top of the stack are 1 or 6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003236 how/why we entered the finally clause:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003237 - TOP = NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003238 - (TOP, SECOND, THIRD) = exc_info()
3239 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003240 Below them is EXIT, the context.__exit__ or context.__aexit__
3241 bound method.
3242 In the first case, we must call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003243 EXIT(None, None, None)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003244 otherwise we must call
3245 EXIT(TOP, SECOND, THIRD)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003246
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003247 In the first case, we remove EXIT from the
3248 stack, leaving TOP, and push TOP on the stack.
3249 Otherwise we shift the bottom 3 values of the
3250 stack down, replace the empty spot with NULL, and push
3251 None on the stack.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003252
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003253 Finally we push the result of the call.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003254 */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003255 PyObject *stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003256 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003257 PyObject *exc, *val, *tb, *res;
3258
3259 val = tb = Py_None;
3260 exc = TOP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003261 if (exc == NULL) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003262 STACK_SHRINK(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003263 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003264 SET_TOP(exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003265 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003266 }
3267 else {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003268 assert(PyExceptionClass_Check(exc));
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003269 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003270 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003271 val = SECOND();
3272 tb = THIRD();
3273 tp2 = FOURTH();
3274 exc2 = PEEK(5);
3275 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003276 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003277 SET_VALUE(7, tb2);
3278 SET_VALUE(6, exc2);
3279 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003280 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3281 SET_FOURTH(NULL);
3282 /* We just shifted the stack down, so we have
3283 to tell the except handler block that the
3284 values are lower than it expects. */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003285 assert(f->f_iblock > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003286 block = &f->f_blockstack[f->f_iblock - 1];
3287 assert(block->b_type == EXCEPT_HANDLER);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003288 assert(block->b_level > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003289 block->b_level--;
3290 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003291
3292 stack[0] = exc;
3293 stack[1] = val;
3294 stack[2] = tb;
3295 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003296 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003297 if (res == NULL)
3298 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003299
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003300 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003301 PUSH(exc);
3302 PUSH(res);
3303 PREDICT(WITH_CLEANUP_FINISH);
3304 DISPATCH();
3305 }
3306
Benjamin Petersonddd19492018-09-16 22:38:02 -07003307 case TARGET(WITH_CLEANUP_FINISH): {
3308 PREDICTED(WITH_CLEANUP_FINISH);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003309 /* TOP = the result of calling the context.__exit__ bound method
3310 SECOND = either None or exception type
3311
3312 If SECOND is None below is NULL or the return address,
3313 otherwise below are 7 values representing an exception.
3314 */
Yury Selivanov75445082015-05-11 22:57:16 -04003315 PyObject *res = POP();
3316 PyObject *exc = POP();
3317 int err;
3318
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003319 if (exc != Py_None)
3320 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003321 else
3322 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003323
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003324 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003325 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003327 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003328 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003329 else if (err > 0) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003330 /* There was an exception and a True return.
3331 * We must manually unwind the EXCEPT_HANDLER block
3332 * which was created when the exception was caught,
Quan Tian3bd0d622018-10-20 05:30:03 +08003333 * otherwise the stack will be in an inconsistent state.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003334 */
3335 PyTryBlock *b = PyFrame_BlockPop(f);
3336 assert(b->b_type == EXCEPT_HANDLER);
3337 UNWIND_EXCEPT_HANDLER(b);
3338 PUSH(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003339 }
3340 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003341 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003342 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003343
Benjamin Petersonddd19492018-09-16 22:38:02 -07003344 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003345 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003346 PyObject *name = GETITEM(names, oparg);
3347 PyObject *obj = TOP();
3348 PyObject *meth = NULL;
3349
3350 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3351
Yury Selivanovf2392132016-12-13 19:03:51 -05003352 if (meth == NULL) {
3353 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003354 goto error;
3355 }
3356
3357 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003358 /* We can bypass temporary bound method object.
3359 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003360
INADA Naoki015bce62017-01-16 17:23:30 +09003361 meth | self | arg1 | ... | argN
3362 */
3363 SET_TOP(meth);
3364 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003365 }
3366 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003367 /* meth is not an unbound method (but a regular attr, or
3368 something was returned by a descriptor protocol). Set
3369 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003370 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003371
3372 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003373 */
INADA Naoki015bce62017-01-16 17:23:30 +09003374 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003375 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003376 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003377 }
3378 DISPATCH();
3379 }
3380
Benjamin Petersonddd19492018-09-16 22:38:02 -07003381 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003382 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003383 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003384
3385 sp = stack_pointer;
3386
INADA Naoki015bce62017-01-16 17:23:30 +09003387 meth = PEEK(oparg + 2);
3388 if (meth == NULL) {
3389 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3390 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003391
3392 Stack layout:
3393
INADA Naoki015bce62017-01-16 17:23:30 +09003394 ... | NULL | callable | arg1 | ... | argN
3395 ^- TOP()
3396 ^- (-oparg)
3397 ^- (-oparg-1)
3398 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003399
Ville Skyttä49b27342017-08-03 09:00:59 +03003400 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003401 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003402 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003403 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003404 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003405 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003406 }
3407 else {
3408 /* This is a method call. Stack layout:
3409
INADA Naoki015bce62017-01-16 17:23:30 +09003410 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003411 ^- TOP()
3412 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003413 ^- (-oparg-1)
3414 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003415
INADA Naoki015bce62017-01-16 17:23:30 +09003416 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003417 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003418 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003419 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003420 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003421 stack_pointer = sp;
3422 }
3423
3424 PUSH(res);
3425 if (res == NULL)
3426 goto error;
3427 DISPATCH();
3428 }
3429
Benjamin Petersonddd19492018-09-16 22:38:02 -07003430 case TARGET(CALL_FUNCTION): {
3431 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003432 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003433 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003434 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003435 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003436 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003437 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003438 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003439 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003440 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003441 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003442
Benjamin Petersonddd19492018-09-16 22:38:02 -07003443 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003444 PyObject **sp, *res, *names;
3445
3446 names = POP();
3447 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003448 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003449 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003450 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003451 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003452 Py_DECREF(names);
3453
3454 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003455 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003456 }
3457 DISPATCH();
3458 }
3459
Benjamin Petersonddd19492018-09-16 22:38:02 -07003460 case TARGET(CALL_FUNCTION_EX): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003461 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003462 if (oparg & 0x01) {
3463 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003464 if (!PyDict_CheckExact(kwargs)) {
3465 PyObject *d = PyDict_New();
3466 if (d == NULL)
3467 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003468 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003469 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003470 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003471 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003472 goto error;
3473 }
3474 Py_DECREF(kwargs);
3475 kwargs = d;
3476 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003477 assert(PyDict_CheckExact(kwargs));
3478 }
3479 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003480 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003481 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003482 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003483 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003484 goto error;
3485 }
3486 Py_SETREF(callargs, PySequence_Tuple(callargs));
3487 if (callargs == NULL) {
3488 goto error;
3489 }
3490 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003491 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003492
Victor Stinner09532fe2019-05-10 23:39:09 +02003493 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003494 Py_DECREF(func);
3495 Py_DECREF(callargs);
3496 Py_XDECREF(kwargs);
3497
3498 SET_TOP(result);
3499 if (result == NULL) {
3500 goto error;
3501 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003502 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003503 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003504
Benjamin Petersonddd19492018-09-16 22:38:02 -07003505 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003506 PyObject *qualname = POP();
3507 PyObject *codeobj = POP();
3508 PyFunctionObject *func = (PyFunctionObject *)
3509 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003510
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003511 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003512 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003513 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003514 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003515 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003516
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003517 if (oparg & 0x08) {
3518 assert(PyTuple_CheckExact(TOP()));
3519 func ->func_closure = POP();
3520 }
3521 if (oparg & 0x04) {
3522 assert(PyDict_CheckExact(TOP()));
3523 func->func_annotations = POP();
3524 }
3525 if (oparg & 0x02) {
3526 assert(PyDict_CheckExact(TOP()));
3527 func->func_kwdefaults = POP();
3528 }
3529 if (oparg & 0x01) {
3530 assert(PyTuple_CheckExact(TOP()));
3531 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003532 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003533
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003534 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003535 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003536 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003537
Benjamin Petersonddd19492018-09-16 22:38:02 -07003538 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003539 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003540 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003541 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003542 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003543 step = NULL;
3544 stop = POP();
3545 start = TOP();
3546 slice = PySlice_New(start, stop, step);
3547 Py_DECREF(start);
3548 Py_DECREF(stop);
3549 Py_XDECREF(step);
3550 SET_TOP(slice);
3551 if (slice == NULL)
3552 goto error;
3553 DISPATCH();
3554 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003555
Benjamin Petersonddd19492018-09-16 22:38:02 -07003556 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003557 /* Handles f-string value formatting. */
3558 PyObject *result;
3559 PyObject *fmt_spec;
3560 PyObject *value;
3561 PyObject *(*conv_fn)(PyObject *);
3562 int which_conversion = oparg & FVC_MASK;
3563 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3564
3565 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003566 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003567
3568 /* See if any conversion is specified. */
3569 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003570 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003571 case FVC_STR: conv_fn = PyObject_Str; break;
3572 case FVC_REPR: conv_fn = PyObject_Repr; break;
3573 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003574 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003575 _PyErr_Format(tstate, PyExc_SystemError,
3576 "unexpected conversion flag %d",
3577 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003578 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003579 }
3580
3581 /* If there's a conversion function, call it and replace
3582 value with that result. Otherwise, just use value,
3583 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003584 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003585 result = conv_fn(value);
3586 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003587 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003588 Py_XDECREF(fmt_spec);
3589 goto error;
3590 }
3591 value = result;
3592 }
3593
3594 /* If value is a unicode object, and there's no fmt_spec,
3595 then we know the result of format(value) is value
3596 itself. In that case, skip calling format(). I plan to
3597 move this optimization in to PyObject_Format()
3598 itself. */
3599 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3600 /* Do nothing, just transfer ownership to result. */
3601 result = value;
3602 } else {
3603 /* Actually call format(). */
3604 result = PyObject_Format(value, fmt_spec);
3605 Py_DECREF(value);
3606 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003607 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003608 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003609 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003610 }
3611
Eric V. Smith135d5f42016-02-05 18:23:08 -05003612 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003613 DISPATCH();
3614 }
3615
Benjamin Petersonddd19492018-09-16 22:38:02 -07003616 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003617 int oldoparg = oparg;
3618 NEXTOPARG();
3619 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003620 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003621 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003622
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003623
Antoine Pitrou042b1282010-08-13 21:15:58 +00003624#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003625 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003626#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003627 default:
3628 fprintf(stderr,
3629 "XXX lineno: %d, opcode: %d\n",
3630 PyFrame_GetLineNumber(f),
3631 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003632 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003633 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003635 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003636
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003637 /* This should never be reached. Every opcode should end with DISPATCH()
3638 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003639 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003640
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003641error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003642 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003643#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003644 if (!_PyErr_Occurred(tstate)) {
3645 _PyErr_SetString(tstate, PyExc_SystemError,
3646 "error return without exception set");
3647 }
Victor Stinner365b6932013-07-12 00:11:58 +02003648#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003649 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003650#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003651
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003652 /* Log traceback info. */
3653 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003654
Benjamin Peterson51f46162013-01-23 08:38:47 -05003655 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003656 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3657 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003658
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003659exception_unwind:
3660 /* Unwind stacks if an exception occurred */
3661 while (f->f_iblock > 0) {
3662 /* Pop the current block. */
3663 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003665 if (b->b_type == EXCEPT_HANDLER) {
3666 UNWIND_EXCEPT_HANDLER(b);
3667 continue;
3668 }
3669 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003670 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003671 PyObject *exc, *val, *tb;
3672 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003673 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003674 /* Beware, this invalidates all b->b_* fields */
3675 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003676 PUSH(exc_info->exc_traceback);
3677 PUSH(exc_info->exc_value);
3678 if (exc_info->exc_type != NULL) {
3679 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003680 }
3681 else {
3682 Py_INCREF(Py_None);
3683 PUSH(Py_None);
3684 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003685 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003686 /* Make the raw exception data
3687 available to the handler,
3688 so a program can emulate the
3689 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003690 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003691 if (tb != NULL)
3692 PyException_SetTraceback(val, tb);
3693 else
3694 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003695 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003696 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003697 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003698 exc_info->exc_value = val;
3699 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003700 if (tb == NULL)
3701 tb = Py_None;
3702 Py_INCREF(tb);
3703 PUSH(tb);
3704 PUSH(val);
3705 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003706 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003707 /* Resume normal execution */
3708 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003709 }
3710 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003711
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003712 /* End the loop as we still have an error */
3713 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003714 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003715
Pablo Galindof00828a2019-05-09 16:52:02 +01003716 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003717 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003718
3719exit_returning:
3720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003721 /* Pop remaining stack entries. */
3722 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003723 PyObject *o = POP();
3724 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003725 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003726
Pablo Galindof00828a2019-05-09 16:52:02 +01003727exit_yielding:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003728 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003729 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003730 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3731 tstate, f, PyTrace_RETURN, retval)) {
3732 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003733 }
3734 }
3735 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003736 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3737 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003738 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003739 }
3740 }
3741 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003743 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003744exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003745 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3746 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003747 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003748 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003749 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003750
Victor Stinnerefde1462015-03-21 15:04:43 +01003751 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003752}
3753
Benjamin Petersonb204a422011-06-05 22:04:07 -05003754static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003755format_missing(PyThreadState *tstate, const char *kind,
3756 PyCodeObject *co, PyObject *names)
Benjamin Petersone109c702011-06-24 09:37:26 -05003757{
3758 int err;
3759 Py_ssize_t len = PyList_GET_SIZE(names);
3760 PyObject *name_str, *comma, *tail, *tmp;
3761
3762 assert(PyList_CheckExact(names));
3763 assert(len >= 1);
3764 /* Deal with the joys of natural language. */
3765 switch (len) {
3766 case 1:
3767 name_str = PyList_GET_ITEM(names, 0);
3768 Py_INCREF(name_str);
3769 break;
3770 case 2:
3771 name_str = PyUnicode_FromFormat("%U and %U",
3772 PyList_GET_ITEM(names, len - 2),
3773 PyList_GET_ITEM(names, len - 1));
3774 break;
3775 default:
3776 tail = PyUnicode_FromFormat(", %U, and %U",
3777 PyList_GET_ITEM(names, len - 2),
3778 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003779 if (tail == NULL)
3780 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003781 /* Chop off the last two objects in the list. This shouldn't actually
3782 fail, but we can't be too careful. */
3783 err = PyList_SetSlice(names, len - 2, len, NULL);
3784 if (err == -1) {
3785 Py_DECREF(tail);
3786 return;
3787 }
3788 /* Stitch everything up into a nice comma-separated list. */
3789 comma = PyUnicode_FromString(", ");
3790 if (comma == NULL) {
3791 Py_DECREF(tail);
3792 return;
3793 }
3794 tmp = PyUnicode_Join(comma, names);
3795 Py_DECREF(comma);
3796 if (tmp == NULL) {
3797 Py_DECREF(tail);
3798 return;
3799 }
3800 name_str = PyUnicode_Concat(tmp, tail);
3801 Py_DECREF(tmp);
3802 Py_DECREF(tail);
3803 break;
3804 }
3805 if (name_str == NULL)
3806 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003807 _PyErr_Format(tstate, PyExc_TypeError,
3808 "%U() missing %i required %s argument%s: %U",
3809 co->co_name,
3810 len,
3811 kind,
3812 len == 1 ? "" : "s",
3813 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003814 Py_DECREF(name_str);
3815}
3816
3817static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003818missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3819 Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003820 PyObject **fastlocals)
3821{
Victor Stinner74319ae2016-08-25 00:04:09 +02003822 Py_ssize_t i, j = 0;
3823 Py_ssize_t start, end;
3824 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003825 const char *kind = positional ? "positional" : "keyword-only";
3826 PyObject *missing_names;
3827
3828 /* Compute the names of the arguments that are missing. */
3829 missing_names = PyList_New(missing);
3830 if (missing_names == NULL)
3831 return;
3832 if (positional) {
3833 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01003834 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003835 }
3836 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003837 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003838 end = start + co->co_kwonlyargcount;
3839 }
3840 for (i = start; i < end; i++) {
3841 if (GETLOCAL(i) == NULL) {
3842 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3843 PyObject *name = PyObject_Repr(raw);
3844 if (name == NULL) {
3845 Py_DECREF(missing_names);
3846 return;
3847 }
3848 PyList_SET_ITEM(missing_names, j++, name);
3849 }
3850 }
3851 assert(j == missing);
Victor Stinner438a12d2019-05-24 17:01:38 +02003852 format_missing(tstate, kind, co, missing_names);
Benjamin Petersone109c702011-06-24 09:37:26 -05003853 Py_DECREF(missing_names);
3854}
3855
3856static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003857too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3858 Py_ssize_t given, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003859 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003860{
3861 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003862 Py_ssize_t kwonly_given = 0;
3863 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003864 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003865 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003866
Benjamin Petersone109c702011-06-24 09:37:26 -05003867 assert((co->co_flags & CO_VARARGS) == 0);
3868 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01003869 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003870 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003871 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003872 }
3873 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003874 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01003875 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003876 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01003877 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003878 }
3879 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003880 plural = (co_argcount != 1);
3881 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003882 }
3883 if (sig == NULL)
3884 return;
3885 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003886 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3887 kwonly_sig = PyUnicode_FromFormat(format,
3888 given != 1 ? "s" : "",
3889 kwonly_given,
3890 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003891 if (kwonly_sig == NULL) {
3892 Py_DECREF(sig);
3893 return;
3894 }
3895 }
3896 else {
3897 /* This will not fail. */
3898 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003899 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003900 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003901 _PyErr_Format(tstate, PyExc_TypeError,
3902 "%U() takes %U positional argument%s but %zd%U %s given",
3903 co->co_name,
3904 sig,
3905 plural ? "s" : "",
3906 given,
3907 kwonly_sig,
3908 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003909 Py_DECREF(sig);
3910 Py_DECREF(kwonly_sig);
3911}
3912
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003913static int
Victor Stinner438a12d2019-05-24 17:01:38 +02003914positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
3915 Py_ssize_t kwcount, PyObject* const* kwnames)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003916{
3917 int posonly_conflicts = 0;
3918 PyObject* posonly_names = PyList_New(0);
3919
3920 for(int k=0; k < co->co_posonlyargcount; k++){
3921 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
3922
3923 for (int k2=0; k2<kwcount; k2++){
3924 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
3925 PyObject* kwname = kwnames[k2];
3926 if (kwname == posonly_name){
3927 if(PyList_Append(posonly_names, kwname) != 0) {
3928 goto fail;
3929 }
3930 posonly_conflicts++;
3931 continue;
3932 }
3933
3934 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
3935
3936 if ( cmp > 0) {
3937 if(PyList_Append(posonly_names, kwname) != 0) {
3938 goto fail;
3939 }
3940 posonly_conflicts++;
3941 } else if (cmp < 0) {
3942 goto fail;
3943 }
3944
3945 }
3946 }
3947 if (posonly_conflicts) {
3948 PyObject* comma = PyUnicode_FromString(", ");
3949 if (comma == NULL) {
3950 goto fail;
3951 }
3952 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
3953 Py_DECREF(comma);
3954 if (error_names == NULL) {
3955 goto fail;
3956 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003957 _PyErr_Format(tstate, PyExc_TypeError,
3958 "%U() got some positional-only arguments passed"
3959 " as keyword arguments: '%U'",
3960 co->co_name, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003961 Py_DECREF(error_names);
3962 goto fail;
3963 }
3964
3965 Py_DECREF(posonly_names);
3966 return 0;
3967
3968fail:
3969 Py_XDECREF(posonly_names);
3970 return 1;
3971
3972}
3973
Guido van Rossumc2e20742006-02-27 22:32:47 +00003974/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003975 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003976 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003977
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003978PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003979_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003980 PyObject *const *args, Py_ssize_t argcount,
3981 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003982 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003983 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003984 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003985 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003986{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003987 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003988 PyFrameObject *f;
3989 PyObject *retval = NULL;
3990 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003991 PyObject *x, *u;
Pablo Galindocd74e662019-06-01 18:08:04 +01003992 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003993 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003994 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003995
Victor Stinner438a12d2019-05-24 17:01:38 +02003996 PyThreadState *tstate = _PyThreadState_GET();
3997 assert(tstate != NULL);
3998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003999 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004000 _PyErr_SetString(tstate, PyExc_SystemError,
4001 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004002 return NULL;
4003 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004004
Victor Stinnerc7020012016-08-16 23:40:29 +02004005 /* Create the frame */
INADA Naoki5a625d02016-12-24 20:19:08 +09004006 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004007 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004008 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004009 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004010 fastlocals = f->f_localsplus;
4011 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004012
Victor Stinnerc7020012016-08-16 23:40:29 +02004013 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004014 if (co->co_flags & CO_VARKEYWORDS) {
4015 kwdict = PyDict_New();
4016 if (kwdict == NULL)
4017 goto fail;
4018 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004019 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004020 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004021 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004022 SETLOCAL(i, kwdict);
4023 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004024 else {
4025 kwdict = NULL;
4026 }
4027
Pablo Galindocd74e662019-06-01 18:08:04 +01004028 /* Copy all positional arguments into local variables */
4029 if (argcount > co->co_argcount) {
4030 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004031 }
4032 else {
4033 n = argcount;
4034 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004035 for (j = 0; j < n; j++) {
4036 x = args[j];
4037 Py_INCREF(x);
4038 SETLOCAL(j, x);
4039 }
4040
Victor Stinnerc7020012016-08-16 23:40:29 +02004041 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004042 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05004043 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004044 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004045 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004046 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004047 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004048 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004049
Serhiy Storchakab7281052016-09-12 00:52:40 +03004050 /* Handle keyword arguments passed as two strided arrays */
4051 kwcount *= kwstep;
4052 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004053 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004054 PyObject *keyword = kwnames[i];
4055 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004056 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004057
Benjamin Petersonb204a422011-06-05 22:04:07 -05004058 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004059 _PyErr_Format(tstate, PyExc_TypeError,
4060 "%U() keywords must be strings",
4061 co->co_name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004062 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004063 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004064
Benjamin Petersonb204a422011-06-05 22:04:07 -05004065 /* Speed hack: do raw pointer compares. As names are
4066 normally interned this should almost always hit. */
4067 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004068 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004069 PyObject *name = co_varnames[j];
4070 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004071 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004072 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004073 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004074
Benjamin Petersonb204a422011-06-05 22:04:07 -05004075 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004076 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004077 PyObject *name = co_varnames[j];
4078 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4079 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004080 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004081 }
4082 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004083 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004084 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004085 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004086
Victor Stinner231d1f32017-01-11 02:12:06 +01004087 assert(j >= total_args);
4088 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004089
Victor Stinner438a12d2019-05-24 17:01:38 +02004090 if (co->co_posonlyargcount
4091 && positional_only_passed_as_keyword(tstate, co,
4092 kwcount, kwnames))
4093 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004094 goto fail;
4095 }
4096
Victor Stinner438a12d2019-05-24 17:01:38 +02004097 _PyErr_Format(tstate, PyExc_TypeError,
4098 "%U() got an unexpected keyword argument '%S'",
4099 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004100 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004101 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004102
Christian Heimes0bd447f2013-07-20 14:48:10 +02004103 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4104 goto fail;
4105 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004106 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004107
Benjamin Petersonb204a422011-06-05 22:04:07 -05004108 kw_found:
4109 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004110 _PyErr_Format(tstate, PyExc_TypeError,
4111 "%U() got multiple values for argument '%S'",
4112 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004113 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004114 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004115 Py_INCREF(value);
4116 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004117 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004118
4119 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004120 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004121 too_many_positional(tstate, co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004122 goto fail;
4123 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004124
4125 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004126 if (argcount < co->co_argcount) {
4127 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004128 Py_ssize_t missing = 0;
4129 for (i = argcount; i < m; i++) {
4130 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004131 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004132 }
4133 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004134 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004135 missing_arguments(tstate, co, missing, defcount, fastlocals);
Benjamin Petersone109c702011-06-24 09:37:26 -05004136 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004137 }
4138 if (n > m)
4139 i = n - m;
4140 else
4141 i = 0;
4142 for (; i < defcount; i++) {
4143 if (GETLOCAL(m+i) == NULL) {
4144 PyObject *def = defs[i];
4145 Py_INCREF(def);
4146 SETLOCAL(m+i, def);
4147 }
4148 }
4149 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004150
4151 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004152 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004153 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004154 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004155 PyObject *name;
4156 if (GETLOCAL(i) != NULL)
4157 continue;
4158 name = PyTuple_GET_ITEM(co->co_varnames, i);
4159 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004160 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004161 if (def) {
4162 Py_INCREF(def);
4163 SETLOCAL(i, def);
4164 continue;
4165 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004166 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004167 goto fail;
4168 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004169 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004170 missing++;
4171 }
4172 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004173 missing_arguments(tstate, co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004174 goto fail;
4175 }
4176 }
4177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004178 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004179 vars into frame. */
4180 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004181 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004182 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004183 /* Possibly account for the cell variable being an argument. */
4184 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004185 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004186 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004187 /* Clear the local copy. */
4188 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004189 }
4190 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004191 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004192 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004193 if (c == NULL)
4194 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004195 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004196 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004197
4198 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004199 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4200 PyObject *o = PyTuple_GET_ITEM(closure, i);
4201 Py_INCREF(o);
4202 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004203 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004204
Yury Selivanoveb636452016-09-08 22:01:51 -07004205 /* Handle generator/coroutine/asynchronous generator */
4206 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004207 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004208 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004210 /* Don't need to keep the reference to f_back, it will be set
4211 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004212 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004214 /* Create a new generator that owns the ready to run frame
4215 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004216 if (is_coro) {
4217 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004218 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4219 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004220 } else {
4221 gen = PyGen_NewWithQualName(f, name, qualname);
4222 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004223 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004224 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004225 }
INADA Naoki9c157762016-12-26 18:52:46 +09004226
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004227 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004228
Yury Selivanov75445082015-05-11 22:57:16 -04004229 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004230 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004231
Victor Stinner59a73272016-12-09 18:51:13 +01004232 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004233
Thomas Woutersce272b62007-09-19 21:19:28 +00004234fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004236 /* decref'ing the frame can cause __del__ methods to get invoked,
4237 which can call back into Python. While we're done with the
4238 current Python frame (f), the associated C stack is still in use,
4239 so recursion_depth must be boosted for the duration.
4240 */
4241 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09004242 if (Py_REFCNT(f) > 1) {
4243 Py_DECREF(f);
4244 _PyObject_GC_TRACK(f);
4245 }
4246 else {
4247 ++tstate->recursion_depth;
4248 Py_DECREF(f);
4249 --tstate->recursion_depth;
4250 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004251 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004252}
4253
Victor Stinner40ee3012014-06-16 15:59:28 +02004254PyObject *
4255PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004256 PyObject *const *args, int argcount,
4257 PyObject *const *kws, int kwcount,
4258 PyObject *const *defs, int defcount,
4259 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004260{
4261 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004262 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004263 kws, kws != NULL ? kws + 1 : NULL,
4264 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004265 defs, defcount,
4266 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004267 NULL, NULL);
4268}
Tim Peters5ca576e2001-06-18 22:08:13 +00004269
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004270static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004271special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004272{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004273 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004274 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004275 if (res == NULL && !_PyErr_Occurred(tstate)) {
4276 _PyErr_SetObject(tstate, PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004277 return NULL;
4278 }
4279 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004280}
4281
4282
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004283/* Logic for the raise statement (too complicated for inlining).
4284 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004285static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004286do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004288 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004290 if (exc == NULL) {
4291 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004292 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004293 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004294 type = exc_info->exc_type;
4295 value = exc_info->exc_value;
4296 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004297 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004298 _PyErr_SetString(tstate, PyExc_RuntimeError,
4299 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004300 return 0;
4301 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004302 Py_XINCREF(type);
4303 Py_XINCREF(value);
4304 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004305 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004306 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004307 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004309 /* We support the following forms of raise:
4310 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004311 raise <instance>
4312 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004314 if (PyExceptionClass_Check(exc)) {
4315 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004316 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004317 if (value == NULL)
4318 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004319 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004320 _PyErr_Format(tstate, PyExc_TypeError,
4321 "calling %R should have returned an instance of "
4322 "BaseException, not %R",
4323 type, Py_TYPE(value));
4324 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004325 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004326 }
4327 else if (PyExceptionInstance_Check(exc)) {
4328 value = exc;
4329 type = PyExceptionInstance_Class(exc);
4330 Py_INCREF(type);
4331 }
4332 else {
4333 /* Not something you can raise. You get an exception
4334 anyway, just not what you specified :-) */
4335 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004336 _PyErr_SetString(tstate, PyExc_TypeError,
4337 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004338 goto raise_error;
4339 }
Collin Winter828f04a2007-08-31 00:04:24 +00004340
Serhiy Storchakac0191582016-09-27 11:37:10 +03004341 assert(type != NULL);
4342 assert(value != NULL);
4343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004344 if (cause) {
4345 PyObject *fixed_cause;
4346 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004347 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004348 if (fixed_cause == NULL)
4349 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004350 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004351 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004352 else if (PyExceptionInstance_Check(cause)) {
4353 fixed_cause = cause;
4354 }
4355 else if (cause == Py_None) {
4356 Py_DECREF(cause);
4357 fixed_cause = NULL;
4358 }
4359 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004360 _PyErr_SetString(tstate, PyExc_TypeError,
4361 "exception causes must derive from "
4362 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004363 goto raise_error;
4364 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004365 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004366 }
Collin Winter828f04a2007-08-31 00:04:24 +00004367
Victor Stinner438a12d2019-05-24 17:01:38 +02004368 _PyErr_SetObject(tstate, type, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004369 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004370 Py_DECREF(value);
4371 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004372 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004373
4374raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004375 Py_XDECREF(value);
4376 Py_XDECREF(type);
4377 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004378 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004379}
4380
Tim Petersd6d010b2001-06-21 02:49:55 +00004381/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004382 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004383
Guido van Rossum0368b722007-05-11 16:50:42 +00004384 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4385 with a variable target.
4386*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004387
Barry Warsawe42b18f1997-08-25 22:13:04 +00004388static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004389unpack_iterable(PyThreadState *tstate, PyObject *v,
4390 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004391{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004392 int i = 0, j = 0;
4393 Py_ssize_t ll = 0;
4394 PyObject *it; /* iter(v) */
4395 PyObject *w;
4396 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004398 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004400 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004401 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004402 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004403 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4404 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004405 _PyErr_Format(tstate, PyExc_TypeError,
4406 "cannot unpack non-iterable %.200s object",
4407 v->ob_type->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004408 }
4409 return 0;
4410 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004412 for (; i < argcnt; i++) {
4413 w = PyIter_Next(it);
4414 if (w == NULL) {
4415 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004416 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004417 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004418 _PyErr_Format(tstate, PyExc_ValueError,
4419 "not enough values to unpack "
4420 "(expected %d, got %d)",
4421 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004422 }
4423 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004424 _PyErr_Format(tstate, PyExc_ValueError,
4425 "not enough values to unpack "
4426 "(expected at least %d, got %d)",
4427 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004428 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004429 }
4430 goto Error;
4431 }
4432 *--sp = w;
4433 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004435 if (argcntafter == -1) {
4436 /* We better have exhausted the iterator now. */
4437 w = PyIter_Next(it);
4438 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004439 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004440 goto Error;
4441 Py_DECREF(it);
4442 return 1;
4443 }
4444 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004445 _PyErr_Format(tstate, PyExc_ValueError,
4446 "too many values to unpack (expected %d)",
4447 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004448 goto Error;
4449 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004451 l = PySequence_List(it);
4452 if (l == NULL)
4453 goto Error;
4454 *--sp = l;
4455 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004457 ll = PyList_GET_SIZE(l);
4458 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004459 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004460 "not enough values to unpack (expected at least %d, got %zd)",
4461 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004462 goto Error;
4463 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004465 /* Pop the "after-variable" args off the list. */
4466 for (j = argcntafter; j > 0; j--, i++) {
4467 *--sp = PyList_GET_ITEM(l, ll - j);
4468 }
4469 /* Resize the list. */
4470 Py_SIZE(l) = ll - argcntafter;
4471 Py_DECREF(it);
4472 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004473
Tim Petersd6d010b2001-06-21 02:49:55 +00004474Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004475 for (; i > 0; i--, sp++)
4476 Py_DECREF(*sp);
4477 Py_XDECREF(it);
4478 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004479}
4480
4481
Guido van Rossum96a42c81992-01-12 02:29:51 +00004482#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004483static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004484prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004485{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004486 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004487 if (PyObject_Print(v, stdout, 0) != 0) {
4488 /* Don't know what else to do */
4489 _PyErr_Clear(tstate);
4490 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004491 printf("\n");
4492 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004493}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004494#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004495
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004496static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004497call_exc_trace(Py_tracefunc func, PyObject *self,
4498 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004499{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004500 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004501 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004502 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004503 if (value == NULL) {
4504 value = Py_None;
4505 Py_INCREF(value);
4506 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004507 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004508 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004509 arg = PyTuple_Pack(3, type, value, traceback);
4510 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004511 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004512 return;
4513 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004514 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004515 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004516 if (err == 0) {
4517 _PyErr_Restore(tstate, type, value, orig_traceback);
4518 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004519 else {
4520 Py_XDECREF(type);
4521 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004522 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004523 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004524}
4525
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004526static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004527call_trace_protected(Py_tracefunc func, PyObject *obj,
4528 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004529 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004531 PyObject *type, *value, *traceback;
4532 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004533 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004534 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004535 if (err == 0)
4536 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004537 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004538 return 0;
4539 }
4540 else {
4541 Py_XDECREF(type);
4542 Py_XDECREF(value);
4543 Py_XDECREF(traceback);
4544 return -1;
4545 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004546}
4547
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004548static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004549call_trace(Py_tracefunc func, PyObject *obj,
4550 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004551 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004553 int result;
4554 if (tstate->tracing)
4555 return 0;
4556 tstate->tracing++;
4557 tstate->use_tracing = 0;
4558 result = func(obj, frame, what, arg);
4559 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4560 || (tstate->c_profilefunc != NULL));
4561 tstate->tracing--;
4562 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004563}
4564
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004565PyObject *
4566_PyEval_CallTracing(PyObject *func, PyObject *args)
4567{
Victor Stinner50b48572018-11-01 01:51:40 +01004568 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004569 int save_tracing = tstate->tracing;
4570 int save_use_tracing = tstate->use_tracing;
4571 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004573 tstate->tracing = 0;
4574 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4575 || (tstate->c_profilefunc != NULL));
4576 result = PyObject_Call(func, args, NULL);
4577 tstate->tracing = save_tracing;
4578 tstate->use_tracing = save_use_tracing;
4579 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004580}
4581
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004582/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004583static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004584maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004585 PyThreadState *tstate, PyFrameObject *frame,
4586 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004587{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004588 int result = 0;
4589 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004591 /* If the last instruction executed isn't in the current
4592 instruction window, reset the window.
4593 */
4594 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4595 PyAddrPair bounds;
4596 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4597 &bounds);
4598 *instr_lb = bounds.ap_lower;
4599 *instr_ub = bounds.ap_upper;
4600 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004601 /* If the last instruction falls at the start of a line or if it
4602 represents a jump backwards, update the frame's line number and
4603 then call the trace function if we're tracing source lines.
4604 */
4605 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004606 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004607 if (frame->f_trace_lines) {
4608 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4609 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004610 }
George King20faa682017-10-18 17:44:22 -07004611 /* Always emit an opcode event if we're tracing all opcodes. */
4612 if (frame->f_trace_opcodes) {
4613 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4614 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004615 *instr_prev = frame->f_lasti;
4616 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004617}
4618
Fred Drake5755ce62001-06-27 19:19:46 +00004619void
4620PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004621{
Steve Dowerb82e17e2019-05-23 08:45:22 -07004622 if (PySys_Audit("sys.setprofile", NULL) < 0) {
4623 return;
4624 }
4625
Victor Stinner50b48572018-11-01 01:51:40 +01004626 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004627 PyObject *temp = tstate->c_profileobj;
4628 Py_XINCREF(arg);
4629 tstate->c_profilefunc = NULL;
4630 tstate->c_profileobj = NULL;
4631 /* Must make sure that tracing is not ignored if 'temp' is freed */
4632 tstate->use_tracing = tstate->c_tracefunc != NULL;
4633 Py_XDECREF(temp);
4634 tstate->c_profilefunc = func;
4635 tstate->c_profileobj = arg;
4636 /* Flag that tracing or profiling is turned on */
4637 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004638}
4639
4640void
4641PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4642{
Steve Dowerb82e17e2019-05-23 08:45:22 -07004643 if (PySys_Audit("sys.settrace", NULL) < 0) {
4644 return;
4645 }
4646
Victor Stinner09532fe2019-05-10 23:39:09 +02004647 _PyRuntimeState *runtime = &_PyRuntime;
4648 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004649 PyObject *temp = tstate->c_traceobj;
Victor Stinner09532fe2019-05-10 23:39:09 +02004650 runtime->ceval.tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004651 Py_XINCREF(arg);
4652 tstate->c_tracefunc = NULL;
4653 tstate->c_traceobj = NULL;
4654 /* Must make sure that profiling is not ignored if 'temp' is freed */
4655 tstate->use_tracing = tstate->c_profilefunc != NULL;
4656 Py_XDECREF(temp);
4657 tstate->c_tracefunc = func;
4658 tstate->c_traceobj = arg;
4659 /* Flag that tracing or profiling is turned on */
4660 tstate->use_tracing = ((func != NULL)
4661 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004662}
4663
Yury Selivanov75445082015-05-11 22:57:16 -04004664void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004665_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4666{
4667 assert(new_depth >= 0);
Victor Stinner50b48572018-11-01 01:51:40 +01004668 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004669 tstate->coroutine_origin_tracking_depth = new_depth;
4670}
4671
4672int
4673_PyEval_GetCoroutineOriginTrackingDepth(void)
4674{
Victor Stinner50b48572018-11-01 01:51:40 +01004675 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004676 return tstate->coroutine_origin_tracking_depth;
4677}
4678
4679void
Yury Selivanoveb636452016-09-08 22:01:51 -07004680_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4681{
Victor Stinner50b48572018-11-01 01:51:40 +01004682 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004683
4684 if (PySys_Audit("sys.set_asyncgen_hook_firstiter", NULL) < 0) {
4685 return;
4686 }
4687
Yury Selivanoveb636452016-09-08 22:01:51 -07004688 Py_XINCREF(firstiter);
4689 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4690}
4691
4692PyObject *
4693_PyEval_GetAsyncGenFirstiter(void)
4694{
Victor Stinner50b48572018-11-01 01:51:40 +01004695 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004696 return tstate->async_gen_firstiter;
4697}
4698
4699void
4700_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4701{
Victor Stinner50b48572018-11-01 01:51:40 +01004702 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004703
4704 if (PySys_Audit("sys.set_asyncgen_hook_finalizer", NULL) < 0) {
4705 return;
4706 }
4707
Yury Selivanoveb636452016-09-08 22:01:51 -07004708 Py_XINCREF(finalizer);
4709 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4710}
4711
4712PyObject *
4713_PyEval_GetAsyncGenFinalizer(void)
4714{
Victor Stinner50b48572018-11-01 01:51:40 +01004715 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004716 return tstate->async_gen_finalizer;
4717}
4718
Victor Stinner438a12d2019-05-24 17:01:38 +02004719static PyFrameObject *
4720_PyEval_GetFrame(PyThreadState *tstate)
4721{
4722 return _PyRuntime.gilstate.getframe(tstate);
4723}
4724
4725PyFrameObject *
4726PyEval_GetFrame(void)
4727{
4728 PyThreadState *tstate = _PyThreadState_GET();
4729 return _PyEval_GetFrame(tstate);
4730}
4731
Guido van Rossumb209a111997-04-29 18:18:01 +00004732PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004733PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004734{
Victor Stinner438a12d2019-05-24 17:01:38 +02004735 PyThreadState *tstate = _PyThreadState_GET();
4736 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004737 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004738 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004739 else
4740 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004741}
4742
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004743/* Convenience function to get a builtin from its name */
4744PyObject *
4745_PyEval_GetBuiltinId(_Py_Identifier *name)
4746{
Victor Stinner438a12d2019-05-24 17:01:38 +02004747 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004748 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4749 if (attr) {
4750 Py_INCREF(attr);
4751 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004752 else if (!_PyErr_Occurred(tstate)) {
4753 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004754 }
4755 return attr;
4756}
4757
Guido van Rossumb209a111997-04-29 18:18:01 +00004758PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004759PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004760{
Victor Stinner438a12d2019-05-24 17:01:38 +02004761 PyThreadState *tstate = _PyThreadState_GET();
4762 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Victor Stinner41bb43a2013-10-29 01:19:37 +01004763 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004764 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004765 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004766 }
4767
Victor Stinner438a12d2019-05-24 17:01:38 +02004768 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004769 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004770 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004771
4772 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004773 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004774}
4775
Guido van Rossumb209a111997-04-29 18:18:01 +00004776PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004777PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004778{
Victor Stinner438a12d2019-05-24 17:01:38 +02004779 PyThreadState *tstate = _PyThreadState_GET();
4780 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
4781 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004782 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004783 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004784
4785 assert(current_frame->f_globals != NULL);
4786 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004787}
4788
Guido van Rossum6135a871995-01-09 17:53:26 +00004789int
Tim Peters5ba58662001-07-16 02:29:45 +00004790PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004791{
Victor Stinner438a12d2019-05-24 17:01:38 +02004792 PyThreadState *tstate = _PyThreadState_GET();
4793 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004794 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004796 if (current_frame != NULL) {
4797 const int codeflags = current_frame->f_code->co_flags;
4798 const int compilerflags = codeflags & PyCF_MASK;
4799 if (compilerflags) {
4800 result = 1;
4801 cf->cf_flags |= compilerflags;
4802 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004803#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004804 if (codeflags & CO_GENERATOR_ALLOWED) {
4805 result = 1;
4806 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4807 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004808#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004809 }
4810 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004811}
4812
Guido van Rossum3f5da241990-12-20 15:06:42 +00004813
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004814const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004815PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004816{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004817 if (PyMethod_Check(func))
4818 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4819 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004820 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004821 else if (PyCFunction_Check(func))
4822 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4823 else
4824 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004825}
4826
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004827const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004828PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004829{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004830 if (PyMethod_Check(func))
4831 return "()";
4832 else if (PyFunction_Check(func))
4833 return "()";
4834 else if (PyCFunction_Check(func))
4835 return "()";
4836 else
4837 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004838}
4839
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004840#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004841if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004842 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4843 tstate, tstate->frame, \
4844 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004845 x = NULL; \
4846 } \
4847 else { \
4848 x = call; \
4849 if (tstate->c_profilefunc != NULL) { \
4850 if (x == NULL) { \
4851 call_trace_protected(tstate->c_profilefunc, \
4852 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004853 tstate, tstate->frame, \
4854 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004855 /* XXX should pass (type, value, tb) */ \
4856 } else { \
4857 if (call_trace(tstate->c_profilefunc, \
4858 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004859 tstate, tstate->frame, \
4860 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004861 Py_DECREF(x); \
4862 x = NULL; \
4863 } \
4864 } \
4865 } \
4866 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004867} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004868 x = call; \
4869 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004870
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004871
4872static PyObject *
4873trace_call_function(PyThreadState *tstate,
4874 PyObject *func,
4875 PyObject **args, Py_ssize_t nargs,
4876 PyObject *kwnames)
4877{
4878 PyObject *x;
4879 if (PyCFunction_Check(func)) {
Jeroen Demeyer37788bc2019-05-30 15:11:22 +02004880 C_TRACE(x, _PyCFunction_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004881 return x;
4882 }
4883 else if (Py_TYPE(func) == &PyMethodDescr_Type && nargs > 0) {
4884 /* We need to create a temporary bound method as argument
4885 for profiling.
4886
4887 If nargs == 0, then this cannot work because we have no
4888 "self". In any case, the call itself would raise
4889 TypeError (foo needs an argument), so we just skip
4890 profiling. */
4891 PyObject *self = args[0];
4892 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4893 if (func == NULL) {
4894 return NULL;
4895 }
Jeroen Demeyer37788bc2019-05-30 15:11:22 +02004896 C_TRACE(x, _PyCFunction_Vectorcall(func,
4897 args+1, nargs-1,
4898 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004899 Py_DECREF(func);
4900 return x;
4901 }
4902 return _PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
4903}
4904
Victor Stinner415c5102017-01-11 00:54:57 +01004905/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4906 to reduce the stack consumption. */
4907Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02004908call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004909{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004910 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004911 PyObject *func = *pfunc;
4912 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004913 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4914 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004915 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004916
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004917 if (tstate->use_tracing) {
4918 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09004919 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004920 else {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004921 x = _PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004922 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004923
Victor Stinner438a12d2019-05-24 17:01:38 +02004924 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004925
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004926 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004927 while ((*pp_stack) > pfunc) {
4928 w = EXT_POP(*pp_stack);
4929 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004930 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004932 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004933}
4934
Jeremy Hylton52820442001-01-03 23:52:36 +00004935static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02004936do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004937{
jdemeyere89de732018-09-19 12:06:20 +02004938 PyObject *result;
4939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004940 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004941 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004942 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004943 }
jdemeyere89de732018-09-19 12:06:20 +02004944 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
jdemeyere89de732018-09-19 12:06:20 +02004945 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4946 if (nargs > 0 && tstate->use_tracing) {
4947 /* We need to create a temporary bound method as argument
4948 for profiling.
4949
4950 If nargs == 0, then this cannot work because we have no
4951 "self". In any case, the call itself would raise
4952 TypeError (foo needs an argument), so we just skip
4953 profiling. */
4954 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4955 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4956 if (func == NULL) {
4957 return NULL;
4958 }
4959
4960 C_TRACE(result, _PyCFunction_FastCallDict(func,
Victor Stinnerd17a6932018-11-09 16:56:48 +01004961 &_PyTuple_ITEMS(callargs)[1],
jdemeyere89de732018-09-19 12:06:20 +02004962 nargs - 1,
4963 kwdict));
4964 Py_DECREF(func);
4965 return result;
4966 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004967 }
jdemeyere89de732018-09-19 12:06:20 +02004968 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004969}
4970
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004971/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004972 nb_index slot defined, and store in *pi.
4973 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004974 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004975 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004976*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004977int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004978_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004979{
Victor Stinner438a12d2019-05-24 17:01:38 +02004980 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004981 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004982 Py_ssize_t x;
4983 if (PyIndex_Check(v)) {
4984 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004985 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004986 return 0;
4987 }
4988 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004989 _PyErr_SetString(tstate, PyExc_TypeError,
4990 "slice indices must be integers or "
4991 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004992 return 0;
4993 }
4994 *pi = x;
4995 }
4996 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004997}
4998
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004999int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005000_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005001{
Victor Stinner438a12d2019-05-24 17:01:38 +02005002 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005003 Py_ssize_t x;
5004 if (PyIndex_Check(v)) {
5005 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005006 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005007 return 0;
5008 }
5009 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005010 _PyErr_SetString(tstate, PyExc_TypeError,
5011 "slice indices must be integers or "
5012 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005013 return 0;
5014 }
5015 *pi = x;
5016 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005017}
5018
5019
Guido van Rossum486364b2007-06-30 05:01:58 +00005020#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005021 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00005022
Guido van Rossumb209a111997-04-29 18:18:01 +00005023static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005024cmp_outcome(PyThreadState *tstate, int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005025{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005026 int res = 0;
5027 switch (op) {
5028 case PyCmp_IS:
5029 res = (v == w);
5030 break;
5031 case PyCmp_IS_NOT:
5032 res = (v != w);
5033 break;
5034 case PyCmp_IN:
5035 res = PySequence_Contains(w, v);
5036 if (res < 0)
5037 return NULL;
5038 break;
5039 case PyCmp_NOT_IN:
5040 res = PySequence_Contains(w, v);
5041 if (res < 0)
5042 return NULL;
5043 res = !res;
5044 break;
5045 case PyCmp_EXC_MATCH:
5046 if (PyTuple_Check(w)) {
5047 Py_ssize_t i, length;
5048 length = PyTuple_Size(w);
5049 for (i = 0; i < length; i += 1) {
5050 PyObject *exc = PyTuple_GET_ITEM(w, i);
5051 if (!PyExceptionClass_Check(exc)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005052 _PyErr_SetString(tstate, PyExc_TypeError,
5053 CANNOT_CATCH_MSG);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005054 return NULL;
5055 }
5056 }
5057 }
5058 else {
5059 if (!PyExceptionClass_Check(w)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005060 _PyErr_SetString(tstate, PyExc_TypeError,
5061 CANNOT_CATCH_MSG);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005062 return NULL;
5063 }
5064 }
5065 res = PyErr_GivenExceptionMatches(v, w);
5066 break;
5067 default:
5068 return PyObject_RichCompare(v, w, op);
5069 }
5070 v = res ? Py_True : Py_False;
5071 Py_INCREF(v);
5072 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005073}
5074
Thomas Wouters52152252000-08-17 22:55:00 +00005075static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005076import_name(PyThreadState *tstate, PyFrameObject *f,
5077 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005078{
5079 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005080 PyObject *import_func, *res;
5081 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005082
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005083 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005084 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005085 if (!_PyErr_Occurred(tstate)) {
5086 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005087 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005088 return NULL;
5089 }
5090
5091 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005092 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005093 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005094 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005095 return NULL;
5096 }
5097 res = PyImport_ImportModuleLevelObject(
5098 name,
5099 f->f_globals,
5100 f->f_locals == NULL ? Py_None : f->f_locals,
5101 fromlist,
5102 ilevel);
5103 return res;
5104 }
5105
5106 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005107
5108 stack[0] = name;
5109 stack[1] = f->f_globals;
5110 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5111 stack[3] = fromlist;
5112 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005113 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005114 Py_DECREF(import_func);
5115 return res;
5116}
5117
5118static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005119import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005120{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005121 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005122 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08005123 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005124
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005125 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005126 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005127 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005128 /* Issue #17636: in case this failed because of a circular relative
5129 import, try to fallback on reading the module directly from
5130 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005131 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005132 if (pkgname == NULL) {
5133 goto error;
5134 }
Oren Milman6db70332017-09-19 14:23:01 +03005135 if (!PyUnicode_Check(pkgname)) {
5136 Py_CLEAR(pkgname);
5137 goto error;
5138 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005139 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005140 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005141 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005142 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005143 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005144 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005145 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005146 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005147 goto error;
5148 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005149 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005150 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005151 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005152 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005153 if (pkgname == NULL) {
5154 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5155 if (pkgname_or_unknown == NULL) {
5156 Py_XDECREF(pkgpath);
5157 return NULL;
5158 }
5159 } else {
5160 pkgname_or_unknown = pkgname;
5161 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005162
5163 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005164 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005165 errmsg = PyUnicode_FromFormat(
5166 "cannot import name %R from %R (unknown location)",
5167 name, pkgname_or_unknown
5168 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005169 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005170 PyErr_SetImportError(errmsg, pkgname, NULL);
5171 }
5172 else {
5173 errmsg = PyUnicode_FromFormat(
5174 "cannot import name %R from %R (%S)",
5175 name, pkgname_or_unknown, pkgpath
5176 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005177 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005178 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005179 }
5180
Xiang Zhang4830f582017-03-21 11:13:42 +08005181 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005182 Py_XDECREF(pkgname_or_unknown);
5183 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005184 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005185}
Guido van Rossumac7be682001-01-17 15:42:30 +00005186
Thomas Wouters52152252000-08-17 22:55:00 +00005187static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005188import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005189{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005190 _Py_IDENTIFIER(__all__);
5191 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005192 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005193 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005194 int skip_leading_underscores = 0;
5195 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005196
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005197 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5198 return -1; /* Unexpected error */
5199 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005200 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005201 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5202 return -1;
5203 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005204 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005205 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005206 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005207 return -1;
5208 }
5209 all = PyMapping_Keys(dict);
5210 Py_DECREF(dict);
5211 if (all == NULL)
5212 return -1;
5213 skip_leading_underscores = 1;
5214 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005216 for (pos = 0, err = 0; ; pos++) {
5217 name = PySequence_GetItem(all, pos);
5218 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005219 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005220 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005221 }
5222 else {
5223 _PyErr_Clear(tstate);
5224 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005225 break;
5226 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005227 if (!PyUnicode_Check(name)) {
5228 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5229 if (modname == NULL) {
5230 Py_DECREF(name);
5231 err = -1;
5232 break;
5233 }
5234 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005235 _PyErr_Format(tstate, PyExc_TypeError,
5236 "module __name__ must be a string, not %.100s",
5237 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005238 }
5239 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005240 _PyErr_Format(tstate, PyExc_TypeError,
5241 "%s in %U.%s must be str, not %.100s",
5242 skip_leading_underscores ? "Key" : "Item",
5243 modname,
5244 skip_leading_underscores ? "__dict__" : "__all__",
5245 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005246 }
5247 Py_DECREF(modname);
5248 Py_DECREF(name);
5249 err = -1;
5250 break;
5251 }
5252 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005253 if (PyUnicode_READY(name) == -1) {
5254 Py_DECREF(name);
5255 err = -1;
5256 break;
5257 }
5258 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5259 Py_DECREF(name);
5260 continue;
5261 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005262 }
5263 value = PyObject_GetAttr(v, name);
5264 if (value == NULL)
5265 err = -1;
5266 else if (PyDict_CheckExact(locals))
5267 err = PyDict_SetItem(locals, name, value);
5268 else
5269 err = PyObject_SetItem(locals, name, value);
5270 Py_DECREF(name);
5271 Py_XDECREF(value);
5272 if (err != 0)
5273 break;
5274 }
5275 Py_DECREF(all);
5276 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005277}
5278
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005279static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005280check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005281{
5282 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005283 _PyErr_Format(tstate, PyExc_TypeError,
5284 "%.200s%.200s argument after * "
5285 "must be an iterable, not %.200s",
5286 PyEval_GetFuncName(func),
5287 PyEval_GetFuncDesc(func),
5288 args->ob_type->tp_name);
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005289 return -1;
5290 }
5291 return 0;
5292}
5293
5294static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005295format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005296{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005297 /* _PyDict_MergeEx raises attribute
5298 * error (percolated from an attempt
5299 * to get 'keys' attribute) instead of
5300 * a type error if its second argument
5301 * is not a mapping.
5302 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005303 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
5304 _PyErr_Format(tstate, PyExc_TypeError,
5305 "%.200s%.200s argument after ** "
5306 "must be a mapping, not %.200s",
5307 PyEval_GetFuncName(func),
5308 PyEval_GetFuncDesc(func),
5309 kwargs->ob_type->tp_name);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005310 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005311 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005312 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005313 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005314 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
5315 PyObject *key = PyTuple_GET_ITEM(val, 0);
5316 if (!PyUnicode_Check(key)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005317 _PyErr_Format(tstate, PyExc_TypeError,
5318 "%.200s%.200s keywords must be strings",
5319 PyEval_GetFuncName(func),
5320 PyEval_GetFuncDesc(func));
5321 }
5322 else {
5323 _PyErr_Format(tstate, PyExc_TypeError,
5324 "%.200s%.200s got multiple "
5325 "values for keyword argument '%U'",
5326 PyEval_GetFuncName(func),
5327 PyEval_GetFuncDesc(func),
5328 key);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005329 }
5330 Py_XDECREF(exc);
5331 Py_XDECREF(val);
5332 Py_XDECREF(tb);
5333 }
5334 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005335 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005336 }
5337 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005338}
5339
Guido van Rossumac7be682001-01-17 15:42:30 +00005340static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005341format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5342 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005344 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005346 if (!obj)
5347 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005348
Serhiy Storchaka06515832016-11-20 09:13:07 +02005349 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005350 if (!obj_str)
5351 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005352
Victor Stinner438a12d2019-05-24 17:01:38 +02005353 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005354}
Guido van Rossum950361c1997-01-24 13:49:28 +00005355
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005356static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005357format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005358{
5359 PyObject *name;
5360 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005361 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005362 return;
5363 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5364 name = PyTuple_GET_ITEM(co->co_cellvars,
5365 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005366 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005367 PyExc_UnboundLocalError,
5368 UNBOUNDLOCAL_ERROR_MSG,
5369 name);
5370 } else {
5371 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5372 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005373 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005374 UNBOUNDFREE_ERROR_MSG, name);
5375 }
5376}
5377
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005378static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005379format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005380{
5381 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5382 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005383 _PyErr_Format(tstate, PyExc_TypeError,
5384 "'async with' received an object from __aenter__ "
5385 "that does not implement __await__: %.100s",
5386 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005387 }
5388 else if (prevopcode == WITH_CLEANUP_START) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005389 _PyErr_Format(tstate, PyExc_TypeError,
5390 "'async with' received an object from __aexit__ "
5391 "that does not implement __await__: %.100s",
5392 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005393 }
5394 }
5395}
5396
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005397static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005398unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005399 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005400{
5401 PyObject *res;
5402 if (Py_REFCNT(v) == 2) {
5403 /* In the common case, there are 2 references to the value
5404 * stored in 'variable' when the += is performed: one on the
5405 * value stack (in 'v') and one still stored in the
5406 * 'variable'. We try to delete the variable now to reduce
5407 * the refcnt to 1.
5408 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005409 int opcode, oparg;
5410 NEXTOPARG();
5411 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005412 case STORE_FAST:
5413 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005414 PyObject **fastlocals = f->f_localsplus;
5415 if (GETLOCAL(oparg) == v)
5416 SETLOCAL(oparg, NULL);
5417 break;
5418 }
5419 case STORE_DEREF:
5420 {
5421 PyObject **freevars = (f->f_localsplus +
5422 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005423 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005424 if (PyCell_GET(c) == v) {
5425 PyCell_SET(c, NULL);
5426 Py_DECREF(v);
5427 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005428 break;
5429 }
5430 case STORE_NAME:
5431 {
5432 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005433 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005434 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005435 if (locals && PyDict_CheckExact(locals)) {
5436 PyObject *w = PyDict_GetItemWithError(locals, name);
5437 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005438 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005439 {
5440 Py_DECREF(v);
5441 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005442 }
5443 }
5444 break;
5445 }
5446 }
5447 }
5448 res = v;
5449 PyUnicode_Append(&res, w);
5450 return res;
5451}
5452
Guido van Rossum950361c1997-01-24 13:49:28 +00005453#ifdef DYNAMIC_EXECUTION_PROFILE
5454
Skip Montanarof118cb12001-10-15 20:51:38 +00005455static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005456getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005457{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005458 int i;
5459 PyObject *l = PyList_New(256);
5460 if (l == NULL) return NULL;
5461 for (i = 0; i < 256; i++) {
5462 PyObject *x = PyLong_FromLong(a[i]);
5463 if (x == NULL) {
5464 Py_DECREF(l);
5465 return NULL;
5466 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005467 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005468 }
5469 for (i = 0; i < 256; i++)
5470 a[i] = 0;
5471 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005472}
5473
5474PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005475_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005476{
5477#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005478 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005479#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005480 int i;
5481 PyObject *l = PyList_New(257);
5482 if (l == NULL) return NULL;
5483 for (i = 0; i < 257; i++) {
5484 PyObject *x = getarray(dxpairs[i]);
5485 if (x == NULL) {
5486 Py_DECREF(l);
5487 return NULL;
5488 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005489 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005490 }
5491 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005492#endif
5493}
5494
5495#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005496
5497Py_ssize_t
5498_PyEval_RequestCodeExtraIndex(freefunc free)
5499{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005500 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005501 Py_ssize_t new_index;
5502
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005503 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005504 return -1;
5505 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005506 new_index = interp->co_extra_user_count++;
5507 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005508 return new_index;
5509}
Łukasz Langaa785c872016-09-09 17:37:37 -07005510
5511static void
5512dtrace_function_entry(PyFrameObject *f)
5513{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005514 const char *filename;
5515 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005516 int lineno;
5517
5518 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5519 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5520 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5521
5522 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5523}
5524
5525static void
5526dtrace_function_return(PyFrameObject *f)
5527{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005528 const char *filename;
5529 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005530 int lineno;
5531
5532 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5533 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5534 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5535
5536 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5537}
5538
5539/* DTrace equivalent of maybe_call_line_trace. */
5540static void
5541maybe_dtrace_line(PyFrameObject *frame,
5542 int *instr_lb, int *instr_ub, int *instr_prev)
5543{
5544 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005545 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005546
5547 /* If the last instruction executed isn't in the current
5548 instruction window, reset the window.
5549 */
5550 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5551 PyAddrPair bounds;
5552 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5553 &bounds);
5554 *instr_lb = bounds.ap_lower;
5555 *instr_ub = bounds.ap_upper;
5556 }
5557 /* If the last instruction falls at the start of a line or if
5558 it represents a jump backwards, update the frame's line
5559 number and call the trace function. */
5560 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5561 frame->f_lineno = line;
5562 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5563 if (!co_filename)
5564 co_filename = "?";
5565 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5566 if (!co_name)
5567 co_name = "?";
5568 PyDTrace_LINE(co_filename, co_name, line);
5569 }
5570 *instr_prev = frame->f_lasti;
5571}