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