Skip to content

Commit bc6a2c9

Browse files
committed
gh-155358: Add test.support.check_immutable_type()
Check that sys types are immutable: * type(sys.flags) * type(sys.get_asyncgen_hooks()) * type(sys.getwindowsversion()) * type(sys.hash_info) * type(sys.version_info)
1 parent 2e519dd commit bc6a2c9

5 files changed

Lines changed: 18 additions & 8 deletions

File tree

Lib/test/support/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3465,3 +3465,9 @@ def skip_on_low_desktop_heap_memory_subprocess(returncode):
34653465
if returncode == STATUS_DLL_INIT_FAILED:
34663466
raise unittest.SkipTest('gh-150436: DLL init failed, likely because '
34673467
'of low desktop heap memory')
3468+
3469+
3470+
def check_immutable_type(testcase, type):
3471+
regex = r'cannot set .* attribute of immutable type'
3472+
with testcase.assertRaisesRegex(TypeError, regex):
3473+
setattr(type, 'custom_attr', 123)

Lib/test/test_decimal.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import unittest
3333
import numbers
3434
import locale
35+
from test import support
3536
from test.support import (is_resource_enabled,
3637
requires_IEEE_754, requires_docstrings,
3738
check_disallow_instantiation)
@@ -5806,8 +5807,7 @@ def test_c_immutable_types(self):
58065807
)
58075808
for tp in types:
58085809
with self.subTest(tp=tp):
5809-
with self.assertRaisesRegex(TypeError, "immutable"):
5810-
tp.foo = 1
5810+
support.check_immutable_type(self, tp)
58115811

58125812
def test_c_disallow_instantiation(self):
58135813
ContextManager = type(C.localcontext())

Lib/test/test_itertools.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,8 +1541,7 @@ def test_immutable_types(self):
15411541
)
15421542
for tp in dataset:
15431543
with self.subTest(tp=tp):
1544-
with self.assertRaisesRegex(TypeError, "immutable"):
1545-
tp.foobar = 1
1544+
support.check_immutable_type(self, tp)
15461545

15471546

15481547
class TestExamples(unittest.TestCase):

Lib/test/test_sys.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ def test_getwindowsversion(self):
425425
self.assertEqual(v[2], v.build)
426426
self.assertEqual(v[3], v.platform)
427427
self.assertEqual(v[4], v.service_pack)
428+
support.check_immutable_type(self, type(v))
428429

429430
# This is how platform.py calls it. Make sure tuple
430431
# still has 5 elements
@@ -690,6 +691,7 @@ def test_attributes(self):
690691
self.assertEqual(algo, 0)
691692
self.assertGreaterEqual(sys.hash_info.cutoff, 0)
692693
self.assertLess(sys.hash_info.cutoff, 8)
694+
support.check_immutable_type(self, type(sys.hash_info))
693695

694696
self.assertIsInstance(sys.maxsize, int)
695697
self.assertIsInstance(sys.maxunicode, int)
@@ -893,11 +895,13 @@ def assert_raise_on_new_sys_type(self, sys_attr):
893895
# sys.flags, sys.version_info, and sys.getwindowsversion.
894896
support.check_disallow_instantiation(self, type(sys_attr), sys_attr)
895897

896-
def test_sys_flags_no_instantiation(self):
898+
def test_sys_flags_type(self):
897899
self.assert_raise_on_new_sys_type(sys.flags)
900+
support.check_immutable_type(self, type(sys.flags))
898901

899-
def test_sys_version_info_no_instantiation(self):
902+
def test_sys_version_info_type(self):
900903
self.assert_raise_on_new_sys_type(sys.version_info)
904+
support.check_immutable_type(self, type(sys.version_info))
901905

902906
def test_sys_getwindowsversion_no_instantiation(self):
903907
# Skip if not being run on Windows.
@@ -1954,6 +1958,7 @@ def test_asyncgen_hooks(self):
19541958
cur = sys.get_asyncgen_hooks()
19551959
self.assertIsNone(cur.firstiter)
19561960
self.assertIsNone(cur.finalizer)
1961+
support.check_immutable_type(self, type(cur))
19571962

19581963
# gh-118473
19591964
with self.assertRaises(TypeError):
@@ -1997,6 +2002,7 @@ def write(self, s):
19972002
self.assertEqual(out, b"")
19982003
self.assertEqual(err, b"")
19992004

2005+
20002006
@test.support.support_remote_exec_only
20012007
@test.support.cpython_only
20022008
class TestRemoteExec(unittest.TestCase):

Lib/test/test_xml_etree_c.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,7 @@ def test_immutable_types(self):
194194
)
195195
for tp in dataset:
196196
with self.subTest(tp=tp):
197-
with self.assertRaisesRegex(TypeError, "immutable"):
198-
tp.foo = 1
197+
support.check_immutable_type(self, tp)
199198

200199
@support.cpython_only
201200
def test_disallow_instantiation(self):

0 commit comments

Comments
 (0)