From 07f81ed878296a204ea64c763177beb8989bafdf Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 27 Dec 2018 09:56:01 +0530 Subject: [PATCH] Mark some string types as binary explicitly --- src/calibre/utils/chm/chm.py | 53 ++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/src/calibre/utils/chm/chm.py b/src/calibre/utils/chm/chm.py index 0bc5f19115..60ad23db5a 100644 --- a/src/calibre/utils/chm/chm.py +++ b/src/calibre/utils/chm/chm.py @@ -26,7 +26,6 @@ ''' import array -import string import sys import codecs @@ -249,7 +248,7 @@ class CHMFile: self.searchable = False self.lcid = None - result, ui = chmlib.chm_resolve_object(self.file, '/#SYSTEM') + result, ui = chmlib.chm_resolve_object(self.file, b'/#SYSTEM') if (result != chmlib.CHM_RESOLVE_SUCCESS): sys.stderr.write('GetArchiveInfo: #SYSTEM does not exist\n') return 0 @@ -269,17 +268,17 @@ class CHMFile: index += 2 cursor = buff[index] + (buff[index+1] * 256) index += 2 - self.topics = '/' + text[index:index+cursor-1] + self.topics = b'/' + text[index:index+cursor-1] elif cursor == 1: index += 2 cursor = buff[index] + (buff[index+1] * 256) index += 2 - self.index = '/' + text[index:index+cursor-1] + self.index = b'/' + text[index:index+cursor-1] elif cursor == 2: index += 2 cursor = buff[index] + (buff[index+1] * 256) index += 2 - self.home = '/' + text[index:index+cursor-1] + self.home = b'/' + text[index:index+cursor-1] elif cursor == 3: index += 2 cursor = buff[index] + (buff[index+1] * 256) @@ -296,16 +295,16 @@ class CHMFile: index += 2 tmp = text[index:index+cursor-1] if not self.topics: - tmp1 = '/' + tmp + '.hhc' - tmp2 = '/' + tmp + '.hhk' + tmp1 = b'/' + tmp + b'.hhc' + tmp2 = b'/' + tmp + b'.hhk' res1, ui1 = chmlib.chm_resolve_object(self.file, tmp1) res2, ui2 = chmlib.chm_resolve_object(self.file, tmp2) if not self.topics and \ res1 == chmlib.CHM_RESOLVE_SUCCESS: - self.topics = '/' + tmp + '.hhc' + self.topics = b'/' + tmp + b'.hhc' if not self.index and \ res2 == chmlib.CHM_RESOLVE_SUCCESS: - self.index = '/' + tmp + '.hhk' + self.index = b'/' + tmp + b'.hhk' elif cursor == 16: index += 2 cursor = buff[index] + (buff[index+1] * 256) @@ -421,7 +420,7 @@ class CHMFile: found, or if it is not possible to find the encoding, None is returned.''' if self.encoding: - vals = string.split(self.encoding, ',') + vals = self.encoding.split(b',') if len(vals) > 2: try: return charset_table[int(vals[2])] @@ -465,7 +464,7 @@ class CHMFile: '''Internal method. Retrieves a string from the #STRINGS buffer. ''' - next = string.find(text, '\x00', idx) + next = text.find(b'\x00', idx) chunk = text[idx:next] return chunk @@ -474,12 +473,12 @@ class CHMFile: Checks the #WINDOWS file to see if it has any info that was not found in #SYSTEM (topics, index or default page. ''' - result, ui = chmlib.chm_resolve_object(self.file, '/#WINDOWS') - if (result != chmlib.CHM_RESOLVE_SUCCESS): + result, ui = chmlib.chm_resolve_object(self.file, b'/#WINDOWS') + if result != chmlib.CHM_RESOLVE_SUCCESS: return -1 size, text = chmlib.chm_retrieve_object(self.file, ui, 0, 8) - if (size < 8): + if size < 8: return -2 buff = array.array('B', text) @@ -490,7 +489,7 @@ class CHMFile: return -3 size, text = chmlib.chm_retrieve_object(self.file, ui, 8, entry_size) - if (size < entry_size): + if size < entry_size: return -4 buff = array.array('B', text) @@ -498,25 +497,25 @@ class CHMFile: idx_index = self.GetDWORD(buff, 0x64) dft_index = self.GetDWORD(buff, 0x68) - result, ui = chmlib.chm_resolve_object(self.file, '/#STRINGS') - if (result != chmlib.CHM_RESOLVE_SUCCESS): + result, ui = chmlib.chm_resolve_object(self.file, b'/#STRINGS') + if result != chmlib.CHM_RESOLVE_SUCCESS: return -5 size, text = chmlib.chm_retrieve_object(self.file, ui, 0, ui.length) - if (size == 0): + if size == 0: return -6 - if (not self.topics): + if not self.topics: self.topics = self.GetString(text, toc_index) - if not self.topics.startswith("/"): - self.topics = "/" + self.topics + if not self.topics.startswith(b"/"): + self.topics = "b/" + self.topics - if (not self.index): + if not self.index: self.index = self.GetString(text, idx_index) - if not self.index.startswith("/"): - self.index = "/" + self.index + if not self.index.startswith(b"/"): + self.index = b"/" + self.index - if (dft_index != 0): + if dft_index != 0: self.home = self.GetString(text, dft_index) - if not self.home.startswith("/"): - self.home = "/" + self.home + if not self.home.startswith(b"/"): + self.home = b"/" + self.home