# HG changeset patch # User cmlenz # Date 1236247605 0 # Node ID cce33406c1cfcaf4358ec856e82525cbbdc77868 # Parent 9fe8fdca279eccff0d609844ce6f59ae705886ed Ported [1005] to 0.5.x branch. diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -6,6 +6,9 @@ attribute values when inside an `i18n:msg` block (ticket #250). * Fix problem with the transformation filter dropping events after the selection (ticket #290). + * `for` loops in template code blocks no longer establish their own locals + scope, meaning you can now access variables assigned in the loop outside + of the loop, just as you can in regular Python code (ticket #259). Version 0.5.1 diff --git a/genshi/template/eval.py b/genshi/template/eval.py --- a/genshi/template/eval.py +++ b/genshi/template/eval.py @@ -759,13 +759,6 @@ finally: self.locals.pop() - def visitFor(self, node): - self.locals.append(set()) - try: - return ASTTransformer.visitFor(self, node) - finally: - self.locals.pop() - def visitFunction(self, node): if len(self.locals) > 1: self.locals[-1].add(node.name) diff --git a/genshi/template/tests/eval.py b/genshi/template/tests/eval.py --- a/genshi/template/tests/eval.py +++ b/genshi/template/tests/eval.py @@ -581,6 +581,18 @@ suite.execute(data) self.assertEqual([0, 1, 4], data['x']) + def test_for_in_def(self): + suite = Suite("""def loop(): + for i in range(10): + if i == 5: + break + return i +""") + data = {} + suite.execute(data) + assert 'loop' in data + self.assertEqual(5, data['loop']()) + def test_if(self): suite = Suite("""if foo == 42: x = True