Skip to content
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
6 changes: 6 additions & 0 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 2 additions & 2 deletions Lib/test/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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())
Expand Down
3 changes: 1 addition & 2 deletions Lib/test/test_itertools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
10 changes: 8 additions & 2 deletions Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
3 changes: 1 addition & 2 deletions Lib/test/test_xml_etree_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Loading