From bc33921fdb6ae7ddf75ae7c8f59375635fe05158 Mon Sep 17 00:00:00 2001 From: Maxim Kurnikov Date: Tue, 4 Jun 2019 14:56:44 +0300 Subject: [PATCH] make empty exception message format compatible with CPython --- vm/src/exceptions.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/vm/src/exceptions.rs b/vm/src/exceptions.rs index 464aea079..4f1f94744 100644 --- a/vm/src/exceptions.rs +++ b/vm/src/exceptions.rs @@ -12,7 +12,8 @@ fn exception_init(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { let msg = if args.args.len() > 1 { args.args[1].clone() } else { - vm.new_str("No msg".to_string()) + let empty_string = String::default(); + vm.new_str(empty_string) }; let traceback = vm.ctx.new_list(Vec::new()); vm.set_attr(&zelf, "msg", msg)?; @@ -132,8 +133,11 @@ fn exception_str(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { } else { panic!("Error message must be set"); }; - let s = format!("{}: {}", exc.class().name, msg); - Ok(vm.new_str(s)) + let mut exc_repr = exc.class().name.clone(); + if !msg.is_empty() { + &exc_repr.push_str(&format!(": {}", msg)); + } + Ok(vm.new_str(exc_repr)) } #[derive(Debug)]