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