[3.10] bpo-43988: Add test.support.check_disallow_instantiation() (GH… · python/cpython@0a3452e (original) (raw)

16 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -928,8 +928,16 @@ The :mod:`test.support` module defines the following functions:
928 928 .. versionadded:: 3.10
929 929
930 930
931 +.. function:: check_disallow_instantiation(test_case, tp, *args, **kwds)
932 +
933 + Assert that type *tp* cannot be instantiated using *args* and *kwds*.
934 +
935 + .. versionadded:: 3.10
936 +
937 +
931 938 The :mod:`test.support` module defines the following classes:
932 939
940 +
933 941 .. class:: SuppressCrashReport()
934 942
935 943 A context manager used to try to prevent crash dialog popups on tests that
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@
25 25 import sqlite3 as sqlite
26 26 import sys
27 27
28 +from test.support import check_disallow_instantiation
28 29 from test.support.os_helper import TESTFN, unlink
29 30
30 31
@@ -94,8 +95,7 @@ def test_shared_cache_deprecated(self):
94 95
95 96 def test_disallow_instantiation(self):
96 97 cx = sqlite.connect(":memory:")
97 -tp = type(cx("select 1"))
98 -self.assertRaises(TypeError, tp)
98 +check_disallow_instantiation(self, type(cx("select 1")))
99 99
100 100
101 101 class ConnectionTests(unittest.TestCase):
Original file line number Diff line number Diff line change
@@ -40,6 +40,7 @@
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",
@@ -1992,3 +1993,19 @@ def infinite_recursion(max_depth=75):
1992 1993 yield
1993 1994 finally:
1994 1995 sys.setrecursionlimit(original_depth)
1996 +
1997 +
1998 +def check_disallow_instantiation(testcase, tp, *args, **kwds):
1999 +"""
2000 + Check that given type cannot be instantiated using *args and **kwds.
2001 +
2002 + See bpo-43916: Add Py_TPFLAGS_DISALLOW_INSTANTIATION type flag.
2003 + """
2004 +mod = tp.__module__
2005 +name = tp.__name__
2006 +if mod != 'builtins':
2007 +qualname = f"{mod}.{name}"
2008 +else:
2009 +qualname = f"{name}"
2010 +msg = f"cannot create '{re.escape(qualname)}' instances"
2011 +testcase.assertRaisesRegex(TypeError, msg, tp, *args, **kwds)
Original file line number Diff line number Diff line change
@@ -42,9 +42,10 @@ 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 +support.check_disallow_instantiation(
47 +self, type(iter(my_array)), my_array
48 + )
48 49
49 50 @support.cpython_only
50 51 def test_immutable(self):
Original file line number Diff line number Diff line change
@@ -6,7 +6,8 @@
6 6 import tempfile
7 7 import unittest
8 8
9 -from test.support import requires, verbose, SaveSignals, cpython_only
9 +from test.support import (requires, verbose, SaveSignals, cpython_only,
10 +check_disallow_instantiation)
10 11 from test.support.import_helper import import_module
11 12
12 13 # Optionally test curses module. This currently requires that the
@@ -1052,7 +1053,7 @@ def test_disallow_instantiation(self):
1052 1053 # Ensure that the type disallows instantiation (bpo-43916)
1053 1054 w = curses.newwin(10, 10)
1054 1055 panel = curses.panel.new_panel(w)
1055 -self.assertRaises(TypeError, type(panel))
1056 +check_disallow_instantiation(self, type(panel))
1056 1057
1057 1058 @requires_curses_func('is_term_resized')
1058 1059 def test_is_term_resized(self):
Original file line number Diff line number Diff line change
@@ -31,8 +31,7 @@ def tearDown(self):
31 31 def test_disallow_instantiation(self):
32 32 # Ensure that the type disallows instantiation (bpo-43916)
33 33 self.g = gdbm.open(filename, 'c')
34 -tp = type(self.g)
35 -self.assertRaises(TypeError, tp)
34 +support.check_disallow_instantiation(self, type(self.g))
36 35
37 36 def test_key_methods(self):
38 37 self.g = gdbm.open(filename, 'c')
Original file line number Diff line number Diff line change
@@ -1541,9 +1541,7 @@ def test_methods(self):
1541 1541 def test_disallow_instantiation(self):
1542 1542 fd = self.get_stdout_fd()
1543 1543 printer = self.create_printer(fd)
1544 -PyStdPrinter_Type = type(printer)
1545 -with self.assertRaises(TypeError):
1546 -PyStdPrinter_Type(fd)
1544 +support.check_disallow_instantiation(self, type(printer))
1547 1545
1548 1546
1549 1547 if __name__ == "__main__":
Original file line number Diff line number Diff line change
@@ -951,8 +951,9 @@ class TestCmpToKeyC(TestCmpToKey, unittest.TestCase):
951 951 @support.cpython_only
952 952 def test_disallow_instantiation(self):
953 953 # Ensure that the type disallows instantiation (bpo-43916)
954 -tp = type(c_functools.cmp_to_key(None))
955 -self.assertRaises(TypeError, tp)
954 +support.check_disallow_instantiation(
955 +self, type(c_functools.cmp_to_key(None))
956 + )
956 957
957 958
958 959 class TestCmpToKeyPy(TestCmpToKey, unittest.TestCase):
Original file line number Diff line number Diff line change
@@ -915,20 +915,13 @@ def test_disallow_instantiation(self):
915 915 except ValueError:
916 916 continue
917 917 with self.subTest(constructor=constructor):
918 -hash_type = type(h)
919 -self.assertRaises(TypeError, hash_type)
918 +support.check_disallow_instantiation(self, type(h))
920 919
921 920 @unittest.skipUnless(HASH is not None, 'need _hashlib')
922 -def test_hash_disallow_instanciation(self):
921 +def test_hash_disallow_instantiation(self):
923 922 # internal types like _hashlib.HASH are not constructable
924 -with self.assertRaisesRegex(
925 -TypeError, "cannot create '_hashlib.HASH' instance"
926 - ):
927 -HASH()
928 -with self.assertRaisesRegex(
929 -TypeError, "cannot create '_hashlib.HASHXOF' instance"
930 - ):
931 -HASHXOF()
923 +support.check_disallow_instantiation(self, HASH)
924 +support.check_disallow_instantiation(self, HASHXOF)
932 925
933 926 def test_readonly_types(self):
934 927 for algorithm, constructors in self.constructors_to_test.items():
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
6 6 import unittest.mock
7 7 import warnings
8 8
9 -from test.support import hashlib_helper
9 +from test.support import hashlib_helper, check_disallow_instantiation
10 10
11 11 from _operator import _compare_digest as operator_compare_digest
12 12
@@ -439,11 +439,7 @@ def test_withmodule(self):
439 439 @unittest.skipUnless(C_HMAC is not None, 'need _hashlib')
440 440 def test_internal_types(self):
441 441 # internal types like _hashlib.C_HMAC are not constructable
442 -with self.assertRaisesRegex(
443 -TypeError, "cannot create '_hashlib.HMAC' instance"
444 - ):
445 -C_HMAC()
446 -
442 +check_disallow_instantiation(self, C_HMAC)
447 443 with self.assertRaisesRegex(TypeError, "immutable type"):
448 444 C_HMAC.value = None
449 445
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
1 1 from test.support import (gc_collect, bigmemtest, _2G,
2 -cpython_only, captured_stdout)
2 +cpython_only, captured_stdout,
3 +check_disallow_instantiation)
3 4 import locale
4 5 import re
5 6 import sre_compile
@@ -2218,11 +2219,10 @@ def test_signedness(self):
2218 2219 @cpython_only
2219 2220 def test_disallow_instantiation(self):
2220 2221 # Ensure that the type disallows instantiation (bpo-43916)
2221 -self.assertRaises(TypeError, re.Match)
2222 -self.assertRaises(TypeError, re.Pattern)
2222 +check_disallow_instantiation(self, re.Match)
2223 +check_disallow_instantiation(self, re.Pattern)
2223 2224 pat = re.compile("")
2224 -tp = type(pat.scanner(""))
2225 -self.assertRaises(TypeError, tp)
2225 +check_disallow_instantiation(self, type(pat.scanner("")))
2226 2226
2227 2227
2228 2228 class ExternalTests(unittest.TestCase):
Original file line number Diff line number Diff line change
@@ -88,12 +88,10 @@ def fileno(self):
88 88 self.assertEqual(select.select([], a, []), ([], a[:5], []))
89 89
90 90 def test_disallow_instantiation(self):
91 -tp = type(select.poll())
92 -self.assertRaises(TypeError, tp)
91 +support.check_disallow_instantiation(self, type(select.poll()))
93 92
94 93 if hasattr(select, 'devpoll'):
95 -tp = type(select.devpoll())
96 -self.assertRaises(TypeError, tp)
94 +support.check_disallow_instantiation(self, type(select.devpoll()))
97 95
98 96 def tearDownModule():
99 97 support.reap_children()
Original file line number Diff line number Diff line change
@@ -358,11 +358,7 @@ def test_ssl_types(self):
358 358 with self.subTest(ssl_type=ssl_type):
359 359 with self.assertRaisesRegex(TypeError, "immutable type"):
360 360 ssl_type.value = None
361 -with self.assertRaisesRegex(
362 -TypeError,
363 -"cannot create '_ssl.Certificate' instances"
364 - ):
365 -_ssl.Certificate()
361 +support.check_disallow_instantiation(self, _ssl.Certificate)
366 362
367 363 def test_private_init(self):
368 364 with self.assertRaisesRegex(TypeError, "public constructor"):
Original file line number Diff line number Diff line change
@@ -124,8 +124,7 @@ def func(): pass
124 124 def test_disallow_instantiation(self):
125 125 # Ensure that the type disallows instantiation (bpo-43916)
126 126 lock = threading.Lock()
127 -tp = type(lock)
128 -self.assertRaises(TypeError, tp)
127 +test.support.check_disallow_instantiation(self, type(lock))
129 128
130 129 # Create a bunch of threads, let each do some work, wait until all are
131 130 # done.
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@
12 12 import unicodedata
13 13 import unittest
14 14 from test.support import (open_urlresource, requires_resource, script_helper,
15 -cpython_only)
15 +cpython_only, check_disallow_instantiation)
16 16
17 17
18 18 class UnicodeMethodsTest(unittest.TestCase):
@@ -229,7 +229,7 @@ class UnicodeMiscTest(UnicodeDatabaseTest):
229 229 @cpython_only
230 230 def test_disallow_instantiation(self):
231 231 # Ensure that the type disallows instantiation (bpo-43916)
232 -self.assertRaises(TypeError, unicodedata.UCD)
232 +check_disallow_instantiation(self, unicodedata.UCD)
233 233
234 234 def test_failed_import_during_compiling(self):
235 235 # Issue 4367
Original file line number Diff line number Diff line change
@@ -132,10 +132,8 @@ def test_overflow(self):
132 132 @support.cpython_only
133 133 def test_disallow_instantiation(self):
134 134 # Ensure that the type disallows instantiation (bpo-43916)
135 -comp_type = type(zlib.compressobj())
136 -decomp_type = type(zlib.decompressobj())
137 -self.assertRaises(TypeError, comp_type)
138 -self.assertRaises(TypeError, decomp_type)
135 +support.check_disallow_instantiation(self, type(zlib.compressobj()))
136 +support.check_disallow_instantiation(self, type(zlib.decompressobj()))
139 137
140 138
141 139 class BaseCompressTestCase(object):