py3 compat: Fix PDB ereader output broken in calibre 5

Fixes #1954889 [Error when converting to pdb ereader](https://bugs.launchpad.net/calibre/+bug/1954889)
This commit is contained in:
Kovid Goyal 2021-12-15 13:36:24 +05:30
parent 2c6a73534a
commit 0a9778caa3
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 4 additions and 2 deletions

View File

@ -67,7 +67,7 @@ class Writer(FormatWriter):
lengths = [len(i) if i not in images else len(i[0]) + len(i[1]) for i in sections]
pdbHeaderBuilder = PdbHeaderBuilder(IDENTITY, metadata[0].partition('\x00')[0])
pdbHeaderBuilder = PdbHeaderBuilder(IDENTITY, metadata[0].partition(b'\x00')[0])
pdbHeaderBuilder.build_header(lengths, out_stream)
for item in sections:

View File

@ -70,7 +70,9 @@ class PdbHeaderBuilder:
def __init__(self, identity, title):
self.identity = identity.ljust(3, '\x00')[:8].encode('utf-8')
self.title = b'%s\x00' % re.sub('[^-A-Za-z0-9 ]+', '_', title).ljust(31, '\x00')[:31].encode('ascii', 'replace')
if isinstance(title, str):
title = title.encode('ascii', 'replace')
self.title = b'%s\x00' % re.sub(b'[^-A-Za-z0-9 ]+', b'_', title).ljust(31, b'\x00')[:31]
def build_header(self, section_lengths, out_stream):
'''