Skip to content

gh-114894: add array.array.clear() method #114919

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 14 commits into from
Feb 10, 2024

Conversation

mikeziminio
Copy link
Contributor

@mikeziminio mikeziminio commented Feb 2, 2024

@@ -868,6 +868,20 @@ array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
return (PyObject *)np;
}

/*[clinic input]
Copy link
Member

Choose a reason for hiding this comment

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

This will also need new unit tests (probably in test_array.py but I haven't checked) and a documentation entry (in array.rst)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This will also need new unit tests (probably in test_array.py but I haven't checked) and a documentation entry (in array.rst)

Thanks for the comment. I'll try to do it now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@mikeziminio
Copy link
Contributor Author

@JelleZijlstra Jelle, I've added tests and documentation)

@JelleZijlstra JelleZijlstra self-requested a review February 2, 2024 19:28
Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

Can you also add this to the What's New document for Python 3.13 (Doc/whatsnew/3.13.rst)?

a = array.array(self.typecode, self.example)
self.assertRaises(TypeError, a.clear, 42)
a.clear()
self.assertEqual(a, array.array(self.typecode))
Copy link
Member

Choose a reason for hiding this comment

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

This is a bit of an indirect test, why not instead test that len(a) == 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All the testing logic from the test_array.py file works so that the methods are tested for all possible typecode.

To be honest, initially I used array comparison expecting that there would be a typecode check, but then I looked at array_richcompare() and realized that comparing arrays using the == operator is not suitable for checking the same typecode.

Therefore, I added two checks - the len() you suggested and the typecode comparison (just in case of future optimizations).

a.append(self.example[2])
a.append(self.example[3])
self.assertEqual(a, array.array(self.typecode, self.example[2:4]))

Copy link
Member

Choose a reason for hiding this comment

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

Add a test that an array with exported buffers cannot be cleared, something like this (untested):

with memoryview(a):
    with self.assertRaises(BufferError):
        a.clear()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@aisk
Copy link
Contributor

aisk commented Feb 3, 2024

In my personal view, according to the original issue, the intention behind adding the clear method is to allow array.array to implement collections.abc.MutableSequence, so we can add a test to test it and ensure we don't break it in the future.

There is already a relevant test case in

def test_MutableSequence(self):
, so what we need to do is simply include array.array in this existing test case.

mikeziminio and others added 4 commits February 6, 2024 09:32
Co-authored-by: AN Long <aisk@users.noreply.github.com>
…F-dSd.rst

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
@mikeziminio
Copy link
Contributor Author

In my personal view, according to the original issue, the intention behind adding the clear method is to allow array.array to implement collections.abc.MutableSequence, so we can add a test to test it and ensure we don't break it in the future.

There is already a relevant test case in

def test_MutableSequence(self):

, so what we need to do is simply include array.array in this existing test case.

done

I added only the issubclass check (without isinstance), because for isinstance it would have to be check all typecodes.

@@ -9,6 +9,7 @@
from random import choice, randrange
from itertools import product, chain, combinations
import string
import array
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: These imports are basically sorted alphabetically, so this line may move to the first line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@rhettinger rhettinger removed their request for review February 6, 2024 16:16
mikeziminio and others added 2 commits February 7, 2024 07:19
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
@mikeziminio
Copy link
Contributor Author

@JelleZijlstra @aisk Guys, if I understand correctly, I made all the changes according to the reviews.
Please tell me what further steps are needed to merge the PR.

@aisk
Copy link
Contributor

aisk commented Feb 8, 2024

LGTM, I think the next step is to wait for a core member to approve and merge it.

@Jason-Y-Z
Copy link
Contributor

Thanks all for the efforts, sorry for tagging but gentle nudge to @vstinner who seems to have touched the relevant files recently (according to BLAME).

@JelleZijlstra JelleZijlstra merged commit 9d1a353 into python:main Feb 10, 2024
@Jason-Y-Z
Copy link
Contributor

Thanks!

fsc-eriker pushed a commit to fsc-eriker/cpython that referenced this pull request Feb 14, 2024
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: AN Long <aisk@users.noreply.github.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants