3062: Stop panic on `fmt::Display` r=davidhewitt a=samuelcolvin

Closes #3060

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
This commit is contained in:
bors[bot] 2023-05-10 05:52:40 +00:00 committed by GitHub
commit 4a0bea2e93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 2 deletions

View File

@ -0,0 +1 @@
Stop panic on `fmt::Display`, instead return `"<unprintable object>"` string and report error via `sys.unraisablehook()`

View File

@ -99,8 +99,15 @@ macro_rules! pyobject_native_type_base(
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>)
-> ::std::result::Result<(), ::std::fmt::Error>
{
let s = self.str().or(::std::result::Result::Err(::std::fmt::Error))?;
f.write_str(&s.to_string_lossy())
match self.str() {
::std::result::Result::Ok(s) => return f.write_str(&s.to_string_lossy()),
::std::result::Result::Err(err) => err.write_unraisable(self.py(), ::std::option::Option::Some(self)),
}
match self.get_type().name() {
::std::result::Result::Ok(name) => ::std::write!(f, "<unprintable {} object>", name),
::std::result::Result::Err(_err) => f.write_str("<unprintable object>"),
}
}
}