transform empty comprehensions into empty literals
authorStefan Behnel <stefan_ml@behnel.de>
Mon, 11 Mar 2013 10:17:47 +0000 (11:17 +0100)
committerStefan Behnel <stefan_ml@behnel.de>
Mon, 11 Mar 2013 10:17:47 +0000 (11:17 +0100)
Cython/Compiler/Optimize.py
tests/run/constant_folding.py
tests/run/constant_folding_cy.pyx

index a68dc67..6563dfa 100644 (file)
@@ -3233,6 +3233,13 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
                     return base
         return node
 
+    def visit_ComprehensionNode(self, node):
+        self.visitchildren(node)
+        if isinstance(node.loop, Nodes.StatListNode) and not node.loop.stats:
+            # loop was pruned already => transform into literal
+            return node.target
+        return node
+
     def visit_ForInStatNode(self, node):
         self.visitchildren(node)
         sequence = node.iterator.sequence
index d88978a..2b1ad59 100644 (file)
@@ -213,10 +213,11 @@ def for_in_empty_else():
 
 
 @cython.test_fail_if_path_exists(
+    "//ComprehensionNode",
     "//ForInStatNode",
 )
 @cython.test_assert_path_exists(
-    "//ComprehensionNode",
+    "//ListNode",
 )
 def for_in_empty_listcomp():
     """
@@ -227,10 +228,11 @@ def for_in_empty_listcomp():
 
 
 @cython.test_fail_if_path_exists(
+    "//ComprehensionNode",
     "//ForInStatNode",
 )
 @cython.test_assert_path_exists(
-    "//ComprehensionNode",
+    "//ListNode",
 )
 def for_in_empty_nested_listcomp():
     """
@@ -242,7 +244,7 @@ def for_in_empty_nested_listcomp():
 
 @cython.test_fail_if_path_exists(
     "//ForInStatNode//ForInStatNode",
-    )
+)
 @cython.test_assert_path_exists(
     "//ForInStatNode",
     "//ComprehensionNode",
index da9a935..b2ca8ec 100644 (file)
@@ -83,10 +83,11 @@ def unicode_slicing_safe_surrogates2():
 
 
 @cython.test_fail_if_path_exists(
+    "//ComprehensionNode",
     "//ForInStatNode",
 )
 @cython.test_assert_path_exists(
-    "//ComprehensionNode",
+    "//SetNode",
 )
 def for_in_empty_setcomp():
     """