From be9733aac4858164486728baba16e5aaece8fbc7 Mon Sep 17 00:00:00 2001 From: Mike Zimin Date: Fri, 2 Feb 2024 19:40:50 +0400 Subject: [PATCH 01/13] pythongh-114894: add array.array.clear() method --- Modules/arraymodule.c | 15 +++++++++++++++ Modules/clinic/arraymodule.c.h | 20 +++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index b97ade6126fa08..58a5f87949ff64 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -868,6 +868,20 @@ 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__ @@ -2342,6 +2356,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 diff --git a/Modules/clinic/arraymodule.c.h b/Modules/clinic/arraymodule.c.h index dbce0313541649..0039c985c633a9 100644 --- a/Modules/clinic/arraymodule.c.h +++ b/Modules/clinic/arraymodule.c.h @@ -5,6 +5,24 @@ preserve #include "pycore_abstract.h" // _PyNumber_Index() #include "pycore_modsupport.h" // _PyArg_CheckPositional() +PyDoc_STRVAR(array_array_clear__doc__, +"clear($self, /)\n" +"--\n" +"\n" +"Remove all items from the array."); + +#define ARRAY_ARRAY_CLEAR_METHODDEF \ + {"clear", (PyCFunction)array_array_clear, METH_NOARGS, array_array_clear__doc__}, + +static PyObject * +array_array_clear_impl(arrayobject *self); + +static PyObject * +array_array_clear(arrayobject *self, PyObject *Py_UNUSED(ignored)) +{ + return array_array_clear_impl(self); +} + PyDoc_STRVAR(array_array___copy____doc__, "__copy__($self, /)\n" "--\n" @@ -667,4 +685,4 @@ PyDoc_STRVAR(array_arrayiterator___setstate____doc__, #define ARRAY_ARRAYITERATOR___SETSTATE___METHODDEF \ {"__setstate__", (PyCFunction)array_arrayiterator___setstate__, METH_O, array_arrayiterator___setstate____doc__}, -/*[clinic end generated code: output=bf086c01e7e482bf input=a9049054013a1b77]*/ +/*[clinic end generated code: output=3a466b574dda4e17 input=a9049054013a1b77]*/ From 9c2c360f51ae960d758081674ddc78dcaf4088f9 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 2 Feb 2024 15:50:14 +0000 Subject: [PATCH 02/13] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20b?= =?UTF-8?q?lurb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2024-02-02-15-50-13.gh-issue-114894.DF-dSd.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2024-02-02-15-50-13.gh-issue-114894.DF-dSd.rst diff --git a/Misc/NEWS.d/next/Library/2024-02-02-15-50-13.gh-issue-114894.DF-dSd.rst b/Misc/NEWS.d/next/Library/2024-02-02-15-50-13.gh-issue-114894.DF-dSd.rst new file mode 100644 index 00000000000000..fc110dea8aa89f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-02-02-15-50-13.gh-issue-114894.DF-dSd.rst @@ -0,0 +1 @@ +Add array.clear() method From 8adac42b0b4b8e9eadbaa70964f8b12ea3118c71 Mon Sep 17 00:00:00 2001 From: Mike Zimin Date: Fri, 2 Feb 2024 22:54:00 +0400 Subject: [PATCH 03/13] added test and documentation --- Doc/library/array.rst | 5 +++++ Lib/test/test_array.py | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/Doc/library/array.rst b/Doc/library/array.rst index a0e8bb20a098fd..a49c392b5b3260 100644 --- a/Doc/library/array.rst +++ b/Doc/library/array.rst @@ -215,6 +215,11 @@ The module defines the following type: Remove the first occurrence of *x* from the array. + .. method:: clear() + + Remove all elements from the array. + + .. method:: reverse() Reverse the order of the items in the array. diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py index a219fa365e7f20..5f60a7af145d6a 100755 --- a/Lib/test/test_array.py +++ b/Lib/test/test_array.py @@ -1013,6 +1013,22 @@ def test_pop(self): a, array.array(self.typecode, self.example[3:]+self.example[:-1]) ) + + def test_clear(self): + a = array.array(self.typecode, self.example) + self.assertRaises(TypeError, a.clear, 42) + a.clear() + self.assertEqual(a, array.array(self.typecode)) + + a = array.array(self.typecode) + a.clear() + self.assertEqual(a, array.array(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])) def test_reverse(self): a = array.array(self.typecode, self.example) From e04c2fa8651975bbaff9b27125513a89307755b3 Mon Sep 17 00:00:00 2001 From: Mike Zimin Date: Fri, 2 Feb 2024 23:10:12 +0400 Subject: [PATCH 04/13] fixed - trim trailing whitespace --- Lib/test/test_array.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py index 5f60a7af145d6a..e558767ee03f6e 100755 --- a/Lib/test/test_array.py +++ b/Lib/test/test_array.py @@ -1013,17 +1013,17 @@ def test_pop(self): a, array.array(self.typecode, self.example[3:]+self.example[:-1]) ) - + def test_clear(self): a = array.array(self.typecode, self.example) self.assertRaises(TypeError, a.clear, 42) a.clear() self.assertEqual(a, array.array(self.typecode)) - + a = array.array(self.typecode) a.clear() self.assertEqual(a, array.array(self.typecode)) - + a = array.array(self.typecode, self.example) a.clear() a.append(self.example[2]) From 763006945d79d9924d61ce4b21d23a0bd2094806 Mon Sep 17 00:00:00 2001 From: Mike Zimin <122507876+mikeziminio@users.noreply.github.com> Date: Tue, 6 Feb 2024 09:32:09 +0400 Subject: [PATCH 05/13] Update Modules/arraymodule.c Co-authored-by: AN Long --- Modules/arraymodule.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index 58a5f87949ff64..df09d9d84789f7 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -878,8 +878,9 @@ static PyObject * array_array_clear_impl(arrayobject *self) /*[clinic end generated code: output=5efe0417062210a9 input=5dffa30e94e717a4]*/ { - if (array_resize(self, 0) == -1) + if (array_resize(self, 0) == -1) { return NULL; + } Py_RETURN_NONE; } From 556bf1a1c756525b9f3287096a8a1bc8c7839fc0 Mon Sep 17 00:00:00 2001 From: Mike Zimin <122507876+mikeziminio@users.noreply.github.com> Date: Tue, 6 Feb 2024 09:33:35 +0400 Subject: [PATCH 06/13] Update Misc/NEWS.d/next/Library/2024-02-02-15-50-13.gh-issue-114894.DF-dSd.rst Co-authored-by: Jelle Zijlstra --- .../next/Library/2024-02-02-15-50-13.gh-issue-114894.DF-dSd.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-02-02-15-50-13.gh-issue-114894.DF-dSd.rst b/Misc/NEWS.d/next/Library/2024-02-02-15-50-13.gh-issue-114894.DF-dSd.rst index fc110dea8aa89f..ec620f2aae3f03 100644 --- a/Misc/NEWS.d/next/Library/2024-02-02-15-50-13.gh-issue-114894.DF-dSd.rst +++ b/Misc/NEWS.d/next/Library/2024-02-02-15-50-13.gh-issue-114894.DF-dSd.rst @@ -1 +1 @@ -Add array.clear() method +Add :meth:`array.array.clear`. From b5b9e63a213c0021212beb372187523a4da5d056 Mon Sep 17 00:00:00 2001 From: Mike Zimin Date: Tue, 6 Feb 2024 15:30:26 +0400 Subject: [PATCH 07/13] add memoryview test, update other tests --- Lib/test/test_array.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py index e558767ee03f6e..95383be9659eb9 100755 --- a/Lib/test/test_array.py +++ b/Lib/test/test_array.py @@ -1016,13 +1016,16 @@ def test_pop(self): def test_clear(self): a = array.array(self.typecode, self.example) - self.assertRaises(TypeError, a.clear, 42) + with self.assertRaises(TypeError): + a.clear(42) a.clear() - self.assertEqual(a, array.array(self.typecode)) + self.assertEqual(len(a), 0) + self.assertEqual(a.typecode, self.typecode) a = array.array(self.typecode) a.clear() - self.assertEqual(a, array.array(self.typecode)) + self.assertEqual(len(a), 0) + self.assertEqual(a.typecode, self.typecode) a = array.array(self.typecode, self.example) a.clear() @@ -1030,6 +1033,10 @@ def test_clear(self): 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) From 83422b001e9ebefe167bd17cd37f591d8359f37a Mon Sep 17 00:00:00 2001 From: Mike Zimin Date: Tue, 6 Feb 2024 16:11:00 +0400 Subject: [PATCH 08/13] add MutableSequence test --- Lib/test/test_collections.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index 7e6f811e17cfa2..e1c2dc5fe4f6e6 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -9,6 +9,7 @@ from random import choice, randrange from itertools import product, chain, combinations import string +import array import sys from test import support import types @@ -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') From 519ad216efdcb2a543bdb11ab35a4deb972e6166 Mon Sep 17 00:00:00 2001 From: Mike Zimin Date: Tue, 6 Feb 2024 17:09:46 +0400 Subject: [PATCH 09/13] add whatsnew doc --- Doc/whatsnew/3.13.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index 372757759b986f..6b1b76601fd91f 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -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 (and reviewers?) in :gh:`114919`.) + ast --- From 624224256f2d87b1cb5e15772ddfd938f17dcfdb Mon Sep 17 00:00:00 2001 From: Mike Zimin Date: Tue, 6 Feb 2024 17:30:55 +0400 Subject: [PATCH 10/13] change issue reference --- Doc/whatsnew/3.13.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index 6b1b76601fd91f..311c273f303036 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -186,7 +186,7 @@ array (Contributed by Inada Naoki in :gh:`80480`.) * Add ``clear()`` method in order to implement ``MutableSequence``. - (Contributed by Mike Zimin (and reviewers?) in :gh:`114919`.) + (Contributed by Mike Zimin (and reviewers?) in :gh:`114894`.) ast --- From fc76d73b3a9a6a90319fe2b8715ab0505445d13c Mon Sep 17 00:00:00 2001 From: Mike Zimin Date: Tue, 6 Feb 2024 17:59:57 +0400 Subject: [PATCH 11/13] move so that it matches the alphabetical order. --- Lib/test/test_collections.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index e1c2dc5fe4f6e6..1fb492ecebd668 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -1,5 +1,6 @@ """Unit tests for collections.py.""" +import array import collections import copy import doctest @@ -9,7 +10,6 @@ from random import choice, randrange from itertools import product, chain, combinations import string -import array import sys from test import support import types From 7b46716e10d1aa52a8347136f8ad020e427637e7 Mon Sep 17 00:00:00 2001 From: Mike Zimin <122507876+mikeziminio@users.noreply.github.com> Date: Wed, 7 Feb 2024 07:19:19 +0400 Subject: [PATCH 12/13] Update Doc/whatsnew/3.13.rst Co-authored-by: Jelle Zijlstra --- Doc/whatsnew/3.13.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index 311c273f303036..65ccca74d2075d 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -186,7 +186,7 @@ array (Contributed by Inada Naoki in :gh:`80480`.) * Add ``clear()`` method in order to implement ``MutableSequence``. - (Contributed by Mike Zimin (and reviewers?) in :gh:`114894`.) + (Contributed by Mike Zimin in :gh:`114894`.) ast --- From a399ac6aab0afb21baa84b8bdbfbd9898d74de55 Mon Sep 17 00:00:00 2001 From: Mike Zimin <122507876+mikeziminio@users.noreply.github.com> Date: Wed, 7 Feb 2024 07:19:36 +0400 Subject: [PATCH 13/13] Update Doc/library/array.rst Co-authored-by: Jelle Zijlstra --- Doc/library/array.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Doc/library/array.rst b/Doc/library/array.rst index a49c392b5b3260..043badf05ffc12 100644 --- a/Doc/library/array.rst +++ b/Doc/library/array.rst @@ -219,6 +219,8 @@ The module defines the following type: Remove all elements from the array. + .. versionadded:: 3.13 + .. method:: reverse()