Coverage Report

Created: 2026-06-14 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/Python-3.8.3/Python/ceval.c
Line
Count
Source
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
225
    "name '%.200s' is not defined"
89
#define UNBOUNDLOCAL_ERROR_MSG \
90
0
    "local variable '%.200s' referenced before assignment"
91
#define UNBOUNDFREE_ERROR_MSG \
92
0
    "free variable '%.200s' referenced before assignment" \
93
0
    " 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
76.9k
#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
0
    _Py_atomic_store_relaxed( \
132
0
        &(ceval)->eval_breaker, \
133
0
        GIL_REQUEST | \
134
0
        _Py_atomic_load_relaxed(&(ceval)->signals_pending) | \
135
0
        _Py_atomic_load_relaxed(&(ceval)->pending.calls_to_do) | \
136
0
        (ceval)->pending.async_exc)
137
138
#define SET_GIL_DROP_REQUEST(ceval) \
139
0
    do { \
140
0
        _Py_atomic_store_relaxed(&(ceval)->gil_drop_request, 1); \
141
0
        _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
142
0
    } while (0)
143
144
#define RESET_GIL_DROP_REQUEST(ceval) \
145
0
    do { \
146
0
        _Py_atomic_store_relaxed(&(ceval)->gil_drop_request, 0); \
147
0
        COMPUTE_EVAL_BREAKER(ceval); \
148
0
    } while (0)
149
150
/* Pending calls are only modified under pending_lock */
151
#define SIGNAL_PENDING_CALLS(ceval) \
152
0
    do { \
153
0
        _Py_atomic_store_relaxed(&(ceval)->pending.calls_to_do, 1); \
154
0
        _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
155
0
    } while (0)
156
157
#define UNSIGNAL_PENDING_CALLS(ceval) \
158
0
    do { \
159
0
        _Py_atomic_store_relaxed(&(ceval)->pending.calls_to_do, 0); \
160
0
        COMPUTE_EVAL_BREAKER(ceval); \
161
0
    } while (0)
162
163
#define SIGNAL_PENDING_SIGNALS(ceval) \
164
0
    do { \
165
0
        _Py_atomic_store_relaxed(&(ceval)->signals_pending, 1); \
166
0
        _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
167
0
    } while (0)
168
169
#define UNSIGNAL_PENDING_SIGNALS(ceval) \
170
0
    do { \
171
0
        _Py_atomic_store_relaxed(&(ceval)->signals_pending, 0); \
172
0
        COMPUTE_EVAL_BREAKER(ceval); \
173
0
    } while (0)
174
175
#define SIGNAL_ASYNC_EXC(ceval) \
176
0
    do { \
177
0
        (ceval)->pending.async_exc = 1; \
178
0
        _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
179
0
    } while (0)
180
181
#define UNSIGNAL_ASYNC_EXC(ceval) \
182
0
    do { \
183
0
        (ceval)->pending.async_exc = 0; \
184
0
        COMPUTE_EVAL_BREAKER(ceval); \
185
0
    } 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
195
PyEval_ThreadsInitialized(void)
196
0
{
197
0
    return gil_created(&_PyRuntime.ceval.gil);
198
0
}
199
200
void
201
PyEval_InitThreads(void)
202
13
{
203
13
    _PyRuntimeState *runtime = &_PyRuntime;
204
13
    struct _ceval_runtime_state *ceval = &runtime->ceval;
205
13
    struct _gil_runtime_state *gil = &ceval->gil;
206
13
    if (gil_created(gil)) {
207
0
        return;
208
0
    }
209
210
13
    PyThread_init_thread();
211
13
    create_gil(gil);
212
13
    PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
213
13
    take_gil(ceval, tstate);
214
215
13
    struct _pending_calls *pending = &ceval->pending;
216
13
    pending->lock = PyThread_allocate_lock();
217
13
    if (pending->lock == NULL) {
218
0
        Py_FatalError("Can't initialize threads for pending calls");
219
0
    }
220
13
}
221
222
void
223
_PyEval_FiniThreads(struct _ceval_runtime_state *ceval)
224
13
{
225
13
    struct _gil_runtime_state *gil = &ceval->gil;
226
13
    if (!gil_created(gil)) {
227
13
        return;
228
13
    }
229
230
0
    destroy_gil(gil);
231
0
    assert(!gil_created(gil));
232
233
0
    struct _pending_calls *pending = &ceval->pending;
234
0
    if (pending->lock != NULL) {
235
0
        PyThread_free_lock(pending->lock);
236
0
        pending->lock = NULL;
237
0
    }
238
0
}
239
240
static inline void
241
exit_thread_if_finalizing(_PyRuntimeState *runtime, PyThreadState *tstate)
242
7.40k
{
243
    /* _Py_Finalizing is protected by the GIL */
244
7.40k
    if (runtime->finalizing != NULL && !_Py_CURRENTLY_FINALIZING(runtime, tstate)) {
245
0
        drop_gil(&runtime->ceval, tstate);
246
0
        PyThread_exit_thread();
247
0
    }
248
7.40k
}
249
250
void
251
_PyEval_Fini(void)
252
0
{
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
0
}
278
279
void
280
PyEval_AcquireLock(void)
281
0
{
282
0
    _PyRuntimeState *runtime = &_PyRuntime;
283
0
    struct _ceval_runtime_state *ceval = &runtime->ceval;
284
0
    PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
285
0
    if (tstate == NULL) {
286
0
        Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
287
0
    }
288
0
    take_gil(ceval, tstate);
289
0
    exit_thread_if_finalizing(runtime, tstate);
290
0
}
291
292
void
293
PyEval_ReleaseLock(void)
294
0
{
295
0
    _PyRuntimeState *runtime = &_PyRuntime;
296
0
    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
0
    drop_gil(&runtime->ceval, tstate);
302
0
}
303
304
void
305
PyEval_AcquireThread(PyThreadState *tstate)
306
0
{
307
0
    if (tstate == NULL) {
308
0
        Py_FatalError("PyEval_AcquireThread: NULL new thread state");
309
0
    }
310
311
0
    _PyRuntimeState *runtime = &_PyRuntime;
312
0
    struct _ceval_runtime_state *ceval = &runtime->ceval;
313
314
    /* Check someone has called PyEval_InitThreads() to create the lock */
315
0
    assert(gil_created(&ceval->gil));
316
0
    take_gil(ceval, tstate);
317
0
    exit_thread_if_finalizing(runtime, tstate);
318
0
    if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
319
0
        Py_FatalError("PyEval_AcquireThread: non-NULL old thread state");
320
0
    }
321
0
}
322
323
void
324
PyEval_ReleaseThread(PyThreadState *tstate)
325
0
{
326
0
    if (tstate == NULL) {
327
0
        Py_FatalError("PyEval_ReleaseThread: NULL thread state");
328
0
    }
329
330
0
    _PyRuntimeState *runtime = &_PyRuntime;
331
0
    PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
332
0
    if (new_tstate != tstate) {
333
0
        Py_FatalError("PyEval_ReleaseThread: wrong thread state");
334
0
    }
335
0
    drop_gil(&runtime->ceval, tstate);
336
0
}
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
344
_PyEval_ReInitThreads(_PyRuntimeState *runtime)
345
0
{
346
0
    struct _ceval_runtime_state *ceval = &runtime->ceval;
347
0
    if (!gil_created(&ceval->gil)) {
348
0
        return;
349
0
    }
350
0
    recreate_gil(&ceval->gil);
351
0
    PyThreadState *current_tstate = _PyRuntimeState_GetThreadState(runtime);
352
0
    take_gil(ceval, current_tstate);
353
354
0
    struct _pending_calls *pending = &ceval->pending;
355
0
    pending->lock = PyThread_allocate_lock();
356
0
    if (pending->lock == NULL) {
357
0
        Py_FatalError("Can't initialize threads for pending calls");
358
0
    }
359
360
    /* Destroy all threads except the current one */
361
0
    _PyThreadState_DeleteExcept(runtime, current_tstate);
362
0
}
363
364
/* This function is used to signal that async exceptions are waiting to be
365
   raised. */
366
367
void
368
_PyEval_SignalAsyncExc(struct _ceval_runtime_state *ceval)
369
0
{
370
0
    SIGNAL_ASYNC_EXC(ceval);
371
0
}
372
373
PyThreadState *
374
PyEval_SaveThread(void)
375
7.40k
{
376
7.40k
    _PyRuntimeState *runtime = &_PyRuntime;
377
7.40k
    struct _ceval_runtime_state *ceval = &runtime->ceval;
378
7.40k
    PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
379
7.40k
    if (tstate == NULL) {
380
0
        Py_FatalError("PyEval_SaveThread: NULL tstate");
381
0
    }
382
7.40k
    assert(gil_created(&ceval->gil));
383
7.40k
    drop_gil(ceval, tstate);
384
7.40k
    return tstate;
385
7.40k
}
386
387
void
388
PyEval_RestoreThread(PyThreadState *tstate)
389
7.40k
{
390
7.40k
    _PyRuntimeState *runtime = &_PyRuntime;
391
7.40k
    struct _ceval_runtime_state *ceval = &runtime->ceval;
392
393
7.40k
    if (tstate == NULL) {
394
0
        Py_FatalError("PyEval_RestoreThread: NULL tstate");
395
0
    }
396
7.40k
    assert(gil_created(&ceval->gil));
397
398
7.40k
    int err = errno;
399
7.40k
    take_gil(ceval, tstate);
400
7.40k
    exit_thread_if_finalizing(runtime, tstate);
401
7.40k
    errno = err;
402
403
7.40k
    _PyThreadState_Swap(&runtime->gilstate, tstate);
404
7.40k
}
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
430
_PyEval_SignalReceived(struct _ceval_runtime_state *ceval)
431
0
{
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
0
    SIGNAL_PENDING_SIGNALS(ceval);
436
0
}
437
438
/* Push one item onto the queue while holding the lock. */
439
static int
440
_push_pending_call(struct _pending_calls *pending,
441
                   int (*func)(void *), void *arg)
442
0
{
443
0
    int i = pending->last;
444
0
    int j = (i + 1) % NPENDINGCALLS;
445
0
    if (j == pending->first) {
446
0
        return -1; /* Queue full */
447
0
    }
448
0
    pending->calls[i].func = func;
449
0
    pending->calls[i].arg = arg;
450
0
    pending->last = j;
451
0
    return 0;
452
0
}
453
454
/* Pop one item off the queue while holding the lock. */
455
static void
456
_pop_pending_call(struct _pending_calls *pending,
457
                  int (**func)(void *), void **arg)
458
0
{
459
0
    int i = pending->first;
460
0
    if (i == pending->last) {
461
0
        return; /* Queue empty */
462
0
    }
463
464
0
    *func = pending->calls[i].func;
465
0
    *arg = pending->calls[i].arg;
466
0
    pending->first = (i + 1) % NPENDINGCALLS;
467
0
}
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
475
_PyEval_AddPendingCall(PyThreadState *tstate,
476
                       struct _ceval_runtime_state *ceval,
477
                       int (*func)(void *), void *arg)
478
0
{
479
0
    struct _pending_calls *pending = &ceval->pending;
480
481
0
    PyThread_acquire_lock(pending->lock, WAIT_LOCK);
482
0
    if (pending->finishing) {
483
0
        PyThread_release_lock(pending->lock);
484
485
0
        PyObject *exc, *val, *tb;
486
0
        _PyErr_Fetch(tstate, &exc, &val, &tb);
487
0
        _PyErr_SetString(tstate, PyExc_SystemError,
488
0
                        "Py_AddPendingCall: cannot add pending calls "
489
0
                        "(Python shutting down)");
490
0
        _PyErr_Print(tstate);
491
0
        _PyErr_Restore(tstate, exc, val, tb);
492
0
        return -1;
493
0
    }
494
0
    int result = _push_pending_call(pending, func, arg);
495
0
    PyThread_release_lock(pending->lock);
496
497
    /* signal main loop */
498
0
    SIGNAL_PENDING_CALLS(ceval);
499
0
    return result;
500
0
}
501
502
int
503
Py_AddPendingCall(int (*func)(void *), void *arg)
504
0
{
505
0
    _PyRuntimeState *runtime = &_PyRuntime;
506
0
    PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
507
0
    return _PyEval_AddPendingCall(tstate, &runtime->ceval, func, arg);
508
0
}
509
510
static int
511
handle_signals(_PyRuntimeState *runtime)
512
0
{
513
    /* Only handle signals on main thread.  PyEval_InitThreads must
514
     * have been called already.
515
     */
516
0
    if (PyThread_get_thread_ident() != runtime->main_thread) {
517
0
        return 0;
518
0
    }
519
    /*
520
     * Ensure that the thread isn't currently running some other
521
     * interpreter.
522
     */
523
0
    PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
524
0
    if (interp != runtime->interpreters.main) {
525
0
        return 0;
526
0
    }
527
528
0
    struct _ceval_runtime_state *ceval = &runtime->ceval;
529
0
    UNSIGNAL_PENDING_SIGNALS(ceval);
530
0
    if (_PyErr_CheckSignals() < 0) {
531
0
        SIGNAL_PENDING_SIGNALS(ceval); /* We're not done yet */
532
0
        return -1;
533
0
    }
534
0
    return 0;
535
0
}
536
537
static int
538
make_pending_calls(_PyRuntimeState *runtime)
539
0
{
540
0
    static int busy = 0;
541
542
    /* only service pending calls on main thread */
543
0
    if (PyThread_get_thread_ident() != runtime->main_thread) {
544
0
        return 0;
545
0
    }
546
547
    /* don't perform recursive pending calls */
548
0
    if (busy) {
549
0
        return 0;
550
0
    }
551
0
    busy = 1;
552
0
    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
0
    UNSIGNAL_PENDING_CALLS(ceval);
556
0
    int res = 0;
557
558
    /* perform a bounded number of calls, in case of recursion */
559
0
    struct _pending_calls *pending = &ceval->pending;
560
0
    for (int i=0; i<NPENDINGCALLS; i++) {
561
0
        int (*func)(void *) = NULL;
562
0
        void *arg = NULL;
563
564
        /* pop one item off the queue while holding the lock */
565
0
        PyThread_acquire_lock(pending->lock, WAIT_LOCK);
566
0
        _pop_pending_call(pending, &func, &arg);
567
0
        PyThread_release_lock(pending->lock);
568
569
        /* having released the lock, perform the callback */
570
0
        if (func == NULL) {
571
0
            break;
572
0
        }
573
0
        res = func(arg);
574
0
        if (res) {
575
0
            goto error;
576
0
        }
577
0
    }
578
579
0
    busy = 0;
580
0
    return res;
581
582
0
error:
583
0
    busy = 0;
584
0
    SIGNAL_PENDING_CALLS(ceval);
585
0
    return res;
586
0
}
587
588
void
589
_Py_FinishPendingCalls(_PyRuntimeState *runtime)
590
0
{
591
0
    assert(PyGILState_Check());
592
593
0
    PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
594
0
    struct _pending_calls *pending = &runtime->ceval.pending;
595
596
0
    PyThread_acquire_lock(pending->lock, WAIT_LOCK);
597
0
    pending->finishing = 1;
598
0
    PyThread_release_lock(pending->lock);
599
600
0
    if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
601
0
        return;
602
0
    }
603
604
0
    if (make_pending_calls(runtime) < 0) {
605
0
        PyObject *exc, *val, *tb;
606
0
        _PyErr_Fetch(tstate, &exc, &val, &tb);
607
0
        PyErr_BadInternalCall();
608
0
        _PyErr_ChainExceptions(exc, val, tb);
609
0
        _PyErr_Print(tstate);
610
0
    }
611
0
}
612
613
/* Py_MakePendingCalls() is a simple wrapper for the sake
614
   of backward-compatibility. */
615
int
616
Py_MakePendingCalls(void)
617
0
{
618
0
    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
0
    _PyRuntimeState *runtime = &_PyRuntime;
623
0
    int res = handle_signals(runtime);
624
0
    if (res != 0) {
625
0
        return res;
626
0
    }
627
628
0
    res = make_pending_calls(runtime);
629
0
    if (res != 0) {
630
0
        return res;
631
0
    }
632
633
0
    return 0;
634
0
}
635
636
/* The interpreter's recursion limit */
637
638
#ifndef Py_DEFAULT_RECURSION_LIMIT
639
26
#define Py_DEFAULT_RECURSION_LIMIT 1000
640
#endif
641
642
int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
643
644
void
645
_PyEval_Initialize(struct _ceval_runtime_state *state)
646
13
{
647
13
    state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
648
13
    _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
649
13
    _gil_initialize(&state->gil);
650
13
}
651
652
int
653
Py_GetRecursionLimit(void)
654
15
{
655
15
    return _PyRuntime.ceval.recursion_limit;
656
15
}
657
658
void
659
Py_SetRecursionLimit(int new_limit)
660
0
{
661
0
    struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
662
0
    ceval->recursion_limit = new_limit;
663
0
    _Py_CheckRecursionLimit = ceval->recursion_limit;
664
0
}
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
672
_Py_CheckRecursiveCall(const char *where)
673
0
{
674
0
    _PyRuntimeState *runtime = &_PyRuntime;
675
0
    PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
676
0
    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
0
    if (tstate->recursion_critical)
689
        /* Somebody asked that we don't check for recursion. */
690
0
        return 0;
691
0
    if (tstate->overflowed) {
692
0
        if (tstate->recursion_depth > recursion_limit + 50) {
693
            /* Overflowing while handling an overflow. Give up. */
694
0
            Py_FatalError("Cannot recover from stack overflow.");
695
0
        }
696
0
        return 0;
697
0
    }
698
0
    if (tstate->recursion_depth > recursion_limit) {
699
0
        --tstate->recursion_depth;
700
0
        tstate->overflowed = 1;
701
0
        _PyErr_Format(tstate, PyExc_RecursionError,
702
0
                      "maximum recursion depth exceeded%s",
703
0
                      where);
704
0
        return -1;
705
0
    }
706
0
    return 0;
707
0
}
708
709
static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
710
static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
711
712
2.17M
#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
713
714
715
PyObject *
716
PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
717
2.75k
{
718
2.75k
    return PyEval_EvalCodeEx(co,
719
2.75k
                      globals, locals,
720
2.75k
                      (PyObject **)NULL, 0,
721
2.75k
                      (PyObject **)NULL, 0,
722
2.75k
                      (PyObject **)NULL, 0,
723
2.75k
                      NULL, NULL);
724
2.75k
}
725
726
727
/* Interpreter main loop */
728
729
PyObject *
730
0
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
0
    return PyEval_EvalFrameEx(f, 0);
735
0
}
736
737
PyObject *
738
PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
739
38.4k
{
740
38.4k
    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
741
38.4k
    return interp->eval_frame(f, throwflag);
742
38.4k
}
743
744
PyObject* _Py_HOT_FUNCTION
745
_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
746
38.4k
{
747
#ifdef DXPAIRS
748
    int lastopcode = 0;
749
#endif
750
38.4k
    PyObject **stack_pointer;  /* Next free slot in value stack */
751
38.4k
    const _Py_CODEUNIT *next_instr;
752
38.4k
    int opcode;        /* Current opcode */
753
38.4k
    int oparg;         /* Current opcode argument, if any */
754
38.4k
    PyObject **fastlocals, **freevars;
755
38.4k
    PyObject *retval = NULL;            /* Return value */
756
38.4k
    _PyRuntimeState * const runtime = &_PyRuntime;
757
38.4k
    PyThreadState * const tstate = _PyRuntimeState_GetThreadState(runtime);
758
38.4k
    struct _ceval_runtime_state * const ceval = &runtime->ceval;
759
38.4k
    _Py_atomic_int * const eval_breaker = &ceval->eval_breaker;
760
38.4k
    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
38.4k
    int instr_ub = -1, instr_lb = 0, instr_prev = -1;
770
771
38.4k
    const _Py_CODEUNIT *first_instr;
772
38.4k
    PyObject *names;
773
38.4k
    PyObject *consts;
774
38.4k
    _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
38.4k
#ifdef HAVE_COMPUTED_GOTOS
825
38.4k
    #ifndef USE_COMPUTED_GOTOS
826
38.4k
    #define USE_COMPUTED_GOTOS 1
827
38.4k
    #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
38.4k
#if USE_COMPUTED_GOTOS
837
/* Import the static jump table */
838
38.4k
#include "opcode_targets.h"
839
840
38.4k
#define TARGET(op) \
841
52.6k
    op: \
842
1.09M
    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
38.4k
#define FAST_DISPATCH() \
856
448k
    { \
857
1.04M
        if (!_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \
858
1.04M
            f->f_lasti = INSTR_OFFSET(); \
859
1.04M
            NEXTOPARG(); \
860
1.04M
            goto *opcode_targets[opcode]; \
861
1.04M
        } \
862
448k
        goto fast_next_opcode; \
863
1.04M
    }
864
38.4k
#endif
865
866
38.4k
#define DISPATCH() \
867
38.4k
    { \
868
536k
        if (!_Py_atomic_load_relaxed(eval_breaker)) { \
869
536k
            FAST_DISPATCH(); \
870
0
        } \
871
0
        continue; \
872
0
    }
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
38.4k
#ifndef Py_DEBUG
884
367k
#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
38.4k
#define INSTR_OFFSET()  \
893
1.10M
    (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
894
1.09M
#define NEXTOPARG()  do { \
895
1.09M
        _Py_CODEUNIT word = *next_instr; \
896
1.09M
        opcode = _Py_OPCODE(word); \
897
1.09M
        oparg = _Py_OPARG(word); \
898
1.09M
        next_instr++; \
899
1.09M
    } while (0)
900
55.2k
#define JUMPTO(x)       (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
901
38.4k
#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
38.4k
#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
930
163k
#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
366k
#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
70.1k
#define STACK_LEVEL()     ((int)(stack_pointer - f->f_valuestack))
951
38.4k
#define EMPTY()           (STACK_LEVEL() == 0)
952
222k
#define TOP()             (stack_pointer[-1])
953
38.4k
#define SECOND()          (stack_pointer[-2])
954
38.4k
#define THIRD()           (stack_pointer[-3])
955
38.4k
#define FOURTH()          (stack_pointer[-4])
956
47.0k
#define PEEK(n)           (stack_pointer[-(n)])
957
166k
#define SET_TOP(v)        (stack_pointer[-1] = (v))
958
38.4k
#define SET_SECOND(v)     (stack_pointer[-2] = (v))
959
38.4k
#define SET_THIRD(v)      (stack_pointer[-3] = (v))
960
38.4k
#define SET_FOURTH(v)     (stack_pointer[-4] = (v))
961
38.4k
#define SET_VALUE(n, v)   (stack_pointer[-(n)] = (v))
962
38.4k
#define BASIC_STACKADJ(n) (stack_pointer += n)
963
651k
#define BASIC_PUSH(v)     (*stack_pointer++ = (v))
964
394k
#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
651k
#define PUSH(v)                BASIC_PUSH(v)
989
394k
#define POP()                  BASIC_POP()
990
38.4k
#define STACK_GROW(n)          BASIC_STACKADJ(n)
991
38.4k
#define STACK_SHRINK(n)        BASIC_STACKADJ(-n)
992
203k
#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
993
38.4k
#endif
994
995
/* Local variable macros */
996
997
402k
#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
92.6k
#define SETLOCAL(i, value)      do { PyObject *tmp = GETLOCAL(i); \
1006
92.6k
                                     GETLOCAL(i) = value; \
1007
92.6k
                                     Py_XDECREF(tmp); } while (0)
