Commits

Victor Stinner committed 07997c2

Fix tests

Comments (0)

Files changed (3)

astoptimizer/__init__.py

-import ast
 import sys
 if sys.version_info < (2, 6):
     raise Exception("astoptimizer requires Python 2.6 or later")
+import ast
 
 UNSET = object()
 

astoptimizer/optimizer.py

             # list(iterable) => iterable
             return node.args[0]
 
+        if (want_type == set
+        and self.config.remove_dead_code
+        and self.check_builtin_func(node, ('frozenset', 'set'),  1, 1)):
+            # set(iterable) => iterable
+            return node.args[0]
+
         if isinstance(node, ast.GeneratorExp):
             # (x for x in "abc") => "abc"
             if is_generator:
         if (constant is not UNSET
         and isinstance(constant, ITERABLE_TYPES)):
             if len(constant) <= self.config.max_tuple_length:
-                if want_type == list:
-                    # "abc" => ["a", "b", "c"]
-                    constant = list(constant)
-                elif want_type == set and sys.version_info >= (2, 7):
+                if want_type == set and sys.version_info >= (2, 7):
                     # "abc" => {"a", "b", "c"}
                     constant = set(constant)
                 else:
-                    # "abc" => ("a", "b", "c")
-                    constant = tuple(constant)
+                    if isinstance(constant, (frozenset, set)):
+                        constant = sort_set_elts(constant)
+                    if want_type == list:
+                        # "abc" => ["a", "b", "c"]
+                        constant = list(constant)
+                    else:
+                        # want_type == tuple or is_generator
+                        # "abc" => ("a", "b", "c")
+                        constant = tuple(constant)
                 return new_literal(node, constant)
 
     def list_to_tuple(self, node):

astoptimizer/tests.py

                    self.text_tuple(),
                    config)
         self.check('tuple({x for x in iterable})',
-                   self.text_ast('tuple(iterable)'),
+                   self.text_ast('tuple(set(iterable))'),
                    config)
         if PYTHON2:
             self.check('set({x for x in range(1000)})',
         config._constants['__debug__'] = True
         config._constants['sys.flags.optimize'] = 0
         config._constants['sys.maxunicode'] = 0x10ffff
+        config._constants['sys.platform'] = 'linux2'
+        config._constants['sys.maxint'] = 2147483647
         return config
 
     def check_line(self, line):