Skip to content

Commit

Permalink
🚑 fix unicode support on Python 2.7
Browse files Browse the repository at this point in the history
Fixes two UnicodeEncodeError's occurring when hashing
and writing files.

Closes #281
  • Loading branch information
jayvdb committed Jul 9, 2019
1 parent ec48af4 commit 10bebef
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
2 changes: 2 additions & 0 deletions moban/hashstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ def get_file_hash(afile):

def get_hash(content):
md5 = hashlib.md5()
if PY2 and content.__class__.__name__ == "unicode":
content = content.encode("utf-8")
md5.update(content)
return md5.digest().decode("latin1")

Expand Down
3 changes: 3 additions & 0 deletions moban/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from moban import constants, exceptions

log = logging.getLogger(__name__)
PY2 = sys.version_info[0] == 2


def merge(left, right):
Expand Down Expand Up @@ -60,6 +61,8 @@ def file_permissions(afile):


def write_file_out(filename, content):
if PY2 and content.__class__.__name__ == "unicode":
content = content.encode("utf-8")
dest_folder = os.path.dirname(filename)
if dest_folder:
mkdir_p(dest_folder)
Expand Down

0 comments on commit 10bebef

Please sign in to comment.