fix_throw.py 835 B

1234567891011121314151617181920212223
  1. u"""Fixer for 'g.throw(E(V).with_traceback(T))' -> 'g.throw(E, V, T)'"""
  2. from lib2to3 import fixer_base
  3. from lib2to3.pytree import Node, Leaf
  4. from lib2to3.pgen2 import token
  5. from lib2to3.fixer_util import Comma
  6. class FixThrow(fixer_base.BaseFix):
  7. PATTERN = u"""
  8. power< any trailer< '.' 'throw' >
  9. trailer< '(' args=power< exc=any trailer< '(' val=any* ')' >
  10. trailer< '.' 'with_traceback' > trailer< '(' trc=any ')' > > ')' > >
  11. """
  12. def transform(self, node, results):
  13. syms = self.syms
  14. exc, val, trc = (results[u"exc"], results[u"val"], results[u"trc"])
  15. val = val[0] if val else Leaf(token.NAME, u"None")
  16. val.prefix = trc.prefix = u" "
  17. kids = [exc.clone(), Comma(), val.clone(), Comma(), trc.clone()]
  18. args = results[u"args"]
  19. args.children = kids