Skip to content

Commit 29c6f4b

Browse files
authored
fix __clear__ slot naming collision with clear method (#4619)
* fix `__clear__` slot naming collision with `clear` method * add newsfragment
1 parent ca4bea3 commit 29c6f4b

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -504,21 +504,21 @@ fn impl_clear_slot(cls: &syn::Type, spec: &FnSpec<'_>, ctx: &Ctx) -> syn::Result
504504
};
505505

506506
let associated_method = quote! {
507-
pub unsafe extern "C" fn __pymethod_clear__(
507+
pub unsafe extern "C" fn __pymethod___clear____(
508508
_slf: *mut #pyo3_path::ffi::PyObject,
509509
) -> ::std::os::raw::c_int {
510510
#pyo3_path::impl_::pymethods::_call_clear(_slf, |py, _slf| {
511511
#holders
512512
let result = #fncall;
513513
let result = #pyo3_path::impl_::wrap::converter(&result).wrap(result)?;
514-
Ok(result)
515-
}, #cls::__pymethod_clear__)
514+
::std::result::Result::Ok(result)
515+
}, #cls::__pymethod___clear____)
516516
}
517517
};
518518
let slot_def = quote! {
519519
#pyo3_path::ffi::PyType_Slot {
520520
slot: #pyo3_path::ffi::Py_tp_clear,
521-
pfunc: #cls::__pymethod_clear__ as #pyo3_path::ffi::inquiry as _
521+
pfunc: #cls::__pymethod___clear____ as #pyo3_path::ffi::inquiry as _
522522
}
523523
};
524524
Ok(MethodAndSlotDef {

src/tests/hygiene/pymethods.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,24 @@ impl Dummy {
415415
// Buffer protocol?
416416
}
417417

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

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

0 commit comments

Comments
 (0)