Skip to content

Commit ddbef71

Browse files
authored
bpo-43916: Rewrite new hashlib tests, fix typo (GH-25791)
* bpo-43916: Rewrite new hashlib tests, fix typo * Flag test as cpython only
1 parent 55e5c68 commit ddbef71

File tree

1 file changed

+12
-30
lines changed

1 file changed

+12
-30
lines changed

Lib/test/test_hashlib.py

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -901,36 +901,18 @@ def test_get_fips_mode(self):
901901
if fips_mode is not None:
902902
self.assertIsInstance(fips_mode, int)
903903

904-
def test_disallow_instanciation(self):
905-
constructors = []
906-
try:
907-
import _md5
908-
constructors.append(_md5.md5)
909-
except ImportError:
910-
pass
911-
try:
912-
import _sha1
913-
constructors.append(_sha1.sha1)
914-
except ImportError:
915-
pass
916-
try:
917-
import _sha256
918-
constructors.append(_sha256.sha224)
919-
constructors.append(_sha256.sha256)
920-
except ImportError:
921-
pass
922-
try:
923-
import _sha512
924-
constructors.append(_sha512.sha384)
925-
constructors.append(_sha512.sha512)
926-
except ImportError:
927-
pass
928-
929-
for constructor in constructors:
930-
h = constructor()
931-
with self.subTest(constructor=constructor):
932-
hash_type = type(h)
933-
self.assertRaises(TypeError, hash_type)
904+
@support.cpython_only
905+
def test_disallow_instantiation(self):
906+
for algorithm, constructors in self.constructors_to_test.items():
907+
if algorithm.startswith(("sha3_", "shake", "blake")):
908+
# _sha3 and _blake types can be instantiated
909+
continue
910+
# all other types have DISALLOW_INSTANTIATION
911+
for constructor in constructors:
912+
h = constructor()
913+
with self.subTest(constructor=constructor):
914+
hash_type = type(h)
915+
self.assertRaises(TypeError, hash_type)
934916

935917
@unittest.skipUnless(HASH is not None, 'need _hashlib')
936918
def test_hash_disallow_instanciation(self):

0 commit comments

Comments
 (0)