ยปCore Development>Code coverage>Lib/lib2to3/fixes/fix_unicode.py

Python code coverage for Lib/lib2to3/fixes/fix_unicode.py

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