GeezerVison is showing github.com — bpo-43988: Add test.support.check_disallow_instantiation() (GH-25757) · python/cpython@4f72526
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: Lib/test/support/__init__.py +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 40 | 40 | "requires_IEEE_754", "requires_zlib", | 41 | 41 | "anticipate_failure", "load_package_tests", "detect_api_mismatch", | 42 | 42 | "check__all__", "skip_if_buggy_ucrt_strfptime", | | 43 | + "check_disallow_instantiation", | 43 | 44 | # sys | 44 | 45 | "is_jython", "is_android", "check_impl_detail", "unix_shell", | 45 | 46 | "setswitchinterval", | @@ -1982,3 +1983,13 @@ def skip_if_broken_multiprocessing_synchronize(): | 1982 | 1983 | synchronize.Lock(ctx=None) | 1983 | 1984 | except OSError as exc: | 1984 | 1985 | 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) |
Collapse file Copy file name to clipboardExpand all lines: Lib/test/test_array.py +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -42,9 +42,9 @@ def test_bad_constructor(self): | 42 | 42 |
| 43 | 43 | @support.cpython_only | 44 | 44 | 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) | 48 | 48 |
| 49 | 49 | @support.cpython_only | 50 | 50 | def test_immutable(self): | |
|