Commits

Anonymous committed dcb9639

Moving normal int literal fixes to fix_int and making explicit.
In all cases but repr(), 3.x int is the same as 2.x long, but repr() is enough of a reason to make this explicit.

Comments (0)

Files changed (4)

lib3to2/fixes/fix_int.py

 """
-Fixer for int -> long.
+Fixer for:
+int -> long
+123 -> 123L
 """
 
 from lib2to3 import fixer_base
-from lib2to3.fixer_util import Name, is_probably_builtin
+from lib2to3.fixer_util import Name, is_probably_builtin, Number
+from lib2to3.pgen2 import token
 
+baseMAPPING = {'b':2, 'o':8, 'x':16}
 
 class FixInt(fixer_base.BaseFix):
-
-    PATTERN = "'int'"
+    
+    explicit = True # In most cases, 3.x ints will work just like 2.x ints.
+    
+    PATTERN = "'int' | NUMBER"
 
     static_long = Name(u"long")
+    
+    def base(self, literal):
+        """Returns the base of a valid py3k numeric literal."""
+        literal = literal.strip()
+        if not literal.startswith(u"0"):
+            return 10
+        else:
+            if literal[1] not in u"box":
+                return 0
+            return baseMAPPING[literal[1]]
+            
+    def unmatch(self, node):
+        """Don't match complex numbers, floats, or longs"""
+        val = node.value
+        #For whatever reason, some ints are being matched after we fix them.
+        if val.endswith("L"):
+            return "L"
+        for bad in u"jJ+-.":
+            if bad in val: return bad
+            
+    def match(self, node):
+        return super(FixInt, self).match(node) and not self.unmatch(node)
 
     def transform(self, node, results):
-        if is_probably_builtin(node):
+        val = node.value
+        if node.type == token.NUMBER and self.base(val) == 10:
+            assert not val[-1] in u"lL", "Invalid py3k literal: " + str(val)
+            val += u"L"
+            return Number(val, prefix=node.prefix)
+        elif is_probably_builtin(node):
+            assert node.type == token.NAME, "Sanity check failed: " + str(val)
             new = self.static_long.clone()
             new.prefix = node.prefix
             return new

lib3to2/fixes/fix_numliterals.py

 """
 Fixer for:
-1 -> 1L
 0x1ed -> __builtins__.long("1ed", 16)
 0b111101101 -> __builtins__.long("111101101", 2)
 0o755 -> 0755
 
 class FixNumliterals(fixer_base.BaseFix):
     # We need to modify all numeric literals except floats, complex.
-
+    
     def base(self, literal):
         """Returns the base of a valid py3k literal."""
         literal = literal.strip()
             if literal[1] not in u"box":
                 return 0
             return baseMAPPING[literal[1]]
-
+    
     def unmatch(self, node):
-        """Don't match complex numbers or floats."""
+        """Don't match complex numbers, floats, or base-10 ints"""
+        val = node.value
+        if self.base(val) == 10:
+            return val
         for bad in u"jJ+-.":
-            if bad in node.value: return bad
+            if bad in val: return bad
         
     def match(self, node):
         return ((node.type == token.NUMBER) and not self.unmatch(node))
         Call __builtins__.long() with the value and the base of the value.
         This works because 0b10 is int("10", 2), 0o10 is int("10", 8), etc.
         """
-        
         val = node.value
-        base = self.base
-        if val.isdigit() and base(val) == 10:
-            assert not val[-1] in u"lL", "Invalid py3k literal"
-            val += u"L"
-            return Number(val, prefix=node.prefix)
-        elif base(val) == 8:
+
+        if self.base(val) == 8:
             assert val.strip().startswith(u"0o") or \
             val.strip().startswith(u"0O"), "Invalid format for octal literal"
             val = u"".join((u"0",val[2:]))
             return Number(val, prefix=node.prefix)
-        elif base(val) == 16 or base(val) == 2:
+        elif self.base(val) == 16 or self.base(val) == 2:
             assert val.startswith(u"0") and val[1] in u"bxBX", \
                                            "Invalid format for numeric literal"
-            base = Number(base(val), prefix=u" ")
+            base = Number(self.base(val), prefix=u" ")
             # __builtins__.long
             func_name = Node(syms.power, Attr(Name(u"__builtins__"), \
                              Name(u"long")))

lib3to2/tests/test_int.py

         a = """x =   long(  x  )"""
         self.check(b, a)
 
+    def test_literal_1(self):
+        b = """5"""
+        a = """5L"""
+        self.check(b, a)
+        
+    def test_literal_2(self):
+        b = """a = 12"""
+        a = """a = 12L"""
+        self.check(b, a)
+
+    def test_complex_1(self):
+        b = """5 + 4j"""
+        a = """5L + 4j"""
+        self.check(b, a)
+
+    def test_complex_2(self):
+        b = """35  +  2j"""
+        a = """35L  +  2j"""
+        self.check(b, a)

lib3to2/tests/test_numliterals.py

 class Test_numliterals(lib3to2FixerTestCase):
     fixer = "numliterals"
 
-    def test_long_1(self):
-        b = """5"""
-        a = """5L"""
-        self.check(b, a)
-        
-    def test_long_2(self):
-        b = """a = 12"""
-        a = """a = 12L"""
-        self.check(b, a)
-    
     def test_octal_1(self):
         b = """0o755"""
         a = """0755"""
         a = """b =   __builtins__.long("12", 16)"""
         self.check(b, a)
 
-    def test_complex_1(self):
-        b = """5 + 4j"""
-        a = """5L + 4j"""
-        self.check(b, a)
-
-    def test_complex_2(self):
-        b = """35  +  2j"""
-        a = """35L  +  2j"""
-        self.check(b, a)
-        
     def test_comments_and_spacing_2(self):
         b = """b = 0o755 # spam"""
         a = """b = 0755 # spam"""