sendMsg時にエンコードエラー(※追記あり)

skpyでメッセージを送るときに下記のエラー発生。

Traceback (most recent call last):
  File "main.py", line 32, in 
    sk.chats[sk.contacts[user].chat.id].sendMsg("Hello")
  File "build/bdist.linux-x86_64/egg/skpy/chat.py", line 142, in sendMsg
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)


ソースを見てると下記の部分で相手の名前を引っ張ってくるときにエンコードエラーが発生しているようだ。

        def __str__(self):
            return " ".join(filter(None, (self.first, self.last)))


というわけで以下のように修正すれば動く。

diff --git a/skpy/user.py b/skpy/user.py
index 46ae450..c68d926 100644
--- a/skpy/user.py
+++ b/skpy/user.py
@@ -47,7 +47,7 @@ class SkypeUser(SkypeObj):
         attrs = ("first", "last")
 
         def __str__(self):
-            return " ".join(filter(None, (self.first, self.last)))
+            return u" ".join(filter(None, (self.first, self.last))).encode('utf-8')
 
     @SkypeUtils.initAttrs
     @SkypeUtils.truthyAttrs


追記 別の個所でも同じ現象が発生。対応する。※grepして修正するのめんどい。

Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "main.py", line 53, in run
    print(v)
  File "build/bdist.linux-x86_64/egg/skpy/core.py", line 90, in __str__
    valStr = ("\n".join(str(i) for i in value) if isinstance(value, list) else str(value))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)


diff --git a/skpy/core.py b/skpy/core.py
index a202b2a..10e9dcb 100644
--- a/skpy/core.py
+++ b/skpy/core.py
@@ -84,11 +84,11 @@ class SkypeObj(object):
 
         Nested objects are indented as needed.
         """
-        out = "[{0}]".format(self.__class__.__name__)
+        out = u"[{0}]".format(self.__class__.__name__).encode("utf-8")
         for attr in self.attrs:
             value = getattr(self, attr)
-            valStr = ("\n".join(str(i) for i in value) if isinstance(value, list) else str(value))
-            out += "\n{0}{1}: {2}".format(attr[0].upper(), attr[1:], valStr.replace("\n", "\n  " + (" " * len(attr))))
+            valStr = ("\n".join(unicode(i) for i in value) if isinstance(value, list) else unicode(value))
+            out += u"\n{0}{1}: {2}".format(attr[0].upper(), attr[1:], valStr.replace("\n", "\n  " + (" " * len(attr)))).encode("utf-8")
         return out