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
9 changes: 9 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,15 @@ def test_parse_normalize_whitespace(self):
with pytest.raises(ParserError):
self.parser.parse(" \n Jun 1\t 2005\n ", "MMM D YYYY")

def test_parse_malformed_fmt_no_indexerror(self):
"""Malformed format strings should raise ParserMatchError, not IndexError."""
with pytest.raises(ParserMatchError):
self.parser.parse(
"foo",
"[-FFFFFFFFFFFFFFFFFFFFF-[-FFFFFFFFFFFF"
"[ |||||||| 7 v.dG(dG\\][3zasks &",
)


@pytest.mark.usefixtures("dt_parser_regex")
class TestDateTimeParserRegex:
Expand Down
Loading