[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:
# implemented so that list.sort() sorts according to the spec.
selfTuple = (
getattr(self, "platformID", None),
getattr(self, "platEncID", None),
getattr(self, "langID", None),
getattr(self, "nameID", None),
self.platformID,
self.platEncID,
self.langID,
self.nameID,
self.toBytes(),
)
otherTuple = (
getattr(other, "platformID", None),
getattr(other, "platEncID", None),
getattr(other, "langID", None),
getattr(other, "nameID", None),
other.platformID,
other.platEncID,
other.langID,
other.nameID,
other.toBytes(),
)
return selfTuple < otherTuple
except UnicodeEncodeError:
# This can only be reached if all IDs are identical but the strings
# can't be encoded for their platform encoding.
logging.warning(
"The name table contains multiple entries for platformID %d, "
"platEncID %d, langID %d, nameID %d with strings that cannot be "
"properly encoded.",
getattr(self, "platformID", None),
getattr(self, "platEncID", None),
getattr(self, "langID", None),
getattr(self, "nameID", None),
)
except (UnicodeEncodeError, AttributeError):
# This can only happen for
# 1) an object that is not a NameRecord, or
# 2) an unlikely incomplete NameRecord object which has not been
# fully populated, or
# 3) when all IDs are identical but the strings can't be encoded
# for their platform encoding.
# In all cases it is best to return NotImplemented.
return NotImplemented
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),
]
with CapturingLogHandler(log, "WARNING") as captor:
with self.assertRaises(TypeError):
table.names.sort()
self.assertTrue(
all("cannot be properly encoded" in r.msg for r in captor.records)
)
def test_addName(self):
table = table__n_a_m_e()