bpo-43916: _md5.md5 uses Py_TPFLAGS_DISALLOW_INSTANTIATION (GH-25753)
The following types use Py_TPFLAGS_DISALLOW_INSTANTIATION flag:
* _md5.md5
* _sha1.sha1
* _sha256.sha224
* _sha256.sha256
* _sha512.sha384
* _sha512.sha512
diff --git a/Lib/test/test_hashlib.py b/Lib/test/test_hashlib.py
index 1236aa7..c7c128e 100644
--- a/Lib/test/test_hashlib.py
+++ b/Lib/test/test_hashlib.py
@@ -901,8 +901,39 @@ def test_get_fips_mode(self):
if fips_mode is not None:
self.assertIsInstance(fips_mode, int)
+ def test_disallow_instanciation(self):
+ constructors = []
+ try:
+ import _md5
+ constructors.append(_md5.md5)
+ except ImportError:
+ pass
+ try:
+ import _sha1
+ constructors.append(_sha1.sha1)
+ except ImportError:
+ pass
+ try:
+ import _sha256
+ constructors.append(_sha256.sha224)
+ constructors.append(_sha256.sha256)
+ except ImportError:
+ pass
+ try:
+ import _sha512
+ constructors.append(_sha512.sha384)
+ constructors.append(_sha512.sha512)
+ except ImportError:
+ pass
+
+ for constructor in constructors:
+ h = constructor()
+ with self.subTest(constructor=constructor):
+ hash_type = type(h)
+ self.assertRaises(TypeError, hash_type)
+
@unittest.skipUnless(HASH is not None, 'need _hashlib')
- def test_internal_types(self):
+ def test_hash_disallow_instanciation(self):
# internal types like _hashlib.HASH are not constructable
with self.assertRaisesRegex(
TypeError, "cannot create '_hashlib.HASH' instance"