|
| 1 | +#[cfg(not(Py_LIMITED_API))] |
| 2 | +use crate::exceptions::PyNotImplementedError; |
1 | 3 | use crate::ffi_ptr_ext::FfiPtrExt; |
2 | 4 | use crate::instance::Borrowed; |
3 | 5 | use crate::py_result_ext::PyResultExt; |
@@ -108,8 +110,19 @@ impl<'py> Iterator for Bound<'py, PyIterator> { |
108 | 110 |
|
109 | 111 | #[cfg(not(Py_LIMITED_API))] |
110 | 112 | fn size_hint(&self) -> (usize, Option<usize>) { |
| 113 | + // SAFETY: `self` is a valid iterator object |
111 | 114 | let hint = unsafe { ffi::PyObject_LengthHint(self.as_ptr(), 0) }; |
112 | | - (hint.max(0) as usize, None) |
| 115 | + if hint < 0 { |
| 116 | + 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 | + } |
| 122 | + (0, None) |
| 123 | + } else { |
| 124 | + (hint as usize, None) |
| 125 | + } |
113 | 126 | } |
114 | 127 | } |
115 | 128 |
|
@@ -144,7 +157,7 @@ mod tests { |
144 | 157 | #[cfg(all(not(PyPy), Py_3_10))] |
145 | 158 | use crate::types::PyNone; |
146 | 159 | use crate::types::{PyAnyMethods, PyDict, PyList, PyListMethods}; |
147 | | - use crate::{IntoPyObject, PyTypeInfo, Python}; |
| 160 | + use crate::{IntoPyObject, PyErr, PyTypeInfo, Python}; |
148 | 161 |
|
149 | 162 | #[test] |
150 | 163 | fn vec_iter() { |
@@ -392,6 +405,35 @@ def fibonacci(target): |
392 | 405 | }); |
393 | 406 | } |
394 | 407 |
|
| 408 | + #[test] |
| 409 | + #[cfg(all(feature = "macros", not(Py_LIMITED_API)))] |
| 410 | + fn length_hint_not_implemented() { |
| 411 | + #[crate::pyfunction(crate = "crate")] |
| 412 | + fn test_size_hint(obj: &crate::Bound<'_, crate::PyAny>) { |
| 413 | + let iter = obj.cast::<PyIterator>().unwrap(); |
| 414 | + assert_eq!((0, None), iter.size_hint()); |
| 415 | + assert!(PyErr::take(obj.py()).is_none()); |
| 416 | + } |
| 417 | + |
| 418 | + Python::attach(|py| { |
| 419 | + let test_size_hint = crate::wrap_pyfunction!(test_size_hint, py).unwrap(); |
| 420 | + crate::py_run!( |
| 421 | + py, |
| 422 | + test_size_hint, |
| 423 | + r#" |
| 424 | + class MyIter: |
| 425 | + def __next__(self): |
| 426 | + raise StopIteration |
| 427 | +
|
| 428 | + def __length_hint__(self): |
| 429 | + raise NotImplementedError |
| 430 | +
|
| 431 | + test_size_hint(MyIter()) |
| 432 | + "# |
| 433 | + ); |
| 434 | + }); |
| 435 | + } |
| 436 | + |
395 | 437 | #[test] |
396 | 438 | fn test_type_object() { |
397 | 439 | Python::attach(|py| { |
|
0 commit comments