summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNoDRM <[email protected]>2022-08-06 19:57:20 +0200
committerNoDRM <[email protected]>2022-08-06 19:57:20 +0200
commitb404605878391f56fc485bd0824c8c168a9c1a1a (patch)
treec8ed3f096cb4049629912fbb226d6dbfa81fce5a
parent1cc5d383ccd60269cc2296b7ecd94e5fbf85e3f3 (diff)
Another Python2 Bugfix for Obok
-rw-r--r--Obok_plugin/obok/obok.py13
-rw-r--r--Other_Tools/Kobo/obok.py14
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):