Skip to content
Myr

Merge pull request #1537 from mkrizek/literal_eval-py-310

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Lord · committed by GitHub · parents a4e2532, bb0db82 · Browse files · 3 files+15 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • native: keep same behavior on Python 3.10

Diff stats

3 files+15 added−1 removed

  • tests:+6 added−0 removed
  • other:+9 added−1 removed

3 files+15 added−1 removed

modified:src/jinja2/nativetypes.py+7 added-1 removed
1import typing as t2from ast import literal_eval3from ast import parse4from itertools import chain5from itertools import islice67from . import nodes
25 unmodified lines
33 else:34 raw = "".join([str(v) for v in chain(head, values)])3536 try:36 return literal_eval(raw)37 return literal_eval(38 # In Python 3.10+ ast.literal_eval removes leading spaces/tabs39 # from the given string. For backwards compatibility we need to40 # parse the string ourselves without removing leading spaces/tabs.41 parse(raw, mode="eval")42 )43 except (ValueError, SyntaxError, MemoryError):44 return raw4546
78 unmodified lines
modified:tests/test_nativetypes.py+6 added
145 unmodified lines
146147def test_spontaneous_env():148 t = NativeTemplate("{{ true }}")149 assert isinstance(t.environment, NativeEnvironment)150151152def test_leading_spaces(env):153 t = env.from_string(" {{ True }}")154 result = t.render()155 assert result == " True"
modified:CHANGES.rst+2 added
5 unmodified lines
6Unreleased78- Fix traceback rewriting internals for Python 3.10 and 3.11.9 :issue:`1535`10- Fix how the native environment treats leading and trailing spaces11 when parsing values on Python 3.10. :pr:`1537`121314Version 3.0.215-------------
894 unmodified lines