Skip to content

Commit c07d865

Browse files
committed
Fix itertools count repr
1 parent fd3bcae commit c07d865

File tree

1 file changed

+6
-13
lines changed

1 file changed

+6
-13
lines changed

vm/src/stdlib/itertools.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ mod decl {
174174
#[derive(Debug, PyPayload)]
175175
struct PyItertoolsCount {
176176
cur: PyRwLock<PyObjectRef>,
177-
step: Option<PyIntRef>,
177+
step: PyIntRef,
178178
}
179179

180180
#[derive(FromArgs)]
@@ -195,13 +195,7 @@ mod decl {
195195
vm: &VirtualMachine,
196196
) -> PyResult {
197197
let start: PyObjectRef = start.into_option().unwrap_or_else(|| vm.new_pyobj(0));
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-
};
198+
let step: PyIntRef = step.into_option().unwrap_or_else(|| vm.new_pyref(1));
205199
if !PyNumber::check(&start, vm) {
206200
return Err(vm.new_value_error("a number is require".to_owned()));
207201
}
@@ -228,8 +222,8 @@ mod decl {
228222
#[pymethod(magic)]
229223
fn repr(&self, vm: &VirtualMachine) -> PyResult<String> {
230224
let mut cur = format!("{}", self.cur.read().clone().repr(vm)?);
231-
let step = self.step.clone();
232-
if let Some(ref step) = step {
225+
let step = format!("{}", self.step.clone());
226+
if step != "1" {
233227
cur.push_str(", ");
234228
cur.push_str(&step.to_string());
235229
}
@@ -240,10 +234,9 @@ mod decl {
240234
impl IterNext for PyItertoolsCount {
241235
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
242236
let mut cur = zelf.cur.write();
237+
let step = zelf.step.clone();
243238
let result = cur.clone();
244-
if let Some(step) = &zelf.step {
245-
*cur = vm._iadd(&*cur, step.as_object())?;
246-
}
239+
*cur = vm._iadd(&*cur, step.as_object())?;
247240
Ok(PyIterReturn::Return(result.to_pyobject(vm)))
248241
}
249242
}

0 commit comments

Comments
 (0)