blob: 1bb4704572b44b08ec707c49db2c48a7cd020a77 [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 Stinner621cebe2018-11-12 16:53:38 +010015#include "pycore_pystate.h"
Victor Stinnerec13b932018-11-25 23:56:17 +010016#include "pycore_tupleobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000017
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000018#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040019#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000020#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000021#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070022#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040023#include "setobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000024#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000025
Guido van Rossumc6004111993-11-05 10:22:19 +000026#include <ctype.h>
27
Guido van Rossum408027e1996-12-30 16:17:54 +000028#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000029/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000030#define LLTRACE 1 /* Low-level trace feature */
31#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000032#endif
33
Victor Stinner5c75f372019-04-17 23:02:26 +020034#if !defined(Py_BUILD_CORE)
35# error "ceval.c must be build with Py_BUILD_CORE define for best performance"
36#endif
37
Yury Selivanovf2392132016-12-13 19:03:51 -050038/* Private API for the LOAD_METHOD opcode. */
39extern int _PyObject_GetMethod(PyObject *, PyObject *, PyObject **);
40
Jeremy Hylton52820442001-01-03 23:52:36 +000041typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000042
Guido van Rossum374a9221991-04-04 10:40:29 +000043/* Forward declarations */
Victor Stinner09532fe2019-05-10 23:39:09 +020044Py_LOCAL_INLINE(PyObject *) call_function(
45 PyThreadState *tstate, PyObject ***pp_stack,
46 Py_ssize_t oparg, PyObject *kwnames);
47static PyObject * do_call_core(
48 PyThreadState *tstate, PyObject *func,
49 PyObject *callargs, PyObject *kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +000050
Guido van Rossum0a066c01992-03-27 17:29:15 +000051#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000052static int lltrace;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020053static int prtrace(PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000054#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010055static int call_trace(Py_tracefunc, PyObject *,
56 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000058static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010059 PyThreadState *, PyFrameObject *,
60 int, PyObject *);
61static void call_exc_trace(Py_tracefunc, PyObject *,
62 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000063static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060064 PyThreadState *, PyFrameObject *,
65 int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070066static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
67static void dtrace_function_entry(PyFrameObject *);
68static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000069
Thomas Wouters477c8d52006-05-27 19:21:47 +000070static PyObject * cmp_outcome(int, PyObject *, PyObject *);
Eric Snow2ebc5ce2017-09-07 23:51:28 -060071static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *,
72 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +000073static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +000074static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +000075static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +000076static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +020077static PyObject * unicode_concatenate(PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030078 PyFrameObject *, const _Py_CODEUNIT *);
Benjamin Petersonce798522012-01-22 11:24:29 -050079static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Serhiy Storchaka25e4f772017-08-03 11:37:15 +030080static int check_args_iterable(PyObject *func, PyObject *vararg);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +020081static void format_kwargs_error(PyObject *func, PyObject *kwargs);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +030082static void format_awaitable_error(PyTypeObject *, int);
Guido van Rossum374a9221991-04-04 10:40:29 +000083
Paul Prescode68140d2000-08-30 20:25:01 +000084#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000086#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000088#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 "free variable '%.200s' referenced before assignment" \
90 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000091
Guido van Rossum950361c1997-01-24 13:49:28 +000092/* Dynamic execution profile */
93#ifdef DYNAMIC_EXECUTION_PROFILE
94#ifdef DXPAIRS
95static long dxpairs[257][256];
96#define dxp dxpairs[256]
97#else
98static long dxp[256];
99#endif
100#endif
101
Victor Stinner09532fe2019-05-10 23:39:09 +0200102#define GIL_REQUEST _Py_atomic_load_relaxed(&ceval->gil_drop_request)
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000103
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. */
Victor Stinner09532fe2019-05-10 23:39:09 +0200107#define COMPUTE_EVAL_BREAKER(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 _Py_atomic_store_relaxed( \
Victor Stinner09532fe2019-05-10 23:39:09 +0200109 &(ceval)->eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000110 GIL_REQUEST | \
Victor Stinner09532fe2019-05-10 23:39:09 +0200111 _Py_atomic_load_relaxed(&(ceval)->signals_pending) | \
112 _Py_atomic_load_relaxed(&(ceval)->pending.calls_to_do) | \
113 (ceval)->pending.async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000114
Victor Stinner09532fe2019-05-10 23:39:09 +0200115#define SET_GIL_DROP_REQUEST(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 do { \
Victor Stinner09532fe2019-05-10 23:39:09 +0200117 _Py_atomic_store_relaxed(&(ceval)->gil_drop_request, 1); \
118 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000119 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000120
Victor Stinner09532fe2019-05-10 23:39:09 +0200121#define RESET_GIL_DROP_REQUEST(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 do { \
Victor Stinner09532fe2019-05-10 23:39:09 +0200123 _Py_atomic_store_relaxed(&(ceval)->gil_drop_request, 0); \
124 COMPUTE_EVAL_BREAKER(ceval); \
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 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200128#define SIGNAL_PENDING_CALLS(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 do { \
Victor Stinner09532fe2019-05-10 23:39:09 +0200130 _Py_atomic_store_relaxed(&(ceval)->pending.calls_to_do, 1); \
131 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000133
Victor Stinner09532fe2019-05-10 23:39:09 +0200134#define UNSIGNAL_PENDING_CALLS(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 do { \
Victor Stinner09532fe2019-05-10 23:39:09 +0200136 _Py_atomic_store_relaxed(&(ceval)->pending.calls_to_do, 0); \
137 COMPUTE_EVAL_BREAKER(ceval); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000139
Victor Stinner09532fe2019-05-10 23:39:09 +0200140#define SIGNAL_PENDING_SIGNALS(ceval) \
Eric Snowfdf282d2019-01-11 14:26:55 -0700141 do { \
Victor Stinner09532fe2019-05-10 23:39:09 +0200142 _Py_atomic_store_relaxed(&(ceval)->signals_pending, 1); \
143 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700144 } while (0)
145
Victor Stinner09532fe2019-05-10 23:39:09 +0200146#define UNSIGNAL_PENDING_SIGNALS(ceval) \
Eric Snowfdf282d2019-01-11 14:26:55 -0700147 do { \
Victor Stinner09532fe2019-05-10 23:39:09 +0200148 _Py_atomic_store_relaxed(&(ceval)->signals_pending, 0); \
149 COMPUTE_EVAL_BREAKER(ceval); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700150 } while (0)
151
Victor Stinner09532fe2019-05-10 23:39:09 +0200152#define SIGNAL_ASYNC_EXC(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 do { \
Victor Stinner09532fe2019-05-10 23:39:09 +0200154 (ceval)->pending.async_exc = 1; \
155 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000157
Victor Stinner09532fe2019-05-10 23:39:09 +0200158#define UNSIGNAL_ASYNC_EXC(ceval) \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600159 do { \
Victor Stinner09532fe2019-05-10 23:39:09 +0200160 (ceval)->pending.async_exc = 0; \
161 COMPUTE_EVAL_BREAKER(ceval); \
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;
181 struct _ceval_runtime_state *ceval = &runtime->ceval;
182 struct _gil_runtime_state *gil = &ceval->gil;
183 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);
190 take_gil(ceval, tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700191
Victor Stinner09532fe2019-05-10 23:39:09 +0200192 struct _pending_calls *pending = &ceval->pending;
193 pending->lock = PyThread_allocate_lock();
194 if (pending->lock == NULL) {
Eric Snowb75b1a352019-04-12 10:20:10 -0600195 Py_FatalError("Can't initialize threads for pending calls");
196 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000197}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000198
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000199void
Victor Stinner09532fe2019-05-10 23:39:09 +0200200_PyEval_FiniThreads(struct _ceval_runtime_state *ceval)
Antoine Pitrou1df15362010-09-13 14:16:46 +0000201{
Victor Stinner09532fe2019-05-10 23:39:09 +0200202 struct _gil_runtime_state *gil = &ceval->gil;
203 if (!gil_created(gil)) {
Antoine Pitrou1df15362010-09-13 14:16:46 +0000204 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100205 }
206
Victor Stinner09532fe2019-05-10 23:39:09 +0200207 destroy_gil(gil);
208 assert(!gil_created(gil));
Victor Stinner99fcc612019-04-29 13:04:07 +0200209
Victor Stinner09532fe2019-05-10 23:39:09 +0200210 struct _pending_calls *pending = &ceval->pending;
211 if (pending->lock != NULL) {
212 PyThread_free_lock(pending->lock);
213 pending->lock = NULL;
Victor Stinner99fcc612019-04-29 13:04:07 +0200214 }
Antoine Pitrou1df15362010-09-13 14:16:46 +0000215}
216
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400217static inline void
Victor Stinner09532fe2019-05-10 23:39:09 +0200218exit_thread_if_finalizing(_PyRuntimeState *runtime, PyThreadState *tstate)
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400219{
220 /* _Py_Finalizing is protected by the GIL */
Victor Stinner09532fe2019-05-10 23:39:09 +0200221 if (runtime->finalizing != NULL && !_Py_CURRENTLY_FINALIZING(runtime, tstate)) {
222 drop_gil(&runtime->ceval, tstate);
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400223 PyThread_exit_thread();
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400224 }
225}
226
Antoine Pitrou1df15362010-09-13 14:16:46 +0000227void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000228PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000229{
Victor Stinner09532fe2019-05-10 23:39:09 +0200230 _PyRuntimeState *runtime = &_PyRuntime;
231 struct _ceval_runtime_state *ceval = &runtime->ceval;
232 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
233 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
Victor Stinner09532fe2019-05-10 23:39:09 +0200235 }
236 take_gil(ceval, tstate);
237 exit_thread_if_finalizing(runtime, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000238}
239
240void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000241PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000242{
Victor Stinner09532fe2019-05-10 23:39:09 +0200243 _PyRuntimeState *runtime = &_PyRuntime;
244 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
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 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200249 drop_gil(&runtime->ceval, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000250}
251
252void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000253PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000254{
Victor Stinner09532fe2019-05-10 23:39:09 +0200255 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200257 }
258
259 _PyRuntimeState *runtime = &_PyRuntime;
260 struct _ceval_runtime_state *ceval = &runtime->ceval;
261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 /* Check someone has called PyEval_InitThreads() to create the lock */
Victor Stinner09532fe2019-05-10 23:39:09 +0200263 assert(gil_created(&ceval->gil));
264 take_gil(ceval, tstate);
265 exit_thread_if_finalizing(runtime, tstate);
266 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
267 Py_FatalError("PyEval_AcquireThread: non-NULL old thread state");
268 }
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000269}
270
271void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000272PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000273{
Victor Stinner09532fe2019-05-10 23:39:09 +0200274 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200276 }
277
278 _PyRuntimeState *runtime = &_PyRuntime;
279 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
280 if (new_tstate != tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200282 }
283 drop_gil(&runtime->ceval, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000284}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000285
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200286/* This function is called from PyOS_AfterFork_Child to destroy all threads
287 * which are not running in the child process, and clear internal locks
288 * which might be held by those threads.
289 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000290
291void
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200292_PyEval_ReInitThreads(_PyRuntimeState *runtime)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000293{
Victor Stinner09532fe2019-05-10 23:39:09 +0200294 struct _ceval_runtime_state *ceval = &runtime->ceval;
295 if (!gil_created(&ceval->gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 return;
Victor Stinner09532fe2019-05-10 23:39:09 +0200297 }
298 recreate_gil(&ceval->gil);
299 PyThreadState *current_tstate = _PyRuntimeState_GetThreadState(runtime);
300 take_gil(ceval, current_tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700301
Victor Stinner09532fe2019-05-10 23:39:09 +0200302 struct _pending_calls *pending = &ceval->pending;
303 pending->lock = PyThread_allocate_lock();
304 if (pending->lock == NULL) {
Eric Snow8479a342019-03-08 23:44:33 -0700305 Py_FatalError("Can't initialize threads for pending calls");
306 }
Jesse Nollera8513972008-07-17 16:49:17 +0000307
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200308 /* Destroy all threads except the current one */
Victor Stinner09532fe2019-05-10 23:39:09 +0200309 _PyThreadState_DeleteExcept(runtime, current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000310}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000311
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000312/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600313 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000314
315void
Victor Stinner09532fe2019-05-10 23:39:09 +0200316_PyEval_SignalAsyncExc(struct _ceval_runtime_state *ceval)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000317{
Victor Stinner09532fe2019-05-10 23:39:09 +0200318 SIGNAL_ASYNC_EXC(ceval);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000319}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000320
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000321PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000322PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000323{
Victor Stinner09532fe2019-05-10 23:39:09 +0200324 _PyRuntimeState *runtime = &_PyRuntime;
325 struct _ceval_runtime_state *ceval = &runtime->ceval;
326 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
327 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 Py_FatalError("PyEval_SaveThread: NULL tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +0200329 }
330 assert(gil_created(&ceval->gil));
331 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000333}
334
335void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000336PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000337{
Victor Stinner09532fe2019-05-10 23:39:09 +0200338 _PyRuntimeState *runtime = &_PyRuntime;
339 struct _ceval_runtime_state *ceval = &runtime->ceval;
340
341 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +0200343 }
344 assert(gil_created(&ceval->gil));
Victor Stinner2914bb32018-01-29 11:57:45 +0100345
346 int err = errno;
Victor Stinner09532fe2019-05-10 23:39:09 +0200347 take_gil(ceval, tstate);
348 exit_thread_if_finalizing(runtime, tstate);
Victor Stinner2914bb32018-01-29 11:57:45 +0100349 errno = err;
350
Victor Stinner09532fe2019-05-10 23:39:09 +0200351 _PyThreadState_Swap(&runtime->gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000352}
353
354
Guido van Rossuma9672091994-09-14 13:31:22 +0000355/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
356 signal handlers or Mac I/O completion routines) can schedule calls
357 to a function to be called synchronously.
358 The synchronous function is called with one void* argument.
359 It should return 0 for success or -1 for failure -- failure should
360 be accompanied by an exception.
361
362 If registry succeeds, the registry function returns 0; if it fails
363 (e.g. due to too many pending calls) it returns -1 (without setting
364 an exception condition).
365
366 Note that because registry may occur from within signal handlers,
367 or other asynchronous events, calling malloc() is unsafe!
368
Guido van Rossuma9672091994-09-14 13:31:22 +0000369 Any thread can schedule pending calls, but only the main thread
370 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000371 There is no facility to schedule calls to a particular thread, but
372 that should be easy to change, should that ever be required. In
373 that case, the static variables here should go into the python
374 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000375*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000376
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200377void
Victor Stinner09532fe2019-05-10 23:39:09 +0200378_PyEval_SignalReceived(struct _ceval_runtime_state *ceval)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200379{
380 /* bpo-30703: Function called when the C signal handler of Python gets a
381 signal. We cannot queue a callback using Py_AddPendingCall() since
382 that function is not async-signal-safe. */
Victor Stinner09532fe2019-05-10 23:39:09 +0200383 SIGNAL_PENDING_SIGNALS(ceval);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200384}
385
Eric Snow5be45a62019-03-08 22:47:07 -0700386/* Push one item onto the queue while holding the lock. */
387static int
Eric Snowb75b1a352019-04-12 10:20:10 -0600388_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600389 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700390{
Eric Snow842a2f02019-03-15 15:47:51 -0600391 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700392 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600393 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700394 return -1; /* Queue full */
395 }
Eric Snow842a2f02019-03-15 15:47:51 -0600396 pending->calls[i].func = func;
397 pending->calls[i].arg = arg;
398 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700399 return 0;
400}
401
402/* Pop one item off the queue while holding the lock. */
403static void
Eric Snowb75b1a352019-04-12 10:20:10 -0600404_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600405 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700406{
Eric Snow842a2f02019-03-15 15:47:51 -0600407 int i = pending->first;
408 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700409 return; /* Queue empty */
410 }
411
Eric Snow842a2f02019-03-15 15:47:51 -0600412 *func = pending->calls[i].func;
413 *arg = pending->calls[i].arg;
414 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700415}
416
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200417/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000418 scheduling to be made from any thread, and even from an executing
419 callback.
420 */
421
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000422int
Victor Stinner09532fe2019-05-10 23:39:09 +0200423_PyEval_AddPendingCall(struct _ceval_runtime_state *ceval,
424 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000425{
Victor Stinner09532fe2019-05-10 23:39:09 +0200426 struct _pending_calls *pending = &ceval->pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600427
428 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
429 if (pending->finishing) {
430 PyThread_release_lock(pending->lock);
431
432 PyObject *exc, *val, *tb;
433 PyErr_Fetch(&exc, &val, &tb);
434 PyErr_SetString(PyExc_SystemError,
435 "Py_AddPendingCall: cannot add pending calls "
436 "(Python shutting down)");
437 PyErr_Print();
438 PyErr_Restore(exc, val, tb);
439 return -1;
440 }
Eric Snowb75b1a352019-04-12 10:20:10 -0600441 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600442 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700443
Eric Snowb75b1a352019-04-12 10:20:10 -0600444 /* signal main loop */
Victor Stinner09532fe2019-05-10 23:39:09 +0200445 SIGNAL_PENDING_CALLS(ceval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000447}
448
Victor Stinner09532fe2019-05-10 23:39:09 +0200449int
450Py_AddPendingCall(int (*func)(void *), void *arg)
451{
452 return _PyEval_AddPendingCall(&_PyRuntime.ceval, func, arg);
453}
454
Eric Snowfdf282d2019-01-11 14:26:55 -0700455static int
Victor Stinner09532fe2019-05-10 23:39:09 +0200456handle_signals(_PyRuntimeState *runtime)
Eric Snowfdf282d2019-01-11 14:26:55 -0700457{
Eric Snow5be45a62019-03-08 22:47:07 -0700458 /* Only handle signals on main thread. PyEval_InitThreads must
459 * have been called already.
460 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200461 if (PyThread_get_thread_ident() != runtime->main_thread) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700462 return 0;
463 }
Eric Snow64d6cc82019-02-23 15:40:43 -0700464 /*
465 * Ensure that the thread isn't currently running some other
466 * interpreter.
467 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200468 PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
469 if (interp != runtime->interpreters.main) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700470 return 0;
471 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700472
Victor Stinner09532fe2019-05-10 23:39:09 +0200473 struct _ceval_runtime_state *ceval = &runtime->ceval;
474 UNSIGNAL_PENDING_SIGNALS(ceval);
Eric Snow64d6cc82019-02-23 15:40:43 -0700475 if (_PyErr_CheckSignals() < 0) {
Victor Stinner09532fe2019-05-10 23:39:09 +0200476 SIGNAL_PENDING_SIGNALS(ceval); /* We're not done yet */
Eric Snowfdf282d2019-01-11 14:26:55 -0700477 return -1;
478 }
479 return 0;
480}
481
482static int
Victor Stinner09532fe2019-05-10 23:39:09 +0200483make_pending_calls(_PyRuntimeState *runtime)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000484{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200485 static int busy = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000486
Eric Snowb75b1a352019-04-12 10:20:10 -0600487 /* only service pending calls on main thread */
Victor Stinner09532fe2019-05-10 23:39:09 +0200488 if (PyThread_get_thread_ident() != runtime->main_thread) {
Eric Snowb75b1a352019-04-12 10:20:10 -0600489 return 0;
490 }
491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 /* don't perform recursive pending calls */
Eric Snowfdf282d2019-01-11 14:26:55 -0700493 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700495 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200496 busy = 1;
Victor Stinner09532fe2019-05-10 23:39:09 +0200497 struct _ceval_runtime_state *ceval = &runtime->ceval;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200498 /* unsignal before starting to call callbacks, so that any callback
499 added in-between re-signals */
Victor Stinner09532fe2019-05-10 23:39:09 +0200500 UNSIGNAL_PENDING_CALLS(ceval);
Eric Snowfdf282d2019-01-11 14:26:55 -0700501 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 /* perform a bounded number of calls, in case of recursion */
Victor Stinner09532fe2019-05-10 23:39:09 +0200504 struct _pending_calls *pending = &ceval->pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700505 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700506 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 void *arg = NULL;
508
509 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600510 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Eric Snowb75b1a352019-04-12 10:20:10 -0600511 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600512 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700513
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100514 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700515 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100516 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700517 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700518 res = func(arg);
519 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200520 goto error;
521 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200523
Charles-François Natalif23339a2011-07-23 18:15:43 +0200524 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700525 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200526
527error:
528 busy = 0;
Victor Stinner09532fe2019-05-10 23:39:09 +0200529 SIGNAL_PENDING_CALLS(ceval);
Eric Snowfdf282d2019-01-11 14:26:55 -0700530 return res;
531}
532
Eric Snow842a2f02019-03-15 15:47:51 -0600533void
Victor Stinner09532fe2019-05-10 23:39:09 +0200534_Py_FinishPendingCalls(_PyRuntimeState *runtime)
Eric Snow842a2f02019-03-15 15:47:51 -0600535{
Eric Snow842a2f02019-03-15 15:47:51 -0600536 assert(PyGILState_Check());
537
Victor Stinner09532fe2019-05-10 23:39:09 +0200538 struct _pending_calls *pending = &runtime->ceval.pending;
539
Eric Snow842a2f02019-03-15 15:47:51 -0600540 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
541 pending->finishing = 1;
542 PyThread_release_lock(pending->lock);
543
544 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
545 return;
546 }
547
Victor Stinner09532fe2019-05-10 23:39:09 +0200548 if (make_pending_calls(runtime) < 0) {
Eric Snow842a2f02019-03-15 15:47:51 -0600549 PyObject *exc, *val, *tb;
550 PyErr_Fetch(&exc, &val, &tb);
551 PyErr_BadInternalCall();
552 _PyErr_ChainExceptions(exc, val, tb);
553 PyErr_Print();
554 }
555}
556
Eric Snowfdf282d2019-01-11 14:26:55 -0700557/* Py_MakePendingCalls() is a simple wrapper for the sake
558 of backward-compatibility. */
559int
560Py_MakePendingCalls(void)
561{
562 assert(PyGILState_Check());
563
564 /* Python signal handler doesn't really queue a callback: it only signals
565 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinner09532fe2019-05-10 23:39:09 +0200566 _PyRuntimeState *runtime = &_PyRuntime;
567 int res = handle_signals(runtime);
Eric Snowfdf282d2019-01-11 14:26:55 -0700568 if (res != 0) {
569 return res;
570 }
571
Victor Stinner09532fe2019-05-10 23:39:09 +0200572 res = make_pending_calls(runtime);
Eric Snowb75b1a352019-04-12 10:20:10 -0600573 if (res != 0) {
574 return res;
575 }
576
577 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000578}
579
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000580/* The interpreter's recursion limit */
581
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000582#ifndef Py_DEFAULT_RECURSION_LIMIT
583#define Py_DEFAULT_RECURSION_LIMIT 1000
584#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600585
Eric Snow05351c12017-09-05 21:43:08 -0700586int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000587
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600588void
589_PyEval_Initialize(struct _ceval_runtime_state *state)
590{
591 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
592 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
593 _gil_initialize(&state->gil);
594}
595
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000596int
597Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000598{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600599 return _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000600}
601
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000602void
603Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000604{
Victor Stinner09532fe2019-05-10 23:39:09 +0200605 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
606 ceval->recursion_limit = new_limit;
607 _Py_CheckRecursionLimit = ceval->recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000608}
609
Armin Rigo2b3eb402003-10-28 12:05:48 +0000610/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
611 if the recursion_depth reaches _Py_CheckRecursionLimit.
612 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
613 to guarantee that _Py_CheckRecursiveCall() is regularly called.
614 Without USE_STACKCHECK, there is no need for this. */
615int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300616_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000617{
Victor Stinner09532fe2019-05-10 23:39:09 +0200618 _PyRuntimeState *runtime = &_PyRuntime;
619 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
620 int recursion_limit = runtime->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000621
622#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700623 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 if (PyOS_CheckStack()) {
625 --tstate->recursion_depth;
626 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
627 return -1;
628 }
pdox18967932017-10-25 23:03:01 -0700629 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700630 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700631#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 if (tstate->recursion_critical)
633 /* Somebody asked that we don't check for recursion. */
634 return 0;
635 if (tstate->overflowed) {
636 if (tstate->recursion_depth > recursion_limit + 50) {
637 /* Overflowing while handling an overflow. Give up. */
638 Py_FatalError("Cannot recover from stack overflow.");
639 }
640 return 0;
641 }
642 if (tstate->recursion_depth > recursion_limit) {
643 --tstate->recursion_depth;
644 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400645 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 "maximum recursion depth exceeded%s",
647 where);
648 return -1;
649 }
650 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000651}
652
Victor Stinner09532fe2019-05-10 23:39:09 +0200653static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Guido van Rossum0368b722007-05-11 16:50:42 +0000654static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000655
Victor Stinner09532fe2019-05-10 23:39:09 +0200656#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000657
Guido van Rossum374a9221991-04-04 10:40:29 +0000658
Guido van Rossumb209a111997-04-29 18:18:01 +0000659PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000660PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000661{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 return PyEval_EvalCodeEx(co,
663 globals, locals,
664 (PyObject **)NULL, 0,
665 (PyObject **)NULL, 0,
666 (PyObject **)NULL, 0,
667 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000668}
669
670
671/* Interpreter main loop */
672
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000673PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000674PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 /* This is for backward compatibility with extension modules that
676 used this API; core interpreter code should call
677 PyEval_EvalFrameEx() */
678 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000679}
680
681PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000682PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000683{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200684 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
685 return interp->eval_frame(f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700686}
687
Victor Stinnerc6944e72016-11-11 02:13:35 +0100688PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700689_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
690{
Guido van Rossum950361c1997-01-24 13:49:28 +0000691#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000693#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200694 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300695 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200696 int opcode; /* Current opcode */
697 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200698 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 PyObject *retval = NULL; /* Return value */
Victor Stinner09532fe2019-05-10 23:39:09 +0200700 _PyRuntimeState * const runtime = &_PyRuntime;
701 PyThreadState * const tstate = _PyRuntimeState_GetThreadState(runtime);
702 struct _ceval_runtime_state * const ceval = &runtime->ceval;
703 _Py_atomic_int * const eval_breaker = &ceval->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000709
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 is true when the line being executed has changed. The
711 initial values are such as to make this false the first
712 time it is tested. */
713 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000714
Serhiy Storchakaab874002016-09-11 13:48:15 +0300715 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 PyObject *names;
717 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000718
Brett Cannon368b4b72012-04-02 12:17:59 -0400719#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200720 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400721#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200722
Antoine Pitroub52ec782009-01-25 16:34:23 +0000723/* Computed GOTOs, or
724 the-optimization-commonly-but-improperly-known-as-"threaded code"
725 using gcc's labels-as-values extension
726 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
727
728 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000730 combined with a lookup table of jump addresses. However, since the
731 indirect jump instruction is shared by all opcodes, the CPU will have a
732 hard time making the right prediction for where to jump next (actually,
733 it will be always wrong except in the uncommon case of a sequence of
734 several identical opcodes).
735
736 "Threaded code" in contrast, uses an explicit jump table and an explicit
737 indirect jump instruction at the end of each opcode. Since the jump
738 instruction is at a different address for each opcode, the CPU will make a
739 separate prediction for each of these instructions, which is equivalent to
740 predicting the second opcode of each opcode pair. These predictions have
741 a much better chance to turn out valid, especially in small bytecode loops.
742
743 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000745 and potentially many more instructions (depending on the pipeline width).
746 A correctly predicted branch, however, is nearly free.
747
748 At the time of this writing, the "threaded code" version is up to 15-20%
749 faster than the normal "switch" version, depending on the compiler and the
750 CPU architecture.
751
752 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
753 because it would render the measurements invalid.
754
755
756 NOTE: care must be taken that the compiler doesn't try to "optimize" the
757 indirect jumps by sharing them between all opcodes. Such optimizations
758 can be disabled on gcc by using the -fno-gcse flag (or possibly
759 -fno-crossjumping).
760*/
761
Antoine Pitrou042b1282010-08-13 21:15:58 +0000762#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000763#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000764#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000765#endif
766
Antoine Pitrou042b1282010-08-13 21:15:58 +0000767#ifdef HAVE_COMPUTED_GOTOS
768 #ifndef USE_COMPUTED_GOTOS
769 #define USE_COMPUTED_GOTOS 1
770 #endif
771#else
772 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
773 #error "Computed gotos are not supported on this compiler."
774 #endif
775 #undef USE_COMPUTED_GOTOS
776 #define USE_COMPUTED_GOTOS 0
777#endif
778
779#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000780/* Import the static jump table */
781#include "opcode_targets.h"
782
Antoine Pitroub52ec782009-01-25 16:34:23 +0000783#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700784 op: \
785 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000786
Antoine Pitroub52ec782009-01-25 16:34:23 +0000787#ifdef LLTRACE
788#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 { \
Victor Stinner09532fe2019-05-10 23:39:09 +0200790 if (!lltrace && !_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300792 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300793 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 } \
795 goto fast_next_opcode; \
796 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000797#else
798#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 { \
Victor Stinner09532fe2019-05-10 23:39:09 +0200800 if (!_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300802 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300803 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 } \
805 goto fast_next_opcode; \
806 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000807#endif
808
Victor Stinner09532fe2019-05-10 23:39:09 +0200809#define DISPATCH() \
810 { \
811 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
812 FAST_DISPATCH(); \
813 } \
814 continue; \
815 }
816
Antoine Pitroub52ec782009-01-25 16:34:23 +0000817#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700818#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000819#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +0200820#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +0000821#endif
822
823
Neal Norwitza81d2202002-07-14 00:27:26 +0000824/* Tuple access macros */
825
826#ifndef Py_DEBUG
827#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
828#else
829#define GETITEM(v, i) PyTuple_GetItem((v), (i))
830#endif
831
Guido van Rossum374a9221991-04-04 10:40:29 +0000832/* Code access macros */
833
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300834/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600835#define INSTR_OFFSET() \
836 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300837#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300838 _Py_CODEUNIT word = *next_instr; \
839 opcode = _Py_OPCODE(word); \
840 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300841 next_instr++; \
842 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300843#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
844#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000845
Raymond Hettingerf606f872003-03-16 03:11:04 +0000846/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 Some opcodes tend to come in pairs thus making it possible to
848 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300849 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 Verifying the prediction costs a single high-speed test of a register
852 variable against a constant. If the pairing was good, then the
853 processor's own internal branch predication has a high likelihood of
854 success, resulting in a nearly zero-overhead transition to the
855 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300856 including its unpredictable switch-case branch. Combined with the
857 processor's internal branch prediction, a successful PREDICT has the
858 effect of making the two opcodes run as if they were a single new opcode
859 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000860
Georg Brandl86b2fb92008-07-16 03:43:04 +0000861 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 predictions turned-on and interpret the results as if some opcodes
863 had been combined or turn-off predictions so that the opcode frequency
864 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000865
866 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 the CPU to record separate branch prediction information for each
868 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000869
Raymond Hettingerf606f872003-03-16 03:11:04 +0000870*/
871
Antoine Pitrou042b1282010-08-13 21:15:58 +0000872#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000874#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300875#define PREDICT(op) \
876 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300877 _Py_CODEUNIT word = *next_instr; \
878 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300879 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300880 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300881 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300882 goto PRED_##op; \
883 } \
884 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000885#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300886#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000887
Raymond Hettingerf606f872003-03-16 03:11:04 +0000888
Guido van Rossum374a9221991-04-04 10:40:29 +0000889/* Stack manipulation macros */
890
Martin v. Löwis18e16552006-02-15 17:27:45 +0000891/* The stack can grow at most MAXINT deep, as co_nlocals and
892 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000893#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
894#define EMPTY() (STACK_LEVEL() == 0)
895#define TOP() (stack_pointer[-1])
896#define SECOND() (stack_pointer[-2])
897#define THIRD() (stack_pointer[-3])
898#define FOURTH() (stack_pointer[-4])
899#define PEEK(n) (stack_pointer[-(n)])
900#define SET_TOP(v) (stack_pointer[-1] = (v))
901#define SET_SECOND(v) (stack_pointer[-2] = (v))
902#define SET_THIRD(v) (stack_pointer[-3] = (v))
903#define SET_FOURTH(v) (stack_pointer[-4] = (v))
904#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
905#define BASIC_STACKADJ(n) (stack_pointer += n)
906#define BASIC_PUSH(v) (*stack_pointer++ = (v))
907#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000908
Guido van Rossum96a42c81992-01-12 02:29:51 +0000909#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000911 lltrace && prtrace(TOP(), "push")); \
912 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000914 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +0000915#define STACK_GROW(n) do { \
916 assert(n >= 0); \
917 (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000918 lltrace && prtrace(TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000919 assert(STACK_LEVEL() <= co->co_stacksize); \
920 } while (0)
921#define STACK_SHRINK(n) do { \
922 assert(n >= 0); \
923 (void)(lltrace && prtrace(TOP(), "stackadj")); \
924 (void)(BASIC_STACKADJ(-n)); \
925 assert(STACK_LEVEL() <= co->co_stacksize); \
926 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +0000927#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000928 prtrace((STACK_POINTER)[-1], "ext_pop")), \
929 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000930#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000931#define PUSH(v) BASIC_PUSH(v)
932#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +0000933#define STACK_GROW(n) BASIC_STACKADJ(n)
934#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000935#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000936#endif
937
Guido van Rossum681d79a1995-07-18 14:51:37 +0000938/* Local variable macros */
939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000941
942/* The SETLOCAL() macro must not DECREF the local variable in-place and
943 then store the new value; it must copy the old value to a temporary
944 value, then store the new value, and then DECREF the temporary value.
945 This is because it is possible that during the DECREF the frame is
946 accessed by other code (e.g. a __del__ method or gc.collect()) and the
947 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000949 GETLOCAL(i) = value; \
950 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000951
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000952
953#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 while (STACK_LEVEL() > (b)->b_level) { \
955 PyObject *v = POP(); \
956 Py_XDECREF(v); \
957 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000958
959#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300960 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +0100962 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 assert(STACK_LEVEL() >= (b)->b_level + 3); \
964 while (STACK_LEVEL() > (b)->b_level + 3) { \
965 value = POP(); \
966 Py_XDECREF(value); \
967 } \
Mark Shannonae3087c2017-10-22 22:41:51 +0100968 exc_info = tstate->exc_info; \
969 type = exc_info->exc_type; \
970 value = exc_info->exc_value; \
971 traceback = exc_info->exc_traceback; \
972 exc_info->exc_type = POP(); \
973 exc_info->exc_value = POP(); \
974 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 Py_XDECREF(type); \
976 Py_XDECREF(value); \
977 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300978 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000979
Guido van Rossuma027efa1997-05-05 20:56:21 +0000980/* Start of code */
981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 /* push frame */
983 if (Py_EnterRecursiveCall(""))
984 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 if (tstate->use_tracing) {
989 if (tstate->c_tracefunc != NULL) {
990 /* tstate->c_tracefunc, if defined, is a
991 function that will be called on *every* entry
992 to a code block. Its return value, if not
993 None, is a function that will be called at
994 the start of each executed line of code.
995 (Actually, the function must return itself
996 in order to continue tracing.) The trace
997 functions are called with three arguments:
998 a pointer to the current frame, a string
999 indicating why the function is called, and
1000 an argument which depends on the situation.
1001 The global trace function is also called
1002 whenever an exception is detected. */
1003 if (call_trace_protected(tstate->c_tracefunc,
1004 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001005 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 /* Trace function raised an error */
1007 goto exit_eval_frame;
1008 }
1009 }
1010 if (tstate->c_profilefunc != NULL) {
1011 /* Similar for c_profilefunc, except it needn't
1012 return itself and isn't called for "line" events */
1013 if (call_trace_protected(tstate->c_profilefunc,
1014 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001015 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 /* Profile function raised an error */
1017 goto exit_eval_frame;
1018 }
1019 }
1020 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001021
Łukasz Langaa785c872016-09-09 17:37:37 -07001022 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1023 dtrace_function_entry(f);
1024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 co = f->f_code;
1026 names = co->co_names;
1027 consts = co->co_consts;
1028 fastlocals = f->f_localsplus;
1029 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001030 assert(PyBytes_Check(co->co_code));
1031 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001032 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1033 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1034 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001035 /*
1036 f->f_lasti refers to the index of the last instruction,
1037 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001038
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001039 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001040 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 When the PREDICT() macros are enabled, some opcode pairs follow in
1043 direct succession without updating f->f_lasti. A successful
1044 prediction effectively links the two codes together as if they
1045 were a single new opcode; accordingly,f->f_lasti will point to
1046 the first code in the pair (for instance, GET_ITER followed by
1047 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001048 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001050 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001051 next_instr = first_instr;
1052 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001053 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1054 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001055 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 stack_pointer = f->f_stacktop;
1057 assert(stack_pointer != NULL);
1058 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001059 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001060
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001061
Tim Peters5ca576e2001-06-18 22:08:13 +00001062#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001063 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001064#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001065
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001066 if (throwflag) /* support for generator.throw() */
1067 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001068
Victor Stinnerace47d72013-07-18 01:41:08 +02001069#ifdef Py_DEBUG
1070 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001071 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001072 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +02001073 assert(!PyErr_Occurred());
1074#endif
1075
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001076main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1079 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +02001080 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 /* Do periodic things. Doing this every time through
1083 the loop would add too much overhead, so we do it
1084 only every Nth instruction. We also do it if
1085 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1086 event needs attention (e.g. a signal handler or
1087 async I/O handler); see Py_AddPendingCall() and
1088 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001089
Eric Snow7bda9de2019-03-08 17:25:54 -07001090 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001091 opcode = _Py_OPCODE(*next_instr);
1092 if (opcode == SETUP_FINALLY ||
1093 opcode == SETUP_WITH ||
1094 opcode == BEFORE_ASYNC_WITH ||
1095 opcode == YIELD_FROM) {
1096 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001097 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001098 - If we're about to enter the 'with:'. It will prevent
1099 emitting a resource warning in the common idiom
1100 'with open(path) as file:'.
1101 - If we're about to enter the 'async with:'.
1102 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001103 *very* useful, but might help in some cases and it's
1104 traditional)
1105 - If we're resuming a chain of nested 'yield from' or
1106 'await' calls, then each frame is parked with YIELD_FROM
1107 as its next opcode. If the user hit control-C we want to
1108 wait until we've reached the innermost frame before
1109 running the signal handler and raising KeyboardInterrupt
1110 (see bpo-30039).
1111 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 goto fast_next_opcode;
1113 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001114
Victor Stinner09532fe2019-05-10 23:39:09 +02001115 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
1116 if (handle_signals(runtime) != 0) {
Eric Snowfdf282d2019-01-11 14:26:55 -07001117 goto error;
1118 }
1119 }
Victor Stinner09532fe2019-05-10 23:39:09 +02001120 if (_Py_atomic_load_relaxed(&ceval->pending.calls_to_do)) {
1121 if (make_pending_calls(runtime) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001122 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001123 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001125
Victor Stinner09532fe2019-05-10 23:39:09 +02001126 if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 /* Give another thread a chance */
Victor Stinner09532fe2019-05-10 23:39:09 +02001128 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 Py_FatalError("ceval: tstate mix-up");
Victor Stinner09532fe2019-05-10 23:39:09 +02001130 }
1131 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132
1133 /* Other threads may run now */
1134
Victor Stinner09532fe2019-05-10 23:39:09 +02001135 take_gil(ceval, tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001136
1137 /* Check if we should make a quick exit. */
Victor Stinner09532fe2019-05-10 23:39:09 +02001138 exit_thread_if_finalizing(runtime, tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001139
Victor Stinner09532fe2019-05-10 23:39:09 +02001140 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 Py_FatalError("ceval: orphan tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +02001142 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 }
1144 /* Check for asynchronous exceptions. */
1145 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001146 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 tstate->async_exc = NULL;
Victor Stinner09532fe2019-05-10 23:39:09 +02001148 UNSIGNAL_ASYNC_EXC(ceval);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001149 PyErr_SetNone(exc);
1150 Py_DECREF(exc);
1151 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 }
1153 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 fast_next_opcode:
1156 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001157
Łukasz Langaa785c872016-09-09 17:37:37 -07001158 if (PyDTrace_LINE_ENABLED())
1159 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001162
Victor Stinner09532fe2019-05-10 23:39:09 +02001163 if (_Py_TracingPossible(ceval) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001164 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001165 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 /* see maybe_call_line_trace
1167 for expository comments */
1168 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 err = maybe_call_line_trace(tstate->c_tracefunc,
1171 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001172 tstate, f,
1173 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 /* Reload possibly changed frame fields */
1175 JUMPTO(f->f_lasti);
1176 if (f->f_stacktop != NULL) {
1177 stack_pointer = f->f_stacktop;
1178 f->f_stacktop = NULL;
1179 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001180 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001182 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001186
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001187 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001188 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001189#ifdef DYNAMIC_EXECUTION_PROFILE
1190#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 dxpairs[lastopcode][opcode]++;
1192 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001193#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001195#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001196
Guido van Rossum96a42c81992-01-12 02:29:51 +00001197#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 if (lltrace) {
1201 if (HAS_ARG(opcode)) {
1202 printf("%d: %d, %d\n",
1203 f->f_lasti, opcode, oparg);
1204 }
1205 else {
1206 printf("%d: %d\n",
1207 f->f_lasti, opcode);
1208 }
1209 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001210#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001215 It is essential that any operation that fails must goto error
1216 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001217
Benjamin Petersonddd19492018-09-16 22:38:02 -07001218 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001220 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001221
Benjamin Petersonddd19492018-09-16 22:38:02 -07001222 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001223 PyObject *value = GETLOCAL(oparg);
1224 if (value == NULL) {
1225 format_exc_check_arg(PyExc_UnboundLocalError,
1226 UNBOUNDLOCAL_ERROR_MSG,
1227 PyTuple_GetItem(co->co_varnames, oparg));
1228 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001230 Py_INCREF(value);
1231 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001233 }
1234
Benjamin Petersonddd19492018-09-16 22:38:02 -07001235 case TARGET(LOAD_CONST): {
1236 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001237 PyObject *value = GETITEM(consts, oparg);
1238 Py_INCREF(value);
1239 PUSH(value);
1240 FAST_DISPATCH();
1241 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001242
Benjamin Petersonddd19492018-09-16 22:38:02 -07001243 case TARGET(STORE_FAST): {
1244 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001245 PyObject *value = POP();
1246 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001248 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001249
Benjamin Petersonddd19492018-09-16 22:38:02 -07001250 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001251 PyObject *value = POP();
1252 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001254 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001255
Benjamin Petersonddd19492018-09-16 22:38:02 -07001256 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001257 PyObject *top = TOP();
1258 PyObject *second = SECOND();
1259 SET_TOP(second);
1260 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001262 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001263
Benjamin Petersonddd19492018-09-16 22:38:02 -07001264 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001265 PyObject *top = TOP();
1266 PyObject *second = SECOND();
1267 PyObject *third = THIRD();
1268 SET_TOP(second);
1269 SET_SECOND(third);
1270 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001272 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001273
Benjamin Petersonddd19492018-09-16 22:38:02 -07001274 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001275 PyObject *top = TOP();
1276 PyObject *second = SECOND();
1277 PyObject *third = THIRD();
1278 PyObject *fourth = FOURTH();
1279 SET_TOP(second);
1280 SET_SECOND(third);
1281 SET_THIRD(fourth);
1282 SET_FOURTH(top);
1283 FAST_DISPATCH();
1284 }
1285
Benjamin Petersonddd19492018-09-16 22:38:02 -07001286 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001287 PyObject *top = TOP();
1288 Py_INCREF(top);
1289 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001291 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001292
Benjamin Petersonddd19492018-09-16 22:38:02 -07001293 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001294 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001295 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001296 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001297 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001298 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001299 SET_TOP(top);
1300 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001301 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001302 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001303
Benjamin Petersonddd19492018-09-16 22:38:02 -07001304 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001305 PyObject *value = TOP();
1306 PyObject *res = PyNumber_Positive(value);
1307 Py_DECREF(value);
1308 SET_TOP(res);
1309 if (res == NULL)
1310 goto error;
1311 DISPATCH();
1312 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001313
Benjamin Petersonddd19492018-09-16 22:38:02 -07001314 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001315 PyObject *value = TOP();
1316 PyObject *res = PyNumber_Negative(value);
1317 Py_DECREF(value);
1318 SET_TOP(res);
1319 if (res == NULL)
1320 goto error;
1321 DISPATCH();
1322 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001323
Benjamin Petersonddd19492018-09-16 22:38:02 -07001324 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001325 PyObject *value = TOP();
1326 int err = PyObject_IsTrue(value);
1327 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 if (err == 0) {
1329 Py_INCREF(Py_True);
1330 SET_TOP(Py_True);
1331 DISPATCH();
1332 }
1333 else if (err > 0) {
1334 Py_INCREF(Py_False);
1335 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 DISPATCH();
1337 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001338 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001339 goto error;
1340 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001341
Benjamin Petersonddd19492018-09-16 22:38:02 -07001342 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001343 PyObject *value = TOP();
1344 PyObject *res = PyNumber_Invert(value);
1345 Py_DECREF(value);
1346 SET_TOP(res);
1347 if (res == NULL)
1348 goto error;
1349 DISPATCH();
1350 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001351
Benjamin Petersonddd19492018-09-16 22:38:02 -07001352 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001353 PyObject *exp = POP();
1354 PyObject *base = TOP();
1355 PyObject *res = PyNumber_Power(base, exp, Py_None);
1356 Py_DECREF(base);
1357 Py_DECREF(exp);
1358 SET_TOP(res);
1359 if (res == NULL)
1360 goto error;
1361 DISPATCH();
1362 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001363
Benjamin Petersonddd19492018-09-16 22:38:02 -07001364 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001365 PyObject *right = POP();
1366 PyObject *left = TOP();
1367 PyObject *res = PyNumber_Multiply(left, right);
1368 Py_DECREF(left);
1369 Py_DECREF(right);
1370 SET_TOP(res);
1371 if (res == NULL)
1372 goto error;
1373 DISPATCH();
1374 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001375
Benjamin Petersonddd19492018-09-16 22:38:02 -07001376 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001377 PyObject *right = POP();
1378 PyObject *left = TOP();
1379 PyObject *res = PyNumber_MatrixMultiply(left, right);
1380 Py_DECREF(left);
1381 Py_DECREF(right);
1382 SET_TOP(res);
1383 if (res == NULL)
1384 goto error;
1385 DISPATCH();
1386 }
1387
Benjamin Petersonddd19492018-09-16 22:38:02 -07001388 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001389 PyObject *divisor = POP();
1390 PyObject *dividend = TOP();
1391 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1392 Py_DECREF(dividend);
1393 Py_DECREF(divisor);
1394 SET_TOP(quotient);
1395 if (quotient == NULL)
1396 goto error;
1397 DISPATCH();
1398 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001399
Benjamin Petersonddd19492018-09-16 22:38:02 -07001400 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001401 PyObject *divisor = POP();
1402 PyObject *dividend = TOP();
1403 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1404 Py_DECREF(dividend);
1405 Py_DECREF(divisor);
1406 SET_TOP(quotient);
1407 if (quotient == NULL)
1408 goto error;
1409 DISPATCH();
1410 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001411
Benjamin Petersonddd19492018-09-16 22:38:02 -07001412 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001413 PyObject *divisor = POP();
1414 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001415 PyObject *res;
1416 if (PyUnicode_CheckExact(dividend) && (
1417 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1418 // fast path; string formatting, but not if the RHS is a str subclass
1419 // (see issue28598)
1420 res = PyUnicode_Format(dividend, divisor);
1421 } else {
1422 res = PyNumber_Remainder(dividend, divisor);
1423 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001424 Py_DECREF(divisor);
1425 Py_DECREF(dividend);
1426 SET_TOP(res);
1427 if (res == NULL)
1428 goto error;
1429 DISPATCH();
1430 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001431
Benjamin Petersonddd19492018-09-16 22:38:02 -07001432 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001433 PyObject *right = POP();
1434 PyObject *left = TOP();
1435 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001436 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1437 CPython using bytecode, it is simply worthless.
1438 See http://bugs.python.org/issue21955 and
1439 http://bugs.python.org/issue10044 for the discussion. In short,
1440 no patch shown any impact on a realistic benchmark, only a minor
1441 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001442 if (PyUnicode_CheckExact(left) &&
1443 PyUnicode_CheckExact(right)) {
1444 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001445 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001446 }
1447 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001448 sum = PyNumber_Add(left, right);
1449 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001450 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001451 Py_DECREF(right);
1452 SET_TOP(sum);
1453 if (sum == NULL)
1454 goto error;
1455 DISPATCH();
1456 }
1457
Benjamin Petersonddd19492018-09-16 22:38:02 -07001458 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001459 PyObject *right = POP();
1460 PyObject *left = TOP();
1461 PyObject *diff = PyNumber_Subtract(left, right);
1462 Py_DECREF(right);
1463 Py_DECREF(left);
1464 SET_TOP(diff);
1465 if (diff == NULL)
1466 goto error;
1467 DISPATCH();
1468 }
1469
Benjamin Petersonddd19492018-09-16 22:38:02 -07001470 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001471 PyObject *sub = POP();
1472 PyObject *container = TOP();
1473 PyObject *res = PyObject_GetItem(container, sub);
1474 Py_DECREF(container);
1475 Py_DECREF(sub);
1476 SET_TOP(res);
1477 if (res == NULL)
1478 goto error;
1479 DISPATCH();
1480 }
1481
Benjamin Petersonddd19492018-09-16 22:38:02 -07001482 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001483 PyObject *right = POP();
1484 PyObject *left = TOP();
1485 PyObject *res = PyNumber_Lshift(left, right);
1486 Py_DECREF(left);
1487 Py_DECREF(right);
1488 SET_TOP(res);
1489 if (res == NULL)
1490 goto error;
1491 DISPATCH();
1492 }
1493
Benjamin Petersonddd19492018-09-16 22:38:02 -07001494 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001495 PyObject *right = POP();
1496 PyObject *left = TOP();
1497 PyObject *res = PyNumber_Rshift(left, right);
1498 Py_DECREF(left);
1499 Py_DECREF(right);
1500 SET_TOP(res);
1501 if (res == NULL)
1502 goto error;
1503 DISPATCH();
1504 }
1505
Benjamin Petersonddd19492018-09-16 22:38:02 -07001506 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001507 PyObject *right = POP();
1508 PyObject *left = TOP();
1509 PyObject *res = PyNumber_And(left, right);
1510 Py_DECREF(left);
1511 Py_DECREF(right);
1512 SET_TOP(res);
1513 if (res == NULL)
1514 goto error;
1515 DISPATCH();
1516 }
1517
Benjamin Petersonddd19492018-09-16 22:38:02 -07001518 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001519 PyObject *right = POP();
1520 PyObject *left = TOP();
1521 PyObject *res = PyNumber_Xor(left, right);
1522 Py_DECREF(left);
1523 Py_DECREF(right);
1524 SET_TOP(res);
1525 if (res == NULL)
1526 goto error;
1527 DISPATCH();
1528 }
1529
Benjamin Petersonddd19492018-09-16 22:38:02 -07001530 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001531 PyObject *right = POP();
1532 PyObject *left = TOP();
1533 PyObject *res = PyNumber_Or(left, right);
1534 Py_DECREF(left);
1535 Py_DECREF(right);
1536 SET_TOP(res);
1537 if (res == NULL)
1538 goto error;
1539 DISPATCH();
1540 }
1541
Benjamin Petersonddd19492018-09-16 22:38:02 -07001542 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001543 PyObject *v = POP();
1544 PyObject *list = PEEK(oparg);
1545 int err;
1546 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001548 if (err != 0)
1549 goto error;
1550 PREDICT(JUMP_ABSOLUTE);
1551 DISPATCH();
1552 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001553
Benjamin Petersonddd19492018-09-16 22:38:02 -07001554 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001555 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001556 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001557 int err;
1558 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001560 if (err != 0)
1561 goto error;
1562 PREDICT(JUMP_ABSOLUTE);
1563 DISPATCH();
1564 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001565
Benjamin Petersonddd19492018-09-16 22:38:02 -07001566 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001567 PyObject *exp = POP();
1568 PyObject *base = TOP();
1569 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1570 Py_DECREF(base);
1571 Py_DECREF(exp);
1572 SET_TOP(res);
1573 if (res == NULL)
1574 goto error;
1575 DISPATCH();
1576 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001577
Benjamin Petersonddd19492018-09-16 22:38:02 -07001578 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001579 PyObject *right = POP();
1580 PyObject *left = TOP();
1581 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1582 Py_DECREF(left);
1583 Py_DECREF(right);
1584 SET_TOP(res);
1585 if (res == NULL)
1586 goto error;
1587 DISPATCH();
1588 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001589
Benjamin Petersonddd19492018-09-16 22:38:02 -07001590 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001591 PyObject *right = POP();
1592 PyObject *left = TOP();
1593 PyObject *res = PyNumber_InPlaceMatrixMultiply(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(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001603 PyObject *divisor = POP();
1604 PyObject *dividend = TOP();
1605 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1606 Py_DECREF(dividend);
1607 Py_DECREF(divisor);
1608 SET_TOP(quotient);
1609 if (quotient == NULL)
1610 goto error;
1611 DISPATCH();
1612 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001613
Benjamin Petersonddd19492018-09-16 22:38:02 -07001614 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001615 PyObject *divisor = POP();
1616 PyObject *dividend = TOP();
1617 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1618 Py_DECREF(dividend);
1619 Py_DECREF(divisor);
1620 SET_TOP(quotient);
1621 if (quotient == NULL)
1622 goto error;
1623 DISPATCH();
1624 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001625
Benjamin Petersonddd19492018-09-16 22:38:02 -07001626 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001627 PyObject *right = POP();
1628 PyObject *left = TOP();
1629 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1630 Py_DECREF(left);
1631 Py_DECREF(right);
1632 SET_TOP(mod);
1633 if (mod == NULL)
1634 goto error;
1635 DISPATCH();
1636 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001637
Benjamin Petersonddd19492018-09-16 22:38:02 -07001638 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001639 PyObject *right = POP();
1640 PyObject *left = TOP();
1641 PyObject *sum;
1642 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1643 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001644 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001645 }
1646 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001647 sum = PyNumber_InPlaceAdd(left, right);
1648 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001649 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001650 Py_DECREF(right);
1651 SET_TOP(sum);
1652 if (sum == NULL)
1653 goto error;
1654 DISPATCH();
1655 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001656
Benjamin Petersonddd19492018-09-16 22:38:02 -07001657 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001658 PyObject *right = POP();
1659 PyObject *left = TOP();
1660 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1661 Py_DECREF(left);
1662 Py_DECREF(right);
1663 SET_TOP(diff);
1664 if (diff == NULL)
1665 goto error;
1666 DISPATCH();
1667 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001668
Benjamin Petersonddd19492018-09-16 22:38:02 -07001669 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001670 PyObject *right = POP();
1671 PyObject *left = TOP();
1672 PyObject *res = PyNumber_InPlaceLshift(left, right);
1673 Py_DECREF(left);
1674 Py_DECREF(right);
1675 SET_TOP(res);
1676 if (res == NULL)
1677 goto error;
1678 DISPATCH();
1679 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001680
Benjamin Petersonddd19492018-09-16 22:38:02 -07001681 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001682 PyObject *right = POP();
1683 PyObject *left = TOP();
1684 PyObject *res = PyNumber_InPlaceRshift(left, right);
1685 Py_DECREF(left);
1686 Py_DECREF(right);
1687 SET_TOP(res);
1688 if (res == NULL)
1689 goto error;
1690 DISPATCH();
1691 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001692
Benjamin Petersonddd19492018-09-16 22:38:02 -07001693 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001694 PyObject *right = POP();
1695 PyObject *left = TOP();
1696 PyObject *res = PyNumber_InPlaceAnd(left, right);
1697 Py_DECREF(left);
1698 Py_DECREF(right);
1699 SET_TOP(res);
1700 if (res == NULL)
1701 goto error;
1702 DISPATCH();
1703 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001704
Benjamin Petersonddd19492018-09-16 22:38:02 -07001705 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001706 PyObject *right = POP();
1707 PyObject *left = TOP();
1708 PyObject *res = PyNumber_InPlaceXor(left, right);
1709 Py_DECREF(left);
1710 Py_DECREF(right);
1711 SET_TOP(res);
1712 if (res == NULL)
1713 goto error;
1714 DISPATCH();
1715 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001716
Benjamin Petersonddd19492018-09-16 22:38:02 -07001717 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001718 PyObject *right = POP();
1719 PyObject *left = TOP();
1720 PyObject *res = PyNumber_InPlaceOr(left, right);
1721 Py_DECREF(left);
1722 Py_DECREF(right);
1723 SET_TOP(res);
1724 if (res == NULL)
1725 goto error;
1726 DISPATCH();
1727 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001728
Benjamin Petersonddd19492018-09-16 22:38:02 -07001729 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001730 PyObject *sub = TOP();
1731 PyObject *container = SECOND();
1732 PyObject *v = THIRD();
1733 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001734 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001735 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001736 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001738 Py_DECREF(container);
1739 Py_DECREF(sub);
1740 if (err != 0)
1741 goto error;
1742 DISPATCH();
1743 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001744
Benjamin Petersonddd19492018-09-16 22:38:02 -07001745 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001746 PyObject *sub = TOP();
1747 PyObject *container = SECOND();
1748 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001749 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001750 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001751 err = PyObject_DelItem(container, sub);
1752 Py_DECREF(container);
1753 Py_DECREF(sub);
1754 if (err != 0)
1755 goto error;
1756 DISPATCH();
1757 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001758
Benjamin Petersonddd19492018-09-16 22:38:02 -07001759 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001760 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001761 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001762 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001763 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001764 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001765 PyErr_SetString(PyExc_RuntimeError,
1766 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001767 Py_DECREF(value);
1768 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001770 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001771 Py_DECREF(value);
1772 if (res == NULL)
1773 goto error;
1774 Py_DECREF(res);
1775 DISPATCH();
1776 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001777
Benjamin Petersonddd19492018-09-16 22:38:02 -07001778 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001779 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001780 switch (oparg) {
1781 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001782 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001783 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001785 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001786 /* fall through */
1787 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02001788 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001789 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001790 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 break;
1792 default:
1793 PyErr_SetString(PyExc_SystemError,
1794 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 break;
1796 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001797 goto error;
1798 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001799
Benjamin Petersonddd19492018-09-16 22:38:02 -07001800 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001802 assert(f->f_iblock == 0);
Pablo Galindof00828a2019-05-09 16:52:02 +01001803 goto exit_returning;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001804 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001805
Benjamin Petersonddd19492018-09-16 22:38:02 -07001806 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001807 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001808 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001809 PyObject *obj = TOP();
1810 PyTypeObject *type = Py_TYPE(obj);
1811
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001812 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001813 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001814 }
Yury Selivanov75445082015-05-11 22:57:16 -04001815
1816 if (getter != NULL) {
1817 iter = (*getter)(obj);
1818 Py_DECREF(obj);
1819 if (iter == NULL) {
1820 SET_TOP(NULL);
1821 goto error;
1822 }
1823 }
1824 else {
1825 SET_TOP(NULL);
1826 PyErr_Format(
1827 PyExc_TypeError,
1828 "'async for' requires an object with "
1829 "__aiter__ method, got %.100s",
1830 type->tp_name);
1831 Py_DECREF(obj);
1832 goto error;
1833 }
1834
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001835 if (Py_TYPE(iter)->tp_as_async == NULL ||
1836 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001837
Yury Selivanov398ff912017-03-02 22:20:00 -05001838 SET_TOP(NULL);
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001839 PyErr_Format(
1840 PyExc_TypeError,
1841 "'async for' received an object from __aiter__ "
1842 "that does not implement __anext__: %.100s",
1843 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001844 Py_DECREF(iter);
1845 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001846 }
1847
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001848 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001849 DISPATCH();
1850 }
1851
Benjamin Petersonddd19492018-09-16 22:38:02 -07001852 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001853 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001854 PyObject *next_iter = NULL;
1855 PyObject *awaitable = NULL;
1856 PyObject *aiter = TOP();
1857 PyTypeObject *type = Py_TYPE(aiter);
1858
Yury Selivanoveb636452016-09-08 22:01:51 -07001859 if (PyAsyncGen_CheckExact(aiter)) {
1860 awaitable = type->tp_as_async->am_anext(aiter);
1861 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001862 goto error;
1863 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001864 } else {
1865 if (type->tp_as_async != NULL){
1866 getter = type->tp_as_async->am_anext;
1867 }
Yury Selivanov75445082015-05-11 22:57:16 -04001868
Yury Selivanoveb636452016-09-08 22:01:51 -07001869 if (getter != NULL) {
1870 next_iter = (*getter)(aiter);
1871 if (next_iter == NULL) {
1872 goto error;
1873 }
1874 }
1875 else {
1876 PyErr_Format(
1877 PyExc_TypeError,
1878 "'async for' requires an iterator with "
1879 "__anext__ method, got %.100s",
1880 type->tp_name);
1881 goto error;
1882 }
Yury Selivanov75445082015-05-11 22:57:16 -04001883
Yury Selivanoveb636452016-09-08 22:01:51 -07001884 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1885 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001886 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001887 PyExc_TypeError,
1888 "'async for' received an invalid object "
1889 "from __anext__: %.100s",
1890 Py_TYPE(next_iter)->tp_name);
1891
1892 Py_DECREF(next_iter);
1893 goto error;
1894 } else {
1895 Py_DECREF(next_iter);
1896 }
1897 }
Yury Selivanov75445082015-05-11 22:57:16 -04001898
1899 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001900 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001901 DISPATCH();
1902 }
1903
Benjamin Petersonddd19492018-09-16 22:38:02 -07001904 case TARGET(GET_AWAITABLE): {
1905 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001906 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001907 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001908
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03001909 if (iter == NULL) {
1910 format_awaitable_error(Py_TYPE(iterable),
1911 _Py_OPCODE(next_instr[-2]));
1912 }
1913
Yury Selivanov75445082015-05-11 22:57:16 -04001914 Py_DECREF(iterable);
1915
Yury Selivanovc724bae2016-03-02 11:30:46 -05001916 if (iter != NULL && PyCoro_CheckExact(iter)) {
1917 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1918 if (yf != NULL) {
1919 /* `iter` is a coroutine object that is being
1920 awaited, `yf` is a pointer to the current awaitable
1921 being awaited on. */
1922 Py_DECREF(yf);
1923 Py_CLEAR(iter);
1924 PyErr_SetString(
1925 PyExc_RuntimeError,
1926 "coroutine is being awaited already");
1927 /* The code below jumps to `error` if `iter` is NULL. */
1928 }
1929 }
1930
Yury Selivanov75445082015-05-11 22:57:16 -04001931 SET_TOP(iter); /* Even if it's NULL */
1932
1933 if (iter == NULL) {
1934 goto error;
1935 }
1936
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001937 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001938 DISPATCH();
1939 }
1940
Benjamin Petersonddd19492018-09-16 22:38:02 -07001941 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001942 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001943 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001944 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001945 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1946 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001947 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001948 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001949 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001950 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001951 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001952 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001953 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001954 Py_DECREF(v);
1955 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001956 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001957 if (tstate->c_tracefunc != NULL
1958 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001959 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001960 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001961 if (err < 0)
1962 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001963 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001964 SET_TOP(val);
1965 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001966 }
Martin Panter95f53c12016-07-18 08:23:26 +00001967 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001968 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001969 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001970 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001971 f->f_lasti -= sizeof(_Py_CODEUNIT);
Pablo Galindof00828a2019-05-09 16:52:02 +01001972 goto exit_yielding;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001973 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001974
Benjamin Petersonddd19492018-09-16 22:38:02 -07001975 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07001977
1978 if (co->co_flags & CO_ASYNC_GENERATOR) {
1979 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
1980 Py_DECREF(retval);
1981 if (w == NULL) {
1982 retval = NULL;
1983 goto error;
1984 }
1985 retval = w;
1986 }
1987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988 f->f_stacktop = stack_pointer;
Pablo Galindof00828a2019-05-09 16:52:02 +01001989 goto exit_yielding;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001990 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001991
Benjamin Petersonddd19492018-09-16 22:38:02 -07001992 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001993 PyObject *type, *value, *traceback;
1994 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001995 PyTryBlock *b = PyFrame_BlockPop(f);
1996 if (b->b_type != EXCEPT_HANDLER) {
1997 PyErr_SetString(PyExc_SystemError,
1998 "popped block is not an except handler");
1999 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002001 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2002 STACK_LEVEL() <= (b)->b_level + 4);
2003 exc_info = tstate->exc_info;
2004 type = exc_info->exc_type;
2005 value = exc_info->exc_value;
2006 traceback = exc_info->exc_traceback;
2007 exc_info->exc_type = POP();
2008 exc_info->exc_value = POP();
2009 exc_info->exc_traceback = POP();
2010 Py_XDECREF(type);
2011 Py_XDECREF(value);
2012 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002013 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002014 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002015
Benjamin Petersonddd19492018-09-16 22:38:02 -07002016 case TARGET(POP_BLOCK): {
2017 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002018 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002019 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002020 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002021
Benjamin Petersonddd19492018-09-16 22:38:02 -07002022 case TARGET(POP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002023 /* If oparg is 0 at the top of the stack are 1 or 6 values:
2024 Either:
2025 - TOP = NULL or an integer
2026 or:
2027 - (TOP, SECOND, THIRD) = exc_info()
2028 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2029
2030 If oparg is 1 the value for 'return' was additionally pushed
2031 at the top of the stack.
2032 */
2033 PyObject *res = NULL;
2034 if (oparg) {
2035 res = POP();
2036 }
2037 PyObject *exc = POP();
2038 if (exc == NULL || PyLong_CheckExact(exc)) {
2039 Py_XDECREF(exc);
2040 }
2041 else {
2042 Py_DECREF(exc);
2043 Py_DECREF(POP());
2044 Py_DECREF(POP());
2045
2046 PyObject *type, *value, *traceback;
2047 _PyErr_StackItem *exc_info;
2048 PyTryBlock *b = PyFrame_BlockPop(f);
2049 if (b->b_type != EXCEPT_HANDLER) {
2050 PyErr_SetString(PyExc_SystemError,
2051 "popped block is not an except handler");
2052 Py_XDECREF(res);
2053 goto error;
2054 }
2055 assert(STACK_LEVEL() == (b)->b_level + 3);
2056 exc_info = tstate->exc_info;
2057 type = exc_info->exc_type;
2058 value = exc_info->exc_value;
2059 traceback = exc_info->exc_traceback;
2060 exc_info->exc_type = POP();
2061 exc_info->exc_value = POP();
2062 exc_info->exc_traceback = POP();
2063 Py_XDECREF(type);
2064 Py_XDECREF(value);
2065 Py_XDECREF(traceback);
2066 }
2067 if (oparg) {
2068 PUSH(res);
2069 }
2070 DISPATCH();
2071 }
2072
Benjamin Petersonddd19492018-09-16 22:38:02 -07002073 case TARGET(CALL_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002074 PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
2075 if (ret == NULL) {
2076 goto error;
2077 }
2078 PUSH(ret);
2079 JUMPBY(oparg);
2080 FAST_DISPATCH();
2081 }
2082
Benjamin Petersonddd19492018-09-16 22:38:02 -07002083 case TARGET(BEGIN_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002084 /* Push NULL onto the stack for using it in END_FINALLY,
2085 POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH.
2086 */
2087 PUSH(NULL);
2088 FAST_DISPATCH();
2089 }
2090
Benjamin Petersonddd19492018-09-16 22:38:02 -07002091 case TARGET(END_FINALLY): {
2092 PREDICTED(END_FINALLY);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002093 /* At the top of the stack are 1 or 6 values:
2094 Either:
2095 - TOP = NULL or an integer
2096 or:
2097 - (TOP, SECOND, THIRD) = exc_info()
2098 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2099 */
2100 PyObject *exc = POP();
2101 if (exc == NULL) {
2102 FAST_DISPATCH();
2103 }
2104 else if (PyLong_CheckExact(exc)) {
2105 int ret = _PyLong_AsInt(exc);
2106 Py_DECREF(exc);
2107 if (ret == -1 && PyErr_Occurred()) {
2108 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002109 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002110 JUMPTO(ret);
2111 FAST_DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002113 else {
2114 assert(PyExceptionClass_Check(exc));
2115 PyObject *val = POP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002116 PyObject *tb = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002117 PyErr_Restore(exc, val, tb);
2118 goto exception_unwind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002119 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002120 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002121
Benjamin Petersonddd19492018-09-16 22:38:02 -07002122 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002123 PyObject *exc = POP();
2124 assert(PyExceptionClass_Check(exc));
2125 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2126 PyTryBlock *b = PyFrame_BlockPop(f);
2127 assert(b->b_type == EXCEPT_HANDLER);
2128 Py_DECREF(exc);
2129 UNWIND_EXCEPT_HANDLER(b);
2130 Py_DECREF(POP());
2131 JUMPBY(oparg);
2132 FAST_DISPATCH();
2133 }
2134 else {
2135 PyObject *val = POP();
2136 PyObject *tb = POP();
2137 PyErr_Restore(exc, val, tb);
2138 goto exception_unwind;
2139 }
2140 }
2141
Benjamin Petersonddd19492018-09-16 22:38:02 -07002142 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002143 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002144
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002145 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002146 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002147 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002148 if (bc == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002149 if (!PyErr_Occurred()) {
2150 PyErr_SetString(PyExc_NameError,
2151 "__build_class__ not found");
2152 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002153 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002154 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002155 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002156 }
2157 else {
2158 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2159 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002160 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002161 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2162 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002163 if (PyErr_ExceptionMatches(PyExc_KeyError))
2164 PyErr_SetString(PyExc_NameError,
2165 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002166 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002167 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002168 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002169 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002170 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002171 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002172
Benjamin Petersonddd19492018-09-16 22:38:02 -07002173 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002174 PyObject *name = GETITEM(names, oparg);
2175 PyObject *v = POP();
2176 PyObject *ns = f->f_locals;
2177 int err;
2178 if (ns == NULL) {
2179 PyErr_Format(PyExc_SystemError,
2180 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002182 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002183 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002184 if (PyDict_CheckExact(ns))
2185 err = PyDict_SetItem(ns, name, v);
2186 else
2187 err = PyObject_SetItem(ns, name, v);
2188 Py_DECREF(v);
2189 if (err != 0)
2190 goto error;
2191 DISPATCH();
2192 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002193
Benjamin Petersonddd19492018-09-16 22:38:02 -07002194 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002195 PyObject *name = GETITEM(names, oparg);
2196 PyObject *ns = f->f_locals;
2197 int err;
2198 if (ns == NULL) {
2199 PyErr_Format(PyExc_SystemError,
2200 "no locals when deleting %R", name);
2201 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002202 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002203 err = PyObject_DelItem(ns, name);
2204 if (err != 0) {
2205 format_exc_check_arg(PyExc_NameError,
2206 NAME_ERROR_MSG,
2207 name);
2208 goto error;
2209 }
2210 DISPATCH();
2211 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002212
Benjamin Petersonddd19492018-09-16 22:38:02 -07002213 case TARGET(UNPACK_SEQUENCE): {
2214 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002215 PyObject *seq = POP(), *item, **items;
2216 if (PyTuple_CheckExact(seq) &&
2217 PyTuple_GET_SIZE(seq) == oparg) {
2218 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002220 item = items[oparg];
2221 Py_INCREF(item);
2222 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002223 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002224 } else if (PyList_CheckExact(seq) &&
2225 PyList_GET_SIZE(seq) == oparg) {
2226 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002227 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002228 item = items[oparg];
2229 Py_INCREF(item);
2230 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002231 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002232 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002233 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002234 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 } else {
2236 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002237 Py_DECREF(seq);
2238 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002239 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002240 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002241 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002242 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002243
Benjamin Petersonddd19492018-09-16 22:38:02 -07002244 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002245 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2246 PyObject *seq = POP();
2247
2248 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2249 stack_pointer + totalargs)) {
2250 stack_pointer += totalargs;
2251 } else {
2252 Py_DECREF(seq);
2253 goto error;
2254 }
2255 Py_DECREF(seq);
2256 DISPATCH();
2257 }
2258
Benjamin Petersonddd19492018-09-16 22:38:02 -07002259 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002260 PyObject *name = GETITEM(names, oparg);
2261 PyObject *owner = TOP();
2262 PyObject *v = SECOND();
2263 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002264 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002265 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002266 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002267 Py_DECREF(owner);
2268 if (err != 0)
2269 goto error;
2270 DISPATCH();
2271 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002272
Benjamin Petersonddd19492018-09-16 22:38:02 -07002273 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002274 PyObject *name = GETITEM(names, oparg);
2275 PyObject *owner = POP();
2276 int err;
2277 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2278 Py_DECREF(owner);
2279 if (err != 0)
2280 goto error;
2281 DISPATCH();
2282 }
2283
Benjamin Petersonddd19492018-09-16 22:38:02 -07002284 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002285 PyObject *name = GETITEM(names, oparg);
2286 PyObject *v = POP();
2287 int err;
2288 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002289 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002290 if (err != 0)
2291 goto error;
2292 DISPATCH();
2293 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002294
Benjamin Petersonddd19492018-09-16 22:38:02 -07002295 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002296 PyObject *name = GETITEM(names, oparg);
2297 int err;
2298 err = PyDict_DelItem(f->f_globals, name);
2299 if (err != 0) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002300 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2301 format_exc_check_arg(
2302 PyExc_NameError, NAME_ERROR_MSG, name);
2303 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002304 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002305 }
2306 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002307 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002308
Benjamin Petersonddd19492018-09-16 22:38:02 -07002309 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002310 PyObject *name = GETITEM(names, oparg);
2311 PyObject *locals = f->f_locals;
2312 PyObject *v;
2313 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002314 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002315 "no locals when loading %R", name);
2316 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002318 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002319 v = PyDict_GetItemWithError(locals, name);
2320 if (v != NULL) {
2321 Py_INCREF(v);
2322 }
2323 else if (PyErr_Occurred()) {
2324 goto error;
2325 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326 }
2327 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002328 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002329 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002330 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2331 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332 PyErr_Clear();
2333 }
2334 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002335 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002336 v = PyDict_GetItemWithError(f->f_globals, name);
2337 if (v != NULL) {
2338 Py_INCREF(v);
2339 }
2340 else if (PyErr_Occurred()) {
2341 goto error;
2342 }
2343 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002344 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002345 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002346 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002347 if (!PyErr_Occurred()) {
2348 format_exc_check_arg(
Victor Stinnerb0b22422012-04-19 00:57:45 +02002349 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002350 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002351 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002352 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002353 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002354 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002355 }
2356 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002357 v = PyObject_GetItem(f->f_builtins, name);
2358 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002359 if (PyErr_ExceptionMatches(PyExc_KeyError))
2360 format_exc_check_arg(
2361 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002362 NAME_ERROR_MSG, name);
2363 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002364 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002365 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002368 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002370 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002371
Benjamin Petersonddd19492018-09-16 22:38:02 -07002372 case TARGET(LOAD_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002373 PyObject *name = GETITEM(names, oparg);
2374 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002375 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002376 && PyDict_CheckExact(f->f_builtins))
2377 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002378 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002379 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002380 name);
2381 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002382 if (!_PyErr_OCCURRED()) {
2383 /* _PyDict_LoadGlobal() returns NULL without raising
2384 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002385 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002386 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002387 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002388 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002390 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002392 else {
2393 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002394
2395 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002396 v = PyObject_GetItem(f->f_globals, name);
2397 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002398 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2399 goto error;
2400 PyErr_Clear();
2401
Victor Stinnerb4efc962015-11-20 09:24:02 +01002402 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002403 v = PyObject_GetItem(f->f_builtins, name);
2404 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002405 if (PyErr_ExceptionMatches(PyExc_KeyError))
2406 format_exc_check_arg(
2407 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002408 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002409 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002410 }
2411 }
2412 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002413 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002414 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002415 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002416
Benjamin Petersonddd19492018-09-16 22:38:02 -07002417 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002418 PyObject *v = GETLOCAL(oparg);
2419 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 SETLOCAL(oparg, NULL);
2421 DISPATCH();
2422 }
2423 format_exc_check_arg(
2424 PyExc_UnboundLocalError,
2425 UNBOUNDLOCAL_ERROR_MSG,
2426 PyTuple_GetItem(co->co_varnames, oparg)
2427 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002428 goto error;
2429 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002430
Benjamin Petersonddd19492018-09-16 22:38:02 -07002431 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002432 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002433 PyObject *oldobj = PyCell_GET(cell);
2434 if (oldobj != NULL) {
2435 PyCell_SET(cell, NULL);
2436 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002437 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002438 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002439 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002440 goto error;
2441 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002442
Benjamin Petersonddd19492018-09-16 22:38:02 -07002443 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002444 PyObject *cell = freevars[oparg];
2445 Py_INCREF(cell);
2446 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002447 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002448 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002449
Benjamin Petersonddd19492018-09-16 22:38:02 -07002450 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002451 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002452 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002453 assert(locals);
2454 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2455 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2456 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2457 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2458 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002459 value = PyDict_GetItemWithError(locals, name);
2460 if (value != NULL) {
2461 Py_INCREF(value);
2462 }
2463 else if (PyErr_Occurred()) {
2464 goto error;
2465 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002466 }
2467 else {
2468 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002469 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002470 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2471 goto error;
2472 PyErr_Clear();
2473 }
2474 }
2475 if (!value) {
2476 PyObject *cell = freevars[oparg];
2477 value = PyCell_GET(cell);
2478 if (value == NULL) {
2479 format_exc_unbound(co, oparg);
2480 goto error;
2481 }
2482 Py_INCREF(value);
2483 }
2484 PUSH(value);
2485 DISPATCH();
2486 }
2487
Benjamin Petersonddd19492018-09-16 22:38:02 -07002488 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002489 PyObject *cell = freevars[oparg];
2490 PyObject *value = PyCell_GET(cell);
2491 if (value == NULL) {
2492 format_exc_unbound(co, oparg);
2493 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002495 Py_INCREF(value);
2496 PUSH(value);
2497 DISPATCH();
2498 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002499
Benjamin Petersonddd19492018-09-16 22:38:02 -07002500 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002501 PyObject *v = POP();
2502 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002503 PyObject *oldobj = PyCell_GET(cell);
2504 PyCell_SET(cell, v);
2505 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002506 DISPATCH();
2507 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002508
Benjamin Petersonddd19492018-09-16 22:38:02 -07002509 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002510 PyObject *str;
2511 PyObject *empty = PyUnicode_New(0, 0);
2512 if (empty == NULL) {
2513 goto error;
2514 }
2515 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2516 Py_DECREF(empty);
2517 if (str == NULL)
2518 goto error;
2519 while (--oparg >= 0) {
2520 PyObject *item = POP();
2521 Py_DECREF(item);
2522 }
2523 PUSH(str);
2524 DISPATCH();
2525 }
2526
Benjamin Petersonddd19492018-09-16 22:38:02 -07002527 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002528 PyObject *tup = PyTuple_New(oparg);
2529 if (tup == NULL)
2530 goto error;
2531 while (--oparg >= 0) {
2532 PyObject *item = POP();
2533 PyTuple_SET_ITEM(tup, oparg, item);
2534 }
2535 PUSH(tup);
2536 DISPATCH();
2537 }
2538
Benjamin Petersonddd19492018-09-16 22:38:02 -07002539 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002540 PyObject *list = PyList_New(oparg);
2541 if (list == NULL)
2542 goto error;
2543 while (--oparg >= 0) {
2544 PyObject *item = POP();
2545 PyList_SET_ITEM(list, oparg, item);
2546 }
2547 PUSH(list);
2548 DISPATCH();
2549 }
2550
Benjamin Petersonddd19492018-09-16 22:38:02 -07002551 case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL):
2552 case TARGET(BUILD_TUPLE_UNPACK):
2553 case TARGET(BUILD_LIST_UNPACK): {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002554 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002555 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002556 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002557 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002558
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002559 if (sum == NULL)
2560 goto error;
2561
2562 for (i = oparg; i > 0; i--) {
2563 PyObject *none_val;
2564
2565 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2566 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002567 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002568 PyErr_ExceptionMatches(PyExc_TypeError))
2569 {
2570 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002571 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002572 Py_DECREF(sum);
2573 goto error;
2574 }
2575 Py_DECREF(none_val);
2576 }
2577
2578 if (convert_to_tuple) {
2579 return_value = PyList_AsTuple(sum);
2580 Py_DECREF(sum);
2581 if (return_value == NULL)
2582 goto error;
2583 }
2584 else {
2585 return_value = sum;
2586 }
2587
2588 while (oparg--)
2589 Py_DECREF(POP());
2590 PUSH(return_value);
2591 DISPATCH();
2592 }
2593
Benjamin Petersonddd19492018-09-16 22:38:02 -07002594 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002595 PyObject *set = PySet_New(NULL);
2596 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002597 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002598 if (set == NULL)
2599 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002600 for (i = oparg; i > 0; i--) {
2601 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002602 if (err == 0)
2603 err = PySet_Add(set, item);
2604 Py_DECREF(item);
2605 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002606 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002607 if (err != 0) {
2608 Py_DECREF(set);
2609 goto error;
2610 }
2611 PUSH(set);
2612 DISPATCH();
2613 }
2614
Benjamin Petersonddd19492018-09-16 22:38:02 -07002615 case TARGET(BUILD_SET_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002616 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002617 PyObject *sum = PySet_New(NULL);
2618 if (sum == NULL)
2619 goto error;
2620
2621 for (i = oparg; i > 0; i--) {
2622 if (_PySet_Update(sum, PEEK(i)) < 0) {
2623 Py_DECREF(sum);
2624 goto error;
2625 }
2626 }
2627
2628 while (oparg--)
2629 Py_DECREF(POP());
2630 PUSH(sum);
2631 DISPATCH();
2632 }
2633
Benjamin Petersonddd19492018-09-16 22:38:02 -07002634 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002635 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002636 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2637 if (map == NULL)
2638 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002639 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002640 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002641 PyObject *key = PEEK(2*i);
2642 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002643 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002644 if (err != 0) {
2645 Py_DECREF(map);
2646 goto error;
2647 }
2648 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002649
2650 while (oparg--) {
2651 Py_DECREF(POP());
2652 Py_DECREF(POP());
2653 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002654 PUSH(map);
2655 DISPATCH();
2656 }
2657
Benjamin Petersonddd19492018-09-16 22:38:02 -07002658 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002659 _Py_IDENTIFIER(__annotations__);
2660 int err;
2661 PyObject *ann_dict;
2662 if (f->f_locals == NULL) {
2663 PyErr_Format(PyExc_SystemError,
2664 "no locals found when setting up annotations");
2665 goto error;
2666 }
2667 /* check if __annotations__ in locals()... */
2668 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002669 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002670 &PyId___annotations__);
2671 if (ann_dict == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002672 if (PyErr_Occurred()) {
2673 goto error;
2674 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002675 /* ...if not, create a new one */
2676 ann_dict = PyDict_New();
2677 if (ann_dict == NULL) {
2678 goto error;
2679 }
2680 err = _PyDict_SetItemId(f->f_locals,
2681 &PyId___annotations__, ann_dict);
2682 Py_DECREF(ann_dict);
2683 if (err != 0) {
2684 goto error;
2685 }
2686 }
2687 }
2688 else {
2689 /* do the same if locals() is not a dict */
2690 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2691 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002692 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002693 }
2694 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2695 if (ann_dict == NULL) {
2696 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2697 goto error;
2698 }
2699 PyErr_Clear();
2700 ann_dict = PyDict_New();
2701 if (ann_dict == NULL) {
2702 goto error;
2703 }
2704 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2705 Py_DECREF(ann_dict);
2706 if (err != 0) {
2707 goto error;
2708 }
2709 }
2710 else {
2711 Py_DECREF(ann_dict);
2712 }
2713 }
2714 DISPATCH();
2715 }
2716
Benjamin Petersonddd19492018-09-16 22:38:02 -07002717 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002718 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002719 PyObject *map;
2720 PyObject *keys = TOP();
2721 if (!PyTuple_CheckExact(keys) ||
2722 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2723 PyErr_SetString(PyExc_SystemError,
2724 "bad BUILD_CONST_KEY_MAP keys argument");
2725 goto error;
2726 }
2727 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2728 if (map == NULL) {
2729 goto error;
2730 }
2731 for (i = oparg; i > 0; i--) {
2732 int err;
2733 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2734 PyObject *value = PEEK(i + 1);
2735 err = PyDict_SetItem(map, key, value);
2736 if (err != 0) {
2737 Py_DECREF(map);
2738 goto error;
2739 }
2740 }
2741
2742 Py_DECREF(POP());
2743 while (oparg--) {
2744 Py_DECREF(POP());
2745 }
2746 PUSH(map);
2747 DISPATCH();
2748 }
2749
Benjamin Petersonddd19492018-09-16 22:38:02 -07002750 case TARGET(BUILD_MAP_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002751 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002752 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002753 if (sum == NULL)
2754 goto error;
2755
2756 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002757 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002758 if (PyDict_Update(sum, arg) < 0) {
2759 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2760 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002761 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002762 arg->ob_type->tp_name);
2763 }
2764 Py_DECREF(sum);
2765 goto error;
2766 }
2767 }
2768
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002769 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002770 Py_DECREF(POP());
2771 PUSH(sum);
2772 DISPATCH();
2773 }
2774
Benjamin Petersonddd19492018-09-16 22:38:02 -07002775 case TARGET(BUILD_MAP_UNPACK_WITH_CALL): {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002776 Py_ssize_t i;
2777 PyObject *sum = PyDict_New();
2778 if (sum == NULL)
2779 goto error;
2780
2781 for (i = oparg; i > 0; i--) {
2782 PyObject *arg = PEEK(i);
2783 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002784 Py_DECREF(sum);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02002785 format_kwargs_error(PEEK(2 + oparg), arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002786 goto error;
2787 }
2788 }
2789
2790 while (oparg--)
2791 Py_DECREF(POP());
2792 PUSH(sum);
2793 DISPATCH();
2794 }
2795
Benjamin Petersonddd19492018-09-16 22:38:02 -07002796 case TARGET(MAP_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002797 PyObject *key = TOP();
2798 PyObject *value = SECOND();
2799 PyObject *map;
2800 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002801 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002802 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002803 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002804 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002805 Py_DECREF(value);
2806 Py_DECREF(key);
2807 if (err != 0)
2808 goto error;
2809 PREDICT(JUMP_ABSOLUTE);
2810 DISPATCH();
2811 }
2812
Benjamin Petersonddd19492018-09-16 22:38:02 -07002813 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002814 PyObject *name = GETITEM(names, oparg);
2815 PyObject *owner = TOP();
2816 PyObject *res = PyObject_GetAttr(owner, name);
2817 Py_DECREF(owner);
2818 SET_TOP(res);
2819 if (res == NULL)
2820 goto error;
2821 DISPATCH();
2822 }
2823
Benjamin Petersonddd19492018-09-16 22:38:02 -07002824 case TARGET(COMPARE_OP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002825 PyObject *right = POP();
2826 PyObject *left = TOP();
2827 PyObject *res = cmp_outcome(oparg, left, right);
2828 Py_DECREF(left);
2829 Py_DECREF(right);
2830 SET_TOP(res);
2831 if (res == NULL)
2832 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002833 PREDICT(POP_JUMP_IF_FALSE);
2834 PREDICT(POP_JUMP_IF_TRUE);
2835 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002836 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002837
Benjamin Petersonddd19492018-09-16 22:38:02 -07002838 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002839 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002840 PyObject *fromlist = POP();
2841 PyObject *level = TOP();
2842 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002843 res = import_name(f, name, fromlist, level);
2844 Py_DECREF(level);
2845 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002846 SET_TOP(res);
2847 if (res == NULL)
2848 goto error;
2849 DISPATCH();
2850 }
2851
Benjamin Petersonddd19492018-09-16 22:38:02 -07002852 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002853 PyObject *from = POP(), *locals;
2854 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002855 if (PyFrame_FastToLocalsWithError(f) < 0) {
2856 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002857 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002858 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002859
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002860 locals = f->f_locals;
2861 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002862 PyErr_SetString(PyExc_SystemError,
2863 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002864 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002865 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002866 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002867 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002869 Py_DECREF(from);
2870 if (err != 0)
2871 goto error;
2872 DISPATCH();
2873 }
Guido van Rossum25831651993-05-19 14:50:45 +00002874
Benjamin Petersonddd19492018-09-16 22:38:02 -07002875 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002876 PyObject *name = GETITEM(names, oparg);
2877 PyObject *from = TOP();
2878 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002879 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002880 PUSH(res);
2881 if (res == NULL)
2882 goto error;
2883 DISPATCH();
2884 }
Thomas Wouters52152252000-08-17 22:55:00 +00002885
Benjamin Petersonddd19492018-09-16 22:38:02 -07002886 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002887 JUMPBY(oparg);
2888 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002889 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002890
Benjamin Petersonddd19492018-09-16 22:38:02 -07002891 case TARGET(POP_JUMP_IF_FALSE): {
2892 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002893 PyObject *cond = POP();
2894 int err;
2895 if (cond == Py_True) {
2896 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002897 FAST_DISPATCH();
2898 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002899 if (cond == Py_False) {
2900 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 JUMPTO(oparg);
2902 FAST_DISPATCH();
2903 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002904 err = PyObject_IsTrue(cond);
2905 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002906 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002907 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 else if (err == 0)
2909 JUMPTO(oparg);
2910 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002911 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002912 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002913 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002914
Benjamin Petersonddd19492018-09-16 22:38:02 -07002915 case TARGET(POP_JUMP_IF_TRUE): {
2916 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002917 PyObject *cond = POP();
2918 int err;
2919 if (cond == Py_False) {
2920 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002921 FAST_DISPATCH();
2922 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002923 if (cond == Py_True) {
2924 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 JUMPTO(oparg);
2926 FAST_DISPATCH();
2927 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002928 err = PyObject_IsTrue(cond);
2929 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002930 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002931 JUMPTO(oparg);
2932 }
2933 else if (err == 0)
2934 ;
2935 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002936 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002937 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002938 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002939
Benjamin Petersonddd19492018-09-16 22:38:02 -07002940 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002941 PyObject *cond = TOP();
2942 int err;
2943 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002944 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002945 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 FAST_DISPATCH();
2947 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002948 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002949 JUMPTO(oparg);
2950 FAST_DISPATCH();
2951 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002952 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002953 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002954 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002955 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002956 }
2957 else if (err == 0)
2958 JUMPTO(oparg);
2959 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002960 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002961 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002962 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002963
Benjamin Petersonddd19492018-09-16 22:38:02 -07002964 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002965 PyObject *cond = TOP();
2966 int err;
2967 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002968 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002969 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002970 FAST_DISPATCH();
2971 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002972 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002973 JUMPTO(oparg);
2974 FAST_DISPATCH();
2975 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002976 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002977 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002978 JUMPTO(oparg);
2979 }
2980 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002981 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002982 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002983 }
2984 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002985 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002986 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002987 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002988
Benjamin Petersonddd19492018-09-16 22:38:02 -07002989 case TARGET(JUMP_ABSOLUTE): {
2990 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002991 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002992#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002993 /* Enabling this path speeds-up all while and for-loops by bypassing
2994 the per-loop checks for signals. By default, this should be turned-off
2995 because it prevents detection of a control-break in tight loops like
2996 "while 1: pass". Compile with this option turned-on when you need
2997 the speed-up and do not need break checking inside tight loops (ones
2998 that contain only instructions ending with FAST_DISPATCH).
2999 */
3000 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003001#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003002 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003003#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003004 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003005
Benjamin Petersonddd19492018-09-16 22:38:02 -07003006 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003007 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003008 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003009 PyObject *iter = PyObject_GetIter(iterable);
3010 Py_DECREF(iterable);
3011 SET_TOP(iter);
3012 if (iter == NULL)
3013 goto error;
3014 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003015 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003016 DISPATCH();
3017 }
3018
Benjamin Petersonddd19492018-09-16 22:38:02 -07003019 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003020 /* before: [obj]; after [getiter(obj)] */
3021 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003022 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003023 if (PyCoro_CheckExact(iterable)) {
3024 /* `iterable` is a coroutine */
3025 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3026 /* and it is used in a 'yield from' expression of a
3027 regular generator. */
3028 Py_DECREF(iterable);
3029 SET_TOP(NULL);
3030 PyErr_SetString(PyExc_TypeError,
3031 "cannot 'yield from' a coroutine object "
3032 "in a non-coroutine generator");
3033 goto error;
3034 }
3035 }
3036 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003037 /* `iterable` is not a generator. */
3038 iter = PyObject_GetIter(iterable);
3039 Py_DECREF(iterable);
3040 SET_TOP(iter);
3041 if (iter == NULL)
3042 goto error;
3043 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003044 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003045 DISPATCH();
3046 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003047
Benjamin Petersonddd19492018-09-16 22:38:02 -07003048 case TARGET(FOR_ITER): {
3049 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003050 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003051 PyObject *iter = TOP();
3052 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3053 if (next != NULL) {
3054 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 PREDICT(STORE_FAST);
3056 PREDICT(UNPACK_SEQUENCE);
3057 DISPATCH();
3058 }
3059 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003060 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
3061 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08003062 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003063 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003064 PyErr_Clear();
3065 }
3066 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003067 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003068 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003069 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003070 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003071 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003072 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003073
Benjamin Petersonddd19492018-09-16 22:38:02 -07003074 case TARGET(SETUP_FINALLY): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003075 /* NOTE: If you add any new block-setup opcodes that
3076 are not try/except/finally handlers, you may need
3077 to update the PyGen_NeedsFinalizing() function.
3078 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003079
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003080 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003081 STACK_LEVEL());
3082 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003083 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003084
Benjamin Petersonddd19492018-09-16 22:38:02 -07003085 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003086 _Py_IDENTIFIER(__aexit__);
3087 _Py_IDENTIFIER(__aenter__);
3088
3089 PyObject *mgr = TOP();
3090 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3091 *enter;
3092 PyObject *res;
3093 if (exit == NULL)
3094 goto error;
3095 SET_TOP(exit);
3096 enter = special_lookup(mgr, &PyId___aenter__);
3097 Py_DECREF(mgr);
3098 if (enter == NULL)
3099 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003100 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003101 Py_DECREF(enter);
3102 if (res == NULL)
3103 goto error;
3104 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003105 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003106 DISPATCH();
3107 }
3108
Benjamin Petersonddd19492018-09-16 22:38:02 -07003109 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003110 PyObject *res = POP();
3111 /* Setup the finally block before pushing the result
3112 of __aenter__ on the stack. */
3113 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3114 STACK_LEVEL());
3115 PUSH(res);
3116 DISPATCH();
3117 }
3118
Benjamin Petersonddd19492018-09-16 22:38:02 -07003119 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003120 _Py_IDENTIFIER(__exit__);
3121 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003122 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08003123 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003124 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08003125 if (enter == NULL)
3126 goto error;
3127 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003128 if (exit == NULL) {
3129 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003130 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003131 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003132 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003133 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003134 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003135 Py_DECREF(enter);
3136 if (res == NULL)
3137 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003138 /* Setup the finally block before pushing the result
3139 of __enter__ on the stack. */
3140 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3141 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003142
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003143 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003144 DISPATCH();
3145 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003146
Benjamin Petersonddd19492018-09-16 22:38:02 -07003147 case TARGET(WITH_CLEANUP_START): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003148 /* At the top of the stack are 1 or 6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003149 how/why we entered the finally clause:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003150 - TOP = NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003151 - (TOP, SECOND, THIRD) = exc_info()
3152 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003153 Below them is EXIT, the context.__exit__ or context.__aexit__
3154 bound method.
3155 In the first case, we must call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003156 EXIT(None, None, None)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003157 otherwise we must call
3158 EXIT(TOP, SECOND, THIRD)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003159
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003160 In the first case, we remove EXIT from the
3161 stack, leaving TOP, and push TOP on the stack.
3162 Otherwise we shift the bottom 3 values of the
3163 stack down, replace the empty spot with NULL, and push
3164 None on the stack.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003165
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003166 Finally we push the result of the call.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003167 */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003168 PyObject *stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003169 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003170 PyObject *exc, *val, *tb, *res;
3171
3172 val = tb = Py_None;
3173 exc = TOP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003174 if (exc == NULL) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003175 STACK_SHRINK(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003176 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003177 SET_TOP(exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003178 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003179 }
3180 else {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003181 assert(PyExceptionClass_Check(exc));
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003182 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003183 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003184 val = SECOND();
3185 tb = THIRD();
3186 tp2 = FOURTH();
3187 exc2 = PEEK(5);
3188 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003189 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003190 SET_VALUE(7, tb2);
3191 SET_VALUE(6, exc2);
3192 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003193 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3194 SET_FOURTH(NULL);
3195 /* We just shifted the stack down, so we have
3196 to tell the except handler block that the
3197 values are lower than it expects. */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003198 assert(f->f_iblock > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003199 block = &f->f_blockstack[f->f_iblock - 1];
3200 assert(block->b_type == EXCEPT_HANDLER);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003201 assert(block->b_level > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003202 block->b_level--;
3203 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003204
3205 stack[0] = exc;
3206 stack[1] = val;
3207 stack[2] = tb;
3208 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003209 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003210 if (res == NULL)
3211 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003212
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003213 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003214 PUSH(exc);
3215 PUSH(res);
3216 PREDICT(WITH_CLEANUP_FINISH);
3217 DISPATCH();
3218 }
3219
Benjamin Petersonddd19492018-09-16 22:38:02 -07003220 case TARGET(WITH_CLEANUP_FINISH): {
3221 PREDICTED(WITH_CLEANUP_FINISH);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003222 /* TOP = the result of calling the context.__exit__ bound method
3223 SECOND = either None or exception type
3224
3225 If SECOND is None below is NULL or the return address,
3226 otherwise below are 7 values representing an exception.
3227 */
Yury Selivanov75445082015-05-11 22:57:16 -04003228 PyObject *res = POP();
3229 PyObject *exc = POP();
3230 int err;
3231
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003232 if (exc != Py_None)
3233 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003234 else
3235 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003236
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003237 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003238 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003240 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003241 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003242 else if (err > 0) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003243 /* There was an exception and a True return.
3244 * We must manually unwind the EXCEPT_HANDLER block
3245 * which was created when the exception was caught,
Quan Tian3bd0d622018-10-20 05:30:03 +08003246 * otherwise the stack will be in an inconsistent state.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003247 */
3248 PyTryBlock *b = PyFrame_BlockPop(f);
3249 assert(b->b_type == EXCEPT_HANDLER);
3250 UNWIND_EXCEPT_HANDLER(b);
3251 PUSH(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003252 }
3253 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003254 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003255 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003256
Benjamin Petersonddd19492018-09-16 22:38:02 -07003257 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003258 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003259 PyObject *name = GETITEM(names, oparg);
3260 PyObject *obj = TOP();
3261 PyObject *meth = NULL;
3262
3263 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3264
Yury Selivanovf2392132016-12-13 19:03:51 -05003265 if (meth == NULL) {
3266 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003267 goto error;
3268 }
3269
3270 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003271 /* We can bypass temporary bound method object.
3272 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003273
INADA Naoki015bce62017-01-16 17:23:30 +09003274 meth | self | arg1 | ... | argN
3275 */
3276 SET_TOP(meth);
3277 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003278 }
3279 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003280 /* meth is not an unbound method (but a regular attr, or
3281 something was returned by a descriptor protocol). Set
3282 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003283 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003284
3285 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003286 */
INADA Naoki015bce62017-01-16 17:23:30 +09003287 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003288 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003289 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003290 }
3291 DISPATCH();
3292 }
3293
Benjamin Petersonddd19492018-09-16 22:38:02 -07003294 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003295 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003296 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003297
3298 sp = stack_pointer;
3299
INADA Naoki015bce62017-01-16 17:23:30 +09003300 meth = PEEK(oparg + 2);
3301 if (meth == NULL) {
3302 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3303 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003304
3305 Stack layout:
3306
INADA Naoki015bce62017-01-16 17:23:30 +09003307 ... | NULL | callable | arg1 | ... | argN
3308 ^- TOP()
3309 ^- (-oparg)
3310 ^- (-oparg-1)
3311 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003312
Ville Skyttä49b27342017-08-03 09:00:59 +03003313 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003314 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003315 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003316 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003317 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003318 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003319 }
3320 else {
3321 /* This is a method call. Stack layout:
3322
INADA Naoki015bce62017-01-16 17:23:30 +09003323 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003324 ^- TOP()
3325 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003326 ^- (-oparg-1)
3327 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003328
INADA Naoki015bce62017-01-16 17:23:30 +09003329 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003330 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003331 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003332 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003333 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003334 stack_pointer = sp;
3335 }
3336
3337 PUSH(res);
3338 if (res == NULL)
3339 goto error;
3340 DISPATCH();
3341 }
3342
Benjamin Petersonddd19492018-09-16 22:38:02 -07003343 case TARGET(CALL_FUNCTION): {
3344 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003345 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003346 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003347 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003348 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003349 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003350 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003351 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003352 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003353 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003354 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003355
Benjamin Petersonddd19492018-09-16 22:38:02 -07003356 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003357 PyObject **sp, *res, *names;
3358
3359 names = POP();
3360 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003361 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003362 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003363 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003364 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003365 Py_DECREF(names);
3366
3367 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003368 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003369 }
3370 DISPATCH();
3371 }
3372
Benjamin Petersonddd19492018-09-16 22:38:02 -07003373 case TARGET(CALL_FUNCTION_EX): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003374 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003375 if (oparg & 0x01) {
3376 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003377 if (!PyDict_CheckExact(kwargs)) {
3378 PyObject *d = PyDict_New();
3379 if (d == NULL)
3380 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003381 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003382 Py_DECREF(d);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003383 format_kwargs_error(SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003384 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003385 goto error;
3386 }
3387 Py_DECREF(kwargs);
3388 kwargs = d;
3389 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003390 assert(PyDict_CheckExact(kwargs));
3391 }
3392 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003393 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003394 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003395 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003396 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003397 goto error;
3398 }
3399 Py_SETREF(callargs, PySequence_Tuple(callargs));
3400 if (callargs == NULL) {
3401 goto error;
3402 }
3403 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003404 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003405
Victor Stinner09532fe2019-05-10 23:39:09 +02003406 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003407 Py_DECREF(func);
3408 Py_DECREF(callargs);
3409 Py_XDECREF(kwargs);
3410
3411 SET_TOP(result);
3412 if (result == NULL) {
3413 goto error;
3414 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003415 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003416 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003417
Benjamin Petersonddd19492018-09-16 22:38:02 -07003418 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003419 PyObject *qualname = POP();
3420 PyObject *codeobj = POP();
3421 PyFunctionObject *func = (PyFunctionObject *)
3422 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003423
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003424 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003425 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003426 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003427 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003428 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003429
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003430 if (oparg & 0x08) {
3431 assert(PyTuple_CheckExact(TOP()));
3432 func ->func_closure = POP();
3433 }
3434 if (oparg & 0x04) {
3435 assert(PyDict_CheckExact(TOP()));
3436 func->func_annotations = POP();
3437 }
3438 if (oparg & 0x02) {
3439 assert(PyDict_CheckExact(TOP()));
3440 func->func_kwdefaults = POP();
3441 }
3442 if (oparg & 0x01) {
3443 assert(PyTuple_CheckExact(TOP()));
3444 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003445 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003446
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003447 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003448 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003449 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003450
Benjamin Petersonddd19492018-09-16 22:38:02 -07003451 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003452 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003453 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003454 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003455 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003456 step = NULL;
3457 stop = POP();
3458 start = TOP();
3459 slice = PySlice_New(start, stop, step);
3460 Py_DECREF(start);
3461 Py_DECREF(stop);
3462 Py_XDECREF(step);
3463 SET_TOP(slice);
3464 if (slice == NULL)
3465 goto error;
3466 DISPATCH();
3467 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003468
Benjamin Petersonddd19492018-09-16 22:38:02 -07003469 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003470 /* Handles f-string value formatting. */
3471 PyObject *result;
3472 PyObject *fmt_spec;
3473 PyObject *value;
3474 PyObject *(*conv_fn)(PyObject *);
3475 int which_conversion = oparg & FVC_MASK;
3476 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3477
3478 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003479 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003480
3481 /* See if any conversion is specified. */
3482 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003483 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003484 case FVC_STR: conv_fn = PyObject_Str; break;
3485 case FVC_REPR: conv_fn = PyObject_Repr; break;
3486 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003487 default:
3488 PyErr_Format(PyExc_SystemError,
3489 "unexpected conversion flag %d",
3490 which_conversion);
3491 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003492 }
3493
3494 /* If there's a conversion function, call it and replace
3495 value with that result. Otherwise, just use value,
3496 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003497 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003498 result = conv_fn(value);
3499 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003500 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003501 Py_XDECREF(fmt_spec);
3502 goto error;
3503 }
3504 value = result;
3505 }
3506
3507 /* If value is a unicode object, and there's no fmt_spec,
3508 then we know the result of format(value) is value
3509 itself. In that case, skip calling format(). I plan to
3510 move this optimization in to PyObject_Format()
3511 itself. */
3512 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3513 /* Do nothing, just transfer ownership to result. */
3514 result = value;
3515 } else {
3516 /* Actually call format(). */
3517 result = PyObject_Format(value, fmt_spec);
3518 Py_DECREF(value);
3519 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003520 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003521 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003522 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003523 }
3524
Eric V. Smith135d5f42016-02-05 18:23:08 -05003525 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003526 DISPATCH();
3527 }
3528
Benjamin Petersonddd19492018-09-16 22:38:02 -07003529 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003530 int oldoparg = oparg;
3531 NEXTOPARG();
3532 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003533 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003534 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003535
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003536
Antoine Pitrou042b1282010-08-13 21:15:58 +00003537#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003538 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003539#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003540 default:
3541 fprintf(stderr,
3542 "XXX lineno: %d, opcode: %d\n",
3543 PyFrame_GetLineNumber(f),
3544 opcode);
3545 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003546 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003548 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003549
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003550 /* This should never be reached. Every opcode should end with DISPATCH()
3551 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003552 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003553
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003554error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003555 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003556#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003557 if (!PyErr_Occurred())
3558 PyErr_SetString(PyExc_SystemError,
3559 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003560#else
3561 assert(PyErr_Occurred());
3562#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003563
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003564 /* Log traceback info. */
3565 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003566
Benjamin Peterson51f46162013-01-23 08:38:47 -05003567 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003568 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3569 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003570
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003571exception_unwind:
3572 /* Unwind stacks if an exception occurred */
3573 while (f->f_iblock > 0) {
3574 /* Pop the current block. */
3575 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003577 if (b->b_type == EXCEPT_HANDLER) {
3578 UNWIND_EXCEPT_HANDLER(b);
3579 continue;
3580 }
3581 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003582 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003583 PyObject *exc, *val, *tb;
3584 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003585 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003586 /* Beware, this invalidates all b->b_* fields */
3587 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003588 PUSH(exc_info->exc_traceback);
3589 PUSH(exc_info->exc_value);
3590 if (exc_info->exc_type != NULL) {
3591 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003592 }
3593 else {
3594 Py_INCREF(Py_None);
3595 PUSH(Py_None);
3596 }
3597 PyErr_Fetch(&exc, &val, &tb);
3598 /* Make the raw exception data
3599 available to the handler,
3600 so a program can emulate the
3601 Python main loop. */
3602 PyErr_NormalizeException(
3603 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003604 if (tb != NULL)
3605 PyException_SetTraceback(val, tb);
3606 else
3607 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003608 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003609 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003610 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003611 exc_info->exc_value = val;
3612 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003613 if (tb == NULL)
3614 tb = Py_None;
3615 Py_INCREF(tb);
3616 PUSH(tb);
3617 PUSH(val);
3618 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003619 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003620 /* Resume normal execution */
3621 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003622 }
3623 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003624
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003625 /* End the loop as we still have an error */
3626 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003627 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003628
Pablo Galindof00828a2019-05-09 16:52:02 +01003629 assert(retval == NULL);
3630 assert(PyErr_Occurred());
3631
3632exit_returning:
3633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003634 /* Pop remaining stack entries. */
3635 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003636 PyObject *o = POP();
3637 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003638 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003639
Pablo Galindof00828a2019-05-09 16:52:02 +01003640exit_yielding:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003641 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003642 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003643 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3644 tstate, f, PyTrace_RETURN, retval)) {
3645 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003646 }
3647 }
3648 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003649 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3650 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003651 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003652 }
3653 }
3654 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003656 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003657exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003658 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3659 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003660 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003661 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003662 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003663
Victor Stinnerefde1462015-03-21 15:04:43 +01003664 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003665}
3666
Benjamin Petersonb204a422011-06-05 22:04:07 -05003667static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003668format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3669{
3670 int err;
3671 Py_ssize_t len = PyList_GET_SIZE(names);
3672 PyObject *name_str, *comma, *tail, *tmp;
3673
3674 assert(PyList_CheckExact(names));
3675 assert(len >= 1);
3676 /* Deal with the joys of natural language. */
3677 switch (len) {
3678 case 1:
3679 name_str = PyList_GET_ITEM(names, 0);
3680 Py_INCREF(name_str);
3681 break;
3682 case 2:
3683 name_str = PyUnicode_FromFormat("%U and %U",
3684 PyList_GET_ITEM(names, len - 2),
3685 PyList_GET_ITEM(names, len - 1));
3686 break;
3687 default:
3688 tail = PyUnicode_FromFormat(", %U, and %U",
3689 PyList_GET_ITEM(names, len - 2),
3690 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003691 if (tail == NULL)
3692 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003693 /* Chop off the last two objects in the list. This shouldn't actually
3694 fail, but we can't be too careful. */
3695 err = PyList_SetSlice(names, len - 2, len, NULL);
3696 if (err == -1) {
3697 Py_DECREF(tail);
3698 return;
3699 }
3700 /* Stitch everything up into a nice comma-separated list. */
3701 comma = PyUnicode_FromString(", ");
3702 if (comma == NULL) {
3703 Py_DECREF(tail);
3704 return;
3705 }
3706 tmp = PyUnicode_Join(comma, names);
3707 Py_DECREF(comma);
3708 if (tmp == NULL) {
3709 Py_DECREF(tail);
3710 return;
3711 }
3712 name_str = PyUnicode_Concat(tmp, tail);
3713 Py_DECREF(tmp);
3714 Py_DECREF(tail);
3715 break;
3716 }
3717 if (name_str == NULL)
3718 return;
3719 PyErr_Format(PyExc_TypeError,
3720 "%U() missing %i required %s argument%s: %U",
3721 co->co_name,
3722 len,
3723 kind,
3724 len == 1 ? "" : "s",
3725 name_str);
3726 Py_DECREF(name_str);
3727}
3728
3729static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003730missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003731 PyObject **fastlocals)
3732{
Victor Stinner74319ae2016-08-25 00:04:09 +02003733 Py_ssize_t i, j = 0;
3734 Py_ssize_t start, end;
3735 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003736 const char *kind = positional ? "positional" : "keyword-only";
3737 PyObject *missing_names;
3738
3739 /* Compute the names of the arguments that are missing. */
3740 missing_names = PyList_New(missing);
3741 if (missing_names == NULL)
3742 return;
3743 if (positional) {
3744 start = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003745 end = co->co_posonlyargcount + co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003746 }
3747 else {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003748 start = co->co_posonlyargcount + co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003749 end = start + co->co_kwonlyargcount;
3750 }
3751 for (i = start; i < end; i++) {
3752 if (GETLOCAL(i) == NULL) {
3753 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3754 PyObject *name = PyObject_Repr(raw);
3755 if (name == NULL) {
3756 Py_DECREF(missing_names);
3757 return;
3758 }
3759 PyList_SET_ITEM(missing_names, j++, name);
3760 }
3761 }
3762 assert(j == missing);
3763 format_missing(kind, co, missing_names);
3764 Py_DECREF(missing_names);
3765}
3766
3767static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003768too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3769 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003770{
3771 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003772 Py_ssize_t kwonly_given = 0;
3773 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003774 PyObject *sig, *kwonly_sig;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003775 Py_ssize_t co_posonlyargcount = co->co_posonlyargcount;
Victor Stinner74319ae2016-08-25 00:04:09 +02003776 Py_ssize_t co_argcount = co->co_argcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003777 Py_ssize_t total_positional = co_argcount + co_posonlyargcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003778
Benjamin Petersone109c702011-06-24 09:37:26 -05003779 assert((co->co_flags & CO_VARARGS) == 0);
3780 /* Count missing keyword-only args. */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003781 for (i = total_positional; i < total_positional + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003782 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003783 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003784 }
3785 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003786 if (defcount) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003787 Py_ssize_t atleast = total_positional - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003788 plural = 1;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003789 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, total_positional);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003790 }
3791 else {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003792 plural = (total_positional != 1);
3793 sig = PyUnicode_FromFormat("%zd", total_positional);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003794 }
3795 if (sig == NULL)
3796 return;
3797 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003798 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3799 kwonly_sig = PyUnicode_FromFormat(format,
3800 given != 1 ? "s" : "",
3801 kwonly_given,
3802 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003803 if (kwonly_sig == NULL) {
3804 Py_DECREF(sig);
3805 return;
3806 }
3807 }
3808 else {
3809 /* This will not fail. */
3810 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003811 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003812 }
3813 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003814 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003815 co->co_name,
3816 sig,
3817 plural ? "s" : "",
3818 given,
3819 kwonly_sig,
3820 given == 1 && !kwonly_given ? "was" : "were");
3821 Py_DECREF(sig);
3822 Py_DECREF(kwonly_sig);
3823}
3824
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003825static int
3826positional_only_passed_as_keyword(PyCodeObject *co, Py_ssize_t kwcount,
3827 PyObject* const* kwnames)
3828{
3829 int posonly_conflicts = 0;
3830 PyObject* posonly_names = PyList_New(0);
3831
3832 for(int k=0; k < co->co_posonlyargcount; k++){
3833 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
3834
3835 for (int k2=0; k2<kwcount; k2++){
3836 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
3837 PyObject* kwname = kwnames[k2];
3838 if (kwname == posonly_name){
3839 if(PyList_Append(posonly_names, kwname) != 0) {
3840 goto fail;
3841 }
3842 posonly_conflicts++;
3843 continue;
3844 }
3845
3846 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
3847
3848 if ( cmp > 0) {
3849 if(PyList_Append(posonly_names, kwname) != 0) {
3850 goto fail;
3851 }
3852 posonly_conflicts++;
3853 } else if (cmp < 0) {
3854 goto fail;
3855 }
3856
3857 }
3858 }
3859 if (posonly_conflicts) {
3860 PyObject* comma = PyUnicode_FromString(", ");
3861 if (comma == NULL) {
3862 goto fail;
3863 }
3864 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
3865 Py_DECREF(comma);
3866 if (error_names == NULL) {
3867 goto fail;
3868 }
3869 PyErr_Format(PyExc_TypeError,
3870 "%U() got some positional-only arguments passed"
3871 " as keyword arguments: '%U'",
3872 co->co_name, error_names);
3873 Py_DECREF(error_names);
3874 goto fail;
3875 }
3876
3877 Py_DECREF(posonly_names);
3878 return 0;
3879
3880fail:
3881 Py_XDECREF(posonly_names);
3882 return 1;
3883
3884}
3885
Guido van Rossumc2e20742006-02-27 22:32:47 +00003886/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003887 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003888 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003889
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003890PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003891_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003892 PyObject *const *args, Py_ssize_t argcount,
3893 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003894 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003895 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003896 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003897 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003898{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003899 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003900 PyFrameObject *f;
3901 PyObject *retval = NULL;
3902 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003903 PyObject *x, *u;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003904 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount + co->co_posonlyargcount;
3905 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003906 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003908 if (globals == NULL) {
3909 PyErr_SetString(PyExc_SystemError,
3910 "PyEval_EvalCodeEx: NULL globals");
3911 return NULL;
3912 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003913
Victor Stinnerc7020012016-08-16 23:40:29 +02003914 /* Create the frame */
Victor Stinner09532fe2019-05-10 23:39:09 +02003915 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003916 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003917 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003918 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003919 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003920 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003921 fastlocals = f->f_localsplus;
3922 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003923
Victor Stinnerc7020012016-08-16 23:40:29 +02003924 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003925 if (co->co_flags & CO_VARKEYWORDS) {
3926 kwdict = PyDict_New();
3927 if (kwdict == NULL)
3928 goto fail;
3929 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003930 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003931 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003932 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003933 SETLOCAL(i, kwdict);
3934 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003935 else {
3936 kwdict = NULL;
3937 }
3938
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003939 /* Copy positional only arguments into local variables */
3940 if (argcount > co->co_argcount + co->co_posonlyargcount) {
3941 n = co->co_posonlyargcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003942 }
3943 else {
3944 n = argcount;
3945 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003946 for (j = 0; j < n; j++) {
3947 x = args[j];
3948 Py_INCREF(x);
3949 SETLOCAL(j, x);
3950 }
3951
3952
3953 /* Copy positional arguments into local variables */
3954 if (argcount > co->co_argcount + co->co_posonlyargcount) {
3955 n += co->co_argcount;
3956 }
3957 else {
3958 n = argcount;
3959 }
3960 for (i = j; i < n; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003961 x = args[i];
3962 Py_INCREF(x);
3963 SETLOCAL(i, x);
3964 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003965
3966 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003967 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05003968 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003969 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003970 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003971 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003972 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003973 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003974
Serhiy Storchakab7281052016-09-12 00:52:40 +03003975 /* Handle keyword arguments passed as two strided arrays */
3976 kwcount *= kwstep;
3977 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003978 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003979 PyObject *keyword = kwnames[i];
3980 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003981 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003982
Benjamin Petersonb204a422011-06-05 22:04:07 -05003983 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3984 PyErr_Format(PyExc_TypeError,
3985 "%U() keywords must be strings",
3986 co->co_name);
3987 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003988 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003989
Benjamin Petersonb204a422011-06-05 22:04:07 -05003990 /* Speed hack: do raw pointer compares. As names are
3991 normally interned this should almost always hit. */
3992 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003993 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003994 PyObject *name = co_varnames[j];
3995 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003996 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003997 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003998 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003999
Benjamin Petersonb204a422011-06-05 22:04:07 -05004000 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004001 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004002 PyObject *name = co_varnames[j];
4003 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4004 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004005 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004006 }
4007 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004008 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004009 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004010 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004011
Victor Stinner231d1f32017-01-11 02:12:06 +01004012 assert(j >= total_args);
4013 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004014
4015 if (co->co_posonlyargcount && positional_only_passed_as_keyword(co, kwcount, kwnames)) {
4016 goto fail;
4017 }
4018
Benjamin Petersonb204a422011-06-05 22:04:07 -05004019 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02004020 "%U() got an unexpected keyword argument '%S'",
4021 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004022 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004023 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004024
Christian Heimes0bd447f2013-07-20 14:48:10 +02004025 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4026 goto fail;
4027 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004028 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004029
Benjamin Petersonb204a422011-06-05 22:04:07 -05004030 kw_found:
4031 if (GETLOCAL(j) != NULL) {
4032 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02004033 "%U() got multiple values for argument '%S'",
4034 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004035 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004036 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004037 Py_INCREF(value);
4038 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004039 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004040
4041 /* Check the number of positional arguments */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004042 if ((argcount > co->co_argcount + co->co_posonlyargcount) && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004043 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004044 goto fail;
4045 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004046
4047 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004048 if (argcount < co->co_posonlyargcount + co->co_argcount) {
4049 Py_ssize_t m = co->co_posonlyargcount + co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004050 Py_ssize_t missing = 0;
4051 for (i = argcount; i < m; i++) {
4052 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004053 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004054 }
4055 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004056 if (missing) {
4057 missing_arguments(co, missing, defcount, fastlocals);
4058 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004059 }
4060 if (n > m)
4061 i = n - m;
4062 else
4063 i = 0;
4064 for (; i < defcount; i++) {
4065 if (GETLOCAL(m+i) == NULL) {
4066 PyObject *def = defs[i];
4067 Py_INCREF(def);
4068 SETLOCAL(m+i, def);
4069 }
4070 }
4071 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004072
4073 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004074 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004075 Py_ssize_t missing = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004076 for (i = co->co_posonlyargcount + co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004077 PyObject *name;
4078 if (GETLOCAL(i) != NULL)
4079 continue;
4080 name = PyTuple_GET_ITEM(co->co_varnames, i);
4081 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004082 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004083 if (def) {
4084 Py_INCREF(def);
4085 SETLOCAL(i, def);
4086 continue;
4087 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004088 else if (PyErr_Occurred()) {
4089 goto fail;
4090 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004091 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004092 missing++;
4093 }
4094 if (missing) {
4095 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004096 goto fail;
4097 }
4098 }
4099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004100 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004101 vars into frame. */
4102 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004103 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004104 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004105 /* Possibly account for the cell variable being an argument. */
4106 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004107 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004108 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004109 /* Clear the local copy. */
4110 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004111 }
4112 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004113 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004114 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004115 if (c == NULL)
4116 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004117 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004118 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004119
4120 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004121 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4122 PyObject *o = PyTuple_GET_ITEM(closure, i);
4123 Py_INCREF(o);
4124 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004125 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004126
Yury Selivanoveb636452016-09-08 22:01:51 -07004127 /* Handle generator/coroutine/asynchronous generator */
4128 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004129 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04004130 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004131 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004132
4133 if (is_coro && tstate->in_coroutine_wrapper) {
4134 assert(coro_wrapper != NULL);
4135 PyErr_Format(PyExc_RuntimeError,
4136 "coroutine wrapper %.200R attempted "
4137 "to recursively wrap %.200R",
4138 coro_wrapper,
4139 co);
4140 goto fail;
4141 }
Yury Selivanov75445082015-05-11 22:57:16 -04004142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004143 /* Don't need to keep the reference to f_back, it will be set
4144 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004145 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004147 /* Create a new generator that owns the ready to run frame
4148 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004149 if (is_coro) {
4150 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004151 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4152 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004153 } else {
4154 gen = PyGen_NewWithQualName(f, name, qualname);
4155 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004156 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004157 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004158 }
INADA Naoki9c157762016-12-26 18:52:46 +09004159
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004160 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004161
Yury Selivanov94c22632015-06-04 10:16:51 -04004162 if (is_coro && coro_wrapper != NULL) {
4163 PyObject *wrapped;
4164 tstate->in_coroutine_wrapper = 1;
4165 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4166 tstate->in_coroutine_wrapper = 0;
4167 return wrapped;
4168 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004169
Yury Selivanov75445082015-05-11 22:57:16 -04004170 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004171 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004172
Victor Stinner59a73272016-12-09 18:51:13 +01004173 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004174
Thomas Woutersce272b62007-09-19 21:19:28 +00004175fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004177 /* decref'ing the frame can cause __del__ methods to get invoked,
4178 which can call back into Python. While we're done with the
4179 current Python frame (f), the associated C stack is still in use,
4180 so recursion_depth must be boosted for the duration.
4181 */
4182 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09004183 if (Py_REFCNT(f) > 1) {
4184 Py_DECREF(f);
4185 _PyObject_GC_TRACK(f);
4186 }
4187 else {
4188 ++tstate->recursion_depth;
4189 Py_DECREF(f);
4190 --tstate->recursion_depth;
4191 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004192 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004193}
4194
Victor Stinner40ee3012014-06-16 15:59:28 +02004195PyObject *
4196PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004197 PyObject *const *args, int argcount,
4198 PyObject *const *kws, int kwcount,
4199 PyObject *const *defs, int defcount,
4200 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004201{
4202 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004203 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004204 kws, kws != NULL ? kws + 1 : NULL,
4205 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004206 defs, defcount,
4207 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004208 NULL, NULL);
4209}
Tim Peters5ca576e2001-06-18 22:08:13 +00004210
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004211static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004212special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004214 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004215 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004216 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004217 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004218 return NULL;
4219 }
4220 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004221}
4222
4223
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004224/* Logic for the raise statement (too complicated for inlining).
4225 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004226static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004227do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004228{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004229 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004231 if (exc == NULL) {
4232 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004233 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004234 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004235 type = exc_info->exc_type;
4236 value = exc_info->exc_value;
4237 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004238 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004239 PyErr_SetString(PyExc_RuntimeError,
4240 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004241 return 0;
4242 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004243 Py_XINCREF(type);
4244 Py_XINCREF(value);
4245 Py_XINCREF(tb);
4246 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004247 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004248 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004250 /* We support the following forms of raise:
4251 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004252 raise <instance>
4253 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004255 if (PyExceptionClass_Check(exc)) {
4256 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004257 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004258 if (value == NULL)
4259 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004260 if (!PyExceptionInstance_Check(value)) {
4261 PyErr_Format(PyExc_TypeError,
4262 "calling %R should have returned an instance of "
4263 "BaseException, not %R",
4264 type, Py_TYPE(value));
4265 goto raise_error;
4266 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004267 }
4268 else if (PyExceptionInstance_Check(exc)) {
4269 value = exc;
4270 type = PyExceptionInstance_Class(exc);
4271 Py_INCREF(type);
4272 }
4273 else {
4274 /* Not something you can raise. You get an exception
4275 anyway, just not what you specified :-) */
4276 Py_DECREF(exc);
4277 PyErr_SetString(PyExc_TypeError,
4278 "exceptions must derive from BaseException");
4279 goto raise_error;
4280 }
Collin Winter828f04a2007-08-31 00:04:24 +00004281
Serhiy Storchakac0191582016-09-27 11:37:10 +03004282 assert(type != NULL);
4283 assert(value != NULL);
4284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004285 if (cause) {
4286 PyObject *fixed_cause;
4287 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004288 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004289 if (fixed_cause == NULL)
4290 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004291 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004292 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004293 else if (PyExceptionInstance_Check(cause)) {
4294 fixed_cause = cause;
4295 }
4296 else if (cause == Py_None) {
4297 Py_DECREF(cause);
4298 fixed_cause = NULL;
4299 }
4300 else {
4301 PyErr_SetString(PyExc_TypeError,
4302 "exception causes must derive from "
4303 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004304 goto raise_error;
4305 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004306 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004307 }
Collin Winter828f04a2007-08-31 00:04:24 +00004308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004309 PyErr_SetObject(type, value);
4310 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004311 Py_DECREF(value);
4312 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004313 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004314
4315raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004316 Py_XDECREF(value);
4317 Py_XDECREF(type);
4318 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004319 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004320}
4321
Tim Petersd6d010b2001-06-21 02:49:55 +00004322/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004323 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004324
Guido van Rossum0368b722007-05-11 16:50:42 +00004325 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4326 with a variable target.
4327*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004328
Barry Warsawe42b18f1997-08-25 22:13:04 +00004329static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004330unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004331{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004332 int i = 0, j = 0;
4333 Py_ssize_t ll = 0;
4334 PyObject *it; /* iter(v) */
4335 PyObject *w;
4336 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004338 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004340 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004341 if (it == NULL) {
4342 if (PyErr_ExceptionMatches(PyExc_TypeError) &&
4343 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4344 {
4345 PyErr_Format(PyExc_TypeError,
4346 "cannot unpack non-iterable %.200s object",
4347 v->ob_type->tp_name);
4348 }
4349 return 0;
4350 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004352 for (; i < argcnt; i++) {
4353 w = PyIter_Next(it);
4354 if (w == NULL) {
4355 /* Iterator done, via error or exhaustion. */
4356 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004357 if (argcntafter == -1) {
4358 PyErr_Format(PyExc_ValueError,
4359 "not enough values to unpack (expected %d, got %d)",
4360 argcnt, i);
4361 }
4362 else {
4363 PyErr_Format(PyExc_ValueError,
4364 "not enough values to unpack "
4365 "(expected at least %d, got %d)",
4366 argcnt + argcntafter, i);
4367 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004368 }
4369 goto Error;
4370 }
4371 *--sp = w;
4372 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004374 if (argcntafter == -1) {
4375 /* We better have exhausted the iterator now. */
4376 w = PyIter_Next(it);
4377 if (w == NULL) {
4378 if (PyErr_Occurred())
4379 goto Error;
4380 Py_DECREF(it);
4381 return 1;
4382 }
4383 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004384 PyErr_Format(PyExc_ValueError,
4385 "too many values to unpack (expected %d)",
4386 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004387 goto Error;
4388 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004390 l = PySequence_List(it);
4391 if (l == NULL)
4392 goto Error;
4393 *--sp = l;
4394 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004396 ll = PyList_GET_SIZE(l);
4397 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004398 PyErr_Format(PyExc_ValueError,
4399 "not enough values to unpack (expected at least %d, got %zd)",
4400 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004401 goto Error;
4402 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004404 /* Pop the "after-variable" args off the list. */
4405 for (j = argcntafter; j > 0; j--, i++) {
4406 *--sp = PyList_GET_ITEM(l, ll - j);
4407 }
4408 /* Resize the list. */
4409 Py_SIZE(l) = ll - argcntafter;
4410 Py_DECREF(it);
4411 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004412
Tim Petersd6d010b2001-06-21 02:49:55 +00004413Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414 for (; i > 0; i--, sp++)
4415 Py_DECREF(*sp);
4416 Py_XDECREF(it);
4417 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004418}
4419
4420
Guido van Rossum96a42c81992-01-12 02:29:51 +00004421#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004422static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004423prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004425 printf("%s ", str);
4426 if (PyObject_Print(v, stdout, 0) != 0)
4427 PyErr_Clear(); /* Don't know what else to do */
4428 printf("\n");
4429 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004430}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004431#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004432
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004433static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004434call_exc_trace(Py_tracefunc func, PyObject *self,
4435 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004436{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004437 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004438 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004439 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004440 if (value == NULL) {
4441 value = Py_None;
4442 Py_INCREF(value);
4443 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004444 PyErr_NormalizeException(&type, &value, &orig_traceback);
4445 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004446 arg = PyTuple_Pack(3, type, value, traceback);
4447 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004448 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004449 return;
4450 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004451 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004452 Py_DECREF(arg);
4453 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004454 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004455 else {
4456 Py_XDECREF(type);
4457 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004458 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004459 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004460}
4461
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004462static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004463call_trace_protected(Py_tracefunc func, PyObject *obj,
4464 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004465 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004466{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004467 PyObject *type, *value, *traceback;
4468 int err;
4469 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004470 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004471 if (err == 0)
4472 {
4473 PyErr_Restore(type, value, traceback);
4474 return 0;
4475 }
4476 else {
4477 Py_XDECREF(type);
4478 Py_XDECREF(value);
4479 Py_XDECREF(traceback);
4480 return -1;
4481 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004482}
4483
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004484static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004485call_trace(Py_tracefunc func, PyObject *obj,
4486 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004487 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004488{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 int result;
4490 if (tstate->tracing)
4491 return 0;
4492 tstate->tracing++;
4493 tstate->use_tracing = 0;
4494 result = func(obj, frame, what, arg);
4495 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4496 || (tstate->c_profilefunc != NULL));
4497 tstate->tracing--;
4498 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004499}
4500
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004501PyObject *
4502_PyEval_CallTracing(PyObject *func, PyObject *args)
4503{
Victor Stinner50b48572018-11-01 01:51:40 +01004504 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004505 int save_tracing = tstate->tracing;
4506 int save_use_tracing = tstate->use_tracing;
4507 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004509 tstate->tracing = 0;
4510 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4511 || (tstate->c_profilefunc != NULL));
4512 result = PyObject_Call(func, args, NULL);
4513 tstate->tracing = save_tracing;
4514 tstate->use_tracing = save_use_tracing;
4515 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004516}
4517
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004518/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004519static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004520maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004521 PyThreadState *tstate, PyFrameObject *frame,
4522 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004523{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004524 int result = 0;
4525 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004527 /* If the last instruction executed isn't in the current
4528 instruction window, reset the window.
4529 */
4530 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4531 PyAddrPair bounds;
4532 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4533 &bounds);
4534 *instr_lb = bounds.ap_lower;
4535 *instr_ub = bounds.ap_upper;
4536 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004537 /* If the last instruction falls at the start of a line or if it
4538 represents a jump backwards, update the frame's line number and
4539 then call the trace function if we're tracing source lines.
4540 */
4541 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004542 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004543 if (frame->f_trace_lines) {
4544 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4545 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004546 }
George King20faa682017-10-18 17:44:22 -07004547 /* Always emit an opcode event if we're tracing all opcodes. */
4548 if (frame->f_trace_opcodes) {
4549 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4550 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004551 *instr_prev = frame->f_lasti;
4552 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004553}
4554
Fred Drake5755ce62001-06-27 19:19:46 +00004555void
4556PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004557{
Victor Stinner50b48572018-11-01 01:51:40 +01004558 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004559 PyObject *temp = tstate->c_profileobj;
4560 Py_XINCREF(arg);
4561 tstate->c_profilefunc = NULL;
4562 tstate->c_profileobj = NULL;
4563 /* Must make sure that tracing is not ignored if 'temp' is freed */
4564 tstate->use_tracing = tstate->c_tracefunc != NULL;
4565 Py_XDECREF(temp);
4566 tstate->c_profilefunc = func;
4567 tstate->c_profileobj = arg;
4568 /* Flag that tracing or profiling is turned on */
4569 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004570}
4571
4572void
4573PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4574{
Victor Stinner09532fe2019-05-10 23:39:09 +02004575 _PyRuntimeState *runtime = &_PyRuntime;
4576 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004577 PyObject *temp = tstate->c_traceobj;
Victor Stinner09532fe2019-05-10 23:39:09 +02004578 runtime->ceval.tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004579 Py_XINCREF(arg);
4580 tstate->c_tracefunc = NULL;
4581 tstate->c_traceobj = NULL;
4582 /* Must make sure that profiling is not ignored if 'temp' is freed */
4583 tstate->use_tracing = tstate->c_profilefunc != NULL;
4584 Py_XDECREF(temp);
4585 tstate->c_tracefunc = func;
4586 tstate->c_traceobj = arg;
4587 /* Flag that tracing or profiling is turned on */
4588 tstate->use_tracing = ((func != NULL)
4589 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004590}
4591
Yury Selivanov75445082015-05-11 22:57:16 -04004592void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004593_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4594{
4595 assert(new_depth >= 0);
Victor Stinner50b48572018-11-01 01:51:40 +01004596 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004597 tstate->coroutine_origin_tracking_depth = new_depth;
4598}
4599
4600int
4601_PyEval_GetCoroutineOriginTrackingDepth(void)
4602{
Victor Stinner50b48572018-11-01 01:51:40 +01004603 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004604 return tstate->coroutine_origin_tracking_depth;
4605}
4606
4607void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004608_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004609{
Victor Stinner50b48572018-11-01 01:51:40 +01004610 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004611 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004612 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004613}
4614
4615PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004616_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004617{
Victor Stinner50b48572018-11-01 01:51:40 +01004618 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004619 return tstate->coroutine_wrapper;
4620}
4621
Yury Selivanoveb636452016-09-08 22:01:51 -07004622void
4623_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4624{
Victor Stinner50b48572018-11-01 01:51:40 +01004625 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004626 Py_XINCREF(firstiter);
4627 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4628}
4629
4630PyObject *
4631_PyEval_GetAsyncGenFirstiter(void)
4632{
Victor Stinner50b48572018-11-01 01:51:40 +01004633 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004634 return tstate->async_gen_firstiter;
4635}
4636
4637void
4638_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4639{
Victor Stinner50b48572018-11-01 01:51:40 +01004640 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004641 Py_XINCREF(finalizer);
4642 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4643}
4644
4645PyObject *
4646_PyEval_GetAsyncGenFinalizer(void)
4647{
Victor Stinner50b48572018-11-01 01:51:40 +01004648 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004649 return tstate->async_gen_finalizer;
4650}
4651
Guido van Rossumb209a111997-04-29 18:18:01 +00004652PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004653PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004654{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004655 PyFrameObject *current_frame = PyEval_GetFrame();
4656 if (current_frame == NULL)
Victor Stinnercaba55b2018-08-03 15:33:52 +02004657 return _PyInterpreterState_GET_UNSAFE()->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004658 else
4659 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004660}
4661
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004662/* Convenience function to get a builtin from its name */
4663PyObject *
4664_PyEval_GetBuiltinId(_Py_Identifier *name)
4665{
4666 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4667 if (attr) {
4668 Py_INCREF(attr);
4669 }
4670 else if (!PyErr_Occurred()) {
4671 PyErr_SetObject(PyExc_AttributeError, _PyUnicode_FromId(name));
4672 }
4673 return attr;
4674}
4675
Guido van Rossumb209a111997-04-29 18:18:01 +00004676PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004677PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004678{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004679 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004680 if (current_frame == NULL) {
4681 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004682 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004683 }
4684
4685 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4686 return NULL;
4687
4688 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004689 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004690}
4691
Guido van Rossumb209a111997-04-29 18:18:01 +00004692PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004693PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004694{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004695 PyFrameObject *current_frame = PyEval_GetFrame();
4696 if (current_frame == NULL)
4697 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004698
4699 assert(current_frame->f_globals != NULL);
4700 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004701}
4702
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004703PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004704PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004705{
Victor Stinner09532fe2019-05-10 23:39:09 +02004706 _PyRuntimeState *runtime = &_PyRuntime;
4707 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
4708 return runtime->gilstate.getframe(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004709}
4710
Guido van Rossum6135a871995-01-09 17:53:26 +00004711int
Tim Peters5ba58662001-07-16 02:29:45 +00004712PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004713{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004714 PyFrameObject *current_frame = PyEval_GetFrame();
4715 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004717 if (current_frame != NULL) {
4718 const int codeflags = current_frame->f_code->co_flags;
4719 const int compilerflags = codeflags & PyCF_MASK;
4720 if (compilerflags) {
4721 result = 1;
4722 cf->cf_flags |= compilerflags;
4723 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004724#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004725 if (codeflags & CO_GENERATOR_ALLOWED) {
4726 result = 1;
4727 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4728 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004729#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004730 }
4731 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004732}
4733
Guido van Rossum3f5da241990-12-20 15:06:42 +00004734
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004735const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004736PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004737{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004738 if (PyMethod_Check(func))
4739 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4740 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004741 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004742 else if (PyCFunction_Check(func))
4743 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4744 else
4745 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004746}
4747
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004748const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004749PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004751 if (PyMethod_Check(func))
4752 return "()";
4753 else if (PyFunction_Check(func))
4754 return "()";
4755 else if (PyCFunction_Check(func))
4756 return "()";
4757 else
4758 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004759}
4760
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004761#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004762if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004763 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4764 tstate, tstate->frame, \
4765 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004766 x = NULL; \
4767 } \
4768 else { \
4769 x = call; \
4770 if (tstate->c_profilefunc != NULL) { \
4771 if (x == NULL) { \
4772 call_trace_protected(tstate->c_profilefunc, \
4773 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004774 tstate, tstate->frame, \
4775 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004776 /* XXX should pass (type, value, tb) */ \
4777 } else { \
4778 if (call_trace(tstate->c_profilefunc, \
4779 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004780 tstate, tstate->frame, \
4781 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004782 Py_DECREF(x); \
4783 x = NULL; \
4784 } \
4785 } \
4786 } \
4787 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004788} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004789 x = call; \
4790 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004791
Victor Stinner415c5102017-01-11 00:54:57 +01004792/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4793 to reduce the stack consumption. */
4794Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02004795call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004796{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004797 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004798 PyObject *func = *pfunc;
4799 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004800 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4801 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004802 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004804 /* Always dispatch PyCFunction first, because these are
4805 presumed to be the most frequent callable object.
4806 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004807 if (PyCFunction_Check(func)) {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004808 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004809 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004810 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
jdemeyer56868f92018-07-21 10:30:59 +02004811 if (nargs > 0 && tstate->use_tracing) {
4812 /* We need to create a temporary bound method as argument
4813 for profiling.
4814
4815 If nargs == 0, then this cannot work because we have no
4816 "self". In any case, the call itself would raise
4817 TypeError (foo needs an argument), so we just skip
4818 profiling. */
4819 PyObject *self = stack[0];
4820 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
jdemeyer147d9552018-07-23 18:41:20 +02004821 if (func != NULL) {
4822 C_TRACE(x, _PyCFunction_FastCallKeywords(func,
4823 stack+1, nargs-1,
4824 kwnames));
4825 Py_DECREF(func);
INADA Naoki93fac8d2017-03-07 14:24:37 +09004826 }
jdemeyer147d9552018-07-23 18:41:20 +02004827 else {
4828 x = NULL;
4829 }
INADA Naoki93fac8d2017-03-07 14:24:37 +09004830 }
4831 else {
4832 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4833 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004834 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004835 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004836 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004837 /* Optimize access to bound methods. Reuse the Python stack
4838 to pass 'self' as the first argument, replace 'func'
4839 with 'self'. It avoids the creation of a new temporary tuple
4840 for arguments (to replace func with self) when the method uses
4841 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004842 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004843 Py_INCREF(self);
4844 func = PyMethod_GET_FUNCTION(func);
4845 Py_INCREF(func);
4846 Py_SETREF(*pfunc, self);
4847 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004848 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004849 }
4850 else {
4851 Py_INCREF(func);
4852 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004853
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004854 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004855 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004856 }
4857 else {
4858 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4859 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004860 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004861 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004862
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004863 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4864
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004865 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004866 while ((*pp_stack) > pfunc) {
4867 w = EXT_POP(*pp_stack);
4868 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004869 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004871 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004872}
4873
Jeremy Hylton52820442001-01-03 23:52:36 +00004874static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02004875do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004876{
jdemeyere89de732018-09-19 12:06:20 +02004877 PyObject *result;
4878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004879 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004880 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004881 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004882 }
jdemeyere89de732018-09-19 12:06:20 +02004883 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
jdemeyere89de732018-09-19 12:06:20 +02004884 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4885 if (nargs > 0 && tstate->use_tracing) {
4886 /* We need to create a temporary bound method as argument
4887 for profiling.
4888
4889 If nargs == 0, then this cannot work because we have no
4890 "self". In any case, the call itself would raise
4891 TypeError (foo needs an argument), so we just skip
4892 profiling. */
4893 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4894 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4895 if (func == NULL) {
4896 return NULL;
4897 }
4898
4899 C_TRACE(result, _PyCFunction_FastCallDict(func,
Victor Stinnerd17a6932018-11-09 16:56:48 +01004900 &_PyTuple_ITEMS(callargs)[1],
jdemeyere89de732018-09-19 12:06:20 +02004901 nargs - 1,
4902 kwdict));
4903 Py_DECREF(func);
4904 return result;
4905 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004906 }
jdemeyere89de732018-09-19 12:06:20 +02004907 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004908}
4909
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004910/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004911 nb_index slot defined, and store in *pi.
4912 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004913 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004914 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004915*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004916int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004917_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004918{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004919 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004920 Py_ssize_t x;
4921 if (PyIndex_Check(v)) {
4922 x = PyNumber_AsSsize_t(v, NULL);
4923 if (x == -1 && PyErr_Occurred())
4924 return 0;
4925 }
4926 else {
4927 PyErr_SetString(PyExc_TypeError,
4928 "slice indices must be integers or "
4929 "None or have an __index__ method");
4930 return 0;
4931 }
4932 *pi = x;
4933 }
4934 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004935}
4936
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004937int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004938_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004939{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004940 Py_ssize_t x;
4941 if (PyIndex_Check(v)) {
4942 x = PyNumber_AsSsize_t(v, NULL);
4943 if (x == -1 && PyErr_Occurred())
4944 return 0;
4945 }
4946 else {
4947 PyErr_SetString(PyExc_TypeError,
4948 "slice indices must be integers or "
4949 "have an __index__ method");
4950 return 0;
4951 }
4952 *pi = x;
4953 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004954}
4955
4956
Guido van Rossum486364b2007-06-30 05:01:58 +00004957#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004958 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004959
Guido van Rossumb209a111997-04-29 18:18:01 +00004960static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004961cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004962{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004963 int res = 0;
4964 switch (op) {
4965 case PyCmp_IS:
4966 res = (v == w);
4967 break;
4968 case PyCmp_IS_NOT:
4969 res = (v != w);
4970 break;
4971 case PyCmp_IN:
4972 res = PySequence_Contains(w, v);
4973 if (res < 0)
4974 return NULL;
4975 break;
4976 case PyCmp_NOT_IN:
4977 res = PySequence_Contains(w, v);
4978 if (res < 0)
4979 return NULL;
4980 res = !res;
4981 break;
4982 case PyCmp_EXC_MATCH:
4983 if (PyTuple_Check(w)) {
4984 Py_ssize_t i, length;
4985 length = PyTuple_Size(w);
4986 for (i = 0; i < length; i += 1) {
4987 PyObject *exc = PyTuple_GET_ITEM(w, i);
4988 if (!PyExceptionClass_Check(exc)) {
4989 PyErr_SetString(PyExc_TypeError,
4990 CANNOT_CATCH_MSG);
4991 return NULL;
4992 }
4993 }
4994 }
4995 else {
4996 if (!PyExceptionClass_Check(w)) {
4997 PyErr_SetString(PyExc_TypeError,
4998 CANNOT_CATCH_MSG);
4999 return NULL;
5000 }
5001 }
5002 res = PyErr_GivenExceptionMatches(v, w);
5003 break;
5004 default:
5005 return PyObject_RichCompare(v, w, op);
5006 }
5007 v = res ? Py_True : Py_False;
5008 Py_INCREF(v);
5009 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005010}
5011
Thomas Wouters52152252000-08-17 22:55:00 +00005012static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005013import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
5014{
5015 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005016 PyObject *import_func, *res;
5017 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005018
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005019 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005020 if (import_func == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005021 if (!PyErr_Occurred()) {
5022 PyErr_SetString(PyExc_ImportError, "__import__ not found");
5023 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005024 return NULL;
5025 }
5026
5027 /* Fast path for not overloaded __import__. */
Victor Stinnercaba55b2018-08-03 15:33:52 +02005028 if (import_func == _PyInterpreterState_GET_UNSAFE()->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005029 int ilevel = _PyLong_AsInt(level);
5030 if (ilevel == -1 && PyErr_Occurred()) {
5031 return NULL;
5032 }
5033 res = PyImport_ImportModuleLevelObject(
5034 name,
5035 f->f_globals,
5036 f->f_locals == NULL ? Py_None : f->f_locals,
5037 fromlist,
5038 ilevel);
5039 return res;
5040 }
5041
5042 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005043
5044 stack[0] = name;
5045 stack[1] = f->f_globals;
5046 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5047 stack[3] = fromlist;
5048 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005049 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005050 Py_DECREF(import_func);
5051 return res;
5052}
5053
5054static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00005055import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005056{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005057 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005058 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08005059 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005060
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005061 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005062 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005063 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005064 /* Issue #17636: in case this failed because of a circular relative
5065 import, try to fallback on reading the module directly from
5066 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005067 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005068 if (pkgname == NULL) {
5069 goto error;
5070 }
Oren Milman6db70332017-09-19 14:23:01 +03005071 if (!PyUnicode_Check(pkgname)) {
5072 Py_CLEAR(pkgname);
5073 goto error;
5074 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005075 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005076 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005077 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005078 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005079 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005080 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005081 Py_DECREF(fullmodname);
Stefan Krah027b09c2019-03-25 21:50:58 +01005082 if (x == NULL && !PyErr_Occurred()) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005083 goto error;
5084 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005085 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005086 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005087 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005088 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005089 if (pkgname == NULL) {
5090 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5091 if (pkgname_or_unknown == NULL) {
5092 Py_XDECREF(pkgpath);
5093 return NULL;
5094 }
5095 } else {
5096 pkgname_or_unknown = pkgname;
5097 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005098
5099 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
5100 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08005101 errmsg = PyUnicode_FromFormat(
5102 "cannot import name %R from %R (unknown location)",
5103 name, pkgname_or_unknown
5104 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005105 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005106 PyErr_SetImportError(errmsg, pkgname, NULL);
5107 }
5108 else {
5109 errmsg = PyUnicode_FromFormat(
5110 "cannot import name %R from %R (%S)",
5111 name, pkgname_or_unknown, pkgpath
5112 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005113 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005114 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005115 }
5116
Xiang Zhang4830f582017-03-21 11:13:42 +08005117 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005118 Py_XDECREF(pkgname_or_unknown);
5119 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005120 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005121}
Guido van Rossumac7be682001-01-17 15:42:30 +00005122
Thomas Wouters52152252000-08-17 22:55:00 +00005123static int
5124import_all_from(PyObject *locals, PyObject *v)
5125{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005126 _Py_IDENTIFIER(__all__);
5127 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005128 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005129 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005130 int skip_leading_underscores = 0;
5131 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005132
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005133 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5134 return -1; /* Unexpected error */
5135 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005136 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005137 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5138 return -1;
5139 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005140 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005141 PyErr_SetString(PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005142 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005143 return -1;
5144 }
5145 all = PyMapping_Keys(dict);
5146 Py_DECREF(dict);
5147 if (all == NULL)
5148 return -1;
5149 skip_leading_underscores = 1;
5150 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005152 for (pos = 0, err = 0; ; pos++) {
5153 name = PySequence_GetItem(all, pos);
5154 if (name == NULL) {
5155 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5156 err = -1;
5157 else
5158 PyErr_Clear();
5159 break;
5160 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005161 if (!PyUnicode_Check(name)) {
5162 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5163 if (modname == NULL) {
5164 Py_DECREF(name);
5165 err = -1;
5166 break;
5167 }
5168 if (!PyUnicode_Check(modname)) {
5169 PyErr_Format(PyExc_TypeError,
5170 "module __name__ must be a string, not %.100s",
5171 Py_TYPE(modname)->tp_name);
5172 }
5173 else {
5174 PyErr_Format(PyExc_TypeError,
5175 "%s in %U.%s must be str, not %.100s",
5176 skip_leading_underscores ? "Key" : "Item",
5177 modname,
5178 skip_leading_underscores ? "__dict__" : "__all__",
5179 Py_TYPE(name)->tp_name);
5180 }
5181 Py_DECREF(modname);
5182 Py_DECREF(name);
5183 err = -1;
5184 break;
5185 }
5186 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005187 if (PyUnicode_READY(name) == -1) {
5188 Py_DECREF(name);
5189 err = -1;
5190 break;
5191 }
5192 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5193 Py_DECREF(name);
5194 continue;
5195 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005196 }
5197 value = PyObject_GetAttr(v, name);
5198 if (value == NULL)
5199 err = -1;
5200 else if (PyDict_CheckExact(locals))
5201 err = PyDict_SetItem(locals, name, value);
5202 else
5203 err = PyObject_SetItem(locals, name, value);
5204 Py_DECREF(name);
5205 Py_XDECREF(value);
5206 if (err != 0)
5207 break;
5208 }
5209 Py_DECREF(all);
5210 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005211}
5212
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005213static int
5214check_args_iterable(PyObject *func, PyObject *args)
5215{
5216 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
5217 PyErr_Format(PyExc_TypeError,
5218 "%.200s%.200s argument after * "
5219 "must be an iterable, not %.200s",
5220 PyEval_GetFuncName(func),
5221 PyEval_GetFuncDesc(func),
5222 args->ob_type->tp_name);
5223 return -1;
5224 }
5225 return 0;
5226}
5227
5228static void
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005229format_kwargs_error(PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005230{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005231 /* _PyDict_MergeEx raises attribute
5232 * error (percolated from an attempt
5233 * to get 'keys' attribute) instead of
5234 * a type error if its second argument
5235 * is not a mapping.
5236 */
5237 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
5238 PyErr_Format(PyExc_TypeError,
5239 "%.200s%.200s argument after ** "
5240 "must be a mapping, not %.200s",
5241 PyEval_GetFuncName(func),
5242 PyEval_GetFuncDesc(func),
5243 kwargs->ob_type->tp_name);
5244 }
5245 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
5246 PyObject *exc, *val, *tb;
5247 PyErr_Fetch(&exc, &val, &tb);
5248 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
5249 PyObject *key = PyTuple_GET_ITEM(val, 0);
5250 if (!PyUnicode_Check(key)) {
5251 PyErr_Format(PyExc_TypeError,
5252 "%.200s%.200s keywords must be strings",
5253 PyEval_GetFuncName(func),
5254 PyEval_GetFuncDesc(func));
5255 } else {
5256 PyErr_Format(PyExc_TypeError,
5257 "%.200s%.200s got multiple "
5258 "values for keyword argument '%U'",
5259 PyEval_GetFuncName(func),
5260 PyEval_GetFuncDesc(func),
5261 key);
5262 }
5263 Py_XDECREF(exc);
5264 Py_XDECREF(val);
5265 Py_XDECREF(tb);
5266 }
5267 else {
5268 PyErr_Restore(exc, val, tb);
5269 }
5270 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005271}
5272
Guido van Rossumac7be682001-01-17 15:42:30 +00005273static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005274format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005275{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005276 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005278 if (!obj)
5279 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005280
Serhiy Storchaka06515832016-11-20 09:13:07 +02005281 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005282 if (!obj_str)
5283 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005285 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005286}
Guido van Rossum950361c1997-01-24 13:49:28 +00005287
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005288static void
5289format_exc_unbound(PyCodeObject *co, int oparg)
5290{
5291 PyObject *name;
5292 /* Don't stomp existing exception */
5293 if (PyErr_Occurred())
5294 return;
5295 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5296 name = PyTuple_GET_ITEM(co->co_cellvars,
5297 oparg);
5298 format_exc_check_arg(
5299 PyExc_UnboundLocalError,
5300 UNBOUNDLOCAL_ERROR_MSG,
5301 name);
5302 } else {
5303 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5304 PyTuple_GET_SIZE(co->co_cellvars));
5305 format_exc_check_arg(PyExc_NameError,
5306 UNBOUNDFREE_ERROR_MSG, name);
5307 }
5308}
5309
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005310static void
5311format_awaitable_error(PyTypeObject *type, int prevopcode)
5312{
5313 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5314 if (prevopcode == BEFORE_ASYNC_WITH) {
5315 PyErr_Format(PyExc_TypeError,
5316 "'async with' received an object from __aenter__ "
5317 "that does not implement __await__: %.100s",
5318 type->tp_name);
5319 }
5320 else if (prevopcode == WITH_CLEANUP_START) {
5321 PyErr_Format(PyExc_TypeError,
5322 "'async with' received an object from __aexit__ "
5323 "that does not implement __await__: %.100s",
5324 type->tp_name);
5325 }
5326 }
5327}
5328
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005329static PyObject *
5330unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005331 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005332{
5333 PyObject *res;
5334 if (Py_REFCNT(v) == 2) {
5335 /* In the common case, there are 2 references to the value
5336 * stored in 'variable' when the += is performed: one on the
5337 * value stack (in 'v') and one still stored in the
5338 * 'variable'. We try to delete the variable now to reduce
5339 * the refcnt to 1.
5340 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005341 int opcode, oparg;
5342 NEXTOPARG();
5343 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005344 case STORE_FAST:
5345 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005346 PyObject **fastlocals = f->f_localsplus;
5347 if (GETLOCAL(oparg) == v)
5348 SETLOCAL(oparg, NULL);
5349 break;
5350 }
5351 case STORE_DEREF:
5352 {
5353 PyObject **freevars = (f->f_localsplus +
5354 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005355 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005356 if (PyCell_GET(c) == v) {
5357 PyCell_SET(c, NULL);
5358 Py_DECREF(v);
5359 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005360 break;
5361 }
5362 case STORE_NAME:
5363 {
5364 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005365 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005366 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005367 if (locals && PyDict_CheckExact(locals)) {
5368 PyObject *w = PyDict_GetItemWithError(locals, name);
5369 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
5370 (w == NULL && PyErr_Occurred()))
5371 {
5372 Py_DECREF(v);
5373 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005374 }
5375 }
5376 break;
5377 }
5378 }
5379 }
5380 res = v;
5381 PyUnicode_Append(&res, w);
5382 return res;
5383}
5384
Guido van Rossum950361c1997-01-24 13:49:28 +00005385#ifdef DYNAMIC_EXECUTION_PROFILE
5386
Skip Montanarof118cb12001-10-15 20:51:38 +00005387static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005388getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005389{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005390 int i;
5391 PyObject *l = PyList_New(256);
5392 if (l == NULL) return NULL;
5393 for (i = 0; i < 256; i++) {
5394 PyObject *x = PyLong_FromLong(a[i]);
5395 if (x == NULL) {
5396 Py_DECREF(l);
5397 return NULL;
5398 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005399 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005400 }
5401 for (i = 0; i < 256; i++)
5402 a[i] = 0;
5403 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005404}
5405
5406PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005407_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005408{
5409#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005410 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005411#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005412 int i;
5413 PyObject *l = PyList_New(257);
5414 if (l == NULL) return NULL;
5415 for (i = 0; i < 257; i++) {
5416 PyObject *x = getarray(dxpairs[i]);
5417 if (x == NULL) {
5418 Py_DECREF(l);
5419 return NULL;
5420 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005421 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005422 }
5423 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005424#endif
5425}
5426
5427#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005428
5429Py_ssize_t
5430_PyEval_RequestCodeExtraIndex(freefunc free)
5431{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005432 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005433 Py_ssize_t new_index;
5434
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005435 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005436 return -1;
5437 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005438 new_index = interp->co_extra_user_count++;
5439 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005440 return new_index;
5441}
Łukasz Langaa785c872016-09-09 17:37:37 -07005442
5443static void
5444dtrace_function_entry(PyFrameObject *f)
5445{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005446 const char *filename;
5447 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005448 int lineno;
5449
5450 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5451 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5452 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5453
5454 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5455}
5456
5457static void
5458dtrace_function_return(PyFrameObject *f)
5459{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005460 const char *filename;
5461 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005462 int lineno;
5463
5464 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5465 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5466 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5467
5468 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5469}
5470
5471/* DTrace equivalent of maybe_call_line_trace. */
5472static void
5473maybe_dtrace_line(PyFrameObject *frame,
5474 int *instr_lb, int *instr_ub, int *instr_prev)
5475{
5476 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005477 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005478
5479 /* If the last instruction executed isn't in the current
5480 instruction window, reset the window.
5481 */
5482 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5483 PyAddrPair bounds;
5484 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5485 &bounds);
5486 *instr_lb = bounds.ap_lower;
5487 *instr_ub = bounds.ap_upper;
5488 }
5489 /* If the last instruction falls at the start of a line or if
5490 it represents a jump backwards, update the frame's line
5491 number and call the trace function. */
5492 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5493 frame->f_lineno = line;
5494 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5495 if (!co_filename)
5496 co_filename = "?";
5497 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5498 if (!co_name)
5499 co_name = "?";
5500 PyDTrace_LINE(co_filename, co_name, line);
5501 }
5502 *instr_prev = frame->f_lasti;
5503}