1008
1009
1010
38.4k
#define UNWIND_BLOCK(b) \
1011
38.4k
    while (STACK_LEVEL() > (b)->b_level) { \
1012
3.47k
        PyObject *v = POP(); \
1013
3.47k
        Py_XDECREF(v); \
1014
3.47k
    }
1015
1016
38.4k
#define UNWIND_EXCEPT_HANDLER(b) \
1017
38.4k
    do { \
1018
290
        PyObject *type, *value, *traceback; \
1019
290
        _PyErr_StackItem *exc_info; \
1020
290
        assert(STACK_LEVEL() >= (b)->b_level + 3); \
1021
497
        while (STACK_LEVEL() > (b)->b_level + 3) { \
1022
207
            value = POP(); \
1023
207
            Py_XDECREF(value); \
1024
207
        } \
1025
290
        exc_info = tstate->exc_info; \
1026
290
        type = exc_info->exc_type; \
1027
290
        value = exc_info->exc_value; \
1028
290
        traceback = exc_info->exc_traceback; \
1029
290
        exc_info->exc_type = POP(); \
1030
290
        exc_info->exc_value = POP(); \
1031
290
        exc_info->exc_traceback = POP(); \
1032
290
        Py_XDECREF(type); \
1033
290
        Py_XDECREF(value); \
1034
290
        Py_XDECREF(traceback); \
1035
290
    } while(0)
1036
1037
    /* macros for opcode cache */
1038
38.4k
#define OPCACHE_CHECK() \
1039
73.4k
    do { \
1040
73.4k
        co_opcache = NULL; \
1041
73.4k
        if (co->co_opcache != NULL) { \
1042
0
            unsigned char co_opt_offset = \
1043
0
                co->co_opcache_map[next_instr - first_instr]; \
1044
0
            if (co_opt_offset > 0) { \
1045
0
                assert(co_opt_offset <= co->co_opcache_size); \
1046
0
                co_opcache = &co->co_opcache[co_opt_offset - 1]; \
1047
0
                assert(co_opcache != NULL); \
1048
0
            } \
1049
0
        } \
1050
73.4k
    } 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
38.4k
#define OPCACHE_STAT_GLOBAL_HIT()
1072
38.4k
#define OPCACHE_STAT_GLOBAL_MISS()
1073
38.4k
#define OPCACHE_STAT_GLOBAL_OPT()
1074
1075
38.4k
#endif
1076
1077
/* Start of code */
1078
1079
    /* push frame */
1080
38.4k
    if (Py_EnterRecursiveCall(""))
1081
0
        return NULL;
1082
1083
38.4k
    tstate->frame = f;
1084
1085
38.4k
    if (tstate->use_tracing) {
1086
0
        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
0
            if (call_trace_protected(tstate->c_tracefunc,
1101
0
                                     tstate->c_traceobj,
1102
0
                                     tstate, f, PyTrace_CALL, Py_None)) {
1103
                /* Trace function raised an error */
1104
0
                goto exit_eval_frame;
1105
0
            }
1106
0
        }
1107
0
        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
0
            if (call_trace_protected(tstate->c_profilefunc,
1111
0
                                     tstate->c_profileobj,
1112
0
                                     tstate, f, PyTrace_CALL, Py_None)) {
1113
                /* Profile function raised an error */
1114
0
                goto exit_eval_frame;
1115
0
            }
1116
0
        }
1117
0
    }
1118
1119
38.4k
    if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1120
0
        dtrace_function_entry(f);
1121
1122
38.4k
    co = f->f_code;
1123
38.4k
    names = co->co_names;
1124
38.4k
    consts = co->co_consts;
1125
38.4k
    fastlocals = f->f_localsplus;
1126
38.4k
    freevars = f->f_localsplus + co->co_nlocals;
1127
38.4k
    assert(PyBytes_Check(co->co_code));
1128
38.4k
    assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
1129
38.4k
    assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1130
38.4k
    assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1131
38.4k
    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
38.4k
    assert(f->f_lasti >= -1);
1148
38.4k
    next_instr = first_instr;
1149
38.4k
    if (f->f_lasti >= 0) {
1150
809
        assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1151
809
        next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
1152
809
    }
1153
38.4k
    stack_pointer = f->f_stacktop;
1154
38.4k
    assert(stack_pointer != NULL);
1155
38.4k
    f->f_stacktop = NULL;       /* remains NULL unless yield suspends frame */
1156
38.4k
    f->f_executing = 1;
1157
1158
38.4k
    if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1159
38.4k
        co->co_opcache_flag++;
1160
38.4k
        if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1161
1
            if (_PyCode_InitOpcache(co) < 0) {
1162
0
                goto exit_eval_frame;
1163
0
            }
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
1
        }
1171
38.4k
    }
1172
1173
#ifdef LLTRACE
1174
    lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
1175
#endif
1176
1177
38.4k
    if (throwflag) /* support for generator.throw() */
1178
45
        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
41.8k
main_loop:
1188
41.8k
    for (;;) {
1189
41.8k
        assert(stack_pointer >= f->f_valuestack); /* else underflow */
1190
41.8k
        assert(STACK_LEVEL() <= co->co_stacksize);  /* else overflow */
1191
41.8k
        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
41.8k
        if (_Py_atomic_load_relaxed(eval_breaker)) {
1202
0
            opcode = _Py_OPCODE(*next_instr);
1203
0
            if (opcode == SETUP_FINALLY ||
1204
0
                opcode == SETUP_WITH ||
1205
0
                opcode == BEFORE_ASYNC_WITH ||
1206
0
                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
0
                goto fast_next_opcode;
1224
0
            }
1225
1226
0
            if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
1227
0
                if (handle_signals(runtime) != 0) {
1228
0
                    goto error;
1229
0
                }
1230
0
            }
1231
0
            if (_Py_atomic_load_relaxed(&ceval->pending.calls_to_do)) {
1232
0
                if (make_pending_calls(runtime) != 0) {
1233
0
                    goto error;
1234
0
                }
1235
0
            }
1236
1237
0
            if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) {
1238
                /* Give another thread a chance */
1239
0
                if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
1240
0
                    Py_FatalError("ceval: tstate mix-up");
1241
0
                }
1242
0
                drop_gil(ceval, tstate);
1243
1244
                /* Other threads may run now */
1245
1246
0
                take_gil(ceval, tstate);
1247
1248
                /* Check if we should make a quick exit. */
1249
0
                exit_thread_if_finalizing(runtime, tstate);
1250
1251
0
                if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
1252
0
                    Py_FatalError("ceval: orphan tstate");
1253
0
                }
1254
0
            }
1255
            /* Check for asynchronous exceptions. */
1256
0
            if (tstate->async_exc != NULL) {
1257
0
                PyObject *exc = tstate->async_exc;
1258
0
                tstate->async_exc = NULL;
1259
0
                UNSIGNAL_ASYNC_EXC(ceval);
1260
0
                _PyErr_SetNone(tstate, exc);
1261
0
                Py_DECREF(exc);
1262
0
                goto error;
1263
0
            }
1264
0
        }
1265
1266
41.8k
    fast_next_opcode:
1267
41.8k
        f->f_lasti = INSTR_OFFSET();
1268
1269
41.8k
        if (PyDTrace_LINE_ENABLED())
1270
0
            maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1271
1272
        /* line-by-line tracing support */
1273
1274
41.8k
        if (_Py_TracingPossible(ceval) &&
1275
0
            tstate->c_tracefunc != NULL && !tstate->tracing) {
1276
0
            int err;
1277
            /* see maybe_call_line_trace
1278
               for expository comments */
1279
0
            f->f_stacktop = stack_pointer;
1280
1281
0
            err = maybe_call_line_trace(tstate->c_tracefunc,
1282
0
                                        tstate->c_traceobj,
1283
0
                                        tstate, f,
1284
0
                                        &instr_lb, &instr_ub, &instr_prev);
1285
            /* Reload possibly changed frame fields */
1286
0
            JUMPTO(f->f_lasti);
1287
0
            if (f->f_stacktop != NULL) {
1288
0
                stack_pointer = f->f_stacktop;
1289
0
                f->f_stacktop = NULL;
1290
0
            }
1291
0
            if (err)
1292
                /* trace function raised an exception */
1293
0
                goto error;
1294
0
        }
1295
1296
        /* Extract opcode and argument */
1297
1298
41.8k
        NEXTOPARG();
1299
52.1k
    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
52.1k
        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
0
        case TARGET(NOP): {
1330
0
            FAST_DISPATCH();
1331
0
        }
1332
1333
205k
        case TARGET(LOAD_FAST): {
1334
205k
            PyObject *value = GETLOCAL(oparg);
1335
205k
            if (value == NULL) {
1336
0
                format_exc_check_arg(tstate, PyExc_UnboundLocalError,
1337
0
                                     UNBOUNDLOCAL_ERROR_MSG,
1338
0
                                     PyTuple_GetItem(co->co_varnames, oparg));
1339
0
                goto error;
1340
0
            }
1341
205k
            Py_INCREF(value);
1342
205k
            PUSH(value);
1343
205k
            FAST_DISPATCH();
1344
0
        }
1345
1346
129k
        case TARGET(LOAD_CONST): {
1347
129k
            PREDICTED(LOAD_CONST);
1348
129k
            PyObject *value = GETITEM(consts, oparg);
1349
129k
            Py_INCREF(value);
1350
129k
            PUSH(value);
1351
129k
            FAST_DISPATCH();
1352
0
        }
1353
1354
56.3k
        case TARGET(STORE_FAST): {
1355
56.3k
            PREDICTED(STORE_FAST);
1356
56.3k
            PyObject *value = POP();
1357
56.3k
            SETLOCAL(oparg, value);
1358
56.3k
            FAST_DISPATCH();
1359
0
        }
1360
1361
32.6k
        case TARGET(POP_TOP): {
1362
32.6k
            PyObject *value = POP();
1363
32.6k
            Py_DECREF(value);
1364
32.6k
            FAST_DISPATCH();
1365
0
        }
1366
1367
2.16k
        case TARGET(ROT_TWO): {
1368
2.16k
            PyObject *top = TOP();
1369
2.16k
            PyObject *second = SECOND();
1370
2.16k
            SET_TOP(second);
1371
2.16k
            SET_SECOND(top);
1372
2.16k
            FAST_DISPATCH();
1373
0
        }
1374
1375
142
        case TARGET(ROT_THREE): {
1376
142
            PyObject *top = TOP();
1377
142
            PyObject *second = SECOND();
1378
142
            PyObject *third = THIRD();
1379
142
            SET_TOP(second);
1380
142
            SET_SECOND(third);
1381
142
            SET_THIRD(top);
1382
142
            FAST_DISPATCH();
1383
0
        }
1384
1385
14
        case TARGET(ROT_FOUR): {
1386
14
            PyObject *top = TOP();
1387
14
            PyObject *second = SECOND();
1388
14
            PyObject *third = THIRD();
1389
14
            PyObject *fourth = FOURTH();
1390
14
            SET_TOP(second);
1391
14
            SET_SECOND(third);
1392
14
            SET_THIRD(fourth);
1393
14
            SET_FOURTH(top);
1394
14
            FAST_DISPATCH();
1395
0
        }
1396
1397
4.81k
        case TARGET(DUP_TOP): {
1398
4.81k
            PyObject *top = TOP();
1399
4.81k
            Py_INCREF(top);
1400
4.81k
            PUSH(top);
1401
4.81k
            FAST_DISPATCH();
1402
0
        }
1403
1404
0
        case TARGET(DUP_TOP_TWO): {
1405
0
            PyObject *top = TOP();
1406
0
            PyObject *second = SECOND();
1407
0
            Py_INCREF(top);
1408
0
            Py_INCREF(second);
1409
0
            STACK_GROW(2);
1410
0
            SET_TOP(top);
1411
0
            SET_SECOND(second);
1412
0
            FAST_DISPATCH();
1413
0
        }
1414
1415
0
        case TARGET(UNARY_POSITIVE): {
1416
0
            PyObject *value = TOP();
1417
0
            PyObject *res = PyNumber_Positive(value);
1418
0
            Py_DECREF(value);
1419
0
            SET_TOP(res);
1420
0
            if (res == NULL)
1421
0
                goto error;
1422
0
            DISPATCH();
1423
0
        }
1424
1425
9
        case TARGET(UNARY_NEGATIVE): {
1426
9
            PyObject *value = TOP();
1427
9
            PyObject *res = PyNumber_Negative(value);
1428
9
            Py_DECREF(value);
1429
9
            SET_TOP(res);
1430
9
            if (res == NULL)
1431
0
                goto error;
1432
9
            DISPATCH();
1433
0
        }
1434
1435
51
        case TARGET(UNARY_NOT): {
1436
51
            PyObject *value = TOP();
1437
51
            int err = PyObject_IsTrue(value);
1438
51
            Py_DECREF(value);
1439
51
            if (err == 0) {
1440
23
                Py_INCREF(Py_True);
1441
23
                SET_TOP(Py_True);
1442
23
                DISPATCH();
1443
0
            }
1444
28
            else if (err > 0) {
1445
28
                Py_INCREF(Py_False);
1446
28
                SET_TOP(Py_False);
1447
28
                DISPATCH();
1448
0
            }
1449
0
            STACK_SHRINK(1);
1450
0
            goto error;
1451
51
        }
1452
1453
20
        case TARGET(UNARY_INVERT): {
1454
20
            PyObject *value = TOP();
1455
20
            PyObject *res = PyNumber_Invert(value);
1456
20
            Py_DECREF(value);
1457
20
            SET_TOP(res);
1458
20
            if (res == NULL)
1459
0
                goto error;
1460
20
            DISPATCH();
1461
0
        }
1462
1463
0
        case TARGET(BINARY_POWER): {
1464
0
            PyObject *exp = POP();
1465
0
            PyObject *base = TOP();
1466
0
            PyObject *res = PyNumber_Power(base, exp, Py_None);
1467
0
            Py_DECREF(base);
1468
0
            Py_DECREF(exp);
1469
0
            SET_TOP(res);
1470
0
            if (res == NULL)
1471
0
                goto error;
1472
0
            DISPATCH();
1473
0
        }
1474
1475
302
        case TARGET(BINARY_MULTIPLY): {
1476
302
            PyObject *right = POP();
1477
302
            PyObject *left = TOP();
1478
302
            PyObject *res = PyNumber_Multiply(left, right);
1479
302
            Py_DECREF(left);
1480
302
            Py_DECREF(right);
1481
302
            SET_TOP(res);
1482
302
            if (res == NULL)
1483
0
                goto error;
1484
302
            DISPATCH();
1485
0
        }
1486
1487
0
        case TARGET(BINARY_MATRIX_MULTIPLY): {
1488
0
            PyObject *right = POP();
1489
0
            PyObject *left = TOP();
1490
0
            PyObject *res = PyNumber_MatrixMultiply(left, right);
1491
0
            Py_DECREF(left);
1492
0
            Py_DECREF(right);
1493
0
            SET_TOP(res);
1494
0
            if (res == NULL)
1495
0
                goto error;
1496
0
            DISPATCH();
1497
0
        }
1498
1499
0
        case TARGET(BINARY_TRUE_DIVIDE): {
1500
0
            PyObject *divisor = POP();
1501
0
            PyObject *dividend = TOP();
1502
0
            PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1503
0
            Py_DECREF(dividend);
1504
0
            Py_DECREF(divisor);
1505
0
            SET_TOP(quotient);
1506
0
            if (quotient == NULL)
1507
0
                goto error;
1508
0
            DISPATCH();
1509
0
        }
1510
1511
512
        case TARGET(BINARY_FLOOR_DIVIDE): {
1512
512
            PyObject *divisor = POP();
1513
512
            PyObject *dividend = TOP();
1514
512
            PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1515
512
            Py_DECREF(dividend);
1516
512
            Py_DECREF(divisor);
1517
512
            SET_TOP(quotient);
1518
512
            if (quotient == NULL)
1519
0
                goto error;
1520
512
            DISPATCH();
1521
0
        }
1522
1523
56
        case TARGET(BINARY_MODULO): {
1524
56
            PyObject *divisor = POP();
1525
56
            PyObject *dividend = TOP();
1526
56
            PyObject *res;
1527
56
            if (PyUnicode_CheckExact(dividend) && (
1528
56
                  !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1529
              // fast path; string formatting, but not if the RHS is a str subclass
1530
              // (see issue28598)
1531
56
              res = PyUnicode_Format(dividend, divisor);
1532
56
            } else {
1533
0
              res = PyNumber_Remainder(dividend, divisor);
1534
0
            }
1535
56
            Py_DECREF(divisor);
1536
56
            Py_DECREF(dividend);
1537
56
            SET_TOP(res);
1538
56
            if (res == NULL)
1539
0
                goto error;
1540
56
            DISPATCH();
1541
0
        }
1542
1543
4.61k
        case TARGET(BINARY_ADD): {
1544
4.61k
            PyObject *right = POP();
1545
4.61k
            PyObject *left = TOP();
1546
4.61k
            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
4.61k
            if (PyUnicode_CheckExact(left) &&
1554
3.16k
                     PyUnicode_CheckExact(right)) {
1555
3.16k
                sum = unicode_concatenate(tstate, left, right, f, next_instr);
1556
                /* unicode_concatenate consumed the ref to left */
1557
3.16k
            }
1558
1.44k
            else {
1559
1.44k
                sum = PyNumber_Add(left, right);
1560
1.44k
                Py_DECREF(left);
1561
1.44k
            }
1562
4.61k
            Py_DECREF(right);
1563
4.61k
            SET_TOP(sum);
1564
4.61k
            if (sum == NULL)
1565
0
                goto error;
1566
4.61k
            DISPATCH();
1567
0
        }
1568
1569
523
        case TARGET(BINARY_SUBTRACT): {
1570
523
            PyObject *right = POP();
1571
523
            PyObject *left = TOP();
1572
523
            PyObject *diff = PyNumber_Subtract(left, right);
1573
523
            Py_DECREF(right);
1574
523
            Py_DECREF(left);
1575
523
            SET_TOP(diff);
1576
523
            if (diff == NULL)
1577
0
                goto error;
1578
523
            DISPATCH();
1579
0
        }
1580
1581
13.0k
        case TARGET(BINARY_SUBSCR): {
1582
13.0k
            PyObject *sub = POP();
1583
13.0k
            PyObject *container = TOP();
1584
13.0k
            PyObject *res = PyObject_GetItem(container, sub);
1585
13.0k
            Py_DECREF(container);
1586
13.0k
            Py_DECREF(sub);
1587
13.0k
            SET_TOP(res);
1588
13.0k
            if (res == NULL)
1589
490
                goto error;
1590
12.5k
            DISPATCH();
1591
0
        }
1592
1593
14
        case TARGET(BINARY_LSHIFT): {
1594
14
            PyObject *right = POP();
1595
14
            PyObject *left = TOP();
1596
14
            PyObject *res = PyNumber_Lshift(left, right);
1597
14
            Py_DECREF(left);
1598
14
            Py_DECREF(right);
1599
14
            SET_TOP(res);
1600
14
            if (res == NULL)
1601
0
                goto error;
1602
14
            DISPATCH();
1603
0
        }
1604
1605
0
        case TARGET(BINARY_RSHIFT): {
1606
0
            PyObject *right = POP();
1607
0
            PyObject *left = TOP();
1608
0
            PyObject *res = PyNumber_Rshift(left, right);
1609
0
            Py_DECREF(left);
1610
0
            Py_DECREF(right);
1611
0
            SET_TOP(res);
1612
0
            if (res == NULL)
1613
0
                goto error;
1614
0
            DISPATCH();
1615
0
        }
1616
1617
1.67k
        case TARGET(BINARY_AND): {
1618
1.67k
            PyObject *right = POP();
1619
1.67k
            PyObject *left = TOP();
1620
1.67k
            PyObject *res = PyNumber_And(left, right);
1621
1.67k
            Py_DECREF(left);
1622
1.67k
            Py_DECREF(right);
1623
1.67k
            SET_TOP(res);
1624
1.67k
            if (res == NULL)
1625
0
                goto error;
1626
1.67k
            DISPATCH();
1627
0
        }
1628
1629
512
        case TARGET(BINARY_XOR): {
1630
512
            PyObject *right = POP();
1631
512
            PyObject *left = TOP();
1632
512
            PyObject *res = PyNumber_Xor(left, right);
1633
512
            Py_DECREF(left);
1634
512
            Py_DECREF(right);
1635
512
            SET_TOP(res);
1636
512
            if (res == NULL)
1637
0
                goto error;
1638
512
            DISPATCH();
1639
0
        }
1640
1641
77
        case TARGET(BINARY_OR): {
1642
77
            PyObject *right = POP();
1643
77
            PyObject *left = TOP();
1644
77
            PyObject *res = PyNumber_Or(left, right);
1645
77
            Py_DECREF(left);
1646
77
            Py_DECREF(right);
1647
77
            SET_TOP(res);
1648
77
            if (res == NULL)
1649
0
                goto error;
1650
77
            DISPATCH();
1651
0
        }
1652
1653
7.60k
        case TARGET(LIST_APPEND): {
1654
7.60k
            PyObject *v = POP();
1655
7.60k
            PyObject *list = PEEK(oparg);
1656
7.60k
            int err;
1657
7.60k
            err = PyList_Append(list, v);
1658
7.60k
            Py_DECREF(v);
1659
7.60k
            if (err != 0)
1660
0
                goto error;
1661
7.60k
            PREDICT(JUMP_ABSOLUTE);
1662
7.60k
            DISPATCH();
1663
0
        }
1664
1665
23
        case TARGET(SET_ADD): {
1666
23
            PyObject *v = POP();
1667
23
            PyObject *set = PEEK(oparg);
1668
23
            int err;
1669
23
            err = PySet_Add(set, v);
1670
23
            Py_DECREF(v);
1671
23
            if (err != 0)
1672
0
                goto error;
1673
23
            PREDICT(JUMP_ABSOLUTE);
1674
23
            DISPATCH();
1675
0
        }
1676
1677
0
        case TARGET(INPLACE_POWER): {
1678
0
            PyObject *exp = POP();
1679
0
            PyObject *base = TOP();
1680
0
            PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1681
0
            Py_DECREF(base);
1682
0
            Py_DECREF(exp);
1683
0
            SET_TOP(res);
1684
0
            if (res == NULL)
1685
0
                goto error;
1686
0
            DISPATCH();
1687
0
        }
1688
1689
0
        case TARGET(INPLACE_MULTIPLY): {
1690
0
            PyObject *right = POP();
1691
0
            PyObject *left = TOP();
1692
0
            PyObject *res = PyNumber_InPlaceMultiply(left, right);
1693
0
            Py_DECREF(left);
1694
0
            Py_DECREF(right);
1695
0
            SET_TOP(res);
1696
0
            if (res == NULL)
1697
0
                goto error;
1698
0
            DISPATCH();
1699
0
        }
1700
1701
0
        case TARGET(INPLACE_MATRIX_MULTIPLY): {
1702
0
            PyObject *right = POP();
1703
0
            PyObject *left = TOP();
1704
0
            PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1705
0
            Py_DECREF(left);
1706
0
            Py_DECREF(right);
1707
0
            SET_TOP(res);
1708
0
            if (res == NULL)
1709
0
                goto error;
1710
0
            DISPATCH();
1711
0
        }
1712
1713
0
        case TARGET(INPLACE_TRUE_DIVIDE): {
1714
0
            PyObject *divisor = POP();
1715
0
            PyObject *dividend = TOP();
1716
0
            PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1717
0
            Py_DECREF(dividend);
1718
0
            Py_DECREF(divisor);
1719
0
            SET_TOP(quotient);
1720
0
            if (quotient == NULL)
1721
0
                goto error;
1722
0
            DISPATCH();
1723
0
        }
1724
1725
0
        case TARGET(INPLACE_FLOOR_DIVIDE): {
1726
0
            PyObject *divisor = POP();
1727
0
            PyObject *dividend = TOP();
1728
0
            PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1729
0
            Py_DECREF(dividend);
1730
0
            Py_DECREF(divisor);
1731
0
            SET_TOP(quotient);
1732
0
            if (quotient == NULL)
1733
0
                goto error;
1734
0
            DISPATCH();
1735
0
        }
1736
1737
0
        case TARGET(INPLACE_MODULO): {
1738
0
            PyObject *right = POP();
1739
0
            PyObject *left = TOP();
1740
0
            PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1741
0
            Py_DECREF(left);
1742
0
            Py_DECREF(right);
1743
0
            SET_TOP(mod);
1744
0
            if (mod == NULL)
1745
0
                goto error;
1746
0
            DISPATCH();
1747
0
        }
1748
1749
703
        case TARGET(INPLACE_ADD): {
1750
703
            PyObject *right = POP();
1751
703
            PyObject *left = TOP();
1752
703
            PyObject *sum;
1753
703
            if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1754
212
                sum = unicode_concatenate(tstate, left, right, f, next_instr);
1755
                /* unicode_concatenate consumed the ref to left */
1756
212
            }
1757
491
            else {
1758
491
                sum = PyNumber_InPlaceAdd(left, right);
1759
491
                Py_DECREF(left);
1760
491
            }
1761
703
            Py_DECREF(right);
1762
703
            SET_TOP(sum);
1763
703
            if (sum == NULL)
1764
0
                goto error;
1765
703
            DISPATCH();
1766
0
        }
