gh-114894: add array.array.clear() method#114919
Conversation
| return (PyObject *)np; | ||
| } | ||
|
|
||
| /*[clinic input] |
There was a problem hiding this comment.
This will also need new unit tests (probably in test_array.py but I haven't checked) and a documentation entry (in array.rst)
There was a problem hiding this comment.
This will also need new unit tests (probably in
test_array.pybut I haven't checked) and a documentation entry (inarray.rst)
Thanks for the comment. I'll try to do it now.
3368a6b to
9c2c360
Compare
|
@JelleZijlstra Jelle, I've added tests and documentation) |
JelleZijlstra
left a comment
There was a problem hiding this comment.
Can you also add this to the What's New document for Python 3.13 (Doc/whatsnew/3.13.rst)?
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)) |
There was a problem hiding this comment.
This is a bit of an indirect test, why not instead test that len(a) == 0?
There was a problem hiding this comment.
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])) | ||
|
|
There was a problem hiding this comment.
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()
Misc/NEWS.d/next/Library/2024-02-02-15-50-13.gh-issue-114894.DF-dSd.rst
Outdated
Show resolved
Hide resolved
|
In my personal view, according to the original issue, the intention behind adding the There is already a relevant test case in cpython/Lib/test/test_collections.py Line 1968 in 28bb296 array.array in this existing test case.
|
Co-authored-by: AN Long <aisk@users.noreply.github.com>
…F-dSd.rst Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
done I added only the |
Lib/test/test_collections.py
Outdated
| from random import choice, randrange | ||
| from itertools import product, chain, combinations | ||
| import string | ||
| import array |
There was a problem hiding this comment.
Nitpick: These imports are basically sorted alphabetically, so this line may move to the first line.
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
|
@JelleZijlstra @aisk Guys, if I understand correctly, I made all the changes according to the reviews. |
|
LGTM, I think the next step is to wait for a core member to approve and merge it. |
|
Thanks all for the efforts, sorry for tagging but gentle nudge to @vstinner who seems to have touched the relevant files recently (according to |
|
Thanks! |
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>
Uh oh!
There was an error while loading. Please reload this page.