[Python-checkins] bpo-43988: Add test.support.check_disallow_instantiation() (GH-25757)

vstinner webhook-mailer at python.org
Wed May 26 10:15:35 EDT 2021


https://github.com/python/cpython/commit/4f725261c6cf23d259e8fdc205e12b76ef4d2d31
commit: 4f725261c6cf23d259e8fdc205e12b76ef4d2d31
branch: main
author: Erlend Egeberg Aasland <erlend.aasland at innova.no>
committer: vstinner <vstinner at python.org>
date: 2021-05-26T16:15:27+02:00
summary:

bpo-43988: Add test.support.check_disallow_instantiation() (GH-25757)

files:
M Lib/test/support/__init__.py
M Lib/test/test_array.py

diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index 80f3a04fcc6180..8c6e5547d5e717 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -40,6 +40,7 @@
     "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)
diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py
index b18467fb889d8b..e7cddf2314732d 100644
--- a/Lib/test/test_array.py
+++ b/Lib/test/test_array.py
@@ -42,9 +42,9 @@ def test_bad_constructor(self):
 
     @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):



More information about the Python-checkins mailing list