Skip to content

Commit 0a3452e

Browse files
author
Erlend Egeberg Aasland
authored
[3.10] bpo-43988: Add test.support.check_disallow_instantiation() (GH-25757) (GH-26885)
(cherry picked from commit 4f72526, fbff538, and 8cec740) Co-authored-by: Erlend Egeberg Aasland <[email protected]> Automerge-Triggered-By: GH:vstinner
1 parent ece3841 commit 0a3452e

16 files changed

+58
-53
lines changed

Doc/library/test.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,8 +928,16 @@ The :mod:`test.support` module defines the following functions:
928928
.. versionadded:: 3.10
929929

930930

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+
931938
The :mod:`test.support` module defines the following classes:
932939

940+
933941
.. class:: SuppressCrashReport()
934942

935943
A context manager used to try to prevent crash dialog popups on tests that

Lib/sqlite3/test/dbapi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import sqlite3 as sqlite
2626
import sys
2727

28+
from test.support import check_disallow_instantiation
2829
from test.support.os_helper import TESTFN, unlink
2930

3031

@@ -94,8 +95,7 @@ def test_shared_cache_deprecated(self):
9495

9596
def test_disallow_instantiation(self):
9697
cx = sqlite.connect(":memory:")
97-
tp = type(cx("select 1"))
98-
self.assertRaises(TypeError, tp)
98+
check_disallow_instantiation(self, type(cx("select 1")))
9999

100100

101101
class ConnectionTests(unittest.TestCase):

Lib/test/support/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"requires_IEEE_754", "requires_zlib",
4141
"anticipate_failure", "load_package_tests", "detect_api_mismatch",
4242
"check__all__", "skip_if_buggy_ucrt_strfptime",
43+
"check_disallow_instantiation",
4344
# sys
4445
"is_jython", "is_android", "check_impl_detail", "unix_shell",
4546
"setswitchinterval",
@@ -1992,3 +1993,19 @@ def infinite_recursion(max_depth=75):
19921993
yield
19931994
finally:
19941995
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)

Lib/test/test_array.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ def test_bad_constructor(self):
4242

4343
@support.cpython_only
4444
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+
)
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
@@ -1541,9 +1541,7 @@ def test_methods(self):
15411541
def test_disallow_instantiation(self):
15421542
fd = self.get_stdout_fd()
15431543
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))
15471545

15481546

15491547
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
@@ -915,20 +915,13 @@ def test_disallow_instantiation(self):
915915
except ValueError:
916916
continue
917917
with self.subTest(constructor=constructor):
918-
hash_type = type(h)
919-
self.assertRaises(TypeError, hash_type)
918+
support.check_disallow_instantiation(self, type(h))
920919

921920
@unittest.skipUnless(HASH is not None, 'need _hashlib')
922-
def test_hash_disallow_instanciation(self):
921+
def test_hash_disallow_instantiation(self):
923922
# 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)
932925

933926
def test_readonly_types(self):
934927
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

0 commit comments

Comments
 (0)