Fix swift with python <2.7.9

Closes-Bug: #1831932

Change-Id: I0d33864f4bffa401082548ee9a52f6eb50cb1f39
This commit is contained in:
Clay Gerrard 2019-06-06 14:25:22 -05:00
parent 05bfaf7ae1
commit dca658103a
2 changed files with 18 additions and 1 deletions

View File

@ -649,7 +649,8 @@ class StrAnonymizer(str):
def __new__(cls, data, method, salt): def __new__(cls, data, method, salt):
method = method.lower() 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) raise ValueError('Unsupported hashing method: %r' % method)
s = str.__new__(cls, data or '') s = str.__new__(cls, data or '')
s.method = method s.method = method

View File

@ -3565,6 +3565,22 @@ cluster_dfw1 = http://dfw1.host/v1/
self.assertRaises(ValueError, utils.StrAnonymizer, self.assertRaises(ValueError, utils.StrAnonymizer,
'Swift is great!', 'sha257', '') '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): def test_str_format_time(self):
dt = utils.StrFormatTime(10000.123456789) dt = utils.StrFormatTime(10000.123456789)
self.assertEqual(str(dt), '10000.123456789') self.assertEqual(str(dt), '10000.123456789')