Skip to content
Snippets Groups Projects
Commit c6ec4d87 authored by Nicolas Dandrimont's avatar Nicolas Dandrimont
Browse files

hashutil: drop all pre-3.6 blake2 workarounds

blake2s and blake2b have been provided by the stdlib hashlib since
Python 3.6, and we declare 3.7 as minimum Python version supported.
parent 9d517c17
No related branches found
Tags v4.1.0
1 merge request!216hashutil: drop all pre-3.6 blake2 workarounds
......@@ -22,8 +22,5 @@ ignore_missing_imports = True
[mypy-pkg_resources.*]
ignore_missing_imports = True
[mypy-pyblake2.*]
ignore_missing_imports = True
[mypy-pytest.*]
ignore_missing_imports = True
......@@ -35,8 +35,6 @@ def parse_requirements(name=None):
return requirements
blake2_requirements = ['pyblake2;python_version<"3.6"']
setup(
name="swh.model",
description="Software Heritage data model",
......@@ -49,9 +47,7 @@ setup(
packages=find_packages(),
setup_requires=["setuptools-scm"],
use_scm_version=True,
install_requires=(
parse_requirements() + parse_requirements("swh") + blake2_requirements
),
install_requires=parse_requirements() + parse_requirements("swh"),
extras_require={
"cli": parse_requirements("cli"),
"testing-minimal": parse_requirements("test"),
......
......@@ -183,20 +183,8 @@ def _new_blake2_hash(algo):
"Digest size for algorithm %s must be a multiple of 8" % algo
)
if lalgo in hashlib.algorithms_available:
# Handle the case where OpenSSL ships the given algorithm
# (e.g. Python 3.5 on Debian 9 stretch)
_blake2_hash_cache[algo] = lambda: hashlib.new(lalgo)
else:
# Try using the built-in implementation for Python 3.6+
if blake_family in hashlib.algorithms_available:
blake2 = getattr(hashlib, blake_family)
else:
import pyblake2
blake2 = getattr(pyblake2, blake_family)
_blake2_hash_cache[algo] = lambda: blake2(digest_size=digest_size)
blake2 = getattr(hashlib, blake_family)
_blake2_hash_cache[algo] = lambda: blake2(digest_size=digest_size)
return _blake2_hash_cache[algo]()
......
......@@ -213,86 +213,8 @@ def test_new_hash_unsupported_hashing_algorithm():
hashutil._new_hash("blake2:10")
@pytest.mark.skipif(
"blake2b512" not in hashlib.algorithms_available, reason="blake2b512 not built-in"
)
@patch("hashlib.new")
def test_new_hash_blake2b_blake2b512_builtin(mock_hashlib_new):
mock_hashlib_new.return_value = sentinel = object()
h = hashutil._new_hash("blake2b512")
assert h is sentinel
mock_hashlib_new.assert_called_with("blake2b512")
@pytest.mark.skipif(
"blake2s256" not in hashlib.algorithms_available, reason="blake2s256 not built-in"
)
@patch("hashlib.new")
def test_new_hash_blake2s_blake2s256_builtin(mock_hashlib_new):
mock_hashlib_new.return_value = sentinel = object()
h = hashutil._new_hash("blake2s256")
assert h is sentinel
mock_hashlib_new.assert_called_with("blake2s256")
@pytest.mark.skipif(
"blake2b" not in hashlib.algorithms_available, reason="blake2b not built-in"
)
def test_new_hash_blake2b_builtin():
removed_hash = False
try:
if "blake2b512" in hashlib.algorithms_available:
removed_hash = True
hashlib.algorithms_available.remove("blake2b512")
with patch_blake2("hashlib.blake2b") as mock_blake2b:
mock_blake2b.return_value = sentinel = object()
h = hashutil._new_hash("blake2b512")
assert h is sentinel
mock_blake2b.assert_called_with(digest_size=512 // 8)
finally:
if removed_hash:
hashlib.algorithms_available.add("blake2b512")
@pytest.mark.skipif(
"blake2s" not in hashlib.algorithms_available, reason="blake2s not built-in"
)
def test_new_hash_blake2s_builtin():
removed_hash = False
try:
if "blake2s256" in hashlib.algorithms_available:
removed_hash = True
hashlib.algorithms_available.remove("blake2s256")
with patch_blake2("hashlib.blake2s") as mock_blake2s:
mock_blake2s.return_value = sentinel = object()
h = hashutil._new_hash("blake2s256")
assert h is sentinel
mock_blake2s.assert_called_with(digest_size=256 // 8)
finally:
if removed_hash:
hashlib.algorithms_available.add("blake2s256")
@pytest.mark.skipif(
"blake2b512" in hashlib.algorithms_available, reason="blake2b512 built-in"
)
@pytest.mark.skipif(
"blake2b" in hashlib.algorithms_available, reason="blake2b built-in"
)
def test_new_hash_blake2b_pyblake2():
with patch_blake2("pyblake2.blake2b") as mock_blake2b:
with patch_blake2("hashlib.blake2b") as mock_blake2b:
mock_blake2b.return_value = sentinel = object()
h = hashutil._new_hash("blake2b512")
......@@ -301,14 +223,8 @@ def test_new_hash_blake2b_pyblake2():
mock_blake2b.assert_called_with(digest_size=512 // 8)
@pytest.mark.skipif(
"blake2s256" in hashlib.algorithms_available, reason="blake2s256 built-in"
)
@pytest.mark.skipif(
"blake2s" in hashlib.algorithms_available, reason="blake2s built-in"
)
def test_new_hash_blake2s_pyblake2():
with patch_blake2("pyblake2.blake2s") as mock_blake2s:
def test_new_hash_blake2s_builtin():
with patch_blake2("hashlib.blake2s") as mock_blake2s:
mock_blake2s.return_value = sentinel = object()
h = hashutil._new_hash("blake2s256")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment