diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index 4a4118aaa2..3469ad37d8 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -1145,6 +1145,24 @@ pub trait TryFromObject: Sized { fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult; } +pub trait TryFromObjectWithMessage: Sized { + type Error: ?Sized; + + fn try_from_object_with_message( + vm: &VirtualMachine, + obj: PyObjectRef, + error: Self::Error, + ) -> PyResult; + + fn default_error() -> Self::Error; +} + +impl TryFromObject for T where Self: Sized, T: TryFromObjectWithMessage, F: Fn(&VirtualMachine, PyObjectRef) -> PyBaseExceptionRef { + fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult { + Self::try_from_object_with_message(vm, obj, Self::default_error()) + } +} + /// Implemented by any type that can be returned from a built-in Python function. /// /// `IntoPyObject` has a blanket implementation for any built-in object payload,