changeset 803:92b9b647e1fe

`for` loops in template code blocks should not establish their own locals block. Closes #259.
author cmlenz
date Thu, 05 Mar 2009 10:05:12 +0000
parents aa274188b77a
children 9871e6ca89b0
files ChangeLog genshi/template/eval.py genshi/template/tests/eval.py
diffstat 3 files changed, 15 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- 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
--- 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
--- 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
Copyright (C) 2012-2017 Edgewall Software