From 000bf81700e37c30b6e2d2ffe0ddb50d57a447c9 Mon Sep 17 00:00:00 2001 From: Khaled Hosny Date: Fri, 30 Jul 2021 03:58:20 +0200 Subject: [PATCH] Default to "\n" for newlinestr instead of None MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If newlinestr is None, os.linesep is used, bu it is the third millennium and we don’t need or want different line endings per-platform. --- Lib/fontTools/misc/xmlWriter.py | 2 +- Lib/fontTools/ttLib/ttCollection.py | 2 +- Lib/fontTools/ttLib/ttFont.py | 2 +- Lib/fontTools/ttx.py | 2 +- Snippets/checksum.py | 4 +--- Tests/feaLib/builder_test.py | 7 +++---- Tests/misc/xmlWriter_test.py | 17 ++++++++--------- Tests/mtiLib/mti_test.py | 6 +++--- Tests/subset/subset_test.py | 7 +++---- Tests/ttLib/tables/_l_t_a_g_test.py | 5 ++--- Tests/ttLib/tables/tables_test.py | 2 +- Tests/ttLib/tables/ttProgram_test.py | 2 +- Tests/varLib/instancer/instancer_test.py | 2 +- Tests/varLib/interpolate_layout_test.py | 7 +++---- Tests/varLib/mutator_test.py | 7 +++---- Tests/varLib/varLib_test.py | 7 +++---- 16 files changed, 36 insertions(+), 45 deletions(-) diff --git a/Lib/fontTools/misc/xmlWriter.py b/Lib/fontTools/misc/xmlWriter.py index fec127a9f..ddc2fdb3e 100644 --- a/Lib/fontTools/misc/xmlWriter.py +++ b/Lib/fontTools/misc/xmlWriter.py @@ -11,7 +11,7 @@ INDENT = " " class XMLWriter(object): def __init__(self, fileOrPath, indentwhite=INDENT, idlefunc=None, encoding="utf_8", - newlinestr=None): + newlinestr="\n"): if encoding.lower().replace('-','').replace('_','') != 'utf8': raise Exception('Only UTF-8 encoding is supported.') if fileOrPath == '-': diff --git a/Lib/fontTools/ttLib/ttCollection.py b/Lib/fontTools/ttLib/ttCollection.py index 3db4c8cd3..0cc11beac 100644 --- a/Lib/fontTools/ttLib/ttCollection.py +++ b/Lib/fontTools/ttLib/ttCollection.py @@ -76,7 +76,7 @@ class TTCollection(object): final.write(file.getvalue()) file.close() - def saveXML(self, fileOrPath, newlinestr=None, writeVersion=True, **kwargs): + def saveXML(self, fileOrPath, newlinestr="\n", writeVersion=True, **kwargs): from fontTools.misc import xmlWriter writer = xmlWriter.XMLWriter(fileOrPath, newlinestr=newlinestr) diff --git a/Lib/fontTools/ttLib/ttFont.py b/Lib/fontTools/ttLib/ttFont.py index 41a487518..5ccd182cd 100644 --- a/Lib/fontTools/ttLib/ttFont.py +++ b/Lib/fontTools/ttLib/ttFont.py @@ -215,7 +215,7 @@ class TTFont(object): return writer.reordersTables() - def saveXML(self, fileOrPath, newlinestr=None, **kwargs): + def saveXML(self, fileOrPath, newlinestr="\n", **kwargs): """Export the font as TTX (an XML-based text file), or as a series of text files when splitTables is true. In the latter case, the 'fileOrPath' argument should be a path to a directory. diff --git a/Lib/fontTools/ttx.py b/Lib/fontTools/ttx.py index 2eed0c5cc..55b27513e 100644 --- a/Lib/fontTools/ttx.py +++ b/Lib/fontTools/ttx.py @@ -122,7 +122,7 @@ class Options(object): ignoreDecompileErrors = True bitmapGlyphDataFormat = 'raw' unicodedata = None - newlinestr = None + newlinestr = "\n" recalcTimestamp = None flavor = None useZopfli = False diff --git a/Snippets/checksum.py b/Snippets/checksum.py index 53a53183d..b965a3571 100644 --- a/Snippets/checksum.py +++ b/Snippets/checksum.py @@ -29,9 +29,7 @@ def write_checksum(filepaths, stdout_write=False, use_ttx=False, include_tables= temp_ttx_path = path + ".ttx" tt = TTFont(path) - # important to keep the newlinestr value defined here as hash values will change across platforms - # if platform specific newline values are assumed - tt.saveXML(temp_ttx_path, newlinestr="\n", skipTables=exclude_tables, tables=include_tables) + tt.saveXML(temp_ttx_path, skipTables=exclude_tables, tables=include_tables) checksum_path = temp_ttx_path else: if include_tables is not None: diff --git a/Tests/feaLib/builder_test.py b/Tests/feaLib/builder_test.py index 951a360fc..97fc0f0b1 100644 --- a/Tests/feaLib/builder_test.py +++ b/Tests/feaLib/builder_test.py @@ -108,12 +108,11 @@ class BuilderTest(unittest.TestCase): lines = [] with open(path, "r", encoding="utf-8") as ttx: for line in ttx.readlines(): - # Elide ttFont attributes because ttLibVersion may change, - # and use os-native line separators so we can run difflib. + # Elide ttFont attributes because ttLibVersion may change. if line.startswith("" + os.linesep) + lines.append("\n") else: - lines.append(line.rstrip() + os.linesep) + lines.append(line.rstrip() + "\n") return lines def expect_ttx(self, font, expected_ttx, replace=None): diff --git a/Tests/misc/xmlWriter_test.py b/Tests/misc/xmlWriter_test.py index fd4f24089..7b9894c66 100644 --- a/Tests/misc/xmlWriter_test.py +++ b/Tests/misc/xmlWriter_test.py @@ -4,8 +4,7 @@ import os import unittest from fontTools.misc.xmlWriter import XMLWriter -linesep = tobytes(os.linesep) -HEADER = b'' + linesep +HEADER = b'\n' class TestXMLWriter(unittest.TestCase): @@ -17,30 +16,30 @@ class TestXMLWriter(unittest.TestCase): def test_comment_multiline(self): writer = XMLWriter(BytesIO()) writer.comment("Hello world\nHow are you?") - self.assertEqual(HEADER + b"", + self.assertEqual(HEADER + b"", writer.file.getvalue()) def test_encoding_default(self): writer = XMLWriter(BytesIO()) - self.assertEqual(b'' + linesep, + self.assertEqual(b'\n', writer.file.getvalue()) def test_encoding_utf8(self): # https://github.com/fonttools/fonttools/issues/246 writer = XMLWriter(BytesIO(), encoding="utf8") - self.assertEqual(b'' + linesep, + self.assertEqual(b'\n', writer.file.getvalue()) def test_encoding_UTF_8(self): # https://github.com/fonttools/fonttools/issues/246 writer = XMLWriter(BytesIO(), encoding="UTF-8") - self.assertEqual(b'' + linesep, + self.assertEqual(b'\n', writer.file.getvalue()) def test_encoding_UTF8(self): # https://github.com/fonttools/fonttools/issues/246 writer = XMLWriter(BytesIO(), encoding="UTF8") - self.assertEqual(b'' + linesep, + self.assertEqual(b'\n', writer.file.getvalue()) def test_encoding_other(self): @@ -61,7 +60,7 @@ class TestXMLWriter(unittest.TestCase): writer.newline() writer.dedent() writer.write("baz") - self.assertEqual(HEADER + bytesjoin(["foo", " bar", "baz"], linesep), + self.assertEqual(HEADER + bytesjoin(["foo", " bar", "baz"], "\n"), writer.file.getvalue()) def test_writecdata(self): @@ -89,7 +88,7 @@ class TestXMLWriter(unittest.TestCase): "66756c20 67726f75 70206f66 206c6574", "74657273 2c206e6f 74206120 67726f75", "70206f66 20626561 75746966 756c206c", - "65747465 72732e ", ""], joiner=linesep), writer.file.getvalue()) + "65747465 72732e ", ""], joiner="\n"), writer.file.getvalue()) def test_stringifyattrs(self): writer = XMLWriter(BytesIO()) diff --git a/Tests/mtiLib/mti_test.py b/Tests/mtiLib/mti_test.py index f0e7bdf3d..8a80113c3 100644 --- a/Tests/mtiLib/mti_test.py +++ b/Tests/mtiLib/mti_test.py @@ -182,14 +182,14 @@ class MtiTest(unittest.TestCase): decompiled.decompile(blob, font) # XML from built object. - writer = XMLWriter(StringIO(), newlinestr='\n') + writer = XMLWriter(StringIO()) writer.begintag(tableTag); writer.newline() table.toXML(writer, font) writer.endtag(tableTag); writer.newline() xml_built = writer.file.getvalue() # XML from decompiled object. - writer = XMLWriter(StringIO(), newlinestr='\n') + writer = XMLWriter(StringIO()) writer.begintag(tableTag); writer.newline() decompiled.toXML(writer, font) writer.endtag(tableTag); writer.newline() @@ -208,7 +208,7 @@ class MtiTest(unittest.TestCase): reader.read(rootless=True) # XML from object read from XML. - writer = XMLWriter(StringIO(), newlinestr='\n') + writer = XMLWriter(StringIO()) writer.begintag(tableTag); writer.newline() font2[tableTag].toXML(writer, font) writer.endtag(tableTag); writer.newline() diff --git a/Tests/subset/subset_test.py b/Tests/subset/subset_test.py index aa96fb7cb..12682ab4e 100644 --- a/Tests/subset/subset_test.py +++ b/Tests/subset/subset_test.py @@ -50,12 +50,11 @@ class SubsetTest(unittest.TestCase): lines = [] with open(path, "r", encoding="utf-8") as ttx: for line in ttx.readlines(): - # Elide ttFont attributes because ttLibVersion may change, - # and use os-native line separators so we can run difflib. + # Elide ttFont attributes because ttLibVersion may change. if line.startswith("" + os.linesep) + lines.append("\n") else: - lines.append(line.rstrip() + os.linesep) + lines.append(line.rstrip() + "\n") return lines def expect_ttx(self, font, expected_ttx, tables=None): diff --git a/Tests/ttLib/tables/_l_t_a_g_test.py b/Tests/ttLib/tables/_l_t_a_g_test.py index fc9be82a3..291199036 100644 --- a/Tests/ttLib/tables/_l_t_a_g_test.py +++ b/Tests/ttLib/tables/_l_t_a_g_test.py @@ -1,7 +1,6 @@ from fontTools.misc.testTools import parseXML from fontTools.misc.xmlWriter import XMLWriter from io import BytesIO -import os import struct import unittest from fontTools.ttLib import newTable @@ -46,14 +45,14 @@ class Test_l_t_a_g(unittest.TestCase): table = newTable("ltag") table.decompile(self.DATA_, ttFont=None) table.toXML(writer, ttFont=None) - expected = os.linesep.join([ + expected = "\n".join([ '', '', '', '', '', '' - ]) + os.linesep + ]) + "\n" self.assertEqual(expected.encode("utf_8"), writer.file.getvalue()) diff --git a/Tests/ttLib/tables/tables_test.py b/Tests/ttLib/tables/tables_test.py index f66323fbf..be8c63e34 100644 --- a/Tests/ttLib/tables/tables_test.py +++ b/Tests/ttLib/tables/tables_test.py @@ -254,7 +254,7 @@ def read_expected_ttx(testfile, tableTag): def dump_ttx(font, tableTag): f = StringIO() - font.saveXML(f, newlinestr='\n', tables=[tableTag]) + font.saveXML(f, tables=[tableTag]) return ttLibVersion_RE.sub('', f.getvalue()) diff --git a/Tests/ttLib/tables/ttProgram_test.py b/Tests/ttLib/tables/ttProgram_test.py index be6e86a0a..13d1ba875 100644 --- a/Tests/ttLib/tables/ttProgram_test.py +++ b/Tests/ttLib/tables/ttProgram_test.py @@ -104,7 +104,7 @@ class ProgramTest(unittest.TestCase): p.fromBytecode(BYTECODE) ttfont = TestFont() buf = StringIO() - writer = XMLWriter(buf, newlinestr='\n') + writer = XMLWriter(buf) try: p.toXML(writer, ttfont) finally: diff --git a/Tests/varLib/instancer/instancer_test.py b/Tests/varLib/instancer/instancer_test.py index 90048c181..cbc313dc9 100644 --- a/Tests/varLib/instancer/instancer_test.py +++ b/Tests/varLib/instancer/instancer_test.py @@ -1411,7 +1411,7 @@ def _dump_ttx(ttFont): tmp.seek(0) ttFont2 = ttLib.TTFont(tmp, recalcBBoxes=False, recalcTimestamp=False) s = StringIO() - ttFont2.saveXML(s, newlinestr="\n") + ttFont2.saveXML(s) return _strip_ttLibVersion(s.getvalue()) diff --git a/Tests/varLib/interpolate_layout_test.py b/Tests/varLib/interpolate_layout_test.py index d7134d79d..18dc3a752 100644 --- a/Tests/varLib/interpolate_layout_test.py +++ b/Tests/varLib/interpolate_layout_test.py @@ -61,12 +61,11 @@ class InterpolateLayoutTest(unittest.TestCase): lines = [] with open(path, "r", encoding="utf-8") as ttx: for line in ttx.readlines(): - # Elide ttFont attributes because ttLibVersion may change, - # and use os-native line separators so we can run difflib. + # Elide ttFont attributes because ttLibVersion may change. if line.startswith("" + os.linesep) + lines.append("\n") else: - lines.append(line.rstrip() + os.linesep) + lines.append(line.rstrip() + "\n") return lines def expect_ttx(self, font, expected_ttx, tables): diff --git a/Tests/varLib/mutator_test.py b/Tests/varLib/mutator_test.py index abe5d0270..03ad870f6 100644 --- a/Tests/varLib/mutator_test.py +++ b/Tests/varLib/mutator_test.py @@ -59,12 +59,11 @@ class MutatorTest(unittest.TestCase): lines = [] with open(path, "r", encoding="utf-8") as ttx: for line in ttx.readlines(): - # Elide ttFont attributes because ttLibVersion may change, - # and use os-native line separators so we can run difflib. + # Elide ttFont attributes because ttLibVersion may change. if line.startswith("" + os.linesep) + lines.append("\n") else: - lines.append(line.rstrip() + os.linesep) + lines.append(line.rstrip() + "\n") return lines def expect_ttx(self, font, expected_ttx, tables): diff --git a/Tests/varLib/varLib_test.py b/Tests/varLib/varLib_test.py index b8e7183bb..484a2e229 100644 --- a/Tests/varLib/varLib_test.py +++ b/Tests/varLib/varLib_test.py @@ -84,12 +84,11 @@ class BuildTest(unittest.TestCase): lines = [] with open(path, "r", encoding="utf-8") as ttx: for line in ttx.readlines(): - # Elide ttFont attributes because ttLibVersion may change, - # and use os-native line separators so we can run difflib. + # Elide ttFont attributes because ttLibVersion may change. if line.startswith("" + os.linesep) + lines.append("\n") else: - lines.append(line.rstrip() + os.linesep) + lines.append(line.rstrip() + "\n") return lines def expect_ttx(self, font, expected_ttx, tables):