From c74194c02b6cdda659abfb550d56a0d93bae2eda Mon Sep 17 00:00:00 2001 From: Icxolu <10486322+Icxolu@users.noreply.github.com> Date: Fri, 25 Oct 2024 23:50:07 +0200 Subject: [PATCH 1/2] fix compile error for `#[classmethod]`/`#[staticmethod]` on magic methods --- pyo3-macros-backend/src/method.rs | 12 ++++-------- src/tests/hygiene/pymethods.rs | 12 +++++++++++- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/pyo3-macros-backend/src/method.rs b/pyo3-macros-backend/src/method.rs index 50f70d8440a..019fb5e644b 100644 --- a/pyo3-macros-backend/src/method.rs +++ b/pyo3-macros-backend/src/method.rs @@ -263,12 +263,12 @@ impl FnType { } FnType::FnClass(span) | FnType::FnNewClass(span) => { let py = syn::Ident::new("py", Span::call_site()); - let slf: Ident = syn::Ident::new("_slf_ref", Span::call_site()); + let slf: Ident = syn::Ident::new("_slf", Span::call_site()); let pyo3_path = pyo3_path.to_tokens_spanned(*span); let ret = quote_spanned! { *span => #[allow(clippy::useless_conversion)] ::std::convert::Into::into( - #pyo3_path::impl_::pymethods::BoundRef::ref_from_ptr(#py, &*(#slf as *const _ as *const *mut _)) + #pyo3_path::impl_::pymethods::BoundRef::ref_from_ptr(#py, &*(&#slf as *const _ as *const *mut _)) .downcast_unchecked::<#pyo3_path::types::PyType>() ), }; @@ -276,12 +276,12 @@ impl FnType { } FnType::FnModule(span) => { let py = syn::Ident::new("py", Span::call_site()); - let slf: Ident = syn::Ident::new("_slf_ref", Span::call_site()); + let slf: Ident = syn::Ident::new("_slf", Span::call_site()); let pyo3_path = pyo3_path.to_tokens_spanned(*span); let ret = quote_spanned! { *span => #[allow(clippy::useless_conversion)] ::std::convert::Into::into( - #pyo3_path::impl_::pymethods::BoundRef::ref_from_ptr(#py, &*(#slf as *const _ as *const *mut _)) + #pyo3_path::impl_::pymethods::BoundRef::ref_from_ptr(#py, &*(&#slf as *const _ as *const *mut _)) .downcast_unchecked::<#pyo3_path::types::PyModule>() ), }; @@ -766,7 +766,6 @@ impl<'a> FnSpec<'a> { _slf: *mut #pyo3_path::ffi::PyObject, ) -> #pyo3_path::PyResult<*mut #pyo3_path::ffi::PyObject> { #deprecation - let _slf_ref = &_slf; let function = #rust_name; // Shadow the function name to avoid #3017 #init_holders let result = #call; @@ -789,7 +788,6 @@ impl<'a> FnSpec<'a> { _kwnames: *mut #pyo3_path::ffi::PyObject ) -> #pyo3_path::PyResult<*mut #pyo3_path::ffi::PyObject> { #deprecation - let _slf_ref = &_slf; let function = #rust_name; // Shadow the function name to avoid #3017 #arg_convert #init_holders @@ -812,7 +810,6 @@ impl<'a> FnSpec<'a> { _kwargs: *mut #pyo3_path::ffi::PyObject ) -> #pyo3_path::PyResult<*mut #pyo3_path::ffi::PyObject> { #deprecation - let _slf_ref = &_slf; let function = #rust_name; // Shadow the function name to avoid #3017 #arg_convert #init_holders @@ -838,7 +835,6 @@ impl<'a> FnSpec<'a> { ) -> #pyo3_path::PyResult<*mut #pyo3_path::ffi::PyObject> { use #pyo3_path::impl_::callback::IntoPyCallbackOutput; #deprecation - let _slf_ref = &_slf; let function = #rust_name; // Shadow the function name to avoid #3017 #arg_convert #init_holders diff --git a/src/tests/hygiene/pymethods.rs b/src/tests/hygiene/pymethods.rs index d6d294c558d..a5856e6413e 100644 --- a/src/tests/hygiene/pymethods.rs +++ b/src/tests/hygiene/pymethods.rs @@ -439,4 +439,14 @@ impl Clear { struct Dummy2; #[crate::pymethods(crate = "crate")] -impl Dummy2 {} +impl Dummy2 { + #[classmethod] + fn __len__(cls: &crate::Bound<'_, crate::types::PyType>) -> crate::PyResult { + ::std::result::Result::Ok(0) + } + + #[staticmethod] + fn __repr__() -> &'static str { + "Dummy" + } +} From f63aff68c8334b0eda299864fb79e9b0a14bc645 Mon Sep 17 00:00:00 2001 From: Icxolu <10486322+Icxolu@users.noreply.github.com> Date: Fri, 25 Oct 2024 23:55:36 +0200 Subject: [PATCH 2/2] add newsfragment --- newsfragments/4654.fixed.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 newsfragments/4654.fixed.md diff --git a/newsfragments/4654.fixed.md b/newsfragments/4654.fixed.md new file mode 100644 index 00000000000..5e470178b55 --- /dev/null +++ b/newsfragments/4654.fixed.md @@ -0,0 +1 @@ +fix compile error for `#[classmethod]`/`#[staticmethod]` on magic methods \ No newline at end of file