Skip to content

bpo-43908: Make re types immutable #25697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Lib/test/test_re.py
Original file line number Diff line number Diff line change
Expand Up @@ -2190,6 +2190,18 @@ class ImplementationTest(unittest.TestCase):
Test implementation details of the re module.
"""

@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):
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(""), [])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Make :mod:`re` types immutable. Patch by
Erlend E. Aasland.
6 changes: 3 additions & 3 deletions Modules/_sre.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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,
};

Expand All @@ -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,
};

Expand Down