]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-43916: Rewrite new hashlib tests, fix typo (GH-25791)
authorChristian Heimes <christian@python.org>
Sat, 1 May 2021 20:42:36 +0000 (22:42 +0200)
committerGitHub <noreply@github.com>
Sat, 1 May 2021 20:42:36 +0000 (22:42 +0200)
* bpo-43916: Rewrite new hashlib tests, fix typo
* Flag test as cpython only

Lib/test/test_hashlib.py

index c7c128e75568e269f35f40763c77016fff761807..a515d3a3469330c0b9f70f8f5387666a92be9736 100644 (file)
@@ -901,36 +901,18 @@ class HashLibTestCase(unittest.TestCase):
         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)
+    @support.cpython_only
+    def test_disallow_instantiation(self):
+        for algorithm, constructors in self.constructors_to_test.items():
+            if algorithm.startswith(("sha3_", "shake", "blake")):
+                # _sha3 and _blake types can be instantiated
+                continue
+            # all other types have DISALLOW_INSTANTIATION
+            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_hash_disallow_instanciation(self):