Skip to content

Commit

Permalink
Fix parsing invalid intervals (#843)
Browse files Browse the repository at this point in the history
* Fix parsing invalid intervals

Parsing string with slash at the start or the end raised IndexError
instead of ParseError.

* Apply suggestion from review
  • Loading branch information
wiget authored Dec 4, 2024
1 parent 51fb4c3 commit 6205754
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/pendulum/parsing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,11 @@ def _parse_iso8601_interval(text: str) -> _Interval:
first, last = text.split("/")
start = end = duration = None

if first[0] == "P":
if first[:1] == "P":
# duration/end
duration = parse_iso8601(first)
end = parse_iso8601(last)
elif last[0] == "P":
elif last[:1] == "P":
# start/duration
start = parse_iso8601(first)
duration = parse_iso8601(last)
Expand Down
10 changes: 10 additions & 0 deletions tests/parsing/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,16 @@ def test_invalid():
with pytest.raises(ParserError):
parse(text)

text = "/2012"

with pytest.raises(ParserError):
parse(text)

text = "2012/"

with pytest.raises(ParserError):
parse(text)


def test_exif_edge_case():
text = "2016:12:26 15:45:28"
Expand Down

0 comments on commit 6205754

Please sign in to comment.