Skip to content

Commit 9fe9a61

Browse files
Test PyNotImplemented return value as well
1 parent 4129275 commit 9fe9a61

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/types/iterator.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#[cfg(not(Py_LIMITED_API))]
2-
use crate::exceptions::PyNotImplementedError;
31
use crate::ffi_ptr_ext::FfiPtrExt;
42
use crate::instance::Borrowed;
53
use crate::py_result_ext::PyResultExt;
@@ -114,11 +112,7 @@ impl<'py> Iterator for Bound<'py, PyIterator> {
114112
let hint = unsafe { ffi::PyObject_LengthHint(self.as_ptr(), 0) };
115113
if hint < 0 {
116114
let py = self.py();
117-
let err = PyErr::fetch(py);
118-
if !err.is_instance_of::<PyNotImplementedError>(py) {
119-
// Write unraisable error only if it's not NotImplementedError
120-
err.write_unraisable(py, Some(self));
121-
}
115+
PyErr::fetch(py).write_unraisable(py, Some(self));
122116
(0, None)
123117
} else {
124118
(hint as usize, None)
@@ -407,7 +401,7 @@ def fibonacci(target):
407401

408402
#[test]
409403
#[cfg(all(feature = "macros", not(Py_LIMITED_API)))]
410-
fn length_hint_not_implemented() {
404+
fn length_hint_error() {
411405
#[crate::pyfunction(crate = "crate")]
412406
fn test_size_hint(obj: &crate::Bound<'_, crate::PyAny>) {
413407
let iter = obj.cast::<PyIterator>().unwrap();
@@ -421,14 +415,22 @@ def fibonacci(target):
421415
py,
422416
test_size_hint,
423417
r#"
424-
class MyIter:
418+
class NoHintIter:
419+
def __next__(self):
420+
raise StopIteration
421+
422+
def __length_hint__(self):
423+
return NotImplemented
424+
425+
class ErrorHintIter:
425426
def __next__(self):
426427
raise StopIteration
427428
428429
def __length_hint__(self):
429-
raise NotImplementedError
430+
raise ValueError("bad hint impl")
430431
431-
test_size_hint(MyIter())
432+
test_size_hint(NoHintIter())
433+
test_size_hint(ErrorHintIter())
432434
"#
433435
);
434436
});

0 commit comments

Comments
 (0)