Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d4bd0cf

Browse files
committed
More robust handling of exceptions and types
1 parent 64f81fc commit d4bd0cf

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

lib/matplotlib/rcsetup.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -918,23 +918,33 @@ def _validate_linestyle(ls):
918918
if isinstance(ls, six.string_types):
919919
try:
920920
return _validate_named_linestyle(ls)
921-
except KeyError:
922-
pass
923-
924-
# On-off ink (in points) sequence *of even length*.
921+
except (UnicodeDecodeError, KeyError):
922+
# On Python 2, string-like *ls*, like for example
923+
# 'solid'.encode('utf-16'), may raise a unicode error.
924+
raise ValueError("the linestyle string is not a valid string.")
925+
926+
if hasattr(ls, 'decode'):
927+
# On Python 2, a string-like *ls* should already have lead to a
928+
# successful return or to raising an exception. On Python 3, we have
929+
# to manually raise an exception in the case of a byte-like *ls*.
930+
# Otherwise, if *ls* is of even-length, it will be passed to the
931+
# instance of validate_nseq_float, which will return an absurd on-off
932+
# ink sequence...
933+
raise ValueError("linestyle neither looks like an on-off ink sequence "
934+
"nor a valid string.")
935+
936+
# Look for an on-off ink sequence (in points) *of even length*.
925937
# Offset is set to None.
926938
try:
927939
if len(ls) % 2 != 0:
928-
# Expecting a sequence of even length
929-
raise ValueError
940+
raise ValueError("the linestyle sequence is not of even length.")
941+
930942
return (None, validate_nseq_float()(ls))
931-
except (ValueError, TypeError):
932-
# TypeError can be raised by wrong types passed to float()
933-
# (called inside the instance of validate_nseq_float).
934-
pass
935943

936-
raise ValueError("linestyle must be a valid string or "
937-
"an even-length sequence of floats.")
944+
except (ValueError, TypeError):
945+
# TypeError can be raised inside the instance of validate_nseq_float,
946+
# by wrong types passed to float(), like NoneType.
947+
raise ValueError("linestyle is not a valid on-off ink sequence.")
938948

939949

940950
# a map from key -> value, converter

0 commit comments

Comments
 (0)