pyo3_ffi/
pystate.rs

1use crate::moduleobject::PyModuleDef;
2use crate::object::PyObject;
3use std::os::raw::c_int;
4
5#[cfg(not(PyPy))]
6use std::os::raw::c_long;
7
8pub const MAX_CO_EXTRA_USERS: c_int = 255;
9
10opaque_struct!(PyThreadState);
11opaque_struct!(PyInterpreterState);
12
13extern "C" {
14    #[cfg(not(PyPy))]
15    pub fn PyInterpreterState_New() -> *mut PyInterpreterState;
16    #[cfg(not(PyPy))]
17    pub fn PyInterpreterState_Clear(arg1: *mut PyInterpreterState);
18    #[cfg(not(PyPy))]
19    pub fn PyInterpreterState_Delete(arg1: *mut PyInterpreterState);
20
21    #[cfg(all(Py_3_9, not(PyPy)))]
22    pub fn PyInterpreterState_Get() -> *mut PyInterpreterState;
23
24    #[cfg(all(Py_3_8, not(PyPy)))]
25    pub fn PyInterpreterState_GetDict(arg1: *mut PyInterpreterState) -> *mut PyObject;
26
27    #[cfg(not(PyPy))]
28    pub fn PyInterpreterState_GetID(arg1: *mut PyInterpreterState) -> i64;
29
30    #[cfg_attr(PyPy, link_name = "PyPyState_AddModule")]
31    pub fn PyState_AddModule(arg1: *mut PyObject, arg2: *mut PyModuleDef) -> c_int;
32
33    #[cfg_attr(PyPy, link_name = "PyPyState_RemoveModule")]
34    pub fn PyState_RemoveModule(arg1: *mut PyModuleDef) -> c_int;
35
36    // only has PyPy prefix since 3.10
37    #[cfg_attr(all(PyPy, Py_3_10), 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
65// skipped non-limited / 3.9 PyThreadState_GetInterpreter
66// skipped non-limited / 3.9 PyThreadState_GetFrame
67// skipped non-limited / 3.9 PyThreadState_GetID
68
69#[repr(C)]
70#[derive(Copy, Clone, Debug, PartialEq, Eq)]
71pub enum PyGILState_STATE {
72    PyGILState_LOCKED,
73    PyGILState_UNLOCKED,
74}
75
76extern "C" {
77    #[cfg_attr(PyPy, link_name = "PyPyGILState_Ensure")]
78    pub fn PyGILState_Ensure() -> PyGILState_STATE;
79    #[cfg_attr(PyPy, link_name = "PyPyGILState_Release")]
80    pub fn PyGILState_Release(arg1: PyGILState_STATE);
81    #[cfg(not(PyPy))]
82    pub fn PyGILState_GetThisThreadState() -> *mut PyThreadState;
83}