diff --git a/Lib/fontTools/ttLib/tables/_n_a_m_e.py b/Lib/fontTools/ttLib/tables/_n_a_m_e.py index ff6740f39..e9ff2151d 100644 --- a/Lib/fontTools/ttLib/tables/_n_a_m_e.py +++ b/Lib/fontTools/ttLib/tables/_n_a_m_e.py @@ -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): diff --git a/Tests/ttLib/tables/_n_a_m_e_test.py b/Tests/ttLib/tables/_n_a_m_e_test.py index 9ce62f792..d770a5231 100644 --- a/Tests/ttLib/tables/_n_a_m_e_test.py +++ b/Tests/ttLib/tables/_n_a_m_e_test.py @@ -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) - ) + with self.assertRaises(TypeError): + table.names.sort() def test_addName(self): table = table__n_a_m_e()