ยปCore Development>Code coverage>Lib/lib2to3/fixes/fix_except.py

Python code coverage for Lib/lib2to3/fixes/fix_except.py

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