~pythoneers/ubuntu/lucid/python2.6/ltsppa

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-03-11 13:30:19 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20100311133019-sonignhpjsu6ld0x
Tags: 2.6.5~rc2-0ubuntu1
Python 2.6.5 release candidate 2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
89
89
    parent.insert_child(i, new_stmt)
90
90
    new_leaf1 = new_stmt.children[0].children[0]
91
91
    old_leaf1 = stmt_node.children[0].children[0]
92
 
    new_leaf1.set_prefix(old_leaf1.get_prefix())
 
92
    new_leaf1.prefix = old_leaf1.prefix
93
93
 
94
94
 
95
95
def remove_trailing_newline(node):
113
113
                # Check if the expr_node is a simple assignment.
114
114
                left_node = expr_node.children[0]
115
115
                if isinstance(left_node, Leaf) and \
116
 
                        left_node.value == '__metaclass__':
 
116
                        left_node.value == u'__metaclass__':
117
117
                    # We found a assignment to __metaclass__.
118
118
                    fixup_simple_stmt(node, i, simple_node)
119
119
                    remove_trailing_newline(simple_node)
136
136
        node = kids.pop()
137
137
        if isinstance(node, Leaf) and node.type != token.DEDENT:
138
138
            if node.prefix:
139
 
                node.set_prefix('')
 
139
                node.prefix = u''
140
140
            return
141
141
        else:
142
142
            kids.extend(node.children[::-1])
150
150
 
151
151
    def transform(self, node, results):
152
152
        if not has_metaclass(node):
153
 
            return node
 
153
            return
154
154
 
155
155
        fixup_parse_tree(node)
156
156
 
182
182
            # Node(classdef, ['class', 'name', ':', suite])
183
183
            #                 0        1       2    3
184
184
            arglist = Node(syms.arglist, [])
185
 
            node.insert_child(2, Leaf(token.RPAR, ')'))
 
185
            node.insert_child(2, Leaf(token.RPAR, u')'))
186
186
            node.insert_child(2, arglist)
187
 
            node.insert_child(2, Leaf(token.LPAR, '('))
 
187
            node.insert_child(2, Leaf(token.LPAR, u'('))
188
188
        else:
189
189
            raise ValueError("Unexpected class definition")
190
190
 
191
191
        # now stick the metaclass in the arglist
192
192
        meta_txt = last_metaclass.children[0].children[0]
193
193
        meta_txt.value = 'metaclass'
194
 
        orig_meta_prefix = meta_txt.get_prefix()
 
194
        orig_meta_prefix = meta_txt.prefix
195
195
 
196
196
        if arglist.children:
197
 
            arglist.append_child(Leaf(token.COMMA, ','))
198
 
            meta_txt.set_prefix(' ')
 
197
            arglist.append_child(Leaf(token.COMMA, u','))
 
198
            meta_txt.prefix = u' '
199
199
        else:
200
 
            meta_txt.set_prefix('')
 
200
            meta_txt.prefix = u''
201
201
 
202
202
        # compact the expression "metaclass = Meta" -> "metaclass=Meta"
203
203
        expr_stmt = last_metaclass.children[0]
204
204
        assert expr_stmt.type == syms.expr_stmt
205
 
        expr_stmt.children[1].set_prefix('')
206
 
        expr_stmt.children[2].set_prefix('')
 
205
        expr_stmt.children[1].prefix = u''
 
206
        expr_stmt.children[2].prefix = u''
207
207
 
208
208
        arglist.append_child(last_metaclass)
209
209
 
213
213
        if not suite.children:
214
214
            # one-liner that was just __metaclass_
215
215
            suite.remove()
216
 
            pass_leaf = Leaf(text_type, 'pass')
217
 
            pass_leaf.set_prefix(orig_meta_prefix)
 
216
            pass_leaf = Leaf(text_type, u'pass')
 
217
            pass_leaf.prefix = orig_meta_prefix
218
218
            node.append_child(pass_leaf)
219
 
            node.append_child(Leaf(token.NEWLINE, '\n'))
 
219
            node.append_child(Leaf(token.NEWLINE, u'\n'))
220
220
 
221
221
        elif len(suite.children) > 1 and \
222
222
                 (suite.children[-2].type == token.INDENT and
223
223
                  suite.children[-1].type == token.DEDENT):
224
224
            # there was only one line in the class body and it was __metaclass__
225
 
            pass_leaf = Leaf(text_type, 'pass')
 
225
            pass_leaf = Leaf(text_type, u'pass')
226
226
            suite.insert_child(-1, pass_leaf)
227
 
            suite.insert_child(-1, Leaf(token.NEWLINE, '\n'))
 
227
            suite.insert_child(-1, Leaf(token.NEWLINE, u'\n'))