ยปCore Development>Code coverage>Python/ceval.c

Python code coverage for Python/ceval.c

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