Skip to content

Commit 0062d5e

Browse files
authored
ffi: expose PyDateTime_*_GET_TZINFO on PyPy (PyO3#5079)
They are available in PyPy3.10 and PyPy3.11.
1 parent 3ae3a6b commit 0062d5e

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

newsfragments/5079.changed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Expose `PyDateTime_DATE_GET_TZINFO` and `PyDateTime_TIME_GET_TZINFO` on PyPy 3.10 and later

pyo3-ffi/src/datetime.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,9 @@ extern "C" {
487487
pub fn PyDateTime_DATE_GET_MICROSECOND(o: *mut PyObject) -> c_int;
488488
#[link_name = "PyPyDateTime_GET_FOLD"]
489489
pub fn PyDateTime_DATE_GET_FOLD(o: *mut PyObject) -> c_int;
490-
// skipped PyDateTime_DATE_GET_TZINFO (not in PyPy)
490+
#[link_name = "PyPyDateTime_DATE_GET_TZINFO"]
491+
#[cfg(Py_3_10)]
492+
pub fn PyDateTime_DATE_GET_TZINFO(o: *mut PyObject) -> *mut PyObject;
491493

492494
#[link_name = "PyPyDateTime_TIME_GET_HOUR"]
493495
pub fn PyDateTime_TIME_GET_HOUR(o: *mut PyObject) -> c_int;
@@ -499,7 +501,9 @@ extern "C" {
499501
pub fn PyDateTime_TIME_GET_MICROSECOND(o: *mut PyObject) -> c_int;
500502
#[link_name = "PyPyDateTime_TIME_GET_FOLD"]
501503
pub fn PyDateTime_TIME_GET_FOLD(o: *mut PyObject) -> c_int;
502-
// skipped PyDateTime_TIME_GET_TZINFO (not in PyPy)
504+
#[link_name = "PyPyDateTime_TIME_GET_TZINFO"]
505+
#[cfg(Py_3_10)]
506+
pub fn PyDateTime_TIME_GET_TZINFO(o: *mut PyObject) -> *mut PyObject;
503507

504508
#[link_name = "PyPyDateTime_DELTA_GET_DAYS"]
505509
pub fn PyDateTime_DELTA_GET_DAYS(o: *mut PyObject) -> c_int;

src/types/datetime.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::ffi::{
1414
PyDateTime_TIME_GET_MICROSECOND, PyDateTime_TIME_GET_MINUTE, PyDateTime_TIME_GET_SECOND,
1515
PyDate_FromTimestamp,
1616
};
17-
#[cfg(all(GraalPy, not(Py_LIMITED_API)))]
17+
#[cfg(all(Py_3_10, not(Py_LIMITED_API)))]
1818
use crate::ffi::{PyDateTime_DATE_GET_TZINFO, PyDateTime_TIME_GET_TZINFO, Py_IsNone};
1919
use crate::types::any::PyAnyMethods;
2020
#[cfg(not(Py_LIMITED_API))]
@@ -518,11 +518,9 @@ impl PyTimeAccess for Bound<'_, PyDateTime> {
518518

519519
impl<'py> PyTzInfoAccess<'py> for Bound<'py, PyDateTime> {
520520
fn get_tzinfo(&self) -> Option<Bound<'py, PyTzInfo>> {
521-
#[cfg(not(Py_LIMITED_API))]
522-
let ptr = self.as_ptr() as *mut ffi::PyDateTime_DateTime;
523-
524-
#[cfg(not(any(GraalPy, Py_LIMITED_API)))]
521+
#[cfg(all(not(Py_3_10), not(Py_LIMITED_API)))]
525522
unsafe {
523+
let ptr = self.as_ptr() as *mut ffi::PyDateTime_DateTime;
526524
if (*ptr).hastzinfo != 0 {
527525
Some(
528526
(*ptr)
@@ -536,9 +534,9 @@ impl<'py> PyTzInfoAccess<'py> for Bound<'py, PyDateTime> {
536534
}
537535
}
538536

539-
#[cfg(all(GraalPy, not(Py_LIMITED_API)))]
537+
#[cfg(all(Py_3_10, not(Py_LIMITED_API)))]
540538
unsafe {
541-
let res = PyDateTime_DATE_GET_TZINFO(ptr as *mut ffi::PyObject);
539+
let res = PyDateTime_DATE_GET_TZINFO(self.as_ptr());
542540
if Py_IsNone(res) == 1 {
543541
None
544542
} else {
@@ -700,11 +698,9 @@ impl PyTimeAccess for Bound<'_, PyTime> {
700698

701699
impl<'py> PyTzInfoAccess<'py> for Bound<'py, PyTime> {
702700
fn get_tzinfo(&self) -> Option<Bound<'py, PyTzInfo>> {
703-
#[cfg(not(Py_LIMITED_API))]
704-
let ptr = self.as_ptr() as *mut ffi::PyDateTime_Time;
705-
706-
#[cfg(not(any(GraalPy, Py_LIMITED_API)))]
701+
#[cfg(all(not(Py_3_10), not(Py_LIMITED_API)))]
707702
unsafe {
703+
let ptr = self.as_ptr() as *mut ffi::PyDateTime_Time;
708704
if (*ptr).hastzinfo != 0 {
709705
Some(
710706
(*ptr)
@@ -718,9 +714,9 @@ impl<'py> PyTzInfoAccess<'py> for Bound<'py, PyTime> {
718714
}
719715
}
720716

721-
#[cfg(all(GraalPy, not(Py_LIMITED_API)))]
717+
#[cfg(all(Py_3_10, not(Py_LIMITED_API)))]
722718
unsafe {
723-
let res = PyDateTime_TIME_GET_TZINFO(ptr as *mut ffi::PyObject);
719+
let res = PyDateTime_TIME_GET_TZINFO(self.as_ptr());
724720
if Py_IsNone(res) == 1 {
725721
None
726722
} else {

0 commit comments

Comments
 (0)