fix_raise.py 1.1 KB

12345678910111213141516171819202122232425
  1. u"""Fixer for 'raise E(V).with_traceback(T)' -> 'raise E, V, T'"""
  2. from lib2to3 import fixer_base
  3. from lib2to3.fixer_util import Comma, Node, Leaf, token, syms
  4. class FixRaise(fixer_base.BaseFix):
  5. PATTERN = u"""
  6. raise_stmt< 'raise' (power< name=any [trailer< '(' val=any* ')' >]
  7. [trailer< '.' 'with_traceback' > trailer< '(' trc=any ')' >] > | any) ['from' chain=any] >"""
  8. def transform(self, node, results):
  9. name, val, trc = (results.get(u"name"), results.get(u"val"), results.get(u"trc"))
  10. chain = results.get(u"chain")
  11. if chain is not None:
  12. self.warning(node, u"explicit exception chaining is not supported in Python 2")
  13. chain.prev_sibling.remove()
  14. chain.remove()
  15. if trc is not None:
  16. val = val[0] if val else Leaf(token.NAME, u"None")
  17. val.prefix = trc.prefix = u" "
  18. kids = [Leaf(token.NAME, u"raise"), name.clone(), Comma(),
  19. val.clone(), Comma(), trc.clone()]
  20. raise_stmt = Node(syms.raise_stmt, kids)
  21. node.replace(raise_stmt)