From b7029fde7955050fcc049d0073bea1b61a24beb2 Mon Sep 17 00:00:00 2001 From: Marco Faedo Date: Fri, 16 Oct 2020 09:52:54 +0000 Subject: [PATCH 1/2] Fix parsing exception when trailing/leading slash --- pendulum/parsing/__init__.py | 2 +- tests/parsing/test_parsing.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/pendulum/parsing/__init__.py b/pendulum/parsing/__init__.py index 1b5c8b91..57a5c010 100644 --- a/pendulum/parsing/__init__.py +++ b/pendulum/parsing/__init__.py @@ -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("/") diff --git a/tests/parsing/test_parsing.py b/tests/parsing/test_parsing.py index bcd7a53e..9b61dc64 100644 --- a/tests/parsing/test_parsing.py +++ b/tests/parsing/test_parsing.py @@ -670,6 +670,17 @@ 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" From 4ea0b90962a5aa26f5713c56cd2beb65bdeeb9d7 Mon Sep 17 00:00:00 2001 From: Marco Faedo Date: Fri, 16 Oct 2020 10:19:40 +0000 Subject: [PATCH 2/2] Make linter happy --- pendulum/__init__.py | 5 ++++- tests/datetime/test_from_format.py | 3 ++- tests/parsing/test_parsing.py | 1 + 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/pendulum/__init__.py b/pendulum/__init__.py index bb1e0ca7..b19f87d5 100644 --- a/pendulum/__init__.py +++ b/pendulum/__init__.py @@ -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. diff --git a/tests/datetime/test_from_format.py b/tests/datetime/test_from_format.py index 0949332c..398b68da 100644 --- a/tests/datetime/test_from_format.py +++ b/tests/datetime/test_from_format.py @@ -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", ) diff --git a/tests/parsing/test_parsing.py b/tests/parsing/test_parsing.py index 9b61dc64..9387667a 100644 --- a/tests/parsing/test_parsing.py +++ b/tests/parsing/test_parsing.py @@ -682,6 +682,7 @@ def test_invalid(): with pytest.raises(ParserError): parse(text) + def test_exif_edge_case(): text = "2016:12:26 15:45:28"