31
31
from .. import fixer_base
32
from ..fixer_util import Call, Comma, Name, Node, syms
32
from ..fixer_util import Call, Comma, Name, Node, BlankLine, syms
34
34
CMP = "(n='!=' | '==' | 'is' | n=comp_op< 'is' 'not' >)"
35
35
TYPE = "power< 'type' trailer< '(' x=any ')' > >"
101
101
def transform_isinstance(self, node, results):
102
102
x = results["x"].clone() # The thing inside of type()
103
103
T = results["T"].clone() # The type being compared against
106
test = Call(Name("isinstance"), [x, Comma(), T])
106
test = Call(Name(u"isinstance"), [x, Comma(), T])
107
107
if "n" in results:
109
test = Node(syms.not_test, [Name("not"), test])
110
test.set_prefix(node.get_prefix())
109
test = Node(syms.not_test, [Name(u"not"), test])
110
test.prefix = node.prefix
113
113
def transform_while(self, node, results):
114
114
one = results["while"]
115
one.replace(Name("True", prefix=one.get_prefix()))
115
one.replace(Name(u"True", prefix=one.prefix))
117
117
def transform_sort(self, node, results):
118
118
sort_stmt = results["sort"]
121
121
simple_expr = results.get("expr")
124
list_call.replace(Name("sorted", prefix=list_call.get_prefix()))
124
list_call.replace(Name(u"sorted", prefix=list_call.prefix))
125
125
elif simple_expr:
126
126
new = simple_expr.clone()
128
simple_expr.replace(Call(Name("sorted"), [new],
129
prefix=simple_expr.get_prefix()))
128
simple_expr.replace(Call(Name(u"sorted"), [new],
129
prefix=simple_expr.prefix))
131
131
raise RuntimeError("should not have reached here")
132
132
sort_stmt.remove()
134
next_stmt[0].set_prefix(sort_stmt.get_prefix())
134
btwn = sort_stmt.prefix
135
# Keep any prefix lines between the sort_stmt and the list_call and
136
# shove them right after the sorted() call.
139
# The new prefix should be everything from the sort_stmt's
140
# prefix up to the last newline, then the old prefix after a new
142
prefix_lines = (btwn.rpartition(u"\n")[0], next_stmt[0].prefix)
143
next_stmt[0].prefix = u"\n".join(prefix_lines)
145
assert list_call.parent
146
assert list_call.next_sibling is None
147
# Put a blank line after list_call and set its prefix.
148
end_line = BlankLine()
149
list_call.parent.append_child(end_line)
150
assert list_call.next_sibling is end_line
151
# The new prefix should be everything up to the first new line
152
# of sort_stmt's prefix.
153
end_line.prefix = btwn.rpartition(u"\n")[0]