Commits

Victor Stinner committed 6e02622

while True: ... => while 1: ...

Comments (0)

Files changed (3)

 
  * Optimize iterators and generators. Examples:
 
-   - ``for x in range(3): ...`` => ``for x in xrange(3): ...``
+   - ``while True: ...`` => ``while 1: ...``
+   - ``for x in range(3): ...`` => ``for x in xrange(3): ...`` (Python 2)
    - ``[x for x in range(3)]`` => ``[x for x in xrange(3)]``
    - ``tuple(x for x in "abc")`` => ``tuple(iter("abc"))``
    - ``(x for x in "abc" if False)`` => ``iter(())``

astoptimizer/optimizer.py

 import operator
 from astoptimizer import UNSET
 from astoptimizer.ast_tools import (
-    ast_contains, copy_lineno,
+    copy_lineno,
     new_constant, new_call, new_pass,
-    check_func_args)
+    ast_contains, check_func_args)
 from astoptimizer.config import optimize_unicode
 from astoptimizer.compatibility import (
     u,
         # Create a pass instruction if needed.
         # Example: if 0: print("debug") => pass
         if len(node_list) == 0:
-            return new_pass(parent)
+            return [new_pass(parent)]
         else:
             return node_list
 
         if constant is UNSET:
             return
         if constant:
+            always_true = new_constant(node.test, 1)
+            node.test = always_true
             return
         # if not self.can_drop(node.body):
         if not can_drop:

astoptimizer/tests.py

         self.check(code, self.text_ast(expected))
 
     def test_remove_dead_code(self):
+        # return, raise
         self.check('def f():\n return 1\n return 2',
                    self.text_ast('def f():\n return 1'))
         self.check('def f():\n raise\n return 2',
         """.strip()
         self.check_not_optimized(code, catch_syntaxerror=True)
 
+    def test_dont_remove_dead_code(self):
         # disable removal of dead code
         config = Config()
         config.remove_dead_code = False
         self.check_not_optimized('if 0: print("log")', config)
         self.check_not_optimized('while 0: print("log")', config)
         self.check_not_optimized('for x in (): pass', config)
+        self.check_not_optimized('for x in (1, 2, 3): pass', config)
 
     def test_IfExp(self):
         self.check('4 if "abc" else 5', self.text_num(4))
     def test_While(self):
         self.check('while 0: print("log")', self.TEXT_PASS)
         self.check_not_optimized('def f():\n while 0:\n  yield')
+        self.check('while "abc": pass',
+                   self.text_ast('while 1: pass'))
+        self.check('while True: pass',
+                   self.text_ast('while 1: pass'))
         self.check('def f():\n yield 3\n while 0:\n  yield 5',
                    self.text_ast('def f():\n yield 3'))
         self.check_not_optimized('while 1: print("log")')
 
     def test_For(self):
         if PYTHON2:
+            # range => xrange
             self.check_not_optimized('for x in range(n): pass')
 
             config = self.create_config('builtin_funcs')