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