diff --git a/build.rs b/build.rs index cfaf06f6e9b..78cebf9a0d0 100644 --- a/build.rs +++ b/build.rs @@ -1,7 +1,7 @@ -use std::{env, process::Command}; +use std::env; use pyo3_build_config::pyo3_build_script_impl::{cargo_env_var, errors::Result}; -use pyo3_build_config::{bail, InterpreterConfig}; +use pyo3_build_config::{bail, print_feature_cfgs, InterpreterConfig}; fn ensure_auto_initialize_ok(interpreter_config: &InterpreterConfig) -> Result<()> { if cargo_env_var("CARGO_FEATURE_AUTO_INITIALIZE").is_some() { @@ -32,17 +32,6 @@ fn ensure_auto_initialize_ok(interpreter_config: &InterpreterConfig) -> Result<( Ok(()) } -fn rustc_minor_version() -> Option { - let rustc = env::var_os("RUSTC")?; - let output = Command::new(rustc).arg("--version").output().ok()?; - let version = core::str::from_utf8(&output.stdout).ok()?; - let mut pieces = version.split('.'); - if pieces.next() != Some("rustc 1") { - return None; - } - pieces.next()?.parse().ok() -} - /// Prepares the PyO3 crate for compilation. /// /// This loads the config from pyo3-build-config and then makes some additional checks to improve UX @@ -55,18 +44,10 @@ fn configure_pyo3() -> Result<()> { interpreter_config.emit_pyo3_cfgs(); - let rustc_minor_version = rustc_minor_version().unwrap_or(0); ensure_auto_initialize_ok(interpreter_config)?; - // Enable use of const generics on Rust 1.51 and greater - if rustc_minor_version >= 51 { - println!("cargo:rustc-cfg=min_const_generics"); - } - - // Enable use of std::ptr::addr_of! on Rust 1.51 and greater - if rustc_minor_version >= 51 { - println!("cargo:rustc-cfg=addr_of"); - } + // Emit cfgs like `addr_of` and `min_const_generics` + print_feature_cfgs(); Ok(()) } diff --git a/pyo3-build-config/src/lib.rs b/pyo3-build-config/src/lib.rs index 0a4a147c9ba..5a89ad9d3d3 100644 --- a/pyo3-build-config/src/lib.rs +++ b/pyo3-build-config/src/lib.rs @@ -10,6 +10,7 @@ mod impl_; #[cfg(feature = "resolve-config")] use std::io::Cursor; +use std::{env, process::Command}; #[cfg(feature = "resolve-config")] use once_cell::sync::OnceCell; @@ -115,6 +116,36 @@ fn abi3_config() -> InterpreterConfig { interpreter_config } +/// Use certain features if we detect the compiler being used supports them. +/// +/// Features may be removed or added as MSRV gets bumped or new features become available, +/// so this function is unstable. +#[doc(hidden)] +pub fn print_feature_cfgs() { + fn rustc_minor_version() -> Option { + let rustc = env::var_os("RUSTC")?; + let output = Command::new(rustc).arg("--version").output().ok()?; + let version = core::str::from_utf8(&output.stdout).ok()?; + let mut pieces = version.split('.'); + if pieces.next() != Some("rustc 1") { + return None; + } + pieces.next()?.parse().ok() + } + + let rustc_minor_version = rustc_minor_version().unwrap_or(0); + + // Enable use of const generics on Rust 1.51 and greater + if rustc_minor_version >= 51 { + println!("cargo:rustc-cfg=min_const_generics"); + } + + // Enable use of std::ptr::addr_of! on Rust 1.51 and greater + if rustc_minor_version >= 51 { + println!("cargo:rustc-cfg=addr_of"); + } +} + /// Private exports used in PyO3's build.rs /// /// Please don't use these - they could change at any time. diff --git a/pyo3-ffi/build.rs b/pyo3-ffi/build.rs index e72cfb97db5..c8d65d5d1c2 100644 --- a/pyo3-ffi/build.rs +++ b/pyo3-ffi/build.rs @@ -1,5 +1,5 @@ use pyo3_build_config::{ - bail, ensure, + bail, ensure, print_feature_cfgs, pyo3_build_script_impl::{ cargo_env_var, env_var, errors::Result, resolve_interpreter_config, InterpreterConfig, PythonVersion, @@ -100,6 +100,9 @@ fn configure_pyo3() -> Result<()> { println!("{}", line); } + // Emit cfgs like `addr_of` and `min_const_generics` + print_feature_cfgs(); + Ok(()) } diff --git a/pyo3-ffi/src/boolobject.rs b/pyo3-ffi/src/boolobject.rs index 2b72db3f746..17af974b04a 100644 --- a/pyo3-ffi/src/boolobject.rs +++ b/pyo3-ffi/src/boolobject.rs @@ -10,7 +10,7 @@ extern "C" { #[inline] pub unsafe fn PyBool_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == &mut PyBool_Type) as c_int + (Py_TYPE(op) == addr_of_mut_shim!(PyBool_Type)) as c_int } #[cfg_attr(windows, link(name = "pythonXY"))] @@ -23,12 +23,12 @@ extern "C" { #[inline] pub unsafe fn Py_False() -> *mut PyObject { - &mut _Py_FalseStruct as *mut PyLongObject as *mut PyObject + addr_of_mut_shim!(_Py_FalseStruct) as *mut PyLongObject as *mut PyObject } #[inline] pub unsafe fn Py_True() -> *mut PyObject { - &mut _Py_TrueStruct as *mut PyLongObject as *mut PyObject + addr_of_mut_shim!(_Py_TrueStruct) as *mut PyLongObject as *mut PyObject } #[inline] diff --git a/pyo3-ffi/src/bytearrayobject.rs b/pyo3-ffi/src/bytearrayobject.rs index d8df2d6a87b..7afa7801ac2 100644 --- a/pyo3-ffi/src/bytearrayobject.rs +++ b/pyo3-ffi/src/bytearrayobject.rs @@ -12,12 +12,12 @@ extern "C" { #[inline] pub unsafe fn PyByteArray_Check(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, &mut PyByteArray_Type) + PyObject_TypeCheck(op, addr_of_mut_shim!(PyByteArray_Type)) } #[inline] pub unsafe fn PyByteArray_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == &mut PyByteArray_Type) as c_int + (Py_TYPE(op) == addr_of_mut_shim!(PyByteArray_Type)) as c_int } extern "C" { diff --git a/pyo3-ffi/src/bytesobject.rs b/pyo3-ffi/src/bytesobject.rs index 058f402c6c3..8cdcd7df4da 100644 --- a/pyo3-ffi/src/bytesobject.rs +++ b/pyo3-ffi/src/bytesobject.rs @@ -16,7 +16,7 @@ pub unsafe fn PyBytes_Check(op: *mut PyObject) -> c_int { #[inline] pub unsafe fn PyBytes_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == &mut PyBytes_Type) as c_int + (Py_TYPE(op) == addr_of_mut_shim!(PyBytes_Type)) as c_int } extern "C" { diff --git a/pyo3-ffi/src/complexobject.rs b/pyo3-ffi/src/complexobject.rs index 829adf82783..83b7620ac28 100644 --- a/pyo3-ffi/src/complexobject.rs +++ b/pyo3-ffi/src/complexobject.rs @@ -40,12 +40,12 @@ extern "C" { #[inline] pub unsafe fn PyComplex_Check(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, &mut PyComplex_Type) + PyObject_TypeCheck(op, addr_of_mut_shim!(PyComplex_Type)) } #[inline] pub unsafe fn PyComplex_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == &mut PyComplex_Type) as c_int + (Py_TYPE(op) == addr_of_mut_shim!(PyComplex_Type)) as c_int } extern "C" { diff --git a/pyo3-ffi/src/context.rs b/pyo3-ffi/src/context.rs index d720cbe077d..a4ee09e3c7d 100644 --- a/pyo3-ffi/src/context.rs +++ b/pyo3-ffi/src/context.rs @@ -12,17 +12,17 @@ extern "C" { #[inline] pub unsafe fn PyContext_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == &mut PyContext_Type) as c_int + (Py_TYPE(op) == addr_of_mut_shim!(PyContext_Type)) as c_int } #[inline] pub unsafe fn PyContextVar_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == &mut PyContextVar_Type) as c_int + (Py_TYPE(op) == addr_of_mut_shim!(PyContextVar_Type)) as c_int } #[inline] pub unsafe fn PyContextToken_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == &mut PyContextToken_Type) as c_int + (Py_TYPE(op) == addr_of_mut_shim!(PyContextToken_Type)) as c_int } extern "C" { diff --git a/pyo3-ffi/src/cpython/code.rs b/pyo3-ffi/src/cpython/code.rs index 9365785508f..760afb7149d 100644 --- a/pyo3-ffi/src/cpython/code.rs +++ b/pyo3-ffi/src/cpython/code.rs @@ -84,7 +84,7 @@ extern "C" { #[inline] #[cfg(not(PyPy))] pub unsafe fn PyCode_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == &mut PyCode_Type) as c_int + (Py_TYPE(op) == addr_of_mut_shim!(PyCode_Type)) as c_int } #[inline] diff --git a/pyo3-ffi/src/cpython/frameobject.rs b/pyo3-ffi/src/cpython/frameobject.rs index 53d2d494531..b51a10cba13 100644 --- a/pyo3-ffi/src/cpython/frameobject.rs +++ b/pyo3-ffi/src/cpython/frameobject.rs @@ -60,7 +60,7 @@ extern "C" { #[inline] pub unsafe fn PyFrame_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == &mut PyFrame_Type) as c_int + (Py_TYPE(op) == addr_of_mut_shim!(PyFrame_Type)) as c_int } extern "C" { diff --git a/pyo3-ffi/src/dictobject.rs b/pyo3-ffi/src/dictobject.rs index c4cb719375d..b03fbb303a8 100644 --- a/pyo3-ffi/src/dictobject.rs +++ b/pyo3-ffi/src/dictobject.rs @@ -15,7 +15,7 @@ pub unsafe fn PyDict_Check(op: *mut PyObject) -> c_int { #[inline] pub unsafe fn PyDict_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == &mut PyDict_Type) as c_int + (Py_TYPE(op) == addr_of_mut_shim!(PyDict_Type)) as c_int } extern "C" { @@ -76,17 +76,17 @@ extern "C" { #[inline] pub unsafe fn PyDictKeys_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == &mut PyDictKeys_Type) as c_int + (Py_TYPE(op) == addr_of_mut_shim!(PyDictKeys_Type)) as c_int } #[inline] pub unsafe fn PyDictValues_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == &mut PyDictValues_Type) as c_int + (Py_TYPE(op) == addr_of_mut_shim!(PyDictValues_Type)) as c_int } #[inline] pub unsafe fn PyDictItems_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == &mut PyDictItems_Type) as c_int + (Py_TYPE(op) == addr_of_mut_shim!(PyDictItems_Type)) as c_int } #[inline] diff --git a/pyo3-ffi/src/floatobject.rs b/pyo3-ffi/src/floatobject.rs index 476f2145fa0..15f71ba2e72 100644 --- a/pyo3-ffi/src/floatobject.rs +++ b/pyo3-ffi/src/floatobject.rs @@ -20,12 +20,12 @@ extern "C" { #[inline] pub unsafe fn PyFloat_Check(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, &mut PyFloat_Type) + PyObject_TypeCheck(op, addr_of_mut_shim!(PyFloat_Type)) } #[inline] pub unsafe fn PyFloat_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == &mut PyFloat_Type) as c_int + (Py_TYPE(op) == addr_of_mut_shim!(PyFloat_Type)) as c_int } // skipped Py_RETURN_NAN diff --git a/pyo3-ffi/src/funcobject.rs b/pyo3-ffi/src/funcobject.rs index 020d2b49470..f3f067e5732 100644 --- a/pyo3-ffi/src/funcobject.rs +++ b/pyo3-ffi/src/funcobject.rs @@ -12,7 +12,7 @@ extern "C" { #[inline] pub unsafe fn PyFunction_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == &mut PyFunction_Type) as c_int + (Py_TYPE(op) == addr_of_mut_shim!(PyFunction_Type)) as c_int } extern "C" { diff --git a/pyo3-ffi/src/genobject.rs b/pyo3-ffi/src/genobject.rs index 054104de906..c13edb079cb 100644 --- a/pyo3-ffi/src/genobject.rs +++ b/pyo3-ffi/src/genobject.rs @@ -25,12 +25,12 @@ extern "C" { #[inline] pub unsafe fn PyGen_Check(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, &mut PyGen_Type) + PyObject_TypeCheck(op, addr_of_mut_shim!(PyGen_Type)) } #[inline] pub unsafe fn PyGen_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == &mut PyGen_Type) as c_int + (Py_TYPE(op) == addr_of_mut_shim!(PyGen_Type)) as c_int } extern "C" { @@ -55,7 +55,7 @@ extern "C" { #[inline] pub unsafe fn PyCoro_CheckExact(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, &mut PyCoro_Type) + PyObject_TypeCheck(op, addr_of_mut_shim!(PyCoro_Type)) } // skipped _PyCoro_GetAwaitableIter @@ -75,7 +75,7 @@ extern "C" { #[inline] pub unsafe fn PyAsyncGen_CheckExact(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, &mut PyAsyncGen_Type) + PyObject_TypeCheck(op, addr_of_mut_shim!(PyAsyncGen_Type)) } // skipped _PyAsyncGenValueWrapperNew diff --git a/pyo3-ffi/src/iterobject.rs b/pyo3-ffi/src/iterobject.rs index f83e30bb5f5..657409c7265 100644 --- a/pyo3-ffi/src/iterobject.rs +++ b/pyo3-ffi/src/iterobject.rs @@ -9,7 +9,7 @@ extern "C" { #[inline] pub unsafe fn PySeqIter_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == &mut PySeqIter_Type) as c_int + (Py_TYPE(op) == addr_of_mut_shim!(PySeqIter_Type)) as c_int } extern "C" { @@ -19,7 +19,7 @@ extern "C" { #[inline] pub unsafe fn PyCallIter_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == &mut PyCallIter_Type) as c_int + (Py_TYPE(op) == addr_of_mut_shim!(PyCallIter_Type)) as c_int } extern "C" { diff --git a/pyo3-ffi/src/lib.rs b/pyo3-ffi/src/lib.rs index 3564604c2ed..f43c708ab2a 100644 --- a/pyo3-ffi/src/lib.rs +++ b/pyo3-ffi/src/lib.rs @@ -265,6 +265,19 @@ macro_rules! opaque_struct { }; } +macro_rules! addr_of_mut_shim { + ($place:expr) => {{ + #[cfg(addr_of)] + { + ::std::ptr::addr_of_mut!($place) + } + #[cfg(not(addr_of))] + { + &mut $place as *mut _ + } + }}; +} + pub use self::abstract_::*; pub use self::bltinmodule::*; pub use self::boolobject::*; diff --git a/pyo3-ffi/src/listobject.rs b/pyo3-ffi/src/listobject.rs index c45b1fcbbf9..9b10a9174d0 100644 --- a/pyo3-ffi/src/listobject.rs +++ b/pyo3-ffi/src/listobject.rs @@ -17,7 +17,7 @@ pub unsafe fn PyList_Check(op: *mut PyObject) -> c_int { #[inline] pub unsafe fn PyList_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == &mut PyList_Type) as c_int + (Py_TYPE(op) == addr_of_mut_shim!(PyList_Type)) as c_int } extern "C" { diff --git a/pyo3-ffi/src/longobject.rs b/pyo3-ffi/src/longobject.rs index f44115cf2c4..2fc64f8bd8c 100644 --- a/pyo3-ffi/src/longobject.rs +++ b/pyo3-ffi/src/longobject.rs @@ -20,7 +20,7 @@ pub unsafe fn PyLong_Check(op: *mut PyObject) -> c_int { #[inline] pub unsafe fn PyLong_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == &mut PyLong_Type) as c_int + (Py_TYPE(op) == addr_of_mut_shim!(PyLong_Type)) as c_int } extern "C" { diff --git a/pyo3-ffi/src/memoryobject.rs b/pyo3-ffi/src/memoryobject.rs index 1bebc881cce..a6569003301 100644 --- a/pyo3-ffi/src/memoryobject.rs +++ b/pyo3-ffi/src/memoryobject.rs @@ -12,7 +12,7 @@ extern "C" { #[inline] pub unsafe fn PyMemoryView_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == &mut PyMemoryView_Type) as c_int + (Py_TYPE(op) == addr_of_mut_shim!(PyMemoryView_Type)) as c_int } // skipped non-limited PyMemoryView_GET_BUFFER diff --git a/pyo3-ffi/src/methodobject.rs b/pyo3-ffi/src/methodobject.rs index 6212090febb..3168fb02cdf 100644 --- a/pyo3-ffi/src/methodobject.rs +++ b/pyo3-ffi/src/methodobject.rs @@ -13,19 +13,19 @@ extern "C" { #[cfg(Py_3_9)] #[inline] pub unsafe fn PyCFunction_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == &mut PyCFunction_Type) as c_int + (Py_TYPE(op) == addr_of_mut_shim!(PyCFunction_Type)) as c_int } #[cfg(Py_3_9)] #[inline] pub unsafe fn PyCFunction_Check(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, &mut PyCFunction_Type) + PyObject_TypeCheck(op, addr_of_mut_shim!(PyCFunction_Type)) } #[cfg(not(Py_3_9))] #[inline] pub unsafe fn PyCFunction_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == &mut PyCFunction_Type) as c_int + (Py_TYPE(op) == addr_of_mut_shim!(PyCFunction_Type)) as c_int } pub type PyCFunction = diff --git a/pyo3-ffi/src/moduleobject.rs b/pyo3-ffi/src/moduleobject.rs index 94939943ba5..63d9f44c61f 100644 --- a/pyo3-ffi/src/moduleobject.rs +++ b/pyo3-ffi/src/moduleobject.rs @@ -11,12 +11,12 @@ extern "C" { #[inline] pub unsafe fn PyModule_Check(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, &mut PyModule_Type) + PyObject_TypeCheck(op, addr_of_mut_shim!(PyModule_Type)) } #[inline] pub unsafe fn PyModule_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == &mut PyModule_Type) as c_int + (Py_TYPE(op) == addr_of_mut_shim!(PyModule_Type)) as c_int } extern "C" { diff --git a/pyo3-ffi/src/object.rs b/pyo3-ffi/src/object.rs index 64d4eec8c72..a0333046bba 100644 --- a/pyo3-ffi/src/object.rs +++ b/pyo3-ffi/src/object.rs @@ -412,7 +412,7 @@ pub unsafe fn Py_DECREF(op: *mut PyObject) { } #[inline] -pub unsafe fn Py_CLEAR(op: &mut *mut PyObject) { +pub unsafe fn Py_CLEAR(op: *mut *mut PyObject) { let tmp = *op; if !tmp.is_null() { *op = ptr::null_mut(); @@ -470,7 +470,7 @@ extern "C" { #[inline] pub unsafe fn Py_None() -> *mut PyObject { - &mut _Py_NoneStruct + addr_of_mut_shim!(_Py_NoneStruct) } #[inline] @@ -488,7 +488,7 @@ extern "C" { #[inline] pub unsafe fn Py_NotImplemented() -> *mut PyObject { - &mut _Py_NotImplementedStruct + addr_of_mut_shim!(_Py_NotImplementedStruct) } // skipped Py_RETURN_NOTIMPLEMENTED @@ -536,5 +536,5 @@ pub unsafe fn PyType_Check(op: *mut PyObject) -> c_int { #[inline] pub unsafe fn PyType_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == &mut PyType_Type) as c_int + (Py_TYPE(op) == addr_of_mut_shim!(PyType_Type)) as c_int } diff --git a/pyo3-ffi/src/pycapsule.rs b/pyo3-ffi/src/pycapsule.rs index 0e15d13849f..382d7eb8e5f 100644 --- a/pyo3-ffi/src/pycapsule.rs +++ b/pyo3-ffi/src/pycapsule.rs @@ -11,7 +11,7 @@ pub type PyCapsule_Destructor = unsafe extern "C" fn(o: *mut PyObject); #[inline] pub unsafe fn PyCapsule_CheckExact(ob: *mut PyObject) -> c_int { - (Py_TYPE(ob) == &mut PyCapsule_Type) as c_int + (Py_TYPE(ob) == addr_of_mut_shim!(PyCapsule_Type)) as c_int } extern "C" { diff --git a/pyo3-ffi/src/rangeobject.rs b/pyo3-ffi/src/rangeobject.rs index af2d3d0b45f..be617377e39 100644 --- a/pyo3-ffi/src/rangeobject.rs +++ b/pyo3-ffi/src/rangeobject.rs @@ -11,5 +11,5 @@ extern "C" { #[inline] pub unsafe fn PyRange_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == &mut PyRange_Type) as c_int + (Py_TYPE(op) == addr_of_mut_shim!(PyRange_Type)) as c_int } diff --git a/pyo3-ffi/src/setobject.rs b/pyo3-ffi/src/setobject.rs index b5e4859274f..c90d26f5d65 100644 --- a/pyo3-ffi/src/setobject.rs +++ b/pyo3-ffi/src/setobject.rs @@ -82,7 +82,7 @@ extern "C" { #[inline] #[cfg(not(PyPy))] pub unsafe fn PyFrozenSet_CheckExact(ob: *mut PyObject) -> c_int { - (Py_TYPE(ob) == &mut PyFrozenSet_Type) as c_int + (Py_TYPE(ob) == addr_of_mut_shim!(PyFrozenSet_Type)) as c_int } extern "C" { @@ -94,8 +94,8 @@ extern "C" { #[inline] #[cfg(not(PyPy))] pub unsafe fn PyFrozenSet_Check(ob: *mut PyObject) -> c_int { - (Py_TYPE(ob) == &mut PyFrozenSet_Type - || PyType_IsSubtype(Py_TYPE(ob), &mut PyFrozenSet_Type) != 0) as c_int + (Py_TYPE(ob) == addr_of_mut_shim!(PyFrozenSet_Type) + || PyType_IsSubtype(Py_TYPE(ob), addr_of_mut_shim!(PyFrozenSet_Type)) != 0) as c_int } extern "C" { @@ -107,20 +107,21 @@ extern "C" { #[inline] #[cfg(not(PyPy))] pub unsafe fn PyAnySet_CheckExact(ob: *mut PyObject) -> c_int { - (Py_TYPE(ob) == &mut PySet_Type || Py_TYPE(ob) == &mut PyFrozenSet_Type) as c_int + (Py_TYPE(ob) == addr_of_mut_shim!(PySet_Type) + || Py_TYPE(ob) == addr_of_mut_shim!(PyFrozenSet_Type)) as c_int } #[inline] pub unsafe fn PyAnySet_Check(ob: *mut PyObject) -> c_int { (PyAnySet_CheckExact(ob) != 0 - || PyType_IsSubtype(Py_TYPE(ob), &mut PySet_Type) != 0 - || PyType_IsSubtype(Py_TYPE(ob), &mut PyFrozenSet_Type) != 0) as c_int + || PyType_IsSubtype(Py_TYPE(ob), addr_of_mut_shim!(PySet_Type)) != 0 + || PyType_IsSubtype(Py_TYPE(ob), addr_of_mut_shim!(PyFrozenSet_Type)) != 0) as c_int } #[inline] #[cfg(Py_3_10)] pub unsafe fn PySet_CheckExact(op: *mut PyObject) -> c_int { - crate::Py_IS_TYPE(op, &mut PySet_Type) + crate::Py_IS_TYPE(op, addr_of_mut_shim!(PySet_Type)) } extern "C" { @@ -132,5 +133,6 @@ extern "C" { #[inline] #[cfg(not(PyPy))] pub unsafe fn PySet_Check(ob: *mut PyObject) -> c_int { - (Py_TYPE(ob) == &mut PySet_Type || PyType_IsSubtype(Py_TYPE(ob), &mut PySet_Type) != 0) as c_int + (Py_TYPE(ob) == addr_of_mut_shim!(PySet_Type) + || PyType_IsSubtype(Py_TYPE(ob), addr_of_mut_shim!(PySet_Type)) != 0) as c_int } diff --git a/pyo3-ffi/src/sliceobject.rs b/pyo3-ffi/src/sliceobject.rs index 036b4c7d6d2..5f3138ef370 100644 --- a/pyo3-ffi/src/sliceobject.rs +++ b/pyo3-ffi/src/sliceobject.rs @@ -9,7 +9,7 @@ extern "C" { #[inline] pub unsafe fn Py_Ellipsis() -> *mut PyObject { - &mut _Py_EllipsisObject + addr_of_mut_shim!(_Py_EllipsisObject) } #[cfg(not(Py_LIMITED_API))] @@ -30,7 +30,7 @@ extern "C" { #[inline] pub unsafe fn PySlice_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == &mut PySlice_Type) as c_int + (Py_TYPE(op) == addr_of_mut_shim!(PySlice_Type)) as c_int } extern "C" { diff --git a/pyo3-ffi/src/traceback.rs b/pyo3-ffi/src/traceback.rs index f9398fb69fb..b3a2584413a 100644 --- a/pyo3-ffi/src/traceback.rs +++ b/pyo3-ffi/src/traceback.rs @@ -21,5 +21,5 @@ extern "C" { #[inline] #[cfg(not(PyPy))] pub unsafe fn PyTraceBack_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == &mut PyTraceBack_Type) as c_int + (Py_TYPE(op) == addr_of_mut_shim!(PyTraceBack_Type)) as c_int } diff --git a/pyo3-ffi/src/tupleobject.rs b/pyo3-ffi/src/tupleobject.rs index e4f4b9ea455..de3c4c5460a 100644 --- a/pyo3-ffi/src/tupleobject.rs +++ b/pyo3-ffi/src/tupleobject.rs @@ -16,7 +16,7 @@ pub unsafe fn PyTuple_Check(op: *mut PyObject) -> c_int { #[inline] pub unsafe fn PyTuple_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == &mut PyTuple_Type) as c_int + (Py_TYPE(op) == addr_of_mut_shim!(PyTuple_Type)) as c_int } extern "C" { diff --git a/pyo3-ffi/src/unicodeobject.rs b/pyo3-ffi/src/unicodeobject.rs index 5dd2b3cbb63..5841fa9e194 100644 --- a/pyo3-ffi/src/unicodeobject.rs +++ b/pyo3-ffi/src/unicodeobject.rs @@ -34,7 +34,7 @@ pub unsafe fn PyUnicode_Check(op: *mut PyObject) -> c_int { #[inline] #[cfg(not(PyPy))] pub unsafe fn PyUnicode_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == &mut PyUnicode_Type) as c_int + (Py_TYPE(op) == addr_of_mut_shim!(PyUnicode_Type)) as c_int } pub const Py_UNICODE_REPLACEMENT_CHARACTER: Py_UCS4 = 0xFFFD; diff --git a/pyo3-ffi/src/weakrefobject.rs b/pyo3-ffi/src/weakrefobject.rs index 575bc495de0..b9506115a4b 100644 --- a/pyo3-ffi/src/weakrefobject.rs +++ b/pyo3-ffi/src/weakrefobject.rs @@ -24,20 +24,20 @@ extern "C" { #[inline] #[cfg(not(PyPy))] pub unsafe fn PyWeakref_CheckRef(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, &mut _PyWeakref_RefType) + PyObject_TypeCheck(op, addr_of_mut_shim!(_PyWeakref_RefType)) } #[inline] #[cfg(not(PyPy))] pub unsafe fn PyWeakref_CheckRefExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == &mut _PyWeakref_RefType) as c_int + (Py_TYPE(op) == addr_of_mut_shim!(_PyWeakref_RefType)) as c_int } #[inline] #[cfg(not(PyPy))] pub unsafe fn PyWeakref_CheckProxy(op: *mut PyObject) -> c_int { - ((Py_TYPE(op) == &mut _PyWeakref_ProxyType) - || (Py_TYPE(op) == &mut _PyWeakref_CallableProxyType)) as c_int + ((Py_TYPE(op) == addr_of_mut_shim!(_PyWeakref_ProxyType)) + || (Py_TYPE(op) == addr_of_mut_shim!(_PyWeakref_CallableProxyType))) as c_int } #[inline] diff --git a/src/types/mod.rs b/src/types/mod.rs index 1058a16a05b..b85c31af9a5 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -148,7 +148,11 @@ macro_rules! pyobject_native_type_info( // Create a very short lived mutable reference and directly // cast it to a pointer: no mutable references can be aliasing // because we hold the GIL. + #[cfg(not(addr_of))] unsafe { &mut $typeobject } + + #[cfg(addr_of)] + unsafe { ::std::ptr::addr_of_mut!($typeobject) } } $(