From b404605878391f56fc485bd0824c8c168a9c1a1a Mon Sep 17 00:00:00 2001 From: NoDRM Date: Sat, 6 Aug 2022 19:57:20 +0200 Subject: [PATCH] Another Python2 Bugfix for Obok --- Obok_plugin/obok/obok.py | 13 ++++++++++--- Other_Tools/Kobo/obok.py | 14 +++++++++++--- 2 files changed, 21 insertions(+), 6 deletions(-) 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):