From 1c9885857db4632470735d6179305dff8f78379f Mon Sep 17 00:00:00 2001 From: Nathan Goldbaum Date: Sat, 24 Aug 2024 11:59:27 -0600 Subject: [PATCH] skip tests using thread-unsafe constructs on free-threaded build (#4476) * skip tests using thread-unsafe constructs on free-threaded build * fix clippy with features=full --- src/conversions/chrono.rs | 5 ++++- src/err/mod.rs | 2 ++ src/tests/common.rs | 12 +++++++++--- src/tests/mod.rs | 1 + tests/test_buffer_protocol.rs | 2 +- tests/test_class_basics.rs | 2 +- tests/test_exceptions.rs | 2 +- 7 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/conversions/chrono.rs b/src/conversions/chrono.rs index 5e61716975e..5a0f9c0231e 100644 --- a/src/conversions/chrono.rs +++ b/src/conversions/chrono.rs @@ -1098,6 +1098,7 @@ mod tests { check_utc("regular", 2014, 5, 6, 7, 8, 9, 999_999, 999_999); + #[cfg(not(Py_GIL_DISABLED))] assert_warnings!( py, check_utc("leap second", 2014, 5, 6, 7, 8, 59, 1_999_999, 999_999), @@ -1140,6 +1141,7 @@ mod tests { check_fixed_offset("regular", 2014, 5, 6, 7, 8, 9, 999_999, 999_999); + #[cfg(not(Py_GIL_DISABLED))] assert_warnings!( py, check_fixed_offset("leap second", 2014, 5, 6, 7, 8, 59, 1_999_999, 999_999), @@ -1295,6 +1297,7 @@ mod tests { check_time("regular", 3, 5, 7, 999_999, 999_999); + #[cfg(not(Py_GIL_DISABLED))] assert_warnings!( py, check_time("leap second", 3, 5, 59, 1_999_999, 999_999), @@ -1342,7 +1345,7 @@ mod tests { .unwrap() } - #[cfg(not(target_arch = "wasm32"))] + #[cfg(not(any(target_arch = "wasm32", Py_GIL_DISABLED)))] mod proptests { use super::*; use crate::tests::common::CatchWarnings; diff --git a/src/err/mod.rs b/src/err/mod.rs index c23abcd5dad..2ef9e531768 100644 --- a/src/err/mod.rs +++ b/src/err/mod.rs @@ -1248,6 +1248,7 @@ mod tests { warnings.call_method0("resetwarnings").unwrap(); // First, test the warning is emitted + #[cfg(not(Py_GIL_DISABLED))] assert_warnings!( py, { PyErr::warn(py, &cls, ffi::c_str!("I am warning you"), 0).unwrap() }, @@ -1267,6 +1268,7 @@ mod tests { .unwrap(); // This has the wrong module and will not raise, just be emitted + #[cfg(not(Py_GIL_DISABLED))] assert_warnings!( py, { PyErr::warn(py, &cls, ffi::c_str!("I am warning you"), 0).unwrap() }, diff --git a/src/tests/common.rs b/src/tests/common.rs index 5ba6ed19401..8af20a2c2fa 100644 --- a/src/tests/common.rs +++ b/src/tests/common.rs @@ -9,8 +9,10 @@ mod inner { #[allow(unused_imports)] // pulls in `use crate as pyo3` in `test_utils.rs` use super::*; + #[cfg(not(Py_GIL_DISABLED))] use pyo3::prelude::*; + #[cfg(not(Py_GIL_DISABLED))] use pyo3::types::{IntoPyDict, PyList}; #[macro_export] @@ -63,14 +65,14 @@ mod inner { } // sys.unraisablehook not available until Python 3.8 - #[cfg(all(feature = "macros", Py_3_8))] + #[cfg(all(feature = "macros", Py_3_8, not(Py_GIL_DISABLED)))] #[pyclass(crate = "pyo3")] pub struct UnraisableCapture { pub capture: Option<(PyErr, PyObject)>, old_hook: Option, } - #[cfg(all(feature = "macros", Py_3_8))] + #[cfg(all(feature = "macros", Py_3_8, not(Py_GIL_DISABLED)))] #[pymethods(crate = "pyo3")] impl UnraisableCapture { pub fn hook(&mut self, unraisable: Bound<'_, PyAny>) { @@ -80,7 +82,7 @@ mod inner { } } - #[cfg(all(feature = "macros", Py_3_8))] + #[cfg(all(feature = "macros", Py_3_8, not(Py_GIL_DISABLED)))] impl UnraisableCapture { pub fn install(py: Python<'_>) -> Py { let sys = py.import("sys").unwrap(); @@ -109,10 +111,12 @@ mod inner { } } + #[cfg(not(Py_GIL_DISABLED))] pub struct CatchWarnings<'py> { catch_warnings: Bound<'py, PyAny>, } + #[cfg(not(Py_GIL_DISABLED))] impl<'py> CatchWarnings<'py> { pub fn enter( py: Python<'py>, @@ -129,6 +133,7 @@ mod inner { } } + #[cfg(not(Py_GIL_DISABLED))] impl Drop for CatchWarnings<'_> { fn drop(&mut self) { let py = self.catch_warnings.py(); @@ -138,6 +143,7 @@ mod inner { } } + #[cfg(not(Py_GIL_DISABLED))] #[macro_export] macro_rules! assert_warnings { ($py:expr, $body:expr, [$(($category:ty, $message:literal)),+] $(,)? ) => {{ diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 7e78b6f19bd..136236d7db0 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -1,5 +1,6 @@ #[macro_use] pub(crate) mod common { + #[cfg(not(Py_GIL_DISABLED))] use crate as pyo3; include!("./common.rs"); } diff --git a/tests/test_buffer_protocol.rs b/tests/test_buffer_protocol.rs index 6ee7b0db328..4d3efec3e4f 100644 --- a/tests/test_buffer_protocol.rs +++ b/tests/test_buffer_protocol.rs @@ -94,7 +94,7 @@ fn test_buffer_referenced() { } #[test] -#[cfg(Py_3_8)] // sys.unraisablehook not available until Python 3.8 +#[cfg(all(Py_3_8, not(Py_GIL_DISABLED)))] // sys.unraisablehook not available until Python 3.8 fn test_releasebuffer_unraisable_error() { use common::UnraisableCapture; use pyo3::exceptions::PyValueError; diff --git a/tests/test_class_basics.rs b/tests/test_class_basics.rs index 5b91ca9e695..a6fb89831cc 100644 --- a/tests/test_class_basics.rs +++ b/tests/test_class_basics.rs @@ -615,7 +615,7 @@ fn access_frozen_class_without_gil() { } #[test] -#[cfg(Py_3_8)] // sys.unraisablehook not available until Python 3.8 +#[cfg(all(Py_3_8, not(Py_GIL_DISABLED)))] // sys.unraisablehook not available until Python 3.8 #[cfg_attr(target_arch = "wasm32", ignore)] fn drop_unsendable_elsewhere() { use common::UnraisableCapture; diff --git a/tests/test_exceptions.rs b/tests/test_exceptions.rs index 34772be5719..777c2aa22d4 100644 --- a/tests/test_exceptions.rs +++ b/tests/test_exceptions.rs @@ -99,7 +99,7 @@ fn test_exception_nosegfault() { } #[test] -#[cfg(Py_3_8)] +#[cfg(all(Py_3_8, not(Py_GIL_DISABLED)))] fn test_write_unraisable() { use common::UnraisableCapture; use pyo3::{exceptions::PyRuntimeError, ffi, types::PyNotImplemented};