if len(head) == 1:
raw = head[0]
else:
- if isinstance(nodes, types.GeneratorType):
- nodes = chain(head, nodes)
- raw = "".join([str(v) for v in nodes])
- raw = u"".join([text_type(v) for v in chain(head, nodes)])
++ raw = "".join([str(v) for v in chain(head, nodes)])
try:
- literal = literal_eval(raw)
+ return literal_eval(raw)
except (ValueError, SyntaxError, MemoryError):
return raw
class NativeCodeGenerator(CodeGenerator):
"""A code generator which renders Python types by not adding
- ``str()`` around output nodes, and using :func:`native_concat`
- to convert complex strings back to Python types if possible.
- ``to_string()`` around output nodes.
++ ``str()`` around output nodes.
"""
@staticmethod
return value
def _output_const_repr(self, group):
- return repr(native_concat(group))
- return repr(u"".join([text_type(v) for v in group]))
++ return repr("".join([str(v) for v in group]))
def _output_child_to_const(self, node, frame, finalize):
const = node.as_const(frame.eval_ctx)
${item} ## the rest of the stuff
<%endfor%>"""
)
- assert tmpl.render(seq=range(5)) == "".join("%s\n" % x for x in range(5))
+ assert tmpl.render(seq=range(5)) == "".join(f"{x}\n" for x in range(5))
+ def test_lstrip_blocks_outside_with_new_line(self):
+ env = Environment(lstrip_blocks=True, trim_blocks=False)
+ tmpl = env.from_string(
+ " {% if kvs %}(\n"
+ " {% for k, v in kvs %}{{ k }}={{ v }} {% endfor %}\n"
+ " ){% endif %}"
+ )
+ out = tmpl.render(kvs=[("a", 1), ("b", 2)])
+ assert out == "(\na=1 b=2 \n )"
+
+ def test_lstrip_trim_blocks_outside_with_new_line(self):
+ env = Environment(lstrip_blocks=True, trim_blocks=True)
+ tmpl = env.from_string(
+ " {% if kvs %}(\n"
+ " {% for k, v in kvs %}{{ k }}={{ v }} {% endfor %}\n"
+ " ){% endif %}"
+ )
+ out = tmpl.render(kvs=[("a", 1), ("b", 2)])
+ assert out == "(\na=1 b=2 )"
+
+ def test_lstrip_blocks_inside_with_new_line(self):
+ env = Environment(lstrip_blocks=True, trim_blocks=False)
+ tmpl = env.from_string(
+ " ({% if kvs %}\n"
+ " {% for k, v in kvs %}{{ k }}={{ v }} {% endfor %}\n"
+ " {% endif %})"
+ )
+ out = tmpl.render(kvs=[("a", 1), ("b", 2)])
+ assert out == " (\na=1 b=2 \n)"
+
+ def test_lstrip_trim_blocks_inside_with_new_line(self):
+ env = Environment(lstrip_blocks=True, trim_blocks=True)
+ tmpl = env.from_string(
+ " ({% if kvs %}\n"
+ " {% for k, v in kvs %}{{ k }}={{ v }} {% endfor %}\n"
+ " {% endif %})"
+ )
+ out = tmpl.render(kvs=[("a", 1), ("b", 2)])
+ assert out == " (a=1 b=2 )"
+
+ def test_lstrip_blocks_without_new_line(self):
+ env = Environment(lstrip_blocks=True, trim_blocks=False)
+ tmpl = env.from_string(
+ " {% if kvs %}"
+ " {% for k, v in kvs %}{{ k }}={{ v }} {% endfor %}"
+ " {% endif %}"
+ )
+ out = tmpl.render(kvs=[("a", 1), ("b", 2)])
+ assert out == " a=1 b=2 "
+
+ def test_lstrip_trim_blocks_without_new_line(self):
+ env = Environment(lstrip_blocks=True, trim_blocks=True)
+ tmpl = env.from_string(
+ " {% if kvs %}"
+ " {% for k, v in kvs %}{{ k }}={{ v }} {% endfor %}"
+ " {% endif %}"
+ )
+ out = tmpl.render(kvs=[("a", 1), ("b", 2)])
+ assert out == " a=1 b=2 "
+
+ def test_lstrip_blocks_consume_after_without_new_line(self):
+ env = Environment(lstrip_blocks=True, trim_blocks=False)
+ tmpl = env.from_string(
+ " {% if kvs -%}"
+ " {% for k, v in kvs %}{{ k }}={{ v }} {% endfor -%}"
+ " {% endif -%}"
+ )
+ out = tmpl.render(kvs=[("a", 1), ("b", 2)])
+ assert out == "a=1 b=2 "
+
+ def test_lstrip_trim_blocks_consume_before_without_new_line(self):
+ env = Environment(lstrip_blocks=False, trim_blocks=False)
+ tmpl = env.from_string(
+ " {%- if kvs %}"
+ " {%- for k, v in kvs %}{{ k }}={{ v }} {% endfor -%}"
+ " {%- endif %}"
+ )
+ out = tmpl.render(kvs=[("a", 1), ("b", 2)])
+ assert out == "a=1 b=2 "
+
+ def test_lstrip_trim_blocks_comment(self):
+ env = Environment(lstrip_blocks=True, trim_blocks=True)
+ tmpl = env.from_string(" {# 1 space #}\n {# 2 spaces #} {# 4 spaces #}")
+ out = tmpl.render()
+ assert out == " " * 4
+
+ def test_lstrip_trim_blocks_raw(self):
+ env = Environment(lstrip_blocks=True, trim_blocks=True)
+ tmpl = env.from_string("{{x}}\n{%- raw %} {% endraw -%}\n{{ y }}")
+ out = tmpl.render(x=1, y=2)
+ assert out == "1 2"
+
def test_php_syntax_with_manual(self, env):
env = Environment(
"<?", "?>", "<?=", "?>", "<!--", "-->", lstrip_blocks=True, trim_blocks=True
++import math
++
import pytest
-from jinja2._compat import text_type
from jinja2.exceptions import UndefinedError
from jinja2.nativetypes import NativeEnvironment
from jinja2.nativetypes import NativeTemplate
assert result == "--host='localhost' --user \"Jinja\""
- assert result < 0.007 # TODO use math.isclose in Python 3
+ def test_no_intermediate_eval(env):
+ t = env.from_string("0.000{{ a }}")
+ result = t.render(a=7)
+ assert isinstance(result, float)
+ # If intermediate eval happened, 0.000 would render 0.0, then 7
+ # would be appended, resulting in 0.07.
++ assert math.isclose(result, 0.0007)
+
+
def test_spontaneous_env():
t = NativeTemplate("{{ true }}")
assert isinstance(t.environment, NativeEnvironment)