]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Fix bug with when using Markup on ChainableUndefined 1047/head
authorLaurence de Bruxelles <lfdebrux@gmail.com>
Sat, 10 Aug 2019 21:08:39 +0000 (22:08 +0100)
committerDavid Lord <davidism@gmail.com>
Fri, 4 Oct 2019 20:06:12 +0000 (13:06 -0700)
Wrapping a ChainableUndefined object with Markup causes an
UndefinedError because Markup thinks that ChainableUndefined has an
attribute called __html__ and tries to call it.

This commit fixes this by defining a method __html__ that calls
ChainableUndefined.__str__. We also add a regression test.

jinja2/runtime.py
tests/test_regression.py

index b37465a950f1fa8d5fff233c4e968f58f234865e..ca391bc67f101835151f1fc25b66864c4f739314 100644 (file)
@@ -753,9 +753,9 @@ def make_logging_undefined(logger=None, base=None):
 # is not overwritten from Undefined in this class.
 # This would cause a recursion error in Python 2.
 class ChainableUndefined(Undefined):
-    """An undefined that is chainable, where both
-    __getattr__ and __getitem__ return itself rather than
-    raising an :exc:`UndefinedError`:
+    """An undefined that is chainable, where both ``__getattr__`` and
+    ``__getitem__`` return itself rather than raising an
+    :exc:`UndefinedError`.
 
     >>> foo = ChainableUndefined(name='foo')
     >>> str(foo.bar['baz'])
@@ -765,10 +765,13 @@ class ChainableUndefined(Undefined):
       ...
     jinja2.exceptions.UndefinedError: 'foo' is undefined
 
-    .. versionadded:: 2.11
+    .. versionadded:: 2.11.0
     """
     __slots__ = ()
 
+    def __html__(self):
+        return self.__str__()
+
     def __getattr__(self, _):
         return self
 
index d540d2dd1c73ff920bc95b7c51f9366ae8c1a434..7477db2f867ad666ec40d06db4136e35140c3767 100644 (file)
@@ -543,3 +543,8 @@ class TestBug(object):
             {%- for value in values recursive %}1{% else %}0{% endfor -%}
         ''')
         assert tmpl.render(values=[]) == '0'
+
+    def test_markup_and_chainable_undefined(self):
+        from jinja2 import Markup
+        from jinja2.runtime import ChainableUndefined
+        assert str(Markup(ChainableUndefined())) == ''