Skip to content
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/5121.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `PyBytes_AS_STRING`
2 changes: 2 additions & 0 deletions newsfragments/5121.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Enable vectorcall methods on GraalPy
Call Py_Is function on GraalPy
3 changes: 1 addition & 2 deletions pyo3-ffi/src/abstract_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ pub unsafe fn PyObject_DelAttr(o: *mut PyObject, attr_name: *mut PyObject) -> c_
extern "C" {
#[cfg(all(
not(PyPy),
not(GraalPy),
any(Py_3_10, all(not(Py_LIMITED_API), Py_3_9)) // Added to python in 3.9 but to limited API in 3.10
))]
#[cfg_attr(PyPy, link_name = "PyPyObject_CallNoArgs")]
Expand Down Expand Up @@ -94,7 +93,7 @@ extern "C" {
kwnames: *mut PyObject,
) -> *mut PyObject;

#[cfg(any(Py_3_12, all(Py_3_9, not(any(Py_LIMITED_API, PyPy, GraalPy)))))]
#[cfg(any(Py_3_12, all(Py_3_9, not(any(Py_LIMITED_API, PyPy)))))]
pub fn PyObject_VectorcallMethod(
name: *mut PyObject,
args: *const *mut PyObject,
Expand Down
3 changes: 1 addition & 2 deletions pyo3-ffi/src/compat/py_3_9.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
compat_function!(
originally_defined_for(all(
not(PyPy),
not(GraalPy),
any(Py_3_10, all(not(Py_LIMITED_API), Py_3_9)) // Added to python in 3.9 but to limited API in 3.10
));

Expand All @@ -12,7 +11,7 @@ compat_function!(
);

compat_function!(
originally_defined_for(all(Py_3_9, not(any(Py_LIMITED_API, PyPy, GraalPy))));
originally_defined_for(all(Py_3_9, not(any(Py_LIMITED_API, PyPy))));

#[inline]
pub unsafe fn PyObject_CallMethodNoArgs(obj: *mut crate::PyObject, name: *mut crate::PyObject) -> *mut crate::PyObject {
Expand Down
22 changes: 11 additions & 11 deletions pyo3-ffi/src/cpython/abstract_.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::{PyObject, Py_ssize_t};
#[cfg(any(all(Py_3_8, not(any(PyPy, GraalPy))), not(Py_3_11)))]
#[cfg(any(all(Py_3_8, not(PyPy)), not(Py_3_11)))]
use std::os::raw::c_char;
use std::os::raw::c_int;

#[cfg(not(Py_3_11))]
use crate::Py_buffer;

#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))]
#[cfg(all(Py_3_8, not(PyPy)))]
use crate::{
vectorcallfunc, PyCallable_Check, PyThreadState, PyThreadState_GET, PyTuple_Check,
PyType_HasFeature, Py_TPFLAGS_HAVE_VECTORCALL,
Expand All @@ -23,15 +23,15 @@ extern "C" {
const _PY_FASTCALL_SMALL_STACK: size_t = 5;

extern "C" {
#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))]
#[cfg(all(Py_3_8, not(PyPy)))]
pub fn _Py_CheckFunctionResult(
tstate: *mut PyThreadState,
callable: *mut PyObject,
result: *mut PyObject,
where_: *const c_char,
) -> *mut PyObject;

#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))]
#[cfg(all(Py_3_8, not(PyPy)))]
pub fn _PyObject_MakeTpCall(
tstate: *mut PyThreadState,
callable: *mut PyObject,
Expand All @@ -52,7 +52,7 @@ pub unsafe fn PyVectorcall_NARGS(n: size_t) -> Py_ssize_t {
n.try_into().expect("cannot fail due to mask")
}

#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))]
#[cfg(all(Py_3_8, not(PyPy)))]
#[inline(always)]
pub unsafe fn PyVectorcall_Function(callable: *mut PyObject) -> Option<vectorcallfunc> {
assert!(!callable.is_null());
Expand All @@ -67,7 +67,7 @@ pub unsafe fn PyVectorcall_Function(callable: *mut PyObject) -> Option<vectorcal
*ptr
}

#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))]
#[cfg(all(Py_3_8, not(PyPy)))]
#[inline(always)]
pub unsafe fn _PyObject_VectorcallTstate(
tstate: *mut PyThreadState,
Expand Down Expand Up @@ -148,7 +148,7 @@ pub unsafe fn _PyObject_FastCall(
_PyObject_FastCallTstate(PyThreadState_GET(), func, args, nargs)
}

#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))]
#[cfg(all(Py_3_8, not(PyPy)))]
#[inline(always)]
pub unsafe fn _PyObject_CallNoArg(func: *mut PyObject) -> *mut PyObject {
_PyObject_VectorcallTstate(
Expand All @@ -166,7 +166,7 @@ extern "C" {
pub fn _PyObject_CallNoArg(func: *mut PyObject) -> *mut PyObject;
}

#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))]
#[cfg(all(Py_3_8, not(PyPy)))]
#[inline(always)]
pub unsafe fn PyObject_CallOneArg(func: *mut PyObject, arg: *mut PyObject) -> *mut PyObject {
assert!(!arg.is_null());
Expand All @@ -177,7 +177,7 @@ pub unsafe fn PyObject_CallOneArg(func: *mut PyObject, arg: *mut PyObject) -> *m
_PyObject_VectorcallTstate(tstate, func, args, nargsf, std::ptr::null_mut())
}

#[cfg(all(Py_3_9, not(any(PyPy, GraalPy))))]
#[cfg(all(Py_3_9, not(PyPy)))]
#[inline(always)]
pub unsafe fn PyObject_CallMethodNoArgs(
self_: *mut PyObject,
Expand All @@ -191,7 +191,7 @@ pub unsafe fn PyObject_CallMethodNoArgs(
)
}

#[cfg(all(Py_3_9, not(any(PyPy, GraalPy))))]
#[cfg(all(Py_3_9, not(PyPy)))]
#[inline(always)]
pub unsafe fn PyObject_CallMethodOneArg(
self_: *mut PyObject,
Expand Down Expand Up @@ -219,7 +219,7 @@ extern "C" {
pub fn PyObject_LengthHint(o: *mut PyObject, arg1: Py_ssize_t) -> Py_ssize_t;

#[cfg(not(Py_3_11))] // moved to src/buffer.rs from 3.11
#[cfg(all(Py_3_9, not(any(PyPy, GraalPy))))]
#[cfg(all(Py_3_9, not(PyPy)))]
pub fn PyObject_CheckBuffer(obj: *mut PyObject) -> c_int;
}

Expand Down
11 changes: 10 additions & 1 deletion pyo3-ffi/src/cpython/bytesobject.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::object::*;
use crate::Py_ssize_t;
#[cfg(not(any(PyPy, GraalPy, Py_LIMITED_API)))]
#[cfg(not(Py_LIMITED_API))]
use std::os::raw::c_char;
use std::os::raw::c_int;

Expand All @@ -23,3 +23,12 @@ extern "C" {
#[cfg_attr(PyPy, link_name = "_PyPyBytes_Resize")]
pub fn _PyBytes_Resize(bytes: *mut *mut PyObject, newsize: Py_ssize_t) -> c_int;
}

#[cfg(not(Py_LIMITED_API))]
#[inline]
pub unsafe fn PyBytes_AS_STRING(op: *mut PyObject) -> *const c_char {
#[cfg(not(any(PyPy, GraalPy)))]
return &(*op.cast::<PyBytesObject>()).ob_sval as *const c_char;
#[cfg(any(PyPy, GraalPy))]
return crate::PyBytes_AsString(op);
}
4 changes: 2 additions & 2 deletions pyo3-ffi/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,13 @@ pub struct PyVarObject {
// skipped private _PyVarObject_CAST

#[inline]
#[cfg(not(all(PyPy, Py_3_10)))]
#[cfg(not(any(GraalPy, all(PyPy, Py_3_10))))]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub unsafe fn Py_Is(x: *mut PyObject, y: *mut PyObject) -> c_int {
(x == y).into()
}

#[cfg(all(PyPy, Py_3_10))]
#[cfg(any(GraalPy, all(PyPy, Py_3_10)))]
#[cfg_attr(docsrs, doc(cfg(all())))]
extern "C" {
#[cfg_attr(PyPy, link_name = "PyPy_Is")]
Expand Down
15 changes: 15 additions & 0 deletions src/types/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,4 +365,19 @@ mod tests {
assert_eq!(*b, py_string);
})
}

#[test]
#[cfg(not(Py_LIMITED_API))]
fn test_as_string() {
Python::with_gil(|py| {
let b = b"hello, world".as_slice();
let py_bytes = PyBytes::new(py, b);
unsafe {
assert_eq!(
ffi::PyBytes_AsString(py_bytes.as_ptr()) as *const std::os::raw::c_char,
ffi::PyBytes_AS_STRING(py_bytes.as_ptr()) as *const std::os::raw::c_char
);
}
})
}
}
Loading