source: python/trunk/Lib/lib2to3/fixes/fix_unicode.py

Last change on this file was 391, checked in by dmik, 11 years ago

python: Merge vendor 2.7.6 to trunk.

  • Property svn:eol-style set to native
File size: 1.2 KB
Line 
1r"""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
11from ..pgen2 import token
12from .. import fixer_base
13
14_mapping = {u"unichr" : u"chr", u"unicode" : u"str"}
15
16class 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 u'\'"' and u'\\' in val:
32 val = ur'\\'.join([
33 v.replace(u'\\u', ur'\\u').replace(u'\\U', ur'\\U')
34 for v in val.split(ur'\\')
35 ])
36 if val[0] in u'uU':
37 val = val[1:]
38 if val == node.value:
39 return node
40 new = node.clone()
41 new.value = val
42 return new
Note: See TracBrowser for help on using the repository browser.