Skip to content

Commit

Permalink
fix __clear__ slot naming collision with clear method (#4619)
Browse files Browse the repository at this point in the history
* fix `__clear__` slot naming collision with `clear` method

* add newsfragment
  • Loading branch information
Icxolu authored and davidhewitt committed Oct 15, 2024
1 parent dff9723 commit 8f6464e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
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
6 changes: 3 additions & 3 deletions pyo3-macros-backend/src/pymethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,20 +509,20 @@ 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;
#pyo3_path::callback::convert(py, result)
}, #cls::__pymethod_clear__)
}, #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 @@ -412,6 +412,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

0 comments on commit 8f6464e

Please sign in to comment.