Scope macro imports more tightly (#4088)

This commit is contained in:
Bruno Kolenbrander 2024-04-23 20:01:41 +02:00 committed by GitHub
parent 5d2f5b5702
commit f5fee94afc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 7 deletions

View File

@ -382,10 +382,7 @@ fn module_initialization(options: PyModuleOptions, ident: &syn::Ident) -> TokenS
fn process_functions_in_module(options: &PyModuleOptions, func: &mut syn::ItemFn) -> Result<()> { fn process_functions_in_module(options: &PyModuleOptions, func: &mut syn::ItemFn) -> Result<()> {
let ctx = &Ctx::new(&options.krate); let ctx = &Ctx::new(&options.krate);
let Ctx { pyo3_path } = ctx; let Ctx { pyo3_path } = ctx;
let mut stmts: Vec<syn::Stmt> = vec![syn::parse_quote!( let mut stmts: Vec<syn::Stmt> = Vec::new();
#[allow(unknown_lints, unused_imports, redundant_imports)]
use #pyo3_path::{PyNativeType, types::PyModuleMethods};
)];
for mut stmt in func.block.stmts.drain(..) { for mut stmt in func.block.stmts.drain(..) {
if let syn::Stmt::Item(Item::Fn(func)) = &mut stmt { if let syn::Stmt::Item(Item::Fn(func)) = &mut stmt {
@ -395,7 +392,11 @@ fn process_functions_in_module(options: &PyModuleOptions, func: &mut syn::ItemFn
let name = &func.sig.ident; let name = &func.sig.ident;
let statements: Vec<syn::Stmt> = syn::parse_quote! { let statements: Vec<syn::Stmt> = syn::parse_quote! {
#wrapped_function #wrapped_function
#module_name.as_borrowed().add_function(#pyo3_path::wrap_pyfunction!(#name, #module_name.as_borrowed())?)?; {
#[allow(unknown_lints, unused_imports, redundant_imports)]
use #pyo3_path::{PyNativeType, types::PyModuleMethods};
#module_name.as_borrowed().add_function(#pyo3_path::wrap_pyfunction!(#name, #module_name.as_borrowed())?)?;
}
}; };
stmts.extend(statements); stmts.extend(statements);
} }

View File

@ -1,5 +1,7 @@
use pyo3::{ use pyo3::{
pyclass, pyfunction, pymodule, types::PyModule, wrap_pyfunction_bound, Bound, PyResult, pyclass, pyfunction, pymodule,
types::{PyModule, PyModuleMethods},
wrap_pyfunction_bound, Bound, PyResult,
}; };
#[pymodule] #[pymodule]

View File

@ -30,7 +30,10 @@ fn basic_module_bound(m: &pyo3::Bound<'_, pyo3::types::PyModule>) -> pyo3::PyRes
42 42
} }
m.add_function(pyo3::wrap_pyfunction_bound!(basic_function, m)?)?; pyo3::types::PyModuleMethods::add_function(
m,
pyo3::wrap_pyfunction_bound!(basic_function, m)?,
)?;
Ok(()) Ok(())
} }