Skip to content

Commit

Permalink
pythongh-114894: add array.array.clear() method (python#114919)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
4 people authored Feb 10, 2024
1 parent 5319c66 commit 9d1a353
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Doc/library/array.rst
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@ The module defines the following type:
Remove the first occurrence of *x* from the array.


.. method:: clear()

Remove all elements from the array.

.. versionadded:: 3.13


.. method:: reverse()

Reverse the order of the items in the array.
Expand Down
3 changes: 3 additions & 0 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ array
It can be used instead of ``'u'`` type code, which is deprecated.
(Contributed by Inada Naoki in :gh:`80480`.)

* Add ``clear()`` method in order to implement ``MutableSequence``.
(Contributed by Mike Zimin in :gh:`114894`.)

ast
---

Expand Down
23 changes: 23 additions & 0 deletions Lib/test/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,29 @@ def test_pop(self):
array.array(self.typecode, self.example[3:]+self.example[:-1])
)

def test_clear(self):
a = array.array(self.typecode, self.example)
with self.assertRaises(TypeError):
a.clear(42)
a.clear()
self.assertEqual(len(a), 0)
self.assertEqual(a.typecode, self.typecode)

a = array.array(self.typecode)
a.clear()
self.assertEqual(len(a), 0)
self.assertEqual(a.typecode, self.typecode)

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

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

def test_reverse(self):
a = array.array(self.typecode, self.example)
self.assertRaises(TypeError, a.reverse, 42)
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_collections.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Unit tests for collections.py."""

import array
import collections
import copy
import doctest
Expand Down Expand Up @@ -1972,6 +1973,7 @@ def test_MutableSequence(self):
for sample in [list, bytearray, deque]:
self.assertIsInstance(sample(), MutableSequence)
self.assertTrue(issubclass(sample, MutableSequence))
self.assertTrue(issubclass(array.array, MutableSequence))
self.assertFalse(issubclass(str, MutableSequence))
self.validate_abstract_methods(MutableSequence, '__contains__', '__iter__',
'__len__', '__getitem__', '__setitem__', '__delitem__', 'insert')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add :meth:`array.array.clear`.
16 changes: 16 additions & 0 deletions Modules/arraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,21 @@ array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
return (PyObject *)np;
}

/*[clinic input]
array.array.clear
Remove all items from the array.
[clinic start generated code]*/

static PyObject *
array_array_clear_impl(arrayobject *self)
/*[clinic end generated code: output=5efe0417062210a9 input=5dffa30e94e717a4]*/
{
if (array_resize(self, 0) == -1) {
return NULL;
}
Py_RETURN_NONE;
}

/*[clinic input]
array.array.__copy__
Expand Down Expand Up @@ -2342,6 +2357,7 @@ static PyMethodDef array_methods[] = {
ARRAY_ARRAY_APPEND_METHODDEF
ARRAY_ARRAY_BUFFER_INFO_METHODDEF
ARRAY_ARRAY_BYTESWAP_METHODDEF
ARRAY_ARRAY_CLEAR_METHODDEF
ARRAY_ARRAY___COPY___METHODDEF
ARRAY_ARRAY_COUNT_METHODDEF
ARRAY_ARRAY___DEEPCOPY___METHODDEF
Expand Down
20 changes: 19 additions & 1 deletion Modules/clinic/arraymodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9d1a353

Please sign in to comment.