Commits

Victor Stinner  committed 531cb10

Fix invalid optimization: tuple(set(iterable))

  • Participants
  • Parent commits 438c435

Comments (0)

Files changed (3)

 bugs:
 
  - "i=0; while i < 10: print(i); i = i + 1": don't replace print(i) with print('0')
+ - "for x in (): try: pass finally: continue" must raise a SyntaxError
+ - "gen=(i for i in range(5)); next(gen); gen.send(1)"
+ - "type(iter([]))"
 
 major optimizations:
 

File astoptimizer/optimizer.py

                 return
 
             if (self.config.remove_dead_code
-            and self.check_builtin_func(arg, BUILTIN_ACCEPTING_ITERABLE,  1, 1)):
+            and self.check_builtin_func(arg, ('list', 'tuple'),  1, 1)):
                 # set(list(iterable)) => set(iterable)
                 node.args[0] = arg.args[0]
                 return

File astoptimizer/tests.py

         self.check('set(set())', self.text_ast('set()'), config)
         self.check('set(frozenset())', self.text_ast('set()'), config)
 
+    def test_drop_builtin_iterable(self):
+        config = self.create_config('builtin_funcs')
+        self.check('dict(tuple(iterable))', self.text_ast('dict(iterable)'), config)
+        self.check('frozenset(list(iterable))', self.text_ast('frozenset(iterable)'), config)
+        self.check('set(tuple(iterable))', self.text_ast('set(iterable)'), config)
+        self.check('tuple(list(iterable))', self.text_ast('tuple(iterable)'), config)
+        self.check('tuple(tuple(iterable))', self.text_ast('tuple(iterable)'), config)
+        self.check_not_optimized('tuple(frozenset(iterable))', config)
+        self.check_not_optimized('tuple(set(iterable))', config)
+        self.check_not_optimized('tuple(dict(iterable))', config)
+
     def test_drop_iter_empty_iterable(self):
         config = self.create_config('builtin_funcs')
         self.check('tuple(iter(""))', self.text_ast('tuple()'), config)