Add string expressions to asserts showing local data (#67)

pull/69/head^2
Hugh Secker-Walker 2017-05-29 03:06:09 -04:00 committed by Goulu
parent fe21725f07
commit 488545ddc7
12 changed files with 52 additions and 55 deletions

View File

@ -35,7 +35,7 @@ def ascii85decode(data):
out += struct.pack('>L', b) out += struct.pack('>L', b)
n = b = 0 n = b = 0
elif c == b'z': elif c == b'z':
assert n == 0 assert n == 0, str(n)
out += b'\0\0\0\0' out += b'\0\0\0\0'
elif c == b'~': elif c == b'~':
if n: if n:
@ -63,11 +63,11 @@ def asciihexdecode(data):
def decode(x): def decode(x):
i=int(x,16) i=int(x,16)
return six.int2byte(i) return six.int2byte(i)
out=b'' out=b''
for x in hex_re.findall(data): for x in hex_re.findall(data):
out+=decode(x) out+=decode(x)
m = trail_re.search(data) m = trail_re.search(data)
if m: if m:
out+=decode(m.group(1)+b'0') out+=decode(m.group(1)+b'0')

View File

@ -80,7 +80,7 @@ class CMap(CMapBase):
return '<CMap: %s>' % self.attrs.get('CMapName') return '<CMap: %s>' % self.attrs.get('CMapName')
def use_cmap(self, cmap): def use_cmap(self, cmap):
assert isinstance(cmap, CMap) assert isinstance(cmap, CMap), str(type(cmap))
def copy(dst, src): def copy(dst, src):
for (k, v) in src.iteritems(): for (k, v) in src.iteritems():
@ -158,7 +158,7 @@ class UnicodeMap(CMapBase):
class FileCMap(CMap): class FileCMap(CMap):
def add_code2cid(self, code, cid): def add_code2cid(self, code, cid):
assert isinstance(code, str) and isinstance(cid, int) assert isinstance(code, str) and isinstance(cid, int), str((type(code), type(cid)))
d = self.code2cid d = self.code2cid
for c in code[:-1]: for c in code[:-1]:
c = ord(c) c = ord(c)
@ -178,7 +178,7 @@ class FileCMap(CMap):
class FileUnicodeMap(UnicodeMap): class FileUnicodeMap(UnicodeMap):
def add_cid2unichr(self, cid, code): def add_cid2unichr(self, cid, code):
assert isinstance(cid, int) assert isinstance(cid, int), str(type(cid))
if isinstance(code, PSLiteral): if isinstance(code, PSLiteral):
# Interpret as an Adobe glyph name. # Interpret as an Adobe glyph name.
self.cid2unichr[cid] = name2unicode(code.name) self.cid2unichr[cid] = name2unicode(code.name)
@ -304,7 +304,7 @@ class CMapParser(PSStackParser):
KEYWORD_ENDBFCHAR = KWD(b'endbfchar') KEYWORD_ENDBFCHAR = KWD(b'endbfchar')
KEYWORD_BEGINNOTDEFRANGE = KWD(b'beginnotdefrange') KEYWORD_BEGINNOTDEFRANGE = KWD(b'beginnotdefrange')
KEYWORD_ENDNOTDEFRANGE = KWD(b'endnotdefrange') KEYWORD_ENDNOTDEFRANGE = KWD(b'endnotdefrange')
def do_keyword(self, pos, token): def do_keyword(self, pos, token):
if token is self.KEYWORD_BEGINCMAP: if token is self.KEYWORD_BEGINCMAP:
self._in_cmap = True self._in_cmap = True
@ -359,7 +359,7 @@ class CMapParser(PSStackParser):
s1 = nunpack(svar) s1 = nunpack(svar)
e1 = nunpack(evar) e1 = nunpack(evar)
vlen = len(svar) vlen = len(svar)
#assert s1 <= e1 #assert s1 <= e1, str((s1, e1))
for i in range(e1-s1+1): for i in range(e1-s1+1):
x = sprefix+struct.pack('>L', s1+i)[-vlen:] x = sprefix+struct.pack('>L', s1+i)[-vlen:]
self.cmap.add_code2cid(x, cid+i) self.cmap.add_code2cid(x, cid+i)
@ -386,7 +386,7 @@ class CMapParser(PSStackParser):
continue continue
s1 = nunpack(s) s1 = nunpack(s)
e1 = nunpack(e) e1 = nunpack(e)
#assert s1 <= e1 #assert s1 <= e1, str((s1, e1))
if isinstance(code, list): if isinstance(code, list):
for i in range(e1-s1+1): for i in range(e1-s1+1):
self.cmap.add_cid2unichr(s1+i, code[i]) self.cmap.add_cid2unichr(s1+i, code[i])

View File

@ -48,8 +48,8 @@ class PDFLayoutAnalyzer(PDFTextDevice):
return return
def end_page(self, page): def end_page(self, page):
assert not self._stack assert not self._stack, str(len(stack))
assert isinstance(self.cur_item, LTPage) assert isinstance(self.cur_item, LTPage), str(type(self.cur_item))
if self.laparams is not None: if self.laparams is not None:
self.cur_item.analyze(self.laparams) self.cur_item.analyze(self.laparams)
self.pageno += 1 self.pageno += 1
@ -63,13 +63,13 @@ class PDFLayoutAnalyzer(PDFTextDevice):
def end_figure(self, _): def end_figure(self, _):
fig = self.cur_item fig = self.cur_item
assert isinstance(self.cur_item, LTFigure) assert isinstance(self.cur_item, LTFigure), str(type(self.cur_item))
self.cur_item = self._stack.pop() self.cur_item = self._stack.pop()
self.cur_item.add(fig) self.cur_item.add(fig)
return return
def render_image(self, name, stream): def render_image(self, name, stream):
assert isinstance(self.cur_item, LTFigure) assert isinstance(self.cur_item, LTFigure), str(type(self.cur_item))
item = LTImage(name, stream, item = LTImage(name, stream,
(self.cur_item.x0, self.cur_item.y0, (self.cur_item.x0, self.cur_item.y0,
self.cur_item.x1, self.cur_item.y1)) self.cur_item.x1, self.cur_item.y1))
@ -115,7 +115,7 @@ class PDFLayoutAnalyzer(PDFTextDevice):
def render_char(self, matrix, font, fontsize, scaling, rise, cid): def render_char(self, matrix, font, fontsize, scaling, rise, cid):
try: try:
text = font.to_unichr(cid) text = font.to_unichr(cid)
assert isinstance(text, six.text_type), text assert isinstance(text, six.text_type), str(type(text))
except PDFUnicodeNotDefined: except PDFUnicodeNotDefined:
text = self.handle_undefined_char(font, cid) text = self.handle_undefined_char(font, cid)
textwidth = font.char_width(cid) textwidth = font.char_width(cid)
@ -535,7 +535,7 @@ class XMLConverter(PDFConverter):
self.write('<image width="%d" height="%d" />\n' % self.write('<image width="%d" height="%d" />\n' %
(item.width, item.height)) (item.width, item.height))
else: else:
assert 0, item assert False, str(('Unhandled', item))
return return
render(ltpage) render(ltpage)
return return

View File

@ -34,9 +34,9 @@ class BMPWriter(object):
self.datasize = self.linesize * self.height self.datasize = self.linesize * self.height
headersize = 14+40+ncols*4 headersize = 14+40+ncols*4
info = struct.pack('<IiiHHIIIIII', 40, self.width, self.height, 1, self.bits, 0, self.datasize, 0, 0, ncols, 0) info = struct.pack('<IiiHHIIIIII', 40, self.width, self.height, 1, self.bits, 0, self.datasize, 0, 0, ncols, 0)
assert len(info) == 40, len(info) assert len(info) == 40, str(len(info))
header = struct.pack('<ccIHHI', b'B', b'M', headersize+self.datasize, 0, 0, headersize) header = struct.pack('<ccIHHI', b'B', b'M', headersize+self.datasize, 0, 0, headersize)
assert len(header) == 14, len(header) assert len(header) == 14, str(len(header))
self.fp.write(header) self.fp.write(header)
self.fp.write(info) self.fp.write(info)
if ncols == 2: if ncols == 2:

View File

@ -114,36 +114,36 @@ class LTComponent(LTItem):
return self.width <= 0 or self.height <= 0 return self.width <= 0 or self.height <= 0
def is_hoverlap(self, obj): def is_hoverlap(self, obj):
assert isinstance(obj, LTComponent) assert isinstance(obj, LTComponent), str(type(obj))
return obj.x0 <= self.x1 and self.x0 <= obj.x1 return obj.x0 <= self.x1 and self.x0 <= obj.x1
def hdistance(self, obj): def hdistance(self, obj):
assert isinstance(obj, LTComponent) assert isinstance(obj, LTComponent), str(type(obj))
if self.is_hoverlap(obj): if self.is_hoverlap(obj):
return 0 return 0
else: else:
return min(abs(self.x0-obj.x1), abs(self.x1-obj.x0)) return min(abs(self.x0-obj.x1), abs(self.x1-obj.x0))
def hoverlap(self, obj): def hoverlap(self, obj):
assert isinstance(obj, LTComponent) assert isinstance(obj, LTComponent), str(type(obj))
if self.is_hoverlap(obj): if self.is_hoverlap(obj):
return min(abs(self.x0-obj.x1), abs(self.x1-obj.x0)) return min(abs(self.x0-obj.x1), abs(self.x1-obj.x0))
else: else:
return 0 return 0
def is_voverlap(self, obj): def is_voverlap(self, obj):
assert isinstance(obj, LTComponent) assert isinstance(obj, LTComponent), str(type(obj))
return obj.y0 <= self.y1 and self.y0 <= obj.y1 return obj.y0 <= self.y1 and self.y0 <= obj.y1
def vdistance(self, obj): def vdistance(self, obj):
assert isinstance(obj, LTComponent) assert isinstance(obj, LTComponent), str(type(obj))
if self.is_voverlap(obj): if self.is_voverlap(obj):
return 0 return 0
else: else:
return min(abs(self.y0-obj.y1), abs(self.y1-obj.y0)) return min(abs(self.y0-obj.y1), abs(self.y1-obj.y0))
def voverlap(self, obj): def voverlap(self, obj):
assert isinstance(obj, LTComponent) assert isinstance(obj, LTComponent), str(type(obj))
if self.is_voverlap(obj): if self.is_voverlap(obj):
return min(abs(self.y0-obj.y1), abs(self.y1-obj.y0)) return min(abs(self.y0-obj.y1), abs(self.y1-obj.y0))
else: else:
@ -603,7 +603,7 @@ class LTLayoutContainer(LTContainer):
# group_textboxes: group textboxes hierarchically. # group_textboxes: group textboxes hierarchically.
def group_textboxes(self, laparams, boxes): def group_textboxes(self, laparams, boxes):
assert boxes assert boxes, str((laparams, boxes))
def dist(obj1, obj2): def dist(obj1, obj2):
"""A distance function between two TextBoxes. """A distance function between two TextBoxes.
@ -666,7 +666,7 @@ class LTLayoutContainer(LTContainer):
dists.append((0, dist(group, other), group, other)) dists.append((0, dist(group, other), group, other))
dists = csort(dists, key=key_obj) dists = csort(dists, key=key_obj)
plane.add(group) plane.add(group)
assert len(plane) == 1 assert len(plane) == 1, str(len(plane))
return list(plane) return list(plane)
def analyze(self, laparams): def analyze(self, laparams):

View File

@ -171,7 +171,7 @@ class TagExtractor(PDFDevice):
return return
def end_tag(self): def end_tag(self):
assert self._stack assert self._stack, str(self.pageno)
tag = self._stack.pop(-1) tag = self._stack.pop(-1)
out_s = '</%s>' % utils.enc(tag.name) out_s = '</%s>' % utils.enc(tag.name)
self.outfp.write(utils.make_compat_bytes(out_s)) self.outfp.write(utils.make_compat_bytes(out_s))

View File

@ -136,7 +136,7 @@ class PDFXRef(PDFBaseXRef):
def load_trailer(self, parser): def load_trailer(self, parser):
try: try:
(_, kwd) = parser.nexttoken() (_, kwd) = parser.nexttoken()
assert kwd is KWD(b'trailer') assert kwd is KWD(b'trailer'), str(kwd)
(_, dic) = parser.nextobject() (_, dic) = parser.nextobject()
except PSEOF: except PSEOF:
x = parser.pop(1) x = parser.pop(1)
@ -571,7 +571,7 @@ class PDFDocument(object):
continue continue
# If there's an encryption info, remember it. # If there's an encryption info, remember it.
if 'Encrypt' in trailer: if 'Encrypt' in trailer:
#assert not self.encryption #assert not self.encryption, str(self.encryption)
self.encryption = (list_value(trailer['ID']), self.encryption = (list_value(trailer['ID']),
dict_value(trailer['Encrypt'])) dict_value(trailer['Encrypt']))
self._initialize_password(password) self._initialize_password(password)
@ -649,7 +649,7 @@ class PDFDocument(object):
# #### hack around malformed pdf files # #### hack around malformed pdf files
# copied from https://github.com/jaepil/pdfminer3k/blob/master/pdfminer/pdfparser.py#L399 # copied from https://github.com/jaepil/pdfminer3k/blob/master/pdfminer/pdfparser.py#L399
#to solve https://github.com/pdfminer/pdfminer.six/issues/56 #to solve https://github.com/pdfminer/pdfminer.six/issues/56
#assert objid1 == objid, (objid, objid1) #assert objid1 == objid, str((objid1, objid))
if objid1 != objid: if objid1 != objid:
x = [] x = []
while kwd is not self.KEYWORD_OBJ: while kwd is not self.KEYWORD_OBJ:

View File

@ -357,7 +357,7 @@ class CFFFont(object):
sid += 1 sid += 1
elif format == b'\x02': elif format == b'\x02':
# Format 2 # Format 2
assert 0 assert False, str(('Unhandled', format))
else: else:
raise ValueError('unsupported charset format: %r' % format) raise ValueError('unsupported charset format: %r' % format)
#print self.code2gid #print self.code2gid
@ -444,7 +444,7 @@ class TrueTypeFont(object):
for c in range(sc, ec+1): for c in range(sc, ec+1):
char2gid[c] = (c + idd) & 0xffff char2gid[c] = (c + idd) & 0xffff
else: else:
assert 0 assert False, str(('Unhandled', fmttype))
# create unicode map # create unicode map
unicode_map = FileUnicodeMap() unicode_map = FileUnicodeMap()
for (char, gid) in char2gid.iteritems(): for (char, gid) in char2gid.iteritems():

View File

@ -184,7 +184,7 @@ def stream_value(x):
class PDFStream(PDFObject): class PDFStream(PDFObject):
def __init__(self, attrs, rawdata, decipher=None): def __init__(self, attrs, rawdata, decipher=None):
assert isinstance(attrs, dict) assert isinstance(attrs, dict), str(type(attrs))
self.attrs = attrs self.attrs = attrs
self.rawdata = rawdata self.rawdata = rawdata
self.decipher = decipher self.decipher = decipher
@ -236,7 +236,7 @@ class PDFStream(PDFObject):
return list(zip(filters, params)) #solves https://github.com/pdfminer/pdfminer.six/issues/15 return list(zip(filters, params)) #solves https://github.com/pdfminer/pdfminer.six/issues/15
def decode(self): def decode(self):
assert self.data is None and self.rawdata is not None assert self.data is None and self.rawdata is not None, str((self.data, self.rawdata))
data = self.rawdata data = self.rawdata
if self.decipher: if self.decipher:
# Handle encryption # Handle encryption

View File

@ -833,7 +833,7 @@ def rijndaelSetupDecrypt(key, keybits):
def rijndaelEncrypt(rk, nrounds, plaintext): def rijndaelEncrypt(rk, nrounds, plaintext):
assert len(plaintext) == 16 assert len(plaintext) == 16, str(len(plaintext))
# map byte array block to cipher state # map byte array block to cipher state
# and add initial round key: # and add initial round key:
@ -931,12 +931,12 @@ def rijndaelEncrypt(rk, nrounds, plaintext):
rk[p+3]) rk[p+3])
ciphertext += PUTU32(s3) ciphertext += PUTU32(s3)
assert len(ciphertext) == 16 assert len(ciphertext) == 16, str(len(ciphertext))
return ciphertext return ciphertext
def rijndaelDecrypt(rk, nrounds, ciphertext): def rijndaelDecrypt(rk, nrounds, ciphertext):
assert len(ciphertext) == 16 assert len(ciphertext) == 16, str(len(ciphertext))
# map byte array block to cipher state # map byte array block to cipher state
# and add initial round key: # and add initial round key:
@ -1034,7 +1034,7 @@ def rijndaelDecrypt(rk, nrounds, ciphertext):
rk[p+3]) rk[p+3])
plaintext += PUTU32(s3) plaintext += PUTU32(s3)
assert len(plaintext) == 16 assert len(plaintext) == 16, str(len(plaintext))
return plaintext return plaintext
@ -1049,14 +1049,14 @@ class RijndaelDecryptor(object):
""" """
def __init__(self, key, keybits=256): def __init__(self, key, keybits=256):
assert len(key) == KEYLENGTH(keybits) assert len(key) == KEYLENGTH(keybits), str((len(key), KEYLENGTH(keybits)))
(self.rk, self.nrounds) = rijndaelSetupDecrypt(key, keybits) (self.rk, self.nrounds) = rijndaelSetupDecrypt(key, keybits)
assert len(self.rk) == RKLENGTH(keybits) assert len(self.rk) == RKLENGTH(keybits), str((len(self.rk), RKLENGTH(keybits)))
assert self.nrounds == NROUNDS(keybits) assert self.nrounds == NROUNDS(keybits), str((self.nrounds, NROUNDS(keybits)))
return return
def decrypt(self, ciphertext): def decrypt(self, ciphertext):
assert len(ciphertext) == 16 assert len(ciphertext) == 16, str(len(ciphertext))
return rijndaelDecrypt(self.rk, self.nrounds, ciphertext) return rijndaelDecrypt(self.rk, self.nrounds, ciphertext)
@ -1064,13 +1064,12 @@ class RijndaelDecryptor(object):
class RijndaelEncryptor(object): class RijndaelEncryptor(object):
def __init__(self, key, keybits=256): def __init__(self, key, keybits=256):
assert len(key) == KEYLENGTH(keybits) assert len(key) == KEYLENGTH(keybits), str((len(key), KEYLENGTH(keybits)))
(self.rk, self.nrounds) = rijndaelSetupEncrypt(key, keybits) (self.rk, self.nrounds) = rijndaelSetupEncrypt(key, keybits)
assert len(self.rk) == RKLENGTH(keybits) assert len(self.rk) == RKLENGTH(keybits), str((len(self.rk), RKLENGTH(keybits)))
assert self.nrounds == NROUNDS(keybits) assert self.nrounds == NROUNDS(keybits), str((self.nrounds, NROUNDS(keybits)))
return return
def encrypt(self, plaintext): def encrypt(self, plaintext):
assert len(plaintext) == 16 assert len(plaintext) == 16, str(len(plaintext))
return rijndaelEncrypt(self.rk, self.nrounds, plaintext) return rijndaelEncrypt(self.rk, self.nrounds, plaintext)

