Skip to content

Commit

Permalink
Fix python3 issues with hashing (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktf authored Oct 20, 2016
1 parent b2a2d1d commit e3c8c61
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
5 changes: 3 additions & 2 deletions aliBuild
Original file line number Diff line number Diff line change
Expand Up @@ -778,8 +778,9 @@ def doMain():
h(str(time.time()))
if spec["package"] in develPkgs and "incremental_recipe" in spec:
h(spec["incremental_recipe"])
incremental_hash = hashlib.sha1(spec["incremental_recipe"]).hexdigest()
spec["incremental_hash"] = incremental_hash
ih = Hasher()
ih(spec["incremental_recipe"])
spec["incremental_hash"] = ih.hexdigest()
elif p in develPkgs:
h(spec.get("devel_hash"))
spec["hash"] = h.hexdigest()
Expand Down
4 changes: 3 additions & 1 deletion alibuild_helpers/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ class Hasher:
def __init__(self):
self.h = hashlib.sha1()
def __call__(self, txt):
self.h.update(txt.encode('utf-8', 'ignore'))
if not type(txt) == bytes:
txt = txt.encode('utf-8', 'ignore')
self.h.update(txt)
def hexdigest(self):
return self.h.hexdigest()
3 changes: 3 additions & 0 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,13 @@ def test_Hasher(self):
def test_UTF8_Hasher(self):
h1 = Hasher()
h2 = Hasher()
h3 = Hasher()
h1(u'\ua000')
h2(u'\ua001')
h3(b'foo')
self.assertEqual(h1.hexdigest(), "2af8e41129115eb231a0af76ec5465d3a9184fc4")
self.assertEqual(h2.hexdigest(), "1619bcdbeff6828138ad9b6e43cc17e856457603")
self.assertEqual(h3.hexdigest(), "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33")
self.assertNotEqual(h1.hexdigest(), h2.hexdigest())

if __name__ == '__main__':
Expand Down

0 comments on commit e3c8c61

Please sign in to comment.