]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.2/Lib/lib2to3/fixes/fix_raise.py
67cc4c052ce3ea245903c6648eb9f67e2a787ee6
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)
7 raise E, None, T -> raise E.with_traceback(T)
9 raise (((E, E'), E''), E'''), V -> raise E(V)
10 raise "foo", V, T -> warns about string exceptions
14 1) "raise E, V" will be incorrectly translated if V is an exception
15 instance. The correct Python 3 idiom is
19 but since we can't detect instance-hood by syntax alone and since
20 any client code would have to be changed as well, we don't automate
23 # Author: Collin Winter
27 from ..pgen2
import token
28 from .. import fixer_base
29 from ..fixer_util
import Name
, Call
, Attr
, ArgList
, is_tuple
31 class FixRaise(fixer_base
.BaseFix
):
35 raise_stmt< 'raise' exc=any [',' val=any [',' tb=any]] >
38 def transform(self
, node
, results
):
41 exc
= results
["exc"].clone()
42 if exc
.type == token
.STRING
:
43 msg
= "Python 3 does not support string exceptions"
44 self
.cannot_convert(node
, msg
)
48 # raise ((((E1, E2), E3), E4), E5), V
51 # Since Python 3 will not support this, we recurse down any tuple
52 # literals, always taking the first element.
55 # exc.children[1:-1] is the unparenthesized tuple
56 # exc.children[1].children[0] is the first element of the tuple
57 exc
= exc
.children
[1].children
[0].clone()
60 if "val" not in results
:
62 new
= pytree
.Node(syms
.raise_stmt
, [Name(u
"raise"), exc
])
63 new
.prefix
= node
.prefix
66 val
= results
["val"].clone()
68 args
= [c
.clone() for c
in val
.children
[1:-1]]
74 tb
= results
["tb"].clone()
78 # If there's a traceback and None is passed as the value, then don't
79 # add a call, since the user probably just wants to add a
80 # traceback. See issue #9661.
81 if val
.type != token
.NAME
or val
.value
!= u
"None":
83 with_tb
= Attr(e
, Name(u
'with_traceback')) + [ArgList([tb
])]
84 new
= pytree
.Node(syms
.simple_stmt
, [Name(u
"raise")] + with_tb
)
85 new
.prefix
= node
.prefix
88 return pytree
.Node(syms
.raise_stmt
,
89 [Name(u
"raise"), Call(exc
, args
)],