Skip to content

Commit

Permalink
deprecate &PyModule as #[pymodule] argument type
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu committed Mar 6, 2024
1 parent fbd5311 commit 1cfba4d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
25 changes: 23 additions & 2 deletions pyo3-macros-backend/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
pyfunction::{impl_wrap_pyfunction, PyFunctionOptions},
};
use proc_macro2::TokenStream;
use quote::quote;
use quote::{quote, quote_spanned};
use syn::{
ext::IdentExt,
parse::{Parse, ParseStream},
Expand Down Expand Up @@ -295,8 +295,29 @@ 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();
}});
}
}
None
});

Ok(quote! {
#function
#(#attrs)*
#vis #sig {
#(#extractors)*
#block
}
#vis mod #ident {
#initialization
}
Expand Down
39 changes: 39 additions & 0 deletions src/impl_/pymethods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,3 +580,42 @@ pub unsafe fn tp_new_impl<T: PyClass>(
.create_class_object_of_type(py, target_type)
.map(Bound::into_ptr)
}

pub fn inspect_type<T>(t: T) -> (T, Extractor<T>) {
(t, Extractor::new())
}

pub struct Extractor<T>(NotAGilRef<T>);
pub struct NotAGilRef<T>(std::marker::PhantomData<T>);

pub trait IsGilRef {}

impl<T: crate::PyNativeType> IsGilRef for &'_ T {}

impl<T> Extractor<T> {
pub fn new() -> Self {
Extractor(NotAGilRef(std::marker::PhantomData))
}
}

impl<T: IsGilRef> Extractor<T> {
#[cfg_attr(
not(feature = "gil-refs"),
deprecated(
since = "0.21.0",
note = "use `&Bound<'_, T>` instead for this function argument"
)
)]
pub fn extract_gil_ref(&self) {}
}

impl<T> NotAGilRef<T> {
pub fn extract_gil_ref(&self) {}
}

impl<T> std::ops::Deref for Extractor<T> {
type Target = NotAGilRef<T>;
fn deref(&self) -> &Self::Target {
&self.0
}
}

0 comments on commit 1cfba4d

Please sign in to comment.