1767
1768
392
        case TARGET(INPLACE_SUBTRACT): {
1769
392
            PyObject *right = POP();
1770
392
            PyObject *left = TOP();
1771
392
            PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1772
392
            Py_DECREF(left);
1773
392
            Py_DECREF(right);
1774
392
            SET_TOP(diff);
1775
392
            if (diff == NULL)
1776
0
                goto error;
1777
392
            DISPATCH();
1778
0
        }
1779
1780
0
        case TARGET(INPLACE_LSHIFT): {
1781
0
            PyObject *right = POP();
1782
0
            PyObject *left = TOP();
1783
0
            PyObject *res = PyNumber_InPlaceLshift(left, right);
1784
0
            Py_DECREF(left);
1785
0
            Py_DECREF(right);
1786
0
            SET_TOP(res);
1787
0
            if (res == NULL)
1788
0
                goto error;
1789
0
            DISPATCH();
1790
0
        }
1791
1792
0
        case TARGET(INPLACE_RSHIFT): {
1793
0
            PyObject *right = POP();
1794
0
            PyObject *left = TOP();
1795
0
            PyObject *res = PyNumber_InPlaceRshift(left, right);
1796
0
            Py_DECREF(left);
1797
0
            Py_DECREF(right);
1798
0
            SET_TOP(res);
1799
0
            if (res == NULL)
1800
0
                goto error;
1801
0
            DISPATCH();
1802
0
        }
1803
1804
4
        case TARGET(INPLACE_AND): {
1805
4
            PyObject *right = POP();
1806
4
            PyObject *left = TOP();
1807
4
            PyObject *res = PyNumber_InPlaceAnd(left, right);
1808
4
            Py_DECREF(left);
1809
4
            Py_DECREF(right);
1810
4
            SET_TOP(res);
1811
4
            if (res == NULL)
1812
0
                goto error;
1813
4
            DISPATCH();
1814
0
        }
1815
1816
0
        case TARGET(INPLACE_XOR): {
1817
0
            PyObject *right = POP();
1818
0
            PyObject *left = TOP();
1819
0
            PyObject *res = PyNumber_InPlaceXor(left, right);
1820
0
            Py_DECREF(left);
1821
0
            Py_DECREF(right);
1822
0
            SET_TOP(res);
1823
0
            if (res == NULL)
1824
0
                goto error;
1825
0
            DISPATCH();
1826
0
        }
1827
1828
8
        case TARGET(INPLACE_OR): {
1829
8
            PyObject *right = POP();
1830
8
            PyObject *left = TOP();
1831
8
            PyObject *res = PyNumber_InPlaceOr(left, right);
1832
8
            Py_DECREF(left);
1833
8
            Py_DECREF(right);
1834
8
            SET_TOP(res);
1835
8
            if (res == NULL)
1836
0
                goto error;
1837
8
            DISPATCH();
1838
0
        }
1839
1840
2.72k
        case TARGET(STORE_SUBSCR): {
1841
2.72k
            PyObject *sub = TOP();
1842
2.72k
            PyObject *container = SECOND();
1843
2.72k
            PyObject *v = THIRD();
1844
2.72k
            int err;
1845
2.72k
            STACK_SHRINK(3);
1846
            /* container[sub] = v */
1847
2.72k
            err = PyObject_SetItem(container, sub, v);
1848
2.72k
            Py_DECREF(v);
1849
2.72k
            Py_DECREF(container);
1850
2.72k
            Py_DECREF(sub);
1851
2.72k
            if (err != 0)
1852
2
                goto error;
1853
2.72k
            DISPATCH();
1854
0
        }
1855
1856
750
        case TARGET(DELETE_SUBSCR): {
1857
750
            PyObject *sub = TOP();
1858
750
            PyObject *container = SECOND();
1859
750
            int err;
1860
750
            STACK_SHRINK(2);
1861
            /* del container[sub] */
1862
750
            err = PyObject_DelItem(container, sub);
1863
750
            Py_DECREF(container);
1864
750
            Py_DECREF(sub);
1865
750
            if (err != 0)
1866
0
                goto error;
1867
750
            DISPATCH();
1868
0
        }
1869
1870
0
        case TARGET(PRINT_EXPR): {
1871
0
            _Py_IDENTIFIER(displayhook);
1872
0
            PyObject *value = POP();
1873
0
            PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
1874
0
            PyObject *res;
1875
0
            if (hook == NULL) {
1876
0
                _PyErr_SetString(tstate, PyExc_RuntimeError,
1877
0
                                 "lost sys.displayhook");
1878
0
                Py_DECREF(value);
1879
0
                goto error;
1880
0
            }
1881
0
            res = PyObject_CallFunctionObjArgs(hook, value, NULL);
1882
0
            Py_DECREF(value);
1883
0
            if (res == NULL)
1884
0
                goto error;
1885
0
            Py_DECREF(res);
1886
0
            DISPATCH();
1887
0
        }
1888
1889
158
        case TARGET(RAISE_VARARGS): {
1890
158
            PyObject *cause = NULL, *exc = NULL;
1891
158
            switch (oparg) {
1892
0
            case 2:
1893
0
                cause = POP(); /* cause */
1894
                /* fall through */
1895
158
            case 1:
1896
158
                exc = POP(); /* exc */
1897
                /* fall through */
1898
158
            case 0:
1899
158
                if (do_raise(tstate, exc, cause)) {
1900
0
                    goto exception_unwind;
1901
0
                }
1902
158
                break;
1903
158
            default:
1904
0
                _PyErr_SetString(tstate, PyExc_SystemError,
1905
0
                                 "bad RAISE_VARARGS oparg");
1906
0
                break;
1907
158
            }
1908
158
            goto error;
1909
158
        }
1910
1911
37.1k
        case TARGET(RETURN_VALUE): {
1912
37.1k
            retval = POP();
1913
37.1k
            assert(f->f_iblock == 0);
1914
37.1k
            goto exit_returning;
1915
0
        }
1916
1917
0
        case TARGET(GET_AITER): {
1918
0
            unaryfunc getter = NULL;
1919
0
            PyObject *iter = NULL;
1920
0
            PyObject *obj = TOP();
1921
0
            PyTypeObject *type = Py_TYPE(obj);
1922
1923
0
            if (type->tp_as_async != NULL) {
1924
0
                getter = type->tp_as_async->am_aiter;
1925
0
            }
1926
1927
0
            if (getter != NULL) {
1928
0
                iter = (*getter)(obj);
1929
0
                Py_DECREF(obj);
1930
0
                if (iter == NULL) {
1931
0
                    SET_TOP(NULL);
1932
0
                    goto error;
1933
0
                }
1934
0
            }
1935
0
            else {
1936
0
                SET_TOP(NULL);
1937
0
                _PyErr_Format(tstate, PyExc_TypeError,
1938
0
                              "'async for' requires an object with "
1939
0
                              "__aiter__ method, got %.100s",
1940
0
                              type->tp_name);
1941
0
                Py_DECREF(obj);
1942
0
                goto error;
1943
0
            }
1944
1945
0
            if (Py_TYPE(iter)->tp_as_async == NULL ||
1946
0
                    Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
1947
1948
0
                SET_TOP(NULL);
1949
0
                _PyErr_Format(tstate, PyExc_TypeError,
1950
0
                              "'async for' received an object from __aiter__ "
1951
0
                              "that does not implement __anext__: %.100s",
1952
0
                              Py_TYPE(iter)->tp_name);
1953
0
                Py_DECREF(iter);
1954
0
                goto error;
1955
0
            }
1956
1957
0
            SET_TOP(iter);
1958
0
            DISPATCH();
1959
0
        }
1960
1961
0
        case TARGET(GET_ANEXT): {
1962
0
            unaryfunc getter = NULL;
1963
0
            PyObject *next_iter = NULL;
1964
0
            PyObject *awaitable = NULL;
1965
0
            PyObject *aiter = TOP();
1966
0
            PyTypeObject *type = Py_TYPE(aiter);
1967
1968
0
            if (PyAsyncGen_CheckExact(aiter)) {
1969
0
                awaitable = type->tp_as_async->am_anext(aiter);
1970
0
                if (awaitable == NULL) {
1971
0
                    goto error;
1972
0
                }
1973
0
            } else {
1974
0
                if (type->tp_as_async != NULL){
1975
0
                    getter = type->tp_as_async->am_anext;
1976
0
                }
1977
1978
0
                if (getter != NULL) {
1979
0
                    next_iter = (*getter)(aiter);
1980
0
                    if (next_iter == NULL) {
1981
0
                        goto error;
1982
0
                    }
1983
0
                }
1984
0
                else {
1985
0
                    _PyErr_Format(tstate, PyExc_TypeError,
1986
0
                                  "'async for' requires an iterator with "
1987
0
                                  "__anext__ method, got %.100s",
1988
0
                                  type->tp_name);
1989
0
                    goto error;
1990
0
                }
1991
1992
0
                awaitable = _PyCoro_GetAwaitableIter(next_iter);
1993
0
                if (awaitable == NULL) {
1994
0
                    _PyErr_FormatFromCause(
1995
0
                        PyExc_TypeError,
1996
0
                        "'async for' received an invalid object "
1997
0
                        "from __anext__: %.100s",
1998
0
                        Py_TYPE(next_iter)->tp_name);
1999
2000
0
                    Py_DECREF(next_iter);
2001
0
                    goto error;
2002
0
                } else {
2003
0
                    Py_DECREF(next_iter);
2004
0
                }
2005
0
            }
2006
2007
0
            PUSH(awaitable);
2008
0
            PREDICT(LOAD_CONST);
2009
0
            DISPATCH();
2010
0
        }
2011
2012
0
        case TARGET(GET_AWAITABLE): {
2013
0
            PREDICTED(GET_AWAITABLE);
2014
0
            PyObject *iterable = TOP();
2015
0
            PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
2016
2017
0
            if (iter == NULL) {
2018
0
                format_awaitable_error(tstate, Py_TYPE(iterable),
2019
0
                                       _Py_OPCODE(next_instr[-2]));
2020
0
            }
2021
2022
0
            Py_DECREF(iterable);
2023
2024
0
            if (iter != NULL && PyCoro_CheckExact(iter)) {
2025
0
                PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2026
0
                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
0
                    Py_DECREF(yf);
2031
0
                    Py_CLEAR(iter);
2032
0
                    _PyErr_SetString(tstate, PyExc_RuntimeError,
2033
0
                                     "coroutine is being awaited already");
2034
                    /* The code below jumps to `error` if `iter` is NULL. */
2035
0
                }
2036
0
            }
2037
2038
0
            SET_TOP(iter); /* Even if it's NULL */
2039
2040
0
            if (iter == NULL) {
2041
0
                goto error;
2042
0
            }
2043
2044
0
            PREDICT(LOAD_CONST);
2045
0
            DISPATCH();
2046
0
        }
2047
2048
70
        case TARGET(YIELD_FROM): {
2049
70
            PyObject *v = POP();
2050
70
            PyObject *receiver = TOP();
2051
70
            int err;
2052
70
            if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2053
28
                retval = _PyGen_Send((PyGenObject *)receiver, v);
2054
42
            } else {
2055
42
                _Py_IDENTIFIER(send);
2056
42
                if (v == Py_None)
2057
42
                    retval = Py_TYPE(receiver)->tp_iternext(receiver);
2058
0
                else
2059
0
                    retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
2060
42
            }
2061
70
            Py_DECREF(v);
2062
70
            if (retval == NULL) {
2063
28
                PyObject *val;
2064
28
                if (tstate->c_tracefunc != NULL
2065
0
                        && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
2066
0
                    call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
2067
28
                err = _PyGen_FetchStopIterationValue(&val);
2068
28
                if (err < 0)
2069
0
                    goto error;
2070
28
                Py_DECREF(receiver);
2071
28
                SET_TOP(val);
2072
28
                DISPATCH();
2073
0
            }
2074
            /* receiver remains on stack, retval is value to be yielded */
2075
42
            f->f_stacktop = stack_pointer;
2076
            /* and repeat... */
2077
42
            assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
2078
42
            f->f_lasti -= sizeof(_Py_CODEUNIT);
2079
42
            goto exit_yielding;
2080
70
        }
2081
2082
767
        case TARGET(YIELD_VALUE): {
2083
767
            retval = POP();
2084
2085
767
            if (co->co_flags & CO_ASYNC_GENERATOR) {
2086
0
                PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2087
0
                Py_DECREF(retval);
2088
0
                if (w == NULL) {
2089
0
                    retval = NULL;
2090
0
                    goto error;
2091
0
                }
2092
0
                retval = w;
2093
0
            }
2094
2095
767
            f->f_stacktop = stack_pointer;
2096
767
            goto exit_yielding;
2097
767
        }
2098
2099
3.15k
        case TARGET(POP_EXCEPT): {
2100
3.15k
            PyObject *type, *value, *traceback;
2101
3.15k
            _PyErr_StackItem *exc_info;
2102
3.15k
            PyTryBlock *b = PyFrame_BlockPop(f);
2103
3.15k
            if (b->b_type != EXCEPT_HANDLER) {
2104
0
                _PyErr_SetString(tstate, PyExc_SystemError,
2105
0
                                 "popped block is not an except handler");
2106
0
                goto error;
2107
0
            }
2108
3.15k
            assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2109
3.15k
                   STACK_LEVEL() <= (b)->b_level + 4);
2110
3.15k
            exc_info = tstate->exc_info;
2111
3.15k
            type = exc_info->exc_type;
2112
3.15k
            value = exc_info->exc_value;
2113
3.15k
            traceback = exc_info->exc_traceback;
2114
3.15k
            exc_info->exc_type = POP();
2115
3.15k
            exc_info->exc_value = POP();
2116
3.15k
            exc_info->exc_traceback = POP();
2117
3.15k
            Py_XDECREF(type);
2118
3.15k
            Py_XDECREF(value);
2119
3.15k
            Py_XDECREF(traceback);
2120
3.15k
            DISPATCH();
2121
0
        }
2122
2123
17.8k
        case TARGET(POP_BLOCK): {
2124
17.8k
            PREDICTED(POP_BLOCK);
2125
17.8k
            PyFrame_BlockPop(f);
2126
17.8k
            DISPATCH();
2127
0
        }
2128
2129
918
        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
918
            PyObject *res = NULL;
2141
918
            if (oparg) {
2142
0
                res = POP();
2143
0
            }
2144
918
            PyObject *exc = POP();
2145
918
            if (exc == NULL || PyLong_CheckExact(exc)) {
2146
918
                Py_XDECREF(exc);
2147
918
            }
2148
0
            else {
2149
0
                Py_DECREF(exc);
2150
0
                Py_DECREF(POP());
2151
0
                Py_DECREF(POP());
2152
2153
0
                PyObject *type, *value, *traceback;
2154
0
                _PyErr_StackItem *exc_info;
2155
0
                PyTryBlock *b = PyFrame_BlockPop(f);
2156
0
                if (b->b_type != EXCEPT_HANDLER) {
2157
0
                    _PyErr_SetString(tstate, PyExc_SystemError,
2158
0
                                     "popped block is not an except handler");
2159
0
                    Py_XDECREF(res);
2160
0
                    goto error;
2161
0
                }
2162
0
                assert(STACK_LEVEL() == (b)->b_level + 3);
2163
0
                exc_info = tstate->exc_info;
2164
0
                type = exc_info->exc_type;
2165
0
                value = exc_info->exc_value;
2166
0
                traceback = exc_info->exc_traceback;
2167
0
                exc_info->exc_type = POP();
2168
0
                exc_info->exc_value = POP();
2169
0
                exc_info->exc_traceback = POP();
2170
0
                Py_XDECREF(type);
2171
0
                Py_XDECREF(value);
2172
0
                Py_XDECREF(traceback);
2173
0
            }
2174
918
            if (oparg) {
2175
0
                PUSH(res);
2176
0
            }
2177
918
            DISPATCH();
2178
0
        }
2179
2180
392
        case TARGET(CALL_FINALLY): {
2181
392
            PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
2182
392
            if (ret == NULL) {
2183
0
                goto error;
2184
0
            }
2185
392
            PUSH(ret);
2186
392
            JUMPBY(oparg);
2187
392
            FAST_DISPATCH();
2188
0
        }
2189
2190
4.64k
        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
4.64k
            PUSH(NULL);
2195
4.64k
            FAST_DISPATCH();
2196
0
        }
2197
2198
4.23k
        case TARGET(END_FINALLY): {
2199
4.23k
            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
4.23k
            PyObject *exc = POP();
2208
4.23k
            if (exc == NULL) {
2209
3.72k
                FAST_DISPATCH();
2210
0
            }
2211
509
            else if (PyLong_CheckExact(exc)) {
2212
392
                int ret = _PyLong_AsInt(exc);
2213
392
                Py_DECREF(exc);
2214
392
                if (ret == -1 && _PyErr_Occurred(tstate)) {
2215
0
                    goto error;
2216
0
                }
2217
392
                JUMPTO(ret);
2218
392
                FAST_DISPATCH();
2219
0
            }
2220
117
            else {
2221
117
                assert(PyExceptionClass_Check(exc));
2222
117
                PyObject *val = POP();
2223
117
                PyObject *tb = POP();
2224
117
                _PyErr_Restore(tstate, exc, val, tb);
2225
117
                goto exception_unwind;
2226
117
            }
2227
4.23k
        }
2228
2229
0
        case TARGET(END_ASYNC_FOR): {
2230
0
            PyObject *exc = POP();
2231
0
            assert(PyExceptionClass_Check(exc));
2232
0
            if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2233
0
                PyTryBlock *b = PyFrame_BlockPop(f);
2234
0
                assert(b->b_type == EXCEPT_HANDLER);
2235
0
                Py_DECREF(exc);
2236
0
                UNWIND_EXCEPT_HANDLER(b);
2237
0
                Py_DECREF(POP());
2238
0
                JUMPBY(oparg);
2239
0
                FAST_DISPATCH();
2240
0
            }
2241
0
            else {
2242
0
                PyObject *val = POP();
2243
0
                PyObject *tb = POP();
2244
0
                _PyErr_Restore(tstate, exc, val, tb);
2245
0
                goto exception_unwind;
2246
0
            }
2247
0
        }
2248
2249
1.24k
        case TARGET(LOAD_BUILD_CLASS): {
2250
1.24k
            _Py_IDENTIFIER(__build_class__);
2251
2252
1.24k
            PyObject *bc;
2253
1.24k
            if (PyDict_CheckExact(f->f_builtins)) {
2254
1.24k
                bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
2255
1.24k
                if (bc == NULL) {
2256
0
                    if (!_PyErr_Occurred(tstate)) {
2257
0
                        _PyErr_SetString(tstate, PyExc_NameError,
2258
0
                                         "__build_class__ not found");
2259
0
                    }
2260
0
                    goto error;
2261
0
                }
2262
1.24k
                Py_INCREF(bc);
2263
1.24k
            }
2264
0
            else {
2265
0
                PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2266
0
                if (build_class_str == NULL)
2267
0
                    goto error;
2268
0
                bc = PyObject_GetItem(f->f_builtins, build_class_str);
2269
0
                if (bc == NULL) {
2270
0
                    if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2271
0
                        _PyErr_SetString(tstate, PyExc_NameError,
2272
0
                                         "__build_class__ not found");
2273
0
                    goto error;
2274
0
                }
2275
0
            }
2276
1.24k
            PUSH(bc);
2277
1.24k
            DISPATCH();
2278
0
        }
2279
2280
28.9k
        case TARGET(STORE_NAME): {
2281
28.9k
            PyObject *name = GETITEM(names, oparg);
2282
28.9k
            PyObject *v = POP();
2283
28.9k
            PyObject *ns = f->f_locals;
2284
28.9k
            int err;
2285
28.9k
            if (ns == NULL) {
2286
0
                _PyErr_Format(tstate, PyExc_SystemError,
2287
0
                              "no locals found when storing %R", name);
2288
0
                Py_DECREF(v);
2289
0
                goto error;
2290
0
            }
2291
28.9k
            if (PyDict_CheckExact(ns))
2292
28.9k
                err = PyDict_SetItem(ns, name, v);
2293
64
            else
2294
64
                err = PyObject_SetItem(ns, name, v);
2295
28.9k
            Py_DECREF(v);
2296
28.9k
            if (err != 0)
2297
0
                goto error;
2298
28.9k
            DISPATCH();
2299
0
        }
2300
2301
1.43k
        case TARGET(DELETE_NAME): {
2302
1.43k
            PyObject *name = GETITEM(names, oparg);
2303
1.43k
            PyObject *ns = f->f_locals;
2304
1.43k
            int err;
2305
1.43k
            if (ns == NULL) {
2306
0
                _PyErr_Format(tstate, PyExc_SystemError,
2307
0
                              "no locals when deleting %R", name);
2308
0
                goto error;
2309
0
            }
2310
1.43k
            err = PyObject_DelItem(ns, name);
2311
1.43k
            if (err != 0) {
2312
0
                format_exc_check_arg(tstate, PyExc_NameError,
2313
0
                                     NAME_ERROR_MSG,
2314
0
                                     name);
2315
0
                goto error;
2316
0
            }
2317
1.43k
            DISPATCH();
2318
0
        }
