Kaydet (Commit) b9da032a authored tarafından Patrick Maupin's avatar Patrick Maupin

Add support for __future__ unicode_literals

  -- This only affects a very few modules that did not roundtrip properly
üst 31fe3980
...@@ -125,6 +125,8 @@ class SourceGenerator(ExplicitNodeVisitor): ...@@ -125,6 +125,8 @@ class SourceGenerator(ExplicitNodeVisitor):
""" """
using_unicode_literals = False
def __init__(self, indent_with, add_line_information=False, def __init__(self, indent_with, add_line_information=False,
pretty_string=pretty_string): pretty_string=pretty_string):
self.result = [] self.result = []
...@@ -264,6 +266,10 @@ class SourceGenerator(ExplicitNodeVisitor): ...@@ -264,6 +266,10 @@ class SourceGenerator(ExplicitNodeVisitor):
self.statement(node, 'from ', node.level * '.', self.statement(node, 'from ', node.level * '.',
node.module or '', ' import ') node.module or '', ' import ')
self.comma_list(node.names) self.comma_list(node.names)
# Goofy stuff for Python 2.7 _pyio module
if node.module == '__future__' and 'unicode_literals' in (
x.name for x in node.names):
self.using_unicode_literals = True
def visit_Import(self, node): def visit_Import(self, node):
self.statement(node, 'import ') self.statement(node, 'import ')
...@@ -497,7 +503,8 @@ class SourceGenerator(ExplicitNodeVisitor): ...@@ -497,7 +503,8 @@ class SourceGenerator(ExplicitNodeVisitor):
# Cheesy way to force a flush # Cheesy way to force a flush
self.write('foo') self.write('foo')
result.pop() result.pop()
result.append(self.pretty_string(node.s, embedded, result)) result.append(self.pretty_string(node.s, embedded, result,
uni_lit=self.using_unicode_literals))
def visit_JoinedStr(self, node, def visit_JoinedStr(self, node,
# constants # constants
......
...@@ -76,7 +76,8 @@ def _prep_triple_quotes(s, mysplit=mysplit, replacements=replacements): ...@@ -76,7 +76,8 @@ def _prep_triple_quotes(s, mysplit=mysplit, replacements=replacements):
return ''.join(s) return ''.join(s)
def pretty_string(s, embedded, current_output, min_trip_str=20, max_line=100): def pretty_string(s, embedded, current_output, min_trip_str=20,
max_line=100, uni_lit=False):
"""There are a lot of reasons why we might not want to or """There are a lot of reasons why we might not want to or
be able to return a triple-quoted string. We can always be able to return a triple-quoted string. We can always
punt back to the default normal string. punt back to the default normal string.
...@@ -87,6 +88,8 @@ def pretty_string(s, embedded, current_output, min_trip_str=20, max_line=100): ...@@ -87,6 +88,8 @@ def pretty_string(s, embedded, current_output, min_trip_str=20, max_line=100):
# Punt on abnormal strings # Punt on abnormal strings
if (isinstance(s, special_unicode) or not isinstance(s, basestring)): if (isinstance(s, special_unicode) or not isinstance(s, basestring)):
return default return default
if uni_lit and isinstance(s, bytes):
return 'b' + default
len_s = len(default) len_s = len(default)
current_line = _get_line(current_output) current_line = _get_line(current_output)
......
...@@ -417,6 +417,14 @@ class CodegenTestCase(unittest.TestCase): ...@@ -417,6 +417,14 @@ class CodegenTestCase(unittest.TestCase):
ast2 = astor.parsefile(__file__) ast2 = astor.parsefile(__file__)
self.assertEqual(astor.to_source(ast1), astor.codegen.to_source(ast2)) self.assertEqual(astor.to_source(ast1), astor.codegen.to_source(ast2))
def test_unicode_literals(self):
source = """
from __future__ import (print_function, unicode_literals)
x = b'abc'
y = u'abc'
"""
self.assertAstEqual(source)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment