1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
#![cfg(not(Py_LIMITED_API))]
use pyo3::prelude::*;
use pyo3::types::{timezone_utc_bound, IntoPyDict, PyDate, PyDateTime, PyTime};
use pyo3_ffi::PyDateTime_IMPORT;
fn _get_subclasses<'py>(
py: Python<'py>,
py_type: &str,
args: &str,
) -> PyResult<(Bound<'py, PyAny>, Bound<'py, PyAny>, Bound<'py, PyAny>)> {
// Import the class from Python and create some subclasses
let datetime = py.import_bound("datetime")?;
let locals = [(py_type, datetime.getattr(py_type)?)].into_py_dict_bound(py);
let make_subclass_py = format!("class Subklass({}):\n pass", py_type);
let make_sub_subclass_py = "class SubSubklass(Subklass):\n pass";
py.run_bound(&make_subclass_py, None, Some(&locals))?;
py.run_bound(make_sub_subclass_py, None, Some(&locals))?;
// Construct an instance of the base class
let obj = py.eval_bound(&format!("{}({})", py_type, args), None, Some(&locals))?;
// Construct an instance of the subclass
let sub_obj = py.eval_bound(&format!("Subklass({})", args), None, Some(&locals))?;
// Construct an instance of the sub-subclass
let sub_sub_obj = py.eval_bound(&format!("SubSubklass({})", args), None, Some(&locals))?;
Ok((obj, sub_obj, sub_sub_obj))
}
macro_rules! assert_check_exact {
($check_func:ident, $check_func_exact:ident, $obj: expr) => {
unsafe {
use pyo3::ffi::*;
assert!($check_func(($obj).as_ptr()) != 0);
assert!($check_func_exact(($obj).as_ptr()) != 0);
}
};
}
macro_rules! assert_check_only {
($check_func:ident, $check_func_exact:ident, $obj: expr) => {
unsafe {
use pyo3::ffi::*;
assert!($check_func(($obj).as_ptr()) != 0);
assert!($check_func_exact(($obj).as_ptr()) == 0);
}
};
}
#[test]
fn test_date_check() {
Python::with_gil(|py| {
let (obj, sub_obj, sub_sub_obj) = _get_subclasses(py, "date", "2018, 1, 1").unwrap();
unsafe { PyDateTime_IMPORT() }
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);
assert!(obj.is_instance_of::<PyDate>());
assert!(!obj.is_instance_of::<PyTime>());
assert!(!obj.is_instance_of::<PyDateTime>());
});
}
#[test]
fn test_time_check() {
Python::with_gil(|py| {
let (obj, sub_obj, sub_sub_obj) = _get_subclasses(py, "time", "12, 30, 15").unwrap();
unsafe { PyDateTime_IMPORT() }
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);
assert!(!obj.is_instance_of::<PyDate>());
assert!(obj.is_instance_of::<PyTime>());
assert!(!obj.is_instance_of::<PyDateTime>());
});
}
#[test]
fn test_datetime_check() {
Python::with_gil(|py| {
let (obj, sub_obj, sub_sub_obj) = _get_subclasses(py, "datetime", "2018, 1, 1, 13, 30, 15")
.map_err(|e| e.display(py))
.unwrap();
unsafe { PyDateTime_IMPORT() }
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);
assert!(obj.is_instance_of::<PyDate>());
assert!(!obj.is_instance_of::<PyTime>());
assert!(obj.is_instance_of::<PyDateTime>());
});
}
#[test]
fn test_delta_check() {
Python::with_gil(|py| {
let (obj, sub_obj, sub_sub_obj) = _get_subclasses(py, "timedelta", "1, -3").unwrap();
unsafe { PyDateTime_IMPORT() }
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);
});
}
#[test]
fn test_datetime_utc() {
use assert_approx_eq::assert_approx_eq;
use pyo3::types::PyDateTime;
Python::with_gil(|py| {
let utc = timezone_utc_bound(py);
let dt = PyDateTime::new_bound(py, 2018, 1, 1, 0, 0, 0, 0, Some(&utc)).unwrap();
let locals = [("dt", dt)].into_py_dict_bound(py);
let offset: f32 = py
.eval_bound("dt.utcoffset().total_seconds()", None, Some(&locals))
.unwrap()
.extract()
.unwrap();
assert_approx_eq!(offset, 0f32);
});
}
static INVALID_DATES: &[(i32, u8, u8)] = &[
(-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),
];
static INVALID_TIMES: &[(u8, u8, u8, u32)] =
&[(25, 0, 0, 0), (255, 0, 0, 0), (0, 60, 0, 0), (0, 0, 61, 0)];
#[test]
fn test_pydate_out_of_bounds() {
use pyo3::types::PyDate;
Python::with_gil(|py| {
for val in INVALID_DATES {
let (year, month, day) = val;
let dt = PyDate::new_bound(py, *year, *month, *day);
dt.unwrap_err();
}
});
}
#[test]
fn test_pytime_out_of_bounds() {
use pyo3::types::PyTime;
Python::with_gil(|py| {
for val in INVALID_TIMES {
let (hour, minute, second, microsecond) = val;
let dt = PyTime::new_bound(py, *hour, *minute, *second, *microsecond, None);
dt.unwrap_err();
}
});
}
#[test]
fn test_pydatetime_out_of_bounds() {
use pyo3::types::PyDateTime;
use std::iter;
Python::with_gil(|py| {
let valid_time = (0, 0, 0, 0);
let valid_date = (2018, 1, 1);
let invalid_dates = INVALID_DATES.iter().zip(iter::repeat(&valid_time));
let invalid_times = iter::repeat(&valid_date).zip(INVALID_TIMES.iter());
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;
let dt = PyDateTime::new_bound(
py,
*year,
*month,
*day,
*hour,
*minute,
*second,
*microsecond,
None,
);
dt.unwrap_err();
}
});
}
|