[ttLib.tables._n_a_m_e] Fix #1997: Only attempt to recovered malformed data from bytes (#1998)

* don't attempt to recover malformed data from str, only from bytes. Fixes #1997
This commit is contained in:
Just van Rossum 2020-06-16 16:35:40 +02:00 committed by GitHub
parent 6b4430a8a3
commit dc7d016538
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 1 deletions

View File

@ -441,7 +441,7 @@ class NameRecord(object):
encoding = self.getEncoding()
string = self.string
if encoding == 'utf_16_be' and len(string) % 2 == 1:
if isinstance(string, bytes) and encoding == 'utf_16_be' and len(string) % 2 == 1:
# Recover badly encoded UTF-16 strings that have an odd number of bytes:
# - If the last byte is zero, drop it. Otherwise,
# - If all the odd bytes are zero and all the even bytes are ASCII,

View File

@ -345,6 +345,11 @@ class NameRecordTest(unittest.TestCase):
self.assertEqual("utf_16_be", name.getEncoding())
self.assertRaises(UnicodeDecodeError, name.toUnicode)
def test_toUnicode_singleChar(self):
# https://github.com/fonttools/fonttools/issues/1997
name = makeName("A", 256, 3, 1, 0x409)
self.assertEqual(name.toUnicode(), "A")
def toXML(self, name):
writer = XMLWriter(BytesIO())
name.toXML(writer, ttFont=None)