Mercurial > repos > shellac > guppy_basecaller
comparison env/lib/python3.7/site-packages/libfuturize/fixes/fix_raise.py @ 2:6af9afd405e9 draft
"planemo upload commit 0a63dd5f4d38a1f6944587f52a8cd79874177fc1"
author | shellac |
---|---|
date | Thu, 14 May 2020 14:56:58 -0400 |
parents | 26e78fe6e8c4 |
children |
comparison
equal
deleted
inserted
replaced
1:75ca89e9b81c | 2:6af9afd405e9 |
---|---|
1 """Fixer for 'raise E, V' | |
2 | |
3 From Armin Ronacher's ``python-modernize``. | |
4 | |
5 raise -> raise | |
6 raise E -> raise E | |
7 raise E, 5 -> raise E(5) | |
8 raise E, 5, T -> raise E(5).with_traceback(T) | |
9 raise E, None, T -> raise E.with_traceback(T) | |
10 | |
11 raise (((E, E'), E''), E'''), 5 -> raise E(5) | |
12 raise "foo", V, T -> warns about string exceptions | |
13 | |
14 raise E, (V1, V2) -> raise E(V1, V2) | |
15 raise E, (V1, V2), T -> raise E(V1, V2).with_traceback(T) | |
16 | |
17 | |
18 CAVEATS: | |
19 1) "raise E, V, T" cannot be translated safely in general. If V | |
20 is not a tuple or a (number, string, None) literal, then: | |
21 | |
22 raise E, V, T -> from future.utils import raise_ | |
23 raise_(E, V, T) | |
24 """ | |
25 # Author: Collin Winter, Armin Ronacher, Mark Huang | |
26 | |
27 # Local imports | |
28 from lib2to3 import pytree, fixer_base | |
29 from lib2to3.pgen2 import token | |
30 from lib2to3.fixer_util import Name, Call, is_tuple, Comma, Attr, ArgList | |
31 | |
32 from libfuturize.fixer_util import touch_import_top | |
33 | |
34 | |
35 class FixRaise(fixer_base.BaseFix): | |
36 | |
37 BM_compatible = True | |
38 PATTERN = """ | |
39 raise_stmt< 'raise' exc=any [',' val=any [',' tb=any]] > | |
40 """ | |
41 | |
42 def transform(self, node, results): | |
43 syms = self.syms | |
44 | |
45 exc = results["exc"].clone() | |
46 if exc.type == token.STRING: | |
47 msg = "Python 3 does not support string exceptions" | |
48 self.cannot_convert(node, msg) | |
49 return | |
50 | |
51 # Python 2 supports | |
52 # raise ((((E1, E2), E3), E4), E5), V | |
53 # as a synonym for | |
54 # raise E1, V | |
55 # Since Python 3 will not support this, we recurse down any tuple | |
56 # literals, always taking the first element. | |
57 if is_tuple(exc): | |
58 while is_tuple(exc): | |
59 # exc.children[1:-1] is the unparenthesized tuple | |
60 # exc.children[1].children[0] is the first element of the tuple | |
61 exc = exc.children[1].children[0].clone() | |
62 exc.prefix = u" " | |
63 | |
64 if "tb" in results: | |
65 tb = results["tb"].clone() | |
66 else: | |
67 tb = None | |
68 | |
69 if "val" in results: | |
70 val = results["val"].clone() | |
71 if is_tuple(val): | |
72 # Assume that exc is a subclass of Exception and call exc(*val). | |
73 args = [c.clone() for c in val.children[1:-1]] | |
74 exc = Call(exc, args) | |
75 elif val.type in (token.NUMBER, token.STRING): | |
76 # Handle numeric and string literals specially, e.g. | |
77 # "raise Exception, 5" -> "raise Exception(5)". | |
78 val.prefix = u"" | |
79 exc = Call(exc, [val]) | |
80 elif val.type == token.NAME and val.value == u"None": | |
81 # Handle None specially, e.g. | |
82 # "raise Exception, None" -> "raise Exception". | |
83 pass | |
84 else: | |
85 # val is some other expression. If val evaluates to an instance | |
86 # of exc, it should just be raised. If val evaluates to None, | |
87 # a default instance of exc should be raised (as above). If val | |
88 # evaluates to a tuple, exc(*val) should be called (as | |
89 # above). Otherwise, exc(val) should be called. We can only | |
90 # tell what to do at runtime, so defer to future.utils.raise_(), | |
91 # which handles all of these cases. | |
92 touch_import_top(u"future.utils", u"raise_", node) | |
93 exc.prefix = u"" | |
94 args = [exc, Comma(), val] | |
95 if tb is not None: | |
96 args += [Comma(), tb] | |
97 return Call(Name(u"raise_"), args) | |
98 | |
99 if tb is not None: | |
100 tb.prefix = "" | |
101 exc_list = Attr(exc, Name('with_traceback')) + [ArgList([tb])] | |
102 else: | |
103 exc_list = [exc] | |
104 | |
105 return pytree.Node(syms.raise_stmt, | |
106 [Name(u"raise")] + exc_list, | |
107 prefix=node.prefix) |