Repository navigation
Fix itertools.count to take PyNumber instead of PyInt #3822
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
aad60e8
Fix to get number of itertools count
mumallaeng 79cb376
Fix itertools count
mumallaeng d3485f4
Fix itertools count
mumallaeng e7c1eb2
Fix itertools count
mumallaeng 89ae703
Fix itertools count step
mumallaeng 4422a54
Fix itertools count PyResult
mumallaeng e1212b7
Fix itertools count next
mumallaeng fd85e71
Fix clippy error
mumallaeng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,14 +11,13 @@ 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_traits::{Signed, ToPrimitive}; | ||
| use std::fmt; | ||
|
|
||
| #[pyattr] | ||
|
|
@@ -174,14 +173,14 @@ mod decl { | |
| #[pyclass(name = "count")] | ||
| #[derive(Debug, PyPayload)] | ||
| struct PyItertoolsCount { | ||
| cur: PyRwLock<BigInt>, | ||
| step: BigInt, | ||
| cur: PyRwLock<PyObjectRef>, | ||
| step: PyIntRef, | ||
| } | ||
|
|
||
| #[derive(FromArgs)] | ||
| struct CountNewArgs { | ||
| #[pyarg(positional, optional)] | ||
| start: OptionalArg<PyIntRef>, | ||
| start: OptionalArg<PyObjectRef>, | ||
|
|
||
| #[pyarg(positional, optional)] | ||
| step: OptionalArg<PyIntRef>, | ||
|
|
@@ -195,14 +194,11 @@ 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 step = match step.into_option() { | ||
| Some(int) => int.as_bigint().clone(), | ||
| None => BigInt::one(), | ||
| }; | ||
| 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())); | ||
| } | ||
|
|
||
| PyItertoolsCount { | ||
| cur: PyRwLock::new(start), | ||
|
|
@@ -219,7 +215,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<Self>) -> (PyTypeRef, (BigInt,)) { | ||
| fn reduce(zelf: PyRef<Self>) -> (PyTypeRef, (PyObjectRef,)) { | ||
| (zelf.class().clone(), (zelf.cur.read().clone(),)) | ||
| } | ||
|
|
||
|
|
@@ -234,8 +230,9 @@ mod decl { | |
| impl IterNext for PyItertoolsCount { | ||
| fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> { | ||
| let mut cur = zelf.cur.write(); | ||
| let step = zelf.step.clone(); | ||
| let result = cur.clone(); | ||
| *cur += &zelf.step; | ||
| *cur = vm._iadd(&*cur, step.as_object())?; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, it does. I didn't know that. |
||
| Ok(PyIterReturn::Return(result.to_pyobject(vm))) | ||
| } | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems
stepalso need to be checked viaPyNumber::check.https://github.com/python/cpython/blob/3.10/Modules/itertoolsmodule.c#L4178-L4182
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't because it's a PyIntRef. I'll fix it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PyIntRef is always a PyNumber.