Always give hashlib.sha1 and friends bytes.

This fixes an experienced bug where you couldn't subscribe to a stream
with non-ASCII characters (failing with a UnicodeEncodeError), as well
as many other potential bugs.

(imported from commit f084a4b4b597b85935655097a7b5a163811c4d71)
This commit is contained in:
Jessica McKellar
2013-03-20 10:31:27 -04:00
parent 55240e3de2
commit 0c3382fabb
4 changed files with 27 additions and 7 deletions

View File

@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import hashlib
from time import sleep
# Runs the callback with slices of all_list of a given batch_size
@@ -21,3 +22,11 @@ def run_in_batches(all_list, batch_size, callback, sleep_time = 0, logger = None
if i != limit - 1:
sleep(sleep_time)
def make_safe_digest(string, hash_func=hashlib.sha1):
"""
return a hex digest of `string`.
"""
# hashlib.sha1, md5, etc. expect bytes, so non-ASCII strings must
# be encoded.
return hash_func(string.encode('utf-8')).hexdigest()