Lock other threads before import datetime to avoid test failure

This commit is contained in:
kngwyu 2020-04-09 22:11:03 +09:00
parent af553685f8
commit 9dc3f9dc1a
1 changed files with 15 additions and 1 deletions

16
tests/test_datetime.rs Normal file → Executable file
View File

@ -54,10 +54,21 @@ macro_rules! assert_check_only {
};
}
// Because of the relase pool unsoundness reported in https://github.com/PyO3/pyo3/issues/756,
// we need to stop other threads before calling `py.import()`.
// TODO(kngwyu): Remove this macro
macro_rules! lock {
() => {
let _mutex = std::sync::Mutex::new(());
let _lock = _mutex.lock().unwrap();
};
}
#[test]
fn test_date_check() {
let gil = Python::acquire_gil();
let py = gil.python();
lock!();
let (obj, sub_obj, sub_sub_obj) = _get_subclasses(&py, "date", "2018, 1, 1").unwrap();
assert_check_exact!(PyDate_Check, obj);
@ -69,6 +80,7 @@ fn test_date_check() {
fn test_time_check() {
let gil = Python::acquire_gil();
let py = gil.python();
lock!();
let (obj, sub_obj, sub_sub_obj) = _get_subclasses(&py, "time", "12, 30, 15").unwrap();
assert_check_exact!(PyTime_Check, obj);
@ -80,6 +92,7 @@ fn test_time_check() {
fn test_datetime_check() {
let gil = Python::acquire_gil();
let py = gil.python();
lock!();
let (obj, sub_obj, sub_sub_obj) = _get_subclasses(&py, "datetime", "2018, 1, 1, 13, 30, 15")
.map_err(|e| e.print(py))
.unwrap();
@ -94,6 +107,7 @@ fn test_datetime_check() {
fn test_delta_check() {
let gil = Python::acquire_gil();
let py = gil.python();
lock!();
let (obj, sub_obj, sub_sub_obj) = _get_subclasses(&py, "timedelta", "1, -3").unwrap();
assert_check_exact!(PyDelta_Check, obj);
@ -108,7 +122,7 @@ fn test_datetime_utc() {
let gil = Python::acquire_gil();
let py = gil.python();
lock!();
let datetime = py.import("datetime").map_err(|e| e.print(py)).unwrap();
let timezone = datetime.get("timezone").unwrap();
let utc = timezone.getattr("utc").unwrap().to_object(py);