Skip to content

Commit

Permalink
Merge pull request #1271 from davidhewitt/panic-abort-tests
Browse files Browse the repository at this point in the history
coverage: simplify mechanism for panic=unwind tests
  • Loading branch information
davidhewitt committed Nov 12, 2020
2 parents fb02a58 + 96984b2 commit 470716e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
22 changes: 11 additions & 11 deletions src/err/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,6 @@ fn exceptions_must_derive_from_base_exception(py: Python) -> PyErr {
mod tests {
use super::PyErrState;
use crate::exceptions;
use crate::panic::PanicException;
use crate::{PyErr, Python};

#[test]
Expand All @@ -540,23 +539,24 @@ mod tests {
}

#[test]
fn fetching_panic_exception_panics() {
// If -Cpanic=abort is specified, we can't catch panic.
if option_env!("RUSTFLAGS")
.map(|s| s.contains("-Cpanic=abort"))
.unwrap_or(false)
{
return;
#[should_panic(expected = "new panic")]
fn fetching_panic_exception_resumes_unwind() {
// TODO replace with #[cfg(panic = "unwind")] once stable
if !crate::cfg_panic_unwind() {
// panic to meet the expected abort in panic=abort :-/
panic!("new panic");
}

use crate::panic::PanicException;

let gil = Python::acquire_gil();
let py = gil.python();
let err: PyErr = PanicException::new_err("new panic");
err.restore(py);
assert!(PyErr::occurred(py));
let started_unwind =
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| PyErr::fetch(py))).is_err();
assert!(started_unwind);

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

#[test]
Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,9 @@ pub mod doc_test {
doctest!("../guide/src/types.md", guide_types_md);
doctest!("../guide/src/trait_bounds.md", guide_trait_bounds_md);
}

// interim helper until #[cfg(panic = ...)] is stable
#[cfg(test)]
fn cfg_panic_unwind() -> bool {
option_env!("RUSTFLAGS").map_or(true, |var| !var.contains("-Cpanic=abort"))
}
7 changes: 2 additions & 5 deletions src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,11 +588,8 @@ mod test {

#[test]
fn test_allow_threads_panics_safely() {
// If -Cpanic=abort is specified, we can't catch panic.
if option_env!("RUSTFLAGS")
.map(|s| s.contains("-Cpanic=abort"))
.unwrap_or(false)
{
// TODO replace with #[cfg(panic = "unwind")] once stable
if !crate::cfg_panic_unwind() {
return;
}

Expand Down

0 comments on commit 470716e

Please sign in to comment.