Skip to content

Commit

Permalink
Reject text after matching string in from_format() (#372)
Browse files Browse the repository at this point in the history
* Reject text after matching string in from_format()

* Let black have its way
  • Loading branch information
altendky authored and sdispater committed Aug 10, 2019
1 parent f629fa6 commit fac17dc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pendulum/formatting/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions tests/datetime/test_from_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit fac17dc

Please sign in to comment.