[name] Be less cautious about getting data for NameRecord comparisons

This commit is contained in:
Nikolaus Waxweiler 2019-10-11 17:39:56 +01:00
parent 2e82438d93
commit e4f7495f32
2 changed files with 18 additions and 26 deletions

View File

@ -462,32 +462,28 @@ class NameRecord(object):
try: try:
# implemented so that list.sort() sorts according to the spec. # implemented so that list.sort() sorts according to the spec.
selfTuple = ( selfTuple = (
getattr(self, "platformID", None), self.platformID,
getattr(self, "platEncID", None), self.platEncID,
getattr(self, "langID", None), self.langID,
getattr(self, "nameID", None), self.nameID,
self.toBytes(), self.toBytes(),
) )
otherTuple = ( otherTuple = (
getattr(other, "platformID", None), other.platformID,
getattr(other, "platEncID", None), other.platEncID,
getattr(other, "langID", None), other.langID,
getattr(other, "nameID", None), other.nameID,
other.toBytes(), other.toBytes(),
) )
return selfTuple < otherTuple return selfTuple < otherTuple
except UnicodeEncodeError: except (UnicodeEncodeError, AttributeError):
# This can only be reached if all IDs are identical but the strings # This can only happen for
# can't be encoded for their platform encoding. # 1) an object that is not a NameRecord, or
logging.warning( # 2) an unlikely incomplete NameRecord object which has not been
"The name table contains multiple entries for platformID %d, " # fully populated, or
"platEncID %d, langID %d, nameID %d with strings that cannot be " # 3) when all IDs are identical but the strings can't be encoded
"properly encoded.", # for their platform encoding.
getattr(self, "platformID", None), # In all cases it is best to return NotImplemented.
getattr(self, "platEncID", None),
getattr(self, "langID", None),
getattr(self, "nameID", None),
)
return NotImplemented return NotImplemented
def __repr__(self): def __repr__(self):

View File

@ -70,12 +70,8 @@ class NameTableTest(unittest.TestCase):
makeName("Test寬", 25, 1, 0, 0), makeName("Test寬", 25, 1, 0, 0),
makeName("Test鬆鬆", 25, 1, 0, 0), makeName("Test鬆鬆", 25, 1, 0, 0),
] ]
with CapturingLogHandler(log, "WARNING") as captor: with self.assertRaises(TypeError):
with self.assertRaises(TypeError): table.names.sort()
table.names.sort()
self.assertTrue(
all("cannot be properly encoded" in r.msg for r in captor.records)
)
def test_addName(self): def test_addName(self):
table = table__n_a_m_e() table = table__n_a_m_e()