namespace object works in async templates

This commit is contained in:
Bart Feenstra 2020-03-28 16:18:58 +00:00 committed by David Lord
parent 5f95471a68
commit 0fd45a4dc7
No known key found for this signature in database
GPG Key ID: 7A1C87E3F5BC42A8
3 changed files with 15 additions and 1 deletions

View File

@ -23,6 +23,8 @@ Unreleased
:pr:`1182`
- Fix line numbers in error messages when newlines are stripped.
:pr:`1178`
- The special ``namespace()`` assignment object in templates works in
async environments. :issue:`1180`
Version 2.11.1

View File

@ -697,7 +697,8 @@ class Namespace(object):
self.__attrs = dict(*args, **kwargs)
def __getattribute__(self, name):
if name == "_Namespace__attrs":
# __class__ is needed for the awaitable check in async mode
if name in {"_Namespace__attrs", "__class__"}:
return object.__getattribute__(self, name)
try:
return self.__attrs[name]

View File

@ -578,3 +578,14 @@ class TestAsyncForLoop(object):
def test_awaitable_property_slicing(self, test_env_async):
t = test_env_async.from_string("{% for x in a.b[:1] %}{{ x }}{% endfor %}")
assert t.render(a=dict(b=[1, 2, 3])) == "1"
def test_namespace_awaitable(test_env_async):
async def _test():
t = test_env_async.from_string(
'{% set ns = namespace(foo="Bar") %}{{ ns.foo }}'
)
actual = await t.render_async()
assert actual == "Bar"
run(_test())