[Python-checkins] bpo-43908: Make array.array type immutable (GH-25696)

vstinner webhook-mailer at python.org
Thu Apr 29 02:47:55 EDT 2021


https://github.com/python/cpython/commit/c6ad03fddf4b04c60dca4327140e59fb2dcca8e5
commit: c6ad03fddf4b04c60dca4327140e59fb2dcca8e5
branch: master
author: Erlend Egeberg Aasland <erlend.aasland at innova.no>
committer: vstinner <vstinner at python.org>
date: 2021-04-29T08:47:48+02:00
summary:

bpo-43908: Make array.array type immutable (GH-25696)

Co-authored-by: Victor Stinner <vstinner at python.org>

files:
A Misc/NEWS.d/next/Core and Builtins/2021-04-26-20-59-17.bpo-43908.-COW4-.rst
M Lib/test/test_array.py
M Modules/arraymodule.c

diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py
index bdcd1254b304f..11184add6d399 100644
--- a/Lib/test/test_array.py
+++ b/Lib/test/test_array.py
@@ -40,6 +40,12 @@ def test_bad_constructor(self):
         self.assertRaises(TypeError, array.array, 'xx')
         self.assertRaises(ValueError, array.array, 'x')
 
+    @support.cpython_only
+    def test_immutable(self):
+        # bpo-43908: check that array.array is immutable
+        with self.assertRaises(TypeError):
+            array.array.foo = 1
+
     def test_empty(self):
         # Exercise code for handling zero-length arrays
         a = array.array('B')
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-04-26-20-59-17.bpo-43908.-COW4-.rst b/Misc/NEWS.d/next/Core and Builtins/2021-04-26-20-59-17.bpo-43908.-COW4-.rst
new file mode 100644
index 0000000000000..07303b99d1f95
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-04-26-20-59-17.bpo-43908.-COW4-.rst	
@@ -0,0 +1,2 @@
+Make the :class:`array.array` type immutable. Patch by
+Erlend E. Aasland.
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
index f5326789521d3..367621fd03b88 100644
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -2847,7 +2847,8 @@ static PyType_Slot array_slots[] = {
 static PyType_Spec array_spec = {
     .name = "array.array",
     .basicsize = sizeof(arrayobject),
-    .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+    .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
+              Py_TPFLAGS_IMMUTABLETYPE),
     .slots = array_slots,
 };
 



More information about the Python-checkins mailing list