Skip to content

Commit

Permalink
Support Unicode in experimental fixer on Python 2
Browse files Browse the repository at this point in the history
  • Loading branch information
myint committed Jan 12, 2014
1 parent b46610b commit 7358d45
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
24 changes: 12 additions & 12 deletions autopep8.py
Original file line number Diff line number Diff line change
Expand Up @@ -1393,7 +1393,7 @@ def __init__(self, elements):
def __repr__(self):
string = ''
prev_val = None
for val in map(repr, self.elements):
for val in map(unicode, self.elements):
if prev_val and prev_val not in '(.' and val[0] not in '[{(,.)}]':
string += ' '
string += val
Expand Down Expand Up @@ -1442,7 +1442,7 @@ def __init__(self, elements):
def __repr__(self):
string = ''
prev_val = None
for elem in map(repr, self.elements):
for elem in map(unicode, self.elements):
if elem == ',':
string += ', '
elif prev_val and prev_val.endswith(tuple(')]}')):
Expand Down Expand Up @@ -1538,7 +1538,7 @@ def __init__(self, key, value):
self.value = value

def __repr__(self):
return repr(self.key) + ': ' + repr(self.value)
return unicode(self.key) + ': ' + unicode(self.value)

def __len__(self):
return 1
Expand Down Expand Up @@ -1676,7 +1676,7 @@ def _get_as_string(atoms):

string = ''
prev_val = None
for val in map(repr, atoms):
for val in map(unicode, atoms):
if prev_val and prev_val not in '(.' and val not in '(),.':
string += ' '
string += val
Expand All @@ -1702,7 +1702,7 @@ def _reflow_lines_recursive(interior, current_indent, max_line_length,
# it.
if lines[curr_idx].endswith(','):
lines[curr_idx] += ' '
lines[curr_idx] += repr(item)
lines[curr_idx] += unicode(item)

else:
# The container will go over multiple lines. Reflow the
Expand All @@ -1721,10 +1721,10 @@ def _reflow_lines_recursive(interior, current_indent, max_line_length,
# We want to place adjacent strings on separate lines.
string_list = []
if isinstance(item, Atom):
string_list.append(current_indent + repr(item))
string_list.append(current_indent + unicode(item))
else:
for string in item:
string_list.append(current_indent + repr(string))
string_list.append(current_indent + unicode(string))

if len(lines[-1]) + len(string_list[0]) + 3 < max_line_length:
lines[-1] += ' ' + string_list[0].lstrip()
Expand All @@ -1737,21 +1737,21 @@ def _reflow_lines_recursive(interior, current_indent, max_line_length,
elif (
line_extent + item.size + 2 < max_line_length or
(line_extent + item.size < max_line_length and
repr(item) == ',')
unicode(item) == ',')
):
# The current element fits on the current line.
if lines[curr_idx].endswith(','):
lines[curr_idx] += ' '
lines[curr_idx] += repr(item)
lines[curr_idx] += unicode(item)

elif not lines[curr_idx].strip():
# A degenerate case. The current atom is over the line length. We
# can't do anything except place it on the line and proceed from
# there.
lines[curr_idx] += repr(item)
lines[curr_idx] += unicode(item)

else:
lines.append(current_indent + repr(item))
lines.append(current_indent + unicode(item))
curr_idx += 1

return lines
Expand All @@ -1765,7 +1765,7 @@ def _reflow_lines(parsed_tokens, indentation, indent_word,

curr_starting_idx = 0

if repr(parsed_tokens[0]) == 'def':
if unicode(parsed_tokens[0]) == 'def':
# A function definition gets indented a bit more.
start_indent = indentation + indent_word * 2
else:
Expand Down
15 changes: 14 additions & 1 deletion test/test_autopep8.py
Original file line number Diff line number Diff line change
Expand Up @@ -3118,7 +3118,7 @@ def test_e501_experimental_with_multiple_lines_and_quotes(self):
with autopep8_context(line, options=['--experimental']) as result:
self.assertEqual(fixed, result)

def test_e501_experimental_avoid_breaking_at_empty_arentheses_if_possible(self):
def test_e501_experimental_avoid_breaking_at_empty_parentheses_if_possible(self):
line = """\
someverylongindenttionwhatnot().foo().bar().baz("and here is a long string 123456789012345678901234567890")
"""
Expand All @@ -3131,6 +3131,19 @@ def test_e501_experimental_avoid_breaking_at_empty_arentheses_if_possible(self):
with autopep8_context(line, options=['--experimental']) as result:
self.assertEqual(fixed, result)

def test_e501_experimental_with_unicode(self):
line = """\
someverylongindenttionwhatnot().foo().bar().baz("and here is a l안녕하세요 123456789012345678901234567890")
"""
fixed = """\
someverylongindenttionwhatnot(
).foo().bar().baz(
"and here is a l안녕하세요 123456789012345678901234567890")
"""

with autopep8_context(line, options=['--experimental']) as result:
self.assertEqual(fixed, result)

def test_e502(self):
line = "print('abc'\\\n 'def')\n"
fixed = "print('abc'\n 'def')\n"
Expand Down

0 comments on commit 7358d45

Please sign in to comment.