Skip to content
Myr

Generalize scoping. This fixes #603

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 147bd57 · Browse files · 4 files+50 added−5 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+50 added−5 removed

  • tests:+35 added−0 removed
  • other:+15 added−5 removed

4 files+50 added−5 removed

modified:jinja2/compiler.py+3 added-3 removed
864 unmodified lines
865 skip_event_yield = False866 if node.with_context:867 loop = self.environment.is_async and 'async for' or 'for'868 self.writeline('%s event in template.root_render_func('869 'template.new_context(context.parent, True, '869 'template.new_context(context.get_all(), True, '870 '%s)):' % (loop, self.dump_local_context(frame)))871 elif self.environment.is_async:872 self.writeline('for event in (await '873 'template._get_default_module_async())'
25 unmodified lines
899 self.write('environment.get_template(')900 self.visit(node.template, frame)901 self.write(', %r).' % self.name)902 if node.with_context:903 self.write('make_module%s(context.parent, True, %s)'903 self.write('make_module%s(context.get_all(), True, %s)'904 % (self.environment.is_async and '_async' or '',905 self.dump_local_context(frame)))906 elif self.environment.is_async:907 self.write('_get_default_module_async()')
9 unmodified lines
917 % (self.environment.is_async and 'await ' or ''))918 self.visit(node.template, frame)919 self.write(', %r).' % self.name)920 if node.with_context:921 self.write('make_module%s(context.parent, True, %s)'921 self.write('make_module%s(context.get_all(), True, %s)'922 % (self.environment.is_async and '_async' or '',923 self.dump_local_context(frame)))924 elif self.environment.is_async:925 self.write('_get_default_module_async()')
694 unmodified lines
modified:jinja2/runtime.py+7 added-2 removed
168 unmodified lines
169 """Get a new dict with the exported variables."""170 return dict((k, self.vars[k]) for k in self.exported_vars)171172 def get_all(self):173 """Return a copy of the complete context as dict including the174 exported variables.173 """Return the complete context as dict including the exported174 variables. For optimizations reasons this might not return an175 actual copy so be careful with using it.176 """177 if not self.vars:178 return self.parent179 if not self.parent:180 return self.vars181 return dict(self.parent, **self.vars)182183 @internalcode184 def call(__self, __obj, *args, **kwargs):
533 unmodified lines
modified:tests/test_regression.py+35 added
350 unmodified lines
351 {% endfor %}352 ''')353 assert list(map(int, tmpl.render().split())) == \354 [3, 2, 1, 5, 4, 3, 7, 6, 5]355356 def test_scopes_and_blocks(self):357 env = Environment(loader=DictLoader({358 'a.html': '''359 {%- set foo = 'bar' -%}360 {% include 'x.html' -%}361 ''',362 'b.html': '''363 {%- set foo = 'bar' -%}364 {% block test %}{% include 'x.html' %}{% endblock -%}365 ''',366 'c.html': '''367 {%- set foo = 'bar' -%}368 {% block test %}{% set foo = foo369 %}{% include 'x.html' %}{% endblock -%}370 ''',371 'x.html': '''{{ foo }}|{{ test }}'''372 }))373374 a = env.get_template('a.html')375 b = env.get_template('b.html')376 c = env.get_template('c.html')377378 assert a.render(test='x').strip() == 'bar|x'379 assert b.render(test='x').strip() == 'bar|x'380 assert c.render(test='x').strip() == 'bar|x'381382 def test_scopes_and_include(self):383 env = Environment(loader=DictLoader({384 'include.html': '{{ var }}',385 'base.html': '{% include "include.html" %}',386 'child.html': '{% extends "base.html" %}{% set var = 42 %}',387 }))388 t = env.get_template('child.html')389 assert t.render() == '42'
modified:CHANGES+5 added
33 unmodified lines
34 a restricted one. This means that you can now properly use multiple35 tests in one expression without extra parentheses. In particular you can36 now write ``foo is divisibleby 2 or foo is divisibleby 3``37 as you would expect.38- Greatly changed the scoping system to be more consistent with what template39 designers and developers expect. There is now no more magic difference40 between the different include and import constructs. Context is now always41 propagated the same way. The only remaining differences is the defaults42 for `with context` and `without context`.4344Version 2.8.245-------------46
381 unmodified lines