py3: port a bunch of places where individual byes are accessed from a bytestring

This commit is contained in:
Kovid Goyal 2019-07-12 05:52:07 +05:30
parent 1b0ce5c17f
commit 36ff1b15b8
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
11 changed files with 26 additions and 22 deletions

View File

@ -43,8 +43,8 @@ class TCRCompressor(object):
possible_codes.append(single_code.pop())
for code in possible_codes:
self.coded_txt = self.coded_txt.replace(code, code[0])
self.codes[ord(code[0])] = b'%s%s' % (self.codes[ord(code[0])], self.codes[ord(code[1])])
self.coded_txt = self.coded_txt.replace(code, code[0:1])
self.codes[ord(code[0:1])] = b'%s%s' % (self.codes[ord(code[0:1])], self.codes[ord(code[1:2])])
def _free_unused_codes(self):
'''

View File

@ -84,9 +84,9 @@ class DjvuChunk(object):
if not res.strip(b'\0'):
raise ValueError('TXTz block is completely null')
l = 0
for x in res[:3]:
for x in bytearray(res[:3]):
l <<= 8
l += ord(x)
l += x
if verbose > 0 and out:
print(l, file=out)
txtout.write(res[3:3+l])
@ -94,9 +94,9 @@ class DjvuChunk(object):
if txtout and self.type == b'TXTa':
res = self.buf[self.datastart: self.dataend]
l = 0
for x in res[:3]:
for x in bytearray(res[:3]):
l <<= 8
l += ord(x)
l += x
if verbose > 0 and out:
print(l, file=out)
txtout.write(res[3:3+l])

View File

@ -871,7 +871,7 @@ class LitFile(object):
for i in range(1, nentries + 1):
if len(data) <= 1:
break
size, data = ord(data[0]), data[1:]
size, data = ord(data[0:1]), data[1:]
if size == 0 or len(data) < size:
break
tags[i], data = data[:size], data[size:]

View File

@ -17,7 +17,7 @@ from calibre.utils.config_base import tweaks
from calibre.utils.date import parse_only_date
from calibre.utils.localization import canonicalize_lang
from calibre.utils.imghdr import identify
from polyglot.builtins import unicode_type, filter
from polyglot.builtins import unicode_type, filter, map
from polyglot.binary import as_base64_bytes, from_base64_bytes
@ -62,7 +62,11 @@ COVER_KEY = "cover_image_base64"
def hexs(string, sep=' '):
return sep.join('%02x' % ord(b) for b in string)
if isinstance(string, bytes):
string = bytearray(string)
else:
string = map(ord, string)
return sep.join('%02x' % b for b in string)
class PackedData(object):

View File

@ -277,9 +277,9 @@ class MetadataUpdater(object):
offset += consumed
self.md_header['tag'] = self.data[offset:offset+taglen]
offset += taglen
self.md_header['flags'] = ord(self.data[offset])
self.md_header['flags'] = ord(self.data[offset:offset+1])
offset += 1
self.md_header['num_recs'] = ord(self.data[offset])
self.md_header['num_recs'] = ord(self.data[offset:offset+1])
offset += 1
# print "self.md_header: %s" % self.md_header

View File

@ -79,7 +79,7 @@ class SecondaryIndexHeader(object): # {{{
raise ValueError('TAGX last entry is not EOF')
idxt0_pos = self.header_length+self.tagx_header_length
num = ord(raw[idxt0_pos])
num = ord(raw[idxt0_pos:idxt0_pos+1])
count_pos = idxt0_pos+1+num
self.last_entry = raw[idxt0_pos+1:count_pos]
self.ncx_count, = struct.unpack(b'>H', raw[count_pos:count_pos+2])

View File

@ -496,11 +496,11 @@ class Reader(FormatReader):
html += u'<p>'
paragraph_open = True
c = ord(d[offset])
c = ord(d[offset:offset+1])
# PHTML "functions"
if c == 0x0:
offset += 1
c = ord(d[offset])
c = ord(d[offset:offset+1])
# Page link begins
# 2 Bytes
# record ID

View File

@ -333,7 +333,7 @@ class SearchFilter(SearchQueryParser):
def __init__(self):
SearchQueryParser.__init__(self, locations=self.USABLE_LOCATIONS)
self.srs = set([])
self.srs = set()
# remove joiner words surrounded by space or at string boundaries
self.joiner_pat = re.compile(r'(^|\s)(and|not|or|a|the|is|of)(\s|$)', re.IGNORECASE)
self.punctuation_table = {ord(x):' ' for x in string.punctuation}

View File

@ -41,7 +41,7 @@ def unpickle_binary_string(data):
sz, = struct.unpack_from('<i', data, offset)
offset += struct.calcsize('<i')
elif which == SHORT_BINSTRING:
sz = ord(data[offset])
sz = ord(data[offset:offset+1])
offset += 1
else:
return

View File

@ -36,11 +36,11 @@ class ByteCode(dict):
return b0 - 139, index
def read_small_int1(self, b0, data, index):
b1 = ord(data[index])
b1 = ord(data[index:index+1])
return (b0-247)*256 + b1 + 108, index+1
def read_small_int2(self, b0, data, index):
b1 = ord(data[index])
b1 = ord(data[index:index+1])
return -(b0-251)*256 - b1 - 108, index+1
def read_short_int(self, b0, data, index):
@ -61,7 +61,7 @@ class ByteCode(dict):
def read_real_number(self, b0, data, index):
number = ''
while True:
b = ord(data[index])
b = ord(data[index:index+1])
index = index + 1
nibble0 = (b & 0xf0) >> 4
nibble1 = b & 0x0f
@ -144,7 +144,7 @@ class Dict(ByteCode):
self.stack = []
index = 0
while index < len(data):
b0 = ord(data[index])
b0 = ord(data[index:index+1])
index += 1
handler = getattr(self, self.operand_encoding[b0])
value, index = handler(b0, data, index)
@ -153,7 +153,7 @@ class Dict(ByteCode):
def do_operator(self, b0, data, index):
if b0 == 12:
op = (b0, ord(data[index]))
op = (b0, ord(data[index:index+1]))
index += 1
else:
op = b0

View File

@ -18,7 +18,7 @@ from css_selectors.errors import SelectorSyntaxError, ExpressionError
from polyglot.builtins import unicode_type, codepoint_to_chr, range
utab = {c:c+32 for c in range(ord('A'), ord('Z')+1)}
utab = {c:c+32 for c in range(ord(u'A'), ord(u'Z')+1)}
if sys.version_info.major < 3:
tab = string.maketrans(string.ascii_uppercase, string.ascii_lowercase)