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

« back to all changes in this revision

Viewing changes to Lib/lib2to3/fixer_util.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:
14
14
 
15
15
def KeywordArg(keyword, value):
16
16
    return Node(syms.argument,
17
 
                [keyword, Leaf(token.EQUAL, '='), value])
 
17
                [keyword, Leaf(token.EQUAL, u'='), value])
18
18
 
19
19
def LParen():
20
 
    return Leaf(token.LPAR, "(")
 
20
    return Leaf(token.LPAR, u"(")
21
21
 
22
22
def RParen():
23
 
    return Leaf(token.RPAR, ")")
 
23
    return Leaf(token.RPAR, u")")
24
24
 
25
25
def Assign(target, source):
26
26
    """Build an assignment statement"""
27
27
    if not isinstance(target, list):
28
28
        target = [target]
29
29
    if not isinstance(source, list):
30
 
        source.set_prefix(" ")
 
30
        source.prefix = u" "
31
31
        source = [source]
32
32
 
33
33
    return Node(syms.atom,
34
 
                target + [Leaf(token.EQUAL, "=", prefix=" ")] + source)
 
34
                target + [Leaf(token.EQUAL, u"=", prefix=u" ")] + source)
35
35
 
36
36
def Name(name, prefix=None):
37
37
    """Return a NAME leaf"""
43
43
 
44
44
def Comma():
45
45
    """A comma leaf"""
46
 
    return Leaf(token.COMMA, ",")
 
46
    return Leaf(token.COMMA, u",")
47
47
 
48
48
def Dot():
49
49
    """A period (.) leaf"""
50
 
    return Leaf(token.DOT, ".")
 
50
    return Leaf(token.DOT, u".")
51
51
 
52
52
def ArgList(args, lparen=LParen(), rparen=RParen()):
53
53
    """A parenthesised argument list, used by Call()"""
60
60
    """A function call"""
61
61
    node = Node(syms.power, [func_name, ArgList(args)])
62
62
    if prefix is not None:
63
 
        node.set_prefix(prefix)
 
63
        node.prefix = prefix
64
64
    return node
65
65
 
66
66
def Newline():
67
67
    """A newline literal"""
68
 
    return Leaf(token.NEWLINE, "\n")
 
68
    return Leaf(token.NEWLINE, u"\n")
69
69
 
70
70
def BlankLine():
71
71
    """A blank line"""
72
 
    return Leaf(token.NEWLINE, "")
 
72
    return Leaf(token.NEWLINE, u"")
73
73
 
74
74
def Number(n, prefix=None):
75
75
    return Leaf(token.NUMBER, n, prefix=prefix)
76
76
 
77
77
def Subscript(index_node):
78
78
    """A numeric or string subscript"""
79
 
    return Node(syms.trailer, [Leaf(token.LBRACE, '['),
 
79
    return Node(syms.trailer, [Leaf(token.LBRACE, u'['),
80
80
                               index_node,
81
 
                               Leaf(token.RBRACE, ']')])
 
81
                               Leaf(token.RBRACE, u']')])
82
82
 
83
83
def String(string, prefix=None):
84
84
    """A string leaf"""
89
89
 
90
90
    If test is None, the "if test" part is omitted.
91
91
    """
92
 
    xp.set_prefix("")
93
 
    fp.set_prefix(" ")
94
 
    it.set_prefix(" ")
95
 
    for_leaf = Leaf(token.NAME, "for")
96
 
    for_leaf.set_prefix(" ")
97
 
    in_leaf = Leaf(token.NAME, "in")
98
 
    in_leaf.set_prefix(" ")
 
92
    xp.prefix = u""
 
93
    fp.prefix = u" "
 
94
    it.prefix = u" "
 
95
    for_leaf = Leaf(token.NAME, u"for")
 
96
    for_leaf.prefix = u" "
 
97
    in_leaf = Leaf(token.NAME, u"in")
 
98
    in_leaf.prefix = u" "
99
99
    inner_args = [for_leaf, fp, in_leaf, it]
100
100
    if test:
101
 
        test.set_prefix(" ")
102
 
        if_leaf = Leaf(token.NAME, "if")
103
 
        if_leaf.set_prefix(" ")
 
101
        test.prefix = u" "
 
102
        if_leaf = Leaf(token.NAME, u"if")
 
103
        if_leaf.prefix = u" "
104
104
        inner_args.append(Node(syms.comp_if, [if_leaf, test]))
105
105
    inner = Node(syms.listmaker, [xp, Node(syms.comp_for, inner_args)])
106
106
    return Node(syms.atom,
107
 
                       [Leaf(token.LBRACE, "["),
 
107
                       [Leaf(token.LBRACE, u"["),
108
108
                        inner,
109
 
                        Leaf(token.RBRACE, "]")])
 
109
                        Leaf(token.RBRACE, u"]")])
110
110
 
111
111
def FromImport(package_name, name_leafs):
112
112
    """ Return an import statement in the form:
