]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-43988: Add test.support.check_disallow_instantiation() (GH-25757)
authorErlend Egeberg Aasland <erlend.aasland@innova.no>
Wed, 26 May 2021 14:15:27 +0000 (16:15 +0200)
committerGitHub <noreply@github.com>
Wed, 26 May 2021 14:15:27 +0000 (16:15 +0200)
Lib/test/support/__init__.py
Lib/test/test_array.py

index 80f3a04fcc61803d77af19a1c9d19fcb1ee6ccc4..8c6e5547d5e717c9b2c8ef4beec216a2eb66ddd4 100644 (file)
@@ -40,6 +40,7 @@ __all__ = [
     "requires_IEEE_754", "requires_zlib",
     "anticipate_failure", "load_package_tests", "detect_api_mismatch",
     "check__all__", "skip_if_buggy_ucrt_strfptime",
+    "check_disallow_instantiation",
     # sys
     "is_jython", "is_android", "check_impl_detail", "unix_shell",
     "setswitchinterval",
@@ -1982,3 +1983,13 @@ def skip_if_broken_multiprocessing_synchronize():
             synchronize.Lock(ctx=None)
         except OSError as exc:
             raise unittest.SkipTest(f"broken multiprocessing SemLock: {exc!r}")
+
+
+def check_disallow_instantiation(testcase, tp, *args, **kwds):
+    """
+    Helper for testing types with the Py_TPFLAGS_DISALLOW_INSTANTIATION flag.
+
+    See bpo-43916.
+    """
+    msg = f"cannot create '{tp.__module__}\.{tp.__name__}' instances"
+    testcase.assertRaisesRegex(TypeError, msg, tp, *args, **kwds)
index b18467fb889d8bcd81db48e15851679cc8228f0e..e7cddf2314732db36230aa0b83eb4e03a24f0f33 100644 (file)
@@ -42,9 +42,9 @@ class MiscTest(unittest.TestCase):
 
     @support.cpython_only
     def test_disallow_instantiation(self):
-        # Ensure that the type disallows instantiation (bpo-43916)
-        tp = type(iter(array.array('I')))
-        self.assertRaises(TypeError, tp)
+        my_array = array.array("I")
+        tp = type(iter(my_array))
+        support.check_disallow_instantiation(self, tp, my_array)
 
     @support.cpython_only
     def test_immutable(self):