From aad60e8d19a1603024d58653e0843b85e890209e Mon Sep 17 00:00:00 2001 From: oow214 Date: Sun, 26 Jun 2022 23:28:29 +0900 Subject: [PATCH 1/8] Fix to get number of itertools count --- vm/src/stdlib/itertools.rs | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index ebdf87fc1a..0cc3eede2a 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -11,14 +11,14 @@ mod decl { convert::ToPyObject, function::{ArgCallable, FuncArgs, OptionalArg, OptionalOption, PosArgs}, identifier, - protocol::{PyIter, PyIterReturn}, + protocol::{PyIter, PyIterReturn, PyNumber}, stdlib::sys, types::{Constructor, IterNext, IterNextIterable}, AsObject, Py, PyObjectRef, PyPayload, PyRef, PyResult, PyWeakRef, VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; - use num_bigint::BigInt; - use num_traits::{One, Signed, ToPrimitive, Zero}; + // use num_bigint::BigInt; + use num_traits::{Signed, ToPrimitive}; use std::fmt; #[pyattr] @@ -174,14 +174,13 @@ mod decl { #[pyclass(name = "count")] #[derive(Debug, PyPayload)] struct PyItertoolsCount { - cur: PyRwLock, - step: BigInt, + cur: PyRwLock, + step: Option, } #[derive(FromArgs)] struct CountNewArgs { - #[pyarg(positional, optional)] - start: OptionalArg, + start: PyObjectRef, #[pyarg(positional, optional)] step: OptionalArg, @@ -195,14 +194,17 @@ mod decl { Self::Args { start, step }: Self::Args, vm: &VirtualMachine, ) -> PyResult { - let start = match start.into_option() { - Some(int) => int.as_bigint().clone(), - None => BigInt::zero(), - }; + let start = start.clone(); let step = match step.into_option() { - Some(int) => int.as_bigint().clone(), - None => BigInt::one(), + Some(int) => { + let val: isize = int.try_to_primitive(vm)?; + Some(vm.new_pyref(val.to_usize().unwrap_or(0))) + } + None => None, }; + if PyNumber::check(&start, vm) == false { + return Err(vm.new_value_error("a number is require".to_owned())); + } PyItertoolsCount { cur: PyRwLock::new(start), @@ -219,7 +221,7 @@ mod decl { // if (lz->cnt == PY_SSIZE_T_MAX) // return Py_BuildValue("0(00)", Py_TYPE(lz), lz->long_cnt, lz->long_step); #[pymethod(magic)] - fn reduce(zelf: PyRef) -> (PyTypeRef, (BigInt,)) { + fn reduce(zelf: PyRef) -> (PyTypeRef, (PyObjectRef,)) { (zelf.class().clone(), (zelf.cur.read().clone(),)) } @@ -235,7 +237,9 @@ mod decl { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { let mut cur = zelf.cur.write(); let result = cur.clone(); - *cur += &zelf.step; + if let Some(step) = &zelf.step { + *cur = vm._iadd(&*cur, step.as_object())?; + } Ok(PyIterReturn::Return(result.to_pyobject(vm))) } } From 79cb376e473af8157d480e378ecdd037506662e9 Mon Sep 17 00:00:00 2001 From: oow214 Date: Mon, 27 Jun 2022 18:14:19 +0900 Subject: [PATCH 2/8] Fix itertools count --- vm/src/stdlib/itertools.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index 0cc3eede2a..7ac81ffe77 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -180,7 +180,8 @@ mod decl { #[derive(FromArgs)] struct CountNewArgs { - start: PyObjectRef, + #[pyarg(positional, optional)] + start: OptionalArg, #[pyarg(positional, optional)] step: OptionalArg, @@ -194,7 +195,10 @@ mod decl { Self::Args { start, step }: Self::Args, vm: &VirtualMachine, ) -> PyResult { - let start = start.clone(); + let start = match start.into_option() { + Some(num) => vm.new_pyobj(num.clone()), + None => vm.new_pyobj(0), + }; let step = match step.into_option() { Some(int) => { let val: isize = int.try_to_primitive(vm)?; @@ -239,6 +243,9 @@ mod decl { let result = cur.clone(); if let Some(step) = &zelf.step { *cur = vm._iadd(&*cur, step.as_object())?; + } else { + let one = vm.new_pyobj(1); + *cur = vm._add(&*cur, &one)?; } Ok(PyIterReturn::Return(result.to_pyobject(vm))) } From d3485f43aff5d77f00434d66583091eaa06f2708 Mon Sep 17 00:00:00 2001 From: oow214 Date: Mon, 27 Jun 2022 18:39:35 +0900 Subject: [PATCH 3/8] Fix itertools count --- vm/src/stdlib/itertools.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index 7ac81ffe77..6c211eeb5a 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -196,7 +196,7 @@ mod decl { vm: &VirtualMachine, ) -> PyResult { let start = match start.into_option() { - Some(num) => vm.new_pyobj(num.clone()), + Some(num) => vm.new_pyobj(num), None => vm.new_pyobj(0), }; let step = match step.into_option() { @@ -206,7 +206,7 @@ mod decl { } None => None, }; - if PyNumber::check(&start, vm) == false { + if !PyNumber::check(&start, vm) { return Err(vm.new_value_error("a number is require".to_owned())); } From e7c1eb2334d38b251c44d3b41340e4881c046615 Mon Sep 17 00:00:00 2001 From: oow214 Date: Mon, 27 Jun 2022 20:12:53 +0900 Subject: [PATCH 4/8] Fix itertools count --- vm/src/stdlib/itertools.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index 6c211eeb5a..baebfe29ee 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -17,7 +17,6 @@ mod decl { AsObject, Py, PyObjectRef, PyPayload, PyRef, PyResult, PyWeakRef, VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; - // use num_bigint::BigInt; use num_traits::{Signed, ToPrimitive}; use std::fmt; From 89ae703b054a64d63b53ddfe69c84f947833821c Mon Sep 17 00:00:00 2001 From: oow214 Date: Mon, 27 Jun 2022 21:50:06 +0900 Subject: [PATCH 5/8] Fix itertools count step --- vm/src/stdlib/itertools.rs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index baebfe29ee..822cb08efe 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -174,7 +174,7 @@ mod decl { #[derive(Debug, PyPayload)] struct PyItertoolsCount { cur: PyRwLock, - step: Option, + step: PyIntRef, } #[derive(FromArgs)] @@ -201,9 +201,9 @@ mod decl { let step = match step.into_option() { Some(int) => { let val: isize = int.try_to_primitive(vm)?; - Some(vm.new_pyref(val.to_usize().unwrap_or(0))) + vm.new_pyref(val.to_usize().unwrap_or(0)) } - None => None, + None => vm.new_pyref(1), }; if !PyNumber::check(&start, vm) { return Err(vm.new_value_error("a number is require".to_owned())); @@ -238,14 +238,9 @@ mod decl { impl IterNextIterable for PyItertoolsCount {} impl IterNext for PyItertoolsCount { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { - let mut cur = zelf.cur.write(); + let cur = zelf.cur.write(); let result = cur.clone(); - if let Some(step) = &zelf.step { - *cur = vm._iadd(&*cur, step.as_object())?; - } else { - let one = vm.new_pyobj(1); - *cur = vm._add(&*cur, &one)?; - } + vm._iadd(&*cur, &zelf.step.as_object())?; Ok(PyIterReturn::Return(result.to_pyobject(vm))) } } From 4422a54fc2bde721aae2cfc29701087da0da884d Mon Sep 17 00:00:00 2001 From: oow214 Date: Mon, 27 Jun 2022 21:54:53 +0900 Subject: [PATCH 6/8] Fix itertools count PyResult --- vm/src/stdlib/itertools.rs | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index 822cb08efe..e8640cb812 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -194,17 +194,8 @@ mod decl { Self::Args { start, step }: Self::Args, vm: &VirtualMachine, ) -> PyResult { - let start = match start.into_option() { - Some(num) => vm.new_pyobj(num), - None => vm.new_pyobj(0), - }; - let step = match step.into_option() { - Some(int) => { - let val: isize = int.try_to_primitive(vm)?; - vm.new_pyref(val.to_usize().unwrap_or(0)) - } - None => vm.new_pyref(1), - }; + let start: PyObjectRef = start.into_option().unwrap_or_else(|| vm.new_pyobj(0)); + let step: PyIntRef = step.into_option().unwrap_or_else(|| vm.new_pyref(1)); if !PyNumber::check(&start, vm) { return Err(vm.new_value_error("a number is require".to_owned())); } From e1212b70d7cf40495c0e173007c6b793e06fdeca Mon Sep 17 00:00:00 2001 From: oow214 Date: Mon, 27 Jun 2022 22:05:00 +0900 Subject: [PATCH 7/8] Fix itertools count next --- vm/src/stdlib/itertools.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index e8640cb812..c80598467b 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -229,9 +229,10 @@ mod decl { impl IterNextIterable for PyItertoolsCount {} impl IterNext for PyItertoolsCount { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { - let cur = zelf.cur.write(); + let mut cur = zelf.cur.write(); + let step = zelf.step.clone(); let result = cur.clone(); - vm._iadd(&*cur, &zelf.step.as_object())?; + *cur = vm._iadd(&*cur, &step.as_object())?; Ok(PyIterReturn::Return(result.to_pyobject(vm))) } } From fd85e7123f05be58502e17bfb36e7ffdce0b9480 Mon Sep 17 00:00:00 2001 From: oow214 Date: Mon, 27 Jun 2022 22:13:54 +0900 Subject: [PATCH 8/8] Fix clippy error --- vm/src/stdlib/itertools.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index c80598467b..a8e1b52583 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -232,7 +232,7 @@ mod decl { let mut cur = zelf.cur.write(); let step = zelf.step.clone(); let result = cur.clone(); - *cur = vm._iadd(&*cur, &step.as_object())?; + *cur = vm._iadd(&*cur, step.as_object())?; Ok(PyIterReturn::Return(result.to_pyobject(vm))) } }