bpo-43988: Use check disallow instantiation helper (GH-26392) · python/cpython@fbff538 (original) (raw)
14 files changed
lines changed
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1991,5 +1991,11 @@ def check_disallow_instantiation(testcase, tp, *args, **kwds): | ||
1991 | 1991 | |
1992 | 1992 | See bpo-43916. |
1993 | 1993 | """ |
1994 | -msg = f"cannot create '{tp.__module__}\.{tp.__name__}' instances" | |
1994 | +mod = tp.__module__ | |
1995 | +name = tp.__name__ | |
1996 | +if mod != 'builtins': | |
1997 | +qualname = f"{mod}.{name}" | |
1998 | +else: | |
1999 | +qualname = f"{name}" | |
2000 | +msg = f"cannot create '{re.escape(qualname)}' instances" | |
1995 | 2001 | testcase.assertRaisesRegex(TypeError, msg, tp, *args, **kwds) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -43,8 +43,9 @@ def test_bad_constructor(self): | ||
43 | 43 | @support.cpython_only |
44 | 44 | def test_disallow_instantiation(self): |
45 | 45 | my_array = array.array("I") |
46 | -tp = type(iter(my_array)) | |
47 | -support.check_disallow_instantiation(self, tp, my_array) | |
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 |
---|---|---|
@@ -1549,9 +1549,7 @@ def test_methods(self): | ||
1549 | 1549 | def test_disallow_instantiation(self): |
1550 | 1550 | fd = self.get_stdout_fd() |
1551 | 1551 | printer = self.create_printer(fd) |
1552 | -PyStdPrinter_Type = type(printer) | |
1553 | -with self.assertRaises(TypeError): | |
1554 | -PyStdPrinter_Type(fd) | |
1552 | +support.check_disallow_instantiation(self, type(printer)) | |
1555 | 1553 | |
1556 | 1554 | |
1557 | 1555 | 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 |
---|---|---|
@@ -911,20 +911,13 @@ def test_disallow_instantiation(self): | ||
911 | 911 | for constructor in constructors: |
912 | 912 | h = constructor() |
913 | 913 | with self.subTest(constructor=constructor): |
914 | -hash_type = type(h) | |
915 | -self.assertRaises(TypeError, hash_type) | |
914 | +support.check_disallow_instantiation(self, type(h)) | |
916 | 915 | |
917 | 916 | @unittest.skipUnless(HASH is not None, 'need _hashlib') |
918 | -def test_hash_disallow_instanciation(self): | |
917 | +def test_hash_disallow_instantiation(self): | |
919 | 918 | # internal types like _hashlib.HASH are not constructable |
920 | -with self.assertRaisesRegex( | |
921 | -TypeError, "cannot create '_hashlib.HASH' instance" | |
922 | - ): | |
923 | -HASH() | |
924 | -with self.assertRaisesRegex( | |
925 | -TypeError, "cannot create '_hashlib.HASHXOF' instance" | |
926 | - ): | |
927 | -HASHXOF() | |
919 | +support.check_disallow_instantiation(self, HASH) | |
920 | +support.check_disallow_instantiation(self, HASHXOF) | |
928 | 921 | |
929 | 922 | def test_readonly_types(self): |
930 | 923 | 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 |
@@ -2224,11 +2225,10 @@ def test_signedness(self): | ||
2224 | 2225 | @cpython_only |
2225 | 2226 | def test_disallow_instantiation(self): |
2226 | 2227 | # Ensure that the type disallows instantiation (bpo-43916) |
2227 | -self.assertRaises(TypeError, re.Match) | |
2228 | -self.assertRaises(TypeError, re.Pattern) | |
2228 | +check_disallow_instantiation(self, re.Match) | |
2229 | +check_disallow_instantiation(self, re.Pattern) | |
2229 | 2230 | pat = re.compile("") |
2230 | -tp = type(pat.scanner("")) | |
2231 | -self.assertRaises(TypeError, tp) | |
2231 | +check_disallow_instantiation(self, type(pat.scanner(""))) | |
2232 | 2232 | |
2233 | 2233 | |
2234 | 2234 | 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): |