From 547baac5dc5c77a2f772a7b88101d434912fe1ce Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Mon, 25 Jul 2016 19:05:26 +0300 Subject: [PATCH] Fix encoding for Py3 compatibility --- m_lib/md5wrapper.py | 4 ++++ m_lib/opstring.py | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) 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"): -- 2.39.5