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