"""Fixer for generator.throw(E, V, T).
g.throw(E, V) -> g.throw(E(V))
g.throw(E, V, T) -> g.throw(E(V).with_traceback(T))
g.throw("foo"[, V[, T]]) will warn about string exceptions."""
from ..pgen2 import token
from .. import fixer_base
from ..fixer_util import Name, Call, ArgList, Attr, is_tuple
class FixThrow(fixer_base.BaseFix):
power< any trailer< '.' 'throw' >
trailer< '(' args=arglist< exc=any ',' val=any [',' tb=any] > ')' >
power< any trailer< '.' 'throw' > trailer< '(' exc=any ')' > >
def transform(self, node, results):
exc = results["exc"].clone()
if exc.type is token.STRING:
self.cannot_convert(node, "Python 3 does not support string exceptions")
# Leave "g.throw(E)" alone
args = [c.clone() for c in val.children[1:-1]]
throw_args = results["args"]
tb = results["tb"].clone()
with_tb = Attr(e, Name('with_traceback')) + [ArgList([tb])]
throw_args.replace(pytree.Node(syms.power, with_tb))
throw_args.replace(Call(exc, args))