Skip to content

Commit f881298

Browse files
authored
Updates to better support ormsgpack on GraalPy. (#5121)
* Expose the vectorcall functions also on GraalPy * Add PyBytes_AS_STRING, seen in ormsgpack * Call Py_Is function on GraalPy
1 parent 2c2f39f commit f881298

File tree

8 files changed

+43
-18
lines changed

8 files changed

+43
-18
lines changed

newsfragments/5121.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add `PyBytes_AS_STRING`

newsfragments/5121.changed.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Enable vectorcall methods on GraalPy
2+
Call Py_Is function on GraalPy

pyo3-ffi/src/abstract_.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ pub unsafe fn PyObject_DelAttr(o: *mut PyObject, attr_name: *mut PyObject) -> c_
2525
extern "C" {
2626
#[cfg(all(
2727
not(PyPy),
28-
not(GraalPy),
2928
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
3029
))]
3130
#[cfg_attr(PyPy, link_name = "PyPyObject_CallNoArgs")]
@@ -94,7 +93,7 @@ extern "C" {
9493
kwnames: *mut PyObject,
9594
) -> *mut PyObject;
9695

97-
#[cfg(any(Py_3_12, all(Py_3_9, not(any(Py_LIMITED_API, PyPy, GraalPy)))))]
96+
#[cfg(any(Py_3_12, all(Py_3_9, not(any(Py_LIMITED_API, PyPy)))))]
9897
pub fn PyObject_VectorcallMethod(
9998
name: *mut PyObject,
10099
args: *const *mut PyObject,

pyo3-ffi/src/compat/py_3_9.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
compat_function!(
22
originally_defined_for(all(
33
not(PyPy),
4-
not(GraalPy),
54
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
65
));
76

@@ -12,7 +11,7 @@ compat_function!(
1211
);
1312

1413
compat_function!(
15-
originally_defined_for(all(Py_3_9, not(any(Py_LIMITED_API, PyPy, GraalPy))));
14+
originally_defined_for(all(Py_3_9, not(any(Py_LIMITED_API, PyPy))));
1615

1716
#[inline]
1817
pub unsafe fn PyObject_CallMethodNoArgs(obj: *mut crate::PyObject, name: *mut crate::PyObject) -> *mut crate::PyObject {

pyo3-ffi/src/cpython/abstract_.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use crate::{PyObject, Py_ssize_t};
2-
#[cfg(any(all(Py_3_8, not(any(PyPy, GraalPy))), not(Py_3_11)))]
2+
#[cfg(any(all(Py_3_8, not(PyPy)), not(Py_3_11)))]
33
use std::os::raw::c_char;
44
use std::os::raw::c_int;
55

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

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

2525
extern "C" {
26-
#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))]
26+
#[cfg(all(Py_3_8, not(PyPy)))]
2727
pub fn _Py_CheckFunctionResult(
2828
tstate: *mut PyThreadState,
2929
callable: *mut PyObject,
3030
result: *mut PyObject,
3131
where_: *const c_char,
3232
) -> *mut PyObject;
3333

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

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

70-
#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))]
70+
#[cfg(all(Py_3_8, not(PyPy)))]
7171
#[inline(always)]
7272
pub unsafe fn _PyObject_VectorcallTstate(
7373
tstate: *mut PyThreadState,
@@ -148,7 +148,7 @@ pub unsafe fn _PyObject_FastCall(
148148
_PyObject_FastCallTstate(PyThreadState_GET(), func, args, nargs)
149149
}
150150

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

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

180-
#[cfg(all(Py_3_9, not(any(PyPy, GraalPy))))]
180+
#[cfg(all(Py_3_9, not(PyPy)))]
181181
#[inline(always)]
182182
pub unsafe fn PyObject_CallMethodNoArgs(
183183
self_: *mut PyObject,
@@ -191,7 +191,7 @@ pub unsafe fn PyObject_CallMethodNoArgs(
191191
)
192192
}
193193

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

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

pyo3-ffi/src/cpython/bytesobject.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::object::*;
22
use crate::Py_ssize_t;
3-
#[cfg(not(any(PyPy, GraalPy, Py_LIMITED_API)))]
3+
#[cfg(not(Py_LIMITED_API))]
44
use std::os::raw::c_char;
55
use std::os::raw::c_int;
66

@@ -23,3 +23,12 @@ extern "C" {
2323
#[cfg_attr(PyPy, link_name = "_PyPyBytes_Resize")]
2424
pub fn _PyBytes_Resize(bytes: *mut *mut PyObject, newsize: Py_ssize_t) -> c_int;
2525
}
26+
27+
#[cfg(not(Py_LIMITED_API))]
28+
#[inline]
29+
pub unsafe fn PyBytes_AS_STRING(op: *mut PyObject) -> *const c_char {
30+
#[cfg(not(any(PyPy, GraalPy)))]
31+
return &(*op.cast::<PyBytesObject>()).ob_sval as *const c_char;
32+
#[cfg(any(PyPy, GraalPy))]
33+
return crate::PyBytes_AsString(op);
34+
}

pyo3-ffi/src/object.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,13 @@ pub struct PyVarObject {
147147
// skipped private _PyVarObject_CAST
148148

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

156-
#[cfg(all(PyPy, Py_3_10))]
156+
#[cfg(any(GraalPy, all(PyPy, Py_3_10)))]
157157
#[cfg_attr(docsrs, doc(cfg(all())))]
158158
extern "C" {
159159
#[cfg_attr(PyPy, link_name = "PyPy_Is")]

src/types/bytes.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,4 +365,19 @@ mod tests {
365365
assert_eq!(*b, py_string);
366366
})
367367
}
368+
369+
#[test]
370+
#[cfg(not(Py_LIMITED_API))]
371+
fn test_as_string() {
372+
Python::with_gil(|py| {
373+
let b = b"hello, world".as_slice();
374+
let py_bytes = PyBytes::new(py, b);
375+
unsafe {
376+
assert_eq!(
377+
ffi::PyBytes_AsString(py_bytes.as_ptr()) as *const std::os::raw::c_char,
378+
ffi::PyBytes_AS_STRING(py_bytes.as_ptr()) as *const std::os::raw::c_char
379+
);
380+
}
381+
})
382+
}
368383
}

0 commit comments

Comments
 (0)