From ba9eb8669ed9bad08b840817833893f5463dc6a6 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 27 May 2021 00:26:40 +0200 Subject: [PATCH 1/6] bpo-43988: Use test.support.check_disallow_instantiation() --- Lib/test/test_curses.py | 6 ++++-- Lib/test/test_dbm_gnu.py | 2 +- Lib/test/test_embed.py | 3 +-- Lib/test/test_functools.py | 2 +- Lib/test/test_hashlib.py | 14 ++++---------- Lib/test/test_hmac.py | 6 +----- Lib/test/test_re.py | 9 +++++---- Lib/test/test_select.py | 4 ++-- Lib/test/test_ssl.py | 6 +----- Lib/test/test_threading.py | 2 +- Lib/test/test_unicodedata.py | 4 ++-- Lib/test/test_zlib.py | 4 ++-- 12 files changed, 25 insertions(+), 37 deletions(-) diff --git a/Lib/test/test_curses.py b/Lib/test/test_curses.py index 8bf48a6454d691..2ef23cf5108db8 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,8 @@ 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)) + tp = type(panel) + check_disallow_instantiation(self, tp) @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..01d7cb4834a2d7 100644 --- a/Lib/test/test_dbm_gnu.py +++ b/Lib/test/test_dbm_gnu.py @@ -32,7 +32,7 @@ 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, tp) 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 f4a18fdf5a566d..1ab8230479bd78 100644 --- a/Lib/test/test_embed.py +++ b/Lib/test/test_embed.py @@ -1550,8 +1550,7 @@ 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, PyStdPrinter_Type) if __name__ == "__main__": diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py index 3320ab7ec6649d..0762a38753fffd 100644 --- a/Lib/test/test_functools.py +++ b/Lib/test/test_functools.py @@ -952,7 +952,7 @@ class TestCmpToKeyC(TestCmpToKey, unittest.TestCase): 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, tp) class TestCmpToKeyPy(TestCmpToKey, unittest.TestCase): diff --git a/Lib/test/test_hashlib.py b/Lib/test/test_hashlib.py index ad2ed69e24b2c4..e14cd031746428 100644 --- a/Lib/test/test_hashlib.py +++ b/Lib/test/test_hashlib.py @@ -912,19 +912,13 @@ def test_disallow_instantiation(self): h = constructor() with self.subTest(constructor=constructor): hash_type = type(h) - self.assertRaises(TypeError, hash_type) + support.check_disallow_instantiation(self, hash_type) @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, HASHOF) 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..788ae2a293f8e1 100644 --- a/Lib/test/test_hmac.py +++ b/Lib/test/test_hmac.py @@ -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() - + support.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 0ed243da3ff9e2..f2ee36c71e58a5 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 @@ -2224,11 +2225,11 @@ 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, tp) class ExternalTests(unittest.TestCase): diff --git a/Lib/test/test_select.py b/Lib/test/test_select.py index 957a633f3230e7..5941a7c6941e7c 100644 --- a/Lib/test/test_select.py +++ b/Lib/test/test_select.py @@ -89,11 +89,11 @@ def fileno(self): def test_disallow_instantiation(self): tp = type(select.poll()) - self.assertRaises(TypeError, tp) + support.check_disallow_instantiation(self, tp) if hasattr(select, 'devpoll'): tp = type(select.devpoll()) - self.assertRaises(TypeError, tp) + support.check_disallow_instantiation(self, tp) def tearDownModule(): support.reap_children() diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py index 00d5eff81537d1..0d38d7756fdc72 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..ef5fafc3b2381f 100644 --- a/Lib/test/test_threading.py +++ b/Lib/test/test_threading.py @@ -125,7 +125,7 @@ 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, tp) # 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..cfacf27cecc292 100644 --- a/Lib/test/test_zlib.py +++ b/Lib/test/test_zlib.py @@ -134,8 +134,8 @@ 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, comp_type) + support.check_disallow_instantiation(self, decomp_type) class BaseCompressTestCase(object): From 3bdcc1ed66d541c0de13250410edaabbfb23138b Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 27 May 2021 00:30:57 +0200 Subject: [PATCH 2/6] Don't check module name for builtins --- Lib/test/support/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index 8c6e5547d5e717..5266daeba884bb 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -1991,5 +1991,7 @@ def check_disallow_instantiation(testcase, tp, *args, **kwds): See bpo-43916. """ - msg = f"cannot create '{tp.__module__}\.{tp.__name__}' instances" + mod = tp.__module__ + name = tp.__name__ + msg = f"cannot create '{mod if mod != 'builtins' else ''}.?{name}' instances" testcase.assertRaisesRegex(TypeError, msg, tp, *args, **kwds) From ffb1f9e3817dcf705bdecf0ab31faea271e5f9db Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 27 May 2021 00:50:11 +0200 Subject: [PATCH 3/6] Typo --- Lib/test/test_hashlib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_hashlib.py b/Lib/test/test_hashlib.py index e14cd031746428..91cb9f68411559 100644 --- a/Lib/test/test_hashlib.py +++ b/Lib/test/test_hashlib.py @@ -918,7 +918,7 @@ def test_disallow_instantiation(self): def test_hash_disallow_instantiation(self): # internal types like _hashlib.HASH are not constructable support.check_disallow_instantiation(self, HASH) - support.check_disallow_instantiation(self, HASHOF) + support.check_disallow_instantiation(self, HASHXOF) def test_readonly_types(self): for algorithm, constructors in self.constructors_to_test.items(): From 387f04ca90e7c0d01f0d0c7d78629fbc9204b8a5 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 27 May 2021 01:00:49 +0200 Subject: [PATCH 4/6] Fix hmac test --- Lib/test/test_hmac.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_hmac.py b/Lib/test/test_hmac.py index 788ae2a293f8e1..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,7 +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 - support.check_disallow_instantiation(self, C_HMAC) + check_disallow_instantiation(self, C_HMAC) with self.assertRaisesRegex(TypeError, "immutable type"): C_HMAC.value = None From bed0821a33b48c43cce0ab0d2e894292eef7baaa Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 27 May 2021 01:03:44 +0200 Subject: [PATCH 5/6] Use fr string --- Lib/test/support/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index 5266daeba884bb..f07d1761486eaa 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -1993,5 +1993,5 @@ def check_disallow_instantiation(testcase, tp, *args, **kwds): """ mod = tp.__module__ name = tp.__name__ - msg = f"cannot create '{mod if mod != 'builtins' else ''}.?{name}' instances" + msg = fr"cannot create '{mod if mod != 'builtins' else ''}.?{name}' instances" testcase.assertRaisesRegex(TypeError, msg, tp, *args, **kwds) From 587625246124f3ccee22e9f79277bb48632e9b4f Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 27 May 2021 01:28:33 +0200 Subject: [PATCH 6/6] Address review --- Lib/test/support/__init__.py | 6 +++++- Lib/test/test_array.py | 5 +++-- Lib/test/test_curses.py | 3 +-- Lib/test/test_dbm_gnu.py | 3 +-- Lib/test/test_embed.py | 3 +-- Lib/test/test_functools.py | 5 +++-- Lib/test/test_hashlib.py | 3 +-- Lib/test/test_re.py | 3 +-- Lib/test/test_select.py | 6 ++---- Lib/test/test_threading.py | 3 +-- Lib/test/test_zlib.py | 6 ++---- 11 files changed, 21 insertions(+), 25 deletions(-) diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index f07d1761486eaa..42ca614dce1ad1 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -1993,5 +1993,9 @@ def check_disallow_instantiation(testcase, tp, *args, **kwds): """ mod = tp.__module__ name = tp.__name__ - msg = fr"cannot create '{mod if mod != 'builtins' else ''}.?{name}' instances" + 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 e7cddf2314732d..18f78d52467e2b 100644 --- a/Lib/test/test_array.py +++ b/Lib/test/test_array.py @@ -43,8 +43,9 @@ def test_bad_constructor(self): @support.cpython_only def test_disallow_instantiation(self): my_array = array.array("I") - tp = type(iter(my_array)) - support.check_disallow_instantiation(self, tp, my_array) + 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 2ef23cf5108db8..d3c152c42cf62f 100644 --- a/Lib/test/test_curses.py +++ b/Lib/test/test_curses.py @@ -1053,8 +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) - tp = type(panel) - check_disallow_instantiation(self, tp) + 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 01d7cb4834a2d7..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) - support.check_disallow_instantiation(self, 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 1ab8230479bd78..373fcf0ffac086 100644 --- a/Lib/test/test_embed.py +++ b/Lib/test/test_embed.py @@ -1549,8 +1549,7 @@ def test_methods(self): def test_disallow_instantiation(self): fd = self.get_stdout_fd() printer = self.create_printer(fd) - PyStdPrinter_Type = type(printer) - support.check_disallow_instantiation(self, PyStdPrinter_Type) + 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 0762a38753fffd..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)) - support.check_disallow_instantiation(self, 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 91cb9f68411559..e419b388460dcd 100644 --- a/Lib/test/test_hashlib.py +++ b/Lib/test/test_hashlib.py @@ -911,8 +911,7 @@ def test_disallow_instantiation(self): for constructor in constructors: h = constructor() with self.subTest(constructor=constructor): - hash_type = type(h) - support.check_disallow_instantiation(self, hash_type) + support.check_disallow_instantiation(self, type(h)) @unittest.skipUnless(HASH is not None, 'need _hashlib') def test_hash_disallow_instantiation(self): diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py index f2ee36c71e58a5..18fa24a99ce032 100644 --- a/Lib/test/test_re.py +++ b/Lib/test/test_re.py @@ -2228,8 +2228,7 @@ def test_disallow_instantiation(self): check_disallow_instantiation(self, re.Match) check_disallow_instantiation(self, re.Pattern) pat = re.compile("") - tp = type(pat.scanner("")) - check_disallow_instantiation(self, 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 5941a7c6941e7c..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()) - support.check_disallow_instantiation(self, tp) + support.check_disallow_instantiation(self, type(select.poll())) if hasattr(select, 'devpoll'): - tp = type(select.devpoll()) - support.check_disallow_instantiation(self, tp) + support.check_disallow_instantiation(self, type(select.devpoll())) def tearDownModule(): support.reap_children() diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py index ef5fafc3b2381f..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) - test.support.check_disallow_instantiation(self, 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_zlib.py b/Lib/test/test_zlib.py index cfacf27cecc292..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()) - support.check_disallow_instantiation(self, comp_type) - support.check_disallow_instantiation(self, decomp_type) + support.check_disallow_instantiation(self, type(zlib.compressobj())) + support.check_disallow_instantiation(self, type(zlib.decompressobj())) class BaseCompressTestCase(object):