- 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
import typing as t
from ast import literal_eval
+from ast import parse
from itertools import chain
from itertools import islice
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
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"