Skip to content

Commit dca6581

Browse files
committed
Fix swift with python <2.7.9
Closes-Bug: #1831932 Change-Id: I0d33864f4bffa401082548ee9a52f6eb50cb1f39
1 parent 05bfaf7 commit dca6581

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

swift/common/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,8 @@ class StrAnonymizer(str):
649649

650650
def __new__(cls, data, method, salt):
651651
method = method.lower()
652-
if method not in hashlib.algorithms_guaranteed:
652+
if method not in (hashlib.algorithms if six.PY2 else
653+
hashlib.algorithms_guaranteed):
653654
raise ValueError('Unsupported hashing method: %r' % method)
654655
s = str.__new__(cls, data or '')
655656
s.method = method

test/unit/common/test_utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3565,6 +3565,22 @@ def test_str_anonymizer(self):
35653565
self.assertRaises(ValueError, utils.StrAnonymizer,
35663566
'Swift is great!', 'sha257', '')
35673567

3568+
def test_str_anonymizer_python_maddness(self):
3569+
with mock.patch('swift.common.utils.hashlib') as mocklib:
3570+
if six.PY2:
3571+
# python <2.7.9 doesn't have this algorithms_guaranteed, but
3572+
# our if block short-circuts before we explode
3573+
mocklib.algorithms = hashlib.algorithms
3574+
mocklib.algorithms_guaranteed.sideEffect = AttributeError()
3575+
else:
3576+
# python 3 doesn't have this algorithms but our if block
3577+
# short-circuts before we explode
3578+
mocklib.algorithms.sideEffect.sideEffect = AttributeError()
3579+
mocklib.algorithms_guaranteed = hashlib.algorithms_guaranteed
3580+
utils.StrAnonymizer('Swift is great!', 'sha1', '')
3581+
self.assertRaises(ValueError, utils.StrAnonymizer,
3582+
'Swift is great!', 'sha257', '')
3583+
35683584
def test_str_format_time(self):
35693585
dt = utils.StrFormatTime(10000.123456789)
35703586
self.assertEqual(str(dt), '10000.123456789')

0 commit comments

Comments
 (0)