Improve compatibility with hashlib Py2/Py3

This commit is contained in:
Ivan Kravets
2019-06-03 13:30:35 +03:00
parent ac3236693f
commit e269c91d26
11 changed files with 68 additions and 45 deletions

View File

@@ -35,12 +35,21 @@ if PY2:
return isinstance(x, (buffer, bytearray))
def path_to_unicode(path):
if isinstance(path, unicode):
return path
return path.decode(get_filesystem_encoding()).encode("utf-8")
def get_file_contents(path):
with open(path) as f:
return f.read()
def hashlib_encode_data(data):
if is_bytes(data):
return data
if not isinstance(data, string_types):
data = str(data)
return data
_magic_check = re.compile('([*?[])')
_magic_check_bytes = re.compile(b'([*?[])')
@@ -74,3 +83,10 @@ else:
except UnicodeDecodeError:
with open(path, encoding="latin-1") as f:
return f.read()
def hashlib_encode_data(data):
if is_bytes(data):
return data
if not isinstance(data, string_types):
data = str(data)
return data.encode()