1
"""Fixer for 'raise E, V, T'
5
raise E, V -> raise E(V)
6
raise E, V, T -> raise E(V).with_traceback(T)
8
raise (((E, E'), E''), E'''), V -> raise E(V)
9
raise "foo", V, T -> warns about string exceptions
13
1) "raise E, V" will be incorrectly translated if V is an exception
14
instance. The correct Python 3 idiom is
18
but since we can't detect instance-hood by syntax alone and since
19
any client code would have to be changed as well, we don't automate
22
# Author: Collin Winter
26
from ..pgen2 import token
27
from .. import fixer_base
28
from ..fixer_util import Name, Call, Attr, ArgList, is_tuple
30
class FixRaise(fixer_base.BaseFix):
33
raise_stmt< 'raise' exc=any [',' val=any [',' tb=any]] >
36
def transform(self, node, results):
39
exc = results["exc"].clone()
40
if exc.type is token.STRING:
41
self.cannot_convert(node, "Python 3 does not support string exceptions")
45
# raise ((((E1, E2), E3), E4), E5), V
48
# Since Python 3 will not support this, we recurse down any tuple
49
# literals, always taking the first element.
52
# exc.children[1:-1] is the unparenthesized tuple
53
# exc.children[1].children[0] is the first element of the tuple
54
exc = exc.children[1].children[0].clone()
57
if "val" not in results:
59
new = pytree.Node(syms.raise_stmt, [Name("raise"), exc])
60
new.set_prefix(node.get_prefix())
63
val = results["val"].clone()
65
args = [c.clone() for c in val.children[1:-1]]
71
tb = results["tb"].clone()
75
with_tb = Attr(e, Name('with_traceback')) + [ArgList([tb])]
76
new = pytree.Node(syms.simple_stmt, [Name("raise")] + with_tb)
77
new.set_prefix(node.get_prefix())
80
return pytree.Node(syms.raise_stmt,
81
[Name("raise"), Call(exc, args)],
82
prefix=node.get_prefix())