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

Preserve panic message after exception is normalized #3326

Merged
merged 1 commit into from
Jul 19, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/3326.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix loss of panic message in `PanicException` when unwinding after the exception was "normalized".
24 changes: 22 additions & 2 deletions src/err/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,10 @@ impl PyErr {
};

if ptype.as_ptr() == PanicException::type_object_raw(py).cast() {
let msg: String = pvalue
let msg = pvalue
.as_ref()
.and_then(|obj| obj.extract(py).ok())
.and_then(|obj| obj.as_ref(py).str().ok())
.map(|py_str| py_str.to_string_lossy().into())
.unwrap_or_else(|| String::from("Unwrapped panic from Python code"));

eprintln!(
Expand Down Expand Up @@ -845,6 +846,25 @@ mod tests {
});
}

#[test]
#[should_panic(expected = "new panic")]
#[cfg(not(Py_3_12))]
fn fetching_normalized_panic_exception_resumes_unwind() {
use crate::panic::PanicException;

Python::with_gil(|py| {
let err: PyErr = PanicException::new_err("new panic");
// Restoring an error doesn't normalize it before Python 3.12,
// so we have to explicitly test this case.
let _ = err.normalized(py);
err.restore(py);
assert!(PyErr::occurred(py));

// should resume unwind
let _ = PyErr::fetch(py);
});
}

#[test]
fn err_debug() {
// Debug representation should be like the following (without the newlines):
Expand Down