Merge branch '2.9-maintenance'
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 · parents 6173cf6, 90ac764 · Browse files · 7 files·+102 added−20 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+102 added−20 removed
- tests:+56 added−11 removed
- other:+46 added−9 removed
Provenance
- Commit
7ec16c5- Tree
b48a747- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent 1
6173cf6- Parent 2
90ac764
7 files changed+102 added−20 removed
modified:jinja2/compiler.py+13 added-1 removed
17 unmodified lines18 from jinja2.optimizer import Optimizer19 from jinja2.exceptions import TemplateAssertionError20 from jinja2.utils import Markup, concat, escape21 from jinja2._compat import range_type, text_type, string_types, \22- iteritems, NativeStringIO, imap22+ iteritems, NativeStringIO, imap, izip23 from jinja2.idtracking import Symbols, VAR_LOAD_PARAMETER, \24 VAR_LOAD_RESOLVE, VAR_LOAD_ALIAS, VAR_LOAD_UNDEFINED25 26 1167 unmodified lines1194 self.visit_Filter(node.filter, filter_frame)1195 self.end_write(frame)1196 self.leave_frame(filter_frame)1197 1198+ def visit_With(self, node, frame):1199+ with_frame = frame.inner()1200+ with_frame.symbols.analyze_node(node)1201+ self.enter_frame(with_frame)1202+ for idx, (target, expr) in enumerate(izip(node.targets, node.values)):1203+ self.newline()1204+ self.visit(target, with_frame)1205+ self.write(' = ')1206+ self.visit(expr, frame)1207+ self.blockvisit(node.body, with_frame)1208+ self.leave_frame(with_frame)1209+1210 def visit_ExprStmt(self, node, frame):1211 self.newline(node)1212 self.visit(node.node, frame)1213 484 unmodified linesmodified:jinja2/idtracking.py+10 added
184 unmodified lines185 raise RuntimeError('Unknown for branch')186 for item in branch or ():187 self.sym_visitor.visit(item)188 189+ def visit_With(self, node, **kwargs):190+ for target in node.targets:191+ self.sym_visitor.visit(target)192+ for child in node.body:193+ self.sym_visitor.visit(child)194+195 def generic_visit(self, node, *args, **kwargs):196 raise NotImplementedError('Cannot find symbols for %r' %197 node.__class__.__name__)198 61 unmodified lines260 261 def visit_FilterBlock(self, node, **kwargs):262 self.visit(node.filter, **kwargs)263 264+ def visit_With(self, node, **kwargs):265+ for target in node.values:266+ self.visit(target)267+268 def visit_AssignBlock(self, node, **kwargs):269 """Stop visiting at block assigns."""270 self.visit(node.target, **kwargs)271 8 unmodified linesmodified:jinja2/nodes.py+9 added
335 unmodified lines336 """Node for filter sections."""337 fields = ('body', 'filter')338 339 340+class With(Stmt):341+ """Specific node for with statements. In older versions of Jinja the342+ with statement was implemented on the base of the `Scope` node instead.343+344+ .. versionadded:: 2.9.3345+ """346+ fields = ('targets', 'values', 'body')347+348+349 class Block(Stmt):350 """A node that represents a block."""351 fields = ('name', 'body', 'scoped')352 603 unmodified linesmodified:jinja2/parser.py+11 added-8 removed
223 unmodified lines224 break225 return result226 227 def parse_with(self):228- node = nodes.Scope(lineno=next(self.stream).lineno)229- assignments = []228+ node = nodes.With(lineno=next(self.stream).lineno)229+ targets = []230+ values = []231 while self.stream.current.type != 'block_end':232 lineno = self.stream.current.lineno232- if assignments:233+ if targets:234 self.stream.expect('comma')235 target = self.parse_assign_target()236+ target.set_ctx('param')237+ targets.append(target)238 self.stream.expect('assign')236- expr = self.parse_expression()237- assignments.append(nodes.Assign(target, expr, lineno=lineno))238- node.body = assignments + \239- list(self.parse_statements(('name:endwith',),240- drop_needle=True))239+ values.append(self.parse_expression())240+ node.targets = targets241+ node.values = values242+ node.body = self.parse_statements(('name:endwith',),243+ drop_needle=True)244 return node245 246 def parse_autoescape(self):247 node = nodes.ScopedEvalContextModifier(lineno=next(self.stream).lineno)651 unmodified linesmodified:tests/test_core_tags.py+23 added
361 unmodified lines362 env = Environment(autoescape=True)363 tmpl = env.from_string('{% set foo %}<em>{{ test }}</em>'364 '{% endset %}foo: {{ foo }}')365 assert tmpl.render(test='<unsafe>') == 'foo: <em><unsafe></em>'366+367+368+@pytest.mark.core_tags369+@pytest.mark.with_370+class TestWith(object):371+372+ def test_with(self, env):373+ tmpl = env.from_string('''\374+ {% with a=42, b=23 -%}375+ {{ a }} = {{ b }}376+ {% endwith -%}377+ {{ a }} = {{ b }}\378+ ''')379+ assert [x.strip() for x in tmpl.render(a=1, b=2).splitlines()] \380+ == ['42 = 23', '1 = 2']381+382+ def test_with_argument_scoping(self, env):383+ tmpl = env.from_string('''\384+ {%- with a=1, b=2, c=b, d=e, e=5 -%}385+ {{ a }}|{{ b }}|{{ c }}|{{ d }}|{{ e }}386+ {%- endwith -%}387+ ''')388+ assert tmpl.render(b=3, e=4) == '1|2|3|4|5'modified:tests/test_ext.py+33 added-11 removed
193 unmodified lines194 {%- do items.append(loop.index0 ~ char) %}195 {%- endfor %}{{ items|join(', ') }}''')196 assert tmpl.render() == '0f, 1o, 2o'197 198- def test_with(self):199- env = Environment(extensions=['jinja2.ext.with_'])200- tmpl = env.from_string('''\201- {% with a=42, b=23 -%}202- {{ a }} = {{ b }}203- {% endwith -%}204- {{ a }} = {{ b }}\205- ''')206- assert [x.strip() for x in tmpl.render(a=1, b=2).splitlines()] \207- == ['42 = 23', '1 = 2']208-198 def test_extension_nodes(self):199 env = Environment(extensions=[ExampleExtension])200 tmpl = env.from_string('{% test %}')201 assert tmpl.render() == 'False|42|23|{}'101 unmodified lines303 ]304 305 306 @pytest.mark.ext307+class TestScope(object):308+309+ def test_basic_scope_behavior(self):310+ # This is what the old with statement compiled down to311+ class ScopeExt(Extension):312+ tags = set(['scope'])313+314+ def parse(self, parser):315+ node = nodes.Scope(lineno=next(parser.stream).lineno)316+ assignments = []317+ while parser.stream.current.type != 'block_end':318+ lineno = parser.stream.current.lineno319+ if assignments:320+ parser.stream.expect('comma')321+ target = parser.parse_assign_target()322+ parser.stream.expect('assign')323+ expr = parser.parse_expression()324+ assignments.append(nodes.Assign(target, expr, lineno=lineno))325+ node.body = assignments + \326+ list(parser.parse_statements(('name:endscope',),327+ drop_needle=True))328+ return node329+330+ env = Environment(extensions=[ScopeExt])331+ tmpl = env.from_string('''\332+ {%- scope a=1, b=2, c=b, d=e, e=5 -%}333+ {{ a }}|{{ b }}|{{ c }}|{{ d }}|{{ e }}334+ {%- endscope -%}335+ ''')336+ assert tmpl.render(b=3, e=4) == '1|2|2|4|5'337+338+339+@pytest.mark.ext340 class TestNewstyleInternationalization(object):341 342 def test_trans(self):343 tmpl = newstyle_i18n_env.get_template('child.html')177 unmodified linesmodified:CHANGES+3 added
22 unmodified lines23 For more information and a discussion see #64124 - Resolved an issue where `block scoped` would not take advantage of the25 new scoping rules. In some more exotic cases a variable overriden in a26 local scope would not make it into a block.27+- Change the code generation of the `with` statement to be in line with the28+ new scoping rules. This resolves some unlikely bugs in edge cases. This29+ also introduces a new internal `With` node that can be used by extensions.30 31 Version 2.9.232 -------------33 (bugfix release, released on January 8th 2017)447 unmodified lines