]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Merge branch '2.11.x'
authorDavid Lord <davidism@gmail.com>
Mon, 13 Apr 2020 16:26:39 +0000 (09:26 -0700)
committerDavid Lord <davidism@gmail.com>
Mon, 13 Apr 2020 16:26:39 +0000 (09:26 -0700)
1  2 
CHANGES.rst
src/jinja2/lexer.py
src/jinja2/nativetypes.py
tests/test_lexnparse.py
tests/test_nativetypes.py

diff --cc CHANGES.rst
index 1911ab40da3ebfaed682917810efbedf559eb429,9b8b24ee030ca9cf415ad0e91d5d42990b6b0d11..57de4ae59df43989730695028a4addb4f8277686
@@@ -1,22 -1,9 +1,22 @@@
  .. currentmodule:: jinja2
  
- 2.11.2
- ------
 +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`
 +
 +
+ 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
Simple merge
index e0ad94d3248e5a5abc3d57648848c578fe61f4f9,a9ead4e2bbf09b881993b4c7112d83c684604c5c..5ecf72b598ce19f3dd0cf80c331e0c702c71d1b1
@@@ -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)
index 6057416971c9c9f4df9f8bbdd51a505e2b23d4f6,83ae75e05ce02dcb7bd920ffdb82876288d9f4ad..c0257cf1d9a4660d5ac9600d34de751debdb6424
@@@ -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
index 947168ce521a213ead72a3258e778c7a8c764525,76871ab5de4bebf8d1dbd4c6bd010f1ee8ca75d9..22581813c009fab66e37a3f50996bc17b9289138
@@@ -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\""
  
  
 -    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)