Skip to content

Commit baff54f

Browse files
daniel-stoneukdavidism
authored andcommitted
imported macros can access template globals in async mode
1 parent fb9cac6 commit baff54f

File tree

4 files changed

+39
-34
lines changed

4 files changed

+39
-34
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Version 3.0.2
99
names. :issue:`1452, 1453`
1010
- Revert an unintended change that caused ``Undefined`` to act like
1111
``StrictUndefined`` for the ``in`` operator. :issue:`1448`
12+
- Imported macros have access to the current template globals in async
13+
environments. :issue:`1494`
1214

1315

1416
Version 3.0.1

src/jinja2/compiler.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,10 +1090,8 @@ def _import_common(
10901090
self.write(
10911091
f"{f_name}(context.get_all(), True, {self.dump_local_context(frame)})"
10921092
)
1093-
elif self.environment.is_async:
1094-
self.write("_get_default_module_async()")
10951093
else:
1096-
self.write("_get_default_module(context)")
1094+
self.write(f"_get_default_module{self.choose_async('_async')}(context)")
10971095

10981096
def visit_Import(self, node: nodes.Import, frame: Frame) -> None:
10991097
"""Visit regular imports."""

tests/test_async.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,29 @@ def test_exports(self, test_env_async):
189189
assert m.variable == 42
190190
assert not hasattr(m, "notthere")
191191

192+
def test_import_with_globals(self, test_env_async):
193+
t = test_env_async.from_string(
194+
'{% import "module" as m %}{{ m.test() }}', globals={"foo": 42}
195+
)
196+
assert t.render() == "[42|23]"
197+
198+
t = test_env_async.from_string('{% import "module" as m %}{{ m.test() }}')
199+
assert t.render() == "[|23]"
200+
201+
def test_import_with_globals_override(self, test_env_async):
202+
t = test_env_async.from_string(
203+
'{% set foo = 41 %}{% import "module" as m %}{{ m.test() }}',
204+
globals={"foo": 42},
205+
)
206+
assert t.render() == "[42|23]"
207+
208+
def test_from_import_with_globals(self, test_env_async):
209+
t = test_env_async.from_string(
210+
'{% from "module" import test %}{{ test() }}',
211+
globals={"foo": 42},
212+
)
213+
assert t.render() == "[42|23]"
214+
192215

193216
class TestAsyncIncludes:
194217
def test_context_include(self, test_env_async):

tests/test_imports.py

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -99,45 +99,27 @@ def test_not_exported(self, test_env):
9999
t.render()
100100

101101
def test_import_with_globals(self, test_env):
102-
env = Environment(
103-
loader=DictLoader(
104-
{
105-
"macros": "{% macro test() %}foo: {{ foo }}{% endmacro %}",
106-
"test": "{% import 'macros' as m %}{{ m.test() }}",
107-
"test1": "{% import 'macros' as m %}{{ m.test() }}",
108-
}
109-
)
102+
t = test_env.from_string(
103+
'{% import "module" as m %}{{ m.test() }}', globals={"foo": 42}
110104
)
111-
tmpl = env.get_template("test", globals={"foo": "bar"})
112-
assert tmpl.render() == "foo: bar"
105+
assert t.render() == "[42|23]"
113106

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

117110
def test_import_with_globals_override(self, test_env):
118-
env = Environment(
119-
loader=DictLoader(
120-
{
121-
"macros": "{% set foo = '42' %}{% macro test() %}"
122-
"foo: {{ foo }}{% endmacro %}",
123-
"test": "{% from 'macros' import test %}{{ test() }}",
124-
}
125-
)
111+
t = test_env.from_string(
112+
'{% set foo = 41 %}{% import "module" as m %}{{ m.test() }}',
113+
globals={"foo": 42},
126114
)
127-
tmpl = env.get_template("test", globals={"foo": "bar"})
128-
assert tmpl.render() == "foo: 42"
115+
assert t.render() == "[42|23]"
129116

130117
def test_from_import_with_globals(self, test_env):
131-
env = Environment(
132-
loader=DictLoader(
133-
{
134-
"macros": "{% macro testing() %}foo: {{ foo }}{% endmacro %}",
135-
"test": "{% from 'macros' import testing %}{{ testing() }}",
136-
}
137-
)
118+
t = test_env.from_string(
119+
'{% from "module" import test %}{{ test() }}',
120+
globals={"foo": 42},
138121
)
139-
tmpl = env.get_template("test", globals={"foo": "bar"})
140-
assert tmpl.render() == "foo: bar"
122+
assert t.render() == "[42|23]"
141123

142124

143125
class TestIncludes:

0 commit comments

Comments
 (0)