Skip to content

Fix parsing exception when trailing/leading slash #505

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
5 changes: 4 additions & 1 deletion pendulum/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,10 @@ def yesterday(tz="local"): # type: (Union[str, _Timezone]) -> DateTime


def from_format(
string, fmt, tz=UTC, locale=None, # noqa
string,
fmt,
tz=UTC,
locale=None, # noqa
): # type: (str, str, Union[str, _Timezone], Optional[str]) -> DateTime
"""
Creates a DateTime instance from a specific format.
Expand Down
2 changes: 1 addition & 1 deletion pendulum/parsing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def __init__(self, start=None, end=None, duration=None):


def _parse_iso8601_interval(text):
if "/" not in text:
if text.count("/") != 1 or text.startswith("/") or text.endswith("/"):
raise ParserError("Invalid interval")

first, last = text.split("/")
Expand Down
3 changes: 2 additions & 1 deletion tests/datetime/test_from_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def test_from_format_with_timezone():
def test_from_format_with_square_bracket_in_timezone():
with pytest.raises(ValueError, match="^String does not match format"):
pendulum.from_format(
"1975-05-21 22:32:11 Eu[rope/London", "YYYY-MM-DD HH:mm:ss z",
"1975-05-21 22:32:11 Eu[rope/London",
"YYYY-MM-DD HH:mm:ss z",
)


Expand Down
12 changes: 12 additions & 0 deletions tests/parsing/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,18 @@ def test_invalid():
with pytest.raises(ParserError):
parse(text)

# Incomplete date
text = "/2020"

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

# Incomplete date
text = "2020/"

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


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