[Python-checkins] r61569 - in sandbox/trunk/2to3/lib2to3: fixes/fix_filter.py tests/test_fixers.py

david.wolever python-checkins at python.org
Tue Mar 18 23:01:27 CET 2008


Author: david.wolever
Date: Tue Mar 18 23:01:27 2008
New Revision: 61569

Modified:
   sandbox/trunk/2to3/lib2to3/fixes/fix_filter.py
   sandbox/trunk/2to3/lib2to3/tests/test_fixers.py
Log:
Fixed 2to3's handing of filter(None, seq).  Now it will return [_f for _f in seq if _f].

Modified: sandbox/trunk/2to3/lib2to3/fixes/fix_filter.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/fixes/fix_filter.py	(original)
+++ sandbox/trunk/2to3/lib2to3/fixes/fix_filter.py	Tue Mar 18 23:01:27 2008
@@ -40,6 +40,11 @@
     |
     power<
         'filter'
+        trailer< '(' arglist< none='None' ',' seq=any > ')' >
+    >
+    |
+    power<
+        'filter'
         args=trailer< '(' [any] ')' >
     >
     """
@@ -65,6 +70,13 @@
                            results.get("fp").clone(),
                            results.get("it").clone(),
                            results.get("xp").clone())
+
+        elif "none" in results:
+            new = ListComp(Name("_f"),
+                           Name("_f"),
+                           results["seq"].clone(),
+                           Name("_f"))
+
         else:
             if in_special_context(node):
                 return None

Modified: sandbox/trunk/2to3/lib2to3/tests/test_fixers.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/tests/test_fixers.py	(original)
+++ sandbox/trunk/2to3/lib2to3/tests/test_fixers.py	Tue Mar 18 23:01:27 2008
@@ -2308,13 +2308,17 @@
     fixer = "filter"
 
     def test_prefix_preservation(self):
-        b = """x =   filter(    None,     'abc'   )"""
-        a = """x =   list(filter(    None,     'abc'   ))"""
+        b = """x =   filter(    foo,     'abc'   )"""
+        a = """x =   list(filter(    foo,     'abc'   ))"""
+        self.check(b, a)
+
+        b = """x =   filter(  None , 'abc'  )"""
+        a = """x =   [_f for _f in 'abc' if _f]"""
         self.check(b, a)
 
     def test_filter_basic(self):
         b = """x = filter(None, 'abc')"""
-        a = """x = list(filter(None, 'abc'))"""
+        a = """x = [_f for _f in 'abc' if _f]"""
         self.check(b, a)
 
         b = """x = len(filter(f, 'abc'))"""


More information about the Python-checkins mailing list