1 
2 /* Execute compiled code */
3 
4 /* XXX TO DO:
5    XXX speed up searching for keywords by using a dictionary
6    XXX document it!
7    */
8 
9 /* enable more aggressive intra-module optimizations, where available */
10 #define PY_LOCAL_AGGRESSIVE
11 
12 #include "Python.h"
13 #include "pycore_ceval.h"
14 #include "pycore_code.h"
15 #include "pycore_object.h"
16 #include "pycore_pyerrors.h"
17 #include "pycore_pylifecycle.h"
18 #include "pycore_pystate.h"
19 #include "pycore_tupleobject.h"
20 
21 #include "code.h"
22 #include "dictobject.h"
23 #include "frameobject.h"
24 #include "opcode.h"
25 #include "pydtrace.h"
26 #include "setobject.h"
27 #include "structmember.h"
28 
29 #include <ctype.h>
30 
31 #ifdef Py_DEBUG
32 /* For debugging the interpreter: */
33 #define LLTRACE  1      /* Low-level trace feature */
34 #define CHECKEXC 1      /* Double-check exception checking */
35 #endif
36 
37 #if !defined(Py_BUILD_CORE)
38 #  error "ceval.c must be build with Py_BUILD_CORE define for best performance"
39 #endif
40 
41 /* Private API for the LOAD_METHOD opcode. */
42 extern int _PyObject_GetMethod(PyObject *, PyObject *, PyObject **);
43 
44 typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
45 
46 /* Forward declarations */
47 Py_LOCAL_INLINE(PyObject *) call_function(
48     PyThreadState *tstate, PyObject ***pp_stack,
49     Py_ssize_t oparg, PyObject *kwnames);
50 static PyObject * do_call_core(
51     PyThreadState *tstate, PyObject *func,
52     PyObject *callargs, PyObject *kwdict);
53 
54 #ifdef LLTRACE
55 static int lltrace;
56 static int prtrace(PyThreadState *, PyObject *, const char *);
57 #endif
58 static int call_trace(Py_tracefunc, PyObject *,
59                       PyThreadState *, PyFrameObject *,
60                       int, PyObject *);
61 static int call_trace_protected(Py_tracefunc, PyObject *,
62                                 PyThreadState *, PyFrameObject *,
63                                 int, PyObject *);
64 static void call_exc_trace(Py_tracefunc, PyObject *,
65                            PyThreadState *, PyFrameObject *);
66 static int maybe_call_line_trace(Py_tracefunc, PyObject *,
67                                  PyThreadState *, PyFrameObject *,
68                                  int *, int *, int *);
69 static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
70 static void dtrace_function_entry(PyFrameObject *);
71 static void dtrace_function_return(PyFrameObject *);
72 
73 static PyObject * cmp_outcome(PyThreadState *, int, PyObject *, PyObject *);
74 static PyObject * import_name(PyThreadState *, PyFrameObject *,
75                               PyObject *, PyObject *, PyObject *);
76 static PyObject * import_from(PyThreadState *, PyObject *, PyObject *);
77 static int import_all_from(PyThreadState *, PyObject *, PyObject *);
78 static void format_exc_check_arg(PyThreadState *, PyObject *, const char *, PyObject *);
79 static void format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
80 static PyObject * unicode_concatenate(PyThreadState *, PyObject *, PyObject *,
81                                       PyFrameObject *, const _Py_CODEUNIT *);
82 static PyObject * special_lookup(PyThreadState *, PyObject *, _Py_Identifier *);
83 static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg);
84 static void format_kwargs_error(PyThreadState *, PyObject *func, PyObject *kwargs);
85 static void format_awaitable_error(PyThreadState *, PyTypeObject *, int);
86 
87 #define NAME_ERROR_MSG \
88     "name '%.200s' is not defined"
89 #define UNBOUNDLOCAL_ERROR_MSG \
90     "local variable '%.200s' referenced before assignment"
91 #define UNBOUNDFREE_ERROR_MSG \
92     "free variable '%.200s' referenced before assignment" \
93     " in enclosing scope"
94 
95 /* Dynamic execution profile */
96 #ifdef DYNAMIC_EXECUTION_PROFILE
97 #ifdef DXPAIRS
98 static long dxpairs[257][256];
99 #define dxp dxpairs[256]
100 #else
101 static long dxp[256];
102 #endif
103 #endif
104 
105 /* per opcode cache */
106 #ifdef Py_DEBUG
107 // --with-pydebug is used to find memory leak.  opcache makes it harder.
108 // So we disable opcache when Py_DEBUG is defined.
109 // See bpo-37146
110 #define OPCACHE_MIN_RUNS 0  /* disable opcache */
111 #else
112 #define OPCACHE_MIN_RUNS 1024  /* create opcache when code executed this time */
113 #endif
114 #define OPCACHE_STATS 0  /* Enable stats */
115 
116 #if OPCACHE_STATS
117 static size_t opcache_code_objects = 0;
118 static size_t opcache_code_objects_extra_mem = 0;
119 
120 static size_t opcache_global_opts = 0;
121 static size_t opcache_global_hits = 0;
122 static size_t opcache_global_misses = 0;
123 #endif
124 
125 #define GIL_REQUEST _Py_atomic_load_relaxed(&ceval->gil_drop_request)
126 
127 /* This can set eval_breaker to 0 even though gil_drop_request became
128    1.  We believe this is all right because the eval loop will release
129    the GIL eventually anyway. */
130 #define COMPUTE_EVAL_BREAKER(ceval) \
131     _Py_atomic_store_relaxed( \
132         &(ceval)->eval_breaker, \
133         GIL_REQUEST | \
134         _Py_atomic_load_relaxed(&(ceval)->signals_pending) | \
135         _Py_atomic_load_relaxed(&(ceval)->pending.calls_to_do) | \
136         (ceval)->pending.async_exc)
137 
138 #define SET_GIL_DROP_REQUEST(ceval) \
139     do { \
140         _Py_atomic_store_relaxed(&(ceval)->gil_drop_request, 1); \
141         _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
142     } while (0)
143 
144 #define RESET_GIL_DROP_REQUEST(ceval) \
145     do { \
146         _Py_atomic_store_relaxed(&(ceval)->gil_drop_request, 0); \
147         COMPUTE_EVAL_BREAKER(ceval); \
148     } while (0)
149 
150 /* Pending calls are only modified under pending_lock */
151 #define SIGNAL_PENDING_CALLS(ceval) \
152     do { \
153         _Py_atomic_store_relaxed(&(ceval)->pending.calls_to_do, 1); \
154         _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
155     } while (0)
156 
157 #define UNSIGNAL_PENDING_CALLS(ceval) \
158     do { \
159         _Py_atomic_store_relaxed(&(ceval)->pending.calls_to_do, 0); \
160         COMPUTE_EVAL_BREAKER(ceval); \
161     } while (0)
162 
163 #define SIGNAL_PENDING_SIGNALS(ceval) \
164     do { \
165         _Py_atomic_store_relaxed(&(ceval)->signals_pending, 1); \
166         _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
167     } while (0)
168 
169 #define UNSIGNAL_PENDING_SIGNALS(ceval) \
170     do { \
171         _Py_atomic_store_relaxed(&(ceval)->signals_pending, 0); \
172         COMPUTE_EVAL_BREAKER(ceval); \
173     } while (0)
174 
175 #define SIGNAL_ASYNC_EXC(ceval) \
176     do { \
177         (ceval)->pending.async_exc = 1; \
178         _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
179     } while (0)
180 
181 #define UNSIGNAL_ASYNC_EXC(ceval) \
182     do { \
183         (ceval)->pending.async_exc = 0; \
184         COMPUTE_EVAL_BREAKER(ceval); \
185     } while (0)
186 
187 
188 #ifdef HAVE_ERRNO_H
189 #include <errno.h>
190 #endif
191 #include "pythread.h"
192 #include "ceval_gil.h"
193 
194 int
PyEval_ThreadsInitialized(void)195 PyEval_ThreadsInitialized(void)
196 {
197     return gil_created(&_PyRuntime.ceval.gil);
198 }
199 
200 void
PyEval_InitThreads(void)201 PyEval_InitThreads(void)
202 {
203     _PyRuntimeState *runtime = &_PyRuntime;
204     struct _ceval_runtime_state *ceval = &runtime->ceval;
205     struct _gil_runtime_state *gil = &ceval->gil;
206     if (gil_created(gil)) {
207         return;
208     }
209 
210     PyThread_init_thread();
211     create_gil(gil);
212     PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
213     take_gil(ceval, tstate);
214 
215     struct _pending_calls *pending = &ceval->pending;
216     pending->lock = PyThread_allocate_lock();
217     if (pending->lock == NULL) {
218         Py_FatalError("Can't initialize threads for pending calls");
219     }
220 }
221 
222 void
_PyEval_FiniThreads(struct _ceval_runtime_state * ceval)223 _PyEval_FiniThreads(struct _ceval_runtime_state *ceval)
224 {
225     struct _gil_runtime_state *gil = &ceval->gil;
226     if (!gil_created(gil)) {
227         return;
228     }
229 
230     destroy_gil(gil);
231     assert(!gil_created(gil));
232 
233     struct _pending_calls *pending = &ceval->pending;
234     if (pending->lock != NULL) {
235         PyThread_free_lock(pending->lock);
236         pending->lock = NULL;
237     }
238 }
239 
240 static inline void
exit_thread_if_finalizing(_PyRuntimeState * runtime,PyThreadState * tstate)241 exit_thread_if_finalizing(_PyRuntimeState *runtime, PyThreadState *tstate)
242 {
243     /* _Py_Finalizing is protected by the GIL */
244     if (runtime->finalizing != NULL && !_Py_CURRENTLY_FINALIZING(runtime, tstate)) {
245         drop_gil(&runtime->ceval, tstate);
246         PyThread_exit_thread();
247     }
248 }
249 
250 void
_PyEval_Fini(void)251 _PyEval_Fini(void)
252 {
253 #if OPCACHE_STATS
254     fprintf(stderr, "-- Opcode cache number of objects  = %zd\n",
255             opcache_code_objects);
256 
257     fprintf(stderr, "-- Opcode cache total extra mem    = %zd\n",
258             opcache_code_objects_extra_mem);
259 
260     fprintf(stderr, "\n");
261 
262     fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits   = %zd (%d%%)\n",
263             opcache_global_hits,
264             (int) (100.0 * opcache_global_hits /
265                 (opcache_global_hits + opcache_global_misses)));
266 
267     fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n",
268             opcache_global_misses,
269             (int) (100.0 * opcache_global_misses /
270                 (opcache_global_hits + opcache_global_misses)));
271 
272     fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts   = %zd\n",
273             opcache_global_opts);
274 
275     fprintf(stderr, "\n");
276 #endif
277 }
278 
279 void
PyEval_AcquireLock(void)280 PyEval_AcquireLock(void)
281 {
282     _PyRuntimeState *runtime = &_PyRuntime;
283     struct _ceval_runtime_state *ceval = &runtime->ceval;
284     PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
285     if (tstate == NULL) {
286         Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
287     }
288     take_gil(ceval, tstate);
289     exit_thread_if_finalizing(runtime, tstate);
290 }
291 
292 void
PyEval_ReleaseLock(void)293 PyEval_ReleaseLock(void)
294 {
295     _PyRuntimeState *runtime = &_PyRuntime;
296     PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
297     /* This function must succeed when the current thread state is NULL.
298        We therefore avoid PyThreadState_Get() which dumps a fatal error
299        in debug mode.
300     */
301     drop_gil(&runtime->ceval, tstate);
302 }
303 
304 void
PyEval_AcquireThread(PyThreadState * tstate)305 PyEval_AcquireThread(PyThreadState *tstate)
306 {
307     if (tstate == NULL) {
308         Py_FatalError("PyEval_AcquireThread: NULL new thread state");
309     }
310 
311     _PyRuntimeState *runtime = &_PyRuntime;
312     struct _ceval_runtime_state *ceval = &runtime->ceval;
313 
314     /* Check someone has called PyEval_InitThreads() to create the lock */
315     assert(gil_created(&ceval->gil));
316     take_gil(ceval, tstate);
317     exit_thread_if_finalizing(runtime, tstate);
318     if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
319         Py_FatalError("PyEval_AcquireThread: non-NULL old thread state");
320     }
321 }
322 
323 void
PyEval_ReleaseThread(PyThreadState * tstate)324 PyEval_ReleaseThread(PyThreadState *tstate)
325 {
326     if (tstate == NULL) {
327         Py_FatalError("PyEval_ReleaseThread: NULL thread state");
328     }
329 
330     _PyRuntimeState *runtime = &_PyRuntime;
331     PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
332     if (new_tstate != tstate) {
333         Py_FatalError("PyEval_ReleaseThread: wrong thread state");
334     }
335     drop_gil(&runtime->ceval, tstate);
336 }
337 
338 /* This function is called from PyOS_AfterFork_Child to destroy all threads
339  * which are not running in the child process, and clear internal locks
340  * which might be held by those threads.
341  */
342 
343 void
_PyEval_ReInitThreads(_PyRuntimeState * runtime)344 _PyEval_ReInitThreads(_PyRuntimeState *runtime)
345 {
346     struct _ceval_runtime_state *ceval = &runtime->ceval;
347     if (!gil_created(&ceval->gil)) {
348         return;
349     }
350     recreate_gil(&ceval->gil);
351     PyThreadState *current_tstate = _PyRuntimeState_GetThreadState(runtime);
352     take_gil(ceval, current_tstate);
353 
354     struct _pending_calls *pending = &ceval->pending;
355     pending->lock = PyThread_allocate_lock();
356     if (pending->lock == NULL) {
357         Py_FatalError("Can't initialize threads for pending calls");
358     }
359 
360     /* Destroy all threads except the current one */
361     _PyThreadState_DeleteExcept(runtime, current_tstate);
362 }
363 
364 /* This function is used to signal that async exceptions are waiting to be
365    raised. */
366 
367 void
_PyEval_SignalAsyncExc(struct _ceval_runtime_state * ceval)368 _PyEval_SignalAsyncExc(struct _ceval_runtime_state *ceval)
369 {
370     SIGNAL_ASYNC_EXC(ceval);
371 }
372 
373 PyThreadState *
PyEval_SaveThread(void)374 PyEval_SaveThread(void)
375 {
376     _PyRuntimeState *runtime = &_PyRuntime;
377     struct _ceval_runtime_state *ceval = &runtime->ceval;
378     PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
379     if (tstate == NULL) {
380         Py_FatalError("PyEval_SaveThread: NULL tstate");
381     }
382     assert(gil_created(&ceval->gil));
383     drop_gil(ceval, tstate);
384     return tstate;
385 }
386 
387 void
PyEval_RestoreThread(PyThreadState * tstate)388 PyEval_RestoreThread(PyThreadState *tstate)
389 {
390     _PyRuntimeState *runtime = &_PyRuntime;
391     struct _ceval_runtime_state *ceval = &runtime->ceval;
392 
393     if (tstate == NULL) {
394         Py_FatalError("PyEval_RestoreThread: NULL tstate");
395     }
396     assert(gil_created(&ceval->gil));
397 
398     int err = errno;
399     take_gil(ceval, tstate);
400     exit_thread_if_finalizing(runtime, tstate);
401     errno = err;
402 
403     _PyThreadState_Swap(&runtime->gilstate, tstate);
404 }
405 
406 
407 /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
408    signal handlers or Mac I/O completion routines) can schedule calls
409    to a function to be called synchronously.
410    The synchronous function is called with one void* argument.
411    It should return 0 for success or -1 for failure -- failure should
412    be accompanied by an exception.
413 
414    If registry succeeds, the registry function returns 0; if it fails
415    (e.g. due to too many pending calls) it returns -1 (without setting
416    an exception condition).
417 
418    Note that because registry may occur from within signal handlers,
419    or other asynchronous events, calling malloc() is unsafe!
420 
421    Any thread can schedule pending calls, but only the main thread
422    will execute them.
423    There is no facility to schedule calls to a particular thread, but
424    that should be easy to change, should that ever be required.  In
425    that case, the static variables here should go into the python
426    threadstate.
427 */
428 
429 void
_PyEval_SignalReceived(struct _ceval_runtime_state * ceval)430 _PyEval_SignalReceived(struct _ceval_runtime_state *ceval)
431 {
432     /* bpo-30703: Function called when the C signal handler of Python gets a
433        signal. We cannot queue a callback using Py_AddPendingCall() since
434        that function is not async-signal-safe. */
435     SIGNAL_PENDING_SIGNALS(ceval);
436 }
437 
438 /* Push one item onto the queue while holding the lock. */
439 static int
_push_pending_call(struct _pending_calls * pending,int (* func)(void *),void * arg)440 _push_pending_call(struct _pending_calls *pending,
441                    int (*func)(void *), void *arg)
442 {
443     int i = pending->last;
444     int j = (i + 1) % NPENDINGCALLS;
445     if (j == pending->first) {
446         return -1; /* Queue full */
447     }
448     pending->calls[i].func = func;
449     pending->calls[i].arg = arg;
450     pending->last = j;
451     return 0;
452 }
453 
454 /* Pop one item off the queue while holding the lock. */
455 static void
_pop_pending_call(struct _pending_calls * pending,int (** func)(void *),void ** arg)456 _pop_pending_call(struct _pending_calls *pending,
457                   int (**func)(void *), void **arg)
458 {
459     int i = pending->first;
460     if (i == pending->last) {
461         return; /* Queue empty */
462     }
463 
464     *func = pending->calls[i].func;
465     *arg = pending->calls[i].arg;
466     pending->first = (i + 1) % NPENDINGCALLS;
467 }
468 
469 /* This implementation is thread-safe.  It allows
470    scheduling to be made from any thread, and even from an executing
471    callback.
472  */
473 
474 int
_PyEval_AddPendingCall(PyThreadState * tstate,struct _ceval_runtime_state * ceval,int (* func)(void *),void * arg)475 _PyEval_AddPendingCall(PyThreadState *tstate,
476                        struct _ceval_runtime_state *ceval,
477                        int (*func)(void *), void *arg)
478 {
479     struct _pending_calls *pending = &ceval->pending;
480 
481     PyThread_acquire_lock(pending->lock, WAIT_LOCK);
482     if (pending->finishing) {
483         PyThread_release_lock(pending->lock);
484 
485         PyObject *exc, *val, *tb;
486         _PyErr_Fetch(tstate, &exc, &val, &tb);
487         _PyErr_SetString(tstate, PyExc_SystemError,
488                         "Py_AddPendingCall: cannot add pending calls "
489                         "(Python shutting down)");
490         _PyErr_Print(tstate);
491         _PyErr_Restore(tstate, exc, val, tb);
492         return -1;
493     }
494     int result = _push_pending_call(pending, func, arg);
495     PyThread_release_lock(pending->lock);
496 
497     /* signal main loop */
498     SIGNAL_PENDING_CALLS(ceval);
499     return result;
500 }
501 
502 int
Py_AddPendingCall(int (* func)(void *),void * arg)503 Py_AddPendingCall(int (*func)(void *), void *arg)
504 {
505     _PyRuntimeState *runtime = &_PyRuntime;
506     PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
507     return _PyEval_AddPendingCall(tstate, &runtime->ceval, func, arg);
508 }
509 
510 static int
handle_signals(_PyRuntimeState * runtime)511 handle_signals(_PyRuntimeState *runtime)
512 {
513     /* Only handle signals on main thread.  PyEval_InitThreads must
514      * have been called already.
515      */
516     if (PyThread_get_thread_ident() != runtime->main_thread) {
517         return 0;
518     }
519     /*
520      * Ensure that the thread isn't currently running some other
521      * interpreter.
522      */
523     PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
524     if (interp != runtime->interpreters.main) {
525         return 0;
526     }
527 
528     struct _ceval_runtime_state *ceval = &runtime->ceval;
529     UNSIGNAL_PENDING_SIGNALS(ceval);
530     if (_PyErr_CheckSignals() < 0) {
531         SIGNAL_PENDING_SIGNALS(ceval); /* We're not done yet */
532         return -1;
533     }
534     return 0;
535 }
536 
537 static int
make_pending_calls(_PyRuntimeState * runtime)538 make_pending_calls(_PyRuntimeState *runtime)
539 {
540     static int busy = 0;
541 
542     /* only service pending calls on main thread */
543     if (PyThread_get_thread_ident() != runtime->main_thread) {
544         return 0;
545     }
546 
547     /* don't perform recursive pending calls */
548     if (busy) {
549         return 0;
550     }
551     busy = 1;
552     struct _ceval_runtime_state *ceval = &runtime->ceval;
553     /* unsignal before starting to call callbacks, so that any callback
554        added in-between re-signals */
555     UNSIGNAL_PENDING_CALLS(ceval);
556     int res = 0;
557 
558     /* perform a bounded number of calls, in case of recursion */
559     struct _pending_calls *pending = &ceval->pending;
560     for (int i=0; i<NPENDINGCALLS; i++) {
561         int (*func)(void *) = NULL;
562         void *arg = NULL;
563 
564         /* pop one item off the queue while holding the lock */
565         PyThread_acquire_lock(pending->lock, WAIT_LOCK);
566         _pop_pending_call(pending, &func, &arg);
567         PyThread_release_lock(pending->lock);
568 
569         /* having released the lock, perform the callback */
570         if (func == NULL) {
571             break;
572         }
573         res = func(arg);
574         if (res) {
575             goto error;
576         }
577     }
578 
579     busy = 0;
580     return res;
581 
582 error:
583     busy = 0;
584     SIGNAL_PENDING_CALLS(ceval);
585     return res;
586 }
587 
588 void
_Py_FinishPendingCalls(_PyRuntimeState * runtime)589 _Py_FinishPendingCalls(_PyRuntimeState *runtime)
590 {
591     assert(PyGILState_Check());
592 
593     PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
594     struct _pending_calls *pending = &runtime->ceval.pending;
595 
596     PyThread_acquire_lock(pending->lock, WAIT_LOCK);
597     pending->finishing = 1;
598     PyThread_release_lock(pending->lock);
599 
600     if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
601         return;
602     }
603 
604     if (make_pending_calls(runtime) < 0) {
605         PyObject *exc, *val, *tb;
606         _PyErr_Fetch(tstate, &exc, &val, &tb);
607         PyErr_BadInternalCall();
608         _PyErr_ChainExceptions(exc, val, tb);
609         _PyErr_Print(tstate);
610     }
611 }
612 
613 /* Py_MakePendingCalls() is a simple wrapper for the sake
614    of backward-compatibility. */
615 int
Py_MakePendingCalls(void)616 Py_MakePendingCalls(void)
617 {
618     assert(PyGILState_Check());
619 
620     /* Python signal handler doesn't really queue a callback: it only signals
621        that a signal was received, see _PyEval_SignalReceived(). */
622     _PyRuntimeState *runtime = &_PyRuntime;
623     int res = handle_signals(runtime);
624     if (res != 0) {
625         return res;
626     }
627 
628     res = make_pending_calls(runtime);
629     if (res != 0) {
630         return res;
631     }
632 
633     return 0;
634 }
635 
636 /* The interpreter's recursion limit */
637 
638 #ifndef Py_DEFAULT_RECURSION_LIMIT
639 #define Py_DEFAULT_RECURSION_LIMIT 1000
640 #endif
641 
642 int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
643 
644 void
_PyEval_Initialize(struct _ceval_runtime_state * state)645 _PyEval_Initialize(struct _ceval_runtime_state *state)
646 {
647     state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
648     _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
649     _gil_initialize(&state->gil);
650 }
651 
652 int
Py_GetRecursionLimit(void)653 Py_GetRecursionLimit(void)
654 {
655     return _PyRuntime.ceval.recursion_limit;
656 }
657 
658 void
Py_SetRecursionLimit(int new_limit)659 Py_SetRecursionLimit(int new_limit)
660 {
661     struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
662     ceval->recursion_limit = new_limit;
663     _Py_CheckRecursionLimit = ceval->recursion_limit;
664 }
665 
666 /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
667    if the recursion_depth reaches _Py_CheckRecursionLimit.
668    If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
669    to guarantee that _Py_CheckRecursiveCall() is regularly called.
670    Without USE_STACKCHECK, there is no need for this. */
671 int
_Py_CheckRecursiveCall(const char * where)672 _Py_CheckRecursiveCall(const char *where)
673 {
674     _PyRuntimeState *runtime = &_PyRuntime;
675     PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
676     int recursion_limit = runtime->ceval.recursion_limit;
677 
678 #ifdef USE_STACKCHECK
679     tstate->stackcheck_counter = 0;
680     if (PyOS_CheckStack()) {
681         --tstate->recursion_depth;
682         _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
683         return -1;
684     }
685     /* Needed for ABI backwards-compatibility (see bpo-31857) */
686     _Py_CheckRecursionLimit = recursion_limit;
687 #endif
688     if (tstate->recursion_critical)
689         /* Somebody asked that we don't check for recursion. */
690         return 0;
691     if (tstate->overflowed) {
692         if (tstate->recursion_depth > recursion_limit + 50) {
693             /* Overflowing while handling an overflow. Give up. */
694             Py_FatalError("Cannot recover from stack overflow.");
695         }
696         return 0;
697     }
698     if (tstate->recursion_depth > recursion_limit) {
699         --tstate->recursion_depth;
700         tstate->overflowed = 1;
701         _PyErr_Format(tstate, PyExc_RecursionError,
702                       "maximum recursion depth exceeded%s",
703                       where);
704         return -1;
705     }
706     return 0;
707 }
708 
709 static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
710 static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
711 
712 #define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
713 
714 
715 PyObject *
PyEval_EvalCode(PyObject * co,PyObject * globals,PyObject * locals)716 PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
717 {
718     return PyEval_EvalCodeEx(co,
719                       globals, locals,
720                       (PyObject **)NULL, 0,
721                       (PyObject **)NULL, 0,
722                       (PyObject **)NULL, 0,
723                       NULL, NULL);
724 }
725 
726 
727 /* Interpreter main loop */
728 
729 PyObject *
PyEval_EvalFrame(PyFrameObject * f)730 PyEval_EvalFrame(PyFrameObject *f) {
731     /* This is for backward compatibility with extension modules that
732        used this API; core interpreter code should call
733        PyEval_EvalFrameEx() */
734     return PyEval_EvalFrameEx(f, 0);
735 }
736 
737 PyObject *
PyEval_EvalFrameEx(PyFrameObject * f,int throwflag)738 PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
739 {
740     PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
741     return interp->eval_frame(f, throwflag);
742 }
743 
744 PyObject* _Py_HOT_FUNCTION
_PyEval_EvalFrameDefault(PyFrameObject * f,int throwflag)745 _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
746 {
747 #ifdef DXPAIRS
748     int lastopcode = 0;
749 #endif
750     PyObject **stack_pointer;  /* Next free slot in value stack */
751     const _Py_CODEUNIT *next_instr;
752     int opcode;        /* Current opcode */
753     int oparg;         /* Current opcode argument, if any */
754     PyObject **fastlocals, **freevars;
755     PyObject *retval = NULL;            /* Return value */
756     _PyRuntimeState * const runtime = &_PyRuntime;
757     PyThreadState * const tstate = _PyRuntimeState_GetThreadState(runtime);
758     struct _ceval_runtime_state * const ceval = &runtime->ceval;
759     _Py_atomic_int * const eval_breaker = &ceval->eval_breaker;
760     PyCodeObject *co;
761 
762     /* when tracing we set things up so that
763 
764            not (instr_lb <= current_bytecode_offset < instr_ub)
765 
766        is true when the line being executed has changed.  The
767        initial values are such as to make this false the first
768        time it is tested. */
769     int instr_ub = -1, instr_lb = 0, instr_prev = -1;
770 
771     const _Py_CODEUNIT *first_instr;
772     PyObject *names;
773     PyObject *consts;
774     _PyOpcache *co_opcache;
775 
776 #ifdef LLTRACE
777     _Py_IDENTIFIER(__ltrace__);
778 #endif
779 
780 /* Computed GOTOs, or
781        the-optimization-commonly-but-improperly-known-as-"threaded code"
782    using gcc's labels-as-values extension
783    (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
784 
785    The traditional bytecode evaluation loop uses a "switch" statement, which
786    decent compilers will optimize as a single indirect branch instruction
787    combined with a lookup table of jump addresses. However, since the
788    indirect jump instruction is shared by all opcodes, the CPU will have a
789    hard time making the right prediction for where to jump next (actually,
790    it will be always wrong except in the uncommon case of a sequence of
791    several identical opcodes).
792 
793    "Threaded code" in contrast, uses an explicit jump table and an explicit
794    indirect jump instruction at the end of each opcode. Since the jump
795    instruction is at a different address for each opcode, the CPU will make a
796    separate prediction for each of these instructions, which is equivalent to
797    predicting the second opcode of each opcode pair. These predictions have
798    a much better chance to turn out valid, especially in small bytecode loops.
799 
800    A mispredicted branch on a modern CPU flushes the whole pipeline and
801    can cost several CPU cycles (depending on the pipeline depth),
802    and potentially many more instructions (depending on the pipeline width).
803    A correctly predicted branch, however, is nearly free.
804 
805    At the time of this writing, the "threaded code" version is up to 15-20%
806    faster than the normal "switch" version, depending on the compiler and the
807    CPU architecture.
808 
809    We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
810    because it would render the measurements invalid.
811 
812 
813    NOTE: care must be taken that the compiler doesn't try to "optimize" the
814    indirect jumps by sharing them between all opcodes. Such optimizations
815    can be disabled on gcc by using the -fno-gcse flag (or possibly
816    -fno-crossjumping).
817 */
818 
819 #ifdef DYNAMIC_EXECUTION_PROFILE
820 #undef USE_COMPUTED_GOTOS
821 #define USE_COMPUTED_GOTOS 0
822 #endif
823 
824 #ifdef HAVE_COMPUTED_GOTOS
825     #ifndef USE_COMPUTED_GOTOS
826     #define USE_COMPUTED_GOTOS 1
827     #endif
828 #else
829     #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
830     #error "Computed gotos are not supported on this compiler."
831     #endif
832     #undef USE_COMPUTED_GOTOS
833     #define USE_COMPUTED_GOTOS 0
834 #endif
835 
836 #if USE_COMPUTED_GOTOS
837 /* Import the static jump table */
838 #include "opcode_targets.h"
839 
840 #define TARGET(op) \
841     op: \
842     TARGET_##op
843 
844 #ifdef LLTRACE
845 #define FAST_DISPATCH() \
846     { \
847         if (!lltrace && !_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \
848             f->f_lasti = INSTR_OFFSET(); \
849             NEXTOPARG(); \
850             goto *opcode_targets[opcode]; \
851         } \
852         goto fast_next_opcode; \
853     }
854 #else
855 #define FAST_DISPATCH() \
856     { \
857         if (!_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \
858             f->f_lasti = INSTR_OFFSET(); \
859             NEXTOPARG(); \
860             goto *opcode_targets[opcode]; \
861         } \
862         goto fast_next_opcode; \
863     }
864 #endif
865 
866 #define DISPATCH() \
867     { \
868         if (!_Py_atomic_load_relaxed(eval_breaker)) { \
869             FAST_DISPATCH(); \
870         } \
871         continue; \
872     }
873 
874 #else
875 #define TARGET(op) op
876 #define FAST_DISPATCH() goto fast_next_opcode
877 #define DISPATCH() continue
878 #endif
879 
880 
881 /* Tuple access macros */
882 
883 #ifndef Py_DEBUG
884 #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
885 #else
886 #define GETITEM(v, i) PyTuple_GetItem((v), (i))
887 #endif
888 
889 /* Code access macros */
890 
891 /* The integer overflow is checked by an assertion below. */
892 #define INSTR_OFFSET()  \
893     (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
894 #define NEXTOPARG()  do { \
895         _Py_CODEUNIT word = *next_instr; \
896         opcode = _Py_OPCODE(word); \
897         oparg = _Py_OPARG(word); \
898         next_instr++; \
899     } while (0)
900 #define JUMPTO(x)       (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
901 #define JUMPBY(x)       (next_instr += (x) / sizeof(_Py_CODEUNIT))
902 
903 /* OpCode prediction macros
904     Some opcodes tend to come in pairs thus making it possible to
905     predict the second code when the first is run.  For example,
906     COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
907 
908     Verifying the prediction costs a single high-speed test of a register
909     variable against a constant.  If the pairing was good, then the
910     processor's own internal branch predication has a high likelihood of
911     success, resulting in a nearly zero-overhead transition to the
912     next opcode.  A successful prediction saves a trip through the eval-loop
913     including its unpredictable switch-case branch.  Combined with the
914     processor's internal branch prediction, a successful PREDICT has the
915     effect of making the two opcodes run as if they were a single new opcode
916     with the bodies combined.
917 
918     If collecting opcode statistics, your choices are to either keep the
919     predictions turned-on and interpret the results as if some opcodes
920     had been combined or turn-off predictions so that the opcode frequency
921     counter updates for both opcodes.
922 
923     Opcode prediction is disabled with threaded code, since the latter allows
924     the CPU to record separate branch prediction information for each
925     opcode.
926 
927 */
928 
929 #if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
930 #define PREDICT(op)             if (0) goto PRED_##op
931 #else
932 #define PREDICT(op) \
933     do{ \
934         _Py_CODEUNIT word = *next_instr; \
935         opcode = _Py_OPCODE(word); \
936         if (opcode == op){ \
937             oparg = _Py_OPARG(word); \
938             next_instr++; \
939             goto PRED_##op; \
940         } \
941     } while(0)
942 #endif
943 #define PREDICTED(op)           PRED_##op:
944 
945 
946 /* Stack manipulation macros */
947 
948 /* The stack can grow at most MAXINT deep, as co_nlocals and
949    co_stacksize are ints. */
950 #define STACK_LEVEL()     ((int)(stack_pointer - f->f_valuestack))
951 #define EMPTY()           (STACK_LEVEL() == 0)
952 #define TOP()             (stack_pointer[-1])
953 #define SECOND()          (stack_pointer[-2])
954 #define THIRD()           (stack_pointer[-3])
955 #define FOURTH()          (stack_pointer[-4])
956 #define PEEK(n)           (stack_pointer[-(n)])
957 #define SET_TOP(v)        (stack_pointer[-1] = (v))
958 #define SET_SECOND(v)     (stack_pointer[-2] = (v))
959 #define SET_THIRD(v)      (stack_pointer[-3] = (v))
960 #define SET_FOURTH(v)     (stack_pointer[-4] = (v))
961 #define SET_VALUE(n, v)   (stack_pointer[-(n)] = (v))
962 #define BASIC_STACKADJ(n) (stack_pointer += n)
963 #define BASIC_PUSH(v)     (*stack_pointer++ = (v))
964 #define BASIC_POP()       (*--stack_pointer)
965 
966 #ifdef LLTRACE
967 #define PUSH(v)         { (void)(BASIC_PUSH(v), \
968                           lltrace && prtrace(tstate, TOP(), "push")); \
969                           assert(STACK_LEVEL() <= co->co_stacksize); }
970 #define POP()           ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
971                          BASIC_POP())
972 #define STACK_GROW(n)   do { \
973                           assert(n >= 0); \
974                           (void)(BASIC_STACKADJ(n), \
975                           lltrace && prtrace(tstate, TOP(), "stackadj")); \
976                           assert(STACK_LEVEL() <= co->co_stacksize); \
977                         } while (0)
978 #define STACK_SHRINK(n) do { \
979                             assert(n >= 0); \
980                             (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
981                             (void)(BASIC_STACKADJ(-n)); \
982                             assert(STACK_LEVEL() <= co->co_stacksize); \
983                         } while (0)
984 #define EXT_POP(STACK_POINTER) ((void)(lltrace && \
985                                 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
986                                 *--(STACK_POINTER))
987 #else
988 #define PUSH(v)                BASIC_PUSH(v)
989 #define POP()                  BASIC_POP()
990 #define STACK_GROW(n)          BASIC_STACKADJ(n)
991 #define STACK_SHRINK(n)        BASIC_STACKADJ(-n)
992 #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
993 #endif
994 
995 /* Local variable macros */
996 
997 #define GETLOCAL(i)     (fastlocals[i])
998 
999 /* The SETLOCAL() macro must not DECREF the local variable in-place and
1000    then store the new value; it must copy the old value to a temporary
1001    value, then store the new value, and then DECREF the temporary value.
1002    This is because it is possible that during the DECREF the frame is
1003    accessed by other code (e.g. a __del__ method or gc.collect()) and the
1004    variable would be pointing to already-freed memory. */
1005 #define SETLOCAL(i, value)      do { PyObject *tmp = GETLOCAL(i); \
1006                                      GETLOCAL(i) = value; \
1007                                      Py_XDECREF(tmp); } while (0)
1008 
1009 
1010 #define UNWIND_BLOCK(b) \
1011     while (STACK_LEVEL() > (b)->b_level) { \
1012         PyObject *v = POP(); \
1013         Py_XDECREF(v); \
1014     }
1015 
1016 #define UNWIND_EXCEPT_HANDLER(b) \
1017     do { \
1018         PyObject *type, *value, *traceback; \
1019         _PyErr_StackItem *exc_info; \
1020         assert(STACK_LEVEL() >= (b)->b_level + 3); \
1021         while (STACK_LEVEL() > (b)->b_level + 3) { \
1022             value = POP(); \
1023             Py_XDECREF(value); \
1024         } \
1025         exc_info = tstate->exc_info; \
1026         type = exc_info->exc_type; \
1027         value = exc_info->exc_value; \
1028         traceback = exc_info->exc_traceback; \
1029         exc_info->exc_type = POP(); \
1030         exc_info->exc_value = POP(); \
1031         exc_info->exc_traceback = POP(); \
1032         Py_XDECREF(type); \
1033         Py_XDECREF(value); \
1034         Py_XDECREF(traceback); \
1035     } while(0)
1036 
1037     /* macros for opcode cache */
1038 #define OPCACHE_CHECK() \
1039     do { \
1040         co_opcache = NULL; \
1041         if (co->co_opcache != NULL) { \
1042             unsigned char co_opt_offset = \
1043                 co->co_opcache_map[next_instr - first_instr]; \
1044             if (co_opt_offset > 0) { \
1045                 assert(co_opt_offset <= co->co_opcache_size); \
1046                 co_opcache = &co->co_opcache[co_opt_offset - 1]; \
1047                 assert(co_opcache != NULL); \
1048             } \
1049         } \
1050     } while (0)
1051 
1052 #if OPCACHE_STATS
1053 
1054 #define OPCACHE_STAT_GLOBAL_HIT() \
1055     do { \
1056         if (co->co_opcache != NULL) opcache_global_hits++; \
1057     } while (0)
1058 
1059 #define OPCACHE_STAT_GLOBAL_MISS() \
1060     do { \
1061         if (co->co_opcache != NULL) opcache_global_misses++; \
1062     } while (0)
1063 
1064 #define OPCACHE_STAT_GLOBAL_OPT() \
1065     do { \
1066         if (co->co_opcache != NULL) opcache_global_opts++; \
1067     } while (0)
1068 
1069 #else /* OPCACHE_STATS */
1070 
1071 #define OPCACHE_STAT_GLOBAL_HIT()
1072 #define OPCACHE_STAT_GLOBAL_MISS()
1073 #define OPCACHE_STAT_GLOBAL_OPT()
1074 
1075 #endif
1076 
1077 /* Start of code */
1078 
1079     /* push frame */
1080     if (Py_EnterRecursiveCall(""))
1081         return NULL;
1082 
1083     tstate->frame = f;
1084 
1085     if (tstate->use_tracing) {
1086         if (tstate->c_tracefunc != NULL) {
1087             /* tstate->c_tracefunc, if defined, is a
1088                function that will be called on *every* entry
1089                to a code block.  Its return value, if not
1090                None, is a function that will be called at
1091                the start of each executed line of code.
1092                (Actually, the function must return itself
1093                in order to continue tracing.)  The trace
1094                functions are called with three arguments:
1095                a pointer to the current frame, a string
1096                indicating why the function is called, and
1097                an argument which depends on the situation.
1098                The global trace function is also called
1099                whenever an exception is detected. */
1100             if (call_trace_protected(tstate->c_tracefunc,
1101                                      tstate->c_traceobj,
1102                                      tstate, f, PyTrace_CALL, Py_None)) {
1103                 /* Trace function raised an error */
1104                 goto exit_eval_frame;
1105             }
1106         }
1107         if (tstate->c_profilefunc != NULL) {
1108             /* Similar for c_profilefunc, except it needn't
1109                return itself and isn't called for "line" events */
1110             if (call_trace_protected(tstate->c_profilefunc,
1111                                      tstate->c_profileobj,
1112                                      tstate, f, PyTrace_CALL, Py_None)) {
1113                 /* Profile function raised an error */
1114                 goto exit_eval_frame;
1115             }
1116         }
1117     }
1118 
1119     if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1120         dtrace_function_entry(f);
1121 
1122     co = f->f_code;
1123     names = co->co_names;
1124     consts = co->co_consts;
1125     fastlocals = f->f_localsplus;
1126     freevars = f->f_localsplus + co->co_nlocals;
1127     assert(PyBytes_Check(co->co_code));
1128     assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
1129     assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1130     assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1131     first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
1132     /*
1133        f->f_lasti refers to the index of the last instruction,
1134        unless it's -1 in which case next_instr should be first_instr.
1135 
1136        YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
1137        multiple values.
1138 
1139        When the PREDICT() macros are enabled, some opcode pairs follow in
1140        direct succession without updating f->f_lasti.  A successful
1141        prediction effectively links the two codes together as if they
1142        were a single new opcode; accordingly,f->f_lasti will point to
1143        the first code in the pair (for instance, GET_ITER followed by
1144        FOR_ITER is effectively a single opcode and f->f_lasti will point
1145        to the beginning of the combined pair.)
1146     */
1147     assert(f->f_lasti >= -1);
1148     next_instr = first_instr;
1149     if (f->f_lasti >= 0) {
1150         assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1151         next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
1152     }
1153     stack_pointer = f->f_stacktop;
1154     assert(stack_pointer != NULL);
1155     f->f_stacktop = NULL;       /* remains NULL unless yield suspends frame */
1156     f->f_executing = 1;
1157 
1158     if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1159         co->co_opcache_flag++;
1160         if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1161             if (_PyCode_InitOpcache(co) < 0) {
1162                 goto exit_eval_frame;
1163             }
1164 #if OPCACHE_STATS
1165             opcache_code_objects_extra_mem +=
1166                 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1167                 sizeof(_PyOpcache) * co->co_opcache_size;
1168             opcache_code_objects++;
1169 #endif
1170         }
1171     }
1172 
1173 #ifdef LLTRACE
1174     lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
1175 #endif
1176 
1177     if (throwflag) /* support for generator.throw() */
1178         goto error;
1179 
1180 #ifdef Py_DEBUG
1181     /* PyEval_EvalFrameEx() must not be called with an exception set,
1182        because it can clear it (directly or indirectly) and so the
1183        caller loses its exception */
1184     assert(!_PyErr_Occurred(tstate));
1185 #endif
1186 
1187 main_loop:
1188     for (;;) {
1189         assert(stack_pointer >= f->f_valuestack); /* else underflow */
1190         assert(STACK_LEVEL() <= co->co_stacksize);  /* else overflow */
1191         assert(!_PyErr_Occurred(tstate));
1192 
1193         /* Do periodic things.  Doing this every time through
1194            the loop would add too much overhead, so we do it
1195            only every Nth instruction.  We also do it if
1196            ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1197            event needs attention (e.g. a signal handler or
1198            async I/O handler); see Py_AddPendingCall() and
1199            Py_MakePendingCalls() above. */
1200 
1201         if (_Py_atomic_load_relaxed(eval_breaker)) {
1202             opcode = _Py_OPCODE(*next_instr);
1203             if (opcode == SETUP_FINALLY ||
1204                 opcode == SETUP_WITH ||
1205                 opcode == BEFORE_ASYNC_WITH ||
1206                 opcode == YIELD_FROM) {
1207                 /* Few cases where we skip running signal handlers and other
1208                    pending calls:
1209                    - If we're about to enter the 'with:'. It will prevent
1210                      emitting a resource warning in the common idiom
1211                      'with open(path) as file:'.
1212                    - If we're about to enter the 'async with:'.
1213                    - If we're about to enter the 'try:' of a try/finally (not
1214                      *very* useful, but might help in some cases and it's
1215                      traditional)
1216                    - If we're resuming a chain of nested 'yield from' or
1217                      'await' calls, then each frame is parked with YIELD_FROM
1218                      as its next opcode. If the user hit control-C we want to
1219                      wait until we've reached the innermost frame before
1220                      running the signal handler and raising KeyboardInterrupt
1221                      (see bpo-30039).
1222                 */
1223                 goto fast_next_opcode;
1224             }
1225 
1226             if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
1227                 if (handle_signals(runtime) != 0) {
1228                     goto error;
1229                 }
1230             }
1231             if (_Py_atomic_load_relaxed(&ceval->pending.calls_to_do)) {
1232                 if (make_pending_calls(runtime) != 0) {
1233                     goto error;
1234                 }
1235             }
1236 
1237             if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) {
1238                 /* Give another thread a chance */
1239                 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
1240                     Py_FatalError("ceval: tstate mix-up");
1241                 }
1242                 drop_gil(ceval, tstate);
1243 
1244                 /* Other threads may run now */
1245 
1246                 take_gil(ceval, tstate);
1247 
1248                 /* Check if we should make a quick exit. */
1249                 exit_thread_if_finalizing(runtime, tstate);
1250 
1251                 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
1252                     Py_FatalError("ceval: orphan tstate");
1253                 }
1254             }
1255             /* Check for asynchronous exceptions. */
1256             if (tstate->async_exc != NULL) {
1257                 PyObject *exc = tstate->async_exc;
1258                 tstate->async_exc = NULL;
1259                 UNSIGNAL_ASYNC_EXC(ceval);
1260                 _PyErr_SetNone(tstate, exc);
1261                 Py_DECREF(exc);
1262                 goto error;
1263             }
1264         }
1265 
1266     fast_next_opcode:
1267         f->f_lasti = INSTR_OFFSET();
1268 
1269         if (PyDTrace_LINE_ENABLED())
1270             maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1271 
1272         /* line-by-line tracing support */
1273 
1274         if (_Py_TracingPossible(ceval) &&
1275             tstate->c_tracefunc != NULL && !tstate->tracing) {
1276             int err;
1277             /* see maybe_call_line_trace
1278                for expository comments */
1279             f->f_stacktop = stack_pointer;
1280 
1281             err = maybe_call_line_trace(tstate->c_tracefunc,
1282                                         tstate->c_traceobj,
1283                                         tstate, f,
1284                                         &instr_lb, &instr_ub, &instr_prev);
1285             /* Reload possibly changed frame fields */
1286             JUMPTO(f->f_lasti);
1287             if (f->f_stacktop != NULL) {
1288                 stack_pointer = f->f_stacktop;
1289                 f->f_stacktop = NULL;
1290             }
1291             if (err)
1292                 /* trace function raised an exception */
1293                 goto error;
1294         }
1295 
1296         /* Extract opcode and argument */
1297 
1298         NEXTOPARG();
1299     dispatch_opcode:
1300 #ifdef DYNAMIC_EXECUTION_PROFILE
1301 #ifdef DXPAIRS
1302         dxpairs[lastopcode][opcode]++;
1303         lastopcode = opcode;
1304 #endif
1305         dxp[opcode]++;
1306 #endif
1307 
1308 #ifdef LLTRACE
1309         /* Instruction tracing */
1310 
1311         if (lltrace) {
1312             if (HAS_ARG(opcode)) {
1313                 printf("%d: %d, %d\n",
1314                        f->f_lasti, opcode, oparg);
1315             }
1316             else {
1317                 printf("%d: %d\n",
1318                        f->f_lasti, opcode);
1319             }
1320         }
1321 #endif
1322 
1323         switch (opcode) {
1324 
1325         /* BEWARE!
1326            It is essential that any operation that fails must goto error
1327            and that all operation that succeed call [FAST_]DISPATCH() ! */
1328 
1329         case TARGET(NOP): {
1330             FAST_DISPATCH();
1331         }
1332 
1333         case TARGET(LOAD_FAST): {
1334             PyObject *value = GETLOCAL(oparg);
1335             if (value == NULL) {
1336                 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
1337                                      UNBOUNDLOCAL_ERROR_MSG,
1338                                      PyTuple_GetItem(co->co_varnames, oparg));
1339                 goto error;
1340             }
1341             Py_INCREF(value);
1342             PUSH(value);
1343             FAST_DISPATCH();
1344         }
1345 
1346         case TARGET(LOAD_CONST): {
1347             PREDICTED(LOAD_CONST);
1348             PyObject *value = GETITEM(consts, oparg);
1349             Py_INCREF(value);
1350             PUSH(value);
1351             FAST_DISPATCH();
1352         }
1353 
1354         case TARGET(STORE_FAST): {
1355             PREDICTED(STORE_FAST);
1356             PyObject *value = POP();
1357             SETLOCAL(oparg, value);
1358             FAST_DISPATCH();
1359         }
1360 
1361         case TARGET(POP_TOP): {
1362             PyObject *value = POP();
1363             Py_DECREF(value);
1364             FAST_DISPATCH();
1365         }
1366 
1367         case TARGET(ROT_TWO): {
1368             PyObject *top = TOP();
1369             PyObject *second = SECOND();
1370             SET_TOP(second);
1371             SET_SECOND(top);
1372             FAST_DISPATCH();
1373         }
1374 
1375         case TARGET(ROT_THREE): {
1376             PyObject *top = TOP();
1377             PyObject *second = SECOND();
1378             PyObject *third = THIRD();
1379             SET_TOP(second);
1380             SET_SECOND(third);
1381             SET_THIRD(top);
1382             FAST_DISPATCH();
1383         }
1384 
1385         case TARGET(ROT_FOUR): {
1386             PyObject *top = TOP();
1387             PyObject *second = SECOND();
1388             PyObject *third = THIRD();
1389             PyObject *fourth = FOURTH();
1390             SET_TOP(second);
1391             SET_SECOND(third);
1392             SET_THIRD(fourth);
1393             SET_FOURTH(top);
1394             FAST_DISPATCH();
1395         }
1396 
1397         case TARGET(DUP_TOP): {
1398             PyObject *top = TOP();
1399             Py_INCREF(top);
1400             PUSH(top);
1401             FAST_DISPATCH();
1402         }
1403 
1404         case TARGET(DUP_TOP_TWO): {
1405             PyObject *top = TOP();
1406             PyObject *second = SECOND();
1407             Py_INCREF(top);
1408             Py_INCREF(second);
1409             STACK_GROW(2);
1410             SET_TOP(top);
1411             SET_SECOND(second);
1412             FAST_DISPATCH();
1413         }
1414 
1415         case TARGET(UNARY_POSITIVE): {
1416             PyObject *value = TOP();
1417             PyObject *res = PyNumber_Positive(value);
1418             Py_DECREF(value);
1419             SET_TOP(res);
1420             if (res == NULL)
1421                 goto error;
1422             DISPATCH();
1423         }
1424 
1425         case TARGET(UNARY_NEGATIVE): {
1426             PyObject *value = TOP();
1427             PyObject *res = PyNumber_Negative(value);
1428             Py_DECREF(value);
1429             SET_TOP(res);
1430             if (res == NULL)
1431                 goto error;
1432             DISPATCH();
1433         }
1434 
1435         case TARGET(UNARY_NOT): {
1436             PyObject *value = TOP();
1437             int err = PyObject_IsTrue(value);
1438             Py_DECREF(value);
1439             if (err == 0) {
1440                 Py_INCREF(Py_True);
1441                 SET_TOP(Py_True);
1442                 DISPATCH();
1443             }
1444             else if (err > 0) {
1445                 Py_INCREF(Py_False);
1446                 SET_TOP(Py_False);
1447                 DISPATCH();
1448             }
1449             STACK_SHRINK(1);
1450             goto error;
1451         }
1452 
1453         case TARGET(UNARY_INVERT): {
1454             PyObject *value = TOP();
1455             PyObject *res = PyNumber_Invert(value);
1456             Py_DECREF(value);
1457             SET_TOP(res);
1458             if (res == NULL)
1459                 goto error;
1460             DISPATCH();
1461         }
1462 
1463         case TARGET(BINARY_POWER): {
1464             PyObject *exp = POP();
1465             PyObject *base = TOP();
1466             PyObject *res = PyNumber_Power(base, exp, Py_None);
1467             Py_DECREF(base);
1468             Py_DECREF(exp);
1469             SET_TOP(res);
1470             if (res == NULL)
1471                 goto error;
1472             DISPATCH();
1473         }
1474 
1475         case TARGET(BINARY_MULTIPLY): {
1476             PyObject *right = POP();
1477             PyObject *left = TOP();
1478             PyObject *res = PyNumber_Multiply(left, right);
1479             Py_DECREF(left);
1480             Py_DECREF(right);
1481             SET_TOP(res);
1482             if (res == NULL)
1483                 goto error;
1484             DISPATCH();
1485         }
1486 
1487         case TARGET(BINARY_MATRIX_MULTIPLY): {
1488             PyObject *right = POP();
1489             PyObject *left = TOP();
1490             PyObject *res = PyNumber_MatrixMultiply(left, right);
1491             Py_DECREF(left);
1492             Py_DECREF(right);
1493             SET_TOP(res);
1494             if (res == NULL)
1495                 goto error;
1496             DISPATCH();
1497         }
1498 
1499         case TARGET(BINARY_TRUE_DIVIDE): {
1500             PyObject *divisor = POP();
1501             PyObject *dividend = TOP();
1502             PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1503             Py_DECREF(dividend);
1504             Py_DECREF(divisor);
1505             SET_TOP(quotient);
1506             if (quotient == NULL)
1507                 goto error;
1508             DISPATCH();
1509         }
1510 
1511         case TARGET(BINARY_FLOOR_DIVIDE): {
1512             PyObject *divisor = POP();
1513             PyObject *dividend = TOP();
1514             PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1515             Py_DECREF(dividend);
1516             Py_DECREF(divisor);
1517             SET_TOP(quotient);
1518             if (quotient == NULL)
1519                 goto error;
1520             DISPATCH();
1521         }
1522 
1523         case TARGET(BINARY_MODULO): {
1524             PyObject *divisor = POP();
1525             PyObject *dividend = TOP();
1526             PyObject *res;
1527             if (PyUnicode_CheckExact(dividend) && (
1528                   !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1529               // fast path; string formatting, but not if the RHS is a str subclass
1530               // (see issue28598)
1531               res = PyUnicode_Format(dividend, divisor);
1532             } else {
1533               res = PyNumber_Remainder(dividend, divisor);
1534             }
1535             Py_DECREF(divisor);
1536             Py_DECREF(dividend);
1537             SET_TOP(res);
1538             if (res == NULL)
1539                 goto error;
1540             DISPATCH();
1541         }
1542 
1543         case TARGET(BINARY_ADD): {
1544             PyObject *right = POP();
1545             PyObject *left = TOP();
1546             PyObject *sum;
1547             /* NOTE(haypo): Please don't try to micro-optimize int+int on
1548                CPython using bytecode, it is simply worthless.
1549                See http://bugs.python.org/issue21955 and
1550                http://bugs.python.org/issue10044 for the discussion. In short,
1551                no patch shown any impact on a realistic benchmark, only a minor
1552                speedup on microbenchmarks. */
1553             if (PyUnicode_CheckExact(left) &&
1554                      PyUnicode_CheckExact(right)) {
1555                 sum = unicode_concatenate(tstate, left, right, f, next_instr);
1556                 /* unicode_concatenate consumed the ref to left */
1557             }
1558             else {
1559                 sum = PyNumber_Add(left, right);
1560                 Py_DECREF(left);
1561             }
1562             Py_DECREF(right);
1563             SET_TOP(sum);
1564             if (sum == NULL)
1565                 goto error;
1566             DISPATCH();
1567         }
1568 
1569         case TARGET(BINARY_SUBTRACT): {
1570             PyObject *right = POP();
1571             PyObject *left = TOP();
1572             PyObject *diff = PyNumber_Subtract(left, right);
1573             Py_DECREF(right);
1574             Py_DECREF(left);
1575             SET_TOP(diff);
1576             if (diff == NULL)
1577                 goto error;
1578             DISPATCH();
1579         }
1580 
1581         case TARGET(BINARY_SUBSCR): {
1582             PyObject *sub = POP();
1583             PyObject *container = TOP();
1584             PyObject *res = PyObject_GetItem(container, sub);
1585             Py_DECREF(container);
1586             Py_DECREF(sub);
1587             SET_TOP(res);
1588             if (res == NULL)
1589                 goto error;
1590             DISPATCH();
1591         }
1592 
1593         case TARGET(BINARY_LSHIFT): {
1594             PyObject *right = POP();
1595             PyObject *left = TOP();
1596             PyObject *res = PyNumber_Lshift(left, right);
1597             Py_DECREF(left);
1598             Py_DECREF(right);
1599             SET_TOP(res);
1600             if (res == NULL)
1601                 goto error;
1602             DISPATCH();
1603         }
1604 
1605         case TARGET(BINARY_RSHIFT): {
1606             PyObject *right = POP();
1607             PyObject *left = TOP();
1608             PyObject *res = PyNumber_Rshift(left, right);
1609             Py_DECREF(left);
1610             Py_DECREF(right);
1611             SET_TOP(res);
1612             if (res == NULL)
1613                 goto error;
1614             DISPATCH();
1615         }
1616 
1617         case TARGET(BINARY_AND): {
1618             PyObject *right = POP();
1619             PyObject *left = TOP();
1620             PyObject *res = PyNumber_And(left, right);
1621             Py_DECREF(left);
1622             Py_DECREF(right);
1623             SET_TOP(res);
1624             if (res == NULL)
1625                 goto error;
1626             DISPATCH();
1627         }
1628 
1629         case TARGET(BINARY_XOR): {
1630             PyObject *right = POP();
1631             PyObject *left = TOP();
1632             PyObject *res = PyNumber_Xor(left, right);
1633             Py_DECREF(left);
1634             Py_DECREF(right);
1635             SET_TOP(res);
1636             if (res == NULL)
1637                 goto error;
1638             DISPATCH();
1639         }
1640 
1641         case TARGET(BINARY_OR): {
1642             PyObject *right = POP();
1643             PyObject *left = TOP();
1644             PyObject *res = PyNumber_Or(left, right);
1645             Py_DECREF(left);
1646             Py_DECREF(right);
1647             SET_TOP(res);
1648             if (res == NULL)
1649                 goto error;
1650             DISPATCH();
1651         }
1652 
1653         case TARGET(LIST_APPEND): {
1654             PyObject *v = POP();
1655             PyObject *list = PEEK(oparg);
1656             int err;
1657             err = PyList_Append(list, v);
1658             Py_DECREF(v);
1659             if (err != 0)
1660                 goto error;
1661             PREDICT(JUMP_ABSOLUTE);
1662             DISPATCH();
1663         }
1664 
1665         case TARGET(SET_ADD): {
1666             PyObject *v = POP();
1667             PyObject *set = PEEK(oparg);
1668             int err;
1669             err = PySet_Add(set, v);
1670             Py_DECREF(v);
1671             if (err != 0)
1672                 goto error;
1673             PREDICT(JUMP_ABSOLUTE);
1674             DISPATCH();
1675         }
1676 
1677         case TARGET(INPLACE_POWER): {
1678             PyObject *exp = POP();
1679             PyObject *base = TOP();
1680             PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1681             Py_DECREF(base);
1682             Py_DECREF(exp);
1683             SET_TOP(res);
1684             if (res == NULL)
1685                 goto error;
1686             DISPATCH();
1687         }
1688 
1689         case TARGET(INPLACE_MULTIPLY): {
1690             PyObject *right = POP();
1691             PyObject *left = TOP();
1692             PyObject *res = PyNumber_InPlaceMultiply(left, right);
1693             Py_DECREF(left);
1694             Py_DECREF(right);
1695             SET_TOP(res);
1696             if (res == NULL)
1697                 goto error;
1698             DISPATCH();
1699         }
1700 
1701         case TARGET(INPLACE_MATRIX_MULTIPLY): {
1702             PyObject *right = POP();
1703             PyObject *left = TOP();
1704             PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1705             Py_DECREF(left);
1706             Py_DECREF(right);
1707             SET_TOP(res);
1708             if (res == NULL)
1709                 goto error;
1710             DISPATCH();
1711         }
1712 
1713         case TARGET(INPLACE_TRUE_DIVIDE): {
1714             PyObject *divisor = POP();
1715             PyObject *dividend = TOP();
1716             PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1717             Py_DECREF(dividend);
1718             Py_DECREF(divisor);
1719             SET_TOP(quotient);
1720             if (quotient == NULL)
1721                 goto error;
1722             DISPATCH();
1723         }
1724 
1725         case TARGET(INPLACE_FLOOR_DIVIDE): {
1726             PyObject *divisor = POP();
1727             PyObject *dividend = TOP();
1728             PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1729             Py_DECREF(dividend);
1730             Py_DECREF(divisor);
1731             SET_TOP(quotient);
1732             if (quotient == NULL)
1733                 goto error;
1734             DISPATCH();
1735         }
1736 
1737         case TARGET(INPLACE_MODULO): {
1738             PyObject *right = POP();
1739             PyObject *left = TOP();
1740             PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1741             Py_DECREF(left);
1742             Py_DECREF(right);
1743             SET_TOP(mod);
1744             if (mod == NULL)
1745                 goto error;
1746             DISPATCH();
1747         }
1748 
1749         case TARGET(INPLACE_ADD): {
1750             PyObject *right = POP();
1751             PyObject *left = TOP();
1752             PyObject *sum;
1753             if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1754                 sum = unicode_concatenate(tstate, left, right, f, next_instr);
1755                 /* unicode_concatenate consumed the ref to left */
1756             }
1757             else {
1758                 sum = PyNumber_InPlaceAdd(left, right);
1759                 Py_DECREF(left);
1760             }
1761             Py_DECREF(right);
1762             SET_TOP(sum);
1763             if (sum == NULL)
1764                 goto error;
1765             DISPATCH();
1766         }
1767 
1768         case TARGET(INPLACE_SUBTRACT): {
1769             PyObject *right = POP();
1770             PyObject *left = TOP();
1771             PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1772             Py_DECREF(left);
1773             Py_DECREF(right);
1774             SET_TOP(diff);
1775             if (diff == NULL)
1776                 goto error;
1777             DISPATCH();
1778         }
1779 
1780         case TARGET(INPLACE_LSHIFT): {
1781             PyObject *right = POP();
1782             PyObject *left = TOP();
1783             PyObject *res = PyNumber_InPlaceLshift(left, right);
1784             Py_DECREF(left);
1785             Py_DECREF(right);
1786             SET_TOP(res);
1787             if (res == NULL)
1788                 goto error;
1789             DISPATCH();
1790         }
1791 
1792         case TARGET(INPLACE_RSHIFT): {
1793             PyObject *right = POP();
1794             PyObject *left = TOP();
1795             PyObject *res = PyNumber_InPlaceRshift(left, right);
1796             Py_DECREF(left);
1797             Py_DECREF(right);
1798             SET_TOP(res);
1799             if (res == NULL)
1800                 goto error;
1801             DISPATCH();
1802         }
1803 
1804         case TARGET(INPLACE_AND): {
1805             PyObject *right = POP();
1806             PyObject *left = TOP();
1807             PyObject *res = PyNumber_InPlaceAnd(left, right);
1808             Py_DECREF(left);
1809             Py_DECREF(right);
1810             SET_TOP(res);
1811             if (res == NULL)
1812                 goto error;
1813             DISPATCH();
1814         }
1815 
1816         case TARGET(INPLACE_XOR): {
1817             PyObject *right = POP();
1818             PyObject *left = TOP();
1819             PyObject *res = PyNumber_InPlaceXor(left, right);
1820             Py_DECREF(left);
1821             Py_DECREF(right);
1822             SET_TOP(res);
1823             if (res == NULL)
1824                 goto error;
1825             DISPATCH();
1826         }
1827 
1828         case TARGET(INPLACE_OR): {
1829             PyObject *right = POP();
1830             PyObject *left = TOP();
1831             PyObject *res = PyNumber_InPlaceOr(left, right);
1832             Py_DECREF(left);
1833             Py_DECREF(right);
1834             SET_TOP(res);
1835             if (res == NULL)
1836                 goto error;
1837             DISPATCH();
1838         }
1839 
1840         case TARGET(STORE_SUBSCR): {
1841             PyObject *sub = TOP();
1842             PyObject *container = SECOND();
1843             PyObject *v = THIRD();
1844             int err;
1845             STACK_SHRINK(3);
1846             /* container[sub] = v */
1847             err = PyObject_SetItem(container, sub, v);
1848             Py_DECREF(v);
1849             Py_DECREF(container);
1850             Py_DECREF(sub);
1851             if (err != 0)
1852                 goto error;
1853             DISPATCH();
1854         }
1855 
1856         case TARGET(DELETE_SUBSCR): {
1857             PyObject *sub = TOP();
1858             PyObject *container = SECOND();
1859             int err;
1860             STACK_SHRINK(2);
1861             /* del container[sub] */
1862             err = PyObject_DelItem(container, sub);
1863             Py_DECREF(container);
1864             Py_DECREF(sub);
1865             if (err != 0)
1866                 goto error;
1867             DISPATCH();
1868         }
1869 
1870         case TARGET(PRINT_EXPR): {
1871             _Py_IDENTIFIER(displayhook);
1872             PyObject *value = POP();
1873             PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
1874             PyObject *res;
1875             if (hook == NULL) {
1876                 _PyErr_SetString(tstate, PyExc_RuntimeError,
1877                                  "lost sys.displayhook");
1878                 Py_DECREF(value);
1879                 goto error;
1880             }
1881             res = PyObject_CallFunctionObjArgs(hook, value, NULL);
1882             Py_DECREF(value);
1883             if (res == NULL)
1884                 goto error;
1885             Py_DECREF(res);
1886             DISPATCH();
1887         }
1888 
1889         case TARGET(RAISE_VARARGS): {
1890             PyObject *cause = NULL, *exc = NULL;
1891             switch (oparg) {
1892             case 2:
1893                 cause = POP(); /* cause */
1894                 /* fall through */
1895             case 1:
1896                 exc = POP(); /* exc */
1897                 /* fall through */
1898             case 0:
1899                 if (do_raise(tstate, exc, cause)) {
1900                     goto exception_unwind;
1901                 }
1902                 break;
1903             default:
1904                 _PyErr_SetString(tstate, PyExc_SystemError,
1905                                  "bad RAISE_VARARGS oparg");
1906                 break;
1907             }
1908             goto error;
1909         }
1910 
1911         case TARGET(RETURN_VALUE): {
1912             retval = POP();
1913             assert(f->f_iblock == 0);
1914             goto exit_returning;
1915         }
1916 
1917         case TARGET(GET_AITER): {
1918             unaryfunc getter = NULL;
1919             PyObject *iter = NULL;
1920             PyObject *obj = TOP();
1921             PyTypeObject *type = Py_TYPE(obj);
1922 
1923             if (type->tp_as_async != NULL) {
1924                 getter = type->tp_as_async->am_aiter;
1925             }
1926 
1927             if (getter != NULL) {
1928                 iter = (*getter)(obj);
1929                 Py_DECREF(obj);
1930                 if (iter == NULL) {
1931                     SET_TOP(NULL);
1932                     goto error;
1933                 }
1934             }
1935             else {
1936                 SET_TOP(NULL);
1937                 _PyErr_Format(tstate, PyExc_TypeError,
1938                               "'async for' requires an object with "
1939                               "__aiter__ method, got %.100s",
1940                               type->tp_name);
1941                 Py_DECREF(obj);
1942                 goto error;
1943             }
1944 
1945             if (Py_TYPE(iter)->tp_as_async == NULL ||
1946                     Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
1947 
1948                 SET_TOP(NULL);
1949                 _PyErr_Format(tstate, PyExc_TypeError,
1950                               "'async for' received an object from __aiter__ "
1951                               "that does not implement __anext__: %.100s",
1952                               Py_TYPE(iter)->tp_name);
1953                 Py_DECREF(iter);
1954                 goto error;
1955             }
1956 
1957             SET_TOP(iter);
1958             DISPATCH();
1959         }
1960 
1961         case TARGET(GET_ANEXT): {
1962             unaryfunc getter = NULL;
1963             PyObject *next_iter = NULL;
1964             PyObject *awaitable = NULL;
1965             PyObject *aiter = TOP();
1966             PyTypeObject *type = Py_TYPE(aiter);
1967 
1968             if (PyAsyncGen_CheckExact(aiter)) {
1969                 awaitable = type->tp_as_async->am_anext(aiter);
1970                 if (awaitable == NULL) {
1971                     goto error;
1972                 }
1973             } else {
1974                 if (type->tp_as_async != NULL){
1975                     getter = type->tp_as_async->am_anext;
1976                 }
1977 
1978                 if (getter != NULL) {
1979                     next_iter = (*getter)(aiter);
1980                     if (next_iter == NULL) {
1981                         goto error;
1982                     }
1983                 }
1984                 else {
1985                     _PyErr_Format(tstate, PyExc_TypeError,
1986                                   "'async for' requires an iterator with "
1987                                   "__anext__ method, got %.100s",
1988                                   type->tp_name);
1989                     goto error;
1990                 }
1991 
1992                 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1993                 if (awaitable == NULL) {
1994                     _PyErr_FormatFromCause(
1995                         PyExc_TypeError,
1996                         "'async for' received an invalid object "
1997                         "from __anext__: %.100s",
1998                         Py_TYPE(next_iter)->tp_name);
1999 
2000                     Py_DECREF(next_iter);
2001                     goto error;
2002                 } else {
2003                     Py_DECREF(next_iter);
2004                 }
2005             }
2006 
2007             PUSH(awaitable);
2008             PREDICT(LOAD_CONST);
2009             DISPATCH();
2010         }
2011 
2012         case TARGET(GET_AWAITABLE): {
2013             PREDICTED(GET_AWAITABLE);
2014             PyObject *iterable = TOP();
2015             PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
2016 
2017             if (iter == NULL) {
2018                 format_awaitable_error(tstate, Py_TYPE(iterable),
2019                                        _Py_OPCODE(next_instr[-2]));
2020             }
2021 
2022             Py_DECREF(iterable);
2023 
2024             if (iter != NULL && PyCoro_CheckExact(iter)) {
2025                 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2026                 if (yf != NULL) {
2027                     /* `iter` is a coroutine object that is being
2028                        awaited, `yf` is a pointer to the current awaitable
2029                        being awaited on. */
2030                     Py_DECREF(yf);
2031                     Py_CLEAR(iter);
2032                     _PyErr_SetString(tstate, PyExc_RuntimeError,
2033                                      "coroutine is being awaited already");
2034                     /* The code below jumps to `error` if `iter` is NULL. */
2035                 }
2036             }
2037 
2038             SET_TOP(iter); /* Even if it's NULL */
2039 
2040             if (iter == NULL) {
2041                 goto error;
2042             }
2043 
2044             PREDICT(LOAD_CONST);
2045             DISPATCH();
2046         }
2047 
2048         case TARGET(YIELD_FROM): {
2049             PyObject *v = POP();
2050             PyObject *receiver = TOP();
2051             int err;
2052             if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2053                 retval = _PyGen_Send((PyGenObject *)receiver, v);
2054             } else {
2055                 _Py_IDENTIFIER(send);
2056                 if (v == Py_None)
2057                     retval = Py_TYPE(receiver)->tp_iternext(receiver);
2058                 else
2059                     retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
2060             }
2061             Py_DECREF(v);
2062             if (retval == NULL) {
2063                 PyObject *val;
2064                 if (tstate->c_tracefunc != NULL
2065                         && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
2066                     call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
2067                 err = _PyGen_FetchStopIterationValue(&val);
2068                 if (err < 0)
2069                     goto error;
2070                 Py_DECREF(receiver);
2071                 SET_TOP(val);
2072                 DISPATCH();
2073             }
2074             /* receiver remains on stack, retval is value to be yielded */
2075             f->f_stacktop = stack_pointer;
2076             /* and repeat... */
2077             assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
2078             f->f_lasti -= sizeof(_Py_CODEUNIT);
2079             goto exit_yielding;
2080         }
2081 
2082         case TARGET(YIELD_VALUE): {
2083             retval = POP();
2084 
2085             if (co->co_flags & CO_ASYNC_GENERATOR) {
2086                 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2087                 Py_DECREF(retval);
2088                 if (w == NULL) {
2089                     retval = NULL;
2090                     goto error;
2091                 }
2092                 retval = w;
2093             }
2094 
2095             f->f_stacktop = stack_pointer;
2096             goto exit_yielding;
2097         }
2098 
2099         case TARGET(POP_EXCEPT): {
2100             PyObject *type, *value, *traceback;
2101             _PyErr_StackItem *exc_info;
2102             PyTryBlock *b = PyFrame_BlockPop(f);
2103             if (b->b_type != EXCEPT_HANDLER) {
2104                 _PyErr_SetString(tstate, PyExc_SystemError,
2105                                  "popped block is not an except handler");
2106                 goto error;
2107             }
2108             assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2109                    STACK_LEVEL() <= (b)->b_level + 4);
2110             exc_info = tstate->exc_info;
2111             type = exc_info->exc_type;
2112             value = exc_info->exc_value;
2113             traceback = exc_info->exc_traceback;
2114             exc_info->exc_type = POP();
2115             exc_info->exc_value = POP();
2116             exc_info->exc_traceback = POP();
2117             Py_XDECREF(type);
2118             Py_XDECREF(value);
2119             Py_XDECREF(traceback);
2120             DISPATCH();
2121         }
2122 
2123         case TARGET(POP_BLOCK): {
2124             PREDICTED(POP_BLOCK);
2125             PyFrame_BlockPop(f);
2126             DISPATCH();
2127         }
2128 
2129         case TARGET(POP_FINALLY): {
2130             /* If oparg is 0 at the top of the stack are 1 or 6 values:
2131                Either:
2132                 - TOP = NULL or an integer
2133                or:
2134                 - (TOP, SECOND, THIRD) = exc_info()
2135                 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2136 
2137                If oparg is 1 the value for 'return' was additionally pushed
2138                at the top of the stack.
2139             */
2140             PyObject *res = NULL;
2141             if (oparg) {
2142                 res = POP();
2143             }
2144             PyObject *exc = POP();
2145             if (exc == NULL || PyLong_CheckExact(exc)) {
2146                 Py_XDECREF(exc);
2147             }
2148             else {
2149                 Py_DECREF(exc);
2150                 Py_DECREF(POP());
2151                 Py_DECREF(POP());
2152 
2153                 PyObject *type, *value, *traceback;
2154                 _PyErr_StackItem *exc_info;
2155                 PyTryBlock *b = PyFrame_BlockPop(f);
2156                 if (b->b_type != EXCEPT_HANDLER) {
2157                     _PyErr_SetString(tstate, PyExc_SystemError,
2158                                      "popped block is not an except handler");
2159                     Py_XDECREF(res);
2160                     goto error;
2161                 }
2162                 assert(STACK_LEVEL() == (b)->b_level + 3);
2163                 exc_info = tstate->exc_info;
2164                 type = exc_info->exc_type;
2165                 value = exc_info->exc_value;
2166                 traceback = exc_info->exc_traceback;
2167                 exc_info->exc_type = POP();
2168                 exc_info->exc_value = POP();
2169                 exc_info->exc_traceback = POP();
2170                 Py_XDECREF(type);
2171                 Py_XDECREF(value);
2172                 Py_XDECREF(traceback);
2173             }
2174             if (oparg) {
2175                 PUSH(res);
2176             }
2177             DISPATCH();
2178         }
2179 
2180         case TARGET(CALL_FINALLY): {
2181             PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
2182             if (ret == NULL) {
2183                 goto error;
2184             }
2185             PUSH(ret);
2186             JUMPBY(oparg);
2187             FAST_DISPATCH();
2188         }
2189 
2190         case TARGET(BEGIN_FINALLY): {
2191             /* Push NULL onto the stack for using it in END_FINALLY,
2192                POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH.
2193              */
2194             PUSH(NULL);
2195             FAST_DISPATCH();
2196         }
2197 
2198         case TARGET(END_FINALLY): {
2199             PREDICTED(END_FINALLY);
2200             /* At the top of the stack are 1 or 6 values:
2201                Either:
2202                 - TOP = NULL or an integer
2203                or:
2204                 - (TOP, SECOND, THIRD) = exc_info()
2205                 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2206             */
2207             PyObject *exc = POP();
2208             if (exc == NULL) {
2209                 FAST_DISPATCH();
2210             }
2211             else if (PyLong_CheckExact(exc)) {
2212                 int ret = _PyLong_AsInt(exc);
2213                 Py_DECREF(exc);
2214                 if (ret == -1 && _PyErr_Occurred(tstate)) {
2215                     goto error;
2216                 }
2217                 JUMPTO(ret);
2218                 FAST_DISPATCH();
2219             }
2220             else {
2221                 assert(PyExceptionClass_Check(exc));
2222                 PyObject *val = POP();
2223                 PyObject *tb = POP();
2224                 _PyErr_Restore(tstate, exc, val, tb);
2225                 goto exception_unwind;
2226             }
2227         }
2228 
2229         case TARGET(END_ASYNC_FOR): {
2230             PyObject *exc = POP();
2231             assert(PyExceptionClass_Check(exc));
2232             if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2233                 PyTryBlock *b = PyFrame_BlockPop(f);
2234                 assert(b->b_type == EXCEPT_HANDLER);
2235                 Py_DECREF(exc);
2236                 UNWIND_EXCEPT_HANDLER(b);
2237                 Py_DECREF(POP());
2238                 JUMPBY(oparg);
2239                 FAST_DISPATCH();
2240             }
2241             else {
2242                 PyObject *val = POP();
2243                 PyObject *tb = POP();
2244                 _PyErr_Restore(tstate, exc, val, tb);
2245                 goto exception_unwind;
2246             }
2247         }
2248 
2249         case TARGET(LOAD_BUILD_CLASS): {
2250             _Py_IDENTIFIER(__build_class__);
2251 
2252             PyObject *bc;
2253             if (PyDict_CheckExact(f->f_builtins)) {
2254                 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
2255                 if (bc == NULL) {
2256                     if (!_PyErr_Occurred(tstate)) {
2257                         _PyErr_SetString(tstate, PyExc_NameError,
2258                                          "__build_class__ not found");
2259                     }
2260                     goto error;
2261                 }
2262                 Py_INCREF(bc);
2263             }
2264             else {
2265                 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2266                 if (build_class_str == NULL)
2267                     goto error;
2268                 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2269                 if (bc == NULL) {
2270                     if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2271                         _PyErr_SetString(tstate, PyExc_NameError,
2272                                          "__build_class__ not found");
2273                     goto error;
2274                 }
2275             }
2276             PUSH(bc);
2277             DISPATCH();
2278         }
2279 
2280         case TARGET(STORE_NAME): {
2281             PyObject *name = GETITEM(names, oparg);
2282             PyObject *v = POP();
2283             PyObject *ns = f->f_locals;
2284             int err;
2285             if (ns == NULL) {
2286                 _PyErr_Format(tstate, PyExc_SystemError,
2287                               "no locals found when storing %R", name);
2288                 Py_DECREF(v);
2289                 goto error;
2290             }
2291             if (PyDict_CheckExact(ns))
2292                 err = PyDict_SetItem(ns, name, v);
2293             else
2294                 err = PyObject_SetItem(ns, name, v);
2295             Py_DECREF(v);
2296             if (err != 0)
2297                 goto error;
2298             DISPATCH();
2299         }
2300 
2301         case TARGET(DELETE_NAME): {
2302             PyObject *name = GETITEM(names, oparg);
2303             PyObject *ns = f->f_locals;
2304             int err;
2305             if (ns == NULL) {
2306                 _PyErr_Format(tstate, PyExc_SystemError,
2307                               "no locals when deleting %R", name);
2308                 goto error;
2309             }
2310             err = PyObject_DelItem(ns, name);
2311             if (err != 0) {
2312                 format_exc_check_arg(tstate, PyExc_NameError,
2313                                      NAME_ERROR_MSG,
2314                                      name);
2315                 goto error;
2316             }
2317             DISPATCH();
2318         }
2319 
2320         case TARGET(UNPACK_SEQUENCE): {
2321             PREDICTED(UNPACK_SEQUENCE);
2322             PyObject *seq = POP(), *item, **items;
2323             if (PyTuple_CheckExact(seq) &&
2324                 PyTuple_GET_SIZE(seq) == oparg) {
2325                 items = ((PyTupleObject *)seq)->ob_item;
2326                 while (oparg--) {
2327                     item = items[oparg];
2328                     Py_INCREF(item);
2329                     PUSH(item);
2330                 }
2331             } else if (PyList_CheckExact(seq) &&
2332                        PyList_GET_SIZE(seq) == oparg) {
2333                 items = ((PyListObject *)seq)->ob_item;
2334                 while (oparg--) {
2335                     item = items[oparg];
2336                     Py_INCREF(item);
2337                     PUSH(item);
2338                 }
2339             } else if (unpack_iterable(tstate, seq, oparg, -1,
2340                                        stack_pointer + oparg)) {
2341                 STACK_GROW(oparg);
2342             } else {
2343                 /* unpack_iterable() raised an exception */
2344                 Py_DECREF(seq);
2345                 goto error;
2346             }
2347             Py_DECREF(seq);
2348             DISPATCH();
2349         }
2350 
2351         case TARGET(UNPACK_EX): {
2352             int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2353             PyObject *seq = POP();
2354 
2355             if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
2356                                 stack_pointer + totalargs)) {
2357                 stack_pointer += totalargs;
2358             } else {
2359                 Py_DECREF(seq);
2360                 goto error;
2361             }
2362             Py_DECREF(seq);
2363             DISPATCH();
2364         }
2365 
2366         case TARGET(STORE_ATTR): {
2367             PyObject *name = GETITEM(names, oparg);
2368             PyObject *owner = TOP();
2369             PyObject *v = SECOND();
2370             int err;
2371             STACK_SHRINK(2);
2372             err = PyObject_SetAttr(owner, name, v);
2373             Py_DECREF(v);
2374             Py_DECREF(owner);
2375             if (err != 0)
2376                 goto error;
2377             DISPATCH();
2378         }
2379 
2380         case TARGET(DELETE_ATTR): {
2381             PyObject *name = GETITEM(names, oparg);
2382             PyObject *owner = POP();
2383             int err;
2384             err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2385             Py_DECREF(owner);
2386             if (err != 0)
2387                 goto error;
2388             DISPATCH();
2389         }
2390 
2391         case TARGET(STORE_GLOBAL): {
2392             PyObject *name = GETITEM(names, oparg);
2393             PyObject *v = POP();
2394             int err;
2395             err = PyDict_SetItem(f->f_globals, name, v);
2396             Py_DECREF(v);
2397             if (err != 0)
2398                 goto error;
2399             DISPATCH();
2400         }
2401 
2402         case TARGET(DELETE_GLOBAL): {
2403             PyObject *name = GETITEM(names, oparg);
2404             int err;
2405             err = PyDict_DelItem(f->f_globals, name);
2406             if (err != 0) {
2407                 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2408                     format_exc_check_arg(tstate, PyExc_NameError,
2409                                          NAME_ERROR_MSG, name);
2410                 }
2411                 goto error;
2412             }
2413             DISPATCH();
2414         }
2415 
2416         case TARGET(LOAD_NAME): {
2417             PyObject *name = GETITEM(names, oparg);
2418             PyObject *locals = f->f_locals;
2419             PyObject *v;
2420             if (locals == NULL) {
2421                 _PyErr_Format(tstate, PyExc_SystemError,
2422                               "no locals when loading %R", name);
2423                 goto error;
2424             }
2425             if (PyDict_CheckExact(locals)) {
2426                 v = PyDict_GetItemWithError(locals, name);
2427                 if (v != NULL) {
2428                     Py_INCREF(v);
2429                 }
2430                 else if (_PyErr_Occurred(tstate)) {
2431                     goto error;
2432                 }
2433             }
2434             else {
2435                 v = PyObject_GetItem(locals, name);
2436                 if (v == NULL) {
2437                     if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2438                         goto error;
2439                     _PyErr_Clear(tstate);
2440                 }
2441             }
2442             if (v == NULL) {
2443                 v = PyDict_GetItemWithError(f->f_globals, name);
2444                 if (v != NULL) {
2445                     Py_INCREF(v);
2446                 }
2447                 else if (_PyErr_Occurred(tstate)) {
2448                     goto error;
2449                 }
2450                 else {
2451                     if (PyDict_CheckExact(f->f_builtins)) {
2452                         v = PyDict_GetItemWithError(f->f_builtins, name);
2453                         if (v == NULL) {
2454                             if (!_PyErr_Occurred(tstate)) {
2455                                 format_exc_check_arg(
2456                                         tstate, PyExc_NameError,
2457                                         NAME_ERROR_MSG, name);
2458                             }
2459                             goto error;
2460                         }
2461                         Py_INCREF(v);
2462                     }
2463                     else {
2464                         v = PyObject_GetItem(f->f_builtins, name);
2465                         if (v == NULL) {
2466                             if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2467                                 format_exc_check_arg(
2468                                             tstate, PyExc_NameError,
2469                                             NAME_ERROR_MSG, name);
2470                             }
2471                             goto error;
2472                         }
2473                     }
2474                 }
2475             }
2476             PUSH(v);
2477             DISPATCH();
2478         }
2479 
2480         case TARGET(LOAD_GLOBAL): {
2481             PyObject *name;
2482             PyObject *v;
2483             if (PyDict_CheckExact(f->f_globals)
2484                 && PyDict_CheckExact(f->f_builtins))
2485             {
2486                 OPCACHE_CHECK();
2487                 if (co_opcache != NULL && co_opcache->optimized > 0) {
2488                     _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2489 
2490                     if (lg->globals_ver ==
2491                             ((PyDictObject *)f->f_globals)->ma_version_tag
2492                         && lg->builtins_ver ==
2493                            ((PyDictObject *)f->f_builtins)->ma_version_tag)
2494                     {
2495                         PyObject *ptr = lg->ptr;
2496                         OPCACHE_STAT_GLOBAL_HIT();
2497                         assert(ptr != NULL);
2498                         Py_INCREF(ptr);
2499                         PUSH(ptr);
2500                         DISPATCH();
2501                     }
2502                 }
2503 
2504                 name = GETITEM(names, oparg);
2505                 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
2506                                        (PyDictObject *)f->f_builtins,
2507                                        name);
2508                 if (v == NULL) {
2509                     if (!_PyErr_OCCURRED()) {
2510                         /* _PyDict_LoadGlobal() returns NULL without raising
2511                          * an exception if the key doesn't exist */
2512                         format_exc_check_arg(tstate, PyExc_NameError,
2513                                              NAME_ERROR_MSG, name);
2514                     }
2515                     goto error;
2516                 }
2517 
2518                 if (co_opcache != NULL) {
2519                     _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2520 
2521                     if (co_opcache->optimized == 0) {
2522                         /* Wasn't optimized before. */
2523                         OPCACHE_STAT_GLOBAL_OPT();
2524                     } else {
2525                         OPCACHE_STAT_GLOBAL_MISS();
2526                     }
2527 
2528                     co_opcache->optimized = 1;
2529                     lg->globals_ver =
2530                         ((PyDictObject *)f->f_globals)->ma_version_tag;
2531                     lg->builtins_ver =
2532                         ((PyDictObject *)f->f_builtins)->ma_version_tag;
2533                     lg->ptr = v; /* borrowed */
2534                 }
2535 
2536                 Py_INCREF(v);
2537             }
2538             else {
2539                 /* Slow-path if globals or builtins is not a dict */
2540 
2541                 /* namespace 1: globals */
2542                 name = GETITEM(names, oparg);
2543                 v = PyObject_GetItem(f->f_globals, name);
2544                 if (v == NULL) {
2545                     if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2546                         goto error;
2547                     }
2548                     _PyErr_Clear(tstate);
2549 
2550                     /* namespace 2: builtins */
2551                     v = PyObject_GetItem(f->f_builtins, name);
2552                     if (v == NULL) {
2553                         if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2554                             format_exc_check_arg(
2555                                         tstate, PyExc_NameError,
2556                                         NAME_ERROR_MSG, name);
2557                         }
2558                         goto error;
2559                     }
2560                 }
2561             }
2562             PUSH(v);
2563             DISPATCH();
2564         }
2565 
2566         case TARGET(DELETE_FAST): {
2567             PyObject *v = GETLOCAL(oparg);
2568             if (v != NULL) {
2569                 SETLOCAL(oparg, NULL);
2570                 DISPATCH();
2571             }
2572             format_exc_check_arg(
2573                 tstate, PyExc_UnboundLocalError,
2574                 UNBOUNDLOCAL_ERROR_MSG,
2575                 PyTuple_GetItem(co->co_varnames, oparg)
2576                 );
2577             goto error;
2578         }
2579 
2580         case TARGET(DELETE_DEREF): {
2581             PyObject *cell = freevars[oparg];
2582             PyObject *oldobj = PyCell_GET(cell);
2583             if (oldobj != NULL) {
2584                 PyCell_SET(cell, NULL);
2585                 Py_DECREF(oldobj);
2586                 DISPATCH();
2587             }
2588             format_exc_unbound(tstate, co, oparg);
2589             goto error;
2590         }
2591 
2592         case TARGET(LOAD_CLOSURE): {
2593             PyObject *cell = freevars[oparg];
2594             Py_INCREF(cell);
2595             PUSH(cell);
2596             DISPATCH();
2597         }
2598 
2599         case TARGET(LOAD_CLASSDEREF): {
2600             PyObject *name, *value, *locals = f->f_locals;
2601             Py_ssize_t idx;
2602             assert(locals);
2603             assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2604             idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2605             assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2606             name = PyTuple_GET_ITEM(co->co_freevars, idx);
2607             if (PyDict_CheckExact(locals)) {
2608                 value = PyDict_GetItemWithError(locals, name);
2609                 if (value != NULL) {
2610                     Py_INCREF(value);
2611                 }
2612                 else if (_PyErr_Occurred(tstate)) {
2613                     goto error;
2614                 }
2615             }
2616             else {
2617                 value = PyObject_GetItem(locals, name);
2618                 if (value == NULL) {
2619                     if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2620                         goto error;
2621                     }
2622                     _PyErr_Clear(tstate);
2623                 }
2624             }
2625             if (!value) {
2626                 PyObject *cell = freevars[oparg];
2627                 value = PyCell_GET(cell);
2628                 if (value == NULL) {
2629                     format_exc_unbound(tstate, co, oparg);
2630                     goto error;
2631                 }
2632                 Py_INCREF(value);
2633             }
2634             PUSH(value);
2635             DISPATCH();
2636         }
2637 
2638         case TARGET(LOAD_DEREF): {
2639             PyObject *cell = freevars[oparg];
2640             PyObject *value = PyCell_GET(cell);
2641             if (value == NULL) {
2642                 format_exc_unbound(tstate, co, oparg);
2643                 goto error;
2644             }
2645             Py_INCREF(value);
2646             PUSH(value);
2647             DISPATCH();
2648         }
2649 
2650         case TARGET(STORE_DEREF): {
2651             PyObject *v = POP();
2652             PyObject *cell = freevars[oparg];
2653             PyObject *oldobj = PyCell_GET(cell);
2654             PyCell_SET(cell, v);
2655             Py_XDECREF(oldobj);
2656             DISPATCH();
2657         }
2658 
2659         case TARGET(BUILD_STRING): {
2660             PyObject *str;
2661             PyObject *empty = PyUnicode_New(0, 0);
2662             if (empty == NULL) {
2663                 goto error;
2664             }
2665             str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2666             Py_DECREF(empty);
2667             if (str == NULL)
2668                 goto error;
2669             while (--oparg >= 0) {
2670                 PyObject *item = POP();
2671                 Py_DECREF(item);
2672             }
2673             PUSH(str);
2674             DISPATCH();
2675         }
2676 
2677         case TARGET(BUILD_TUPLE): {
2678             PyObject *tup = PyTuple_New(oparg);
2679             if (tup == NULL)
2680                 goto error;
2681             while (--oparg >= 0) {
2682                 PyObject *item = POP();
2683                 PyTuple_SET_ITEM(tup, oparg, item);
2684             }
2685             PUSH(tup);
2686             DISPATCH();
2687         }
2688 
2689         case TARGET(BUILD_LIST): {
2690             PyObject *list =  PyList_New(oparg);
2691             if (list == NULL)
2692                 goto error;
2693             while (--oparg >= 0) {
2694                 PyObject *item = POP();
2695                 PyList_SET_ITEM(list, oparg, item);
2696             }
2697             PUSH(list);
2698             DISPATCH();
2699         }
2700 
2701         case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL):
2702         case TARGET(BUILD_TUPLE_UNPACK):
2703         case TARGET(BUILD_LIST_UNPACK): {
2704             int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
2705             Py_ssize_t i;
2706             PyObject *sum = PyList_New(0);
2707             PyObject *return_value;
2708 
2709             if (sum == NULL)
2710                 goto error;
2711 
2712             for (i = oparg; i > 0; i--) {
2713                 PyObject *none_val;
2714 
2715                 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2716                 if (none_val == NULL) {
2717                     if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
2718                         _PyErr_ExceptionMatches(tstate, PyExc_TypeError))
2719                     {
2720                         check_args_iterable(tstate, PEEK(1 + oparg), PEEK(i));
2721                     }
2722                     Py_DECREF(sum);
2723                     goto error;
2724                 }
2725                 Py_DECREF(none_val);
2726             }
2727 
2728             if (convert_to_tuple) {
2729                 return_value = PyList_AsTuple(sum);
2730                 Py_DECREF(sum);
2731                 if (return_value == NULL)
2732                     goto error;
2733             }
2734             else {
2735                 return_value = sum;
2736             }
2737 
2738             while (oparg--)
2739                 Py_DECREF(POP());
2740             PUSH(return_value);
2741             DISPATCH();
2742         }
2743 
2744         case TARGET(BUILD_SET): {
2745             PyObject *set = PySet_New(NULL);
2746             int err = 0;
2747             int i;
2748             if (set == NULL)
2749                 goto error;
2750             for (i = oparg; i > 0; i--) {
2751                 PyObject *item = PEEK(i);
2752                 if (err == 0)
2753                     err = PySet_Add(set, item);
2754                 Py_DECREF(item);
2755             }
2756             STACK_SHRINK(oparg);
2757             if (err != 0) {
2758                 Py_DECREF(set);
2759                 goto error;
2760             }
2761             PUSH(set);
2762             DISPATCH();
2763         }
2764 
2765         case TARGET(BUILD_SET_UNPACK): {
2766             Py_ssize_t i;
2767             PyObject *sum = PySet_New(NULL);
2768             if (sum == NULL)
2769                 goto error;
2770 
2771             for (i = oparg; i > 0; i--) {
2772                 if (_PySet_Update(sum, PEEK(i)) < 0) {
2773                     Py_DECREF(sum);
2774                     goto error;
2775                 }
2776             }
2777 
2778             while (oparg--)
2779                 Py_DECREF(POP());
2780             PUSH(sum);
2781             DISPATCH();
2782         }
2783 
2784         case TARGET(BUILD_MAP): {
2785             Py_ssize_t i;
2786             PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2787             if (map == NULL)
2788                 goto error;
2789             for (i = oparg; i > 0; i--) {
2790                 int err;
2791                 PyObject *key = PEEK(2*i);
2792                 PyObject *value = PEEK(2*i - 1);
2793                 err = PyDict_SetItem(map, key, value);
2794                 if (err != 0) {
2795                     Py_DECREF(map);
2796                     goto error;
2797                 }
2798             }
2799 
2800             while (oparg--) {
2801                 Py_DECREF(POP());
2802                 Py_DECREF(POP());
2803             }
2804             PUSH(map);
2805             DISPATCH();
2806         }
2807 
2808         case TARGET(SETUP_ANNOTATIONS): {
2809             _Py_IDENTIFIER(__annotations__);
2810             int err;
2811             PyObject *ann_dict;
2812             if (f->f_locals == NULL) {
2813                 _PyErr_Format(tstate, PyExc_SystemError,
2814                               "no locals found when setting up annotations");
2815                 goto error;
2816             }
2817             /* check if __annotations__ in locals()... */
2818             if (PyDict_CheckExact(f->f_locals)) {
2819                 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
2820                                              &PyId___annotations__);
2821                 if (ann_dict == NULL) {
2822                     if (_PyErr_Occurred(tstate)) {
2823                         goto error;
2824                     }
2825                     /* ...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) {
2842                     goto error;
2843                 }
2844                 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2845                 if (ann_dict == NULL) {
2846                     if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2847                         goto error;
2848                     }
2849                     _PyErr_Clear(tstate);
2850                     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 
2867         case TARGET(BUILD_CONST_KEY_MAP): {
2868             Py_ssize_t i;
2869             PyObject *map;
2870             PyObject *keys = TOP();
2871             if (!PyTuple_CheckExact(keys) ||
2872                 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2873                 _PyErr_SetString(tstate, PyExc_SystemError,
2874                                  "bad BUILD_CONST_KEY_MAP keys argument");
2875                 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 
2900         case TARGET(BUILD_MAP_UNPACK): {
2901             Py_ssize_t i;
2902             PyObject *sum = PyDict_New();
2903             if (sum == NULL)
2904                 goto error;
2905 
2906             for (i = oparg; i > 0; i--) {
2907                 PyObject *arg = PEEK(i);
2908                 if (PyDict_Update(sum, arg) < 0) {
2909                     if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2910                         _PyErr_Format(tstate, PyExc_TypeError,
2911                                       "'%.200s' object is not a mapping",
2912                                       arg->ob_type->tp_name);
2913                     }
2914                     Py_DECREF(sum);
2915                     goto error;
2916                 }
2917             }
2918 
2919             while (oparg--)
2920                 Py_DECREF(POP());
2921             PUSH(sum);
2922             DISPATCH();
2923         }
2924 
2925         case TARGET(BUILD_MAP_UNPACK_WITH_CALL): {
2926             Py_ssize_t i;
2927             PyObject *sum = PyDict_New();
2928             if (sum == NULL)
2929                 goto error;
2930 
2931             for (i = oparg; i > 0; i--) {
2932                 PyObject *arg = PEEK(i);
2933                 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
2934                     Py_DECREF(sum);
2935                     format_kwargs_error(tstate, PEEK(2 + oparg), arg);
2936                     goto error;
2937                 }
2938             }
2939 
2940             while (oparg--)
2941                 Py_DECREF(POP());
2942             PUSH(sum);
2943             DISPATCH();
2944         }
2945 
2946         case TARGET(MAP_ADD): {
2947             PyObject *value = TOP();
2948             PyObject *key = SECOND();
2949             PyObject *map;
2950             int err;
2951             STACK_SHRINK(2);
2952             map = PEEK(oparg);                      /* dict */
2953             assert(PyDict_CheckExact(map));
2954             err = PyDict_SetItem(map, key, value);  /* map[key] = value */
2955             Py_DECREF(value);
2956             Py_DECREF(key);
2957             if (err != 0)
2958                 goto error;
2959             PREDICT(JUMP_ABSOLUTE);
2960             DISPATCH();
2961         }
2962 
2963         case TARGET(LOAD_ATTR): {
2964             PyObject *name = GETITEM(names, oparg);
2965             PyObject *owner = TOP();
2966             PyObject *res = PyObject_GetAttr(owner, name);
2967             Py_DECREF(owner);
2968             SET_TOP(res);
2969             if (res == NULL)
2970                 goto error;
2971             DISPATCH();
2972         }
2973 
2974         case TARGET(COMPARE_OP): {
2975             PyObject *right = POP();
2976             PyObject *left = TOP();
2977             PyObject *res = cmp_outcome(tstate, oparg, left, right);
2978             Py_DECREF(left);
2979             Py_DECREF(right);
2980             SET_TOP(res);
2981             if (res == NULL)
2982                 goto error;
2983             PREDICT(POP_JUMP_IF_FALSE);
2984             PREDICT(POP_JUMP_IF_TRUE);
2985             DISPATCH();
2986         }
2987 
2988         case TARGET(IMPORT_NAME): {
2989             PyObject *name = GETITEM(names, oparg);
2990             PyObject *fromlist = POP();
2991             PyObject *level = TOP();
2992             PyObject *res;
2993             res = import_name(tstate, f, name, fromlist, level);
2994             Py_DECREF(level);
2995             Py_DECREF(fromlist);
2996             SET_TOP(res);
2997             if (res == NULL)
2998                 goto error;
2999             DISPATCH();
3000         }
3001 
3002         case TARGET(IMPORT_STAR): {
3003             PyObject *from = POP(), *locals;
3004             int err;
3005             if (PyFrame_FastToLocalsWithError(f) < 0) {
3006                 Py_DECREF(from);
3007                 goto error;
3008             }
3009 
3010             locals = f->f_locals;
3011             if (locals == NULL) {
3012                 _PyErr_SetString(tstate, PyExc_SystemError,
3013                                  "no locals found during 'import *'");
3014                 Py_DECREF(from);
3015                 goto error;
3016             }
3017             err = import_all_from(tstate, locals, from);
3018             PyFrame_LocalsToFast(f, 0);
3019             Py_DECREF(from);
3020             if (err != 0)
3021                 goto error;
3022             DISPATCH();
3023         }
3024 
3025         case TARGET(IMPORT_FROM): {
3026             PyObject *name = GETITEM(names, oparg);
3027             PyObject *from = TOP();
3028             PyObject *res;
3029             res = import_from(tstate, from, name);
3030             PUSH(res);
3031             if (res == NULL)
3032                 goto error;
3033             DISPATCH();
3034         }
3035 
3036         case TARGET(JUMP_FORWARD): {
3037             JUMPBY(oparg);
3038             FAST_DISPATCH();
3039         }
3040 
3041         case TARGET(POP_JUMP_IF_FALSE): {
3042             PREDICTED(POP_JUMP_IF_FALSE);
3043             PyObject *cond = POP();
3044             int err;
3045             if (cond == Py_True) {
3046                 Py_DECREF(cond);
3047                 FAST_DISPATCH();
3048             }
3049             if (cond == Py_False) {
3050                 Py_DECREF(cond);
3051                 JUMPTO(oparg);
3052                 FAST_DISPATCH();
3053             }
3054             err = PyObject_IsTrue(cond);
3055             Py_DECREF(cond);
3056             if (err > 0)
3057                 ;
3058             else if (err == 0)
3059                 JUMPTO(oparg);
3060             else
3061                 goto error;
3062             DISPATCH();
3063         }
3064 
3065         case TARGET(POP_JUMP_IF_TRUE): {
3066             PREDICTED(POP_JUMP_IF_TRUE);
3067             PyObject *cond = POP();
3068             int err;
3069             if (cond == Py_False) {
3070                 Py_DECREF(cond);
3071                 FAST_DISPATCH();
3072             }
3073             if (cond == Py_True) {
3074                 Py_DECREF(cond);
3075                 JUMPTO(oparg);
3076                 FAST_DISPATCH();
3077             }
3078             err = PyObject_IsTrue(cond);
3079             Py_DECREF(cond);
3080             if (err > 0) {
3081                 JUMPTO(oparg);
3082             }
3083             else if (err == 0)
3084                 ;
3085             else
3086                 goto error;
3087             DISPATCH();
3088         }
3089 
3090         case TARGET(JUMP_IF_FALSE_OR_POP): {
3091             PyObject *cond = TOP();
3092             int err;
3093             if (cond == Py_True) {
3094                 STACK_SHRINK(1);
3095                 Py_DECREF(cond);
3096                 FAST_DISPATCH();
3097             }
3098             if (cond == Py_False) {
3099                 JUMPTO(oparg);
3100                 FAST_DISPATCH();
3101             }
3102             err = PyObject_IsTrue(cond);
3103             if (err > 0) {
3104                 STACK_SHRINK(1);
3105                 Py_DECREF(cond);
3106             }
3107             else if (err == 0)
3108                 JUMPTO(oparg);
3109             else
3110                 goto error;
3111             DISPATCH();
3112         }
3113 
3114         case TARGET(JUMP_IF_TRUE_OR_POP): {
3115             PyObject *cond = TOP();
3116             int err;
3117             if (cond == Py_False) {
3118                 STACK_SHRINK(1);
3119                 Py_DECREF(cond);
3120                 FAST_DISPATCH();
3121             }
3122             if (cond == Py_True) {
3123                 JUMPTO(oparg);
3124                 FAST_DISPATCH();
3125             }
3126             err = PyObject_IsTrue(cond);
3127             if (err > 0) {
3128                 JUMPTO(oparg);
3129             }
3130             else if (err == 0) {
3131                 STACK_SHRINK(1);
3132                 Py_DECREF(cond);
3133             }
3134             else
3135                 goto error;
3136             DISPATCH();
3137         }
3138 
3139         case TARGET(JUMP_ABSOLUTE): {
3140             PREDICTED(JUMP_ABSOLUTE);
3141             JUMPTO(oparg);
3142 #if FAST_LOOPS
3143             /* Enabling this path speeds-up all while and for-loops by bypassing
3144                the per-loop checks for signals.  By default, this should be turned-off
3145                because it prevents detection of a control-break in tight loops like
3146                "while 1: pass".  Compile with this option turned-on when you need
3147                the speed-up and do not need break checking inside tight loops (ones
3148                that contain only instructions ending with FAST_DISPATCH).
3149             */
3150             FAST_DISPATCH();
3151 #else
3152             DISPATCH();
3153 #endif
3154         }
3155 
3156         case TARGET(GET_ITER): {
3157             /* before: [obj]; after [getiter(obj)] */
3158             PyObject *iterable = TOP();
3159             PyObject *iter = PyObject_GetIter(iterable);
3160             Py_DECREF(iterable);
3161             SET_TOP(iter);
3162             if (iter == NULL)
3163                 goto error;
3164             PREDICT(FOR_ITER);
3165             PREDICT(CALL_FUNCTION);
3166             DISPATCH();
3167         }
3168 
3169         case TARGET(GET_YIELD_FROM_ITER): {
3170             /* before: [obj]; after [getiter(obj)] */
3171             PyObject *iterable = TOP();
3172             PyObject *iter;
3173             if (PyCoro_CheckExact(iterable)) {
3174                 /* `iterable` is a coroutine */
3175                 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3176                     /* and it is used in a 'yield from' expression of a
3177                        regular generator. */
3178                     Py_DECREF(iterable);
3179                     SET_TOP(NULL);
3180                     _PyErr_SetString(tstate, PyExc_TypeError,
3181                                      "cannot 'yield from' a coroutine object "
3182                                      "in a non-coroutine generator");
3183                     goto error;
3184                 }
3185             }
3186             else if (!PyGen_CheckExact(iterable)) {
3187                 /* `iterable` is not a generator. */
3188                 iter = PyObject_GetIter(iterable);
3189                 Py_DECREF(iterable);
3190                 SET_TOP(iter);
3191                 if (iter == NULL)
3192                     goto error;
3193             }
3194             PREDICT(LOAD_CONST);
3195             DISPATCH();
3196         }
3197 
3198         case TARGET(FOR_ITER): {
3199             PREDICTED(FOR_ITER);
3200             /* before: [iter]; after: [iter, iter()] *or* [] */
3201             PyObject *iter = TOP();
3202             PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3203             if (next != NULL) {
3204                 PUSH(next);
3205                 PREDICT(STORE_FAST);
3206                 PREDICT(UNPACK_SEQUENCE);
3207                 DISPATCH();
3208             }
3209             if (_PyErr_Occurred(tstate)) {
3210                 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
3211                     goto error;
3212                 }
3213                 else if (tstate->c_tracefunc != NULL) {
3214                     call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
3215                 }
3216                 _PyErr_Clear(tstate);
3217             }
3218             /* iterator ended normally */
3219             STACK_SHRINK(1);
3220             Py_DECREF(iter);
3221             JUMPBY(oparg);
3222             PREDICT(POP_BLOCK);
3223             DISPATCH();
3224         }
3225 
3226         case TARGET(SETUP_FINALLY): {
3227             /* NOTE: If you add any new block-setup opcodes that
3228                are not try/except/finally handlers, you may need
3229                to update the PyGen_NeedsFinalizing() function.
3230                */
3231 
3232             PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3233                                STACK_LEVEL());
3234             DISPATCH();
3235         }
3236 
3237         case TARGET(BEFORE_ASYNC_WITH): {
3238             _Py_IDENTIFIER(__aexit__);
3239             _Py_IDENTIFIER(__aenter__);
3240 
3241             PyObject *mgr = TOP();
3242             PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__),
3243                      *enter;
3244             PyObject *res;
3245             if (exit == NULL)
3246                 goto error;
3247             SET_TOP(exit);
3248             enter = special_lookup(tstate, mgr, &PyId___aenter__);
3249             Py_DECREF(mgr);
3250             if (enter == NULL)
3251                 goto error;
3252             res = _PyObject_CallNoArg(enter);
3253             Py_DECREF(enter);
3254             if (res == NULL)
3255                 goto error;
3256             PUSH(res);
3257             PREDICT(GET_AWAITABLE);
3258             DISPATCH();
3259         }
3260 
3261         case TARGET(SETUP_ASYNC_WITH): {
3262             PyObject *res = POP();
3263             /* Setup the finally block before pushing the result
3264                of __aenter__ on the stack. */
3265             PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3266                                STACK_LEVEL());
3267             PUSH(res);
3268             DISPATCH();
3269         }
3270 
3271         case TARGET(SETUP_WITH): {
3272             _Py_IDENTIFIER(__exit__);
3273             _Py_IDENTIFIER(__enter__);
3274             PyObject *mgr = TOP();
3275             PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
3276             PyObject *res;
3277             if (enter == NULL) {
3278                 goto error;
3279             }
3280             PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
3281             if (exit == NULL) {
3282                 Py_DECREF(enter);
3283                 goto error;
3284             }
3285             SET_TOP(exit);
3286             Py_DECREF(mgr);
3287             res = _PyObject_CallNoArg(enter);
3288             Py_DECREF(enter);
3289             if (res == NULL)
3290                 goto error;
3291             /* Setup the finally block before pushing the result
3292                of __enter__ on the stack. */
3293             PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3294                                STACK_LEVEL());
3295 
3296             PUSH(res);
3297             DISPATCH();
3298         }
3299 
3300         case TARGET(WITH_CLEANUP_START): {
3301             /* At the top of the stack are 1 or 6 values indicating
3302                how/why we entered the finally clause:
3303                - TOP = NULL
3304                - (TOP, SECOND, THIRD) = exc_info()
3305                  (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
3306                Below them is EXIT, the context.__exit__ or context.__aexit__
3307                bound method.
3308                In the first case, we must call
3309                  EXIT(None, None, None)
3310                otherwise we must call
3311                  EXIT(TOP, SECOND, THIRD)
3312 
3313                In the first case, we remove EXIT from the
3314                stack, leaving TOP, and push TOP on the stack.
3315                Otherwise we shift the bottom 3 values of the
3316                stack down, replace the empty spot with NULL, and push
3317                None on the stack.
3318 
3319                Finally we push the result of the call.
3320             */
3321             PyObject *stack[3];
3322             PyObject *exit_func;
3323             PyObject *exc, *val, *tb, *res;
3324 
3325             val = tb = Py_None;
3326             exc = TOP();
3327             if (exc == NULL) {
3328                 STACK_SHRINK(1);
3329                 exit_func = TOP();
3330                 SET_TOP(exc);
3331                 exc = Py_None;
3332             }
3333             else {
3334                 assert(PyExceptionClass_Check(exc));
3335                 PyObject *tp2, *exc2, *tb2;
3336                 PyTryBlock *block;
3337                 val = SECOND();
3338                 tb = THIRD();
3339                 tp2 = FOURTH();
3340                 exc2 = PEEK(5);
3341                 tb2 = PEEK(6);
3342                 exit_func = PEEK(7);
3343                 SET_VALUE(7, tb2);
3344                 SET_VALUE(6, exc2);
3345                 SET_VALUE(5, tp2);
3346                 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3347                 SET_FOURTH(NULL);
3348                 /* We just shifted the stack down, so we have
3349                    to tell the except handler block that the
3350                    values are lower than it expects. */
3351                 assert(f->f_iblock > 0);
3352                 block = &f->f_blockstack[f->f_iblock - 1];
3353                 assert(block->b_type == EXCEPT_HANDLER);
3354                 assert(block->b_level > 0);
3355                 block->b_level--;
3356             }
3357 
3358             stack[0] = exc;
3359             stack[1] = val;
3360             stack[2] = tb;
3361             res = _PyObject_FastCall(exit_func, stack, 3);
3362             Py_DECREF(exit_func);
3363             if (res == NULL)
3364                 goto error;
3365 
3366             Py_INCREF(exc); /* Duplicating the exception on the stack */
3367             PUSH(exc);
3368             PUSH(res);
3369             PREDICT(WITH_CLEANUP_FINISH);
3370             DISPATCH();
3371         }
3372 
3373         case TARGET(WITH_CLEANUP_FINISH): {
3374             PREDICTED(WITH_CLEANUP_FINISH);
3375             /* TOP = the result of calling the context.__exit__ bound method
3376                SECOND = either None or exception type
3377 
3378                If SECOND is None below is NULL or the return address,
3379                otherwise below are 7 values representing an exception.
3380             */
3381             PyObject *res = POP();
3382             PyObject *exc = POP();
3383             int err;
3384 
3385             if (exc != Py_None)
3386                 err = PyObject_IsTrue(res);
3387             else
3388                 err = 0;
3389 
3390             Py_DECREF(res);
3391             Py_DECREF(exc);
3392 
3393             if (err < 0)
3394                 goto error;
3395             else if (err > 0) {
3396                 /* There was an exception and a True return.
3397                  * We must manually unwind the EXCEPT_HANDLER block
3398                  * which was created when the exception was caught,
3399                  * otherwise the stack will be in an inconsistent state.
3400                  */
3401                 PyTryBlock *b = PyFrame_BlockPop(f);
3402                 assert(b->b_type == EXCEPT_HANDLER);
3403                 UNWIND_EXCEPT_HANDLER(b);
3404                 PUSH(NULL);
3405             }
3406             PREDICT(END_FINALLY);
3407             DISPATCH();
3408         }
3409 
3410         case TARGET(LOAD_METHOD): {
3411             /* Designed to work in tandem with CALL_METHOD. */
3412             PyObject *name = GETITEM(names, oparg);
3413             PyObject *obj = TOP();
3414             PyObject *meth = NULL;
3415 
3416             int meth_found = _PyObject_GetMethod(obj, name, &meth);
3417 
3418             if (meth == NULL) {
3419                 /* Most likely attribute wasn't found. */
3420                 goto error;
3421             }
3422 
3423             if (meth_found) {
3424                 /* We can bypass temporary bound method object.
3425                    meth is unbound method and obj is self.
3426 
3427                    meth | self | arg1 | ... | argN
3428                  */
3429                 SET_TOP(meth);
3430                 PUSH(obj);  // self
3431             }
3432             else {
3433                 /* meth is not an unbound method (but a regular attr, or
3434                    something was returned by a descriptor protocol).  Set
3435                    the second element of the stack to NULL, to signal
3436                    CALL_METHOD that it's not a method call.
3437 
3438                    NULL | meth | arg1 | ... | argN
3439                 */
3440                 SET_TOP(NULL);
3441                 Py_DECREF(obj);
3442                 PUSH(meth);
3443             }
3444             DISPATCH();
3445         }
3446 
3447         case TARGET(CALL_METHOD): {
3448             /* Designed to work in tamdem with LOAD_METHOD. */
3449             PyObject **sp, *res, *meth;
3450 
3451             sp = stack_pointer;
3452 
3453             meth = PEEK(oparg + 2);
3454             if (meth == NULL) {
3455                 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3456                    a method call.
3457 
3458                    Stack layout:
3459 
3460                        ... | NULL | callable | arg1 | ... | argN
3461                                                             ^- TOP()
3462                                                ^- (-oparg)
3463                                     ^- (-oparg-1)
3464                              ^- (-oparg-2)
3465 
3466                    `callable` will be POPed by call_function.
3467                    NULL will will be POPed manually later.
3468                 */
3469                 res = call_function(tstate, &sp, oparg, NULL);
3470                 stack_pointer = sp;
3471                 (void)POP(); /* POP the NULL. */
3472             }
3473             else {
3474                 /* This is a method call.  Stack layout:
3475 
3476                      ... | method | self | arg1 | ... | argN
3477                                                         ^- TOP()
3478                                            ^- (-oparg)
3479                                     ^- (-oparg-1)
3480                            ^- (-oparg-2)
3481 
3482                   `self` and `method` will be POPed by call_function.
3483                   We'll be passing `oparg + 1` to call_function, to
3484                   make it accept the `self` as a first argument.
3485                 */
3486                 res = call_function(tstate, &sp, oparg + 1, NULL);
3487                 stack_pointer = sp;
3488             }
3489 
3490             PUSH(res);
3491             if (res == NULL)
3492                 goto error;
3493             DISPATCH();
3494         }
3495 
3496         case TARGET(CALL_FUNCTION): {
3497             PREDICTED(CALL_FUNCTION);
3498             PyObject **sp, *res;
3499             sp = stack_pointer;
3500             res = call_function(tstate, &sp, oparg, NULL);
3501             stack_pointer = sp;
3502             PUSH(res);
3503             if (res == NULL) {
3504                 goto error;
3505             }
3506             DISPATCH();
3507         }
3508 
3509         case TARGET(CALL_FUNCTION_KW): {
3510             PyObject **sp, *res, *names;
3511 
3512             names = POP();
3513             assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
3514             sp = stack_pointer;
3515             res = call_function(tstate, &sp, oparg, names);
3516             stack_pointer = sp;
3517             PUSH(res);
3518             Py_DECREF(names);
3519 
3520             if (res == NULL) {
3521                 goto error;
3522             }
3523             DISPATCH();
3524         }
3525 
3526         case TARGET(CALL_FUNCTION_EX): {
3527             PyObject *func, *callargs, *kwargs = NULL, *result;
3528             if (oparg & 0x01) {
3529                 kwargs = POP();
3530                 if (!PyDict_CheckExact(kwargs)) {
3531                     PyObject *d = PyDict_New();
3532                     if (d == NULL)
3533                         goto error;
3534                     if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
3535                         Py_DECREF(d);
3536                         format_kwargs_error(tstate, SECOND(), kwargs);
3537                         Py_DECREF(kwargs);
3538                         goto error;
3539                     }
3540                     Py_DECREF(kwargs);
3541                     kwargs = d;
3542                 }
3543                 assert(PyDict_CheckExact(kwargs));
3544             }
3545             callargs = POP();
3546             func = TOP();
3547             if (!PyTuple_CheckExact(callargs)) {
3548                 if (check_args_iterable(tstate, func, callargs) < 0) {
3549                     Py_DECREF(callargs);
3550                     goto error;
3551                 }
3552                 Py_SETREF(callargs, PySequence_Tuple(callargs));
3553                 if (callargs == NULL) {
3554                     goto error;
3555                 }
3556             }
3557             assert(PyTuple_CheckExact(callargs));
3558 
3559             result = do_call_core(tstate, func, callargs, kwargs);
3560             Py_DECREF(func);
3561             Py_DECREF(callargs);
3562             Py_XDECREF(kwargs);
3563 
3564             SET_TOP(result);
3565             if (result == NULL) {
3566                 goto error;
3567             }
3568             DISPATCH();
3569         }
3570 
3571         case TARGET(MAKE_FUNCTION): {
3572             PyObject *qualname = POP();
3573             PyObject *codeobj = POP();
3574             PyFunctionObject *func = (PyFunctionObject *)
3575                 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
3576 
3577             Py_DECREF(codeobj);
3578             Py_DECREF(qualname);
3579             if (func == NULL) {
3580                 goto error;
3581             }
3582 
3583             if (oparg & 0x08) {
3584                 assert(PyTuple_CheckExact(TOP()));
3585                 func ->func_closure = POP();
3586             }
3587             if (oparg & 0x04) {
3588                 assert(PyDict_CheckExact(TOP()));
3589                 func->func_annotations = POP();
3590             }
3591             if (oparg & 0x02) {
3592                 assert(PyDict_CheckExact(TOP()));
3593                 func->func_kwdefaults = POP();
3594             }
3595             if (oparg & 0x01) {
3596                 assert(PyTuple_CheckExact(TOP()));
3597                 func->func_defaults = POP();
3598             }
3599 
3600             PUSH((PyObject *)func);
3601             DISPATCH();
3602         }
3603 
3604         case TARGET(BUILD_SLICE): {
3605             PyObject *start, *stop, *step, *slice;
3606             if (oparg == 3)
3607                 step = POP();
3608             else
3609                 step = NULL;
3610             stop = POP();
3611             start = TOP();
3612             slice = PySlice_New(start, stop, step);
3613             Py_DECREF(start);
3614             Py_DECREF(stop);
3615             Py_XDECREF(step);
3616             SET_TOP(slice);
3617             if (slice == NULL)
3618                 goto error;
3619             DISPATCH();
3620         }
3621 
3622         case TARGET(FORMAT_VALUE): {
3623             /* Handles f-string value formatting. */
3624             PyObject *result;
3625             PyObject *fmt_spec;
3626             PyObject *value;
3627             PyObject *(*conv_fn)(PyObject *);
3628             int which_conversion = oparg & FVC_MASK;
3629             int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3630 
3631             fmt_spec = have_fmt_spec ? POP() : NULL;
3632             value = POP();
3633 
3634             /* See if any conversion is specified. */
3635             switch (which_conversion) {
3636             case FVC_NONE:  conv_fn = NULL;           break;
3637             case FVC_STR:   conv_fn = PyObject_Str;   break;
3638             case FVC_REPR:  conv_fn = PyObject_Repr;  break;
3639             case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3640             default:
3641                 _PyErr_Format(tstate, PyExc_SystemError,
3642                               "unexpected conversion flag %d",
3643                               which_conversion);
3644                 goto error;
3645             }
3646 
3647             /* If there's a conversion function, call it and replace
3648                value with that result. Otherwise, just use value,
3649                without conversion. */
3650             if (conv_fn != NULL) {
3651                 result = conv_fn(value);
3652                 Py_DECREF(value);
3653                 if (result == NULL) {
3654                     Py_XDECREF(fmt_spec);
3655                     goto error;
3656                 }
3657                 value = result;
3658             }
3659 
3660             /* If value is a unicode object, and there's no fmt_spec,
3661                then we know the result of format(value) is value
3662                itself. In that case, skip calling format(). I plan to
3663                move this optimization in to PyObject_Format()
3664                itself. */
3665             if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3666                 /* Do nothing, just transfer ownership to result. */
3667                 result = value;
3668             } else {
3669                 /* Actually call format(). */
3670                 result = PyObject_Format(value, fmt_spec);
3671                 Py_DECREF(value);
3672                 Py_XDECREF(fmt_spec);
3673                 if (result == NULL) {
3674                     goto error;
3675                 }
3676             }
3677 
3678             PUSH(result);
3679             DISPATCH();
3680         }
3681 
3682         case TARGET(EXTENDED_ARG): {
3683             int oldoparg = oparg;
3684             NEXTOPARG();
3685             oparg |= oldoparg << 8;
3686             goto dispatch_opcode;
3687         }
3688 
3689 
3690 #if USE_COMPUTED_GOTOS
3691         _unknown_opcode:
3692 #endif
3693         default:
3694             fprintf(stderr,
3695                 "XXX lineno: %d, opcode: %d\n",
3696                 PyFrame_GetLineNumber(f),
3697                 opcode);
3698             _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
3699             goto error;
3700 
3701         } /* switch */
3702 
3703         /* This should never be reached. Every opcode should end with DISPATCH()
3704            or goto error. */
3705         Py_UNREACHABLE();
3706 
3707 error:
3708         /* Double-check exception status. */
3709 #ifdef NDEBUG
3710         if (!_PyErr_Occurred(tstate)) {
3711             _PyErr_SetString(tstate, PyExc_SystemError,
3712                              "error return without exception set");
3713         }
3714 #else
3715         assert(_PyErr_Occurred(tstate));
3716 #endif
3717 
3718         /* Log traceback info. */
3719         PyTraceBack_Here(f);
3720 
3721         if (tstate->c_tracefunc != NULL)
3722             call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3723                            tstate, f);
3724 
3725 exception_unwind:
3726         /* Unwind stacks if an exception occurred */
3727         while (f->f_iblock > 0) {
3728             /* Pop the current block. */
3729             PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
3730 
3731             if (b->b_type == EXCEPT_HANDLER) {
3732                 UNWIND_EXCEPT_HANDLER(b);
3733                 continue;
3734             }
3735             UNWIND_BLOCK(b);
3736             if (b->b_type == SETUP_FINALLY) {
3737                 PyObject *exc, *val, *tb;
3738                 int handler = b->b_handler;
3739                 _PyErr_StackItem *exc_info = tstate->exc_info;
3740                 /* Beware, this invalidates all b->b_* fields */
3741                 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3742                 PUSH(exc_info->exc_traceback);
3743                 PUSH(exc_info->exc_value);
3744                 if (exc_info->exc_type != NULL) {
3745                     PUSH(exc_info->exc_type);
3746                 }
3747                 else {
3748                     Py_INCREF(Py_None);
3749                     PUSH(Py_None);
3750                 }
3751                 _PyErr_Fetch(tstate, &exc, &val, &tb);
3752                 /* Make the raw exception data
3753                    available to the handler,
3754                    so a program can emulate the
3755                    Python main loop. */
3756                 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
3757                 if (tb != NULL)
3758                     PyException_SetTraceback(val, tb);
3759                 else
3760                     PyException_SetTraceback(val, Py_None);
3761                 Py_INCREF(exc);
3762                 exc_info->exc_type = exc;
3763                 Py_INCREF(val);
3764                 exc_info->exc_value = val;
3765                 exc_info->exc_traceback = tb;
3766                 if (tb == NULL)
3767                     tb = Py_None;
3768                 Py_INCREF(tb);
3769                 PUSH(tb);
3770                 PUSH(val);
3771                 PUSH(exc);
3772                 JUMPTO(handler);
3773                 /* Resume normal execution */
3774                 goto main_loop;
3775             }
3776         } /* unwind stack */
3777 
3778         /* End the loop as we still have an error */
3779         break;
3780     } /* main loop */
3781 
3782     assert(retval == NULL);
3783     assert(_PyErr_Occurred(tstate));
3784 
3785 exit_returning:
3786 
3787     /* Pop remaining stack entries. */
3788     while (!EMPTY()) {
3789         PyObject *o = POP();
3790         Py_XDECREF(o);
3791     }
3792 
3793 exit_yielding:
3794     if (tstate->use_tracing) {
3795         if (tstate->c_tracefunc) {
3796             if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3797                                      tstate, f, PyTrace_RETURN, retval)) {
3798                 Py_CLEAR(retval);
3799             }
3800         }
3801         if (tstate->c_profilefunc) {
3802             if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3803                                      tstate, f, PyTrace_RETURN, retval)) {
3804                 Py_CLEAR(retval);
3805             }
3806         }
3807     }
3808 
3809     /* pop frame */
3810 exit_eval_frame:
3811     if (PyDTrace_FUNCTION_RETURN_ENABLED())
3812         dtrace_function_return(f);
3813     Py_LeaveRecursiveCall();
3814     f->f_executing = 0;
3815     tstate->frame = f->f_back;
3816 
3817     return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
3818 }
3819 
3820 static void
format_missing(PyThreadState * tstate,const char * kind,PyCodeObject * co,PyObject * names)3821 format_missing(PyThreadState *tstate, const char *kind,
3822                PyCodeObject *co, PyObject *names)
3823 {
3824     int err;
3825     Py_ssize_t len = PyList_GET_SIZE(names);
3826     PyObject *name_str, *comma, *tail, *tmp;
3827 
3828     assert(PyList_CheckExact(names));
3829     assert(len >= 1);
3830     /* Deal with the joys of natural language. */
3831     switch (len) {
3832     case 1:
3833         name_str = PyList_GET_ITEM(names, 0);
3834         Py_INCREF(name_str);
3835         break;
3836     case 2:
3837         name_str = PyUnicode_FromFormat("%U and %U",
3838                                         PyList_GET_ITEM(names, len - 2),
3839                                         PyList_GET_ITEM(names, len - 1));
3840         break;
3841     default:
3842         tail = PyUnicode_FromFormat(", %U, and %U",
3843                                     PyList_GET_ITEM(names, len - 2),
3844                                     PyList_GET_ITEM(names, len - 1));
3845         if (tail == NULL)
3846             return;
3847         /* Chop off the last two objects in the list. This shouldn't actually
3848            fail, but we can't be too careful. */
3849         err = PyList_SetSlice(names, len - 2, len, NULL);
3850         if (err == -1) {
3851             Py_DECREF(tail);
3852             return;
3853         }
3854         /* Stitch everything up into a nice comma-separated list. */
3855         comma = PyUnicode_FromString(", ");
3856         if (comma == NULL) {
3857             Py_DECREF(tail);
3858             return;
3859         }
3860         tmp = PyUnicode_Join(comma, names);
3861         Py_DECREF(comma);
3862         if (tmp == NULL) {
3863             Py_DECREF(tail);
3864             return;
3865         }
3866         name_str = PyUnicode_Concat(tmp, tail);
3867         Py_DECREF(tmp);
3868         Py_DECREF(tail);
3869         break;
3870     }
3871     if (name_str == NULL)
3872         return;
3873     _PyErr_Format(tstate, PyExc_TypeError,
3874                   "%U() missing %i required %s argument%s: %U",
3875                   co->co_name,
3876                   len,
3877                   kind,
3878                   len == 1 ? "" : "s",
3879                   name_str);
3880     Py_DECREF(name_str);
3881 }
3882 
3883 static void
missing_arguments(PyThreadState * tstate,PyCodeObject * co,Py_ssize_t missing,Py_ssize_t defcount,PyObject ** fastlocals)3884 missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3885                   Py_ssize_t missing, Py_ssize_t defcount,
3886                   PyObject **fastlocals)
3887 {
3888     Py_ssize_t i, j = 0;
3889     Py_ssize_t start, end;
3890     int positional = (defcount != -1);
3891     const char *kind = positional ? "positional" : "keyword-only";
3892     PyObject *missing_names;
3893 
3894     /* Compute the names of the arguments that are missing. */
3895     missing_names = PyList_New(missing);
3896     if (missing_names == NULL)
3897         return;
3898     if (positional) {
3899         start = 0;
3900         end = co->co_argcount - defcount;
3901     }
3902     else {
3903         start = co->co_argcount;
3904         end = start + co->co_kwonlyargcount;
3905     }
3906     for (i = start; i < end; i++) {
3907         if (GETLOCAL(i) == NULL) {
3908             PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3909             PyObject *name = PyObject_Repr(raw);
3910             if (name == NULL) {
3911                 Py_DECREF(missing_names);
3912                 return;
3913             }
3914             PyList_SET_ITEM(missing_names, j++, name);
3915         }
3916     }
3917     assert(j == missing);
3918     format_missing(tstate, kind, co, missing_names);
3919     Py_DECREF(missing_names);
3920 }
3921 
3922 static void
too_many_positional(PyThreadState * tstate,PyCodeObject * co,Py_ssize_t given,Py_ssize_t defcount,PyObject ** fastlocals)3923 too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3924                     Py_ssize_t given, Py_ssize_t defcount,
3925                     PyObject **fastlocals)
3926 {
3927     int plural;
3928     Py_ssize_t kwonly_given = 0;
3929     Py_ssize_t i;
3930     PyObject *sig, *kwonly_sig;
3931     Py_ssize_t co_argcount = co->co_argcount;
3932 
3933     assert((co->co_flags & CO_VARARGS) == 0);
3934     /* Count missing keyword-only args. */
3935     for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3936         if (GETLOCAL(i) != NULL) {
3937             kwonly_given++;
3938         }
3939     }
3940     if (defcount) {
3941         Py_ssize_t atleast = co_argcount - defcount;
3942         plural = 1;
3943         sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
3944     }
3945     else {
3946         plural = (co_argcount != 1);
3947         sig = PyUnicode_FromFormat("%zd", co_argcount);
3948     }
3949     if (sig == NULL)
3950         return;
3951     if (kwonly_given) {
3952         const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3953         kwonly_sig = PyUnicode_FromFormat(format,
3954                                           given != 1 ? "s" : "",
3955                                           kwonly_given,
3956                                           kwonly_given != 1 ? "s" : "");
3957         if (kwonly_sig == NULL) {
3958             Py_DECREF(sig);
3959             return;
3960         }
3961     }
3962     else {
3963         /* This will not fail. */
3964         kwonly_sig = PyUnicode_FromString("");
3965         assert(kwonly_sig != NULL);
3966     }
3967     _PyErr_Format(tstate, PyExc_TypeError,
3968                   "%U() takes %U positional argument%s but %zd%U %s given",
3969                   co->co_name,
3970                   sig,
3971                   plural ? "s" : "",
3972                   given,
3973                   kwonly_sig,
3974                   given == 1 && !kwonly_given ? "was" : "were");
3975     Py_DECREF(sig);
3976     Py_DECREF(kwonly_sig);
3977 }
3978 
3979 static int
positional_only_passed_as_keyword(PyThreadState * tstate,PyCodeObject * co,Py_ssize_t kwcount,PyObject * const * kwnames)3980 positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
3981                                   Py_ssize_t kwcount, PyObject* const* kwnames)
3982 {
3983     int posonly_conflicts = 0;
3984     PyObject* posonly_names = PyList_New(0);
3985 
3986     for(int k=0; k < co->co_posonlyargcount; k++){
3987         PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
3988 
3989         for (int k2=0; k2<kwcount; k2++){
3990             /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
3991             PyObject* kwname = kwnames[k2];
3992             if (kwname == posonly_name){
3993                 if(PyList_Append(posonly_names, kwname) != 0) {
3994                     goto fail;
3995                 }
3996                 posonly_conflicts++;
3997                 continue;
3998             }
3999 
4000             int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
4001 
4002             if ( cmp > 0) {
4003                 if(PyList_Append(posonly_names, kwname) != 0) {
4004                     goto fail;
4005                 }
4006                 posonly_conflicts++;
4007             } else if (cmp < 0) {
4008                 goto fail;
4009             }
4010 
4011         }
4012     }
4013     if (posonly_conflicts) {
4014         PyObject* comma = PyUnicode_FromString(", ");
4015         if (comma == NULL) {
4016             goto fail;
4017         }
4018         PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4019         Py_DECREF(comma);
4020         if (error_names == NULL) {
4021             goto fail;
4022         }
4023         _PyErr_Format(tstate, PyExc_TypeError,
4024                       "%U() got some positional-only arguments passed"
4025                       " as keyword arguments: '%U'",
4026                       co->co_name, error_names);
4027         Py_DECREF(error_names);
4028         goto fail;
4029     }
4030 
4031     Py_DECREF(posonly_names);
4032     return 0;
4033 
4034 fail:
4035     Py_XDECREF(posonly_names);
4036     return 1;
4037 
4038 }
4039 
4040 /* This is gonna seem *real weird*, but if you put some other code between
4041    PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
4042    the test in the if statements in Misc/gdbinit (pystack and pystackv). */
4043 
4044 PyObject *
_PyEval_EvalCodeWithName(PyObject * _co,PyObject * globals,PyObject * locals,PyObject * const * args,Py_ssize_t argcount,PyObject * const * kwnames,PyObject * const * kwargs,Py_ssize_t kwcount,int kwstep,PyObject * const * defs,Py_ssize_t defcount,PyObject * kwdefs,PyObject * closure,PyObject * name,PyObject * qualname)4045 _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4046            PyObject *const *args, Py_ssize_t argcount,
4047            PyObject *const *kwnames, PyObject *const *kwargs,
4048            Py_ssize_t kwcount, int kwstep,
4049            PyObject *const *defs, Py_ssize_t defcount,
4050            PyObject *kwdefs, PyObject *closure,
4051            PyObject *name, PyObject *qualname)
4052 {
4053     PyCodeObject* co = (PyCodeObject*)_co;
4054     PyFrameObject *f;
4055     PyObject *retval = NULL;
4056     PyObject **fastlocals, **freevars;
4057     PyObject *x, *u;
4058     const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
4059     Py_ssize_t i, j, n;
4060     PyObject *kwdict;
4061 
4062     PyThreadState *tstate = _PyThreadState_GET();
4063     assert(tstate != NULL);
4064 
4065     if (globals == NULL) {
4066         _PyErr_SetString(tstate, PyExc_SystemError,
4067                          "PyEval_EvalCodeEx: NULL globals");
4068         return NULL;
4069     }
4070 
4071     /* Create the frame */
4072     f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
4073     if (f == NULL) {
4074         return NULL;
4075     }
4076     fastlocals = f->f_localsplus;
4077     freevars = f->f_localsplus + co->co_nlocals;
4078 
4079     /* Create a dictionary for keyword parameters (**kwags) */
4080     if (co->co_flags & CO_VARKEYWORDS) {
4081         kwdict = PyDict_New();
4082         if (kwdict == NULL)
4083             goto fail;
4084         i = total_args;
4085         if (co->co_flags & CO_VARARGS) {
4086             i++;
4087         }
4088         SETLOCAL(i, kwdict);
4089     }
4090     else {
4091         kwdict = NULL;
4092     }
4093 
4094     /* Copy all positional arguments into local variables */
4095     if (argcount > co->co_argcount) {
4096         n = co->co_argcount;
4097     }
4098     else {
4099         n = argcount;
4100     }
4101     for (j = 0; j < n; j++) {
4102         x = args[j];
4103         Py_INCREF(x);
4104         SETLOCAL(j, x);
4105     }
4106 
4107     /* Pack other positional arguments into the *args argument */
4108     if (co->co_flags & CO_VARARGS) {
4109         u = _PyTuple_FromArray(args + n, argcount - n);
4110         if (u == NULL) {
4111             goto fail;
4112         }
4113         SETLOCAL(total_args, u);
4114     }
4115 
4116     /* Handle keyword arguments passed as two strided arrays */
4117     kwcount *= kwstep;
4118     for (i = 0; i < kwcount; i += kwstep) {
4119         PyObject **co_varnames;
4120         PyObject *keyword = kwnames[i];
4121         PyObject *value = kwargs[i];
4122         Py_ssize_t j;
4123 
4124         if (keyword == NULL || !PyUnicode_Check(keyword)) {
4125             _PyErr_Format(tstate, PyExc_TypeError,
4126                           "%U() keywords must be strings",
4127                           co->co_name);
4128             goto fail;
4129         }
4130 
4131         /* Speed hack: do raw pointer compares. As names are
4132            normally interned this should almost always hit. */
4133         co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
4134         for (j = co->co_posonlyargcount; j < total_args; j++) {
4135             PyObject *name = co_varnames[j];
4136             if (name == keyword) {
4137                 goto kw_found;
4138             }
4139         }
4140 
4141         /* Slow fallback, just in case */
4142         for (j = co->co_posonlyargcount; j < total_args; j++) {
4143             PyObject *name = co_varnames[j];
4144             int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4145             if (cmp > 0) {
4146                 goto kw_found;
4147             }
4148             else if (cmp < 0) {
4149                 goto fail;
4150             }
4151         }
4152 
4153         assert(j >= total_args);
4154         if (kwdict == NULL) {
4155 
4156             if (co->co_posonlyargcount
4157                 && positional_only_passed_as_keyword(tstate, co,
4158                                                      kwcount, kwnames))
4159             {
4160                 goto fail;
4161             }
4162 
4163             _PyErr_Format(tstate, PyExc_TypeError,
4164                           "%U() got an unexpected keyword argument '%S'",
4165                           co->co_name, keyword);
4166             goto fail;
4167         }
4168 
4169         if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4170             goto fail;
4171         }
4172         continue;
4173 
4174       kw_found:
4175         if (GETLOCAL(j) != NULL) {
4176             _PyErr_Format(tstate, PyExc_TypeError,
4177                           "%U() got multiple values for argument '%S'",
4178                           co->co_name, keyword);
4179             goto fail;
4180         }
4181         Py_INCREF(value);
4182         SETLOCAL(j, value);
4183     }
4184 
4185     /* Check the number of positional arguments */
4186     if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
4187         too_many_positional(tstate, co, argcount, defcount, fastlocals);
4188         goto fail;
4189     }
4190 
4191     /* Add missing positional arguments (copy default values from defs) */
4192     if (argcount < co->co_argcount) {
4193         Py_ssize_t m = co->co_argcount - defcount;
4194         Py_ssize_t missing = 0;
4195         for (i = argcount; i < m; i++) {
4196             if (GETLOCAL(i) == NULL) {
4197                 missing++;
4198             }
4199         }
4200         if (missing) {
4201             missing_arguments(tstate, co, missing, defcount, fastlocals);
4202             goto fail;
4203         }
4204         if (n > m)
4205             i = n - m;
4206         else
4207             i = 0;
4208         for (; i < defcount; i++) {
4209             if (GETLOCAL(m+i) == NULL) {
4210                 PyObject *def = defs[i];
4211                 Py_INCREF(def);
4212                 SETLOCAL(m+i, def);
4213             }
4214         }
4215     }
4216 
4217     /* Add missing keyword arguments (copy default values from kwdefs) */
4218     if (co->co_kwonlyargcount > 0) {
4219         Py_ssize_t missing = 0;
4220         for (i = co->co_argcount; i < total_args; i++) {
4221             PyObject *name;
4222             if (GETLOCAL(i) != NULL)
4223                 continue;
4224             name = PyTuple_GET_ITEM(co->co_varnames, i);
4225             if (kwdefs != NULL) {
4226                 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
4227                 if (def) {
4228                     Py_INCREF(def);
4229                     SETLOCAL(i, def);
4230                     continue;
4231                 }
4232                 else if (_PyErr_Occurred(tstate)) {
4233                     goto fail;
4234                 }
4235             }
4236             missing++;
4237         }
4238         if (missing) {
4239             missing_arguments(tstate, co, missing, -1, fastlocals);
4240             goto fail;
4241         }
4242     }
4243 
4244     /* Allocate and initialize storage for cell vars, and copy free
4245        vars into frame. */
4246     for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
4247         PyObject *c;
4248         Py_ssize_t arg;
4249         /* Possibly account for the cell variable being an argument. */
4250         if (co->co_cell2arg != NULL &&
4251             (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
4252             c = PyCell_New(GETLOCAL(arg));
4253             /* Clear the local copy. */
4254             SETLOCAL(arg, NULL);
4255         }
4256         else {
4257             c = PyCell_New(NULL);
4258         }
4259         if (c == NULL)
4260             goto fail;
4261         SETLOCAL(co->co_nlocals + i, c);
4262     }
4263 
4264     /* Copy closure variables to free variables */
4265     for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4266         PyObject *o = PyTuple_GET_ITEM(closure, i);
4267         Py_INCREF(o);
4268         freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
4269     }
4270 
4271     /* Handle generator/coroutine/asynchronous generator */
4272     if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
4273         PyObject *gen;
4274         int is_coro = co->co_flags & CO_COROUTINE;
4275 
4276         /* Don't need to keep the reference to f_back, it will be set
4277          * when the generator is resumed. */
4278         Py_CLEAR(f->f_back);
4279 
4280         /* Create a new generator that owns the ready to run frame
4281          * and return that as the value. */
4282         if (is_coro) {
4283             gen = PyCoro_New(f, name, qualname);
4284         } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4285             gen = PyAsyncGen_New(f, name, qualname);
4286         } else {
4287             gen = PyGen_NewWithQualName(f, name, qualname);
4288         }
4289         if (gen == NULL) {
4290             return NULL;
4291         }
4292 
4293         _PyObject_GC_TRACK(f);
4294 
4295         return gen;
4296     }
4297 
4298     retval = PyEval_EvalFrameEx(f,0);
4299 
4300 fail: /* Jump here from prelude on failure */
4301 
4302     /* decref'ing the frame can cause __del__ methods to get invoked,
4303        which can call back into Python.  While we're done with the
4304        current Python frame (f), the associated C stack is still in use,
4305        so recursion_depth must be boosted for the duration.
4306     */
4307     assert(tstate != NULL);
4308     if (Py_REFCNT(f) > 1) {
4309         Py_DECREF(f);
4310         _PyObject_GC_TRACK(f);
4311     }
4312     else {
4313         ++tstate->recursion_depth;
4314         Py_DECREF(f);
4315         --tstate->recursion_depth;
4316     }
4317     return retval;
4318 }
4319 
4320 PyObject *
PyEval_EvalCodeEx(PyObject * _co,PyObject * globals,PyObject * locals,PyObject * const * args,int argcount,PyObject * const * kws,int kwcount,PyObject * const * defs,int defcount,PyObject * kwdefs,PyObject * closure)4321 PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
4322                   PyObject *const *args, int argcount,
4323                   PyObject *const *kws, int kwcount,
4324                   PyObject *const *defs, int defcount,
4325                   PyObject *kwdefs, PyObject *closure)
4326 {
4327     return _PyEval_EvalCodeWithName(_co, globals, locals,
4328                                     args, argcount,
4329                                     kws, kws != NULL ? kws + 1 : NULL,
4330                                     kwcount, 2,
4331                                     defs, defcount,
4332                                     kwdefs, closure,
4333                                     NULL, NULL);
4334 }
4335 
4336 static PyObject *
special_lookup(PyThreadState * tstate,PyObject * o,_Py_Identifier * id)4337 special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
4338 {
4339     PyObject *res;
4340     res = _PyObject_LookupSpecial(o, id);
4341     if (res == NULL && !_PyErr_Occurred(tstate)) {
4342         _PyErr_SetObject(tstate, PyExc_AttributeError, id->object);
4343         return NULL;
4344     }
4345     return res;
4346 }
4347 
4348 
4349 /* Logic for the raise statement (too complicated for inlining).
4350    This *consumes* a reference count to each of its arguments. */
4351 static int
do_raise(PyThreadState * tstate,PyObject * exc,PyObject * cause)4352 do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
4353 {
4354     PyObject *type = NULL, *value = NULL;
4355 
4356     if (exc == NULL) {
4357         /* Reraise */
4358         _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
4359         PyObject *tb;
4360         type = exc_info->exc_type;
4361         value = exc_info->exc_value;
4362         tb = exc_info->exc_traceback;
4363         if (type == Py_None || type == NULL) {
4364             _PyErr_SetString(tstate, PyExc_RuntimeError,
4365                              "No active exception to reraise");
4366             return 0;
4367         }
4368         Py_XINCREF(type);
4369         Py_XINCREF(value);
4370         Py_XINCREF(tb);
4371         _PyErr_Restore(tstate, type, value, tb);
4372         return 1;
4373     }
4374 
4375     /* We support the following forms of raise:
4376        raise
4377        raise <instance>
4378        raise <type> */
4379 
4380     if (PyExceptionClass_Check(exc)) {
4381         type = exc;
4382         value = _PyObject_CallNoArg(exc);
4383         if (value == NULL)
4384             goto raise_error;
4385         if (!PyExceptionInstance_Check(value)) {
4386             _PyErr_Format(tstate, PyExc_TypeError,
4387                           "calling %R should have returned an instance of "
4388                           "BaseException, not %R",
4389                           type, Py_TYPE(value));
4390              goto raise_error;
4391         }
4392     }
4393     else if (PyExceptionInstance_Check(exc)) {
4394         value = exc;
4395         type = PyExceptionInstance_Class(exc);
4396         Py_INCREF(type);
4397     }
4398     else {
4399         /* Not something you can raise.  You get an exception
4400            anyway, just not what you specified :-) */
4401         Py_DECREF(exc);
4402         _PyErr_SetString(tstate, PyExc_TypeError,
4403                          "exceptions must derive from BaseException");
4404         goto raise_error;
4405     }
4406 
4407     assert(type != NULL);
4408     assert(value != NULL);
4409 
4410     if (cause) {
4411         PyObject *fixed_cause;
4412         if (PyExceptionClass_Check(cause)) {
4413             fixed_cause = _PyObject_CallNoArg(cause);
4414             if (fixed_cause == NULL)
4415                 goto raise_error;
4416             Py_DECREF(cause);
4417         }
4418         else if (PyExceptionInstance_Check(cause)) {
4419             fixed_cause = cause;
4420         }
4421         else if (cause == Py_None) {
4422             Py_DECREF(cause);
4423             fixed_cause = NULL;
4424         }
4425         else {
4426             _PyErr_SetString(tstate, PyExc_TypeError,
4427                              "exception causes must derive from "
4428                              "BaseException");
4429             goto raise_error;
4430         }
4431         PyException_SetCause(value, fixed_cause);
4432     }
4433 
4434     _PyErr_SetObject(tstate, type, value);
4435     /* PyErr_SetObject incref's its arguments */
4436     Py_DECREF(value);
4437     Py_DECREF(type);
4438     return 0;
4439 
4440 raise_error:
4441     Py_XDECREF(value);
4442     Py_XDECREF(type);
4443     Py_XDECREF(cause);
4444     return 0;
4445 }
4446 
4447 /* Iterate v argcnt times and store the results on the stack (via decreasing
4448    sp).  Return 1 for success, 0 if error.
4449 
4450    If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4451    with a variable target.
4452 */
4453 
4454 static int
unpack_iterable(PyThreadState * tstate,PyObject * v,int argcnt,int argcntafter,PyObject ** sp)4455 unpack_iterable(PyThreadState *tstate, PyObject *v,
4456                 int argcnt, int argcntafter, PyObject **sp)
4457 {
4458     int i = 0, j = 0;
4459     Py_ssize_t ll = 0;
4460     PyObject *it;  /* iter(v) */
4461     PyObject *w;
4462     PyObject *l = NULL; /* variable list */
4463 
4464     assert(v != NULL);
4465 
4466     it = PyObject_GetIter(v);
4467     if (it == NULL) {
4468         if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
4469             v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4470         {
4471             _PyErr_Format(tstate, PyExc_TypeError,
4472                           "cannot unpack non-iterable %.200s object",
4473                           v->ob_type->tp_name);
4474         }
4475         return 0;
4476     }
4477 
4478     for (; i < argcnt; i++) {
4479         w = PyIter_Next(it);
4480         if (w == NULL) {
4481             /* Iterator done, via error or exhaustion. */
4482             if (!_PyErr_Occurred(tstate)) {
4483                 if (argcntafter == -1) {
4484                     _PyErr_Format(tstate, PyExc_ValueError,
4485                                   "not enough values to unpack "
4486                                   "(expected %d, got %d)",
4487                                   argcnt, i);
4488                 }
4489                 else {
4490                     _PyErr_Format(tstate, PyExc_ValueError,
4491                                   "not enough values to unpack "
4492                                   "(expected at least %d, got %d)",
4493                                   argcnt + argcntafter, i);
4494                 }
4495             }
4496             goto Error;
4497         }
4498         *--sp = w;
4499     }
4500 
4501     if (argcntafter == -1) {
4502         /* We better have exhausted the iterator now. */
4503         w = PyIter_Next(it);
4504         if (w == NULL) {
4505             if (_PyErr_Occurred(tstate))
4506                 goto Error;
4507             Py_DECREF(it);
4508             return 1;
4509         }
4510         Py_DECREF(w);
4511         _PyErr_Format(tstate, PyExc_ValueError,
4512                       "too many values to unpack (expected %d)",
4513                       argcnt);
4514         goto Error;
4515     }
4516 
4517     l = PySequence_List(it);
4518     if (l == NULL)
4519         goto Error;
4520     *--sp = l;
4521     i++;
4522 
4523     ll = PyList_GET_SIZE(l);
4524     if (ll < argcntafter) {
4525         _PyErr_Format(tstate, PyExc_ValueError,
4526             "not enough values to unpack (expected at least %d, got %zd)",
4527             argcnt + argcntafter, argcnt + ll);
4528         goto Error;
4529     }
4530 
4531     /* Pop the "after-variable" args off the list. */
4532     for (j = argcntafter; j > 0; j--, i++) {
4533         *--sp = PyList_GET_ITEM(l, ll - j);
4534     }
4535     /* Resize the list. */
4536     Py_SIZE(l) = ll - argcntafter;
4537     Py_DECREF(it);
4538     return 1;
4539 
4540 Error:
4541     for (; i > 0; i--, sp++)
4542         Py_DECREF(*sp);
4543     Py_XDECREF(it);
4544     return 0;
4545 }
4546 
4547 
4548 #ifdef LLTRACE
4549 static int
prtrace(PyThreadState * tstate,PyObject * v,const char * str)4550 prtrace(PyThreadState *tstate, PyObject *v, const char *str)
4551 {
4552     printf("%s ", str);
4553     if (PyObject_Print(v, stdout, 0) != 0) {
4554         /* Don't know what else to do */
4555         _PyErr_Clear(tstate);
4556     }
4557     printf("\n");
4558     return 1;
4559 }
4560 #endif
4561 
4562 static void
call_exc_trace(Py_tracefunc func,PyObject * self,PyThreadState * tstate,PyFrameObject * f)4563 call_exc_trace(Py_tracefunc func, PyObject *self,
4564                PyThreadState *tstate, PyFrameObject *f)
4565 {
4566     PyObject *type, *value, *traceback, *orig_traceback, *arg;
4567     int err;
4568     _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
4569     if (value == NULL) {
4570         value = Py_None;
4571         Py_INCREF(value);
4572     }
4573     _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
4574     traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
4575     arg = PyTuple_Pack(3, type, value, traceback);
4576     if (arg == NULL) {
4577         _PyErr_Restore(tstate, type, value, orig_traceback);
4578         return;
4579     }
4580     err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
4581     Py_DECREF(arg);
4582     if (err == 0) {
4583         _PyErr_Restore(tstate, type, value, orig_traceback);
4584     }
4585     else {
4586         Py_XDECREF(type);
4587         Py_XDECREF(value);
4588         Py_XDECREF(orig_traceback);
4589     }
4590 }
4591 
4592 static int
call_trace_protected(Py_tracefunc func,PyObject * obj,PyThreadState * tstate,PyFrameObject * frame,int what,PyObject * arg)4593 call_trace_protected(Py_tracefunc func, PyObject *obj,
4594                      PyThreadState *tstate, PyFrameObject *frame,
4595                      int what, PyObject *arg)
4596 {
4597     PyObject *type, *value, *traceback;
4598     int err;
4599     _PyErr_Fetch(tstate, &type, &value, &traceback);
4600     err = call_trace(func, obj, tstate, frame, what, arg);
4601     if (err == 0)
4602     {
4603         _PyErr_Restore(tstate, type, value, traceback);
4604         return 0;
4605     }
4606     else {
4607         Py_XDECREF(type);
4608         Py_XDECREF(value);
4609         Py_XDECREF(traceback);
4610         return -1;
4611     }
4612 }
4613 
4614 static int
call_trace(Py_tracefunc func,PyObject * obj,PyThreadState * tstate,PyFrameObject * frame,int what,PyObject * arg)4615 call_trace(Py_tracefunc func, PyObject *obj,
4616            PyThreadState *tstate, PyFrameObject *frame,
4617            int what, PyObject *arg)
4618 {
4619     int result;
4620     if (tstate->tracing)
4621         return 0;
4622     tstate->tracing++;
4623     tstate->use_tracing = 0;
4624     result = func(obj, frame, what, arg);
4625     tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4626                            || (tstate->c_profilefunc != NULL));
4627     tstate->tracing--;
4628     return result;
4629 }
4630 
4631 PyObject *
_PyEval_CallTracing(PyObject * func,PyObject * args)4632 _PyEval_CallTracing(PyObject *func, PyObject *args)
4633 {
4634     PyThreadState *tstate = _PyThreadState_GET();
4635     int save_tracing = tstate->tracing;
4636     int save_use_tracing = tstate->use_tracing;
4637     PyObject *result;
4638 
4639     tstate->tracing = 0;
4640     tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4641                            || (tstate->c_profilefunc != NULL));
4642     result = PyObject_Call(func, args, NULL);
4643     tstate->tracing = save_tracing;
4644     tstate->use_tracing = save_use_tracing;
4645     return result;
4646 }
4647 
4648 /* See Objects/lnotab_notes.txt for a description of how tracing works. */
4649 static int
maybe_call_line_trace(Py_tracefunc func,PyObject * obj,PyThreadState * tstate,PyFrameObject * frame,int * instr_lb,int * instr_ub,int * instr_prev)4650 maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
4651                       PyThreadState *tstate, PyFrameObject *frame,
4652                       int *instr_lb, int *instr_ub, int *instr_prev)
4653 {
4654     int result = 0;
4655     int line = frame->f_lineno;
4656 
4657     /* If the last instruction executed isn't in the current
4658        instruction window, reset the window.
4659     */
4660     if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4661         PyAddrPair bounds;
4662         line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4663                                        &bounds);
4664         *instr_lb = bounds.ap_lower;
4665         *instr_ub = bounds.ap_upper;
4666     }
4667     /* If the last instruction falls at the start of a line or if it
4668        represents a jump backwards, update the frame's line number and
4669        then call the trace function if we're tracing source lines.
4670     */
4671     if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
4672         frame->f_lineno = line;
4673         if (frame->f_trace_lines) {
4674             result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4675         }
4676     }
4677     /* Always emit an opcode event if we're tracing all opcodes. */
4678     if (frame->f_trace_opcodes) {
4679         result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4680     }
4681     *instr_prev = frame->f_lasti;
4682     return result;
4683 }
4684 
4685 void
PyEval_SetProfile(Py_tracefunc func,PyObject * arg)4686 PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
4687 {
4688     if (PySys_Audit("sys.setprofile", NULL) < 0) {
4689         _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
4690         return;
4691     }
4692 
4693     PyThreadState *tstate = _PyThreadState_GET();
4694     PyObject *temp = tstate->c_profileobj;
4695     Py_XINCREF(arg);
4696     tstate->c_profilefunc = NULL;
4697     tstate->c_profileobj = NULL;
4698     /* Must make sure that tracing is not ignored if 'temp' is freed */
4699     tstate->use_tracing = tstate->c_tracefunc != NULL;
4700     Py_XDECREF(temp);
4701     tstate->c_profilefunc = func;
4702     tstate->c_profileobj = arg;
4703     /* Flag that tracing or profiling is turned on */
4704     tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
4705 }
4706 
4707 void
PyEval_SetTrace(Py_tracefunc func,PyObject * arg)4708 PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4709 {
4710     if (PySys_Audit("sys.settrace", NULL) < 0) {
4711         _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
4712         return;
4713     }
4714 
4715     _PyRuntimeState *runtime = &_PyRuntime;
4716     PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
4717     PyObject *temp = tstate->c_traceobj;
4718     runtime->ceval.tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
4719     Py_XINCREF(arg);
4720     tstate->c_tracefunc = NULL;
4721     tstate->c_traceobj = NULL;
4722     /* Must make sure that profiling is not ignored if 'temp' is freed */
4723     tstate->use_tracing = tstate->c_profilefunc != NULL;
4724     Py_XDECREF(temp);
4725     tstate->c_tracefunc = func;
4726     tstate->c_traceobj = arg;
4727     /* Flag that tracing or profiling is turned on */
4728     tstate->use_tracing = ((func != NULL)
4729                            || (tstate->c_profilefunc != NULL));
4730 }
4731 
4732 void
_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)4733 _PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4734 {
4735     assert(new_depth >= 0);
4736     PyThreadState *tstate = _PyThreadState_GET();
4737     tstate->coroutine_origin_tracking_depth = new_depth;
4738 }
4739 
4740 int
_PyEval_GetCoroutineOriginTrackingDepth(void)4741 _PyEval_GetCoroutineOriginTrackingDepth(void)
4742 {
4743     PyThreadState *tstate = _PyThreadState_GET();
4744     return tstate->coroutine_origin_tracking_depth;
4745 }
4746 
4747 PyObject *
_PyEval_GetAsyncGenFirstiter(void)4748 _PyEval_GetAsyncGenFirstiter(void)
4749 {
4750     PyThreadState *tstate = _PyThreadState_GET();
4751     return tstate->async_gen_firstiter;
4752 }
4753 
4754 PyObject *
_PyEval_GetAsyncGenFinalizer(void)4755 _PyEval_GetAsyncGenFinalizer(void)
4756 {
4757     PyThreadState *tstate = _PyThreadState_GET();
4758     return tstate->async_gen_finalizer;
4759 }
4760 
4761 static PyFrameObject *
_PyEval_GetFrame(PyThreadState * tstate)4762 _PyEval_GetFrame(PyThreadState *tstate)
4763 {
4764     return _PyRuntime.gilstate.getframe(tstate);
4765 }
4766 
4767 PyFrameObject *
PyEval_GetFrame(void)4768 PyEval_GetFrame(void)
4769 {
4770     PyThreadState *tstate = _PyThreadState_GET();
4771     return _PyEval_GetFrame(tstate);
4772 }
4773 
4774 PyObject *
PyEval_GetBuiltins(void)4775 PyEval_GetBuiltins(void)
4776 {
4777     PyThreadState *tstate = _PyThreadState_GET();
4778     PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
4779     if (current_frame == NULL)
4780         return tstate->interp->builtins;
4781     else
4782         return current_frame->f_builtins;
4783 }
4784 
4785 /* Convenience function to get a builtin from its name */
4786 PyObject *
_PyEval_GetBuiltinId(_Py_Identifier * name)4787 _PyEval_GetBuiltinId(_Py_Identifier *name)
4788 {
4789     PyThreadState *tstate = _PyThreadState_GET();
4790     PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4791     if (attr) {
4792         Py_INCREF(attr);
4793     }
4794     else if (!_PyErr_Occurred(tstate)) {
4795         _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
4796     }
4797     return attr;
4798 }
4799 
4800 PyObject *
PyEval_GetLocals(void)4801 PyEval_GetLocals(void)
4802 {
4803     PyThreadState *tstate = _PyThreadState_GET();
4804     PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
4805     if (current_frame == NULL) {
4806         _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
4807         return NULL;
4808     }
4809 
4810     if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
4811         return NULL;
4812     }
4813 
4814     assert(current_frame->f_locals != NULL);
4815     return current_frame->f_locals;
4816 }
4817 
4818 PyObject *
PyEval_GetGlobals(void)4819 PyEval_GetGlobals(void)
4820 {
4821     PyThreadState *tstate = _PyThreadState_GET();
4822     PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
4823     if (current_frame == NULL) {
4824         return NULL;
4825     }
4826 
4827     assert(current_frame->f_globals != NULL);
4828     return current_frame->f_globals;
4829 }
4830 
4831 int
PyEval_MergeCompilerFlags(PyCompilerFlags * cf)4832 PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
4833 {
4834     PyThreadState *tstate = _PyThreadState_GET();
4835     PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
4836     int result = cf->cf_flags != 0;
4837 
4838     if (current_frame != NULL) {
4839         const int codeflags = current_frame->f_code->co_flags;
4840         const int compilerflags = codeflags & PyCF_MASK;
4841         if (compilerflags) {
4842             result = 1;
4843             cf->cf_flags |= compilerflags;
4844         }
4845 #if 0 /* future keyword */
4846         if (codeflags & CO_GENERATOR_ALLOWED) {
4847             result = 1;
4848             cf->cf_flags |= CO_GENERATOR_ALLOWED;
4849         }
4850 #endif
4851     }
4852     return result;
4853 }
4854 
4855 
4856 const char *
PyEval_GetFuncName(PyObject * func)4857 PyEval_GetFuncName(PyObject *func)
4858 {
4859     if (PyMethod_Check(func))
4860         return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4861     else if (PyFunction_Check(func))
4862         return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
4863     else if (PyCFunction_Check(func))
4864         return ((PyCFunctionObject*)func)->m_ml->ml_name;
4865     else
4866         return func->ob_type->tp_name;
4867 }
4868 
4869 const char *
PyEval_GetFuncDesc(PyObject * func)4870 PyEval_GetFuncDesc(PyObject *func)
4871 {
4872     if (PyMethod_Check(func))
4873         return "()";
4874     else if (PyFunction_Check(func))
4875         return "()";
4876     else if (PyCFunction_Check(func))
4877         return "()";
4878     else
4879         return " object";
4880 }
4881 
4882 #define C_TRACE(x, call) \
4883 if (tstate->use_tracing && tstate->c_profilefunc) { \
4884     if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4885         tstate, tstate->frame, \
4886         PyTrace_C_CALL, func)) { \
4887         x = NULL; \
4888     } \
4889     else { \
4890         x = call; \
4891         if (tstate->c_profilefunc != NULL) { \
4892             if (x == NULL) { \
4893                 call_trace_protected(tstate->c_profilefunc, \
4894                     tstate->c_profileobj, \
4895                     tstate, tstate->frame, \
4896                     PyTrace_C_EXCEPTION, func); \
4897                 /* XXX should pass (type, value, tb) */ \
4898             } else { \
4899                 if (call_trace(tstate->c_profilefunc, \
4900                     tstate->c_profileobj, \
4901                     tstate, tstate->frame, \
4902                     PyTrace_C_RETURN, func)) { \
4903                     Py_DECREF(x); \
4904                     x = NULL; \
4905                 } \
4906             } \
4907         } \
4908     } \
4909 } else { \
4910     x = call; \
4911     }
4912 
4913 
4914 static PyObject *
trace_call_function(PyThreadState * tstate,PyObject * func,PyObject ** args,Py_ssize_t nargs,PyObject * kwnames)4915 trace_call_function(PyThreadState *tstate,
4916                     PyObject *func,
4917                     PyObject **args, Py_ssize_t nargs,
4918                     PyObject *kwnames)
4919 {
4920     PyObject *x;
4921     if (PyCFunction_Check(func)) {
4922         C_TRACE(x, _PyObject_Vectorcall(func, args, nargs, kwnames));
4923         return x;
4924     }
4925     else if (Py_TYPE(func) == &PyMethodDescr_Type && nargs > 0) {
4926         /* We need to create a temporary bound method as argument
4927            for profiling.
4928 
4929            If nargs == 0, then this cannot work because we have no
4930            "self". In any case, the call itself would raise
4931            TypeError (foo needs an argument), so we just skip
4932            profiling. */
4933         PyObject *self = args[0];
4934         func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4935         if (func == NULL) {
4936             return NULL;
4937         }
4938         C_TRACE(x, _PyObject_Vectorcall(func,
4939                                         args+1, nargs-1,
4940                                         kwnames));
4941         Py_DECREF(func);
4942         return x;
4943     }
4944     return _PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
4945 }
4946 
4947 /* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4948    to reduce the stack consumption. */
Py_LOCAL_INLINE(PyObject *)4949 Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
4950 call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
4951 {
4952     PyObject **pfunc = (*pp_stack) - oparg - 1;
4953     PyObject *func = *pfunc;
4954     PyObject *x, *w;
4955     Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4956     Py_ssize_t nargs = oparg - nkwargs;
4957     PyObject **stack = (*pp_stack) - nargs - nkwargs;
4958 
4959     if (tstate->use_tracing) {
4960         x = trace_call_function(tstate, func, stack, nargs, kwnames);
4961     }
4962     else {
4963         x = _PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
4964     }
4965 
4966     assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
4967 
4968     /* Clear the stack of the function object. */
4969     while ((*pp_stack) > pfunc) {
4970         w = EXT_POP(*pp_stack);
4971         Py_DECREF(w);
4972     }
4973 
4974     return x;
4975 }
4976 
4977 static PyObject *
do_call_core(PyThreadState * tstate,PyObject * func,PyObject * callargs,PyObject * kwdict)4978 do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
4979 {
4980     PyObject *result;
4981 
4982     if (PyCFunction_Check(func)) {
4983         C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4984         return result;
4985     }
4986     else if (Py_TYPE(func) == &PyMethodDescr_Type) {
4987         Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4988         if (nargs > 0 && tstate->use_tracing) {
4989             /* We need to create a temporary bound method as argument
4990                for profiling.
4991 
4992                If nargs == 0, then this cannot work because we have no
4993                "self". In any case, the call itself would raise
4994                TypeError (foo needs an argument), so we just skip
4995                profiling. */
4996             PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4997             func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4998             if (func == NULL) {
4999                 return NULL;
5000             }
5001 
5002             C_TRACE(result, _PyObject_FastCallDict(func,
5003                                                    &_PyTuple_ITEMS(callargs)[1],
5004                                                    nargs - 1,
5005                                                    kwdict));
5006             Py_DECREF(func);
5007             return result;
5008         }
5009     }
5010     return PyObject_Call(func, callargs, kwdict);
5011 }
5012 
5013 /* Extract a slice index from a PyLong or an object with the
5014    nb_index slot defined, and store in *pi.
5015    Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
5016    and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
5017    Return 0 on error, 1 on success.
5018 */
5019 int
_PyEval_SliceIndex(PyObject * v,Py_ssize_t * pi)5020 _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
5021 {
5022     PyThreadState *tstate = _PyThreadState_GET();
5023     if (v != Py_None) {
5024         Py_ssize_t x;
5025         if (PyIndex_Check(v)) {
5026             x = PyNumber_AsSsize_t(v, NULL);
5027             if (x == -1 && _PyErr_Occurred(tstate))
5028                 return 0;
5029         }
5030         else {
5031             _PyErr_SetString(tstate, PyExc_TypeError,
5032                              "slice indices must be integers or "
5033                              "None or have an __index__ method");
5034             return 0;
5035         }
5036         *pi = x;
5037     }
5038     return 1;
5039 }
5040 
5041 int
_PyEval_SliceIndexNotNone(PyObject * v,Py_ssize_t * pi)5042 _PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
5043 {
5044     PyThreadState *tstate = _PyThreadState_GET();
5045     Py_ssize_t x;
5046     if (PyIndex_Check(v)) {
5047         x = PyNumber_AsSsize_t(v, NULL);
5048         if (x == -1 && _PyErr_Occurred(tstate))
5049             return 0;
5050     }
5051     else {
5052         _PyErr_SetString(tstate, PyExc_TypeError,
5053                          "slice indices must be integers or "
5054                          "have an __index__ method");
5055         return 0;
5056     }
5057     *pi = x;
5058     return 1;
5059 }
5060 
5061 
5062 #define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
5063                          "BaseException is not allowed"
5064 
5065 static PyObject *
cmp_outcome(PyThreadState * tstate,int op,PyObject * v,PyObject * w)5066 cmp_outcome(PyThreadState *tstate, int op, PyObject *v, PyObject *w)
5067 {
5068     int res = 0;
5069     switch (op) {
5070     case PyCmp_IS:
5071         res = (v == w);
5072         break;
5073     case PyCmp_IS_NOT:
5074         res = (v != w);
5075         break;
5076     case PyCmp_IN:
5077         res = PySequence_Contains(w, v);
5078         if (res < 0)
5079             return NULL;
5080         break;
5081     case PyCmp_NOT_IN:
5082         res = PySequence_Contains(w, v);
5083         if (res < 0)
5084             return NULL;
5085         res = !res;
5086         break;
5087     case PyCmp_EXC_MATCH:
5088         if (PyTuple_Check(w)) {
5089             Py_ssize_t i, length;
5090             length = PyTuple_Size(w);
5091             for (i = 0; i < length; i += 1) {
5092                 PyObject *exc = PyTuple_GET_ITEM(w, i);
5093                 if (!PyExceptionClass_Check(exc)) {
5094                     _PyErr_SetString(tstate, PyExc_TypeError,
5095                                      CANNOT_CATCH_MSG);
5096                     return NULL;
5097                 }
5098             }
5099         }
5100         else {
5101             if (!PyExceptionClass_Check(w)) {
5102                 _PyErr_SetString(tstate, PyExc_TypeError,
5103                                  CANNOT_CATCH_MSG);
5104                 return NULL;
5105             }
5106         }
5107         res = PyErr_GivenExceptionMatches(v, w);
5108         break;
5109     default:
5110         return PyObject_RichCompare(v, w, op);
5111     }
5112     v = res ? Py_True : Py_False;
5113     Py_INCREF(v);
5114     return v;
5115 }
5116 
5117 static PyObject *
import_name(PyThreadState * tstate,PyFrameObject * f,PyObject * name,PyObject * fromlist,PyObject * level)5118 import_name(PyThreadState *tstate, PyFrameObject *f,
5119             PyObject *name, PyObject *fromlist, PyObject *level)
5120 {
5121     _Py_IDENTIFIER(__import__);
5122     PyObject *import_func, *res;
5123     PyObject* stack[5];
5124 
5125     import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
5126     if (import_func == NULL) {
5127         if (!_PyErr_Occurred(tstate)) {
5128             _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
5129         }
5130         return NULL;
5131     }
5132 
5133     /* Fast path for not overloaded __import__. */
5134     if (import_func == tstate->interp->import_func) {
5135         int ilevel = _PyLong_AsInt(level);
5136         if (ilevel == -1 && _PyErr_Occurred(tstate)) {
5137             return NULL;
5138         }
5139         res = PyImport_ImportModuleLevelObject(
5140                         name,
5141                         f->f_globals,
5142                         f->f_locals == NULL ? Py_None : f->f_locals,
5143                         fromlist,
5144                         ilevel);
5145         return res;
5146     }
5147 
5148     Py_INCREF(import_func);
5149 
5150     stack[0] = name;
5151     stack[1] = f->f_globals;
5152     stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5153     stack[3] = fromlist;
5154     stack[4] = level;
5155     res = _PyObject_FastCall(import_func, stack, 5);
5156     Py_DECREF(import_func);
5157     return res;
5158 }
5159 
5160 static PyObject *
import_from(PyThreadState * tstate,PyObject * v,PyObject * name)5161 import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
5162 {
5163     PyObject *x;
5164     _Py_IDENTIFIER(__name__);
5165     PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
5166 
5167     if (_PyObject_LookupAttr(v, name, &x) != 0) {
5168         return x;
5169     }
5170     /* Issue #17636: in case this failed because of a circular relative
5171        import, try to fallback on reading the module directly from
5172        sys.modules. */
5173     pkgname = _PyObject_GetAttrId(v, &PyId___name__);
5174     if (pkgname == NULL) {
5175         goto error;
5176     }
5177     if (!PyUnicode_Check(pkgname)) {
5178         Py_CLEAR(pkgname);
5179         goto error;
5180     }
5181     fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
5182     if (fullmodname == NULL) {
5183         Py_DECREF(pkgname);
5184         return NULL;
5185     }
5186     x = PyImport_GetModule(fullmodname);
5187     Py_DECREF(fullmodname);
5188     if (x == NULL && !_PyErr_Occurred(tstate)) {
5189         goto error;
5190     }
5191     Py_DECREF(pkgname);
5192     return x;
5193  error:
5194     pkgpath = PyModule_GetFilenameObject(v);
5195     if (pkgname == NULL) {
5196         pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5197         if (pkgname_or_unknown == NULL) {
5198             Py_XDECREF(pkgpath);
5199             return NULL;
5200         }
5201     } else {
5202         pkgname_or_unknown = pkgname;
5203     }
5204 
5205     if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
5206         _PyErr_Clear(tstate);
5207         errmsg = PyUnicode_FromFormat(
5208             "cannot import name %R from %R (unknown location)",
5209             name, pkgname_or_unknown
5210         );
5211         /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
5212         PyErr_SetImportError(errmsg, pkgname, NULL);
5213     }
5214     else {
5215         _Py_IDENTIFIER(__spec__);
5216         PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
5217         const char *fmt =
5218             _PyModuleSpec_IsInitializing(spec) ?
5219             "cannot import name %R from partially initialized module %R "
5220             "(most likely due to a circular import) (%S)" :
5221             "cannot import name %R from %R (%S)";
5222         Py_XDECREF(spec);
5223 
5224         errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
5225         /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
5226         PyErr_SetImportError(errmsg, pkgname, pkgpath);
5227     }
5228 
5229     Py_XDECREF(errmsg);
5230     Py_XDECREF(pkgname_or_unknown);
5231     Py_XDECREF(pkgpath);
5232     return NULL;
5233 }
5234 
5235 static int
import_all_from(PyThreadState * tstate,PyObject * locals,PyObject * v)5236 import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
5237 {
5238     _Py_IDENTIFIER(__all__);
5239     _Py_IDENTIFIER(__dict__);
5240     _Py_IDENTIFIER(__name__);
5241     PyObject *all, *dict, *name, *value;
5242     int skip_leading_underscores = 0;
5243     int pos, err;
5244 
5245     if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5246         return -1; /* Unexpected error */
5247     }
5248     if (all == NULL) {
5249         if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5250             return -1;
5251         }
5252         if (dict == NULL) {
5253             _PyErr_SetString(tstate, PyExc_ImportError,
5254                     "from-import-* object has no __dict__ and no __all__");
5255             return -1;
5256         }
5257         all = PyMapping_Keys(dict);
5258         Py_DECREF(dict);
5259         if (all == NULL)
5260             return -1;
5261         skip_leading_underscores = 1;
5262     }
5263 
5264     for (pos = 0, err = 0; ; pos++) {
5265         name = PySequence_GetItem(all, pos);
5266         if (name == NULL) {
5267             if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
5268                 err = -1;
5269             }
5270             else {
5271                 _PyErr_Clear(tstate);
5272             }
5273             break;
5274         }
5275         if (!PyUnicode_Check(name)) {
5276             PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5277             if (modname == NULL) {
5278                 Py_DECREF(name);
5279                 err = -1;
5280                 break;
5281             }
5282             if (!PyUnicode_Check(modname)) {
5283                 _PyErr_Format(tstate, PyExc_TypeError,
5284                               "module __name__ must be a string, not %.100s",
5285                               Py_TYPE(modname)->tp_name);
5286             }
5287             else {
5288                 _PyErr_Format(tstate, PyExc_TypeError,
5289                               "%s in %U.%s must be str, not %.100s",
5290                               skip_leading_underscores ? "Key" : "Item",
5291                               modname,
5292                               skip_leading_underscores ? "__dict__" : "__all__",
5293                               Py_TYPE(name)->tp_name);
5294             }
5295             Py_DECREF(modname);
5296             Py_DECREF(name);
5297             err = -1;
5298             break;
5299         }
5300         if (skip_leading_underscores) {
5301             if (PyUnicode_READY(name) == -1) {
5302                 Py_DECREF(name);
5303                 err = -1;
5304                 break;
5305             }
5306             if (PyUnicode_READ_CHAR(name, 0) == '_') {
5307                 Py_DECREF(name);
5308                 continue;
5309             }
5310         }
5311         value = PyObject_GetAttr(v, name);
5312         if (value == NULL)
5313             err = -1;
5314         else if (PyDict_CheckExact(locals))
5315             err = PyDict_SetItem(locals, name, value);
5316         else
5317             err = PyObject_SetItem(locals, name, value);
5318         Py_DECREF(name);
5319         Py_XDECREF(value);
5320         if (err != 0)
5321             break;
5322     }
5323     Py_DECREF(all);
5324     return err;
5325 }
5326 
5327 static int
check_args_iterable(PyThreadState * tstate,PyObject * func,PyObject * args)5328 check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
5329 {
5330     if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
5331         _PyErr_Format(tstate, PyExc_TypeError,
5332                       "%.200s%.200s argument after * "
5333                       "must be an iterable, not %.200s",
5334                       PyEval_GetFuncName(func),
5335                       PyEval_GetFuncDesc(func),
5336                       args->ob_type->tp_name);
5337         return -1;
5338     }
5339     return 0;
5340 }
5341 
5342 static void
format_kwargs_error(PyThreadState * tstate,PyObject * func,PyObject * kwargs)5343 format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
5344 {
5345     /* _PyDict_MergeEx raises attribute
5346      * error (percolated from an attempt
5347      * to get 'keys' attribute) instead of
5348      * a type error if its second argument
5349      * is not a mapping.
5350      */
5351     if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
5352         _PyErr_Format(tstate, PyExc_TypeError,
5353                       "%.200s%.200s argument after ** "
5354                       "must be a mapping, not %.200s",
5355                       PyEval_GetFuncName(func),
5356                       PyEval_GetFuncDesc(func),
5357                       kwargs->ob_type->tp_name);
5358     }
5359     else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
5360         PyObject *exc, *val, *tb;
5361         _PyErr_Fetch(tstate, &exc, &val, &tb);
5362         if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
5363             PyObject *key = PyTuple_GET_ITEM(val, 0);
5364             if (!PyUnicode_Check(key)) {
5365                 _PyErr_Format(tstate, PyExc_TypeError,
5366                               "%.200s%.200s keywords must be strings",
5367                               PyEval_GetFuncName(func),
5368                               PyEval_GetFuncDesc(func));
5369             }
5370             else {
5371                 _PyErr_Format(tstate, PyExc_TypeError,
5372                               "%.200s%.200s got multiple "
5373                               "values for keyword argument '%U'",
5374                               PyEval_GetFuncName(func),
5375                               PyEval_GetFuncDesc(func),
5376                               key);
5377             }
5378             Py_XDECREF(exc);
5379             Py_XDECREF(val);
5380             Py_XDECREF(tb);
5381         }
5382         else {
5383             _PyErr_Restore(tstate, exc, val, tb);
5384         }
5385     }
5386 }
5387 
5388 static void
format_exc_check_arg(PyThreadState * tstate,PyObject * exc,const char * format_str,PyObject * obj)5389 format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5390                      const char *format_str, PyObject *obj)
5391 {
5392     const char *obj_str;
5393 
5394     if (!obj)
5395         return;
5396 
5397     obj_str = PyUnicode_AsUTF8(obj);
5398     if (!obj_str)
5399         return;
5400 
5401     _PyErr_Format(tstate, exc, format_str, obj_str);
5402 }
5403 
5404 static void
format_exc_unbound(PyThreadState * tstate,PyCodeObject * co,int oparg)5405 format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
5406 {
5407     PyObject *name;
5408     /* Don't stomp existing exception */
5409     if (_PyErr_Occurred(tstate))
5410         return;
5411     if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5412         name = PyTuple_GET_ITEM(co->co_cellvars,
5413                                 oparg);
5414         format_exc_check_arg(tstate,
5415             PyExc_UnboundLocalError,
5416             UNBOUNDLOCAL_ERROR_MSG,
5417             name);
5418     } else {
5419         name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5420                                 PyTuple_GET_SIZE(co->co_cellvars));
5421         format_exc_check_arg(tstate, PyExc_NameError,
5422                              UNBOUNDFREE_ERROR_MSG, name);
5423     }
5424 }
5425 
5426 static void
format_awaitable_error(PyThreadState * tstate,PyTypeObject * type,int prevopcode)5427 format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevopcode)
5428 {
5429     if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5430         if (prevopcode == BEFORE_ASYNC_WITH) {
5431             _PyErr_Format(tstate, PyExc_TypeError,
5432                           "'async with' received an object from __aenter__ "
5433                           "that does not implement __await__: %.100s",
5434                           type->tp_name);
5435         }
5436         else if (prevopcode == WITH_CLEANUP_START) {
5437             _PyErr_Format(tstate, PyExc_TypeError,
5438                           "'async with' received an object from __aexit__ "
5439                           "that does not implement __await__: %.100s",
5440                           type->tp_name);
5441         }
5442     }
5443 }
5444 
5445 static PyObject *
unicode_concatenate(PyThreadState * tstate,PyObject * v,PyObject * w,PyFrameObject * f,const _Py_CODEUNIT * next_instr)5446 unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
5447                     PyFrameObject *f, const _Py_CODEUNIT *next_instr)
5448 {
5449     PyObject *res;
5450     if (Py_REFCNT(v) == 2) {
5451         /* In the common case, there are 2 references to the value
5452          * stored in 'variable' when the += is performed: one on the
5453          * value stack (in 'v') and one still stored in the
5454          * 'variable'.  We try to delete the variable now to reduce
5455          * the refcnt to 1.
5456          */
5457         int opcode, oparg;
5458         NEXTOPARG();
5459         switch (opcode) {
5460         case STORE_FAST:
5461         {
5462             PyObject **fastlocals = f->f_localsplus;
5463             if (GETLOCAL(oparg) == v)
5464                 SETLOCAL(oparg, NULL);
5465             break;
5466         }
5467         case STORE_DEREF:
5468         {
5469             PyObject **freevars = (f->f_localsplus +
5470                                    f->f_code->co_nlocals);
5471             PyObject *c = freevars[oparg];
5472             if (PyCell_GET(c) ==  v) {
5473                 PyCell_SET(c, NULL);
5474                 Py_DECREF(v);
5475             }
5476             break;
5477         }
5478         case STORE_NAME:
5479         {
5480             PyObject *names = f->f_code->co_names;
5481             PyObject *name = GETITEM(names, oparg);
5482             PyObject *locals = f->f_locals;
5483             if (locals && PyDict_CheckExact(locals)) {
5484                 PyObject *w = PyDict_GetItemWithError(locals, name);
5485                 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
5486                     (w == NULL && _PyErr_Occurred(tstate)))
5487                 {
5488                     Py_DECREF(v);
5489                     return NULL;
5490                 }
5491             }
5492             break;
5493         }
5494         }
5495     }
5496     res = v;
5497     PyUnicode_Append(&res, w);
5498     return res;
5499 }
5500 
5501 #ifdef DYNAMIC_EXECUTION_PROFILE
5502 
5503 static PyObject *
getarray(long a[256])5504 getarray(long a[256])
5505 {
5506     int i;
5507     PyObject *l = PyList_New(256);
5508     if (l == NULL) return NULL;
5509     for (i = 0; i < 256; i++) {
5510         PyObject *x = PyLong_FromLong(a[i]);
5511         if (x == NULL) {
5512             Py_DECREF(l);
5513             return NULL;
5514         }
5515         PyList_SET_ITEM(l, i, x);
5516     }
5517     for (i = 0; i < 256; i++)
5518         a[i] = 0;
5519     return l;
5520 }
5521 
5522 PyObject *
_Py_GetDXProfile(PyObject * self,PyObject * args)5523 _Py_GetDXProfile(PyObject *self, PyObject *args)
5524 {
5525 #ifndef DXPAIRS
5526     return getarray(dxp);
5527 #else
5528     int i;
5529     PyObject *l = PyList_New(257);
5530     if (l == NULL) return NULL;
5531     for (i = 0; i < 257; i++) {
5532         PyObject *x = getarray(dxpairs[i]);
5533         if (x == NULL) {
5534             Py_DECREF(l);
5535             return NULL;
5536         }
5537         PyList_SET_ITEM(l, i, x);
5538     }
5539     return l;
5540 #endif
5541 }
5542 
5543 #endif
5544 
5545 Py_ssize_t
_PyEval_RequestCodeExtraIndex(freefunc free)5546 _PyEval_RequestCodeExtraIndex(freefunc free)
5547 {
5548     PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
5549     Py_ssize_t new_index;
5550 
5551     if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
5552         return -1;
5553     }
5554     new_index = interp->co_extra_user_count++;
5555     interp->co_extra_freefuncs[new_index] = free;
5556     return new_index;
5557 }
5558 
5559 static void
dtrace_function_entry(PyFrameObject * f)5560 dtrace_function_entry(PyFrameObject *f)
5561 {
5562     const char *filename;
5563     const char *funcname;
5564     int lineno;
5565 
5566     filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5567     funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5568     lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5569 
5570     PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5571 }
5572 
5573 static void
dtrace_function_return(PyFrameObject * f)5574 dtrace_function_return(PyFrameObject *f)
5575 {
5576     const char *filename;
5577     const char *funcname;
5578     int lineno;
5579 
5580     filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5581     funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5582     lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5583 
5584     PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5585 }
5586 
5587 /* DTrace equivalent of maybe_call_line_trace. */
5588 static void
maybe_dtrace_line(PyFrameObject * frame,int * instr_lb,int * instr_ub,int * instr_prev)5589 maybe_dtrace_line(PyFrameObject *frame,
5590                   int *instr_lb, int *instr_ub, int *instr_prev)
5591 {
5592     int line = frame->f_lineno;
5593     const char *co_filename, *co_name;
5594 
5595     /* If the last instruction executed isn't in the current
5596        instruction window, reset the window.
5597     */
5598     if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5599         PyAddrPair bounds;
5600         line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5601                                        &bounds);
5602         *instr_lb = bounds.ap_lower;
5603         *instr_ub = bounds.ap_upper;
5604     }
5605     /* If the last instruction falls at the start of a line or if
5606        it represents a jump backwards, update the frame's line
5607        number and call the trace function. */
5608     if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5609         frame->f_lineno = line;
5610         co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5611         if (!co_filename)
5612             co_filename = "?";
5613         co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5614         if (!co_name)
5615             co_name = "?";
5616         PyDTrace_LINE(co_filename, co_name, line);
5617     }
5618     *instr_prev = frame->f_lasti;
5619 }
5620