Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu committed Mar 7, 2024
1 parent 1cfba4d commit 34acc50
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
46 changes: 25 additions & 21 deletions pyo3-macros-backend/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ use crate::{
pyfunction::{impl_wrap_pyfunction, PyFunctionOptions},
};
use proc_macro2::TokenStream;
use quote::{quote, quote_spanned};
use quote::quote;
use syn::{
ext::IdentExt,
parse::{Parse, ParseStream},
parse_quote, parse_quote_spanned,
spanned::Spanned,
token::Comma,
Item, Path, Result,
Expand Down Expand Up @@ -281,6 +282,7 @@ pub fn pymodule_function_impl(mut function: syn::ItemFn) -> Result<TokenStream>
let options = PyModuleOptions::from_attrs(&mut function.attrs)?;
process_functions_in_module(&options, &mut function)?;
let ctx = &Ctx::new(&options.krate);
let stmts = std::mem::take(&mut function.block.stmts);
let Ctx { pyo3_path } = ctx;
let ident = &function.sig.ident;
let vis = &function.vis;
Expand All @@ -295,29 +297,31 @@ pub fn pymodule_function_impl(mut function: syn::ItemFn) -> Result<TokenStream>
}
module_args.push(quote!(::std::convert::Into::into(BoundRef(module))));

let syn::ItemFn {
attrs, sig, block, ..
} = &function;

let extractors = sig.inputs.iter().filter_map(|param| {
if let syn::FnArg::Typed(pat_type) = param {
if let syn::Pat::Ident(pat_ident) = &*pat_type.pat {
let ident = &pat_ident.ident;
return Some(quote_spanned! { pat_type.span() => {
let (_, e) = #pyo3_path::impl_::pymethods::inspect_type(#ident);
let _ = e.extract_gil_ref();
}});
let extractors = function
.sig
.inputs
.iter()
.filter_map(|param| {
if let syn::FnArg::Typed(pat_type) = param {
if let syn::Pat::Ident(pat_ident) = &*pat_type.pat {
let ident = &pat_ident.ident;
return Some([
parse_quote! { let (#ident, e) = #pyo3_path::impl_::pymethods::inspect_type(#ident); },
parse_quote_spanned! { pat_type.span() => e.extract_gil_ref(); },
]);
}
}
}
None
});
None
})
.flatten();

function.block.stmts = extractors.chain(stmts).collect();
function
.attrs
.push(parse_quote!(#[allow(clippy::used_underscore_binding)]));

Ok(quote! {
#(#attrs)*
#vis #sig {
#(#extractors)*
#block
}
#function
#vis mod #ident {
#initialization
}
Expand Down
1 change: 1 addition & 0 deletions src/impl_/pymethods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@ pub trait IsGilRef {}
impl<T: crate::PyNativeType> IsGilRef for &'_ T {}

impl<T> Extractor<T> {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Extractor(NotAGilRef(std::marker::PhantomData))
}
Expand Down

0 comments on commit 34acc50

Please sign in to comment.