Skip to content

Commit

Permalink
Make BoundSequenceIterator iteration fallible
Browse files Browse the repository at this point in the history
  • Loading branch information
LilyFoote committed Mar 22, 2024
1 parent cc75742 commit 1e6fcf8
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/types/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,13 +490,13 @@ impl<'py> BoundSequenceIterator<'py> {
}
}

unsafe fn get_item(&self, index: usize) -> Bound<'py, PyAny> {
self.sequence.get_item(index).expect("sequence.get failed")
unsafe fn get_item(&self, index: usize) -> PyResult<Bound<'py, PyAny>> {
self.sequence.get_item(index)
}
}

impl<'py> Iterator for BoundSequenceIterator<'py> {
type Item = Bound<'py, PyAny>;
type Item = PyResult<Bound<'py, PyAny>>;

#[inline]
fn next(&mut self) -> Option<Self::Item> {
Expand Down Expand Up @@ -546,7 +546,7 @@ impl ExactSizeIterator for BoundSequenceIterator<'_> {
impl FusedIterator for BoundSequenceIterator<'_> {}

impl<'py> IntoIterator for Bound<'py, PySequence> {
type Item = Bound<'py, PyAny>;
type Item = PyResult<Bound<'py, PyAny>>;
type IntoIter = BoundSequenceIterator<'py>;

fn into_iter(self) -> Self::IntoIter {
Expand All @@ -555,7 +555,7 @@ impl<'py> IntoIterator for Bound<'py, PySequence> {
}

impl<'py> IntoIterator for &Bound<'py, PySequence> {
type Item = Bound<'py, PyAny>;
type Item = PyResult<Bound<'py, PyAny>>;
type IntoIter = BoundSequenceIterator<'py>;

fn into_iter(self) -> Self::IntoIter {
Expand Down Expand Up @@ -609,7 +609,7 @@ where

let mut v = Vec::with_capacity(seq.len().unwrap_or(0));
for item in seq.iter() {
v.push(item.extract::<T>()?);
v.push(item?.extract::<T>()?);
}
Ok(v)
}
Expand Down Expand Up @@ -1009,7 +1009,7 @@ mod tests {
let seq = ob.downcast_bound::<PySequence>(py).unwrap();
let mut idx = 0;
for el in seq {
assert_eq!(v[idx], el.extract::<i32>().unwrap());
assert_eq!(v[idx], el.unwrap().extract::<i32>().unwrap());
idx += 1;
}
assert_eq!(idx, v.len());
Expand Down

0 comments on commit 1e6fcf8

Please sign in to comment.