Skip to content

Commit

Permalink
Add wrapper functions for the statically_allocated field
Browse files Browse the repository at this point in the history
  • Loading branch information
clin1234 committed Jan 23, 2025
1 parent 7791885 commit ab16e04
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
44 changes: 42 additions & 2 deletions pyo3-ffi/src/cpython/unicodeobject.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#[cfg(not(PyPy))]
use crate::Py_hash_t;
use crate::{PyObject, Py_UCS1, Py_UCS2, Py_UCS4, Py_ssize_t};
use libc::wchar_t;
use std::os::raw::{c_char, c_int, c_uint, c_void};
use libc::{c_ushort, wchar_t};
#[cfg(Py_3_14)]
use std::os::raw::c_ushort;
use std::os::raw::{c_char, c_int, c_uint, c_void};

// skipped Py_UNICODE_ISSPACE()
// skipped Py_UNICODE_ISLOWER()
Expand Down Expand Up @@ -587,6 +587,46 @@ impl PyASCIIObject {
state.set_ready(val);
self.state = u32::from(state);
}

/// Get the `statically_allocated` field of the [`PyASCIIObject`] state bitfield.
///
/// Returns either `0` or `1`.
#[inline]
#[cfg(all(Py_3_12, not(Py_3_14)))]
pub unsafe fn statically_allocated(&self) -> c_uint {
PyASCIIObjectState::from(self.state).statically_allocated()
}

/// Set the `statically_allocated` flag of the [`PyASCIIObject`] state bitfield.
///
/// Calling this function with an argument that is neither `0` nor `1` is invalid.
#[inline]
#[cfg(all(Py_3_12, not(Py_3_14)))]
pub unsafe fn set_statically_allocated(&mut self, val: c_uint) {
let mut state = PyASCIIObjectState::from(self.state);
state.set_statically_allocated(val);
self.state = u32::from(state);
}

/// Get the `statically_allocated` field of the [`PyASCIIObject`] state bitfield.
///
/// Returns either `0` or `1`.
#[inline]
#[cfg(Py_3_14)]
pub unsafe fn statically_allocated(&self) -> c_ushort {
PyASCIIObjectState::from(self.state).statically_allocated()
}

/// Set the `statically_allocated` flag of the [`PyASCIIObject`] state bitfield.
///
/// Calling this function with an argument that is neither `0` nor `1` is invalid.
#[inline]
#[cfg(Py_3_14)]
pub unsafe fn set_statically_allocated(&mut self, val: c_ushort) {
let mut state = PyASCIIObjectState::from(self.state);
state.set_statically_allocated(val);
self.state = u32::from(state);
}
}

#[repr(C)]
Expand Down
5 changes: 5 additions & 0 deletions src/ffi/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ fn ascii_object_bitfield() {
o.set_ready(1);
#[cfg(not(Py_3_12))]
assert_eq!(o.ready(), 1);

#[cfg(Py_3_12)]
o.set_statically_allocated(1);
#[cfg(Py_3_12)]
assert_eq!(o.statically_allocated(), 1);
}
}

Expand Down

0 comments on commit ab16e04

Please sign in to comment.