1 | n/a | """Fixer for __metaclass__ = X -> (metaclass=X) methods. |
---|
2 | n/a | |
---|
3 | n/a | The various forms of classef (inherits nothing, inherits once, inherints |
---|
4 | n/a | many) don't parse the same in the CST so we look at ALL classes for |
---|
5 | n/a | a __metaclass__ and if we find one normalize the inherits to all be |
---|
6 | n/a | an arglist. |
---|
7 | n/a | |
---|
8 | n/a | For one-liner classes ('class X: pass') there is no indent/dedent so |
---|
9 | n/a | we normalize those into having a suite. |
---|
10 | n/a | |
---|
11 | n/a | Moving the __metaclass__ into the classdef can also cause the class |
---|
12 | n/a | body to be empty so there is some special casing for that as well. |
---|
13 | n/a | |
---|
14 | n/a | This fixer also tries very hard to keep original indenting and spacing |
---|
15 | n/a | in all those corner cases. |
---|
16 | n/a | |
---|
17 | n/a | """ |
---|
18 | n/a | # Author: Jack Diederich |
---|
19 | n/a | |
---|
20 | n/a | # Local imports |
---|
21 | n/a | from .. import fixer_base |
---|
22 | n/a | from ..pygram import token |
---|
23 | n/a | from ..fixer_util import syms, Node, Leaf |
---|
24 | n/a | |
---|
25 | n/a | |
---|
26 | n/a | def has_metaclass(parent): |
---|
27 | n/a | """ we have to check the cls_node without changing it. |
---|
28 | n/a | There are two possibilities: |
---|
29 | n/a | 1) clsdef => suite => simple_stmt => expr_stmt => Leaf('__meta') |
---|
30 | n/a | 2) clsdef => simple_stmt => expr_stmt => Leaf('__meta') |
---|
31 | n/a | """ |
---|
32 | n/a | for node in parent.children: |
---|
33 | n/a | if node.type == syms.suite: |
---|
34 | n/a | return has_metaclass(node) |
---|
35 | n/a | elif node.type == syms.simple_stmt and node.children: |
---|
36 | n/a | expr_node = node.children[0] |
---|
37 | n/a | if expr_node.type == syms.expr_stmt and expr_node.children: |
---|
38 | n/a | left_side = expr_node.children[0] |
---|
39 | n/a | if isinstance(left_side, Leaf) and \ |
---|
40 | n/a | left_side.value == '__metaclass__': |
---|
41 | n/a | return True |
---|
42 | n/a | return False |
---|
43 | n/a | |
---|
44 | n/a | |
---|
45 | n/a | def fixup_parse_tree(cls_node): |
---|
46 | n/a | """ one-line classes don't get a suite in the parse tree so we add |
---|
47 | n/a | one to normalize the tree |
---|
48 | n/a | """ |
---|
49 | n/a | for node in cls_node.children: |
---|
50 | n/a | if node.type == syms.suite: |
---|
51 | n/a | # already in the preferred format, do nothing |
---|
52 | n/a | return |
---|
53 | n/a | |
---|
54 | n/a | # !%@#! oneliners have no suite node, we have to fake one up |
---|
55 | n/a | for i, node in enumerate(cls_node.children): |
---|
56 | n/a | if node.type == token.COLON: |
---|
57 | n/a | break |
---|
58 | n/a | else: |
---|
59 | n/a | raise ValueError("No class suite and no ':'!") |
---|
60 | n/a | |
---|
61 | n/a | # move everything into a suite node |
---|
62 | n/a | suite = Node(syms.suite, []) |
---|
63 | n/a | while cls_node.children[i+1:]: |
---|
64 | n/a | move_node = cls_node.children[i+1] |
---|
65 | n/a | suite.append_child(move_node.clone()) |
---|
66 | n/a | move_node.remove() |
---|
67 | n/a | cls_node.append_child(suite) |
---|
68 | n/a | node = suite |
---|
69 | n/a | |
---|
70 | n/a | |
---|
71 | n/a | def fixup_simple_stmt(parent, i, stmt_node): |
---|
72 | n/a | """ if there is a semi-colon all the parts count as part of the same |
---|
73 | n/a | simple_stmt. We just want the __metaclass__ part so we move |
---|
74 | n/a | everything after the semi-colon into its own simple_stmt node |
---|
75 | n/a | """ |
---|
76 | n/a | for semi_ind, node in enumerate(stmt_node.children): |
---|
77 | n/a | if node.type == token.SEMI: # *sigh* |
---|
78 | n/a | break |
---|
79 | n/a | else: |
---|
80 | n/a | return |
---|
81 | n/a | |
---|
82 | n/a | node.remove() # kill the semicolon |
---|
83 | n/a | new_expr = Node(syms.expr_stmt, []) |
---|
84 | n/a | new_stmt = Node(syms.simple_stmt, [new_expr]) |
---|
85 | n/a | while stmt_node.children[semi_ind:]: |
---|
86 | n/a | move_node = stmt_node.children[semi_ind] |
---|
87 | n/a | new_expr.append_child(move_node.clone()) |
---|
88 | n/a | move_node.remove() |
---|
89 | n/a | parent.insert_child(i, new_stmt) |
---|
90 | n/a | new_leaf1 = new_stmt.children[0].children[0] |
---|
91 | n/a | old_leaf1 = stmt_node.children[0].children[0] |
---|
92 | n/a | new_leaf1.prefix = old_leaf1.prefix |
---|
93 | n/a | |
---|
94 | n/a | |
---|
95 | n/a | def remove_trailing_newline(node): |
---|
96 | n/a | if node.children and node.children[-1].type == token.NEWLINE: |
---|
97 | n/a | node.children[-1].remove() |
---|
98 | n/a | |
---|
99 | n/a | |
---|
100 | n/a | def find_metas(cls_node): |
---|
101 | n/a | # find the suite node (Mmm, sweet nodes) |
---|
102 | n/a | for node in cls_node.children: |
---|
103 | n/a | if node.type == syms.suite: |
---|
104 | n/a | break |
---|
105 | n/a | else: |
---|
106 | n/a | raise ValueError("No class suite!") |
---|
107 | n/a | |
---|
108 | n/a | # look for simple_stmt[ expr_stmt[ Leaf('__metaclass__') ] ] |
---|
109 | n/a | for i, simple_node in list(enumerate(node.children)): |
---|
110 | n/a | if simple_node.type == syms.simple_stmt and simple_node.children: |
---|
111 | n/a | expr_node = simple_node.children[0] |
---|
112 | n/a | if expr_node.type == syms.expr_stmt and expr_node.children: |
---|
113 | n/a | # Check if the expr_node is a simple assignment. |
---|
114 | n/a | left_node = expr_node.children[0] |
---|
115 | n/a | if isinstance(left_node, Leaf) and \ |
---|
116 | n/a | left_node.value == '__metaclass__': |
---|
117 | n/a | # We found an assignment to __metaclass__. |
---|
118 | n/a | fixup_simple_stmt(node, i, simple_node) |
---|
119 | n/a | remove_trailing_newline(simple_node) |
---|
120 | n/a | yield (node, i, simple_node) |
---|
121 | n/a | |
---|
122 | n/a | |
---|
123 | n/a | def fixup_indent(suite): |
---|
124 | n/a | """ If an INDENT is followed by a thing with a prefix then nuke the prefix |
---|
125 | n/a | Otherwise we get in trouble when removing __metaclass__ at suite start |
---|
126 | n/a | """ |
---|
127 | n/a | kids = suite.children[::-1] |
---|
128 | n/a | # find the first indent |
---|
129 | n/a | while kids: |
---|
130 | n/a | node = kids.pop() |
---|
131 | n/a | if node.type == token.INDENT: |
---|
132 | n/a | break |
---|
133 | n/a | |
---|
134 | n/a | # find the first Leaf |
---|
135 | n/a | while kids: |
---|
136 | n/a | node = kids.pop() |
---|
137 | n/a | if isinstance(node, Leaf) and node.type != token.DEDENT: |
---|
138 | n/a | if node.prefix: |
---|
139 | n/a | node.prefix = '' |
---|
140 | n/a | return |
---|
141 | n/a | else: |
---|
142 | n/a | kids.extend(node.children[::-1]) |
---|
143 | n/a | |
---|
144 | n/a | |
---|
145 | n/a | class FixMetaclass(fixer_base.BaseFix): |
---|
146 | n/a | BM_compatible = True |
---|
147 | n/a | |
---|
148 | n/a | PATTERN = """ |
---|
149 | n/a | classdef<any*> |
---|
150 | n/a | """ |
---|
151 | n/a | |
---|
152 | n/a | def transform(self, node, results): |
---|
153 | n/a | if not has_metaclass(node): |
---|
154 | n/a | return |
---|
155 | n/a | |
---|
156 | n/a | fixup_parse_tree(node) |
---|
157 | n/a | |
---|
158 | n/a | # find metaclasses, keep the last one |
---|
159 | n/a | last_metaclass = None |
---|
160 | n/a | for suite, i, stmt in find_metas(node): |
---|
161 | n/a | last_metaclass = stmt |
---|
162 | n/a | stmt.remove() |
---|
163 | n/a | |
---|
164 | n/a | text_type = node.children[0].type # always Leaf(nnn, 'class') |
---|
165 | n/a | |
---|
166 | n/a | # figure out what kind of classdef we have |
---|
167 | n/a | if len(node.children) == 7: |
---|
168 | n/a | # Node(classdef, ['class', 'name', '(', arglist, ')', ':', suite]) |
---|
169 | n/a | # 0 1 2 3 4 5 6 |
---|
170 | n/a | if node.children[3].type == syms.arglist: |
---|
171 | n/a | arglist = node.children[3] |
---|
172 | n/a | # Node(classdef, ['class', 'name', '(', 'Parent', ')', ':', suite]) |
---|
173 | n/a | else: |
---|
174 | n/a | parent = node.children[3].clone() |
---|
175 | n/a | arglist = Node(syms.arglist, [parent]) |
---|
176 | n/a | node.set_child(3, arglist) |
---|
177 | n/a | elif len(node.children) == 6: |
---|
178 | n/a | # Node(classdef, ['class', 'name', '(', ')', ':', suite]) |
---|
179 | n/a | # 0 1 2 3 4 5 |
---|
180 | n/a | arglist = Node(syms.arglist, []) |
---|
181 | n/a | node.insert_child(3, arglist) |
---|
182 | n/a | elif len(node.children) == 4: |
---|
183 | n/a | # Node(classdef, ['class', 'name', ':', suite]) |
---|
184 | n/a | # 0 1 2 3 |
---|
185 | n/a | arglist = Node(syms.arglist, []) |
---|
186 | n/a | node.insert_child(2, Leaf(token.RPAR, ')')) |
---|
187 | n/a | node.insert_child(2, arglist) |
---|
188 | n/a | node.insert_child(2, Leaf(token.LPAR, '(')) |
---|
189 | n/a | else: |
---|
190 | n/a | raise ValueError("Unexpected class definition") |
---|
191 | n/a | |
---|
192 | n/a | # now stick the metaclass in the arglist |
---|
193 | n/a | meta_txt = last_metaclass.children[0].children[0] |
---|
194 | n/a | meta_txt.value = 'metaclass' |
---|
195 | n/a | orig_meta_prefix = meta_txt.prefix |
---|
196 | n/a | |
---|
197 | n/a | if arglist.children: |
---|
198 | n/a | arglist.append_child(Leaf(token.COMMA, ',')) |
---|
199 | n/a | meta_txt.prefix = ' ' |
---|
200 | n/a | else: |
---|
201 | n/a | meta_txt.prefix = '' |
---|
202 | n/a | |
---|
203 | n/a | # compact the expression "metaclass = Meta" -> "metaclass=Meta" |
---|
204 | n/a | expr_stmt = last_metaclass.children[0] |
---|
205 | n/a | assert expr_stmt.type == syms.expr_stmt |
---|
206 | n/a | expr_stmt.children[1].prefix = '' |
---|
207 | n/a | expr_stmt.children[2].prefix = '' |
---|
208 | n/a | |
---|
209 | n/a | arglist.append_child(last_metaclass) |
---|
210 | n/a | |
---|
211 | n/a | fixup_indent(suite) |
---|
212 | n/a | |
---|
213 | n/a | # check for empty suite |
---|
214 | n/a | if not suite.children: |
---|
215 | n/a | # one-liner that was just __metaclass_ |
---|
216 | n/a | suite.remove() |
---|
217 | n/a | pass_leaf = Leaf(text_type, 'pass') |
---|
218 | n/a | pass_leaf.prefix = orig_meta_prefix |
---|
219 | n/a | node.append_child(pass_leaf) |
---|
220 | n/a | node.append_child(Leaf(token.NEWLINE, '\n')) |
---|
221 | n/a | |
---|
222 | n/a | elif len(suite.children) > 1 and \ |
---|
223 | n/a | (suite.children[-2].type == token.INDENT and |
---|
224 | n/a | suite.children[-1].type == token.DEDENT): |
---|
225 | n/a | # there was only one line in the class body and it was __metaclass__ |
---|
226 | n/a | pass_leaf = Leaf(text_type, 'pass') |
---|
227 | n/a | suite.insert_child(-1, pass_leaf) |
---|
228 | n/a | suite.insert_child(-1, Leaf(token.NEWLINE, '\n')) |
---|