Skip to content

Commit 8e0a86d

Browse files
authored
init debug helper (RustPython#6315)
1 parent b84f7c1 commit 8e0a86d

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

crates/vm/src/types/slot.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,14 +835,36 @@ pub trait Initializer: PyPayload {
835835
#[pyslot]
836836
#[inline]
837837
fn slot_init(zelf: PyObjectRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult<()> {
838-
let zelf = zelf.try_into_value(vm)?;
838+
#[cfg(debug_assertions)]
839+
let class_name_for_debug = zelf.class().name().to_string();
840+
841+
let zelf = match zelf.try_into_value(vm) {
842+
Ok(zelf) => zelf,
843+
Err(err) => {
844+
#[cfg(debug_assertions)]
845+
{
846+
if let Ok(msg) = err.as_object().repr(vm) {
847+
let double_appearance =
848+
msg.as_str().matches(&class_name_for_debug as &str).count() == 2;
849+
if double_appearance {
850+
panic!(
851+
"This type `{}` doesn't seem to support `init`. Override `slot_init` instead: {}",
852+
class_name_for_debug, msg
853+
);
854+
}
855+
}
856+
}
857+
return Err(err);
858+
}
859+
};
839860
let args: Self::Args = args.bind(vm)?;
840861
Self::init(zelf, args, vm)
841862
}
842863

843864
#[pymethod]
844865
#[inline]
845866
fn __init__(zelf: PyRef<Self>, args: Self::Args, vm: &VirtualMachine) -> PyResult<()> {
867+
// TODO: check if this is safe. zelf may need to be `PyObjectRef`
846868
Self::init(zelf, args, vm)
847869
}
848870

crates/vm/src/vm/vm_new.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ impl VirtualMachine {
509509
#[cfg(debug_assertions)]
510510
let msg = if class.get_id() == actual_class.get_id() {
511511
let mut msg = msg;
512-
msg += " Did you forget to add `#[pyclass(with(Constructor))]`?";
512+
msg += " It might mean this type doesn't support subclassing very well. e.g. Did you forget to add `#[pyclass(with(Constructor))]`?";
513513
msg
514514
} else {
515515
msg

0 commit comments

Comments
 (0)