fix_unicode_keep_u.py 779 B

123456789101112131415161718192021222324
  1. """Fixer that changes unicode to str and unichr to chr, but -- unlike the
  2. lib2to3 fix_unicode.py fixer, does not change u"..." into "...".
  3. The reason is that Py3.3+ supports the u"..." string prefix, and, if
  4. present, the prefix may provide useful information for disambiguating
  5. between byte strings and unicode strings, which is often the hardest part
  6. of the porting task.
  7. """
  8. from lib2to3.pgen2 import token
  9. from lib2to3 import fixer_base
  10. _mapping = {u"unichr" : u"chr", u"unicode" : u"str"}
  11. class FixUnicodeKeepU(fixer_base.BaseFix):
  12. BM_compatible = True
  13. PATTERN = "'unicode' | 'unichr'"
  14. def transform(self, node, results):
  15. if node.type == token.NAME:
  16. new = node.clone()
  17. new.value = _mapping[node.value]
  18. return new