View File

@ -15,7 +15,7 @@ if six.PY3:
def make_compat_bytes(in_str): def make_compat_bytes(in_str):
"In Py2, does nothing. In Py3, converts to bytes, encoding to unicode." "In Py2, does nothing. In Py3, converts to bytes, encoding to unicode."
assert isinstance(in_str, str) assert isinstance(in_str, str), str(type(in_str))
if six.PY2: if six.PY2:
return in_str return in_str
else: else:
@ -23,7 +23,7 @@ def make_compat_bytes(in_str):
def make_compat_str(in_str): def make_compat_str(in_str):
"In Py2, does nothing. In Py3, converts to string, guessing encoding." "In Py2, does nothing. In Py3, converts to string, guessing encoding."
assert isinstance(in_str, (bytes, str, unicode)) assert isinstance(in_str, (bytes, str, unicode)), str(type(in_str))
if six.PY3 and isinstance(in_str, bytes): if six.PY3 and isinstance(in_str, bytes):
enc = chardet.detect(in_str) enc = chardet.detect(in_str)
in_str = in_str.decode(enc['encoding']) in_str = in_str.decode(enc['encoding'])
@ -32,13 +32,11 @@ def make_compat_str(in_str):
def compatible_encode_method(bytesorstring, encoding='utf-8', erraction='ignore'): def compatible_encode_method(bytesorstring, encoding='utf-8', erraction='ignore'):
"When Py2 str.encode is called, it often means bytes.encode in Py3. This does either." "When Py2 str.encode is called, it often means bytes.encode in Py3. This does either."
if six.PY2: if six.PY2:
assert isinstance(bytesorstring, (str, unicode)), ("Error: Assumed was calling" assert isinstance(bytesorstring, (str, unicode)), str(type(bytesorstring))
" encode() on a string in Py2: {}").format(type(bytesorstring))
return bytesorstring.encode(encoding, erraction) return bytesorstring.encode(encoding, erraction)
if six.PY3: if six.PY3:
if isinstance(bytesorstring, str): return bytesorstring if isinstance(bytesorstring, str): return bytesorstring
assert isinstance(bytesorstring, bytes), ("Error: Assumed was calling" assert isinstance(bytesorstring, bytes), str(type(bytesorstring))
" encode() on a bytes in Py3: {}").format(type(bytesorstring))
return bytesorstring.decode(encoding, erraction) return bytesorstring.decode(encoding, erraction)
## PNG Predictor ## PNG Predictor
@ -170,7 +168,7 @@ def fsplit(pred, objs):
# drange # drange
def drange(v0, v1, d): def drange(v0, v1, d):
"""Returns a discrete range.""" """Returns a discrete range."""
assert v0 < v1 assert v0 < v1, str((v0, v1, d))
return range(int(v0)//d, int(v1+d)//d) return range(int(v0)//d, int(v1+d)//d)

View File

@ -53,7 +53,7 @@ class CMapConverter(object):
if not line: continue if not line: continue
values = line.split('\t') values = line.split('\t')
if encs is None: if encs is None:
assert values[0] == 'CID' assert values[0] == 'CID', str(values)
encs = values encs = values
continue continue