From 660b4233aaa8636dd088ffa5b8ed5ebb29b5112b Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Tue, 16 Apr 2019 17:53:15 -0400 Subject: [PATCH 1/3] py3: remove unneeded use of cmp No need to carry around a polyglot implementation of cmp here, when sort(key=) works too. Fixes unnoticed error due to .sort(function) being implied as cmp= on python2, and not being permitted a positional argument on python3. --- src/calibre/ebooks/snb/snbfile.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/calibre/ebooks/snb/snbfile.py b/src/calibre/ebooks/snb/snbfile.py index 5a09652ccf..34c07a0cb0 100644 --- a/src/calibre/ebooks/snb/snbfile.py +++ b/src/calibre/ebooks/snb/snbfile.py @@ -8,7 +8,7 @@ __docformat__ = 'restructuredtext en' import sys, struct, zlib, bz2, os from calibre import guess_type -from polyglot.builtins import unicode_type, cmp +from polyglot.builtins import unicode_type class FileStream: @@ -17,10 +17,6 @@ class FileStream: return self.attr & 0x41000000 != 0x41000000 -def compareFileStream(file1, file2): - return cmp(file1.fileName, file2.fileName) - - class BlockData: pass @@ -200,7 +196,7 @@ class SNBFile: # Sort the files in file buffer, # requried by the SNB file format - self.files.sort(compareFileStream) + self.files.sort(key=lambda x: x.fileName) outputFile = open(outputFile, 'wb') # File header part 1 From 6583e1e2fbbe34bfd9f0deb568ec8599afe5007e Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Tue, 16 Apr 2019 18:24:18 -0400 Subject: [PATCH 2/3] don't print exceptions directly, use traceback.print_exc() instead This makes debugging actually useful, by seeing what failed. --- src/calibre/ebooks/snb/snbfile.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/calibre/ebooks/snb/snbfile.py b/src/calibre/ebooks/snb/snbfile.py index 34c07a0cb0..5c2ddd57f2 100644 --- a/src/calibre/ebooks/snb/snbfile.py +++ b/src/calibre/ebooks/snb/snbfile.py @@ -89,8 +89,9 @@ class SNBFile: uncompressedData += bzdc.decompress(data) else: uncompressedData += data - except Exception as e: - print(e) + except Exception: + import traceback + print(traceback.print_exc()) if len(uncompressedData) != self.plainStreamSizeUncompressed: raise Exception() f.fileBody = uncompressedData[plainPos:plainPos+f.fileSize] From 7fbf985870b70b7a13efec5ce069b95196852f87 Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Tue, 16 Apr 2019 18:36:03 -0400 Subject: [PATCH 3/3] py3: make SNB output work variables initialized as implicit bytes '' on python2, should be b'' python2 used integer division, make this explicit on python3 --- src/calibre/ebooks/snb/snbfile.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/calibre/ebooks/snb/snbfile.py b/src/calibre/ebooks/snb/snbfile.py index 5c2ddd57f2..35feb04ee4 100644 --- a/src/calibre/ebooks/snb/snbfile.py +++ b/src/calibre/ebooks/snb/snbfile.py @@ -23,7 +23,7 @@ class BlockData: class SNBFile: - MAGIC = 'SNBP000B' + MAGIC = b'SNBP000B' REV80 = 0x00008000 REVA3 = 0x00A3A3A3 REVZ1 = 0x00000000 @@ -75,7 +75,7 @@ class SNBFile: if f.attr & 0x41000000 == 0x41000000: # Compressed Files if uncompressedData is None: - uncompressedData = "" + uncompressedData = b"" for i in range(self.plainBlock): bzdc = bz2.BZ2Decompressor() if (i < self.plainBlock - 1): @@ -106,7 +106,7 @@ class SNBFile: raise Exception("Invalid file") def ParseFile(self, vfat, fileCount): - fileNames = vfat[fileCount*12:].split('\0') + fileNames = vfat[fileCount*12:].split(b'\0') for i in range(fileCount): f = FileStream() (f.attr, f.fileNameOffset, f.fileSize) = struct.unpack('>iii', vfat[i * 12 : (i+1)*12]) @@ -114,8 +114,8 @@ class SNBFile: self.files.append(f) def ParseTail(self, vtail, fileCount): - self.binBlock = (self.binStreamSize + 0x8000 - 1) / 0x8000 - self.plainBlock = (self.plainStreamSizeUncompressed + 0x8000 - 1) / 0x8000 + self.binBlock = (self.binStreamSize + 0x8000 - 1) // 0x8000 + self.plainBlock = (self.plainStreamSizeUncompressed + 0x8000 - 1) // 0x8000 for i in range(self.binBlock + self.plainBlock): block = BlockData() (block.Offset,) = struct.unpack('>i', vtail[i * 4 : (i+1) * 4]) @@ -204,13 +204,13 @@ class SNBFile: vmbrp1 = struct.pack('>8siiii', SNBFile.MAGIC, SNBFile.REV80, SNBFile.REVA3, SNBFile.REVZ1, len(self.files)) # Create VFAT & file stream - vfat = '' - fileNameTable = '' - plainStream = '' - binStream = '' + vfat = b'' + fileNameTable = b'' + plainStream = b'' + binStream = b'' for f in self.files: vfat += struct.pack('>iii', f.attr, len(fileNameTable), f.fileSize) - fileNameTable += (f.fileName + '\0') + fileNameTable += (f.fileName + b'\0') if f.attr & 0x41000000 == 0x41000000: # Plain Files @@ -236,22 +236,22 @@ class SNBFile: binBlockOffset = 0x2C + len(vfatCompressed) plainBlockOffset = binBlockOffset + len(binStream) - binBlock = (len(binStream) + 0x8000 - 1) / 0x8000 + binBlock = (len(binStream) + 0x8000 - 1) // 0x8000 # plainBlock = (len(plainStream) + 0x8000 - 1) / 0x8000 offset = 0 - tailBlock = '' + tailBlock = b'' for i in range(binBlock): tailBlock += struct.pack('>i', binBlockOffset + offset) offset += 0x8000 - tailRec = '' + tailRec = b'' for f in self.files: t = 0 if f.IsBinary(): t = 0 else: t = binBlock - tailRec += struct.pack('>ii', f.contentOffset / 0x8000 + t, f.contentOffset % 0x8000) + tailRec += struct.pack('>ii', f.contentOffset // 0x8000 + t, f.contentOffset % 0x8000) # Write binary stream outputFile.write(binStream)