1use crate::moduleobject::PyModuleDef;
2use crate::object::PyObject;
3use std::os::raw::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::os::raw::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(all(PyPy, Py_3_10), link_name = "PyPyState_FindModule")]
41 pub fn PyState_FindModule(arg1: *mut PyModuleDef) -> *mut PyObject;
42
43 #[cfg_attr(PyPy, link_name = "PyPyThreadState_New")]
44 pub fn PyThreadState_New(arg1: *mut PyInterpreterState) -> *mut PyThreadState;
45 #[cfg_attr(PyPy, link_name = "PyPyThreadState_Clear")]
46 pub fn PyThreadState_Clear(arg1: *mut PyThreadState);
47 #[cfg_attr(PyPy, link_name = "PyPyThreadState_Delete")]
48 pub fn PyThreadState_Delete(arg1: *mut PyThreadState);
49
50 #[cfg_attr(PyPy, link_name = "PyPyThreadState_Get")]
51 pub fn PyThreadState_Get() -> *mut PyThreadState;
52}
53
54#[inline]
55pub unsafe fn PyThreadState_GET() -> *mut PyThreadState {
56 PyThreadState_Get()
57}
58
59extern "C" {
60 #[cfg_attr(PyPy, link_name = "PyPyThreadState_Swap")]
61 pub fn PyThreadState_Swap(arg1: *mut PyThreadState) -> *mut PyThreadState;
62 #[cfg_attr(PyPy, link_name = "PyPyThreadState_GetDict")]
63 pub fn PyThreadState_GetDict() -> *mut PyObject;
64 #[cfg(not(PyPy))]
65 pub fn PyThreadState_SetAsyncExc(arg1: c_long, arg2: *mut PyObject) -> c_int;
66}
67
68extern "C" {
72 #[cfg(all(Py_3_10, not(PyPy), not(Py_LIMITED_API)))]
74 pub fn PyThreadState_GetFrame(arg1: *mut PyThreadState) -> *mut PyFrameObject;
75}
76
77#[repr(C)]
78#[derive(Copy, Clone, Debug, PartialEq, Eq)]
79pub enum PyGILState_STATE {
80 PyGILState_LOCKED,
81 PyGILState_UNLOCKED,
82}
83
84#[cfg(not(any(Py_3_14, target_arch = "wasm32")))]
85struct HangThread;
86
87#[cfg(not(any(Py_3_14, target_arch = "wasm32")))]
88impl Drop for HangThread {
89 fn drop(&mut self) {
90 loop {
91 std::thread::park(); }
93 }
94}
95
96mod raw {
105 #[cfg(not(any(Py_3_14, target_arch = "wasm32")))]
106 extern "C-unwind" {
107 #[cfg_attr(PyPy, link_name = "PyPyGILState_Ensure")]
108 pub fn PyGILState_Ensure() -> super::PyGILState_STATE;
109 }
110
111 #[cfg(any(Py_3_14, target_arch = "wasm32"))]
112 extern "C" {
113 #[cfg_attr(PyPy, link_name = "PyPyGILState_Ensure")]
114 pub fn PyGILState_Ensure() -> super::PyGILState_STATE;
115 }
116}
117
118#[cfg(not(any(Py_3_14, target_arch = "wasm32")))]
119pub unsafe extern "C" fn PyGILState_Ensure() -> PyGILState_STATE {
120 let guard = HangThread;
121 let ret: PyGILState_STATE = raw::PyGILState_Ensure();
140 std::mem::forget(guard);
141 ret
142}
143
144#[cfg(any(Py_3_14, target_arch = "wasm32"))]
145pub use self::raw::PyGILState_Ensure;
146
147extern "C" {
148 #[cfg_attr(PyPy, link_name = "PyPyGILState_Release")]
149 pub fn PyGILState_Release(arg1: PyGILState_STATE);
150 #[cfg(not(PyPy))]
151 pub fn PyGILState_GetThisThreadState() -> *mut PyThreadState;
152}