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
24 changes: 18 additions & 6 deletions arrow/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,12 +422,24 @@ 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:
# A malformed format can leave a token in ``fmt_tokens`` without a
# corresponding capture group in the compiled pattern. Surface this
# as a ParserError rather than letting a raw IndexError escape.
raise ParserMatchError(
f"Unable to find a match group for the specified token {token!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 @@ -191,6 +191,14 @@ def test_parse_parse_no_match(self):
with pytest.raises(ParserError):
self.parser.parse("01-01", "YYYY-MM-DD")

def test_parse_malformed_fmt_no_match_group(self):
# A malformed format string can leave a token without a corresponding
# capture group in the compiled pattern; this must raise a ParserError
# rather than leaking a raw IndexError from match.group().
# Regression test for https://github.com/arrow-py/arrow/issues/1191
with pytest.raises(ParserMatchError):
self.parser.parse("foo", "[|(\\]s")

def test_parse_separators(self):
with pytest.raises(ParserError):
self.parser.parse("1403549231", "YYYY-MM-DD")
Expand Down
Loading