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

armin.ronacher python-checkins at python.org
Sun Dec 7 22:39:44 CET 2008


Author: armin.ronacher
Date: Sun Dec  7 22:39:43 2008
New Revision: 67652

Log:
Added a fixer that cleans up a tuple argument to isinstance after the tokens
in it were fixed.  This is mainly used to remove double occurrences of
tokens as a leftover of the long -> int / unicode -> str conversion.



Added:
   sandbox/trunk/2to3/lib2to3/fixes/fix_isinstance.py
Modified:
   sandbox/trunk/2to3/lib2to3/tests/test_fixers.py

Added: sandbox/trunk/2to3/lib2to3/fixes/fix_isinstance.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/2to3/lib2to3/fixes/fix_isinstance.py	Sun Dec  7 22:39:43 2008
@@ -0,0 +1,45 @@
+"""Fixer that cleans up a tuple argument to isinstance after the tokens
+in it were fixed.  This is mainly used to remove double occurrences of
+tokens as a leftover of the long -> int / unicode -> str conversion.
+
+eg.  isinstance(x, (int, long)) -> isinstance(x, (int, int))
+       -> isinstance(x, (int))
+
+TODO: currently a pair of bogus parentheses is left if only one item
+      is left in the list.  This doesn't do harm but doesn't look very
+      nice.
+"""
+
+from .. import fixer_base
+from ..pgen2 import token
+
+
+class FixIsinstance(fixer_base.BaseFix):
+
+    PATTERN = """
+    power<
+        'isinstance'
+        trailer< '(' arglist< any ',' atom< '('
+            args=testlist_gexp< any+ >
+        ')' > > ')' >
+    >
+    """
+
+    def transform(self, node, results):
+        names_inserted = set()
+        args = results['args'].children
+        new_args = []
+        iterator = enumerate(args)
+        for idx, arg in iterator:
+            if arg.type == token.NAME and arg.value in names_inserted:
+                if idx < len(args) - 1 and args[idx + 1].type == token.COMMA:
+                    iterator.next()
+                    continue
+            else:
+                new_args.append(arg)
+                if arg.type == token.NAME:
+                    names_inserted.add(arg.value)
+        if new_args and new_args[-1].type == token.COMMA:
+            del new_args[-1]
+        args[:] = new_args
+        node.changed()

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	Sun Dec  7 22:39:43 2008
@@ -1069,6 +1069,37 @@
         a = """x =   int(  x  )"""
         self.check(b, a)
 
+class Test_isinstance(FixerTestCase):
+    fixer = "isinstance"
+
+    def test_remove_multiple_items1(self):
+        b = """isinstance(x, (int, int, int))"""
+        a = """isinstance(x, (int))"""
+        self.check(b, a)
+
+    def test_remove_multiple_items2(self):
+        b = """isinstance(x, (int, float, int, int, float))"""
+        a = """isinstance(x, (int, float))"""
+        self.check(b, a)
+
+    def test_remove_multiple_items3(self):
+        b = """isinstance(x, (int, float, int, int, float, str))"""
+        a = """isinstance(x, (int, float, str))"""
+        self.check(b, a)
+
+    def test_remove_multiple_items4(self):
+        b = """isinstance(foo() + bar(), (x(), y(), x(), int, int))"""
+        a = """isinstance(foo() + bar(), (x(), y(), x(), int))"""
+        self.check(b, a)
+
+    def test_prefix_preservation(self):
+        b = """if    isinstance(  foo(), (  bar, bar, baz )) : pass"""
+        a = """if    isinstance(  foo(), (  bar, baz )) : pass"""
+        self.check(b, a)
+
+    def test_unchanged(self):
+        self.unchanged("isinstance(x, (str, int))")
+
 class Test_dict(FixerTestCase):
     fixer = "dict"
 


More information about the Python-checkins mailing list