native: keep same behavior on Python 3.10

This commit is contained in:
Martin Krizek 2021-11-09 17:15:31 +01:00 committed by David Lord
parent a4e2532489
commit bb0db82b91
No known key found for this signature in database
GPG Key ID: 7A1C87E3F5BC42A8
3 changed files with 15 additions and 1 deletions

View File

@ -7,6 +7,8 @@ Unreleased
- Fix traceback rewriting internals for Python 3.10 and 3.11.
:issue:`1535`
- Fix how the native environment treats leading and trailing spaces
when parsing values on Python 3.10. :pr:`1537`
Version 3.0.2

View File

@ -1,5 +1,6 @@
import typing as t
from ast import literal_eval
from ast import parse
from itertools import chain
from itertools import islice
@ -33,7 +34,12 @@ def native_concat(values: t.Iterable[t.Any]) -> t.Optional[t.Any]:
raw = "".join([str(v) for v in chain(head, values)])
try:
return literal_eval(raw)
return literal_eval(
# In Python 3.10+ ast.literal_eval removes leading spaces/tabs
# from the given string. For backwards compatibility we need to
# parse the string ourselves without removing leading spaces/tabs.
parse(raw, mode="eval")
)
except (ValueError, SyntaxError, MemoryError):
return raw

View File

@ -147,3 +147,9 @@ def test_no_intermediate_eval(env):
def test_spontaneous_env():
t = NativeTemplate("{{ true }}")
assert isinstance(t.environment, NativeEnvironment)
def test_leading_spaces(env):
t = env.from_string(" {{ True }}")
result = t.render()
assert result == " True"