63
63
)
64
64
_DAYNAMES = (None , "Mon" , "Tue" , "Wed" , "Thu" , "Fri" , "Sat" , "Sun" )
65
65
66
+ _INVALID_ISO_ERROR = "Invalid isoformat string: '{}'"
67
+
66
68
# Utility functions - universal
67
69
def _cmp (obj_x , obj_y ):
68
70
return 0 if obj_x == obj_y else 1 if obj_x > obj_y else - 1
@@ -670,7 +672,7 @@ def fromisoformat(cls, date_string):
670
672
if match :
671
673
y , m , d = int (match .group (1 )), int (match .group (2 )), int (match .group (3 ))
672
674
return cls (y , m , d )
673
- raise ValueError ("Invalid isoformat string" )
675
+ raise ValueError (_INVALID_ISO_ERROR . format ( date_string ) )
674
676
675
677
@classmethod
676
678
def today (cls ):
@@ -926,16 +928,17 @@ def tzinfo(self):
926
928
def _parse_iso_string (string_to_parse , segments ):
927
929
results = []
928
930
931
+ remaining_string = string_to_parse
929
932
for regex in segments :
930
- match = _re .match (regex , string_to_parse )
933
+ match = _re .match (regex , remaining_string )
931
934
if match :
932
935
for grp in range (regex .count ("(" )):
933
936
results .append (int (match .group (grp + 1 )))
934
- string_to_parse = string_to_parse [len (match .group (0 )) :]
935
- elif string_to_parse : # Only raise an error if we're not done yet
936
- raise ValueError ("Invalid isoformat string" )
937
- if string_to_parse :
938
- raise ValueError ("Invalid isoformat string" )
937
+ remaining_string = remaining_string [len (match .group (0 )) :]
938
+ elif remaining_string : # Only raise an error if we're not done yet
939
+ raise ValueError ()
940
+ if remaining_string :
941
+ raise ValueError ()
939
942
return results
940
943
941
944
# pylint: disable=too-many-locals
@@ -945,6 +948,8 @@ def fromisoformat(cls, time_string):
945
948
Valid format is ``HH[:MM[:SS[.fff[fff]]]][+HH:MM[:SS[.ffffff]]]``
946
949
947
950
"""
951
+ # Store the original string in an error message
952
+ original_string = time_string
948
953
match = _re .match (r"(.*)[\-\+]" , time_string )
949
954
offset_string = None
950
955
if match :
@@ -964,13 +969,16 @@ def fromisoformat(cls, time_string):
964
969
r"\.([0-9][0-9][0-9][0-9][0-9][0-9])" ,
965
970
)
966
971
967
- results = cls ._parse_iso_string (time_string , time_segments )
968
- if len (results ) < 1 :
969
- raise ValueError ("Invalid isoformat string" )
970
- if len (results ) < len (time_segments ):
971
- results += [None ] * (len (time_segments ) - len (results ))
972
- if offset_string :
973
- results += cls ._parse_iso_string (offset_string , offset_segments )
972
+ try :
973
+ results = cls ._parse_iso_string (time_string , time_segments )
974
+ if len (results ) < 1 :
975
+ raise ValueError (_INVALID_ISO_ERROR .format (original_string ))
976
+ if len (results ) < len (time_segments ):
977
+ results += [None ] * (len (time_segments ) - len (results ))
978
+ if offset_string :
979
+ results += cls ._parse_iso_string (offset_string , offset_segments )
980
+ except ValueError as error :
981
+ raise ValueError (_INVALID_ISO_ERROR .format (original_string )) from error
974
982
975
983
hh = results [0 ]
976
984
mm = results [1 ] if len (results ) >= 2 and results [1 ] is not None else 0
@@ -1316,15 +1324,20 @@ def fromisoformat(cls, date_string):
1316
1324
Valid format is ``YYYY-MM-DD[*HH[:MM[:SS[.fff[fff]]]][+HH:MM[:SS[.ffffff]]]]``
1317
1325
1318
1326
"""
1327
+ original_string = date_string
1328
+
1319
1329
time_string = None
1320
- if len (date_string ) > 10 :
1321
- time_string = date_string [11 :]
1322
- date_string = date_string [:10 ]
1323
- dateval = date .fromisoformat (date_string )
1324
- timeval = time .fromisoformat (time_string )
1325
- else :
1326
- dateval = date .fromisoformat (date_string )
1327
- timeval = time ()
1330
+ try :
1331
+ if len (date_string ) > 10 :
1332
+ time_string = date_string [11 :]
1333
+ date_string = date_string [:10 ]
1334
+ dateval = date .fromisoformat (date_string )
1335
+ timeval = time .fromisoformat (time_string )
1336
+ else :
1337
+ dateval = date .fromisoformat (date_string )
1338
+ timeval = time ()
1339
+ except ValueError as error :
1340
+ raise ValueError (_INVALID_ISO_ERROR .format (original_string )) from error
1328
1341
1329
1342
return cls .combine (dateval , timeval )
1330
1343
0 commit comments