openmedialibrary_platform/Darwin/lib/python3.5/lib2to3/fixes/fix_unicode.py

43 lines
1.2 KiB
Python
Raw Normal View History

2013-10-11 17:28:32 +00:00
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 "...".
"""
from ..pgen2 import token
from .. import fixer_base
2014-09-30 16:15:32 +00:00
_mapping = {"unichr" : "chr", "unicode" : "str"}
2013-10-11 17:28:32 +00:00
class FixUnicode(fixer_base.BaseFix):
BM_compatible = True
PATTERN = "STRING | 'unicode' | 'unichr'"
def start_tree(self, tree, filename):
super(FixUnicode, self).start_tree(tree, filename)
self.unicode_literals = 'unicode_literals' in tree.future_features
def transform(self, node, results):
if node.type == token.NAME:
new = node.clone()
new.value = _mapping[node.value]
return new
elif node.type == token.STRING:
val = node.value
2014-09-30 16:15:32 +00:00
if not self.unicode_literals and val[0] in '\'"' and '\\' in val:
val = r'\\'.join([
v.replace('\\u', r'\\u').replace('\\U', r'\\U')
for v in val.split(r'\\')
2013-10-11 17:28:32 +00:00
])
2014-09-30 16:15:32 +00:00
if val[0] in 'uU':
2013-10-11 17:28:32 +00:00
val = val[1:]
if val == node.value:
return node
new = node.clone()
new.value = val
return new