[Python-checkins] r58644 - in sandbox/trunk/2to3: example.py fixes/fix_basestring.py fixes/fix_buffer.py tests/test_fixers.py

guido.van.rossum python-checkins at python.org
Wed Oct 24 21:56:50 CEST 2007


Author: guido.van.rossum
Date: Wed Oct 24 21:56:49 2007
New Revision: 58644

Added:
   sandbox/trunk/2to3/fixes/fix_basestring.py   (contents, props changed)
   sandbox/trunk/2to3/fixes/fix_buffer.py   (contents, props changed)
Modified:
   sandbox/trunk/2to3/example.py
   sandbox/trunk/2to3/tests/test_fixers.py
Log:
Add two new fixers:
- basestring: a simple substitute basestring -> str, by Christian Heimes;
- buffer: buffer(...) -> memoryview(...); but not other uses, since
  buffer is such a popular variable name (mine).


Modified: sandbox/trunk/2to3/example.py
==============================================================================
--- sandbox/trunk/2to3/example.py	(original)
+++ sandbox/trunk/2to3/example.py	Wed Oct 24 21:56:49 2007
@@ -355,6 +355,12 @@
     map(None, foo, bar)
     map(f, foo.bar)
     map(lambda x: x+1, range(10))
-    
+
+def basestring_examples():
+    if isinstance(x, basestring): pass
+
+def buffer_examples():
+    x = buffer(y)
+
 
 # This is the last line.

Added: sandbox/trunk/2to3/fixes/fix_basestring.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/2to3/fixes/fix_basestring.py	Wed Oct 24 21:56:49 2007
@@ -0,0 +1,13 @@
+"""Fixer for basestring -> str."""
+# Author: Christian Heimes
+
+# Local imports
+from fixes import basefix
+from fixes.util import Name
+
+class FixBasestring(basefix.BaseFix):
+
+    PATTERN = "'basestring'"
+
+    def transform(self, node, results):
+        return Name("str", prefix=node.get_prefix())

Added: sandbox/trunk/2to3/fixes/fix_buffer.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/2to3/fixes/fix_buffer.py	Wed Oct 24 21:56:49 2007
@@ -0,0 +1,21 @@
+# Copyright 2007 Google, Inc. All Rights Reserved.
+# Licensed to PSF under a Contributor Agreement.
+
+"""Fixer that changes buffer(...) into memoryview(...)."""
+
+# Local imports
+from fixes import basefix
+from fixes.util import Name
+
+
+class FixBuffer(basefix.BaseFix):
+
+    explicit = True # The user must ask for this fixer
+
+    PATTERN = """
+              power< name='buffer' trailer< '(' [any] ')' > >
+              """
+
+    def transform(self, node, results):
+        name = results["name"]
+        name.replace(Name("memoryview", prefix=name.get_prefix()))

Modified: sandbox/trunk/2to3/tests/test_fixers.py
==============================================================================
--- sandbox/trunk/2to3/tests/test_fixers.py	(original)
+++ sandbox/trunk/2to3/tests/test_fixers.py	Wed Oct 24 21:56:49 2007
@@ -2716,6 +2716,24 @@
         self.unchanged(s)
 
 
+class Test_basestring(FixerTestCase):
+    fixer = "basestring"
+
+    def test_basestring(self):
+        b = """isinstance(x, basestring)"""
+        a = """isinstance(x, str)"""
+        self.check(b, a)
+
+
+class Test_buffer(FixerTestCase):
+    fixer = "buffer"
+
+    def test_buffer(self):
+        b = """x = buffer(y)"""
+        a = """x = memoryview(y)"""
+        self.check(b, a)
+
+
 if __name__ == "__main__":
     import __main__
     support.run_all_tests(__main__)


More information about the Python-checkins mailing list