Skip to content

Commit

Permalink
Merge pull request #1495 from daniel-stoneuk/patch-async-globals
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism authored Oct 4, 2021
2 parents fb9cac6 + baff54f commit 355ef53
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 34 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Version 3.0.2
names. :issue:`1452, 1453`
- Revert an unintended change that caused ``Undefined`` to act like
``StrictUndefined`` for the ``in`` operator. :issue:`1448`
- Imported macros have access to the current template globals in async
environments. :issue:`1494`


Version 3.0.1
Expand Down
4 changes: 1 addition & 3 deletions src/jinja2/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1090,10 +1090,8 @@ def _import_common(
self.write(
f"{f_name}(context.get_all(), True, {self.dump_local_context(frame)})"
)
elif self.environment.is_async:
self.write("_get_default_module_async()")
else:
self.write("_get_default_module(context)")
self.write(f"_get_default_module{self.choose_async('_async')}(context)")

def visit_Import(self, node: nodes.Import, frame: Frame) -> None:
"""Visit regular imports."""
Expand Down
23 changes: 23 additions & 0 deletions tests/test_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,29 @@ def test_exports(self, test_env_async):
assert m.variable == 42
assert not hasattr(m, "notthere")

def test_import_with_globals(self, test_env_async):
t = test_env_async.from_string(
'{% import "module" as m %}{{ m.test() }}', globals={"foo": 42}
)
assert t.render() == "[42|23]"

t = test_env_async.from_string('{% import "module" as m %}{{ m.test() }}')
assert t.render() == "[|23]"

def test_import_with_globals_override(self, test_env_async):
t = test_env_async.from_string(
'{% set foo = 41 %}{% import "module" as m %}{{ m.test() }}',
globals={"foo": 42},
)
assert t.render() == "[42|23]"

def test_from_import_with_globals(self, test_env_async):
t = test_env_async.from_string(
'{% from "module" import test %}{{ test() }}',
globals={"foo": 42},
)
assert t.render() == "[42|23]"


class TestAsyncIncludes:
def test_context_include(self, test_env_async):
Expand Down
44 changes: 13 additions & 31 deletions tests/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,45 +99,27 @@ def test_not_exported(self, test_env):
t.render()

def test_import_with_globals(self, test_env):
env = Environment(
loader=DictLoader(
{
"macros": "{% macro test() %}foo: {{ foo }}{% endmacro %}",
"test": "{% import 'macros' as m %}{{ m.test() }}",
"test1": "{% import 'macros' as m %}{{ m.test() }}",
}
)
t = test_env.from_string(
'{% import "module" as m %}{{ m.test() }}', globals={"foo": 42}
)
tmpl = env.get_template("test", globals={"foo": "bar"})
assert tmpl.render() == "foo: bar"
assert t.render() == "[42|23]"

tmpl = env.get_template("test1")
assert tmpl.render() == "foo: "
t = test_env.from_string('{% import "module" as m %}{{ m.test() }}')
assert t.render() == "[|23]"

def test_import_with_globals_override(self, test_env):
env = Environment(
loader=DictLoader(
{
"macros": "{% set foo = '42' %}{% macro test() %}"
"foo: {{ foo }}{% endmacro %}",
"test": "{% from 'macros' import test %}{{ test() }}",
}
)
t = test_env.from_string(
'{% set foo = 41 %}{% import "module" as m %}{{ m.test() }}',
globals={"foo": 42},
)
tmpl = env.get_template("test", globals={"foo": "bar"})
assert tmpl.render() == "foo: 42"
assert t.render() == "[42|23]"

def test_from_import_with_globals(self, test_env):
env = Environment(
loader=DictLoader(
{
"macros": "{% macro testing() %}foo: {{ foo }}{% endmacro %}",
"test": "{% from 'macros' import testing %}{{ testing() }}",
}
)
t = test_env.from_string(
'{% from "module" import test %}{{ test() }}',
globals={"foo": 42},
)
tmpl = env.get_template("test", globals={"foo": "bar"})
assert tmpl.render() == "foo: bar"
assert t.render() == "[42|23]"


class TestIncludes:
Expand Down

0 comments on commit 355ef53

Please sign in to comment.