From: David Lord Date: Mon, 13 Apr 2020 16:26:39 +0000 (-0700) Subject: Merge branch '2.11.x' X-Git-Tag: 3.0.0rc1~108 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=da812816ff1a459eefa7ca946b4c108cc7106c85;p=thirdparty%2Fjinja.git Merge branch '2.11.x' --- da812816ff1a459eefa7ca946b4c108cc7106c85 diff --cc CHANGES.rst index 1911ab40,9b8b24ee..57de4ae5 --- a/CHANGES.rst +++ b/CHANGES.rst @@@ -1,22 -1,9 +1,22 @@@ .. currentmodule:: jinja2 +Version 3.0.0 +------------- + +Unreleased + +- Drop support for Python 2.7 and 3.5. +- Bump MarkupSafe dependency to >=1.1. +- Bump Babel optional dependency to >=2.1. +- Remove code that was marked deprecated. +- Use :pep:`451` API to load templates with + :class:`~loaders.PackageLoader`. :issue:`1168` + + - 2.11.2 - ------ + Version 2.11.2 + -------------- - Unreleased + Released 2020-04-13 - Fix a bug that caused callable objects with ``__getattr__``, like :class:`~unittest.mock.Mock` to be treated as a diff --cc src/jinja2/nativetypes.py index e0ad94d3,a9ead4e2..5ecf72b5 --- a/src/jinja2/nativetypes.py +++ b/src/jinja2/nativetypes.py @@@ -30,30 -27,17 +26,17 @@@ def native_concat(nodes) 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 @@@ -61,7 -45,7 +44,7 @@@ 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) diff --cc tests/test_lexnparse.py index 60574169,83ae75e0..c0257cf1 --- a/tests/test_lexnparse.py +++ b/tests/test_lexnparse.py @@@ -711,8 -727,100 +711,100 @@@ ${item} ## the rest of the stuf ${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 diff --cc tests/test_nativetypes.py index 947168ce,76871ab5..22581813 --- a/tests/test_nativetypes.py +++ b/tests/test_nativetypes.py @@@ -1,5 -1,6 +1,7 @@@ ++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 @@@ -133,6 -134,15 +135,15 @@@ def test_concat_strings_with_quotes(env assert result == "--host='localhost' --user \"Jinja\"" + 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 result < 0.007 # TODO use math.isclose in Python 3 ++ assert math.isclose(result, 0.0007) + + def test_spontaneous_env(): t = NativeTemplate("{{ true }}") assert isinstance(t.environment, NativeEnvironment)