From ba080f51e0d7e20a7870573d1814eeccbc32d99a Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Thu, 27 Feb 2020 12:46:58 -0600 Subject: [PATCH] use soft_str in do_wordcount, to trigger undefined --- CHANGES.rst | 3 ++- src/jinja2/filters.py | 2 +- tests/test_filters.py | 7 +++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 651ab430..9635fc55 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -8,7 +8,8 @@ Unreleased - Fix a bug that caused callable objects with ``__getattr__``, like :class:`~unittest.mock.Mock` to be treated as a :func:`contextfunction`. :issue:`1145` - +- Update ``wordcount`` filter to trigger :class:`Undefined` methods + by wrapping the input in :func:`soft_unicode`. :pr:`1160` Version 2.11.1 diff --git a/src/jinja2/filters.py b/src/jinja2/filters.py index 1af7ac88..97415673 100644 --- a/src/jinja2/filters.py +++ b/src/jinja2/filters.py @@ -761,7 +761,7 @@ def do_wordwrap( def do_wordcount(s): """Count the words in that string.""" - return len(_word_re.findall(s)) + return len(_word_re.findall(soft_unicode(s))) def do_int(value, default=0, base=10): diff --git a/tests/test_filters.py b/tests/test_filters.py index 37dde44d..388c3462 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -6,6 +6,8 @@ import pytest from jinja2 import Environment from jinja2 import Markup +from jinja2 import StrictUndefined +from jinja2 import UndefinedError from jinja2._compat import implements_to_string from jinja2._compat import text_type @@ -369,6 +371,11 @@ class TestFilter(object): tmpl = env.from_string('{{ "foo bar baz"|wordcount }}') assert tmpl.render() == "3" + strict_env = Environment(undefined=StrictUndefined) + t = strict_env.from_string("{{ s|wordcount }}") + with pytest.raises(UndefinedError): + t.render() + def test_block(self, env): tmpl = env.from_string("{% filter lower|escape %}{% endfilter %}") assert tmpl.render() == "<hehe>" -- 2.47.2