• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Fixer that changes unicode to str, unichr to chr, and u"..." into "...".
2
3"""
4
5import re
6from ..pgen2 import token
7from .. import fixer_base
8
9_mapping = {u"unichr" : u"chr", u"unicode" : u"str"}
10_literal_re = re.compile(ur"[uU][rR]?[\'\"]")
11
12class FixUnicode(fixer_base.BaseFix):
13    BM_compatible = True
14    PATTERN = "STRING | 'unicode' | 'unichr'"
15
16    def transform(self, node, results):
17        if node.type == token.NAME:
18            new = node.clone()
19            new.value = _mapping[node.value]
20            return new
21        elif node.type == token.STRING:
22            if _literal_re.match(node.value):
23                new = node.clone()
24                new.value = new.value[1:]
25                return new
26