@@ -238,10 +238,10 @@ mod decl {
238238
239239 #[ derive( FromArgs ) ]
240240 struct CountNewArgs {
241- #[ pyarg( positional , optional) ]
241+ #[ pyarg( any , optional) ]
242242 start : OptionalArg < PyObjectRef > ,
243243
244- #[ pyarg( positional , optional) ]
244+ #[ pyarg( any , optional) ]
245245 step : OptionalArg < PyObjectRef > ,
246246 }
247247
@@ -1945,6 +1945,7 @@ mod decl {
19451945 exhausted : AtomicCell < bool > ,
19461946 iterable : PyIter ,
19471947 n : AtomicCell < usize > ,
1948+ strict : AtomicCell < bool > ,
19481949 }
19491950
19501951 #[ derive( FromArgs ) ]
@@ -1953,14 +1954,20 @@ mod decl {
19531954 iterable_ref : PyObjectRef ,
19541955 #[ pyarg( positional) ]
19551956 n : PyIntRef ,
1957+ #[ pyarg( named, default = false ) ]
1958+ strict : bool ,
19561959 }
19571960
19581961 impl Constructor for PyItertoolsBatched {
19591962 type Args = BatchedNewArgs ;
19601963
19611964 fn py_new (
19621965 cls : PyTypeRef ,
1963- Self :: Args { iterable_ref, n } : Self :: Args ,
1966+ Self :: Args {
1967+ iterable_ref,
1968+ n,
1969+ strict,
1970+ } : Self :: Args ,
19641971 vm : & VirtualMachine ,
19651972 ) -> PyResult {
19661973 let n = n. as_bigint ( ) ;
@@ -1976,6 +1983,7 @@ mod decl {
19761983 iterable,
19771984 n : AtomicCell :: new ( n) ,
19781985 exhausted : AtomicCell :: new ( false ) ,
1986+ strict : AtomicCell :: new ( strict) ,
19791987 }
19801988 . into_ref_with_type ( vm, cls)
19811989 . map ( Into :: into)
@@ -2005,9 +2013,16 @@ mod decl {
20052013 }
20062014 }
20072015 }
2008- match result. len ( ) {
2016+ let res_len = result. len ( ) ;
2017+ match res_len {
20092018 0 => Ok ( PyIterReturn :: StopIteration ( None ) ) ,
2010- _ => Ok ( PyIterReturn :: Return ( vm. ctx . new_tuple ( result) . into ( ) ) ) ,
2019+ _ => {
2020+ if zelf. strict . load ( ) && res_len != n {
2021+ Err ( vm. new_value_error ( "batched(): incomplete batch" ) )
2022+ } else {
2023+ Ok ( PyIterReturn :: Return ( vm. ctx . new_tuple ( result) . into ( ) ) )
2024+ }
2025+ }
20112026 }
20122027 }
20132028 }
0 commit comments