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

Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
Erlend Egeberg Aasland 2021-04-29 08:47:48 +02:00 committed by GitHub
parent 5daf70b22e
commit c6ad03fddf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 1 deletions

View 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')

View file

@ -0,0 +1,2 @@
Make the :class:`array.array` type immutable. Patch by
Erlend E. Aasland.

View 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,
};