1- #[ cfg( not( Py_LIMITED_API ) ) ]
2- use crate :: exceptions:: PyNotImplementedError ;
31use crate :: ffi_ptr_ext:: FfiPtrExt ;
42use crate :: instance:: Borrowed ;
53use 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 )
@@ -157,7 +151,9 @@ mod tests {
157151 #[ cfg( all( not( PyPy ) , Py_3_10 ) ) ]
158152 use crate :: types:: PyNone ;
159153 use crate :: types:: { PyAnyMethods , PyDict , PyList , PyListMethods } ;
160- use crate :: { IntoPyObject , PyErr , PyTypeInfo , Python } ;
154+ #[ cfg( all( feature = "macros" , not( Py_LIMITED_API ) ) ) ]
155+ use crate :: PyErr ;
156+ use crate :: { IntoPyObject , PyTypeInfo , Python } ;
161157
162158 #[ test]
163159 fn vec_iter ( ) {
@@ -407,7 +403,7 @@ def fibonacci(target):
407403
408404 #[ test]
409405 #[ cfg( all( feature = "macros" , not( Py_LIMITED_API ) ) ) ]
410- fn length_hint_not_implemented ( ) {
406+ fn length_hint_error ( ) {
411407 #[ crate :: pyfunction( crate = "crate" ) ]
412408 fn test_size_hint ( obj : & crate :: Bound < ' _ , crate :: PyAny > ) {
413409 let iter = obj. cast :: < PyIterator > ( ) . unwrap ( ) ;
@@ -421,14 +417,22 @@ def fibonacci(target):
421417 py,
422418 test_size_hint,
423419 r#"
424- class MyIter:
420+ class NoHintIter:
421+ def __next__(self):
422+ raise StopIteration
423+
424+ def __length_hint__(self):
425+ return NotImplemented
426+
427+ class ErrorHintIter:
425428 def __next__(self):
426429 raise StopIteration
427430
428431 def __length_hint__(self):
429- raise NotImplementedError
432+ raise ValueError("bad hint impl")
430433
431- test_size_hint(MyIter())
434+ test_size_hint(NoHintIter())
435+ test_size_hint(ErrorHintIter())
432436 "#
433437 ) ;
434438 } ) ;
0 commit comments