2319
2320
4.31k
        case TARGET(UNPACK_SEQUENCE): {
2321
4.31k
            PREDICTED(UNPACK_SEQUENCE);
2322
4.31k
            PyObject *seq = POP(), *item, **items;
2323
4.31k
            if (PyTuple_CheckExact(seq) &&
2324
4.31k
                PyTuple_GET_SIZE(seq) == oparg) {
2325
4.31k
                items = ((PyTupleObject *)seq)->ob_item;
2326
14.0k
                while (oparg--) {
2327
9.74k
                    item = items[oparg];
2328
9.74k
                    Py_INCREF(item);
2329
9.74k
                    PUSH(item);
2330
9.74k
                }
2331
4.31k
            } else if (PyList_CheckExact(seq) &&
2332
0
                       PyList_GET_SIZE(seq) == oparg) {
2333
0
                items = ((PyListObject *)seq)->ob_item;
2334
0
                while (oparg--) {
2335
0
                    item = items[oparg];
2336
0
                    Py_INCREF(item);
2337
0
                    PUSH(item);
2338
0
                }
2339
0
            } else if (unpack_iterable(tstate, seq, oparg, -1,
2340
0
                                       stack_pointer + oparg)) {
2341
0
                STACK_GROW(oparg);
2342
0
            } else {
2343
                /* unpack_iterable() raised an exception */
2344
0
                Py_DECREF(seq);
2345
0
                goto error;
2346
0
            }
2347
4.31k
            Py_DECREF(seq);
2348
4.31k
            DISPATCH();
2349
0
        }
2350
2351
0
        case TARGET(UNPACK_EX): {
2352
0
            int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2353
0
            PyObject *seq = POP();
2354
2355
0
            if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
2356
0
                                stack_pointer + totalargs)) {
2357
0
                stack_pointer += totalargs;
2358
0
            } else {
2359
0
                Py_DECREF(seq);
2360
0
                goto error;
2361
0
            }
2362
0
            Py_DECREF(seq);
2363
0
            DISPATCH();
2364
0
        }
2365
2366
15.6k
        case TARGET(STORE_ATTR): {
2367
15.6k
            PyObject *name = GETITEM(names, oparg);
2368
15.6k
            PyObject *owner = TOP();
2369
15.6k
            PyObject *v = SECOND();
2370
15.6k
            int err;
2371
15.6k
            STACK_SHRINK(2);
2372
15.6k
            err = PyObject_SetAttr(owner, name, v);
2373
15.6k
            Py_DECREF(v);
2374
15.6k
            Py_DECREF(owner);
2375
15.6k
            if (err != 0)
2376
0
                goto error;
2377
15.6k
            DISPATCH();
2378
0
        }
2379
2380
0
        case TARGET(DELETE_ATTR): {
2381
0
            PyObject *name = GETITEM(names, oparg);
2382
0
            PyObject *owner = POP();
2383
0
            int err;
2384
0
            err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2385
0
            Py_DECREF(owner);
2386
0
            if (err != 0)
2387
0
                goto error;
2388
0
            DISPATCH();
2389
0
        }
2390
2391
232
        case TARGET(STORE_GLOBAL): {
2392
232
            PyObject *name = GETITEM(names, oparg);
2393
232
            PyObject *v = POP();
2394
232
            int err;
2395
232
            err = PyDict_SetItem(f->f_globals, name, v);
2396
232
            Py_DECREF(v);
2397
232
            if (err != 0)
2398
0
                goto error;
2399
232
            DISPATCH();
2400
0
        }
2401
2402
0
        case TARGET(DELETE_GLOBAL): {
2403
0
            PyObject *name = GETITEM(names, oparg);
2404
0
            int err;
2405
0
            err = PyDict_DelItem(f->f_globals, name);
2406
0
            if (err != 0) {
2407
0
                if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2408
0
                    format_exc_check_arg(tstate, PyExc_NameError,
2409
0
                                         NAME_ERROR_MSG, name);
2410
0
                }
2411
0
                goto error;
2412
0
            }
2413
0
            DISPATCH();
2414
0
        }
2415
2416
35.0k
        case TARGET(LOAD_NAME): {
2417
35.0k
            PyObject *name = GETITEM(names, oparg);
2418
35.0k
            PyObject *locals = f->f_locals;
2419
35.0k
            PyObject *v;
2420
35.0k
            if (locals == NULL) {
2421
0
                _PyErr_Format(tstate, PyExc_SystemError,
2422
0
                              "no locals when loading %R", name);
2423
0
                goto error;
2424
0
            }
2425
35.0k
            if (PyDict_CheckExact(locals)) {
2426
35.0k
                v = PyDict_GetItemWithError(locals, name);
2427
35.0k
                if (v != NULL) {
2428
20.9k
                    Py_INCREF(v);
2429
20.9k
                }
2430
14.0k
                else if (_PyErr_Occurred(tstate)) {
2431
0
                    goto error;
2432
0
                }
2433
35.0k
            }
2434
25
            else {
2435
25
                v = PyObject_GetItem(locals, name);
2436
25
                if (v == NULL) {
2437
22
                    if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2438
0
                        goto error;
2439
22
                    _PyErr_Clear(tstate);
2440
22
                }
2441
25
            }
2442
35.0k
            if (v == NULL) {
2443
14.0k
                v = PyDict_GetItemWithError(f->f_globals, name);
2444
14.0k
                if (v != NULL) {
2445
9.36k
                    Py_INCREF(v);
2446
9.36k
                }
2447
4.72k
                else if (_PyErr_Occurred(tstate)) {
2448
0
                    goto error;
2449
0
                }
2450
4.72k
                else {
2451
4.72k
                    if (PyDict_CheckExact(f->f_builtins)) {
2452
4.72k
                        v = PyDict_GetItemWithError(f->f_builtins, name);
2453
4.72k
                        if (v == NULL) {
2454
173
                            if (!_PyErr_Occurred(tstate)) {
2455
173
                                format_exc_check_arg(
2456
173
                                        tstate, PyExc_NameError,
2457
173
                                        NAME_ERROR_MSG, name);
2458
173
                            }
2459
173
                            goto error;
2460
173
                        }
2461
4.55k
                        Py_INCREF(v);
2462
4.55k
                    }
2463
0
                    else {
2464
0
                        v = PyObject_GetItem(f->f_builtins, name);
2465
0
                        if (v == NULL) {
2466
0
                            if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2467
0
                                format_exc_check_arg(
2468
0
                                            tstate, PyExc_NameError,
2469
0
                                            NAME_ERROR_MSG, name);
2470
0
                            }
2471
0
                            goto error;
2472
0
                        }
2473
0
                    }
2474
4.72k
                }
2475
14.0k
            }
2476
34.8k
            PUSH(v);
2477
34.8k
            DISPATCH();
2478
0
        }
2479
2480
73.4k
        case TARGET(LOAD_GLOBAL): {
2481
73.4k
            PyObject *name;
2482
73.4k
            PyObject *v;
2483
73.4k
            if (PyDict_CheckExact(f->f_globals)
2484
73.4k
                && PyDict_CheckExact(f->f_builtins))
2485
73.4k
            {
2486
73.4k
                OPCACHE_CHECK();
2487
73.4k
                if (co_opcache != NULL && co_opcache->optimized > 0) {
2488
0
                    _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2489
2490
0
                    if (lg->globals_ver ==
2491
0
                            ((PyDictObject *)f->f_globals)->ma_version_tag
2492
0
                        && lg->builtins_ver ==
2493
0
                           ((PyDictObject *)f->f_builtins)->ma_version_tag)
2494
0
                    {
2495
0
                        PyObject *ptr = lg->ptr;
2496
0
                        OPCACHE_STAT_GLOBAL_HIT();
2497
0
                        assert(ptr != NULL);
2498
0
                        Py_INCREF(ptr);
2499
0
                        PUSH(ptr);
2500
0
                        DISPATCH();
2501
0
                    }
2502
0
                }
2503
2504
73.4k
                name = GETITEM(names, oparg);
2505
73.4k
                v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
2506
73.4k
                                       (PyDictObject *)f->f_builtins,
2507
73.4k
                                       name);
2508
73.4k
                if (v == NULL) {
2509
52
                    if (!_PyErr_OCCURRED()) {
2510
                        /* _PyDict_LoadGlobal() returns NULL without raising
2511
                         * an exception if the key doesn't exist */
2512
52
                        format_exc_check_arg(tstate, PyExc_NameError,
2513
52
                                             NAME_ERROR_MSG, name);
2514
52
                    }
2515
52
                    goto error;
2516
52
                }
2517
2518
73.3k
                if (co_opcache != NULL) {
2519
0
                    _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2520
2521
0
                    if (co_opcache->optimized == 0) {
2522
                        /* Wasn't optimized before. */
2523
0
                        OPCACHE_STAT_GLOBAL_OPT();
2524
0
                    } else {
2525
0
                        OPCACHE_STAT_GLOBAL_MISS();
2526
0
                    }
2527
2528
0
                    co_opcache->optimized = 1;
2529
0
                    lg->globals_ver =
2530
0
                        ((PyDictObject *)f->f_globals)->ma_version_tag;
2531
0
                    lg->builtins_ver =
2532
0
                        ((PyDictObject *)f->f_builtins)->ma_version_tag;
2533
0
                    lg->ptr = v; /* borrowed */
2534
0
                }
2535
2536
73.3k
                Py_INCREF(v);
2537
73.3k
            }
2538
0
            else {
2539
                /* Slow-path if globals or builtins is not a dict */
2540
2541
                /* namespace 1: globals */
2542
0
                name = GETITEM(names, oparg);
2543
0
                v = PyObject_GetItem(f->f_globals, name);
2544
0
                if (v == NULL) {
2545
0
                    if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2546
0
                        goto error;
2547
0
                    }
2548
0
                    _PyErr_Clear(tstate);
2549
2550
                    /* namespace 2: builtins */
2551
0
                    v = PyObject_GetItem(f->f_builtins, name);
2552
0
                    if (v == NULL) {
2553
0
                        if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2554
0
                            format_exc_check_arg(
2555
0
                                        tstate, PyExc_NameError,
2556
0
                                        NAME_ERROR_MSG, name);
2557
0
                        }
2558
0
                        goto error;
2559
0
                    }
2560
0
                }
2561
0
            }
2562
73.3k
            PUSH(v);
2563
73.3k
            DISPATCH();
2564
0
        }
2565
2566
13
        case TARGET(DELETE_FAST): {
2567
13
            PyObject *v = GETLOCAL(oparg);
2568
13
            if (v != NULL) {
2569
13
                SETLOCAL(oparg, NULL);
2570
13
                DISPATCH();
2571
0
            }
2572
0
            format_exc_check_arg(
2573
0
                tstate, PyExc_UnboundLocalError,
2574
0
                UNBOUNDLOCAL_ERROR_MSG,
2575
0
                PyTuple_GetItem(co->co_varnames, oparg)
2576
0
                );
2577
0
            goto error;
2578
13
        }
2579
2580
0
        case TARGET(DELETE_DEREF): {
2581
0
            PyObject *cell = freevars[oparg];
2582
0
            PyObject *oldobj = PyCell_GET(cell);
2583
0
            if (oldobj != NULL) {
2584
0
                PyCell_SET(cell, NULL);
2585
0
                Py_DECREF(oldobj);
2586
0
                DISPATCH();
2587
0
            }
2588
0
            format_exc_unbound(tstate, co, oparg);
2589
0
            goto error;
2590
0
        }
2591
2592
629
        case TARGET(LOAD_CLOSURE): {
2593
629
            PyObject *cell = freevars[oparg];
2594
629
            Py_INCREF(cell);
2595
629
            PUSH(cell);
2596
629
            DISPATCH();
2597
0
        }
2598
2599
0
        case TARGET(LOAD_CLASSDEREF): {
2600
0
            PyObject *name, *value, *locals = f->f_locals;
2601
0
            Py_ssize_t idx;
2602
0
            assert(locals);
2603
0
            assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2604
0
            idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2605
0
            assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2606
0
            name = PyTuple_GET_ITEM(co->co_freevars, idx);
2607
0
            if (PyDict_CheckExact(locals)) {
2608
0
                value = PyDict_GetItemWithError(locals, name);
2609
0
                if (value != NULL) {
2610
0
                    Py_INCREF(value);
2611
0
                }
2612
0
                else if (_PyErr_Occurred(tstate)) {
2613
0
                    goto error;
2614
0
                }
2615
0
            }
2616
0
            else {
2617
0
                value = PyObject_GetItem(locals, name);
2618
0
                if (value == NULL) {
2619
0
                    if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2620
0
                        goto error;
2621
0
                    }
2622
0
                    _PyErr_Clear(tstate);
2623
0
                }
2624
0
            }
2625
0
            if (!value) {
2626
0
                PyObject *cell = freevars[oparg];
2627
0
                value = PyCell_GET(cell);
2628
0
                if (value == NULL) {
2629
0
                    format_exc_unbound(tstate, co, oparg);
2630
0
                    goto error;
2631
0
                }
2632
0
                Py_INCREF(value);
2633
0
            }
2634
0
            PUSH(value);
2635
0
            DISPATCH();
2636
0
        }
2637
2638
1.55k
        case TARGET(LOAD_DEREF): {
2639
1.55k
            PyObject *cell = freevars[oparg];
2640
1.55k
            PyObject *value = PyCell_GET(cell);
2641
1.55k
            if (value == NULL) {
2642
0
                format_exc_unbound(tstate, co, oparg);
2643
0
                goto error;
2644
0
            }
2645
1.55k
            Py_INCREF(value);
2646
1.55k
            PUSH(value);
2647
1.55k
            DISPATCH();
2648
0
        }
2649
2650
251
        case TARGET(STORE_DEREF): {
2651
251
            PyObject *v = POP();
2652
251
            PyObject *cell = freevars[oparg];
2653
251
            PyObject *oldobj = PyCell_GET(cell);
2654
251
            PyCell_SET(cell, v);
2655
251
            Py_XDECREF(oldobj);
2656
251
            DISPATCH();
2657
0
        }
2658
2659
68
        case TARGET(BUILD_STRING): {
2660
68
            PyObject *str;
2661
68
            PyObject *empty = PyUnicode_New(0, 0);
2662
68
            if (empty == NULL) {
2663
0
                goto error;
2664
0
            }
2665
68
            str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2666
68
            Py_DECREF(empty);
2667
68
            if (str == NULL)
2668
0
                goto error;
2669
288
            while (--oparg >= 0) {
2670
220
                PyObject *item = POP();
2671
220
                Py_DECREF(item);
2672
220
            }
2673
68
            PUSH(str);
2674
68
            DISPATCH();
2675
0
        }
2676
2677
5.92k
        case TARGET(BUILD_TUPLE): {
2678
5.92k
            PyObject *tup = PyTuple_New(oparg);
2679
5.92k
            if (tup == NULL)
2680
0
                goto error;
2681
22.1k
            while (--oparg >= 0) {
2682
16.2k
                PyObject *item = POP();
2683
16.2k
                PyTuple_SET_ITEM(tup, oparg, item);
2684
16.2k
            }
2685
5.92k
            PUSH(tup);
2686
5.92k
            DISPATCH();
2687
0
        }
2688
2689
3.49k
        case TARGET(BUILD_LIST): {
2690
3.49k
            PyObject *list =  PyList_New(oparg);
2691
3.49k
            if (list == NULL)
2692
0
                goto error;
2693
7.80k
            while (--oparg >= 0) {
2694
4.30k
                PyObject *item = POP();
2695
4.30k
                PyList_SET_ITEM(list, oparg, item);
2696
4.30k
            }
2697
3.49k
            PUSH(list);
2698
3.49k
            DISPATCH();
2699
0
        }
2700
2701
292
        case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL):
2702
292
        case TARGET(BUILD_TUPLE_UNPACK):
2703
292
        case TARGET(BUILD_LIST_UNPACK): {
2704
292
            int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
2705
292
            Py_ssize_t i;
2706
292
            PyObject *sum = PyList_New(0);
2707
292
            PyObject *return_value;
2708
2709
292
            if (sum == NULL)
2710
0
                goto error;
2711
2712
876
            for (i = oparg; i > 0; i--) {
2713
584
                PyObject *none_val;
2714
2715
584
                none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2716
584
                if (none_val == NULL) {
2717
0
                    if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
2718
0
                        _PyErr_ExceptionMatches(tstate, PyExc_TypeError))
2719
0
                    {
2720
0
                        check_args_iterable(tstate, PEEK(1 + oparg), PEEK(i));
2721
0
                    }
2722
0
                    Py_DECREF(sum);
2723
0
                    goto error;
2724
0
                }
2725
584
                Py_DECREF(none_val);
2726
584
            }
2727
2728
292
            if (convert_to_tuple) {
2729
292
                return_value = PyList_AsTuple(sum);
2730
292
                Py_DECREF(sum);
2731
292
                if (return_value == NULL)
2732
0
                    goto error;
2733
292
            }
2734
0
            else {
2735
0
                return_value = sum;
2736
0
            }
2737
2738
876
            while (oparg--)
2739
584
                Py_DECREF(POP());
2740
292
            PUSH(return_value);
2741
292
            DISPATCH();
2742
0
        }
2743
2744
74
        case TARGET(BUILD_SET): {
2745
74
            PyObject *set = PySet_New(NULL);
2746
74
            int err = 0;
2747
74
            int i;
2748
74
            if (set == NULL)
2749
0
                goto error;
2750
220
            for (i = oparg; i > 0; i--) {
2751
146
                PyObject *item = PEEK(i);
2752
146
                if (err == 0)
2753
146
                    err = PySet_Add(set, item);
2754
146
                Py_DECREF(item);
2755
146
            }
2756
74
            STACK_SHRINK(oparg);
2757
74
            if (err != 0) {
2758
0
                Py_DECREF(set);
2759
0
                goto error;
2760
0
            }
2761
74
            PUSH(set);
2762
74
            DISPATCH();
2763
0
        }
2764
2765
0
        case TARGET(BUILD_SET_UNPACK): {
2766
0
            Py_ssize_t i;
2767
0
            PyObject *sum = PySet_New(NULL);
2768
0
            if (sum == NULL)
2769
0
                goto error;
2770
2771
0
            for (i = oparg; i > 0; i--) {
2772
0
                if (_PySet_Update(sum, PEEK(i)) < 0) {
2773
0
                    Py_DECREF(sum);
2774
0
                    goto error;
2775
0
                }
2776
0
            }
2777
2778
0
            while (oparg--)
2779
0
                Py_DECREF(POP());
2780
0
            PUSH(sum);
2781
0
            DISPATCH();
2782
0
        }
2783
2784
2.36k
        case TARGET(BUILD_MAP): {
2785
2.36k
            Py_ssize_t i;
2786
2.36k
            PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2787
2.36k
            if (map == NULL)
2788
0
                goto error;
2789
2.40k
            for (i = oparg; i > 0; i--) {
2790
48
                int err;
2791
48
                PyObject *key = PEEK(2*i);
2792
48
                PyObject *value = PEEK(2*i - 1);
2793
48
                err = PyDict_SetItem(map, key, value);
2794
48
                if (err != 0) {
2795
0
                    Py_DECREF(map);
2796
0
                    goto error;
2797
0
                }
2798
48
            }
2799
2800
2.40k
            while (oparg--) {
2801
48
                Py_DECREF(POP());
2802
48
                Py_DECREF(POP());
2803
48
            }
2804
2.36k
            PUSH(map);
2805
2.36k
            DISPATCH();
2806
0
        }
2807
2808
0
        case TARGET(SETUP_ANNOTATIONS): {
2809
0
            _Py_IDENTIFIER(__annotations__);
2810
0
            int err;
2811
0
            PyObject *ann_dict;
2812
0
            if (f->f_locals == NULL) {
2813
0
                _PyErr_Format(tstate, PyExc_SystemError,
2814
0
                              "no locals found when setting up annotations");
2815
0
                goto error;
2816
0
            }
2817
            /* check if __annotations__ in locals()... */
2818
0
            if (PyDict_CheckExact(f->f_locals)) {
2819
0
                ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
2820
0
                                             &PyId___annotations__);
2821
0
                if (ann_dict == NULL) {
2822
0
                    if (_PyErr_Occurred(tstate)) {
2823
0
                        goto error;
2824
0
                    }
2825
                    /* ...if not, create a new one */
2826
0
                    ann_dict = PyDict_New();
2827
0
                    if (ann_dict == NULL) {
2828
0
                        goto error;
2829
0
                    }
2830
0
                    err = _PyDict_SetItemId(f->f_locals,
2831
0
                                            &PyId___annotations__, ann_dict);
2832
0
                    Py_DECREF(ann_dict);
2833
0
                    if (err != 0) {
2834
0
                        goto error;
2835
0
                    }
2836
0
                }
2837
0
            }
2838
0
            else {
2839
                /* do the same if locals() is not a dict */
2840
0
                PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2841
0
                if (ann_str == NULL) {
2842
0
                    goto error;
2843
0
                }
2844
0
                ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2845
0
                if (ann_dict == NULL) {
2846
0
                    if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2847
0
                        goto error;
2848
0
                    }
2849
0
                    _PyErr_Clear(tstate);
2850
0
                    ann_dict = PyDict_New();
2851
0
                    if (ann_dict == NULL) {
2852
0
                        goto error;
2853
0
                    }
2854
0
                    err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2855
0
                    Py_DECREF(ann_dict);
2856
0
                    if (err != 0) {
2857
0
                        goto error;
2858
0
                    }
2859
0
                }
2860
0
                else {
2861
0
                    Py_DECREF(ann_dict);
2862
0
                }
2863
0
            }
2864
0
            DISPATCH();
2865
0
        }
2866
2867
621
        case TARGET(BUILD_CONST_KEY_MAP): {
2868
621
            Py_ssize_t i;
2869
621
            PyObject *map;
2870
621
            PyObject *keys = TOP();
2871
621
            if (!PyTuple_CheckExact(keys) ||
2872
621
                PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2873
0
                _PyErr_SetString(tstate, PyExc_SystemError,
2874
0
                                 "bad BUILD_CONST_KEY_MAP keys argument");
2875
0
                goto error;
2876
0
            }
2877
621
            map = _PyDict_NewPresized((Py_ssize_t)oparg);
2878
621
            if (map == NULL) {
2879
0
                goto error;
2880
0
            }
2881
6.09k
            for (i = oparg; i > 0; i--) {
2882
5.47k
                int err;
2883
5.47k
                PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2884
5.47k
                PyObject *value = PEEK(i + 1);
2885
5.47k
                err = PyDict_SetItem(map, key, value);
2886
5.47k
                if (err != 0) {
2887
0
                    Py_DECREF(map);
2888
0
                    goto error;
2889
0
                }
2890
5.47k
            }
2891
2892
621
            Py_DECREF(POP());
2893
6.09k
            while (oparg--) {
2894
5.47k
                Py_DECREF(POP());
2895
5.47k
            }
2896
621
            PUSH(map);
2897
621
            DISPATCH();
2898
0
        }
2899
2900
0
        case TARGET(BUILD_MAP_UNPACK): {
2901
0
            Py_ssize_t i;
2902
0
            PyObject *sum = PyDict_New();
2903
0
            if (sum == NULL)
2904
0
                goto error;
2905
2906
0
            for (i = oparg; i > 0; i--) {
2907
0
                PyObject *arg = PEEK(i);
2908
0
                if (PyDict_Update(sum, arg) < 0) {
2909
0
                    if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2910
0
                        _PyErr_Format(tstate, PyExc_TypeError,
2911
0
                                      "'%.200s' object is not a mapping",
2912
0
                                      arg->ob_type->tp_name);
2913
0
                    }
2914
0
                    Py_DECREF(sum);
2915
0
                    goto error;
2916
0
                }
2917
0
            }
2918
2919
0
            while (oparg--)
2920
0
                Py_DECREF(POP());
2921
0
            PUSH(sum);
2922
0
            DISPATCH();
2923
0
        }
