From 96984b2860a98ee470f93c75d96eb1ff37f1f18b Mon Sep 17 00:00:00 2001 From: David Hewitt <1939362+davidhewitt@users.noreply.github.com> Date: Thu, 12 Nov 2020 08:52:11 +0000 Subject: [PATCH] coverage: simplify mechanism for panic=unwind tests --- src/err/mod.rs | 22 +++++++++++----------- src/lib.rs | 6 ++++++ src/python.rs | 7 ++----- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/err/mod.rs b/src/err/mod.rs index 8a1b93987f8..c1a384cfd8e 100644 --- a/src/err/mod.rs +++ b/src/err/mod.rs @@ -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] @@ -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] diff --git a/src/lib.rs b/src/lib.rs index 67544e4f095..9326700c344 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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")) +} diff --git a/src/python.rs b/src/python.rs index bdd15babe50..f58f2f2b654 100644 --- a/src/python.rs +++ b/src/python.rs @@ -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; }