diff --git a/src/calibre/ebooks/lrf/objects.py b/src/calibre/ebooks/lrf/objects.py index b04689feec..bde732f906 100644 --- a/src/calibre/ebooks/lrf/objects.py +++ b/src/calibre/ebooks/lrf/objects.py @@ -32,7 +32,7 @@ class LRFObject: a[i] ^= xorKey i+=1 l-=1 - return a.tostring() + return a.tobytes() @classmethod def parse_empdots(self, tag, f): @@ -799,8 +799,9 @@ class Text(LRFStream): def initialize(self): self.content = collections.deque() - stream = io.BytesIO(self.stream) - length = len(self.stream) + s = self.stream or b'' + stream = io.BytesIO(s) + length = len(s) style = self.style.as_dict() current_style = style.copy() text_tags = set(list(TextAttr.tag_map.keys()) + @@ -813,7 +814,7 @@ class Text(LRFStream): # Is there some text before a tag? def find_first_tag(start): - pos = self.stream.find(b'\xf5', start) + pos = s.find(b'\xf5', start) if pos == -1: return -1 try: @@ -830,10 +831,10 @@ class Text(LRFStream): tag_pos = find_first_tag(start_pos) if tag_pos >= start_pos: if tag_pos > start_pos: - self.add_text(self.stream[start_pos:tag_pos]) + self.add_text(s[start_pos:tag_pos]) stream.seek(tag_pos) else: # No tags in this stream - self.add_text(self.stream) + self.add_text(s) stream.seek(0, 2) break @@ -969,8 +970,9 @@ class Canvas(LRFStream): if hasattr(self, attr): self.attrs[attr] = getattr(self, attr) self._contents = [] - stream = io.BytesIO(self.stream) - while stream.tell() < len(self.stream): + s = self.stream or b'' + stream = io.BytesIO(s) + while stream.tell() < len(s): tag = Tag(stream) try: self._contents.append( @@ -1202,7 +1204,7 @@ class TocLabel: class TOCObject(LRFStream): def initialize(self): - stream = io.BytesIO(self.stream) + stream = io.BytesIO(self.stream or b'') c = struct.unpack("