Skip to content

Commit

Permalink
fix chrono::DateTime<Tz> intoPyObject conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
bschoenmaeckers committed Dec 11, 2024
1 parent 926f5cf commit 1456bc1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
1 change: 1 addition & 0 deletions newsfragments/4790.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fix chrono::DateTime<Tz> intoPyObject conversion when `Tz` is `chrono_tz::Tz`
22 changes: 16 additions & 6 deletions src/conversions/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use crate::types::{
timezone_utc, PyDate, PyDateAccess, PyDateTime, PyDelta, PyDeltaAccess, PyTime, PyTimeAccess,
PyTzInfo, PyTzInfoAccess,
};
use crate::{ffi, Bound, FromPyObject, PyAny, PyErr, PyObject, PyResult, Python};
use crate::{ffi, Bound, FromPyObject, IntoPyObjectExt, PyAny, PyErr, PyObject, PyResult, Python};
#[cfg(Py_LIMITED_API)]
use crate::{intern, DowncastError};
#[allow(deprecated)]
Expand Down Expand Up @@ -418,11 +418,14 @@ impl<Tz: TimeZone> ToPyObject for DateTime<Tz> {
#[allow(deprecated)]
impl<Tz: TimeZone> IntoPy<PyObject> for DateTime<Tz> {
fn into_py(self, py: Python<'_>) -> PyObject {
self.into_pyobject(py).unwrap().into_any().unbind()
self.to_object(py)
}
}

impl<'py, Tz: TimeZone> IntoPyObject<'py> for DateTime<Tz> {
impl<'py, Tz: TimeZone> IntoPyObject<'py> for DateTime<Tz>
where
Tz: IntoPyObject<'py>,
{
#[cfg(Py_LIMITED_API)]
type Target = PyAny;
#[cfg(not(Py_LIMITED_API))]
Expand All @@ -436,7 +439,10 @@ impl<'py, Tz: TimeZone> IntoPyObject<'py> for DateTime<Tz> {
}
}

impl<'py, Tz: TimeZone> IntoPyObject<'py> for &DateTime<Tz> {
impl<'py, Tz: TimeZone> IntoPyObject<'py> for &DateTime<Tz>
where
Tz: IntoPyObject<'py>,
{
#[cfg(Py_LIMITED_API)]
type Target = PyAny;
#[cfg(not(Py_LIMITED_API))]
Expand All @@ -445,7 +451,11 @@ impl<'py, Tz: TimeZone> IntoPyObject<'py> for &DateTime<Tz> {
type Error = PyErr;

fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
let tz = self.offset().fix().into_pyobject(py)?;
let tz = self.timezone().into_bound_py_any(py)?;

#[cfg(not(Py_LIMITED_API))]
let tz = tz.downcast()?;

let DateArgs { year, month, day } = (&self.naive_local().date()).into();
let TimeArgs {
hour,
Expand All @@ -456,7 +466,7 @@ impl<'py, Tz: TimeZone> IntoPyObject<'py> for &DateTime<Tz> {
} = (&self.naive_local().time()).into();

#[cfg(not(Py_LIMITED_API))]
let datetime = PyDateTime::new(py, year, month, day, hour, min, sec, micro, Some(&tz))?;
let datetime = PyDateTime::new(py, year, month, day, hour, min, sec, micro, Some(tz))?;

#[cfg(Py_LIMITED_API)]
let datetime = DatetimeTypes::try_get(py).and_then(|dt| {
Expand Down

0 comments on commit 1456bc1

Please sign in to comment.