Skip to main content

pyo3_ffi/
moduleobject.rs

1#[cfg(not(all(Py_LIMITED_API, Py_GIL_DISABLED)))]
2use crate::methodobject::PyMethodDef;
3use crate::object::*;
4use crate::pyport::Py_ssize_t;
5// this is pub to avoid unnecessary churn elsewhere
6#[cfg(all(Py_LIMITED_API, Py_GIL_DISABLED))]
7pub use crate::pytypedefs::PyModuleDef;
8#[cfg(Py_3_15)]
9use crate::slots::PySlot;
10use core::ffi::{c_char, c_int, c_void};
11
12#[cfg(not(RustPython))]
13extern_libpython! {
14    #[cfg_attr(PyPy, link_name = "PyPyModule_Type")]
15    pub static mut PyModule_Type: PyTypeObject;
16}
17
18#[inline]
19#[cfg(not(RustPython))]
20pub unsafe fn PyModule_Check(op: *mut PyObject) -> c_int {
21    PyObject_TypeCheck(op, &raw mut PyModule_Type)
22}
23
24#[inline]
25#[cfg(not(RustPython))]
26pub unsafe fn PyModule_CheckExact(op: *mut PyObject) -> c_int {
27    Py_IS_TYPE(op, &raw mut PyModule_Type)
28}
29
30extern_libpython! {
31    #[cfg(RustPython)]
32    pub fn PyModule_Check(op: *mut PyObject) -> c_int;
33    #[cfg(RustPython)]
34    pub fn PyModule_CheckExact(op: *mut PyObject) -> c_int;
35
36    #[cfg_attr(PyPy, link_name = "PyPyModule_NewObject")]
37    pub fn PyModule_NewObject(name: *mut PyObject) -> *mut PyObject;
38    #[cfg_attr(PyPy, link_name = "PyPyModule_New")]
39    pub fn PyModule_New(name: *const c_char) -> *mut PyObject;
40    #[cfg_attr(PyPy, link_name = "PyPyModule_GetDict")]
41    pub fn PyModule_GetDict(arg1: *mut PyObject) -> *mut PyObject;
42    #[cfg(not(PyPy))]
43    pub fn PyModule_GetNameObject(arg1: *mut PyObject) -> *mut PyObject;
44    #[cfg_attr(PyPy, link_name = "PyPyModule_GetName")]
45    pub fn PyModule_GetName(arg1: *mut PyObject) -> *const c_char;
46    #[cfg(not(all(windows, PyPy)))]
47    #[deprecated(note = "Python 3.2")]
48    pub fn PyModule_GetFilename(arg1: *mut PyObject) -> *const c_char;
49    #[cfg(not(PyPy))]
50    pub fn PyModule_GetFilenameObject(arg1: *mut PyObject) -> *mut PyObject;
51    // skipped non-limited _PyModule_Clear
52    // skipped non-limited _PyModule_ClearDict
53    // skipped non-limited _PyModuleSpec_IsInitializing
54    #[cfg_attr(PyPy, link_name = "PyPyModule_GetDef")]
55    pub fn PyModule_GetDef(arg1: *mut PyObject) -> *mut PyModuleDef;
56    #[cfg_attr(PyPy, link_name = "PyPyModule_GetState")]
57    pub fn PyModule_GetState(arg1: *mut PyObject) -> *mut c_void;
58    #[cfg_attr(PyPy, link_name = "PyPyModuleDef_Init")]
59    pub fn PyModuleDef_Init(arg1: *mut PyModuleDef) -> *mut PyObject;
60
61    #[cfg(not(RustPython))]
62    pub static mut PyModuleDef_Type: PyTypeObject;
63}
64
65#[cfg(not(all(Py_LIMITED_API, Py_GIL_DISABLED)))]
66#[repr(C)]
67pub struct PyModuleDef_Base {
68    pub ob_base: PyObject,
69    // Rust function pointers are non-null so an Option is needed here.
70    pub m_init: Option<extern "C" fn() -> *mut PyObject>,
71    pub m_index: Py_ssize_t,
72    pub m_copy: *mut PyObject,
73}
74
75#[cfg(not(all(Py_LIMITED_API, Py_GIL_DISABLED)))]
76#[allow(
77    clippy::declare_interior_mutable_const,
78    reason = "contains atomic refcount on free-threaded builds"
79)]
80pub const PyModuleDef_HEAD_INIT: PyModuleDef_Base = PyModuleDef_Base {
81    ob_base: PyObject_HEAD_INIT,
82    m_init: None,
83    m_index: 0,
84    m_copy: core::ptr::null_mut(),
85};
86
87#[repr(C)]
88#[derive(Copy, Clone, Eq, PartialEq)]
89pub struct PyModuleDef_Slot {
90    pub slot: c_int,
91    pub value: *mut c_void,
92}
93
94impl Default for PyModuleDef_Slot {
95    fn default() -> PyModuleDef_Slot {
96        PyModuleDef_Slot {
97            slot: 0,
98            value: core::ptr::null_mut(),
99        }
100    }
101}
102
103#[cfg(Py_3_12)]
104#[allow(
105    clippy::zero_ptr,
106    reason = "matches the way that the rest of these constants are defined"
107)]
108pub const Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED: *mut c_void = 0 as *mut c_void;
109#[cfg(Py_3_12)]
110pub const Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED: *mut c_void = 1 as *mut c_void;
111#[cfg(Py_3_12)]
112pub const Py_MOD_PER_INTERPRETER_GIL_SUPPORTED: *mut c_void = 2 as *mut c_void;
113
114#[cfg(Py_3_13)]
115#[allow(
116    clippy::zero_ptr,
117    reason = "matches the way that the rest of these constants are defined"
118)]
119pub const Py_MOD_GIL_USED: *mut c_void = 0 as *mut c_void;
120#[cfg(Py_3_13)]
121pub const Py_MOD_GIL_NOT_USED: *mut c_void = 1 as *mut c_void;
122
123extern_libpython! {
124    #[cfg(all(not(Py_LIMITED_API), Py_GIL_DISABLED))]
125    pub fn PyUnstable_Module_SetGIL(module: *mut PyObject, gil: *mut c_void) -> c_int;
126}
127
128#[cfg(Py_3_15)]
129extern_libpython! {
130    pub fn PyModule_FromSlotsAndSpec(slots: *const PySlot, spec: *mut PyObject) -> *mut PyObject;
131    pub fn PyModule_Exec(_mod: *mut PyObject) -> c_int;
132    pub fn PyModule_GetStateSize(_mod: *mut PyObject, result: *mut Py_ssize_t) -> c_int;
133    pub fn PyModule_GetToken(module: *mut PyObject, result: *mut *mut c_void) -> c_int;
134}
135
136#[cfg(not(all(Py_LIMITED_API, Py_GIL_DISABLED)))]
137#[repr(C)]
138pub struct PyModuleDef {
139    pub m_base: PyModuleDef_Base,
140    pub m_name: *const c_char,
141    pub m_doc: *const c_char,
142    pub m_size: Py_ssize_t,
143    pub m_methods: *mut PyMethodDef,
144    pub m_slots: *mut PyModuleDef_Slot,
145    // Rust function pointers are non-null so an Option is needed here.
146    pub m_traverse: Option<traverseproc>,
147    pub m_clear: Option<inquiry>,
148    pub m_free: Option<freefunc>,
149}