blob: cb0275c4183d2a8ab12824b12f40bfd7cbf9beb6 [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"
Inada Naoki91234a12019-06-03 21:30:58 +090014#include "pycore_code.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +010015#include "pycore_object.h"
Victor Stinner438a12d2019-05-24 17:01:38 +020016#include "pycore_pyerrors.h"
17#include "pycore_pylifecycle.h"
Victor Stinner621cebe2018-11-12 16:53:38 +010018#include "pycore_pystate.h"
Victor Stinnerec13b932018-11-25 23:56:17 +010019#include "pycore_tupleobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000020
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000021#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040022#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000023#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000024#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070025#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040026#include "setobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000027#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000028
Guido van Rossumc6004111993-11-05 10:22:19 +000029#include <ctype.h>
30
Guido van Rossum408027e1996-12-30 16:17:54 +000031#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000032/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000033#define LLTRACE 1 /* Low-level trace feature */
34#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000035#endif
36
Victor Stinner5c75f372019-04-17 23:02:26 +020037#if !defined(Py_BUILD_CORE)
38# error "ceval.c must be build with Py_BUILD_CORE define for best performance"
39#endif
40
Yury Selivanovf2392132016-12-13 19:03:51 -050041/* Private API for the LOAD_METHOD opcode. */
42extern int _PyObject_GetMethod(PyObject *, PyObject *, PyObject **);
43
Jeremy Hylton52820442001-01-03 23:52:36 +000044typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000045
Guido van Rossum374a9221991-04-04 10:40:29 +000046/* Forward declarations */
Victor Stinner09532fe2019-05-10 23:39:09 +020047Py_LOCAL_INLINE(PyObject *) call_function(
48 PyThreadState *tstate, PyObject ***pp_stack,
49 Py_ssize_t oparg, PyObject *kwnames);
50static PyObject * do_call_core(
51 PyThreadState *tstate, PyObject *func,
52 PyObject *callargs, PyObject *kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +000053
Guido van Rossum0a066c01992-03-27 17:29:15 +000054#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000055static int lltrace;
Victor Stinner438a12d2019-05-24 17:01:38 +020056static int prtrace(PyThreadState *, PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000057#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010058static int call_trace(Py_tracefunc, PyObject *,
59 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000061static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010062 PyThreadState *, PyFrameObject *,
63 int, PyObject *);
64static void call_exc_trace(Py_tracefunc, PyObject *,
65 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000066static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060067 PyThreadState *, PyFrameObject *,
68 int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070069static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
70static void dtrace_function_entry(PyFrameObject *);
71static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000072
Victor Stinner438a12d2019-05-24 17:01:38 +020073static PyObject * cmp_outcome(PyThreadState *, int, PyObject *, PyObject *);
74static PyObject * import_name(PyThreadState *, PyFrameObject *,
75 PyObject *, PyObject *, PyObject *);
76static PyObject * import_from(PyThreadState *, PyObject *, PyObject *);
77static int import_all_from(PyThreadState *, PyObject *, PyObject *);
78static void format_exc_check_arg(PyThreadState *, PyObject *, const char *, PyObject *);
79static void format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
80static PyObject * unicode_concatenate(PyThreadState *, PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030081 PyFrameObject *, const _Py_CODEUNIT *);
Victor Stinner438a12d2019-05-24 17:01:38 +020082static PyObject * special_lookup(PyThreadState *, PyObject *, _Py_Identifier *);
83static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg);
84static void format_kwargs_error(PyThreadState *, PyObject *func, PyObject *kwargs);
85static void format_awaitable_error(PyThreadState *, PyTypeObject *, int);
Guido van Rossum374a9221991-04-04 10:40:29 +000086
Paul Prescode68140d2000-08-30 20:25:01 +000087#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000089#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000091#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 "free variable '%.200s' referenced before assignment" \
93 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000094
Guido van Rossum950361c1997-01-24 13:49:28 +000095/* Dynamic execution profile */
96#ifdef DYNAMIC_EXECUTION_PROFILE
97#ifdef DXPAIRS
98static long dxpairs[257][256];
99#define dxp dxpairs[256]
100#else
101static long dxp[256];
102#endif
103#endif
104
Inada Naoki91234a12019-06-03 21:30:58 +0900105/* per opcode cache */
106#define OPCACHE_MIN_RUNS 1024 /* create opcache when code executed this time */
107#define OPCACHE_STATS 0 /* Enable stats */
108
109#if OPCACHE_STATS
110static size_t opcache_code_objects = 0;
111static size_t opcache_code_objects_extra_mem = 0;
112
113static size_t opcache_global_opts = 0;
114static size_t opcache_global_hits = 0;
115static size_t opcache_global_misses = 0;
116#endif
117
118
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000119/* This can set eval_breaker to 0 even though gil_drop_request became
120 1. We believe this is all right because the eval loop will release
121 the GIL eventually anyway. */
Eric Snow6a150bc2019-06-01 15:39:46 -0600122#define COMPUTE_EVAL_BREAKER(ceval_r, ceval_i) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 _Py_atomic_store_relaxed( \
Eric Snow6a150bc2019-06-01 15:39:46 -0600124 &(ceval_r)->eval_breaker, \
125 _Py_atomic_load_relaxed(&(ceval_r)->gil_drop_request) | \
126 _Py_atomic_load_relaxed(&(ceval_r)->signals_pending) | \
127 _Py_atomic_load_relaxed(&(ceval_i)->pending.calls_to_do) | \
128 (ceval_i)->pending.async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000129
Eric Snow6a150bc2019-06-01 15:39:46 -0600130#define SET_GIL_DROP_REQUEST(ceval_r) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 do { \
Eric Snow6a150bc2019-06-01 15:39:46 -0600132 _Py_atomic_store_relaxed(&(ceval_r)->gil_drop_request, 1); \
133 _Py_atomic_store_relaxed(&(ceval_r)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000135
Eric Snow6a150bc2019-06-01 15:39:46 -0600136#define RESET_GIL_DROP_REQUEST(ceval_r, ceval_i) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 do { \
Eric Snow6a150bc2019-06-01 15:39:46 -0600138 _Py_atomic_store_relaxed(&(ceval_r)->gil_drop_request, 0); \
139 COMPUTE_EVAL_BREAKER(ceval_r, ceval_i); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000141
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000142/* Pending calls are only modified under pending_lock */
Eric Snow6a150bc2019-06-01 15:39:46 -0600143#define SIGNAL_PENDING_CALLS(ceval_r, ceval_i) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 do { \
Eric Snow6a150bc2019-06-01 15:39:46 -0600145 _Py_atomic_store_relaxed(&(ceval_i)->pending.calls_to_do, 1); \
146 _Py_atomic_store_relaxed(&(ceval_r)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000148
Eric Snow6a150bc2019-06-01 15:39:46 -0600149#define UNSIGNAL_PENDING_CALLS(ceval_r, ceval_i) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 do { \
Eric Snow6a150bc2019-06-01 15:39:46 -0600151 _Py_atomic_store_relaxed(&(ceval_i)->pending.calls_to_do, 0); \
152 COMPUTE_EVAL_BREAKER(ceval_r, ceval_i); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000154
Eric Snow6a150bc2019-06-01 15:39:46 -0600155#define SIGNAL_PENDING_SIGNALS(ceval_r) \
Eric Snowfdf282d2019-01-11 14:26:55 -0700156 do { \
Eric Snow6a150bc2019-06-01 15:39:46 -0600157 _Py_atomic_store_relaxed(&(ceval_r)->signals_pending, 1); \
158 _Py_atomic_store_relaxed(&(ceval_r)->eval_breaker, 1); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700159 } while (0)
160
Eric Snow6a150bc2019-06-01 15:39:46 -0600161#define UNSIGNAL_PENDING_SIGNALS(ceval_r, ceval_i) \
Eric Snowfdf282d2019-01-11 14:26:55 -0700162 do { \
Eric Snow6a150bc2019-06-01 15:39:46 -0600163 _Py_atomic_store_relaxed(&(ceval_r)->signals_pending, 0); \
164 COMPUTE_EVAL_BREAKER(ceval_r, ceval_i); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700165 } while (0)
166
Eric Snow6a150bc2019-06-01 15:39:46 -0600167#define SIGNAL_ASYNC_EXC(ceval_r, ceval_i) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 do { \
Eric Snow6a150bc2019-06-01 15:39:46 -0600169 (ceval_i)->pending.async_exc = 1; \
170 _Py_atomic_store_relaxed(&(ceval_r)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000172
Eric Snow6a150bc2019-06-01 15:39:46 -0600173#define UNSIGNAL_ASYNC_EXC(ceval_r, ceval_i) \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600174 do { \
Eric Snow6a150bc2019-06-01 15:39:46 -0600175 (ceval_i)->pending.async_exc = 0; \
176 COMPUTE_EVAL_BREAKER(ceval_r, ceval_i); \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600177 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000178
179
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000180#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000181#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000182#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000183#include "pythread.h"
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000184#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000185
Tim Peters7f468f22004-10-11 02:40:51 +0000186int
187PyEval_ThreadsInitialized(void)
188{
Victor Stinner09532fe2019-05-10 23:39:09 +0200189 return gil_created(&_PyRuntime.ceval.gil);
Tim Peters7f468f22004-10-11 02:40:51 +0000190}
191
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000192void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000193PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000194{
Victor Stinner09532fe2019-05-10 23:39:09 +0200195 _PyRuntimeState *runtime = &_PyRuntime;
Eric Snow6a150bc2019-06-01 15:39:46 -0600196 struct _ceval_runtime_state *ceval_r = &runtime->ceval;
197 struct _gil_runtime_state *gil = &ceval_r->gil;
Victor Stinner09532fe2019-05-10 23:39:09 +0200198 if (gil_created(gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100200 }
201
Inada Naoki001fee12019-02-20 10:00:09 +0900202 PyThread_init_thread();
Victor Stinner09532fe2019-05-10 23:39:09 +0200203 create_gil(gil);
204 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Eric Snow6a150bc2019-06-01 15:39:46 -0600205 take_gil(ceval_r, tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700206
Eric Snow6a150bc2019-06-01 15:39:46 -0600207 // The pending calls mutex is initialized in PyInterpreterState_New().
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000208}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000209
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000210void
Eric Snow6a150bc2019-06-01 15:39:46 -0600211_PyEval_FiniThreads(struct _ceval_runtime_state *ceval_r)
Antoine Pitrou1df15362010-09-13 14:16:46 +0000212{
Eric Snow6a150bc2019-06-01 15:39:46 -0600213 struct _gil_runtime_state *gil = &ceval_r->gil;
Victor Stinner09532fe2019-05-10 23:39:09 +0200214 if (!gil_created(gil)) {
Antoine Pitrou1df15362010-09-13 14:16:46 +0000215 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100216 }
217
Victor Stinner09532fe2019-05-10 23:39:09 +0200218 destroy_gil(gil);
219 assert(!gil_created(gil));
Victor Stinner99fcc612019-04-29 13:04:07 +0200220
Eric Snow6a150bc2019-06-01 15:39:46 -0600221 // The pending calls mutex is freed in PyInterpreterState_Delete().
Antoine Pitrou1df15362010-09-13 14:16:46 +0000222}
223
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400224static inline void
Eric Snow396e0a82019-05-31 21:16:47 -0600225exit_thread_if_finalizing(PyThreadState *tstate)
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400226{
Eric Snow6a150bc2019-06-01 15:39:46 -0600227 PyInterpreterState *interp = tstate->interp;
228 // Stop if thread/interpreter inalization already stated.
229 if (interp == NULL) {
230 return;
231 }
232 _PyRuntimeState *runtime = interp->runtime;
233 if (runtime == NULL) {
234 return;
235 }
236 // Don't exit if the main thread (i.e. of the main interpreter).
Victor Stinner09532fe2019-05-10 23:39:09 +0200237 if (runtime->finalizing != NULL && !_Py_CURRENTLY_FINALIZING(runtime, tstate)) {
Eric Snow6a150bc2019-06-01 15:39:46 -0600238 drop_gil(&runtime->ceval, &interp->ceval, tstate);
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400239 PyThread_exit_thread();
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400240 }
241}
242
Antoine Pitrou1df15362010-09-13 14:16:46 +0000243void
Inada Naoki91234a12019-06-03 21:30:58 +0900244_PyEval_Fini(void)
245{
246#if OPCACHE_STATS
247 fprintf(stderr, "-- Opcode cache number of objects = %zd\n",
248 opcache_code_objects);
249
250 fprintf(stderr, "-- Opcode cache total extra mem = %zd\n",
251 opcache_code_objects_extra_mem);
252
253 fprintf(stderr, "\n");
254
255 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n",
256 opcache_global_hits,
257 (int) (100.0 * opcache_global_hits /
258 (opcache_global_hits + opcache_global_misses)));
259
260 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n",
261 opcache_global_misses,
262 (int) (100.0 * opcache_global_misses /
263 (opcache_global_hits + opcache_global_misses)));
264
265 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n",
266 opcache_global_opts);
267
268 fprintf(stderr, "\n");
269#endif
270}
271
272void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000273PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000274{
Victor Stinner09532fe2019-05-10 23:39:09 +0200275 _PyRuntimeState *runtime = &_PyRuntime;
Eric Snow6a150bc2019-06-01 15:39:46 -0600276 struct _ceval_runtime_state *ceval_r = &runtime->ceval;
Victor Stinner09532fe2019-05-10 23:39:09 +0200277 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
278 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
Victor Stinner09532fe2019-05-10 23:39:09 +0200280 }
Eric Snow6a150bc2019-06-01 15:39:46 -0600281 take_gil(ceval_r, tstate);
Eric Snow396e0a82019-05-31 21:16:47 -0600282 exit_thread_if_finalizing(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000283}
284
285void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000286PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000287{
Victor Stinner09532fe2019-05-10 23:39:09 +0200288 _PyRuntimeState *runtime = &_PyRuntime;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100290 We therefore avoid PyThreadState_Get() which dumps a fatal error
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 in debug mode.
292 */
Eric Snow6a150bc2019-06-01 15:39:46 -0600293 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
294 // Fall back to the main interpreter if there is not active Python
295 // thread. This only affects the eval_breaker.
296 PyInterpreterState *interp = runtime->interpreters.main;
297 if (tstate != NULL) {
298 interp = tstate->interp;
299 if (interp == NULL) {
300 Py_FatalError("PyEval_ReleaseLock: NULL interpreter state");
301 }
302 }
303 drop_gil(&runtime->ceval, &interp->ceval, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000304}
305
306void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000307PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000308{
Victor Stinner09532fe2019-05-10 23:39:09 +0200309 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200311 }
Eric Snow6a150bc2019-06-01 15:39:46 -0600312 PyInterpreterState *interp = tstate->interp;
313 if (interp == NULL) {
314 Py_FatalError("PyEval_AcquireThread: NULL interpreter state");
315 }
316 _PyRuntimeState *runtime = interp->runtime;
317 if (runtime == NULL) {
318 Py_FatalError("PyEval_AcquireThread: NULL runtime state");
319 }
320 struct _ceval_runtime_state *ceval_r = &runtime->ceval;
Victor Stinner09532fe2019-05-10 23:39:09 +0200321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 /* Check someone has called PyEval_InitThreads() to create the lock */
Eric Snow6a150bc2019-06-01 15:39:46 -0600323 assert(gil_created(&ceval_r->gil));
324 take_gil(ceval_r, tstate);
Eric Snow396e0a82019-05-31 21:16:47 -0600325 exit_thread_if_finalizing(tstate);
Victor Stinner09532fe2019-05-10 23:39:09 +0200326 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
327 Py_FatalError("PyEval_AcquireThread: non-NULL old thread state");
328 }
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000329}
330
331void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000332PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000333{
Victor Stinner09532fe2019-05-10 23:39:09 +0200334 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200336 }
Eric Snow6a150bc2019-06-01 15:39:46 -0600337 PyInterpreterState *interp = tstate->interp;
338 if (interp == NULL) {
339 Py_FatalError("PyEval_ReleaseThread: NULL interpreter state");
340 }
341 _PyRuntimeState *runtime = interp->runtime;
342 if (runtime == NULL) {
343 Py_FatalError("PyEval_ReleaseThread: NULL runtime state");
344 }
Victor Stinner09532fe2019-05-10 23:39:09 +0200345
Victor Stinner09532fe2019-05-10 23:39:09 +0200346 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
347 if (new_tstate != tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200349 }
Eric Snow6a150bc2019-06-01 15:39:46 -0600350 drop_gil(&runtime->ceval, &interp->ceval, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000351}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000352
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200353/* This function is called from PyOS_AfterFork_Child to destroy all threads
354 * which are not running in the child process, and clear internal locks
355 * which might be held by those threads.
356 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000357
358void
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200359_PyEval_ReInitThreads(_PyRuntimeState *runtime)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000360{
Eric Snow6a150bc2019-06-01 15:39:46 -0600361 struct _ceval_runtime_state *ceval_r = &runtime->ceval;
362 if (!gil_created(&ceval_r->gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 return;
Victor Stinner09532fe2019-05-10 23:39:09 +0200364 }
Eric Snow6a150bc2019-06-01 15:39:46 -0600365 recreate_gil(&ceval_r->gil);
Victor Stinner09532fe2019-05-10 23:39:09 +0200366 PyThreadState *current_tstate = _PyRuntimeState_GetThreadState(runtime);
Eric Snow6a150bc2019-06-01 15:39:46 -0600367 take_gil(ceval_r, current_tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700368
Eric Snow6a150bc2019-06-01 15:39:46 -0600369 // Only the main interpreter remains, so ignore the rest.
370 PyInterpreterState *interp = _PyRuntime.interpreters.main;
371 struct _ceval_pending_calls *pending = &interp->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200372 pending->lock = PyThread_allocate_lock();
373 if (pending->lock == NULL) {
Eric Snow8479a342019-03-08 23:44:33 -0700374 Py_FatalError("Can't initialize threads for pending calls");
375 }
Jesse Nollera8513972008-07-17 16:49:17 +0000376
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200377 /* Destroy all threads except the current one */
Eric Snow396e0a82019-05-31 21:16:47 -0600378 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000379}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000380
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000381/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600382 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000383
384void
Eric Snow6a150bc2019-06-01 15:39:46 -0600385_PyEval_SignalAsyncExc(struct _ceval_runtime_state *ceval_r,
386 struct _ceval_interpreter_state *ceval_i)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000387{
Eric Snow6a150bc2019-06-01 15:39:46 -0600388 SIGNAL_ASYNC_EXC(ceval_r, ceval_i);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000389}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000390
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000391PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000392PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000393{
Victor Stinner09532fe2019-05-10 23:39:09 +0200394 _PyRuntimeState *runtime = &_PyRuntime;
Eric Snow6a150bc2019-06-01 15:39:46 -0600395 struct _ceval_runtime_state *ceval_r = &runtime->ceval;
Victor Stinner09532fe2019-05-10 23:39:09 +0200396 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
397 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 Py_FatalError("PyEval_SaveThread: NULL tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +0200399 }
Eric Snow6a150bc2019-06-01 15:39:46 -0600400 PyInterpreterState *interp = tstate->interp;
401 if (interp == NULL) {
402 Py_FatalError("PyEval_SaveThread: NULL interpreter state");
403 }
404
405 assert(gil_created(&ceval_r->gil));
406 drop_gil(ceval_r, &interp->ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000408}
409
410void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000411PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000412{
Victor Stinner09532fe2019-05-10 23:39:09 +0200413 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +0200415 }
Eric Snow6a150bc2019-06-01 15:39:46 -0600416 PyInterpreterState *interp = tstate->interp;
417 if (interp == NULL) {
418 Py_FatalError("PyEval_RestoreThread: NULL interpreter state");
419 }
420 _PyRuntimeState *runtime = interp->runtime;
421 if (runtime == NULL) {
422 Py_FatalError("PyEval_RestoreThread: NULL runtime state");
423 }
424 struct _ceval_runtime_state *ceval_r = &runtime->ceval;
Eric Snow396e0a82019-05-31 21:16:47 -0600425
Eric Snow6a150bc2019-06-01 15:39:46 -0600426 assert(gil_created(&ceval_r->gil));
Victor Stinner2914bb32018-01-29 11:57:45 +0100427
428 int err = errno;
Eric Snow6a150bc2019-06-01 15:39:46 -0600429 take_gil(ceval_r, tstate);
Eric Snow396e0a82019-05-31 21:16:47 -0600430 exit_thread_if_finalizing(tstate);
Victor Stinner2914bb32018-01-29 11:57:45 +0100431 errno = err;
432
Victor Stinner09532fe2019-05-10 23:39:09 +0200433 _PyThreadState_Swap(&runtime->gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000434}
435
436
Guido van Rossuma9672091994-09-14 13:31:22 +0000437/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
438 signal handlers or Mac I/O completion routines) can schedule calls
439 to a function to be called synchronously.
440 The synchronous function is called with one void* argument.
441 It should return 0 for success or -1 for failure -- failure should
442 be accompanied by an exception.
443
444 If registry succeeds, the registry function returns 0; if it fails
445 (e.g. due to too many pending calls) it returns -1 (without setting
446 an exception condition).
447
448 Note that because registry may occur from within signal handlers,
449 or other asynchronous events, calling malloc() is unsafe!
450
Guido van Rossuma9672091994-09-14 13:31:22 +0000451 Any thread can schedule pending calls, but only the main thread
452 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000453 There is no facility to schedule calls to a particular thread, but
454 that should be easy to change, should that ever be required. In
455 that case, the static variables here should go into the python
456 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000457*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000458
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200459void
Eric Snow6a150bc2019-06-01 15:39:46 -0600460_PyEval_SignalReceived(struct _ceval_runtime_state *ceval_r)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200461{
462 /* bpo-30703: Function called when the C signal handler of Python gets a
463 signal. We cannot queue a callback using Py_AddPendingCall() since
464 that function is not async-signal-safe. */
Eric Snow6a150bc2019-06-01 15:39:46 -0600465 SIGNAL_PENDING_SIGNALS(ceval_r);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200466}
467
Eric Snow5be45a62019-03-08 22:47:07 -0700468/* Push one item onto the queue while holding the lock. */
469static int
Eric Snow6a150bc2019-06-01 15:39:46 -0600470_push_pending_call(struct _ceval_pending_calls *pending, unsigned long thread_id,
Eric Snow842a2f02019-03-15 15:47:51 -0600471 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700472{
Eric Snow842a2f02019-03-15 15:47:51 -0600473 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700474 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600475 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700476 return -1; /* Queue full */
477 }
Eric Snow6a150bc2019-06-01 15:39:46 -0600478 pending->calls[i].thread_id = thread_id;
Eric Snow842a2f02019-03-15 15:47:51 -0600479 pending->calls[i].func = func;
480 pending->calls[i].arg = arg;
481 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700482 return 0;
483}
484
485/* Pop one item off the queue while holding the lock. */
486static void
Eric Snow6a150bc2019-06-01 15:39:46 -0600487_pop_pending_call(struct _ceval_pending_calls *pending, unsigned long *thread_id,
Eric Snow842a2f02019-03-15 15:47:51 -0600488 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700489{
Eric Snow842a2f02019-03-15 15:47:51 -0600490 int i = pending->first;
491 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700492 return; /* Queue empty */
493 }
494
Eric Snow842a2f02019-03-15 15:47:51 -0600495 *func = pending->calls[i].func;
496 *arg = pending->calls[i].arg;
Eric Snow6a150bc2019-06-01 15:39:46 -0600497 *thread_id = pending->calls[i].thread_id;
Eric Snow842a2f02019-03-15 15:47:51 -0600498 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700499}
500
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200501/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000502 scheduling to be made from any thread, and even from an executing
503 callback.
504 */
505
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000506int
Victor Stinner438a12d2019-05-24 17:01:38 +0200507_PyEval_AddPendingCall(PyThreadState *tstate,
Eric Snow6a150bc2019-06-01 15:39:46 -0600508 struct _ceval_runtime_state *ceval_r,
509 struct _ceval_interpreter_state *ceval_i,
510 unsigned long thread_id,
Victor Stinner09532fe2019-05-10 23:39:09 +0200511 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000512{
Eric Snow6a150bc2019-06-01 15:39:46 -0600513 struct _ceval_pending_calls *pending = &ceval_i->pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600514
515 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
516 if (pending->finishing) {
517 PyThread_release_lock(pending->lock);
518
519 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +0200520 _PyErr_Fetch(tstate, &exc, &val, &tb);
521 _PyErr_SetString(tstate, PyExc_SystemError,
Eric Snow842a2f02019-03-15 15:47:51 -0600522 "Py_AddPendingCall: cannot add pending calls "
523 "(Python shutting down)");
Victor Stinner438a12d2019-05-24 17:01:38 +0200524 _PyErr_Print(tstate);
525 _PyErr_Restore(tstate, exc, val, tb);
Eric Snow842a2f02019-03-15 15:47:51 -0600526 return -1;
527 }
Eric Snow6a150bc2019-06-01 15:39:46 -0600528 int result = _push_pending_call(pending, thread_id, func, arg);
529
530 /* signal loop */
531 SIGNAL_PENDING_CALLS(ceval_r, ceval_i);
Eric Snow842a2f02019-03-15 15:47:51 -0600532 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000535}
536
Eric Snow6a150bc2019-06-01 15:39:46 -0600537/* Py_AddPendingCall() is a simple wrapper for the sake
538 of backward-compatibility. */
Victor Stinner09532fe2019-05-10 23:39:09 +0200539int
540Py_AddPendingCall(int (*func)(void *), void *arg)
541{
Victor Stinner438a12d2019-05-24 17:01:38 +0200542 _PyRuntimeState *runtime = &_PyRuntime;
Eric Snow6a150bc2019-06-01 15:39:46 -0600543 PyInterpreterState *interp = runtime->interpreters.main;
Victor Stinner438a12d2019-05-24 17:01:38 +0200544 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Eric Snow6a150bc2019-06-01 15:39:46 -0600545 return _PyEval_AddPendingCall(tstate,
546 &runtime->ceval, &interp->ceval,
547 runtime->main_thread,
548 func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200549}
550
Eric Snowfdf282d2019-01-11 14:26:55 -0700551static int
Victor Stinner09532fe2019-05-10 23:39:09 +0200552handle_signals(_PyRuntimeState *runtime)
Eric Snowfdf282d2019-01-11 14:26:55 -0700553{
Eric Snow5be45a62019-03-08 22:47:07 -0700554 /* Only handle signals on main thread. PyEval_InitThreads must
555 * have been called already.
556 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200557 if (PyThread_get_thread_ident() != runtime->main_thread) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700558 return 0;
559 }
Eric Snow64d6cc82019-02-23 15:40:43 -0700560 /*
561 * Ensure that the thread isn't currently running some other
562 * interpreter.
563 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200564 PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
565 if (interp != runtime->interpreters.main) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700566 return 0;
567 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700568
Eric Snow6a150bc2019-06-01 15:39:46 -0600569 struct _ceval_runtime_state *ceval_r = &runtime->ceval;
570 struct _ceval_interpreter_state *ceval_i = &interp->ceval;
571 UNSIGNAL_PENDING_SIGNALS(ceval_r, ceval_i);
Eric Snow64d6cc82019-02-23 15:40:43 -0700572 if (_PyErr_CheckSignals() < 0) {
Eric Snow6a150bc2019-06-01 15:39:46 -0600573 SIGNAL_PENDING_SIGNALS(ceval_r); /* We're not done yet */
Eric Snowfdf282d2019-01-11 14:26:55 -0700574 return -1;
575 }
576 return 0;
577}
578
579static int
Eric Snow6a150bc2019-06-01 15:39:46 -0600580make_pending_calls(PyInterpreterState *interp)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000581{
Eric Snow6a150bc2019-06-01 15:39:46 -0600582 if (interp == NULL) {
583 Py_FatalError("make_pending_calls: NULL interpreter state");
Eric Snowb75b1a352019-04-12 10:20:10 -0600584 }
Eric Snow6a150bc2019-06-01 15:39:46 -0600585 _PyRuntimeState *runtime = interp->runtime;
586 if (runtime == NULL) {
587 Py_FatalError("make_pending_calls: NULL runtime state");
588 }
589 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
590 if (tstate == NULL) {
591 Py_FatalError("make_pending_calls: NULL thread state");
592 }
593 if (tstate->interp == NULL || tstate->interp != interp) {
594 Py_FatalError("make_pending_calls: thread state mismatch");
595 }
596 static int busy = 0;
Eric Snowb75b1a352019-04-12 10:20:10 -0600597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 /* don't perform recursive pending calls */
Eric Snowfdf282d2019-01-11 14:26:55 -0700599 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700601 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200602 busy = 1;
Eric Snow6a150bc2019-06-01 15:39:46 -0600603 struct _ceval_runtime_state *ceval_r = &runtime->ceval;
604 struct _ceval_interpreter_state *ceval_i = &interp->ceval;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200605 /* unsignal before starting to call callbacks, so that any callback
606 added in-between re-signals */
Eric Snow6a150bc2019-06-01 15:39:46 -0600607 UNSIGNAL_PENDING_CALLS(ceval_r, ceval_i);
Eric Snowfdf282d2019-01-11 14:26:55 -0700608 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 /* perform a bounded number of calls, in case of recursion */
Eric Snow6a150bc2019-06-01 15:39:46 -0600611 struct _ceval_pending_calls *pending = &ceval_i->pending;
612 unsigned long thread_id = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700613 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700614 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 void *arg = NULL;
616
617 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600618 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Eric Snow6a150bc2019-06-01 15:39:46 -0600619 _pop_pending_call(pending, &thread_id, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600620 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700621
Eric Snow6a150bc2019-06-01 15:39:46 -0600622 if (thread_id && PyThread_get_thread_ident() != thread_id) {
623 // Thread mismatch, so move it to the end of the list
624 // and start over.
625 _PyEval_AddPendingCall(tstate,
626 &runtime->ceval, &interp->ceval,
627 thread_id,
628 func, arg);
629 goto error;
630 }
631
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100632 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700633 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100634 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700635 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700636 res = func(arg);
637 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200638 goto error;
639 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200641
Charles-François Natalif23339a2011-07-23 18:15:43 +0200642 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700643 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200644
645error:
646 busy = 0;
Eric Snow6a150bc2019-06-01 15:39:46 -0600647 SIGNAL_PENDING_CALLS(ceval_r, ceval_i);
Eric Snowfdf282d2019-01-11 14:26:55 -0700648 return res;
649}
650
Eric Snow842a2f02019-03-15 15:47:51 -0600651void
Eric Snow6a150bc2019-06-01 15:39:46 -0600652_PyEval_FinishPendingCalls(PyInterpreterState *interp)
Eric Snow842a2f02019-03-15 15:47:51 -0600653{
Eric Snow842a2f02019-03-15 15:47:51 -0600654 assert(PyGILState_Check());
655
Eric Snow6a150bc2019-06-01 15:39:46 -0600656 struct _ceval_pending_calls *pending = &interp->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200657
Eric Snow842a2f02019-03-15 15:47:51 -0600658 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
659 pending->finishing = 1;
660 PyThread_release_lock(pending->lock);
661
662 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
663 return;
664 }
665
Eric Snow6a150bc2019-06-01 15:39:46 -0600666 if (make_pending_calls(interp) < 0) {
667 _PyRuntimeState *runtime = interp->runtime;
668 if (runtime == NULL) {
669 runtime = &_PyRuntime;
670 }
671 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
672 if (tstate != NULL) {
673 PyObject *exc, *val, *tb;
674 _PyErr_Fetch(tstate, &exc, &val, &tb);
675 PyErr_BadInternalCall();
676 _PyErr_ChainExceptions(exc, val, tb);
677 _PyErr_Print(tstate);
678 }
Eric Snow842a2f02019-03-15 15:47:51 -0600679 }
680}
681
Eric Snowfdf282d2019-01-11 14:26:55 -0700682/* Py_MakePendingCalls() is a simple wrapper for the sake
683 of backward-compatibility. */
684int
685Py_MakePendingCalls(void)
686{
687 assert(PyGILState_Check());
688
689 /* Python signal handler doesn't really queue a callback: it only signals
690 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinner09532fe2019-05-10 23:39:09 +0200691 _PyRuntimeState *runtime = &_PyRuntime;
692 int res = handle_signals(runtime);
Eric Snowfdf282d2019-01-11 14:26:55 -0700693 if (res != 0) {
694 return res;
695 }
696
Eric Snow6a150bc2019-06-01 15:39:46 -0600697 PyInterpreterState *interp = _PyRuntime.interpreters.main;
698 res = make_pending_calls(interp);
Eric Snowb75b1a352019-04-12 10:20:10 -0600699 if (res != 0) {
700 return res;
701 }
702
703 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000704}
705
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000706/* The interpreter's recursion limit */
707
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000708#ifndef Py_DEFAULT_RECURSION_LIMIT
709#define Py_DEFAULT_RECURSION_LIMIT 1000
710#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600711
Eric Snow05351c12017-09-05 21:43:08 -0700712int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000713
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600714void
Eric Snow6a150bc2019-06-01 15:39:46 -0600715_PyEval_Initialize(struct _ceval_runtime_state *ceval_r)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600716{
Eric Snow6a150bc2019-06-01 15:39:46 -0600717 ceval_r->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600718 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Eric Snow6a150bc2019-06-01 15:39:46 -0600719 _gil_initialize(&ceval_r->gil);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600720}
721
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000722int
723Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000724{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600725 return _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000726}
727
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000728void
729Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000730{
Eric Snow6a150bc2019-06-01 15:39:46 -0600731 struct _ceval_runtime_state *ceval_r = &_PyRuntime.ceval;
732 ceval_r->recursion_limit = new_limit;
733 _Py_CheckRecursionLimit = ceval_r->recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000734}
735
Armin Rigo2b3eb402003-10-28 12:05:48 +0000736/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
737 if the recursion_depth reaches _Py_CheckRecursionLimit.
738 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
739 to guarantee that _Py_CheckRecursiveCall() is regularly called.
740 Without USE_STACKCHECK, there is no need for this. */
741int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300742_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000743{
Victor Stinner09532fe2019-05-10 23:39:09 +0200744 _PyRuntimeState *runtime = &_PyRuntime;
745 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
746 int recursion_limit = runtime->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000747
748#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700749 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 if (PyOS_CheckStack()) {
751 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200752 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 return -1;
754 }
pdox18967932017-10-25 23:03:01 -0700755 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700756 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700757#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 if (tstate->recursion_critical)
759 /* Somebody asked that we don't check for recursion. */
760 return 0;
761 if (tstate->overflowed) {
762 if (tstate->recursion_depth > recursion_limit + 50) {
763 /* Overflowing while handling an overflow. Give up. */
764 Py_FatalError("Cannot recover from stack overflow.");
765 }
766 return 0;
767 }
768 if (tstate->recursion_depth > recursion_limit) {
769 --tstate->recursion_depth;
770 tstate->overflowed = 1;
Victor Stinner438a12d2019-05-24 17:01:38 +0200771 _PyErr_Format(tstate, PyExc_RecursionError,
772 "maximum recursion depth exceeded%s",
773 where);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 return -1;
775 }
776 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000777}
778
Victor Stinner09532fe2019-05-10 23:39:09 +0200779static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200780static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000781
Eric Snow6a150bc2019-06-01 15:39:46 -0600782#define _Py_TracingPossible(ceval_r) ((ceval_r)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000783
Guido van Rossum374a9221991-04-04 10:40:29 +0000784
Guido van Rossumb209a111997-04-29 18:18:01 +0000785PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000786PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000787{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 return PyEval_EvalCodeEx(co,
789 globals, locals,
790 (PyObject **)NULL, 0,
791 (PyObject **)NULL, 0,
792 (PyObject **)NULL, 0,
793 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000794}
795
796
797/* Interpreter main loop */
798
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000799PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000800PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 /* This is for backward compatibility with extension modules that
802 used this API; core interpreter code should call
803 PyEval_EvalFrameEx() */
804 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000805}
806
807PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000808PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000809{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200810 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
811 return interp->eval_frame(f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700812}
813
Victor Stinnerc6944e72016-11-11 02:13:35 +0100814PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700815_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
816{
Guido van Rossum950361c1997-01-24 13:49:28 +0000817#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000819#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200820 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300821 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200822 int opcode; /* Current opcode */
823 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200824 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 PyObject *retval = NULL; /* Return value */
Victor Stinner09532fe2019-05-10 23:39:09 +0200826 _PyRuntimeState * const runtime = &_PyRuntime;
827 PyThreadState * const tstate = _PyRuntimeState_GetThreadState(runtime);
Eric Snow6a150bc2019-06-01 15:39:46 -0600828 PyInterpreterState * const interp = tstate->interp;
829 struct _ceval_runtime_state * const ceval_r = &runtime->ceval;
830 struct _ceval_interpreter_state * const ceval_i = &interp->ceval;
831 _Py_atomic_int * const eval_breaker = &ceval_r->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 is true when the line being executed has changed. The
839 initial values are such as to make this false the first
840 time it is tested. */
841 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000842
Serhiy Storchakaab874002016-09-11 13:48:15 +0300843 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 PyObject *names;
845 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +0900846 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +0000847
Brett Cannon368b4b72012-04-02 12:17:59 -0400848#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200849 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400850#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200851
Antoine Pitroub52ec782009-01-25 16:34:23 +0000852/* Computed GOTOs, or
853 the-optimization-commonly-but-improperly-known-as-"threaded code"
854 using gcc's labels-as-values extension
855 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
856
857 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000859 combined with a lookup table of jump addresses. However, since the
860 indirect jump instruction is shared by all opcodes, the CPU will have a
861 hard time making the right prediction for where to jump next (actually,
862 it will be always wrong except in the uncommon case of a sequence of
863 several identical opcodes).
864
865 "Threaded code" in contrast, uses an explicit jump table and an explicit
866 indirect jump instruction at the end of each opcode. Since the jump
867 instruction is at a different address for each opcode, the CPU will make a
868 separate prediction for each of these instructions, which is equivalent to
869 predicting the second opcode of each opcode pair. These predictions have
870 a much better chance to turn out valid, especially in small bytecode loops.
871
872 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000874 and potentially many more instructions (depending on the pipeline width).
875 A correctly predicted branch, however, is nearly free.
876
877 At the time of this writing, the "threaded code" version is up to 15-20%
878 faster than the normal "switch" version, depending on the compiler and the
879 CPU architecture.
880
881 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
882 because it would render the measurements invalid.
883
884
885 NOTE: care must be taken that the compiler doesn't try to "optimize" the
886 indirect jumps by sharing them between all opcodes. Such optimizations
887 can be disabled on gcc by using the -fno-gcse flag (or possibly
888 -fno-crossjumping).
889*/
890
Antoine Pitrou042b1282010-08-13 21:15:58 +0000891#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000892#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000893#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000894#endif
895
Antoine Pitrou042b1282010-08-13 21:15:58 +0000896#ifdef HAVE_COMPUTED_GOTOS
897 #ifndef USE_COMPUTED_GOTOS
898 #define USE_COMPUTED_GOTOS 1
899 #endif
900#else
901 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
902 #error "Computed gotos are not supported on this compiler."
903 #endif
904 #undef USE_COMPUTED_GOTOS
905 #define USE_COMPUTED_GOTOS 0
906#endif
907
908#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000909/* Import the static jump table */
910#include "opcode_targets.h"
911
Antoine Pitroub52ec782009-01-25 16:34:23 +0000912#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700913 op: \
914 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000915
Antoine Pitroub52ec782009-01-25 16:34:23 +0000916#ifdef LLTRACE
917#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 { \
Eric Snow6a150bc2019-06-01 15:39:46 -0600919 if (!lltrace && !_Py_TracingPossible(ceval_r) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300921 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300922 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 } \
924 goto fast_next_opcode; \
925 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000926#else
927#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 { \
Eric Snow6a150bc2019-06-01 15:39:46 -0600929 if (!_Py_TracingPossible(ceval_r) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300931 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300932 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 } \
934 goto fast_next_opcode; \
935 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000936#endif
937
Victor Stinner09532fe2019-05-10 23:39:09 +0200938#define DISPATCH() \
939 { \
940 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
941 FAST_DISPATCH(); \
942 } \
943 continue; \
944 }
945
Antoine Pitroub52ec782009-01-25 16:34:23 +0000946#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700947#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000948#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +0200949#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +0000950#endif
951
952
Neal Norwitza81d2202002-07-14 00:27:26 +0000953/* Tuple access macros */
954
955#ifndef Py_DEBUG
956#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
957#else
958#define GETITEM(v, i) PyTuple_GetItem((v), (i))
959#endif
960
Guido van Rossum374a9221991-04-04 10:40:29 +0000961/* Code access macros */
962
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300963/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600964#define INSTR_OFFSET() \
965 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300966#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300967 _Py_CODEUNIT word = *next_instr; \
968 opcode = _Py_OPCODE(word); \
969 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300970 next_instr++; \
971 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300972#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
973#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000974
Raymond Hettingerf606f872003-03-16 03:11:04 +0000975/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 Some opcodes tend to come in pairs thus making it possible to
977 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300978 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 Verifying the prediction costs a single high-speed test of a register
981 variable against a constant. If the pairing was good, then the
982 processor's own internal branch predication has a high likelihood of
983 success, resulting in a nearly zero-overhead transition to the
984 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300985 including its unpredictable switch-case branch. Combined with the
986 processor's internal branch prediction, a successful PREDICT has the
987 effect of making the two opcodes run as if they were a single new opcode
988 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000989
Georg Brandl86b2fb92008-07-16 03:43:04 +0000990 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 predictions turned-on and interpret the results as if some opcodes
992 had been combined or turn-off predictions so that the opcode frequency
993 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000994
995 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 the CPU to record separate branch prediction information for each
997 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000998
Raymond Hettingerf606f872003-03-16 03:11:04 +0000999*/
1000
Antoine Pitrou042b1282010-08-13 21:15:58 +00001001#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +00001003#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001004#define PREDICT(op) \
1005 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001006 _Py_CODEUNIT word = *next_instr; \
1007 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001008 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001009 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001010 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001011 goto PRED_##op; \
1012 } \
1013 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001014#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001015#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +00001016
Raymond Hettingerf606f872003-03-16 03:11:04 +00001017
Guido van Rossum374a9221991-04-04 10:40:29 +00001018/* Stack manipulation macros */
1019
Martin v. Löwis18e16552006-02-15 17:27:45 +00001020/* The stack can grow at most MAXINT deep, as co_nlocals and
1021 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001022#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1023#define EMPTY() (STACK_LEVEL() == 0)
1024#define TOP() (stack_pointer[-1])
1025#define SECOND() (stack_pointer[-2])
1026#define THIRD() (stack_pointer[-3])
1027#define FOURTH() (stack_pointer[-4])
1028#define PEEK(n) (stack_pointer[-(n)])
1029#define SET_TOP(v) (stack_pointer[-1] = (v))
1030#define SET_SECOND(v) (stack_pointer[-2] = (v))
1031#define SET_THIRD(v) (stack_pointer[-3] = (v))
1032#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1033#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1034#define BASIC_STACKADJ(n) (stack_pointer += n)
1035#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1036#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001037
Guido van Rossum96a42c81992-01-12 02:29:51 +00001038#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001040 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001041 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +02001042#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001043 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +00001044#define STACK_GROW(n) do { \
1045 assert(n >= 0); \
1046 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001047 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001048 assert(STACK_LEVEL() <= co->co_stacksize); \
1049 } while (0)
1050#define STACK_SHRINK(n) do { \
1051 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001052 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001053 (void)(BASIC_STACKADJ(-n)); \
1054 assert(STACK_LEVEL() <= co->co_stacksize); \
1055 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001056#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001057 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001058 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001059#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001060#define PUSH(v) BASIC_PUSH(v)
1061#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001062#define STACK_GROW(n) BASIC_STACKADJ(n)
1063#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001064#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001065#endif
1066
Guido van Rossum681d79a1995-07-18 14:51:37 +00001067/* Local variable macros */
1068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001070
1071/* The SETLOCAL() macro must not DECREF the local variable in-place and
1072 then store the new value; it must copy the old value to a temporary
1073 value, then store the new value, and then DECREF the temporary value.
1074 This is because it is possible that during the DECREF the frame is
1075 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1076 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001078 GETLOCAL(i) = value; \
1079 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001080
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001081
1082#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 while (STACK_LEVEL() > (b)->b_level) { \
1084 PyObject *v = POP(); \
1085 Py_XDECREF(v); \
1086 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001087
1088#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001089 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001091 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1093 while (STACK_LEVEL() > (b)->b_level + 3) { \
1094 value = POP(); \
1095 Py_XDECREF(value); \
1096 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001097 exc_info = tstate->exc_info; \
1098 type = exc_info->exc_type; \
1099 value = exc_info->exc_value; \
1100 traceback = exc_info->exc_traceback; \
1101 exc_info->exc_type = POP(); \
1102 exc_info->exc_value = POP(); \
1103 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 Py_XDECREF(type); \
1105 Py_XDECREF(value); \
1106 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001107 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001108
Inada Naoki91234a12019-06-03 21:30:58 +09001109 /* macros for opcode cache */
1110#define OPCACHE_CHECK() \
1111 do { \
1112 co_opcache = NULL; \
1113 if (co->co_opcache != NULL) { \
1114 unsigned char co_opt_offset = \
1115 co->co_opcache_map[next_instr - first_instr]; \
1116 if (co_opt_offset > 0) { \
1117 assert(co_opt_offset <= co->co_opcache_size); \
1118 co_opcache = &co->co_opcache[co_opt_offset - 1]; \
1119 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001120 } \
1121 } \
1122 } while (0)
1123
1124#if OPCACHE_STATS
1125
1126#define OPCACHE_STAT_GLOBAL_HIT() \
1127 do { \
1128 if (co->co_opcache != NULL) opcache_global_hits++; \
1129 } while (0)
1130
1131#define OPCACHE_STAT_GLOBAL_MISS() \
1132 do { \
1133 if (co->co_opcache != NULL) opcache_global_misses++; \
1134 } while (0)
1135
1136#define OPCACHE_STAT_GLOBAL_OPT() \
1137 do { \
1138 if (co->co_opcache != NULL) opcache_global_opts++; \
1139 } while (0)
1140
1141#else /* OPCACHE_STATS */
1142
1143#define OPCACHE_STAT_GLOBAL_HIT()
1144#define OPCACHE_STAT_GLOBAL_MISS()
1145#define OPCACHE_STAT_GLOBAL_OPT()
1146
1147#endif
1148
Guido van Rossuma027efa1997-05-05 20:56:21 +00001149/* Start of code */
1150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 /* push frame */
1152 if (Py_EnterRecursiveCall(""))
1153 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 if (tstate->use_tracing) {
1158 if (tstate->c_tracefunc != NULL) {
1159 /* tstate->c_tracefunc, if defined, is a
1160 function that will be called on *every* entry
1161 to a code block. Its return value, if not
1162 None, is a function that will be called at
1163 the start of each executed line of code.
1164 (Actually, the function must return itself
1165 in order to continue tracing.) The trace
1166 functions are called with three arguments:
1167 a pointer to the current frame, a string
1168 indicating why the function is called, and
1169 an argument which depends on the situation.
1170 The global trace function is also called
1171 whenever an exception is detected. */
1172 if (call_trace_protected(tstate->c_tracefunc,
1173 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001174 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 /* Trace function raised an error */
1176 goto exit_eval_frame;
1177 }
1178 }
1179 if (tstate->c_profilefunc != NULL) {
1180 /* Similar for c_profilefunc, except it needn't
1181 return itself and isn't called for "line" events */
1182 if (call_trace_protected(tstate->c_profilefunc,
1183 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001184 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 /* Profile function raised an error */
1186 goto exit_eval_frame;
1187 }
1188 }
1189 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001190
Łukasz Langaa785c872016-09-09 17:37:37 -07001191 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1192 dtrace_function_entry(f);
1193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 co = f->f_code;
1195 names = co->co_names;
1196 consts = co->co_consts;
1197 fastlocals = f->f_localsplus;
1198 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001199 assert(PyBytes_Check(co->co_code));
1200 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001201 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1202 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1203 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001204 /*
1205 f->f_lasti refers to the index of the last instruction,
1206 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001207
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001208 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001209 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 When the PREDICT() macros are enabled, some opcode pairs follow in
1212 direct succession without updating f->f_lasti. A successful
1213 prediction effectively links the two codes together as if they
1214 were a single new opcode; accordingly,f->f_lasti will point to
1215 the first code in the pair (for instance, GET_ITER followed by
1216 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001217 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001219 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001220 next_instr = first_instr;
1221 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001222 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1223 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001224 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 stack_pointer = f->f_stacktop;
1226 assert(stack_pointer != NULL);
1227 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001228 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001229
Inada Naoki91234a12019-06-03 21:30:58 +09001230 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1231 co->co_opcache_flag++;
1232 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1233 if (_PyCode_InitOpcache(co) < 0) {
1234 return NULL;
1235 }
1236#if OPCACHE_STATS
1237 opcache_code_objects_extra_mem +=
1238 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1239 sizeof(_PyOpcache) * co->co_opcache_size;
1240 opcache_code_objects++;
1241#endif
1242 }
1243 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001244
Tim Peters5ca576e2001-06-18 22:08:13 +00001245#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001246 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001247#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001248
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001249 if (throwflag) /* support for generator.throw() */
1250 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001251
Victor Stinnerace47d72013-07-18 01:41:08 +02001252#ifdef Py_DEBUG
1253 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001254 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001255 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001256 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001257#endif
1258
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001259main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1262 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001263 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 /* Do periodic things. Doing this every time through
1266 the loop would add too much overhead, so we do it
1267 only every Nth instruction. We also do it if
1268 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1269 event needs attention (e.g. a signal handler or
1270 async I/O handler); see Py_AddPendingCall() and
1271 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001272
Eric Snow7bda9de2019-03-08 17:25:54 -07001273 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001274 opcode = _Py_OPCODE(*next_instr);
1275 if (opcode == SETUP_FINALLY ||
1276 opcode == SETUP_WITH ||
1277 opcode == BEFORE_ASYNC_WITH ||
1278 opcode == YIELD_FROM) {
1279 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001280 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001281 - If we're about to enter the 'with:'. It will prevent
1282 emitting a resource warning in the common idiom
1283 'with open(path) as file:'.
1284 - If we're about to enter the 'async with:'.
1285 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001286 *very* useful, but might help in some cases and it's
1287 traditional)
1288 - If we're resuming a chain of nested 'yield from' or
1289 'await' calls, then each frame is parked with YIELD_FROM
1290 as its next opcode. If the user hit control-C we want to
1291 wait until we've reached the innermost frame before
1292 running the signal handler and raising KeyboardInterrupt
1293 (see bpo-30039).
1294 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 goto fast_next_opcode;
1296 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001297
Eric Snow6a150bc2019-06-01 15:39:46 -06001298 if (_Py_atomic_load_relaxed(&ceval_r->signals_pending)) {
Victor Stinner09532fe2019-05-10 23:39:09 +02001299 if (handle_signals(runtime) != 0) {
Eric Snowfdf282d2019-01-11 14:26:55 -07001300 goto error;
1301 }
1302 }
Eric Snow6a150bc2019-06-01 15:39:46 -06001303 if (_Py_atomic_load_relaxed(&ceval_i->pending.calls_to_do)) {
1304 if (make_pending_calls(interp) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001305 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001306 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001308
Eric Snow6a150bc2019-06-01 15:39:46 -06001309 if (_Py_atomic_load_relaxed(&ceval_r->gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 /* Give another thread a chance */
Victor Stinner09532fe2019-05-10 23:39:09 +02001311 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 Py_FatalError("ceval: tstate mix-up");
Victor Stinner09532fe2019-05-10 23:39:09 +02001313 }
Eric Snow6a150bc2019-06-01 15:39:46 -06001314 drop_gil(ceval_r, ceval_i, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315
1316 /* Other threads may run now */
1317
Eric Snow6a150bc2019-06-01 15:39:46 -06001318 take_gil(ceval_r, tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001319
1320 /* Check if we should make a quick exit. */
Eric Snow396e0a82019-05-31 21:16:47 -06001321 exit_thread_if_finalizing(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001322
Victor Stinner09532fe2019-05-10 23:39:09 +02001323 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 Py_FatalError("ceval: orphan tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +02001325 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 }
1327 /* Check for asynchronous exceptions. */
1328 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001329 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 tstate->async_exc = NULL;
Eric Snow6a150bc2019-06-01 15:39:46 -06001331 UNSIGNAL_ASYNC_EXC(ceval_r, ceval_i);
Victor Stinner438a12d2019-05-24 17:01:38 +02001332 _PyErr_SetNone(tstate, exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001333 Py_DECREF(exc);
1334 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 }
1336 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 fast_next_opcode:
1339 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001340
Łukasz Langaa785c872016-09-09 17:37:37 -07001341 if (PyDTrace_LINE_ENABLED())
1342 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001345
Eric Snow6a150bc2019-06-01 15:39:46 -06001346 if (_Py_TracingPossible(ceval_r) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001347 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001348 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 /* see maybe_call_line_trace
1350 for expository comments */
1351 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 err = maybe_call_line_trace(tstate->c_tracefunc,
1354 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001355 tstate, f,
1356 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 /* Reload possibly changed frame fields */
1358 JUMPTO(f->f_lasti);
1359 if (f->f_stacktop != NULL) {
1360 stack_pointer = f->f_stacktop;
1361 f->f_stacktop = NULL;
1362 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001363 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001365 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001369
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001370 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001371 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001372#ifdef DYNAMIC_EXECUTION_PROFILE
1373#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 dxpairs[lastopcode][opcode]++;
1375 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001376#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001378#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001379
Guido van Rossum96a42c81992-01-12 02:29:51 +00001380#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 if (lltrace) {
1384 if (HAS_ARG(opcode)) {
1385 printf("%d: %d, %d\n",
1386 f->f_lasti, opcode, oparg);
1387 }
1388 else {
1389 printf("%d: %d\n",
1390 f->f_lasti, opcode);
1391 }
1392 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001393#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001398 It is essential that any operation that fails must goto error
1399 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001400
Benjamin Petersonddd19492018-09-16 22:38:02 -07001401 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001403 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001404
Benjamin Petersonddd19492018-09-16 22:38:02 -07001405 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001406 PyObject *value = GETLOCAL(oparg);
1407 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001408 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001409 UNBOUNDLOCAL_ERROR_MSG,
1410 PyTuple_GetItem(co->co_varnames, oparg));
1411 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001413 Py_INCREF(value);
1414 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001416 }
1417
Benjamin Petersonddd19492018-09-16 22:38:02 -07001418 case TARGET(LOAD_CONST): {
1419 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001420 PyObject *value = GETITEM(consts, oparg);
1421 Py_INCREF(value);
1422 PUSH(value);
1423 FAST_DISPATCH();
1424 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001425
Benjamin Petersonddd19492018-09-16 22:38:02 -07001426 case TARGET(STORE_FAST): {
1427 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001428 PyObject *value = POP();
1429 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001431 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001432
Benjamin Petersonddd19492018-09-16 22:38:02 -07001433 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001434 PyObject *value = POP();
1435 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001437 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001438
Benjamin Petersonddd19492018-09-16 22:38:02 -07001439 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001440 PyObject *top = TOP();
1441 PyObject *second = SECOND();
1442 SET_TOP(second);
1443 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001445 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001446
Benjamin Petersonddd19492018-09-16 22:38:02 -07001447 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001448 PyObject *top = TOP();
1449 PyObject *second = SECOND();
1450 PyObject *third = THIRD();
1451 SET_TOP(second);
1452 SET_SECOND(third);
1453 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001455 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001456
Benjamin Petersonddd19492018-09-16 22:38:02 -07001457 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001458 PyObject *top = TOP();
1459 PyObject *second = SECOND();
1460 PyObject *third = THIRD();
1461 PyObject *fourth = FOURTH();
1462 SET_TOP(second);
1463 SET_SECOND(third);
1464 SET_THIRD(fourth);
1465 SET_FOURTH(top);
1466 FAST_DISPATCH();
1467 }
1468
Benjamin Petersonddd19492018-09-16 22:38:02 -07001469 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001470 PyObject *top = TOP();
1471 Py_INCREF(top);
1472 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001474 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001475
Benjamin Petersonddd19492018-09-16 22:38:02 -07001476 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001477 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001478 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001479 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001480 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001481 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001482 SET_TOP(top);
1483 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001484 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001485 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001486
Benjamin Petersonddd19492018-09-16 22:38:02 -07001487 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001488 PyObject *value = TOP();
1489 PyObject *res = PyNumber_Positive(value);
1490 Py_DECREF(value);
1491 SET_TOP(res);
1492 if (res == NULL)
1493 goto error;
1494 DISPATCH();
1495 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001496
Benjamin Petersonddd19492018-09-16 22:38:02 -07001497 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001498 PyObject *value = TOP();
1499 PyObject *res = PyNumber_Negative(value);
1500 Py_DECREF(value);
1501 SET_TOP(res);
1502 if (res == NULL)
1503 goto error;
1504 DISPATCH();
1505 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001506
Benjamin Petersonddd19492018-09-16 22:38:02 -07001507 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001508 PyObject *value = TOP();
1509 int err = PyObject_IsTrue(value);
1510 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 if (err == 0) {
1512 Py_INCREF(Py_True);
1513 SET_TOP(Py_True);
1514 DISPATCH();
1515 }
1516 else if (err > 0) {
1517 Py_INCREF(Py_False);
1518 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 DISPATCH();
1520 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001521 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001522 goto error;
1523 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001524
Benjamin Petersonddd19492018-09-16 22:38:02 -07001525 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001526 PyObject *value = TOP();
1527 PyObject *res = PyNumber_Invert(value);
1528 Py_DECREF(value);
1529 SET_TOP(res);
1530 if (res == NULL)
1531 goto error;
1532 DISPATCH();
1533 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001534
Benjamin Petersonddd19492018-09-16 22:38:02 -07001535 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001536 PyObject *exp = POP();
1537 PyObject *base = TOP();
1538 PyObject *res = PyNumber_Power(base, exp, Py_None);
1539 Py_DECREF(base);
1540 Py_DECREF(exp);
1541 SET_TOP(res);
1542 if (res == NULL)
1543 goto error;
1544 DISPATCH();
1545 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001546
Benjamin Petersonddd19492018-09-16 22:38:02 -07001547 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001548 PyObject *right = POP();
1549 PyObject *left = TOP();
1550 PyObject *res = PyNumber_Multiply(left, right);
1551 Py_DECREF(left);
1552 Py_DECREF(right);
1553 SET_TOP(res);
1554 if (res == NULL)
1555 goto error;
1556 DISPATCH();
1557 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001558
Benjamin Petersonddd19492018-09-16 22:38:02 -07001559 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001560 PyObject *right = POP();
1561 PyObject *left = TOP();
1562 PyObject *res = PyNumber_MatrixMultiply(left, right);
1563 Py_DECREF(left);
1564 Py_DECREF(right);
1565 SET_TOP(res);
1566 if (res == NULL)
1567 goto error;
1568 DISPATCH();
1569 }
1570
Benjamin Petersonddd19492018-09-16 22:38:02 -07001571 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001572 PyObject *divisor = POP();
1573 PyObject *dividend = TOP();
1574 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1575 Py_DECREF(dividend);
1576 Py_DECREF(divisor);
1577 SET_TOP(quotient);
1578 if (quotient == NULL)
1579 goto error;
1580 DISPATCH();
1581 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001582
Benjamin Petersonddd19492018-09-16 22:38:02 -07001583 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001584 PyObject *divisor = POP();
1585 PyObject *dividend = TOP();
1586 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1587 Py_DECREF(dividend);
1588 Py_DECREF(divisor);
1589 SET_TOP(quotient);
1590 if (quotient == NULL)
1591 goto error;
1592 DISPATCH();
1593 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001594
Benjamin Petersonddd19492018-09-16 22:38:02 -07001595 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001596 PyObject *divisor = POP();
1597 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001598 PyObject *res;
1599 if (PyUnicode_CheckExact(dividend) && (
1600 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1601 // fast path; string formatting, but not if the RHS is a str subclass
1602 // (see issue28598)
1603 res = PyUnicode_Format(dividend, divisor);
1604 } else {
1605 res = PyNumber_Remainder(dividend, divisor);
1606 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001607 Py_DECREF(divisor);
1608 Py_DECREF(dividend);
1609 SET_TOP(res);
1610 if (res == NULL)
1611 goto error;
1612 DISPATCH();
1613 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001614
Benjamin Petersonddd19492018-09-16 22:38:02 -07001615 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001616 PyObject *right = POP();
1617 PyObject *left = TOP();
1618 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001619 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1620 CPython using bytecode, it is simply worthless.
1621 See http://bugs.python.org/issue21955 and
1622 http://bugs.python.org/issue10044 for the discussion. In short,
1623 no patch shown any impact on a realistic benchmark, only a minor
1624 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001625 if (PyUnicode_CheckExact(left) &&
1626 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001627 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001628 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001629 }
1630 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001631 sum = PyNumber_Add(left, right);
1632 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001633 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001634 Py_DECREF(right);
1635 SET_TOP(sum);
1636 if (sum == NULL)
1637 goto error;
1638 DISPATCH();
1639 }
1640
Benjamin Petersonddd19492018-09-16 22:38:02 -07001641 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001642 PyObject *right = POP();
1643 PyObject *left = TOP();
1644 PyObject *diff = PyNumber_Subtract(left, right);
1645 Py_DECREF(right);
1646 Py_DECREF(left);
1647 SET_TOP(diff);
1648 if (diff == NULL)
1649 goto error;
1650 DISPATCH();
1651 }
1652
Benjamin Petersonddd19492018-09-16 22:38:02 -07001653 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001654 PyObject *sub = POP();
1655 PyObject *container = TOP();
1656 PyObject *res = PyObject_GetItem(container, sub);
1657 Py_DECREF(container);
1658 Py_DECREF(sub);
1659 SET_TOP(res);
1660 if (res == NULL)
1661 goto error;
1662 DISPATCH();
1663 }
1664
Benjamin Petersonddd19492018-09-16 22:38:02 -07001665 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001666 PyObject *right = POP();
1667 PyObject *left = TOP();
1668 PyObject *res = PyNumber_Lshift(left, right);
1669 Py_DECREF(left);
1670 Py_DECREF(right);
1671 SET_TOP(res);
1672 if (res == NULL)
1673 goto error;
1674 DISPATCH();
1675 }
1676
Benjamin Petersonddd19492018-09-16 22:38:02 -07001677 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001678 PyObject *right = POP();
1679 PyObject *left = TOP();
1680 PyObject *res = PyNumber_Rshift(left, right);
1681 Py_DECREF(left);
1682 Py_DECREF(right);
1683 SET_TOP(res);
1684 if (res == NULL)
1685 goto error;
1686 DISPATCH();
1687 }
1688
Benjamin Petersonddd19492018-09-16 22:38:02 -07001689 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001690 PyObject *right = POP();
1691 PyObject *left = TOP();
1692 PyObject *res = PyNumber_And(left, right);
1693 Py_DECREF(left);
1694 Py_DECREF(right);
1695 SET_TOP(res);
1696 if (res == NULL)
1697 goto error;
1698 DISPATCH();
1699 }
1700
Benjamin Petersonddd19492018-09-16 22:38:02 -07001701 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001702 PyObject *right = POP();
1703 PyObject *left = TOP();
1704 PyObject *res = PyNumber_Xor(left, right);
1705 Py_DECREF(left);
1706 Py_DECREF(right);
1707 SET_TOP(res);
1708 if (res == NULL)
1709 goto error;
1710 DISPATCH();
1711 }
1712
Benjamin Petersonddd19492018-09-16 22:38:02 -07001713 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001714 PyObject *right = POP();
1715 PyObject *left = TOP();
1716 PyObject *res = PyNumber_Or(left, right);
1717 Py_DECREF(left);
1718 Py_DECREF(right);
1719 SET_TOP(res);
1720 if (res == NULL)
1721 goto error;
1722 DISPATCH();
1723 }
1724
Benjamin Petersonddd19492018-09-16 22:38:02 -07001725 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001726 PyObject *v = POP();
1727 PyObject *list = PEEK(oparg);
1728 int err;
1729 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001731 if (err != 0)
1732 goto error;
1733 PREDICT(JUMP_ABSOLUTE);
1734 DISPATCH();
1735 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001736
Benjamin Petersonddd19492018-09-16 22:38:02 -07001737 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001738 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001739 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001740 int err;
1741 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001743 if (err != 0)
1744 goto error;
1745 PREDICT(JUMP_ABSOLUTE);
1746 DISPATCH();
1747 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001748
Benjamin Petersonddd19492018-09-16 22:38:02 -07001749 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001750 PyObject *exp = POP();
1751 PyObject *base = TOP();
1752 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1753 Py_DECREF(base);
1754 Py_DECREF(exp);
1755 SET_TOP(res);
1756 if (res == NULL)
1757 goto error;
1758 DISPATCH();
1759 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001760
Benjamin Petersonddd19492018-09-16 22:38:02 -07001761 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001762 PyObject *right = POP();
1763 PyObject *left = TOP();
1764 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1765 Py_DECREF(left);
1766 Py_DECREF(right);
1767 SET_TOP(res);
1768 if (res == NULL)
1769 goto error;
1770 DISPATCH();
1771 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001772
Benjamin Petersonddd19492018-09-16 22:38:02 -07001773 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001774 PyObject *right = POP();
1775 PyObject *left = TOP();
1776 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1777 Py_DECREF(left);
1778 Py_DECREF(right);
1779 SET_TOP(res);
1780 if (res == NULL)
1781 goto error;
1782 DISPATCH();
1783 }
1784
Benjamin Petersonddd19492018-09-16 22:38:02 -07001785 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001786 PyObject *divisor = POP();
1787 PyObject *dividend = TOP();
1788 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1789 Py_DECREF(dividend);
1790 Py_DECREF(divisor);
1791 SET_TOP(quotient);
1792 if (quotient == NULL)
1793 goto error;
1794 DISPATCH();
1795 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001796
Benjamin Petersonddd19492018-09-16 22:38:02 -07001797 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001798 PyObject *divisor = POP();
1799 PyObject *dividend = TOP();
1800 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1801 Py_DECREF(dividend);
1802 Py_DECREF(divisor);
1803 SET_TOP(quotient);
1804 if (quotient == NULL)
1805 goto error;
1806 DISPATCH();
1807 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001808
Benjamin Petersonddd19492018-09-16 22:38:02 -07001809 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001810 PyObject *right = POP();
1811 PyObject *left = TOP();
1812 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1813 Py_DECREF(left);
1814 Py_DECREF(right);
1815 SET_TOP(mod);
1816 if (mod == NULL)
1817 goto error;
1818 DISPATCH();
1819 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001820
Benjamin Petersonddd19492018-09-16 22:38:02 -07001821 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001822 PyObject *right = POP();
1823 PyObject *left = TOP();
1824 PyObject *sum;
1825 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001826 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001827 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001828 }
1829 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001830 sum = PyNumber_InPlaceAdd(left, right);
1831 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001832 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001833 Py_DECREF(right);
1834 SET_TOP(sum);
1835 if (sum == NULL)
1836 goto error;
1837 DISPATCH();
1838 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001839
Benjamin Petersonddd19492018-09-16 22:38:02 -07001840 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001841 PyObject *right = POP();
1842 PyObject *left = TOP();
1843 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1844 Py_DECREF(left);
1845 Py_DECREF(right);
1846 SET_TOP(diff);
1847 if (diff == NULL)
1848 goto error;
1849 DISPATCH();
1850 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001851
Benjamin Petersonddd19492018-09-16 22:38:02 -07001852 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001853 PyObject *right = POP();
1854 PyObject *left = TOP();
1855 PyObject *res = PyNumber_InPlaceLshift(left, right);
1856 Py_DECREF(left);
1857 Py_DECREF(right);
1858 SET_TOP(res);
1859 if (res == NULL)
1860 goto error;
1861 DISPATCH();
1862 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001863
Benjamin Petersonddd19492018-09-16 22:38:02 -07001864 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001865 PyObject *right = POP();
1866 PyObject *left = TOP();
1867 PyObject *res = PyNumber_InPlaceRshift(left, right);
1868 Py_DECREF(left);
1869 Py_DECREF(right);
1870 SET_TOP(res);
1871 if (res == NULL)
1872 goto error;
1873 DISPATCH();
1874 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001875
Benjamin Petersonddd19492018-09-16 22:38:02 -07001876 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001877 PyObject *right = POP();
1878 PyObject *left = TOP();
1879 PyObject *res = PyNumber_InPlaceAnd(left, right);
1880 Py_DECREF(left);
1881 Py_DECREF(right);
1882 SET_TOP(res);
1883 if (res == NULL)
1884 goto error;
1885 DISPATCH();
1886 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001887
Benjamin Petersonddd19492018-09-16 22:38:02 -07001888 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001889 PyObject *right = POP();
1890 PyObject *left = TOP();
1891 PyObject *res = PyNumber_InPlaceXor(left, right);
1892 Py_DECREF(left);
1893 Py_DECREF(right);
1894 SET_TOP(res);
1895 if (res == NULL)
1896 goto error;
1897 DISPATCH();
1898 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001899
Benjamin Petersonddd19492018-09-16 22:38:02 -07001900 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001901 PyObject *right = POP();
1902 PyObject *left = TOP();
1903 PyObject *res = PyNumber_InPlaceOr(left, right);
1904 Py_DECREF(left);
1905 Py_DECREF(right);
1906 SET_TOP(res);
1907 if (res == NULL)
1908 goto error;
1909 DISPATCH();
1910 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001911
Benjamin Petersonddd19492018-09-16 22:38:02 -07001912 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001913 PyObject *sub = TOP();
1914 PyObject *container = SECOND();
1915 PyObject *v = THIRD();
1916 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001917 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001918 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001919 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001921 Py_DECREF(container);
1922 Py_DECREF(sub);
1923 if (err != 0)
1924 goto error;
1925 DISPATCH();
1926 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001927
Benjamin Petersonddd19492018-09-16 22:38:02 -07001928 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001929 PyObject *sub = TOP();
1930 PyObject *container = SECOND();
1931 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001932 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001933 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001934 err = PyObject_DelItem(container, sub);
1935 Py_DECREF(container);
1936 Py_DECREF(sub);
1937 if (err != 0)
1938 goto error;
1939 DISPATCH();
1940 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001941
Benjamin Petersonddd19492018-09-16 22:38:02 -07001942 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001943 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001944 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001945 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001946 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001947 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001948 _PyErr_SetString(tstate, PyExc_RuntimeError,
1949 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001950 Py_DECREF(value);
1951 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001953 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001954 Py_DECREF(value);
1955 if (res == NULL)
1956 goto error;
1957 Py_DECREF(res);
1958 DISPATCH();
1959 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001960
Benjamin Petersonddd19492018-09-16 22:38:02 -07001961 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001962 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 switch (oparg) {
1964 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001965 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001966 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001968 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001969 /* fall through */
1970 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02001971 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001972 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001973 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 break;
1975 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02001976 _PyErr_SetString(tstate, PyExc_SystemError,
1977 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001978 break;
1979 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001980 goto error;
1981 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001982
Benjamin Petersonddd19492018-09-16 22:38:02 -07001983 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001985 assert(f->f_iblock == 0);
Pablo Galindof00828a2019-05-09 16:52:02 +01001986 goto exit_returning;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001987 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001988
Benjamin Petersonddd19492018-09-16 22:38:02 -07001989 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001990 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001991 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001992 PyObject *obj = TOP();
1993 PyTypeObject *type = Py_TYPE(obj);
1994
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001995 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001996 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001997 }
Yury Selivanov75445082015-05-11 22:57:16 -04001998
1999 if (getter != NULL) {
2000 iter = (*getter)(obj);
2001 Py_DECREF(obj);
2002 if (iter == NULL) {
2003 SET_TOP(NULL);
2004 goto error;
2005 }
2006 }
2007 else {
2008 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002009 _PyErr_Format(tstate, PyExc_TypeError,
2010 "'async for' requires an object with "
2011 "__aiter__ method, got %.100s",
2012 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002013 Py_DECREF(obj);
2014 goto error;
2015 }
2016
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002017 if (Py_TYPE(iter)->tp_as_async == NULL ||
2018 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002019
Yury Selivanov398ff912017-03-02 22:20:00 -05002020 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002021 _PyErr_Format(tstate, PyExc_TypeError,
2022 "'async for' received an object from __aiter__ "
2023 "that does not implement __anext__: %.100s",
2024 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002025 Py_DECREF(iter);
2026 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002027 }
2028
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002029 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002030 DISPATCH();
2031 }
2032
Benjamin Petersonddd19492018-09-16 22:38:02 -07002033 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002034 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002035 PyObject *next_iter = NULL;
2036 PyObject *awaitable = NULL;
2037 PyObject *aiter = TOP();
2038 PyTypeObject *type = Py_TYPE(aiter);
2039
Yury Selivanoveb636452016-09-08 22:01:51 -07002040 if (PyAsyncGen_CheckExact(aiter)) {
2041 awaitable = type->tp_as_async->am_anext(aiter);
2042 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002043 goto error;
2044 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002045 } else {
2046 if (type->tp_as_async != NULL){
2047 getter = type->tp_as_async->am_anext;
2048 }
Yury Selivanov75445082015-05-11 22:57:16 -04002049
Yury Selivanoveb636452016-09-08 22:01:51 -07002050 if (getter != NULL) {
2051 next_iter = (*getter)(aiter);
2052 if (next_iter == NULL) {
2053 goto error;
2054 }
2055 }
2056 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002057 _PyErr_Format(tstate, PyExc_TypeError,
2058 "'async for' requires an iterator with "
2059 "__anext__ method, got %.100s",
2060 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002061 goto error;
2062 }
Yury Selivanov75445082015-05-11 22:57:16 -04002063
Yury Selivanoveb636452016-09-08 22:01:51 -07002064 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2065 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002066 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002067 PyExc_TypeError,
2068 "'async for' received an invalid object "
2069 "from __anext__: %.100s",
2070 Py_TYPE(next_iter)->tp_name);
2071
2072 Py_DECREF(next_iter);
2073 goto error;
2074 } else {
2075 Py_DECREF(next_iter);
2076 }
2077 }
Yury Selivanov75445082015-05-11 22:57:16 -04002078
2079 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002080 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002081 DISPATCH();
2082 }
2083
Benjamin Petersonddd19492018-09-16 22:38:02 -07002084 case TARGET(GET_AWAITABLE): {
2085 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002086 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002087 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002088
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002089 if (iter == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002090 format_awaitable_error(tstate, Py_TYPE(iterable),
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002091 _Py_OPCODE(next_instr[-2]));
2092 }
2093
Yury Selivanov75445082015-05-11 22:57:16 -04002094 Py_DECREF(iterable);
2095
Yury Selivanovc724bae2016-03-02 11:30:46 -05002096 if (iter != NULL && PyCoro_CheckExact(iter)) {
2097 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2098 if (yf != NULL) {
2099 /* `iter` is a coroutine object that is being
2100 awaited, `yf` is a pointer to the current awaitable
2101 being awaited on. */
2102 Py_DECREF(yf);
2103 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002104 _PyErr_SetString(tstate, PyExc_RuntimeError,
2105 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002106 /* The code below jumps to `error` if `iter` is NULL. */
2107 }
2108 }
2109
Yury Selivanov75445082015-05-11 22:57:16 -04002110 SET_TOP(iter); /* Even if it's NULL */
2111
2112 if (iter == NULL) {
2113 goto error;
2114 }
2115
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002116 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002117 DISPATCH();
2118 }
2119
Benjamin Petersonddd19492018-09-16 22:38:02 -07002120 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002121 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002122 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002123 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002124 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2125 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002126 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002127 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002128 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002129 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002130 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002131 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002132 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002133 Py_DECREF(v);
2134 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002135 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002136 if (tstate->c_tracefunc != NULL
Victor Stinner438a12d2019-05-24 17:01:38 +02002137 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002138 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002139 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002140 if (err < 0)
2141 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002142 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002143 SET_TOP(val);
2144 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002145 }
Martin Panter95f53c12016-07-18 08:23:26 +00002146 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002147 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002148 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002149 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002150 f->f_lasti -= sizeof(_Py_CODEUNIT);
Pablo Galindof00828a2019-05-09 16:52:02 +01002151 goto exit_yielding;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002152 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002153
Benjamin Petersonddd19492018-09-16 22:38:02 -07002154 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002156
2157 if (co->co_flags & CO_ASYNC_GENERATOR) {
2158 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2159 Py_DECREF(retval);
2160 if (w == NULL) {
2161 retval = NULL;
2162 goto error;
2163 }
2164 retval = w;
2165 }
2166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002167 f->f_stacktop = stack_pointer;
Pablo Galindof00828a2019-05-09 16:52:02 +01002168 goto exit_yielding;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002169 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002170
Benjamin Petersonddd19492018-09-16 22:38:02 -07002171 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002172 PyObject *type, *value, *traceback;
2173 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002174 PyTryBlock *b = PyFrame_BlockPop(f);
2175 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002176 _PyErr_SetString(tstate, PyExc_SystemError,
2177 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002178 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002180 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2181 STACK_LEVEL() <= (b)->b_level + 4);
2182 exc_info = tstate->exc_info;
2183 type = exc_info->exc_type;
2184 value = exc_info->exc_value;
2185 traceback = exc_info->exc_traceback;
2186 exc_info->exc_type = POP();
2187 exc_info->exc_value = POP();
2188 exc_info->exc_traceback = POP();
2189 Py_XDECREF(type);
2190 Py_XDECREF(value);
2191 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002193 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002194
Benjamin Petersonddd19492018-09-16 22:38:02 -07002195 case TARGET(POP_BLOCK): {
2196 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002197 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002199 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002200
Benjamin Petersonddd19492018-09-16 22:38:02 -07002201 case TARGET(POP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002202 /* If oparg is 0 at the top of the stack are 1 or 6 values:
2203 Either:
2204 - TOP = NULL or an integer
2205 or:
2206 - (TOP, SECOND, THIRD) = exc_info()
2207 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2208
2209 If oparg is 1 the value for 'return' was additionally pushed
2210 at the top of the stack.
2211 */
2212 PyObject *res = NULL;
2213 if (oparg) {
2214 res = POP();
2215 }
2216 PyObject *exc = POP();
2217 if (exc == NULL || PyLong_CheckExact(exc)) {
2218 Py_XDECREF(exc);
2219 }
2220 else {
2221 Py_DECREF(exc);
2222 Py_DECREF(POP());
2223 Py_DECREF(POP());
2224
2225 PyObject *type, *value, *traceback;
2226 _PyErr_StackItem *exc_info;
2227 PyTryBlock *b = PyFrame_BlockPop(f);
2228 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002229 _PyErr_SetString(tstate, PyExc_SystemError,
2230 "popped block is not an except handler");
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002231 Py_XDECREF(res);
2232 goto error;
2233 }
2234 assert(STACK_LEVEL() == (b)->b_level + 3);
2235 exc_info = tstate->exc_info;
2236 type = exc_info->exc_type;
2237 value = exc_info->exc_value;
2238 traceback = exc_info->exc_traceback;
2239 exc_info->exc_type = POP();
2240 exc_info->exc_value = POP();
2241 exc_info->exc_traceback = POP();
2242 Py_XDECREF(type);
2243 Py_XDECREF(value);
2244 Py_XDECREF(traceback);
2245 }
2246 if (oparg) {
2247 PUSH(res);
2248 }
2249 DISPATCH();
2250 }
2251
Benjamin Petersonddd19492018-09-16 22:38:02 -07002252 case TARGET(CALL_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002253 PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
2254 if (ret == NULL) {
2255 goto error;
2256 }
2257 PUSH(ret);
2258 JUMPBY(oparg);
2259 FAST_DISPATCH();
2260 }
2261
Benjamin Petersonddd19492018-09-16 22:38:02 -07002262 case TARGET(BEGIN_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002263 /* Push NULL onto the stack for using it in END_FINALLY,
2264 POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH.
2265 */
2266 PUSH(NULL);
2267 FAST_DISPATCH();
2268 }
2269
Benjamin Petersonddd19492018-09-16 22:38:02 -07002270 case TARGET(END_FINALLY): {
2271 PREDICTED(END_FINALLY);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002272 /* At the top of the stack are 1 or 6 values:
2273 Either:
2274 - TOP = NULL or an integer
2275 or:
2276 - (TOP, SECOND, THIRD) = exc_info()
2277 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2278 */
2279 PyObject *exc = POP();
2280 if (exc == NULL) {
2281 FAST_DISPATCH();
2282 }
2283 else if (PyLong_CheckExact(exc)) {
2284 int ret = _PyLong_AsInt(exc);
2285 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02002286 if (ret == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002287 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002289 JUMPTO(ret);
2290 FAST_DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002292 else {
2293 assert(PyExceptionClass_Check(exc));
2294 PyObject *val = POP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002295 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002296 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002297 goto exception_unwind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002299 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002300
Benjamin Petersonddd19492018-09-16 22:38:02 -07002301 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002302 PyObject *exc = POP();
2303 assert(PyExceptionClass_Check(exc));
2304 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2305 PyTryBlock *b = PyFrame_BlockPop(f);
2306 assert(b->b_type == EXCEPT_HANDLER);
2307 Py_DECREF(exc);
2308 UNWIND_EXCEPT_HANDLER(b);
2309 Py_DECREF(POP());
2310 JUMPBY(oparg);
2311 FAST_DISPATCH();
2312 }
2313 else {
2314 PyObject *val = POP();
2315 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002316 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002317 goto exception_unwind;
2318 }
2319 }
2320
Benjamin Petersonddd19492018-09-16 22:38:02 -07002321 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002322 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002323
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002324 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002325 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002326 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002327 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002328 if (!_PyErr_Occurred(tstate)) {
2329 _PyErr_SetString(tstate, PyExc_NameError,
2330 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002331 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002332 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002333 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002334 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002335 }
2336 else {
2337 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2338 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002339 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002340 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2341 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002342 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2343 _PyErr_SetString(tstate, PyExc_NameError,
2344 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002345 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002346 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002347 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002348 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002349 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002350 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002351
Benjamin Petersonddd19492018-09-16 22:38:02 -07002352 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002353 PyObject *name = GETITEM(names, oparg);
2354 PyObject *v = POP();
2355 PyObject *ns = f->f_locals;
2356 int err;
2357 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002358 _PyErr_Format(tstate, PyExc_SystemError,
2359 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002361 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002363 if (PyDict_CheckExact(ns))
2364 err = PyDict_SetItem(ns, name, v);
2365 else
2366 err = PyObject_SetItem(ns, name, v);
2367 Py_DECREF(v);
2368 if (err != 0)
2369 goto error;
2370 DISPATCH();
2371 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002372
Benjamin Petersonddd19492018-09-16 22:38:02 -07002373 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002374 PyObject *name = GETITEM(names, oparg);
2375 PyObject *ns = f->f_locals;
2376 int err;
2377 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002378 _PyErr_Format(tstate, PyExc_SystemError,
2379 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002380 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002382 err = PyObject_DelItem(ns, name);
2383 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002384 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002385 NAME_ERROR_MSG,
2386 name);
2387 goto error;
2388 }
2389 DISPATCH();
2390 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002391
Benjamin Petersonddd19492018-09-16 22:38:02 -07002392 case TARGET(UNPACK_SEQUENCE): {
2393 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002394 PyObject *seq = POP(), *item, **items;
2395 if (PyTuple_CheckExact(seq) &&
2396 PyTuple_GET_SIZE(seq) == oparg) {
2397 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002399 item = items[oparg];
2400 Py_INCREF(item);
2401 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002403 } else if (PyList_CheckExact(seq) &&
2404 PyList_GET_SIZE(seq) == oparg) {
2405 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002406 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002407 item = items[oparg];
2408 Py_INCREF(item);
2409 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002410 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002411 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002413 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002414 } else {
2415 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002416 Py_DECREF(seq);
2417 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002419 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002420 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002421 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002422
Benjamin Petersonddd19492018-09-16 22:38:02 -07002423 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002424 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2425 PyObject *seq = POP();
2426
Victor Stinner438a12d2019-05-24 17:01:38 +02002427 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002428 stack_pointer + totalargs)) {
2429 stack_pointer += totalargs;
2430 } else {
2431 Py_DECREF(seq);
2432 goto error;
2433 }
2434 Py_DECREF(seq);
2435 DISPATCH();
2436 }
2437
Benjamin Petersonddd19492018-09-16 22:38:02 -07002438 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002439 PyObject *name = GETITEM(names, oparg);
2440 PyObject *owner = TOP();
2441 PyObject *v = SECOND();
2442 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002443 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002444 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002446 Py_DECREF(owner);
2447 if (err != 0)
2448 goto error;
2449 DISPATCH();
2450 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002451
Benjamin Petersonddd19492018-09-16 22:38:02 -07002452 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002453 PyObject *name = GETITEM(names, oparg);
2454 PyObject *owner = POP();
2455 int err;
2456 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2457 Py_DECREF(owner);
2458 if (err != 0)
2459 goto error;
2460 DISPATCH();
2461 }
2462
Benjamin Petersonddd19492018-09-16 22:38:02 -07002463 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002464 PyObject *name = GETITEM(names, oparg);
2465 PyObject *v = POP();
2466 int err;
2467 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002468 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002469 if (err != 0)
2470 goto error;
2471 DISPATCH();
2472 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002473
Benjamin Petersonddd19492018-09-16 22:38:02 -07002474 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002475 PyObject *name = GETITEM(names, oparg);
2476 int err;
2477 err = PyDict_DelItem(f->f_globals, name);
2478 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002479 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2480 format_exc_check_arg(tstate, PyExc_NameError,
2481 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002482 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002483 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002484 }
2485 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002486 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002487
Benjamin Petersonddd19492018-09-16 22:38:02 -07002488 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002489 PyObject *name = GETITEM(names, oparg);
2490 PyObject *locals = f->f_locals;
2491 PyObject *v;
2492 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002493 _PyErr_Format(tstate, PyExc_SystemError,
2494 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002495 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002496 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002497 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002498 v = PyDict_GetItemWithError(locals, name);
2499 if (v != NULL) {
2500 Py_INCREF(v);
2501 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002502 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002503 goto error;
2504 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002505 }
2506 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002507 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002508 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002509 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002510 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002511 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002512 }
2513 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002514 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002515 v = PyDict_GetItemWithError(f->f_globals, name);
2516 if (v != NULL) {
2517 Py_INCREF(v);
2518 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002519 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002520 goto error;
2521 }
2522 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002523 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002524 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002525 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002526 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002527 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002528 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002529 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002530 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002531 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002532 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002533 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002534 }
2535 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002536 v = PyObject_GetItem(f->f_builtins, name);
2537 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002538 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002539 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002540 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002541 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002542 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002543 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002544 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002545 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002546 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002547 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002548 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002549 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002550 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002551
Benjamin Petersonddd19492018-09-16 22:38:02 -07002552 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002553 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002554 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002555 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002556 && PyDict_CheckExact(f->f_builtins))
2557 {
Inada Naoki91234a12019-06-03 21:30:58 +09002558 OPCACHE_CHECK();
2559 if (co_opcache != NULL && co_opcache->optimized > 0) {
2560 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2561
2562 if (lg->globals_ver ==
2563 ((PyDictObject *)f->f_globals)->ma_version_tag
2564 && lg->builtins_ver ==
2565 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2566 {
2567 PyObject *ptr = lg->ptr;
2568 OPCACHE_STAT_GLOBAL_HIT();
2569 assert(ptr != NULL);
2570 Py_INCREF(ptr);
2571 PUSH(ptr);
2572 DISPATCH();
2573 }
2574 }
2575
2576 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002577 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002578 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002579 name);
2580 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002581 if (!_PyErr_OCCURRED()) {
2582 /* _PyDict_LoadGlobal() returns NULL without raising
2583 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002584 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002585 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002586 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002587 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002588 }
Inada Naoki91234a12019-06-03 21:30:58 +09002589
2590 if (co_opcache != NULL) {
2591 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2592
2593 if (co_opcache->optimized == 0) {
2594 /* Wasn't optimized before. */
2595 OPCACHE_STAT_GLOBAL_OPT();
2596 } else {
2597 OPCACHE_STAT_GLOBAL_MISS();
2598 }
2599
2600 co_opcache->optimized = 1;
2601 lg->globals_ver =
2602 ((PyDictObject *)f->f_globals)->ma_version_tag;
2603 lg->builtins_ver =
2604 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2605 lg->ptr = v; /* borrowed */
2606 }
2607
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002608 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002609 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002610 else {
2611 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002612
2613 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002614 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002615 v = PyObject_GetItem(f->f_globals, name);
2616 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002617 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002618 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002619 }
2620 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002621
Victor Stinnerb4efc962015-11-20 09:24:02 +01002622 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002623 v = PyObject_GetItem(f->f_builtins, name);
2624 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002625 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002626 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002627 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002628 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002629 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002630 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002631 }
2632 }
2633 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002634 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002635 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002636 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002637
Benjamin Petersonddd19492018-09-16 22:38:02 -07002638 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002639 PyObject *v = GETLOCAL(oparg);
2640 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002641 SETLOCAL(oparg, NULL);
2642 DISPATCH();
2643 }
2644 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002645 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002646 UNBOUNDLOCAL_ERROR_MSG,
2647 PyTuple_GetItem(co->co_varnames, oparg)
2648 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002649 goto error;
2650 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002651
Benjamin Petersonddd19492018-09-16 22:38:02 -07002652 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002653 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002654 PyObject *oldobj = PyCell_GET(cell);
2655 if (oldobj != NULL) {
2656 PyCell_SET(cell, NULL);
2657 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002658 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002659 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002660 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002661 goto error;
2662 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002663
Benjamin Petersonddd19492018-09-16 22:38:02 -07002664 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002665 PyObject *cell = freevars[oparg];
2666 Py_INCREF(cell);
2667 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002668 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002669 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002670
Benjamin Petersonddd19492018-09-16 22:38:02 -07002671 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002672 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002673 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002674 assert(locals);
2675 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2676 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2677 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2678 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2679 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002680 value = PyDict_GetItemWithError(locals, name);
2681 if (value != NULL) {
2682 Py_INCREF(value);
2683 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002684 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002685 goto error;
2686 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002687 }
2688 else {
2689 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002690 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002691 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002692 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002693 }
2694 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002695 }
2696 }
2697 if (!value) {
2698 PyObject *cell = freevars[oparg];
2699 value = PyCell_GET(cell);
2700 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002701 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002702 goto error;
2703 }
2704 Py_INCREF(value);
2705 }
2706 PUSH(value);
2707 DISPATCH();
2708 }
2709
Benjamin Petersonddd19492018-09-16 22:38:02 -07002710 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002711 PyObject *cell = freevars[oparg];
2712 PyObject *value = PyCell_GET(cell);
2713 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002714 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002715 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002716 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002717 Py_INCREF(value);
2718 PUSH(value);
2719 DISPATCH();
2720 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002721
Benjamin Petersonddd19492018-09-16 22:38:02 -07002722 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002723 PyObject *v = POP();
2724 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002725 PyObject *oldobj = PyCell_GET(cell);
2726 PyCell_SET(cell, v);
2727 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002728 DISPATCH();
2729 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002730
Benjamin Petersonddd19492018-09-16 22:38:02 -07002731 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002732 PyObject *str;
2733 PyObject *empty = PyUnicode_New(0, 0);
2734 if (empty == NULL) {
2735 goto error;
2736 }
2737 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2738 Py_DECREF(empty);
2739 if (str == NULL)
2740 goto error;
2741 while (--oparg >= 0) {
2742 PyObject *item = POP();
2743 Py_DECREF(item);
2744 }
2745 PUSH(str);
2746 DISPATCH();
2747 }
2748
Benjamin Petersonddd19492018-09-16 22:38:02 -07002749 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002750 PyObject *tup = PyTuple_New(oparg);
2751 if (tup == NULL)
2752 goto error;
2753 while (--oparg >= 0) {
2754 PyObject *item = POP();
2755 PyTuple_SET_ITEM(tup, oparg, item);
2756 }
2757 PUSH(tup);
2758 DISPATCH();
2759 }
2760
Benjamin Petersonddd19492018-09-16 22:38:02 -07002761 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002762 PyObject *list = PyList_New(oparg);
2763 if (list == NULL)
2764 goto error;
2765 while (--oparg >= 0) {
2766 PyObject *item = POP();
2767 PyList_SET_ITEM(list, oparg, item);
2768 }
2769 PUSH(list);
2770 DISPATCH();
2771 }
2772
Benjamin Petersonddd19492018-09-16 22:38:02 -07002773 case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL):
2774 case TARGET(BUILD_TUPLE_UNPACK):
2775 case TARGET(BUILD_LIST_UNPACK): {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002776 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002777 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002778 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002779 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002780
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002781 if (sum == NULL)
2782 goto error;
2783
2784 for (i = oparg; i > 0; i--) {
2785 PyObject *none_val;
2786
2787 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2788 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002789 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Victor Stinner438a12d2019-05-24 17:01:38 +02002790 _PyErr_ExceptionMatches(tstate, PyExc_TypeError))
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002791 {
Victor Stinner438a12d2019-05-24 17:01:38 +02002792 check_args_iterable(tstate, PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002793 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002794 Py_DECREF(sum);
2795 goto error;
2796 }
2797 Py_DECREF(none_val);
2798 }
2799
2800 if (convert_to_tuple) {
2801 return_value = PyList_AsTuple(sum);
2802 Py_DECREF(sum);
2803 if (return_value == NULL)
2804 goto error;
2805 }
2806 else {
2807 return_value = sum;
2808 }
2809
2810 while (oparg--)
2811 Py_DECREF(POP());
2812 PUSH(return_value);
2813 DISPATCH();
2814 }
2815
Benjamin Petersonddd19492018-09-16 22:38:02 -07002816 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002817 PyObject *set = PySet_New(NULL);
2818 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002819 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002820 if (set == NULL)
2821 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002822 for (i = oparg; i > 0; i--) {
2823 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002824 if (err == 0)
2825 err = PySet_Add(set, item);
2826 Py_DECREF(item);
2827 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002828 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002829 if (err != 0) {
2830 Py_DECREF(set);
2831 goto error;
2832 }
2833 PUSH(set);
2834 DISPATCH();
2835 }
2836
Benjamin Petersonddd19492018-09-16 22:38:02 -07002837 case TARGET(BUILD_SET_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002838 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002839 PyObject *sum = PySet_New(NULL);
2840 if (sum == NULL)
2841 goto error;
2842
2843 for (i = oparg; i > 0; i--) {
2844 if (_PySet_Update(sum, PEEK(i)) < 0) {
2845 Py_DECREF(sum);
2846 goto error;
2847 }
2848 }
2849
2850 while (oparg--)
2851 Py_DECREF(POP());
2852 PUSH(sum);
2853 DISPATCH();
2854 }
2855
Benjamin Petersonddd19492018-09-16 22:38:02 -07002856 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002857 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002858 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2859 if (map == NULL)
2860 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002861 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002862 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002863 PyObject *key = PEEK(2*i);
2864 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002865 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002866 if (err != 0) {
2867 Py_DECREF(map);
2868 goto error;
2869 }
2870 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002871
2872 while (oparg--) {
2873 Py_DECREF(POP());
2874 Py_DECREF(POP());
2875 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002876 PUSH(map);
2877 DISPATCH();
2878 }
2879
Benjamin Petersonddd19492018-09-16 22:38:02 -07002880 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002881 _Py_IDENTIFIER(__annotations__);
2882 int err;
2883 PyObject *ann_dict;
2884 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002885 _PyErr_Format(tstate, PyExc_SystemError,
2886 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002887 goto error;
2888 }
2889 /* check if __annotations__ in locals()... */
2890 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002891 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002892 &PyId___annotations__);
2893 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002894 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002895 goto error;
2896 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002897 /* ...if not, create a new one */
2898 ann_dict = PyDict_New();
2899 if (ann_dict == NULL) {
2900 goto error;
2901 }
2902 err = _PyDict_SetItemId(f->f_locals,
2903 &PyId___annotations__, ann_dict);
2904 Py_DECREF(ann_dict);
2905 if (err != 0) {
2906 goto error;
2907 }
2908 }
2909 }
2910 else {
2911 /* do the same if locals() is not a dict */
2912 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2913 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002914 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002915 }
2916 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2917 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002918 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002919 goto error;
2920 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002921 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002922 ann_dict = PyDict_New();
2923 if (ann_dict == NULL) {
2924 goto error;
2925 }
2926 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2927 Py_DECREF(ann_dict);
2928 if (err != 0) {
2929 goto error;
2930 }
2931 }
2932 else {
2933 Py_DECREF(ann_dict);
2934 }
2935 }
2936 DISPATCH();
2937 }
2938
Benjamin Petersonddd19492018-09-16 22:38:02 -07002939 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002940 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002941 PyObject *map;
2942 PyObject *keys = TOP();
2943 if (!PyTuple_CheckExact(keys) ||
2944 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002945 _PyErr_SetString(tstate, PyExc_SystemError,
2946 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002947 goto error;
2948 }
2949 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2950 if (map == NULL) {
2951 goto error;
2952 }
2953 for (i = oparg; i > 0; i--) {
2954 int err;
2955 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2956 PyObject *value = PEEK(i + 1);
2957 err = PyDict_SetItem(map, key, value);
2958 if (err != 0) {
2959 Py_DECREF(map);
2960 goto error;
2961 }
2962 }
2963
2964 Py_DECREF(POP());
2965 while (oparg--) {
2966 Py_DECREF(POP());
2967 }
2968 PUSH(map);
2969 DISPATCH();
2970 }
2971
Benjamin Petersonddd19492018-09-16 22:38:02 -07002972 case TARGET(BUILD_MAP_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002973 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002974 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002975 if (sum == NULL)
2976 goto error;
2977
2978 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002979 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002980 if (PyDict_Update(sum, arg) < 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002981 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2982 _PyErr_Format(tstate, PyExc_TypeError,
2983 "'%.200s' object is not a mapping",
2984 arg->ob_type->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002985 }
2986 Py_DECREF(sum);
2987 goto error;
2988 }
2989 }
2990
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002991 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002992 Py_DECREF(POP());
2993 PUSH(sum);
2994 DISPATCH();
2995 }
2996
Benjamin Petersonddd19492018-09-16 22:38:02 -07002997 case TARGET(BUILD_MAP_UNPACK_WITH_CALL): {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002998 Py_ssize_t i;
2999 PyObject *sum = PyDict_New();
3000 if (sum == NULL)
3001 goto error;
3002
3003 for (i = oparg; i > 0; i--) {
3004 PyObject *arg = PEEK(i);
3005 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003006 Py_DECREF(sum);
Victor Stinner438a12d2019-05-24 17:01:38 +02003007 format_kwargs_error(tstate, PEEK(2 + oparg), arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003008 goto error;
3009 }
3010 }
3011
3012 while (oparg--)
3013 Py_DECREF(POP());
3014 PUSH(sum);
3015 DISPATCH();
3016 }
3017
Benjamin Petersonddd19492018-09-16 22:38:02 -07003018 case TARGET(MAP_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003019 PyObject *key = TOP();
3020 PyObject *value = SECOND();
3021 PyObject *map;
3022 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00003023 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07003024 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003025 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00003026 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003027 Py_DECREF(value);
3028 Py_DECREF(key);
3029 if (err != 0)
3030 goto error;
3031 PREDICT(JUMP_ABSOLUTE);
3032 DISPATCH();
3033 }
3034
Benjamin Petersonddd19492018-09-16 22:38:02 -07003035 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003036 PyObject *name = GETITEM(names, oparg);
3037 PyObject *owner = TOP();
3038 PyObject *res = PyObject_GetAttr(owner, name);
3039 Py_DECREF(owner);
3040 SET_TOP(res);
3041 if (res == NULL)
3042 goto error;
3043 DISPATCH();
3044 }
3045
Benjamin Petersonddd19492018-09-16 22:38:02 -07003046 case TARGET(COMPARE_OP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003047 PyObject *right = POP();
3048 PyObject *left = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003049 PyObject *res = cmp_outcome(tstate, oparg, left, right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003050 Py_DECREF(left);
3051 Py_DECREF(right);
3052 SET_TOP(res);
3053 if (res == NULL)
3054 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 PREDICT(POP_JUMP_IF_FALSE);
3056 PREDICT(POP_JUMP_IF_TRUE);
3057 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003058 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003059
Benjamin Petersonddd19492018-09-16 22:38:02 -07003060 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003061 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003062 PyObject *fromlist = POP();
3063 PyObject *level = TOP();
3064 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003065 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003066 Py_DECREF(level);
3067 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003068 SET_TOP(res);
3069 if (res == NULL)
3070 goto error;
3071 DISPATCH();
3072 }
3073
Benjamin Petersonddd19492018-09-16 22:38:02 -07003074 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003075 PyObject *from = POP(), *locals;
3076 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003077 if (PyFrame_FastToLocalsWithError(f) < 0) {
3078 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003079 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003080 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003081
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003082 locals = f->f_locals;
3083 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003084 _PyErr_SetString(tstate, PyExc_SystemError,
3085 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003086 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003087 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003089 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003091 Py_DECREF(from);
3092 if (err != 0)
3093 goto error;
3094 DISPATCH();
3095 }
Guido van Rossum25831651993-05-19 14:50:45 +00003096
Benjamin Petersonddd19492018-09-16 22:38:02 -07003097 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003098 PyObject *name = GETITEM(names, oparg);
3099 PyObject *from = TOP();
3100 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003101 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003102 PUSH(res);
3103 if (res == NULL)
3104 goto error;
3105 DISPATCH();
3106 }
Thomas Wouters52152252000-08-17 22:55:00 +00003107
Benjamin Petersonddd19492018-09-16 22:38:02 -07003108 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003109 JUMPBY(oparg);
3110 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003111 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003112
Benjamin Petersonddd19492018-09-16 22:38:02 -07003113 case TARGET(POP_JUMP_IF_FALSE): {
3114 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003115 PyObject *cond = POP();
3116 int err;
3117 if (cond == Py_True) {
3118 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003119 FAST_DISPATCH();
3120 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003121 if (cond == Py_False) {
3122 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003123 JUMPTO(oparg);
3124 FAST_DISPATCH();
3125 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003126 err = PyObject_IsTrue(cond);
3127 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003128 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003129 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003130 else if (err == 0)
3131 JUMPTO(oparg);
3132 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003133 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003134 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003135 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003136
Benjamin Petersonddd19492018-09-16 22:38:02 -07003137 case TARGET(POP_JUMP_IF_TRUE): {
3138 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003139 PyObject *cond = POP();
3140 int err;
3141 if (cond == Py_False) {
3142 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003143 FAST_DISPATCH();
3144 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003145 if (cond == Py_True) {
3146 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003147 JUMPTO(oparg);
3148 FAST_DISPATCH();
3149 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003150 err = PyObject_IsTrue(cond);
3151 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003152 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003153 JUMPTO(oparg);
3154 }
3155 else if (err == 0)
3156 ;
3157 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003158 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003159 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003160 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003161
Benjamin Petersonddd19492018-09-16 22:38:02 -07003162 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003163 PyObject *cond = TOP();
3164 int err;
3165 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003166 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003167 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003168 FAST_DISPATCH();
3169 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003170 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003171 JUMPTO(oparg);
3172 FAST_DISPATCH();
3173 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003174 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003175 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003176 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003177 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003178 }
3179 else if (err == 0)
3180 JUMPTO(oparg);
3181 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003182 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003183 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003184 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003185
Benjamin Petersonddd19492018-09-16 22:38:02 -07003186 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003187 PyObject *cond = TOP();
3188 int err;
3189 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003190 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003191 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003192 FAST_DISPATCH();
3193 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003194 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003195 JUMPTO(oparg);
3196 FAST_DISPATCH();
3197 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003198 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003199 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003200 JUMPTO(oparg);
3201 }
3202 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003203 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003204 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003205 }
3206 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003207 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003208 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003209 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003210
Benjamin Petersonddd19492018-09-16 22:38:02 -07003211 case TARGET(JUMP_ABSOLUTE): {
3212 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003213 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003214#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003215 /* Enabling this path speeds-up all while and for-loops by bypassing
3216 the per-loop checks for signals. By default, this should be turned-off
3217 because it prevents detection of a control-break in tight loops like
3218 "while 1: pass". Compile with this option turned-on when you need
3219 the speed-up and do not need break checking inside tight loops (ones
3220 that contain only instructions ending with FAST_DISPATCH).
3221 */
3222 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003223#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003224 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003225#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003226 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003227
Benjamin Petersonddd19492018-09-16 22:38:02 -07003228 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003229 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003230 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003231 PyObject *iter = PyObject_GetIter(iterable);
3232 Py_DECREF(iterable);
3233 SET_TOP(iter);
3234 if (iter == NULL)
3235 goto error;
3236 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003237 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003238 DISPATCH();
3239 }
3240
Benjamin Petersonddd19492018-09-16 22:38:02 -07003241 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003242 /* before: [obj]; after [getiter(obj)] */
3243 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003244 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003245 if (PyCoro_CheckExact(iterable)) {
3246 /* `iterable` is a coroutine */
3247 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3248 /* and it is used in a 'yield from' expression of a
3249 regular generator. */
3250 Py_DECREF(iterable);
3251 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003252 _PyErr_SetString(tstate, PyExc_TypeError,
3253 "cannot 'yield from' a coroutine object "
3254 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003255 goto error;
3256 }
3257 }
3258 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003259 /* `iterable` is not a generator. */
3260 iter = PyObject_GetIter(iterable);
3261 Py_DECREF(iterable);
3262 SET_TOP(iter);
3263 if (iter == NULL)
3264 goto error;
3265 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003266 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003267 DISPATCH();
3268 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003269
Benjamin Petersonddd19492018-09-16 22:38:02 -07003270 case TARGET(FOR_ITER): {
3271 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003272 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003273 PyObject *iter = TOP();
3274 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3275 if (next != NULL) {
3276 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003277 PREDICT(STORE_FAST);
3278 PREDICT(UNPACK_SEQUENCE);
3279 DISPATCH();
3280 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003281 if (_PyErr_Occurred(tstate)) {
3282 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003283 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003284 }
3285 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003286 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003287 }
3288 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003289 }
3290 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003291 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003292 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003293 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003294 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003295 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003296 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003297
Benjamin Petersonddd19492018-09-16 22:38:02 -07003298 case TARGET(SETUP_FINALLY): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003299 /* NOTE: If you add any new block-setup opcodes that
3300 are not try/except/finally handlers, you may need
3301 to update the PyGen_NeedsFinalizing() function.
3302 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003303
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003304 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003305 STACK_LEVEL());
3306 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003307 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003308
Benjamin Petersonddd19492018-09-16 22:38:02 -07003309 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003310 _Py_IDENTIFIER(__aexit__);
3311 _Py_IDENTIFIER(__aenter__);
3312
3313 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003314 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__),
Yury Selivanov75445082015-05-11 22:57:16 -04003315 *enter;
3316 PyObject *res;
3317 if (exit == NULL)
3318 goto error;
3319 SET_TOP(exit);
Victor Stinner438a12d2019-05-24 17:01:38 +02003320 enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003321 Py_DECREF(mgr);
3322 if (enter == NULL)
3323 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003324 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003325 Py_DECREF(enter);
3326 if (res == NULL)
3327 goto error;
3328 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003329 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003330 DISPATCH();
3331 }
3332
Benjamin Petersonddd19492018-09-16 22:38:02 -07003333 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003334 PyObject *res = POP();
3335 /* Setup the finally block before pushing the result
3336 of __aenter__ on the stack. */
3337 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3338 STACK_LEVEL());
3339 PUSH(res);
3340 DISPATCH();
3341 }
3342
Benjamin Petersonddd19492018-09-16 22:38:02 -07003343 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003344 _Py_IDENTIFIER(__exit__);
3345 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003346 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003347 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003348 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003349 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003350 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003351 }
3352 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003353 if (exit == NULL) {
3354 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003355 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003356 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003357 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003358 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003359 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003360 Py_DECREF(enter);
3361 if (res == NULL)
3362 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003363 /* Setup the finally block before pushing the result
3364 of __enter__ on the stack. */
3365 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3366 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003367
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003368 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003369 DISPATCH();
3370 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003371
Benjamin Petersonddd19492018-09-16 22:38:02 -07003372 case TARGET(WITH_CLEANUP_START): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003373 /* At the top of the stack are 1 or 6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003374 how/why we entered the finally clause:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003375 - TOP = NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003376 - (TOP, SECOND, THIRD) = exc_info()
3377 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003378 Below them is EXIT, the context.__exit__ or context.__aexit__
3379 bound method.
3380 In the first case, we must call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003381 EXIT(None, None, None)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003382 otherwise we must call
3383 EXIT(TOP, SECOND, THIRD)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003384
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003385 In the first case, we remove EXIT from the
3386 stack, leaving TOP, and push TOP on the stack.
3387 Otherwise we shift the bottom 3 values of the
3388 stack down, replace the empty spot with NULL, and push
3389 None on the stack.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003390
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003391 Finally we push the result of the call.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003392 */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003393 PyObject *stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003394 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003395 PyObject *exc, *val, *tb, *res;
3396
3397 val = tb = Py_None;
3398 exc = TOP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003399 if (exc == NULL) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003400 STACK_SHRINK(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003401 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003402 SET_TOP(exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003403 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003404 }
3405 else {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003406 assert(PyExceptionClass_Check(exc));
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003407 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003408 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003409 val = SECOND();
3410 tb = THIRD();
3411 tp2 = FOURTH();
3412 exc2 = PEEK(5);
3413 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003414 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003415 SET_VALUE(7, tb2);
3416 SET_VALUE(6, exc2);
3417 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003418 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3419 SET_FOURTH(NULL);
3420 /* We just shifted the stack down, so we have
3421 to tell the except handler block that the
3422 values are lower than it expects. */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003423 assert(f->f_iblock > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003424 block = &f->f_blockstack[f->f_iblock - 1];
3425 assert(block->b_type == EXCEPT_HANDLER);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003426 assert(block->b_level > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003427 block->b_level--;
3428 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003429
3430 stack[0] = exc;
3431 stack[1] = val;
3432 stack[2] = tb;
3433 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003434 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003435 if (res == NULL)
3436 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003437
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003438 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003439 PUSH(exc);
3440 PUSH(res);
3441 PREDICT(WITH_CLEANUP_FINISH);
3442 DISPATCH();
3443 }
3444
Benjamin Petersonddd19492018-09-16 22:38:02 -07003445 case TARGET(WITH_CLEANUP_FINISH): {
3446 PREDICTED(WITH_CLEANUP_FINISH);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003447 /* TOP = the result of calling the context.__exit__ bound method
3448 SECOND = either None or exception type
3449
3450 If SECOND is None below is NULL or the return address,
3451 otherwise below are 7 values representing an exception.
3452 */
Yury Selivanov75445082015-05-11 22:57:16 -04003453 PyObject *res = POP();
3454 PyObject *exc = POP();
3455 int err;
3456
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003457 if (exc != Py_None)
3458 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003459 else
3460 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003461
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003462 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003463 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003465 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003466 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003467 else if (err > 0) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003468 /* There was an exception and a True return.
3469 * We must manually unwind the EXCEPT_HANDLER block
3470 * which was created when the exception was caught,
Quan Tian3bd0d622018-10-20 05:30:03 +08003471 * otherwise the stack will be in an inconsistent state.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003472 */
3473 PyTryBlock *b = PyFrame_BlockPop(f);
3474 assert(b->b_type == EXCEPT_HANDLER);
3475 UNWIND_EXCEPT_HANDLER(b);
3476 PUSH(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003477 }
3478 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003479 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003480 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003481
Benjamin Petersonddd19492018-09-16 22:38:02 -07003482 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003483 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003484 PyObject *name = GETITEM(names, oparg);
3485 PyObject *obj = TOP();
3486 PyObject *meth = NULL;
3487
3488 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3489
Yury Selivanovf2392132016-12-13 19:03:51 -05003490 if (meth == NULL) {
3491 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003492 goto error;
3493 }
3494
3495 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003496 /* We can bypass temporary bound method object.
3497 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003498
INADA Naoki015bce62017-01-16 17:23:30 +09003499 meth | self | arg1 | ... | argN
3500 */
3501 SET_TOP(meth);
3502 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003503 }
3504 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003505 /* meth is not an unbound method (but a regular attr, or
3506 something was returned by a descriptor protocol). Set
3507 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003508 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003509
3510 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003511 */
INADA Naoki015bce62017-01-16 17:23:30 +09003512 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003513 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003514 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003515 }
3516 DISPATCH();
3517 }
3518
Benjamin Petersonddd19492018-09-16 22:38:02 -07003519 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003520 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003521 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003522
3523 sp = stack_pointer;
3524
INADA Naoki015bce62017-01-16 17:23:30 +09003525 meth = PEEK(oparg + 2);
3526 if (meth == NULL) {
3527 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3528 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003529
3530 Stack layout:
3531
INADA Naoki015bce62017-01-16 17:23:30 +09003532 ... | NULL | callable | arg1 | ... | argN
3533 ^- TOP()
3534 ^- (-oparg)
3535 ^- (-oparg-1)
3536 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003537
Ville Skyttä49b27342017-08-03 09:00:59 +03003538 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003539 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003540 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003541 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003542 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003543 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003544 }
3545 else {
3546 /* This is a method call. Stack layout:
3547
INADA Naoki015bce62017-01-16 17:23:30 +09003548 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003549 ^- TOP()
3550 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003551 ^- (-oparg-1)
3552 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003553
INADA Naoki015bce62017-01-16 17:23:30 +09003554 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003555 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003556 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003557 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003558 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003559 stack_pointer = sp;
3560 }
3561
3562 PUSH(res);
3563 if (res == NULL)
3564 goto error;
3565 DISPATCH();
3566 }
3567
Benjamin Petersonddd19492018-09-16 22:38:02 -07003568 case TARGET(CALL_FUNCTION): {
3569 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003570 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003571 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003572 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003573 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003574 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003575 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003576 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003577 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003578 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003579 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003580
Benjamin Petersonddd19492018-09-16 22:38:02 -07003581 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003582 PyObject **sp, *res, *names;
3583
3584 names = POP();
3585 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003586 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003587 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003588 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003589 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003590 Py_DECREF(names);
3591
3592 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003593 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003594 }
3595 DISPATCH();
3596 }
3597
Benjamin Petersonddd19492018-09-16 22:38:02 -07003598 case TARGET(CALL_FUNCTION_EX): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003599 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003600 if (oparg & 0x01) {
3601 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003602 if (!PyDict_CheckExact(kwargs)) {
3603 PyObject *d = PyDict_New();
3604 if (d == NULL)
3605 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003606 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003607 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003608 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003609 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003610 goto error;
3611 }
3612 Py_DECREF(kwargs);
3613 kwargs = d;
3614 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003615 assert(PyDict_CheckExact(kwargs));
3616 }
3617 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003618 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003619 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003620 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003621 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003622 goto error;
3623 }
3624 Py_SETREF(callargs, PySequence_Tuple(callargs));
3625 if (callargs == NULL) {
3626 goto error;
3627 }
3628 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003629 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003630
Victor Stinner09532fe2019-05-10 23:39:09 +02003631 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003632 Py_DECREF(func);
3633 Py_DECREF(callargs);
3634 Py_XDECREF(kwargs);
3635
3636 SET_TOP(result);
3637 if (result == NULL) {
3638 goto error;
3639 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003640 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003641 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003642
Benjamin Petersonddd19492018-09-16 22:38:02 -07003643 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003644 PyObject *qualname = POP();
3645 PyObject *codeobj = POP();
3646 PyFunctionObject *func = (PyFunctionObject *)
3647 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003648
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003649 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003650 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003651 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003652 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003653 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003654
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003655 if (oparg & 0x08) {
3656 assert(PyTuple_CheckExact(TOP()));
3657 func ->func_closure = POP();
3658 }
3659 if (oparg & 0x04) {
3660 assert(PyDict_CheckExact(TOP()));
3661 func->func_annotations = POP();
3662 }
3663 if (oparg & 0x02) {
3664 assert(PyDict_CheckExact(TOP()));
3665 func->func_kwdefaults = POP();
3666 }
3667 if (oparg & 0x01) {
3668 assert(PyTuple_CheckExact(TOP()));
3669 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003670 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003671
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003672 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003673 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003674 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003675
Benjamin Petersonddd19492018-09-16 22:38:02 -07003676 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003677 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003678 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003679 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003680 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003681 step = NULL;
3682 stop = POP();
3683 start = TOP();
3684 slice = PySlice_New(start, stop, step);
3685 Py_DECREF(start);
3686 Py_DECREF(stop);
3687 Py_XDECREF(step);
3688 SET_TOP(slice);
3689 if (slice == NULL)
3690 goto error;
3691 DISPATCH();
3692 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003693
Benjamin Petersonddd19492018-09-16 22:38:02 -07003694 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003695 /* Handles f-string value formatting. */
3696 PyObject *result;
3697 PyObject *fmt_spec;
3698 PyObject *value;
3699 PyObject *(*conv_fn)(PyObject *);
3700 int which_conversion = oparg & FVC_MASK;
3701 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3702
3703 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003704 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003705
3706 /* See if any conversion is specified. */
3707 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003708 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003709 case FVC_STR: conv_fn = PyObject_Str; break;
3710 case FVC_REPR: conv_fn = PyObject_Repr; break;
3711 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003712 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003713 _PyErr_Format(tstate, PyExc_SystemError,
3714 "unexpected conversion flag %d",
3715 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003716 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003717 }
3718
3719 /* If there's a conversion function, call it and replace
3720 value with that result. Otherwise, just use value,
3721 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003722 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003723 result = conv_fn(value);
3724 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003725 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003726 Py_XDECREF(fmt_spec);
3727 goto error;
3728 }
3729 value = result;
3730 }
3731
3732 /* If value is a unicode object, and there's no fmt_spec,
3733 then we know the result of format(value) is value
3734 itself. In that case, skip calling format(). I plan to
3735 move this optimization in to PyObject_Format()
3736 itself. */
3737 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3738 /* Do nothing, just transfer ownership to result. */
3739 result = value;
3740 } else {
3741 /* Actually call format(). */
3742 result = PyObject_Format(value, fmt_spec);
3743 Py_DECREF(value);
3744 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003745 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003746 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003747 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003748 }
3749
Eric V. Smith135d5f42016-02-05 18:23:08 -05003750 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003751 DISPATCH();
3752 }
3753
Benjamin Petersonddd19492018-09-16 22:38:02 -07003754 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003755 int oldoparg = oparg;
3756 NEXTOPARG();
3757 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003758 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003759 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003760
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003761
Antoine Pitrou042b1282010-08-13 21:15:58 +00003762#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003763 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003764#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003765 default:
3766 fprintf(stderr,
3767 "XXX lineno: %d, opcode: %d\n",
3768 PyFrame_GetLineNumber(f),
3769 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003770 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003771 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003773 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003774
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003775 /* This should never be reached. Every opcode should end with DISPATCH()
3776 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003777 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003778
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003779error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003780 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003781#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003782 if (!_PyErr_Occurred(tstate)) {
3783 _PyErr_SetString(tstate, PyExc_SystemError,
3784 "error return without exception set");
3785 }
Victor Stinner365b6932013-07-12 00:11:58 +02003786#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003787 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003788#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003789
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003790 /* Log traceback info. */
3791 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003792
Benjamin Peterson51f46162013-01-23 08:38:47 -05003793 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003794 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3795 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003796
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003797exception_unwind:
3798 /* Unwind stacks if an exception occurred */
3799 while (f->f_iblock > 0) {
3800 /* Pop the current block. */
3801 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003803 if (b->b_type == EXCEPT_HANDLER) {
3804 UNWIND_EXCEPT_HANDLER(b);
3805 continue;
3806 }
3807 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003808 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003809 PyObject *exc, *val, *tb;
3810 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003811 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003812 /* Beware, this invalidates all b->b_* fields */
3813 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003814 PUSH(exc_info->exc_traceback);
3815 PUSH(exc_info->exc_value);
3816 if (exc_info->exc_type != NULL) {
3817 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003818 }
3819 else {
3820 Py_INCREF(Py_None);
3821 PUSH(Py_None);
3822 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003823 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003824 /* Make the raw exception data
3825 available to the handler,
3826 so a program can emulate the
3827 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003828 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003829 if (tb != NULL)
3830 PyException_SetTraceback(val, tb);
3831 else
3832 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003833 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003834 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003835 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003836 exc_info->exc_value = val;
3837 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003838 if (tb == NULL)
3839 tb = Py_None;
3840 Py_INCREF(tb);
3841 PUSH(tb);
3842 PUSH(val);
3843 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003844 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003845 /* Resume normal execution */
3846 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003847 }
3848 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003849
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003850 /* End the loop as we still have an error */
3851 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003852 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003853
Pablo Galindof00828a2019-05-09 16:52:02 +01003854 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003855 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003856
3857exit_returning:
3858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003859 /* Pop remaining stack entries. */
3860 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003861 PyObject *o = POP();
3862 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003863 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003864
Pablo Galindof00828a2019-05-09 16:52:02 +01003865exit_yielding:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003866 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003867 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003868 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3869 tstate, f, PyTrace_RETURN, retval)) {
3870 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003871 }
3872 }
3873 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003874 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3875 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003876 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003877 }
3878 }
3879 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003881 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003882exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003883 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3884 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003885 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003886 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003887 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003888
Victor Stinnerefde1462015-03-21 15:04:43 +01003889 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003890}
3891
Benjamin Petersonb204a422011-06-05 22:04:07 -05003892static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003893format_missing(PyThreadState *tstate, const char *kind,
3894 PyCodeObject *co, PyObject *names)
Benjamin Petersone109c702011-06-24 09:37:26 -05003895{
3896 int err;
3897 Py_ssize_t len = PyList_GET_SIZE(names);
3898 PyObject *name_str, *comma, *tail, *tmp;
3899
3900 assert(PyList_CheckExact(names));
3901 assert(len >= 1);
3902 /* Deal with the joys of natural language. */
3903 switch (len) {
3904 case 1:
3905 name_str = PyList_GET_ITEM(names, 0);
3906 Py_INCREF(name_str);
3907 break;
3908 case 2:
3909 name_str = PyUnicode_FromFormat("%U and %U",
3910 PyList_GET_ITEM(names, len - 2),
3911 PyList_GET_ITEM(names, len - 1));
3912 break;
3913 default:
3914 tail = PyUnicode_FromFormat(", %U, and %U",
3915 PyList_GET_ITEM(names, len - 2),
3916 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003917 if (tail == NULL)
3918 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003919 /* Chop off the last two objects in the list. This shouldn't actually
3920 fail, but we can't be too careful. */
3921 err = PyList_SetSlice(names, len - 2, len, NULL);
3922 if (err == -1) {
3923 Py_DECREF(tail);
3924 return;
3925 }
3926 /* Stitch everything up into a nice comma-separated list. */
3927 comma = PyUnicode_FromString(", ");
3928 if (comma == NULL) {
3929 Py_DECREF(tail);
3930 return;
3931 }
3932 tmp = PyUnicode_Join(comma, names);
3933 Py_DECREF(comma);
3934 if (tmp == NULL) {
3935 Py_DECREF(tail);
3936 return;
3937 }
3938 name_str = PyUnicode_Concat(tmp, tail);
3939 Py_DECREF(tmp);
3940 Py_DECREF(tail);
3941 break;
3942 }
3943 if (name_str == NULL)
3944 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003945 _PyErr_Format(tstate, PyExc_TypeError,
3946 "%U() missing %i required %s argument%s: %U",
3947 co->co_name,
3948 len,
3949 kind,
3950 len == 1 ? "" : "s",
3951 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003952 Py_DECREF(name_str);
3953}
3954
3955static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003956missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3957 Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003958 PyObject **fastlocals)
3959{
Victor Stinner74319ae2016-08-25 00:04:09 +02003960 Py_ssize_t i, j = 0;
3961 Py_ssize_t start, end;
3962 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003963 const char *kind = positional ? "positional" : "keyword-only";
3964 PyObject *missing_names;
3965
3966 /* Compute the names of the arguments that are missing. */
3967 missing_names = PyList_New(missing);
3968 if (missing_names == NULL)
3969 return;
3970 if (positional) {
3971 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01003972 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003973 }
3974 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003975 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003976 end = start + co->co_kwonlyargcount;
3977 }
3978 for (i = start; i < end; i++) {
3979 if (GETLOCAL(i) == NULL) {
3980 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3981 PyObject *name = PyObject_Repr(raw);
3982 if (name == NULL) {
3983 Py_DECREF(missing_names);
3984 return;
3985 }
3986 PyList_SET_ITEM(missing_names, j++, name);
3987 }
3988 }
3989 assert(j == missing);
Victor Stinner438a12d2019-05-24 17:01:38 +02003990 format_missing(tstate, kind, co, missing_names);
Benjamin Petersone109c702011-06-24 09:37:26 -05003991 Py_DECREF(missing_names);
3992}
3993
3994static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003995too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3996 Py_ssize_t given, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003997 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003998{
3999 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02004000 Py_ssize_t kwonly_given = 0;
4001 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004002 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02004003 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004004
Benjamin Petersone109c702011-06-24 09:37:26 -05004005 assert((co->co_flags & CO_VARARGS) == 0);
4006 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01004007 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004008 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004009 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02004010 }
4011 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004012 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01004013 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004014 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01004015 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004016 }
4017 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004018 plural = (co_argcount != 1);
4019 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004020 }
4021 if (sig == NULL)
4022 return;
4023 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004024 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
4025 kwonly_sig = PyUnicode_FromFormat(format,
4026 given != 1 ? "s" : "",
4027 kwonly_given,
4028 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004029 if (kwonly_sig == NULL) {
4030 Py_DECREF(sig);
4031 return;
4032 }
4033 }
4034 else {
4035 /* This will not fail. */
4036 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05004037 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004038 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004039 _PyErr_Format(tstate, PyExc_TypeError,
4040 "%U() takes %U positional argument%s but %zd%U %s given",
4041 co->co_name,
4042 sig,
4043 plural ? "s" : "",
4044 given,
4045 kwonly_sig,
4046 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004047 Py_DECREF(sig);
4048 Py_DECREF(kwonly_sig);
4049}
4050
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004051static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004052positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
4053 Py_ssize_t kwcount, PyObject* const* kwnames)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004054{
4055 int posonly_conflicts = 0;
4056 PyObject* posonly_names = PyList_New(0);
4057
4058 for(int k=0; k < co->co_posonlyargcount; k++){
4059 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
4060
4061 for (int k2=0; k2<kwcount; k2++){
4062 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
4063 PyObject* kwname = kwnames[k2];
4064 if (kwname == posonly_name){
4065 if(PyList_Append(posonly_names, kwname) != 0) {
4066 goto fail;
4067 }
4068 posonly_conflicts++;
4069 continue;
4070 }
4071
4072 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
4073
4074 if ( cmp > 0) {
4075 if(PyList_Append(posonly_names, kwname) != 0) {
4076 goto fail;
4077 }
4078 posonly_conflicts++;
4079 } else if (cmp < 0) {
4080 goto fail;
4081 }
4082
4083 }
4084 }
4085 if (posonly_conflicts) {
4086 PyObject* comma = PyUnicode_FromString(", ");
4087 if (comma == NULL) {
4088 goto fail;
4089 }
4090 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4091 Py_DECREF(comma);
4092 if (error_names == NULL) {
4093 goto fail;
4094 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004095 _PyErr_Format(tstate, PyExc_TypeError,
4096 "%U() got some positional-only arguments passed"
4097 " as keyword arguments: '%U'",
4098 co->co_name, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004099 Py_DECREF(error_names);
4100 goto fail;
4101 }
4102
4103 Py_DECREF(posonly_names);
4104 return 0;
4105
4106fail:
4107 Py_XDECREF(posonly_names);
4108 return 1;
4109
4110}
4111
Guido van Rossumc2e20742006-02-27 22:32:47 +00004112/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02004113 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00004114 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004115
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004116PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02004117_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004118 PyObject *const *args, Py_ssize_t argcount,
4119 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004120 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004121 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02004122 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004123 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00004124{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004125 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004126 PyFrameObject *f;
4127 PyObject *retval = NULL;
4128 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004129 PyObject *x, *u;
Pablo Galindocd74e662019-06-01 18:08:04 +01004130 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004131 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02004132 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00004133
Victor Stinner438a12d2019-05-24 17:01:38 +02004134 PyThreadState *tstate = _PyThreadState_GET();
4135 assert(tstate != NULL);
4136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004137 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004138 _PyErr_SetString(tstate, PyExc_SystemError,
4139 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004140 return NULL;
4141 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004142
Victor Stinnerc7020012016-08-16 23:40:29 +02004143 /* Create the frame */
INADA Naoki5a625d02016-12-24 20:19:08 +09004144 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004145 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004146 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004147 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004148 fastlocals = f->f_localsplus;
4149 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004150
Victor Stinnerc7020012016-08-16 23:40:29 +02004151 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004152 if (co->co_flags & CO_VARKEYWORDS) {
4153 kwdict = PyDict_New();
4154 if (kwdict == NULL)
4155 goto fail;
4156 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004157 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004158 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004159 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004160 SETLOCAL(i, kwdict);
4161 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004162 else {
4163 kwdict = NULL;
4164 }
4165
Pablo Galindocd74e662019-06-01 18:08:04 +01004166 /* Copy all positional arguments into local variables */
4167 if (argcount > co->co_argcount) {
4168 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004169 }
4170 else {
4171 n = argcount;
4172 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004173 for (j = 0; j < n; j++) {
4174 x = args[j];
4175 Py_INCREF(x);
4176 SETLOCAL(j, x);
4177 }
4178
Victor Stinnerc7020012016-08-16 23:40:29 +02004179 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004180 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05004181 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004182 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004183 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004184 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004185 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004186 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004187
Serhiy Storchakab7281052016-09-12 00:52:40 +03004188 /* Handle keyword arguments passed as two strided arrays */
4189 kwcount *= kwstep;
4190 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004191 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004192 PyObject *keyword = kwnames[i];
4193 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004194 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004195
Benjamin Petersonb204a422011-06-05 22:04:07 -05004196 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004197 _PyErr_Format(tstate, PyExc_TypeError,
4198 "%U() keywords must be strings",
4199 co->co_name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004200 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004201 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004202
Benjamin Petersonb204a422011-06-05 22:04:07 -05004203 /* Speed hack: do raw pointer compares. As names are
4204 normally interned this should almost always hit. */
4205 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004206 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004207 PyObject *name = co_varnames[j];
4208 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004209 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004210 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004211 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004212
Benjamin Petersonb204a422011-06-05 22:04:07 -05004213 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004214 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004215 PyObject *name = co_varnames[j];
4216 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4217 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004218 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004219 }
4220 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004221 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004222 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004223 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004224
Victor Stinner231d1f32017-01-11 02:12:06 +01004225 assert(j >= total_args);
4226 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004227
Victor Stinner438a12d2019-05-24 17:01:38 +02004228 if (co->co_posonlyargcount
4229 && positional_only_passed_as_keyword(tstate, co,
4230 kwcount, kwnames))
4231 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004232 goto fail;
4233 }
4234
Victor Stinner438a12d2019-05-24 17:01:38 +02004235 _PyErr_Format(tstate, PyExc_TypeError,
4236 "%U() got an unexpected keyword argument '%S'",
4237 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004238 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004239 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004240
Christian Heimes0bd447f2013-07-20 14:48:10 +02004241 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4242 goto fail;
4243 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004244 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004245
Benjamin Petersonb204a422011-06-05 22:04:07 -05004246 kw_found:
4247 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004248 _PyErr_Format(tstate, PyExc_TypeError,
4249 "%U() got multiple values for argument '%S'",
4250 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004251 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004252 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004253 Py_INCREF(value);
4254 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004255 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004256
4257 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004258 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004259 too_many_positional(tstate, co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004260 goto fail;
4261 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004262
4263 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004264 if (argcount < co->co_argcount) {
4265 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004266 Py_ssize_t missing = 0;
4267 for (i = argcount; i < m; i++) {
4268 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004269 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004270 }
4271 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004272 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004273 missing_arguments(tstate, co, missing, defcount, fastlocals);
Benjamin Petersone109c702011-06-24 09:37:26 -05004274 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004275 }
4276 if (n > m)
4277 i = n - m;
4278 else
4279 i = 0;
4280 for (; i < defcount; i++) {
4281 if (GETLOCAL(m+i) == NULL) {
4282 PyObject *def = defs[i];
4283 Py_INCREF(def);
4284 SETLOCAL(m+i, def);
4285 }
4286 }
4287 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004288
4289 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004290 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004291 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004292 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004293 PyObject *name;
4294 if (GETLOCAL(i) != NULL)
4295 continue;
4296 name = PyTuple_GET_ITEM(co->co_varnames, i);
4297 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004298 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004299 if (def) {
4300 Py_INCREF(def);
4301 SETLOCAL(i, def);
4302 continue;
4303 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004304 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004305 goto fail;
4306 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004307 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004308 missing++;
4309 }
4310 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004311 missing_arguments(tstate, co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004312 goto fail;
4313 }
4314 }
4315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004316 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004317 vars into frame. */
4318 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004319 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004320 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004321 /* Possibly account for the cell variable being an argument. */
4322 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004323 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004324 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004325 /* Clear the local copy. */
4326 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004327 }
4328 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004329 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004330 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004331 if (c == NULL)
4332 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004333 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004334 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004335
4336 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004337 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4338 PyObject *o = PyTuple_GET_ITEM(closure, i);
4339 Py_INCREF(o);
4340 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004341 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004342
Yury Selivanoveb636452016-09-08 22:01:51 -07004343 /* Handle generator/coroutine/asynchronous generator */
4344 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004345 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004346 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004348 /* Don't need to keep the reference to f_back, it will be set
4349 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004350 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004352 /* Create a new generator that owns the ready to run frame
4353 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004354 if (is_coro) {
4355 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004356 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4357 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004358 } else {
4359 gen = PyGen_NewWithQualName(f, name, qualname);
4360 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004361 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004362 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004363 }
INADA Naoki9c157762016-12-26 18:52:46 +09004364
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004365 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004366
Yury Selivanov75445082015-05-11 22:57:16 -04004367 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004368 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004369
Victor Stinner59a73272016-12-09 18:51:13 +01004370 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004371
Thomas Woutersce272b62007-09-19 21:19:28 +00004372fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004374 /* decref'ing the frame can cause __del__ methods to get invoked,
4375 which can call back into Python. While we're done with the
4376 current Python frame (f), the associated C stack is still in use,
4377 so recursion_depth must be boosted for the duration.
4378 */
4379 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09004380 if (Py_REFCNT(f) > 1) {
4381 Py_DECREF(f);
4382 _PyObject_GC_TRACK(f);
4383 }
4384 else {
4385 ++tstate->recursion_depth;
4386 Py_DECREF(f);
4387 --tstate->recursion_depth;
4388 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004389 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004390}
4391
Victor Stinner40ee3012014-06-16 15:59:28 +02004392PyObject *
4393PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004394 PyObject *const *args, int argcount,
4395 PyObject *const *kws, int kwcount,
4396 PyObject *const *defs, int defcount,
4397 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004398{
4399 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004400 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004401 kws, kws != NULL ? kws + 1 : NULL,
4402 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004403 defs, defcount,
4404 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004405 NULL, NULL);
4406}
Tim Peters5ca576e2001-06-18 22:08:13 +00004407
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004408static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004409special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004410{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004411 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004412 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004413 if (res == NULL && !_PyErr_Occurred(tstate)) {
4414 _PyErr_SetObject(tstate, PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004415 return NULL;
4416 }
4417 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004418}
4419
4420
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004421/* Logic for the raise statement (too complicated for inlining).
4422 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004423static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004424do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004425{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004426 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004428 if (exc == NULL) {
4429 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004430 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004431 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004432 type = exc_info->exc_type;
4433 value = exc_info->exc_value;
4434 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004435 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004436 _PyErr_SetString(tstate, PyExc_RuntimeError,
4437 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004438 return 0;
4439 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004440 Py_XINCREF(type);
4441 Py_XINCREF(value);
4442 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004443 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004444 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004445 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004447 /* We support the following forms of raise:
4448 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004449 raise <instance>
4450 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004452 if (PyExceptionClass_Check(exc)) {
4453 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004454 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004455 if (value == NULL)
4456 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004457 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004458 _PyErr_Format(tstate, PyExc_TypeError,
4459 "calling %R should have returned an instance of "
4460 "BaseException, not %R",
4461 type, Py_TYPE(value));
4462 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004463 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004464 }
4465 else if (PyExceptionInstance_Check(exc)) {
4466 value = exc;
4467 type = PyExceptionInstance_Class(exc);
4468 Py_INCREF(type);
4469 }
4470 else {
4471 /* Not something you can raise. You get an exception
4472 anyway, just not what you specified :-) */
4473 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004474 _PyErr_SetString(tstate, PyExc_TypeError,
4475 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004476 goto raise_error;
4477 }
Collin Winter828f04a2007-08-31 00:04:24 +00004478
Serhiy Storchakac0191582016-09-27 11:37:10 +03004479 assert(type != NULL);
4480 assert(value != NULL);
4481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004482 if (cause) {
4483 PyObject *fixed_cause;
4484 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004485 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004486 if (fixed_cause == NULL)
4487 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004488 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004490 else if (PyExceptionInstance_Check(cause)) {
4491 fixed_cause = cause;
4492 }
4493 else if (cause == Py_None) {
4494 Py_DECREF(cause);
4495 fixed_cause = NULL;
4496 }
4497 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004498 _PyErr_SetString(tstate, PyExc_TypeError,
4499 "exception causes must derive from "
4500 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004501 goto raise_error;
4502 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004503 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004504 }
Collin Winter828f04a2007-08-31 00:04:24 +00004505
Victor Stinner438a12d2019-05-24 17:01:38 +02004506 _PyErr_SetObject(tstate, type, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004507 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004508 Py_DECREF(value);
4509 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004510 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004511
4512raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004513 Py_XDECREF(value);
4514 Py_XDECREF(type);
4515 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004516 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004517}
4518
Tim Petersd6d010b2001-06-21 02:49:55 +00004519/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004520 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004521
Guido van Rossum0368b722007-05-11 16:50:42 +00004522 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4523 with a variable target.
4524*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004525
Barry Warsawe42b18f1997-08-25 22:13:04 +00004526static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004527unpack_iterable(PyThreadState *tstate, PyObject *v,
4528 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004529{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004530 int i = 0, j = 0;
4531 Py_ssize_t ll = 0;
4532 PyObject *it; /* iter(v) */
4533 PyObject *w;
4534 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004536 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004538 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004539 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004540 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004541 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4542 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004543 _PyErr_Format(tstate, PyExc_TypeError,
4544 "cannot unpack non-iterable %.200s object",
4545 v->ob_type->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004546 }
4547 return 0;
4548 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004550 for (; i < argcnt; i++) {
4551 w = PyIter_Next(it);
4552 if (w == NULL) {
4553 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004554 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004555 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004556 _PyErr_Format(tstate, PyExc_ValueError,
4557 "not enough values to unpack "
4558 "(expected %d, got %d)",
4559 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004560 }
4561 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004562 _PyErr_Format(tstate, PyExc_ValueError,
4563 "not enough values to unpack "
4564 "(expected at least %d, got %d)",
4565 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004566 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004567 }
4568 goto Error;
4569 }
4570 *--sp = w;
4571 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004573 if (argcntafter == -1) {
4574 /* We better have exhausted the iterator now. */
4575 w = PyIter_Next(it);
4576 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004577 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004578 goto Error;
4579 Py_DECREF(it);
4580 return 1;
4581 }
4582 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004583 _PyErr_Format(tstate, PyExc_ValueError,
4584 "too many values to unpack (expected %d)",
4585 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004586 goto Error;
4587 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004589 l = PySequence_List(it);
4590 if (l == NULL)
4591 goto Error;
4592 *--sp = l;
4593 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004595 ll = PyList_GET_SIZE(l);
4596 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004597 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004598 "not enough values to unpack (expected at least %d, got %zd)",
4599 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004600 goto Error;
4601 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004603 /* Pop the "after-variable" args off the list. */
4604 for (j = argcntafter; j > 0; j--, i++) {
4605 *--sp = PyList_GET_ITEM(l, ll - j);
4606 }
4607 /* Resize the list. */
4608 Py_SIZE(l) = ll - argcntafter;
4609 Py_DECREF(it);
4610 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004611
Tim Petersd6d010b2001-06-21 02:49:55 +00004612Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004613 for (; i > 0; i--, sp++)
4614 Py_DECREF(*sp);
4615 Py_XDECREF(it);
4616 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004617}
4618
4619
Guido van Rossum96a42c81992-01-12 02:29:51 +00004620#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004621static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004622prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004623{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004624 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004625 if (PyObject_Print(v, stdout, 0) != 0) {
4626 /* Don't know what else to do */
4627 _PyErr_Clear(tstate);
4628 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004629 printf("\n");
4630 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004631}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004632#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004633
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004634static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004635call_exc_trace(Py_tracefunc func, PyObject *self,
4636 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004637{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004638 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004639 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004640 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004641 if (value == NULL) {
4642 value = Py_None;
4643 Py_INCREF(value);
4644 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004645 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004646 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004647 arg = PyTuple_Pack(3, type, value, traceback);
4648 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004649 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004650 return;
4651 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004652 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004653 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004654 if (err == 0) {
4655 _PyErr_Restore(tstate, type, value, orig_traceback);
4656 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004657 else {
4658 Py_XDECREF(type);
4659 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004660 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004661 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004662}
4663
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004664static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004665call_trace_protected(Py_tracefunc func, PyObject *obj,
4666 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004667 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004668{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004669 PyObject *type, *value, *traceback;
4670 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004671 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004672 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004673 if (err == 0)
4674 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004675 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004676 return 0;
4677 }
4678 else {
4679 Py_XDECREF(type);
4680 Py_XDECREF(value);
4681 Py_XDECREF(traceback);
4682 return -1;
4683 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004684}
4685
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004686static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004687call_trace(Py_tracefunc func, PyObject *obj,
4688 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004689 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004690{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004691 int result;
4692 if (tstate->tracing)
4693 return 0;
4694 tstate->tracing++;
4695 tstate->use_tracing = 0;
4696 result = func(obj, frame, what, arg);
4697 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4698 || (tstate->c_profilefunc != NULL));
4699 tstate->tracing--;
4700 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004701}
4702
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004703PyObject *
4704_PyEval_CallTracing(PyObject *func, PyObject *args)
4705{
Victor Stinner50b48572018-11-01 01:51:40 +01004706 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004707 int save_tracing = tstate->tracing;
4708 int save_use_tracing = tstate->use_tracing;
4709 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004711 tstate->tracing = 0;
4712 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4713 || (tstate->c_profilefunc != NULL));
4714 result = PyObject_Call(func, args, NULL);
4715 tstate->tracing = save_tracing;
4716 tstate->use_tracing = save_use_tracing;
4717 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004718}
4719
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004720/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004721static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004722maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004723 PyThreadState *tstate, PyFrameObject *frame,
4724 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004725{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004726 int result = 0;
4727 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004729 /* If the last instruction executed isn't in the current
4730 instruction window, reset the window.
4731 */
4732 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4733 PyAddrPair bounds;
4734 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4735 &bounds);
4736 *instr_lb = bounds.ap_lower;
4737 *instr_ub = bounds.ap_upper;
4738 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004739 /* If the last instruction falls at the start of a line or if it
4740 represents a jump backwards, update the frame's line number and
4741 then call the trace function if we're tracing source lines.
4742 */
4743 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004744 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004745 if (frame->f_trace_lines) {
4746 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4747 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004748 }
George King20faa682017-10-18 17:44:22 -07004749 /* Always emit an opcode event if we're tracing all opcodes. */
4750 if (frame->f_trace_opcodes) {
4751 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4752 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004753 *instr_prev = frame->f_lasti;
4754 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004755}
4756
Fred Drake5755ce62001-06-27 19:19:46 +00004757void
4758PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004759{
Steve Dowerb82e17e2019-05-23 08:45:22 -07004760 if (PySys_Audit("sys.setprofile", NULL) < 0) {
4761 return;
4762 }
4763
Victor Stinner50b48572018-11-01 01:51:40 +01004764 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004765 PyObject *temp = tstate->c_profileobj;
4766 Py_XINCREF(arg);
4767 tstate->c_profilefunc = NULL;
4768 tstate->c_profileobj = NULL;
4769 /* Must make sure that tracing is not ignored if 'temp' is freed */
4770 tstate->use_tracing = tstate->c_tracefunc != NULL;
4771 Py_XDECREF(temp);
4772 tstate->c_profilefunc = func;
4773 tstate->c_profileobj = arg;
4774 /* Flag that tracing or profiling is turned on */
4775 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004776}
4777
4778void
4779PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4780{
Steve Dowerb82e17e2019-05-23 08:45:22 -07004781 if (PySys_Audit("sys.settrace", NULL) < 0) {
4782 return;
4783 }
4784
Victor Stinner09532fe2019-05-10 23:39:09 +02004785 _PyRuntimeState *runtime = &_PyRuntime;
4786 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004787 PyObject *temp = tstate->c_traceobj;
Victor Stinner09532fe2019-05-10 23:39:09 +02004788 runtime->ceval.tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004789 Py_XINCREF(arg);
4790 tstate->c_tracefunc = NULL;
4791 tstate->c_traceobj = NULL;
4792 /* Must make sure that profiling is not ignored if 'temp' is freed */
4793 tstate->use_tracing = tstate->c_profilefunc != NULL;
4794 Py_XDECREF(temp);
4795 tstate->c_tracefunc = func;
4796 tstate->c_traceobj = arg;
4797 /* Flag that tracing or profiling is turned on */
4798 tstate->use_tracing = ((func != NULL)
4799 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004800}
4801
Yury Selivanov75445082015-05-11 22:57:16 -04004802void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004803_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4804{
4805 assert(new_depth >= 0);
Victor Stinner50b48572018-11-01 01:51:40 +01004806 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004807 tstate->coroutine_origin_tracking_depth = new_depth;
4808}
4809
4810int
4811_PyEval_GetCoroutineOriginTrackingDepth(void)
4812{
Victor Stinner50b48572018-11-01 01:51:40 +01004813 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004814 return tstate->coroutine_origin_tracking_depth;
4815}
4816
4817void
Yury Selivanoveb636452016-09-08 22:01:51 -07004818_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4819{
Victor Stinner50b48572018-11-01 01:51:40 +01004820 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004821
4822 if (PySys_Audit("sys.set_asyncgen_hook_firstiter", NULL) < 0) {
4823 return;
4824 }
4825
Yury Selivanoveb636452016-09-08 22:01:51 -07004826 Py_XINCREF(firstiter);
4827 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4828}
4829
4830PyObject *
4831_PyEval_GetAsyncGenFirstiter(void)
4832{
Victor Stinner50b48572018-11-01 01:51:40 +01004833 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004834 return tstate->async_gen_firstiter;
4835}
4836
4837void
4838_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4839{
Victor Stinner50b48572018-11-01 01:51:40 +01004840 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004841
4842 if (PySys_Audit("sys.set_asyncgen_hook_finalizer", NULL) < 0) {
4843 return;
4844 }
4845
Yury Selivanoveb636452016-09-08 22:01:51 -07004846 Py_XINCREF(finalizer);
4847 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4848}
4849
4850PyObject *
4851_PyEval_GetAsyncGenFinalizer(void)
4852{
Victor Stinner50b48572018-11-01 01:51:40 +01004853 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004854 return tstate->async_gen_finalizer;
4855}
4856
Victor Stinner438a12d2019-05-24 17:01:38 +02004857static PyFrameObject *
4858_PyEval_GetFrame(PyThreadState *tstate)
4859{
4860 return _PyRuntime.gilstate.getframe(tstate);
4861}
4862
4863PyFrameObject *
4864PyEval_GetFrame(void)
4865{
4866 PyThreadState *tstate = _PyThreadState_GET();
4867 return _PyEval_GetFrame(tstate);
4868}
4869
Guido van Rossumb209a111997-04-29 18:18:01 +00004870PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004871PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004872{
Victor Stinner438a12d2019-05-24 17:01:38 +02004873 PyThreadState *tstate = _PyThreadState_GET();
4874 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004875 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004876 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004877 else
4878 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004879}
4880
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004881/* Convenience function to get a builtin from its name */
4882PyObject *
4883_PyEval_GetBuiltinId(_Py_Identifier *name)
4884{
Victor Stinner438a12d2019-05-24 17:01:38 +02004885 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004886 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4887 if (attr) {
4888 Py_INCREF(attr);
4889 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004890 else if (!_PyErr_Occurred(tstate)) {
4891 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004892 }
4893 return attr;
4894}
4895
Guido van Rossumb209a111997-04-29 18:18:01 +00004896PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004897PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004898{
Victor Stinner438a12d2019-05-24 17:01:38 +02004899 PyThreadState *tstate = _PyThreadState_GET();
4900 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Victor Stinner41bb43a2013-10-29 01:19:37 +01004901 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004902 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004903 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004904 }
4905
Victor Stinner438a12d2019-05-24 17:01:38 +02004906 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004907 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004908 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004909
4910 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004911 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004912}
4913
Guido van Rossumb209a111997-04-29 18:18:01 +00004914PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004915PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004916{
Victor Stinner438a12d2019-05-24 17:01:38 +02004917 PyThreadState *tstate = _PyThreadState_GET();
4918 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
4919 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004920 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004921 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004922
4923 assert(current_frame->f_globals != NULL);
4924 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004925}
4926
Guido van Rossum6135a871995-01-09 17:53:26 +00004927int
Tim Peters5ba58662001-07-16 02:29:45 +00004928PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004929{
Victor Stinner438a12d2019-05-24 17:01:38 +02004930 PyThreadState *tstate = _PyThreadState_GET();
4931 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004932 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004934 if (current_frame != NULL) {
4935 const int codeflags = current_frame->f_code->co_flags;
4936 const int compilerflags = codeflags & PyCF_MASK;
4937 if (compilerflags) {
4938 result = 1;
4939 cf->cf_flags |= compilerflags;
4940 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004941#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004942 if (codeflags & CO_GENERATOR_ALLOWED) {
4943 result = 1;
4944 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4945 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004946#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004947 }
4948 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004949}
4950
Guido van Rossum3f5da241990-12-20 15:06:42 +00004951
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004952const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004953PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004954{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004955 if (PyMethod_Check(func))
4956 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4957 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004958 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004959 else if (PyCFunction_Check(func))
4960 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4961 else
4962 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004963}
4964
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004965const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004966PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004967{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004968 if (PyMethod_Check(func))
4969 return "()";
4970 else if (PyFunction_Check(func))
4971 return "()";
4972 else if (PyCFunction_Check(func))
4973 return "()";
4974 else
4975 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004976}
4977
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004978#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004979if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004980 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4981 tstate, tstate->frame, \
4982 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004983 x = NULL; \
4984 } \
4985 else { \
4986 x = call; \
4987 if (tstate->c_profilefunc != NULL) { \
4988 if (x == NULL) { \
4989 call_trace_protected(tstate->c_profilefunc, \
4990 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004991 tstate, tstate->frame, \
4992 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004993 /* XXX should pass (type, value, tb) */ \
4994 } else { \
4995 if (call_trace(tstate->c_profilefunc, \
4996 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004997 tstate, tstate->frame, \
4998 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004999 Py_DECREF(x); \
5000 x = NULL; \
5001 } \
5002 } \
5003 } \
5004 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00005005} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005006 x = call; \
5007 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00005008
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005009
5010static PyObject *
5011trace_call_function(PyThreadState *tstate,
5012 PyObject *func,
5013 PyObject **args, Py_ssize_t nargs,
5014 PyObject *kwnames)
5015{
5016 PyObject *x;
5017 if (PyCFunction_Check(func)) {
Jeroen Demeyer37788bc2019-05-30 15:11:22 +02005018 C_TRACE(x, _PyCFunction_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005019 return x;
5020 }
5021 else if (Py_TYPE(func) == &PyMethodDescr_Type && nargs > 0) {
5022 /* We need to create a temporary bound method as argument
5023 for profiling.
5024
5025 If nargs == 0, then this cannot work because we have no
5026 "self". In any case, the call itself would raise
5027 TypeError (foo needs an argument), so we just skip
5028 profiling. */
5029 PyObject *self = args[0];
5030 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5031 if (func == NULL) {
5032 return NULL;
5033 }
Jeroen Demeyer37788bc2019-05-30 15:11:22 +02005034 C_TRACE(x, _PyCFunction_Vectorcall(func,
5035 args+1, nargs-1,
5036 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005037 Py_DECREF(func);
5038 return x;
5039 }
5040 return _PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
5041}
5042
Victor Stinner415c5102017-01-11 00:54:57 +01005043/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
5044 to reduce the stack consumption. */
5045Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02005046call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005047{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005048 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005049 PyObject *func = *pfunc;
5050 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005051 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5052 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005053 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005054
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005055 if (tstate->use_tracing) {
5056 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005057 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005058 else {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005059 x = _PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005060 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005061
Victor Stinner438a12d2019-05-24 17:01:38 +02005062 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005063
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005064 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005065 while ((*pp_stack) > pfunc) {
5066 w = EXT_POP(*pp_stack);
5067 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005068 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005070 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005071}
5072
Jeremy Hylton52820442001-01-03 23:52:36 +00005073static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02005074do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005075{
jdemeyere89de732018-09-19 12:06:20 +02005076 PyObject *result;
5077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005078 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005079 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005080 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005081 }
jdemeyere89de732018-09-19 12:06:20 +02005082 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
jdemeyere89de732018-09-19 12:06:20 +02005083 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
5084 if (nargs > 0 && tstate->use_tracing) {
5085 /* We need to create a temporary bound method as argument
5086 for profiling.
5087
5088 If nargs == 0, then this cannot work because we have no
5089 "self". In any case, the call itself would raise
5090 TypeError (foo needs an argument), so we just skip
5091 profiling. */
5092 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5093 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5094 if (func == NULL) {
5095 return NULL;
5096 }
5097
5098 C_TRACE(result, _PyCFunction_FastCallDict(func,
Victor Stinnerd17a6932018-11-09 16:56:48 +01005099 &_PyTuple_ITEMS(callargs)[1],
jdemeyere89de732018-09-19 12:06:20 +02005100 nargs - 1,
5101 kwdict));
5102 Py_DECREF(func);
5103 return result;
5104 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005105 }
jdemeyere89de732018-09-19 12:06:20 +02005106 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005107}
5108
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005109/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005110 nb_index slot defined, and store in *pi.
5111 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005112 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005113 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005114*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005115int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005116_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005117{
Victor Stinner438a12d2019-05-24 17:01:38 +02005118 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005119 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005120 Py_ssize_t x;
5121 if (PyIndex_Check(v)) {
5122 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005123 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005124 return 0;
5125 }
5126 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005127 _PyErr_SetString(tstate, PyExc_TypeError,
5128 "slice indices must be integers or "
5129 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005130 return 0;
5131 }
5132 *pi = x;
5133 }
5134 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005135}
5136
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005137int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005138_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005139{
Victor Stinner438a12d2019-05-24 17:01:38 +02005140 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005141 Py_ssize_t x;
5142 if (PyIndex_Check(v)) {
5143 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005144 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005145 return 0;
5146 }
5147 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005148 _PyErr_SetString(tstate, PyExc_TypeError,
5149 "slice indices must be integers or "
5150 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005151 return 0;
5152 }
5153 *pi = x;
5154 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005155}
5156
5157
Guido van Rossum486364b2007-06-30 05:01:58 +00005158#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005159 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00005160
Guido van Rossumb209a111997-04-29 18:18:01 +00005161static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005162cmp_outcome(PyThreadState *tstate, int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005163{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005164 int res = 0;
5165 switch (op) {
5166 case PyCmp_IS:
5167 res = (v == w);
5168 break;
5169 case PyCmp_IS_NOT:
5170 res = (v != w);
5171 break;
5172 case PyCmp_IN:
5173 res = PySequence_Contains(w, v);
5174 if (res < 0)
5175 return NULL;
5176 break;
5177 case PyCmp_NOT_IN:
5178 res = PySequence_Contains(w, v);
5179 if (res < 0)
5180 return NULL;
5181 res = !res;
5182 break;
5183 case PyCmp_EXC_MATCH:
5184 if (PyTuple_Check(w)) {
5185 Py_ssize_t i, length;
5186 length = PyTuple_Size(w);
5187 for (i = 0; i < length; i += 1) {
5188 PyObject *exc = PyTuple_GET_ITEM(w, i);
5189 if (!PyExceptionClass_Check(exc)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005190 _PyErr_SetString(tstate, PyExc_TypeError,
5191 CANNOT_CATCH_MSG);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005192 return NULL;
5193 }
5194 }
5195 }
5196 else {
5197 if (!PyExceptionClass_Check(w)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005198 _PyErr_SetString(tstate, PyExc_TypeError,
5199 CANNOT_CATCH_MSG);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005200 return NULL;
5201 }
5202 }
5203 res = PyErr_GivenExceptionMatches(v, w);
5204 break;
5205 default:
5206 return PyObject_RichCompare(v, w, op);
5207 }
5208 v = res ? Py_True : Py_False;
5209 Py_INCREF(v);
5210 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005211}
5212
Thomas Wouters52152252000-08-17 22:55:00 +00005213static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005214import_name(PyThreadState *tstate, PyFrameObject *f,
5215 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005216{
5217 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005218 PyObject *import_func, *res;
5219 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005220
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005221 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005222 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005223 if (!_PyErr_Occurred(tstate)) {
5224 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005225 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005226 return NULL;
5227 }
5228
5229 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005230 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005231 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005232 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005233 return NULL;
5234 }
5235 res = PyImport_ImportModuleLevelObject(
5236 name,
5237 f->f_globals,
5238 f->f_locals == NULL ? Py_None : f->f_locals,
5239 fromlist,
5240 ilevel);
5241 return res;
5242 }
5243
5244 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005245
5246 stack[0] = name;
5247 stack[1] = f->f_globals;
5248 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5249 stack[3] = fromlist;
5250 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005251 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005252 Py_DECREF(import_func);
5253 return res;
5254}
5255
5256static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005257import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005258{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005259 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005260 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08005261 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005262
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005263 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005264 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005265 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005266 /* Issue #17636: in case this failed because of a circular relative
5267 import, try to fallback on reading the module directly from
5268 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005269 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005270 if (pkgname == NULL) {
5271 goto error;
5272 }
Oren Milman6db70332017-09-19 14:23:01 +03005273 if (!PyUnicode_Check(pkgname)) {
5274 Py_CLEAR(pkgname);
5275 goto error;
5276 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005277 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005278 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005279 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005280 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005281 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005282 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005283 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005284 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005285 goto error;
5286 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005287 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005288 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005289 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005290 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005291 if (pkgname == NULL) {
5292 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5293 if (pkgname_or_unknown == NULL) {
5294 Py_XDECREF(pkgpath);
5295 return NULL;
5296 }
5297 } else {
5298 pkgname_or_unknown = pkgname;
5299 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005300
5301 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005302 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005303 errmsg = PyUnicode_FromFormat(
5304 "cannot import name %R from %R (unknown location)",
5305 name, pkgname_or_unknown
5306 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005307 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005308 PyErr_SetImportError(errmsg, pkgname, NULL);
5309 }
5310 else {
5311 errmsg = PyUnicode_FromFormat(
5312 "cannot import name %R from %R (%S)",
5313 name, pkgname_or_unknown, pkgpath
5314 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005315 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005316 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005317 }
5318
Xiang Zhang4830f582017-03-21 11:13:42 +08005319 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005320 Py_XDECREF(pkgname_or_unknown);
5321 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005322 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005323}
Guido van Rossumac7be682001-01-17 15:42:30 +00005324
Thomas Wouters52152252000-08-17 22:55:00 +00005325static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005326import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005327{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005328 _Py_IDENTIFIER(__all__);
5329 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005330 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005331 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005332 int skip_leading_underscores = 0;
5333 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005334
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005335 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5336 return -1; /* Unexpected error */
5337 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005338 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005339 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5340 return -1;
5341 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005342 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005343 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005344 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005345 return -1;
5346 }
5347 all = PyMapping_Keys(dict);
5348 Py_DECREF(dict);
5349 if (all == NULL)
5350 return -1;
5351 skip_leading_underscores = 1;
5352 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005354 for (pos = 0, err = 0; ; pos++) {
5355 name = PySequence_GetItem(all, pos);
5356 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005357 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005358 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005359 }
5360 else {
5361 _PyErr_Clear(tstate);
5362 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005363 break;
5364 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005365 if (!PyUnicode_Check(name)) {
5366 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5367 if (modname == NULL) {
5368 Py_DECREF(name);
5369 err = -1;
5370 break;
5371 }
5372 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005373 _PyErr_Format(tstate, PyExc_TypeError,
5374 "module __name__ must be a string, not %.100s",
5375 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005376 }
5377 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005378 _PyErr_Format(tstate, PyExc_TypeError,
5379 "%s in %U.%s must be str, not %.100s",
5380 skip_leading_underscores ? "Key" : "Item",
5381 modname,
5382 skip_leading_underscores ? "__dict__" : "__all__",
5383 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005384 }
5385 Py_DECREF(modname);
5386 Py_DECREF(name);
5387 err = -1;
5388 break;
5389 }
5390 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005391 if (PyUnicode_READY(name) == -1) {
5392 Py_DECREF(name);
5393 err = -1;
5394 break;
5395 }
5396 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5397 Py_DECREF(name);
5398 continue;
5399 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005400 }
5401 value = PyObject_GetAttr(v, name);
5402 if (value == NULL)
5403 err = -1;
5404 else if (PyDict_CheckExact(locals))
5405 err = PyDict_SetItem(locals, name, value);
5406 else
5407 err = PyObject_SetItem(locals, name, value);
5408 Py_DECREF(name);
5409 Py_XDECREF(value);
5410 if (err != 0)
5411 break;
5412 }
5413 Py_DECREF(all);
5414 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005415}
5416
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005417static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005418check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005419{
5420 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005421 _PyErr_Format(tstate, PyExc_TypeError,
5422 "%.200s%.200s argument after * "
5423 "must be an iterable, not %.200s",
5424 PyEval_GetFuncName(func),
5425 PyEval_GetFuncDesc(func),
5426 args->ob_type->tp_name);
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005427 return -1;
5428 }
5429 return 0;
5430}
5431
5432static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005433format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005434{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005435 /* _PyDict_MergeEx raises attribute
5436 * error (percolated from an attempt
5437 * to get 'keys' attribute) instead of
5438 * a type error if its second argument
5439 * is not a mapping.
5440 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005441 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
5442 _PyErr_Format(tstate, PyExc_TypeError,
5443 "%.200s%.200s argument after ** "
5444 "must be a mapping, not %.200s",
5445 PyEval_GetFuncName(func),
5446 PyEval_GetFuncDesc(func),
5447 kwargs->ob_type->tp_name);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005448 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005449 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005450 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005451 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005452 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
5453 PyObject *key = PyTuple_GET_ITEM(val, 0);
5454 if (!PyUnicode_Check(key)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005455 _PyErr_Format(tstate, PyExc_TypeError,
5456 "%.200s%.200s keywords must be strings",
5457 PyEval_GetFuncName(func),
5458 PyEval_GetFuncDesc(func));
5459 }
5460 else {
5461 _PyErr_Format(tstate, PyExc_TypeError,
5462 "%.200s%.200s got multiple "
5463 "values for keyword argument '%U'",
5464 PyEval_GetFuncName(func),
5465 PyEval_GetFuncDesc(func),
5466 key);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005467 }
5468 Py_XDECREF(exc);
5469 Py_XDECREF(val);
5470 Py_XDECREF(tb);
5471 }
5472 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005473 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005474 }
5475 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005476}
5477
Guido van Rossumac7be682001-01-17 15:42:30 +00005478static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005479format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5480 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005481{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005482 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005484 if (!obj)
5485 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005486
Serhiy Storchaka06515832016-11-20 09:13:07 +02005487 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005488 if (!obj_str)
5489 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005490
Victor Stinner438a12d2019-05-24 17:01:38 +02005491 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005492}
Guido van Rossum950361c1997-01-24 13:49:28 +00005493
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005494static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005495format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005496{
5497 PyObject *name;
5498 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005499 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005500 return;
5501 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5502 name = PyTuple_GET_ITEM(co->co_cellvars,
5503 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005504 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005505 PyExc_UnboundLocalError,
5506 UNBOUNDLOCAL_ERROR_MSG,
5507 name);
5508 } else {
5509 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5510 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005511 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005512 UNBOUNDFREE_ERROR_MSG, name);
5513 }
5514}
5515
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005516static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005517format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005518{
5519 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5520 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005521 _PyErr_Format(tstate, PyExc_TypeError,
5522 "'async with' received an object from __aenter__ "
5523 "that does not implement __await__: %.100s",
5524 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005525 }
5526 else if (prevopcode == WITH_CLEANUP_START) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005527 _PyErr_Format(tstate, PyExc_TypeError,
5528 "'async with' received an object from __aexit__ "
5529 "that does not implement __await__: %.100s",
5530 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005531 }
5532 }
5533}
5534
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005535static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005536unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005537 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005538{
5539 PyObject *res;
5540 if (Py_REFCNT(v) == 2) {
5541 /* In the common case, there are 2 references to the value
5542 * stored in 'variable' when the += is performed: one on the
5543 * value stack (in 'v') and one still stored in the
5544 * 'variable'. We try to delete the variable now to reduce
5545 * the refcnt to 1.
5546 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005547 int opcode, oparg;
5548 NEXTOPARG();
5549 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005550 case STORE_FAST:
5551 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005552 PyObject **fastlocals = f->f_localsplus;
5553 if (GETLOCAL(oparg) == v)
5554 SETLOCAL(oparg, NULL);
5555 break;
5556 }
5557 case STORE_DEREF:
5558 {
5559 PyObject **freevars = (f->f_localsplus +
5560 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005561 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005562 if (PyCell_GET(c) == v) {
5563 PyCell_SET(c, NULL);
5564 Py_DECREF(v);
5565 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005566 break;
5567 }
5568 case STORE_NAME:
5569 {
5570 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005571 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005572 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005573 if (locals && PyDict_CheckExact(locals)) {
5574 PyObject *w = PyDict_GetItemWithError(locals, name);
5575 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005576 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005577 {
5578 Py_DECREF(v);
5579 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005580 }
5581 }
5582 break;
5583 }
5584 }
5585 }
5586 res = v;
5587 PyUnicode_Append(&res, w);
5588 return res;
5589}
5590
Guido van Rossum950361c1997-01-24 13:49:28 +00005591#ifdef DYNAMIC_EXECUTION_PROFILE
5592
Skip Montanarof118cb12001-10-15 20:51:38 +00005593static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005594getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005595{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005596 int i;
5597 PyObject *l = PyList_New(256);
5598 if (l == NULL) return NULL;
5599 for (i = 0; i < 256; i++) {
5600 PyObject *x = PyLong_FromLong(a[i]);
5601 if (x == NULL) {
5602 Py_DECREF(l);
5603 return NULL;
5604 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005605 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005606 }
5607 for (i = 0; i < 256; i++)
5608 a[i] = 0;
5609 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005610}
5611
5612PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005613_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005614{
5615#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005616 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005617#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005618 int i;
5619 PyObject *l = PyList_New(257);
5620 if (l == NULL) return NULL;
5621 for (i = 0; i < 257; i++) {
5622 PyObject *x = getarray(dxpairs[i]);
5623 if (x == NULL) {
5624 Py_DECREF(l);
5625 return NULL;
5626 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005627 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005628 }
5629 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005630#endif
5631}
5632
5633#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005634
5635Py_ssize_t
5636_PyEval_RequestCodeExtraIndex(freefunc free)
5637{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005638 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005639 Py_ssize_t new_index;
5640
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005641 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005642 return -1;
5643 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005644 new_index = interp->co_extra_user_count++;
5645 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005646 return new_index;
5647}
Łukasz Langaa785c872016-09-09 17:37:37 -07005648
5649static void
5650dtrace_function_entry(PyFrameObject *f)
5651{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005652 const char *filename;
5653 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005654 int lineno;
5655
5656 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5657 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5658 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5659
5660 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5661}
5662
5663static void
5664dtrace_function_return(PyFrameObject *f)
5665{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005666 const char *filename;
5667 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005668 int lineno;
5669
5670 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5671 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5672 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5673
5674 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5675}
5676
5677/* DTrace equivalent of maybe_call_line_trace. */
5678static void
5679maybe_dtrace_line(PyFrameObject *frame,
5680 int *instr_lb, int *instr_ub, int *instr_prev)
5681{
5682 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005683 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005684
5685 /* If the last instruction executed isn't in the current
5686 instruction window, reset the window.
5687 */
5688 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5689 PyAddrPair bounds;
5690 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5691 &bounds);
5692 *instr_lb = bounds.ap_lower;
5693 *instr_ub = bounds.ap_upper;
5694 }
5695 /* If the last instruction falls at the start of a line or if
5696 it represents a jump backwards, update the frame's line
5697 number and call the trace function. */
5698 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5699 frame->f_lineno = line;
5700 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5701 if (!co_filename)
5702 co_filename = "?";
5703 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5704 if (!co_name)
5705 co_name = "?";
5706 PyDTrace_LINE(co_filename, co_name, line);
5707 }
5708 *instr_prev = frame->f_lasti;
5709}