Skip to content

Commit 296d738

Browse files
committed
Don't generate #[cfg]s in proc macro
1 parent 1b6e3f6 commit 296d738

File tree

6 files changed

+29
-7
lines changed

6 files changed

+29
-7
lines changed

examples/decorator/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ crate-type = ["cdylib"]
1010
[dependencies]
1111
pyo3 = { path = "../../", features = ["extension-module"] }
1212

13+
[features]
14+
pep489 = ["pyo3/pep489"]
15+
1316
[workspace]

examples/maturin-starter/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ crate-type = ["cdylib"]
1010
[dependencies]
1111
pyo3 = { path = "../../", features = ["extension-module"] }
1212

13+
[features]
14+
pep489 = ["pyo3/pep489"]
15+
1316
[workspace]

examples/setuptools-rust-starter/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ crate-type = ["cdylib"]
1010
[dependencies]
1111
pyo3 = { path = "../../", features = ["extension-module"] }
1212

13+
[features]
14+
pep489 = ["pyo3/pep489"]
15+
1316
[workspace]

examples/word-count/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ crate-type = ["cdylib"]
1111
pyo3 = { path = "../..", features = ["extension-module"] }
1212
rayon = "1.0.2"
1313

14+
[features]
15+
pep489 = ["pyo3/pep489"]
16+
1417
[workspace]

pyo3-macros-backend/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ edition = "2018"
1616
[dependencies]
1717
quote = { version = "1", default-features = false }
1818
proc-macro2 = { version = "1", default-features = false }
19+
pyo3-build-config = { path = "../pyo3-build-config", version = "0.16.2", features = ["resolve-config"] }
1920

2021
[dependencies.syn]
2122
version = "1.0.56"

pyo3-macros-backend/src/module.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ pub fn pymodule_impl(
7676

7777
let module_def_name = module_def_ident(fnname);
7878

79-
quote! {
80-
#[cfg(any(not(feature = "pep489"), PyPy))]
79+
let single_phase_module = quote! {
8180
#[no_mangle]
8281
#[allow(non_snake_case)]
8382
/// This autogenerated function is called by the python interpreter when importing
@@ -86,13 +85,13 @@ pub fn pymodule_impl(
8685
unsafe { #module_def_name.module_init() }
8786
}
8887

89-
#[cfg(any(not(feature = "pep489"), PyPy))]
9088
#[doc(hidden)]
9189
#visibility static #module_def_name: #krate::impl_::pymodule::ModuleDef = unsafe {
9290
#krate::impl_::pymodule::ModuleDef::new(concat!(stringify!(#name), "\0"), #doc, #krate::impl_::pymodule::ModuleInitializer(#fnname))
9391
};
92+
};
9493

95-
#[cfg(all(feature = "pep489", not(PyPy)))]
94+
let multi_phase_module = quote! {
9695
#[no_mangle]
9796
#[allow(non_snake_case)]
9897
/// This autogenerated function is called by the python interpreter when importing
@@ -101,7 +100,6 @@ pub fn pymodule_impl(
101100
unsafe { #krate::ffi::PyModuleDef_Init(#module_def_name.module_def()) }
102101
}
103102

104-
#[cfg(all(feature = "pep489", not(PyPy)))]
105103
pub unsafe extern "C" fn #mod_exec_name(module: *mut #krate::ffi::PyObject) -> ::std::os::raw::c_int {
106104
use #krate::conversion::FromPyPointer;
107105

@@ -118,7 +116,6 @@ pub fn pymodule_impl(
118116
}
119117

120118
// An array of slot definitions for multi-phase initialization, terminated by a {0, NULL} entry.
121-
#[cfg(all(feature = "pep489", not(PyPy)))]
122119
#[doc(hidden)]
123120
#visibility static #slots_name: [#krate::impl_::pymodule::ModuleDefSlot; 2] = [
124121
#krate::impl_::pymodule::ModuleDefSlot::new(
@@ -131,7 +128,6 @@ pub fn pymodule_impl(
131128
),
132129
];
133130

134-
#[cfg(all(feature = "pep489", not(PyPy)))]
135131
#[doc(hidden)]
136132
#visibility static #module_def_name: #krate::impl_::pymodule::ModuleDef = unsafe {
137133
#krate::impl_::pymodule::ModuleDef::new(
@@ -140,9 +136,22 @@ pub fn pymodule_impl(
140136
&#slots_name as *const #krate::impl_::pymodule::ModuleDefSlot as *mut #krate::ffi::PyModuleDef_Slot,
141137
)
142138
};
139+
};
140+
141+
if is_pypy() {
142+
single_phase_module
143+
} else if cfg!(feature = "pep489") {
144+
multi_phase_module
145+
} else {
146+
single_phase_module
143147
}
144148
}
145149

150+
fn is_pypy() -> bool {
151+
let config = pyo3_build_config::get();
152+
config.implementation.is_pypy()
153+
}
154+
146155
/// Finds and takes care of the #[pyfn(...)] in `#[pymodule]`
147156
pub fn process_functions_in_module(func: &mut syn::ItemFn) -> syn::Result<()> {
148157
let mut stmts: Vec<syn::Stmt> = Vec::new();

0 commit comments

Comments
 (0)