summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2014-08-28 13:41:29 (GMT)
committerBenjamin Peterson <benjamin@python.org>2014-08-28 13:41:29 (GMT)
commit0062d1e7f42b3036aa02701c723ff63d1d3d6028 (patch)
treed646a256590212ddb56342a6021a61d7c356a472 /Lib
parent876473eba3e7fab7d91446b66bd58c43d8c96647 (diff)
downloadcpython-0062d1e7f42b3036aa02701c723ff63d1d3d6028.zip
cpython-0062d1e7f42b3036aa02701c723ff63d1d3d6028.tar.gz
cpython-0062d1e7f42b3036aa02701c723ff63d1d3d6028.tar.bz2
PEP 466: backport hashlib algorithm constants (closes #21307)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/hashlib.py14
-rw-r--r--Lib/test/test_hashlib.py9
2 files changed, 20 insertions, 3 deletions
diff --git a/Lib/hashlib.py b/Lib/hashlib.py
index 6d69ad2..4a411bc 100644
--- a/Lib/hashlib.py
+++ b/Lib/hashlib.py
@@ -15,8 +15,9 @@ than using new():
md5(), sha1(), sha224(), sha256(), sha384(), and sha512()
-More algorithms may be available on your platform but the above are
-guaranteed to exist.
+More algorithms may be available on your platform but the above are guaranteed
+to exist. See the algorithms_guaranteed and algorithms_available attributes
+to find out what algorithm names can be passed to new().
NOTE: If you want the adler32 or crc32 hash functions they are available in
the zlib module.
@@ -58,9 +59,14 @@ More condensed:
# always available algorithm is added.
__always_supported = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512')
+algorithms_guaranteed = set(__always_supported)
+algorithms_available = set(__always_supported)
+
algorithms = __always_supported
-__all__ = __always_supported + ('new', 'algorithms', 'pbkdf2_hmac')
+__all__ = __always_supported + ('new', 'algorithms_guaranteed',
+ 'algorithms_available', 'algorithms',
+ 'pbkdf2_hmac')
def __get_builtin_constructor(name):
@@ -128,6 +134,8 @@ try:
import _hashlib
new = __hash_new
__get_hash = __get_openssl_constructor
+ algorithms_available = algorithms_available.union(
+ _hashlib.openssl_md_meth_names)
except ImportError:
new = __py_new
__get_hash = __get_builtin_constructor
diff --git a/Lib/test/test_hashlib.py b/Lib/test/test_hashlib.py
index 3fc172f..76be461 100644
--- a/Lib/test/test_hashlib.py
+++ b/Lib/test/test_hashlib.py
@@ -109,6 +109,15 @@ class HashLibTestCase(unittest.TestCase):
tuple([_algo for _algo in self.supported_hash_names if
_algo.islower()]))
+ def test_algorithms_guaranteed(self):
+ self.assertEqual(hashlib.algorithms_guaranteed,
+ set(_algo for _algo in self.supported_hash_names
+ if _algo.islower()))
+
+ def test_algorithms_available(self):
+ self.assertTrue(set(hashlib.algorithms_guaranteed).
+ issubset(hashlib.algorithms_available))
+
def test_unknown_hash(self):
self.assertRaises(ValueError, hashlib.new, 'spam spam spam spam spam')
self.assertRaises(TypeError, hashlib.new, 1)