fix for-from loop in list comprehensions
authorStefan Behnel <scoder@users.berlios.de>
Fri, 19 Dec 2008 01:18:27 +0000 (02:18 +0100)
committerStefan Behnel <scoder@users.berlios.de>
Fri, 19 Dec 2008 01:18:27 +0000 (02:18 +0100)
Cython/Compiler/ParseTreeTransforms.py
tests/run/forfrom.pyx [new file with mode: 0644]

index 0de8104f874ca15cc916d30d66fcb7f1def80e43..9e383806d9af07c31dc4eb117e5670c78d031e4a 100644 (file)
@@ -625,8 +625,12 @@ class ComprehensionTransform(VisitorTransform):
         # replace name references in the loop code by their temp node
         self.visitchildren(node, ['loop'])
 
-        self.comprehension_targets = outer_comprehension_targets
+        loop = node.loop
+        if type(loop) is Nodes.ForFromStatNode and loop.target.type.is_numeric:
+            loop.loopvar_node = loop.target
+
         node.loop = TempsBlockNode(node.pos, body=node.loop, temps=temps)
+        self.comprehension_targets = outer_comprehension_targets
         return node
 
     def visit_NameNode(self, node):
diff --git a/tests/run/forfrom.pyx b/tests/run/forfrom.pyx
new file mode 100644 (file)
index 0000000..1135bb4
--- /dev/null
@@ -0,0 +1,19 @@
+u"""
+>>> for_else()
+30
+>>> print( u'*'.join(int_comp()) )
+00*01*02
+"""
+
+def for_else():
+    cdef int i, j=0, k=2
+    for i from 0 <= i < 10:
+        j += k
+    else:
+        k = j+10
+    return k
+
+def int_comp():
+    cdef int i
+    return tuple([ u"%02d" % i
+                   for i from 0 <= i < 3 ])