Skip to content

add arrayiter.__reduce__ #3868

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 8 commits into from
Jul 18, 2022
Merged

Conversation

maong0927
Copy link
Contributor

No description provided.

@@ -1258,9 +1258,11 @@ mod array {

impl Iterable for PyArray {
fn iter(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult {
let zelf_ = zelf.clone();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let zelf_ = zelf.clone();

@@ -1258,9 +1258,11 @@ mod array {

impl Iterable for PyArray {
fn iter(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult {
let zelf_ = zelf.clone();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_ prefix usually means unused variable in rust code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The _ suffix usually indicates that you're dodging the use of a reserved keyword. From the Rust style guide:

When a name is forbidden because it is a reserved word (e.g., crate), use a trailing underscore to make the name legal (e.g., crate_), or use raw identifiers if possible.

@@ -1258,9 +1258,11 @@ mod array {

impl Iterable for PyArray {
fn iter(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult {
let zelf_ = zelf.clone();
Ok(PyArrayIter {
position: AtomicUsize::new(0),
array: zelf,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
array: zelf,
array: zelf.clone(),

change here to allow to use zelf later.

Ok(PyArrayIter {
position: AtomicUsize::new(0),
array: zelf,
internal: PyMutex::new(PositionIterInternal::new(zelf_, 0)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
internal: PyMutex::new(PositionIterInternal::new(zelf_, 0)),
internal: PyMutex::new(PositionIterInternal::new(zelf.clone(), 0)),

Is PositionIterInternal necessary for implementing __reduce__?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, this is a good point. internal is doing nothing here. it must be position.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation was implemented by referring to the implementation of list iter.
Is there any reason to use zelf as it is instead of zelf.clone in list iter?

Copy link
Member

@youknowone youknowone Jul 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @Snowapril got confused here. because zelf is already moved in upper line, it can't be cloned here.

@Snowapril
Copy link
Contributor

Please do not use git merge. Would you try to rebase it?
https://docs.gitlab.com/ee/topics/git/git_rebase.html

@@ -1281,10 +1282,22 @@ mod array {
pub struct PyArrayIter {
position: AtomicUsize,
array: PyArrayRef,
internal: PyMutex<PositionIterInternal<PyArrayRef>>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you really need both position and internal? Though I didn't look in details, those fields look like duplication.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it because internal contains position because it is PositionIterInternal as its name suggests?

Copy link
Member

@youknowone youknowone Jul 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check the PositionIterInternal type. it contains position field. By this change, the code have duplicated position fields but most of code uses position and new code uses internal. I don't think it can correctly work

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With comparison with other types' iterator, we really don't need position and array field because PyMutex<PositionIterInternal<PyArrayRef>> will do their roles. Because of __next__ and __setstate__, we need to modify position and array fields. It leads to the need for PyMutex.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we replace position and array fields with PyMutex<PositionIterInternal<PyArrayRef>>, we can implement other methods as usual we did (__next__ and __reduce__)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does arrayiterator require closed field? I thought it only need position.
closed is useful when it include another iterator in it.

@fanninpm
Copy link
Contributor

Please do not use git merge. Would you try to rebase it?
https://docs.gitlab.com/ee/topics/git/git_rebase.html

The following commands should get you started:

git switch main
git pull upstream main
git checkout arrayiter_reduce
git rebase -i main

@youknowone
Copy link
Member

youknowone commented Jul 12, 2022

because people sometimes use main as a working branch, I like to recommend to use upstream/main directly

git fetch upstream
git rebase -i upstream/main

this is same to

git pull --rebase upstream main 

Though I never use pull.

@maong0927
Copy link
Contributor Author

I'll use rebase from now on

@youknowone youknowone added the z-ca-2022 Tag to track contrubution-academy 2022 label Jul 13, 2022
maong0927 and others added 3 commits July 13, 2022 18:04
Co-authored-by: Yaminyam <siontama@gmail.com>
Co-authored-by: Yaminyam <siontama@gmail.com>
@fanninpm
Copy link
Contributor

According to the CI, there are a few more tests to address. You can run the following command to take a closer look:

cargo run --release --features ssl,jit -- -m test.test_array -vf

In addition, you can run cargo fmt --all to fix the formatting errors.

Copy link
Member

@youknowone youknowone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for contributing!

@youknowone youknowone merged commit 4824183 into RustPython:main Jul 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
z-ca-2022 Tag to track contrubution-academy 2022
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants