Forward cfgs on pyclass fields to the method defs

This commit is contained in:
mejrs 2022-12-06 22:38:32 +01:00
parent 55592afdb9
commit 44310ec7e0
2 changed files with 43 additions and 0 deletions

View file

@ -519,7 +519,15 @@ pub fn impl_py_setter_def(
}
};
let mut cfg_attrs = TokenStream::new();
if let PropertyType::Descriptor { field, .. } = &property_type {
for attr in field.attrs.iter().filter(|attr| attr.path.is_ident("cfg")) {
attr.to_tokens(&mut cfg_attrs);
}
}
let associated_method = quote! {
#cfg_attrs
unsafe fn #wrapper_ident(
_py: _pyo3::Python<'_>,
_slf: *mut _pyo3::ffi::PyObject,
@ -538,6 +546,7 @@ pub fn impl_py_setter_def(
};
let method_def = quote! {
#cfg_attrs
_pyo3::class::PyMethodDefType::Setter({
#deprecations
_pyo3::class::PySetterDef::new(
@ -653,7 +662,15 @@ pub fn impl_py_getter_def(
}
};
let mut cfg_attrs = TokenStream::new();
if let PropertyType::Descriptor { field, .. } = &property_type {
for attr in field.attrs.iter().filter(|attr| attr.path.is_ident("cfg")) {
attr.to_tokens(&mut cfg_attrs);
}
}
let associated_method = quote! {
#cfg_attrs
unsafe fn #wrapper_ident(
_py: _pyo3::Python<'_>,
_slf: *mut _pyo3::ffi::PyObject
@ -665,6 +682,7 @@ pub fn impl_py_getter_def(
};
let method_def = quote! {
#cfg_attrs
_pyo3::class::PyMethodDefType::Getter({
#deprecations
_pyo3::class::PyGetterDef::new(

View file

@ -34,3 +34,28 @@ pub struct Bar {
pub enum Enum {
Var0,
}
#[crate::pyclass]
#[pyo3(crate = "crate")]
pub struct Foo3 {
#[pyo3(get, set)]
#[cfg(FALSE)]
field: i32,
#[pyo3(get, set)]
#[cfg(not(FALSE))]
field: u32,
}
#[crate::pyclass]
#[pyo3(crate = "crate")]
pub struct Foo4 {
#[pyo3(get, set)]
#[cfg(FALSE)]
#[cfg(not(FALSE))]
field: i32,
#[pyo3(get, set)]
#[cfg(any(not(FALSE)))]
field: u32,
}