Skip to content

Commit 8f6464e

Browse files
Icxoludavidhewitt
authored andcommitted
fix __clear__ slot naming collision with clear method (#4619)
* fix `__clear__` slot naming collision with `clear` method * add newsfragment
1 parent dff9723 commit 8f6464e

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

newsfragments/4619.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fixed `__clear__` slot naming collision with `clear` method

pyo3-macros-backend/src/pymethod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,20 +509,20 @@ fn impl_clear_slot(cls: &syn::Type, spec: &FnSpec<'_>, ctx: &Ctx) -> syn::Result
509509
};
510510

511511
let associated_method = quote! {
512-
pub unsafe extern "C" fn __pymethod_clear__(
512+
pub unsafe extern "C" fn __pymethod___clear____(
513513
_slf: *mut #pyo3_path::ffi::PyObject,
514514
) -> ::std::os::raw::c_int {
515515
#pyo3_path::impl_::pymethods::_call_clear(_slf, |py, _slf| {
516516
#holders
517517
let result = #fncall;
518518
#pyo3_path::callback::convert(py, result)
519-
}, #cls::__pymethod_clear__)
519+
}, #cls::__pymethod___clear____)
520520
}
521521
};
522522
let slot_def = quote! {
523523
#pyo3_path::ffi::PyType_Slot {
524524
slot: #pyo3_path::ffi::Py_tp_clear,
525-
pfunc: #cls::__pymethod_clear__ as #pyo3_path::ffi::inquiry as _
525+
pfunc: #cls::__pymethod___clear____ as #pyo3_path::ffi::inquiry as _
526526
}
527527
};
528528
Ok(MethodAndSlotDef {

src/tests/hygiene/pymethods.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,24 @@ impl Dummy {
412412
// Buffer protocol?
413413
}
414414

415+
#[crate::pyclass(crate = "crate")]
416+
struct Clear;
417+
418+
#[crate::pymethods(crate = "crate")]
419+
impl Clear {
420+
pub fn __traverse__(
421+
&self,
422+
visit: crate::PyVisit<'_>,
423+
) -> ::std::result::Result<(), crate::PyTraverseError> {
424+
::std::result::Result::Ok(())
425+
}
426+
427+
pub fn __clear__(&self) {}
428+
429+
#[pyo3(signature=(*, reuse=false))]
430+
pub fn clear(&self, reuse: bool) {}
431+
}
432+
415433
// Ensure that crate argument is also accepted inline
416434

417435
#[crate::pyclass(crate = "crate")]

0 commit comments

Comments
 (0)