1 #ifndef Py_INTERNAL_CEVAL_H
2 #define Py_INTERNAL_CEVAL_H
3 #ifdef __cplusplus
4 extern "C" {
5 #endif
6
7 #ifndef Py_BUILD_CORE
8 # error "this header requires Py_BUILD_CORE define"
9 #endif
10
11 /* Forward declarations */
12 struct pyruntimestate;
13 struct _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
30 extern void _Py_FinishPendingCalls(PyThreadState *tstate);
31 extern void _PyEval_InitRuntimeState(struct _ceval_runtime_state *);
32 extern void _PyEval_InitState(struct _ceval_state *, PyThread_type_lock);
33 extern void _PyEval_FiniState(struct _ceval_state *ceval);
34 PyAPI_FUNC(void) _PyEval_SignalReceived(PyInterpreterState *interp);
35 PyAPI_FUNC(int) _PyEval_AddPendingCall(
36 PyInterpreterState *interp,
37 int (*func)(void *),
38 void *arg);
39 PyAPI_FUNC(void) _PyEval_SignalAsyncExc(PyInterpreterState *interp);
40 #ifdef HAVE_FORK
41 extern PyStatus _PyEval_ReInitThreads(PyThreadState *tstate);
42 #endif
43
44 // Used by sys.call_tracing()
45 extern PyObject* _PyEval_CallTracing(PyObject *func, PyObject *args);
46
47 // Used by sys.get_asyncgen_hooks()
48 extern PyObject* _PyEval_GetAsyncGenFirstiter(void);
49 extern PyObject* _PyEval_GetAsyncGenFinalizer(void);
50
51 // Used by sys.set_asyncgen_hooks()
52 extern int _PyEval_SetAsyncGenFirstiter(PyObject *);
53 extern int _PyEval_SetAsyncGenFinalizer(PyObject *);
54
55 // Used by sys.get_coroutine_origin_tracking_depth()
56 // and sys.set_coroutine_origin_tracking_depth()
57 extern int _PyEval_GetCoroutineOriginTrackingDepth(void);
58 extern int _PyEval_SetCoroutineOriginTrackingDepth(int depth);
59
60 extern void _PyEval_Fini(void);
61
62
63 extern PyObject* _PyEval_GetBuiltins(PyThreadState *tstate);
64 extern PyObject* _PyEval_BuiltinsFromGlobals(
65 PyThreadState *tstate,
66 PyObject *globals);
67
68
69 static inline PyObject*
_PyEval_EvalFrame(PyThreadState * tstate,struct _PyInterpreterFrame * frame,int throwflag)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
78 extern PyObject*
79 _PyEval_Vector(PyThreadState *tstate,
80 PyFunctionObject *func, PyObject *locals,
81 PyObject* const* args, size_t argcount,
82 PyObject *kwnames);
83
84 extern int _PyEval_ThreadsInitialized(struct pyruntimestate *runtime);
85 extern PyStatus _PyEval_InitGIL(PyThreadState *tstate);
86 extern void _PyEval_FiniGIL(PyInterpreterState *interp);
87
88 extern void _PyEval_ReleaseLock(PyThreadState *tstate);
89
90 extern 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. */
_Py_MakeRecCheck(PyThreadState * tstate)98 static inline int _Py_MakeRecCheck(PyThreadState *tstate) {
99 return (tstate->recursion_remaining-- <= 0
100 || (tstate->recursion_remaining & 63) == 0);
101 }
102 #else
_Py_MakeRecCheck(PyThreadState * tstate)103 static inline int _Py_MakeRecCheck(PyThreadState *tstate) {
104 return tstate->recursion_remaining-- <= 0;
105 }
106 #endif
107
108 PyAPI_FUNC(int) _Py_CheckRecursiveCall(
109 PyThreadState *tstate,
110 const char *where);
111
_Py_EnterRecursiveCallTstate(PyThreadState * tstate,const char * where)112 static inline int _Py_EnterRecursiveCallTstate(PyThreadState *tstate,
113 const char *where) {
114 return (_Py_MakeRecCheck(tstate) && _Py_CheckRecursiveCall(tstate, where));
115 }
116
_Py_EnterRecursiveCall(const char * where)117 static inline int _Py_EnterRecursiveCall(const char *where) {
118 PyThreadState *tstate = _PyThreadState_GET();
119 return _Py_EnterRecursiveCallTstate(tstate, where);
120 }
121
_Py_LeaveRecursiveCallTstate(PyThreadState * tstate)122 static inline void _Py_LeaveRecursiveCallTstate(PyThreadState *tstate) {
123 tstate->recursion_remaining++;
124 }
125
_Py_LeaveRecursiveCall(void)126 static inline void _Py_LeaveRecursiveCall(void) {
127 PyThreadState *tstate = _PyThreadState_GET();
128 _Py_LeaveRecursiveCallTstate(tstate);
129 }
130
131 extern struct _PyInterpreterFrame* _PyEval_GetFrame(void);
132
133 extern PyObject* _Py_MakeCoro(PyFunctionObject *func);
134
135 #ifdef __cplusplus
136 }
137 #endif
138 #endif /* !Py_INTERNAL_CEVAL_H */
139