blob: 05aa21e74a30ff101c593dc03514efc26f10cecf [file] [log] [blame]
Martin v. Löwisef04c442008-03-19 05:04:44 +00001"""Fixer for 'raise E, V, T'
2
3raise -> raise
4raise E -> raise E
5raise E, V -> raise E(V)
6raise E, V, T -> raise E(V).with_traceback(T)
Benjamin Petersonf37eb3a2010-10-14 23:00:04 +00007raise E, None, T -> raise E.with_traceback(T)
Martin v. Löwisef04c442008-03-19 05:04:44 +00008
9raise (((E, E'), E''), E'''), V -> raise E(V)
10raise "foo", V, T -> warns about string exceptions
11
12
13CAVEATS:
141) "raise E, V" will be incorrectly translated if V is an exception
15 instance. The correct Python 3 idiom is
Martin v. Löwisf733c602008-03-19 05:26:18 +000016
Martin v. Löwisef04c442008-03-19 05:04:44 +000017 raise E from V
Martin v. Löwisf733c602008-03-19 05:26:18 +000018
Martin v. Löwisef04c442008-03-19 05:04:44 +000019 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
21 this.
22"""
23# Author: Collin Winter
24
25# Local imports
26from .. import pytree
27from ..pgen2 import token
Benjamin Petersondf6dc8f2008-06-15 02:57:40 +000028from .. import fixer_base
29from ..fixer_util import Name, Call, Attr, ArgList, is_tuple
Martin v. Löwisef04c442008-03-19 05:04:44 +000030
Benjamin Petersondf6dc8f2008-06-15 02:57:40 +000031class FixRaise(fixer_base.BaseFix):
Martin v. Löwisef04c442008-03-19 05:04:44 +000032
Benjamin Petersonf37eb3a2010-10-14 23:00:04 +000033 BM_compatible = True
Martin v. Löwisef04c442008-03-19 05:04:44 +000034 PATTERN = """
35 raise_stmt< 'raise' exc=any [',' val=any [',' tb=any]] >
36 """
37
38 def transform(self, node, results):
39 syms = self.syms
40
41 exc = results["exc"].clone()
Benjamin Petersonf37eb3a2010-10-14 23:00:04 +000042 if exc.type == token.STRING:
43 msg = "Python 3 does not support string exceptions"
44 self.cannot_convert(node, msg)
Martin v. Löwisef04c442008-03-19 05:04:44 +000045 return
46
47 # Python 2 supports
48 # raise ((((E1, E2), E3), E4), E5), V
49 # as a synonym for
50 # raise E1, V
51 # Since Python 3 will not support this, we recurse down any tuple
52 # literals, always taking the first element.
53 if is_tuple(exc):
Martin v. Löwisf733c602008-03-19 05:26:18 +000054 while is_tuple(exc):
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()
Benjamin Peterson2c3ac6b2009-06-11 23:47:38 +000058 exc.prefix = " "
Martin v. Löwisef04c442008-03-19 05:04:44 +000059
60 if "val" not in results:
61 # One-argument raise
62 new = pytree.Node(syms.raise_stmt, [Name("raise"), exc])
Benjamin Peterson2c3ac6b2009-06-11 23:47:38 +000063 new.prefix = node.prefix
Martin v. Löwisef04c442008-03-19 05:04:44 +000064 return new
65
66 val = results["val"].clone()
67 if is_tuple(val):
68 args = [c.clone() for c in val.children[1:-1]]
69 else:
Benjamin Peterson2c3ac6b2009-06-11 23:47:38 +000070 val.prefix = ""
Martin v. Löwisef04c442008-03-19 05:04:44 +000071 args = [val]
72
73 if "tb" in results:
74 tb = results["tb"].clone()
Benjamin Peterson2c3ac6b2009-06-11 23:47:38 +000075 tb.prefix = ""
Martin v. Löwisef04c442008-03-19 05:04:44 +000076
Benjamin Petersonf37eb3a2010-10-14 23:00:04 +000077 e = exc
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 != "None":
82 e = Call(exc, args)
Martin v. Löwisef04c442008-03-19 05:04:44 +000083 with_tb = Attr(e, Name('with_traceback')) + [ArgList([tb])]
84 new = pytree.Node(syms.simple_stmt, [Name("raise")] + with_tb)
Benjamin Peterson2c3ac6b2009-06-11 23:47:38 +000085 new.prefix = node.prefix
Martin v. Löwisef04c442008-03-19 05:04:44 +000086 return new
87 else:
88 return pytree.Node(syms.raise_stmt,
89 [Name("raise"), Call(exc, args)],
Benjamin Peterson2c3ac6b2009-06-11 23:47:38 +000090 prefix=node.prefix)