[Python-checkins] r68106 - in sandbox/trunk/2to3/lib2to3: fixer_util.py fixes/fix_long.py tests/test_fixers.py

benjamin.peterson python-checkins at python.org
Wed Dec 31 18:53:58 CET 2008


Author: benjamin.peterson
Date: Wed Dec 31 18:53:58 2008
New Revision: 68106

Log:
#2734 don't convert every instance of long (eg if it's an attribute)

Modified:
   sandbox/trunk/2to3/lib2to3/fixer_util.py
   sandbox/trunk/2to3/lib2to3/fixes/fix_long.py
   sandbox/trunk/2to3/lib2to3/tests/test_fixers.py

Modified: sandbox/trunk/2to3/lib2to3/fixer_util.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/fixer_util.py	(original)
+++ sandbox/trunk/2to3/lib2to3/fixer_util.py	Wed Dec 31 18:53:58 2008
@@ -222,6 +222,27 @@
             return True
     return False
 
+def is_probably_builtin(node):
+    """
+    Check that something isn't an attribute or function name etc.
+    """
+    prev = node.get_prev_sibling()
+    if prev is not None and prev.type == token.DOT:
+        # Attribute lookup.
+        return False
+    parent = node.parent
+    if parent.type in (syms.funcdef, syms.classdef):
+        return False
+    if parent.type == syms.expr_stmt and parent.children[0] is node:
+        # Assignment.
+        return False
+    if parent.type == syms.typedargslist and (
+        (prev is not None and prev.type == token.COMMA) or
+        parent.children[0] is node
+        ):
+        return False
+    return True
+
 ###########################################################
 ### The following functions are to find bindings in a suite
 ###########################################################

Modified: sandbox/trunk/2to3/lib2to3/fixes/fix_long.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/fixes/fix_long.py	(original)
+++ sandbox/trunk/2to3/lib2to3/fixes/fix_long.py	Wed Dec 31 18:53:58 2008
@@ -7,18 +7,17 @@
 # Local imports
 from .. import pytree
 from .. import fixer_base
-from ..fixer_util import Name, Number
+from ..fixer_util import Name, Number, is_probably_builtin
 
 
 class FixLong(fixer_base.BaseFix):
 
     PATTERN = "'long'"
 
-    static_long = Name("long")
     static_int = Name("int")
 
     def transform(self, node, results):
-        assert node == self.static_long, node
-        new = self.static_int.clone()
-        new.set_prefix(node.get_prefix())
-        return new
+        if is_probably_builtin(node):
+            new = self.static_int.clone()
+            new.set_prefix(node.get_prefix())
+            return new

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	Wed Dec 31 18:53:58 2008
@@ -1071,6 +1071,22 @@
         a = """z = type(x) in (int, int)"""
         self.check(b, a)
 
+    def test_unchanged(self):
+        s = """long = True"""
+        self.unchanged(s)
+
+        s = """s.long = True"""
+        self.unchanged(s)
+
+        s = """def long(): pass"""
+        self.unchanged(s)
+
+        s = """class long(): pass"""
+        self.unchanged(s)
+
+        s = """def f(x, long=True): pass"""
+        self.unchanged(s)
+
     def test_prefix_preservation(self):
         b = """x =   long(  x  )"""
         a = """x =   int(  x  )"""


More information about the Python-checkins mailing list