From f75be5a3386d14ceee237fc30b99d04552482a4a Mon Sep 17 00:00:00 2001
From: "Antoine R. Dumont (@ardumont)" <antoine.romain.dumont@gmail.com>
Date: Wed, 15 Mar 2017 10:02:28 +0100
Subject: [PATCH] swh.model.hashutil: Make unknown variable length algo
 creation break

Remove the limit on the python3 version, this should be transparent.
If the hash requested is not available, this will raise with an
explanation on the error.

Related T692
---
 swh/model/hashutil.py            | 16 +++++-----------
 swh/model/tests/test_hashutil.py | 24 ++++--------------------
 2 files changed, 9 insertions(+), 31 deletions(-)

diff --git a/swh/model/hashutil.py b/swh/model/hashutil.py
index 946edc4a..81c88c3a 100644
--- a/swh/model/hashutil.py
+++ b/swh/model/hashutil.py
@@ -7,7 +7,6 @@ import binascii
 import functools
 import hashlib
 import os
-import sys
 
 from io import BytesIO
 
@@ -77,16 +76,11 @@ def _new_hash(algo, length=None):
             raise ValueError('Missing length for git hashing algorithm')
         base_algo = algo[:-4]
         h = _new_git_hash(base_algo, 'blob', length)
-    elif ':' in algo:   # variable length hashing algorithms (only from
-                        # python3 >= 3.6)
-        if sys.version_info.major == 3 and sys.version_info.minor >= 6:
-            _algo = algo.split(':')
-            base_algo = _algo[0]
-            variable_length = int(_algo[1])
-
-            h = hashlib.new('%s%s' % (base_algo, variable_length))
-        else:
-            raise ValueError('Unsupported hashing algorithm %s' % algo)
+    elif ':' in algo:   # variable length hashing algorithms
+        _algo = algo.split(':')
+        base_algo = _algo[0]
+        variable_length = int(_algo[1])
+        h = hashlib.new('%s%s' % (base_algo, variable_length))
     else:
         h = hashlib.new(algo)
 
diff --git a/swh/model/tests/test_hashutil.py b/swh/model/tests/test_hashutil.py
index 435909de..8d14524d 100644
--- a/swh/model/tests/test_hashutil.py
+++ b/swh/model/tests/test_hashutil.py
@@ -8,7 +8,7 @@ import tempfile
 import unittest
 
 from nose.tools import istest
-from unittest.mock import MagicMock, patch
+from unittest.mock import patch
 
 from swh.model import hashutil
 
@@ -131,24 +131,11 @@ class Hashutil(unittest.TestCase):
             hashutil._new_hash('blake2:10')
         except ValueError as e:
             self.assertEquals(str(e),
-                              'Unsupported hashing algorithm blake2:10')
+                              'unsupported hash type blake210')
 
-    @patch('swh.model.hashutil.sys')
-    @istest
-    def new_hash_unexpected_hashing_algo(self, mock_sys):
-        mock_sys.version_info = MagicMock(major=3, minor=6)
-
-        try:
-            hashutil._new_hash('blake3:256')
-        except ValueError as e:
-            self.assertEquals(str(e),
-                              'unsupported hash type blake3256')
-
-    @patch('swh.model.hashutil.sys')
     @patch('swh.model.hashutil.hashlib')
     @istest
-    def new_hash_blake2b(self, mock_hashlib, mock_sys):
-        mock_sys.version_info = MagicMock(major=3, minor=6)
+    def new_hash_blake2b(self, mock_hashlib):
         mock_hashlib.new.return_value = 'some-hashlib-object'
 
         h = hashutil._new_hash('blake2b:512')
@@ -156,17 +143,14 @@ class Hashutil(unittest.TestCase):
         self.assertEquals(h, 'some-hashlib-object')
         mock_hashlib.new.assert_called_with('blake2b512')
 
-    @patch('swh.model.hashutil.sys')
     @patch('swh.model.hashutil.hashlib')
     @istest
-    def new_hash_blake2s(self, mock_hashlib, mock_sys):
-        mock_sys.version_info = MagicMock(major=3, minor=6)
+    def new_hash_blake2s(self, mock_hashlib):
         mock_hashlib.new.return_value = 'some-hashlib-object'
 
         h = hashutil._new_hash('blake2s:256')
 
         self.assertEquals(h, 'some-hashlib-object')
-        mock_hashlib.blake2s.assert_called_with(digest_size=128)
         mock_hashlib.new.assert_called_with('blake2s256')
 
 
-- 
GitLab