diff options
author | Mario Corchero <mariocj89@gmail.com> | 2021-07-02 17:06:30 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-02 17:06:30 +0200 |
commit | 54a756d98a5a5e3e5627ab6b9f2fc8d72881b124 (patch) | |
tree | b6197423e81fda4fb016333ec25daa3258ed0ecf | |
parent | ed4b9beec6df813c7d4fdbfb6a60bd84dc07226b (diff) | |
parent | aa62cdc0e76e014afacf0d8cec034f80d0b1d47d (diff) | |
download | dateutil-git-54a756d98a5a5e3e5627ab6b9f2fc8d72881b124.tar.gz |
Merge pull request #1125 from mariocj89/pu/strict-iso-sep
isoparse: Fail with inconsistent time separators
-rw-r--r-- | changelog.d/1125.bugfix.rst | 1 | ||||
-rw-r--r-- | dateutil/parser/isoparser.py | 13 | ||||
-rw-r--r-- | dateutil/test/test_isoparser.py | 25 |
3 files changed, 13 insertions, 26 deletions
diff --git a/changelog.d/1125.bugfix.rst b/changelog.d/1125.bugfix.rst new file mode 100644 index 0000000..6ed0449 --- /dev/null +++ b/changelog.d/1125.bugfix.rst @@ -0,0 +1 @@ +Make `isoparse` raise when trying to parse times with inconsistent use of `:` separator. Reported and fixed by @mariocj89 (gh pr #1125). diff --git a/dateutil/parser/isoparser.py b/dateutil/parser/isoparser.py index 749270d..5d7bee3 100644 --- a/dateutil/parser/isoparser.py +++ b/dateutil/parser/isoparser.py @@ -336,7 +336,7 @@ class isoparser(object): if len_str < 2: raise ValueError('ISO time too short') - has_sep = len_str >= 3 and timestr[2:3] == self._TIME_SEP + has_sep = False while pos < len_str and comp < 5: comp += 1 @@ -347,13 +347,18 @@ class isoparser(object): pos = len_str break + if comp == 1 and timestr[pos:pos+1] == self._TIME_SEP: + has_sep = True + pos += 1 + elif comp == 2 and has_sep: + if timestr[pos:pos+1] != self._TIME_SEP: + raise ValueError('Inconsistent use of colon separator') + pos += 1 + if comp < 3: # Hour, minute, second components[comp] = int(timestr[pos:pos + 2]) pos += 2 - if (has_sep and pos < len_str and - timestr[pos:pos + 1] == self._TIME_SEP): - pos += 1 if comp == 3: # Fraction of a second diff --git a/dateutil/test/test_isoparser.py b/dateutil/test/test_isoparser.py index 096e709..35899ab 100644 --- a/dateutil/test/test_isoparser.py +++ b/dateutil/test/test_isoparser.py @@ -244,6 +244,8 @@ def test_bytes(isostr, dt): ('2012-0425', ValueError), # Inconsistent date separators ('201204-25', ValueError), # Inconsistent date separators ('20120425T0120:00', ValueError), # Inconsistent time separators + ('20120425T01:2000', ValueError), # Inconsistent time separators + ('14:3015', ValueError), # Inconsistent time separator ('20120425T012500-334', ValueError), # Wrong microsecond separator ('2001-1', ValueError), # YYYY-M not valid ('2012-04-9', ValueError), # YYYY-MM-D not valid @@ -286,17 +288,6 @@ def test_iso_with_sep_raises(sep_act, valid_sep, exception): parser.isoparse(isostr) -@pytest.mark.xfail() -@pytest.mark.parametrize('isostr,exception', [ # pragma: nocover - ('20120425T01:2000', ValueError), # Inconsistent time separators -]) -def test_iso_raises_failing(isostr, exception): - # These are test cases where the current implementation is too lenient - # and need to be fixed - with pytest.raises(exception): - isoparse(isostr) - - ### # Test ISOParser constructor @pytest.mark.parametrize('sep', [' ', '9', '🍛']) @@ -397,6 +388,7 @@ def test_parse_isodate(d, dt_fmt, as_bytes): ('2013-02-29', ValueError), # Not a leap year ('2014/12/03', ValueError), # Wrong separators ('2014-04-19T', ValueError), # Unknown components + ('201202', ValueError), # Invalid format ]) def test_isodate_raises(isostr, exception): with pytest.raises(exception): @@ -515,14 +507,3 @@ def test_isotime_raises(isostr, exception): iparser = isoparser() with pytest.raises(exception): iparser.parse_isotime(isostr) - - -@pytest.mark.xfail() -@pytest.mark.parametrize('isostr,exception', [ # pragma: nocover - ('14:3015', ValueError), # Inconsistent separator use - ('201202', ValueError) # Invalid ISO format -]) -def test_isotime_raises_xfail(isostr, exception): - iparser = isoparser() - with pytest.raises(exception): - iparser.parse_isotime(isostr) |