init debug helper (#6315)

This commit is contained in:
Jeong, YunWon
2025-11-30 16:58:07 +09:00
committed by GitHub
parent b84f7c19ad
commit 8e0a86d163
2 changed files with 24 additions and 2 deletions

View File

@@ -835,7 +835,28 @@ pub trait Initializer: PyPayload {
#[pyslot]
#[inline]
fn slot_init(zelf: PyObjectRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult<()> {
let zelf = zelf.try_into_value(vm)?;
#[cfg(debug_assertions)]
let class_name_for_debug = zelf.class().name().to_string();
let zelf = match zelf.try_into_value(vm) {
Ok(zelf) => zelf,
Err(err) => {
#[cfg(debug_assertions)]
{
if let Ok(msg) = err.as_object().repr(vm) {
let double_appearance =
msg.as_str().matches(&class_name_for_debug as &str).count() == 2;
if double_appearance {
panic!(
"This type `{}` doesn't seem to support `init`. Override `slot_init` instead: {}",
class_name_for_debug, msg
);
}
}
}
return Err(err);
}
};
let args: Self::Args = args.bind(vm)?;
Self::init(zelf, args, vm)
}
@@ -843,6 +864,7 @@ pub trait Initializer: PyPayload {
#[pymethod]
#[inline]
fn __init__(zelf: PyRef<Self>, args: Self::Args, vm: &VirtualMachine) -> PyResult<()> {
// TODO: check if this is safe. zelf may need to be `PyObjectRef`
Self::init(zelf, args, vm)
}

View File

@@ -509,7 +509,7 @@ impl VirtualMachine {
#[cfg(debug_assertions)]
let msg = if class.get_id() == actual_class.get_id() {
let mut msg = msg;
msg += " Did you forget to add `#[pyclass(with(Constructor))]`?";
msg += " It might mean this type doesn't support subclassing very well. e.g. Did you forget to add `#[pyclass(with(Constructor))]`?";
msg
} else {
msg