fix_raise_.py 1.2 KB

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