1 | n/a | """Fixer for except statements with named exceptions. |
---|
2 | n/a | |
---|
3 | n/a | The following cases will be converted: |
---|
4 | n/a | |
---|
5 | n/a | - "except E, T:" where T is a name: |
---|
6 | n/a | |
---|
7 | n/a | except E as T: |
---|
8 | n/a | |
---|
9 | n/a | - "except E, T:" where T is not a name, tuple or list: |
---|
10 | n/a | |
---|
11 | n/a | except E as t: |
---|
12 | n/a | T = t |
---|
13 | n/a | |
---|
14 | n/a | This is done because the target of an "except" clause must be a |
---|
15 | n/a | name. |
---|
16 | n/a | |
---|
17 | n/a | - "except E, T:" where T is a tuple or list literal: |
---|
18 | n/a | |
---|
19 | n/a | except E as t: |
---|
20 | n/a | T = t.args |
---|
21 | n/a | """ |
---|
22 | n/a | # Author: Collin Winter |
---|
23 | n/a | |
---|
24 | n/a | # Local imports |
---|
25 | n/a | from .. import pytree |
---|
26 | n/a | from ..pgen2 import token |
---|
27 | n/a | from .. import fixer_base |
---|
28 | n/a | from ..fixer_util import Assign, Attr, Name, is_tuple, is_list, syms |
---|
29 | n/a | |
---|
30 | n/a | def find_excepts(nodes): |
---|
31 | n/a | for i, n in enumerate(nodes): |
---|
32 | n/a | if n.type == syms.except_clause: |
---|
33 | n/a | if n.children[0].value == 'except': |
---|
34 | n/a | yield (n, nodes[i+2]) |
---|
35 | n/a | |
---|
36 | n/a | class FixExcept(fixer_base.BaseFix): |
---|
37 | n/a | BM_compatible = True |
---|
38 | n/a | |
---|
39 | n/a | PATTERN = """ |
---|
40 | n/a | try_stmt< 'try' ':' (simple_stmt | suite) |
---|
41 | n/a | cleanup=(except_clause ':' (simple_stmt | suite))+ |
---|
42 | n/a | tail=(['except' ':' (simple_stmt | suite)] |
---|
43 | n/a | ['else' ':' (simple_stmt | suite)] |
---|
44 | n/a | ['finally' ':' (simple_stmt | suite)]) > |
---|
45 | n/a | """ |
---|
46 | n/a | |
---|
47 | n/a | def transform(self, node, results): |
---|
48 | n/a | syms = self.syms |
---|
49 | n/a | |
---|
50 | n/a | tail = [n.clone() for n in results["tail"]] |
---|
51 | n/a | |
---|
52 | n/a | try_cleanup = [ch.clone() for ch in results["cleanup"]] |
---|
53 | n/a | for except_clause, e_suite in find_excepts(try_cleanup): |
---|
54 | n/a | if len(except_clause.children) == 4: |
---|
55 | n/a | (E, comma, N) = except_clause.children[1:4] |
---|
56 | n/a | comma.replace(Name("as", prefix=" ")) |
---|
57 | n/a | |
---|
58 | n/a | if N.type != token.NAME: |
---|
59 | n/a | # Generate a new N for the except clause |
---|
60 | n/a | new_N = Name(self.new_name(), prefix=" ") |
---|
61 | n/a | target = N.clone() |
---|
62 | n/a | target.prefix = "" |
---|
63 | n/a | N.replace(new_N) |
---|
64 | n/a | new_N = new_N.clone() |
---|
65 | n/a | |
---|
66 | n/a | # Insert "old_N = new_N" as the first statement in |
---|
67 | n/a | # the except body. This loop skips leading whitespace |
---|
68 | n/a | # and indents |
---|
69 | n/a | #TODO(cwinter) suite-cleanup |
---|
70 | n/a | suite_stmts = e_suite.children |
---|
71 | n/a | for i, stmt in enumerate(suite_stmts): |
---|
72 | n/a | if isinstance(stmt, pytree.Node): |
---|
73 | n/a | break |
---|
74 | n/a | |
---|
75 | n/a | # The assignment is different if old_N is a tuple or list |
---|
76 | n/a | # In that case, the assignment is old_N = new_N.args |
---|
77 | n/a | if is_tuple(N) or is_list(N): |
---|
78 | n/a | assign = Assign(target, Attr(new_N, Name('args'))) |
---|
79 | n/a | else: |
---|
80 | n/a | assign = Assign(target, new_N) |
---|
81 | n/a | |
---|
82 | n/a | #TODO(cwinter) stopgap until children becomes a smart list |
---|
83 | n/a | for child in reversed(suite_stmts[:i]): |
---|
84 | n/a | e_suite.insert_child(0, child) |
---|
85 | n/a | e_suite.insert_child(i, assign) |
---|
86 | n/a | elif N.prefix == "": |
---|
87 | n/a | # No space after a comma is legal; no space after "as", |
---|
88 | n/a | # not so much. |
---|
89 | n/a | N.prefix = " " |
---|
90 | n/a | |
---|
91 | n/a | #TODO(cwinter) fix this when children becomes a smart list |
---|
92 | n/a | children = [c.clone() for c in node.children[:3]] + try_cleanup + tail |
---|
93 | n/a | return pytree.Node(node.type, children) |
---|