From 6fab8e37f97da62555b94a7f1f0c6b0e50cda7bd Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Sat, 25 Jun 2022 16:15:43 +0000 Subject: [PATCH 1/5] make _struct module types immutable --- Modules/_struct.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Modules/_struct.c b/Modules/_struct.c index 20307ad15be814..9d66568a282662 100644 --- a/Modules/_struct.c +++ b/Modules/_struct.c @@ -1741,7 +1741,8 @@ static PyType_Spec unpackiter_type_spec = { "_struct.unpack_iterator", sizeof(unpackiterobject), 0, - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, + (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | + Py_TPFLAGS_IMMUTABLETYPE), unpackiter_type_slots }; @@ -2110,7 +2111,8 @@ static PyType_Spec PyStructType_spec = { "_struct.Struct", sizeof(PyStructObject), 0, - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, + (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | + Py_TPFLAGS_BASETYPE | Py_TPFLAGS_IMMUTABLETYPE), PyStructType_slots }; From cbc4cb101412cbaf802df2732d7de3dfb31b8a82 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Sat, 25 Jun 2022 16:25:37 +0000 Subject: [PATCH 2/5] add test --- Lib/test/test_struct.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py index 8f14ed30588810..e0bdc23d4f52fa 100644 --- a/Lib/test/test_struct.py +++ b/Lib/test/test_struct.py @@ -689,6 +689,16 @@ def test__struct_reference_cycle_cleaned_up(self): self.assertIsNone( module_ref(), "_struct module was not garbage collected") + def test__struct_types_immutable(self) -> None: + # See https://github.com/python/cpython/issues/94254 + + Struct = struct.Struct + unpack_iterator = type(struct.iter_unpack("b", b'x')) + for cls in (Struct, unpack_iterator): + with self.assertRaises(TypeError): + cls.x = 1 + + def test_issue35714(self): # Embedded null characters should not be allowed in format strings. for s in '\0', '2\0i', b'\0': From 1b1ac971dc4eea3bb93cd13a58edb0b1000a7a6c Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 25 Jun 2022 16:27:04 +0000 Subject: [PATCH 3/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2022-06-25-16-27-02.gh-issue-94254.beP16v.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-06-25-16-27-02.gh-issue-94254.beP16v.rst diff --git a/Misc/NEWS.d/next/Library/2022-06-25-16-27-02.gh-issue-94254.beP16v.rst b/Misc/NEWS.d/next/Library/2022-06-25-16-27-02.gh-issue-94254.beP16v.rst new file mode 100644 index 00000000000000..81482bcd4f8974 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-06-25-16-27-02.gh-issue-94254.beP16v.rst @@ -0,0 +1 @@ +Fixed types of :mod:`struct` module to be immutable. Patch by Kumar Aditya. From 2f25d3f26fb3310ef671b0b5fe2093fb654d0a96 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Sat, 25 Jun 2022 16:30:34 +0000 Subject: [PATCH 4/5] use subtest --- Lib/test/test_struct.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py index e0bdc23d4f52fa..b65c188897fd67 100644 --- a/Lib/test/test_struct.py +++ b/Lib/test/test_struct.py @@ -695,8 +695,9 @@ def test__struct_types_immutable(self) -> None: Struct = struct.Struct unpack_iterator = type(struct.iter_unpack("b", b'x')) for cls in (Struct, unpack_iterator): - with self.assertRaises(TypeError): - cls.x = 1 + with self.subTest(cls=cls): + with self.assertRaises(TypeError): + cls.x = 1 def test_issue35714(self): From 2ad2cec6cb3985454c0c206dfb2b59020a9fd7ca Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Sun, 26 Jun 2022 09:08:27 +0000 Subject: [PATCH 5/5] mark cpython specific --- Lib/test/test_struct.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py index b65c188897fd67..ab738770546c0b 100644 --- a/Lib/test/test_struct.py +++ b/Lib/test/test_struct.py @@ -689,7 +689,8 @@ def test__struct_reference_cycle_cleaned_up(self): self.assertIsNone( module_ref(), "_struct module was not garbage collected") - def test__struct_types_immutable(self) -> None: + @support.cpython_only + def test__struct_types_immutable(self): # See https://github.com/python/cpython/issues/94254 Struct = struct.Struct