From 7c6337facf79232f6a3a6c8bbc32a8f4af4b71d8 Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Tue, 19 Jan 2021 17:53:53 +0000 Subject: [PATCH 1/3] Add exception and test --- Lib/fontTools/otlLib/builder.py | 3 ++- Tests/otlLib/builder_test.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Lib/fontTools/otlLib/builder.py b/Lib/fontTools/otlLib/builder.py index 7e1444518..7711fa4c5 100644 --- a/Lib/fontTools/otlLib/builder.py +++ b/Lib/fontTools/otlLib/builder.py @@ -2574,7 +2574,8 @@ class ClassDefBuilder(object): return self.classes_.add(glyphs) for glyph in glyphs: - assert glyph not in self.glyphs_ + if glyph in self.glyphs_: + raise TypeError(f"Glyph {glyph} already present in class.") self.glyphs_[glyph] = glyphs def classes(self): diff --git a/Tests/otlLib/builder_test.py b/Tests/otlLib/builder_test.py index bdfc64509..a2c51331d 100644 --- a/Tests/otlLib/builder_test.py +++ b/Tests/otlLib/builder_test.py @@ -1101,6 +1101,18 @@ class ClassDefBuilderTest(object): assert not b.canAdd({"d", "e", "f"}) assert not b.canAdd({"f"}) + def test_add_exception(self): + b = builder.ClassDefBuilder(useClass0=True) + b.add({"a", "b", "c", "d"}) + with pytest.raises(TypeError): + b.add({"a"}) + with pytest.raises(TypeError): + b.add({"b"}) + with pytest.raises(TypeError): + b.add({"c"}) + with pytest.raises(TypeError): + b.add({"d"}) + buildStatTable_test_data = [ ([ From 97124070540076056b51020aeaf32410ae355ea5 Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Wed, 20 Jan 2021 11:19:43 +0000 Subject: [PATCH 2/3] Change TypeError to ValueError --- Lib/fontTools/otlLib/builder.py | 2 +- Tests/otlLib/builder_test.py | 12 +++--------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/Lib/fontTools/otlLib/builder.py b/Lib/fontTools/otlLib/builder.py index 7711fa4c5..5d16b83c2 100644 --- a/Lib/fontTools/otlLib/builder.py +++ b/Lib/fontTools/otlLib/builder.py @@ -2575,7 +2575,7 @@ class ClassDefBuilder(object): self.classes_.add(glyphs) for glyph in glyphs: if glyph in self.glyphs_: - raise TypeError(f"Glyph {glyph} already present in class.") + raise ValueError(f"Glyph {glyph} is already present in class.") self.glyphs_[glyph] = glyphs def classes(self): diff --git a/Tests/otlLib/builder_test.py b/Tests/otlLib/builder_test.py index a2c51331d..6bd05615b 100644 --- a/Tests/otlLib/builder_test.py +++ b/Tests/otlLib/builder_test.py @@ -1103,15 +1103,9 @@ class ClassDefBuilderTest(object): def test_add_exception(self): b = builder.ClassDefBuilder(useClass0=True) - b.add({"a", "b", "c", "d"}) - with pytest.raises(TypeError): - b.add({"a"}) - with pytest.raises(TypeError): - b.add({"b"}) - with pytest.raises(TypeError): - b.add({"c"}) - with pytest.raises(TypeError): - b.add({"d"}) + b.add({"a", "b", "c"}) + with pytest.raises(ValueError): + b.add({"a", "d"}) buildStatTable_test_data = [ From be898ec6f9079f5faa45b8abc3204055dca10d28 Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Wed, 20 Jan 2021 14:30:49 +0000 Subject: [PATCH 3/3] Change ValueError to OpenTypeLibError --- Lib/fontTools/otlLib/builder.py | 2 +- Tests/otlLib/builder_test.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/fontTools/otlLib/builder.py b/Lib/fontTools/otlLib/builder.py index 5d16b83c2..029aa3fc5 100644 --- a/Lib/fontTools/otlLib/builder.py +++ b/Lib/fontTools/otlLib/builder.py @@ -2575,7 +2575,7 @@ class ClassDefBuilder(object): self.classes_.add(glyphs) for glyph in glyphs: if glyph in self.glyphs_: - raise ValueError(f"Glyph {glyph} is already present in class.") + raise OpenTypeLibError(f"Glyph {glyph} is already present in class.", None) self.glyphs_[glyph] = glyphs def classes(self): diff --git a/Tests/otlLib/builder_test.py b/Tests/otlLib/builder_test.py index 6bd05615b..3ea5a7459 100644 --- a/Tests/otlLib/builder_test.py +++ b/Tests/otlLib/builder_test.py @@ -2,7 +2,7 @@ import io import struct from fontTools.misc.fixedTools import floatToFixed from fontTools.misc.testTools import getXML -from fontTools.otlLib import builder +from fontTools.otlLib import builder, error from fontTools import ttLib from fontTools.ttLib.tables import otTables import pytest @@ -1104,7 +1104,7 @@ class ClassDefBuilderTest(object): def test_add_exception(self): b = builder.ClassDefBuilder(useClass0=True) b.add({"a", "b", "c"}) - with pytest.raises(ValueError): + with pytest.raises(error.OpenTypeLibError): b.add({"a", "d"})