Skip to content

Attempt to mark various generated code with #[automatically_derived] #3994

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/3994.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Generated functions and impls are now marked `#[automatically_derived]`
14 changes: 4 additions & 10 deletions pyo3-macros-backend/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,21 +269,13 @@ pub struct FnSpec<'a> {
// r# can be removed by syn::ext::IdentExt::unraw()
pub python_name: syn::Ident,
pub signature: FunctionSignature<'a>,
pub output: syn::Type,
pub convention: CallingConvention,
pub text_signature: Option<TextSignatureAttribute>,
pub asyncness: Option<syn::Token![async]>,
pub unsafety: Option<syn::Token![unsafe]>,
pub deprecations: Deprecations<'a>,
}

pub fn get_return_info(output: &syn::ReturnType) -> syn::Type {
match output {
syn::ReturnType::Default => syn::Type::Infer(syn::parse_quote! {_}),
syn::ReturnType::Type(_, ty) => *ty.clone(),
}
}

pub fn parse_method_receiver(arg: &syn::FnArg) -> Result<SelfType> {
match arg {
syn::FnArg::Receiver(
Expand Down Expand Up @@ -329,7 +321,6 @@ impl<'a> FnSpec<'a> {
ensure_signatures_on_valid_method(&fn_type, signature.as_ref(), text_signature.as_ref())?;

let name = &sig.ident;
let ty = get_return_info(&sig.output);
let python_name = python_name.as_ref().unwrap_or(name).unraw();

let arguments: Vec<_> = sig
Expand Down Expand Up @@ -361,7 +352,6 @@ impl<'a> FnSpec<'a> {
convention,
python_name,
signature,
output: ty,
text_signature,
asyncness: sig.asyncness,
unsafety: sig.unsafety,
Expand Down Expand Up @@ -621,6 +611,7 @@ impl<'a> FnSpec<'a> {
let init_holders = holders.init_holders(ctx);

quote! {
#[automatically_derived]
unsafe fn #ident<'py>(
py: #pyo3_path::Python<'py>,
_slf: *mut #pyo3_path::ffi::PyObject,
Expand All @@ -642,6 +633,7 @@ impl<'a> FnSpec<'a> {
let check_gil_refs = holders.check_gil_refs();

quote! {
#[automatically_derived]
unsafe fn #ident<'py>(
py: #pyo3_path::Python<'py>,
_slf: *mut #pyo3_path::ffi::PyObject,
Expand All @@ -667,6 +659,7 @@ impl<'a> FnSpec<'a> {
let check_gil_refs = holders.check_gil_refs();

quote! {
#[automatically_derived]
unsafe fn #ident<'py>(
py: #pyo3_path::Python<'py>,
_slf: *mut #pyo3_path::ffi::PyObject,
Expand All @@ -693,6 +686,7 @@ impl<'a> FnSpec<'a> {
let init_holders = holders.init_holders(ctx);
let check_gil_refs = holders.check_gil_refs();
quote! {
#[automatically_derived]
unsafe fn #ident(
py: #pyo3_path::Python<'_>,
_slf: *mut #pyo3_path::ffi::PyTypeObject,
Expand Down
14 changes: 4 additions & 10 deletions pyo3-macros-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -975,13 +975,8 @@ fn impl_complex_enum_struct_variant_cls(
let field_type = field.ty;
let field_with_type = quote! { #field_name: #field_type };

let field_getter = complex_enum_variant_field_getter(
&variant_cls_type,
field_name,
field_type,
field.span,
ctx,
)?;
let field_getter =
complex_enum_variant_field_getter(&variant_cls_type, field_name, field.span, ctx)?;

let field_getter_impl = quote! {
fn #field_name(slf: #pyo3_path::PyRef<Self>) -> #pyo3_path::PyResult<#field_type> {
Expand Down Expand Up @@ -1188,7 +1183,6 @@ fn complex_enum_struct_variant_new<'a>(
name: &format_ident!("__pymethod_constructor__"),
python_name: format_ident!("__new__"),
signature,
output: variant_cls_type.clone(),
convention: crate::method::CallingConvention::TpNew,
text_signature: None,
asyncness: None,
Expand All @@ -1202,7 +1196,6 @@ fn complex_enum_struct_variant_new<'a>(
fn complex_enum_variant_field_getter<'a>(
variant_cls_type: &'a syn::Type,
field_name: &'a syn::Ident,
field_type: &'a syn::Type,
field_span: Span,
ctx: &Ctx,
) -> Result<MethodAndMethodDef> {
Expand All @@ -1215,7 +1208,6 @@ fn complex_enum_variant_field_getter<'a>(
name: field_name,
python_name: field_name.clone(),
signature,
output: field_type.clone(),
convention: crate::method::CallingConvention::Noargs,
text_signature: None,
asyncness: None,
Expand Down Expand Up @@ -1301,10 +1293,12 @@ fn impl_pytypeinfo(

quote! {
#[allow(deprecated)]
#[automatically_derived]
unsafe impl #pyo3_path::type_object::HasPyGilRef for #cls {
type AsRefTarget = #pyo3_path::PyCell<Self>;
}

#[automatically_derived]
unsafe impl #pyo3_path::type_object::PyTypeInfo for #cls {
const NAME: &'static str = #cls_name;
const MODULE: ::std::option::Option<&'static str> = #module;
Expand Down
3 changes: 0 additions & 3 deletions pyo3-macros-backend/src/pyfunction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,15 +240,12 @@ pub fn impl_wrap_pyfunction(
FunctionSignature::from_arguments(arguments)?
};

let ty = method::get_return_info(&func.sig.output);

let spec = method::FnSpec {
tp,
name: &func.sig.ident,
convention: CallingConvention::from_signature(&signature),
python_name,
signature,
output: ty,
text_signature,
asyncness: func.sig.asyncness,
unsafety: func.sig.unsafety,
Expand Down
1 change: 1 addition & 0 deletions src/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ macro_rules! import_exception {
#module=::std::option::Option::Some(stringify!($module))
);

#[automatically_derived]
impl $name {
fn type_object_raw(py: $crate::Python<'_>) -> *mut $crate::ffi::PyTypeObject {
use $crate::sync::GILOnceCell;
Expand Down