[Python-checkins] r53419 - in sandbox/trunk/2to3: example.py fixes/fix_long.py

guido.van.rossum python-checkins at python.org
Sun Jan 14 01:53:28 CET 2007


Author: guido.van.rossum
Date: Sun Jan 14 01:53:27 2007
New Revision: 53419

Added:
   sandbox/trunk/2to3/fixes/fix_long.py   (contents, props changed)
Modified:
   sandbox/trunk/2to3/example.py
Log:
Change long into int and strip trailing 'L' or 'l' from long literals.


Modified: sandbox/trunk/2to3/example.py
==============================================================================
--- sandbox/trunk/2to3/example.py	(original)
+++ sandbox/trunk/2to3/example.py	Sun Jan 14 01:53:27 2007
@@ -227,6 +227,17 @@
     raise Exception, 5, 6
     #
     raise Exception,5,6
+
+def long_examples():
+    x = long(x)
+    y = isinstance(x, long)
+    z = type(x) in (int, long)
+    a = 12L
+    b = 0x12l
+    # unchanged:
+    a = 12
+    b = 0x12
+    c = 3.14
     
     
 # This is the last line.

Added: sandbox/trunk/2to3/fixes/fix_long.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/2to3/fixes/fix_long.py	Sun Jan 14 01:53:27 2007
@@ -0,0 +1,38 @@
+# Copyright 2006 Google, Inc. All Rights Reserved.
+# Licensed to PSF under a Contributor Agreement.
+
+"""Fixer that turns 'long' into 'int' everywhere.
+
+This also strips the trailing 'L' or 'l' from long loterals.
+"""
+
+# Python imports
+import token
+
+# Local imports
+import pytree
+from fixes import basefix
+
+
+class FixLong(basefix.BaseFix):
+
+    PATTERN = """
+    (long_type = 'long' | number = NUMBER)
+    """
+
+    static_long = pytree.Leaf(token.NAME, "long")
+    static_int = pytree.Leaf(token.NAME, "int")
+
+    def transform(self, node):
+        results = self.match(node)
+        long_type = results.get("long_type")
+        number = results.get("number")
+        new = None
+        if long_type:
+            assert node == self.static_long, node
+            new = self.static_int.clone()
+        if number and node.value[-1] in ("l", "L"):
+            new = pytree.Leaf(token.NUMBER, node.value[:-1])
+        if new is not None:
+            new.set_prefix(node.get_prefix())
+            return new


More information about the Python-checkins mailing list