1
"""Fixer for generator.throw(E, V, T).
3
g.throw(E) -> g.throw(E)
4
g.throw(E, V) -> g.throw(E(V))
5
g.throw(E, V, T) -> g.throw(E(V).with_traceback(T))
7
g.throw("foo"[, V[, T]]) will warn about string exceptions."""
8
# Author: Collin Winter
12
from ..pgen2 import token
13
from .. import fixer_base
14
from ..fixer_util import Name, Call, ArgList, Attr, is_tuple
16
class FixThrow(fixer_base.BaseFix):
19
power< any trailer< '.' 'throw' >
20
trailer< '(' args=arglist< exc=any ',' val=any [',' tb=any] > ')' >
23
power< any trailer< '.' 'throw' > trailer< '(' exc=any ')' > >
26
def transform(self, node, results):
29
exc = results["exc"].clone()
30
if exc.type is token.STRING:
31
self.cannot_convert(node, "Python 3 does not support string exceptions")
34
# Leave "g.throw(E)" alone
35
val = results.get("val")
41
args = [c.clone() for c in val.children[1:-1]]
46
throw_args = results["args"]
49
tb = results["tb"].clone()
53
with_tb = Attr(e, Name('with_traceback')) + [ArgList([tb])]
54
throw_args.replace(pytree.Node(syms.power, with_tb))
56
throw_args.replace(Call(exc, args))