Merge pull request #2172 from mejrs/addr

Use addr_of_mut in pyo3-ffi
This commit is contained in:
Bruno Kolenbrander 2022-02-19 17:56:09 +01:00 committed by GitHub
commit 01788b7220
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
31 changed files with 115 additions and 81 deletions

View File

@ -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::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<()> { fn ensure_auto_initialize_ok(interpreter_config: &InterpreterConfig) -> Result<()> {
if cargo_env_var("CARGO_FEATURE_AUTO_INITIALIZE").is_some() { if cargo_env_var("CARGO_FEATURE_AUTO_INITIALIZE").is_some() {
@ -32,17 +32,6 @@ fn ensure_auto_initialize_ok(interpreter_config: &InterpreterConfig) -> Result<(
Ok(()) Ok(())
} }
fn rustc_minor_version() -> Option<u32> {
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. /// Prepares the PyO3 crate for compilation.
/// ///
/// This loads the config from pyo3-build-config and then makes some additional checks to improve UX /// 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(); interpreter_config.emit_pyo3_cfgs();
let rustc_minor_version = rustc_minor_version().unwrap_or(0);
ensure_auto_initialize_ok(interpreter_config)?; ensure_auto_initialize_ok(interpreter_config)?;
// Enable use of const generics on Rust 1.51 and greater // Emit cfgs like `addr_of` and `min_const_generics`
if rustc_minor_version >= 51 { print_feature_cfgs();
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");
}
Ok(()) Ok(())
} }

View File

@ -10,6 +10,7 @@ mod impl_;
#[cfg(feature = "resolve-config")] #[cfg(feature = "resolve-config")]
use std::io::Cursor; use std::io::Cursor;
use std::{env, process::Command};
#[cfg(feature = "resolve-config")] #[cfg(feature = "resolve-config")]
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
@ -115,6 +116,36 @@ fn abi3_config() -> InterpreterConfig {
interpreter_config 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<u32> {
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 /// Private exports used in PyO3's build.rs
/// ///
/// Please don't use these - they could change at any time. /// Please don't use these - they could change at any time.

View File

@ -1,5 +1,5 @@
use pyo3_build_config::{ use pyo3_build_config::{
bail, ensure, bail, ensure, print_feature_cfgs,
pyo3_build_script_impl::{ pyo3_build_script_impl::{
cargo_env_var, env_var, errors::Result, resolve_interpreter_config, InterpreterConfig, cargo_env_var, env_var, errors::Result, resolve_interpreter_config, InterpreterConfig,
PythonVersion, PythonVersion,
@ -100,6 +100,9 @@ fn configure_pyo3() -> Result<()> {
println!("{}", line); println!("{}", line);
} }
// Emit cfgs like `addr_of` and `min_const_generics`
print_feature_cfgs();
Ok(()) Ok(())
} }

View File

@ -10,7 +10,7 @@ extern "C" {
#[inline] #[inline]
pub unsafe fn PyBool_Check(op: *mut PyObject) -> c_int { 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"))] #[cfg_attr(windows, link(name = "pythonXY"))]
@ -23,12 +23,12 @@ extern "C" {
#[inline] #[inline]
pub unsafe fn Py_False() -> *mut PyObject { 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] #[inline]
pub unsafe fn Py_True() -> *mut PyObject { 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] #[inline]

View File

@ -12,12 +12,12 @@ extern "C" {
#[inline] #[inline]
pub unsafe fn PyByteArray_Check(op: *mut PyObject) -> c_int { 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] #[inline]
pub unsafe fn PyByteArray_CheckExact(op: *mut PyObject) -> c_int { 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" { extern "C" {

View File

@ -16,7 +16,7 @@ pub unsafe fn PyBytes_Check(op: *mut PyObject) -> c_int {
#[inline] #[inline]
pub unsafe fn PyBytes_CheckExact(op: *mut PyObject) -> c_int { 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" { extern "C" {

View File

@ -40,12 +40,12 @@ extern "C" {
#[inline] #[inline]
pub unsafe fn PyComplex_Check(op: *mut PyObject) -> c_int { 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] #[inline]
pub unsafe fn PyComplex_CheckExact(op: *mut PyObject) -> c_int { 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" { extern "C" {

View File

@ -12,17 +12,17 @@ extern "C" {
#[inline] #[inline]
pub unsafe fn PyContext_CheckExact(op: *mut PyObject) -> c_int { 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] #[inline]
pub unsafe fn PyContextVar_CheckExact(op: *mut PyObject) -> c_int { 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] #[inline]
pub unsafe fn PyContextToken_CheckExact(op: *mut PyObject) -> c_int { 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" { extern "C" {

View File

@ -84,7 +84,7 @@ extern "C" {
#[inline] #[inline]
#[cfg(not(PyPy))] #[cfg(not(PyPy))]
pub unsafe fn PyCode_Check(op: *mut PyObject) -> c_int { 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] #[inline]

View File

@ -60,7 +60,7 @@ extern "C" {
#[inline] #[inline]
pub unsafe fn PyFrame_Check(op: *mut PyObject) -> c_int { 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" { extern "C" {

View File

@ -15,7 +15,7 @@ pub unsafe fn PyDict_Check(op: *mut PyObject) -> c_int {
#[inline] #[inline]
pub unsafe fn PyDict_CheckExact(op: *mut PyObject) -> c_int { 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" { extern "C" {
@ -76,17 +76,17 @@ extern "C" {
#[inline] #[inline]
pub unsafe fn PyDictKeys_Check(op: *mut PyObject) -> c_int { 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] #[inline]
pub unsafe fn PyDictValues_Check(op: *mut PyObject) -> c_int { 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] #[inline]
pub unsafe fn PyDictItems_Check(op: *mut PyObject) -> c_int { 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] #[inline]

View File

@ -20,12 +20,12 @@ extern "C" {
#[inline] #[inline]
pub unsafe fn PyFloat_Check(op: *mut PyObject) -> c_int { 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] #[inline]
pub unsafe fn PyFloat_CheckExact(op: *mut PyObject) -> c_int { 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 // skipped Py_RETURN_NAN

View File

@ -12,7 +12,7 @@ extern "C" {
#[inline] #[inline]
pub unsafe fn PyFunction_Check(op: *mut PyObject) -> c_int { 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" { extern "C" {

View File

@ -25,12 +25,12 @@ extern "C" {
#[inline] #[inline]
pub unsafe fn PyGen_Check(op: *mut PyObject) -> c_int { 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] #[inline]
pub unsafe fn PyGen_CheckExact(op: *mut PyObject) -> c_int { 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" { extern "C" {
@ -55,7 +55,7 @@ extern "C" {
#[inline] #[inline]
pub unsafe fn PyCoro_CheckExact(op: *mut PyObject) -> c_int { 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 // skipped _PyCoro_GetAwaitableIter
@ -75,7 +75,7 @@ extern "C" {
#[inline] #[inline]
pub unsafe fn PyAsyncGen_CheckExact(op: *mut PyObject) -> c_int { 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 // skipped _PyAsyncGenValueWrapperNew

View File

@ -9,7 +9,7 @@ extern "C" {
#[inline] #[inline]
pub unsafe fn PySeqIter_Check(op: *mut PyObject) -> c_int { 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" { extern "C" {
@ -19,7 +19,7 @@ extern "C" {
#[inline] #[inline]
pub unsafe fn PyCallIter_Check(op: *mut PyObject) -> c_int { 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" { extern "C" {

View File

@ -263,6 +263,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::abstract_::*;
pub use self::bltinmodule::*; pub use self::bltinmodule::*;
pub use self::boolobject::*; pub use self::boolobject::*;

View File

@ -17,7 +17,7 @@ pub unsafe fn PyList_Check(op: *mut PyObject) -> c_int {
#[inline] #[inline]
pub unsafe fn PyList_CheckExact(op: *mut PyObject) -> c_int { 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" { extern "C" {

View File

@ -20,7 +20,7 @@ pub unsafe fn PyLong_Check(op: *mut PyObject) -> c_int {
#[inline] #[inline]
pub unsafe fn PyLong_CheckExact(op: *mut PyObject) -> c_int { 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" { extern "C" {

View File

@ -12,7 +12,7 @@ extern "C" {
#[inline] #[inline]
pub unsafe fn PyMemoryView_Check(op: *mut PyObject) -> c_int { 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 // skipped non-limited PyMemoryView_GET_BUFFER

View File

@ -13,19 +13,19 @@ extern "C" {
#[cfg(Py_3_9)] #[cfg(Py_3_9)]
#[inline] #[inline]
pub unsafe fn PyCFunction_CheckExact(op: *mut PyObject) -> c_int { 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)] #[cfg(Py_3_9)]
#[inline] #[inline]
pub unsafe fn PyCFunction_Check(op: *mut PyObject) -> c_int { 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))] #[cfg(not(Py_3_9))]
#[inline] #[inline]
pub unsafe fn PyCFunction_Check(op: *mut PyObject) -> c_int { 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 = pub type PyCFunction =

View File

@ -11,12 +11,12 @@ extern "C" {
#[inline] #[inline]
pub unsafe fn PyModule_Check(op: *mut PyObject) -> c_int { 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] #[inline]
pub unsafe fn PyModule_CheckExact(op: *mut PyObject) -> c_int { 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" { extern "C" {

View File

@ -412,7 +412,7 @@ pub unsafe fn Py_DECREF(op: *mut PyObject) {
} }
#[inline] #[inline]
pub unsafe fn Py_CLEAR(op: &mut *mut PyObject) { pub unsafe fn Py_CLEAR(op: *mut *mut PyObject) {
let tmp = *op; let tmp = *op;
if !tmp.is_null() { if !tmp.is_null() {
*op = ptr::null_mut(); *op = ptr::null_mut();
@ -470,7 +470,7 @@ extern "C" {
#[inline] #[inline]
pub unsafe fn Py_None() -> *mut PyObject { pub unsafe fn Py_None() -> *mut PyObject {
&mut _Py_NoneStruct addr_of_mut_shim!(_Py_NoneStruct)
} }
#[inline] #[inline]
@ -488,7 +488,7 @@ extern "C" {
#[inline] #[inline]
pub unsafe fn Py_NotImplemented() -> *mut PyObject { pub unsafe fn Py_NotImplemented() -> *mut PyObject {
&mut _Py_NotImplementedStruct addr_of_mut_shim!(_Py_NotImplementedStruct)
} }
// skipped Py_RETURN_NOTIMPLEMENTED // skipped Py_RETURN_NOTIMPLEMENTED
@ -536,5 +536,5 @@ pub unsafe fn PyType_Check(op: *mut PyObject) -> c_int {
#[inline] #[inline]
pub unsafe fn PyType_CheckExact(op: *mut PyObject) -> c_int { 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
} }

View File

@ -11,7 +11,7 @@ pub type PyCapsule_Destructor = unsafe extern "C" fn(o: *mut PyObject);
#[inline] #[inline]
pub unsafe fn PyCapsule_CheckExact(ob: *mut PyObject) -> c_int { 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" { extern "C" {

View File

@ -11,5 +11,5 @@ extern "C" {
#[inline] #[inline]
pub unsafe fn PyRange_Check(op: *mut PyObject) -> c_int { 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
} }

View File

@ -82,7 +82,7 @@ extern "C" {
#[inline] #[inline]
#[cfg(not(PyPy))] #[cfg(not(PyPy))]
pub unsafe fn PyFrozenSet_CheckExact(ob: *mut PyObject) -> c_int { 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" { extern "C" {
@ -94,8 +94,8 @@ extern "C" {
#[inline] #[inline]
#[cfg(not(PyPy))] #[cfg(not(PyPy))]
pub unsafe fn PyFrozenSet_Check(ob: *mut PyObject) -> c_int { pub unsafe fn PyFrozenSet_Check(ob: *mut PyObject) -> c_int {
(Py_TYPE(ob) == &mut PyFrozenSet_Type (Py_TYPE(ob) == addr_of_mut_shim!(PyFrozenSet_Type)
|| PyType_IsSubtype(Py_TYPE(ob), &mut PyFrozenSet_Type) != 0) as c_int || PyType_IsSubtype(Py_TYPE(ob), addr_of_mut_shim!(PyFrozenSet_Type)) != 0) as c_int
} }
extern "C" { extern "C" {
@ -107,20 +107,21 @@ extern "C" {
#[inline] #[inline]
#[cfg(not(PyPy))] #[cfg(not(PyPy))]
pub unsafe fn PyAnySet_CheckExact(ob: *mut PyObject) -> c_int { 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] #[inline]
pub unsafe fn PyAnySet_Check(ob: *mut PyObject) -> c_int { pub unsafe fn PyAnySet_Check(ob: *mut PyObject) -> c_int {
(PyAnySet_CheckExact(ob) != 0 (PyAnySet_CheckExact(ob) != 0
|| PyType_IsSubtype(Py_TYPE(ob), &mut PySet_Type) != 0 || PyType_IsSubtype(Py_TYPE(ob), addr_of_mut_shim!(PySet_Type)) != 0
|| PyType_IsSubtype(Py_TYPE(ob), &mut PyFrozenSet_Type) != 0) as c_int || PyType_IsSubtype(Py_TYPE(ob), addr_of_mut_shim!(PyFrozenSet_Type)) != 0) as c_int
} }
#[inline] #[inline]
#[cfg(Py_3_10)] #[cfg(Py_3_10)]
pub unsafe fn PySet_CheckExact(op: *mut PyObject) -> c_int { 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" { extern "C" {
@ -132,5 +133,6 @@ extern "C" {
#[inline] #[inline]
#[cfg(not(PyPy))] #[cfg(not(PyPy))]
pub unsafe fn PySet_Check(ob: *mut PyObject) -> c_int { 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
} }

View File

@ -9,7 +9,7 @@ extern "C" {
#[inline] #[inline]
pub unsafe fn Py_Ellipsis() -> *mut PyObject { pub unsafe fn Py_Ellipsis() -> *mut PyObject {
&mut _Py_EllipsisObject addr_of_mut_shim!(_Py_EllipsisObject)
} }
#[cfg(not(Py_LIMITED_API))] #[cfg(not(Py_LIMITED_API))]
@ -30,7 +30,7 @@ extern "C" {
#[inline] #[inline]
pub unsafe fn PySlice_Check(op: *mut PyObject) -> c_int { 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" { extern "C" {

View File

@ -21,5 +21,5 @@ extern "C" {
#[inline] #[inline]
#[cfg(not(PyPy))] #[cfg(not(PyPy))]
pub unsafe fn PyTraceBack_Check(op: *mut PyObject) -> c_int { 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
} }

View File

@ -16,7 +16,7 @@ pub unsafe fn PyTuple_Check(op: *mut PyObject) -> c_int {
#[inline] #[inline]
pub unsafe fn PyTuple_CheckExact(op: *mut PyObject) -> c_int { 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" { extern "C" {

View File

@ -34,7 +34,7 @@ pub unsafe fn PyUnicode_Check(op: *mut PyObject) -> c_int {
#[inline] #[inline]
#[cfg(not(PyPy))] #[cfg(not(PyPy))]
pub unsafe fn PyUnicode_CheckExact(op: *mut PyObject) -> c_int { 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; pub const Py_UNICODE_REPLACEMENT_CHARACTER: Py_UCS4 = 0xFFFD;

View File

@ -24,20 +24,20 @@ extern "C" {
#[inline] #[inline]
#[cfg(not(PyPy))] #[cfg(not(PyPy))]
pub unsafe fn PyWeakref_CheckRef(op: *mut PyObject) -> c_int { 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] #[inline]
#[cfg(not(PyPy))] #[cfg(not(PyPy))]
pub unsafe fn PyWeakref_CheckRefExact(op: *mut PyObject) -> c_int { 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] #[inline]
#[cfg(not(PyPy))] #[cfg(not(PyPy))]
pub unsafe fn PyWeakref_CheckProxy(op: *mut PyObject) -> c_int { pub unsafe fn PyWeakref_CheckProxy(op: *mut PyObject) -> c_int {
((Py_TYPE(op) == &mut _PyWeakref_ProxyType) ((Py_TYPE(op) == addr_of_mut_shim!(_PyWeakref_ProxyType))
|| (Py_TYPE(op) == &mut _PyWeakref_CallableProxyType)) as c_int || (Py_TYPE(op) == addr_of_mut_shim!(_PyWeakref_CallableProxyType))) as c_int
} }
#[inline] #[inline]

View File

@ -148,7 +148,11 @@ macro_rules! pyobject_native_type_info(
// Create a very short lived mutable reference and directly // Create a very short lived mutable reference and directly
// cast it to a pointer: no mutable references can be aliasing // cast it to a pointer: no mutable references can be aliasing
// because we hold the GIL. // because we hold the GIL.
#[cfg(not(addr_of))]
unsafe { &mut $typeobject } unsafe { &mut $typeobject }
#[cfg(addr_of)]
unsafe { ::std::ptr::addr_of_mut!($typeobject) }
} }
$( $(