/Lib/lib2to3/fixes/fix_unicode.py

http://unladen-swallow.googlecode.com/ · Python · 28 lines · 23 code · 4 blank · 1 comment · 5 complexity · 05e9e9ae6cbc1c396bc11b19b5dab25a MD5 · raw file

  1. """Fixer that changes unicode to str, unichr to chr, and u"..." into "...".
  2. """
  3. import re
  4. from ..pgen2 import token
  5. from .. import fixer_base
  6. class FixUnicode(fixer_base.BaseFix):
  7. PATTERN = "STRING | NAME<'unicode' | 'unichr'>"
  8. def transform(self, node, results):
  9. if node.type == token.NAME:
  10. if node.value == "unicode":
  11. new = node.clone()
  12. new.value = "str"
  13. return new
  14. if node.value == "unichr":
  15. new = node.clone()
  16. new.value = "chr"
  17. return new
  18. # XXX Warn when __unicode__ found?
  19. elif node.type == token.STRING:
  20. if re.match(r"[uU][rR]?[\'\"]", node.value):
  21. new = node.clone()
  22. new.value = new.value[1:]
  23. return new