+++ /dev/null
-"""Fixer for 'raise E, V, T'\r
-\r
-raise -> raise\r
-raise E -> raise E\r
-raise E, V -> raise E(V)\r
-raise E, V, T -> raise E(V).with_traceback(T)\r
-raise E, None, T -> raise E.with_traceback(T)\r
-\r
-raise (((E, E'), E''), E'''), V -> raise E(V)\r
-raise "foo", V, T -> warns about string exceptions\r
-\r
-\r
-CAVEATS:\r
-1) "raise E, V" will be incorrectly translated if V is an exception\r
- instance. The correct Python 3 idiom is\r
-\r
- raise E from V\r
-\r
- but since we can't detect instance-hood by syntax alone and since\r
- any client code would have to be changed as well, we don't automate\r
- this.\r
-"""\r
-# Author: Collin Winter\r
-\r
-# Local imports\r
-from .. import pytree\r
-from ..pgen2 import token\r
-from .. import fixer_base\r
-from ..fixer_util import Name, Call, Attr, ArgList, is_tuple\r
-\r
-class FixRaise(fixer_base.BaseFix):\r
-\r
- BM_compatible = True\r
- PATTERN = """\r
- raise_stmt< 'raise' exc=any [',' val=any [',' tb=any]] >\r
- """\r
-\r
- def transform(self, node, results):\r
- syms = self.syms\r
-\r
- exc = results["exc"].clone()\r
- if exc.type == token.STRING:\r
- msg = "Python 3 does not support string exceptions"\r
- self.cannot_convert(node, msg)\r
- return\r
-\r
- # Python 2 supports\r
- # raise ((((E1, E2), E3), E4), E5), V\r
- # as a synonym for\r
- # raise E1, V\r
- # Since Python 3 will not support this, we recurse down any tuple\r
- # literals, always taking the first element.\r
- if is_tuple(exc):\r
- while is_tuple(exc):\r
- # exc.children[1:-1] is the unparenthesized tuple\r
- # exc.children[1].children[0] is the first element of the tuple\r
- exc = exc.children[1].children[0].clone()\r
- exc.prefix = u" "\r
-\r
- if "val" not in results:\r
- # One-argument raise\r
- new = pytree.Node(syms.raise_stmt, [Name(u"raise"), exc])\r
- new.prefix = node.prefix\r
- return new\r
-\r
- val = results["val"].clone()\r
- if is_tuple(val):\r
- args = [c.clone() for c in val.children[1:-1]]\r
- else:\r
- val.prefix = u""\r
- args = [val]\r
-\r
- if "tb" in results:\r
- tb = results["tb"].clone()\r
- tb.prefix = u""\r
-\r
- e = exc\r
- # If there's a traceback and None is passed as the value, then don't\r
- # add a call, since the user probably just wants to add a\r
- # traceback. See issue #9661.\r
- if val.type != token.NAME or val.value != u"None":\r
- e = Call(exc, args)\r
- with_tb = Attr(e, Name(u'with_traceback')) + [ArgList([tb])]\r
- new = pytree.Node(syms.simple_stmt, [Name(u"raise")] + with_tb)\r
- new.prefix = node.prefix\r
- return new\r
- else:\r
- return pytree.Node(syms.raise_stmt,\r
- [Name(u"raise"), Call(exc, args)],\r
- prefix=node.prefix)\r