From 391a375bae332531cde46f70b222de4de47e8c71 Mon Sep 17 00:00:00 2001 From: David Hewitt <1939362+davidhewitt@users.noreply.github.com> Date: Tue, 12 Apr 2022 07:46:38 +0100 Subject: [PATCH] ffi: add BaseDateTime and BaseTime definitions --- CHANGELOG.md | 1 + pyo3-ffi/src/datetime.rs | 41 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c2b74a3..984461e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Add an experimental `generate-abi3-import-lib` feature to auto-generate `python3.dll` import libraries for Windows. [#2282](https://github.com/PyO3/pyo3/pull/2282) +- Add FFI definitions for `PyDateTime_BaseTime` and `PyDateTime_BaseDateTime`. [#2294](https://github.com/PyO3/pyo3/pull/2294) ### Changed diff --git a/pyo3-ffi/src/datetime.rs b/pyo3-ffi/src/datetime.rs index 2b1194c5..74f83bbf 100644 --- a/pyo3-ffi/src/datetime.rs +++ b/pyo3-ffi/src/datetime.rs @@ -38,11 +38,29 @@ pub struct PyDateTime_Delta { // skipped non-limited PyDateTime_TZInfo // skipped non-limited _PyDateTime_BaseTZInfo -// skipped non-limited _PyDateTime_BaseTime + +#[repr(C)] +#[derive(Debug, Copy, Clone)] +/// Structure representing a `datetime.time` without a `tzinfo` member. +pub struct PyDateTime_BaseTime { + pub ob_base: PyObject, + #[cfg(not(PyPy))] + pub hashcode: Py_hash_t, + pub hastzinfo: c_char, + #[cfg(not(PyPy))] + pub data: [c_uchar; _PyDateTime_TIME_DATASIZE], + #[cfg(not(PyPy))] + pub fold: c_uchar, +} #[repr(C)] #[derive(Debug, Copy, Clone)] /// Structure representing a `datetime.time`. +/// +/// # Safety +/// +/// Care should be taken when reading the `tzinfo` field of this type. If the time does not have a +/// tzinfo then the Python interpreter is free to allocate it as a [PyDateTime_BaseTime]. pub struct PyDateTime_Time { pub ob_base: PyObject, #[cfg(not(PyPy))] @@ -66,11 +84,28 @@ pub struct PyDateTime_Date { pub data: [c_uchar; _PyDateTime_DATE_DATASIZE], } -// skipped non-limited _PyDateTime_BaseDateTime +#[repr(C)] +#[derive(Debug, Copy, Clone)] +/// Structure representing a `datetime.datetime` without a `tzinfo` member. +pub struct PyDateTime_BaseDateTime { + pub ob_base: PyObject, + #[cfg(not(PyPy))] + pub hashcode: Py_hash_t, + pub hastzinfo: c_char, + #[cfg(not(PyPy))] + pub data: [c_uchar; _PyDateTime_DATETIME_DATASIZE], + #[cfg(not(PyPy))] + pub fold: c_uchar, +} #[repr(C)] #[derive(Debug, Copy, Clone)] -/// Structure representing a `datetime.datetime` +/// Structure representing a `datetime.datetime`. +/// +/// # Safety +/// +/// Care should be taken when reading the `tzinfo` field of this type. If the datetime does not have a +/// tzinfo then the Python interpreter is free to allocate it as a [PyDateTime_BaseDateTime]. pub struct PyDateTime_DateTime { pub ob_base: PyObject, #[cfg(not(PyPy))]