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

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

#countcontent
1n/a"""Fixer for generator.throw(E, V, T).
2n/a
3n/ag.throw(E) -> g.throw(E)
4n/ag.throw(E, V) -> g.throw(E(V))
5n/ag.throw(E, V, T) -> g.throw(E(V).with_traceback(T))
6n/a
7n/ag.throw("foo"[, V[, T]]) will warn about string exceptions."""
8n/a# Author: Collin Winter
9n/a
10n/a# Local imports
11n/afrom .. import pytree
12n/afrom ..pgen2 import token
13n/afrom .. import fixer_base
14n/afrom ..fixer_util import Name, Call, ArgList, Attr, is_tuple
15n/a
16n/aclass FixThrow(fixer_base.BaseFix):
17n/a BM_compatible = True
18n/a PATTERN = """
19n/a power< any trailer< '.' 'throw' >
20n/a trailer< '(' args=arglist< exc=any ',' val=any [',' tb=any] > ')' >
21n/a >
22n/a |
23n/a power< any trailer< '.' 'throw' > trailer< '(' exc=any ')' > >
24n/a """
25n/a
26n/a def transform(self, node, results):
27n/a syms = self.syms
28n/a
29n/a exc = results["exc"].clone()
30n/a if exc.type is token.STRING:
31n/a self.cannot_convert(node, "Python 3 does not support string exceptions")
32n/a return
33n/a
34n/a # Leave "g.throw(E)" alone
35n/a val = results.get("val")
36n/a if val is None:
37n/a return
38n/a
39n/a val = val.clone()
40n/a if is_tuple(val):
41n/a args = [c.clone() for c in val.children[1:-1]]
42n/a else:
43n/a val.prefix = ""
44n/a args = [val]
45n/a
46n/a throw_args = results["args"]
47n/a
48n/a if "tb" in results:
49n/a tb = results["tb"].clone()
50n/a tb.prefix = ""
51n/a
52n/a e = Call(exc, args)
53n/a with_tb = Attr(e, Name('with_traceback')) + [ArgList([tb])]
54n/a throw_args.replace(pytree.Node(syms.power, with_tb))
55n/a else:
56n/a throw_args.replace(Call(exc, args))