]> git.phdru.name Git - m_lib.git/commitdiff
Fix encoding for Py3 compatibility
authorOleg Broytman <phd@phdru.name>
Mon, 25 Jul 2016 16:05:26 +0000 (19:05 +0300)
committerOleg Broytman <phd@phdru.name>
Mon, 25 Jul 2016 18:36:49 +0000 (21:36 +0300)
m_lib/md5wrapper.py
m_lib/opstring.py

index d428d55586841491926fdace564260768fdb86ef..14013027f687356bf692cecee605bfdda9a0567d 100755 (executable)
@@ -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
 
index 8bd864acb0f3de2c07bf5b3047854c16cfb66b8e..0341bced97cfc82d8992b5ce10794aba95682798 100755 (executable)
@@ -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"):