imported macros can access template globals in async mode
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Daniel Stone · committed by David Lord · parent fb9cac6 · Browse files · 4 files·+39 added−34 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+39 added−34 removed
- tests:+36 added−31 removed
- other:+3 added−3 removed
Provenance
- Commit
baff54f- Tree
8c9e1a6- Author
Daniel Stone <me@danstone.uk>- Committer
David Lord <davidism@gmail.com>- Parent
fb9cac6
4 files changed+39 added−34 removed
modified:src/jinja2/compiler.py+1 added-3 removed
1088 unmodified lines1089 f_name = f"make_module{self.choose_async('_async')}"1090 self.write(1091 f"{f_name}(context.get_all(), True, {self.dump_local_context(frame)})"1092 )1093- elif self.environment.is_async:1094- self.write("_get_default_module_async()")1093 else:1096- self.write("_get_default_module(context)")1094+ self.write(f"_get_default_module{self.choose_async('_async')}(context)")1095 1096 def visit_Import(self, node: nodes.Import, frame: Frame) -> None:1097 """Visit regular imports."""1098 self.writeline(f"{frame.symbols.ref(node.target)} = ", node)859 unmodified linesmodified:tests/test_async.py+23 added
187 unmodified lines188 assert not hasattr(m, "__missing")189 assert m.variable == 42190 assert not hasattr(m, "notthere")191 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+215+216 class TestAsyncIncludes:217 def test_context_include(self, test_env_async):218 t = test_env_async.from_string('{% include "header" %}')219 assert t.render(foo=42) == "[42|23]"441 unmodified linesmodified:tests/test_imports.py+13 added-31 removed
97 unmodified lines98 with pytest.raises(UndefinedError, match="does not export the requested name"):99 t.render()100 101 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}104 )111- tmpl = env.get_template("test", globals={"foo": "bar"})112- assert tmpl.render() == "foo: bar"105+ assert t.render() == "[42|23]"106 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]"109 110 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},114 )127- tmpl = env.get_template("test", globals={"foo": "bar"})128- assert tmpl.render() == "foo: 42"115+ assert t.render() == "[42|23]"116 117 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},121 )139- tmpl = env.get_template("test", globals={"foo": "bar"})140- assert tmpl.render() == "foo: bar"122+ assert t.render() == "[42|23]"123 124 125 class TestIncludes:126 def test_context_include(self, test_env):79 unmodified linesmodified:CHANGES.rst+2 added
7 unmodified lines8 - Make ``compile_templates`` deterministic for filter and import9 names. :issue:`1452, 1453`10 - Revert an unintended change that caused ``Undefined`` to act like11 ``StrictUndefined`` for the ``in`` operator. :issue:`1448`12+- Imported macros have access to the current template globals in async13+ environments. :issue:`1494`14 15 16 Version 3.0.117 -------------876 unmodified lines