From: James Tanner Date: Wed, 26 Jul 2017 01:48:12 +0000 (-0400) Subject: Add requested patch for safe_repr and nodes.Impossible X-Git-Tag: 2.10~3^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2ffeb693e337828e9955ab1371cf20a81bef0459;p=thirdparty%2Fjinja.git Add requested patch for safe_repr and nodes.Impossible --- diff --git a/jinja2/nativetypes.py b/jinja2/nativetypes.py index 3aca683c..fe17e413 100644 --- a/jinja2/nativetypes.py +++ b/jinja2/nativetypes.py @@ -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 diff --git a/tests/test_nativetypes.py b/tests/test_nativetypes.py index d295466c..8bff5038 100644 --- a/tests/test_nativetypes.py +++ b/tests/test_nativetypes.py @@ -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 == ""