Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = fr"cannot create '{mod if mod != 'builtins' else ''}.?{name}' instances"
testcase.assertRaisesRegex(TypeError, msg, tp, *args, **kwds)
6 changes: 4 additions & 2 deletions Lib/test/test_curses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_dbm_gnu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
3 changes: 1 addition & 2 deletions Lib/test/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
14 changes: 4 additions & 10 deletions Lib/test/test_hashlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, HASHXOF)

def test_readonly_types(self):
for algorithm, constructors in self.constructors_to_test.items():
Expand Down
8 changes: 2 additions & 6 deletions Lib/test/test_hmac.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()

check_disallow_instantiation(self, C_HMAC)
with self.assertRaisesRegex(TypeError, "immutable type"):
C_HMAC.value = None

Expand Down
9 changes: 5 additions & 4 deletions Lib/test/test_re.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 1 addition & 5 deletions Lib/test/test_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_unicodedata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_zlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down