[Python-checkins] r75278 - in sandbox/trunk/2to3/lib2to3: fixes/fix_idioms.py tests/test_fixers.py
benjamin.peterson
python-checkins at python.org
Wed Oct 7 23:25:56 CEST 2009
Author: benjamin.peterson
Date: Wed Oct 7 23:25:56 2009
New Revision: 75278
Log:
fix whitespace problems with fix_idioms #3563
Patch by Joe Amenta.
Modified:
sandbox/trunk/2to3/lib2to3/fixes/fix_idioms.py
sandbox/trunk/2to3/lib2to3/tests/test_fixers.py
Modified: sandbox/trunk/2to3/lib2to3/fixes/fix_idioms.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/fixes/fix_idioms.py (original)
+++ sandbox/trunk/2to3/lib2to3/fixes/fix_idioms.py Wed Oct 7 23:25:56 2009
@@ -29,7 +29,7 @@
# Local imports
from .. import fixer_base
-from ..fixer_util import Call, Comma, Name, Node, syms
+from ..fixer_util import Call, Comma, Name, Node, BlankLine, syms
CMP = "(n='!=' | '==' | 'is' | n=comp_op< 'is' 'not' >)"
TYPE = "power< 'type' trailer< '(' x=any ')' > >"
@@ -130,5 +130,24 @@
else:
raise RuntimeError("should not have reached here")
sort_stmt.remove()
- if next_stmt:
- next_stmt[0].prefix = sort_stmt.prefix
+
+ btwn = sort_stmt.prefix
+ # Keep any prefix lines between the sort_stmt and the list_call and
+ # shove them right after the sorted() call.
+ if u"\n" in btwn:
+ if next_stmt:
+ # The new prefix should be everything from the sort_stmt's
+ # prefix up to the last newline, then the old prefix after a new
+ # line.
+ prefix_lines = (btwn.rpartition(u"\n")[0], next_stmt[0].prefix)
+ next_stmt[0].prefix = u"\n".join(prefix_lines)
+ else:
+ assert list_call.parent
+ assert list_call.next_sibling is None
+ # Put a blank line after list_call and set its prefix.
+ end_line = BlankLine()
+ list_call.parent.append_child(end_line)
+ assert list_call.next_sibling is end_line
+ # The new prefix should be everything up to the first new line
+ # of sort_stmt's prefix.
+ end_line.prefix = btwn.rpartition(u"\n")[0]
Modified: sandbox/trunk/2to3/lib2to3/tests/test_fixers.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/tests/test_fixers.py (original)
+++ sandbox/trunk/2to3/lib2to3/tests/test_fixers.py Wed Oct 7 23:25:56 2009
@@ -3238,6 +3238,46 @@
"""
self.check(b, a)
+ b = r"""
+ try:
+ m = list(s)
+ m.sort()
+ except: pass
+ """
+
+ a = r"""
+ try:
+ m = sorted(s)
+ except: pass
+ """
+ self.check(b, a)
+
+ b = r"""
+ try:
+ m = list(s)
+ # foo
+ m.sort()
+ except: pass
+ """
+
+ a = r"""
+ try:
+ m = sorted(s)
+ # foo
+ except: pass
+ """
+ self.check(b, a)
+
+ b = r"""
+ m = list(s)
+ # more comments
+ m.sort()"""
+
+ a = r"""
+ m = sorted(s)
+ # more comments"""
+ self.check(b, a)
+
def test_sort_simple_expr(self):
b = """
v = t
More information about the Python-checkins
mailing list