]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Add requested patch for safe_repr and nodes.Impossible
authorJames Tanner <tanner.jc@gmail.com>
Wed, 26 Jul 2017 01:48:12 +0000 (21:48 -0400)
committerJames Tanner <tanner.jc@gmail.com>
Wed, 26 Jul 2017 01:48:12 +0000 (21:48 -0400)
jinja2/nativetypes.py
tests/test_nativetypes.py

index 3aca683cdb318059ae1da3ad349f5241fb4883df..fe17e4138df3f1c1d3cf8d207135c847f5d66615 100644 (file)
@@ -3,7 +3,7 @@ from ast import literal_eval
 from itertools import islice, chain
 from jinja2 import nodes
 from jinja2._compat import text_type
-from jinja2.compiler import CodeGenerator
+from jinja2.compiler import CodeGenerator, has_safe_repr
 from jinja2.environment import Environment, Template
 from jinja2.utils import concat, escape
 
@@ -78,6 +78,8 @@ class NativeCodeGenerator(CodeGenerator):
                     raise nodes.Impossible()
 
                 const = child.as_const(frame.eval_ctx)
+                if not has_safe_repr(const):
+                    raise nodes.Impossible()
             except nodes.Impossible:
                 body.append(child)
                 continue
index d295466c9b6ea98052dd670a5b64ab5d80dfe0a1..8bff503807fe6335f7ffafdcf9923b7100a8707d 100644 (file)
@@ -92,3 +92,19 @@ class TestNativeEnvironment(object):
         result = t.render()
         assert isinstance(result, bool)
         assert result is False
+
+    def test_variable_dunder(self, env):
+        t = env.from_string("{{ x.__class__ }}")
+        result = t.render(x=True)
+        assert isinstance(result, type)
+
+    def test_constant_dunder(self, env):
+        t = env.from_string("{{ true.__class__ }}")
+        result = t.render()
+        assert isinstance(result, type)
+
+    def test_constant_dunder_to_string(self, env):
+        t = env.from_string("{{ true.__class__|string }}")
+        result = t.render()
+        assert not isinstance(result, type)
+        assert result == "<type 'bool'>"