diff --git a/kiva/celiagg.py b/kiva/celiagg.py index f15f47190..10f1876d8 100644 --- a/kiva/celiagg.py +++ b/kiva/celiagg.py @@ -280,6 +280,15 @@ def set_line_dash(self, lengths, phase=0): """ if lengths is not None: count = len(lengths) + if count % 2 == 1: + raise ValueError( + "Array of lengths should have an even number of values, " + f"got {lengths!r}" + ) + if any(length <= 0 for length in lengths): + raise ValueError( + f"All length values should be positive, got {lengths!r}" + ) lengths = np.array(lengths).reshape(count // 2, 2) else: lengths = [] diff --git a/kiva/tests/test_celiagg_drawing.py b/kiva/tests/test_celiagg_drawing.py index 559566107..cf5f1e6da 100644 --- a/kiva/tests/test_celiagg_drawing.py +++ b/kiva/tests/test_celiagg_drawing.py @@ -14,6 +14,7 @@ class TestCeliaggDrawing(DrawingImageTester, unittest.TestCase): + def create_graphics_context(self, width, height, pixel_scale): return GraphicsContext((width, height), base_pixel_scale=pixel_scale) @@ -37,3 +38,12 @@ def test_ipython_repr_png(self): with open(filename, 'wb') as fp: fp.write(stream) self.assertImageSavedWithContent(filename) + + def test_set_line_dash_odd(self): + with self.assertRaises(ValueError): + self.gc.set_line_dash([1.0, 2.0, 3.0]) + + def test_set_line_dash_positive(self): + # regression test for #909 + with self.assertRaises(ValueError): + self.gc.set_line_dash([0.0, 0.0])