1 | n/a | # Copyright 2007 Google, Inc. All Rights Reserved. |
---|
2 | n/a | # Licensed to PSF under a Contributor Agreement. |
---|
3 | n/a | |
---|
4 | n/a | """Fixer that changes map(F, ...) into list(map(F, ...)) unless there |
---|
5 | n/a | exists a 'from future_builtins import map' statement in the top-level |
---|
6 | n/a | namespace. |
---|
7 | n/a | |
---|
8 | n/a | As a special case, map(None, X) is changed into list(X). (This is |
---|
9 | n/a | necessary because the semantics are changed in this case -- the new |
---|
10 | n/a | map(None, X) is equivalent to [(x,) for x in X].) |
---|
11 | n/a | |
---|
12 | n/a | We avoid the transformation (except for the special case mentioned |
---|
13 | n/a | above) if the map() call is directly contained in iter(<>), list(<>), |
---|
14 | n/a | tuple(<>), sorted(<>), ...join(<>), or for V in <>:. |
---|
15 | n/a | |
---|
16 | n/a | NOTE: This is still not correct if the original code was depending on |
---|
17 | n/a | map(F, X, Y, ...) to go on until the longest argument is exhausted, |
---|
18 | n/a | substituting None for missing values -- like zip(), it now stops as |
---|
19 | n/a | soon as the shortest argument is exhausted. |
---|
20 | n/a | """ |
---|
21 | n/a | |
---|
22 | n/a | # Local imports |
---|
23 | n/a | from ..pgen2 import token |
---|
24 | n/a | from .. import fixer_base |
---|
25 | n/a | from ..fixer_util import Name, Call, ListComp, in_special_context |
---|
26 | n/a | from ..pygram import python_symbols as syms |
---|
27 | n/a | |
---|
28 | n/a | class FixMap(fixer_base.ConditionalFix): |
---|
29 | n/a | BM_compatible = True |
---|
30 | n/a | |
---|
31 | n/a | PATTERN = """ |
---|
32 | n/a | map_none=power< |
---|
33 | n/a | 'map' |
---|
34 | n/a | trailer< '(' arglist< 'None' ',' arg=any [','] > ')' > |
---|
35 | n/a | > |
---|
36 | n/a | | |
---|
37 | n/a | map_lambda=power< |
---|
38 | n/a | 'map' |
---|
39 | n/a | trailer< |
---|
40 | n/a | '(' |
---|
41 | n/a | arglist< |
---|
42 | n/a | lambdef< 'lambda' |
---|
43 | n/a | (fp=NAME | vfpdef< '(' fp=NAME ')'> ) ':' xp=any |
---|
44 | n/a | > |
---|
45 | n/a | ',' |
---|
46 | n/a | it=any |
---|
47 | n/a | > |
---|
48 | n/a | ')' |
---|
49 | n/a | > |
---|
50 | n/a | > |
---|
51 | n/a | | |
---|
52 | n/a | power< |
---|
53 | n/a | 'map' trailer< '(' [arglist=any] ')' > |
---|
54 | n/a | > |
---|
55 | n/a | """ |
---|
56 | n/a | |
---|
57 | n/a | skip_on = 'future_builtins.map' |
---|
58 | n/a | |
---|
59 | n/a | def transform(self, node, results): |
---|
60 | n/a | if self.should_skip(node): |
---|
61 | n/a | return |
---|
62 | n/a | |
---|
63 | n/a | if node.parent.type == syms.simple_stmt: |
---|
64 | n/a | self.warning(node, "You should use a for loop here") |
---|
65 | n/a | new = node.clone() |
---|
66 | n/a | new.prefix = "" |
---|
67 | n/a | new = Call(Name("list"), [new]) |
---|
68 | n/a | elif "map_lambda" in results: |
---|
69 | n/a | new = ListComp(results["xp"].clone(), |
---|
70 | n/a | results["fp"].clone(), |
---|
71 | n/a | results["it"].clone()) |
---|
72 | n/a | else: |
---|
73 | n/a | if "map_none" in results: |
---|
74 | n/a | new = results["arg"].clone() |
---|
75 | n/a | else: |
---|
76 | n/a | if "arglist" in results: |
---|
77 | n/a | args = results["arglist"] |
---|
78 | n/a | if args.type == syms.arglist and \ |
---|
79 | n/a | args.children[0].type == token.NAME and \ |
---|
80 | n/a | args.children[0].value == "None": |
---|
81 | n/a | self.warning(node, "cannot convert map(None, ...) " |
---|
82 | n/a | "with multiple arguments because map() " |
---|
83 | n/a | "now truncates to the shortest sequence") |
---|
84 | n/a | return |
---|
85 | n/a | if in_special_context(node): |
---|
86 | n/a | return None |
---|
87 | n/a | new = node.clone() |
---|
88 | n/a | new.prefix = "" |
---|
89 | n/a | new = Call(Name("list"), [new]) |
---|
90 | n/a | new.prefix = node.prefix |
---|
91 | n/a | return new |
---|