Skip to content
Myr

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

6 files+15 added−93 removed

modified:docs/faq.rst+8 added-19 removed
150 unmodified lines
151harder to maintain the code for older Python versions. If you really need152Python 2.3 support you either have to use `Jinja 1`_ or other templating153engines that still support 2.3.154155.. _jinja-scoping-bug:155My Macros are overriden by something156------------------------------------157157Scoping Bug in Jinja2158---------------------158In some situations the Jinja scoping appears arbitrary:159160Jinja2 currently has a scoping bug that causes confusing behavior. If you161have a layout template that defines a macro, and a child template that162does the same thing, it will see the macro from the layout template:163160layout.tmpl:161162.. sourcecode:: jinja163
7 unmodified lines
171 {% extends 'layout.tmpl' %}172 {% macro foo() %}CHILD{% endmacro %}173 {% block body %}{{ foo() }}{% endblock %}174179This will print ``LAYOUT`` in Jinja2 versions older than 2.5. Starting180with Jinja 2.2.2, there will however be a deprecation warning for this181behavior. Starting with Jinja 2.5, this behavior will change so that182``CHILD`` is printed instead.175This will print ``LAYOUT`` in Jinja2. This is a side effect of having176the parent template evaluated after the child one. This allows child177templates passing information to the parent template. To avoid this178issue rename the macro or variable in the parent template to have an179uncommon prefix.180184The reason why it was not changed right away is that some templates could185depend on macros or variables defined in the layout template to be186available in a child template. This however was undocumented behavior and187is considered a bug.188189If you depend on this behavior, move your macros into a different file and190import it into the layout and child template.191181.. _Jinja 1: http://jinja.pocoo.org/1/
modified:jinja2/compiler.py+2 added-11 removed
354 unmodified lines
355 self.environment = environment356 self.name = name357 self.filename = filename358 self.stream = stream359 self.created_block_context = False360361 # aliases for imports362 self.import_aliases = {}363
372 unmodified lines
736 # 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 None739739 # are there any block tags? If yes, we need a copy of the scope.740 have_blocks = node.find(nodes.Block) is not None741740 # 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 lines
768 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 lines
800 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 lines
829 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 lines
modified:jinja2/runtime.py+1 added-44 removed
109 unmodified lines
110 'blocks', '__weakref__')111112 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 lines
184 self.parent, True, None, locals)185 context.blocks.update((k, list(v)) for k, v in self.blocks.iteritems())186 return context187188 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!193194 :private:195 """196 return _BlockContext(self, block)197188 def _all(meth):189 proxy = lambda self: getattr(self.get_all(), meth)()190 proxy.__doc__ = getattr(dict, meth).__doc__191 proxy.__name__ = meth
29 unmodified lines
221 self.name222 )223224235class _BlockContext(Context):236 """Implements a deprecation warning for the changed block assignments."""237 __slots__ = ('real_context', 'block_name')238239 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_name248249 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_rv266267225# register the context as mapping if possible226try:227 from collections import Mapping228 Mapping.register(Context)
298 unmodified lines
modified:tests/test_old_bugs.py-12 removed
76 unmodified lines
77 assert tmpl.render(a=23) == '23'78 assert tmpl.render(b=True) == '42'798081def 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'919281def test_stacked_locals_scoping_bug():82 env = Environment(line_statement_prefix='#')83 t = env.from_string('''\84# for j in [1, 2]:
19 unmodified lines
modified:CHANGES+3 added-6 removed
1Jinja2 Changelog2================34Version 2.35-----------6(codename Gnok, release date to be selected.)4Version 2.2.25-------------6(bugfix release, release date to be selected.)78- 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.1011Version 2.2.112-------------13(bugfix release, released on September 14th 2009)
114 unmodified lines
modified:setup.py+1 added-1 removed
47 unmodified lines
484950setup(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