Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/5604.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Handle errors on `PyIterator` when calling `size_hint`
51 changes: 50 additions & 1 deletion src/types/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,15 @@ impl<'py> Iterator for Bound<'py, PyIterator> {

#[cfg(not(Py_LIMITED_API))]
fn size_hint(&self) -> (usize, Option<usize>) {
// SAFETY: `self` is a valid iterator object
let hint = unsafe { ffi::PyObject_LengthHint(self.as_ptr(), 0) };
(hint.max(0) as usize, None)
if hint < 0 {
let py = self.py();
PyErr::fetch(py).write_unraisable(py, Some(self));
(0, None)
} else {
(hint as usize, None)
}
}
}

Expand Down Expand Up @@ -144,6 +151,8 @@ mod tests {
#[cfg(all(not(PyPy), Py_3_10))]
use crate::types::PyNone;
use crate::types::{PyAnyMethods, PyDict, PyList, PyListMethods};
#[cfg(all(feature = "macros", Py_3_8, not(Py_LIMITED_API)))]
use crate::PyErr;
use crate::{IntoPyObject, PyTypeInfo, Python};

#[test]
Expand Down Expand Up @@ -392,6 +401,46 @@ def fibonacci(target):
});
}

#[test]
#[cfg(all(feature = "macros", Py_3_8, not(Py_LIMITED_API)))]
fn length_hint_error() {
#[crate::pyfunction(crate = "crate")]
fn test_size_hint(obj: &crate::Bound<'_, crate::PyAny>, should_error: bool) {
let iter = obj.cast::<PyIterator>().unwrap();
crate::test_utils::UnraisableCapture::enter(obj.py(), |capture| {
assert_eq!((0, None), iter.size_hint());
assert_eq!(should_error, capture.take_capture().is_some());
});
assert!(PyErr::take(obj.py()).is_none());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps use UnraisableCapture to demonstrate that the error was sent to sys.unraisablehook?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Did not know yet about this utility.

}

Python::attach(|py| {
let test_size_hint = crate::wrap_pyfunction!(test_size_hint, py).unwrap();
crate::py_run!(
py,
test_size_hint,
r#"
class NoHintIter:
def __next__(self):
raise StopIteration

def __length_hint__(self):
return NotImplemented

class ErrorHintIter:
def __next__(self):
raise StopIteration

def __length_hint__(self):
raise ValueError("bad hint impl")

test_size_hint(NoHintIter(), False)
test_size_hint(ErrorHintIter(), True)
"#
);
});
}

#[test]
fn test_type_object() {
Python::attach(|py| {
Expand Down
Loading