Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9d1a353

Browse files
mikeziminioblurb-it[bot]aiskJelleZijlstra
authored
gh-114894: add array.array.clear() method (#114919)
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: AN Long <[email protected]> Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 5319c66 commit 9d1a353

File tree

7 files changed

+71
-1
lines changed

7 files changed

+71
-1
lines changed

Doc/library/array.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,13 @@ The module defines the following type:
215215
Remove the first occurrence of *x* from the array.
216216

217217

218+
.. method:: clear()
219+
220+
Remove all elements from the array.
221+
222+
.. versionadded:: 3.13
223+
224+
218225
.. method:: reverse()
219226

220227
Reverse the order of the items in the array.

Doc/whatsnew/3.13.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ array
185185
It can be used instead of ``'u'`` type code, which is deprecated.
186186
(Contributed by Inada Naoki in :gh:`80480`.)
187187

188+
* Add ``clear()`` method in order to implement ``MutableSequence``.
189+
(Contributed by Mike Zimin in :gh:`114894`.)
190+
188191
ast
189192
---
190193

Lib/test/test_array.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,29 @@ def test_pop(self):
10141014
array.array(self.typecode, self.example[3:]+self.example[:-1])
10151015
)
10161016

1017+
def test_clear(self):
1018+
a = array.array(self.typecode, self.example)
1019+
with self.assertRaises(TypeError):
1020+
a.clear(42)
1021+
a.clear()
1022+
self.assertEqual(len(a), 0)
1023+
self.assertEqual(a.typecode, self.typecode)
1024+
1025+
a = array.array(self.typecode)
1026+
a.clear()
1027+
self.assertEqual(len(a), 0)
1028+
self.assertEqual(a.typecode, self.typecode)
1029+
1030+
a = array.array(self.typecode, self.example)
1031+
a.clear()
1032+
a.append(self.example[2])
1033+
a.append(self.example[3])
1034+
self.assertEqual(a, array.array(self.typecode, self.example[2:4]))
1035+
1036+
with memoryview(a):
1037+
with self.assertRaises(BufferError):
1038+
a.clear()
1039+
10171040
def test_reverse(self):
10181041
a = array.array(self.typecode, self.example)
10191042
self.assertRaises(TypeError, a.reverse, 42)

Lib/test/test_collections.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Unit tests for collections.py."""
22

3+
import array
34
import collections
45
import copy
56
import doctest
@@ -1972,6 +1973,7 @@ def test_MutableSequence(self):
19721973
for sample in [list, bytearray, deque]:
19731974
self.assertIsInstance(sample(), MutableSequence)
19741975
self.assertTrue(issubclass(sample, MutableSequence))
1976+
self.assertTrue(issubclass(array.array, MutableSequence))
19751977
self.assertFalse(issubclass(str, MutableSequence))
19761978
self.validate_abstract_methods(MutableSequence, '__contains__', '__iter__',
19771979
'__len__', '__getitem__', '__setitem__', '__delitem__', 'insert')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add :meth:`array.array.clear`.

Modules/arraymodule.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,21 @@ array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
868868
return (PyObject *)np;
869869
}
870870

871+
/*[clinic input]
872+
array.array.clear
873+
874+
Remove all items from the array.
875+
[clinic start generated code]*/
876+
877+
static PyObject *
878+
array_array_clear_impl(arrayobject *self)
879+
/*[clinic end generated code: output=5efe0417062210a9 input=5dffa30e94e717a4]*/
880+
{
881+
if (array_resize(self, 0) == -1) {
882+
return NULL;
883+
}
884+
Py_RETURN_NONE;
885+
}
871886

872887
/*[clinic input]
873888
array.array.__copy__
@@ -2342,6 +2357,7 @@ static PyMethodDef array_methods[] = {
23422357
ARRAY_ARRAY_APPEND_METHODDEF
23432358
ARRAY_ARRAY_BUFFER_INFO_METHODDEF
23442359
ARRAY_ARRAY_BYTESWAP_METHODDEF
2360+
ARRAY_ARRAY_CLEAR_METHODDEF
23452361
ARRAY_ARRAY___COPY___METHODDEF
23462362
ARRAY_ARRAY_COUNT_METHODDEF
23472363
ARRAY_ARRAY___DEEPCOPY___METHODDEF

Modules/clinic/arraymodule.c.h

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)