From 8e7822ce19cb6b7c917ebcca8286ceeaa1909d98 Mon Sep 17 00:00:00 2001 From: Icxolu <10486322+Icxolu@users.noreply.github.com> Date: Sun, 13 Oct 2024 22:00:21 +0200 Subject: [PATCH 1/2] fix `__clear__` slot naming collision with `clear` method --- pyo3-macros-backend/src/pymethod.rs | 8 ++++---- src/tests/hygiene/pymethods.rs | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/pyo3-macros-backend/src/pymethod.rs b/pyo3-macros-backend/src/pymethod.rs index 0425e467be2..d825609cd77 100644 --- a/pyo3-macros-backend/src/pymethod.rs +++ b/pyo3-macros-backend/src/pymethod.rs @@ -504,21 +504,21 @@ fn impl_clear_slot(cls: &syn::Type, spec: &FnSpec<'_>, ctx: &Ctx) -> syn::Result }; let associated_method = quote! { - pub unsafe extern "C" fn __pymethod_clear__( + pub unsafe extern "C" fn __pymethod___clear____( _slf: *mut #pyo3_path::ffi::PyObject, ) -> ::std::os::raw::c_int { #pyo3_path::impl_::pymethods::_call_clear(_slf, |py, _slf| { #holders let result = #fncall; let result = #pyo3_path::impl_::wrap::converter(&result).wrap(result)?; - Ok(result) - }, #cls::__pymethod_clear__) + ::std::result::Result::Ok(result) + }, #cls::__pymethod___clear____) } }; let slot_def = quote! { #pyo3_path::ffi::PyType_Slot { slot: #pyo3_path::ffi::Py_tp_clear, - pfunc: #cls::__pymethod_clear__ as #pyo3_path::ffi::inquiry as _ + pfunc: #cls::__pymethod___clear____ as #pyo3_path::ffi::inquiry as _ } }; Ok(MethodAndSlotDef { diff --git a/src/tests/hygiene/pymethods.rs b/src/tests/hygiene/pymethods.rs index 5c027a5c3ae..d6d294c558d 100644 --- a/src/tests/hygiene/pymethods.rs +++ b/src/tests/hygiene/pymethods.rs @@ -415,6 +415,24 @@ impl Dummy { // Buffer protocol? } +#[crate::pyclass(crate = "crate")] +struct Clear; + +#[crate::pymethods(crate = "crate")] +impl Clear { + pub fn __traverse__( + &self, + visit: crate::PyVisit<'_>, + ) -> ::std::result::Result<(), crate::PyTraverseError> { + ::std::result::Result::Ok(()) + } + + pub fn __clear__(&self) {} + + #[pyo3(signature=(*, reuse=false))] + pub fn clear(&self, reuse: bool) {} +} + // Ensure that crate argument is also accepted inline #[crate::pyclass(crate = "crate")] From e025e1a59f6831d98ca30d405e7dd39f57de36e4 Mon Sep 17 00:00:00 2001 From: Icxolu <10486322+Icxolu@users.noreply.github.com> Date: Sun, 13 Oct 2024 22:03:02 +0200 Subject: [PATCH 2/2] add newsfragment --- newsfragments/4619.fixed.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 newsfragments/4619.fixed.md diff --git a/newsfragments/4619.fixed.md b/newsfragments/4619.fixed.md new file mode 100644 index 00000000000..c0ff9dbb16a --- /dev/null +++ b/newsfragments/4619.fixed.md @@ -0,0 +1 @@ +fixed `__clear__` slot naming collision with `clear` method \ No newline at end of file