From 3480e428489a8a503a588ead7161ea763ce86ea5 Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Sat, 1 May 2021 21:50:57 +0200 Subject: [PATCH 1/2] bpo-43916: Rewrite new hashlib tests, fix typo Signed-off-by: Christian Heimes --- Lib/test/test_hashlib.py | 41 +++++++++++----------------------------- 1 file changed, 11 insertions(+), 30 deletions(-) diff --git a/Lib/test/test_hashlib.py b/Lib/test/test_hashlib.py index c7c128e75568e2..4d5ab19393d42d 100644 --- a/Lib/test/test_hashlib.py +++ b/Lib/test/test_hashlib.py @@ -901,36 +901,17 @@ 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) + 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): From e556ccad910d4b01222615cd96ab7c1746cc5456 Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Sat, 1 May 2021 22:23:23 +0200 Subject: [PATCH 2/2] Flag test as cpython only --- Lib/test/test_hashlib.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_hashlib.py b/Lib/test/test_hashlib.py index 4d5ab19393d42d..a515d3a3469330 100644 --- a/Lib/test/test_hashlib.py +++ b/Lib/test/test_hashlib.py @@ -901,6 +901,7 @@ def test_get_fips_mode(self): if fips_mode is not None: self.assertIsInstance(fips_mode, int) + @support.cpython_only def test_disallow_instantiation(self): for algorithm, constructors in self.constructors_to_test.items(): if algorithm.startswith(("sha3_", "shake", "blake")):