]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
use soft_str in do_wordcount, to trigger undefined 1160/head
authorMatt Martz <matt@sivel.net>
Thu, 27 Feb 2020 18:46:58 +0000 (12:46 -0600)
committerDavid Lord <davidism@gmail.com>
Thu, 27 Feb 2020 19:06:35 +0000 (11:06 -0800)
CHANGES.rst
src/jinja2/filters.py
tests/test_filters.py

index 651ab43089dbc0d32ebfb4d349a88644236e56b2..9635fc5588264b9276d3e389b5c53843a7b0cb50 100644 (file)
@@ -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
index 1af7ac88a7c9aeea80b979982818addc5444612f..974156735123b88ed996997cf18e2db03bc3c7be 100644 (file)
@@ -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):
index 37dde44d5c2939289c29bddf6df1674fa6a39021..388c346212ee1a5db15b8403c2afa278cb5690e0 100644 (file)
@@ -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 %}<HEHE>{% endfilter %}")
         assert tmpl.render() == "&lt;hehe&gt;"