Skip to content
Myr

loops and `tests` tests pass now

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

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent f64efb8 · Browse files · 4 files+23 added−14 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+23 added−14 removed

  • tests:+8 added−8 removed
  • other:+15 added−6 removed

4 files+23 added−14 removed

modified:jinja2/compiler.py+9 added-1 removed
74 unmodified lines
75 # variables that are known to be declared (probably from outer76 # frames or because they are special for the frame)77 self.declared = set()7879 # undeclared variables from outer scopes80 self.outer_undeclared = set()8182 # names that are accessed without being explicitly declared by83 # this one or any of the outer scopes. Names can appear both in84 # declared and undeclared.85 self.undeclared = set()
22 unmodified lines
108 return name in self.declared109110 def find_shadowed(self):111 """Find all the shadowed names."""112 return (self.declared | self.outer_undeclared) & \109 return self.declared & (self.declared_locally | self.declared_parameter)113 (self.declared_locally | self.declared_parameter)114115116class Frame(object):117 """Holds compile time information for us."""
30 unmodified lines
148 parent.identifiers.declared |149 parent.identifiers.declared_locally |150 parent.identifiers.declared_parameter151 )152 self.identifiers.outer_undeclared.update(153 parent.identifiers.undeclared -154 self.identifiers.declared155 )156 self.buffer = parent.buffer157 self.name_overrides = parent.name_overrides.copy()158159 def copy(self):
899 unmodified lines
modified:jinja2/parser.py+2 added-2 removed
624 unmodified lines
625 negated = True626 else:627 negated = False628 name = self.stream.expect('name').value629 dyn_args = dyn_kwargs = None630 kwargs = []631 if self.stream.current.type is 'lparen':632 args, kwargs, dyn_args, dyn_kwargs = self.parse_call(None)633 elif self.stream.current.type in ('name', 'string', 'integer',634 'float', 'lparen', 'lbracket',635 'lbrace', 'regex'):636 args = [self.parse_expression()]637 else:638 args = []637 kwargs = []638 dyn_args = dyn_kwargs = None639 node = nodes.Test(node, name, args, kwargs, dyn_args,640 dyn_kwargs, lineno=token.lineno)641 if negated:642 node = nodes.Not(node, lineno=token.lineno)
48 unmodified lines
modified:jinja2/runtime.py+4 added-3 removed
140 unmodified lines
141142 first = property(lambda x: x.index0 == 0)143 last = property(lambda x: x.revindex0 == 0)144 index = property(lambda x: x.index0 + 1)145 revindex = property(lambda x: x.length)146 revindex0 = property(lambda x: x.length - 1)145 revindex = property(lambda x: x.length - x.index0)146 revindex0 = property(lambda x: x.length - x.index)147148 def __len__(self):149 return self.length150
39 unmodified lines
190class StaticLoopContext(LoopContextBase):191 """The static loop context is used in the optimizer to "freeze" the192 status of an iteration. The only reason for this object is if the193 loop object is accessed in a non static way (eg: becomes part of a194 function call)."""194 function call).195 """196197 def __init__(self, index0, length):198 self.index0 = index0199 self.length = length
133 unmodified lines
modified:tests/test_forloop.py+8 added-8 removed
4 unmodified lines
56 :copyright: 2007 by Armin Ronacher.7 :license: BSD, see LICENSE for more details.8"""9from py.test import raises101112SIMPLE = '''{% for item in seq %}{{ item }}{% endfor %}'''13ELSE = '''{% for item in seq %}XXX{% else %}...{% endfor %}'''14EMPTYBLOCKS = '''<{% for item in seq %}{% else %}{% endfor %}>'''15CONTEXTVARS = '''{% for item in seq %}\16{{ loop.index }}|{{ loop.index0 }}|{{ loop.revindex }}|{{17 loop.revindex0 }}|{{ loop.first }}|{{ loop.last }}|{{16 loop.even }}|{{ loop.odd }}|{{ loop.length }}###{% endfor %}'''17CYCLING = '''{% for item in seq %}{% cycle '<1>', '<2>' %}{% endfor %}\18{% for item in seq %}{% cycle through %}{% endfor %}'''18 loop.length }}###{% endfor %}'''19CYCLING = '''{% for item in seq %}{{ loop.cycle('<1>', '<2>') }}{% endfor %}\20{% for item in seq %}{{ loop.cycle(*through) }}{% endfor %}'''21SCOPE = '''{% for item in seq %}{% endfor %}{{ item }}'''22VARLEN = '''{% for item in iter %}{{ item }}{% endfor %}'''23NONITER = '''{% for item in none %}...{% endfor %}'''24
16 unmodified lines
41def test_context_vars(env):42 tmpl = env.from_string(CONTEXTVARS)43 one, two, _ = tmpl.render(seq=[0, 1]).split('###')44 (one_index, one_index0, one_revindex, one_revindex0, one_first,43 one_last, one_even, one_odd, one_length) = one.split('|')45 one_last, one_length) = one.split('|')46 (two_index, two_index0, two_revindex, two_revindex0, two_first,45 two_last, two_even, two_odd, two_length) = two.split('|')47 two_last, two_length) = two.split('|')4849 assert int(one_index) == 1 and int(two_index) == 250 assert int(one_index0) == 0 and int(two_index0) == 151 assert int(one_revindex) == 2 and int(two_revindex) == 152 assert int(one_revindex0) == 1 and int(two_revindex0) == 053 assert one_first == 'True' and two_first == 'False'54 assert one_last == 'False' and two_last == 'True'53 assert one_even == 'False' and two_even == 'True'54 assert one_odd == 'True' and two_odd == 'False'55 assert one_length == two_length == '2'565758def test_cycling(env):
18 unmodified lines
777879def test_noniter(env):80 tmpl = env.from_string(NONITER)81 assert not tmpl.render()81 raises(TypeError, tmpl.render)