]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
imported macros can access template globals in async mode 1495/head
authorDaniel Stone <me@danstone.uk>
Sat, 18 Sep 2021 15:24:10 +0000 (16:24 +0100)
committerDavid Lord <davidism@gmail.com>
Mon, 4 Oct 2021 20:49:13 +0000 (13:49 -0700)
CHANGES.rst
src/jinja2/compiler.py
tests/test_async.py
tests/test_imports.py

index 074dd1f803fbc9f3a4bebd37446f899e5863ddfc..eb51581d5816f391d6992ba598746ac84d064e09 100644 (file)
@@ -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
index b6297202ad78fd7e0739fc7ef2edd8fb5940c5d1..52fd5b83e20964930dde5c8d71e149031a48d5bb 100644 (file)
@@ -1090,10 +1090,8 @@ class CodeGenerator(NodeVisitor):
             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."""
index e1c8d2345f9560f805097b7c742b4bf18b69b662..375a7bac33d0ed55ed31daafc0b28880ed93e9a8 100644 (file)
@@ -189,6 +189,29 @@ class TestAsyncImports:
         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):
index 054c9010480dab6d4a0fe068bbd11f892a8e3d8b..b59fb49dde8bc0e65eda2c08763281813ab80b69 100644 (file)
@@ -99,45 +99,27 @@ class TestImports:
             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: