diff --git a/vm/src/frame.rs b/vm/src/frame.rs index 814286694..872eed95c 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -331,7 +331,7 @@ impl ExecutingFrame<'_> { let instr = &self.code.instructions[idx]; let result = self.execute_instruction(instr, vm); match result { - Ok(None) => {} + Ok(None) => continue, Ok(Some(value)) => { break Ok(value); } @@ -349,7 +349,7 @@ impl ExecutingFrame<'_> { exception.set_traceback(Some(new_traceback.into_ref(vm))); match self.unwind_blocks(vm, UnwindReason::Raising { exception }) { - Ok(None) => {} + Ok(None) => continue, Ok(Some(result)) => { break Ok(result); } diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index 77f5ff643..a2d29e9f7 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -383,26 +383,41 @@ impl TryFromObject for PyRef where T: PyValue, { + #[inline] fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult { let class = T::class(vm); if obj.isinstance(class) { - obj.downcast().map_err(|obj| { - vm.new_runtime_error(format!( - "Unexpected payload '{}' for type '{}'", - class.name, - obj.class().name, - )) - }) + obj.downcast() + .map_err(|obj| pyref_payload_error(vm, class, obj)) } else { - let expected_type = &class.name; - let actual_type = &obj.class().name; - Err(vm.new_type_error(format!( - "Expected type '{}', not '{}'", - expected_type, actual_type, - ))) + Err(pyref_type_error(vm, class, obj)) } } } +// the impl Borrow allows to pass PyObjectRef or &PyObjectRef +fn pyref_payload_error( + vm: &VirtualMachine, + class: &PyTypeRef, + obj: impl std::borrow::Borrow, +) -> PyBaseExceptionRef { + vm.new_runtime_error(format!( + "Unexpected payload '{}' for type '{}'", + &*class.name, + &*obj.borrow().class().name, + )) +} +fn pyref_type_error( + vm: &VirtualMachine, + class: &PyTypeRef, + obj: impl std::borrow::Borrow, +) -> PyBaseExceptionRef { + let expected_type = &*class.name; + let actual_type = &*obj.borrow().class().name; + vm.new_type_error(format!( + "Expected type '{}', not '{}'", + expected_type, actual_type, + )) +} impl<'a, T: PyValue> From<&'a PyRef> for &'a PyObjectRef { fn from(obj: &'a PyRef) -> Self { @@ -787,19 +802,10 @@ unsafe impl TransmuteFromObject for PyRef { if obj.payload_is::() { Ok(()) } else { - Err(vm.new_runtime_error(format!( - "Unexpected payload '{}' for type '{}'", - class.name, - obj.class().name, - ))) + Err(pyref_payload_error(vm, class, obj)) } } else { - let expected_type = &class.name; - let actual_type = &obj.class().name; - Err(vm.new_type_error(format!( - "Expected type '{}', not '{}'", - expected_type, actual_type, - ))) + Err(pyref_type_error(vm, class, obj)) } } }