blob: 836457d1a57a4313e7b90b1fca3fed875bd8d7b3 [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 Stinner4d231bc2019-11-14 13:36:21 +010013#include "pycore_call.h"
Victor Stinner09532fe2019-05-10 23:39:09 +020014#include "pycore_ceval.h"
Inada Naoki91234a12019-06-03 21:30:58 +090015#include "pycore_code.h"
Victor Stinner111e4ee2020-03-09 21:24:14 +010016#include "pycore_initconfig.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +010017#include "pycore_object.h"
Victor Stinner438a12d2019-05-24 17:01:38 +020018#include "pycore_pyerrors.h"
19#include "pycore_pylifecycle.h"
Victor Stinner621cebe2018-11-12 16:53:38 +010020#include "pycore_pystate.h"
Victor Stinnerec13b932018-11-25 23:56:17 +010021#include "pycore_tupleobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000022
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000023#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040024#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000025#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000026#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070027#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040028#include "setobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000029#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000030
Guido van Rossumc6004111993-11-05 10:22:19 +000031#include <ctype.h>
32
Guido van Rossum408027e1996-12-30 16:17:54 +000033#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000034/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035#define LLTRACE 1 /* Low-level trace feature */
36#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000037#endif
38
Victor Stinner5c75f372019-04-17 23:02:26 +020039#if !defined(Py_BUILD_CORE)
40# error "ceval.c must be build with Py_BUILD_CORE define for best performance"
41#endif
42
Hai Shi46874c22020-01-30 17:20:25 -060043_Py_IDENTIFIER(__name__);
Guido van Rossum5b722181993-03-30 17:46:03 +000044
Guido van Rossum374a9221991-04-04 10:40:29 +000045/* Forward declarations */
Victor Stinner09532fe2019-05-10 23:39:09 +020046Py_LOCAL_INLINE(PyObject *) call_function(
47 PyThreadState *tstate, PyObject ***pp_stack,
48 Py_ssize_t oparg, PyObject *kwnames);
49static PyObject * do_call_core(
50 PyThreadState *tstate, PyObject *func,
51 PyObject *callargs, PyObject *kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +000052
Guido van Rossum0a066c01992-03-27 17:29:15 +000053#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000054static int lltrace;
Victor Stinner438a12d2019-05-24 17:01:38 +020055static int prtrace(PyThreadState *, PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000056#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010057static int call_trace(Py_tracefunc, PyObject *,
58 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000060static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010061 PyThreadState *, PyFrameObject *,
62 int, PyObject *);
63static void call_exc_trace(Py_tracefunc, PyObject *,
64 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000065static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060066 PyThreadState *, PyFrameObject *,
67 int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070068static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
69static void dtrace_function_entry(PyFrameObject *);
70static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000071
Victor Stinner438a12d2019-05-24 17:01:38 +020072static PyObject * import_name(PyThreadState *, PyFrameObject *,
73 PyObject *, PyObject *, PyObject *);
74static PyObject * import_from(PyThreadState *, PyObject *, PyObject *);
75static int import_all_from(PyThreadState *, PyObject *, PyObject *);
76static void format_exc_check_arg(PyThreadState *, PyObject *, const char *, PyObject *);
77static void format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
78static PyObject * unicode_concatenate(PyThreadState *, PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030079 PyFrameObject *, const _Py_CODEUNIT *);
Victor Stinner438a12d2019-05-24 17:01:38 +020080static PyObject * special_lookup(PyThreadState *, PyObject *, _Py_Identifier *);
81static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg);
82static void format_kwargs_error(PyThreadState *, PyObject *func, PyObject *kwargs);
Mark Shannonfee55262019-11-21 09:11:43 +000083static void format_awaitable_error(PyThreadState *, PyTypeObject *, int, int);
Guido van Rossum374a9221991-04-04 10:40:29 +000084
Paul Prescode68140d2000-08-30 20:25:01 +000085#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000087#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000089#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 "free variable '%.200s' referenced before assignment" \
91 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000092
Guido van Rossum950361c1997-01-24 13:49:28 +000093/* Dynamic execution profile */
94#ifdef DYNAMIC_EXECUTION_PROFILE
95#ifdef DXPAIRS
96static long dxpairs[257][256];
97#define dxp dxpairs[256]
98#else
99static long dxp[256];
100#endif
101#endif
102
Inada Naoki91234a12019-06-03 21:30:58 +0900103/* per opcode cache */
Inada Naokieddef862019-06-04 07:38:10 +0900104#ifdef Py_DEBUG
105// --with-pydebug is used to find memory leak. opcache makes it harder.
106// So we disable opcache when Py_DEBUG is defined.
107// See bpo-37146
108#define OPCACHE_MIN_RUNS 0 /* disable opcache */
109#else
Inada Naoki91234a12019-06-03 21:30:58 +0900110#define OPCACHE_MIN_RUNS 1024 /* create opcache when code executed this time */
Inada Naokieddef862019-06-04 07:38:10 +0900111#endif
Inada Naoki91234a12019-06-03 21:30:58 +0900112#define OPCACHE_STATS 0 /* Enable stats */
113
114#if OPCACHE_STATS
115static size_t opcache_code_objects = 0;
116static size_t opcache_code_objects_extra_mem = 0;
117
118static size_t opcache_global_opts = 0;
119static size_t opcache_global_hits = 0;
120static size_t opcache_global_misses = 0;
121#endif
122
Victor Stinner5a3a71d2020-03-19 17:40:12 +0100123
Victor Stinnerda2914d2020-03-20 09:29:08 +0100124#ifndef NDEBUG
125/* Ensure that tstate is valid: sanity check for PyEval_AcquireThread() and
126 PyEval_RestoreThread(). Detect if tstate memory was freed. It can happen
127 when a thread continues to run after Python finalization, especially
128 daemon threads. */
129static int
130is_tstate_valid(PyThreadState *tstate)
131{
132 assert(!_PyMem_IsPtrFreed(tstate));
133 assert(!_PyMem_IsPtrFreed(tstate->interp));
134 return 1;
135}
136#endif
137
138
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000139/* This can set eval_breaker to 0 even though gil_drop_request became
140 1. We believe this is all right because the eval loop will release
141 the GIL eventually anyway. */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100142static inline void
143COMPUTE_EVAL_BREAKER(PyThreadState *tstate,
144 struct _ceval_runtime_state *ceval,
145 struct _ceval_state *ceval2)
146{
147 assert(is_tstate_valid(tstate));
148 _Py_atomic_store_relaxed(&ceval2->eval_breaker,
149 _Py_atomic_load_relaxed(&ceval->gil_drop_request)
150 | (_Py_atomic_load_relaxed(&ceval->signals_pending)
Victor Stinnerd2a8e5b2020-03-20 13:38:58 +0100151 && _Py_ThreadCanHandleSignals(tstate))
Victor Stinnerd8316882020-03-20 14:50:35 +0100152 | (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)
153 && _Py_ThreadCanHandlePendingCalls())
Victor Stinnerda2914d2020-03-20 09:29:08 +0100154 | ceval2->pending.async_exc);
155}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000156
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000157
Victor Stinnerda2914d2020-03-20 09:29:08 +0100158static inline void
159SET_GIL_DROP_REQUEST(PyThreadState *tstate)
160{
161 assert(is_tstate_valid(tstate));
162 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
163 struct _ceval_state *ceval2 = &tstate->interp->ceval;
164 _Py_atomic_store_relaxed(&ceval->gil_drop_request, 1);
165 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
166}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000167
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000168
Victor Stinnerda2914d2020-03-20 09:29:08 +0100169static inline void
170RESET_GIL_DROP_REQUEST(PyThreadState *tstate)
171{
172 assert(is_tstate_valid(tstate));
173 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
174 struct _ceval_state *ceval2 = &tstate->interp->ceval;
175 _Py_atomic_store_relaxed(&ceval->gil_drop_request, 0);
176 COMPUTE_EVAL_BREAKER(tstate, ceval, ceval2);
177}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000178
Eric Snowfdf282d2019-01-11 14:26:55 -0700179
Victor Stinnerda2914d2020-03-20 09:29:08 +0100180static inline void
181SIGNAL_PENDING_CALLS(PyThreadState *tstate)
182{
183 assert(is_tstate_valid(tstate));
Victor Stinnerd8316882020-03-20 14:50:35 +0100184 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100185 struct _ceval_state *ceval2 = &tstate->interp->ceval;
186 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 1);
Victor Stinnerd8316882020-03-20 14:50:35 +0100187 COMPUTE_EVAL_BREAKER(tstate, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100188}
Eric Snowfdf282d2019-01-11 14:26:55 -0700189
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000190
Victor Stinnerda2914d2020-03-20 09:29:08 +0100191static inline void
192UNSIGNAL_PENDING_CALLS(PyThreadState *tstate)
193{
194 assert(is_tstate_valid(tstate));
195 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
196 struct _ceval_state *ceval2 = &tstate->interp->ceval;
197 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 0);
198 COMPUTE_EVAL_BREAKER(tstate, ceval, ceval2);
199}
200
201
202static inline void
203SIGNAL_PENDING_SIGNALS(PyThreadState *tstate)
204{
205 assert(is_tstate_valid(tstate));
206 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
207 struct _ceval_state *ceval2 = &tstate->interp->ceval;
208 _Py_atomic_store_relaxed(&ceval->signals_pending, 1);
209 /* eval_breaker is not set to 1 if thread_can_handle_signals() is false */
210 COMPUTE_EVAL_BREAKER(tstate, ceval, ceval2);
211}
212
213
214static inline void
215UNSIGNAL_PENDING_SIGNALS(PyThreadState *tstate)
216{
217 assert(is_tstate_valid(tstate));
218 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
219 struct _ceval_state *ceval2 = &tstate->interp->ceval;
220 _Py_atomic_store_relaxed(&ceval->signals_pending, 0);
221 COMPUTE_EVAL_BREAKER(tstate, ceval, ceval2);
222}
223
224
225static inline void
226SIGNAL_ASYNC_EXC(PyThreadState *tstate)
227{
228 assert(is_tstate_valid(tstate));
229 struct _ceval_state *ceval2 = &tstate->interp->ceval;
230 ceval2->pending.async_exc = 1;
231 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
232}
233
234
235static inline void
236UNSIGNAL_ASYNC_EXC(PyThreadState *tstate)
237{
238 assert(is_tstate_valid(tstate));
239 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
240 struct _ceval_state *ceval2 = &tstate->interp->ceval;
241 ceval2->pending.async_exc = 0;
242 COMPUTE_EVAL_BREAKER(tstate, ceval, ceval2);
243}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000244
245
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000246#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000247#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000248#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000249#include "pythread.h"
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000250#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000251
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100252static void
253ensure_tstate_not_null(const char *func, PyThreadState *tstate)
254{
255 if (tstate == NULL) {
Victor Stinner23ef89d2020-03-18 02:26:04 +0100256 _Py_FatalErrorFunc(func,
257 "current thread state is NULL (released GIL?)");
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100258 }
259}
260
261
Tim Peters7f468f22004-10-11 02:40:51 +0000262int
Victor Stinner175a7042020-03-10 00:37:48 +0100263_PyEval_ThreadsInitialized(_PyRuntimeState *runtime)
264{
265 return gil_created(&runtime->ceval.gil);
266}
267
268int
Tim Peters7f468f22004-10-11 02:40:51 +0000269PyEval_ThreadsInitialized(void)
270{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100271 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner175a7042020-03-10 00:37:48 +0100272 return _PyEval_ThreadsInitialized(runtime);
Tim Peters7f468f22004-10-11 02:40:51 +0000273}
274
Victor Stinner111e4ee2020-03-09 21:24:14 +0100275PyStatus
276_PyEval_InitThreads(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000277{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100278 assert(is_tstate_valid(tstate));
279
Victor Stinner50e6e992020-03-19 02:41:21 +0100280 if (_Py_IsMainInterpreter(tstate)) {
281 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
282 if (gil_created(gil)) {
283 return _PyStatus_OK();
284 }
285
286 PyThread_init_thread();
287 create_gil(gil);
288
289 take_gil(tstate);
Victor Stinner111e4ee2020-03-09 21:24:14 +0100290 }
291
Victor Stinner50e6e992020-03-19 02:41:21 +0100292 struct _pending_calls *pending = &tstate->interp->ceval.pending;
293 assert(pending->lock == NULL);
Victor Stinnere225beb2019-06-03 18:14:24 +0200294 pending->lock = PyThread_allocate_lock();
295 if (pending->lock == NULL) {
Victor Stinner111e4ee2020-03-09 21:24:14 +0100296 return _PyStatus_NO_MEMORY();
297 }
Victor Stinner85f5a692020-03-09 22:12:04 +0100298
Victor Stinner111e4ee2020-03-09 21:24:14 +0100299 return _PyStatus_OK();
300}
301
302void
303PyEval_InitThreads(void)
304{
Victor Stinnerb4698ec2020-03-10 01:28:54 +0100305 /* Do nothing: kept for backward compatibility */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000306}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000307
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000308void
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100309_PyEval_FiniThreads(PyThreadState *tstate)
Antoine Pitrou1df15362010-09-13 14:16:46 +0000310{
Victor Stinner50e6e992020-03-19 02:41:21 +0100311 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
Victor Stinner09532fe2019-05-10 23:39:09 +0200312 if (!gil_created(gil)) {
Antoine Pitrou1df15362010-09-13 14:16:46 +0000313 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100314 }
315
Victor Stinner09532fe2019-05-10 23:39:09 +0200316 destroy_gil(gil);
317 assert(!gil_created(gil));
Victor Stinner99fcc612019-04-29 13:04:07 +0200318
Victor Stinner50e6e992020-03-19 02:41:21 +0100319 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Victor Stinnere225beb2019-06-03 18:14:24 +0200320 if (pending->lock != NULL) {
321 PyThread_free_lock(pending->lock);
322 pending->lock = NULL;
323 }
Antoine Pitrou1df15362010-09-13 14:16:46 +0000324}
325
326void
Inada Naoki91234a12019-06-03 21:30:58 +0900327_PyEval_Fini(void)
328{
329#if OPCACHE_STATS
330 fprintf(stderr, "-- Opcode cache number of objects = %zd\n",
331 opcache_code_objects);
332
333 fprintf(stderr, "-- Opcode cache total extra mem = %zd\n",
334 opcache_code_objects_extra_mem);
335
336 fprintf(stderr, "\n");
337
338 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n",
339 opcache_global_hits,
340 (int) (100.0 * opcache_global_hits /
341 (opcache_global_hits + opcache_global_misses)));
342
343 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n",
344 opcache_global_misses,
345 (int) (100.0 * opcache_global_misses /
346 (opcache_global_hits + opcache_global_misses)));
347
348 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n",
349 opcache_global_opts);
350
351 fprintf(stderr, "\n");
352#endif
353}
354
355void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000356PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000357{
Victor Stinner09532fe2019-05-10 23:39:09 +0200358 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200359 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100360 ensure_tstate_not_null(__func__, tstate);
361
Victor Stinner85f5a692020-03-09 22:12:04 +0100362 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000363}
364
365void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000366PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000367{
Victor Stinner09532fe2019-05-10 23:39:09 +0200368 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200369 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100371 We therefore avoid PyThreadState_Get() which dumps a fatal error
Victor Stinnerda2914d2020-03-20 09:29:08 +0100372 in debug mode. */
373 drop_gil(&runtime->ceval, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000374}
375
376void
Victor Stinner23ef89d2020-03-18 02:26:04 +0100377_PyEval_ReleaseLock(PyThreadState *tstate)
378{
379 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100380 drop_gil(ceval, tstate);
Victor Stinner23ef89d2020-03-18 02:26:04 +0100381}
382
383void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000384PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000385{
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100386 ensure_tstate_not_null(__func__, tstate);
387
Victor Stinner85f5a692020-03-09 22:12:04 +0100388 take_gil(tstate);
Victor Stinnere225beb2019-06-03 18:14:24 +0200389
Victor Stinner85f5a692020-03-09 22:12:04 +0100390 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
391 if (_PyThreadState_Swap(gilstate, tstate) != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100392 Py_FatalError("non-NULL old thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200393 }
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000394}
395
396void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000397PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000398{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100399 assert(is_tstate_valid(tstate));
Victor Stinner09532fe2019-05-10 23:39:09 +0200400
Victor Stinner01b1cc12019-11-20 02:27:56 +0100401 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200402 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
403 if (new_tstate != tstate) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100404 Py_FatalError("wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200405 }
Victor Stinnerda2914d2020-03-20 09:29:08 +0100406 drop_gil(&runtime->ceval, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000407}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000408
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200409/* This function is called from PyOS_AfterFork_Child to destroy all threads
410 * which are not running in the child process, and clear internal locks
411 * which might be held by those threads.
412 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000413
414void
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200415_PyEval_ReInitThreads(_PyRuntimeState *runtime)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000416{
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100417 struct _gil_runtime_state *gil = &runtime->ceval.gil;
418 if (!gil_created(gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 return;
Victor Stinner09532fe2019-05-10 23:39:09 +0200420 }
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100421 recreate_gil(gil);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100422 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
423 ensure_tstate_not_null(__func__, tstate);
Victor Stinner85f5a692020-03-09 22:12:04 +0100424
425 take_gil(tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700426
Victor Stinner50e6e992020-03-19 02:41:21 +0100427 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200428 pending->lock = PyThread_allocate_lock();
429 if (pending->lock == NULL) {
Eric Snow8479a342019-03-08 23:44:33 -0700430 Py_FatalError("Can't initialize threads for pending calls");
431 }
Jesse Nollera8513972008-07-17 16:49:17 +0000432
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200433 /* Destroy all threads except the current one */
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100434 _PyThreadState_DeleteExcept(runtime, tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000435}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000436
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000437/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600438 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000439
440void
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100441_PyEval_SignalAsyncExc(PyThreadState *tstate)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000442{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100443 SIGNAL_ASYNC_EXC(tstate);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000444}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000445
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000446PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000447PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000448{
Victor Stinner09532fe2019-05-10 23:39:09 +0200449 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200450 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100451
Victor Stinner09532fe2019-05-10 23:39:09 +0200452 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100453 ensure_tstate_not_null(__func__, tstate);
454
Victor Stinnere225beb2019-06-03 18:14:24 +0200455 assert(gil_created(&ceval->gil));
Victor Stinnerda2914d2020-03-20 09:29:08 +0100456 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000458}
459
460void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000461PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000462{
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100463 ensure_tstate_not_null(__func__, tstate);
464
Victor Stinner85f5a692020-03-09 22:12:04 +0100465 take_gil(tstate);
Victor Stinner17c68b82020-01-30 12:20:48 +0100466
Victor Stinner85f5a692020-03-09 22:12:04 +0100467 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
468 _PyThreadState_Swap(gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000469}
470
471
Guido van Rossuma9672091994-09-14 13:31:22 +0000472/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
473 signal handlers or Mac I/O completion routines) can schedule calls
474 to a function to be called synchronously.
475 The synchronous function is called with one void* argument.
476 It should return 0 for success or -1 for failure -- failure should
477 be accompanied by an exception.
478
479 If registry succeeds, the registry function returns 0; if it fails
480 (e.g. due to too many pending calls) it returns -1 (without setting
481 an exception condition).
482
483 Note that because registry may occur from within signal handlers,
484 or other asynchronous events, calling malloc() is unsafe!
485
Guido van Rossuma9672091994-09-14 13:31:22 +0000486 Any thread can schedule pending calls, but only the main thread
487 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000488 There is no facility to schedule calls to a particular thread, but
489 that should be easy to change, should that ever be required. In
490 that case, the static variables here should go into the python
491 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000492*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000493
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200494void
Victor Stinner8849e592020-03-18 19:28:53 +0100495_PyEval_SignalReceived(PyThreadState *tstate)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200496{
497 /* bpo-30703: Function called when the C signal handler of Python gets a
Victor Stinner50e6e992020-03-19 02:41:21 +0100498 signal. We cannot queue a callback using _PyEval_AddPendingCall() since
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200499 that function is not async-signal-safe. */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100500 SIGNAL_PENDING_SIGNALS(tstate);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200501}
502
Eric Snow5be45a62019-03-08 22:47:07 -0700503/* Push one item onto the queue while holding the lock. */
504static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200505_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600506 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700507{
Eric Snow842a2f02019-03-15 15:47:51 -0600508 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700509 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600510 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700511 return -1; /* Queue full */
512 }
Eric Snow842a2f02019-03-15 15:47:51 -0600513 pending->calls[i].func = func;
514 pending->calls[i].arg = arg;
515 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700516 return 0;
517}
518
519/* Pop one item off the queue while holding the lock. */
520static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200521_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600522 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700523{
Eric Snow842a2f02019-03-15 15:47:51 -0600524 int i = pending->first;
525 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700526 return; /* Queue empty */
527 }
528
Eric Snow842a2f02019-03-15 15:47:51 -0600529 *func = pending->calls[i].func;
530 *arg = pending->calls[i].arg;
531 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700532}
533
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200534/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000535 scheduling to be made from any thread, and even from an executing
536 callback.
537 */
538
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000539int
Victor Stinner438a12d2019-05-24 17:01:38 +0200540_PyEval_AddPendingCall(PyThreadState *tstate,
Victor Stinner09532fe2019-05-10 23:39:09 +0200541 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000542{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100543 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600544
545 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
546 if (pending->finishing) {
547 PyThread_release_lock(pending->lock);
548
549 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +0200550 _PyErr_Fetch(tstate, &exc, &val, &tb);
551 _PyErr_SetString(tstate, PyExc_SystemError,
Victor Stinner50e6e992020-03-19 02:41:21 +0100552 "Py_AddPendingCall: cannot add pending calls "
553 "(Python shutting down)");
Victor Stinner438a12d2019-05-24 17:01:38 +0200554 _PyErr_Print(tstate);
555 _PyErr_Restore(tstate, exc, val, tb);
Eric Snow842a2f02019-03-15 15:47:51 -0600556 return -1;
557 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200558 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600559 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700560
Victor Stinnere225beb2019-06-03 18:14:24 +0200561 /* signal main loop */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100562 SIGNAL_PENDING_CALLS(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000564}
565
Victor Stinner09532fe2019-05-10 23:39:09 +0200566int
567Py_AddPendingCall(int (*func)(void *), void *arg)
568{
Victor Stinner50e6e992020-03-19 02:41:21 +0100569 /* Best-effort to support subinterpreters and calls with the GIL released.
570
571 First attempt _PyThreadState_GET() since it supports subinterpreters.
572
573 If the GIL is released, _PyThreadState_GET() returns NULL . In this
574 case, use PyGILState_GetThisThreadState() which works even if the GIL
575 is released.
576
577 Sadly, PyGILState_GetThisThreadState() doesn't support subinterpreters:
578 see bpo-10915 and bpo-15751.
579
Victor Stinner8849e592020-03-18 19:28:53 +0100580 Py_AddPendingCall() doesn't require the caller to hold the GIL. */
Victor Stinner50e6e992020-03-19 02:41:21 +0100581 PyThreadState *tstate = _PyThreadState_GET();
582 if (tstate == NULL) {
583 tstate = PyGILState_GetThisThreadState();
584 }
585 /* tstate can be NULL if Py_AddPendingCall() is called in a thread
586 which is no Python thread state. Fail with a fatal error in this
587 case. */
588 ensure_tstate_not_null(__func__, tstate);
Victor Stinner8849e592020-03-18 19:28:53 +0100589 return _PyEval_AddPendingCall(tstate, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200590}
591
Eric Snowfdf282d2019-01-11 14:26:55 -0700592static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100593handle_signals(PyThreadState *tstate)
Eric Snowfdf282d2019-01-11 14:26:55 -0700594{
Victor Stinnerd2a8e5b2020-03-20 13:38:58 +0100595 if (!_Py_ThreadCanHandleSignals(tstate)) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700596 return 0;
597 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700598
Victor Stinnerda2914d2020-03-20 09:29:08 +0100599 UNSIGNAL_PENDING_SIGNALS(tstate);
Eric Snow64d6cc82019-02-23 15:40:43 -0700600 if (_PyErr_CheckSignals() < 0) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100601 /* We're not done yet */
602 SIGNAL_PENDING_SIGNALS(tstate);
Eric Snowfdf282d2019-01-11 14:26:55 -0700603 return -1;
604 }
605 return 0;
606}
607
608static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100609make_pending_calls(PyThreadState *tstate)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000610{
Victor Stinnerd8316882020-03-20 14:50:35 +0100611 /* only execute pending calls on main thread */
612 if (!_Py_ThreadCanHandlePendingCalls()) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200613 return 0;
614 }
615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 /* don't perform recursive pending calls */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100617 static int busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700618 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700620 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200621 busy = 1;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100622
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200623 /* unsignal before starting to call callbacks, so that any callback
624 added in-between re-signals */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100625 UNSIGNAL_PENDING_CALLS(tstate);
Eric Snowfdf282d2019-01-11 14:26:55 -0700626 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 /* perform a bounded number of calls, in case of recursion */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100629 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700630 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700631 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 void *arg = NULL;
633
634 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600635 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200636 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600637 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700638
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100639 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700640 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100641 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700642 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700643 res = func(arg);
644 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200645 goto error;
646 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200648
Charles-François Natalif23339a2011-07-23 18:15:43 +0200649 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700650 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200651
652error:
653 busy = 0;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100654 SIGNAL_PENDING_CALLS(tstate);
Eric Snowfdf282d2019-01-11 14:26:55 -0700655 return res;
656}
657
Eric Snow842a2f02019-03-15 15:47:51 -0600658void
Victor Stinner2b1df452020-01-13 18:46:59 +0100659_Py_FinishPendingCalls(PyThreadState *tstate)
Eric Snow842a2f02019-03-15 15:47:51 -0600660{
Eric Snow842a2f02019-03-15 15:47:51 -0600661 assert(PyGILState_Check());
662
Victor Stinner50e6e992020-03-19 02:41:21 +0100663 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200664
Eric Snow842a2f02019-03-15 15:47:51 -0600665 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
666 pending->finishing = 1;
667 PyThread_release_lock(pending->lock);
668
669 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
670 return;
671 }
672
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100673 if (make_pending_calls(tstate) < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200674 PyObject *exc, *val, *tb;
675 _PyErr_Fetch(tstate, &exc, &val, &tb);
676 PyErr_BadInternalCall();
677 _PyErr_ChainExceptions(exc, val, tb);
678 _PyErr_Print(tstate);
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
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100689 PyThreadState *tstate = _PyThreadState_GET();
690
Eric Snowfdf282d2019-01-11 14:26:55 -0700691 /* Python signal handler doesn't really queue a callback: it only signals
692 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100693 int res = handle_signals(tstate);
Eric Snowfdf282d2019-01-11 14:26:55 -0700694 if (res != 0) {
695 return res;
696 }
697
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100698 res = make_pending_calls(tstate);
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
Victor Stinnerdab84232020-03-17 18:56:44 +0100715_PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600716{
Victor Stinnerdab84232020-03-17 18:56:44 +0100717 ceval->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600718 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Victor Stinnerdab84232020-03-17 18:56:44 +0100719 _gil_initialize(&ceval->gil);
720}
721
722void
723_PyEval_InitState(struct _ceval_state *ceval)
724{
725 /* PyInterpreterState_New() initializes ceval to zero */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600726}
727
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000728int
729Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000730{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100731 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
732 return ceval->recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000733}
734
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000735void
736Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000737{
Victor Stinnere225beb2019-06-03 18:14:24 +0200738 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
739 ceval->recursion_limit = new_limit;
Victor Stinnerdab84232020-03-17 18:56:44 +0100740 _Py_CheckRecursionLimit = new_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000741}
742
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100743/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Armin Rigo2b3eb402003-10-28 12:05:48 +0000744 if the recursion_depth reaches _Py_CheckRecursionLimit.
745 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
746 to guarantee that _Py_CheckRecursiveCall() is regularly called.
747 Without USE_STACKCHECK, there is no need for this. */
748int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100749_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000750{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100751 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200752 int recursion_limit = runtime->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000753
754#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700755 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 if (PyOS_CheckStack()) {
757 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200758 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 return -1;
760 }
pdox18967932017-10-25 23:03:01 -0700761 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700762 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700763#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 if (tstate->recursion_critical)
765 /* Somebody asked that we don't check for recursion. */
766 return 0;
767 if (tstate->overflowed) {
768 if (tstate->recursion_depth > recursion_limit + 50) {
769 /* Overflowing while handling an overflow. Give up. */
770 Py_FatalError("Cannot recover from stack overflow.");
771 }
772 return 0;
773 }
774 if (tstate->recursion_depth > recursion_limit) {
775 --tstate->recursion_depth;
776 tstate->overflowed = 1;
Victor Stinner438a12d2019-05-24 17:01:38 +0200777 _PyErr_Format(tstate, PyExc_RecursionError,
778 "maximum recursion depth exceeded%s",
779 where);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 return -1;
781 }
782 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000783}
784
Victor Stinner09532fe2019-05-10 23:39:09 +0200785static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200786static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000787
Victor Stinnere225beb2019-06-03 18:14:24 +0200788#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000789
Guido van Rossum374a9221991-04-04 10:40:29 +0000790
Guido van Rossumb209a111997-04-29 18:18:01 +0000791PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000792PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000793{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 return PyEval_EvalCodeEx(co,
795 globals, locals,
796 (PyObject **)NULL, 0,
797 (PyObject **)NULL, 0,
798 (PyObject **)NULL, 0,
799 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000800}
801
802
803/* Interpreter main loop */
804
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000805PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +0100806PyEval_EvalFrame(PyFrameObject *f)
807{
Victor Stinner0b72b232020-03-12 23:18:39 +0100808 /* Function kept for backward compatibility */
Victor Stinnerb9e68122019-11-14 12:20:46 +0100809 PyThreadState *tstate = _PyThreadState_GET();
810 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000811}
812
813PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000814PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000815{
Victor Stinnerb9e68122019-11-14 12:20:46 +0100816 PyThreadState *tstate = _PyThreadState_GET();
817 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700818}
819
Victor Stinnerda2914d2020-03-20 09:29:08 +0100820
821/* Handle signals, pending calls, GIL drop request
822 and asynchronous exception */
823static int
824eval_frame_handle_pending(PyThreadState *tstate)
825{
826 /* Pending signals */
827 _PyRuntimeState * const runtime = &_PyRuntime;
828 struct _ceval_runtime_state *ceval = &runtime->ceval;
829 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
830 if (handle_signals(tstate) != 0) {
831 return -1;
832 }
833 }
834
835 /* Pending calls */
836 struct _ceval_state *ceval2 = &tstate->interp->ceval;
837 if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) {
838 if (make_pending_calls(tstate) != 0) {
839 return -1;
840 }
841 }
842
843 /* GIL drop request */
844 if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) {
845 /* Give another thread a chance */
846 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
847 Py_FatalError("tstate mix-up");
848 }
849 drop_gil(ceval, tstate);
850
851 /* Other threads may run now */
852
853 take_gil(tstate);
854
855 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
856 Py_FatalError("orphan tstate");
857 }
858 }
859
860 /* Check for asynchronous exception. */
861 if (tstate->async_exc != NULL) {
862 PyObject *exc = tstate->async_exc;
863 tstate->async_exc = NULL;
864 UNSIGNAL_ASYNC_EXC(tstate);
865 _PyErr_SetNone(tstate, exc);
866 Py_DECREF(exc);
867 return -1;
868 }
869
870 return 0;
871}
872
Victor Stinnerc6944e72016-11-11 02:13:35 +0100873PyObject* _Py_HOT_FUNCTION
Victor Stinner0b72b232020-03-12 23:18:39 +0100874_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
Brett Cannon3cebf932016-09-05 15:33:46 -0700875{
Victor Stinner0b72b232020-03-12 23:18:39 +0100876 ensure_tstate_not_null(__func__, tstate);
877
Guido van Rossum950361c1997-01-24 13:49:28 +0000878#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000880#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200881 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300882 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200883 int opcode; /* Current opcode */
884 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200885 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 PyObject *retval = NULL; /* Return value */
Victor Stinnerdab84232020-03-17 18:56:44 +0100887 struct _ceval_state * const ceval2 = &tstate->interp->ceval;
Victor Stinner50e6e992020-03-19 02:41:21 +0100888 _Py_atomic_int * const eval_breaker = &ceval2->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 is true when the line being executed has changed. The
896 initial values are such as to make this false the first
897 time it is tested. */
898 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000899
Serhiy Storchakaab874002016-09-11 13:48:15 +0300900 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 PyObject *names;
902 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +0900903 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +0000904
Brett Cannon368b4b72012-04-02 12:17:59 -0400905#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200906 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400907#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200908
Antoine Pitroub52ec782009-01-25 16:34:23 +0000909/* Computed GOTOs, or
910 the-optimization-commonly-but-improperly-known-as-"threaded code"
911 using gcc's labels-as-values extension
912 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
913
914 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000916 combined with a lookup table of jump addresses. However, since the
917 indirect jump instruction is shared by all opcodes, the CPU will have a
918 hard time making the right prediction for where to jump next (actually,
919 it will be always wrong except in the uncommon case of a sequence of
920 several identical opcodes).
921
922 "Threaded code" in contrast, uses an explicit jump table and an explicit
923 indirect jump instruction at the end of each opcode. Since the jump
924 instruction is at a different address for each opcode, the CPU will make a
925 separate prediction for each of these instructions, which is equivalent to
926 predicting the second opcode of each opcode pair. These predictions have
927 a much better chance to turn out valid, especially in small bytecode loops.
928
929 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000931 and potentially many more instructions (depending on the pipeline width).
932 A correctly predicted branch, however, is nearly free.
933
934 At the time of this writing, the "threaded code" version is up to 15-20%
935 faster than the normal "switch" version, depending on the compiler and the
936 CPU architecture.
937
938 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
939 because it would render the measurements invalid.
940
941
942 NOTE: care must be taken that the compiler doesn't try to "optimize" the
943 indirect jumps by sharing them between all opcodes. Such optimizations
944 can be disabled on gcc by using the -fno-gcse flag (or possibly
945 -fno-crossjumping).
946*/
947
Antoine Pitrou042b1282010-08-13 21:15:58 +0000948#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000949#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000950#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000951#endif
952
Antoine Pitrou042b1282010-08-13 21:15:58 +0000953#ifdef HAVE_COMPUTED_GOTOS
954 #ifndef USE_COMPUTED_GOTOS
955 #define USE_COMPUTED_GOTOS 1
956 #endif
957#else
958 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
959 #error "Computed gotos are not supported on this compiler."
960 #endif
961 #undef USE_COMPUTED_GOTOS
962 #define USE_COMPUTED_GOTOS 0
963#endif
964
965#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000966/* Import the static jump table */
967#include "opcode_targets.h"
968
Antoine Pitroub52ec782009-01-25 16:34:23 +0000969#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700970 op: \
971 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000972
Antoine Pitroub52ec782009-01-25 16:34:23 +0000973#ifdef LLTRACE
974#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 { \
Victor Stinnerdab84232020-03-17 18:56:44 +0100976 if (!lltrace && !_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300978 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300979 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 } \
981 goto fast_next_opcode; \
982 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000983#else
984#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 { \
Victor Stinnerdab84232020-03-17 18:56:44 +0100986 if (!_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300988 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300989 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 } \
991 goto fast_next_opcode; \
992 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000993#endif
994
Victor Stinner09532fe2019-05-10 23:39:09 +0200995#define DISPATCH() \
996 { \
997 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
998 FAST_DISPATCH(); \
999 } \
1000 continue; \
1001 }
1002
Antoine Pitroub52ec782009-01-25 16:34:23 +00001003#else
Benjamin Petersonddd19492018-09-16 22:38:02 -07001004#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001005#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +02001006#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +00001007#endif
1008
1009
Neal Norwitza81d2202002-07-14 00:27:26 +00001010/* Tuple access macros */
1011
1012#ifndef Py_DEBUG
1013#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
1014#else
1015#define GETITEM(v, i) PyTuple_GetItem((v), (i))
1016#endif
1017
Guido van Rossum374a9221991-04-04 10:40:29 +00001018/* Code access macros */
1019
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001020/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001021#define INSTR_OFFSET() \
1022 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001023#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001024 _Py_CODEUNIT word = *next_instr; \
1025 opcode = _Py_OPCODE(word); \
1026 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001027 next_instr++; \
1028 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +03001029#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
1030#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +00001031
Raymond Hettingerf606f872003-03-16 03:11:04 +00001032/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 Some opcodes tend to come in pairs thus making it possible to
1034 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001035 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 Verifying the prediction costs a single high-speed test of a register
1038 variable against a constant. If the pairing was good, then the
1039 processor's own internal branch predication has a high likelihood of
1040 success, resulting in a nearly zero-overhead transition to the
1041 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001042 including its unpredictable switch-case branch. Combined with the
1043 processor's internal branch prediction, a successful PREDICT has the
1044 effect of making the two opcodes run as if they were a single new opcode
1045 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001046
Georg Brandl86b2fb92008-07-16 03:43:04 +00001047 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 predictions turned-on and interpret the results as if some opcodes
1049 had been combined or turn-off predictions so that the opcode frequency
1050 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001051
1052 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 the CPU to record separate branch prediction information for each
1054 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001055
Raymond Hettingerf606f872003-03-16 03:11:04 +00001056*/
1057
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001058#define PREDICT_ID(op) PRED_##op
1059
Antoine Pitrou042b1282010-08-13 21:15:58 +00001060#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001061#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +00001062#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001063#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001064 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001065 _Py_CODEUNIT word = *next_instr; \
1066 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001067 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001068 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001069 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001070 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001071 } \
1072 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001073#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001074#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +00001075
Raymond Hettingerf606f872003-03-16 03:11:04 +00001076
Guido van Rossum374a9221991-04-04 10:40:29 +00001077/* Stack manipulation macros */
1078
Martin v. Löwis18e16552006-02-15 17:27:45 +00001079/* The stack can grow at most MAXINT deep, as co_nlocals and
1080 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001081#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1082#define EMPTY() (STACK_LEVEL() == 0)
1083#define TOP() (stack_pointer[-1])
1084#define SECOND() (stack_pointer[-2])
1085#define THIRD() (stack_pointer[-3])
1086#define FOURTH() (stack_pointer[-4])
1087#define PEEK(n) (stack_pointer[-(n)])
1088#define SET_TOP(v) (stack_pointer[-1] = (v))
1089#define SET_SECOND(v) (stack_pointer[-2] = (v))
1090#define SET_THIRD(v) (stack_pointer[-3] = (v))
1091#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1092#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1093#define BASIC_STACKADJ(n) (stack_pointer += n)
1094#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1095#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001096
Guido van Rossum96a42c81992-01-12 02:29:51 +00001097#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001099 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001100 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +02001101#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001102 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +00001103#define STACK_GROW(n) do { \
1104 assert(n >= 0); \
1105 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001106 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001107 assert(STACK_LEVEL() <= co->co_stacksize); \
1108 } while (0)
1109#define STACK_SHRINK(n) do { \
1110 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001111 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001112 (void)(BASIC_STACKADJ(-n)); \
1113 assert(STACK_LEVEL() <= co->co_stacksize); \
1114 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001115#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001116 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001117 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001118#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001119#define PUSH(v) BASIC_PUSH(v)
1120#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001121#define STACK_GROW(n) BASIC_STACKADJ(n)
1122#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001123#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001124#endif
1125
Guido van Rossum681d79a1995-07-18 14:51:37 +00001126/* Local variable macros */
1127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001129
1130/* The SETLOCAL() macro must not DECREF the local variable in-place and
1131 then store the new value; it must copy the old value to a temporary
1132 value, then store the new value, and then DECREF the temporary value.
1133 This is because it is possible that during the DECREF the frame is
1134 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1135 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001137 GETLOCAL(i) = value; \
1138 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001139
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001140
1141#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 while (STACK_LEVEL() > (b)->b_level) { \
1143 PyObject *v = POP(); \
1144 Py_XDECREF(v); \
1145 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001146
1147#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001148 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001150 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1152 while (STACK_LEVEL() > (b)->b_level + 3) { \
1153 value = POP(); \
1154 Py_XDECREF(value); \
1155 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001156 exc_info = tstate->exc_info; \
1157 type = exc_info->exc_type; \
1158 value = exc_info->exc_value; \
1159 traceback = exc_info->exc_traceback; \
1160 exc_info->exc_type = POP(); \
1161 exc_info->exc_value = POP(); \
1162 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 Py_XDECREF(type); \
1164 Py_XDECREF(value); \
1165 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001166 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001167
Inada Naoki91234a12019-06-03 21:30:58 +09001168 /* macros for opcode cache */
1169#define OPCACHE_CHECK() \
1170 do { \
1171 co_opcache = NULL; \
1172 if (co->co_opcache != NULL) { \
1173 unsigned char co_opt_offset = \
1174 co->co_opcache_map[next_instr - first_instr]; \
1175 if (co_opt_offset > 0) { \
1176 assert(co_opt_offset <= co->co_opcache_size); \
1177 co_opcache = &co->co_opcache[co_opt_offset - 1]; \
1178 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001179 } \
1180 } \
1181 } while (0)
1182
1183#if OPCACHE_STATS
1184
1185#define OPCACHE_STAT_GLOBAL_HIT() \
1186 do { \
1187 if (co->co_opcache != NULL) opcache_global_hits++; \
1188 } while (0)
1189
1190#define OPCACHE_STAT_GLOBAL_MISS() \
1191 do { \
1192 if (co->co_opcache != NULL) opcache_global_misses++; \
1193 } while (0)
1194
1195#define OPCACHE_STAT_GLOBAL_OPT() \
1196 do { \
1197 if (co->co_opcache != NULL) opcache_global_opts++; \
1198 } while (0)
1199
1200#else /* OPCACHE_STATS */
1201
1202#define OPCACHE_STAT_GLOBAL_HIT()
1203#define OPCACHE_STAT_GLOBAL_MISS()
1204#define OPCACHE_STAT_GLOBAL_OPT()
1205
1206#endif
1207
Guido van Rossuma027efa1997-05-05 20:56:21 +00001208/* Start of code */
1209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001211 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001213 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 if (tstate->use_tracing) {
1218 if (tstate->c_tracefunc != NULL) {
1219 /* tstate->c_tracefunc, if defined, is a
1220 function that will be called on *every* entry
1221 to a code block. Its return value, if not
1222 None, is a function that will be called at
1223 the start of each executed line of code.
1224 (Actually, the function must return itself
1225 in order to continue tracing.) The trace
1226 functions are called with three arguments:
1227 a pointer to the current frame, a string
1228 indicating why the function is called, and
1229 an argument which depends on the situation.
1230 The global trace function is also called
1231 whenever an exception is detected. */
1232 if (call_trace_protected(tstate->c_tracefunc,
1233 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001234 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 /* Trace function raised an error */
1236 goto exit_eval_frame;
1237 }
1238 }
1239 if (tstate->c_profilefunc != NULL) {
1240 /* Similar for c_profilefunc, except it needn't
1241 return itself and isn't called for "line" events */
1242 if (call_trace_protected(tstate->c_profilefunc,
1243 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001244 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 /* Profile function raised an error */
1246 goto exit_eval_frame;
1247 }
1248 }
1249 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001250
Łukasz Langaa785c872016-09-09 17:37:37 -07001251 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1252 dtrace_function_entry(f);
1253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 co = f->f_code;
1255 names = co->co_names;
1256 consts = co->co_consts;
1257 fastlocals = f->f_localsplus;
1258 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001259 assert(PyBytes_Check(co->co_code));
1260 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001261 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1262 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1263 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001264 /*
1265 f->f_lasti refers to the index of the last instruction,
1266 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001267
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001268 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001269 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 When the PREDICT() macros are enabled, some opcode pairs follow in
1272 direct succession without updating f->f_lasti. A successful
1273 prediction effectively links the two codes together as if they
1274 were a single new opcode; accordingly,f->f_lasti will point to
1275 the first code in the pair (for instance, GET_ITER followed by
1276 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001277 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001279 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001280 next_instr = first_instr;
1281 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001282 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1283 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001284 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 stack_pointer = f->f_stacktop;
1286 assert(stack_pointer != NULL);
1287 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001288 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001289
Inada Naoki91234a12019-06-03 21:30:58 +09001290 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1291 co->co_opcache_flag++;
1292 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1293 if (_PyCode_InitOpcache(co) < 0) {
1294 return NULL;
1295 }
1296#if OPCACHE_STATS
1297 opcache_code_objects_extra_mem +=
1298 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1299 sizeof(_PyOpcache) * co->co_opcache_size;
1300 opcache_code_objects++;
1301#endif
1302 }
1303 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001304
Tim Peters5ca576e2001-06-18 22:08:13 +00001305#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001306 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001307#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001308
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001309 if (throwflag) /* support for generator.throw() */
1310 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001311
Victor Stinnerace47d72013-07-18 01:41:08 +02001312#ifdef Py_DEBUG
Victor Stinner0b72b232020-03-12 23:18:39 +01001313 /* _PyEval_EvalFrameDefault() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001314 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001315 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001316 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001317#endif
1318
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001319main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1322 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001323 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 /* Do periodic things. Doing this every time through
1326 the loop would add too much overhead, so we do it
1327 only every Nth instruction. We also do it if
1328 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1329 event needs attention (e.g. a signal handler or
1330 async I/O handler); see Py_AddPendingCall() and
1331 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001332
Eric Snow7bda9de2019-03-08 17:25:54 -07001333 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001334 opcode = _Py_OPCODE(*next_instr);
1335 if (opcode == SETUP_FINALLY ||
1336 opcode == SETUP_WITH ||
1337 opcode == BEFORE_ASYNC_WITH ||
1338 opcode == YIELD_FROM) {
1339 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001340 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001341 - If we're about to enter the 'with:'. It will prevent
1342 emitting a resource warning in the common idiom
1343 'with open(path) as file:'.
1344 - If we're about to enter the 'async with:'.
1345 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001346 *very* useful, but might help in some cases and it's
1347 traditional)
1348 - If we're resuming a chain of nested 'yield from' or
1349 'await' calls, then each frame is parked with YIELD_FROM
1350 as its next opcode. If the user hit control-C we want to
1351 wait until we've reached the innermost frame before
1352 running the signal handler and raising KeyboardInterrupt
1353 (see bpo-30039).
1354 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 goto fast_next_opcode;
1356 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001357
Victor Stinnerda2914d2020-03-20 09:29:08 +01001358 if (eval_frame_handle_pending(tstate) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001359 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 }
1361 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 fast_next_opcode:
1364 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001365
Łukasz Langaa785c872016-09-09 17:37:37 -07001366 if (PyDTrace_LINE_ENABLED())
1367 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001370
Victor Stinnerdab84232020-03-17 18:56:44 +01001371 if (_Py_TracingPossible(ceval2) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001372 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001373 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 /* see maybe_call_line_trace
1375 for expository comments */
1376 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 err = maybe_call_line_trace(tstate->c_tracefunc,
1379 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001380 tstate, f,
1381 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 /* Reload possibly changed frame fields */
1383 JUMPTO(f->f_lasti);
1384 if (f->f_stacktop != NULL) {
1385 stack_pointer = f->f_stacktop;
1386 f->f_stacktop = NULL;
1387 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001388 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001390 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001394
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001395 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001396 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001397#ifdef DYNAMIC_EXECUTION_PROFILE
1398#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 dxpairs[lastopcode][opcode]++;
1400 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001401#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001403#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001404
Guido van Rossum96a42c81992-01-12 02:29:51 +00001405#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 if (lltrace) {
1409 if (HAS_ARG(opcode)) {
1410 printf("%d: %d, %d\n",
1411 f->f_lasti, opcode, oparg);
1412 }
1413 else {
1414 printf("%d: %d\n",
1415 f->f_lasti, opcode);
1416 }
1417 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001418#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001423 It is essential that any operation that fails must goto error
1424 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001425
Benjamin Petersonddd19492018-09-16 22:38:02 -07001426 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001428 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001429
Benjamin Petersonddd19492018-09-16 22:38:02 -07001430 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001431 PyObject *value = GETLOCAL(oparg);
1432 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001433 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001434 UNBOUNDLOCAL_ERROR_MSG,
1435 PyTuple_GetItem(co->co_varnames, oparg));
1436 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001438 Py_INCREF(value);
1439 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001441 }
1442
Benjamin Petersonddd19492018-09-16 22:38:02 -07001443 case TARGET(LOAD_CONST): {
1444 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001445 PyObject *value = GETITEM(consts, oparg);
1446 Py_INCREF(value);
1447 PUSH(value);
1448 FAST_DISPATCH();
1449 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001450
Benjamin Petersonddd19492018-09-16 22:38:02 -07001451 case TARGET(STORE_FAST): {
1452 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001453 PyObject *value = POP();
1454 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001456 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001457
Benjamin Petersonddd19492018-09-16 22:38:02 -07001458 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001459 PyObject *value = POP();
1460 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001462 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001463
Benjamin Petersonddd19492018-09-16 22:38:02 -07001464 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001465 PyObject *top = TOP();
1466 PyObject *second = SECOND();
1467 SET_TOP(second);
1468 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001470 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001471
Benjamin Petersonddd19492018-09-16 22:38:02 -07001472 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001473 PyObject *top = TOP();
1474 PyObject *second = SECOND();
1475 PyObject *third = THIRD();
1476 SET_TOP(second);
1477 SET_SECOND(third);
1478 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001480 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001481
Benjamin Petersonddd19492018-09-16 22:38:02 -07001482 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001483 PyObject *top = TOP();
1484 PyObject *second = SECOND();
1485 PyObject *third = THIRD();
1486 PyObject *fourth = FOURTH();
1487 SET_TOP(second);
1488 SET_SECOND(third);
1489 SET_THIRD(fourth);
1490 SET_FOURTH(top);
1491 FAST_DISPATCH();
1492 }
1493
Benjamin Petersonddd19492018-09-16 22:38:02 -07001494 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001495 PyObject *top = TOP();
1496 Py_INCREF(top);
1497 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001499 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001500
Benjamin Petersonddd19492018-09-16 22:38:02 -07001501 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001502 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001503 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001504 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001505 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001506 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001507 SET_TOP(top);
1508 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001509 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001510 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001511
Benjamin Petersonddd19492018-09-16 22:38:02 -07001512 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001513 PyObject *value = TOP();
1514 PyObject *res = PyNumber_Positive(value);
1515 Py_DECREF(value);
1516 SET_TOP(res);
1517 if (res == NULL)
1518 goto error;
1519 DISPATCH();
1520 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001521
Benjamin Petersonddd19492018-09-16 22:38:02 -07001522 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001523 PyObject *value = TOP();
1524 PyObject *res = PyNumber_Negative(value);
1525 Py_DECREF(value);
1526 SET_TOP(res);
1527 if (res == NULL)
1528 goto error;
1529 DISPATCH();
1530 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001531
Benjamin Petersonddd19492018-09-16 22:38:02 -07001532 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001533 PyObject *value = TOP();
1534 int err = PyObject_IsTrue(value);
1535 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 if (err == 0) {
1537 Py_INCREF(Py_True);
1538 SET_TOP(Py_True);
1539 DISPATCH();
1540 }
1541 else if (err > 0) {
1542 Py_INCREF(Py_False);
1543 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 DISPATCH();
1545 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001546 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001547 goto error;
1548 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001549
Benjamin Petersonddd19492018-09-16 22:38:02 -07001550 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001551 PyObject *value = TOP();
1552 PyObject *res = PyNumber_Invert(value);
1553 Py_DECREF(value);
1554 SET_TOP(res);
1555 if (res == NULL)
1556 goto error;
1557 DISPATCH();
1558 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001559
Benjamin Petersonddd19492018-09-16 22:38:02 -07001560 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001561 PyObject *exp = POP();
1562 PyObject *base = TOP();
1563 PyObject *res = PyNumber_Power(base, exp, Py_None);
1564 Py_DECREF(base);
1565 Py_DECREF(exp);
1566 SET_TOP(res);
1567 if (res == NULL)
1568 goto error;
1569 DISPATCH();
1570 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001571
Benjamin Petersonddd19492018-09-16 22:38:02 -07001572 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001573 PyObject *right = POP();
1574 PyObject *left = TOP();
1575 PyObject *res = PyNumber_Multiply(left, right);
1576 Py_DECREF(left);
1577 Py_DECREF(right);
1578 SET_TOP(res);
1579 if (res == NULL)
1580 goto error;
1581 DISPATCH();
1582 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001583
Benjamin Petersonddd19492018-09-16 22:38:02 -07001584 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001585 PyObject *right = POP();
1586 PyObject *left = TOP();
1587 PyObject *res = PyNumber_MatrixMultiply(left, right);
1588 Py_DECREF(left);
1589 Py_DECREF(right);
1590 SET_TOP(res);
1591 if (res == NULL)
1592 goto error;
1593 DISPATCH();
1594 }
1595
Benjamin Petersonddd19492018-09-16 22:38:02 -07001596 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001597 PyObject *divisor = POP();
1598 PyObject *dividend = TOP();
1599 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1600 Py_DECREF(dividend);
1601 Py_DECREF(divisor);
1602 SET_TOP(quotient);
1603 if (quotient == NULL)
1604 goto error;
1605 DISPATCH();
1606 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001607
Benjamin Petersonddd19492018-09-16 22:38:02 -07001608 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001609 PyObject *divisor = POP();
1610 PyObject *dividend = TOP();
1611 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1612 Py_DECREF(dividend);
1613 Py_DECREF(divisor);
1614 SET_TOP(quotient);
1615 if (quotient == NULL)
1616 goto error;
1617 DISPATCH();
1618 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001619
Benjamin Petersonddd19492018-09-16 22:38:02 -07001620 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001621 PyObject *divisor = POP();
1622 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001623 PyObject *res;
1624 if (PyUnicode_CheckExact(dividend) && (
1625 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1626 // fast path; string formatting, but not if the RHS is a str subclass
1627 // (see issue28598)
1628 res = PyUnicode_Format(dividend, divisor);
1629 } else {
1630 res = PyNumber_Remainder(dividend, divisor);
1631 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001632 Py_DECREF(divisor);
1633 Py_DECREF(dividend);
1634 SET_TOP(res);
1635 if (res == NULL)
1636 goto error;
1637 DISPATCH();
1638 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001639
Benjamin Petersonddd19492018-09-16 22:38:02 -07001640 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001641 PyObject *right = POP();
1642 PyObject *left = TOP();
1643 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001644 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1645 CPython using bytecode, it is simply worthless.
1646 See http://bugs.python.org/issue21955 and
1647 http://bugs.python.org/issue10044 for the discussion. In short,
1648 no patch shown any impact on a realistic benchmark, only a minor
1649 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001650 if (PyUnicode_CheckExact(left) &&
1651 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001652 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001653 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001654 }
1655 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001656 sum = PyNumber_Add(left, right);
1657 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001658 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001659 Py_DECREF(right);
1660 SET_TOP(sum);
1661 if (sum == NULL)
1662 goto error;
1663 DISPATCH();
1664 }
1665
Benjamin Petersonddd19492018-09-16 22:38:02 -07001666 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001667 PyObject *right = POP();
1668 PyObject *left = TOP();
1669 PyObject *diff = PyNumber_Subtract(left, right);
1670 Py_DECREF(right);
1671 Py_DECREF(left);
1672 SET_TOP(diff);
1673 if (diff == NULL)
1674 goto error;
1675 DISPATCH();
1676 }
1677
Benjamin Petersonddd19492018-09-16 22:38:02 -07001678 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001679 PyObject *sub = POP();
1680 PyObject *container = TOP();
1681 PyObject *res = PyObject_GetItem(container, sub);
1682 Py_DECREF(container);
1683 Py_DECREF(sub);
1684 SET_TOP(res);
1685 if (res == NULL)
1686 goto error;
1687 DISPATCH();
1688 }
1689
Benjamin Petersonddd19492018-09-16 22:38:02 -07001690 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001691 PyObject *right = POP();
1692 PyObject *left = TOP();
1693 PyObject *res = PyNumber_Lshift(left, right);
1694 Py_DECREF(left);
1695 Py_DECREF(right);
1696 SET_TOP(res);
1697 if (res == NULL)
1698 goto error;
1699 DISPATCH();
1700 }
1701
Benjamin Petersonddd19492018-09-16 22:38:02 -07001702 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001703 PyObject *right = POP();
1704 PyObject *left = TOP();
1705 PyObject *res = PyNumber_Rshift(left, right);
1706 Py_DECREF(left);
1707 Py_DECREF(right);
1708 SET_TOP(res);
1709 if (res == NULL)
1710 goto error;
1711 DISPATCH();
1712 }
1713
Benjamin Petersonddd19492018-09-16 22:38:02 -07001714 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001715 PyObject *right = POP();
1716 PyObject *left = TOP();
1717 PyObject *res = PyNumber_And(left, right);
1718 Py_DECREF(left);
1719 Py_DECREF(right);
1720 SET_TOP(res);
1721 if (res == NULL)
1722 goto error;
1723 DISPATCH();
1724 }
1725
Benjamin Petersonddd19492018-09-16 22:38:02 -07001726 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001727 PyObject *right = POP();
1728 PyObject *left = TOP();
1729 PyObject *res = PyNumber_Xor(left, right);
1730 Py_DECREF(left);
1731 Py_DECREF(right);
1732 SET_TOP(res);
1733 if (res == NULL)
1734 goto error;
1735 DISPATCH();
1736 }
1737
Benjamin Petersonddd19492018-09-16 22:38:02 -07001738 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001739 PyObject *right = POP();
1740 PyObject *left = TOP();
1741 PyObject *res = PyNumber_Or(left, right);
1742 Py_DECREF(left);
1743 Py_DECREF(right);
1744 SET_TOP(res);
1745 if (res == NULL)
1746 goto error;
1747 DISPATCH();
1748 }
1749
Benjamin Petersonddd19492018-09-16 22:38:02 -07001750 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001751 PyObject *v = POP();
1752 PyObject *list = PEEK(oparg);
1753 int err;
1754 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001756 if (err != 0)
1757 goto error;
1758 PREDICT(JUMP_ABSOLUTE);
1759 DISPATCH();
1760 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001761
Benjamin Petersonddd19492018-09-16 22:38:02 -07001762 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001763 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001764 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001765 int err;
1766 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001767 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001768 if (err != 0)
1769 goto error;
1770 PREDICT(JUMP_ABSOLUTE);
1771 DISPATCH();
1772 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001773
Benjamin Petersonddd19492018-09-16 22:38:02 -07001774 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001775 PyObject *exp = POP();
1776 PyObject *base = TOP();
1777 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1778 Py_DECREF(base);
1779 Py_DECREF(exp);
1780 SET_TOP(res);
1781 if (res == NULL)
1782 goto error;
1783 DISPATCH();
1784 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001785
Benjamin Petersonddd19492018-09-16 22:38:02 -07001786 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001787 PyObject *right = POP();
1788 PyObject *left = TOP();
1789 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1790 Py_DECREF(left);
1791 Py_DECREF(right);
1792 SET_TOP(res);
1793 if (res == NULL)
1794 goto error;
1795 DISPATCH();
1796 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001797
Benjamin Petersonddd19492018-09-16 22:38:02 -07001798 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001799 PyObject *right = POP();
1800 PyObject *left = TOP();
1801 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1802 Py_DECREF(left);
1803 Py_DECREF(right);
1804 SET_TOP(res);
1805 if (res == NULL)
1806 goto error;
1807 DISPATCH();
1808 }
1809
Benjamin Petersonddd19492018-09-16 22:38:02 -07001810 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001811 PyObject *divisor = POP();
1812 PyObject *dividend = TOP();
1813 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1814 Py_DECREF(dividend);
1815 Py_DECREF(divisor);
1816 SET_TOP(quotient);
1817 if (quotient == NULL)
1818 goto error;
1819 DISPATCH();
1820 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001821
Benjamin Petersonddd19492018-09-16 22:38:02 -07001822 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001823 PyObject *divisor = POP();
1824 PyObject *dividend = TOP();
1825 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1826 Py_DECREF(dividend);
1827 Py_DECREF(divisor);
1828 SET_TOP(quotient);
1829 if (quotient == NULL)
1830 goto error;
1831 DISPATCH();
1832 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001833
Benjamin Petersonddd19492018-09-16 22:38:02 -07001834 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001835 PyObject *right = POP();
1836 PyObject *left = TOP();
1837 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1838 Py_DECREF(left);
1839 Py_DECREF(right);
1840 SET_TOP(mod);
1841 if (mod == NULL)
1842 goto error;
1843 DISPATCH();
1844 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001845
Benjamin Petersonddd19492018-09-16 22:38:02 -07001846 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001847 PyObject *right = POP();
1848 PyObject *left = TOP();
1849 PyObject *sum;
1850 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001851 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001852 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001853 }
1854 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001855 sum = PyNumber_InPlaceAdd(left, right);
1856 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001857 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001858 Py_DECREF(right);
1859 SET_TOP(sum);
1860 if (sum == NULL)
1861 goto error;
1862 DISPATCH();
1863 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001864
Benjamin Petersonddd19492018-09-16 22:38:02 -07001865 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001866 PyObject *right = POP();
1867 PyObject *left = TOP();
1868 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1869 Py_DECREF(left);
1870 Py_DECREF(right);
1871 SET_TOP(diff);
1872 if (diff == NULL)
1873 goto error;
1874 DISPATCH();
1875 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001876
Benjamin Petersonddd19492018-09-16 22:38:02 -07001877 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001878 PyObject *right = POP();
1879 PyObject *left = TOP();
1880 PyObject *res = PyNumber_InPlaceLshift(left, right);
1881 Py_DECREF(left);
1882 Py_DECREF(right);
1883 SET_TOP(res);
1884 if (res == NULL)
1885 goto error;
1886 DISPATCH();
1887 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001888
Benjamin Petersonddd19492018-09-16 22:38:02 -07001889 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001890 PyObject *right = POP();
1891 PyObject *left = TOP();
1892 PyObject *res = PyNumber_InPlaceRshift(left, right);
1893 Py_DECREF(left);
1894 Py_DECREF(right);
1895 SET_TOP(res);
1896 if (res == NULL)
1897 goto error;
1898 DISPATCH();
1899 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001900
Benjamin Petersonddd19492018-09-16 22:38:02 -07001901 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001902 PyObject *right = POP();
1903 PyObject *left = TOP();
1904 PyObject *res = PyNumber_InPlaceAnd(left, right);
1905 Py_DECREF(left);
1906 Py_DECREF(right);
1907 SET_TOP(res);
1908 if (res == NULL)
1909 goto error;
1910 DISPATCH();
1911 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001912
Benjamin Petersonddd19492018-09-16 22:38:02 -07001913 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001914 PyObject *right = POP();
1915 PyObject *left = TOP();
1916 PyObject *res = PyNumber_InPlaceXor(left, right);
1917 Py_DECREF(left);
1918 Py_DECREF(right);
1919 SET_TOP(res);
1920 if (res == NULL)
1921 goto error;
1922 DISPATCH();
1923 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001924
Benjamin Petersonddd19492018-09-16 22:38:02 -07001925 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001926 PyObject *right = POP();
1927 PyObject *left = TOP();
1928 PyObject *res = PyNumber_InPlaceOr(left, right);
1929 Py_DECREF(left);
1930 Py_DECREF(right);
1931 SET_TOP(res);
1932 if (res == NULL)
1933 goto error;
1934 DISPATCH();
1935 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001936
Benjamin Petersonddd19492018-09-16 22:38:02 -07001937 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001938 PyObject *sub = TOP();
1939 PyObject *container = SECOND();
1940 PyObject *v = THIRD();
1941 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001942 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001943 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001944 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001946 Py_DECREF(container);
1947 Py_DECREF(sub);
1948 if (err != 0)
1949 goto error;
1950 DISPATCH();
1951 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001952
Benjamin Petersonddd19492018-09-16 22:38:02 -07001953 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001954 PyObject *sub = TOP();
1955 PyObject *container = SECOND();
1956 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001957 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001958 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001959 err = PyObject_DelItem(container, sub);
1960 Py_DECREF(container);
1961 Py_DECREF(sub);
1962 if (err != 0)
1963 goto error;
1964 DISPATCH();
1965 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001966
Benjamin Petersonddd19492018-09-16 22:38:02 -07001967 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001968 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001969 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001970 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001971 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001972 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001973 _PyErr_SetString(tstate, PyExc_RuntimeError,
1974 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001975 Py_DECREF(value);
1976 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001977 }
Petr Viktorinffd97532020-02-11 17:46:57 +01001978 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001979 Py_DECREF(value);
1980 if (res == NULL)
1981 goto error;
1982 Py_DECREF(res);
1983 DISPATCH();
1984 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001985
Benjamin Petersonddd19492018-09-16 22:38:02 -07001986 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001987 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988 switch (oparg) {
1989 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001990 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001991 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001992 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001993 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001994 /* fall through */
1995 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02001996 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001997 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001998 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001999 break;
2000 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02002001 _PyErr_SetString(tstate, PyExc_SystemError,
2002 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 break;
2004 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002005 goto error;
2006 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002007
Benjamin Petersonddd19492018-09-16 22:38:02 -07002008 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002010 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002011 assert(EMPTY());
2012 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002013 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002014
Benjamin Petersonddd19492018-09-16 22:38:02 -07002015 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002016 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002017 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002018 PyObject *obj = TOP();
2019 PyTypeObject *type = Py_TYPE(obj);
2020
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002021 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002022 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002023 }
Yury Selivanov75445082015-05-11 22:57:16 -04002024
2025 if (getter != NULL) {
2026 iter = (*getter)(obj);
2027 Py_DECREF(obj);
2028 if (iter == NULL) {
2029 SET_TOP(NULL);
2030 goto error;
2031 }
2032 }
2033 else {
2034 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002035 _PyErr_Format(tstate, PyExc_TypeError,
2036 "'async for' requires an object with "
2037 "__aiter__ method, got %.100s",
2038 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002039 Py_DECREF(obj);
2040 goto error;
2041 }
2042
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002043 if (Py_TYPE(iter)->tp_as_async == NULL ||
2044 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002045
Yury Selivanov398ff912017-03-02 22:20:00 -05002046 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002047 _PyErr_Format(tstate, PyExc_TypeError,
2048 "'async for' received an object from __aiter__ "
2049 "that does not implement __anext__: %.100s",
2050 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002051 Py_DECREF(iter);
2052 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002053 }
2054
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002055 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002056 DISPATCH();
2057 }
2058
Benjamin Petersonddd19492018-09-16 22:38:02 -07002059 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002060 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002061 PyObject *next_iter = NULL;
2062 PyObject *awaitable = NULL;
2063 PyObject *aiter = TOP();
2064 PyTypeObject *type = Py_TYPE(aiter);
2065
Yury Selivanoveb636452016-09-08 22:01:51 -07002066 if (PyAsyncGen_CheckExact(aiter)) {
2067 awaitable = type->tp_as_async->am_anext(aiter);
2068 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002069 goto error;
2070 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002071 } else {
2072 if (type->tp_as_async != NULL){
2073 getter = type->tp_as_async->am_anext;
2074 }
Yury Selivanov75445082015-05-11 22:57:16 -04002075
Yury Selivanoveb636452016-09-08 22:01:51 -07002076 if (getter != NULL) {
2077 next_iter = (*getter)(aiter);
2078 if (next_iter == NULL) {
2079 goto error;
2080 }
2081 }
2082 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002083 _PyErr_Format(tstate, PyExc_TypeError,
2084 "'async for' requires an iterator with "
2085 "__anext__ method, got %.100s",
2086 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002087 goto error;
2088 }
Yury Selivanov75445082015-05-11 22:57:16 -04002089
Yury Selivanoveb636452016-09-08 22:01:51 -07002090 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2091 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002092 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002093 PyExc_TypeError,
2094 "'async for' received an invalid object "
2095 "from __anext__: %.100s",
2096 Py_TYPE(next_iter)->tp_name);
2097
2098 Py_DECREF(next_iter);
2099 goto error;
2100 } else {
2101 Py_DECREF(next_iter);
2102 }
2103 }
Yury Selivanov75445082015-05-11 22:57:16 -04002104
2105 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002106 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002107 DISPATCH();
2108 }
2109
Benjamin Petersonddd19492018-09-16 22:38:02 -07002110 case TARGET(GET_AWAITABLE): {
2111 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002112 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002113 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002114
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002115 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002116 int opcode_at_minus_3 = 0;
2117 if ((next_instr - first_instr) > 2) {
2118 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2119 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002120 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002121 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002122 _Py_OPCODE(next_instr[-2]));
2123 }
2124
Yury Selivanov75445082015-05-11 22:57:16 -04002125 Py_DECREF(iterable);
2126
Yury Selivanovc724bae2016-03-02 11:30:46 -05002127 if (iter != NULL && PyCoro_CheckExact(iter)) {
2128 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2129 if (yf != NULL) {
2130 /* `iter` is a coroutine object that is being
2131 awaited, `yf` is a pointer to the current awaitable
2132 being awaited on. */
2133 Py_DECREF(yf);
2134 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002135 _PyErr_SetString(tstate, PyExc_RuntimeError,
2136 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002137 /* The code below jumps to `error` if `iter` is NULL. */
2138 }
2139 }
2140
Yury Selivanov75445082015-05-11 22:57:16 -04002141 SET_TOP(iter); /* Even if it's NULL */
2142
2143 if (iter == NULL) {
2144 goto error;
2145 }
2146
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002147 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002148 DISPATCH();
2149 }
2150
Benjamin Petersonddd19492018-09-16 22:38:02 -07002151 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002152 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002153 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002154 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002155 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2156 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002157 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002158 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002159 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002160 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002161 else
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02002162 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002163 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002164 Py_DECREF(v);
2165 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002166 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002167 if (tstate->c_tracefunc != NULL
Victor Stinner438a12d2019-05-24 17:01:38 +02002168 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002169 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002170 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002171 if (err < 0)
2172 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002173 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002174 SET_TOP(val);
2175 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002176 }
Martin Panter95f53c12016-07-18 08:23:26 +00002177 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002178 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002179 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002180 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002181 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002182 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002183 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002184
Benjamin Petersonddd19492018-09-16 22:38:02 -07002185 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002186 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002187
2188 if (co->co_flags & CO_ASYNC_GENERATOR) {
2189 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2190 Py_DECREF(retval);
2191 if (w == NULL) {
2192 retval = NULL;
2193 goto error;
2194 }
2195 retval = w;
2196 }
2197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 f->f_stacktop = stack_pointer;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002199 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002200 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002201
Benjamin Petersonddd19492018-09-16 22:38:02 -07002202 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002203 PyObject *type, *value, *traceback;
2204 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002205 PyTryBlock *b = PyFrame_BlockPop(f);
2206 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002207 _PyErr_SetString(tstate, PyExc_SystemError,
2208 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002209 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002210 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002211 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2212 STACK_LEVEL() <= (b)->b_level + 4);
2213 exc_info = tstate->exc_info;
2214 type = exc_info->exc_type;
2215 value = exc_info->exc_value;
2216 traceback = exc_info->exc_traceback;
2217 exc_info->exc_type = POP();
2218 exc_info->exc_value = POP();
2219 exc_info->exc_traceback = POP();
2220 Py_XDECREF(type);
2221 Py_XDECREF(value);
2222 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002223 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002224 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002225
Benjamin Petersonddd19492018-09-16 22:38:02 -07002226 case TARGET(POP_BLOCK): {
2227 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002228 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002229 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002230 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002231
Mark Shannonfee55262019-11-21 09:11:43 +00002232 case TARGET(RERAISE): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002233 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002234 PyObject *val = POP();
2235 PyObject *tb = POP();
2236 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002237 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002238 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002239 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002240
Benjamin Petersonddd19492018-09-16 22:38:02 -07002241 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002242 PyObject *exc = POP();
2243 assert(PyExceptionClass_Check(exc));
2244 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2245 PyTryBlock *b = PyFrame_BlockPop(f);
2246 assert(b->b_type == EXCEPT_HANDLER);
2247 Py_DECREF(exc);
2248 UNWIND_EXCEPT_HANDLER(b);
2249 Py_DECREF(POP());
2250 JUMPBY(oparg);
2251 FAST_DISPATCH();
2252 }
2253 else {
2254 PyObject *val = POP();
2255 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002256 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002257 goto exception_unwind;
2258 }
2259 }
2260
Zackery Spytzce6a0702019-08-25 03:44:09 -06002261 case TARGET(LOAD_ASSERTION_ERROR): {
2262 PyObject *value = PyExc_AssertionError;
2263 Py_INCREF(value);
2264 PUSH(value);
2265 FAST_DISPATCH();
2266 }
2267
Benjamin Petersonddd19492018-09-16 22:38:02 -07002268 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002269 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002270
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002271 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002272 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002273 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002274 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002275 if (!_PyErr_Occurred(tstate)) {
2276 _PyErr_SetString(tstate, PyExc_NameError,
2277 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002278 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002279 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002280 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002281 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002282 }
2283 else {
2284 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2285 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002286 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002287 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2288 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002289 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2290 _PyErr_SetString(tstate, PyExc_NameError,
2291 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002292 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002293 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002295 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002296 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002297 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002298
Benjamin Petersonddd19492018-09-16 22:38:02 -07002299 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002300 PyObject *name = GETITEM(names, oparg);
2301 PyObject *v = POP();
2302 PyObject *ns = f->f_locals;
2303 int err;
2304 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002305 _PyErr_Format(tstate, PyExc_SystemError,
2306 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002308 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002309 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002310 if (PyDict_CheckExact(ns))
2311 err = PyDict_SetItem(ns, name, v);
2312 else
2313 err = PyObject_SetItem(ns, name, v);
2314 Py_DECREF(v);
2315 if (err != 0)
2316 goto error;
2317 DISPATCH();
2318 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002319
Benjamin Petersonddd19492018-09-16 22:38:02 -07002320 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002321 PyObject *name = GETITEM(names, oparg);
2322 PyObject *ns = f->f_locals;
2323 int err;
2324 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002325 _PyErr_Format(tstate, PyExc_SystemError,
2326 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002327 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002328 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002329 err = PyObject_DelItem(ns, name);
2330 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002331 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002332 NAME_ERROR_MSG,
2333 name);
2334 goto error;
2335 }
2336 DISPATCH();
2337 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002338
Benjamin Petersonddd19492018-09-16 22:38:02 -07002339 case TARGET(UNPACK_SEQUENCE): {
2340 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002341 PyObject *seq = POP(), *item, **items;
2342 if (PyTuple_CheckExact(seq) &&
2343 PyTuple_GET_SIZE(seq) == oparg) {
2344 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002345 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002346 item = items[oparg];
2347 Py_INCREF(item);
2348 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002349 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002350 } else if (PyList_CheckExact(seq) &&
2351 PyList_GET_SIZE(seq) == oparg) {
2352 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002354 item = items[oparg];
2355 Py_INCREF(item);
2356 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002358 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002360 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 } else {
2362 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002363 Py_DECREF(seq);
2364 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002366 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002367 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002369
Benjamin Petersonddd19492018-09-16 22:38:02 -07002370 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002371 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2372 PyObject *seq = POP();
2373
Victor Stinner438a12d2019-05-24 17:01:38 +02002374 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002375 stack_pointer + totalargs)) {
2376 stack_pointer += totalargs;
2377 } else {
2378 Py_DECREF(seq);
2379 goto error;
2380 }
2381 Py_DECREF(seq);
2382 DISPATCH();
2383 }
2384
Benjamin Petersonddd19492018-09-16 22:38:02 -07002385 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002386 PyObject *name = GETITEM(names, oparg);
2387 PyObject *owner = TOP();
2388 PyObject *v = SECOND();
2389 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002390 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002391 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002393 Py_DECREF(owner);
2394 if (err != 0)
2395 goto error;
2396 DISPATCH();
2397 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002398
Benjamin Petersonddd19492018-09-16 22:38:02 -07002399 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002400 PyObject *name = GETITEM(names, oparg);
2401 PyObject *owner = POP();
2402 int err;
2403 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2404 Py_DECREF(owner);
2405 if (err != 0)
2406 goto error;
2407 DISPATCH();
2408 }
2409
Benjamin Petersonddd19492018-09-16 22:38:02 -07002410 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002411 PyObject *name = GETITEM(names, oparg);
2412 PyObject *v = POP();
2413 int err;
2414 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002416 if (err != 0)
2417 goto error;
2418 DISPATCH();
2419 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002420
Benjamin Petersonddd19492018-09-16 22:38:02 -07002421 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002422 PyObject *name = GETITEM(names, oparg);
2423 int err;
2424 err = PyDict_DelItem(f->f_globals, name);
2425 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002426 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2427 format_exc_check_arg(tstate, PyExc_NameError,
2428 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002429 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002430 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002431 }
2432 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002433 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002434
Benjamin Petersonddd19492018-09-16 22:38:02 -07002435 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002436 PyObject *name = GETITEM(names, oparg);
2437 PyObject *locals = f->f_locals;
2438 PyObject *v;
2439 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002440 _PyErr_Format(tstate, PyExc_SystemError,
2441 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002442 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002443 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002444 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002445 v = PyDict_GetItemWithError(locals, name);
2446 if (v != NULL) {
2447 Py_INCREF(v);
2448 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002449 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002450 goto error;
2451 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002452 }
2453 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002454 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002455 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002456 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002457 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002458 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002459 }
2460 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002461 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002462 v = PyDict_GetItemWithError(f->f_globals, name);
2463 if (v != NULL) {
2464 Py_INCREF(v);
2465 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002466 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002467 goto error;
2468 }
2469 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002470 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002471 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002472 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002473 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002474 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002475 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002476 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002477 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002478 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002479 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002480 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002481 }
2482 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002483 v = PyObject_GetItem(f->f_builtins, name);
2484 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002485 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002486 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002487 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002488 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002489 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002490 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002491 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002492 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002493 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002495 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002496 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002497 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002498
Benjamin Petersonddd19492018-09-16 22:38:02 -07002499 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002500 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002501 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002502 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002503 && PyDict_CheckExact(f->f_builtins))
2504 {
Inada Naoki91234a12019-06-03 21:30:58 +09002505 OPCACHE_CHECK();
2506 if (co_opcache != NULL && co_opcache->optimized > 0) {
2507 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2508
2509 if (lg->globals_ver ==
2510 ((PyDictObject *)f->f_globals)->ma_version_tag
2511 && lg->builtins_ver ==
2512 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2513 {
2514 PyObject *ptr = lg->ptr;
2515 OPCACHE_STAT_GLOBAL_HIT();
2516 assert(ptr != NULL);
2517 Py_INCREF(ptr);
2518 PUSH(ptr);
2519 DISPATCH();
2520 }
2521 }
2522
2523 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002524 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002525 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002526 name);
2527 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002528 if (!_PyErr_OCCURRED()) {
2529 /* _PyDict_LoadGlobal() returns NULL without raising
2530 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002531 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002532 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002533 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002534 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002535 }
Inada Naoki91234a12019-06-03 21:30:58 +09002536
2537 if (co_opcache != NULL) {
2538 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2539
2540 if (co_opcache->optimized == 0) {
2541 /* Wasn't optimized before. */
2542 OPCACHE_STAT_GLOBAL_OPT();
2543 } else {
2544 OPCACHE_STAT_GLOBAL_MISS();
2545 }
2546
2547 co_opcache->optimized = 1;
2548 lg->globals_ver =
2549 ((PyDictObject *)f->f_globals)->ma_version_tag;
2550 lg->builtins_ver =
2551 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2552 lg->ptr = v; /* borrowed */
2553 }
2554
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002555 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002556 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002557 else {
2558 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002559
2560 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002561 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002562 v = PyObject_GetItem(f->f_globals, name);
2563 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002564 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002565 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002566 }
2567 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002568
Victor Stinnerb4efc962015-11-20 09:24:02 +01002569 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002570 v = PyObject_GetItem(f->f_builtins, name);
2571 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002572 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002573 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002574 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002575 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002576 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002577 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002578 }
2579 }
2580 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002581 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002582 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002583 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002584
Benjamin Petersonddd19492018-09-16 22:38:02 -07002585 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002586 PyObject *v = GETLOCAL(oparg);
2587 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002588 SETLOCAL(oparg, NULL);
2589 DISPATCH();
2590 }
2591 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002592 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002593 UNBOUNDLOCAL_ERROR_MSG,
2594 PyTuple_GetItem(co->co_varnames, oparg)
2595 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002596 goto error;
2597 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002598
Benjamin Petersonddd19492018-09-16 22:38:02 -07002599 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002600 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002601 PyObject *oldobj = PyCell_GET(cell);
2602 if (oldobj != NULL) {
2603 PyCell_SET(cell, NULL);
2604 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002605 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002606 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002607 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002608 goto error;
2609 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002610
Benjamin Petersonddd19492018-09-16 22:38:02 -07002611 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002612 PyObject *cell = freevars[oparg];
2613 Py_INCREF(cell);
2614 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002615 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002616 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002617
Benjamin Petersonddd19492018-09-16 22:38:02 -07002618 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002619 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002620 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002621 assert(locals);
2622 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2623 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2624 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2625 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2626 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002627 value = PyDict_GetItemWithError(locals, name);
2628 if (value != NULL) {
2629 Py_INCREF(value);
2630 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002631 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002632 goto error;
2633 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002634 }
2635 else {
2636 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002637 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002638 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002639 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002640 }
2641 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002642 }
2643 }
2644 if (!value) {
2645 PyObject *cell = freevars[oparg];
2646 value = PyCell_GET(cell);
2647 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002648 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002649 goto error;
2650 }
2651 Py_INCREF(value);
2652 }
2653 PUSH(value);
2654 DISPATCH();
2655 }
2656
Benjamin Petersonddd19492018-09-16 22:38:02 -07002657 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002658 PyObject *cell = freevars[oparg];
2659 PyObject *value = PyCell_GET(cell);
2660 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002661 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002662 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002663 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002664 Py_INCREF(value);
2665 PUSH(value);
2666 DISPATCH();
2667 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002668
Benjamin Petersonddd19492018-09-16 22:38:02 -07002669 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002670 PyObject *v = POP();
2671 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002672 PyObject *oldobj = PyCell_GET(cell);
2673 PyCell_SET(cell, v);
2674 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002675 DISPATCH();
2676 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002677
Benjamin Petersonddd19492018-09-16 22:38:02 -07002678 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002679 PyObject *str;
2680 PyObject *empty = PyUnicode_New(0, 0);
2681 if (empty == NULL) {
2682 goto error;
2683 }
2684 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2685 Py_DECREF(empty);
2686 if (str == NULL)
2687 goto error;
2688 while (--oparg >= 0) {
2689 PyObject *item = POP();
2690 Py_DECREF(item);
2691 }
2692 PUSH(str);
2693 DISPATCH();
2694 }
2695
Benjamin Petersonddd19492018-09-16 22:38:02 -07002696 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002697 PyObject *tup = PyTuple_New(oparg);
2698 if (tup == NULL)
2699 goto error;
2700 while (--oparg >= 0) {
2701 PyObject *item = POP();
2702 PyTuple_SET_ITEM(tup, oparg, item);
2703 }
2704 PUSH(tup);
2705 DISPATCH();
2706 }
2707
Benjamin Petersonddd19492018-09-16 22:38:02 -07002708 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002709 PyObject *list = PyList_New(oparg);
2710 if (list == NULL)
2711 goto error;
2712 while (--oparg >= 0) {
2713 PyObject *item = POP();
2714 PyList_SET_ITEM(list, oparg, item);
2715 }
2716 PUSH(list);
2717 DISPATCH();
2718 }
2719
Mark Shannon13bc1392020-01-23 09:25:17 +00002720 case TARGET(LIST_TO_TUPLE): {
2721 PyObject *list = POP();
2722 PyObject *tuple = PyList_AsTuple(list);
2723 Py_DECREF(list);
2724 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002725 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00002726 }
2727 PUSH(tuple);
2728 DISPATCH();
2729 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002730
Mark Shannon13bc1392020-01-23 09:25:17 +00002731 case TARGET(LIST_EXTEND): {
2732 PyObject *iterable = POP();
2733 PyObject *list = PEEK(oparg);
2734 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
2735 if (none_val == NULL) {
2736 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01002737 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00002738 {
Victor Stinner61f4db82020-01-28 03:37:45 +01002739 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00002740 _PyErr_Format(tstate, PyExc_TypeError,
2741 "Value after * must be an iterable, not %.200s",
2742 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002743 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002744 Py_DECREF(iterable);
2745 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002746 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002747 Py_DECREF(none_val);
2748 Py_DECREF(iterable);
2749 DISPATCH();
2750 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002751
Mark Shannon13bc1392020-01-23 09:25:17 +00002752 case TARGET(SET_UPDATE): {
2753 PyObject *iterable = POP();
2754 PyObject *set = PEEK(oparg);
2755 int err = _PySet_Update(set, iterable);
2756 Py_DECREF(iterable);
2757 if (err < 0) {
2758 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002759 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002760 DISPATCH();
2761 }
2762
Benjamin Petersonddd19492018-09-16 22:38:02 -07002763 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002764 PyObject *set = PySet_New(NULL);
2765 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002766 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002767 if (set == NULL)
2768 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002769 for (i = oparg; i > 0; i--) {
2770 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002771 if (err == 0)
2772 err = PySet_Add(set, item);
2773 Py_DECREF(item);
2774 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002775 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002776 if (err != 0) {
2777 Py_DECREF(set);
2778 goto error;
2779 }
2780 PUSH(set);
2781 DISPATCH();
2782 }
2783
Benjamin Petersonddd19492018-09-16 22:38:02 -07002784 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002785 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002786 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2787 if (map == NULL)
2788 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002789 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002790 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002791 PyObject *key = PEEK(2*i);
2792 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002793 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002794 if (err != 0) {
2795 Py_DECREF(map);
2796 goto error;
2797 }
2798 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002799
2800 while (oparg--) {
2801 Py_DECREF(POP());
2802 Py_DECREF(POP());
2803 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002804 PUSH(map);
2805 DISPATCH();
2806 }
2807
Benjamin Petersonddd19492018-09-16 22:38:02 -07002808 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002809 _Py_IDENTIFIER(__annotations__);
2810 int err;
2811 PyObject *ann_dict;
2812 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002813 _PyErr_Format(tstate, PyExc_SystemError,
2814 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002815 goto error;
2816 }
2817 /* check if __annotations__ in locals()... */
2818 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002819 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002820 &PyId___annotations__);
2821 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002822 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002823 goto error;
2824 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002825 /* ...if not, create a new one */
2826 ann_dict = PyDict_New();
2827 if (ann_dict == NULL) {
2828 goto error;
2829 }
2830 err = _PyDict_SetItemId(f->f_locals,
2831 &PyId___annotations__, ann_dict);
2832 Py_DECREF(ann_dict);
2833 if (err != 0) {
2834 goto error;
2835 }
2836 }
2837 }
2838 else {
2839 /* do the same if locals() is not a dict */
2840 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2841 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002842 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002843 }
2844 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2845 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002846 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002847 goto error;
2848 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002849 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002850 ann_dict = PyDict_New();
2851 if (ann_dict == NULL) {
2852 goto error;
2853 }
2854 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2855 Py_DECREF(ann_dict);
2856 if (err != 0) {
2857 goto error;
2858 }
2859 }
2860 else {
2861 Py_DECREF(ann_dict);
2862 }
2863 }
2864 DISPATCH();
2865 }
2866
Benjamin Petersonddd19492018-09-16 22:38:02 -07002867 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002868 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002869 PyObject *map;
2870 PyObject *keys = TOP();
2871 if (!PyTuple_CheckExact(keys) ||
2872 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002873 _PyErr_SetString(tstate, PyExc_SystemError,
2874 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002875 goto error;
2876 }
2877 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2878 if (map == NULL) {
2879 goto error;
2880 }
2881 for (i = oparg; i > 0; i--) {
2882 int err;
2883 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2884 PyObject *value = PEEK(i + 1);
2885 err = PyDict_SetItem(map, key, value);
2886 if (err != 0) {
2887 Py_DECREF(map);
2888 goto error;
2889 }
2890 }
2891
2892 Py_DECREF(POP());
2893 while (oparg--) {
2894 Py_DECREF(POP());
2895 }
2896 PUSH(map);
2897 DISPATCH();
2898 }
2899
Mark Shannon8a4cd702020-01-27 09:57:45 +00002900 case TARGET(DICT_UPDATE): {
2901 PyObject *update = POP();
2902 PyObject *dict = PEEK(oparg);
2903 if (PyDict_Update(dict, update) < 0) {
2904 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2905 _PyErr_Format(tstate, PyExc_TypeError,
2906 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01002907 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002908 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002909 Py_DECREF(update);
2910 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002911 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002912 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002913 DISPATCH();
2914 }
2915
Mark Shannon8a4cd702020-01-27 09:57:45 +00002916 case TARGET(DICT_MERGE): {
2917 PyObject *update = POP();
2918 PyObject *dict = PEEK(oparg);
2919
2920 if (_PyDict_MergeEx(dict, update, 2) < 0) {
2921 format_kwargs_error(tstate, PEEK(2 + oparg), update);
2922 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002923 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002924 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002925 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07002926 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002927 DISPATCH();
2928 }
2929
Benjamin Petersonddd19492018-09-16 22:38:02 -07002930 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02002931 PyObject *value = TOP();
2932 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002933 PyObject *map;
2934 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002935 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002936 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002937 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002938 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002939 Py_DECREF(value);
2940 Py_DECREF(key);
2941 if (err != 0)
2942 goto error;
2943 PREDICT(JUMP_ABSOLUTE);
2944 DISPATCH();
2945 }
2946
Benjamin Petersonddd19492018-09-16 22:38:02 -07002947 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002948 PyObject *name = GETITEM(names, oparg);
2949 PyObject *owner = TOP();
2950 PyObject *res = PyObject_GetAttr(owner, name);
2951 Py_DECREF(owner);
2952 SET_TOP(res);
2953 if (res == NULL)
2954 goto error;
2955 DISPATCH();
2956 }
2957
Benjamin Petersonddd19492018-09-16 22:38:02 -07002958 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00002959 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002960 PyObject *right = POP();
2961 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00002962 PyObject *res = PyObject_RichCompare(left, right, oparg);
2963 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002964 Py_DECREF(left);
2965 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002966 if (res == NULL)
2967 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002968 PREDICT(POP_JUMP_IF_FALSE);
2969 PREDICT(POP_JUMP_IF_TRUE);
2970 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002971 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002972
Mark Shannon9af0e472020-01-14 10:12:45 +00002973 case TARGET(IS_OP): {
2974 PyObject *right = POP();
2975 PyObject *left = TOP();
2976 int res = (left == right)^oparg;
2977 PyObject *b = res ? Py_True : Py_False;
2978 Py_INCREF(b);
2979 SET_TOP(b);
2980 Py_DECREF(left);
2981 Py_DECREF(right);
2982 PREDICT(POP_JUMP_IF_FALSE);
2983 PREDICT(POP_JUMP_IF_TRUE);
2984 FAST_DISPATCH();
2985 }
2986
2987 case TARGET(CONTAINS_OP): {
2988 PyObject *right = POP();
2989 PyObject *left = POP();
2990 int res = PySequence_Contains(right, left);
2991 Py_DECREF(left);
2992 Py_DECREF(right);
2993 if (res < 0) {
2994 goto error;
2995 }
2996 PyObject *b = (res^oparg) ? Py_True : Py_False;
2997 Py_INCREF(b);
2998 PUSH(b);
2999 PREDICT(POP_JUMP_IF_FALSE);
3000 PREDICT(POP_JUMP_IF_TRUE);
3001 FAST_DISPATCH();
3002 }
3003
3004#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
3005 "BaseException is not allowed"
3006
3007 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
3008 PyObject *right = POP();
3009 PyObject *left = POP();
3010 if (PyTuple_Check(right)) {
3011 Py_ssize_t i, length;
3012 length = PyTuple_GET_SIZE(right);
3013 for (i = 0; i < length; i++) {
3014 PyObject *exc = PyTuple_GET_ITEM(right, i);
3015 if (!PyExceptionClass_Check(exc)) {
3016 _PyErr_SetString(tstate, PyExc_TypeError,
3017 CANNOT_CATCH_MSG);
3018 Py_DECREF(left);
3019 Py_DECREF(right);
3020 goto error;
3021 }
3022 }
3023 }
3024 else {
3025 if (!PyExceptionClass_Check(right)) {
3026 _PyErr_SetString(tstate, PyExc_TypeError,
3027 CANNOT_CATCH_MSG);
3028 Py_DECREF(left);
3029 Py_DECREF(right);
3030 goto error;
3031 }
3032 }
3033 int res = PyErr_GivenExceptionMatches(left, right);
3034 Py_DECREF(left);
3035 Py_DECREF(right);
3036 if (res > 0) {
3037 /* Exception matches -- Do nothing */;
3038 }
3039 else if (res == 0) {
3040 JUMPTO(oparg);
3041 }
3042 else {
3043 goto error;
3044 }
3045 DISPATCH();
3046 }
3047
Benjamin Petersonddd19492018-09-16 22:38:02 -07003048 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003049 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003050 PyObject *fromlist = POP();
3051 PyObject *level = TOP();
3052 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003053 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003054 Py_DECREF(level);
3055 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003056 SET_TOP(res);
3057 if (res == NULL)
3058 goto error;
3059 DISPATCH();
3060 }
3061
Benjamin Petersonddd19492018-09-16 22:38:02 -07003062 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003063 PyObject *from = POP(), *locals;
3064 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003065 if (PyFrame_FastToLocalsWithError(f) < 0) {
3066 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003067 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003068 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003069
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003070 locals = f->f_locals;
3071 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003072 _PyErr_SetString(tstate, PyExc_SystemError,
3073 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003074 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003075 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003076 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003077 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003078 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003079 Py_DECREF(from);
3080 if (err != 0)
3081 goto error;
3082 DISPATCH();
3083 }
Guido van Rossum25831651993-05-19 14:50:45 +00003084
Benjamin Petersonddd19492018-09-16 22:38:02 -07003085 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003086 PyObject *name = GETITEM(names, oparg);
3087 PyObject *from = TOP();
3088 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003089 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003090 PUSH(res);
3091 if (res == NULL)
3092 goto error;
3093 DISPATCH();
3094 }
Thomas Wouters52152252000-08-17 22:55:00 +00003095
Benjamin Petersonddd19492018-09-16 22:38:02 -07003096 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003097 JUMPBY(oparg);
3098 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003099 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003100
Benjamin Petersonddd19492018-09-16 22:38:02 -07003101 case TARGET(POP_JUMP_IF_FALSE): {
3102 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003103 PyObject *cond = POP();
3104 int err;
3105 if (cond == Py_True) {
3106 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003107 FAST_DISPATCH();
3108 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003109 if (cond == Py_False) {
3110 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003111 JUMPTO(oparg);
3112 FAST_DISPATCH();
3113 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003114 err = PyObject_IsTrue(cond);
3115 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003116 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003117 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003118 else if (err == 0)
3119 JUMPTO(oparg);
3120 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003121 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003122 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003123 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003124
Benjamin Petersonddd19492018-09-16 22:38:02 -07003125 case TARGET(POP_JUMP_IF_TRUE): {
3126 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003127 PyObject *cond = POP();
3128 int err;
3129 if (cond == Py_False) {
3130 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003131 FAST_DISPATCH();
3132 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003133 if (cond == Py_True) {
3134 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003135 JUMPTO(oparg);
3136 FAST_DISPATCH();
3137 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003138 err = PyObject_IsTrue(cond);
3139 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003140 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003141 JUMPTO(oparg);
3142 }
3143 else if (err == 0)
3144 ;
3145 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003146 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003147 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003148 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003149
Benjamin Petersonddd19492018-09-16 22:38:02 -07003150 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003151 PyObject *cond = TOP();
3152 int err;
3153 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003154 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003155 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003156 FAST_DISPATCH();
3157 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003158 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003159 JUMPTO(oparg);
3160 FAST_DISPATCH();
3161 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003162 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003163 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003164 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003165 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003166 }
3167 else if (err == 0)
3168 JUMPTO(oparg);
3169 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003170 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003171 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003172 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003173
Benjamin Petersonddd19492018-09-16 22:38:02 -07003174 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003175 PyObject *cond = TOP();
3176 int err;
3177 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003178 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003179 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003180 FAST_DISPATCH();
3181 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003182 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003183 JUMPTO(oparg);
3184 FAST_DISPATCH();
3185 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003186 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003187 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003188 JUMPTO(oparg);
3189 }
3190 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003191 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003192 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003193 }
3194 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003195 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003196 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003197 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003198
Benjamin Petersonddd19492018-09-16 22:38:02 -07003199 case TARGET(JUMP_ABSOLUTE): {
3200 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003201 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003202#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003203 /* Enabling this path speeds-up all while and for-loops by bypassing
3204 the per-loop checks for signals. By default, this should be turned-off
3205 because it prevents detection of a control-break in tight loops like
3206 "while 1: pass". Compile with this option turned-on when you need
3207 the speed-up and do not need break checking inside tight loops (ones
3208 that contain only instructions ending with FAST_DISPATCH).
3209 */
3210 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003211#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003212 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003213#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003214 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003215
Benjamin Petersonddd19492018-09-16 22:38:02 -07003216 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003217 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003218 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003219 PyObject *iter = PyObject_GetIter(iterable);
3220 Py_DECREF(iterable);
3221 SET_TOP(iter);
3222 if (iter == NULL)
3223 goto error;
3224 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003225 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003226 DISPATCH();
3227 }
3228
Benjamin Petersonddd19492018-09-16 22:38:02 -07003229 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003230 /* before: [obj]; after [getiter(obj)] */
3231 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003232 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003233 if (PyCoro_CheckExact(iterable)) {
3234 /* `iterable` is a coroutine */
3235 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3236 /* and it is used in a 'yield from' expression of a
3237 regular generator. */
3238 Py_DECREF(iterable);
3239 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003240 _PyErr_SetString(tstate, PyExc_TypeError,
3241 "cannot 'yield from' a coroutine object "
3242 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003243 goto error;
3244 }
3245 }
3246 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003247 /* `iterable` is not a generator. */
3248 iter = PyObject_GetIter(iterable);
3249 Py_DECREF(iterable);
3250 SET_TOP(iter);
3251 if (iter == NULL)
3252 goto error;
3253 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003254 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003255 DISPATCH();
3256 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003257
Benjamin Petersonddd19492018-09-16 22:38:02 -07003258 case TARGET(FOR_ITER): {
3259 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003260 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003261 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01003262 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003263 if (next != NULL) {
3264 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003265 PREDICT(STORE_FAST);
3266 PREDICT(UNPACK_SEQUENCE);
3267 DISPATCH();
3268 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003269 if (_PyErr_Occurred(tstate)) {
3270 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003271 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003272 }
3273 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003274 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003275 }
3276 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003277 }
3278 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003279 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003280 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003281 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003282 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003283 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003284 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003285
Benjamin Petersonddd19492018-09-16 22:38:02 -07003286 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003287 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003288 STACK_LEVEL());
3289 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003290 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003291
Benjamin Petersonddd19492018-09-16 22:38:02 -07003292 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003293 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003294 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04003295 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003296 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003297 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003298 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003299 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003300 }
3301 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
3302 if (exit == NULL) {
3303 Py_DECREF(enter);
3304 goto error;
3305 }
Yury Selivanov75445082015-05-11 22:57:16 -04003306 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04003307 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003308 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003309 Py_DECREF(enter);
3310 if (res == NULL)
3311 goto error;
3312 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003313 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003314 DISPATCH();
3315 }
3316
Benjamin Petersonddd19492018-09-16 22:38:02 -07003317 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003318 PyObject *res = POP();
3319 /* Setup the finally block before pushing the result
3320 of __aenter__ on the stack. */
3321 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3322 STACK_LEVEL());
3323 PUSH(res);
3324 DISPATCH();
3325 }
3326
Benjamin Petersonddd19492018-09-16 22:38:02 -07003327 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003328 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003329 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003330 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003331 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003332 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003333 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003334 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003335 }
3336 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003337 if (exit == NULL) {
3338 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003339 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003340 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003341 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003342 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003343 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003344 Py_DECREF(enter);
3345 if (res == NULL)
3346 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003347 /* Setup the finally block before pushing the result
3348 of __enter__ on the stack. */
3349 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3350 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003351
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003352 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003353 DISPATCH();
3354 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003355
Mark Shannonfee55262019-11-21 09:11:43 +00003356 case TARGET(WITH_EXCEPT_START): {
3357 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003358 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00003359 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
3360 - SEVENTH: the context.__exit__ bound method
3361 We call SEVENTH(TOP, SECOND, THIRD).
3362 Then we push again the TOP exception and the __exit__
3363 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003364 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003365 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003366 PyObject *exc, *val, *tb, *res;
3367
Victor Stinner842cfff2016-12-01 14:45:31 +01003368 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00003369 val = SECOND();
3370 tb = THIRD();
3371 assert(exc != Py_None);
3372 assert(!PyLong_Check(exc));
3373 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003374 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01003375 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003376 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003377 if (res == NULL)
3378 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003379
Yury Selivanov75445082015-05-11 22:57:16 -04003380 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003381 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003382 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003383
Benjamin Petersonddd19492018-09-16 22:38:02 -07003384 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003385 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003386 PyObject *name = GETITEM(names, oparg);
3387 PyObject *obj = TOP();
3388 PyObject *meth = NULL;
3389
3390 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3391
Yury Selivanovf2392132016-12-13 19:03:51 -05003392 if (meth == NULL) {
3393 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003394 goto error;
3395 }
3396
3397 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003398 /* We can bypass temporary bound method object.
3399 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003400
INADA Naoki015bce62017-01-16 17:23:30 +09003401 meth | self | arg1 | ... | argN
3402 */
3403 SET_TOP(meth);
3404 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003405 }
3406 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003407 /* meth is not an unbound method (but a regular attr, or
3408 something was returned by a descriptor protocol). Set
3409 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003410 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003411
3412 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003413 */
INADA Naoki015bce62017-01-16 17:23:30 +09003414 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003415 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003416 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003417 }
3418 DISPATCH();
3419 }
3420
Benjamin Petersonddd19492018-09-16 22:38:02 -07003421 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003422 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003423 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003424
3425 sp = stack_pointer;
3426
INADA Naoki015bce62017-01-16 17:23:30 +09003427 meth = PEEK(oparg + 2);
3428 if (meth == NULL) {
3429 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3430 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003431
3432 Stack layout:
3433
INADA Naoki015bce62017-01-16 17:23:30 +09003434 ... | NULL | callable | arg1 | ... | argN
3435 ^- TOP()
3436 ^- (-oparg)
3437 ^- (-oparg-1)
3438 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003439
Ville Skyttä49b27342017-08-03 09:00:59 +03003440 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003441 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003442 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003443 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003444 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003445 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003446 }
3447 else {
3448 /* This is a method call. Stack layout:
3449
INADA Naoki015bce62017-01-16 17:23:30 +09003450 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003451 ^- TOP()
3452 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003453 ^- (-oparg-1)
3454 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003455
INADA Naoki015bce62017-01-16 17:23:30 +09003456 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003457 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003458 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003459 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003460 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003461 stack_pointer = sp;
3462 }
3463
3464 PUSH(res);
3465 if (res == NULL)
3466 goto error;
3467 DISPATCH();
3468 }
3469
Benjamin Petersonddd19492018-09-16 22:38:02 -07003470 case TARGET(CALL_FUNCTION): {
3471 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003472 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003473 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003474 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003475 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003476 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003477 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003478 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003479 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003480 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003481 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003482
Benjamin Petersonddd19492018-09-16 22:38:02 -07003483 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003484 PyObject **sp, *res, *names;
3485
3486 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003487 assert(PyTuple_Check(names));
3488 assert(PyTuple_GET_SIZE(names) <= oparg);
3489 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003490 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003491 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003492 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003493 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003494 Py_DECREF(names);
3495
3496 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003497 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003498 }
3499 DISPATCH();
3500 }
3501
Benjamin Petersonddd19492018-09-16 22:38:02 -07003502 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003503 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003504 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003505 if (oparg & 0x01) {
3506 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003507 if (!PyDict_CheckExact(kwargs)) {
3508 PyObject *d = PyDict_New();
3509 if (d == NULL)
3510 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003511 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003512 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003513 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003514 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003515 goto error;
3516 }
3517 Py_DECREF(kwargs);
3518 kwargs = d;
3519 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003520 assert(PyDict_CheckExact(kwargs));
3521 }
3522 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003523 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003524 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003525 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003526 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003527 goto error;
3528 }
3529 Py_SETREF(callargs, PySequence_Tuple(callargs));
3530 if (callargs == NULL) {
3531 goto error;
3532 }
3533 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003534 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003535
Victor Stinner09532fe2019-05-10 23:39:09 +02003536 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003537 Py_DECREF(func);
3538 Py_DECREF(callargs);
3539 Py_XDECREF(kwargs);
3540
3541 SET_TOP(result);
3542 if (result == NULL) {
3543 goto error;
3544 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003545 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003546 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003547
Benjamin Petersonddd19492018-09-16 22:38:02 -07003548 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003549 PyObject *qualname = POP();
3550 PyObject *codeobj = POP();
3551 PyFunctionObject *func = (PyFunctionObject *)
3552 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003553
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003554 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003555 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003556 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003557 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003558 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003559
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003560 if (oparg & 0x08) {
3561 assert(PyTuple_CheckExact(TOP()));
3562 func ->func_closure = POP();
3563 }
3564 if (oparg & 0x04) {
3565 assert(PyDict_CheckExact(TOP()));
3566 func->func_annotations = POP();
3567 }
3568 if (oparg & 0x02) {
3569 assert(PyDict_CheckExact(TOP()));
3570 func->func_kwdefaults = POP();
3571 }
3572 if (oparg & 0x01) {
3573 assert(PyTuple_CheckExact(TOP()));
3574 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003575 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003576
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003577 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003578 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003579 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003580
Benjamin Petersonddd19492018-09-16 22:38:02 -07003581 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003582 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003583 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003584 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003585 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003586 step = NULL;
3587 stop = POP();
3588 start = TOP();
3589 slice = PySlice_New(start, stop, step);
3590 Py_DECREF(start);
3591 Py_DECREF(stop);
3592 Py_XDECREF(step);
3593 SET_TOP(slice);
3594 if (slice == NULL)
3595 goto error;
3596 DISPATCH();
3597 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003598
Benjamin Petersonddd19492018-09-16 22:38:02 -07003599 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003600 /* Handles f-string value formatting. */
3601 PyObject *result;
3602 PyObject *fmt_spec;
3603 PyObject *value;
3604 PyObject *(*conv_fn)(PyObject *);
3605 int which_conversion = oparg & FVC_MASK;
3606 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3607
3608 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003609 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003610
3611 /* See if any conversion is specified. */
3612 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003613 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003614 case FVC_STR: conv_fn = PyObject_Str; break;
3615 case FVC_REPR: conv_fn = PyObject_Repr; break;
3616 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003617 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003618 _PyErr_Format(tstate, PyExc_SystemError,
3619 "unexpected conversion flag %d",
3620 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003621 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003622 }
3623
3624 /* If there's a conversion function, call it and replace
3625 value with that result. Otherwise, just use value,
3626 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003627 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003628 result = conv_fn(value);
3629 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003630 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003631 Py_XDECREF(fmt_spec);
3632 goto error;
3633 }
3634 value = result;
3635 }
3636
3637 /* If value is a unicode object, and there's no fmt_spec,
3638 then we know the result of format(value) is value
3639 itself. In that case, skip calling format(). I plan to
3640 move this optimization in to PyObject_Format()
3641 itself. */
3642 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3643 /* Do nothing, just transfer ownership to result. */
3644 result = value;
3645 } else {
3646 /* Actually call format(). */
3647 result = PyObject_Format(value, fmt_spec);
3648 Py_DECREF(value);
3649 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003650 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003651 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003652 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003653 }
3654
Eric V. Smith135d5f42016-02-05 18:23:08 -05003655 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003656 DISPATCH();
3657 }
3658
Benjamin Petersonddd19492018-09-16 22:38:02 -07003659 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003660 int oldoparg = oparg;
3661 NEXTOPARG();
3662 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003663 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003664 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003665
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003666
Antoine Pitrou042b1282010-08-13 21:15:58 +00003667#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003668 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003669#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003670 default:
3671 fprintf(stderr,
3672 "XXX lineno: %d, opcode: %d\n",
3673 PyFrame_GetLineNumber(f),
3674 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003675 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003676 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003678 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003679
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003680 /* This should never be reached. Every opcode should end with DISPATCH()
3681 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003682 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003683
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003684error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003685 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003686#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003687 if (!_PyErr_Occurred(tstate)) {
3688 _PyErr_SetString(tstate, PyExc_SystemError,
3689 "error return without exception set");
3690 }
Victor Stinner365b6932013-07-12 00:11:58 +02003691#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003692 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003693#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003694
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003695 /* Log traceback info. */
3696 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003697
Benjamin Peterson51f46162013-01-23 08:38:47 -05003698 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003699 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3700 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003701
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003702exception_unwind:
3703 /* Unwind stacks if an exception occurred */
3704 while (f->f_iblock > 0) {
3705 /* Pop the current block. */
3706 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003708 if (b->b_type == EXCEPT_HANDLER) {
3709 UNWIND_EXCEPT_HANDLER(b);
3710 continue;
3711 }
3712 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003713 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003714 PyObject *exc, *val, *tb;
3715 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003716 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003717 /* Beware, this invalidates all b->b_* fields */
3718 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003719 PUSH(exc_info->exc_traceback);
3720 PUSH(exc_info->exc_value);
3721 if (exc_info->exc_type != NULL) {
3722 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003723 }
3724 else {
3725 Py_INCREF(Py_None);
3726 PUSH(Py_None);
3727 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003728 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003729 /* Make the raw exception data
3730 available to the handler,
3731 so a program can emulate the
3732 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003733 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003734 if (tb != NULL)
3735 PyException_SetTraceback(val, tb);
3736 else
3737 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003738 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003739 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003740 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003741 exc_info->exc_value = val;
3742 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003743 if (tb == NULL)
3744 tb = Py_None;
3745 Py_INCREF(tb);
3746 PUSH(tb);
3747 PUSH(val);
3748 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003749 JUMPTO(handler);
Victor Stinnerdab84232020-03-17 18:56:44 +01003750 if (_Py_TracingPossible(ceval2)) {
Pablo Galindo4c53e632020-01-10 09:24:22 +00003751 int needs_new_execution_window = (f->f_lasti < instr_lb || f->f_lasti >= instr_ub);
3752 int needs_line_update = (f->f_lasti == instr_lb || f->f_lasti < instr_prev);
3753 /* Make sure that we trace line after exception if we are in a new execution
3754 * window or we don't need a line update and we are not in the first instruction
3755 * of the line. */
3756 if (needs_new_execution_window || (!needs_line_update && instr_lb > 0)) {
3757 instr_prev = INT_MAX;
3758 }
Mark Shannonfee55262019-11-21 09:11:43 +00003759 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003760 /* Resume normal execution */
3761 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003762 }
3763 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003764
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003765 /* End the loop as we still have an error */
3766 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003767 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003768
Pablo Galindof00828a2019-05-09 16:52:02 +01003769 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003770 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003772 /* Pop remaining stack entries. */
3773 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003774 PyObject *o = POP();
3775 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003776 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003777
Mark Shannone7c9f4a2020-01-13 12:51:26 +00003778exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003779 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003780 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003781 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3782 tstate, f, PyTrace_RETURN, retval)) {
3783 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003784 }
3785 }
3786 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003787 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3788 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003789 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003790 }
3791 }
3792 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003794 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003795exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003796 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3797 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01003798 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrou58720d62013-08-05 23:26:40 +02003799 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003800 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003801
Victor Stinner0b72b232020-03-12 23:18:39 +01003802 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00003803}
3804
Benjamin Petersonb204a422011-06-05 22:04:07 -05003805static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003806format_missing(PyThreadState *tstate, const char *kind,
3807 PyCodeObject *co, PyObject *names)
Benjamin Petersone109c702011-06-24 09:37:26 -05003808{
3809 int err;
3810 Py_ssize_t len = PyList_GET_SIZE(names);
3811 PyObject *name_str, *comma, *tail, *tmp;
3812
3813 assert(PyList_CheckExact(names));
3814 assert(len >= 1);
3815 /* Deal with the joys of natural language. */
3816 switch (len) {
3817 case 1:
3818 name_str = PyList_GET_ITEM(names, 0);
3819 Py_INCREF(name_str);
3820 break;
3821 case 2:
3822 name_str = PyUnicode_FromFormat("%U and %U",
3823 PyList_GET_ITEM(names, len - 2),
3824 PyList_GET_ITEM(names, len - 1));
3825 break;
3826 default:
3827 tail = PyUnicode_FromFormat(", %U, and %U",
3828 PyList_GET_ITEM(names, len - 2),
3829 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003830 if (tail == NULL)
3831 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003832 /* Chop off the last two objects in the list. This shouldn't actually
3833 fail, but we can't be too careful. */
3834 err = PyList_SetSlice(names, len - 2, len, NULL);
3835 if (err == -1) {
3836 Py_DECREF(tail);
3837 return;
3838 }
3839 /* Stitch everything up into a nice comma-separated list. */
3840 comma = PyUnicode_FromString(", ");
3841 if (comma == NULL) {
3842 Py_DECREF(tail);
3843 return;
3844 }
3845 tmp = PyUnicode_Join(comma, names);
3846 Py_DECREF(comma);
3847 if (tmp == NULL) {
3848 Py_DECREF(tail);
3849 return;
3850 }
3851 name_str = PyUnicode_Concat(tmp, tail);
3852 Py_DECREF(tmp);
3853 Py_DECREF(tail);
3854 break;
3855 }
3856 if (name_str == NULL)
3857 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003858 _PyErr_Format(tstate, PyExc_TypeError,
3859 "%U() missing %i required %s argument%s: %U",
3860 co->co_name,
3861 len,
3862 kind,
3863 len == 1 ? "" : "s",
3864 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003865 Py_DECREF(name_str);
3866}
3867
3868static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003869missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3870 Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003871 PyObject **fastlocals)
3872{
Victor Stinner74319ae2016-08-25 00:04:09 +02003873 Py_ssize_t i, j = 0;
3874 Py_ssize_t start, end;
3875 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003876 const char *kind = positional ? "positional" : "keyword-only";
3877 PyObject *missing_names;
3878
3879 /* Compute the names of the arguments that are missing. */
3880 missing_names = PyList_New(missing);
3881 if (missing_names == NULL)
3882 return;
3883 if (positional) {
3884 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01003885 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003886 }
3887 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003888 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003889 end = start + co->co_kwonlyargcount;
3890 }
3891 for (i = start; i < end; i++) {
3892 if (GETLOCAL(i) == NULL) {
3893 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3894 PyObject *name = PyObject_Repr(raw);
3895 if (name == NULL) {
3896 Py_DECREF(missing_names);
3897 return;
3898 }
3899 PyList_SET_ITEM(missing_names, j++, name);
3900 }
3901 }
3902 assert(j == missing);
Victor Stinner438a12d2019-05-24 17:01:38 +02003903 format_missing(tstate, kind, co, missing_names);
Benjamin Petersone109c702011-06-24 09:37:26 -05003904 Py_DECREF(missing_names);
3905}
3906
3907static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003908too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3909 Py_ssize_t given, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003910 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003911{
3912 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003913 Py_ssize_t kwonly_given = 0;
3914 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003915 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003916 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003917
Benjamin Petersone109c702011-06-24 09:37:26 -05003918 assert((co->co_flags & CO_VARARGS) == 0);
3919 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01003920 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003921 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003922 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003923 }
3924 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003925 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01003926 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003927 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01003928 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003929 }
3930 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003931 plural = (co_argcount != 1);
3932 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003933 }
3934 if (sig == NULL)
3935 return;
3936 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003937 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3938 kwonly_sig = PyUnicode_FromFormat(format,
3939 given != 1 ? "s" : "",
3940 kwonly_given,
3941 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003942 if (kwonly_sig == NULL) {
3943 Py_DECREF(sig);
3944 return;
3945 }
3946 }
3947 else {
3948 /* This will not fail. */
3949 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003950 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003951 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003952 _PyErr_Format(tstate, PyExc_TypeError,
3953 "%U() takes %U positional argument%s but %zd%U %s given",
3954 co->co_name,
3955 sig,
3956 plural ? "s" : "",
3957 given,
3958 kwonly_sig,
3959 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003960 Py_DECREF(sig);
3961 Py_DECREF(kwonly_sig);
3962}
3963
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003964static int
Victor Stinner438a12d2019-05-24 17:01:38 +02003965positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
3966 Py_ssize_t kwcount, PyObject* const* kwnames)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003967{
3968 int posonly_conflicts = 0;
3969 PyObject* posonly_names = PyList_New(0);
3970
3971 for(int k=0; k < co->co_posonlyargcount; k++){
3972 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
3973
3974 for (int k2=0; k2<kwcount; k2++){
3975 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
3976 PyObject* kwname = kwnames[k2];
3977 if (kwname == posonly_name){
3978 if(PyList_Append(posonly_names, kwname) != 0) {
3979 goto fail;
3980 }
3981 posonly_conflicts++;
3982 continue;
3983 }
3984
3985 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
3986
3987 if ( cmp > 0) {
3988 if(PyList_Append(posonly_names, kwname) != 0) {
3989 goto fail;
3990 }
3991 posonly_conflicts++;
3992 } else if (cmp < 0) {
3993 goto fail;
3994 }
3995
3996 }
3997 }
3998 if (posonly_conflicts) {
3999 PyObject* comma = PyUnicode_FromString(", ");
4000 if (comma == NULL) {
4001 goto fail;
4002 }
4003 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4004 Py_DECREF(comma);
4005 if (error_names == NULL) {
4006 goto fail;
4007 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004008 _PyErr_Format(tstate, PyExc_TypeError,
4009 "%U() got some positional-only arguments passed"
4010 " as keyword arguments: '%U'",
4011 co->co_name, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004012 Py_DECREF(error_names);
4013 goto fail;
4014 }
4015
4016 Py_DECREF(posonly_names);
4017 return 0;
4018
4019fail:
4020 Py_XDECREF(posonly_names);
4021 return 1;
4022
4023}
4024
Guido van Rossumc2e20742006-02-27 22:32:47 +00004025/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02004026 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00004027 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004028
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004029PyObject *
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004030_PyEval_EvalCode(PyThreadState *tstate,
4031 PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004032 PyObject *const *args, Py_ssize_t argcount,
4033 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004034 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004035 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02004036 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004037 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00004038{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004039 assert(is_tstate_valid(tstate));
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004040
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004041 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004042 PyFrameObject *f;
4043 PyObject *retval = NULL;
4044 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004045 PyObject *x, *u;
Pablo Galindocd74e662019-06-01 18:08:04 +01004046 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004047 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02004048 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00004049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004050 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004051 _PyErr_SetString(tstate, PyExc_SystemError,
4052 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004053 return NULL;
4054 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004055
Victor Stinnerc7020012016-08-16 23:40:29 +02004056 /* Create the frame */
INADA Naoki5a625d02016-12-24 20:19:08 +09004057 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004058 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004059 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004060 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004061 fastlocals = f->f_localsplus;
4062 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004063
Victor Stinnerc7020012016-08-16 23:40:29 +02004064 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004065 if (co->co_flags & CO_VARKEYWORDS) {
4066 kwdict = PyDict_New();
4067 if (kwdict == NULL)
4068 goto fail;
4069 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004070 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004071 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004072 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004073 SETLOCAL(i, kwdict);
4074 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004075 else {
4076 kwdict = NULL;
4077 }
4078
Pablo Galindocd74e662019-06-01 18:08:04 +01004079 /* Copy all positional arguments into local variables */
4080 if (argcount > co->co_argcount) {
4081 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004082 }
4083 else {
4084 n = argcount;
4085 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004086 for (j = 0; j < n; j++) {
4087 x = args[j];
4088 Py_INCREF(x);
4089 SETLOCAL(j, x);
4090 }
4091
Victor Stinnerc7020012016-08-16 23:40:29 +02004092 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004093 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05004094 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004095 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004096 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004097 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004098 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004099 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004100
Serhiy Storchakab7281052016-09-12 00:52:40 +03004101 /* Handle keyword arguments passed as two strided arrays */
4102 kwcount *= kwstep;
4103 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004104 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004105 PyObject *keyword = kwnames[i];
4106 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004107 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004108
Benjamin Petersonb204a422011-06-05 22:04:07 -05004109 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004110 _PyErr_Format(tstate, PyExc_TypeError,
4111 "%U() keywords must be strings",
4112 co->co_name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004113 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004114 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004115
Benjamin Petersonb204a422011-06-05 22:04:07 -05004116 /* Speed hack: do raw pointer compares. As names are
4117 normally interned this should almost always hit. */
4118 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004119 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004120 PyObject *name = co_varnames[j];
4121 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004122 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004123 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004124 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004125
Benjamin Petersonb204a422011-06-05 22:04:07 -05004126 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004127 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004128 PyObject *name = co_varnames[j];
4129 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4130 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004131 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004132 }
4133 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004134 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004135 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004136 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004137
Victor Stinner231d1f32017-01-11 02:12:06 +01004138 assert(j >= total_args);
4139 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004140
Victor Stinner438a12d2019-05-24 17:01:38 +02004141 if (co->co_posonlyargcount
4142 && positional_only_passed_as_keyword(tstate, co,
4143 kwcount, kwnames))
4144 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004145 goto fail;
4146 }
4147
Victor Stinner438a12d2019-05-24 17:01:38 +02004148 _PyErr_Format(tstate, PyExc_TypeError,
4149 "%U() got an unexpected keyword argument '%S'",
4150 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004151 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004152 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004153
Christian Heimes0bd447f2013-07-20 14:48:10 +02004154 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4155 goto fail;
4156 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004157 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004158
Benjamin Petersonb204a422011-06-05 22:04:07 -05004159 kw_found:
4160 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004161 _PyErr_Format(tstate, PyExc_TypeError,
4162 "%U() got multiple values for argument '%S'",
4163 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004164 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004165 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004166 Py_INCREF(value);
4167 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004168 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004169
4170 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004171 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004172 too_many_positional(tstate, co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004173 goto fail;
4174 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004175
4176 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004177 if (argcount < co->co_argcount) {
4178 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004179 Py_ssize_t missing = 0;
4180 for (i = argcount; i < m; i++) {
4181 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004182 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004183 }
4184 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004185 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004186 missing_arguments(tstate, co, missing, defcount, fastlocals);
Benjamin Petersone109c702011-06-24 09:37:26 -05004187 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004188 }
4189 if (n > m)
4190 i = n - m;
4191 else
4192 i = 0;
4193 for (; i < defcount; i++) {
4194 if (GETLOCAL(m+i) == NULL) {
4195 PyObject *def = defs[i];
4196 Py_INCREF(def);
4197 SETLOCAL(m+i, def);
4198 }
4199 }
4200 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004201
4202 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004203 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004204 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004205 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004206 PyObject *name;
4207 if (GETLOCAL(i) != NULL)
4208 continue;
4209 name = PyTuple_GET_ITEM(co->co_varnames, i);
4210 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004211 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004212 if (def) {
4213 Py_INCREF(def);
4214 SETLOCAL(i, def);
4215 continue;
4216 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004217 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004218 goto fail;
4219 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004220 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004221 missing++;
4222 }
4223 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004224 missing_arguments(tstate, co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004225 goto fail;
4226 }
4227 }
4228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004229 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004230 vars into frame. */
4231 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004232 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004233 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004234 /* Possibly account for the cell variable being an argument. */
4235 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004236 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004237 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004238 /* Clear the local copy. */
4239 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004240 }
4241 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004242 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004243 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004244 if (c == NULL)
4245 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004246 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004247 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004248
4249 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004250 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4251 PyObject *o = PyTuple_GET_ITEM(closure, i);
4252 Py_INCREF(o);
4253 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004254 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004255
Yury Selivanoveb636452016-09-08 22:01:51 -07004256 /* Handle generator/coroutine/asynchronous generator */
4257 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004258 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004259 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004261 /* Don't need to keep the reference to f_back, it will be set
4262 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004263 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004265 /* Create a new generator that owns the ready to run frame
4266 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004267 if (is_coro) {
4268 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004269 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4270 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004271 } else {
4272 gen = PyGen_NewWithQualName(f, name, qualname);
4273 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004274 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004275 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004276 }
INADA Naoki9c157762016-12-26 18:52:46 +09004277
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004278 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004279
Yury Selivanov75445082015-05-11 22:57:16 -04004280 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004281 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004282
Victor Stinnerb9e68122019-11-14 12:20:46 +01004283 retval = _PyEval_EvalFrame(tstate, f, 0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004284
Thomas Woutersce272b62007-09-19 21:19:28 +00004285fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004287 /* decref'ing the frame can cause __del__ methods to get invoked,
4288 which can call back into Python. While we're done with the
4289 current Python frame (f), the associated C stack is still in use,
4290 so recursion_depth must be boosted for the duration.
4291 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004292 if (Py_REFCNT(f) > 1) {
4293 Py_DECREF(f);
4294 _PyObject_GC_TRACK(f);
4295 }
4296 else {
4297 ++tstate->recursion_depth;
4298 Py_DECREF(f);
4299 --tstate->recursion_depth;
4300 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004301 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004302}
4303
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004304
4305PyObject *
4306_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4307 PyObject *const *args, Py_ssize_t argcount,
4308 PyObject *const *kwnames, PyObject *const *kwargs,
4309 Py_ssize_t kwcount, int kwstep,
4310 PyObject *const *defs, Py_ssize_t defcount,
4311 PyObject *kwdefs, PyObject *closure,
4312 PyObject *name, PyObject *qualname)
4313{
4314 PyThreadState *tstate = _PyThreadState_GET();
4315 return _PyEval_EvalCode(tstate, _co, globals, locals,
4316 args, argcount,
4317 kwnames, kwargs,
4318 kwcount, kwstep,
4319 defs, defcount,
4320 kwdefs, closure,
4321 name, qualname);
4322}
4323
Victor Stinner40ee3012014-06-16 15:59:28 +02004324PyObject *
4325PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004326 PyObject *const *args, int argcount,
4327 PyObject *const *kws, int kwcount,
4328 PyObject *const *defs, int defcount,
4329 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004330{
4331 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004332 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004333 kws, kws != NULL ? kws + 1 : NULL,
4334 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004335 defs, defcount,
4336 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004337 NULL, NULL);
4338}
Tim Peters5ca576e2001-06-18 22:08:13 +00004339
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004340static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004341special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004342{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004343 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004344 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004345 if (res == NULL && !_PyErr_Occurred(tstate)) {
4346 _PyErr_SetObject(tstate, PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004347 return NULL;
4348 }
4349 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004350}
4351
4352
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004353/* Logic for the raise statement (too complicated for inlining).
4354 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004355static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004356do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004357{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004358 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004360 if (exc == NULL) {
4361 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004362 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004363 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004364 type = exc_info->exc_type;
4365 value = exc_info->exc_value;
4366 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004367 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004368 _PyErr_SetString(tstate, PyExc_RuntimeError,
4369 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004370 return 0;
4371 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004372 Py_XINCREF(type);
4373 Py_XINCREF(value);
4374 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004375 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004376 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004377 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004379 /* We support the following forms of raise:
4380 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004381 raise <instance>
4382 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004384 if (PyExceptionClass_Check(exc)) {
4385 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004386 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004387 if (value == NULL)
4388 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004389 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004390 _PyErr_Format(tstate, PyExc_TypeError,
4391 "calling %R should have returned an instance of "
4392 "BaseException, not %R",
4393 type, Py_TYPE(value));
4394 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004395 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004396 }
4397 else if (PyExceptionInstance_Check(exc)) {
4398 value = exc;
4399 type = PyExceptionInstance_Class(exc);
4400 Py_INCREF(type);
4401 }
4402 else {
4403 /* Not something you can raise. You get an exception
4404 anyway, just not what you specified :-) */
4405 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004406 _PyErr_SetString(tstate, PyExc_TypeError,
4407 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004408 goto raise_error;
4409 }
Collin Winter828f04a2007-08-31 00:04:24 +00004410
Serhiy Storchakac0191582016-09-27 11:37:10 +03004411 assert(type != NULL);
4412 assert(value != NULL);
4413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414 if (cause) {
4415 PyObject *fixed_cause;
4416 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004417 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004418 if (fixed_cause == NULL)
4419 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004420 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004421 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004422 else if (PyExceptionInstance_Check(cause)) {
4423 fixed_cause = cause;
4424 }
4425 else if (cause == Py_None) {
4426 Py_DECREF(cause);
4427 fixed_cause = NULL;
4428 }
4429 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004430 _PyErr_SetString(tstate, PyExc_TypeError,
4431 "exception causes must derive from "
4432 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004433 goto raise_error;
4434 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004435 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004436 }
Collin Winter828f04a2007-08-31 00:04:24 +00004437
Victor Stinner438a12d2019-05-24 17:01:38 +02004438 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01004439 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004440 Py_DECREF(value);
4441 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004442 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004443
4444raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004445 Py_XDECREF(value);
4446 Py_XDECREF(type);
4447 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004448 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004449}
4450
Tim Petersd6d010b2001-06-21 02:49:55 +00004451/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004452 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004453
Guido van Rossum0368b722007-05-11 16:50:42 +00004454 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4455 with a variable target.
4456*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004457
Barry Warsawe42b18f1997-08-25 22:13:04 +00004458static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004459unpack_iterable(PyThreadState *tstate, PyObject *v,
4460 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004461{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004462 int i = 0, j = 0;
4463 Py_ssize_t ll = 0;
4464 PyObject *it; /* iter(v) */
4465 PyObject *w;
4466 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004468 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004470 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004471 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004472 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01004473 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004474 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004475 _PyErr_Format(tstate, PyExc_TypeError,
4476 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01004477 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004478 }
4479 return 0;
4480 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004482 for (; i < argcnt; i++) {
4483 w = PyIter_Next(it);
4484 if (w == NULL) {
4485 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004486 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004487 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004488 _PyErr_Format(tstate, PyExc_ValueError,
4489 "not enough values to unpack "
4490 "(expected %d, got %d)",
4491 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004492 }
4493 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004494 _PyErr_Format(tstate, PyExc_ValueError,
4495 "not enough values to unpack "
4496 "(expected at least %d, got %d)",
4497 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004498 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004499 }
4500 goto Error;
4501 }
4502 *--sp = w;
4503 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004505 if (argcntafter == -1) {
4506 /* We better have exhausted the iterator now. */
4507 w = PyIter_Next(it);
4508 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004509 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004510 goto Error;
4511 Py_DECREF(it);
4512 return 1;
4513 }
4514 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004515 _PyErr_Format(tstate, PyExc_ValueError,
4516 "too many values to unpack (expected %d)",
4517 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004518 goto Error;
4519 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004521 l = PySequence_List(it);
4522 if (l == NULL)
4523 goto Error;
4524 *--sp = l;
4525 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004527 ll = PyList_GET_SIZE(l);
4528 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004529 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004530 "not enough values to unpack (expected at least %d, got %zd)",
4531 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004532 goto Error;
4533 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004535 /* Pop the "after-variable" args off the list. */
4536 for (j = argcntafter; j > 0; j--, i++) {
4537 *--sp = PyList_GET_ITEM(l, ll - j);
4538 }
4539 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01004540 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004541 Py_DECREF(it);
4542 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004543
Tim Petersd6d010b2001-06-21 02:49:55 +00004544Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004545 for (; i > 0; i--, sp++)
4546 Py_DECREF(*sp);
4547 Py_XDECREF(it);
4548 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004549}
4550
4551
Guido van Rossum96a42c81992-01-12 02:29:51 +00004552#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004553static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004554prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004555{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004556 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004557 if (PyObject_Print(v, stdout, 0) != 0) {
4558 /* Don't know what else to do */
4559 _PyErr_Clear(tstate);
4560 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004561 printf("\n");
4562 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004563}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004564#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004565
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004566static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004567call_exc_trace(Py_tracefunc func, PyObject *self,
4568 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004569{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004570 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004571 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004572 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004573 if (value == NULL) {
4574 value = Py_None;
4575 Py_INCREF(value);
4576 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004577 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004578 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004579 arg = PyTuple_Pack(3, type, value, traceback);
4580 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004581 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004582 return;
4583 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004584 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004585 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004586 if (err == 0) {
4587 _PyErr_Restore(tstate, type, value, orig_traceback);
4588 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004589 else {
4590 Py_XDECREF(type);
4591 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004592 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004593 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004594}
4595
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004596static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004597call_trace_protected(Py_tracefunc func, PyObject *obj,
4598 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004599 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004600{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004601 PyObject *type, *value, *traceback;
4602 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004603 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004604 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004605 if (err == 0)
4606 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004607 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004608 return 0;
4609 }
4610 else {
4611 Py_XDECREF(type);
4612 Py_XDECREF(value);
4613 Py_XDECREF(traceback);
4614 return -1;
4615 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004616}
4617
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004618static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004619call_trace(Py_tracefunc func, PyObject *obj,
4620 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004621 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004622{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004623 int result;
4624 if (tstate->tracing)
4625 return 0;
4626 tstate->tracing++;
4627 tstate->use_tracing = 0;
4628 result = func(obj, frame, what, arg);
4629 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4630 || (tstate->c_profilefunc != NULL));
4631 tstate->tracing--;
4632 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004633}
4634
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004635PyObject *
4636_PyEval_CallTracing(PyObject *func, PyObject *args)
4637{
Victor Stinner50b48572018-11-01 01:51:40 +01004638 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004639 int save_tracing = tstate->tracing;
4640 int save_use_tracing = tstate->use_tracing;
4641 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004643 tstate->tracing = 0;
4644 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4645 || (tstate->c_profilefunc != NULL));
4646 result = PyObject_Call(func, args, NULL);
4647 tstate->tracing = save_tracing;
4648 tstate->use_tracing = save_use_tracing;
4649 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004650}
4651
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004652/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004653static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004654maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004655 PyThreadState *tstate, PyFrameObject *frame,
4656 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004657{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004658 int result = 0;
4659 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004661 /* If the last instruction executed isn't in the current
4662 instruction window, reset the window.
4663 */
4664 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4665 PyAddrPair bounds;
4666 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4667 &bounds);
4668 *instr_lb = bounds.ap_lower;
4669 *instr_ub = bounds.ap_upper;
4670 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004671 /* If the last instruction falls at the start of a line or if it
4672 represents a jump backwards, update the frame's line number and
4673 then call the trace function if we're tracing source lines.
4674 */
4675 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004676 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004677 if (frame->f_trace_lines) {
4678 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4679 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004680 }
George King20faa682017-10-18 17:44:22 -07004681 /* Always emit an opcode event if we're tracing all opcodes. */
4682 if (frame->f_trace_opcodes) {
4683 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4684 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004685 *instr_prev = frame->f_lasti;
4686 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004687}
4688
Victor Stinner309d7cc2020-03-13 16:39:12 +01004689int
4690_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4691{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004692 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01004693 /* The caller must hold the GIL */
4694 assert(PyGILState_Check());
4695
4696 /* Call PySys_Audit() in the context of the current thread state,
4697 even if tstate is not the current thread state. */
4698 if (PySys_Audit("sys.setprofile", NULL) < 0) {
4699 return -1;
4700 }
4701
4702 PyObject *profileobj = tstate->c_profileobj;
4703
4704 tstate->c_profilefunc = NULL;
4705 tstate->c_profileobj = NULL;
4706 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
4707 tstate->use_tracing = tstate->c_tracefunc != NULL;
4708 Py_XDECREF(profileobj);
4709
4710 Py_XINCREF(arg);
4711 tstate->c_profileobj = arg;
4712 tstate->c_profilefunc = func;
4713
4714 /* Flag that tracing or profiling is turned on */
4715 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
4716 return 0;
4717}
4718
Fred Drake5755ce62001-06-27 19:19:46 +00004719void
4720PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004721{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004722 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004723 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
4724 /* Log PySys_Audit() error */
4725 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
4726 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01004727}
4728
4729int
4730_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4731{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004732 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01004733 /* The caller must hold the GIL */
4734 assert(PyGILState_Check());
4735
4736 /* Call PySys_Audit() in the context of the current thread state,
4737 even if tstate is not the current thread state. */
4738 if (PySys_Audit("sys.settrace", NULL) < 0) {
4739 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004740 }
4741
Victor Stinnerda2914d2020-03-20 09:29:08 +01004742 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner309d7cc2020-03-13 16:39:12 +01004743 PyObject *traceobj = tstate->c_traceobj;
Victor Stinnerda2914d2020-03-20 09:29:08 +01004744 ceval2->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004745
4746 tstate->c_tracefunc = NULL;
4747 tstate->c_traceobj = NULL;
4748 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
4749 tstate->use_tracing = (tstate->c_profilefunc != NULL);
4750 Py_XDECREF(traceobj);
4751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004752 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004753 tstate->c_traceobj = arg;
4754 tstate->c_tracefunc = func;
4755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004756 /* Flag that tracing or profiling is turned on */
Victor Stinner309d7cc2020-03-13 16:39:12 +01004757 tstate->use_tracing = ((func != NULL)
4758 || (tstate->c_profilefunc != NULL));
4759
4760 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00004761}
4762
4763void
4764PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4765{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004766 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004767 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
4768 /* Log PySys_Audit() error */
4769 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
4770 }
Fred Draked0838392001-06-16 21:02:31 +00004771}
4772
Victor Stinner309d7cc2020-03-13 16:39:12 +01004773
Yury Selivanov75445082015-05-11 22:57:16 -04004774void
Victor Stinner838f2642019-06-13 22:41:23 +02004775_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004776{
4777 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004778 tstate->coroutine_origin_tracking_depth = new_depth;
4779}
4780
4781int
4782_PyEval_GetCoroutineOriginTrackingDepth(void)
4783{
Victor Stinner50b48572018-11-01 01:51:40 +01004784 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004785 return tstate->coroutine_origin_tracking_depth;
4786}
4787
4788void
Yury Selivanoveb636452016-09-08 22:01:51 -07004789_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4790{
Victor Stinner50b48572018-11-01 01:51:40 +01004791 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004792
4793 if (PySys_Audit("sys.set_asyncgen_hook_firstiter", NULL) < 0) {
4794 return;
4795 }
4796
Yury Selivanoveb636452016-09-08 22:01:51 -07004797 Py_XINCREF(firstiter);
4798 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4799}
4800
4801PyObject *
4802_PyEval_GetAsyncGenFirstiter(void)
4803{
Victor Stinner50b48572018-11-01 01:51:40 +01004804 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004805 return tstate->async_gen_firstiter;
4806}
4807
4808void
4809_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4810{
Victor Stinner50b48572018-11-01 01:51:40 +01004811 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004812
4813 if (PySys_Audit("sys.set_asyncgen_hook_finalizer", NULL) < 0) {
4814 return;
4815 }
4816
Yury Selivanoveb636452016-09-08 22:01:51 -07004817 Py_XINCREF(finalizer);
4818 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4819}
4820
4821PyObject *
4822_PyEval_GetAsyncGenFinalizer(void)
4823{
Victor Stinner50b48572018-11-01 01:51:40 +01004824 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004825 return tstate->async_gen_finalizer;
4826}
4827
Victor Stinner438a12d2019-05-24 17:01:38 +02004828PyFrameObject *
4829PyEval_GetFrame(void)
4830{
4831 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004832 return tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02004833}
4834
Guido van Rossumb209a111997-04-29 18:18:01 +00004835PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004836PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004837{
Victor Stinner438a12d2019-05-24 17:01:38 +02004838 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004839 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004840 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004841 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004842 else
4843 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004844}
4845
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004846/* Convenience function to get a builtin from its name */
4847PyObject *
4848_PyEval_GetBuiltinId(_Py_Identifier *name)
4849{
Victor Stinner438a12d2019-05-24 17:01:38 +02004850 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004851 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4852 if (attr) {
4853 Py_INCREF(attr);
4854 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004855 else if (!_PyErr_Occurred(tstate)) {
4856 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004857 }
4858 return attr;
4859}
4860
Guido van Rossumb209a111997-04-29 18:18:01 +00004861PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004862PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004863{
Victor Stinner438a12d2019-05-24 17:01:38 +02004864 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004865 PyFrameObject *current_frame = tstate->frame;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004866 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004867 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004868 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004869 }
4870
Victor Stinner438a12d2019-05-24 17:01:38 +02004871 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004872 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004873 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004874
4875 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004876 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004877}
4878
Guido van Rossumb209a111997-04-29 18:18:01 +00004879PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004880PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004881{
Victor Stinner438a12d2019-05-24 17:01:38 +02004882 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004883 PyFrameObject *current_frame = tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02004884 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004885 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004886 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004887
4888 assert(current_frame->f_globals != NULL);
4889 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004890}
4891
Guido van Rossum6135a871995-01-09 17:53:26 +00004892int
Tim Peters5ba58662001-07-16 02:29:45 +00004893PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004894{
Victor Stinner438a12d2019-05-24 17:01:38 +02004895 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004896 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004897 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004899 if (current_frame != NULL) {
4900 const int codeflags = current_frame->f_code->co_flags;
4901 const int compilerflags = codeflags & PyCF_MASK;
4902 if (compilerflags) {
4903 result = 1;
4904 cf->cf_flags |= compilerflags;
4905 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004906#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004907 if (codeflags & CO_GENERATOR_ALLOWED) {
4908 result = 1;
4909 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4910 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004911#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004912 }
4913 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004914}
4915
Guido van Rossum3f5da241990-12-20 15:06:42 +00004916
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004917const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004918PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004919{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004920 if (PyMethod_Check(func))
4921 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4922 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004923 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004924 else if (PyCFunction_Check(func))
4925 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4926 else
Victor Stinnera102ed72020-02-07 02:24:48 +01004927 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004928}
4929
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004930const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004931PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004932{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004933 if (PyMethod_Check(func))
4934 return "()";
4935 else if (PyFunction_Check(func))
4936 return "()";
4937 else if (PyCFunction_Check(func))
4938 return "()";
4939 else
4940 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004941}
4942
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004943#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004944if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004945 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4946 tstate, tstate->frame, \
4947 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004948 x = NULL; \
4949 } \
4950 else { \
4951 x = call; \
4952 if (tstate->c_profilefunc != NULL) { \
4953 if (x == NULL) { \
4954 call_trace_protected(tstate->c_profilefunc, \
4955 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004956 tstate, tstate->frame, \
4957 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004958 /* XXX should pass (type, value, tb) */ \
4959 } else { \
4960 if (call_trace(tstate->c_profilefunc, \
4961 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004962 tstate, tstate->frame, \
4963 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004964 Py_DECREF(x); \
4965 x = NULL; \
4966 } \
4967 } \
4968 } \
4969 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004970} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004971 x = call; \
4972 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004973
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004974
4975static PyObject *
4976trace_call_function(PyThreadState *tstate,
4977 PyObject *func,
4978 PyObject **args, Py_ssize_t nargs,
4979 PyObject *kwnames)
4980{
4981 PyObject *x;
4982 if (PyCFunction_Check(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01004983 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004984 return x;
4985 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06004986 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004987 /* We need to create a temporary bound method as argument
4988 for profiling.
4989
4990 If nargs == 0, then this cannot work because we have no
4991 "self". In any case, the call itself would raise
4992 TypeError (foo needs an argument), so we just skip
4993 profiling. */
4994 PyObject *self = args[0];
4995 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4996 if (func == NULL) {
4997 return NULL;
4998 }
Petr Viktorinffd97532020-02-11 17:46:57 +01004999 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02005000 args+1, nargs-1,
5001 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005002 Py_DECREF(func);
5003 return x;
5004 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005005 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005006}
5007
Victor Stinner415c5102017-01-11 00:54:57 +01005008/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
5009 to reduce the stack consumption. */
5010Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02005011call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005012{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005013 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005014 PyObject *func = *pfunc;
5015 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005016 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5017 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005018 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005019
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005020 if (tstate->use_tracing) {
5021 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005022 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005023 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01005024 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005025 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005026
Victor Stinner438a12d2019-05-24 17:01:38 +02005027 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005028
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005029 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005030 while ((*pp_stack) > pfunc) {
5031 w = EXT_POP(*pp_stack);
5032 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005033 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005035 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005036}
5037
Jeremy Hylton52820442001-01-03 23:52:36 +00005038static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02005039do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005040{
jdemeyere89de732018-09-19 12:06:20 +02005041 PyObject *result;
5042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005043 if (PyCFunction_Check(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005044 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005045 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005046 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005047 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02005048 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
5049 if (nargs > 0 && tstate->use_tracing) {
5050 /* We need to create a temporary bound method as argument
5051 for profiling.
5052
5053 If nargs == 0, then this cannot work because we have no
5054 "self". In any case, the call itself would raise
5055 TypeError (foo needs an argument), so we just skip
5056 profiling. */
5057 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5058 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5059 if (func == NULL) {
5060 return NULL;
5061 }
5062
Victor Stinner4d231bc2019-11-14 13:36:21 +01005063 C_TRACE(result, _PyObject_FastCallDictTstate(
5064 tstate, func,
5065 &_PyTuple_ITEMS(callargs)[1],
5066 nargs - 1,
5067 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005068 Py_DECREF(func);
5069 return result;
5070 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005071 }
jdemeyere89de732018-09-19 12:06:20 +02005072 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005073}
5074
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005075/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005076 nb_index slot defined, and store in *pi.
5077 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005078 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005079 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005080*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005081int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005082_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005083{
Victor Stinner438a12d2019-05-24 17:01:38 +02005084 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005085 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005086 Py_ssize_t x;
5087 if (PyIndex_Check(v)) {
5088 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005089 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005090 return 0;
5091 }
5092 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005093 _PyErr_SetString(tstate, PyExc_TypeError,
5094 "slice indices must be integers or "
5095 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005096 return 0;
5097 }
5098 *pi = x;
5099 }
5100 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005101}
5102
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005103int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005104_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005105{
Victor Stinner438a12d2019-05-24 17:01:38 +02005106 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005107 Py_ssize_t x;
5108 if (PyIndex_Check(v)) {
5109 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005110 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005111 return 0;
5112 }
5113 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005114 _PyErr_SetString(tstate, PyExc_TypeError,
5115 "slice indices must be integers or "
5116 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005117 return 0;
5118 }
5119 *pi = x;
5120 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005121}
5122
Thomas Wouters52152252000-08-17 22:55:00 +00005123static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005124import_name(PyThreadState *tstate, PyFrameObject *f,
5125 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005126{
5127 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005128 PyObject *import_func, *res;
5129 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005130
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005131 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005132 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005133 if (!_PyErr_Occurred(tstate)) {
5134 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005135 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005136 return NULL;
5137 }
5138
5139 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005140 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005141 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005142 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005143 return NULL;
5144 }
5145 res = PyImport_ImportModuleLevelObject(
5146 name,
5147 f->f_globals,
5148 f->f_locals == NULL ? Py_None : f->f_locals,
5149 fromlist,
5150 ilevel);
5151 return res;
5152 }
5153
5154 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005155
5156 stack[0] = name;
5157 stack[1] = f->f_globals;
5158 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5159 stack[3] = fromlist;
5160 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005161 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005162 Py_DECREF(import_func);
5163 return res;
5164}
5165
5166static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005167import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005168{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005169 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08005170 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005171
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005172 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005173 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005174 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005175 /* Issue #17636: in case this failed because of a circular relative
5176 import, try to fallback on reading the module directly from
5177 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005178 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005179 if (pkgname == NULL) {
5180 goto error;
5181 }
Oren Milman6db70332017-09-19 14:23:01 +03005182 if (!PyUnicode_Check(pkgname)) {
5183 Py_CLEAR(pkgname);
5184 goto error;
5185 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005186 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005187 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005188 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005189 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005190 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005191 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005192 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005193 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005194 goto error;
5195 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005196 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005197 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005198 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005199 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005200 if (pkgname == NULL) {
5201 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5202 if (pkgname_or_unknown == NULL) {
5203 Py_XDECREF(pkgpath);
5204 return NULL;
5205 }
5206 } else {
5207 pkgname_or_unknown = pkgname;
5208 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005209
5210 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005211 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005212 errmsg = PyUnicode_FromFormat(
5213 "cannot import name %R from %R (unknown location)",
5214 name, pkgname_or_unknown
5215 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005216 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005217 PyErr_SetImportError(errmsg, pkgname, NULL);
5218 }
5219 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005220 _Py_IDENTIFIER(__spec__);
5221 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005222 const char *fmt =
5223 _PyModuleSpec_IsInitializing(spec) ?
5224 "cannot import name %R from partially initialized module %R "
5225 "(most likely due to a circular import) (%S)" :
5226 "cannot import name %R from %R (%S)";
5227 Py_XDECREF(spec);
5228
5229 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005230 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005231 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005232 }
5233
Xiang Zhang4830f582017-03-21 11:13:42 +08005234 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005235 Py_XDECREF(pkgname_or_unknown);
5236 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005237 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005238}
Guido van Rossumac7be682001-01-17 15:42:30 +00005239
Thomas Wouters52152252000-08-17 22:55:00 +00005240static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005241import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005242{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005243 _Py_IDENTIFIER(__all__);
5244 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005245 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005246 int skip_leading_underscores = 0;
5247 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005248
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005249 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5250 return -1; /* Unexpected error */
5251 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005252 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005253 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5254 return -1;
5255 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005256 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005257 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005258 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005259 return -1;
5260 }
5261 all = PyMapping_Keys(dict);
5262 Py_DECREF(dict);
5263 if (all == NULL)
5264 return -1;
5265 skip_leading_underscores = 1;
5266 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005268 for (pos = 0, err = 0; ; pos++) {
5269 name = PySequence_GetItem(all, pos);
5270 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005271 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005272 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005273 }
5274 else {
5275 _PyErr_Clear(tstate);
5276 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005277 break;
5278 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005279 if (!PyUnicode_Check(name)) {
5280 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5281 if (modname == NULL) {
5282 Py_DECREF(name);
5283 err = -1;
5284 break;
5285 }
5286 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005287 _PyErr_Format(tstate, PyExc_TypeError,
5288 "module __name__ must be a string, not %.100s",
5289 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005290 }
5291 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005292 _PyErr_Format(tstate, PyExc_TypeError,
5293 "%s in %U.%s must be str, not %.100s",
5294 skip_leading_underscores ? "Key" : "Item",
5295 modname,
5296 skip_leading_underscores ? "__dict__" : "__all__",
5297 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005298 }
5299 Py_DECREF(modname);
5300 Py_DECREF(name);
5301 err = -1;
5302 break;
5303 }
5304 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005305 if (PyUnicode_READY(name) == -1) {
5306 Py_DECREF(name);
5307 err = -1;
5308 break;
5309 }
5310 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5311 Py_DECREF(name);
5312 continue;
5313 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005314 }
5315 value = PyObject_GetAttr(v, name);
5316 if (value == NULL)
5317 err = -1;
5318 else if (PyDict_CheckExact(locals))
5319 err = PyDict_SetItem(locals, name, value);
5320 else
5321 err = PyObject_SetItem(locals, name, value);
5322 Py_DECREF(name);
5323 Py_XDECREF(value);
5324 if (err != 0)
5325 break;
5326 }
5327 Py_DECREF(all);
5328 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005329}
5330
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005331static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005332check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005333{
Victor Stinnera102ed72020-02-07 02:24:48 +01005334 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005335 /* check_args_iterable() may be called with a live exception:
5336 * clear it to prevent calling _PyObject_FunctionStr() with an
5337 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01005338 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005339 PyObject *funcstr = _PyObject_FunctionStr(func);
5340 if (funcstr != NULL) {
5341 _PyErr_Format(tstate, PyExc_TypeError,
5342 "%U argument after * must be an iterable, not %.200s",
5343 funcstr, Py_TYPE(args)->tp_name);
5344 Py_DECREF(funcstr);
5345 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005346 return -1;
5347 }
5348 return 0;
5349}
5350
5351static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005352format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005353{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005354 /* _PyDict_MergeEx raises attribute
5355 * error (percolated from an attempt
5356 * to get 'keys' attribute) instead of
5357 * a type error if its second argument
5358 * is not a mapping.
5359 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005360 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005361 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005362 PyObject *funcstr = _PyObject_FunctionStr(func);
5363 if (funcstr != NULL) {
5364 _PyErr_Format(
5365 tstate, PyExc_TypeError,
5366 "%U argument after ** must be a mapping, not %.200s",
5367 funcstr, Py_TYPE(kwargs)->tp_name);
5368 Py_DECREF(funcstr);
5369 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005370 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005371 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005372 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005373 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005374 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005375 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005376 PyObject *funcstr = _PyObject_FunctionStr(func);
5377 if (funcstr != NULL) {
5378 PyObject *key = PyTuple_GET_ITEM(val, 0);
5379 _PyErr_Format(
5380 tstate, PyExc_TypeError,
5381 "%U got multiple values for keyword argument '%S'",
5382 funcstr, key);
5383 Py_DECREF(funcstr);
5384 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005385 Py_XDECREF(exc);
5386 Py_XDECREF(val);
5387 Py_XDECREF(tb);
5388 }
5389 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005390 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005391 }
5392 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005393}
5394
Guido van Rossumac7be682001-01-17 15:42:30 +00005395static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005396format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5397 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005398{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005399 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005401 if (!obj)
5402 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005403
Serhiy Storchaka06515832016-11-20 09:13:07 +02005404 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005405 if (!obj_str)
5406 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005407
Victor Stinner438a12d2019-05-24 17:01:38 +02005408 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005409}
Guido van Rossum950361c1997-01-24 13:49:28 +00005410
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005411static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005412format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005413{
5414 PyObject *name;
5415 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005416 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005417 return;
5418 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5419 name = PyTuple_GET_ITEM(co->co_cellvars,
5420 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005421 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005422 PyExc_UnboundLocalError,
5423 UNBOUNDLOCAL_ERROR_MSG,
5424 name);
5425 } else {
5426 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5427 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005428 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005429 UNBOUNDFREE_ERROR_MSG, name);
5430 }
5431}
5432
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005433static void
Mark Shannonfee55262019-11-21 09:11:43 +00005434format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005435{
5436 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5437 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005438 _PyErr_Format(tstate, PyExc_TypeError,
5439 "'async with' received an object from __aenter__ "
5440 "that does not implement __await__: %.100s",
5441 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005442 }
Mark Shannonfee55262019-11-21 09:11:43 +00005443 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005444 _PyErr_Format(tstate, PyExc_TypeError,
5445 "'async with' received an object from __aexit__ "
5446 "that does not implement __await__: %.100s",
5447 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005448 }
5449 }
5450}
5451
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005452static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005453unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005454 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005455{
5456 PyObject *res;
5457 if (Py_REFCNT(v) == 2) {
5458 /* In the common case, there are 2 references to the value
5459 * stored in 'variable' when the += is performed: one on the
5460 * value stack (in 'v') and one still stored in the
5461 * 'variable'. We try to delete the variable now to reduce
5462 * the refcnt to 1.
5463 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005464 int opcode, oparg;
5465 NEXTOPARG();
5466 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005467 case STORE_FAST:
5468 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005469 PyObject **fastlocals = f->f_localsplus;
5470 if (GETLOCAL(oparg) == v)
5471 SETLOCAL(oparg, NULL);
5472 break;
5473 }
5474 case STORE_DEREF:
5475 {
5476 PyObject **freevars = (f->f_localsplus +
5477 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005478 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005479 if (PyCell_GET(c) == v) {
5480 PyCell_SET(c, NULL);
5481 Py_DECREF(v);
5482 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005483 break;
5484 }
5485 case STORE_NAME:
5486 {
5487 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005488 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005489 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005490 if (locals && PyDict_CheckExact(locals)) {
5491 PyObject *w = PyDict_GetItemWithError(locals, name);
5492 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005493 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005494 {
5495 Py_DECREF(v);
5496 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005497 }
5498 }
5499 break;
5500 }
5501 }
5502 }
5503 res = v;
5504 PyUnicode_Append(&res, w);
5505 return res;
5506}
5507
Guido van Rossum950361c1997-01-24 13:49:28 +00005508#ifdef DYNAMIC_EXECUTION_PROFILE
5509
Skip Montanarof118cb12001-10-15 20:51:38 +00005510static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005511getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005512{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005513 int i;
5514 PyObject *l = PyList_New(256);
5515 if (l == NULL) return NULL;
5516 for (i = 0; i < 256; i++) {
5517 PyObject *x = PyLong_FromLong(a[i]);
5518 if (x == NULL) {
5519 Py_DECREF(l);
5520 return NULL;
5521 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005522 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005523 }
5524 for (i = 0; i < 256; i++)
5525 a[i] = 0;
5526 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005527}
5528
5529PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005530_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005531{
5532#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005533 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005534#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005535 int i;
5536 PyObject *l = PyList_New(257);
5537 if (l == NULL) return NULL;
5538 for (i = 0; i < 257; i++) {
5539 PyObject *x = getarray(dxpairs[i]);
5540 if (x == NULL) {
5541 Py_DECREF(l);
5542 return NULL;
5543 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005544 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005545 }
5546 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005547#endif
5548}
5549
5550#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005551
5552Py_ssize_t
5553_PyEval_RequestCodeExtraIndex(freefunc free)
5554{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005555 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005556 Py_ssize_t new_index;
5557
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005558 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005559 return -1;
5560 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005561 new_index = interp->co_extra_user_count++;
5562 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005563 return new_index;
5564}
Łukasz Langaa785c872016-09-09 17:37:37 -07005565
5566static void
5567dtrace_function_entry(PyFrameObject *f)
5568{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005569 const char *filename;
5570 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005571 int lineno;
5572
5573 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5574 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5575 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5576
Andy Lestere6be9b52020-02-11 20:28:35 -06005577 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005578}
5579
5580static void
5581dtrace_function_return(PyFrameObject *f)
5582{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005583 const char *filename;
5584 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005585 int lineno;
5586
5587 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5588 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5589 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5590
Andy Lestere6be9b52020-02-11 20:28:35 -06005591 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005592}
5593
5594/* DTrace equivalent of maybe_call_line_trace. */
5595static void
5596maybe_dtrace_line(PyFrameObject *frame,
5597 int *instr_lb, int *instr_ub, int *instr_prev)
5598{
5599 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005600 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005601
5602 /* If the last instruction executed isn't in the current
5603 instruction window, reset the window.
5604 */
5605 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5606 PyAddrPair bounds;
5607 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5608 &bounds);
5609 *instr_lb = bounds.ap_lower;
5610 *instr_ub = bounds.ap_upper;
5611 }
5612 /* If the last instruction falls at the start of a line or if
5613 it represents a jump backwards, update the frame's line
5614 number and call the trace function. */
5615 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5616 frame->f_lineno = line;
5617 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5618 if (!co_filename)
5619 co_filename = "?";
5620 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5621 if (!co_name)
5622 co_name = "?";
Andy Lestere6be9b52020-02-11 20:28:35 -06005623 PyDTrace_LINE(co_filename, co_name, line);
Łukasz Langaa785c872016-09-09 17:37:37 -07005624 }
5625 *instr_prev = frame->f_lasti;
5626}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005627
5628
5629/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
5630 for the limited API. */
5631
5632#undef Py_EnterRecursiveCall
5633
5634int Py_EnterRecursiveCall(const char *where)
5635{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005636 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005637}
5638
5639#undef Py_LeaveRecursiveCall
5640
5641void Py_LeaveRecursiveCall(void)
5642{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005643 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005644}