diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index 897c210f4767e1d..c1460b806806f81 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -3465,3 +3465,9 @@ def skip_on_low_desktop_heap_memory_subprocess(returncode): if returncode == STATUS_DLL_INIT_FAILED: raise unittest.SkipTest('gh-150436: DLL init failed, likely because ' 'of low desktop heap memory') + + +def check_immutable_type(testcase, type): + regex = r'cannot set .* attribute of immutable type' + with testcase.assertRaisesRegex(TypeError, regex): + setattr(type, 'custom_attr', 123) diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py index b8c09c7f43e3e3b..a0ba5a8351aebdf 100644 --- a/Lib/test/test_decimal.py +++ b/Lib/test/test_decimal.py @@ -32,6 +32,7 @@ import unittest import numbers import locale +from test import support from test.support import (is_resource_enabled, requires_IEEE_754, requires_docstrings, check_disallow_instantiation) @@ -5806,8 +5807,7 @@ def test_c_immutable_types(self): ) for tp in types: with self.subTest(tp=tp): - with self.assertRaisesRegex(TypeError, "immutable"): - tp.foo = 1 + support.check_immutable_type(self, tp) def test_c_disallow_instantiation(self): ContextManager = type(C.localcontext()) diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py index cf579d4da4e0dfb..d47f9acf019dca6 100644 --- a/Lib/test/test_itertools.py +++ b/Lib/test/test_itertools.py @@ -1541,8 +1541,7 @@ def test_immutable_types(self): ) for tp in dataset: with self.subTest(tp=tp): - with self.assertRaisesRegex(TypeError, "immutable"): - tp.foobar = 1 + support.check_immutable_type(self, tp) class TestExamples(unittest.TestCase): diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py index dab03ef06a8b8e5..f2adce532595e70 100644 --- a/Lib/test/test_sys.py +++ b/Lib/test/test_sys.py @@ -425,6 +425,7 @@ def test_getwindowsversion(self): self.assertEqual(v[2], v.build) self.assertEqual(v[3], v.platform) self.assertEqual(v[4], v.service_pack) + support.check_immutable_type(self, type(v)) # This is how platform.py calls it. Make sure tuple # still has 5 elements @@ -690,6 +691,7 @@ def test_attributes(self): self.assertEqual(algo, 0) self.assertGreaterEqual(sys.hash_info.cutoff, 0) self.assertLess(sys.hash_info.cutoff, 8) + support.check_immutable_type(self, type(sys.hash_info)) self.assertIsInstance(sys.maxsize, int) self.assertIsInstance(sys.maxunicode, int) @@ -893,11 +895,13 @@ def assert_raise_on_new_sys_type(self, sys_attr): # sys.flags, sys.version_info, and sys.getwindowsversion. support.check_disallow_instantiation(self, type(sys_attr), sys_attr) - def test_sys_flags_no_instantiation(self): + def test_sys_flags_type(self): self.assert_raise_on_new_sys_type(sys.flags) + support.check_immutable_type(self, type(sys.flags)) - def test_sys_version_info_no_instantiation(self): + def test_sys_version_info_type(self): self.assert_raise_on_new_sys_type(sys.version_info) + support.check_immutable_type(self, type(sys.version_info)) def test_sys_getwindowsversion_no_instantiation(self): # Skip if not being run on Windows. @@ -1954,6 +1958,7 @@ def test_asyncgen_hooks(self): cur = sys.get_asyncgen_hooks() self.assertIsNone(cur.firstiter) self.assertIsNone(cur.finalizer) + support.check_immutable_type(self, type(cur)) # gh-118473 with self.assertRaises(TypeError): @@ -1997,6 +2002,7 @@ def write(self, s): self.assertEqual(out, b"") self.assertEqual(err, b"") + @test.support.support_remote_exec_only @test.support.cpython_only class TestRemoteExec(unittest.TestCase): diff --git a/Lib/test/test_xml_etree_c.py b/Lib/test/test_xml_etree_c.py index 270b9d6da8e7b9e..2a18396afb60881 100644 --- a/Lib/test/test_xml_etree_c.py +++ b/Lib/test/test_xml_etree_c.py @@ -194,8 +194,7 @@ def test_immutable_types(self): ) for tp in dataset: with self.subTest(tp=tp): - with self.assertRaisesRegex(TypeError, "immutable"): - tp.foo = 1 + support.check_immutable_type(self, tp) @support.cpython_only def test_disallow_instantiation(self):