From a86129da64e94a6ef274a3a5ebfee4447fdf5f65 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 26 Apr 2021 20:56:43 +0200 Subject: [PATCH 1/5] bpo-43908: Apply Py_TPFLAGS_IMMUTABLETYPE to array.array --- Modules/arraymodule.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index f5326789521d30..367621fd03b882 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, }; From 0b0a496f26bb1bb69a7c77c10376ddb6983c73d7 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 26 Apr 2021 20:59:22 +0200 Subject: [PATCH 2/5] Add NEWS --- .../Core and Builtins/2021-04-26-20-59-17.bpo-43908.-COW4-.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2021-04-26-20-59-17.bpo-43908.-COW4-.rst 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 00000000000000..de31b1d3e38981 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2021-04-26-20-59-17.bpo-43908.-COW4-.rst @@ -0,0 +1,2 @@ +Apply :const:`Py_TPFLAGS_IMMUTABLETYPE` flag to :class:`array.array`. Patch by +Erlend E. Aasland. From 29c6746ae99cf0456434e2fb518789068c02abb6 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 28 Apr 2021 19:50:38 +0200 Subject: [PATCH 3/5] Add test --- Lib/test/test_array.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py index bdcd1254b304ff..172a4f350eec25 100644 --- a/Lib/test/test_array.py +++ b/Lib/test/test_array.py @@ -40,6 +40,11 @@ def test_bad_constructor(self): self.assertRaises(TypeError, array.array, 'xx') self.assertRaises(ValueError, array.array, 'x') + @support.cpython_only + def test_immutable(self): + with self.assertRaises(TypeError): + array.array.foo = 1 + def test_empty(self): # Exercise code for handling zero-length arrays a = array.array('B') From 834801808b0f6762fb00c3cff64e167ef84baa7d Mon Sep 17 00:00:00 2001 From: Erlend Egeberg Aasland Date: Wed, 28 Apr 2021 22:22:00 +0200 Subject: [PATCH 4/5] Improve NEWS entry wording Co-authored-by: Victor Stinner --- .../Core and Builtins/2021-04-26-20-59-17.bpo-43908.-COW4-.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index de31b1d3e38981..07303b99d1f95d 100644 --- 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 @@ -1,2 +1,2 @@ -Apply :const:`Py_TPFLAGS_IMMUTABLETYPE` flag to :class:`array.array`. Patch by +Make the :class:`array.array` type immutable. Patch by Erlend E. Aasland. From 551f93e604277badb9706c012b118505fd268a8b Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 28 Apr 2021 22:25:04 +0200 Subject: [PATCH 5/5] Address review: explain the purpose of the test Co-authored-by: Victor Stinner --- Lib/test/test_array.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py index 172a4f350eec25..11184add6d399b 100644 --- a/Lib/test/test_array.py +++ b/Lib/test/test_array.py @@ -42,6 +42,7 @@ def test_bad_constructor(self): @support.cpython_only def test_immutable(self): + # bpo-43908: check that array.array is immutable with self.assertRaises(TypeError): array.array.foo = 1