Skip to content

Commit 55a5689

Browse files
committed
Make it transparent for external projects using PyTuple macros to build on GraalPy/PyPy
1 parent af76988 commit 55a5689

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

newsfragments/1.changed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
Enable vectorcall methods on GraalPy
2+
Provide compatibility shims in PyTuple macros for PyPy and GraalPy

pyo3-ffi/src/cpython/tupleobject.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::object::*;
22
#[cfg(not(PyPy))]
33
use crate::pyport::Py_ssize_t;
4+
#[cfg(any(GraalPy, PyPy))]
5+
use crate::{PyTuple_GetItem, PyTuple_SetItem};
46

57
#[repr(C)]
68
pub struct PyTupleObject {
@@ -21,16 +23,26 @@ pub unsafe fn PyTuple_GET_SIZE(op: *mut PyObject) -> Py_ssize_t {
2123
}
2224

2325
#[inline]
24-
#[cfg(not(any(PyPy, GraalPy)))]
2526
pub unsafe fn PyTuple_GET_ITEM(op: *mut PyObject, i: Py_ssize_t) -> *mut PyObject {
26-
*(*(op as *mut PyTupleObject)).ob_item.as_ptr().offset(i)
27+
#[cfg(not(any(GraalPy, PyPy)))]
28+
return *(*(op as *mut PyTupleObject)).ob_item.as_ptr().offset(i);
29+
#[cfg(any(GraalPy, PyPy))]
30+
return PyTuple_GetItem(op, i);
2731
}
2832

2933
/// Macro, *only* to be used to fill in brand new tuples
3034
#[inline]
31-
#[cfg(not(any(PyPy, GraalPy)))]
3235
pub unsafe fn PyTuple_SET_ITEM(op: *mut PyObject, i: Py_ssize_t, v: *mut PyObject) {
33-
*(*(op as *mut PyTupleObject)).ob_item.as_mut_ptr().offset(i) = v;
36+
#[cfg(not(any(GraalPy, PyPy)))]
37+
{
38+
*(*(op as *mut PyTupleObject)).ob_item.as_mut_ptr().offset(i) = v;
39+
}
40+
#[cfg(any(GraalPy, PyPy))]
41+
{
42+
let previous = PyTuple_GetItem(op, i);
43+
Py_XDECREF(previous);
44+
PyTuple_SetItem(op, i, v);
45+
}
3446
}
3547

3648
// skipped _PyTuple_DebugMallocStats

0 commit comments

Comments
 (0)