@@ -918,23 +918,33 @@ def _validate_linestyle(ls):
918
918
if isinstance (ls , six .string_types ):
919
919
try :
920
920
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*.
925
937
# Offset is set to None.
926
938
try :
927
939
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
+
930
942
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
935
943
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." )
938
948
939
949
940
950
# a map from key -> value, converter
0 commit comments