~ubuntu-branches/ubuntu/lucid/python2.6/lucid

« back to all changes in this revision

Viewing changes to Lib/lib2to3/fixes/fix_idioms.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-03-11 13:30:19 UTC
  • mto: (10.1.13 sid)
  • mto: This revision was merged to the branch mainline in revision 44.
  • Revision ID: james.westby@ubuntu.com-20100311133019-sblbooa3uqrkoe70
Tags: upstream-2.6.5~rc2
ImportĀ upstreamĀ versionĀ 2.6.5~rc2

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
 
30
30
# Local imports
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
33
33
 
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
104
 
        x.set_prefix("")
105
 
        T.set_prefix(" ")
106
 
        test = Call(Name("isinstance"), [x, Comma(), T])
 
104
        x.prefix = u""
 
105
        T.prefix = u" "
 
106
        test = Call(Name(u"isinstance"), [x, Comma(), T])
107
107
        if "n" in results:
108
 
            test.set_prefix(" ")
109
 
            test = Node(syms.not_test, [Name("not"), test])
110
 
        test.set_prefix(node.get_prefix())
 
108
            test.prefix = u" "
 
109
            test = Node(syms.not_test, [Name(u"not"), test])
 
110
        test.prefix = node.prefix
111
111
        return test
112
112
 
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))
116
116
 
117
117
    def transform_sort(self, node, results):
118
118
        sort_stmt = results["sort"]
121
121
        simple_expr = results.get("expr")
122
122
 
123
123
        if list_call:
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()
127
 
            new.set_prefix("")
128
 
            simple_expr.replace(Call(Name("sorted"), [new],
129
 
                                     prefix=simple_expr.get_prefix()))
 
127
            new.prefix = u""
 
128
            simple_expr.replace(Call(Name(u"sorted"), [new],
 
129
                                     prefix=simple_expr.prefix))
130
130
        else:
131
131
            raise RuntimeError("should not have reached here")
132
132
        sort_stmt.remove()
133
 
        if next_stmt:
134
 
            next_stmt[0].set_prefix(sort_stmt.get_prefix())
 
133
 
 
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.
 
137
        if u"\n" in btwn:
 
138
            if next_stmt:
 
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
 
141
                # line.
 
142
                prefix_lines = (btwn.rpartition(u"\n")[0], next_stmt[0].prefix)
 
143
                next_stmt[0].prefix = u"\n".join(prefix_lines)
 
144
            else:
 
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]