summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMario Corchero <mcorcherojim@bloomberg.net>2021-05-16 22:35:11 +0200
committerMario Corchero <mcorcherojim@bloomberg.net>2021-05-19 21:00:32 +0200
commit34906dcd4112197c8f6386f799c10b8a34cb248f (patch)
tree587de2ccd383150ceed35744a1b81c689d4df886
parent9edac50ef325156e06356304087feeb20e4b7d4a (diff)
downloaddateutil-git-34906dcd4112197c8f6386f799c10b8a34cb248f.tar.gz
isoparse: Fail with inconsistent time separators
Fail when separators are used inconsistently to split the time porting of a string. Even if more restrictive, we have warned that we were going to fail on invalid cases of ISO formatted strings. This will prevent invalid iso formatted strings from being unexpectedly parsed.
-rw-r--r--changelog.d/1125.bugfix.rst1
-rw-r--r--dateutil/parser/isoparser.py13
-rw-r--r--dateutil/test/test_isoparser.py14
3 files changed, 12 insertions, 16 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 48f86a3..64a54ff 100644
--- a/dateutil/parser/isoparser.py
+++ b/dateutil/parser/isoparser.py
@@ -336,7 +336,7 @@ class isoparser(object):
if len(timestr) < 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 e81db3a..0c91db7 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', '🍛'])
@@ -507,7 +498,6 @@ def test_isotime_raises(isostr, exception):
@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):