From fac17dc6284a8604870747be66d76677776a716b Mon Sep 17 00:00:00 2001 From: Kyle Altendorf Date: Sat, 10 Aug 2019 08:32:57 -0400 Subject: [PATCH] Reject text after matching string in from_format() (#372) * Reject text after matching string in from_format() * Let black have its way --- pendulum/formatting/formatter.py | 2 +- tests/datetime/test_from_format.py | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/pendulum/formatting/formatter.py b/pendulum/formatting/formatter.py index 76a4db4f..b41873ca 100644 --- a/pendulum/formatting/formatter.py +++ b/pendulum/formatting/formatter.py @@ -403,7 +403,7 @@ def parse( lambda m: self._replace_tokens(m.group(0), locale), escaped_fmt ) - if not re.match(pattern, time): + if not re.search("^" + pattern + "$", time): raise ValueError("String does not match format {}".format(fmt)) re.sub(pattern, lambda m: self._get_parsed_values(m, parsed, locale, now), time) diff --git a/tests/datetime/test_from_format.py b/tests/datetime/test_from_format.py index c5aeaa27..06404548 100644 --- a/tests/datetime/test_from_format.py +++ b/tests/datetime/test_from_format.py @@ -13,6 +13,11 @@ def test_from_format_returns_datetime(): assert "UTC" == d.timezone_name +def test_from_format_rejects_extra_text(): + with pytest.raises(ValueError): + pendulum.from_format("1975-05-21 22:32:11 extra text", "YYYY-MM-DD HH:mm:ss") + + def test_from_format_with_timezone_string(): d = pendulum.from_format( "1975-05-21 22:32:11", "YYYY-MM-DD HH:mm:ss", tz="Europe/London"