if the binary data happens to be ascii, emit a comment with an ascii representation

This commit is contained in:
justvanrossum 2020-05-08 10:37:01 +02:00
parent 4febf38be2
commit 0dc0222f59
2 changed files with 18 additions and 1 deletions

View File

@ -85,7 +85,11 @@ class table__m_e_t_a(DefaultTable.DefaultTable):
else:
writer.begintag("hexdata", tag=tag)
writer.newline()
writer.dumphex(self.data[tag])
data = self.data[tag]
if min(data) >= 0x20 and max(data) <= 0x7E:
writer.comment("ascii: " + data.decode("ascii"))
writer.newline()
writer.dumphex(data)
writer.endtag("hexdata")
writer.newline()

View File

@ -58,6 +58,19 @@ class MetaTableTest(unittest.TestCase):
'</hexdata>'
], [line.strip() for line in xml.splitlines()][1:])
def test_toXML_ascii_data(self):
table = table__m_e_t_a()
table.data["TEST"] = b"Hello!"
writer = XMLWriter(BytesIO())
table.toXML(writer, {"meta": table})
xml = writer.file.getvalue().decode("utf-8")
self.assertEqual([
'<hexdata tag="TEST">',
'<!-- ascii: Hello! -->',
'48656c6c 6f21',
'</hexdata>'
], [line.strip() for line in xml.splitlines()][1:])
def test_fromXML(self):
table = table__m_e_t_a()
for name, attrs, content in parseXML(