Skip to content

Commit fbff538

Browse files
author
Erlend Egeberg Aasland
authored
bpo-43988: Use check disallow instantiation helper (GH-26392)
1 parent 3e7ee02 commit fbff538

14 files changed

+37
-51
lines changed

Lib/test/support/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1991,5 +1991,11 @@ def check_disallow_instantiation(testcase, tp, *args, **kwds):
19911991
19921992
See bpo-43916.
19931993
"""
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"
19952001
testcase.assertRaisesRegex(TypeError, msg, tp, *args, **kwds)

Lib/test/test_array.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ def test_bad_constructor(self):
4343
@support.cpython_only
4444
def test_disallow_instantiation(self):
4545
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+
)
4849

4950
@support.cpython_only
5051
def test_immutable(self):

Lib/test/test_curses.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import tempfile
77
import unittest
88

9-
from test.support import requires, verbose, SaveSignals, cpython_only
9+
from test.support import (requires, verbose, SaveSignals, cpython_only,
10+
check_disallow_instantiation)
1011
from test.support.import_helper import import_module
1112

1213
# Optionally test curses module. This currently requires that the
@@ -1052,7 +1053,7 @@ def test_disallow_instantiation(self):
10521053
# Ensure that the type disallows instantiation (bpo-43916)
10531054
w = curses.newwin(10, 10)
10541055
panel = curses.panel.new_panel(w)
1055-
self.assertRaises(TypeError, type(panel))
1056+
check_disallow_instantiation(self, type(panel))
10561057

10571058
@requires_curses_func('is_term_resized')
10581059
def test_is_term_resized(self):

Lib/test/test_dbm_gnu.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ def tearDown(self):
3131
def test_disallow_instantiation(self):
3232
# Ensure that the type disallows instantiation (bpo-43916)
3333
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))
3635

3736
def test_key_methods(self):
3837
self.g = gdbm.open(filename, 'c')

Lib/test/test_embed.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,9 +1549,7 @@ def test_methods(self):
15491549
def test_disallow_instantiation(self):
15501550
fd = self.get_stdout_fd()
15511551
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))
15551553

15561554

15571555
if __name__ == "__main__":

Lib/test/test_functools.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -951,8 +951,9 @@ class TestCmpToKeyC(TestCmpToKey, unittest.TestCase):
951951
@support.cpython_only
952952
def test_disallow_instantiation(self):
953953
# 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+
)
956957

957958

958959
class TestCmpToKeyPy(TestCmpToKey, unittest.TestCase):

Lib/test/test_hashlib.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -911,20 +911,13 @@ def test_disallow_instantiation(self):
911911
for constructor in constructors:
912912
h = constructor()
913913
with self.subTest(constructor=constructor):
914-
hash_type = type(h)
915-
self.assertRaises(TypeError, hash_type)
914+
support.check_disallow_instantiation(self, type(h))
916915

917916
@unittest.skipUnless(HASH is not None, 'need _hashlib')
918-
def test_hash_disallow_instanciation(self):
917+
def test_hash_disallow_instantiation(self):
919918
# 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)
928921

929922
def test_readonly_types(self):
930923
for algorithm, constructors in self.constructors_to_test.items():

Lib/test/test_hmac.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import unittest.mock
77
import warnings
88

9-
from test.support import hashlib_helper
9+
from test.support import hashlib_helper, check_disallow_instantiation
1010

1111
from _operator import _compare_digest as operator_compare_digest
1212

@@ -439,11 +439,7 @@ def test_withmodule(self):
439439
@unittest.skipUnless(C_HMAC is not None, 'need _hashlib')
440440
def test_internal_types(self):
441441
# 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)
447443
with self.assertRaisesRegex(TypeError, "immutable type"):
448444
C_HMAC.value = None
449445

Lib/test/test_re.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from test.support import (gc_collect, bigmemtest, _2G,
2-
cpython_only, captured_stdout)
2+
cpython_only, captured_stdout,
3+
check_disallow_instantiation)
34
import locale
45
import re
56
import sre_compile
@@ -2224,11 +2225,10 @@ def test_signedness(self):
22242225
@cpython_only
22252226
def test_disallow_instantiation(self):
22262227
# 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)
22292230
pat = re.compile("")
2230-
tp = type(pat.scanner(""))
2231-
self.assertRaises(TypeError, tp)
2231+
check_disallow_instantiation(self, type(pat.scanner("")))
22322232

22332233

22342234
class ExternalTests(unittest.TestCase):

Lib/test/test_select.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,10 @@ def fileno(self):
8888
self.assertEqual(select.select([], a, []), ([], a[:5], []))
8989

9090
def test_disallow_instantiation(self):
91-
tp = type(select.poll())
92-
self.assertRaises(TypeError, tp)
91+
support.check_disallow_instantiation(self, type(select.poll()))
9392

9493
if hasattr(select, 'devpoll'):
95-
tp = type(select.devpoll())
96-
self.assertRaises(TypeError, tp)
94+
support.check_disallow_instantiation(self, type(select.devpoll()))
9795

9896
def tearDownModule():
9997
support.reap_children()

Lib/test/test_ssl.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,7 @@ def test_ssl_types(self):
358358
with self.subTest(ssl_type=ssl_type):
359359
with self.assertRaisesRegex(TypeError, "immutable type"):
360360
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)
366362

367363
def test_private_init(self):
368364
with self.assertRaisesRegex(TypeError, "public constructor"):

Lib/test/test_threading.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ def func(): pass
124124
def test_disallow_instantiation(self):
125125
# Ensure that the type disallows instantiation (bpo-43916)
126126
lock = threading.Lock()
127-
tp = type(lock)
128-
self.assertRaises(TypeError, tp)
127+
test.support.check_disallow_instantiation(self, type(lock))
129128

130129
# Create a bunch of threads, let each do some work, wait until all are
131130
# done.

Lib/test/test_unicodedata.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import unicodedata
1313
import unittest
1414
from test.support import (open_urlresource, requires_resource, script_helper,
15-
cpython_only)
15+
cpython_only, check_disallow_instantiation)
1616

1717

1818
class UnicodeMethodsTest(unittest.TestCase):
@@ -229,7 +229,7 @@ class UnicodeMiscTest(UnicodeDatabaseTest):
229229
@cpython_only
230230
def test_disallow_instantiation(self):
231231
# Ensure that the type disallows instantiation (bpo-43916)
232-
self.assertRaises(TypeError, unicodedata.UCD)
232+
check_disallow_instantiation(self, unicodedata.UCD)
233233

234234
def test_failed_import_during_compiling(self):
235235
# Issue 4367

Lib/test/test_zlib.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,8 @@ def test_overflow(self):
132132
@support.cpython_only
133133
def test_disallow_instantiation(self):
134134
# 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()))
139137

140138

141139
class BaseCompressTestCase(object):

0 commit comments

Comments
 (0)