-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
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
Conversation
mikeziminio
commented
Feb 2, 2024
•
edited by bedevere-app
bot
Loading
edited by bedevere-app
bot
- Issue: Add array.clear() #114894
@@ -868,6 +868,20 @@ array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh) | |||
return (PyObject *)np; | |||
} | |||
|
|||
/*[clinic input] |
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.
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.
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 (inarray.rst
)
Thanks for the comment. I'll try to do it now.
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.
done
3368a6b
to
9c2c360
Compare
@JelleZijlstra Jelle, I've added tests and documentation) |
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.
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.
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
?
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.
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.
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()
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.
done
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
@@ -9,6 +9,7 @@ | |||
from random import choice, randrange | |||
from itertools import product, chain, combinations | |||
import string | |||
import array |
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.
Nitpick: These imports are basically sorted alphabetically, so this line may move to the first line.
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.
done
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>