1#ifndef Py_INTERNAL_CEVAL_H
2#define Py_INTERNAL_CEVAL_H
3#ifdef __cplusplus
4extern "C" {
5#endif
6
7#ifndef Py_BUILD_CORE
8#  error "this header requires Py_BUILD_CORE define"
9#endif
10
11/* Forward declarations */
12struct pyruntimestate;
13struct _ceval_runtime_state;
14
15/* WASI has limited call stack. Python's recursion limit depends on code
16   layout, optimization, and WASI runtime. Wasmtime can handle about 700-750
17   recursions, sometimes less. 600 is a more conservative limit. */
18#ifndef Py_DEFAULT_RECURSION_LIMIT
19#  ifdef __wasi__
20#    define Py_DEFAULT_RECURSION_LIMIT 600
21#  else
22#    define Py_DEFAULT_RECURSION_LIMIT 1000
23#  endif
24#endif
25
26#include "pycore_interp.h"        // PyInterpreterState.eval_frame
27#include "pycore_pystate.h"       // _PyThreadState_GET()
28
29
30extern void _Py_FinishPendingCalls(PyThreadState *tstate);
31extern void _PyEval_InitRuntimeState(struct _ceval_runtime_state *);
32extern void _PyEval_InitState(struct _ceval_state *, PyThread_type_lock);
33extern void _PyEval_FiniState(struct _ceval_state *ceval);
34PyAPI_FUNC(void) _PyEval_SignalReceived(PyInterpreterState *interp);
35PyAPI_FUNC(int) _PyEval_AddPendingCall(
36    PyInterpreterState *interp,
37    int (*func)(void *),
38    void *arg);
39PyAPI_FUNC(void) _PyEval_SignalAsyncExc(PyInterpreterState *interp);
40#ifdef HAVE_FORK
41extern PyStatus _PyEval_ReInitThreads(PyThreadState *tstate);
42#endif
43
44// Used by sys.call_tracing()
45extern PyObject* _PyEval_CallTracing(PyObject *func, PyObject *args);
46
47// Used by sys.get_asyncgen_hooks()
48extern PyObject* _PyEval_GetAsyncGenFirstiter(void);
49extern PyObject* _PyEval_GetAsyncGenFinalizer(void);
50
51// Used by sys.set_asyncgen_hooks()
52extern int _PyEval_SetAsyncGenFirstiter(PyObject *);
53extern int _PyEval_SetAsyncGenFinalizer(PyObject *);
54
55// Used by sys.get_coroutine_origin_tracking_depth()
56// and sys.set_coroutine_origin_tracking_depth()
57extern int _PyEval_GetCoroutineOriginTrackingDepth(void);
58extern int _PyEval_SetCoroutineOriginTrackingDepth(int depth);
59
60extern void _PyEval_Fini(void);
61
62
63extern PyObject* _PyEval_GetBuiltins(PyThreadState *tstate);
64extern PyObject* _PyEval_BuiltinsFromGlobals(
65    PyThreadState *tstate,
66    PyObject *globals);
67
68
69static inline PyObject*
70_PyEval_EvalFrame(PyThreadState *tstate, struct _PyInterpreterFrame *frame, int throwflag)
71{
72    if (tstate->interp->eval_frame == NULL) {
73        return _PyEval_EvalFrameDefault(tstate, frame, throwflag);
74    }
75    return tstate->interp->eval_frame(tstate, frame, throwflag);
76}
77
78extern PyObject*
79_PyEval_Vector(PyThreadState *tstate,
80            PyFunctionObject *func, PyObject *locals,
81            PyObject* const* args, size_t argcount,
82            PyObject *kwnames);
83
84extern int _PyEval_ThreadsInitialized(struct pyruntimestate *runtime);
85extern PyStatus _PyEval_InitGIL(PyThreadState *tstate);
86extern void _PyEval_FiniGIL(PyInterpreterState *interp);
87
88extern void _PyEval_ReleaseLock(PyThreadState *tstate);
89
90extern void _PyEval_DeactivateOpCache(void);
91
92
93/* --- _Py_EnterRecursiveCall() ----------------------------------------- */
94
95#ifdef USE_STACKCHECK
96/* With USE_STACKCHECK macro defined, trigger stack checks in
97   _Py_CheckRecursiveCall() on every 64th call to _Py_EnterRecursiveCall. */
98static inline int _Py_MakeRecCheck(PyThreadState *tstate)  {
99    return (tstate->recursion_remaining-- <= 0
100            || (tstate->recursion_remaining & 63) == 0);
101}
102#else
103static inline int _Py_MakeRecCheck(PyThreadState *tstate) {
104    return tstate->recursion_remaining-- <= 0;
105}
106#endif
107
108PyAPI_FUNC(int) _Py_CheckRecursiveCall(
109    PyThreadState *tstate,
110    const char *where);
111
112static inline int _Py_EnterRecursiveCallTstate(PyThreadState *tstate,
113                                               const char *where) {
114    return (_Py_MakeRecCheck(tstate) && _Py_CheckRecursiveCall(tstate, where));
115}
116
117static inline int _Py_EnterRecursiveCall(const char *where) {
118    PyThreadState *tstate = _PyThreadState_GET();
119    return _Py_EnterRecursiveCallTstate(tstate, where);
120}
121
122static inline void _Py_LeaveRecursiveCallTstate(PyThreadState *tstate)  {
123    tstate->recursion_remaining++;
124}
125
126static inline void _Py_LeaveRecursiveCall(void)  {
127    PyThreadState *tstate = _PyThreadState_GET();
128    _Py_LeaveRecursiveCallTstate(tstate);
129}
130
131extern struct _PyInterpreterFrame* _PyEval_GetFrame(void);
132
133extern PyObject* _Py_MakeCoro(PyFunctionObject *func);
134
135#ifdef __cplusplus
136}
137#endif
138#endif /* !Py_INTERNAL_CEVAL_H */
139