Reverted [73b04625ab54]. The old behavior is the new behavior, the other
Before adoption: this commit came in with the history the project adopted, so no landing records it.
is not consistenly implementable. --HG-- branch : trunk
authored by Armin Ronacher · committed by Armin Ronacher · parent 1965e31 · Browse files · 6 files·+15 added−93 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+15 added−93 removed
- tests:+0 added−12 removed
- other:+15 added−81 removed
Provenance
- Commit
2f0d659- Tree
79905a2- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
1965e31
6 files changed+15 added−93 removed
modified:docs/faq.rst+8 added-19 removed
150 unmodified lines151 harder to maintain the code for older Python versions. If you really need152 Python 2.3 support you either have to use `Jinja 1`_ or other templating153 engines that still support 2.3.154 155-.. _jinja-scoping-bug:155+My Macros are overriden by something156+------------------------------------157 157-Scoping Bug in Jinja2158----------------------158+In some situations the Jinja scoping appears arbitrary:159 160-Jinja2 currently has a scoping bug that causes confusing behavior. If you161-have a layout template that defines a macro, and a child template that162-does the same thing, it will see the macro from the layout template:163-160 layout.tmpl:161 162 .. sourcecode:: jinja163 7 unmodified lines171 {% extends 'layout.tmpl' %}172 {% macro foo() %}CHILD{% endmacro %}173 {% block body %}{{ foo() }}{% endblock %}174 179-This will print ``LAYOUT`` in Jinja2 versions older than 2.5. Starting180-with Jinja 2.2.2, there will however be a deprecation warning for this181-behavior. Starting with Jinja 2.5, this behavior will change so that182-``CHILD`` is printed instead.175+This will print ``LAYOUT`` in Jinja2. This is a side effect of having176+the parent template evaluated after the child one. This allows child177+templates passing information to the parent template. To avoid this178+issue rename the macro or variable in the parent template to have an179+uncommon prefix.180 184-The reason why it was not changed right away is that some templates could185-depend on macros or variables defined in the layout template to be186-available in a child template. This however was undocumented behavior and187-is considered a bug.188-189-If you depend on this behavior, move your macros into a different file and190-import it into the layout and child template.191-181 .. _Jinja 1: http://jinja.pocoo.org/1/modified:jinja2/compiler.py+2 added-11 removed
354 unmodified lines355 self.environment = environment356 self.name = name357 self.filename = filename358 self.stream = stream359+ self.created_block_context = False360 361 # aliases for imports362 self.import_aliases = {}363 372 unmodified lines736 # do we have an extends tag at all? If not, we can save some737 # overhead by just not processing any inheritance code.738 have_extends = node.find(nodes.Extends) is not None739 739- # are there any block tags? If yes, we need a copy of the scope.740- have_blocks = node.find(nodes.Block) is not None741-740 # find all blocks741 for block in node.find_all(nodes.Block):742 if block.name in self.blocks:743 self.fail('block %r defined twice' % block.name, block.lineno)24 unmodified lines768 frame.require_output_check = have_extends and not self.has_known_extends769 self.indent()770 if have_extends:771 self.writeline('parent_template = None')774- if have_blocks:775- self.writeline('block_context = context._block()')772 if 'self' in find_undeclared(node.body, ('self',)):773 frame.identifiers.add_special('self')774 self.writeline('l_self = TemplateReference(context)')775 self.pull_locals(frame)24 unmodified lines800 undeclared = find_undeclared(block.body, ('self', 'super'))801 if 'self' in undeclared:802 block_frame.identifiers.add_special('self')803 self.writeline('l_self = TemplateReference(context)')808- if block.find(nodes.Block) is not None:809- self.writeline('block_context = context._block(%r)' % name)804 if 'super' in undeclared:805 block_frame.identifiers.add_special('super')806 self.writeline('l_super = context.super(%r, '807 'block_%s)' % (name, name))21 unmodified lines829 if self.extends_so_far > 0:830 self.writeline('if parent_template is None:')831 self.indent()832 level += 1839- if node.scoped:840- context = 'block_context.derived(locals())'841- else:842- context = 'block_context'833+ context = node.scoped and 'context.derived(locals())' or 'context'834 self.writeline('for event in context.blocks[%r][0](%s):' % (835 node.name, context), node)836 self.indent()837 self.simple_write('event', frame)703 unmodified linesmodified:jinja2/runtime.py+1 added-44 removed
109 unmodified lines110 'blocks', '__weakref__')111 112 def __init__(self, environment, parent, name, blocks):113 self.parent = parent114- self.vars = vars = {}114+ self.vars = {}115 self.environment = environment116 self.exported_vars = set()117 self.name = name118 65 unmodified lines184 self.parent, True, None, locals)185 context.blocks.update((k, list(v)) for k, v in self.blocks.iteritems())186 return context187 188- def _block(self, block=None):189- """Creates a context that is used for block execution. Currently this190- returns a special `_BlockContext` that warns about changed behavior.191- In Jinja 2.5, this will instead just return a new context with the same192- resolve behavior. Do not call from anywhere but the generated code!193-194- :private:195- """196- return _BlockContext(self, block)197-188 def _all(meth):189 proxy = lambda self: getattr(self.get_all(), meth)()190 proxy.__doc__ = getattr(dict, meth).__doc__191 proxy.__name__ = meth29 unmodified lines221 self.name222 )223 224 235-class _BlockContext(Context):236- """Implements a deprecation warning for the changed block assignments."""237- __slots__ = ('real_context', 'block_name')238-239- def __init__(self, real_context, block_name):240- super(_BlockContext, self).__init__(real_context.environment,241- real_context.parent,242- real_context.name, {})243- self.vars = dict(real_context.vars)244- self.exported_vars = set(real_context.exported_vars)245- self.blocks = real_context.blocks246- self.real_context = real_context247- self.block_name = block_name248-249- def resolve(self, key):250- self_rv = super(_BlockContext, self).resolve(key)251- base_rv = self.real_context.resolve(key)252- if self_rv != base_rv and not isinstance(base_rv, Undefined):253- from warnings import warn254- if self.block_name is not None:255- detail = 'accessing %r from inside block %r' \256- % (key, self.block_name)257- else:258- detail = 'accessing %r toplevel' % key259- detail += ', in template %s' % self.name260- warn(DeprecationWarning('variables set in a base template '261- 'will no longer leak into the child '262- 'context in future versions. Happened '263- 'when ' + detail))264- return base_rv265- return self_rv266-267-225 # register the context as mapping if possible226 try:227 from collections import Mapping228 Mapping.register(Context)298 unmodified linesmodified:tests/test_old_bugs.py-12 removed
76 unmodified lines77 assert tmpl.render(a=23) == '23'78 assert tmpl.render(b=True) == '42'79 80 81-def test_local_macros_first():82- raise SkipTest('Behavior will change in 2.3')83- env = Environment(loader=DictLoader({84- 'layout.html': ('{% macro foo() %}LAYOUT{% endmacro %}'85- '{% block body %}{% endblock %}'),86- 'child.html': ('{% extends "layout.html" %}'87- '{% macro foo() %}CHILD{% endmacro %}'88- '{% block body %}{{ foo() }}{% endblock %}')89- }))90- assert env.get_template('child.html').render() == 'CHILD'91-92-81 def test_stacked_locals_scoping_bug():82 env = Environment(line_statement_prefix='#')83 t = env.from_string('''\84 # for j in [1, 2]:19 unmodified linesmodified:CHANGES+3 added-6 removed
1 Jinja2 Changelog2 ================3 4-Version 2.35------------6-(codename Gnok, release date to be selected.)4+Version 2.2.25+-------------6+(bugfix release, release date to be selected.)7 8 - fixes issue with code generator that causes unbound variables9 to be generated if set was used in if-blocks.10-- added a deprecation warning for a variable assignment, scope bug11- that exists since 2.0, code could depend on. See :ref:`jinja-scoping-bug`12- for more information on this problem.10 11 Version 2.2.112 -------------13 (bugfix release, released on September 14th 2009)114 unmodified linesmodified:setup.py+1 added-1 removed
47 unmodified lines48 49 50 setup(51 name='Jinja2',52- version='2.3',52+ version='2.2.2',53 url='http://jinja.pocoo.org/',54 license='BSD',55 author='Armin Ronacher',56 author_email='armin.ronacher@active-4.com',31 unmodified lines