From e308bf251856ccc79c3803bfe0e3246fb9a07657 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Thu, 30 Oct 2008 19:18:45 +0100 Subject: [PATCH] Fixed a macro scoping bug discovered by ckknight introduced in one of the earlier changes for the 2.1 release. --HG-- branch : trunk --- jinja2/compiler.py | 2 ++ tests/test_inheritance.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/jinja2/compiler.py b/jinja2/compiler.py index e6b2de0..5074a34 100644 --- a/jinja2/compiler.py +++ b/jinja2/compiler.py @@ -623,6 +623,8 @@ class CodeGenerator(NodeVisitor): def macro_body(self, node, frame, children=None): """Dump the function def of a macro or call block.""" frame = self.function_scoping(node, frame, children) + # macros are delayed, they never require output checks + frame.require_output_check = False args = frame.arguments self.writeline('def macro(%s):' % ', '.join(args), node) self.indent() diff --git a/tests/test_inheritance.py b/tests/test_inheritance.py index 8884b9f..bb86b41 100644 --- a/tests/test_inheritance.py +++ b/tests/test_inheritance.py @@ -131,3 +131,38 @@ def test_multi_inheritance(): assert tmpl.render(master='master2') == 'MASTER2CHILD' assert tmpl.render(master='master1') == 'MASTER1CHILD' assert tmpl.render() == 'MASTER1CHILD' + + +def test_fixed_macro_scoping_bug(): + assert Environment(loader=DictLoader({ + 'test.html': '''\ + {% extends 'details.html' %} + + {% macro my_macro() %} + my_macro + {% endmacro %} + + {% block inner_box %} + {{ my_macro() }} + {% endblock %} + ''', + 'details.html': '''\ + {% extends 'standard.html' %} + + {% macro my_macro() %} + my_macro + {% endmacro %} + + {% block content %} + {% block outer_box %} + outer_box + {% block inner_box %} + inner_box + {% endblock %} + {% endblock %} + {% endblock %} + ''', + 'standard.html': ''' + {% block content %} {% endblock %} + ''' + })).get_template("test.html").render().split() == [u'outer_box', u'my_macro'] -- 2.26.2