2924
2925
14
        case TARGET(BUILD_MAP_UNPACK_WITH_CALL): {
2926
14
            Py_ssize_t i;
2927
14
            PyObject *sum = PyDict_New();
2928
14
            if (sum == NULL)
2929
0
                goto error;
2930
2931
42
            for (i = oparg; i > 0; i--) {
2932
28
                PyObject *arg = PEEK(i);
2933
28
                if (_PyDict_MergeEx(sum, arg, 2) < 0) {
2934
0
                    Py_DECREF(sum);
2935
0
                    format_kwargs_error(tstate, PEEK(2 + oparg), arg);
2936
0
                    goto error;
2937
0
                }
2938
28
            }
2939
2940
42
            while (oparg--)
2941
28
                Py_DECREF(POP());
2942
14
            PUSH(sum);
2943
14
            DISPATCH();
2944
0
        }
2945
2946
212
        case TARGET(MAP_ADD): {
2947
212
            PyObject *value = TOP();
2948
212
            PyObject *key = SECOND();
2949
212
            PyObject *map;
2950
212
            int err;
2951
212
            STACK_SHRINK(2);
2952
212
            map = PEEK(oparg);                      /* dict */
2953
212
            assert(PyDict_CheckExact(map));
2954
212
            err = PyDict_SetItem(map, key, value);  /* map[key] = value */
2955
212
            Py_DECREF(value);
2956
212
            Py_DECREF(key);
2957
212
            if (err != 0)
2958
0
                goto error;
2959
212
            PREDICT(JUMP_ABSOLUTE);
2960
212
            DISPATCH();
2961
0
        }
2962
2963
49.3k
        case TARGET(LOAD_ATTR): {
2964
49.3k
            PyObject *name = GETITEM(names, oparg);
2965
49.3k
            PyObject *owner = TOP();
2966
49.3k
            PyObject *res = PyObject_GetAttr(owner, name);
2967
49.3k
            Py_DECREF(owner);
2968
49.3k
            SET_TOP(res);
2969
49.3k
            if (res == NULL)
2970
211
                goto error;
2971
49.1k
            DISPATCH();
2972
0
        }
2973
2974
44.9k
        case TARGET(COMPARE_OP): {
2975
44.9k
            PyObject *right = POP();
2976
44.9k
            PyObject *left = TOP();
2977
44.9k
            PyObject *res = cmp_outcome(tstate, oparg, left, right);
2978
44.9k
            Py_DECREF(left);
2979
44.9k
            Py_DECREF(right);
2980
44.9k
            SET_TOP(res);
2981
44.9k
            if (res == NULL)
2982
0
                goto error;
2983
44.9k
            PREDICT(POP_JUMP_IF_FALSE);
2984
44.9k
            PREDICT(POP_JUMP_IF_TRUE);
2985
44.9k
            DISPATCH();
2986
0
        }
2987
2988
832
        case TARGET(IMPORT_NAME): {
2989
832
            PyObject *name = GETITEM(names, oparg);
2990
832
            PyObject *fromlist = POP();
2991
832
            PyObject *level = TOP();
2992
832
            PyObject *res;
2993
832
            res = import_name(tstate, f, name, fromlist, level);
2994
832
            Py_DECREF(level);
2995
832
            Py_DECREF(fromlist);
2996
832
            SET_TOP(res);
2997
832
            if (res == NULL)
2998
34
                goto error;
2999
798
            DISPATCH();
3000
0
        }
3001
3002
57
        case TARGET(IMPORT_STAR): {
3003
57
            PyObject *from = POP(), *locals;
3004
57
            int err;
3005
57
            if (PyFrame_FastToLocalsWithError(f) < 0) {
3006
0
                Py_DECREF(from);
3007
0
                goto error;
3008
0
            }
3009
3010
57
            locals = f->f_locals;
3011
57
            if (locals == NULL) {
3012
0
                _PyErr_SetString(tstate, PyExc_SystemError,
3013
0
                                 "no locals found during 'import *'");
3014
0
                Py_DECREF(from);
3015
0
                goto error;
3016
0
            }
3017
57
            err = import_all_from(tstate, locals, from);
3018
57
            PyFrame_LocalsToFast(f, 0);
3019
57
            Py_DECREF(from);
3020
57
            if (err != 0)
3021
0
                goto error;
3022
57
            DISPATCH();
3023
0
        }
3024
3025
584
        case TARGET(IMPORT_FROM): {
3026
584
            PyObject *name = GETITEM(names, oparg);
3027
584
            PyObject *from = TOP();
3028
584
            PyObject *res;
3029
584
            res = import_from(tstate, from, name);
3030
584
            PUSH(res);
3031
584
            if (res == NULL)
3032
13
                goto error;
3033
571
            DISPATCH();
3034
0
        }
3035
3036
12.2k
        case TARGET(JUMP_FORWARD): {
3037
12.2k
            JUMPBY(oparg);
3038
12.2k
            FAST_DISPATCH();
3039
0
        }
3040
3041
56.1k
        case TARGET(POP_JUMP_IF_FALSE): {
3042
56.1k
            PREDICTED(POP_JUMP_IF_FALSE);
3043
56.1k
            PyObject *cond = POP();
3044
56.1k
            int err;
3045
56.1k
            if (cond == Py_True) {
3046
23.7k
                Py_DECREF(cond);
3047
23.7k
                FAST_DISPATCH();
3048
0
            }
3049
32.3k
            if (cond == Py_False) {
3050
24.9k
                Py_DECREF(cond);
3051
24.9k
                JUMPTO(oparg);
3052
24.9k
                FAST_DISPATCH();
3053
0
            }
3054
7.43k
            err = PyObject_IsTrue(cond);
3055
7.43k
            Py_DECREF(cond);
3056
7.43k
            if (err > 0)
3057
5.17k
                ;
3058
2.26k
            else if (err == 0)
3059
2.26k
                JUMPTO(oparg);
3060
0
            else
3061
0
                goto error;
3062
7.43k
            DISPATCH();
3063
0
        }
3064
3065
7.59k
        case TARGET(POP_JUMP_IF_TRUE): {
3066
7.59k
            PREDICTED(POP_JUMP_IF_TRUE);
3067
7.59k
            PyObject *cond = POP();
3068
7.59k
            int err;
3069
7.59k
            if (cond == Py_False) {
3070
3.27k
                Py_DECREF(cond);
3071
3.27k
                FAST_DISPATCH();
3072
0
            }
3073
4.31k
            if (cond == Py_True) {
3074
3.36k
                Py_DECREF(cond);
3075
3.36k
                JUMPTO(oparg);
3076
3.36k
                FAST_DISPATCH();
3077
0
            }
3078
956
            err = PyObject_IsTrue(cond);
3079
956
            Py_DECREF(cond);
3080
956
            if (err > 0) {
3081
759
                JUMPTO(oparg);
3082
759
            }
3083
197
            else if (err == 0)
3084
197
                ;
3085
0
            else
3086
0
                goto error;
3087
956
            DISPATCH();
3088
0
        }
3089
3090
505
        case TARGET(JUMP_IF_FALSE_OR_POP): {
3091
505
            PyObject *cond = TOP();
3092
505
            int err;
3093
505
            if (cond == Py_True) {
3094
363
                STACK_SHRINK(1);
3095
363
                Py_DECREF(cond);
3096
363
                FAST_DISPATCH();
3097
0
            }
3098
142
            if (cond == Py_False) {
3099
131
                JUMPTO(oparg);
3100
131
                FAST_DISPATCH();
3101
0
            }
3102
11
            err = PyObject_IsTrue(cond);
3103
11
            if (err > 0) {
3104
0
                STACK_SHRINK(1);
3105
0
                Py_DECREF(cond);
3106
0
            }
3107
11
            else if (err == 0)
3108
11
                JUMPTO(oparg);
3109
0
            else
3110
0
                goto error;
3111
11
            DISPATCH();
3112
0
        }
3113
3114
545
        case TARGET(JUMP_IF_TRUE_OR_POP): {
3115
545
            PyObject *cond = TOP();
3116
545
            int err;
3117
545
            if (cond == Py_False) {
3118
34
                STACK_SHRINK(1);
3119
34
                Py_DECREF(cond);
3120
34
                FAST_DISPATCH();
3121
0
            }
3122
511
            if (cond == Py_True) {
3123
2
                JUMPTO(oparg);
3124
2
                FAST_DISPATCH();
3125
0
            }
3126
509
            err = PyObject_IsTrue(cond);
3127
509
            if (err > 0) {
3128
487
                JUMPTO(oparg);
3129
487
            }
3130
22
            else if (err == 0) {
3131
22
                STACK_SHRINK(1);
3132
22
                Py_DECREF(cond);
3133
22
            }
3134
0
            else
3135
0
                goto error;
3136
509
            DISPATCH();
3137
0
        }
3138
3139
19.4k
        case TARGET(JUMP_ABSOLUTE): {
3140
19.4k
            PREDICTED(JUMP_ABSOLUTE);
3141
19.4k
            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
19.4k
            DISPATCH();
3153
0
#endif
3154
0
        }
3155
3156
5.82k
        case TARGET(GET_ITER): {
3157
            /* before: [obj]; after [getiter(obj)] */
3158
5.82k
            PyObject *iterable = TOP();
3159
5.82k
            PyObject *iter = PyObject_GetIter(iterable);
3160
5.82k
            Py_DECREF(iterable);
3161
5.82k
            SET_TOP(iter);
3162
5.82k
            if (iter == NULL)
3163
0
                goto error;
3164
5.82k
            PREDICT(FOR_ITER);
3165
5.82k
            PREDICT(CALL_FUNCTION);
3166
5.82k
            DISPATCH();
3167
0
        }
3168
3169
28
        case TARGET(GET_YIELD_FROM_ITER): {
3170
            /* before: [obj]; after [getiter(obj)] */
3171
28
            PyObject *iterable = TOP();
3172
28
            PyObject *iter;
3173
28
            if (PyCoro_CheckExact(iterable)) {
3174
                /* `iterable` is a coroutine */
3175
0
                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
0
                    Py_DECREF(iterable);
3179
0
                    SET_TOP(NULL);
3180
0
                    _PyErr_SetString(tstate, PyExc_TypeError,
3181
0
                                     "cannot 'yield from' a coroutine object "
3182
0
                                     "in a non-coroutine generator");
3183
0
                    goto error;
3184
0
                }
3185
0
            }
3186
28
            else if (!PyGen_CheckExact(iterable)) {
3187
                /* `iterable` is not a generator. */
3188
14
                iter = PyObject_GetIter(iterable);
3189
14
                Py_DECREF(iterable);
3190
14
                SET_TOP(iter);
3191
14
                if (iter == NULL)
3192
0
                    goto error;
3193
14
            }
3194
28
            PREDICT(LOAD_CONST);
3195
28
            DISPATCH();
3196
0
        }
3197
3198
26.7k
        case TARGET(FOR_ITER): {
3199
26.7k
            PREDICTED(FOR_ITER);
3200
            /* before: [iter]; after: [iter, iter()] *or* [] */
3201
26.7k
            PyObject *iter = TOP();
3202
26.7k
            PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3203
26.7k
            if (next != NULL) {
3204
22.5k
                PUSH(next);
3205
22.5k
                PREDICT(STORE_FAST);
3206
22.5k
                PREDICT(UNPACK_SEQUENCE);
3207
22.5k
                DISPATCH();
3208
0
            }
3209
4.18k
            if (_PyErr_Occurred(tstate)) {
3210
28
                if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
3211
0
                    goto error;
3212
0
                }
3213
28
                else if (tstate->c_tracefunc != NULL) {
3214
0
                    call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
3215
0
                }
3216
28
                _PyErr_Clear(tstate);
3217
28
            }
3218
            /* iterator ended normally */
3219
4.18k
            STACK_SHRINK(1);
3220
4.18k
            Py_DECREF(iter);
3221
4.18k
            JUMPBY(oparg);
3222
4.18k
            PREDICT(POP_BLOCK);
3223
4.18k
            DISPATCH();
3224
0
        }
3225
3226
19.1k
        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
19.1k
            PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3233
19.1k
                               STACK_LEVEL());
3234
19.1k
            DISPATCH();
3235
0
        }
3236
3237
0
        case TARGET(BEFORE_ASYNC_WITH): {
3238
0
            _Py_IDENTIFIER(__aexit__);
3239
0
            _Py_IDENTIFIER(__aenter__);
3240
3241
0
            PyObject *mgr = TOP();
3242
0
            PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__),
3243
0
                     *enter;
3244
0
            PyObject *res;
3245
0
            if (exit == NULL)
3246
0
                goto error;
3247
0
            SET_TOP(exit);
3248
0
            enter = special_lookup(tstate, mgr, &PyId___aenter__);
3249
0
            Py_DECREF(mgr);
3250
0
            if (enter == NULL)
3251
0
                goto error;
3252
0
            res = _PyObject_CallNoArg(enter);
3253
0
            Py_DECREF(enter);
3254
0
            if (res == NULL)
3255
0
                goto error;
3256
0
            PUSH(res);
3257
0
            PREDICT(GET_AWAITABLE);
3258
0
            DISPATCH();
3259
0
        }
3260
3261
0
        case TARGET(SETUP_ASYNC_WITH): {
3262
0
            PyObject *res = POP();
3263
            /* Setup the finally block before pushing the result
3264
               of __aenter__ on the stack. */
3265
0
            PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3266
0
                               STACK_LEVEL());
3267
0
            PUSH(res);
3268
0
            DISPATCH();
3269
0
        }
3270
3271
2.22k
        case TARGET(SETUP_WITH): {
3272
2.22k
            _Py_IDENTIFIER(__exit__);
3273
2.22k
            _Py_IDENTIFIER(__enter__);
3274
2.22k
            PyObject *mgr = TOP();
3275
2.22k
            PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
3276
2.22k
            PyObject *res;
3277
2.22k
            if (enter == NULL) {
3278
0
                goto error;
3279
0
            }
3280
2.22k
            PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
3281
2.22k
            if (exit == NULL) {
3282
0
                Py_DECREF(enter);
3283
0
                goto error;
3284
0
            }
3285
2.22k
            SET_TOP(exit);
3286
2.22k
            Py_DECREF(mgr);
3287
2.22k
            res = _PyObject_CallNoArg(enter);
3288
2.22k
            Py_DECREF(enter);
3289
2.22k
            if (res == NULL)
3290
0
                goto error;
3291
            /* Setup the finally block before pushing the result
3292
               of __enter__ on the stack. */
3293
2.22k
            PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3294
2.22k
                               STACK_LEVEL());
3295
3296
2.22k
            PUSH(res);
3297
2.22k
            DISPATCH();
3298
0
        }
3299
3300
2.22k
        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
2.22k
            PyObject *stack[3];
3322
2.22k
            PyObject *exit_func;
3323
2.22k
            PyObject *exc, *val, *tb, *res;
3324
3325
2.22k
            val = tb = Py_None;
3326
2.22k
            exc = TOP();
3327
2.22k
            if (exc == NULL) {
3328
2.18k
                STACK_SHRINK(1);
3329
2.18k
                exit_func = TOP();
3330
2.18k
                SET_TOP(exc);
3331
2.18k
                exc = Py_None;
3332
2.18k
            }
3333
34
            else {
3334
34
                assert(PyExceptionClass_Check(exc));
3335
34
                PyObject *tp2, *exc2, *tb2;
3336
34
                PyTryBlock *block;
3337
34
                val = SECOND();
3338
34
                tb = THIRD();
3339
34
                tp2 = FOURTH();
3340
34
                exc2 = PEEK(5);
3341
34
                tb2 = PEEK(6);
3342
34
                exit_func = PEEK(7);
3343
34
                SET_VALUE(7, tb2);
3344
34
                SET_VALUE(6, exc2);
3345
34
                SET_VALUE(5, tp2);
3346
                /* UNWIND_EXCEPT_HANDLER will pop this off. */
3347
34
                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
34
                assert(f->f_iblock > 0);
3352
34
                block = &f->f_blockstack[f->f_iblock - 1];
3353
34
                assert(block->b_type == EXCEPT_HANDLER);
3354
34
                assert(block->b_level > 0);
3355
34
                block->b_level--;
3356
34
            }
3357
3358
2.22k
            stack[0] = exc;
3359
2.22k
            stack[1] = val;
3360
2.22k
            stack[2] = tb;
3361
2.22k
            res = _PyObject_FastCall(exit_func, stack, 3);
3362
2.22k
            Py_DECREF(exit_func);
3363
2.22k
            if (res == NULL)
3364
0
                goto error;
3365
3366
2.22k
            Py_INCREF(exc); /* Duplicating the exception on the stack */
3367
2.22k
            PUSH(exc);
3368
2.22k
            PUSH(res);
3369
2.22k
            PREDICT(WITH_CLEANUP_FINISH);
3370
2.22k
            DISPATCH();
3371
0
        }
3372
3373
2.22k
        case TARGET(WITH_CLEANUP_FINISH): {
3374
2.22k
            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
2.22k
            PyObject *res = POP();
3382
2.22k
            PyObject *exc = POP();
3383
2.22k
            int err;
3384
3385
2.22k
            if (exc != Py_None)
3386
34
                err = PyObject_IsTrue(res);
3387
2.18k
            else
3388
2.18k
                err = 0;
3389
3390
2.22k
            Py_DECREF(res);
3391
2.22k
            Py_DECREF(exc);
3392
3393
2.22k
            if (err < 0)
3394
0
                goto error;
3395
2.22k
            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
0
                PyTryBlock *b = PyFrame_BlockPop(f);
3402
0
                assert(b->b_type == EXCEPT_HANDLER);
3403
0
                UNWIND_EXCEPT_HANDLER(b);
3404
0
                PUSH(NULL);
3405
0
            }
3406
2.22k
            PREDICT(END_FINALLY);
3407
2.22k
            DISPATCH();
3408
0
        }
3409
3410
32.8k
        case TARGET(LOAD_METHOD): {
3411
            /* Designed to work in tandem with CALL_METHOD. */
3412
32.8k
            PyObject *name = GETITEM(names, oparg);
3413
32.8k
            PyObject *obj = TOP();
3414
32.8k
            PyObject *meth = NULL;
3415
3416
32.8k
            int meth_found = _PyObject_GetMethod(obj, name, &meth);
3417
3418
32.8k
            if (meth == NULL) {
3419
                /* Most likely attribute wasn't found. */
3420
27
                goto error;
3421
27
            }
3422
3423
32.7k
            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
17.7k
                SET_TOP(meth);
3430
17.7k
                PUSH(obj);  // self
3431
17.7k
            }
3432
15.0k
            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
15.0k
                SET_TOP(NULL);
3441
15.0k
                Py_DECREF(obj);
3442
15.0k
                PUSH(meth);
3443
15.0k
            }
3444
32.7k
            DISPATCH();
3445
0
        }
3446
3447
32.7k
        case TARGET(CALL_METHOD): {
3448
            /* Designed to work in tamdem with LOAD_METHOD. */
3449
32.7k
            PyObject **sp, *res, *meth;
3450
3451
32.7k
            sp = stack_pointer;
3452
3453
32.7k
            meth = PEEK(oparg + 2);
3454
32.7k
            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
15.0k
                res = call_function(tstate, &sp, oparg, NULL);
3470
15.0k
                stack_pointer = sp;
3471
15.0k
                (void)POP(); /* POP the NULL. */
3472
15.0k
            }
3473
17.7k
            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
17.7k
                res = call_function(tstate, &sp, oparg + 1, NULL);
3487
17.7k
                stack_pointer = sp;
3488
17.7k
            }
3489
3490
32.7k
            PUSH(res);
3491
32.7k
            if (res == NULL)
3492
1.31k
                goto error;
3493
31.4k
            DISPATCH();
3494
0
        }
3495
3496
42.6k
        case TARGET(CALL_FUNCTION): {
3497
42.6k
            PREDICTED(CALL_FUNCTION);
3498
42.6k
            PyObject **sp, *res;
3499
42.6k
            sp = stack_pointer;
3500
42.6k
            res = call_function(tstate, &sp, oparg, NULL);
3501
42.6k
            stack_pointer = sp;
3502
42.6k
            PUSH(res);
3503
42.6k
            if (res == NULL) {
3504
1.28k
                goto error;
3505
1.28k
            }
3506
41.3k
            DISPATCH();
3507
0
        }
3508
3509
2.52k
        case TARGET(CALL_FUNCTION_KW): {
3510
2.52k
            PyObject **sp, *res, *names;
3511
3512
2.52k
            names = POP();
3513
2.52k
            assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
3514
2.52k
            sp = stack_pointer;
3515
2.52k
            res = call_function(tstate, &sp, oparg, names);
3516
2.52k
            stack_pointer = sp;
3517
2.52k
            PUSH(res);
3518
2.52k
            Py_DECREF(names);
3519
3520
2.52k
            if (res == NULL) {
3521
0
                goto error;
3522
0
            }
3523
2.52k
            DISPATCH();
3524
0
        }
3525
3526
1.45k
        case TARGET(CALL_FUNCTION_EX): {
3527
1.45k
            PyObject *func, *callargs, *kwargs = NULL, *result;
3528
1.45k
            if (oparg & 0x01) {
3529
1.27k
                kwargs = POP();
3530
1.27k
                if (!PyDict_CheckExact(kwargs)) {
3531
0
                    PyObject *d = PyDict_New();
3532
0
                    if (d == NULL)
3533
0
                        goto error;
3534
0
                    if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
3535
0
                        Py_DECREF(d);
3536
0
                        format_kwargs_error(tstate, SECOND(), kwargs);
3537
0
                        Py_DECREF(kwargs);
3538
0
                        goto error;
3539
0
                    }
3540
0
                    Py_DECREF(kwargs);
3541
0
                    kwargs = d;
3542
0
                }
3543
1.27k
                assert(PyDict_CheckExact(kwargs));
3544
1.27k
            }
3545
1.45k
            callargs = POP();
3546
1.45k
            func = TOP();
3547
1.45k
            if (!PyTuple_CheckExact(callargs)) {
3548
39
                if (check_args_iterable(tstate, func, callargs) < 0) {
3549
0
                    Py_DECREF(callargs);
3550
0
                    goto error;
3551
0
                }
3552
39
                Py_SETREF(callargs, PySequence_Tuple(callargs));
3553
39
                if (callargs == NULL) {
3554
0
                    goto error;
3555
0
                }
3556
39
            }
3557
1.45k
            assert(PyTuple_CheckExact(callargs));
3558
3559
1.45k
            result = do_call_core(tstate, func, callargs, kwargs);
3560
1.45k
            Py_DECREF(func);
3561
1.45k
            Py_DECREF(callargs);
3562
1.45k
            Py_XDECREF(kwargs);
3563
3564
1.45k
            SET_TOP(result);
3565
1.45k
            if (result == NULL) {
3566
0
                goto error;
3567
0
            }
3568
1.45k
            DISPATCH();
3569
0
        }
3570
3571
11.7k
        case TARGET(MAKE_FUNCTION): {
3572
11.7k
            PyObject *qualname = POP();
3573
11.7k
            PyObject *codeobj = POP();
3574
11.7k
            PyFunctionObject *func = (PyFunctionObject *)
3575
11.7k
                PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
3576
3577
11.7k
            Py_DECREF(codeobj);
3578
11.7k
            Py_DECREF(qualname);
3579
11.7k
            if (func == NULL) {
3580
0
                goto error;
3581
0
            }
3582
3583
11.7k
            if (oparg & 0x08) {
3584
452
                assert(PyTuple_CheckExact(TOP()));
3585
452
                func ->func_closure = POP();
3586
452
            }
3587
11.7k
            if (oparg & 0x04) {
3588
0
                assert(PyDict_CheckExact(TOP()));
3589
0
                func->func_annotations = POP();
3590
0
            }
3591
11.7k
            if (oparg & 0x02) {
3592
156
                assert(PyDict_CheckExact(TOP()));
3593
156
                func->func_kwdefaults = POP();
3594
156
            }
3595
11.7k
            if (oparg & 0x01) {
3596
1.73k
                assert(PyTuple_CheckExact(TOP()));
3597
1.73k
                func->func_defaults = POP();
3598
1.73k
            }
3599
3600
11.7k
            PUSH((PyObject *)func);
3601
11.7k
            DISPATCH();
3602
0
        }
3603
3604
2.28k
        case TARGET(BUILD_SLICE): {
3605
2.28k
            PyObject *start, *stop, *step, *slice;
3606
2.28k
            if (oparg == 3)
3607
45
                step = POP();
3608
2.23k
            else
3609
2.23k
                step = NULL;
3610
2.28k
            stop = POP();
3611
2.28k
            start = TOP();
3612
2.28k
            slice = PySlice_New(start, stop, step);
3613
2.28k
            Py_DECREF(start);
3614
2.28k
            Py_DECREF(stop);
3615
2.28k
            Py_XDECREF(step);
3616
2.28k
            SET_TOP(slice);
3617
2.28k
            if (slice == NULL)
3618
0
                goto error;
3619
2.28k
            DISPATCH();
3620
0
        }
3621
3622
112
        case TARGET(FORMAT_VALUE): {
3623
            /* Handles f-string value formatting. */
3624
112
            PyObject *result;
3625
112
            PyObject *fmt_spec;
3626
112
            PyObject *value;
3627
112
            PyObject *(*conv_fn)(PyObject *);
3628
112
            int which_conversion = oparg & FVC_MASK;
3629
112
            int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3630
3631
112
            fmt_spec = have_fmt_spec ? POP() : NULL;
3632
112
            value = POP();
3633
3634
            /* See if any conversion is specified. */
3635
112
            switch (which_conversion) {
3636
112
            case FVC_NONE:  conv_fn = NULL;           break;
3637
0
            case FVC_STR:   conv_fn = PyObject_Str;   break;
3638
0
            case FVC_REPR:  conv_fn = PyObject_Repr;  break;
3639
0
            case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3640
0
            default:
3641
0
                _PyErr_Format(tstate, PyExc_SystemError,
3642
0
                              "unexpected conversion flag %d",
3643
0
                              which_conversion);
3644
0
                goto error;
3645
112
            }
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
112
            if (conv_fn != NULL) {
3651
0
                result = conv_fn(value);
3652
0
                Py_DECREF(value);
3653
0
                if (result == NULL) {
3654
0
                    Py_XDECREF(fmt_spec);
3655
0
                    goto error;
3656
0
                }
3657
0
                value = result;
3658
0
            }
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
112
            if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3666
                /* Do nothing, just transfer ownership to result. */
3667
77
                result = value;
3668
77
            } else {
3669
                /* Actually call format(). */
3670
35
                result = PyObject_Format(value, fmt_spec);
3671
35
                Py_DECREF(value);
3672
35
                Py_XDECREF(fmt_spec);
3673
35
                if (result == NULL) {
3674
0
                    goto error;
3675
0
                }
3676
35
            }
3677
3678
112
            PUSH(result);
3679
112
            DISPATCH();
3680
0
        }
3681
3682
10.2k
        case TARGET(EXTENDED_ARG): {
3683
10.2k
            int oldoparg = oparg;
3684
10.2k
            NEXTOPARG();
3685
10.2k
            oparg |= oldoparg << 8;
3686
10.2k
            goto dispatch_opcode;
3687
0
        }
3688
3689
3690
0
#if USE_COMPUTED_GOTOS
3691
0
        _unknown_opcode:
3692
0
#endif
3693
0
        default:
3694
0
            fprintf(stderr,
3695
0
                "XXX lineno: %d, opcode: %d\n",
3696
0
                PyFrame_GetLineNumber(f),
3697
0
                opcode);
3698
0
            _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
3699
0
            goto error;
3700
3701
52.1k
        } /* switch */
3702
3703
        /* This should never be reached. Every opcode should end with DISPATCH()
3704
           or goto error. */
3705
52.1k
        Py_UNREACHABLE();
3706
3707
3.80k
error:
3708
        /* Double-check exception status. */
3709
3.80k
#ifdef NDEBUG
3710
3.80k
        if (!_PyErr_Occurred(tstate)) {
3711
0
            _PyErr_SetString(tstate, PyExc_SystemError,
3712
0
                             "error return without exception set");
3713
0
        }
3714
#else
3715
        assert(_PyErr_Occurred(tstate));
3716
#endif
3717
3718
        /* Log traceback info. */
3719
3.80k
        PyTraceBack_Here(f);
3720
3721
3.80k
        if (tstate->c_tracefunc != NULL)
3722
0
            call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3723
0
                           tstate, f);
3724
3725
3.92k
exception_unwind:
3726
        /* Unwind stacks if an exception occurred */
3727
4.21k
        while (f->f_iblock > 0) {
3728
            /* Pop the current block. */
3729
3.73k
            PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
3730
3731
3.73k
            if (b->b_type == EXCEPT_HANDLER) {
3732
290
                UNWIND_EXCEPT_HANDLER(b);
3733
290
                continue;
3734
290
            }
3735
3.44k
            UNWIND_BLOCK(b);
3736
3.44k
            if (b->b_type == SETUP_FINALLY) {
3737
3.44k
                PyObject *exc, *val, *tb;
3738
3.44k
                int handler = b->b_handler;
3739
3.44k
                _PyErr_StackItem *exc_info = tstate->exc_info;
3740
                /* Beware, this invalidates all b->b_* fields */
3741
3.44k
                PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3742
3.44k
                PUSH(exc_info->exc_traceback);
3743
3.44k
                PUSH(exc_info->exc_value);
3744
3.44k
                if (exc_info->exc_type != NULL) {
3745
3.43k
                    PUSH(exc_info->exc_type);
3746
3.43k
                }
3747
13
                else {
3748
13
                    Py_INCREF(Py_None);
3749
13
                    PUSH(Py_None);
3750
13
                }
3751
3.44k
                _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
3.44k
                _PyErr_NormalizeException(tstate, &exc, &val, &tb);
3757
3.44k
                if (tb != NULL)
3758
3.44k
                    PyException_SetTraceback(val, tb);
3759
0
                else
3760
0
                    PyException_SetTraceback(val, Py_None);
3761
3.44k
                Py_INCREF(exc);
3762
3.44k
                exc_info->exc_type = exc;
3763
3.44k
                Py_INCREF(val);
3764
3.44k
                exc_info->exc_value = val;
3765
3.44k
                exc_info->exc_traceback = tb;
3766
3.44k
                if (tb == NULL)
3767
0
                    tb = Py_None;
3768
3.44k
                Py_INCREF(tb);
3769
3.44k
                PUSH(tb);
3770
3.44k
                PUSH(val);
3771
3.44k
                PUSH(exc);
3772
3.44k
                JUMPTO(handler);
3773
                /* Resume normal execution */
3774
3.44k
                goto main_loop;
3775
3.44k
            }
3776
3.44k
        } /* unwind stack */
3777
3778
        /* End the loop as we still have an error */
3779
478
        break;
3780
3.92k
    } /* main loop */
3781
3782
41.8k
    assert(retval == NULL);
3783
478
    assert(_PyErr_Occurred(tstate));
3784
3785
37.6k
exit_returning:
3786
3787
    /* Pop remaining stack entries. */
3788
37.9k
    while (!EMPTY()) {
3789
248
        PyObject *o = POP();
3790
248
        Py_XDECREF(o);
3791
248
    }
3792
3793
38.4k
exit_yielding:
3794
38.4k
    if (tstate->use_tracing) {
3795
0
        if (tstate->c_tracefunc) {
3796
0
            if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3797
0
                                     tstate, f, PyTrace_RETURN, retval)) {
3798
0
                Py_CLEAR(retval);
3799
0
            }
3800
0
        }
3801
0
        if (tstate->c_profilefunc) {
3802
0
            if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3803
0
                                     tstate, f, PyTrace_RETURN, retval)) {
3804
0
                Py_CLEAR(retval);
3805
0
            }
3806
0
        }
3807
0
    }
3808
3809
    /* pop frame */
3810
38.4k
exit_eval_frame:
3811
38.4k
    if (PyDTrace_FUNCTION_RETURN_ENABLED())
3812
0
        dtrace_function_return(f);
3813
38.4k
    Py_LeaveRecursiveCall();
3814
38.4k
    f->f_executing = 0;
3815
38.4k
    tstate->frame = f->f_back;
3816
3817
38.4k
    return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
3818
38.4k
}
3819
3820
static void
3821
format_missing(PyThreadState *tstate, const char *kind,
3822
               PyCodeObject *co, PyObject *names)
3823
0
{
3824
0
    int err;
3825
0
    Py_ssize_t len = PyList_GET_SIZE(names);
3826
0
    PyObject *name_str, *comma, *tail, *tmp;
3827
3828
0
    assert(PyList_CheckExact(names));
3829
0
    assert(len >= 1);
3830
    /* Deal with the joys of natural language. */
3831
0
    switch (len) {
3832
0
    case 1:
3833
0
        name_str = PyList_GET_ITEM(names, 0);
3834
0
        Py_INCREF(name_str);
3835
0
        break;
3836
0
    case 2:
3837
0
        name_str = PyUnicode_FromFormat("%U and %U",
3838
0
                                        PyList_GET_ITEM(names, len - 2),
3839
0
                                        PyList_GET_ITEM(names, len - 1));
3840
0
        break;
3841
0
    default:
3842
0
        tail = PyUnicode_FromFormat(", %U, and %U",
3843
0
                                    PyList_GET_ITEM(names, len - 2),
3844
0
                                    PyList_GET_ITEM(names, len - 1));
3845
0
        if (tail == NULL)
3846
0
            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
0
        err = PyList_SetSlice(names, len - 2, len, NULL);
3850
0
        if (err == -1) {
3851
0
            Py_DECREF(tail);
3852
0
            return;
3853
0
        }
3854
        /* Stitch everything up into a nice comma-separated list. */
3855
0
        comma = PyUnicode_FromString(", ");
3856
0
        if (comma == NULL) {
3857
0
            Py_DECREF(tail);
3858
0
            return;
3859
0
        }
3860
0
        tmp = PyUnicode_Join(comma, names);
3861
0
        Py_DECREF(comma);
3862
0
        if (tmp == NULL) {
3863
0
            Py_DECREF(tail);
3864
0
            return;
3865
0
        }
3866
0
        name_str = PyUnicode_Concat(tmp, tail);
3867
0
        Py_DECREF(tmp);
3868
0
        Py_DECREF(tail);
3869
0
        break;
3870
0
    }
3871
0
    if (name_str == NULL)
3872
0
        return;
3873
0
    _PyErr_Format(tstate, PyExc_TypeError,
3874
0
                  "%U() missing %i required %s argument%s: %U",
3875
0
                  co->co_name,
3876
0
                  len,
3877
0
                  kind,
3878
0
                  len == 1 ? "" : "s",
3879
0
                  name_str);
3880
0
    Py_DECREF(name_str);
3881
0
}
3882
3883
static void
3884
missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3885
                  Py_ssize_t missing, Py_ssize_t defcount,
3886
                  PyObject **fastlocals)
3887
0
{
3888
0
    Py_ssize_t i, j = 0;
3889
0
    Py_ssize_t start, end;
3890
0
    int positional = (defcount != -1);
3891
0
    const char *kind = positional ? "positional" : "keyword-only";
3892
0
    PyObject *missing_names;
3893
3894
    /* Compute the names of the arguments that are missing. */
3895
0
    missing_names = PyList_New(missing);
3896
0
    if (missing_names == NULL)
3897
0
        return;
3898
0
    if (positional) {
3899
0
        start = 0;
3900
0
        end = co->co_argcount - defcount;
3901
0
    }
3902
0
    else {
3903
0
        start = co->co_argcount;
3904
0
        end = start + co->co_kwonlyargcount;
3905
0
    }
3906
0
    for (i = start; i < end; i++) {
3907
0
        if (GETLOCAL(i) == NULL) {
3908
0
            PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3909
0
            PyObject *name = PyObject_Repr(raw);
3910
0
            if (name == NULL) {
3911
0
                Py_DECREF(missing_names);
3912
0
                return;
3913
0
            }
3914
0
            PyList_SET_ITEM(missing_names, j++, name);
3915
0
        }
3916
0
    }
3917
0
    assert(j == missing);
3918
0
    format_missing(tstate, kind, co, missing_names);
3919
0
    Py_DECREF(missing_names);
3920
0
}
3921
3922
static void
3923
too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3924
                    Py_ssize_t given, Py_ssize_t defcount,
3925
                    PyObject **fastlocals)
3926
0
{
3927
0
    int plural;
3928
0
    Py_ssize_t kwonly_given = 0;
3929
0
    Py_ssize_t i;
3930
0
    PyObject *sig, *kwonly_sig;
3931
0
    Py_ssize_t co_argcount = co->co_argcount;
3932
3933
0
    assert((co->co_flags & CO_VARARGS) == 0);
3934
    /* Count missing keyword-only args. */
3935
0
    for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3936
0
        if (GETLOCAL(i) != NULL) {
3937
0
            kwonly_given++;
3938
0
        }
3939
0
    }
3940
0
    if (defcount) {
3941
0
        Py_ssize_t atleast = co_argcount - defcount;
3942
0
        plural = 1;
3943
0
        sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
3944
0
    }
3945
0
    else {
3946
0
        plural = (co_argcount != 1);
3947
0
        sig = PyUnicode_FromFormat("%zd", co_argcount);
3948
0
    }
3949
0
    if (sig == NULL)
3950
0
        return;
3951
0
    if (kwonly_given) {
3952
0
        const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3953
0
        kwonly_sig = PyUnicode_FromFormat(format,
3954
0
                                          given != 1 ? "s" : "",
3955
0
                                          kwonly_given,
3956
0
                                          kwonly_given != 1 ? "s" : "");
3957
0
        if (kwonly_sig == NULL) {
3958
0
            Py_DECREF(sig);
3959
0
            return;
3960
0
        }
3961
0
    }
3962
0
    else {
3963
        /* This will not fail. */
3964
0
        kwonly_sig = PyUnicode_FromString("");
3965
0
        assert(kwonly_sig != NULL);
3966
0
    }
3967
0
    _PyErr_Format(tstate, PyExc_TypeError,
3968
0
                  "%U() takes %U positional argument%s but %zd%U %s given",
3969
0
                  co->co_name,
3970
0
                  sig,
3971
0
                  plural ? "s" : "",
3972
0
                  given,
3973
0
                  kwonly_sig,
3974
0
                  given == 1 && !kwonly_given ? "was" : "were");
3975
0
    Py_DECREF(sig);
3976
0
    Py_DECREF(kwonly_sig);
3977
0
}
3978
3979
static int
3980
positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
3981
                                  Py_ssize_t kwcount, PyObject* const* kwnames)
3982
0
{
3983
0
    int posonly_conflicts = 0;
3984
0
    PyObject* posonly_names = PyList_New(0);
3985
3986
0
    for(int k=0; k < co->co_posonlyargcount; k++){
3987
0
        PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
3988
3989
0
        for (int k2=0; k2<kwcount; k2++){
3990
            /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
3991
0
            PyObject* kwname = kwnames[k2];
3992
0
            if (kwname == posonly_name){
3993
0
                if(PyList_Append(posonly_names, kwname) != 0) {
3994
0
                    goto fail;
3995
0
                }
3996
0
                posonly_conflicts++;
3997
0
                continue;
3998
0
            }
3999
4000
0
            int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
4001
4002
0
            if ( cmp > 0) {
4003
0
                if(PyList_Append(posonly_names, kwname) != 0) {
4004
0
                    goto fail;
4005
0
                }
4006
0
                posonly_conflicts++;
4007
0
            } else if (cmp < 0) {
4008
0
                goto fail;
4009
0
            }
4010
4011
0
        }
4012
0
    }
4013
0
    if (posonly_conflicts) {
4014
0
        PyObject* comma = PyUnicode_FromString(", ");
4015
0
        if (comma == NULL) {
4016
0
            goto fail;
4017
0
        }
4018
0
        PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4019
0
        Py_DECREF(comma);
4020
0
        if (error_names == NULL) {
4021
0
            goto fail;
4022
0
        }
4023
0
        _PyErr_Format(tstate, PyExc_TypeError,
4024
0
                      "%U() got some positional-only arguments passed"
4025
0
                      " as keyword arguments: '%U'",
4026
0
                      co->co_name, error_names);
4027
0
        Py_DECREF(error_names);
4028
0
        goto fail;
4029
0
    }
4030
4031
0
    Py_DECREF(posonly_names);
4032
0
    return 0;
4033
4034
0
fail:
4035
0
    Py_XDECREF(posonly_names);
4036
0
    return 1;
4037
4038
0
}
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 *
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
17.1k
{
4053
17.1k
    PyCodeObject* co = (PyCodeObject*)_co;
4054
17.1k
    PyFrameObject *f;
4055
17.1k
    PyObject *retval = NULL;
4056
17.1k
    PyObject **fastlocals, **freevars;
4057
17.1k
    PyObject *x, *u;
4058
17.1k
    const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
4059
17.1k
    Py_ssize_t i, j, n;
4060
17.1k
    PyObject *kwdict;
4061
4062
17.1k
    PyThreadState *tstate = _PyThreadState_GET();
4063
17.1k
    assert(tstate != NULL);
4064
4065
17.1k
    if (globals == NULL) {
4066
0
        _PyErr_SetString(tstate, PyExc_SystemError,
4067
0
                         "PyEval_EvalCodeEx: NULL globals");
4068
0
        return NULL;
4069
0
    }
4070
4071
    /* Create the frame */
4072
17.1k
    f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
4073
17.1k
    if (f == NULL) {
4074
0
        return NULL;
4075
0
    }
4076
17.1k
    fastlocals = f->f_localsplus;
4077
17.1k
    freevars = f->f_localsplus + co->co_nlocals;
4078
4079
    /* Create a dictionary for keyword parameters (**kwags) */
4080
17.1k
    if (co->co_flags & CO_VARKEYWORDS) {
4081
1.63k
        kwdict = PyDict_New();
4082
1.63k
        if (kwdict == NULL)
4083
0
            goto fail;
4084
1.63k
        i = total_args;
4085
1.63k
        if (co->co_flags & CO_VARARGS) {
4086
1.19k
            i++;
4087
1.19k
        }
4088
1.63k
        SETLOCAL(i, kwdict);
4089
1.63k
    }
4090
15.5k
    else {
4091
15.5k
        kwdict = NULL;
4092
15.5k
    }
4093
4094
    /* Copy all positional arguments into local variables */
4095
17.1k
    if (argcount > co->co_argcount) {
4096
4.76k
        n = co->co_argcount;
4097
4.76k
    }
4098
12.3k
    else {
4099
12.3k
        n = argcount;
4100
12.3k
    }
4101
38.1k
    for (j = 0; j < n; j++) {
4102
20.9k
        x = args[j];
4103
20.9k
        Py_INCREF(x);
4104
20.9k
        SETLOCAL(j, x);
4105
20.9k
    }
4106
4107
    /* Pack other positional arguments into the *args argument */
4108
17.1k
    if (co->co_flags & CO_VARARGS) {
4109
5.04k
        u = _PyTuple_FromArray(args + n, argcount - n);
4110
5.04k
        if (u == NULL) {
4111
0
            goto fail;
4112
0
        }
4113
5.04k
        SETLOCAL(total_args, u);
4114
5.04k
    }
4115
4116
    /* Handle keyword arguments passed as two strided arrays */
4117
17.1k
    kwcount *= kwstep;
4118
20.4k
    for (i = 0; i < kwcount; i += kwstep) {
4119
3.29k
        PyObject **co_varnames;
4120
3.29k
        PyObject *keyword = kwnames[i];
4121
3.29k
        PyObject *value = kwargs[i];
4122
3.29k
        Py_ssize_t j;
4123
4124
3.29k
        if (keyword == NULL || !PyUnicode_Check(keyword)) {
4125
0
            _PyErr_Format(tstate, PyExc_TypeError,
4126
0
                          "%U() keywords must be strings",
4127
0
                          co->co_name);
4128
0
            goto fail;
4129
0
        }
4130
4131
        /* Speed hack: do raw pointer compares. As names are
4132
           normally interned this should almost always hit. */
4133
3.29k
        co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
4134
10.7k
        for (j = co->co_posonlyargcount; j < total_args; j++) {
4135
10.7k
            PyObject *name = co_varnames[j];
4136
10.7k
            if (name == keyword) {
4137
3.26k
                goto kw_found;
4138
3.26k
            }
4139
10.7k
        }
4140
4141
        /* Slow fallback, just in case */
4142
70
        for (j = co->co_posonlyargcount; j < total_args; j++) {
4143
42
            PyObject *name = co_varnames[j];
4144
42
            int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4145
42
            if (cmp > 0) {
4146
0
                goto kw_found;
4147
0
            }
4148
42
            else if (cmp < 0) {
4149
0
                goto fail;
4150
0
            }
4151
42
        }
4152
4153
28
        assert(j >= total_args);
4154
28
        if (kwdict == NULL) {
4155
4156
0
            if (co->co_posonlyargcount
4157
0
                && positional_only_passed_as_keyword(tstate, co,
4158
0
                                                     kwcount, kwnames))
4159
0
            {
4160
0
                goto fail;
4161
0
            }
4162
4163
0
            _PyErr_Format(tstate, PyExc_TypeError,
4164
0
                          "%U() got an unexpected keyword argument '%S'",
4165
0
                          co->co_name, keyword);
4166
0
            goto fail;
4167
0
        }
4168
4169
28
        if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4170
0
            goto fail;
4171
0
        }
4172
28
        continue;
4173
4174
3.26k
      kw_found:
4175
3.26k
        if (GETLOCAL(j) != NULL) {
4176
0
            _PyErr_Format(tstate, PyExc_TypeError,
4177
0
                          "%U() got multiple values for argument '%S'",
4178
0
                          co->co_name, keyword);
4179
0
            goto fail;
4180
0
        }
4181
3.26k
        Py_INCREF(value);
4182
3.26k
        SETLOCAL(j, value);
4183
3.26k
    }
4184
4185
    /* Check the number of positional arguments */
4186
17.1k
    if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
4187
0
        too_many_positional(tstate, co, argcount, defcount, fastlocals);
4188
0
        goto fail;
4189
0
    }
4190
4191
    /* Add missing positional arguments (copy default values from defs) */
4192
17.1k
    if (argcount < co->co_argcount) {
4193
1.72k
        Py_ssize_t m = co->co_argcount - defcount;
4194
1.72k
        Py_ssize_t missing = 0;
4195
1.77k
        for (i = argcount; i < m; i++) {
4196
54
            if (GETLOCAL(i) == NULL) {
4197
0
                missing++;
4198
0
            }
4199
54
        }
4200
1.72k
        if (missing) {
4201
0
            missing_arguments(tstate, co, missing, defcount, fastlocals);
4202
0
            goto fail;
4203
0
        }
4204
1.72k
        if (n > m)
4205
78
            i = n - m;
4206
1.64k
        else
4207
1.64k
            i = 0;
4208
4.18k
        for (; i < defcount; i++) {
4209
2.46k
            if (GETLOCAL(m+i) == NULL) {
4210
1.65k
                PyObject *def = defs[i];
4211
1.65k
                Py_INCREF(def);
4212
1.65k
                SETLOCAL(m+i, def);
4213
1.65k
            }
4214
2.46k
        }
4215
1.72k
    }
