Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions arrow/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,12 +422,21 @@ def parse(
parts: _Parts = {}
for token in fmt_tokens:
value: Union[Tuple[str, str, str], str]
if token == "Do":
value = match.group("value")
elif token == "W":
value = (match.group("year"), match.group("week"), match.group("day"))
else:
value = match.group(token)
try:
if token == "Do":
value = match.group("value")
elif token == "W":
value = (
match.group("year"),
match.group("week"),
match.group("day"),
)
else:
value = match.group(token)
except IndexError:
raise ParserMatchError(
f"Failed to match {fmt!r} when parsing {datetime_string!r}."
)

if value is None:
raise ParserMatchError(
Expand Down
8 changes: 8 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,14 @@ def test_parse_with_leading_and_trailing_whitespace(self):

assert self.parser.parse(" 2016 ", "YYYY") == datetime(2016, 1, 1)

def test_parse_Do_indexerror_becomes_parser_match_error(self):
"""Regression for #1191: On locales whose ordinal_day_re does not expose
a named 'value' capture group (e.g. GermanLocale), match.group('value')
previously raised a bare IndexError. It must raise ParserMatchError."""
de_parser = DateTimeParser(locale="de")
with pytest.raises(ParserMatchError):
de_parser.parse("1", "Do")

assert self.parser.parse(
" 2016-05-16 04:05:06.789120 ", "YYYY-MM-DD hh:mm:ss.S"
) == datetime(2016, 5, 16, 4, 5, 6, 789120)
Expand Down
Loading