diff --git a/examples/sequential/src/module.rs b/examples/sequential/src/module.rs index 604bfe393c5..5e71f07a865 100644 --- a/examples/sequential/src/module.rs +++ b/examples/sequential/src/module.rs @@ -3,7 +3,7 @@ use pyo3_ffi::*; use std::os::raw::{c_int, c_void}; pub static mut MODULE_DEF: PyModuleDef = PyModuleDef { - m_base: PyModuleDef_HEAD_INIT(), + m_base: PyModuleDef_HEAD_INIT, m_name: c_str!("sequential").as_ptr(), m_doc: c_str!("A library for generating sequential ids, written in Rust.").as_ptr(), m_size: mem::size_of::() as Py_ssize_t, diff --git a/examples/string-sum/src/lib.rs b/examples/string-sum/src/lib.rs index 8929d788caf..ce71ab38f87 100644 --- a/examples/string-sum/src/lib.rs +++ b/examples/string-sum/src/lib.rs @@ -4,7 +4,7 @@ use std::ptr; use pyo3_ffi::*; static mut MODULE_DEF: PyModuleDef = PyModuleDef { - m_base: PyModuleDef_HEAD_INIT(), + m_base: PyModuleDef_HEAD_INIT, m_name: c_str!("string_sum").as_ptr(), m_doc: c_str!("A Python module written in Rust.").as_ptr(), m_size: 0, diff --git a/pyo3-ffi/README.md b/pyo3-ffi/README.md index 2f433c62435..200c78cec14 100644 --- a/pyo3-ffi/README.md +++ b/pyo3-ffi/README.md @@ -50,7 +50,7 @@ use std::ptr; use pyo3_ffi::*; static mut MODULE_DEF: PyModuleDef = PyModuleDef { - m_base: PyModuleDef_HEAD_INIT(), + m_base: PyModuleDef_HEAD_INIT, m_name: c_str!("string_sum").as_ptr(), m_doc: c_str!("A Python module written in Rust.").as_ptr(), m_size: 0, diff --git a/pyo3-ffi/src/lib.rs b/pyo3-ffi/src/lib.rs index fbc17878211..55c7f31404f 100644 --- a/pyo3-ffi/src/lib.rs +++ b/pyo3-ffi/src/lib.rs @@ -88,7 +88,7 @@ //! use pyo3_ffi::*; //! //! static mut MODULE_DEF: PyModuleDef = PyModuleDef { -//! m_base: PyModuleDef_HEAD_INIT(), +//! m_base: PyModuleDef_HEAD_INIT, //! m_name: c_str!("string_sum").as_ptr(), //! m_doc: c_str!("A Python module written in Rust.").as_ptr(), //! m_size: 0, diff --git a/pyo3-ffi/src/moduleobject.rs b/pyo3-ffi/src/moduleobject.rs index bf04da660cf..b9026997d2e 100644 --- a/pyo3-ffi/src/moduleobject.rs +++ b/pyo3-ffi/src/moduleobject.rs @@ -59,14 +59,12 @@ pub struct PyModuleDef_Base { pub m_copy: *mut PyObject, } -pub const fn PyModuleDef_HEAD_INIT() -> PyModuleDef_Base { - PyModuleDef_Base { - ob_base: PyObject_HEAD_INIT(), - m_init: None, - m_index: 0, - m_copy: std::ptr::null_mut(), - } -} +pub const PyModuleDef_HEAD_INIT: PyModuleDef_Base = PyModuleDef_Base { + ob_base: PyObject_HEAD_INIT, + m_init: None, + m_index: 0, + m_copy: std::ptr::null_mut(), +}; #[repr(C)] #[derive(Copy, Clone, Eq, PartialEq)] diff --git a/pyo3-ffi/src/object.rs b/pyo3-ffi/src/object.rs index 9baebb8a51f..48aa1f31861 100644 --- a/pyo3-ffi/src/object.rs +++ b/pyo3-ffi/src/object.rs @@ -5,7 +5,7 @@ use std::mem; use std::os::raw::{c_char, c_int, c_uint, c_ulong, c_void}; use std::ptr; #[cfg(Py_GIL_DISABLED)] -use std::sync::atomic::{AtomicIsize, AtomicU32, Ordering::Relaxed}; +use std::sync::atomic::{AtomicIsize, AtomicU32, AtomicU8, Ordering::Relaxed}; #[cfg(Py_LIMITED_API)] opaque_struct!(PyTypeObject); @@ -31,33 +31,33 @@ pub const _Py_IMMORTAL_REFCNT_LOCAL: u32 = u32::MAX; #[cfg(Py_GIL_DISABLED)] pub const _Py_REF_SHARED_SHIFT: isize = 2; -pub const fn PyObject_HEAD_INIT() -> PyObject { - PyObject { - #[cfg(py_sys_config = "Py_TRACE_REFS")] - _ob_next: std::ptr::null_mut(), - #[cfg(py_sys_config = "Py_TRACE_REFS")] - _ob_prev: std::ptr::null_mut(), - #[cfg(Py_GIL_DISABLED)] - ob_tid: 0, - #[cfg(Py_GIL_DISABLED)] - _padding: 0, - #[cfg(Py_GIL_DISABLED)] - ob_mutex: unsafe { mem::zeroed::() }, - #[cfg(Py_GIL_DISABLED)] - ob_gc_bits: 0, - #[cfg(Py_GIL_DISABLED)] - ob_ref_local: AtomicU32::new(_Py_IMMORTAL_REFCNT_LOCAL), - #[cfg(Py_GIL_DISABLED)] - ob_ref_shared: AtomicIsize::new(0), - #[cfg(all(not(Py_GIL_DISABLED), Py_3_12))] - ob_refcnt: PyObjectObRefcnt { ob_refcnt: 1 }, - #[cfg(not(Py_3_12))] - ob_refcnt: 1, - #[cfg(PyPy)] - ob_pypy_link: 0, - ob_type: std::ptr::null_mut(), - } -} +pub const PyObject_HEAD_INIT: PyObject = PyObject { + #[cfg(py_sys_config = "Py_TRACE_REFS")] + _ob_next: std::ptr::null_mut(), + #[cfg(py_sys_config = "Py_TRACE_REFS")] + _ob_prev: std::ptr::null_mut(), + #[cfg(Py_GIL_DISABLED)] + ob_tid: 0, + #[cfg(Py_GIL_DISABLED)] + _padding: 0, + #[cfg(Py_GIL_DISABLED)] + ob_mutex: PyMutex { + _bits: AtomicU8::new(0), + }, + #[cfg(Py_GIL_DISABLED)] + ob_gc_bits: 0, + #[cfg(Py_GIL_DISABLED)] + ob_ref_local: AtomicU32::new(_Py_IMMORTAL_REFCNT_LOCAL), + #[cfg(Py_GIL_DISABLED)] + ob_ref_shared: AtomicIsize::new(0), + #[cfg(all(not(Py_GIL_DISABLED), Py_3_12))] + ob_refcnt: PyObjectObRefcnt { ob_refcnt: 1 }, + #[cfg(not(Py_3_12))] + ob_refcnt: 1, + #[cfg(PyPy)] + ob_pypy_link: 0, + ob_type: std::ptr::null_mut(), +}; // skipped PyObject_VAR_HEAD // skipped Py_INVALID_SIZE diff --git a/src/impl_/pymodule.rs b/src/impl_/pymodule.rs index a2325dc07fb..08d55bfa5e8 100644 --- a/src/impl_/pymodule.rs +++ b/src/impl_/pymodule.rs @@ -54,8 +54,8 @@ impl ModuleDef { doc: &'static CStr, initializer: ModuleInitializer, ) -> Self { - let init = ffi::PyModuleDef { - m_base: ffi::PyModuleDef_HEAD_INIT(), + const INIT: ffi::PyModuleDef = ffi::PyModuleDef { + m_base: ffi::PyModuleDef_HEAD_INIT, m_name: std::ptr::null(), m_doc: std::ptr::null(), m_size: 0, @@ -69,7 +69,7 @@ impl ModuleDef { let ffi_def = UnsafeCell::new(ffi::PyModuleDef { m_name: name.as_ptr(), m_doc: doc.as_ptr(), - ..init + ..INIT }); ModuleDef {