Skip to content

Commit 0c9d3c8

Browse files
Merge pull request #1015 from mkurnikov/empty-exception-message
Make empty exception message format compatible with CPython
2 parents e871ab0 + bc33921 commit 0c9d3c8

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

vm/src/exceptions.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ fn exception_init(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
1212
let msg = if args.args.len() > 1 {
1313
args.args[1].clone()
1414
} else {
15-
vm.new_str("No msg".to_string())
15+
let empty_string = String::default();
16+
vm.new_str(empty_string)
1617
};
1718
let traceback = vm.ctx.new_list(Vec::new());
1819
vm.set_attr(&zelf, "msg", msg)?;
@@ -132,8 +133,11 @@ fn exception_str(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
132133
} else {
133134
panic!("Error message must be set");
134135
};
135-
let s = format!("{}: {}", exc.class().name, msg);
136-
Ok(vm.new_str(s))
136+
let mut exc_repr = exc.class().name.clone();
137+
if !msg.is_empty() {
138+
&exc_repr.push_str(&format!(": {}", msg));
139+
}
140+
Ok(vm.new_str(exc_repr))
137141
}
138142

139143
#[derive(Debug)]

0 commit comments

Comments
 (0)