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 #include "pycore_interp.h"   /* PyInterpreterState.eval_frame */
16 
17 extern void _Py_FinishPendingCalls(PyThreadState *tstate);
18 extern void _PyEval_InitRuntimeState(struct _ceval_runtime_state *);
19 extern int _PyEval_InitState(struct _ceval_state *ceval);
20 extern void _PyEval_FiniState(struct _ceval_state *ceval);
21 PyAPI_FUNC(void) _PyEval_SignalReceived(PyInterpreterState *interp);
22 PyAPI_FUNC(int) _PyEval_AddPendingCall(
23     PyInterpreterState *interp,
24     int (*func)(void *),
25     void *arg);
26 PyAPI_FUNC(void) _PyEval_SignalAsyncExc(PyThreadState *tstate);
27 #ifdef HAVE_FORK
28 extern void _PyEval_ReInitThreads(struct pyruntimestate *runtime);
29 #endif
30 PyAPI_FUNC(void) _PyEval_SetCoroutineOriginTrackingDepth(
31     PyThreadState *tstate,
32     int new_depth);
33 
34 /* Private function */
35 void _PyEval_Fini(void);
36 
37 static inline PyObject*
_PyEval_EvalFrame(PyThreadState * tstate,PyFrameObject * f,int throwflag)38 _PyEval_EvalFrame(PyThreadState *tstate, PyFrameObject *f, int throwflag)
39 {
40     return tstate->interp->eval_frame(tstate, f, throwflag);
41 }
42 
43 extern PyObject *_PyEval_EvalCode(
44     PyThreadState *tstate,
45     PyObject *_co, PyObject *globals, PyObject *locals,
46     PyObject *const *args, Py_ssize_t argcount,
47     PyObject *const *kwnames, PyObject *const *kwargs,
48     Py_ssize_t kwcount, int kwstep,
49     PyObject *const *defs, Py_ssize_t defcount,
50     PyObject *kwdefs, PyObject *closure,
51     PyObject *name, PyObject *qualname);
52 
53 extern int _PyEval_ThreadsInitialized(struct pyruntimestate *runtime);
54 extern PyStatus _PyEval_InitGIL(PyThreadState *tstate);
55 extern void _PyEval_FiniGIL(PyThreadState *tstate);
56 
57 extern void _PyEval_ReleaseLock(PyThreadState *tstate);
58 
59 
60 /* --- _Py_EnterRecursiveCall() ----------------------------------------- */
61 
62 PyAPI_DATA(int) _Py_CheckRecursionLimit;
63 
64 #ifdef USE_STACKCHECK
65 /* With USE_STACKCHECK macro defined, trigger stack checks in
66    _Py_CheckRecursiveCall() on every 64th call to Py_EnterRecursiveCall. */
_Py_MakeRecCheck(PyThreadState * tstate)67 static inline int _Py_MakeRecCheck(PyThreadState *tstate)  {
68     return (++tstate->recursion_depth > tstate->interp->ceval.recursion_limit
69             || ++tstate->stackcheck_counter > 64);
70 }
71 #else
_Py_MakeRecCheck(PyThreadState * tstate)72 static inline int _Py_MakeRecCheck(PyThreadState *tstate) {
73     return (++tstate->recursion_depth > tstate->interp->ceval.recursion_limit);
74 }
75 #endif
76 
77 PyAPI_FUNC(int) _Py_CheckRecursiveCall(
78     PyThreadState *tstate,
79     const char *where);
80 
_Py_EnterRecursiveCall(PyThreadState * tstate,const char * where)81 static inline int _Py_EnterRecursiveCall(PyThreadState *tstate,
82                                          const char *where) {
83     return (_Py_MakeRecCheck(tstate) && _Py_CheckRecursiveCall(tstate, where));
84 }
85 
_Py_EnterRecursiveCall_inline(const char * where)86 static inline int _Py_EnterRecursiveCall_inline(const char *where) {
87     PyThreadState *tstate = PyThreadState_GET();
88     return _Py_EnterRecursiveCall(tstate, where);
89 }
90 
91 #define Py_EnterRecursiveCall(where) _Py_EnterRecursiveCall_inline(where)
92 
93 /* Compute the "lower-water mark" for a recursion limit. When
94  * Py_LeaveRecursiveCall() is called with a recursion depth below this mark,
95  * the overflowed flag is reset to 0. */
_Py_RecursionLimitLowerWaterMark(int limit)96 static inline int _Py_RecursionLimitLowerWaterMark(int limit) {
97     if (limit > 200) {
98         return (limit - 50);
99     }
100     else {
101         return (3 * (limit >> 2));
102     }
103 }
104 
_Py_LeaveRecursiveCall(PyThreadState * tstate)105 static inline void _Py_LeaveRecursiveCall(PyThreadState *tstate)  {
106     tstate->recursion_depth--;
107     int limit = tstate->interp->ceval.recursion_limit;
108     if (tstate->recursion_depth < _Py_RecursionLimitLowerWaterMark(limit)) {
109         tstate->overflowed = 0;
110     }
111 }
112 
_Py_LeaveRecursiveCall_inline(void)113 static inline void _Py_LeaveRecursiveCall_inline(void)  {
114     PyThreadState *tstate = PyThreadState_GET();
115     _Py_LeaveRecursiveCall(tstate);
116 }
117 
118 #define Py_LeaveRecursiveCall() _Py_LeaveRecursiveCall_inline()
119 
120 
121 #ifdef __cplusplus
122 }
123 #endif
124 #endif /* !Py_INTERNAL_CEVAL_H */
125