@@ -174,7 +174,7 @@ mod decl {
174
174
#[ derive( Debug , PyPayload ) ]
175
175
struct PyItertoolsCount {
176
176
cur : PyRwLock < PyObjectRef > ,
177
- step : PyIntRef ,
177
+ step : Option < PyIntRef > ,
178
178
}
179
179
180
180
#[ derive( FromArgs ) ]
@@ -195,7 +195,13 @@ mod decl {
195
195
vm : & VirtualMachine ,
196
196
) -> PyResult {
197
197
let start: PyObjectRef = start. into_option ( ) . unwrap_or_else ( || vm. new_pyobj ( 0 ) ) ;
198
- let step: PyIntRef = step. into_option ( ) . unwrap_or_else ( || vm. new_pyref ( 1 ) ) ;
198
+ let step = match step. into_option ( ) {
199
+ Some ( int) => {
200
+ let val: isize = int. try_to_primitive ( vm) ?;
201
+ Some ( vm. new_pyref ( val. to_usize ( ) . unwrap_or ( 0 ) ) )
202
+ }
203
+ None => None ,
204
+ } ;
199
205
if !PyNumber :: check ( & start, vm) {
200
206
return Err ( vm. new_value_error ( "a number is require" . to_owned ( ) ) ) ;
201
207
}
@@ -220,19 +226,24 @@ mod decl {
220
226
}
221
227
222
228
#[ pymethod( magic) ]
223
- fn repr ( & self ) -> PyResult < String > {
224
- let cur = self . cur . read ( ) ;
225
-
229
+ fn repr ( & self , vm : & VirtualMachine ) -> PyResult < String > {
230
+ let mut cur = format ! ( "{}" , self . cur. read( ) . clone( ) . repr( vm) ?) ;
231
+ let step = self . step . clone ( ) ;
232
+ if let Some ( ref step) = step {
233
+ cur. push_str ( ", " ) ;
234
+ cur. push_str ( & step. to_string ( ) ) ;
235
+ }
226
236
Ok ( format ! ( "count({})" , cur) )
227
237
}
228
238
}
229
239
impl IterNextIterable for PyItertoolsCount { }
230
240
impl IterNext for PyItertoolsCount {
231
241
fn next ( zelf : & Py < Self > , vm : & VirtualMachine ) -> PyResult < PyIterReturn > {
232
242
let mut cur = zelf. cur . write ( ) ;
233
- let step = zelf. step . clone ( ) ;
234
243
let result = cur. clone ( ) ;
235
- * cur = vm. _iadd ( & * cur, step. as_object ( ) ) ?;
244
+ if let Some ( step) = & zelf. step {
245
+ * cur = vm. _iadd ( & * cur, step. as_object ( ) ) ?;
246
+ }
236
247
Ok ( PyIterReturn :: Return ( result. to_pyobject ( vm) ) )
237
248
}
238
249
}
0 commit comments