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