Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix case of gil-refs feature breaking create_exception! macro #4589

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
Comment on lines +188 to +202
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these any useful without gil-refs?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, in general I think it's helpful to not have the structure of the type objects be constrained by AsRef / Deref.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see what you mean, I've duplicated too much here. Will fix later!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually we still have AsRef / Deref here in this macro on main, maybe we could remove later (i.e. in 0.23 or later) but I think we have to leave these here in this PR for the patch fix. AsPyPointer (just below) should be removed though, I pushed that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, removing them from main makes sense. I don't think these can ever be used now, because there is no way to construct a literal PyAny (or similar)


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
Loading