File indexing completed on 2025-11-19 09:50:45
0001 #ifndef Py_INTERNAL_CEVAL_H
0002 #define Py_INTERNAL_CEVAL_H
0003 #ifdef __cplusplus
0004 extern "C" {
0005 #endif
0006
0007 #ifndef Py_BUILD_CORE
0008 # error "this header requires Py_BUILD_CORE define"
0009 #endif
0010
0011 #include "dynamic_annotations.h" // _Py_ANNOTATE_RWLOCK_CREATE
0012
0013 #include "pycore_interp.h" // PyInterpreterState.eval_frame
0014 #include "pycore_pystate.h" // _PyThreadState_GET()
0015
0016
0017 struct pyruntimestate;
0018 struct _ceval_runtime_state;
0019
0020
0021 PyAPI_FUNC(int) _PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg);
0022
0023 extern int _PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg);
0024
0025 extern int _PyEval_SetOpcodeTrace(PyFrameObject *f, bool enable);
0026
0027
0028
0029 PyAPI_FUNC(PyObject*) _PyEval_GetBuiltin(PyObject *);
0030
0031 extern PyObject* _PyEval_GetBuiltinId(_Py_Identifier *);
0032
0033 extern void _PyEval_SetSwitchInterval(unsigned long microseconds);
0034 extern unsigned long _PyEval_GetSwitchInterval(void);
0035
0036
0037 PyAPI_FUNC(int) _PyEval_MakePendingCalls(PyThreadState *);
0038
0039 #ifndef Py_DEFAULT_RECURSION_LIMIT
0040 # define Py_DEFAULT_RECURSION_LIMIT 1000
0041 #endif
0042
0043 extern void _Py_FinishPendingCalls(PyThreadState *tstate);
0044 extern void _PyEval_InitState(PyInterpreterState *);
0045 extern void _PyEval_SignalReceived(void);
0046
0047
0048 #define _Py_PENDING_MAINTHREADONLY 1
0049 #define _Py_PENDING_RAWFREE 2
0050
0051 typedef int _Py_add_pending_call_result;
0052 #define _Py_ADD_PENDING_SUCCESS 0
0053 #define _Py_ADD_PENDING_FULL -1
0054
0055
0056 PyAPI_FUNC(_Py_add_pending_call_result) _PyEval_AddPendingCall(
0057 PyInterpreterState *interp,
0058 _Py_pending_call_func func,
0059 void *arg,
0060 int flags);
0061
0062 #ifdef HAVE_FORK
0063 extern PyStatus _PyEval_ReInitThreads(PyThreadState *tstate);
0064 #endif
0065
0066
0067 extern PyObject* _PyEval_CallTracing(PyObject *func, PyObject *args);
0068
0069
0070 extern PyObject* _PyEval_GetAsyncGenFirstiter(void);
0071 extern PyObject* _PyEval_GetAsyncGenFinalizer(void);
0072
0073
0074 extern int _PyEval_SetAsyncGenFirstiter(PyObject *);
0075 extern int _PyEval_SetAsyncGenFinalizer(PyObject *);
0076
0077
0078
0079 extern int _PyEval_GetCoroutineOriginTrackingDepth(void);
0080 extern int _PyEval_SetCoroutineOriginTrackingDepth(int depth);
0081
0082 extern void _PyEval_Fini(void);
0083
0084
0085 extern PyObject* _PyEval_GetBuiltins(PyThreadState *tstate);
0086 extern PyObject* _PyEval_BuiltinsFromGlobals(
0087 PyThreadState *tstate,
0088 PyObject *globals);
0089
0090
0091
0092 typedef struct {
0093
0094 void* (*init_state)(void);
0095
0096 void (*write_state)(void* state, const void *code_addr,
0097 unsigned int code_size, PyCodeObject* code);
0098
0099 int (*free_state)(void* state);
0100 } _PyPerf_Callbacks;
0101
0102 extern int _PyPerfTrampoline_SetCallbacks(_PyPerf_Callbacks *);
0103 extern void _PyPerfTrampoline_GetCallbacks(_PyPerf_Callbacks *);
0104 extern int _PyPerfTrampoline_Init(int activate);
0105 extern int _PyPerfTrampoline_Fini(void);
0106 extern void _PyPerfTrampoline_FreeArenas(void);
0107 extern int _PyIsPerfTrampolineActive(void);
0108 extern PyStatus _PyPerfTrampoline_AfterFork_Child(void);
0109 #ifdef PY_HAVE_PERF_TRAMPOLINE
0110 extern _PyPerf_Callbacks _Py_perfmap_callbacks;
0111 extern _PyPerf_Callbacks _Py_perfmap_jit_callbacks;
0112 #endif
0113
0114 static inline PyObject*
0115 _PyEval_EvalFrame(PyThreadState *tstate, struct _PyInterpreterFrame *frame, int throwflag)
0116 {
0117 EVAL_CALL_STAT_INC(EVAL_CALL_TOTAL);
0118 if (tstate->interp->eval_frame == NULL) {
0119 return _PyEval_EvalFrameDefault(tstate, frame, throwflag);
0120 }
0121 return tstate->interp->eval_frame(tstate, frame, throwflag);
0122 }
0123
0124 extern PyObject*
0125 _PyEval_Vector(PyThreadState *tstate,
0126 PyFunctionObject *func, PyObject *locals,
0127 PyObject* const* args, size_t argcount,
0128 PyObject *kwnames);
0129
0130 extern int _PyEval_ThreadsInitialized(void);
0131 extern void _PyEval_InitGIL(PyThreadState *tstate, int own_gil);
0132 extern void _PyEval_FiniGIL(PyInterpreterState *interp);
0133
0134 extern void _PyEval_AcquireLock(PyThreadState *tstate);
0135
0136 extern void _PyEval_ReleaseLock(PyInterpreterState *, PyThreadState *,
0137 int final_release);
0138
0139 #ifdef Py_GIL_DISABLED
0140
0141
0142
0143
0144
0145 static inline int
0146 _PyEval_IsGILEnabled(PyThreadState *tstate)
0147 {
0148 struct _gil_runtime_state *gil = tstate->interp->ceval.gil;
0149 return _Py_atomic_load_int_relaxed(&gil->enabled) != 0;
0150 }
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177 extern int _PyEval_EnableGILTransient(PyThreadState *tstate);
0178 extern int _PyEval_EnableGILPermanent(PyThreadState *tstate);
0179 extern int _PyEval_DisableGIL(PyThreadState *state);
0180 #endif
0181
0182 extern void _PyEval_DeactivateOpCache(void);
0183
0184
0185
0186
0187 #ifdef USE_STACKCHECK
0188
0189
0190 static inline int _Py_MakeRecCheck(PyThreadState *tstate) {
0191 return (tstate->c_recursion_remaining-- < 0
0192 || (tstate->c_recursion_remaining & 63) == 0);
0193 }
0194 #else
0195 static inline int _Py_MakeRecCheck(PyThreadState *tstate) {
0196 return tstate->c_recursion_remaining-- < 0;
0197 }
0198 #endif
0199
0200
0201
0202 PyAPI_FUNC(int) _Py_CheckRecursiveCall(
0203 PyThreadState *tstate,
0204 const char *where);
0205
0206 int _Py_CheckRecursiveCallPy(
0207 PyThreadState *tstate);
0208
0209 static inline int _Py_EnterRecursiveCallTstate(PyThreadState *tstate,
0210 const char *where) {
0211 return (_Py_MakeRecCheck(tstate) && _Py_CheckRecursiveCall(tstate, where));
0212 }
0213
0214 static inline void _Py_EnterRecursiveCallTstateUnchecked(PyThreadState *tstate) {
0215 assert(tstate->c_recursion_remaining > 0);
0216 tstate->c_recursion_remaining--;
0217 }
0218
0219 static inline int _Py_EnterRecursiveCall(const char *where) {
0220 PyThreadState *tstate = _PyThreadState_GET();
0221 return _Py_EnterRecursiveCallTstate(tstate, where);
0222 }
0223
0224 static inline void _Py_LeaveRecursiveCallTstate(PyThreadState *tstate) {
0225 tstate->c_recursion_remaining++;
0226 }
0227
0228 static inline void _Py_LeaveRecursiveCall(void) {
0229 PyThreadState *tstate = _PyThreadState_GET();
0230 _Py_LeaveRecursiveCallTstate(tstate);
0231 }
0232
0233 extern struct _PyInterpreterFrame* _PyEval_GetFrame(void);
0234
0235 PyAPI_FUNC(PyObject *)_Py_MakeCoro(PyFunctionObject *func);
0236
0237
0238
0239 PyAPI_FUNC(int) _Py_HandlePending(PyThreadState *tstate);
0240
0241 extern PyObject * _PyEval_GetFrameLocals(void);
0242
0243 typedef PyObject *(*conversion_func)(PyObject *);
0244
0245 PyAPI_DATA(const binaryfunc) _PyEval_BinaryOps[];
0246 PyAPI_DATA(const conversion_func) _PyEval_ConversionFuncs[];
0247
0248 PyAPI_FUNC(int) _PyEval_CheckExceptStarTypeValid(PyThreadState *tstate, PyObject* right);
0249 PyAPI_FUNC(int) _PyEval_CheckExceptTypeValid(PyThreadState *tstate, PyObject* right);
0250 PyAPI_FUNC(int) _PyEval_ExceptionGroupMatch(PyObject* exc_value, PyObject *match_type, PyObject **match, PyObject **rest);
0251 PyAPI_FUNC(void) _PyEval_FormatAwaitableError(PyThreadState *tstate, PyTypeObject *type, int oparg);
0252 PyAPI_FUNC(void) _PyEval_FormatExcCheckArg(PyThreadState *tstate, PyObject *exc, const char *format_str, PyObject *obj);
0253 PyAPI_FUNC(void) _PyEval_FormatExcUnbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
0254 PyAPI_FUNC(void) _PyEval_FormatKwargsError(PyThreadState *tstate, PyObject *func, PyObject *kwargs);
0255 PyAPI_FUNC(PyObject *)_PyEval_MatchClass(PyThreadState *tstate, PyObject *subject, PyObject *type, Py_ssize_t nargs, PyObject *kwargs);
0256 PyAPI_FUNC(PyObject *)_PyEval_MatchKeys(PyThreadState *tstate, PyObject *map, PyObject *keys);
0257 PyAPI_FUNC(int) _PyEval_UnpackIterable(PyThreadState *tstate, PyObject *v, int argcnt, int argcntafter, PyObject **sp);
0258 PyAPI_FUNC(void) _PyEval_MonitorRaise(PyThreadState *tstate, _PyInterpreterFrame *frame, _Py_CODEUNIT *instr);
0259 PyAPI_FUNC(void) _PyEval_FrameClearAndPop(PyThreadState *tstate, _PyInterpreterFrame *frame);
0260
0261
0262
0263 #define _PY_GIL_DROP_REQUEST_BIT (1U << 0)
0264 #define _PY_SIGNALS_PENDING_BIT (1U << 1)
0265 #define _PY_CALLS_TO_DO_BIT (1U << 2)
0266 #define _PY_ASYNC_EXCEPTION_BIT (1U << 3)
0267 #define _PY_GC_SCHEDULED_BIT (1U << 4)
0268 #define _PY_EVAL_PLEASE_STOP_BIT (1U << 5)
0269 #define _PY_EVAL_EXPLICIT_MERGE_BIT (1U << 6)
0270
0271
0272 #define _PY_EVAL_EVENTS_BITS 8
0273 #define _PY_EVAL_EVENTS_MASK ((1 << _PY_EVAL_EVENTS_BITS)-1)
0274
0275 static inline void
0276 _Py_set_eval_breaker_bit(PyThreadState *tstate, uintptr_t bit)
0277 {
0278 _Py_atomic_or_uintptr(&tstate->eval_breaker, bit);
0279 }
0280
0281 static inline void
0282 _Py_unset_eval_breaker_bit(PyThreadState *tstate, uintptr_t bit)
0283 {
0284 _Py_atomic_and_uintptr(&tstate->eval_breaker, ~bit);
0285 }
0286
0287 static inline int
0288 _Py_eval_breaker_bit_is_set(PyThreadState *tstate, uintptr_t bit)
0289 {
0290 uintptr_t b = _Py_atomic_load_uintptr_relaxed(&tstate->eval_breaker);
0291 return (b & bit) != 0;
0292 }
0293
0294
0295
0296 void _Py_set_eval_breaker_bit_all(PyInterpreterState *interp, uintptr_t bit);
0297 void _Py_unset_eval_breaker_bit_all(PyInterpreterState *interp, uintptr_t bit);
0298
0299
0300 #ifdef __cplusplus
0301 }
0302 #endif
0303 #endif