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

miss-islington webhook-mailer at python.org
Wed Jun 23 19:46:34 EDT 2021


https://github.com/python/cpython/commit/0a3452e7cf00c51ab1af0f674b670520b09f0e39
commit: 0a3452e7cf00c51ab1af0f674b670520b09f0e39
branch: 3.10
author: Erlend Egeberg Aasland <erlend.aasland at innova.no>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2021-06-23T16:46:25-07:00
summary:

[3.10] bpo-43988: Add test.support.check_disallow_instantiation() (GH-25757) (GH-26885)



(cherry picked from commit 4f725261c6cf23d259e8fdc205e12b76ef4d2d31, fbff5387c3e1f3904420fa5a27738c6c5881305b, and 8cec740820fc875117bfa7b6bdb10202ebeb8fd5)

Co-authored-by: Erlend Egeberg Aasland <erlend.aasland at innova.no>

Automerge-Triggered-By: GH:vstinner

files:
M Doc/library/test.rst
M Lib/sqlite3/test/dbapi.py
M Lib/test/support/__init__.py
M Lib/test/test_array.py
M Lib/test/test_curses.py
M Lib/test/test_dbm_gnu.py
M Lib/test/test_embed.py
M Lib/test/test_functools.py
M Lib/test/test_hashlib.py
M Lib/test/test_hmac.py
M Lib/test/test_re.py
M Lib/test/test_select.py
M Lib/test/test_ssl.py
M Lib/test/test_threading.py
M Lib/test/test_unicodedata.py
M Lib/test/test_zlib.py

diff --git a/Doc/library/test.rst b/Doc/library/test.rst
index e4f779bd83eb87..7ee96d375a1d04 100644
--- a/Doc/library/test.rst
+++ b/Doc/library/test.rst
@@ -928,8 +928,16 @@ The :mod:`test.support` module defines the following functions:
    .. versionadded:: 3.10
 
 
+.. function:: check_disallow_instantiation(test_case, tp, *args, **kwds)
+
+   Assert that type *tp* cannot be instantiated using *args* and *kwds*.
+
+   .. versionadded:: 3.10
+
+
 The :mod:`test.support` module defines the following classes:
 
+
 .. class:: SuppressCrashReport()
 
    A context manager used to try to prevent crash dialog popups on tests that
diff --git a/Lib/sqlite3/test/dbapi.py b/Lib/sqlite3/test/dbapi.py
index 0716e656a7f26f..a958d4df8bb0c8 100644
--- a/Lib/sqlite3/test/dbapi.py
+++ b/Lib/sqlite3/test/dbapi.py
@@ -25,6 +25,7 @@
 import sqlite3 as sqlite
 import sys
 
+from test.support import check_disallow_instantiation
 from test.support.os_helper import TESTFN, unlink
 
 
@@ -94,8 +95,7 @@ def test_shared_cache_deprecated(self):
 
     def test_disallow_instantiation(self):
         cx = sqlite.connect(":memory:")
-        tp = type(cx("select 1"))
-        self.assertRaises(TypeError, tp)
+        check_disallow_instantiation(self, type(cx("select 1")))
 
 
 class ConnectionTests(unittest.TestCase):
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index b9040a9ac507a4..9e5064dbea4831 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",
@@ -1992,3 +1993,19 @@ def infinite_recursion(max_depth=75):
         yield
     finally:
         sys.setrecursionlimit(original_depth)
+
+
+def check_disallow_instantiation(testcase, tp, *args, **kwds):
+    """
+    Check that given type cannot be instantiated using *args and **kwds.
+
+    See bpo-43916: Add Py_TPFLAGS_DISALLOW_INSTANTIATION type flag.
+    """
+    mod = tp.__module__
+    name = tp.__name__
+    if mod != 'builtins':
+        qualname = f"{mod}.{name}"
+    else:
+        qualname = f"{name}"
+    msg = f"cannot create '{re.escape(qualname)}' instances"
+    testcase.assertRaisesRegex(TypeError, msg, tp, *args, **kwds)
diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py
index b18467fb889d8b..18f78d52467e2b 100644
--- a/Lib/test/test_array.py
+++ b/Lib/test/test_array.py
@@ -42,9 +42,10 @@ 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")
+        support.check_disallow_instantiation(
+            self, type(iter(my_array)), my_array
+        )
 
     @support.cpython_only
     def test_immutable(self):
diff --git a/Lib/test/test_curses.py b/Lib/test/test_curses.py
index 8bf48a6454d691..d3c152c42cf62f 100644
--- a/Lib/test/test_curses.py
+++ b/Lib/test/test_curses.py
@@ -6,7 +6,8 @@
 import tempfile
 import unittest
 
-from test.support import requires, verbose, SaveSignals, cpython_only
+from test.support import (requires, verbose, SaveSignals, cpython_only,
+                          check_disallow_instantiation)
 from test.support.import_helper import import_module
 
 # Optionally test curses module.  This currently requires that the
