From 456e159a5aabae098eb63fe2d9a58632063c0a9f Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Fri, 13 Jan 2017 09:34:42 +0000 Subject: [PATCH] [t2CharStringPen] raise ValueError if tolerance not in range 0 <= t <= 0.5 --- Lib/fontTools/pens/t2CharStringPen.py | 3 ++- Lib/fontTools/pens/t2CharStringPen_test.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Lib/fontTools/pens/t2CharStringPen.py b/Lib/fontTools/pens/t2CharStringPen.py index 2bd405b03..9f628031d 100644 --- a/Lib/fontTools/pens/t2CharStringPen.py +++ b/Lib/fontTools/pens/t2CharStringPen.py @@ -67,7 +67,8 @@ class RelativeCoordinatePen(BasePen): def makeRoundFunc(tolerance): - assert 0 <= tolerance <= 0.5 + if not (0 <= tolerance <= 0.5): + raise ValueError("Rounding tolerance out of range: %g" % tolerance) def _round(number): if tolerance == 0: diff --git a/Lib/fontTools/pens/t2CharStringPen_test.py b/Lib/fontTools/pens/t2CharStringPen_test.py index 7c23823a2..f88008596 100644 --- a/Lib/fontTools/pens/t2CharStringPen_test.py +++ b/Lib/fontTools/pens/t2CharStringPen_test.py @@ -6,6 +6,13 @@ import unittest class T2CharStringPenTest(unittest.TestCase): + def __init__(self, methodName): + unittest.TestCase.__init__(self, methodName) + # Python 3 renamed assertRaisesRegexp to assertRaisesRegex, + # and fires deprecation warnings if a program uses the old name. + if not hasattr(self, "assertRaisesRegex"): + self.assertRaisesRegex = self.assertRaisesRegexp + def assertAlmostEqualProgram(self, expected, actual): self.assertEqual(len(expected), len(actual)) for i1, i2 in zip(expected, actual): @@ -106,6 +113,13 @@ class T2CharStringPenTest(unittest.TestCase): 'endchar'], charstring.program) + def test_invalid_tolerance(self): + for value in (-0.1, 0.6): + self.assertRaisesRegex( + ValueError, + "Rounding tolerance out of range", + T2CharStringPen, None, {}, roundTolerance=value) + if __name__ == '__main__': import sys