fix_unicode.py 1.23 KB
Newer Older
1 2 3 4 5 6 7
r"""Fixer for unicode.

* Changes unicode to str and unichr to chr.

* If "...\u..." is not unicode literal change it into "...\\u...".

* Change u"..." into "...".
8 9 10 11

"""

from ..pgen2 import token
12
from .. import fixer_base
13

14 15
_mapping = {"unichr" : "chr", "unicode" : "str"}

16
class FixUnicode(fixer_base.BaseFix):
Benjamin Peterson's avatar
Benjamin Peterson committed
17
    BM_compatible = True
18
    PATTERN = "STRING | 'unicode' | 'unichr'"
19

20 21 22 23
    def start_tree(self, tree, filename):
        super(FixUnicode, self).start_tree(tree, filename)
        self.unicode_literals = 'unicode_literals' in tree.future_features

24 25
    def transform(self, node, results):
        if node.type == token.NAME:
26 27 28
            new = node.clone()
            new.value = _mapping[node.value]
            return new
29
        elif node.type == token.STRING:
30
            val = node.value
31
            if not self.unicode_literals and val[0] in '\'"' and '\\' in val:
32 33 34 35 36 37 38 39 40 41 42
                val = r'\\'.join([
                    v.replace('\\u', r'\\u').replace('\\U', r'\\U')
                    for v in val.split(r'\\')
                ])
            if val[0] in 'uU':
                val = val[1:]
            if val == node.value:
                return node
            new = node.clone()
            new.value = val
            return new