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

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

#countcontent
1n/a# Copyright 2006 Google, Inc. All Rights Reserved.
2n/a# Licensed to PSF under a Contributor Agreement.
3n/a
4n/a"""Fixer for apply().
5n/a
6n/aThis converts apply(func, v, k) into (func)(*v, **k)."""
7n/a
8n/a# Local imports
9n/afrom .. import pytree
10n/afrom ..pgen2 import token
11n/afrom .. import fixer_base
12n/afrom ..fixer_util import Call, Comma, parenthesize
13n/a
14n/aclass FixApply(fixer_base.BaseFix):
15n/a BM_compatible = True
16n/a
17n/a PATTERN = """
18n/a power< 'apply'
19n/a trailer<
20n/a '('
21n/a arglist<
22n/a (not argument<NAME '=' any>) func=any ','
23n/a (not argument<NAME '=' any>) args=any [','
24n/a (not argument<NAME '=' any>) kwds=any] [',']
25n/a >
26n/a ')'
27n/a >
28n/a >
29n/a """
30n/a
31n/a def transform(self, node, results):
32n/a syms = self.syms
33n/a assert results
34n/a func = results["func"]
35n/a args = results["args"]
36n/a kwds = results.get("kwds")
37n/a # I feel like we should be able to express this logic in the
38n/a # PATTERN above but I don't know how to do it so...
39n/a if args:
40n/a if args.type == self.syms.star_expr:
41n/a return # Make no change.
42n/a if (args.type == self.syms.argument and
43n/a args.children[0].value == '**'):
44n/a return # Make no change.
45n/a if kwds and (kwds.type == self.syms.argument and
46n/a kwds.children[0].value == '**'):
47n/a return # Make no change.
48n/a prefix = node.prefix
49n/a func = func.clone()
50n/a if (func.type not in (token.NAME, syms.atom) and
51n/a (func.type != syms.power or
52n/a func.children[-2].type == token.DOUBLESTAR)):
53n/a # Need to parenthesize
54n/a func = parenthesize(func)
55n/a func.prefix = ""
56n/a args = args.clone()
57n/a args.prefix = ""
58n/a if kwds is not None:
59n/a kwds = kwds.clone()
60n/a kwds.prefix = ""
61n/a l_newargs = [pytree.Leaf(token.STAR, "*"), args]
62n/a if kwds is not None:
63n/a l_newargs.extend([Comma(),
64n/a pytree.Leaf(token.DOUBLESTAR, "**"),
65n/a kwds])
66n/a l_newargs[-2].prefix = " " # that's the ** token
67n/a # XXX Sometimes we could be cleverer, e.g. apply(f, (x, y) + t)
68n/a # can be translated into f(x, y, *t) instead of f(*(x, y) + t)
69n/a #new = pytree.Node(syms.power, (func, ArgList(l_newargs)))
70n/a return Call(func, l_newargs, prefix=prefix)