Skip to content

Commit a5e309a

Browse files
committed
Support cfg with module members
1 parent 4b2b8cc commit a5e309a

File tree

6 files changed

+20
-7
lines changed

6 files changed

+20
-7
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,7 @@ jobs:
788788
components: rust-src
789789
- uses: actions/setup-python@v5
790790
with:
791+
python-version: "3.12"
791792
architecture: ${{ matrix.platform.python-architecture }}
792793
- uses: Swatinem/rust-cache@v2
793794
with:

pyo3-introspection/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ edition = "2021"
1010

1111
[dependencies]
1212
anyhow = "1"
13-
goblin = "0.8.0"
13+
goblin = "0.9.0"
1414
serde = { version = "1", features = ["derive"] }
1515
serde_json = "1"
1616

pyo3-macros-backend/src/introspection.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ use std::collections::HashMap;
1313
use std::hash::{Hash, Hasher};
1414
use std::mem::take;
1515
use std::sync::atomic::{AtomicUsize, Ordering};
16-
use syn::Ident;
16+
use syn::{Attribute, Ident};
1717

1818
static GLOBAL_COUNTER_FOR_UNIQUE_NAMES: AtomicUsize = AtomicUsize::new(0);
1919

2020
pub fn module_introspection_code<'a>(
2121
pyo3_crate_path: &PyO3CratePath,
2222
name: &str,
2323
members: impl IntoIterator<Item = &'a Ident>,
24+
members_cfg_attrs: impl IntoIterator<Item = &'a Vec<Attribute>>,
2425
) -> TokenStream {
2526
let stub = IntrospectionNode::Map(
2627
[
@@ -32,7 +33,14 @@ pub fn module_introspection_code<'a>(
3233
IntrospectionNode::List(
3334
members
3435
.into_iter()
35-
.map(|member| IntrospectionNode::IntrospectionId(Some(member)))
36+
.zip(members_cfg_attrs)
37+
.filter_map(|(member, attributes)| {
38+
if attributes.is_empty() {
39+
Some(IntrospectionNode::IntrospectionId(Some(member)))
40+
} else {
41+
None // TODO: properly interpret cfg attributes
42+
}
43+
})
3644
.collect(),
3745
),
3846
),

pyo3-macros-backend/src/module.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,12 @@ pub fn pymodule_module_impl(
340340
}
341341

342342
#[cfg(feature = "experimental-inspect")]
343-
let introspection = module_introspection_code(pyo3_path, &name.to_string(), &module_items);
343+
let introspection = module_introspection_code(
344+
pyo3_path,
345+
&name.to_string(),
346+
&module_items,
347+
&module_items_cfg_attrs,
348+
);
344349
#[cfg(not(feature = "experimental-inspect"))]
345350
let introspection = quote! {};
346351

@@ -410,7 +415,7 @@ pub fn pymodule_function_impl(
410415
);
411416

412417
#[cfg(feature = "experimental-inspect")]
413-
let introspection = module_introspection_code(pyo3_path, &name.to_string(), &[]);
418+
let introspection = module_introspection_code(pyo3_path, &name.to_string(), &[], &[]);
414419
#[cfg(not(feature = "experimental-inspect"))]
415420
let introspection = quote! {};
416421

pytests/stubs/pyclasses.pyi

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
class ClassWithDict: ...
21
class AssertingBaseClass: ...
32
class ClassWithoutConstructor: ...
43
class EmptyClass: ...

tests/test_compile_error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ fn test_compile_errors() {
5858
#[cfg(any(not(Py_LIMITED_API), Py_3_10))] // to avoid PyFunctionArgument for &str
5959
t.compile_fail("tests/ui/invalid_cancel_handle.rs");
6060
t.pass("tests/ui/pymodule_missing_docs.rs");
61-
#[cfg(not(Py_LIMITED_API))]
61+
#[cfg(not(any(Py_LIMITED_API, feature = "experimental-inspect")))]
6262
t.pass("tests/ui/forbid_unsafe.rs");
6363
#[cfg(all(Py_LIMITED_API, not(feature = "experimental-async")))]
6464
// output changes with async feature

0 commit comments

Comments
 (0)