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
Provenance
- Commit
10f3ba2- Tree
fcc37bc- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
f64efb8
4 files changed+23 added−14 removed
modified:jinja2/compiler.py+9 added-1 removed
74 unmodified lines75 # variables that are known to be declared (probably from outer76 # frames or because they are special for the frame)77 self.declared = set()78 79+ # undeclared variables from outer scopes80+ self.outer_undeclared = set()81+82 # 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 lines108 return name in self.declared109 110 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)114 115 116 class Frame(object):117 """Holds compile time information for us."""30 unmodified lines148 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()158 159 def copy(self):899 unmodified linesmodified:jinja2/parser.py+2 added-2 removed
624 unmodified lines625 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 linesmodified:jinja2/runtime.py+4 added-3 removed
140 unmodified lines141 142 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)147 148 def __len__(self):149 return self.length150 39 unmodified lines190 class 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+ """196 197 def __init__(self, index0, length):198 self.index0 = index0199 self.length = length133 unmodified linesmodified:tests/test_forloop.py+8 added-8 removed
4 unmodified lines5 6 :copyright: 2007 by Armin Ronacher.7 :license: BSD, see LICENSE for more details.8 """9+from py.test import raises10 11+12 SIMPLE = '''{% for item in seq %}{{ item }}{% endfor %}'''13 ELSE = '''{% for item in seq %}XXX{% else %}...{% endfor %}'''14 EMPTYBLOCKS = '''<{% for item in seq %}{% else %}{% endfor %}>'''15 CONTEXTVARS = '''{% 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 %}'''17-CYCLING = '''{% for item in seq %}{% cycle '<1>', '<2>' %}{% endfor %}\18-{% for item in seq %}{% cycle through %}{% endfor %}'''18+ loop.length }}###{% endfor %}'''19+CYCLING = '''{% for item in seq %}{{ loop.cycle('<1>', '<2>') }}{% endfor %}\20+{% for item in seq %}{{ loop.cycle(*through) }}{% endfor %}'''21 SCOPE = '''{% for item in seq %}{% endfor %}{{ item }}'''22 VARLEN = '''{% for item in iter %}{{ item }}{% endfor %}'''23 NONITER = '''{% for item in none %}...{% endfor %}'''24 16 unmodified lines41 def 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('|')48 49 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'56 57 58 def test_cycling(env):18 unmodified lines77 78 79 def test_noniter(env):80 tmpl = env.from_string(NONITER)81- assert not tmpl.render()81+ raises(TypeError, tmpl.render)