FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-114894: add array.array.clear() method by mikeziminio · Pull Request #114919 · python/cpython · GitHub

/ cpython Public

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

Merged
JelleZijlstra merged 14 commits into
python:mainfrom
mikeziminio:fix-issue-114894
Feb 10, 2024
Merged

gh-114894: add array.array.clear() method#114919
JelleZijlstra merged 14 commits into
python:mainfrom
mikeziminio:fix-issue-114894

Conversation

mikeziminio commented Feb 2, 2024
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Contributor

Comment thread Modules/arraymodule.c
return (PyObject *)np;
}

/*[clinic input]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

done

Copy link
Copy Markdown
Contributor Author

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

JelleZijlstra self-requested a review February 2, 2024 19:28

JelleZijlstra left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Comment thread Lib/test/test_array.py Outdated
a = array.array(self.typecode, self.example)
self.assertRaises(TypeError, a.clear, 42)
a.clear()
self.assertEqual(a, array.array(self.typecode))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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).

Comment thread Lib/test/test_array.py
a.append(self.example[2])
a.append(self.example[3])
self.assertEqual(a, array.array(self.typecode, self.example[2:4]))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

done

Comment thread Modules/arraymodule.c Outdated

aisk commented Feb 3, 2024
edited
Loading

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
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.

Comment thread Lib/test/test_collections.py Outdated
from random import choice, randrange
from itertools import product, chain, combinations
import string
import array

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

done

rhettinger removed their request for review February 6, 2024 16:16
Comment thread Doc/whatsnew/3.13.rst Outdated
Comment thread Doc/library/array.rst
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>

Copy link
Copy Markdown
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 commented Feb 8, 2024

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
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 merged commit 9d1a353 into python:main Feb 10, 2024

Copy link
Copy Markdown
Contributor

Thanks!

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
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


Back | FazBrowse Home | New Git URL