fix_unicode.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. r"""Fixer for unicode.
  2. * Changes unicode to str and unichr to chr.
  3. * If "...\u..." is not unicode literal change it into "...\\u...".
  4. * Change u"..." into "...".
  5. """
  6. from ..pgen2 import token
  7. from .. import fixer_base
  8. _mapping = {"unichr" : "chr", "unicode" : "str"}
  9. class FixUnicode(fixer_base.BaseFix):
  10. BM_compatible = True
  11. PATTERN = "STRING | 'unicode' | 'unichr'"
  12. def start_tree(self, tree, filename):
  13. super(FixUnicode, self).start_tree(tree, filename)
  14. self.unicode_literals = 'unicode_literals' in tree.future_features
  15. def transform(self, node, results):
  16. if node.type == token.NAME:
  17. new = node.clone()
  18. new.value = _mapping[node.value]
  19. return new
  20. elif node.type == token.STRING:
  21. val = node.value
  22. if not self.unicode_literals and val[0] in '\'"' and '\\' in val:
  23. val = r'\\'.join([
  24. v.replace('\\u', r'\\u').replace('\\U', r'\\U')
  25. for v in val.split(r'\\')
  26. ])
  27. if val[0] in 'uU':
  28. val = val[1:]
  29. if val == node.value:
  30. return node
  31. new = node.clone()
  32. new.value = val
  33. return new