Skip to content

Commit d01fbab

Browse files
committed
fix case of gil-refs feature breaking create_exception! macro (#4589)
* fix case of gil-refs feature breaking `create_exception!` macro * remove `AsPyPointer` on non-gil-refs
1 parent 9a641f7 commit d01fbab

File tree

1 file changed

+54
-8
lines changed

1 file changed

+54
-8
lines changed

src/types/mod.rs

+54-8
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,24 @@ pub trait DerefToPyAny {
121121
// Implementations core to all native types
122122
#[doc(hidden)]
123123
#[macro_export]
124+
#[cfg_attr(docsrs, doc(cfg(all())))]
125+
#[cfg(not(feature = "gil-refs"))]
126+
macro_rules! pyobject_native_type_base(
127+
// empty implementation on non-gil-refs
128+
($name:ty $(;$generics:ident)* ) => {};
129+
);
130+
131+
// Implementations core to all native types
132+
#[doc(hidden)]
133+
#[macro_export]
134+
#[cfg_attr(docsrs, doc(cfg(all())))]
135+
#[cfg(feature = "gil-refs")]
124136
macro_rules! pyobject_native_type_base(
125137
($name:ty $(;$generics:ident)* ) => {
126-
#[cfg(feature = "gil-refs")]
127138
unsafe impl<$($generics,)*> $crate::PyNativeType for $name {
128139
type AsRefSource = Self;
129140
}
130141

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

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

160-
#[cfg(feature = "gil-refs")]
161169
impl<$($generics,)*> $crate::ToPyObject for $name
162170
{
163171
#[inline]
@@ -172,6 +180,35 @@ macro_rules! pyobject_native_type_base(
172180
// make sense on PyAny / have different implementations).
173181
#[doc(hidden)]
174182
#[macro_export]
183+
#[cfg_attr(docsrs, doc(cfg(all())))]
184+
#[cfg(not(feature = "gil-refs"))]
185+
macro_rules! pyobject_native_type_named (
186+
($name:ty $(;$generics:ident)*) => {
187+
188+
impl<$($generics,)*> ::std::convert::AsRef<$crate::PyAny> for $name {
189+
#[inline]
190+
fn as_ref(&self) -> &$crate::PyAny {
191+
&self.0
192+
}
193+
}
194+
195+
impl<$($generics,)*> ::std::ops::Deref for $name {
196+
type Target = $crate::PyAny;
197+
198+
#[inline]
199+
fn deref(&self) -> &$crate::PyAny {
200+
&self.0
201+
}
202+
}
203+
204+
impl $crate::types::DerefToPyAny for $name {}
205+
};
206+
);
207+
208+
#[doc(hidden)]
209+
#[macro_export]
210+
#[cfg_attr(docsrs, doc(cfg(all())))]
211+
#[cfg(feature = "gil-refs")]
175212
macro_rules! pyobject_native_type_named (
176213
($name:ty $(;$generics:ident)*) => {
177214
$crate::pyobject_native_type_base!($name $(;$generics)*);
@@ -202,7 +239,6 @@ macro_rules! pyobject_native_type_named (
202239

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

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

224259
// FIXME https://github.com/PyO3/pyo3/issues/3903
225260
#[allow(unknown_lints, non_local_definitions)]
226-
#[cfg(feature = "gil-refs")]
227261
impl<'a, $($generics,)*> ::std::convert::From<&'a $name> for &'a $crate::PyAny {
228262
fn from(ob: &'a $name) -> Self {
229263
unsafe{&*(ob as *const $name as *const $crate::PyAny)}
@@ -279,11 +313,23 @@ macro_rules! pyobject_native_type_info(
279313
// because rust-numpy has a special implementation.
280314
#[doc(hidden)]
281315
#[macro_export]
316+
#[cfg_attr(docsrs, doc(cfg(all())))]
317+
#[cfg(not(feature = "gil-refs"))]
318+
macro_rules! pyobject_native_type_extract {
319+
// no body for non-gil-refs
320+
($name:ty $(;$generics:ident)*) => {};
321+
}
322+
323+
// NOTE: This macro is not included in pyobject_native_type_base!
324+
// because rust-numpy has a special implementation.
325+
#[doc(hidden)]
326+
#[macro_export]
327+
#[cfg_attr(docsrs, doc(cfg(all())))]
328+
#[cfg(feature = "gil-refs")]
282329
macro_rules! pyobject_native_type_extract {
283330
($name:ty $(;$generics:ident)*) => {
284331
// FIXME https://github.com/PyO3/pyo3/issues/3903
285332
#[allow(unknown_lints, non_local_definitions)]
286-
#[cfg(feature = "gil-refs")]
287333
impl<'py, $($generics,)*> $crate::FromPyObject<'py> for &'py $name {
288334
#[inline]
289335
fn extract_bound(obj: &$crate::Bound<'py, $crate::PyAny>) -> $crate::PyResult<Self> {

0 commit comments

Comments
 (0)