From 78232718be5b6469b8469fdfb31da05c5cd14f85 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 26 Apr 2021 21:19:15 +0200 Subject: [PATCH 1/5] bpo-43908: Apply Py_TPFLAGS_IMMUTABLETYPE to re types --- Modules/_sre.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Modules/_sre.c b/Modules/_sre.c index d4bfff6e849e37..59f7551a227cd3 100644 --- a/Modules/_sre.c +++ b/Modules/_sre.c @@ -2690,7 +2690,7 @@ static PyType_Spec pattern_spec = { .name = "re.Pattern", .basicsize = sizeof(PatternObject), .itemsize = sizeof(SRE_CODE), - .flags = Py_TPFLAGS_DEFAULT, + .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE, .slots = pattern_slots, }; @@ -2755,7 +2755,7 @@ static PyType_Spec match_spec = { .name = "re.Match", .basicsize = sizeof(MatchObject), .itemsize = sizeof(Py_ssize_t), - .flags = Py_TPFLAGS_DEFAULT, + .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE, .slots = match_slots, }; @@ -2781,7 +2781,7 @@ static PyType_Slot scanner_slots[] = { static PyType_Spec scanner_spec = { .name = "_" SRE_MODULE ".SRE_Scanner", .basicsize = sizeof(ScannerObject), - .flags = Py_TPFLAGS_DEFAULT, + .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE, .slots = scanner_slots, }; From 4baf809e3ecffb2505cf3a262ccb5b285433f876 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 26 Apr 2021 21:20:52 +0200 Subject: [PATCH 2/5] Add NEWS --- .../Core and Builtins/2021-04-26-21-20-41.bpo-43908.2L51nO.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2021-04-26-21-20-41.bpo-43908.2L51nO.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-04-26-21-20-41.bpo-43908.2L51nO.rst b/Misc/NEWS.d/next/Core and Builtins/2021-04-26-21-20-41.bpo-43908.2L51nO.rst new file mode 100644 index 00000000000000..ce0c535c174e2b --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2021-04-26-21-20-41.bpo-43908.2L51nO.rst @@ -0,0 +1,2 @@ +Apply :const:`Py_TPFLAGS_IMMUTABLETYPE` flag to :mod:`re` types. Patch by +Erlend E. Aasland. From b9afe4ed3ed11ac9e0e5ad7cf96867c93278acd1 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 28 Apr 2021 19:52:33 +0200 Subject: [PATCH 3/5] Add tests --- Lib/test/test_re.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py index bd689582523c32..e56ff2f896e206 100644 --- a/Lib/test/test_re.py +++ b/Lib/test/test_re.py @@ -2190,6 +2190,17 @@ class ImplementationTest(unittest.TestCase): Test implementation details of the re module. """ + @cpython_only + def test_immutable(self): + with self.assertRaises(TypeError): + re.Match.foo = 1 + with self.assertRaises(TypeError): + re.Pattern.foo = 1 + with self.assertRaises(TypeError): + pat = re.compile("") + tp = type(pat.scanner("")) + tp.foo = 1 + def test_overlap_table(self): f = sre_compile._generate_overlap_table self.assertEqual(f(""), []) From cba885ffff5db681df1b09d8d6e8fa2b71844b07 Mon Sep 17 00:00:00 2001 From: Erlend Egeberg Aasland Date: Wed, 28 Apr 2021 22:22:15 +0200 Subject: [PATCH 4/5] Improve NEWS entry wording Co-authored-by: Victor Stinner --- .../Core and Builtins/2021-04-26-21-20-41.bpo-43908.2L51nO.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-04-26-21-20-41.bpo-43908.2L51nO.rst b/Misc/NEWS.d/next/Core and Builtins/2021-04-26-21-20-41.bpo-43908.2L51nO.rst index ce0c535c174e2b..1709351726f965 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2021-04-26-21-20-41.bpo-43908.2L51nO.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2021-04-26-21-20-41.bpo-43908.2L51nO.rst @@ -1,2 +1,2 @@ -Apply :const:`Py_TPFLAGS_IMMUTABLETYPE` flag to :mod:`re` types. Patch by +Make :mod:`re` types immutable. Patch by Erlend E. Aasland. From 1cc81ca7dad4e4fbd9b9812eee15fb1f18fe32f8 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 28 Apr 2021 22:28:08 +0200 Subject: [PATCH 5/5] Address review: add comment explaining the purpose of the test Co-authored-by: Victor Stinner --- Lib/test/test_re.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py index e56ff2f896e206..96d0cdb17a7ee3 100644 --- a/Lib/test/test_re.py +++ b/Lib/test/test_re.py @@ -2192,6 +2192,7 @@ class ImplementationTest(unittest.TestCase): @cpython_only def test_immutable(self): + # bpo-43908: check that re types are immutable with self.assertRaises(TypeError): re.Match.foo = 1 with self.assertRaises(TypeError):