Skip to content

Commit 2fa818b

Browse files
authored
avoid races in tests asserting warnings (#5357)
1 parent 62f7114 commit 2fa818b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+97
-139
lines changed

src/conversions/chrono.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ fn py_time_to_naive_time(py_time: &Bound<'_, PyAny>) -> PyResult<NaiveTime> {
593593
#[cfg(test)]
594594
mod tests {
595595
use super::*;
596-
use crate::{types::PyTuple, BoundObject};
596+
use crate::{test_utils::assert_warnings, types::PyTuple, BoundObject};
597597
use std::{cmp::Ordering, panic};
598598

599599
#[test]
@@ -875,7 +875,6 @@ mod tests {
875875

876876
check_utc("regular", 2014, 5, 6, 7, 8, 9, 999_999, 999_999);
877877

878-
#[cfg(not(Py_GIL_DISABLED))]
879878
assert_warnings!(
880879
py,
881880
check_utc("leap second", 2014, 5, 6, 7, 8, 59, 1_999_999, 999_999),
@@ -915,7 +914,6 @@ mod tests {
915914

916915
check_fixed_offset("regular", 2014, 5, 6, 7, 8, 9, 999_999, 999_999);
917916

918-
#[cfg(not(Py_GIL_DISABLED))]
919917
assert_warnings!(
920918
py,
921919
check_fixed_offset("leap second", 2014, 5, 6, 7, 8, 59, 1_999_999, 999_999),
@@ -1101,7 +1099,6 @@ mod tests {
11011099

11021100
check_time("regular", 3, 5, 7, 999_999, 999_999);
11031101

1104-
#[cfg(not(Py_GIL_DISABLED))]
11051102
assert_warnings!(
11061103
py,
11071104
check_time("leap second", 3, 5, 59, 1_999_999, 999_999),
@@ -1163,10 +1160,10 @@ mod tests {
11631160
.unwrap()
11641161
}
11651162

1166-
#[cfg(not(any(target_arch = "wasm32", Py_GIL_DISABLED)))]
1163+
#[cfg(not(any(target_arch = "wasm32")))]
11671164
mod proptests {
11681165
use super::*;
1169-
use crate::tests::common::CatchWarnings;
1166+
use crate::test_utils::CatchWarnings;
11701167
use crate::types::IntoPyDict;
11711168
use proptest::prelude::*;
11721169
use std::ffi::CString;

src/conversions/num_bigint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ fn int_n_bits(long: &Bound<'_, PyInt>) -> PyResult<usize> {
317317
#[cfg(test)]
318318
mod tests {
319319
use super::*;
320-
use crate::tests::common::generate_unique_module_name;
320+
use crate::test_utils::generate_unique_module_name;
321321
use crate::types::{PyAnyMethods as _, PyDict, PyModule};
322322
use indoc::indoc;
323323
use pyo3_ffi::c_str;

src/conversions/num_complex.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ complex_conversion!(f64);
196196
#[cfg(test)]
197197
mod tests {
198198
use super::*;
199-
use crate::tests::common::generate_unique_module_name;
199+
use crate::test_utils::generate_unique_module_name;
200200
use crate::types::PyAnyMethods as _;
201201
use crate::types::{complex::PyComplexMethods, PyModule};
202202
use crate::IntoPyObject;

src/err/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,7 @@ mod tests {
853853
use super::PyErrState;
854854
use crate::exceptions::{self, PyTypeError, PyValueError};
855855
use crate::impl_::pyclass::{value_of, IsSend, IsSync};
856+
use crate::test_utils::assert_warnings;
856857
use crate::{ffi, PyErr, PyTypeInfo, Python};
857858

858859
#[test]
@@ -1052,7 +1053,6 @@ mod tests {
10521053
warnings.call_method0("resetwarnings").unwrap();
10531054

10541055
// First, test the warning is emitted
1055-
#[cfg(not(Py_GIL_DISABLED))]
10561056
assert_warnings!(
10571057
py,
10581058
{ PyErr::warn(py, &cls, ffi::c_str!("I am warning you"), 0).unwrap() },
@@ -1072,7 +1072,6 @@ mod tests {
10721072
.unwrap();
10731073

10741074
// This has the wrong module and will not raise, just be emitted
1075-
#[cfg(not(Py_GIL_DISABLED))]
10761075
assert_warnings!(
10771076
py,
10781077
{ PyErr::warn(py, &cls, ffi::c_str!("I am warning you"), 0).unwrap() },

src/instance.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2171,7 +2171,7 @@ impl Py<PyAny> {
21712171
#[cfg(test)]
21722172
mod tests {
21732173
use super::{Bound, IntoPyObject, Py};
2174-
use crate::tests::common::generate_unique_module_name;
2174+
use crate::test_utils::generate_unique_module_name;
21752175
use crate::types::{dict::IntoPyDict, PyAnyMethods, PyCapsule, PyDict, PyString};
21762176
use crate::{ffi, Borrowed, PyAny, PyResult, Python};
21772177
use pyo3_ffi::c_str;

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,8 @@ pub use inventory; // Re-exported for `#[pyclass]` and `#[pymethods]` with `mult
410410
/// Tests and helpers which reside inside PyO3's main library. Declared first so that macros
411411
/// are available in unit tests.
412412
#[cfg(test)]
413-
#[macro_use]
413+
mod test_utils;
414+
#[cfg(test)]
414415
mod tests;
415416

416417
// Macro dependencies, also contains macros exported for use across the codebase and

src/test_utils.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Brings in `test_utils` from the `tests` directory
2+
//
3+
// to make that file function (lots of references to `pyo3` within it) need
4+
// re-bind `crate` as pyo3
5+
use crate as pyo3;
6+
include!("../tests/test_utils/mod.rs");

src/tests/mod.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
#[macro_use]
2-
pub(crate) mod common {
3-
#[cfg(not(Py_GIL_DISABLED))]
4-
use crate as pyo3;
5-
include!("./common.rs");
6-
}
7-
81
/// Test macro hygiene - this is in the crate since we won't have
92
/// `pyo3` available in the crate root.
103
#[cfg(all(test, feature = "macros"))]

src/types/any.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1624,7 +1624,7 @@ mod tests {
16241624
use crate::{
16251625
basic::CompareOp,
16261626
ffi,
1627-
tests::common::generate_unique_module_name,
1627+
test_utils::generate_unique_module_name,
16281628
types::{IntoPyDict, PyAny, PyAnyMethods, PyBool, PyInt, PyList, PyModule, PyTypeMethods},
16291629
Bound, BoundObject, IntoPyObject, PyTypeInfo, Python,
16301630
};

src/types/typeobject.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ impl<'py> PyTypeMethods<'py> for Bound<'py, PyType> {
244244

245245
#[cfg(test)]
246246
mod tests {
247-
use crate::tests::common::generate_unique_module_name;
247+
use crate::test_utils::generate_unique_module_name;
248248
use crate::types::{PyAnyMethods, PyBool, PyInt, PyModule, PyTuple, PyType, PyTypeMethods};
249249
use crate::PyAny;
250250
use crate::Python;

0 commit comments

Comments
 (0)