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

benjamin.peterson python-checkins at python.org
Sat May 30 00:33:21 CEST 2009


Author: benjamin.peterson
Date: Sat May 30 00:33:20 2009
New Revision: 73036

Log:
simplify fix_unicode

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

Modified: sandbox/trunk/2to3/lib2to3/fixes/fix_unicode.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/fixes/fix_unicode.py	(original)
+++ sandbox/trunk/2to3/lib2to3/fixes/fix_unicode.py	Sat May 30 00:33:20 2009
@@ -6,23 +6,20 @@
 from ..pgen2 import token
 from .. import fixer_base
 
+_mapping = {u"unichr" : u"chr", u"unicode" : u"str"}
+_literal_re = re.compile(ur"[uU][rR]?[\'\"]")
+
 class FixUnicode(fixer_base.BaseFix):
 
-    PATTERN = "STRING | NAME<'unicode' | 'unichr'>"
+    PATTERN = "STRING | 'unicode' | 'unichr'"
 
     def transform(self, node, results):
         if node.type == token.NAME:
-            if node.value == u"unicode":
-                new = node.clone()
-                new.value = u"str"
-                return new
-            if node.value == u"unichr":
-                new = node.clone()
-                new.value = u"chr"
-                return new
-            # XXX Warn when __unicode__ found?
+            new = node.clone()
+            new.value = _mapping[node.value]
+            return new
         elif node.type == token.STRING:
-            if re.match(ur"[uU][rR]?[\'\"]", node.value):
+            if _literal_re.match(node.value):
                 new = node.clone()
                 new.value = new.value[1:]
                 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	Sat May 30 00:33:20 2009
@@ -2646,6 +2646,11 @@
         a = """str(x, y, z)"""
         self.check(b, a)
 
+    def test_unichr(self):
+        b = """unichr(u'h')"""
+        a = """chr('h')"""
+        self.check(b, a)
+
     def test_unicode_literal_1(self):
         b = '''u"x"'''
         a = '''"x"'''


More information about the Python-checkins mailing list