From 9de47b5e00526e593a1c97e01f15839a68115cf6 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Fri, 19 Dec 2008 02:18:27 +0100 Subject: [PATCH] fix for-from loop in list comprehensions --- Cython/Compiler/ParseTreeTransforms.py | 6 +++++- tests/run/forfrom.pyx | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 tests/run/forfrom.pyx diff --git a/Cython/Compiler/ParseTreeTransforms.py b/Cython/Compiler/ParseTreeTransforms.py index 0de8104f..9e383806 100644 --- a/Cython/Compiler/ParseTreeTransforms.py +++ b/Cython/Compiler/ParseTreeTransforms.py @@ -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 index 00000000..1135bb42 --- /dev/null +++ b/tests/run/forfrom.pyx @@ -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 ]) -- 2.26.2