[Python-checkins] r57901 - in sandbox/trunk/2to3: fixes/fix_dict.py fixes/fix_filter.py fixes/fix_map.py tests/test_fixers.py

collin.winter python-checkins at python.org
Sun Sep 2 00:54:35 CEST 2007


Author: collin.winter
Date: Sun Sep  2 00:54:34 2007
New Revision: 57901

Modified:
   sandbox/trunk/2to3/fixes/fix_dict.py
   sandbox/trunk/2to3/fixes/fix_filter.py
   sandbox/trunk/2to3/fixes/fix_map.py
   sandbox/trunk/2to3/tests/test_fixers.py
Log:
Bring fix_dict's list of special contexts into line with that for fix_map and fix_filter; drop enumerate() from all these lists (it's lazy, doh).

Modified: sandbox/trunk/2to3/fixes/fix_dict.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_dict.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_dict.py	Sun Sep  2 00:54:34 2007
@@ -14,7 +14,8 @@
 Except in certain very specific contexts: the iter() can be dropped
 when the context is list(), sorted(), iter() or for...in; the list()
 can be dropped when the context is list() or sorted() (but not iter()
-or for...in!).
+or for...in!). Special contexts that apply to both: list(), sorted(), tuple()
+set(), any(), all().
 
 Note: iter(d.keys()) could be written as iter(d) but since the
 original d.iterkeys() was also redundant we don't fix this.  And there
@@ -27,7 +28,11 @@
 import patcomp
 from pgen2 import token
 from fixes import basefix
-from fixes.util import Name, Call, LParen, RParen, ArgList, Dot
+from fixes.util import Name, Call, LParen, RParen, ArgList, Dot, set
+
+
+exempt = set(["sorted", "enumerate", "list", "set", "any", "all", "tuple"])
+
 
 class FixDict(basefix.BaseFix):
     PATTERN = """
@@ -83,10 +88,10 @@
                results["node"] is node):
             if isiter:
                 # iter(d.iterkeys()) -> iter(d.keys()), etc.
-                return results["func"].value in ("iter", "list", "sorted")
+                return results["func"].value in exempt | set(["iter"])
             else:
                 # list(d.keys()) -> list(d.keys()), etc.
-                return results["func"].value in ("list", "sorted")
+                return results["func"].value in exempt
         if not isiter:
             return False
         # for ... in d.iterkeys() -> for ... in d.keys(), etc.

Modified: sandbox/trunk/2to3/fixes/fix_filter.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_filter.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_filter.py	Sun Sep  2 00:54:34 2007
@@ -64,7 +64,7 @@
 
 P1 = """
 power<
-    ( 'iter' | 'list' | 'tuple' | 'sorted' | 'set' | 'enumerate' |
+    ( 'iter' | 'list' | 'tuple' | 'sorted' | 'set' |
       'any' | 'all' | (any* trailer< '.' 'join' >) )
     trailer< '(' node=any ')' >
     any*

Modified: sandbox/trunk/2to3/fixes/fix_map.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_map.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_map.py	Sun Sep  2 00:54:34 2007
@@ -81,7 +81,7 @@
 
 P1 = """
 power<
-    ( 'iter' | 'list' | 'tuple' | 'sorted' | 'set' | 'enumerate' |
+    ( 'iter' | 'list' | 'tuple' | 'sorted' | 'set' |
       'any' | 'all' | (any* trailer< '.' 'join' >) )
     trailer< '(' node=any ')' >
     any*

Modified: sandbox/trunk/2to3/tests/test_fixers.py
==============================================================================
--- sandbox/trunk/2to3/tests/test_fixers.py	(original)
+++ sandbox/trunk/2to3/tests/test_fixers.py	Sun Sep  2 00:54:34 2007
@@ -1024,6 +1024,18 @@
                ]"""
         self.check(b, a)
 
+    def test_unchanged(self):
+        wrappers = ["set", "sorted", "any", "all", "tuple"]
+        for wrapper in wrappers:
+            s = "s = %s(d.keys())" % wrapper
+            self.unchanged(s)
+
+            s = "s = %s(d.values())" % wrapper
+            self.unchanged(s)
+
+            s = "s = %s(d.items())" % wrapper
+            self.unchanged(s)
+
     def test_01(self):
         b = "d.keys()"
         a = "list(d.keys())"
@@ -2224,8 +2236,6 @@
         self.unchanged(a)
         a = """tuple(filter(f, 'abc'))"""
         self.unchanged(a)
-        a = """enumerate(filter(f, 'abc'))"""
-        self.unchanged(a)
         a = """any(filter(f, 'abc'))"""
         self.unchanged(a)
         a = """all(filter(f, 'abc'))"""
@@ -2311,8 +2321,6 @@
         self.unchanged(a)
         a = """tuple(map(f, 'abc'))"""
         self.unchanged(a)
-        a = """enumerate(map(f, 'abc'))"""
-        self.unchanged(a)
         a = """any(map(f, 'abc'))"""
         self.unchanged(a)
         a = """all(map(f, 'abc'))"""


More information about the Python-checkins mailing list