Debian Codegraph @

Include/internal/pycore_ceval.h

raw
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_code.h" // _PyCode_GetTLBCFast()
14 #include "pycore_interp.h" // PyInterpreterState.eval_frame
15 #include "pycore_pystate.h" // _PyThreadState_GET()
16 #include "pycore_stats.h" // EVAL_CALL_STAT_INC()
17 #include "pycore_typedefs.h" // _PyInterpreterFrame
18
19
20 /* Forward declarations */
21 struct _ceval_runtime_state;
22
23 // Export for '_lsprof' shared extension
24 PyAPI_FUNC(int) _PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg);
25 extern int _PyEval_SetProfileAllThreads(PyInterpreterState *interp, Py_tracefunc func, PyObject *arg);
26
27 extern int _PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg);
28 extern int _PyEval_SetTraceAllThreads(PyInterpreterState *interp, Py_tracefunc func, PyObject *arg);
29
30 extern int _PyEval_SetOpcodeTrace(PyFrameObject *f, bool enable);
31
32 // Helper to look up a builtin object
33 // Export for 'array' shared extension
34 PyAPI_FUNC(PyObject*) _PyEval_GetBuiltin(PyObject *);
35
36 extern PyObject* _PyEval_GetBuiltinId(_Py_Identifier *);
37
38 extern void _PyEval_SetSwitchInterval(unsigned long microseconds);
39 extern unsigned long _PyEval_GetSwitchInterval(void);
40
41 // Export for '_queue' shared extension
42 PyAPI_FUNC(int) _PyEval_MakePendingCalls(PyThreadState *);
43
44 #ifndef Py_DEFAULT_RECURSION_LIMIT
45 # define Py_DEFAULT_RECURSION_LIMIT 1000
46 #endif
47
48 extern void _Py_FinishPendingCalls(PyThreadState *tstate);
49 extern void _PyEval_InitState(PyInterpreterState *);
50 extern void _PyEval_SignalReceived(void);
51
52 // bitwise flags:
53 #define _Py_PENDING_MAINTHREADONLY 1
54 #define _Py_PENDING_RAWFREE 2
55
56 typedef int _Py_add_pending_call_result;
57 #define _Py_ADD_PENDING_SUCCESS 0
58 #define _Py_ADD_PENDING_FULL -1
59
60 // Export for '_testinternalcapi' shared extension
61 PyAPI_FUNC(_Py_add_pending_call_result) _PyEval_AddPendingCall(
62 PyInterpreterState *interp,
63 _Py_pending_call_func func,
64 void *arg,
65 int flags);
66
67 #ifdef HAVE_FORK
68 extern PyStatus _PyEval_ReInitThreads(PyThreadState *tstate);
69 #endif
70
71 // Used by sys.call_tracing()
72 extern PyObject* _PyEval_CallTracing(PyObject *func, PyObject *args);
73
74 // Used by sys.get_asyncgen_hooks()
75 extern PyObject* _PyEval_GetAsyncGenFirstiter(void);
76 extern PyObject* _PyEval_GetAsyncGenFinalizer(void);
77
78 // Used by sys.set_asyncgen_hooks()
79 extern int _PyEval_SetAsyncGenFirstiter(PyObject *);
80 extern int _PyEval_SetAsyncGenFinalizer(PyObject *);
81
82 // Used by sys.get_coroutine_origin_tracking_depth()
83 // and sys.set_coroutine_origin_tracking_depth()
84 extern int _PyEval_GetCoroutineOriginTrackingDepth(void);
85 extern int _PyEval_SetCoroutineOriginTrackingDepth(int depth);
86
87 extern void _PyEval_Fini(void);
88
89
90 extern PyObject* _PyEval_GetBuiltins(PyThreadState *tstate);
91
92 // Trampoline API
93
94 typedef struct {
95 // Callback to initialize the trampoline state
96 void* (*init_state)(void);
97 // Callback to register every trampoline being created
98 void (*write_state)(void* state, const void *code_addr,
99 unsigned int code_size, PyCodeObject* code);
100 // Callback to free the trampoline state
101 int (*free_state)(void* state);
102 } _PyPerf_Callbacks;
103
104 extern int _PyPerfTrampoline_SetCallbacks(_PyPerf_Callbacks *);
105 extern void _PyPerfTrampoline_GetCallbacks(_PyPerf_Callbacks *);
106 extern int _PyPerfTrampoline_Init(int activate);
107 extern int _PyPerfTrampoline_Fini(void);
108 extern int _PyIsPerfTrampolineActive(void);
109 extern PyStatus _PyPerfTrampoline_AfterFork_Child(void);
110 #ifdef PY_HAVE_PERF_TRAMPOLINE
111 extern _PyPerf_Callbacks _Py_perfmap_callbacks;
112 extern _PyPerf_Callbacks _Py_perfmap_jit_callbacks;
113 #endif
114
115 static inline PyObject*
116 _PyEval_EvalFrame(PyThreadState *tstate, _PyInterpreterFrame *frame, int throwflag)
117 {
118 EVAL_CALL_STAT_INC(EVAL_CALL_TOTAL);
119 if (tstate->interp->eval_frame == NULL) {
120 return _PyEval_EvalFrameDefault(tstate, frame, throwflag);
121 }
122 return tstate->interp->eval_frame(tstate, frame, throwflag);
123 }
124
125 extern PyObject*
126 _PyEval_Vector(PyThreadState *tstate,
127 PyFunctionObject *func, PyObject *locals,
128 PyObject* const* args, size_t argcount,
129 PyObject *kwnames);
130
131 extern int _PyEval_ThreadsInitialized(void);
132 extern void _PyEval_InitGIL(PyThreadState *tstate, int own_gil);
133 extern void _PyEval_FiniGIL(PyInterpreterState *interp);
134
135 extern void _PyEval_AcquireLock(PyThreadState *tstate);
136
137 extern void _PyEval_ReleaseLock(PyInterpreterState *, PyThreadState *,
138 int final_release);
139
140 #ifdef Py_GIL_DISABLED
141 // Returns 0 or 1 if the GIL for the given thread's interpreter is disabled or
142 // enabled, respectively.
143 //
144 // The enabled state of the GIL will not change while one or more threads are
145 // attached.
146 static inline int
147 _PyEval_IsGILEnabled(PyThreadState *tstate)
148 {
149 struct _gil_runtime_state *gil = tstate->interp->ceval.gil;
150 return _Py_atomic_load_int_relaxed(&gil->enabled) != 0;
151 }
152
153 // Enable or disable the GIL used by the interpreter that owns tstate, which
154 // must be the current thread. This may affect other interpreters, if the GIL
155 // is shared. All three functions will be no-ops (and return 0) if the
156 // interpreter's `enable_gil' config is not _PyConfig_GIL_DEFAULT.
157 //
158 // Every call to _PyEval_EnableGILTransient() must be paired with exactly one
159 // call to either _PyEval_EnableGILPermanent() or
160 // _PyEval_DisableGIL(). _PyEval_EnableGILPermanent() and _PyEval_DisableGIL()
161 // must only be called while the GIL is enabled from a call to
162 // _PyEval_EnableGILTransient().
163 //
164 // _PyEval_EnableGILTransient() returns 1 if it enabled the GIL, or 0 if the
165 // GIL was already enabled, whether transiently or permanently. The caller will
166 // hold the GIL upon return.
167 //
168 // _PyEval_EnableGILPermanent() returns 1 if it permanently enabled the GIL
169 // (which must already be enabled), or 0 if it was already permanently
170 // enabled. Once _PyEval_EnableGILPermanent() has been called once, all
171 // subsequent calls to any of the three functions will be no-ops.
172 //
173 // _PyEval_DisableGIL() returns 1 if it disabled the GIL, or 0 if the GIL was
174 // kept enabled because of another request, whether transient or permanent.
175 //
176 // All three functions must be called by an attached thread (this implies that
177 // if the GIL is enabled, the current thread must hold it).
178 extern int _PyEval_EnableGILTransient(PyThreadState *tstate);
179 extern int _PyEval_EnableGILPermanent(PyThreadState *tstate);
180 extern int _PyEval_DisableGIL(PyThreadState *state);
181
182
183 static inline _Py_CODEUNIT *
184 _PyEval_GetExecutableCode(PyThreadState *tstate, PyCodeObject *co)
185 {
186 _Py_CODEUNIT *bc = _PyCode_GetTLBCFast(tstate, co);
187 if (bc != NULL) {
188 return bc;
189 }
190 return _PyCode_GetTLBC(co);
191 }
192
193 #endif
194
195 extern void _PyEval_DeactivateOpCache(void);
196
197
198 /* --- _Py_EnterRecursiveCall() ----------------------------------------- */
199
200 static inline int _Py_MakeRecCheck(PyThreadState *tstate) {
201 uintptr_t here_addr = _Py_get_machine_stack_pointer();
202 _PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate;
203 // Overflow if stack pointer is between soft limit and the base of the hardware stack.
204 // If it is below the hardware stack base, assume that we have the wrong stack limits, and do nothing.
205 // We could have the wrong stack limits because of limited platform support, or user-space threads.
206 #if _Py_STACK_GROWS_DOWN
207 return here_addr < _tstate->c_stack_soft_limit && here_addr >= _tstate->c_stack_soft_limit - 2 * _PyOS_STACK_MARGIN_BYTES;
208 #else
209 return here_addr > _tstate->c_stack_soft_limit && here_addr <= _tstate->c_stack_soft_limit + 2 * _PyOS_STACK_MARGIN_BYTES;
210 #endif
211 }
212
213 // Export for '_json' shared extension, used via _Py_EnterRecursiveCall()
214 // static inline function.
215 PyAPI_FUNC(int) _Py_CheckRecursiveCall(
216 PyThreadState *tstate,
217 const char *where);
218
219 int _Py_CheckRecursiveCallPy(
220 PyThreadState *tstate);
221
222 static inline int _Py_EnterRecursiveCallTstate(PyThreadState *tstate,
223 const char *where) {
224 return (_Py_MakeRecCheck(tstate) && _Py_CheckRecursiveCall(tstate, where));
225 }
226
227 static inline int _Py_EnterRecursiveCall(const char *where) {
228 PyThreadState *tstate = _PyThreadState_GET();
229 return _Py_EnterRecursiveCallTstate(tstate, where);
230 }
231
232 static inline void _Py_LeaveRecursiveCallTstate(PyThreadState *tstate) {
233 (void)tstate;
234 }
235
236 PyAPI_FUNC(void) _Py_InitializeRecursionLimits(PyThreadState *tstate);
237
238 static inline int _Py_ReachedRecursionLimit(PyThreadState *tstate) {
239 uintptr_t here_addr = _Py_get_machine_stack_pointer();
240 _PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate;
241 assert(_tstate->c_stack_hard_limit != 0);
242 #if _Py_STACK_GROWS_DOWN
243 return here_addr <= _tstate->c_stack_soft_limit;
244 #else
245 return here_addr >= _tstate->c_stack_soft_limit;
246 #endif
247 }
248
249 static inline void _Py_LeaveRecursiveCall(void) {
250 }
251
252 extern _PyInterpreterFrame* _PyEval_GetFrame(void);
253
254 extern PyObject * _PyEval_GetGlobalsFromRunningMain(PyThreadState *);
255 extern int _PyEval_EnsureBuiltins(
256 PyThreadState *,
257 PyObject *,
258 PyObject **p_builtins);
259 extern int _PyEval_EnsureBuiltinsWithModule(
260 PyThreadState *,
261 PyObject *,
262 PyObject **p_builtins);
263
264 PyAPI_FUNC(PyObject *)_Py_MakeCoro(PyFunctionObject *func);
265
266 /* Handle signals, pending calls, GIL drop request
267 and asynchronous exception */
268 PyAPI_FUNC(int) _Py_HandlePending(PyThreadState *tstate);
269
270 extern PyObject * _PyEval_GetFrameLocals(void);
271
272 typedef PyObject *(*conversion_func)(PyObject *);
273
274 PyAPI_DATA(const binaryfunc) _PyEval_BinaryOps[];
275 PyAPI_DATA(const conversion_func) _PyEval_ConversionFuncs[];
276
277 typedef struct _special_method {
278 PyObject *name;
279 const char *error;
280 const char *error_suggestion; // improved optional suggestion
281 } _Py_SpecialMethod;
282
283 PyAPI_DATA(const _Py_SpecialMethod) _Py_SpecialMethods[];
284 PyAPI_DATA(const size_t) _Py_FunctionAttributeOffsets[];
285
286 PyAPI_FUNC(int) _PyEval_CheckExceptStarTypeValid(PyThreadState *tstate, PyObject* right);
287 PyAPI_FUNC(int) _PyEval_CheckExceptTypeValid(PyThreadState *tstate, PyObject* right);
288 PyAPI_FUNC(int) _PyEval_ExceptionGroupMatch(_PyInterpreterFrame *, PyObject* exc_value, PyObject *match_type, PyObject **match, PyObject **rest);
289 PyAPI_FUNC(void) _PyEval_FormatAwaitableError(PyThreadState *tstate, PyTypeObject *type, int oparg);
290 PyAPI_FUNC(void) _PyEval_FormatExcCheckArg(PyThreadState *tstate, PyObject *exc, const char *format_str, PyObject *obj);
291 PyAPI_FUNC(void) _PyEval_FormatExcUnbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
292 PyAPI_FUNC(void) _PyEval_FormatKwargsError(PyThreadState *tstate, PyObject *func, PyObject *kwargs);
293 PyAPI_FUNC(PyObject *) _PyEval_ImportFrom(PyThreadState *, PyObject *, PyObject *);
294 PyAPI_FUNC(PyObject *) _PyEval_ImportName(PyThreadState *, _PyInterpreterFrame *, PyObject *, PyObject *, PyObject *);
295 PyAPI_FUNC(PyObject *)_PyEval_MatchClass(PyThreadState *tstate, PyObject *subject, PyObject *type, Py_ssize_t nargs, PyObject *kwargs);
296 PyAPI_FUNC(PyObject *)_PyEval_MatchKeys(PyThreadState *tstate, PyObject *map, PyObject *keys);
297 PyAPI_FUNC(void) _PyEval_MonitorRaise(PyThreadState *tstate, _PyInterpreterFrame *frame, _Py_CODEUNIT *instr);
298 PyAPI_FUNC(bool) _PyEval_NoToolsForUnwind(PyThreadState *tstate);
299 PyAPI_FUNC(int) _PyEval_UnpackIterableStackRef(PyThreadState *tstate, PyObject *v, int argcnt, int argcntafter, _PyStackRef *sp);
300 PyAPI_FUNC(void) _PyEval_FrameClearAndPop(PyThreadState *tstate, _PyInterpreterFrame *frame);
301 PyAPI_FUNC(PyObject **) _PyObjectArray_FromStackRefArray(_PyStackRef *input, Py_ssize_t nargs, PyObject **scratch);
302
303 PyAPI_FUNC(void) _PyObjectArray_Free(PyObject **array, PyObject **scratch);
304
305 PyAPI_FUNC(PyObject *) _PyEval_GetANext(PyObject *aiter);
306 PyAPI_FUNC(void) _PyEval_LoadGlobalStackRef(PyObject *globals, PyObject *builtins, PyObject *name, _PyStackRef *writeto);
307 PyAPI_FUNC(PyObject *) _PyEval_GetAwaitable(PyObject *iterable, int oparg);
308 PyAPI_FUNC(PyObject *) _PyEval_LoadName(PyThreadState *tstate, _PyInterpreterFrame *frame, PyObject *name);
309 PyAPI_FUNC(int)
310 _Py_Check_ArgsIterable(PyThreadState *tstate, PyObject *func, PyObject *args);
311
312 /*
313 * Indicate whether a special method of given 'oparg' can use the (improved)
314 * alternative error message instead. Only methods loaded by LOAD_SPECIAL
315 * support alternative error messages.
316 *
317 * Symbol is exported for the JIT (see discussion on GH-132218).
318 */
319 PyAPI_FUNC(int)
320 _PyEval_SpecialMethodCanSuggest(PyObject *self, int oparg);
321
322 /* Bits that can be set in PyThreadState.eval_breaker */
323 #define _PY_GIL_DROP_REQUEST_BIT (1U << 0)
324 #define _PY_SIGNALS_PENDING_BIT (1U << 1)
325 #define _PY_CALLS_TO_DO_BIT (1U << 2)
326 #define _PY_ASYNC_EXCEPTION_BIT (1U << 3)
327 #define _PY_GC_SCHEDULED_BIT (1U << 4)
328 #define _PY_EVAL_PLEASE_STOP_BIT (1U << 5)
329 #define _PY_EVAL_EXPLICIT_MERGE_BIT (1U << 6)
330 #define _PY_EVAL_JIT_INVALIDATE_COLD_BIT (1U << 7)
331
332 /* Reserve a few bits for future use */
333 #define _PY_EVAL_EVENTS_BITS 8
334 #define _PY_EVAL_EVENTS_MASK ((1 << _PY_EVAL_EVENTS_BITS)-1)
335
336 static inline void
337 _Py_set_eval_breaker_bit(PyThreadState *tstate, uintptr_t bit)
338 {
339 _Py_atomic_or_uintptr(&tstate->eval_breaker, bit);
340 }
341
342 static inline void
343 _Py_unset_eval_breaker_bit(PyThreadState *tstate, uintptr_t bit)
344 {
345 _Py_atomic_and_uintptr(&tstate->eval_breaker, ~bit);
346 }
347
348 static inline int
349 _Py_eval_breaker_bit_is_set(PyThreadState *tstate, uintptr_t bit)
350 {
351 uintptr_t b = _Py_atomic_load_uintptr_relaxed(&tstate->eval_breaker);
352 return (b & bit) != 0;
353 }
354
355 // Free-threaded builds use these functions to set or unset a bit on all
356 // threads in the given interpreter.
357 void _Py_set_eval_breaker_bit_all(PyInterpreterState *interp, uintptr_t bit);
358 void _Py_unset_eval_breaker_bit_all(PyInterpreterState *interp, uintptr_t bit);
359
360 PyAPI_FUNC(_PyStackRef) _PyFloat_FromDouble_ConsumeInputs(_PyStackRef left, _PyStackRef right, double value);
361
362 #ifndef Py_SUPPORTS_REMOTE_DEBUG
363 #if defined(__APPLE__)
364 #include <TargetConditionals.h>
365 # if !defined(TARGET_OS_OSX)
366 // Older macOS SDKs do not define TARGET_OS_OSX
367 # define TARGET_OS_OSX 1
368 # endif
369 #endif
370 #if ((defined(__APPLE__) && TARGET_OS_OSX) || defined(MS_WINDOWS) || (defined(__linux__) && HAVE_PROCESS_VM_READV))
371 # define Py_SUPPORTS_REMOTE_DEBUG 1
372 #endif
373 #endif
374
375 #if defined(Py_REMOTE_DEBUG) && defined(Py_SUPPORTS_REMOTE_DEBUG)
376 extern int _PyRunRemoteDebugger(PyThreadState *tstate);
377 #endif
378
379 /* Special methods used by LOAD_SPECIAL */
380 #define SPECIAL___ENTER__ 0
381 #define SPECIAL___EXIT__ 1
382 #define SPECIAL___AENTER__ 2
383 #define SPECIAL___AEXIT__ 3
384 #define SPECIAL_MAX 3
385
386 PyAPI_FUNC(_PyStackRef)
387 _Py_LoadAttr_StackRefSteal(
388 PyThreadState *tstate, _PyStackRef owner,
389 PyObject *name, _PyStackRef *self_or_null);
390
391 #ifdef __cplusplus
392 }
393 #endif
394 #endif /* !Py_INTERNAL_CEVAL_H */