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 #include "dynamic_annotations.h" // _Py_ANNOTATE_RWLOCK_CREATE
12
13 #include "pycore_interp.h" // PyInterpreterState.eval_frame
14 #include "pycore_pystate.h" // _PyThreadState_GET()
15
16 /* Forward declarations */
17 struct pyruntimestate;
18 struct _ceval_runtime_state;
19
20 // Export for '_lsprof' shared extension
21 PyAPI_FUNC(int) _PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg);
22
23 extern int _PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg);
24
25 extern int _PyEval_SetOpcodeTrace(PyFrameObject *f, bool enable);
26
27 // Helper to look up a builtin object
28 // Export for 'array' shared extension
29 PyAPI_FUNC(PyObject*) _PyEval_GetBuiltin(PyObject *);
30
31 extern PyObject* _PyEval_GetBuiltinId(_Py_Identifier *);
32
33 extern void _PyEval_SetSwitchInterval(unsigned long microseconds);
34 extern unsigned long _PyEval_GetSwitchInterval(void);
35
36 // Export for '_queue' shared extension
37 PyAPI_FUNC(int) _PyEval_MakePendingCalls(PyThreadState *);
38
39 #ifndef Py_DEFAULT_RECURSION_LIMIT
40 # define Py_DEFAULT_RECURSION_LIMIT 1000
41 #endif
42
43 extern void _Py_FinishPendingCalls(PyThreadState *tstate);
44 extern void _PyEval_InitState(PyInterpreterState *);
45 extern void _PyEval_SignalReceived(void);
46
47 // bitwise flags:
48 #define _Py_PENDING_MAINTHREADONLY 1
49 #define _Py_PENDING_RAWFREE 2
50
51 typedef int _Py_add_pending_call_result;
52 #define _Py_ADD_PENDING_SUCCESS 0
53 #define _Py_ADD_PENDING_FULL -1
54
55 // Export for '_testinternalcapi' shared extension
56 PyAPI_FUNC(_Py_add_pending_call_result) _PyEval_AddPendingCall(
57 PyInterpreterState *interp,
58 _Py_pending_call_func func,
59 void *arg,
60 int flags);
61
62 #ifdef HAVE_FORK
63 extern PyStatus _PyEval_ReInitThreads(PyThreadState *tstate);
64 #endif
65
66 // Used by sys.call_tracing()
67 extern PyObject* _PyEval_CallTracing(PyObject *func, PyObject *args);
68
69 // Used by sys.get_asyncgen_hooks()
70 extern PyObject* _PyEval_GetAsyncGenFirstiter(void);
71 extern PyObject* _PyEval_GetAsyncGenFinalizer(void);
72
73 // Used by sys.set_asyncgen_hooks()
74 extern int _PyEval_SetAsyncGenFirstiter(PyObject *);
75 extern int _PyEval_SetAsyncGenFinalizer(PyObject *);
76
77 // Used by sys.get_coroutine_origin_tracking_depth()
78 // and sys.set_coroutine_origin_tracking_depth()
79 extern int _PyEval_GetCoroutineOriginTrackingDepth(void);
80 extern int _PyEval_SetCoroutineOriginTrackingDepth(int depth);
81
82 extern void _PyEval_Fini(void);
83
84
85 extern PyObject* _PyEval_GetBuiltins(PyThreadState *tstate);
86 extern PyObject* _PyEval_BuiltinsFromGlobals(
87 PyThreadState *tstate,
88 PyObject *globals);
89
90 // Trampoline API
91
92 typedef struct {
93 // Callback to initialize the trampoline state
94 void* (*init_state)(void);
95 // Callback to register every trampoline being created
96 void (*write_state)(void* state, const void *code_addr,
97 unsigned int code_size, PyCodeObject* code);
98 // Callback to free the trampoline state
99 int (*free_state)(void* state);
100 } _PyPerf_Callbacks;
101
102 extern int _PyPerfTrampoline_SetCallbacks(_PyPerf_Callbacks *);
103 extern void _PyPerfTrampoline_GetCallbacks(_PyPerf_Callbacks *);
104 extern int _PyPerfTrampoline_Init(int activate);
105 extern int _PyPerfTrampoline_Fini(void);
106 extern void _PyPerfTrampoline_FreeArenas(void);
107 extern int _PyIsPerfTrampolineActive(void);
108 extern PyStatus _PyPerfTrampoline_AfterFork_Child(void);
109 #ifdef PY_HAVE_PERF_TRAMPOLINE
110 extern _PyPerf_Callbacks _Py_perfmap_callbacks;
111 extern _PyPerf_Callbacks _Py_perfmap_jit_callbacks;
112 #endif
113
114 static inline PyObject*
_PyEval_EvalFrame(PyThreadState * tstate,struct _PyInterpreterFrame * frame,int throwflag)115 _PyEval_EvalFrame(PyThreadState *tstate, struct _PyInterpreterFrame *frame, int throwflag)
116 {
117 EVAL_CALL_STAT_INC(EVAL_CALL_TOTAL);
118 if (tstate->interp->eval_frame == NULL) {
119 return _PyEval_EvalFrameDefault(tstate, frame, throwflag);
120 }
121 return tstate->interp->eval_frame(tstate, frame, throwflag);
122 }
123
124 extern PyObject*
125 _PyEval_Vector(PyThreadState *tstate,
126 PyFunctionObject *func, PyObject *locals,
127 PyObject* const* args, size_t argcount,
128 PyObject *kwnames);
129
130 extern int _PyEval_ThreadsInitialized(void);
131 extern void _PyEval_InitGIL(PyThreadState *tstate, int own_gil);
132 extern void _PyEval_FiniGIL(PyInterpreterState *interp);
133
134 extern void _PyEval_AcquireLock(PyThreadState *tstate);
135
136 extern void _PyEval_ReleaseLock(PyInterpreterState *, PyThreadState *,
137 int final_release);
138
139 #ifdef Py_GIL_DISABLED
140 // Returns 0 or 1 if the GIL for the given thread's interpreter is disabled or
141 // enabled, respectively.
142 //
143 // The enabled state of the GIL will not change while one or more threads are
144 // attached.
145 static inline int
_PyEval_IsGILEnabled(PyThreadState * tstate)146 _PyEval_IsGILEnabled(PyThreadState *tstate)
147 {
148 struct _gil_runtime_state *gil = tstate->interp->ceval.gil;
149 return _Py_atomic_load_int_relaxed(&gil->enabled) != 0;
150 }
151
152 // Enable or disable the GIL used by the interpreter that owns tstate, which
153 // must be the current thread. This may affect other interpreters, if the GIL
154 // is shared. All three functions will be no-ops (and return 0) if the
155 // interpreter's `enable_gil' config is not _PyConfig_GIL_DEFAULT.
156 //
157 // Every call to _PyEval_EnableGILTransient() must be paired with exactly one
158 // call to either _PyEval_EnableGILPermanent() or
159 // _PyEval_DisableGIL(). _PyEval_EnableGILPermanent() and _PyEval_DisableGIL()
160 // must only be called while the GIL is enabled from a call to
161 // _PyEval_EnableGILTransient().
162 //
163 // _PyEval_EnableGILTransient() returns 1 if it enabled the GIL, or 0 if the
164 // GIL was already enabled, whether transiently or permanently. The caller will
165 // hold the GIL upon return.
166 //
167 // _PyEval_EnableGILPermanent() returns 1 if it permanently enabled the GIL
168 // (which must already be enabled), or 0 if it was already permanently
169 // enabled. Once _PyEval_EnableGILPermanent() has been called once, all
170 // subsequent calls to any of the three functions will be no-ops.
171 //
172 // _PyEval_DisableGIL() returns 1 if it disabled the GIL, or 0 if the GIL was
173 // kept enabled because of another request, whether transient or permanent.
174 //
175 // All three functions must be called by an attached thread (this implies that
176 // if the GIL is enabled, the current thread must hold it).
177 extern int _PyEval_EnableGILTransient(PyThreadState *tstate);
178 extern int _PyEval_EnableGILPermanent(PyThreadState *tstate);
179 extern int _PyEval_DisableGIL(PyThreadState *state);
180 #endif
181
182 extern void _PyEval_DeactivateOpCache(void);
183
184
185 /* --- _Py_EnterRecursiveCall() ----------------------------------------- */
186
187 #ifdef USE_STACKCHECK
188 /* With USE_STACKCHECK macro defined, trigger stack checks in
189 _Py_CheckRecursiveCall() on every 64th call to _Py_EnterRecursiveCall. */
_Py_MakeRecCheck(PyThreadState * tstate)190 static inline int _Py_MakeRecCheck(PyThreadState *tstate) {
191 return (tstate->c_recursion_remaining-- < 0
192 || (tstate->c_recursion_remaining & 63) == 0);
193 }
194 #else
_Py_MakeRecCheck(PyThreadState * tstate)195 static inline int _Py_MakeRecCheck(PyThreadState *tstate) {
196 return tstate->c_recursion_remaining-- < 0;
197 }
198 #endif
199
200 // Export for '_json' shared extension, used via _Py_EnterRecursiveCall()
201 // static inline function.
202 PyAPI_FUNC(int) _Py_CheckRecursiveCall(
203 PyThreadState *tstate,
204 const char *where);
205
206 int _Py_CheckRecursiveCallPy(
207 PyThreadState *tstate);
208
_Py_EnterRecursiveCallTstate(PyThreadState * tstate,const char * where)209 static inline int _Py_EnterRecursiveCallTstate(PyThreadState *tstate,
210 const char *where) {
211 return (_Py_MakeRecCheck(tstate) && _Py_CheckRecursiveCall(tstate, where));
212 }
213
_Py_EnterRecursiveCallTstateUnchecked(PyThreadState * tstate)214 static inline void _Py_EnterRecursiveCallTstateUnchecked(PyThreadState *tstate) {
215 assert(tstate->c_recursion_remaining > 0);
216 tstate->c_recursion_remaining--;
217 }
218
_Py_EnterRecursiveCall(const char * where)219 static inline int _Py_EnterRecursiveCall(const char *where) {
220 PyThreadState *tstate = _PyThreadState_GET();
221 return _Py_EnterRecursiveCallTstate(tstate, where);
222 }
223
_Py_LeaveRecursiveCallTstate(PyThreadState * tstate)224 static inline void _Py_LeaveRecursiveCallTstate(PyThreadState *tstate) {
225 tstate->c_recursion_remaining++;
226 }
227
_Py_LeaveRecursiveCall(void)228 static inline void _Py_LeaveRecursiveCall(void) {
229 PyThreadState *tstate = _PyThreadState_GET();
230 _Py_LeaveRecursiveCallTstate(tstate);
231 }
232
233 extern struct _PyInterpreterFrame* _PyEval_GetFrame(void);
234
235 PyAPI_FUNC(PyObject *)_Py_MakeCoro(PyFunctionObject *func);
236
237 /* Handle signals, pending calls, GIL drop request
238 and asynchronous exception */
239 PyAPI_FUNC(int) _Py_HandlePending(PyThreadState *tstate);
240
241 extern PyObject * _PyEval_GetFrameLocals(void);
242
243 typedef PyObject *(*conversion_func)(PyObject *);
244
245 PyAPI_DATA(const binaryfunc) _PyEval_BinaryOps[];
246 PyAPI_DATA(const conversion_func) _PyEval_ConversionFuncs[];
247
248 PyAPI_FUNC(int) _PyEval_CheckExceptStarTypeValid(PyThreadState *tstate, PyObject* right);
249 PyAPI_FUNC(int) _PyEval_CheckExceptTypeValid(PyThreadState *tstate, PyObject* right);
250 PyAPI_FUNC(int) _PyEval_ExceptionGroupMatch(PyObject* exc_value, PyObject *match_type, PyObject **match, PyObject **rest);
251 PyAPI_FUNC(void) _PyEval_FormatAwaitableError(PyThreadState *tstate, PyTypeObject *type, int oparg);
252 PyAPI_FUNC(void) _PyEval_FormatExcCheckArg(PyThreadState *tstate, PyObject *exc, const char *format_str, PyObject *obj);
253 PyAPI_FUNC(void) _PyEval_FormatExcUnbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
254 PyAPI_FUNC(void) _PyEval_FormatKwargsError(PyThreadState *tstate, PyObject *func, PyObject *kwargs);
255 PyAPI_FUNC(PyObject *)_PyEval_MatchClass(PyThreadState *tstate, PyObject *subject, PyObject *type, Py_ssize_t nargs, PyObject *kwargs);
256 PyAPI_FUNC(PyObject *)_PyEval_MatchKeys(PyThreadState *tstate, PyObject *map, PyObject *keys);
257 PyAPI_FUNC(int) _PyEval_UnpackIterable(PyThreadState *tstate, PyObject *v, int argcnt, int argcntafter, PyObject **sp);
258 PyAPI_FUNC(void) _PyEval_MonitorRaise(PyThreadState *tstate, _PyInterpreterFrame *frame, _Py_CODEUNIT *instr);
259 PyAPI_FUNC(void) _PyEval_FrameClearAndPop(PyThreadState *tstate, _PyInterpreterFrame *frame);
260
261
262 /* Bits that can be set in PyThreadState.eval_breaker */
263 #define _PY_GIL_DROP_REQUEST_BIT (1U << 0)
264 #define _PY_SIGNALS_PENDING_BIT (1U << 1)
265 #define _PY_CALLS_TO_DO_BIT (1U << 2)
266 #define _PY_ASYNC_EXCEPTION_BIT (1U << 3)
267 #define _PY_GC_SCHEDULED_BIT (1U << 4)
268 #define _PY_EVAL_PLEASE_STOP_BIT (1U << 5)
269 #define _PY_EVAL_EXPLICIT_MERGE_BIT (1U << 6)
270
271 /* Reserve a few bits for future use */
272 #define _PY_EVAL_EVENTS_BITS 8
273 #define _PY_EVAL_EVENTS_MASK ((1 << _PY_EVAL_EVENTS_BITS)-1)
274
275 static inline void
_Py_set_eval_breaker_bit(PyThreadState * tstate,uintptr_t bit)276 _Py_set_eval_breaker_bit(PyThreadState *tstate, uintptr_t bit)
277 {
278 _Py_atomic_or_uintptr(&tstate->eval_breaker, bit);
279 }
280
281 static inline void
_Py_unset_eval_breaker_bit(PyThreadState * tstate,uintptr_t bit)282 _Py_unset_eval_breaker_bit(PyThreadState *tstate, uintptr_t bit)
283 {
284 _Py_atomic_and_uintptr(&tstate->eval_breaker, ~bit);
285 }
286
287 static inline int
_Py_eval_breaker_bit_is_set(PyThreadState * tstate,uintptr_t bit)288 _Py_eval_breaker_bit_is_set(PyThreadState *tstate, uintptr_t bit)
289 {
290 uintptr_t b = _Py_atomic_load_uintptr_relaxed(&tstate->eval_breaker);
291 return (b & bit) != 0;
292 }
293
294 // Free-threaded builds use these functions to set or unset a bit on all
295 // threads in the given interpreter.
296 void _Py_set_eval_breaker_bit_all(PyInterpreterState *interp, uintptr_t bit);
297 void _Py_unset_eval_breaker_bit_all(PyInterpreterState *interp, uintptr_t bit);
298
299
300 #ifdef __cplusplus
301 }
302 #endif
303 #endif /* !Py_INTERNAL_CEVAL_H */
304