1. Victor Stinner
  2. astoptimizer

Commits

Victor Stinner  committed 20e37cd

set("abc")

  • Participants
  • Parent commits 7a10922
  • Branches default

Comments (0)

Files changed (2)

File astoptimizer/optimizer.py

View file
  • Ignore whitespace
     is_bytes_ascii, is_singleton,
     INT_TYPES, FLOAT_TYPES, COMPLEX_TYPES,
     BYTES_TYPE, UNICODE_TYPE, STR_TYPES,
-    ITERABLE_TYPES)
+    IMMUTABLE_ITERABLE_TYPES, ITERABLE_TYPES)
 
 DROP_NODE = object()
 
                 # list([1, 2, 3]) => [1, 2, 3]
                 return arg
         elif qualname in ('frozenset', 'set'):
-            if PYTHON3:
-                ast_types = (ast.Tuple, ast.Str, ast.Bytes)
-            else:
-                ast_types = (ast.Tuple, ast.Str)
-            if isinstance(arg, ast_types):
-                constant = self.get_constant(arg)
+            constant = self.get_constant(arg)
+            if isinstance(constant, IMMUTABLE_ITERABLE_TYPES):
                 elts = frozenset(constant)
-                if len(elts) != len(constant):
+                use_literal_set = (qualname == 'set' and sys.version_info >= (2, 7))
+                if (len(elts) != len(constant)) or use_literal_set:
                     try:
                         # sort elements for astoptimizer unit tests
                         elts = list(elts)
                         pass
 
                     if len(elts) <= self.config.max_tuple_length:
-                        if (qualname == 'set'
-                        and sys.version_info >= (2, 7)):
+                        if use_literal_set:
                             # set((1, 2, 3)) => {1, 2, 3}
                             return new_set(node, elts)
                         else:

File astoptimizer/tests.py

View file
  • Ignore whitespace
         self.check('print 1, 2,', self.text_ast('print %r,' % ('1 2',)))
         self.check('print >>f, "x =", 2', self.text_ast('print >> f, %r' % ('x = 2',)))
 
+    def test_call_builtin(self):
+        config = self.create_config('builtin_funcs')
+
+        self.check('set(tuple(list(iterable)))',
+                   self.text_ast('set(iterable)'),
+                   config)
+        self.check('set(x for x in "abc")',
+                   self.text_set('a', 'b', 'c'),
+                   config)
+        self.check_not_optimized('set((1, "b", (), []))',
+                   config)
+
     def test_optimize_iter(self):
         config = self.create_config('builtin_funcs')