From 0c9d1762d1bc7172d9b5d3b80e87f9c0c2a20371 Mon Sep 17 00:00:00 2001 From: Yang Hongyang Date: Sat, 27 Feb 2016 21:20:15 +0800 Subject: [PATCH] Encode string before hash it Otherwise, in python3.4, it will throw error: b'TypeError: Unicode-objects must be encoded before hashing' Partially-Implements: blueprint magnum-python3 Change-Id: Ia8aff8f4b5634fd8abda0ec185e8d9dc58c11fcd --- magnum/common/utils.py | 2 +- magnum/tests/unit/common/test_utils.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/magnum/common/utils.py b/magnum/common/utils.py index 834e62f503..7c8910504b 100644 --- a/magnum/common/utils.py +++ b/magnum/common/utils.py @@ -354,7 +354,7 @@ def file_open(*args, **kwargs): def hash_file(file_like_object): """Generate a hash for the contents of a file.""" checksum = hashlib.sha1() - for chunk in iter(lambda: file_like_object.read(32768), b''): + for chunk in iter(lambda: six.b(file_like_object.read(32768)), b''): checksum.update(chunk) return checksum.hexdigest() diff --git a/magnum/tests/unit/common/test_utils.py b/magnum/tests/unit/common/test_utils.py index ce388cf60a..9fdc8f0c37 100644 --- a/magnum/tests/unit/common/test_utils.py +++ b/magnum/tests/unit/common/test_utils.py @@ -336,7 +336,7 @@ class GenericUtilsTestCase(base.TestCase): data = 'Mary had a little lamb, its fleece as white as snow' flo = six.StringIO(data) h1 = utils.hash_file(flo) - h2 = hashlib.sha1(data).hexdigest() + h2 = hashlib.sha1(six.b(data)).hexdigest() self.assertEqual(h1, h2) def test_is_valid_boolstr(self):