@@ -1052,7 +1053,7 @@ def test_disallow_instantiation(self):
         # Ensure that the type disallows instantiation (bpo-43916)
         w = curses.newwin(10, 10)
         panel = curses.panel.new_panel(w)
-        self.assertRaises(TypeError, type(panel))
+        check_disallow_instantiation(self, type(panel))
 
     @requires_curses_func('is_term_resized')
     def test_is_term_resized(self):
diff --git a/Lib/test/test_dbm_gnu.py b/Lib/test/test_dbm_gnu.py
index b3e55728c8e70d..f39b0029373485 100644
--- a/Lib/test/test_dbm_gnu.py
+++ b/Lib/test/test_dbm_gnu.py
@@ -31,8 +31,7 @@ def tearDown(self):
     def test_disallow_instantiation(self):
         # Ensure that the type disallows instantiation (bpo-43916)
         self.g = gdbm.open(filename, 'c')
-        tp = type(self.g)
-        self.assertRaises(TypeError, tp)
+        support.check_disallow_instantiation(self, type(self.g))
 
     def test_key_methods(self):
         self.g = gdbm.open(filename, 'c')
diff --git a/Lib/test/test_embed.py b/Lib/test/test_embed.py
index f2cf4a63add287..479d712da65d94 100644
--- a/Lib/test/test_embed.py
+++ b/Lib/test/test_embed.py
@@ -1541,9 +1541,7 @@ def test_methods(self):
     def test_disallow_instantiation(self):
         fd = self.get_stdout_fd()
         printer = self.create_printer(fd)
-        PyStdPrinter_Type = type(printer)
-        with self.assertRaises(TypeError):
-            PyStdPrinter_Type(fd)
+        support.check_disallow_instantiation(self, type(printer))
 
 
 if __name__ == "__main__":
diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py
index 3320ab7ec6649d..78a8a5fcc0feaa 100644
--- a/Lib/test/test_functools.py
+++ b/Lib/test/test_functools.py
@@ -951,8 +951,9 @@ class TestCmpToKeyC(TestCmpToKey, unittest.TestCase):
     @support.cpython_only
     def test_disallow_instantiation(self):
         # Ensure that the type disallows instantiation (bpo-43916)
-        tp = type(c_functools.cmp_to_key(None))
-        self.assertRaises(TypeError, tp)
+        support.check_disallow_instantiation(
+            self, type(c_functools.cmp_to_key(None))
+        )
 
 
 class TestCmpToKeyPy(TestCmpToKey, unittest.TestCase):
diff --git a/Lib/test/test_hashlib.py b/Lib/test/test_hashlib.py
index 76754b6b8e13da..1623bf350e2875 100644
--- a/Lib/test/test_hashlib.py
+++ b/Lib/test/test_hashlib.py
@@ -915,20 +915,13 @@ def test_disallow_instantiation(self):
                 except ValueError:
                     continue
                 with self.subTest(constructor=constructor):
-                    hash_type = type(h)
-                    self.assertRaises(TypeError, hash_type)
+                    support.check_disallow_instantiation(self, type(h))
 
     @unittest.skipUnless(HASH is not None, 'need _hashlib')
-    def test_hash_disallow_instanciation(self):
+    def test_hash_disallow_instantiation(self):
         # internal types like _hashlib.HASH are not constructable
-        with self.assertRaisesRegex(
-            TypeError, "cannot create '_hashlib.HASH' instance"
-        ):
-            HASH()
-        with self.assertRaisesRegex(
-            TypeError, "cannot create '_hashlib.HASHXOF' instance"
-        ):
-            HASHXOF()
+        support.check_disallow_instantiation(self, HASH)
+        support.check_disallow_instantiation(self, HASHXOF)
 
     def test_readonly_types(self):
         for algorithm, constructors in self.constructors_to_test.items():
diff --git a/Lib/test/test_hmac.py b/Lib/test/test_hmac.py
index 964acd0361e342..7cf99735ca39f0 100644
--- a/Lib/test/test_hmac.py
+++ b/Lib/test/test_hmac.py
@@ -6,7 +6,7 @@
 import unittest.mock
 import warnings
 
-from test.support import hashlib_helper
+from test.support import hashlib_helper, check_disallow_instantiation
 
 from _operator import _compare_digest as operator_compare_digest
 
@@ -439,11 +439,7 @@ def test_withmodule(self):
     @unittest.skipUnless(C_HMAC is not None, 'need _hashlib')
     def test_internal_types(self):
         # internal types like _hashlib.C_HMAC are not constructable
-        with self.assertRaisesRegex(
-            TypeError, "cannot create '_hashlib.HMAC' instance"
-        ):
-            C_HMAC()
-
+        check_disallow_instantiation(self, C_HMAC)
         with self.assertRaisesRegex(TypeError, "immutable type"):
             C_HMAC.value = None
 
diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py
index e1b2c794291848..4231e962b23e8a 100644
--- a/Lib/test/test_re.py
+++ b/Lib/test/test_re.py
@@ -1,5 +1,6 @@
 from test.support import (gc_collect, bigmemtest, _2G,
-                          cpython_only, captured_stdout)
+                          cpython_only, captured_stdout,
+                          check_disallow_instantiation)
 import locale
 import re
 import sre_compile
@@ -2218,11 +2219,10 @@ def test_signedness(self):
     @cpython_only
     def test_disallow_instantiation(self):
         # Ensure that the type disallows instantiation (bpo-43916)
-        self.assertRaises(TypeError, re.Match)
-        self.assertRaises(TypeError, re.Pattern)
+        check_disallow_instantiation(self, re.Match)
+        check_disallow_instantiation(self, re.Pattern)
         pat = re.compile("")
-        tp = type(pat.scanner(""))
-        self.assertRaises(TypeError, tp)
+        check_disallow_instantiation(self, type(pat.scanner("")))
 
 
 class ExternalTests(unittest.TestCase):
diff --git a/Lib/test/test_select.py b/Lib/test/test_select.py
index 957a633f3230e7..cf32cf2f6a6f8b 100644
--- a/Lib/test/test_select.py
+++ b/Lib/test/test_select.py
@@ -88,12 +88,10 @@ def fileno(self):
         self.assertEqual(select.select([], a, []), ([], a[:5], []))
 
     def test_disallow_instantiation(self):
-        tp = type(select.poll())
-        self.assertRaises(TypeError, tp)
+        support.check_disallow_instantiation(self, type(select.poll()))
 
         if hasattr(select, 'devpoll'):
-            tp = type(select.devpoll())
-            self.assertRaises(TypeError, tp)
+            support.check_disallow_instantiation(self, type(select.devpoll()))
 
 def tearDownModule():
     support.reap_children()
diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py
index 06e501e3967849..1a2f0fc6b69fa3 100644
--- a/Lib/test/test_ssl.py
+++ b/Lib/test/test_ssl.py
@@ -358,11 +358,7 @@ def test_ssl_types(self):
             with self.subTest(ssl_type=ssl_type):
                 with self.assertRaisesRegex(TypeError, "immutable type"):
                     ssl_type.value = None
-        with self.assertRaisesRegex(
-            TypeError,
-            "cannot create '_ssl.Certificate' instances"
-        ):
-            _ssl.Certificate()
+        support.check_disallow_instantiation(self, _ssl.Certificate)
 
     def test_private_init(self):
         with self.assertRaisesRegex(TypeError, "public constructor"):
diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py
index b563797cbd0d39..f648a8b2bc52f5 100644
--- a/Lib/test/test_threading.py
+++ b/Lib/test/test_threading.py
@@ -124,8 +124,7 @@ def func(): pass
     def test_disallow_instantiation(self):
         # Ensure that the type disallows instantiation (bpo-43916)
         lock = threading.Lock()
-        tp = type(lock)
-        self.assertRaises(TypeError, tp)
+        test.support.check_disallow_instantiation(self, type(lock))
 
     # Create a bunch of threads, let each do some work, wait until all are
     # done.
diff --git a/Lib/test/test_unicodedata.py b/Lib/test/test_unicodedata.py
index c6bbe3f5ff2b33..213b3cf2529986 100644
--- a/Lib/test/test_unicodedata.py
+++ b/Lib/test/test_unicodedata.py
@@ -12,7 +12,7 @@
 import unicodedata
 import unittest
 from test.support import (open_urlresource, requires_resource, script_helper,
-                          cpython_only)
+                          cpython_only, check_disallow_instantiation)
 
 
 class UnicodeMethodsTest(unittest.TestCase):
@@ -229,7 +229,7 @@ class UnicodeMiscTest(UnicodeDatabaseTest):
     @cpython_only
     def test_disallow_instantiation(self):
         # Ensure that the type disallows instantiation (bpo-43916)
-        self.assertRaises(TypeError, unicodedata.UCD)
+        check_disallow_instantiation(self, unicodedata.UCD)
 
     def test_failed_import_during_compiling(self):
         # Issue 4367
diff --git a/Lib/test/test_zlib.py b/Lib/test/test_zlib.py
index 694ef6e48757b4..cb0610837baafc 100644
--- a/Lib/test/test_zlib.py
+++ b/Lib/test/test_zlib.py
@@ -132,10 +132,8 @@ def test_overflow(self):
     @support.cpython_only
     def test_disallow_instantiation(self):
         # Ensure that the type disallows instantiation (bpo-43916)
-        comp_type = type(zlib.compressobj())
-        decomp_type = type(zlib.decompressobj())
-        self.assertRaises(TypeError, comp_type)
-        self.assertRaises(TypeError, decomp_type)
+        support.check_disallow_instantiation(self, type(zlib.compressobj()))
+        support.check_disallow_instantiation(self, type(zlib.decompressobj()))
 
 
 class BaseCompressTestCase(object):



More information about the Python-checkins mailing list