Skip to content

Commit 5f7d8a7

Browse files
committed
Use CPython stable APIs for implementing tuples.
Refs #1125 Reduces the number of compilation failures with `--features abi3` from 56 to 53.
1 parent 3151c48 commit 5f7d8a7

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/types/tuple.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl PyTuple {
4242
pub fn len(&self) -> usize {
4343
unsafe {
4444
// non-negative Py_ssize_t should always fit into Rust uint
45-
ffi::PyTuple_GET_SIZE(self.as_ptr()) as usize
45+
ffi::PyTuple_Size(self.as_ptr()) as usize
4646
}
4747
}
4848

@@ -62,8 +62,7 @@ impl PyTuple {
6262
/// Takes a slice of the tuple from `low` to the end and returns it as a new tuple.
6363
pub fn split_from(&self, low: isize) -> &PyTuple {
6464
unsafe {
65-
let ptr =
66-
ffi::PyTuple_GetSlice(self.as_ptr(), low, ffi::PyTuple_GET_SIZE(self.as_ptr()));
65+
let ptr = ffi::PyTuple_GetSlice(self.as_ptr(), low, self.len() as Py_ssize_t);
6766
self.py().from_owned_ptr(ptr)
6867
}
6968
}
@@ -75,11 +74,14 @@ impl PyTuple {
7574
assert!(index < self.len());
7675
unsafe {
7776
self.py()
78-
.from_borrowed_ptr(ffi::PyTuple_GET_ITEM(self.as_ptr(), index as Py_ssize_t))
77+
.from_borrowed_ptr(ffi::PyTuple_GetItem(self.as_ptr(), index as Py_ssize_t))
7978
}
8079
}
8180

8281
/// Returns `self` as a slice of objects.
82+
///
83+
/// Not available when compiled with Py_LIMITED_API.
84+
#[cfg(not(Py_LIMITED_API))]
8385
pub fn as_slice(&self) -> &[&PyAny] {
8486
// This is safe because &PyAny has the same memory layout as *mut ffi::PyObject,
8587
// and because tuples are immutable.
@@ -93,15 +95,15 @@ impl PyTuple {
9395
/// Returns an iterator over the tuple items.
9496
pub fn iter(&self) -> PyTupleIterator {
9597
PyTupleIterator {
96-
slice: self.as_slice(),
98+
tuple: self,
9799
index: 0,
98100
}
99101
}
100102
}
101103

102104
/// Used by `PyTuple::iter()`.
103105
pub struct PyTupleIterator<'a> {
104-
slice: &'a [&'a PyAny],
106+
tuple: &'a PyTuple,
105107
index: usize,
106108
}
107109

@@ -110,8 +112,8 @@ impl<'a> Iterator for PyTupleIterator<'a> {
110112

111113
#[inline]
112114
fn next(&mut self) -> Option<&'a PyAny> {
113-
if self.index < self.slice.len() {
114-
let item = self.slice[self.index];
115+
if self.index < self.tuple.len() {
116+
let item = self.tuple.get_item(self.index);
115117
self.index += 1;
116118
Some(item)
117119
} else {
@@ -172,10 +174,9 @@ macro_rules! tuple_conversion ({$length:expr,$(($refN:ident, $n:tt, $T:ident)),+
172174
fn extract(obj: &'s PyAny) -> PyResult<Self>
173175
{
174176
let t = <PyTuple as PyTryFrom>::try_from(obj)?;
175-
let slice = t.as_slice();
176177
if t.len() == $length {
177178
Ok((
178-
$(slice[$n].extract::<$T>()?,)+
179+
$(t.get_item($n).extract::<$T>()?,)+
179180
))
180181
} else {
181182
Err(wrong_tuple_length(t, $length))

0 commit comments

Comments
 (0)