1use crate::moduleobject::PyModuleDef;
2use crate::object::PyObject;
3use std::ffi::c_int;
4
5#[cfg(all(Py_3_10, not(PyPy), not(Py_LIMITED_API)))]
6use crate::PyFrameObject;
7
8#[cfg(not(PyPy))]
9use std::ffi::c_long;
10
11pub const MAX_CO_EXTRA_USERS: c_int = 255;
12
13opaque_struct!(pub PyThreadState);
14opaque_struct!(pub PyInterpreterState);
15
16extern "C" {
17 #[cfg(not(PyPy))]
18 pub fn PyInterpreterState_New() -> *mut PyInterpreterState;
19 #[cfg(not(PyPy))]
20 pub fn PyInterpreterState_Clear(arg1: *mut PyInterpreterState);
21 #[cfg(not(PyPy))]
22 pub fn PyInterpreterState_Delete(arg1: *mut PyInterpreterState);
23
24 #[cfg(all(Py_3_9, not(PyPy)))]
25 pub fn PyInterpreterState_Get() -> *mut PyInterpreterState;
26
27 #[cfg(all(Py_3_8, not(PyPy)))]
28 pub fn PyInterpreterState_GetDict(arg1: *mut PyInterpreterState) -> *mut PyObject;
29
30 #[cfg(not(PyPy))]
31 pub fn PyInterpreterState_GetID(arg1: *mut PyInterpreterState) -> i64;
32
33 #[cfg_attr(PyPy, link_name = "PyPyState_AddModule")]
34 pub fn PyState_AddModule(arg1: *mut PyObject, arg2: *mut PyModuleDef) -> c_int;
35
36 #[cfg_attr(PyPy, link_name = "PyPyState_RemoveModule")]
37 pub fn PyState_RemoveModule(arg1: *mut PyModuleDef) -> c_int;
38
39 #[cfg_attr(PyPy, link_name = "PyPyState_FindModule")]
40 pub fn PyState_FindModule(arg1: *mut PyModuleDef) -> *mut PyObject;
41
42 #[cfg_attr(PyPy, link_name = "PyPyThreadState_New")]
43 pub fn PyThreadState_New(arg1: *mut PyInterpreterState) -> *mut PyThreadState;
44 #[cfg_attr(PyPy, link_name = "PyPyThreadState_Clear")]
45 pub fn PyThreadState_Clear(arg1: *mut PyThreadState);
46 #[cfg_attr(PyPy, link_name = "PyPyThreadState_Delete")]
47 pub fn PyThreadState_Delete(arg1: *mut PyThreadState);
48
49 #[cfg_attr(PyPy, link_name = "PyPyThreadState_Get")]
50 pub fn PyThreadState_Get() -> *mut PyThreadState;
51}
52
53#[inline]
54pub unsafe fn PyThreadState_GET() -> *mut PyThreadState {
55 PyThreadState_Get()
56}
57
58extern "C" {
59 #[cfg_attr(PyPy, link_name = "PyPyThreadState_Swap")]
60 pub fn PyThreadState_Swap(arg1: *mut PyThreadState) -> *mut PyThreadState;
61 #[cfg_attr(PyPy, link_name = "PyPyThreadState_GetDict")]
62 pub fn PyThreadState_GetDict() -> *mut PyObject;
63 #[cfg(not(PyPy))]
64 pub fn PyThreadState_SetAsyncExc(arg1: c_long, arg2: *mut PyObject) -> c_int;
65}
66
67extern "C" {
71 #[cfg(all(Py_3_10, not(PyPy), not(Py_LIMITED_API)))]
73 pub fn PyThreadState_GetFrame(arg1: *mut PyThreadState) -> *mut PyFrameObject;
74}
75
76#[repr(C)]
77#[derive(Copy, Clone, Debug, PartialEq, Eq)]
78pub enum PyGILState_STATE {
79 PyGILState_LOCKED,
80 PyGILState_UNLOCKED,
81}
82
83#[cfg(not(any(Py_3_14, target_arch = "wasm32")))]
84struct HangThread;
85
86#[cfg(not(any(Py_3_14, target_arch = "wasm32")))]
87impl Drop for HangThread {
88 fn drop(&mut self) {
89 loop {
90 std::thread::park(); }
92 }
93}
94
95mod raw {
104 #[cfg(not(any(Py_3_14, target_arch = "wasm32")))]
105 extern "C-unwind" {
106 #[cfg_attr(PyPy, link_name = "PyPyGILState_Ensure")]
107 pub fn PyGILState_Ensure() -> super::PyGILState_STATE;
108 }
109
110 #[cfg(any(Py_3_14, target_arch = "wasm32"))]
111 extern "C" {
112 #[cfg_attr(PyPy, link_name = "PyPyGILState_Ensure")]
113 pub fn PyGILState_Ensure() -> super::PyGILState_STATE;
114 }
115}
116
117#[cfg(not(any(Py_3_14, target_arch = "wasm32")))]
118pub unsafe extern "C" fn PyGILState_Ensure() -> PyGILState_STATE {
119 let guard = HangThread;
120 let ret: PyGILState_STATE = raw::PyGILState_Ensure();
139 std::mem::forget(guard);
140 ret
141}
142
143#[cfg(any(Py_3_14, target_arch = "wasm32"))]
144pub use self::raw::PyGILState_Ensure;
145
146extern "C" {
147 #[cfg_attr(PyPy, link_name = "PyPyGILState_Release")]
148 pub fn PyGILState_Release(arg1: PyGILState_STATE);
149 #[cfg(not(PyPy))]
150 pub fn PyGILState_GetThisThreadState() -> *mut PyThreadState;
151}