Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop panic on fmt::Display #3062

Merged
merged 1 commit into from May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/3062.fixed.md
@@ -0,0 +1 @@
Stop panic on `fmt::Display`, instead return `"<unprintable object>"` string and report error via `sys.unraisablehook()`
11 changes: 9 additions & 2 deletions src/types/mod.rs
Expand Up @@ -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>"),
}
}
}

Expand Down