list/tuple/sequence: implement Index

See #1667.
This commit is contained in:
Georg Brandl 2021-08-24 08:24:01 +02:00
parent cfd6a5bc35
commit 611126d994
4 changed files with 105 additions and 0 deletions

View file

@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Add `PyList::get_item_unchecked()` and `PyTuple::get_item_unchecked()` to get items without bounds checks. [#1733](https://github.com/PyO3/pyo3/pull/1733)
- Add `PyAny::py()` as a convenience for `PyNativeType::py()`. [#1751](https://github.com/PyO3/pyo3/pull/1751)
- Add implementation of `std::ops::Index` for `PyList`, `PyTuple` and `PySequence`. [#1825](https://github.com/PyO3/pyo3/pull/1825)
### Changed

View file

@ -8,6 +8,7 @@ use crate::internal_tricks::get_ssize_index;
use crate::{
AsPyPointer, IntoPy, IntoPyPointer, PyAny, PyObject, Python, ToBorrowedObject, ToPyObject,
};
use std::ops::Index;
/// Represents a Python `list`.
#[repr(transparent)]
@ -176,6 +177,20 @@ impl PyList {
}
}
impl Index<usize> for PyList {
type Output = PyAny;
fn index(&self, index: usize) -> &Self::Output {
self.get_item(index).unwrap_or_else(|_| {
panic!(
"index {} out of range for list of length {}",
index,
self.len()
);
})
}
}
/// Used by `PyList::iter()`.
pub struct PyListIterator<'a> {
list: &'a PyList,
@ -510,4 +525,23 @@ mod tests {
assert_eq!(obj.extract::<i32>().unwrap(), 2);
});
}
#[test]
fn test_list_index_trait() {
Python::with_gil(|py| {
let list = PyList::new(py, &[2, 3, 5]);
assert_eq!(2, list[0].extract::<i32>().unwrap());
assert_eq!(3, list[1].extract::<i32>().unwrap());
assert_eq!(5, list[2].extract::<i32>().unwrap());
});
}
#[test]
#[should_panic]
fn test_list_index_trait_panic() {
Python::with_gil(|py| {
let list = PyList::new(py, &[2, 3, 5]);
let _ = &list[7];
});
}
}

View file

@ -6,6 +6,7 @@ use crate::internal_tricks::get_ssize_index;
use crate::types::{PyAny, PyList, PyTuple};
use crate::AsPyPointer;
use crate::{FromPyObject, PyTryFrom, ToBorrowedObject};
use std::ops::Index;
/// Represents a reference to a Python object supporting the sequence protocol.
#[repr(transparent)]
@ -268,6 +269,16 @@ impl PySequence {
}
}
impl Index<usize> for PySequence {
type Output = PyAny;
fn index(&self, index: usize) -> &Self::Output {
self.get_item(index).unwrap_or_else(|_| {
panic!("index {} out of range for sequence", index);
})
}
}
impl<'a, T> FromPyObject<'a> for Vec<T>
where
T: FromPyObject<'a>,
@ -430,6 +441,29 @@ mod tests {
});
}
#[test]
fn test_seq_index_trait() {
Python::with_gil(|py| {
let v: Vec<i32> = vec![1, 1, 2];
let ob = v.to_object(py);
let seq = ob.cast_as::<PySequence>(py).unwrap();
assert_eq!(1, seq[0].extract::<i32>().unwrap());
assert_eq!(1, seq[1].extract::<i32>().unwrap());
assert_eq!(2, seq[2].extract::<i32>().unwrap());
});
}
#[test]
#[should_panic]
fn test_seq_index_trait_panic() {
Python::with_gil(|py| {
let v: Vec<i32> = vec![1, 1, 2];
let ob = v.to_object(py);
let seq = ob.cast_as::<PySequence>(py).unwrap();
let _ = &seq[7];
});
}
#[test]
fn test_seq_del_item() {
Python::with_gil(|py| {

View file

@ -6,6 +6,7 @@ use crate::{
exceptions, AsPyPointer, FromPyObject, IntoPy, IntoPyPointer, Py, PyAny, PyErr, PyObject,
PyResult, PyTryFrom, Python, ToPyObject,
};
use std::ops::Index;
/// Represents a Python `tuple` object.
///
@ -139,6 +140,20 @@ impl PyTuple {
}
}
impl Index<usize> for PyTuple {
type Output = PyAny;
fn index(&self, index: usize) -> &Self::Output {
self.get_item(index).unwrap_or_else(|_| {
panic!(
"index {} out of range for tuple of length {}",
index,
self.len()
);
})
}
}
/// Used by `PyTuple::iter()`.
pub struct PyTupleIterator<'a> {
tuple: &'a PyTuple,
@ -537,4 +552,25 @@ mod tests {
assert_eq!(obj.extract::<i32>().unwrap(), 1);
});
}
#[test]
fn test_tuple_index_trait() {
Python::with_gil(|py| {
let ob = (1, 2, 3).to_object(py);
let tuple = <PyTuple as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
assert_eq!(1, tuple[0].extract::<i32>().unwrap());
assert_eq!(2, tuple[1].extract::<i32>().unwrap());
assert_eq!(3, tuple[2].extract::<i32>().unwrap());
});
}
#[test]
#[should_panic]
fn test_tuple_index_trait_panic() {
Python::with_gil(|py| {
let ob = (1, 2, 3).to_object(py);
let tuple = <PyTuple as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
let _ = &tuple[7];
});
}
}