1use crate::moduleobject::PyModuleDef;
2use crate::object::PyObject;
3use crate::pytypedefs::{PyInterpreterState, PyThreadState};
4use std::ffi::c_int;
5
6#[cfg(any(all(Py_3_9, not(Py_LIMITED_API)), Py_3_10))]
7use crate::PyFrameObject;
8
9use std::ffi::c_long;
10
11pub const MAX_CO_EXTRA_USERS: c_int = 255;
12
13extern_libpython! {
14 pub fn PyInterpreterState_New() -> *mut PyInterpreterState;
15 pub fn PyInterpreterState_Clear(arg1: *mut PyInterpreterState);
16 pub fn PyInterpreterState_Delete(arg1: *mut PyInterpreterState);
17
18 #[cfg(Py_3_9)]
19 pub fn PyInterpreterState_Get() -> *mut PyInterpreterState;
20
21 pub fn PyInterpreterState_GetDict(arg1: *mut PyInterpreterState) -> *mut PyObject;
22
23 pub fn PyInterpreterState_GetID(arg1: *mut PyInterpreterState) -> i64;
24
25 pub fn PyState_AddModule(arg1: *mut PyObject, arg2: *mut PyModuleDef) -> c_int;
26 pub fn PyState_RemoveModule(arg1: *mut PyModuleDef) -> c_int;
27
28 pub fn PyState_FindModule(arg1: *mut PyModuleDef) -> *mut PyObject;
29
30 pub fn PyThreadState_New(arg1: *mut PyInterpreterState) -> *mut PyThreadState;
31 pub fn PyThreadState_Clear(arg1: *mut PyThreadState);
32 pub fn PyThreadState_Delete(arg1: *mut PyThreadState);
33
34 pub fn PyThreadState_Get() -> *mut PyThreadState;
35}
36
37#[inline]
38pub unsafe fn PyThreadState_GET() -> *mut PyThreadState {
39 PyThreadState_Get()
40}
41
42extern_libpython! {
43 pub fn PyThreadState_Swap(arg1: *mut PyThreadState) -> *mut PyThreadState;
44 pub fn PyThreadState_GetDict() -> *mut PyObject;
45 pub fn PyThreadState_SetAsyncExc(arg1: c_long, arg2: *mut PyObject) -> c_int;
46
47 #[cfg(any(all(Py_3_9, not(Py_LIMITED_API)), Py_3_10))]
48 pub fn PyThreadState_GetInterpreter(arg1: *mut PyThreadState) -> *mut PyInterpreterState;
49 #[cfg(any(all(Py_3_9, not(Py_LIMITED_API)), Py_3_10))]
50 pub fn PyThreadState_GetFrame(arg1: *mut PyThreadState) -> *mut PyFrameObject;
51 #[cfg(any(all(Py_3_9, not(Py_LIMITED_API)), Py_3_10))]
52 pub fn PyThreadState_GetID(arg1: *mut PyThreadState) -> i64;
53}
54
55#[repr(C)]
56#[derive(Copy, Clone, Debug, PartialEq, Eq)]
57pub enum PyGILState_STATE {
58 PyGILState_LOCKED,
59 PyGILState_UNLOCKED,
60}
61
62#[cfg(not(any(Py_3_14, target_arch = "wasm32")))]
63struct HangThread;
64
65#[cfg(not(any(Py_3_14, target_arch = "wasm32")))]
66impl Drop for HangThread {
67 fn drop(&mut self) {
68 loop {
69 std::thread::park(); }
71 }
72}
73
74mod raw {
83 #[cfg(not(any(Py_3_14, target_arch = "wasm32")))]
84 extern_libpython! { "C-unwind" {
85 pub fn PyGILState_Ensure() -> super::PyGILState_STATE;
86 }}
87
88 #[cfg(any(Py_3_14, target_arch = "wasm32"))]
89 extern_libpython! {
90 pub fn PyGILState_Ensure() -> super::PyGILState_STATE;
91 }
92}
93
94#[cfg(not(any(Py_3_14, target_arch = "wasm32")))]
95pub unsafe extern "C" fn PyGILState_Ensure() -> PyGILState_STATE {
96 let guard = HangThread;
97 let ret: PyGILState_STATE = raw::PyGILState_Ensure();
116 std::mem::forget(guard);
117 ret
118}
119
120#[cfg(any(Py_3_14, target_arch = "wasm32"))]
121pub use self::raw::PyGILState_Ensure;
122
123extern_libpython! {
124 pub fn PyGILState_Release(arg1: PyGILState_STATE);
125 pub fn PyGILState_GetThisThreadState() -> *mut PyThreadState;
126}