4216
4217
    /* Add missing keyword arguments (copy default values from kwdefs) */
4218
17.1k
    if (co->co_kwonlyargcount > 0) {
4219
3.79k
        Py_ssize_t missing = 0;
4220
9.15k
        for (i = co->co_argcount; i < total_args; i++) {
4221
5.36k
            PyObject *name;
4222
5.36k
            if (GETLOCAL(i) != NULL)
4223
2.39k
                continue;
4224
2.96k
            name = PyTuple_GET_ITEM(co->co_varnames, i);
4225
2.96k
            if (kwdefs != NULL) {
4226
2.96k
                PyObject *def = PyDict_GetItemWithError(kwdefs, name);
4227
2.96k
                if (def) {
4228
2.96k
                    Py_INCREF(def);
4229
2.96k
                    SETLOCAL(i, def);
4230
2.96k
                    continue;
4231
2.96k
                }
4232
0
                else if (_PyErr_Occurred(tstate)) {
4233
0
                    goto fail;
4234
0
                }
4235
2.96k
            }
4236
0
            missing++;
4237
0
        }
4238
3.79k
        if (missing) {
4239
0
            missing_arguments(tstate, co, missing, -1, fastlocals);
4240
0
            goto fail;
4241
0
        }
4242
3.79k
    }
4243
4244
    /* Allocate and initialize storage for cell vars, and copy free
4245
       vars into frame. */
4246
17.6k
    for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
4247
463
        PyObject *c;
4248
463
        Py_ssize_t arg;
4249
        /* Possibly account for the cell variable being an argument. */
4250
463
        if (co->co_cell2arg != NULL &&
4251
303
            (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
4252
229
            c = PyCell_New(GETLOCAL(arg));
4253
            /* Clear the local copy. */
4254
229
            SETLOCAL(arg, NULL);
4255
229
        }
4256
234
        else {
4257
234
            c = PyCell_New(NULL);
4258
234
        }
4259
463
        if (c == NULL)
4260
0
            goto fail;
4261
463
        SETLOCAL(co->co_nlocals + i, c);
4262
463
    }
4263
4264
    /* Copy closure variables to free variables */
4265
18.5k
    for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4266
1.43k
        PyObject *o = PyTuple_GET_ITEM(closure, i);
4267
1.43k
        Py_INCREF(o);
4268
1.43k
        freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
4269
1.43k
    }
4270
4271
    /* Handle generator/coroutine/asynchronous generator */
4272
17.1k
    if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
4273
218
        PyObject *gen;
4274
218
        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
218
        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
218
        if (is_coro) {
4283
14
            gen = PyCoro_New(f, name, qualname);
4284
204
        } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4285
14
            gen = PyAsyncGen_New(f, name, qualname);
4286
190
        } else {
4287
190
            gen = PyGen_NewWithQualName(f, name, qualname);
4288
190
        }
4289
218
        if (gen == NULL) {
4290
0
            return NULL;
4291
0
        }
4292
4293
218
        _PyObject_GC_TRACK(f);
4294
4295
218
        return gen;
4296
218
    }
4297
4298
16.9k
    retval = PyEval_EvalFrameEx(f,0);
4299
4300
16.9k
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
16.9k
    assert(tstate != NULL);
4308
16.9k
    if (Py_REFCNT(f) > 1) {
4309
39
        Py_DECREF(f);
4310
39
        _PyObject_GC_TRACK(f);
4311
39
    }
4312
16.9k
    else {
4313
16.9k
        ++tstate->recursion_depth;
4314
16.9k
        Py_DECREF(f);
4315
16.9k
        --tstate->recursion_depth;
4316
16.9k
    }
4317
16.9k
    return retval;
4318
16.9k
}
4319
4320
PyObject *
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
3.99k
{
4327
3.99k
    return _PyEval_EvalCodeWithName(_co, globals, locals,
4328
3.99k
                                    args, argcount,
4329
3.99k
                                    kws, kws != NULL ? kws + 1 : NULL,
4330
3.99k
                                    kwcount, 2,
4331
3.99k
                                    defs, defcount,
4332
3.99k
                                    kwdefs, closure,
4333
3.99k
                                    NULL, NULL);
4334
3.99k
}
4335
4336
static PyObject *
4337
special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
4338
4.44k
{
4339
4.44k
    PyObject *res;
4340
4.44k
    res = _PyObject_LookupSpecial(o, id);
4341
4.44k
    if (res == NULL && !_PyErr_Occurred(tstate)) {
4342
0
        _PyErr_SetObject(tstate, PyExc_AttributeError, id->object);
4343
0
        return NULL;
4344
0
    }
4345
4.44k
    return res;
4346
4.44k
}
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
4352
do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
4353
158
{
4354
158
    PyObject *type = NULL, *value = NULL;
4355
4356
158
    if (exc == NULL) {
4357
        /* Reraise */
4358
0
        _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
4359
0
        PyObject *tb;
4360
0
        type = exc_info->exc_type;
4361
0
        value = exc_info->exc_value;
4362
0
        tb = exc_info->exc_traceback;
4363
0
        if (type == Py_None || type == NULL) {
4364
0
            _PyErr_SetString(tstate, PyExc_RuntimeError,
4365
0
                             "No active exception to reraise");
4366
0
            return 0;
4367
0
        }
4368
0
        Py_XINCREF(type);
4369
0
        Py_XINCREF(value);
4370
0
        Py_XINCREF(tb);
4371
0
        _PyErr_Restore(tstate, type, value, tb);
4372
0
        return 1;
4373
0
    }
4374
4375
    /* We support the following forms of raise:
4376
       raise
4377
       raise <instance>
4378
       raise <type> */
4379
4380
158
    if (PyExceptionClass_Check(exc)) {
4381
1
        type = exc;
4382
1
        value = _PyObject_CallNoArg(exc);
4383
1
        if (value == NULL)
4384
0
            goto raise_error;
4385
1
        if (!PyExceptionInstance_Check(value)) {
4386
0
            _PyErr_Format(tstate, PyExc_TypeError,
4387
0
                          "calling %R should have returned an instance of "
4388
0
                          "BaseException, not %R",
4389
0
                          type, Py_TYPE(value));
4390
0
             goto raise_error;
4391
0
        }
4392
1
    }
4393
157
    else if (PyExceptionInstance_Check(exc)) {
4394
157
        value = exc;
4395
157
        type = PyExceptionInstance_Class(exc);
4396
157
        Py_INCREF(type);
4397
157
    }
4398
0
    else {
4399
        /* Not something you can raise.  You get an exception
4400
           anyway, just not what you specified :-) */
4401
0
        Py_DECREF(exc);
4402
0
        _PyErr_SetString(tstate, PyExc_TypeError,
4403
0
                         "exceptions must derive from BaseException");
4404
0
        goto raise_error;
4405
0
    }
4406
4407
158
    assert(type != NULL);
4408
158
    assert(value != NULL);
4409
4410
158
    if (cause) {
4411
0
        PyObject *fixed_cause;
4412
0
        if (PyExceptionClass_Check(cause)) {
4413
0
            fixed_cause = _PyObject_CallNoArg(cause);
4414
0
            if (fixed_cause == NULL)
4415
0
                goto raise_error;
4416
0
            Py_DECREF(cause);
4417
0
        }
4418
0
        else if (PyExceptionInstance_Check(cause)) {
4419
0
            fixed_cause = cause;
4420
0
        }
4421
0
        else if (cause == Py_None) {
4422
0
            Py_DECREF(cause);
4423
0
            fixed_cause = NULL;
4424
0
        }
4425
0
        else {
4426
0
            _PyErr_SetString(tstate, PyExc_TypeError,
4427
0
                             "exception causes must derive from "
4428
0
                             "BaseException");
4429
0
            goto raise_error;
4430
0
        }
4431
0
        PyException_SetCause(value, fixed_cause);
4432
0
    }
4433
4434
158
    _PyErr_SetObject(tstate, type, value);
4435
    /* PyErr_SetObject incref's its arguments */
4436
158
    Py_DECREF(value);
4437
158
    Py_DECREF(type);
4438
158
    return 0;
4439
4440
0
raise_error:
4441
0
    Py_XDECREF(value);
4442
0
    Py_XDECREF(type);
4443
0
    Py_XDECREF(cause);
4444
0
    return 0;
4445
158
}
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
4455
unpack_iterable(PyThreadState *tstate, PyObject *v,
4456
                int argcnt, int argcntafter, PyObject **sp)
4457
0
{
4458
0
    int i = 0, j = 0;
4459
0
    Py_ssize_t ll = 0;
4460
0
    PyObject *it;  /* iter(v) */
4461
0
    PyObject *w;
4462
0
    PyObject *l = NULL; /* variable list */
4463
4464
0
    assert(v != NULL);
4465
4466
0
    it = PyObject_GetIter(v);
4467
0
    if (it == NULL) {
4468
0
        if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
4469
0
            v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4470
0
        {
4471
0
            _PyErr_Format(tstate, PyExc_TypeError,
4472
0
                          "cannot unpack non-iterable %.200s object",
4473
0
                          v->ob_type->tp_name);
4474
0
        }
4475
0
        return 0;
4476
0
    }
4477
4478
0
    for (; i < argcnt; i++) {
4479
0
        w = PyIter_Next(it);
4480
0
        if (w == NULL) {
4481
            /* Iterator done, via error or exhaustion. */
4482
0
            if (!_PyErr_Occurred(tstate)) {
4483
0
                if (argcntafter == -1) {
4484
0
                    _PyErr_Format(tstate, PyExc_ValueError,
4485
0
                                  "not enough values to unpack "
4486
0
                                  "(expected %d, got %d)",
4487
0
                                  argcnt, i);
4488
0
                }
4489
0
                else {
4490
0
                    _PyErr_Format(tstate, PyExc_ValueError,
4491
0
                                  "not enough values to unpack "
4492
0
                                  "(expected at least %d, got %d)",
4493
0
                                  argcnt + argcntafter, i);
4494
0
                }
4495
0
            }
4496
0
            goto Error;
4497
0
        }
4498
0
        *--sp = w;
4499
0
    }
4500
4501
0
    if (argcntafter == -1) {
4502
        /* We better have exhausted the iterator now. */
4503
0
        w = PyIter_Next(it);
4504
0
        if (w == NULL) {
4505
0
            if (_PyErr_Occurred(tstate))
4506
0
                goto Error;
4507
0
            Py_DECREF(it);
4508
0
            return 1;
4509
0
        }
4510
0
        Py_DECREF(w);
4511
0
        _PyErr_Format(tstate, PyExc_ValueError,
4512
0
                      "too many values to unpack (expected %d)",
4513
0
                      argcnt);
4514
0
        goto Error;
4515
0
    }
4516
4517
0
    l = PySequence_List(it);
4518
0
    if (l == NULL)
4519
0
        goto Error;
4520
0
    *--sp = l;
4521
0
    i++;
4522
4523
0
    ll = PyList_GET_SIZE(l);
4524
0
    if (ll < argcntafter) {
4525
0
        _PyErr_Format(tstate, PyExc_ValueError,
4526
0
            "not enough values to unpack (expected at least %d, got %zd)",
4527
0
            argcnt + argcntafter, argcnt + ll);
4528
0
        goto Error;
4529
0
    }
4530
4531
    /* Pop the "after-variable" args off the list. */
4532
0
    for (j = argcntafter; j > 0; j--, i++) {
4533
0
        *--sp = PyList_GET_ITEM(l, ll - j);
4534
0
    }
4535
    /* Resize the list. */
4536
0
    Py_SIZE(l) = ll - argcntafter;
4537
0
    Py_DECREF(it);
4538
0
    return 1;
4539
4540
0
Error:
4541
0
    for (; i > 0; i--, sp++)
4542
0
        Py_DECREF(*sp);
4543
0
    Py_XDECREF(it);
4544
0
    return 0;
4545
0
}
4546
4547
4548
#ifdef LLTRACE
4549
static int
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
4563
call_exc_trace(Py_tracefunc func, PyObject *self,
4564
               PyThreadState *tstate, PyFrameObject *f)
4565
0
{
4566
0
    PyObject *type, *value, *traceback, *orig_traceback, *arg;
4567
0
    int err;
4568
0
    _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
4569
0
    if (value == NULL) {
4570
0
        value = Py_None;
4571
0
        Py_INCREF(value);
4572
0
    }
4573
0
    _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
4574
0
    traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
4575
0
    arg = PyTuple_Pack(3, type, value, traceback);
4576
0
    if (arg == NULL) {
4577
0
        _PyErr_Restore(tstate, type, value, orig_traceback);
4578
0
        return;
4579
0
    }
4580
0
    err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
4581
0
    Py_DECREF(arg);
4582
0
    if (err == 0) {
4583
0
        _PyErr_Restore(tstate, type, value, orig_traceback);
4584
0
    }
4585
0
    else {
4586
0
        Py_XDECREF(type);
4587
0
        Py_XDECREF(value);
4588
0
        Py_XDECREF(orig_traceback);
4589
0
    }
4590
0
}
4591
4592
static int
4593
call_trace_protected(Py_tracefunc func, PyObject *obj,
4594
                     PyThreadState *tstate, PyFrameObject *frame,
4595
                     int what, PyObject *arg)
4596
0
{
4597
0
    PyObject *type, *value, *traceback;
4598
0
    int err;
4599
0
    _PyErr_Fetch(tstate, &type, &value, &traceback);
4600
0
    err = call_trace(func, obj, tstate, frame, what, arg);
4601
0
    if (err == 0)
4602
0
    {
4603
0
        _PyErr_Restore(tstate, type, value, traceback);
4604
0
        return 0;
4605
0
    }
4606
0
    else {
4607
0
        Py_XDECREF(type);
4608
0
        Py_XDECREF(value);
4609
0
        Py_XDECREF(traceback);
4610
0
        return -1;
4611
0
    }
4612
0
}
4613
4614
static int
4615
call_trace(Py_tracefunc func, PyObject *obj,
4616
           PyThreadState *tstate, PyFrameObject *frame,
4617
           int what, PyObject *arg)
4618
0
{
4619
0
    int result;
4620
0
    if (tstate->tracing)
4621
0
        return 0;
4622
0
    tstate->tracing++;
4623
0
    tstate->use_tracing = 0;
4624
0
    result = func(obj, frame, what, arg);
4625
0
    tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4626
0
                           || (tstate->c_profilefunc != NULL));
4627
0
    tstate->tracing--;
4628
0
    return result;
4629
0
}
4630
4631
PyObject *
4632
_PyEval_CallTracing(PyObject *func, PyObject *args)
4633
0
{
4634
0
    PyThreadState *tstate = _PyThreadState_GET();
4635
0
    int save_tracing = tstate->tracing;
4636
0
    int save_use_tracing = tstate->use_tracing;
4637
0
    PyObject *result;
4638
4639
0
    tstate->tracing = 0;
4640
0
    tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4641
0
                           || (tstate->c_profilefunc != NULL));
4642
0
    result = PyObject_Call(func, args, NULL);
4643
0
    tstate->tracing = save_tracing;
4644
0
    tstate->use_tracing = save_use_tracing;
4645
0
    return result;
4646
0
}
4647
4648
/* See Objects/lnotab_notes.txt for a description of how tracing works. */
4649
static int
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
0
{
4654
0
    int result = 0;
4655
0
    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
0
    if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4661
0
        PyAddrPair bounds;
4662
0
        line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4663
0
                                       &bounds);
4664
0
        *instr_lb = bounds.ap_lower;
4665
0
        *instr_ub = bounds.ap_upper;
4666
0
    }
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
0
    if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
4672
0
        frame->f_lineno = line;
4673
0
        if (frame->f_trace_lines) {
4674
0
            result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4675
0
        }
4676
0
    }
4677
    /* Always emit an opcode event if we're tracing all opcodes. */
4678
0
    if (frame->f_trace_opcodes) {
4679
0
        result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4680
0
    }
4681
0
    *instr_prev = frame->f_lasti;
4682
0
    return result;
4683
0
}
4684
4685
void
4686
PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
4687
0
{
4688
0
    if (PySys_Audit("sys.setprofile", NULL) < 0) {
4689
0
        _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
4690
0
        return;
4691
0
    }
4692
4693
0
    PyThreadState *tstate = _PyThreadState_GET();
4694
0
    PyObject *temp = tstate->c_profileobj;
4695
0
    Py_XINCREF(arg);
4696
0
    tstate->c_profilefunc = NULL;
4697
0
    tstate->c_profileobj = NULL;
4698
    /* Must make sure that tracing is not ignored if 'temp' is freed */
4699
0
    tstate->use_tracing = tstate->c_tracefunc != NULL;
4700
0
    Py_XDECREF(temp);
4701
0
    tstate->c_profilefunc = func;
4702
0
    tstate->c_profileobj = arg;
4703
    /* Flag that tracing or profiling is turned on */
4704
0
    tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
4705
0
}
4706
4707
void
4708
PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4709
0
{
4710
0
    if (PySys_Audit("sys.settrace", NULL) < 0) {
4711
0
        _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
4712
0
        return;
4713
0
    }
4714
4715
0
    _PyRuntimeState *runtime = &_PyRuntime;
4716
0
    PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
4717
0
    PyObject *temp = tstate->c_traceobj;
4718
0
    runtime->ceval.tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
4719
0
    Py_XINCREF(arg);
4720
0
    tstate->c_tracefunc = NULL;
4721
0
    tstate->c_traceobj = NULL;
4722
    /* Must make sure that profiling is not ignored if 'temp' is freed */
4723
0
    tstate->use_tracing = tstate->c_profilefunc != NULL;
4724
0
    Py_XDECREF(temp);
4725
0
    tstate->c_tracefunc = func;
4726
0
    tstate->c_traceobj = arg;
4727
    /* Flag that tracing or profiling is turned on */
4728
0
    tstate->use_tracing = ((func != NULL)
4729
0
                           || (tstate->c_profilefunc != NULL));
4730
0
}
4731
4732
void
4733
_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4734
0
{
4735
0
    assert(new_depth >= 0);
4736
0
    PyThreadState *tstate = _PyThreadState_GET();
4737
0
    tstate->coroutine_origin_tracking_depth = new_depth;
4738
0
}
4739
4740
int
4741
_PyEval_GetCoroutineOriginTrackingDepth(void)
4742
0
{
4743
0
    PyThreadState *tstate = _PyThreadState_GET();
4744
0
    return tstate->coroutine_origin_tracking_depth;
4745
0
}
4746
4747
PyObject *
4748
_PyEval_GetAsyncGenFirstiter(void)
4749
0
{
4750
0
    PyThreadState *tstate = _PyThreadState_GET();
4751
0
    return tstate->async_gen_firstiter;
4752
0
}
4753
4754
PyObject *
4755
_PyEval_GetAsyncGenFinalizer(void)
4756
0
{
4757
0
    PyThreadState *tstate = _PyThreadState_GET();
4758
0
    return tstate->async_gen_finalizer;
4759
0
}
4760
4761
static PyFrameObject *
4762
_PyEval_GetFrame(PyThreadState *tstate)
4763
937
{
4764
937
    return _PyRuntime.gilstate.getframe(tstate);
4765
937
}
4766
4767
PyFrameObject *
4768
PyEval_GetFrame(void)
4769
0
{
4770
0
    PyThreadState *tstate = _PyThreadState_GET();
4771
0
    return _PyEval_GetFrame(tstate);
4772
0
}
4773
4774
PyObject *
4775
PyEval_GetBuiltins(void)
4776
261
{
4777
261
    PyThreadState *tstate = _PyThreadState_GET();
4778
261
    PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
4779
261
    if (current_frame == NULL)
4780
13
        return tstate->interp->builtins;
4781
248
    else
4782
248
        return current_frame->f_builtins;
4783
261
}
4784
4785
/* Convenience function to get a builtin from its name */
4786
PyObject *
4787
_PyEval_GetBuiltinId(_Py_Identifier *name)
4788
0
{
4789
0
    PyThreadState *tstate = _PyThreadState_GET();
4790
0
    PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4791
0
    if (attr) {
4792
0
        Py_INCREF(attr);
4793
0
    }
4794
0
    else if (!_PyErr_Occurred(tstate)) {
4795
0
        _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
4796
0
    }
4797
0
    return attr;
4798
0
}
4799
4800
PyObject *
4801
PyEval_GetLocals(void)
4802
0
{
4803
0
    PyThreadState *tstate = _PyThreadState_GET();
4804
0
    PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
4805
0
    if (current_frame == NULL) {
4806
0
        _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
4807
0
        return NULL;
4808
0
    }
4809
4810
0
    if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
4811
0
        return NULL;
4812
0
    }
4813
4814
0
    assert(current_frame->f_locals != NULL);
4815
0
    return current_frame->f_locals;
4816
0
}
4817
4818
PyObject *
4819
PyEval_GetGlobals(void)
4820
674
{
4821
674
    PyThreadState *tstate = _PyThreadState_GET();
4822
674
    PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
4823
674
    if (current_frame == NULL) {
4824
117
        return NULL;
4825
117
    }
4826
4827
674
    assert(current_frame->f_globals != NULL);
4828
557
    return current_frame->f_globals;
4829
674
}
4830
4831
int
4832
PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
4833
2
{
4834
2
    PyThreadState *tstate = _PyThreadState_GET();
4835
2
    PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
4836
2
    int result = cf->cf_flags != 0;
4837
4838
2
    if (current_frame != NULL) {
4839
2
        const int codeflags = current_frame->f_code->co_flags;
4840
2
        const int compilerflags = codeflags & PyCF_MASK;
4841
2
        if (compilerflags) {
4842
0
            result = 1;
4843
0
            cf->cf_flags |= compilerflags;
4844
0
        }
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
2
    }
4852
2
    return result;
4853
2
}
4854
4855
4856
const char *
4857
PyEval_GetFuncName(PyObject *func)
4858
0
{
4859
0
    if (PyMethod_Check(func))
4860
0
        return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4861
0
    else if (PyFunction_Check(func))
4862
0
        return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
4863
0
    else if (PyCFunction_Check(func))
4864
0
        return ((PyCFunctionObject*)func)->m_ml->ml_name;
4865
0
    else
4866
0
        return func->ob_type->tp_name;
4867
0
}
4868
4869
const char *
4870
PyEval_GetFuncDesc(PyObject *func)
4871
0
{
4872
0
    if (PyMethod_Check(func))
4873
0
        return "()";
4874
0
    else if (PyFunction_Check(func))
4875
0
        return "()";
4876
0
    else if (PyCFunction_Check(func))
4877
0
        return "()";
4878
0
    else
4879
0
        return " object";
4880
0
}
4881
4882
1.03k
#define C_TRACE(x, call) \
4883
1.03k
if (tstate->use_tracing && tstate->c_profilefunc) { \
4884
0
    if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4885
0
        tstate, tstate->frame, \
4886
0
        PyTrace_C_CALL, func)) { \
4887
0
        x = NULL; \
4888
0
    } \
4889
0
    else { \
4890
0
        x = call; \
4891
0
        if (tstate->c_profilefunc != NULL) { \
4892
0
            if (x == NULL) { \
4893
0
                call_trace_protected(tstate->c_profilefunc, \
4894
0
                    tstate->c_profileobj, \
4895
0
                    tstate, tstate->frame, \
4896
0
                    PyTrace_C_EXCEPTION, func); \
4897
0
                /* XXX should pass (type, value, tb) */ \
4898
0
            } else { \
4899
0
                if (call_trace(tstate->c_profilefunc, \
4900
0
                    tstate->c_profileobj, \
4901
0
                    tstate, tstate->frame, \
4902
0
                    PyTrace_C_RETURN, func)) { \
4903
0
                    Py_DECREF(x); \
4904
0
                    x = NULL; \
4905
0
                } \
4906
0
            } \
4907
0
        } \
4908
0
    } \
