Skip to content

Commit 4f72526

Browse files
author
Erlend Egeberg Aasland
authored
bpo-43988: Add test.support.check_disallow_instantiation() (GH-25757)
1 parent 46db39d commit 4f72526

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

Lib/test/support/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"requires_IEEE_754", "requires_zlib",
4141
"anticipate_failure", "load_package_tests", "detect_api_mismatch",
4242
"check__all__", "skip_if_buggy_ucrt_strfptime",
43+
"check_disallow_instantiation",
4344
# sys
4445
"is_jython", "is_android", "check_impl_detail", "unix_shell",
4546
"setswitchinterval",
@@ -1982,3 +1983,13 @@ def skip_if_broken_multiprocessing_synchronize():
19821983
synchronize.Lock(ctx=None)
19831984
except OSError as exc:
19841985
raise unittest.SkipTest(f"broken multiprocessing SemLock: {exc!r}")
1986+
1987+
1988+
def check_disallow_instantiation(testcase, tp, *args, **kwds):
1989+
"""
1990+
Helper for testing types with the Py_TPFLAGS_DISALLOW_INSTANTIATION flag.
1991+
1992+
See bpo-43916.
1993+
"""
1994+
msg = f"cannot create '{tp.__module__}\.{tp.__name__}' instances"
1995+
testcase.assertRaisesRegex(TypeError, msg, tp, *args, **kwds)

Lib/test/test_array.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ def test_bad_constructor(self):
4242

4343
@support.cpython_only
4444
def test_disallow_instantiation(self):
45-
# Ensure that the type disallows instantiation (bpo-43916)
46-
tp = type(iter(array.array('I')))
47-
self.assertRaises(TypeError, tp)
45+
my_array = array.array("I")
46+
tp = type(iter(my_array))
47+
support.check_disallow_instantiation(self, tp, my_array)
4848

4949
@support.cpython_only
5050
def test_immutable(self):

0 commit comments

Comments
 (0)