Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable PyUnicode_DATA on PyPy #4116

Merged
merged 3 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/4116.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Disable `PyUnicode_DATA` on PyPy: Not exposed by PyPy.
10 changes: 5 additions & 5 deletions pyo3-ffi/src/cpython/unicodeobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,19 +449,19 @@ pub const PyUnicode_1BYTE_KIND: c_uint = 1;
pub const PyUnicode_2BYTE_KIND: c_uint = 2;
pub const PyUnicode_4BYTE_KIND: c_uint = 4;

#[cfg(not(GraalPy))]
#[cfg(not(any(GraalPy, PyPy)))]
#[inline]
pub unsafe fn PyUnicode_1BYTE_DATA(op: *mut PyObject) -> *mut Py_UCS1 {
PyUnicode_DATA(op) as *mut Py_UCS1
}

#[cfg(not(GraalPy))]
#[cfg(not(any(GraalPy, PyPy)))]
#[inline]
pub unsafe fn PyUnicode_2BYTE_DATA(op: *mut PyObject) -> *mut Py_UCS2 {
PyUnicode_DATA(op) as *mut Py_UCS2
}

#[cfg(not(GraalPy))]
#[cfg(not(any(GraalPy, PyPy)))]
#[inline]
pub unsafe fn PyUnicode_4BYTE_DATA(op: *mut PyObject) -> *mut Py_UCS4 {
PyUnicode_DATA(op) as *mut Py_UCS4
Expand All @@ -487,15 +487,15 @@ pub unsafe fn _PyUnicode_COMPACT_DATA(op: *mut PyObject) -> *mut c_void {
}
}

#[cfg(not(GraalPy))]
#[cfg(not(any(GraalPy, PyPy)))]
#[inline]
pub unsafe fn _PyUnicode_NONCOMPACT_DATA(op: *mut PyObject) -> *mut c_void {
debug_assert!(!(*(op as *mut PyUnicodeObject)).data.any.is_null());

(*(op as *mut PyUnicodeObject)).data.any
}

#[cfg(not(GraalPy))]
#[cfg(not(any(GraalPy, PyPy)))]
#[inline]
pub unsafe fn PyUnicode_DATA(op: *mut PyObject) -> *mut c_void {
debug_assert!(crate::PyUnicode_Check(op) != 0);
Expand Down
15 changes: 9 additions & 6 deletions src/ffi/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ use crate::ffi::*;
use crate::types::any::PyAnyMethods;
use crate::Python;

#[cfg(all(PyPy, feature = "macros"))]
use crate::types::PyString;

#[cfg(not(any(Py_LIMITED_API, PyPy)))]
use crate::types::PyString;

#[cfg(not(Py_LIMITED_API))]
use crate::{
types::{PyDict, PyString},
Bound, IntoPy, Py, PyAny,
};
use crate::{types::PyDict, Bound, IntoPy, Py, PyAny};
#[cfg(not(any(Py_3_12, Py_LIMITED_API)))]
use libc::wchar_t;

Expand Down Expand Up @@ -160,7 +163,7 @@ fn ascii_object_bitfield() {
}

#[test]
#[cfg(not(Py_LIMITED_API))]
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
#[cfg_attr(Py_3_10, allow(deprecated))]
fn ascii() {
Python::with_gil(|py| {
Expand Down Expand Up @@ -202,7 +205,7 @@ fn ascii() {
}

#[test]
#[cfg(not(Py_LIMITED_API))]
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
#[cfg_attr(Py_3_10, allow(deprecated))]
fn ucs4() {
Python::with_gil(|py| {
Expand Down
20 changes: 10 additions & 10 deletions src/types/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ impl PyString {
///
/// By using this API, you accept responsibility for testing that PyStringData behaves as
/// expected on the targets where you plan to distribute your software.
#[cfg(not(any(Py_LIMITED_API, GraalPy)))]
#[cfg(not(any(Py_LIMITED_API, GraalPy, PyPy)))]
pub unsafe fn data(&self) -> PyResult<PyStringData<'_>> {
self.as_borrowed().data()
}
Expand Down Expand Up @@ -313,7 +313,7 @@ pub trait PyStringMethods<'py>: crate::sealed::Sealed {
///
/// By using this API, you accept responsibility for testing that PyStringData behaves as
/// expected on the targets where you plan to distribute your software.
#[cfg(not(any(Py_LIMITED_API, GraalPy)))]
#[cfg(not(any(Py_LIMITED_API, GraalPy, PyPy)))]
unsafe fn data(&self) -> PyResult<PyStringData<'_>>;
}

Expand All @@ -339,7 +339,7 @@ impl<'py> PyStringMethods<'py> for Bound<'py, PyString> {
}
}

#[cfg(not(any(Py_LIMITED_API, GraalPy)))]
#[cfg(not(any(Py_LIMITED_API, GraalPy, PyPy)))]
unsafe fn data(&self) -> PyResult<PyStringData<'_>> {
self.as_borrowed().data()
}
Expand Down Expand Up @@ -402,7 +402,7 @@ impl<'a> Borrowed<'a, '_, PyString> {
Cow::Owned(String::from_utf8_lossy(bytes.as_bytes()).into_owned())
}

#[cfg(not(any(Py_LIMITED_API, GraalPy)))]
#[cfg(not(any(Py_LIMITED_API, GraalPy, PyPy)))]
unsafe fn data(self) -> PyResult<PyStringData<'a>> {
let ptr = self.as_ptr();

Expand Down Expand Up @@ -584,7 +584,7 @@ mod tests {
}

#[test]
#[cfg(not(Py_LIMITED_API))]
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
fn test_string_data_ucs1() {
Python::with_gil(|py| {
let s = PyString::new_bound(py, "hello, world");
Expand All @@ -597,7 +597,7 @@ mod tests {
}

#[test]
#[cfg(not(Py_LIMITED_API))]
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
fn test_string_data_ucs1_invalid() {
Python::with_gil(|py| {
// 0xfe is not allowed in UTF-8.
Expand Down Expand Up @@ -625,7 +625,7 @@ mod tests {
}

#[test]
#[cfg(not(Py_LIMITED_API))]
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
fn test_string_data_ucs2() {
Python::with_gil(|py| {
let s = py.eval_bound("'foo\\ud800'", None, None).unwrap();
Expand All @@ -641,7 +641,7 @@ mod tests {
}

#[test]
#[cfg(all(not(Py_LIMITED_API), target_endian = "little"))]
#[cfg(all(not(any(Py_LIMITED_API, PyPy)), target_endian = "little"))]
fn test_string_data_ucs2_invalid() {
Python::with_gil(|py| {
// U+FF22 (valid) & U+d800 (never valid)
Expand Down Expand Up @@ -669,7 +669,7 @@ mod tests {
}

#[test]
#[cfg(not(Py_LIMITED_API))]
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
fn test_string_data_ucs4() {
Python::with_gil(|py| {
let s = "哈哈🐈";
Expand All @@ -682,7 +682,7 @@ mod tests {
}

#[test]
#[cfg(all(not(Py_LIMITED_API), target_endian = "little"))]
#[cfg(all(not(any(Py_LIMITED_API, PyPy)), target_endian = "little"))]
fn test_string_data_ucs4_invalid() {
Python::with_gil(|py| {
// U+20000 (valid) & U+d800 (never valid)
Expand Down
Loading