# HG changeset patch # User cmlenz # Date 1236247512 0 # Node ID 92b9b647e1fe3db2d0c0bb755c07952501e8a963 # Parent aa274188b77ac3ccbc513796497d514a68f47187 `for` loops in template code blocks should not establish their own locals block. Closes #259. diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -13,6 +13,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 @@ -517,13 +517,6 @@ finally: self.locals.pop() - def visit_For(self, node): - self.locals.append(set()) - try: - return ASTTransformer.visit_For(self, node) - finally: - self.locals.pop() - def visit_ImportFrom(self, node): if not has_star_import_bug or [a.name for a in node.names] != ['*']: # This is a Python 2.4 bug. Only if we have a broken Python 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 @@ -627,6 +627,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