4909
1.03k
} else { \
4910
1.03k
    x = call; \
4911
1.03k
    }
4912
4913
4914
static PyObject *
4915
trace_call_function(PyThreadState *tstate,
4916
                    PyObject *func,
4917
                    PyObject **args, Py_ssize_t nargs,
4918
                    PyObject *kwnames)
4919
0
{
4920
0
    PyObject *x;
4921
0
    if (PyCFunction_Check(func)) {
4922
0
        C_TRACE(x, _PyObject_Vectorcall(func, args, nargs, kwnames));
4923
0
        return x;
4924
0
    }
4925
0
    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
0
        PyObject *self = args[0];
4934
0
        func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4935
0
        if (func == NULL) {
4936
0
            return NULL;
4937
0
        }
4938
0
        C_TRACE(x, _PyObject_Vectorcall(func,
4939
0
                                        args+1, nargs-1,
4940
0
                                        kwnames));
4941
0
        Py_DECREF(func);
4942
0
        return x;
4943
0
    }
4944
0
    return _PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
4945
0
}
4946
4947
/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4948
   to reduce the stack consumption. */
4949
Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
4950
call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
4951
77.9k
{
4952
77.9k
    PyObject **pfunc = (*pp_stack) - oparg - 1;
4953
77.9k
    PyObject *func = *pfunc;
4954
77.9k
    PyObject *x, *w;
4955
77.9k
    Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4956
77.9k
    Py_ssize_t nargs = oparg - nkwargs;
4957
77.9k
    PyObject **stack = (*pp_stack) - nargs - nkwargs;
4958
4959
77.9k
    if (tstate->use_tracing) {
4960
0
        x = trace_call_function(tstate, func, stack, nargs, kwnames);
4961
0
    }
4962
77.9k
    else {
4963
77.9k
        x = _PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
4964
77.9k
    }
4965
4966
77.9k
    assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
4967
4968
    /* Clear the stack of the function object. */
4969
281k
    while ((*pp_stack) > pfunc) {
4970
203k
        w = EXT_POP(*pp_stack);
4971
203k
        Py_DECREF(w);
4972
203k
    }
4973
4974
77.9k
    return x;
4975
77.9k
}
4976
4977
static PyObject *
4978
do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
4979
1.45k
{
4980
1.45k
    PyObject *result;
4981
4982
1.45k
    if (PyCFunction_Check(func)) {
4983
1.03k
        C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4984
1.03k
        return result;
4985
1.03k
    }
4986
421
    else if (Py_TYPE(func) == &PyMethodDescr_Type) {
4987
0
        Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4988
0
        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
0
            PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4997
0
            func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4998
0
            if (func == NULL) {
4999
0
                return NULL;
5000
0
            }
5001
5002
0
            C_TRACE(result, _PyObject_FastCallDict(func,
5003
0
                                                   &_PyTuple_ITEMS(callargs)[1],
5004
0
                                                   nargs - 1,
5005
0
                                                   kwdict));
5006
0
            Py_DECREF(func);
5007
0
            return result;
5008
0
        }
5009
0
    }
5010
421
    return PyObject_Call(func, callargs, kwdict);
5011
1.45k
}
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
5020
_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
5021
3.67k
{
5022
3.67k
    PyThreadState *tstate = _PyThreadState_GET();
5023
3.67k
    if (v != Py_None) {
5024
3.67k
        Py_ssize_t x;
5025
3.67k
        if (PyIndex_Check(v)) {
5026
3.67k
            x = PyNumber_AsSsize_t(v, NULL);
5027
3.67k
            if (x == -1 && _PyErr_Occurred(tstate))
5028
0
                return 0;
5029
3.67k
        }
5030
0
        else {
5031
0
            _PyErr_SetString(tstate, PyExc_TypeError,
5032
0
                             "slice indices must be integers or "
5033
0
                             "None or have an __index__ method");
5034
0
            return 0;
5035
0
        }
5036
3.67k
        *pi = x;
5037
3.67k
    }
5038
3.67k
    return 1;
5039
3.67k
}
5040
5041
int
5042
_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
5043
0
{
5044
0
    PyThreadState *tstate = _PyThreadState_GET();
5045
0
    Py_ssize_t x;
5046
0
    if (PyIndex_Check(v)) {
5047
0
        x = PyNumber_AsSsize_t(v, NULL);
5048
0
        if (x == -1 && _PyErr_Occurred(tstate))
5049
0
            return 0;
5050
0
    }
5051
0
    else {
5052
0
        _PyErr_SetString(tstate, PyExc_TypeError,
5053
0
                         "slice indices must be integers or "
5054
0
                         "have an __index__ method");
5055
0
        return 0;
5056
0
    }
5057
0
    *pi = x;
5058
0
    return 1;
5059
0
}
5060
5061
5062
0
#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
5063
0
                         "BaseException is not allowed"
5064
5065
static PyObject *
5066
cmp_outcome(PyThreadState *tstate, int op, PyObject *v, PyObject *w)
5067
44.9k
{
5068
44.9k
    int res = 0;
5069
44.9k
    switch (op) {
5070
13.4k
    case PyCmp_IS:
5071
13.4k
        res = (v == w);
5072
13.4k
        break;
5073
5.22k
    case PyCmp_IS_NOT:
5074
5.22k
        res = (v != w);
5075
5.22k
        break;
5076
5.94k
    case PyCmp_IN:
5077
5.94k
        res = PySequence_Contains(w, v);
5078
5.94k
        if (res < 0)
5079
0
            return NULL;
5080
5.94k
        break;
5081
5.94k
    case PyCmp_NOT_IN:
5082
710
        res = PySequence_Contains(w, v);
5083
710
        if (res < 0)
5084
0
            return NULL;
5085
710
        res = !res;
5086
710
        break;
5087
3.66k
    case PyCmp_EXC_MATCH:
5088
3.66k
        if (PyTuple_Check(w)) {
5089
130
            Py_ssize_t i, length;
5090
130
            length = PyTuple_Size(w);
5091
390
            for (i = 0; i < length; i += 1) {
5092
260
                PyObject *exc = PyTuple_GET_ITEM(w, i);
5093
260
                if (!PyExceptionClass_Check(exc)) {
5094
0
                    _PyErr_SetString(tstate, PyExc_TypeError,
5095
0
                                     CANNOT_CATCH_MSG);
5096
0
                    return NULL;
5097
0
                }
5098
260
            }
5099
130
        }
5100
3.53k
        else {
5101
3.53k
            if (!PyExceptionClass_Check(w)) {
5102
0
                _PyErr_SetString(tstate, PyExc_TypeError,
5103
0
                                 CANNOT_CATCH_MSG);
5104
0
                return NULL;
5105
0
            }
5106
3.53k
        }
5107
3.66k
        res = PyErr_GivenExceptionMatches(v, w);
5108
3.66k
        break;
5109
15.9k
    default:
5110
15.9k
        return PyObject_RichCompare(v, w, op);
5111
44.9k
    }
5112
29.0k
    v = res ? Py_True : Py_False;
5113
29.0k
    Py_INCREF(v);
5114
29.0k
    return v;
5115
44.9k
}
5116
5117
static PyObject *
5118
import_name(PyThreadState *tstate, PyFrameObject *f,
5119
            PyObject *name, PyObject *fromlist, PyObject *level)
5120
832
{
5121
832
    _Py_IDENTIFIER(__import__);
5122
832
    PyObject *import_func, *res;
5123
832
    PyObject* stack[5];
5124
5125
832
    import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
5126
832
    if (import_func == NULL) {
5127
0
        if (!_PyErr_Occurred(tstate)) {
5128
0
            _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
5129
0
        }
5130
0
        return NULL;
5131
0
    }
5132
5133
    /* Fast path for not overloaded __import__. */
5134
832
    if (import_func == tstate->interp->import_func) {
5135
832
        int ilevel = _PyLong_AsInt(level);
5136
832
        if (ilevel == -1 && _PyErr_Occurred(tstate)) {
5137
0
            return NULL;
5138
0
        }
5139
832
        res = PyImport_ImportModuleLevelObject(
5140
832
                        name,
5141
832
                        f->f_globals,
5142
832
                        f->f_locals == NULL ? Py_None : f->f_locals,
5143
832
                        fromlist,
5144
832
                        ilevel);
5145
832
        return res;
5146
832
    }
5147
5148
0
    Py_INCREF(import_func);
5149
5150
0
    stack[0] = name;
5151
0
    stack[1] = f->f_globals;
5152
0
    stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5153
0
    stack[3] = fromlist;
5154
0
    stack[4] = level;
5155
0
    res = _PyObject_FastCall(import_func, stack, 5);
5156
0
    Py_DECREF(import_func);
5157
0
    return res;
5158
832
}
5159
5160
static PyObject *
5161
import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
5162
584
{
5163
584
    PyObject *x;
5164
584
    _Py_IDENTIFIER(__name__);
5165
584
    PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
5166
5167
584
    if (_PyObject_LookupAttr(v, name, &x) != 0) {
5168
571
        return x;
5169
571
    }
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
13
    pkgname = _PyObject_GetAttrId(v, &PyId___name__);
5174
13
    if (pkgname == NULL) {
5175
0
        goto error;
5176
0
    }
5177
13
    if (!PyUnicode_Check(pkgname)) {
5178
0
        Py_CLEAR(pkgname);
5179
0
        goto error;
5180
0
    }
5181
13
    fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
5182
13
    if (fullmodname == NULL) {
5183
0
        Py_DECREF(pkgname);
5184
0
        return NULL;
5185
0
    }
5186
13
    x = PyImport_GetModule(fullmodname);
5187
13
    Py_DECREF(fullmodname);
5188
13
    if (x == NULL && !_PyErr_Occurred(tstate)) {
5189
13
        goto error;
5190
13
    }
5191
0
    Py_DECREF(pkgname);
5192
0
    return x;
5193
13
 error:
5194
13
    pkgpath = PyModule_GetFilenameObject(v);
5195
13
    if (pkgname == NULL) {
5196
0
        pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5197
0
        if (pkgname_or_unknown == NULL) {
5198
0
            Py_XDECREF(pkgpath);
5199
0
            return NULL;
5200
0
        }
5201
13
    } else {
5202
13
        pkgname_or_unknown = pkgname;
5203
13
    }
5204
5205
13
    if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
5206
13
        _PyErr_Clear(tstate);
5207
13
        errmsg = PyUnicode_FromFormat(
5208
13
            "cannot import name %R from %R (unknown location)",
5209
13
            name, pkgname_or_unknown
5210
13
        );
5211
        /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
5212
13
        PyErr_SetImportError(errmsg, pkgname, NULL);
5213
13
    }
5214
0
    else {
5215
0
        _Py_IDENTIFIER(__spec__);
5216
0
        PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
5217
0
        const char *fmt =
5218
0
            _PyModuleSpec_IsInitializing(spec) ?
5219
0
            "cannot import name %R from partially initialized module %R "
5220
0
            "(most likely due to a circular import) (%S)" :
5221
0
            "cannot import name %R from %R (%S)";
5222
0
        Py_XDECREF(spec);
5223
5224
0
        errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
5225
        /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
5226
0
        PyErr_SetImportError(errmsg, pkgname, pkgpath);
5227
0
    }
5228
5229
13
    Py_XDECREF(errmsg);
5230
13
    Py_XDECREF(pkgname_or_unknown);
5231
13
    Py_XDECREF(pkgpath);
5232
13
    return NULL;
5233
13
}
5234
5235
static int
5236
import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
5237
57
{
5238
57
    _Py_IDENTIFIER(__all__);
5239
57
    _Py_IDENTIFIER(__dict__);
5240
57
    _Py_IDENTIFIER(__name__);
5241
57
    PyObject *all, *dict, *name, *value;
5242
57
    int skip_leading_underscores = 0;
5243
57
    int pos, err;
5244
5245
57
    if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5246
0
        return -1; /* Unexpected error */
5247
0
    }
5248
57
    if (all == NULL) {
5249
42
        if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5250
0
            return -1;
5251
0
        }
5252
42
        if (dict == NULL) {
5253
0
            _PyErr_SetString(tstate, PyExc_ImportError,
5254
0
                    "from-import-* object has no __dict__ and no __all__");
5255
0
            return -1;
5256
0
        }
5257
42
        all = PyMapping_Keys(dict);
5258
42
        Py_DECREF(dict);
5259
42
        if (all == NULL)
5260
0
            return -1;
5261
42
        skip_leading_underscores = 1;
5262
42
    }
5263
5264
5.95k
    for (pos = 0, err = 0; ; pos++) {
5265
5.95k
        name = PySequence_GetItem(all, pos);
5266
5.95k
        if (name == NULL) {
5267
57
            if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
5268
0
                err = -1;
5269
0
            }
5270
57
            else {
5271
57
                _PyErr_Clear(tstate);
5272
57
            }
5273
57
            break;
5274
57
        }
5275
5.89k
        if (!PyUnicode_Check(name)) {
5276
0
            PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5277
0
            if (modname == NULL) {
5278
0
                Py_DECREF(name);
5279
0
                err = -1;
5280
0
                break;
5281
0
            }
5282
0
            if (!PyUnicode_Check(modname)) {
5283
0
                _PyErr_Format(tstate, PyExc_TypeError,
5284
0
                              "module __name__ must be a string, not %.100s",
5285
0
                              Py_TYPE(modname)->tp_name);
5286
0
            }
5287
0
            else {
5288
0
                _PyErr_Format(tstate, PyExc_TypeError,
5289
0
                              "%s in %U.%s must be str, not %.100s",
5290
0
                              skip_leading_underscores ? "Key" : "Item",
5291
0
                              modname,
5292
0
                              skip_leading_underscores ? "__dict__" : "__all__",
5293
0
                              Py_TYPE(name)->tp_name);
5294
0
            }
5295
0
            Py_DECREF(modname);
5296
0
            Py_DECREF(name);
5297
0
            err = -1;
5298
0
            break;
5299
0
        }
5300
5.89k
        if (skip_leading_underscores) {
5301
5.66k
            if (PyUnicode_READY(name) == -1) {
5302
0
                Py_DECREF(name);
5303
0
                err = -1;
5304
0
                break;
5305
0
            }
5306
5.66k
            if (PyUnicode_READ_CHAR(name, 0) == '_') {
5307
260
                Py_DECREF(name);
5308
260
                continue;
5309
260
            }
5310
5.66k
        }
5311
5.63k
        value = PyObject_GetAttr(v, name);
5312
5.63k
        if (value == NULL)
5313
0
            err = -1;
5314
5.63k
        else if (PyDict_CheckExact(locals))
5315
5.63k
            err = PyDict_SetItem(locals, name, value);
5316
0
        else
5317
0
            err = PyObject_SetItem(locals, name, value);
5318
5.63k
        Py_DECREF(name);
5319
5.63k
        Py_XDECREF(value);
5320
5.63k
        if (err != 0)
5321
0
            break;
5322
5.63k
    }
5323
57
    Py_DECREF(all);
5324
57
    return err;
5325
57
}
5326
5327
static int
5328
check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
5329
39
{
5330
39
    if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
5331
0
        _PyErr_Format(tstate, PyExc_TypeError,
5332
0
                      "%.200s%.200s argument after * "
5333
0
                      "must be an iterable, not %.200s",
5334
0
                      PyEval_GetFuncName(func),
5335
0
                      PyEval_GetFuncDesc(func),
5336
0
                      args->ob_type->tp_name);
5337
0
        return -1;
5338
0
    }
5339
39
    return 0;
5340
39
}
5341
5342
static void
5343
format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
5344
0
{
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
0
    if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
5352
0
        _PyErr_Format(tstate, PyExc_TypeError,
5353
0
                      "%.200s%.200s argument after ** "
5354
0
                      "must be a mapping, not %.200s",
5355
0
                      PyEval_GetFuncName(func),
5356
0
                      PyEval_GetFuncDesc(func),
5357
0
                      kwargs->ob_type->tp_name);
5358
0
    }
5359
0
    else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
5360
0
        PyObject *exc, *val, *tb;
5361
0
        _PyErr_Fetch(tstate, &exc, &val, &tb);
5362
0
        if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
5363
0
            PyObject *key = PyTuple_GET_ITEM(val, 0);
5364
0
            if (!PyUnicode_Check(key)) {
5365
0
                _PyErr_Format(tstate, PyExc_TypeError,
5366
0
                              "%.200s%.200s keywords must be strings",
5367
0
                              PyEval_GetFuncName(func),
5368
0
                              PyEval_GetFuncDesc(func));
5369
0
            }
5370
0
            else {
5371
0
                _PyErr_Format(tstate, PyExc_TypeError,
5372
0
                              "%.200s%.200s got multiple "
5373
0
                              "values for keyword argument '%U'",
5374
0
                              PyEval_GetFuncName(func),
5375
0
                              PyEval_GetFuncDesc(func),
5376
0
                              key);
5377
0
            }
5378
0
            Py_XDECREF(exc);
5379
0
            Py_XDECREF(val);
5380
0
            Py_XDECREF(tb);
5381
0
        }
5382
0
        else {
5383
0
            _PyErr_Restore(tstate, exc, val, tb);
5384
0
        }
5385
0
    }
5386
0
}
5387
5388
static void
5389
format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5390
                     const char *format_str, PyObject *obj)
5391
225
{
5392
225
    const char *obj_str;
5393
5394
225
    if (!obj)
5395
0
        return;
5396
5397
225
    obj_str = PyUnicode_AsUTF8(obj);
5398
225
    if (!obj_str)
5399
0
        return;
5400
5401
225
    _PyErr_Format(tstate, exc, format_str, obj_str);
5402
225
}
5403
5404
static void
5405
format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
5406
0
{
5407
0
    PyObject *name;
5408
    /* Don't stomp existing exception */
5409
0
    if (_PyErr_Occurred(tstate))
5410
0
        return;
5411
0
    if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5412
0
        name = PyTuple_GET_ITEM(co->co_cellvars,
5413
0
                                oparg);
5414
0
        format_exc_check_arg(tstate,
5415
0
            PyExc_UnboundLocalError,
5416
0
            UNBOUNDLOCAL_ERROR_MSG,
5417
0
            name);
5418
0
    } else {
5419
0
        name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5420
0
                                PyTuple_GET_SIZE(co->co_cellvars));
5421
0
        format_exc_check_arg(tstate, PyExc_NameError,
5422
0
                             UNBOUNDFREE_ERROR_MSG, name);
5423
0
    }
5424
0
}
5425
5426
static void
5427
format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevopcode)
5428
0
{
5429
0
    if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5430
0
        if (prevopcode == BEFORE_ASYNC_WITH) {
5431
0
            _PyErr_Format(tstate, PyExc_TypeError,
5432
0
                          "'async with' received an object from __aenter__ "
5433
0
                          "that does not implement __await__: %.100s",
5434
0
                          type->tp_name);
5435
0
        }
5436
0
        else if (prevopcode == WITH_CLEANUP_START) {
5437
0
            _PyErr_Format(tstate, PyExc_TypeError,
5438
0
                          "'async with' received an object from __aexit__ "
5439
0
                          "that does not implement __await__: %.100s",
5440
0
                          type->tp_name);
5441
0
        }
5442
0
    }
5443
0
}
5444
5445
static PyObject *
5446
unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
5447
                    PyFrameObject *f, const _Py_CODEUNIT *next_instr)
5448
3.37k
{
5449
3.37k
    PyObject *res;
5450
3.37k
    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
576
        int opcode, oparg;
5458
576
        NEXTOPARG();
5459
576
        switch (opcode) {
5460
511
        case STORE_FAST:
5461
511
        {
5462
511
            PyObject **fastlocals = f->f_localsplus;
5463
511
            if (GETLOCAL(oparg) == v)
5464
71
                SETLOCAL(oparg, NULL);
5465
511
            break;
5466
0
        }
5467
0
        case STORE_DEREF:
5468
0
        {
5469
0
            PyObject **freevars = (f->f_localsplus +
5470
0
                                   f->f_code->co_nlocals);
5471
0
            PyObject *c = freevars[oparg];
5472
0
            if (PyCell_GET(c) ==  v) {
5473
0
                PyCell_SET(c, NULL);
5474
0
                Py_DECREF(v);
5475
0
            }
5476
0
            break;
5477
0
        }
5478
2
        case STORE_NAME:
5479
2
        {
5480
2
            PyObject *names = f->f_code->co_names;
5481
2
            PyObject *name = GETITEM(names, oparg);
5482
2
            PyObject *locals = f->f_locals;
5483
2
            if (locals && PyDict_CheckExact(locals)) {
5484
2
                PyObject *w = PyDict_GetItemWithError(locals, name);
5485
2
                if ((w == v && PyDict_DelItem(locals, name) != 0) ||
5486
2
                    (w == NULL && _PyErr_Occurred(tstate)))
5487
0
                {
5488
0
                    Py_DECREF(v);
5489
0
                    return NULL;
5490
0
                }
5491
2
            }
5492
2
            break;
5493
2
        }
5494
576
        }
5495
576
    }
5496
3.37k
    res = v;
5497
3.37k
    PyUnicode_Append(&res, w);
5498
3.37k
    return res;
5499
3.37k
}
5500
5501
#ifdef DYNAMIC_EXECUTION_PROFILE
5502
5503
static PyObject *
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 *
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
5546
_PyEval_RequestCodeExtraIndex(freefunc free)
5547
0
{
5548
0
    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
5549
0
    Py_ssize_t new_index;
5550
5551
0
    if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
5552
0
        return -1;
5553
0
    }
5554
0
    new_index = interp->co_extra_user_count++;
5555
0
    interp->co_extra_freefuncs[new_index] = free;
5556
0
    return new_index;
5557
0
}
5558
5559
static void
5560
dtrace_function_entry(PyFrameObject *f)
5561
0
{
5562
0
    const char *filename;
5563
0
    const char *funcname;
5564
0
    int lineno;
5565
5566
0
    filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5567
0
    funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5568
0
    lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5569
5570
0
    PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5571
0
}
5572
5573
static void
5574
dtrace_function_return(PyFrameObject *f)
5575
0
{
5576
0
    const char *filename;
5577
0
    const char *funcname;
5578
0
    int lineno;
5579
5580
0
    filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5581
0
    funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5582
0
    lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5583
5584
0
    PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5585
0
}
5586
5587
/* DTrace equivalent of maybe_call_line_trace. */
5588
static void
5589
maybe_dtrace_line(PyFrameObject *frame,
5590
                  int *instr_lb, int *instr_ub, int *instr_prev)
5591
0
{
5592
0
    int line = frame->f_lineno;
5593
0
    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
0
    if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5599
0
        PyAddrPair bounds;
5600
0
        line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5601
0
                                       &bounds);
5602
0
        *instr_lb = bounds.ap_lower;
5603
0
        *instr_ub = bounds.ap_upper;
5604
0
    }
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
0
    if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5609
0
        frame->f_lineno = line;
5610
0
        co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5611
0
        if (!co_filename)
5612
0
            co_filename = "?";
5613
0
        co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5614
0
        if (!co_name)
5615
0
            co_name = "?";
5616
0
        PyDTrace_LINE(co_filename, co_name, line);
5617
0
    }
5618
0
    *instr_prev = frame->f_lasti;
5619
0
}