fix_metaclass.py 8.0 KB

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