From: Oleg Broytman Date: Mon, 25 Jul 2016 16:05:26 +0000 (+0300) Subject: Fix encoding for Py3 compatibility X-Git-Tag: 3.0.0b1~3 X-Git-Url: https://git.phdru.name/?p=m_lib.git;a=commitdiff_plain;h=547baac5dc5c77a2f772a7b88101d434912fe1ce Fix encoding for Py3 compatibility --- diff --git a/m_lib/md5wrapper.py b/m_lib/md5wrapper.py index d428d55..1401302 100755 --- a/m_lib/md5wrapper.py +++ b/m_lib/md5wrapper.py @@ -13,6 +13,8 @@ __all__ = ['md5wrapper'] class md5wrapper: def __init__(self, init=None): if init: + if not isinstance(init, bytes): + init = init.encode('ascii') self._md5 = md5(init) else: self._md5 = md5() @@ -25,6 +27,8 @@ class md5wrapper: def __repr__(self): str = self.digest() + if isinstance(str, bytes): + str = str.decode('latin1') return "%02x"*len(str) % tuple(map(ord, str)) # This nice was suggested by Guido diff --git a/m_lib/opstring.py b/m_lib/opstring.py index 8bd864a..0341bce 100755 --- a/m_lib/opstring.py +++ b/m_lib/opstring.py @@ -126,10 +126,10 @@ def translate_a(val, id): return transl_adict[id][val] -# Encodings, especially cyrillic. Requires Unicode, hence Python 2.0+ - def recode(s, from_encoding, to_encoding, errors = "strict"): - return unicode(s, from_encoding, errors).encode(to_encoding, errors) + if isinstance(s, bytes): + s = s.decode(from_encoding, errors) + return s.encode(to_encoding, errors) def win2koi(s, errors = "strict"):