diff --git a/src/calibre/devices/kindle/apnx.py b/src/calibre/devices/kindle/apnx.py index d225c91b90..89b56bc03b 100644 --- a/src/calibre/devices/kindle/apnx.py +++ b/src/calibre/devices/kindle/apnx.py @@ -17,7 +17,7 @@ from calibre.ebooks.mobi.reader.headers import MetadataHeader from calibre.utils.logging import default_log from calibre import prints, fsync from calibre.constants import DEBUG -from polyglot.builtins import range +from polyglot.builtins import range, as_unicode, as_bytes class APNXBuilder(object): @@ -37,10 +37,10 @@ class APNXBuilder(object): with lopen(mobi_file_path, 'rb') as mf: ident = PdbHeaderReader(mf).identity() - if ident != 'BOOKMOBI': + if ident != b'BOOKMOBI': # Check that this is really a MOBI file. raise Exception(_('Not a valid MOBI file. Reports identity of %s') % ident) - apnx_meta['acr'] = str(PdbHeaderReader(mf).name()) + apnx_meta['acr'] = as_unicode(PdbHeaderReader(mf).name(), errors='replace') # We'll need the PDB name, the MOBI version, and some metadata to make FW 3.4 happy with KF8 files... with lopen(mobi_file_path, 'rb') as mf: @@ -92,7 +92,7 @@ class APNXBuilder(object): fsync(apnxf) def generate_apnx(self, pages, apnx_meta): - apnx = '' + apnx = b'' if DEBUG: prints('APNX META: guid:', apnx_meta['guid']) @@ -117,12 +117,12 @@ class APNXBuilder(object): apnx += struct.pack('>I', 65537) apnx += struct.pack('>I', 12 + len(content_header)) apnx += struct.pack('>I', len(content_header)) - apnx += content_header + apnx += as_bytes(content_header) apnx += struct.pack('>H', 1) apnx += struct.pack('>H', len(page_header)) apnx += struct.pack('>H', len(pages)) apnx += struct.pack('>H', 32) - apnx += page_header + apnx += as_bytes(page_header) # Write page values to APNX. for page in pages: diff --git a/src/calibre/devices/usbms/driver.py b/src/calibre/devices/usbms/driver.py index 806d8bd3a1..5b204904ee 100644 --- a/src/calibre/devices/usbms/driver.py +++ b/src/calibre/devices/usbms/driver.py @@ -354,6 +354,7 @@ class USBMS(CLI, Device): debug_print('USBMS: adding metadata for %d books'%(len(metadata))) metadata = iter(metadata) + locations = tuple(locations) for i, location in enumerate(locations): self.report_progress((i+1) / float(len(locations)), _('Adding books to device metadata listing...')) info = next(metadata) diff --git a/src/calibre/ebooks/pdb/header.py b/src/calibre/ebooks/pdb/header.py index 6e954843e1..86ae4d3bcc 100644 --- a/src/calibre/ebooks/pdb/header.py +++ b/src/calibre/ebooks/pdb/header.py @@ -32,7 +32,7 @@ class PdbHeaderReader(object): def name(self): self.stream.seek(0) - return re.sub('[^-A-Za-z0-9 ]+', '_', self.stream.read(32).replace('\x00', '')) + return re.sub(b'[^-A-Za-z0-9 ]+', b'_', self.stream.read(32).replace(b'\x00', b'')) def full_section_info(self, number): if number not in range(0, self.num_sections):