diff --git a/Obok_plugin/obok/obok.py b/Obok_plugin/obok/obok.py index a986965..4de7644 100644 --- a/Obok_plugin/obok/obok.py +++ b/Obok_plugin/obok/obok.py @@ -224,10 +224,17 @@ class SafeUnbuffered: if self.encoding == None: self.encoding = "utf-8" def write(self, data): - if isinstance(data,str): + if isinstance(data,str) or isinstance(data,unicode): + # str for Python3, unicode for Python2 data = data.encode(self.encoding,"replace") - self.stream.buffer.write(data) - self.stream.buffer.flush() + try: + buffer = getattr(self.stream, 'buffer', self.stream) + # self.stream.buffer for Python3, self.stream for Python2 + buffer.write(data) + buffer.flush() + except: + # We can do nothing if a write fails + raise def __getattr__(self, attr): return getattr(self.stream, attr) diff --git a/Other_Tools/Kobo/obok.py b/Other_Tools/Kobo/obok.py index ae08800..c26d422 100644 --- a/Other_Tools/Kobo/obok.py +++ b/Other_Tools/Kobo/obok.py @@ -276,12 +276,20 @@ class SafeUnbuffered: if self.encoding == None: self.encoding = "utf-8" def write(self, data): - if isinstance(data,unicode): + if isinstance(data,str) or isinstance(data,unicode): + # str for Python3, unicode for Python2 data = data.encode(self.encoding,"replace") - self.stream.write(data) - self.stream.flush() + try: + buffer = getattr(self.stream, 'buffer', self.stream) + # self.stream.buffer for Python3, self.stream for Python2 + buffer.write(data) + buffer.flush() + except: + # We can do nothing if a write fails + raise def __getattr__(self, attr): return getattr(self.stream, attr) + class KoboLibrary(object):