Skip to content

Commit 080a2f6

Browse files
committed
init debug helper
1 parent b84f7c1 commit 080a2f6

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

crates/vm/src/types/slot.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,14 +835,35 @@ 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+
let 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+
return Err(err);
857+
}
858+
};
839859
let args: Self::Args = args.bind(vm)?;
840860
Self::init(zelf, args, vm)
841861
}
842862

843863
#[pymethod]
844864
#[inline]
845865
fn __init__(zelf: PyRef<Self>, args: Self::Args, vm: &VirtualMachine) -> PyResult<()> {
866+
// TODO: check if this is safe. zelf may need to be `PyObjectRef`
846867
Self::init(zelf, args, vm)
847868
}
848869

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)