| 1 | n/a | """Fixer for 'raise E, V, T' |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | raise -> raise |
|---|
| 4 | n/a | raise E -> raise E |
|---|
| 5 | n/a | raise E, V -> raise E(V) |
|---|
| 6 | n/a | raise E, V, T -> raise E(V).with_traceback(T) |
|---|
| 7 | n/a | raise E, None, T -> raise E.with_traceback(T) |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | raise (((E, E'), E''), E'''), V -> raise E(V) |
|---|
| 10 | n/a | raise "foo", V, T -> warns about string exceptions |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | CAVEATS: |
|---|
| 14 | n/a | 1) "raise E, V" will be incorrectly translated if V is an exception |
|---|
| 15 | n/a | instance. The correct Python 3 idiom is |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | raise E from V |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | but since we can't detect instance-hood by syntax alone and since |
|---|
| 20 | n/a | any client code would have to be changed as well, we don't automate |
|---|
| 21 | n/a | this. |
|---|
| 22 | n/a | """ |
|---|
| 23 | n/a | # Author: Collin Winter |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | # Local imports |
|---|
| 26 | n/a | from .. import pytree |
|---|
| 27 | n/a | from ..pgen2 import token |
|---|
| 28 | n/a | from .. import fixer_base |
|---|
| 29 | n/a | from ..fixer_util import Name, Call, Attr, ArgList, is_tuple |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | class FixRaise(fixer_base.BaseFix): |
|---|
| 32 | n/a | |
|---|
| 33 | n/a | BM_compatible = True |
|---|
| 34 | n/a | PATTERN = """ |
|---|
| 35 | n/a | raise_stmt< 'raise' exc=any [',' val=any [',' tb=any]] > |
|---|
| 36 | n/a | """ |
|---|
| 37 | n/a | |
|---|
| 38 | n/a | def transform(self, node, results): |
|---|
| 39 | n/a | syms = self.syms |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | exc = results["exc"].clone() |
|---|
| 42 | n/a | if exc.type == token.STRING: |
|---|
| 43 | n/a | msg = "Python 3 does not support string exceptions" |
|---|
| 44 | n/a | self.cannot_convert(node, msg) |
|---|
| 45 | n/a | return |
|---|
| 46 | n/a | |
|---|
| 47 | n/a | # Python 2 supports |
|---|
| 48 | n/a | # raise ((((E1, E2), E3), E4), E5), V |
|---|
| 49 | n/a | # as a synonym for |
|---|
| 50 | n/a | # raise E1, V |
|---|
| 51 | n/a | # Since Python 3 will not support this, we recurse down any tuple |
|---|
| 52 | n/a | # literals, always taking the first element. |
|---|
| 53 | n/a | if is_tuple(exc): |
|---|
| 54 | n/a | while is_tuple(exc): |
|---|
| 55 | n/a | # exc.children[1:-1] is the unparenthesized tuple |
|---|
| 56 | n/a | # exc.children[1].children[0] is the first element of the tuple |
|---|
| 57 | n/a | exc = exc.children[1].children[0].clone() |
|---|
| 58 | n/a | exc.prefix = " " |
|---|
| 59 | n/a | |
|---|
| 60 | n/a | if "val" not in results: |
|---|
| 61 | n/a | # One-argument raise |
|---|
| 62 | n/a | new = pytree.Node(syms.raise_stmt, [Name("raise"), exc]) |
|---|
| 63 | n/a | new.prefix = node.prefix |
|---|
| 64 | n/a | return new |
|---|
| 65 | n/a | |
|---|
| 66 | n/a | val = results["val"].clone() |
|---|
| 67 | n/a | if is_tuple(val): |
|---|
| 68 | n/a | args = [c.clone() for c in val.children[1:-1]] |
|---|
| 69 | n/a | else: |
|---|
| 70 | n/a | val.prefix = "" |
|---|
| 71 | n/a | args = [val] |
|---|
| 72 | n/a | |
|---|
| 73 | n/a | if "tb" in results: |
|---|
| 74 | n/a | tb = results["tb"].clone() |
|---|
| 75 | n/a | tb.prefix = "" |
|---|
| 76 | n/a | |
|---|
| 77 | n/a | e = exc |
|---|
| 78 | n/a | # If there's a traceback and None is passed as the value, then don't |
|---|
| 79 | n/a | # add a call, since the user probably just wants to add a |
|---|
| 80 | n/a | # traceback. See issue #9661. |
|---|
| 81 | n/a | if val.type != token.NAME or val.value != "None": |
|---|
| 82 | n/a | e = Call(exc, args) |
|---|
| 83 | n/a | with_tb = Attr(e, Name('with_traceback')) + [ArgList([tb])] |
|---|
| 84 | n/a | new = pytree.Node(syms.simple_stmt, [Name("raise")] + with_tb) |
|---|
| 85 | n/a | new.prefix = node.prefix |
|---|
| 86 | n/a | return new |
|---|
| 87 | n/a | else: |
|---|
| 88 | n/a | return pytree.Node(syms.raise_stmt, |
|---|
| 89 | n/a | [Name("raise"), Call(exc, args)], |
|---|
| 90 | n/a | prefix=node.prefix) |
|---|