Skip to content

Commit

Permalink
ffi: add FFI definition PyDateTime_CAPSULE_NAME
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Oct 19, 2024
1 parent 6e7a578 commit b056850
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
1 change: 1 addition & 0 deletions newsfragments/4634.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add FFI definition `PyDateTime_CAPSULE_NAME`.
22 changes: 9 additions & 13 deletions pyo3-ffi/src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
#[cfg(GraalPy)]
use crate::{PyLong_AsLong, PyLong_Check, PyObject_GetAttrString, Py_DecRef};
use crate::{PyObject, PyObject_TypeCheck, PyTypeObject, Py_TYPE};
use std::cell::UnsafeCell;
#[cfg(not(GraalPy))]
use std::os::raw::c_char;
use std::os::raw::c_int;
use std::ptr;
use std::{cell::UnsafeCell, ffi::CStr};
#[cfg(not(PyPy))]
use {crate::PyCapsule_Import, std::ffi::CString};
#[cfg(not(any(PyPy, GraalPy)))]
Expand Down Expand Up @@ -593,6 +593,8 @@ pub struct PyDateTime_CAPI {
// Python already shares this object between threads, so it's no more evil for us to do it too!
unsafe impl Sync for PyDateTime_CAPI {}

pub const PyDateTime_CAPSULE_NAME: &CStr = c_str!("datetime.datetime_CAPI");

/// Returns a pointer to a `PyDateTime_CAPI` instance
///
/// # Note
Expand All @@ -603,11 +605,6 @@ pub unsafe fn PyDateTimeAPI() -> *mut PyDateTime_CAPI {
*PyDateTimeAPI_impl.0.get()
}

#[inline]
pub unsafe fn PyDateTime_TimeZone_UTC() -> *mut PyObject {
(*PyDateTimeAPI()).TimeZone_UTC
}

/// Populates the `PyDateTimeAPI` object
pub unsafe fn PyDateTime_IMPORT() {
// PyPy expects the C-API to be initialized via PyDateTime_Import, so trying to use
Expand All @@ -616,17 +613,16 @@ pub unsafe fn PyDateTime_IMPORT() {
let py_datetime_c_api = PyDateTime_Import();

#[cfg(not(PyPy))]
let py_datetime_c_api = {
// PyDateTime_CAPSULE_NAME is a macro in C
let PyDateTime_CAPSULE_NAME = CString::new("datetime.datetime_CAPI").unwrap();

PyCapsule_Import(PyDateTime_CAPSULE_NAME.as_ptr(), 1) as *mut PyDateTime_CAPI
};
let py_datetime_c_api =
PyCapsule_Import(PyDateTime_CAPSULE_NAME.as_ptr(), 1) as *mut PyDateTime_CAPI;

*PyDateTimeAPI_impl.0.get() = py_datetime_c_api;
}

// skipped non-limited PyDateTime_TimeZone_UTC
#[inline]
pub unsafe fn PyDateTime_TimeZone_UTC() -> *mut PyObject {
(*PyDateTimeAPI()).TimeZone_UTC
}

/// Type Check macros
///
Expand Down

0 comments on commit b056850

Please sign in to comment.