From fd041113d075e1e9ba5b300e2a39009a9afb7b09 Mon Sep 17 00:00:00 2001 From: Joscha Reimer Date: Wed, 21 Nov 2018 14:20:15 +0100 Subject: [PATCH 1/3] ENH: LogLocator: check for correct dimension of subs added --- lib/matplotlib/ticker.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index 3552c09c8852..025926286af8 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -2382,7 +2382,16 @@ def subs(self, subs): cbook._check_in_list(('all', 'auto'), subs=subs) self._subs = subs else: - self._subs = np.asarray(subs, dtype=float) + try: + self._subs = np.asarray(subs, dtype=float) + except ValueError as e: + raise ValueError("If subs is not None and not a string, " + "it must be a sequence of float.") from e + if self._subs.ndim != 1: + raise ValueError("If subs is not None and not a string, it " + "must be a sequence of float. Hence subs " + "should have 1 dimension but it has {} " + "dimensions.".format(self._subs.ndim)) def __call__(self): 'Return the locations of the ticks' From 9d30d5656232d63989959dddc6303d5885146a87 Mon Sep 17 00:00:00 2001 From: Joscha Reimer Date: Thu, 22 Nov 2018 09:24:09 +0100 Subject: [PATCH 2/3] MAINT: LogLocator: error messages in subs changed --- lib/matplotlib/ticker.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index 025926286af8..fa4cea7b5c3e 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -2385,13 +2385,13 @@ def subs(self, subs): try: self._subs = np.asarray(subs, dtype=float) except ValueError as e: - raise ValueError("If subs is not None and not a string, " - "it must be a sequence of float.") from e + raise ValueError("subs must be None, 'all', 'auto' or " + "a sequence of floats, not " + "{}.".format(subs)) from e if self._subs.ndim != 1: - raise ValueError("If subs is not None and not a string, it " - "must be a sequence of float. Hence subs " - "should have 1 dimension but it has {} " - "dimensions.".format(self._subs.ndim)) + raise ValueError("A sequence passed to subs must be " + "1-dimensional, not " + "{}-dimensional.".format(self._subs.ndim)) def __call__(self): 'Return the locations of the ticks' From 06507f272dbcacc9137a7788fd1fb0df830e90dc Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Sat, 24 Aug 2019 21:21:28 -0400 Subject: [PATCH 3/3] TST: add test of LogLocator.subs input validation --- lib/matplotlib/tests/test_ticker.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/matplotlib/tests/test_ticker.py b/lib/matplotlib/tests/test_ticker.py index 191afa0b9950..05c553ae2b07 100644 --- a/lib/matplotlib/tests/test_ticker.py +++ b/lib/matplotlib/tests/test_ticker.py @@ -1260,3 +1260,12 @@ def test_remove_overlap(remove_overlapping_locs, expected_num): assert len(ax.xaxis.get_minor_ticks()) == expected_num assert len(ax.xaxis.get_minorticklabels()) == expected_num assert len(ax.xaxis.get_minorticklines()) == expected_num*2 + + +@pytest.mark.parametrize('sub', [ + ['hi', 'aardvark'], + np.zeros((2, 2))]) +def test_bad_locator_subs(sub): + ll = mticker.LogLocator() + with pytest.raises(ValueError): + ll.subs(sub)