]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-43908: Make array.array type immutable (GH-25696)
authorErlend Egeberg Aasland <erlend.aasland@innova.no>
Thu, 29 Apr 2021 06:47:48 +0000 (08:47 +0200)
committerGitHub <noreply@github.com>
Thu, 29 Apr 2021 06:47:48 +0000 (08:47 +0200)
Co-authored-by: Victor Stinner <vstinner@python.org>
Lib/test/test_array.py
Misc/NEWS.d/next/Core and Builtins/2021-04-26-20-59-17.bpo-43908.-COW4-.rst [new file with mode: 0644]
Modules/arraymodule.c

index bdcd1254b304ff87be60110ab08d8a8e69c0707d..11184add6d399b68da8d256a1f4b3be992fd9858 100644 (file)
@@ -40,6 +40,12 @@ class MiscTest(unittest.TestCase):
         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 (file)
index 0000000..07303b9
--- /dev/null
@@ -0,0 +1,2 @@
+Make the :class:`array.array` type immutable. Patch by
+Erlend E. Aasland.
index f5326789521d30233a3172e5f5a50194ed8df9a9..367621fd03b8824ee95ccffa950d88f7b332a6b3 100644 (file)
@@ -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,
 };