Skip to content

Commit fd3bcae

Browse files
committed
Fix itertools count repr
1 parent 8fa0570 commit fd3bcae

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

vm/src/stdlib/itertools.rs

Lines changed: 18 additions & 7 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: PyIntRef,
177+
step: Option<PyIntRef>,
178178
}
179179

180180
#[derive(FromArgs)]
@@ -195,7 +195,13 @@ mod decl {
195195
vm: &VirtualMachine,
196196
) -> PyResult {
197197
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+
};
199205
if !PyNumber::check(&start, vm) {
200206
return Err(vm.new_value_error("a number is require".to_owned()));
201207
}
@@ -220,19 +226,24 @@ mod decl {
220226
}
221227

222228
#[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+
}
226236
Ok(format!("count({})", cur))
227237
}
228238
}
229239
impl IterNextIterable for PyItertoolsCount {}
230240
impl IterNext for PyItertoolsCount {
231241
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
232242
let mut cur = zelf.cur.write();
233-
let step = zelf.step.clone();
234243
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+
}
236247
Ok(PyIterReturn::Return(result.to_pyobject(vm)))
237248
}
238249
}

0 commit comments

Comments
 (0)