Skip to content

Commit 2e21095

Browse files
committed
fix case of gil-refs feature breaking create_exception! macro
1 parent ea7e28c commit 2e21095

File tree

1 file changed

+62
-8
lines changed

1 file changed

+62
-8
lines changed

src/types/mod.rs

Lines changed: 62 additions & 8 deletions
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,43 @@ 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+
unsafe impl<$($generics,)*> $crate::AsPyPointer for $name {
205+
/// Gets the underlying FFI pointer, returns a borrowed pointer.
206+
#[inline]
207+
fn as_ptr(&self) -> *mut $crate::ffi::PyObject {
208+
self.0.as_ptr()
209+
}
210+
}
211+
212+
impl $crate::types::DerefToPyAny for $name {}
213+
};
214+
);
215+
216+
#[doc(hidden)]
217+
#[macro_export]
218+
#[cfg_attr(docsrs, doc(cfg(all())))]
219+
#[cfg(feature = "gil-refs")]
175220
macro_rules! pyobject_native_type_named (
176221
($name:ty $(;$generics:ident)*) => {
177222
$crate::pyobject_native_type_base!($name $(;$generics)*);
@@ -202,7 +247,6 @@ macro_rules! pyobject_native_type_named (
202247

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

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

224267
// FIXME https://github.com/PyO3/pyo3/issues/3903
225268
#[allow(unknown_lints, non_local_definitions)]
226-
#[cfg(feature = "gil-refs")]
227269
impl<'a, $($generics,)*> ::std::convert::From<&'a $name> for &'a $crate::PyAny {
228270
fn from(ob: &'a $name) -> Self {
229271
unsafe{&*(ob as *const $name as *const $crate::PyAny)}
@@ -279,11 +321,23 @@ macro_rules! pyobject_native_type_info(
279321
// because rust-numpy has a special implementation.
280322
#[doc(hidden)]
281323
#[macro_export]
324+
#[cfg_attr(docsrs, doc(cfg(all())))]
325+
#[cfg(not(feature = "gil-refs"))]
326+
macro_rules! pyobject_native_type_extract {
327+
// no body for non-gil-refs
328+
($name:ty $(;$generics:ident)*) => {};
329+
}
330+
331+
// NOTE: This macro is not included in pyobject_native_type_base!
332+
// because rust-numpy has a special implementation.
333+
#[doc(hidden)]
334+
#[macro_export]
335+
#[cfg_attr(docsrs, doc(cfg(all())))]
336+
#[cfg(feature = "gil-refs")]
282337
macro_rules! pyobject_native_type_extract {
283338
($name:ty $(;$generics:ident)*) => {
284339
// FIXME https://github.com/PyO3/pyo3/issues/3903
285340
#[allow(unknown_lints, non_local_definitions)]
286-
#[cfg(feature = "gil-refs")]
287341
impl<'py, $($generics,)*> $crate::FromPyObject<'py> for &'py $name {
288342
#[inline]
289343
fn extract_bound(obj: &$crate::Bound<'py, $crate::PyAny>) -> $crate::PyResult<Self> {

0 commit comments

Comments
 (0)