120
120
        # Pull the leaves out of their old tree
121
121
        leaf.remove()
122
122
 
123
 
    children = [Leaf(token.NAME, 'from'),
124
 
                Leaf(token.NAME, package_name, prefix=" "),
125
 
                Leaf(token.NAME, 'import', prefix=" "),
 
123
    children = [Leaf(token.NAME, u'from'),
 
124
                Leaf(token.NAME, package_name, prefix=u" "),
 
125
                Leaf(token.NAME, u'import', prefix=u" "),
126
126
                Node(syms.import_as_names, name_leafs)]
127
127
    imp = Node(syms.import_from, children)
128
128
    return imp
141
141
            and isinstance(node.children[0], Leaf)
142
142
            and isinstance(node.children[1], Node)
143
143
            and isinstance(node.children[2], Leaf)
144
 
            and node.children[0].value == "("
145
 
            and node.children[2].value == ")")
 
144
            and node.children[0].value == u"("
 
145
            and node.children[2].value == u")")
146
146
 
147
147
def is_list(node):
148
148
    """Does the node represent a list literal?"""
150
150
            and len(node.children) > 1
151
151
            and isinstance(node.children[0], Leaf)
152
152
            and isinstance(node.children[-1], Leaf)
153
 
            and node.children[0].value == "["
154
 
            and node.children[-1].value == "]")
 
153
            and node.children[0].value == u"["
 
154
            and node.children[-1].value == u"]")
155
155
 
156
156
 
157
157
###########################################################
226
226
    """
227
227
    Check that something isn't an attribute or function name etc.
228
228
    """
229
 
    prev = node.get_prev_sibling()
 
229
    prev = node.prev_sibling
230
230
    if prev is not None and prev.type == token.DOT:
231
231
        # Attribute lookup.
232
232
        return False
291
291
    if does_tree_import(package, name, root):
292
292
        return
293
293
 
294
 
    add_newline_before = False
295
 
 
296
294
    # figure out where to insert the new import.  First try to find
297
295
    # the first import and then skip to the last one.
298
296
    insert_pos = offset = 0
312
310
            if node.type == syms.simple_stmt and node.children and \
313
311
               node.children[0].type == token.STRING:
314
312
                insert_pos = idx + 1
315
 
                add_newline_before
316
313
                break
317
314
 
318
315
    if package is None:
319
316
        import_ = Node(syms.import_name, [
320
 
            Leaf(token.NAME, 'import'),
321
 
            Leaf(token.NAME, name, prefix=' ')
 
317
            Leaf(token.NAME, u'import'),
 
318
            Leaf(token.NAME, name, prefix=u' ')
322
319
        ])
323
320
    else:
324
 
        import_ = FromImport(package, [Leaf(token.NAME, name, prefix=' ')])
 
321
        import_ = FromImport(package, [Leaf(token.NAME, name, prefix=u' ')])
325
322
 
326
323
    children = [import_, Newline()]
327
 
    if add_newline_before:
328
 
        children.insert(0, Newline())
329
324
    root.insert_child(insert_pos, Node(syms.simple_stmt, children))
330
325
 
331
326
 
409
404
        if package and unicode(node.children[1]).strip() != package:
410
405
            return None
411
406
        n = node.children[3]
412
 
        if package and _find('as', n):
 
407
        if package and _find(u'as', n):
413
408
            # See test_from_import_as for explanation
414
409
            return None
415
410
        elif n.type == syms.import_as_names and _find(name, n):