Skip to content
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

fix __clear__ slot naming collision with clear method #4619

Merged
merged 2 commits into from
Oct 14, 2024
Merged
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/4619.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fixed `__clear__` slot naming collision with `clear` method
8 changes: 4 additions & 4 deletions pyo3-macros-backend/src/pymethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 18 additions & 0 deletions src/tests/hygiene/pymethods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
Loading