Skip to content

Commit

Permalink
fix case of gil-refs feature breaking create_exception! macro (#4589)
Browse files Browse the repository at this point in the history
* fix case of gil-refs feature breaking `create_exception!` macro

* remove `AsPyPointer` on non-gil-refs
  • Loading branch information
davidhewitt committed Oct 12, 2024
1 parent 9a641f7 commit d01fbab
Showing 1 changed file with 54 additions and 8 deletions.
62 changes: 54 additions & 8 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,24 @@ pub trait DerefToPyAny {
// Implementations core to all native types
#[doc(hidden)]
#[macro_export]
#[cfg_attr(docsrs, doc(cfg(all())))]
#[cfg(not(feature = "gil-refs"))]
macro_rules! pyobject_native_type_base(
// empty implementation on non-gil-refs
($name:ty $(;$generics:ident)* ) => {};
);

// Implementations core to all native types
#[doc(hidden)]
#[macro_export]
#[cfg_attr(docsrs, doc(cfg(all())))]
#[cfg(feature = "gil-refs")]
macro_rules! pyobject_native_type_base(
($name:ty $(;$generics:ident)* ) => {
#[cfg(feature = "gil-refs")]
unsafe impl<$($generics,)*> $crate::PyNativeType for $name {
type AsRefSource = Self;
}

#[cfg(feature = "gil-refs")]
impl<$($generics,)*> ::std::fmt::Debug for $name {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>)
-> ::std::result::Result<(), ::std::fmt::Error>
Expand All @@ -139,7 +149,6 @@ macro_rules! pyobject_native_type_base(
}
}

#[cfg(feature = "gil-refs")]
impl<$($generics,)*> ::std::fmt::Display for $name {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>)
-> ::std::result::Result<(), ::std::fmt::Error>
Expand All @@ -157,7 +166,6 @@ macro_rules! pyobject_native_type_base(
}
}

#[cfg(feature = "gil-refs")]
impl<$($generics,)*> $crate::ToPyObject for $name
{
#[inline]
Expand All @@ -172,6 +180,35 @@ macro_rules! pyobject_native_type_base(
// make sense on PyAny / have different implementations).
#[doc(hidden)]
#[macro_export]
#[cfg_attr(docsrs, doc(cfg(all())))]
#[cfg(not(feature = "gil-refs"))]
macro_rules! pyobject_native_type_named (
($name:ty $(;$generics:ident)*) => {

impl<$($generics,)*> ::std::convert::AsRef<$crate::PyAny> for $name {
#[inline]
fn as_ref(&self) -> &$crate::PyAny {
&self.0
}
}

impl<$($generics,)*> ::std::ops::Deref for $name {
type Target = $crate::PyAny;

#[inline]
fn deref(&self) -> &$crate::PyAny {
&self.0
}
}

impl $crate::types::DerefToPyAny for $name {}
};
);

#[doc(hidden)]
#[macro_export]
#[cfg_attr(docsrs, doc(cfg(all())))]
#[cfg(feature = "gil-refs")]
macro_rules! pyobject_native_type_named (
($name:ty $(;$generics:ident)*) => {
$crate::pyobject_native_type_base!($name $(;$generics)*);
Expand Down Expand Up @@ -202,7 +239,6 @@ macro_rules! pyobject_native_type_named (

// FIXME https://github.com/PyO3/pyo3/issues/3903
#[allow(unknown_lints, non_local_definitions)]
#[cfg(feature = "gil-refs")]
impl<$($generics,)*> $crate::IntoPy<$crate::Py<$name>> for &'_ $name {
#[inline]
fn into_py(self, py: $crate::Python<'_>) -> $crate::Py<$name> {
Expand All @@ -212,7 +248,6 @@ macro_rules! pyobject_native_type_named (

// FIXME https://github.com/PyO3/pyo3/issues/3903
#[allow(unknown_lints, non_local_definitions)]
#[cfg(feature = "gil-refs")]
impl<$($generics,)*> ::std::convert::From<&'_ $name> for $crate::Py<$name> {
#[inline]
fn from(other: &$name) -> Self {
Expand All @@ -223,7 +258,6 @@ macro_rules! pyobject_native_type_named (

// FIXME https://github.com/PyO3/pyo3/issues/3903
#[allow(unknown_lints, non_local_definitions)]
#[cfg(feature = "gil-refs")]
impl<'a, $($generics,)*> ::std::convert::From<&'a $name> for &'a $crate::PyAny {
fn from(ob: &'a $name) -> Self {
unsafe{&*(ob as *const $name as *const $crate::PyAny)}
Expand Down Expand Up @@ -279,11 +313,23 @@ macro_rules! pyobject_native_type_info(
// because rust-numpy has a special implementation.
#[doc(hidden)]
#[macro_export]
#[cfg_attr(docsrs, doc(cfg(all())))]
#[cfg(not(feature = "gil-refs"))]
macro_rules! pyobject_native_type_extract {
// no body for non-gil-refs
($name:ty $(;$generics:ident)*) => {};
}

// NOTE: This macro is not included in pyobject_native_type_base!
// because rust-numpy has a special implementation.
#[doc(hidden)]
#[macro_export]
#[cfg_attr(docsrs, doc(cfg(all())))]
#[cfg(feature = "gil-refs")]
macro_rules! pyobject_native_type_extract {
($name:ty $(;$generics:ident)*) => {
// FIXME https://github.com/PyO3/pyo3/issues/3903
#[allow(unknown_lints, non_local_definitions)]
#[cfg(feature = "gil-refs")]
impl<'py, $($generics,)*> $crate::FromPyObject<'py> for &'py $name {
#[inline]
fn extract_bound(obj: &$crate::Bound<'py, $crate::PyAny>) -> $crate::PyResult<Self> {
Expand Down

0 comments on commit d01fbab

Please sign in to comment.