2020-12-15 15:04:40 +00:00
|
|
|
#![cfg(not(Py_LIMITED_API))]
|
|
|
|
|
2018-08-21 18:14:01 +00:00
|
|
|
use pyo3::prelude::*;
|
2020-03-18 04:31:22 +00:00
|
|
|
use pyo3::types::IntoPyDict;
|
2022-01-25 13:46:27 +00:00
|
|
|
use pyo3_ffi::PyDateTime_IMPORT;
|
2018-08-09 18:17:34 +00:00
|
|
|
|
2018-08-09 20:29:59 +00:00
|
|
|
fn _get_subclasses<'p>(
|
2022-03-23 07:07:28 +00:00
|
|
|
py: Python<'p>,
|
2018-08-09 20:29:59 +00:00
|
|
|
py_type: &str,
|
|
|
|
args: &str,
|
2019-03-04 04:50:43 +00:00
|
|
|
) -> PyResult<(&'p PyAny, &'p PyAny, &'p PyAny)> {
|
2018-08-09 18:17:34 +00:00
|
|
|
// Import the class from Python and create some subclasses
|
2018-08-20 19:11:54 +00:00
|
|
|
let datetime = py.import("datetime")?;
|
2018-08-09 18:17:34 +00:00
|
|
|
|
2022-03-23 07:07:28 +00:00
|
|
|
let locals = [(py_type, datetime.getattr(py_type)?)].into_py_dict(py);
|
2018-08-09 18:17:34 +00:00
|
|
|
|
2018-08-09 20:29:59 +00:00
|
|
|
let make_subclass_py = format!("class Subklass({}):\n pass", py_type);
|
2018-08-09 18:17:34 +00:00
|
|
|
|
2018-08-09 20:29:59 +00:00
|
|
|
let make_sub_subclass_py = "class SubSubklass(Subklass):\n pass";
|
2018-08-09 18:17:34 +00:00
|
|
|
|
2021-07-30 22:30:56 +00:00
|
|
|
py.run(&make_subclass_py, None, Some(locals))?;
|
|
|
|
py.run(make_sub_subclass_py, None, Some(locals))?;
|
2018-08-09 18:17:34 +00:00
|
|
|
|
|
|
|
// Construct an instance of the base class
|
2021-07-30 22:30:56 +00:00
|
|
|
let obj = py.eval(&format!("{}({})", py_type, args), None, Some(locals))?;
|
2018-08-09 18:17:34 +00:00
|
|
|
|
|
|
|
// Construct an instance of the subclass
|
2021-07-30 22:30:56 +00:00
|
|
|
let sub_obj = py.eval(&format!("Subklass({})", args), None, Some(locals))?;
|
2018-08-09 18:17:34 +00:00
|
|
|
|
|
|
|
// Construct an instance of the sub-subclass
|
2021-07-30 22:30:56 +00:00
|
|
|
let sub_sub_obj = py.eval(&format!("SubSubklass({})", args), None, Some(locals))?;
|
2018-08-09 18:17:34 +00:00
|
|
|
|
2018-08-20 19:11:54 +00:00
|
|
|
Ok((obj, sub_obj, sub_sub_obj))
|
2018-08-09 18:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! assert_check_exact {
|
2021-12-03 00:03:32 +00:00
|
|
|
($check_func:ident, $check_func_exact:ident, $obj: expr) => {
|
2018-08-09 18:17:34 +00:00
|
|
|
unsafe {
|
2021-12-03 00:03:32 +00:00
|
|
|
use pyo3::{ffi::*, AsPyPointer};
|
2018-08-09 18:17:34 +00:00
|
|
|
assert!($check_func(($obj).as_ptr()) != 0);
|
2021-12-03 00:03:32 +00:00
|
|
|
assert!($check_func_exact(($obj).as_ptr()) != 0);
|
2018-08-09 18:17:34 +00:00
|
|
|
}
|
2018-08-09 20:29:59 +00:00
|
|
|
};
|
2018-08-09 18:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! assert_check_only {
|
2021-12-03 00:03:32 +00:00
|
|
|
($check_func:ident, $check_func_exact:ident, $obj: expr) => {
|
2018-08-09 18:17:34 +00:00
|
|
|
unsafe {
|
2021-12-03 00:03:32 +00:00
|
|
|
use pyo3::{ffi::*, AsPyPointer};
|
2018-08-09 18:17:34 +00:00
|
|
|
assert!($check_func(($obj).as_ptr()) != 0);
|
2021-12-03 00:03:32 +00:00
|
|
|
assert!($check_func_exact(($obj).as_ptr()) == 0);
|
2018-08-09 18:17:34 +00:00
|
|
|
}
|
2018-08-09 20:29:59 +00:00
|
|
|
};
|
2018-08-09 18:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_date_check() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
2022-03-23 07:07:28 +00:00
|
|
|
let (obj, sub_obj, sub_sub_obj) = _get_subclasses(py, "date", "2018, 1, 1").unwrap();
|
2022-01-25 13:46:27 +00:00
|
|
|
unsafe { PyDateTime_IMPORT() }
|
2021-12-03 00:03:32 +00:00
|
|
|
assert_check_exact!(PyDate_Check, PyDate_CheckExact, obj);
|
|
|
|
assert_check_only!(PyDate_Check, PyDate_CheckExact, sub_obj);
|
|
|
|
assert_check_only!(PyDate_Check, PyDate_CheckExact, sub_sub_obj);
|
2018-08-09 18:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_time_check() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
2022-03-23 07:07:28 +00:00
|
|
|
let (obj, sub_obj, sub_sub_obj) = _get_subclasses(py, "time", "12, 30, 15").unwrap();
|
2022-01-25 13:46:27 +00:00
|
|
|
unsafe { PyDateTime_IMPORT() }
|
2018-08-09 18:17:34 +00:00
|
|
|
|
2021-12-03 00:03:32 +00:00
|
|
|
assert_check_exact!(PyTime_Check, PyTime_CheckExact, obj);
|
|
|
|
assert_check_only!(PyTime_Check, PyTime_CheckExact, sub_obj);
|
|
|
|
assert_check_only!(PyTime_Check, PyTime_CheckExact, sub_sub_obj);
|
2018-08-09 18:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_datetime_check() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
2022-03-23 07:07:28 +00:00
|
|
|
let (obj, sub_obj, sub_sub_obj) = _get_subclasses(py, "datetime", "2018, 1, 1, 13, 30, 15")
|
2020-01-12 14:44:15 +00:00
|
|
|
.map_err(|e| e.print(py))
|
|
|
|
.unwrap();
|
2022-01-25 13:46:27 +00:00
|
|
|
unsafe { PyDateTime_IMPORT() }
|
2018-08-09 18:17:34 +00:00
|
|
|
|
2021-12-03 00:03:32 +00:00
|
|
|
assert_check_only!(PyDate_Check, PyDate_CheckExact, obj);
|
|
|
|
assert_check_exact!(PyDateTime_Check, PyDateTime_CheckExact, obj);
|
|
|
|
assert_check_only!(PyDateTime_Check, PyDateTime_CheckExact, sub_obj);
|
|
|
|
assert_check_only!(PyDateTime_Check, PyDateTime_CheckExact, sub_sub_obj);
|
2018-08-09 18:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_delta_check() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
2022-03-23 07:07:28 +00:00
|
|
|
let (obj, sub_obj, sub_sub_obj) = _get_subclasses(py, "timedelta", "1, -3").unwrap();
|
2022-01-25 13:46:27 +00:00
|
|
|
unsafe { PyDateTime_IMPORT() }
|
2018-08-09 18:17:34 +00:00
|
|
|
|
2021-12-03 00:03:32 +00:00
|
|
|
assert_check_exact!(PyDelta_Check, PyDelta_CheckExact, obj);
|
|
|
|
assert_check_only!(PyDelta_Check, PyDelta_CheckExact, sub_obj);
|
|
|
|
assert_check_only!(PyDelta_Check, PyDelta_CheckExact, sub_sub_obj);
|
2018-08-09 18:17:34 +00:00
|
|
|
}
|
2018-08-20 17:31:39 +00:00
|
|
|
|
2018-08-20 17:52:19 +00:00
|
|
|
#[test]
|
|
|
|
fn test_datetime_utc() {
|
2019-08-17 12:10:36 +00:00
|
|
|
use assert_approx_eq::assert_approx_eq;
|
2019-02-23 17:42:40 +00:00
|
|
|
use pyo3::types::PyDateTime;
|
|
|
|
|
2018-08-20 17:52:19 +00:00
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
let datetime = py.import("datetime").map_err(|e| e.print(py)).unwrap();
|
2021-03-13 17:36:17 +00:00
|
|
|
let timezone = datetime.getattr("timezone").unwrap();
|
2018-08-20 17:52:19 +00:00
|
|
|
let utc = timezone.getattr("utc").unwrap().to_object(py);
|
|
|
|
|
|
|
|
let dt = PyDateTime::new(py, 2018, 1, 1, 0, 0, 0, 0, Some(&utc)).unwrap();
|
|
|
|
|
2019-03-20 18:37:27 +00:00
|
|
|
let locals = [("dt", dt)].into_py_dict(py);
|
2018-08-20 17:52:19 +00:00
|
|
|
|
|
|
|
let offset: f32 = py
|
|
|
|
.eval("dt.utcoffset().total_seconds()", None, Some(locals))
|
|
|
|
.unwrap()
|
|
|
|
.extract()
|
|
|
|
.unwrap();
|
2019-08-17 12:10:36 +00:00
|
|
|
assert_approx_eq!(offset, 0f32);
|
2018-08-20 17:52:19 +00:00
|
|
|
}
|
|
|
|
|
2019-08-17 12:10:36 +00:00
|
|
|
static INVALID_DATES: &[(i32, u8, u8)] = &[
|
2018-08-21 18:14:01 +00:00
|
|
|
(-1, 1, 1),
|
|
|
|
(0, 1, 1),
|
|
|
|
(10000, 1, 1),
|
|
|
|
(2 << 30, 1, 1),
|
|
|
|
(2018, 0, 1),
|
|
|
|
(2018, 13, 1),
|
|
|
|
(2018, 1, 0),
|
|
|
|
(2017, 2, 29),
|
|
|
|
(2018, 1, 32),
|
2018-08-21 17:43:32 +00:00
|
|
|
];
|
|
|
|
|
2019-08-17 12:10:36 +00:00
|
|
|
static INVALID_TIMES: &[(u8, u8, u8, u32)] =
|
2018-08-21 18:14:01 +00:00
|
|
|
&[(25, 0, 0, 0), (255, 0, 0, 0), (0, 60, 0, 0), (0, 0, 61, 0)];
|
2018-08-21 17:43:32 +00:00
|
|
|
|
2018-08-20 17:31:39 +00:00
|
|
|
#[test]
|
|
|
|
fn test_pydate_out_of_bounds() {
|
2019-02-23 17:42:40 +00:00
|
|
|
use pyo3::types::PyDate;
|
|
|
|
|
2018-08-20 17:31:39 +00:00
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
2019-08-17 13:27:05 +00:00
|
|
|
for val in INVALID_DATES {
|
2018-08-20 17:31:39 +00:00
|
|
|
let (year, month, day) = val;
|
|
|
|
let dt = PyDate::new(py, *year, *month, *day);
|
2019-02-23 17:38:00 +00:00
|
|
|
dt.unwrap_err();
|
2018-08-20 17:31:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_pytime_out_of_bounds() {
|
2019-02-23 17:42:40 +00:00
|
|
|
use pyo3::types::PyTime;
|
|
|
|
|
2018-08-20 17:31:39 +00:00
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
2019-02-23 17:38:00 +00:00
|
|
|
for val in INVALID_TIMES {
|
2018-08-20 17:31:39 +00:00
|
|
|
let (hour, minute, second, microsecond) = val;
|
|
|
|
let dt = PyTime::new(py, *hour, *minute, *second, *microsecond, None);
|
2019-02-23 17:38:00 +00:00
|
|
|
dt.unwrap_err();
|
2018-08-20 17:31:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_pydatetime_out_of_bounds() {
|
2019-02-23 17:42:40 +00:00
|
|
|
use pyo3::types::PyDateTime;
|
|
|
|
use std::iter;
|
|
|
|
|
2018-08-20 17:31:39 +00:00
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
2018-08-21 17:43:32 +00:00
|
|
|
let valid_time = (0, 0, 0, 0);
|
|
|
|
let valid_date = (2018, 1, 1);
|
|
|
|
|
2019-02-23 17:38:00 +00:00
|
|
|
let invalid_dates = INVALID_DATES.iter().zip(iter::repeat(&valid_time));
|
|
|
|
let invalid_times = iter::repeat(&valid_date).zip(INVALID_TIMES.iter());
|
2018-08-21 17:43:32 +00:00
|
|
|
|
|
|
|
let vals = invalid_dates.chain(invalid_times);
|
|
|
|
|
|
|
|
for val in vals {
|
|
|
|
let (date, time) = val;
|
|
|
|
let (year, month, day) = date;
|
|
|
|
let (hour, minute, second, microsecond) = time;
|
2018-08-20 17:31:39 +00:00
|
|
|
let dt = PyDateTime::new(
|
|
|
|
py,
|
|
|
|
*year,
|
|
|
|
*month,
|
|
|
|
*day,
|
|
|
|
*hour,
|
|
|
|
*minute,
|
|
|
|
*second,
|
|
|
|
*microsecond,
|
|
|
|
None,
|
|
|
|
);
|
2019-02-23 17:38:00 +00:00
|
|
|
dt.unwrap_err();
|
2018-08-20 17:31:39 +00:00
|
|
|
}
|
|
|
|
}
|