From dca658103a63d212bdf9195fcde6038557c13401 Mon Sep 17 00:00:00 2001 From: Clay Gerrard Date: Thu, 6 Jun 2019 14:25:22 -0500 Subject: [PATCH] Fix swift with python <2.7.9 Closes-Bug: #1831932 Change-Id: I0d33864f4bffa401082548ee9a52f6eb50cb1f39 --- swift/common/utils.py | 3 ++- test/unit/common/test_utils.py | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/swift/common/utils.py b/swift/common/utils.py index 336f039adf..acd6d161ed 100644 --- a/swift/common/utils.py +++ b/swift/common/utils.py @@ -649,7 +649,8 @@ class StrAnonymizer(str): def __new__(cls, data, method, salt): method = method.lower() - if method not in hashlib.algorithms_guaranteed: + if method not in (hashlib.algorithms if six.PY2 else + hashlib.algorithms_guaranteed): raise ValueError('Unsupported hashing method: %r' % method) s = str.__new__(cls, data or '') s.method = method diff --git a/test/unit/common/test_utils.py b/test/unit/common/test_utils.py index bc9b3cd664..9dbbadcf89 100644 --- a/test/unit/common/test_utils.py +++ b/test/unit/common/test_utils.py @@ -3565,6 +3565,22 @@ cluster_dfw1 = http://dfw1.host/v1/ self.assertRaises(ValueError, utils.StrAnonymizer, 'Swift is great!', 'sha257', '') + def test_str_anonymizer_python_maddness(self): + with mock.patch('swift.common.utils.hashlib') as mocklib: + if six.PY2: + # python <2.7.9 doesn't have this algorithms_guaranteed, but + # our if block short-circuts before we explode + mocklib.algorithms = hashlib.algorithms + mocklib.algorithms_guaranteed.sideEffect = AttributeError() + else: + # python 3 doesn't have this algorithms but our if block + # short-circuts before we explode + mocklib.algorithms.sideEffect.sideEffect = AttributeError() + mocklib.algorithms_guaranteed = hashlib.algorithms_guaranteed + utils.StrAnonymizer('Swift is great!', 'sha1', '') + self.assertRaises(ValueError, utils.StrAnonymizer, + 'Swift is great!', 'sha257', '') + def test_str_format_time(self): dt = utils.StrFormatTime(10000.123456789) self.assertEqual(str(dt), '10000.123456789')